/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1
/*  -*- coding: utf-8 -*- */
2
/*
261 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Fix name in header.
3
 * Mandos-client - get and decrypt data from a Mandos server
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
4
 *
5
 * This program is partly derived from an example program for an Avahi
6
 * service browser, downloaded from
7
 * <http://avahi.org/browser/examples/core-browse-services.c>.  This
8
 * includes the following functions: "resolve_callback",
9
 * "browse_callback", and parts of "main".
10
 * 
28 by Teddy Hogeborn
* server.conf: New file.
11
 * Everything else is
466 by Teddy Hogeborn
Update copyright year to "2011" wherever appropriate.
12
 * Copyright © 2008-2011 Teddy Hogeborn
13
 * Copyright © 2008-2011 Björn Påhlsson
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
14
 * 
15
 * This program is free software: you can redistribute it and/or
16
 * modify it under the terms of the GNU General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 * 
20
 * This program is distributed in the hope that it will be useful, but
21
 * WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
 * General Public License for more details.
24
 * 
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program.  If not, see
27
 * <http://www.gnu.org/licenses/>.
28
 * 
505.1.2 by Teddy Hogeborn
Change "fukt.bsnet.se" to "recompile.se" throughout.
29
 * Contact the authors at <mandos@recompile.se>.
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
30
 */
31
28 by Teddy Hogeborn
* server.conf: New file.
32
/* Needed by GPGME, specifically gpgme_data_seek() */
317 by Teddy Hogeborn
Use "getconf" to get correct LFS compile and link flags.
33
#ifndef _LARGEFILE_SOURCE
13 by Björn Påhlsson
Added following support:
34
#define _LARGEFILE_SOURCE
317 by Teddy Hogeborn
Use "getconf" to get correct LFS compile and link flags.
35
#endif
36
#ifndef _FILE_OFFSET_BITS
13 by Björn Påhlsson
Added following support:
37
#define _FILE_OFFSET_BITS 64
317 by Teddy Hogeborn
Use "getconf" to get correct LFS compile and link flags.
38
#endif
13 by Björn Påhlsson
Added following support:
39
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
40
#define _GNU_SOURCE		/* TEMP_FAILURE_RETRY(), asprintf() */
24.1.10 by Björn Påhlsson
merge commit
41
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
42
#include <stdio.h>		/* fprintf(), stderr, fwrite(),
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
43
				   stdout, ferror(), remove() */
24.1.26 by Björn Påhlsson
tally count of used symbols
44
#include <stdint.h> 		/* uint16_t, uint32_t */
45
#include <stddef.h>		/* NULL, size_t, ssize_t */
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
46
#include <stdlib.h> 		/* free(), EXIT_SUCCESS, srand(),
47
				   strtof(), abort() */
304 by Teddy Hogeborn
Four new interrelated features:
48
#include <stdbool.h>		/* bool, false, true */
24.1.29 by Björn Påhlsson
Added more header file comments
49
#include <string.h>		/* memset(), strcmp(), strlen(),
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
50
				   strerror(), asprintf(), strcpy() */
304 by Teddy Hogeborn
Four new interrelated features:
51
#include <sys/ioctl.h>		/* ioctl */
24.1.26 by Björn Påhlsson
tally count of used symbols
52
#include <sys/types.h>		/* socket(), inet_pton(), sockaddr,
24.1.29 by Björn Påhlsson
Added more header file comments
53
				   sockaddr_in6, PF_INET6,
304 by Teddy Hogeborn
Four new interrelated features:
54
				   SOCK_STREAM, uid_t, gid_t, open(),
55
				   opendir(), DIR */
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
56
#include <sys/stat.h>		/* open(), S_ISREG */
24.1.26 by Björn Påhlsson
tally count of used symbols
57
#include <sys/socket.h>		/* socket(), struct sockaddr_in6,
304 by Teddy Hogeborn
Four new interrelated features:
58
				   inet_pton(), connect() */
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
59
#include <fcntl.h>		/* open() */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
60
#include <dirent.h>		/* opendir(), struct dirent, readdir()
61
				 */
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
62
#include <inttypes.h>		/* PRIu16, PRIdMAX, intmax_t,
63
				   strtoimax() */
24.1.29 by Björn Påhlsson
Added more header file comments
64
#include <assert.h>		/* assert() */
485 by Teddy Hogeborn
Merge from Björn.
65
#include <errno.h>		/* perror(), errno,
66
				   program_invocation_short_name */
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
67
#include <time.h>		/* nanosleep(), time(), sleep() */
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
68
#include <net/if.h>		/* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
24.1.26 by Björn Påhlsson
tally count of used symbols
69
				   SIOCSIFFLAGS, if_indextoname(),
70
				   if_nametoindex(), IF_NAMESIZE */
304 by Teddy Hogeborn
Four new interrelated features:
71
#include <netinet/in.h>		/* IN6_IS_ADDR_LINKLOCAL,
72
				   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
73
				*/
24.1.29 by Björn Påhlsson
Added more header file comments
74
#include <unistd.h>		/* close(), SEEK_SET, off_t, write(),
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
75
				   getuid(), getgid(), seteuid(),
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
76
				   setgid(), pause() */
484 by Teddy Hogeborn
* Makefile (plugins.d/mandos-client): Bug fix: Put $^ before all
77
#include <arpa/inet.h>		/* inet_pton(), htons, inet_ntop() */
304 by Teddy Hogeborn
Four new interrelated features:
78
#include <iso646.h>		/* not, or, and */
24.1.29 by Björn Påhlsson
Added more header file comments
79
#include <argp.h>		/* struct argp_option, error_t, struct
80
				   argp_state, struct argp,
81
				   argp_parse(), ARGP_KEY_ARG,
82
				   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
307 by Teddy Hogeborn
Merge from Björn:
83
#include <signal.h>		/* sigemptyset(), sigaddset(),
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
84
				   sigaction(), SIGTERM, sig_atomic_t,
85
				   raise() */
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
86
#include <sysexits.h>		/* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
87
				   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
88
#include <sys/wait.h>		/* waitpid(), WIFEXITED(),
89
				   WEXITSTATUS(), WTERMSIG() */
307 by Teddy Hogeborn
Merge from Björn:
90
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
91
#ifdef __linux__
24.1.124 by Björn Påhlsson
Added lower kernel loglevel to reduce clutter on system console.
92
#include <sys/klog.h> 		/* klogctl() */
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
93
#endif	/* __linux__ */
24.1.26 by Björn Påhlsson
tally count of used symbols
94
95
/* Avahi */
24.1.29 by Björn Påhlsson
Added more header file comments
96
/* All Avahi types, constants and functions
97
 Avahi*, avahi_*,
98
 AVAHI_* */
99
#include <avahi-core/core.h>
24.1.26 by Björn Påhlsson
tally count of used symbols
100
#include <avahi-core/lookup.h>
24.1.29 by Björn Påhlsson
Added more header file comments
101
#include <avahi-core/log.h>
24.1.26 by Björn Påhlsson
tally count of used symbols
102
#include <avahi-common/simple-watch.h>
103
#include <avahi-common/malloc.h>
104
#include <avahi-common/error.h>
105
106
/* GnuTLS */
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
107
#include <gnutls/gnutls.h>	/* All GnuTLS types, constants and
108
				   functions:
24.1.29 by Björn Påhlsson
Added more header file comments
109
				   gnutls_*
24.1.26 by Björn Påhlsson
tally count of used symbols
110
				   init_gnutls_session(),
24.1.29 by Björn Påhlsson
Added more header file comments
111
				   GNUTLS_* */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
112
#include <gnutls/openpgp.h>
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
113
			 /* gnutls_certificate_set_openpgp_key_file(),
114
			    GNUTLS_OPENPGP_FMT_BASE64 */
24.1.26 by Björn Påhlsson
tally count of used symbols
115
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
116
/* GPGME */
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
117
#include <gpgme.h> 		/* All GPGME types, constants and
118
				   functions:
24.1.29 by Björn Påhlsson
Added more header file comments
119
				   gpgme_*
24.1.26 by Björn Påhlsson
tally count of used symbols
120
				   GPGME_PROTOCOL_OpenPGP,
24.1.29 by Björn Påhlsson
Added more header file comments
121
				   GPG_ERR_NO_* */
13 by Björn Påhlsson
Added following support:
122
123
#define BUFFER_SIZE 256
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
124
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
125
#define PATHDIR "/conf/conf.d/mandos"
126
#define SECKEY "seckey.txt"
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
127
#define PUBKEY "pubkey.txt"
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
128
#define HOOKDIR "/lib/mandos/network-hooks.d"
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
129
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
130
bool debug = false;
43 by Teddy Hogeborn
* plugins.d/mandosclient.c: Cosmetic changes.
131
static const char mandos_protocol_version[] = "1";
217 by Teddy Hogeborn
* .bzrignore: Added "man" directory (created by "make install-html").
132
const char *argp_program_version = "mandos-client " VERSION;
505.1.2 by Teddy Hogeborn
Change "fukt.bsnet.se" to "recompile.se" throughout.
133
const char *argp_program_bug_address = "<mandos@recompile.se>";
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
134
static const char sys_class_net[] = "/sys/class/net";
135
char *connect_to = NULL;
505.3.4 by teddy at bsnet
* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
136
const char *hookdir = HOOKDIR;
24.1.10 by Björn Påhlsson
merge commit
137
485 by Teddy Hogeborn
Merge from Björn.
138
/* Doubly linked list that need to be circularly linked when used */
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
139
typedef struct server{
140
  const char *ip;
141
  uint16_t port;
142
  AvahiIfIndex if_index;
143
  int af;
144
  struct timespec last_seen;
145
  struct server *next;
146
  struct server *prev;
147
} server;
148
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
149
/* Used for passing in values through the Avahi callback functions */
13 by Björn Påhlsson
Added following support:
150
typedef struct {
24.1.9 by Björn Påhlsson
not working midwork...
151
  AvahiSimplePoll *simple_poll;
152
  AvahiServer *server;
13 by Björn Påhlsson
Added following support:
153
  gnutls_certificate_credentials_t cred;
24.1.9 by Björn Påhlsson
not working midwork...
154
  unsigned int dh_bits;
24.1.13 by Björn Påhlsson
mandosclient
155
  gnutls_dh_params_t dh_params;
24.1.9 by Björn Påhlsson
not working midwork...
156
  const char *priority;
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
157
  gpgme_ctx_t ctx;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
158
  server *current_server;
24.1.9 by Björn Påhlsson
not working midwork...
159
} mandos_context;
13 by Björn Påhlsson
Added following support:
160
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
161
/* global context so signal handler can reach it*/
24.1.135 by Björn Påhlsson
Earlier signal handling
162
mandos_context mc = { .simple_poll = NULL, .server = NULL,
163
		      .dh_bits = 1024, .priority = "SECURE256"
485 by Teddy Hogeborn
Merge from Björn.
164
		      ":!CTYPE-X.509:+CTYPE-OPENPGP",
165
		      .current_server = NULL };
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
166
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
167
sig_atomic_t quit_now = 0;
168
int signal_received = 0;
169
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
170
/* Function to use when printing errors */
171
void perror_plus(const char *print_text){
485 by Teddy Hogeborn
Merge from Björn.
172
  fprintf(stderr, "Mandos plugin %s: ",
173
	  program_invocation_short_name);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
174
  perror(print_text);
175
}
176
505.2.4 by Björn Påhlsson
New convinence error printer: fprintf_plus
177
int fprintf_plus(FILE *stream, const char *format, ...){
178
  va_list ap;
179
  va_start (ap, format);
180
  
181
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
182
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
183
}
184
43 by Teddy Hogeborn
* plugins.d/mandosclient.c: Cosmetic changes.
185
/*
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
186
 * Make additional room in "buffer" for at least BUFFER_SIZE more
187
 * bytes. "buffer_capacity" is how much is currently allocated,
188
 * "buffer_length" is how much is already used.
43 by Teddy Hogeborn
* plugins.d/mandosclient.c: Cosmetic changes.
189
 */
24.1.132 by Björn Påhlsson
Fixed a bug in fallback handling
190
size_t incbuffer(char **buffer, size_t buffer_length,
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
191
		 size_t buffer_capacity){
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
192
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
24.1.12 by Björn Påhlsson
merge +
193
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
194
    if(buffer == NULL){
24.1.10 by Björn Påhlsson
merge commit
195
      return 0;
196
    }
197
    buffer_capacity += BUFFER_SIZE;
198
  }
199
  return buffer_capacity;
200
}
201
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
202
/* Add server to set of servers to retry periodically */
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
203
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
204
	       int af){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
205
  int ret;
206
  server *new_server = malloc(sizeof(server));
207
  if(new_server == NULL){
208
    perror_plus("malloc");
209
    return -1;
210
  }
211
  *new_server = (server){ .ip = strdup(ip),
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
212
			  .port = port,
213
			  .if_index = if_index,
214
			  .af = af };
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
215
  if(new_server->ip == NULL){
216
    perror_plus("strdup");
485 by Teddy Hogeborn
Merge from Björn.
217
    return -1;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
218
  }
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
219
  /* Special case of first server */
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
220
  if (mc.current_server == NULL){
221
    new_server->next = new_server;
222
    new_server->prev = new_server;
223
    mc.current_server = new_server;
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
224
  /* Place the new server last in the list */
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
225
  } else {
226
    new_server->next = mc.current_server;
227
    new_server->prev = mc.current_server->prev;
228
    new_server->prev->next = new_server;
229
    mc.current_server->prev = new_server;
230
  }
231
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
232
  if(ret == -1){
233
    perror_plus("clock_gettime");
234
    return -1;
235
  }
236
  return 0;
237
}
238
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
239
/* 
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
240
 * Initialize GPGME.
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
241
 */
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
242
static bool init_gpgme(const char *seckey, const char *pubkey,
243
		       const char *tempdir){
13 by Björn Påhlsson
Added following support:
244
  gpgme_error_t rc;
245
  gpgme_engine_info_t engine_info;
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
246
  
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
247
  
248
  /*
288 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Use separate bool variable instead
249
   * Helper function to insert pub and seckey to the engine keyring.
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
250
   */
251
  bool import_key(const char *filename){
361 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gpgme): Move variable "ret" into the
252
    int ret;
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
253
    int fd;
254
    gpgme_data_t pgp_data;
255
    
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
256
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
257
    if(fd == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
258
      perror_plus("open");
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
259
      return false;
260
    }
261
    
262
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
263
    if(rc != GPG_ERR_NO_ERROR){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
264
      fprintf(stderr, "Mandos plugin mandos-client: "
265
	      "bad gpgme_data_new_from_fd: %s: %s\n",
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
266
	      gpgme_strsource(rc), gpgme_strerror(rc));
267
      return false;
268
    }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
269
    
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
270
    rc = gpgme_op_import(mc.ctx, pgp_data);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
271
    if(rc != GPG_ERR_NO_ERROR){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
272
      fprintf(stderr, "Mandos plugin mandos-client: "
273
	      "bad gpgme_op_import: %s: %s\n",
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
274
	      gpgme_strsource(rc), gpgme_strerror(rc));
275
      return false;
276
    }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
277
    
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
278
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
279
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
280
      perror_plus("close");
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
281
    }
282
    gpgme_data_release(pgp_data);
283
    return true;
284
  }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
285
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
286
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
287
    fprintf(stderr, "Mandos plugin mandos-client: "
288
	    "Initializing GPGME\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
289
  }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
290
  
13 by Björn Påhlsson
Added following support:
291
  /* Init GPGME */
292
  gpgme_check_version(NULL);
24.1.4 by Björn Påhlsson
Added optional parameters certdir, certkey and certfile that can be iven at start in the command line.
293
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
294
  if(rc != GPG_ERR_NO_ERROR){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
295
    fprintf(stderr, "Mandos plugin mandos-client: "
296
	    "bad gpgme_engine_check_version: %s: %s\n",
24.1.4 by Björn Påhlsson
Added optional parameters certdir, certkey and certfile that can be iven at start in the command line.
297
	    gpgme_strsource(rc), gpgme_strerror(rc));
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
298
    return false;
24.1.4 by Björn Påhlsson
Added optional parameters certdir, certkey and certfile that can be iven at start in the command line.
299
  }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
300
  
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
301
  /* Set GPGME home directory for the OpenPGP engine only */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
302
  rc = gpgme_get_engine_info(&engine_info);
303
  if(rc != GPG_ERR_NO_ERROR){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
304
    fprintf(stderr, "Mandos plugin mandos-client: "
305
	    "bad gpgme_get_engine_info: %s: %s\n",
13 by Björn Påhlsson
Added following support:
306
	    gpgme_strsource(rc), gpgme_strerror(rc));
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
307
    return false;
13 by Björn Påhlsson
Added following support:
308
  }
309
  while(engine_info != NULL){
310
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
311
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
312
			    engine_info->file_name, tempdir);
13 by Björn Påhlsson
Added following support:
313
      break;
314
    }
315
    engine_info = engine_info->next;
316
  }
317
  if(engine_info == NULL){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
318
    fprintf(stderr, "Mandos plugin mandos-client: "
319
	    "Could not set GPGME home dir to %s\n", tempdir);
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
320
    return false;
321
  }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
322
  
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
323
  /* Create new GPGME "context" */
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
324
  rc = gpgme_new(&(mc.ctx));
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
325
  if(rc != GPG_ERR_NO_ERROR){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
326
    fprintf(stderr, "Mandos plugin mandos-client: "
327
	    "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
328
	    gpgme_strerror(rc));
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
329
    return false;
330
  }
331
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
332
  if(not import_key(pubkey) or not import_key(seckey)){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
333
    return false;
334
  }
335
  
355 by Teddy Hogeborn
* mandos: White-space fixes only.
336
  return true;
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
337
}
338
339
/* 
340
 * Decrypt OpenPGP data.
341
 * Returns -1 on error
342
 */
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
343
static ssize_t pgp_packet_decrypt(const char *cryptotext,
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
344
				  size_t crypto_size,
345
				  char **plaintext){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
346
  gpgme_data_t dh_crypto, dh_plain;
347
  gpgme_error_t rc;
348
  ssize_t ret;
349
  size_t plaintext_capacity = 0;
350
  ssize_t plaintext_length = 0;
351
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
352
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
353
    fprintf(stderr, "Mandos plugin mandos-client: "
354
	    "Trying to decrypt OpenPGP data\n");
13 by Björn Påhlsson
Added following support:
355
  }
356
  
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
357
  /* Create new GPGME data buffer from memory cryptotext */
358
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
359
			       0);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
360
  if(rc != GPG_ERR_NO_ERROR){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
361
    fprintf(stderr, "Mandos plugin mandos-client: "
362
	    "bad gpgme_data_new_from_mem: %s: %s\n",
13 by Björn Påhlsson
Added following support:
363
	    gpgme_strsource(rc), gpgme_strerror(rc));
364
    return -1;
365
  }
366
  
367
  /* Create new empty GPGME data buffer for the plaintext */
368
  rc = gpgme_data_new(&dh_plain);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
369
  if(rc != GPG_ERR_NO_ERROR){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
370
    fprintf(stderr, "Mandos plugin mandos-client: "
371
	    "bad gpgme_data_new: %s: %s\n",
13 by Björn Påhlsson
Added following support:
372
	    gpgme_strsource(rc), gpgme_strerror(rc));
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
373
    gpgme_data_release(dh_crypto);
13 by Björn Påhlsson
Added following support:
374
    return -1;
375
  }
376
  
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
377
  /* Decrypt data from the cryptotext data buffer to the plaintext
378
     data buffer */
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
379
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
380
  if(rc != GPG_ERR_NO_ERROR){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
381
    fprintf(stderr, "Mandos plugin mandos-client: "
382
	    "bad gpgme_op_decrypt: %s: %s\n",
13 by Björn Påhlsson
Added following support:
383
	    gpgme_strsource(rc), gpgme_strerror(rc));
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
384
    plaintext_length = -1;
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
385
    if(debug){
99 by Teddy Hogeborn
* mandos (fingerprint): Bug fix: Check crtverify.value, not crtverify.
386
      gpgme_decrypt_result_t result;
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
387
      result = gpgme_op_decrypt_result(mc.ctx);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
388
      if(result == NULL){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
389
	fprintf(stderr, "Mandos plugin mandos-client: "
390
		"gpgme_op_decrypt_result failed\n");
99 by Teddy Hogeborn
* mandos (fingerprint): Bug fix: Check crtverify.value, not crtverify.
391
      } else {
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
392
	fprintf(stderr, "Mandos plugin mandos-client: "
393
		"Unsupported algorithm: %s\n",
99 by Teddy Hogeborn
* mandos (fingerprint): Bug fix: Check crtverify.value, not crtverify.
394
		result->unsupported_algorithm);
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
395
	fprintf(stderr, "Mandos plugin mandos-client: "
396
		"Wrong key usage: %u\n",
99 by Teddy Hogeborn
* mandos (fingerprint): Bug fix: Check crtverify.value, not crtverify.
397
		result->wrong_key_usage);
398
	if(result->file_name != NULL){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
399
	  fprintf(stderr, "Mandos plugin mandos-client: "
400
		  "File name: %s\n", result->file_name);
99 by Teddy Hogeborn
* mandos (fingerprint): Bug fix: Check crtverify.value, not crtverify.
401
	}
402
	gpgme_recipient_t recipient;
403
	recipient = result->recipients;
349 by Teddy Hogeborn
* plugins.d/mandos-client.c (pgp_packet_decrypt): Remove redundant
404
	while(recipient != NULL){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
405
	  fprintf(stderr, "Mandos plugin mandos-client: "
406
		  "Public key algorithm: %s\n",
349 by Teddy Hogeborn
* plugins.d/mandos-client.c (pgp_packet_decrypt): Remove redundant
407
		  gpgme_pubkey_algo_name(recipient->pubkey_algo));
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
408
	  fprintf(stderr, "Mandos plugin mandos-client: "
409
		  "Key ID: %s\n", recipient->keyid);
410
	  fprintf(stderr, "Mandos plugin mandos-client: "
411
		  "Secret key available: %s\n",
349 by Teddy Hogeborn
* plugins.d/mandos-client.c (pgp_packet_decrypt): Remove redundant
412
		  recipient->status == GPG_ERR_NO_SECKEY
413
		  ? "No" : "Yes");
414
	  recipient = recipient->next;
99 by Teddy Hogeborn
* mandos (fingerprint): Bug fix: Check crtverify.value, not crtverify.
415
	}
416
      }
417
    }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
418
    goto decrypt_end;
13 by Björn Påhlsson
Added following support:
419
  }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
420
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
421
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
422
    fprintf(stderr, "Mandos plugin mandos-client: "
423
	    "Decryption of OpenPGP data succeeded\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
424
  }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
425
  
13 by Björn Påhlsson
Added following support:
426
  /* Seek back to the beginning of the GPGME plaintext data buffer */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
427
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
428
    perror_plus("gpgme_data_seek");
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
429
    plaintext_length = -1;
430
    goto decrypt_end;
24.1.5 by Björn Påhlsson
plugbasedclient:
431
  }
432
  
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
433
  *plaintext = NULL;
13 by Björn Påhlsson
Added following support:
434
  while(true){
24.1.132 by Björn Påhlsson
Fixed a bug in fallback handling
435
    plaintext_capacity = incbuffer(plaintext,
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
436
				   (size_t)plaintext_length,
437
				   plaintext_capacity);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
438
    if(plaintext_capacity == 0){
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
439
      perror_plus("incbuffer");
440
      plaintext_length = -1;
441
      goto decrypt_end;
13 by Björn Påhlsson
Added following support:
442
    }
443
    
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
444
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
445
			  BUFFER_SIZE);
13 by Björn Påhlsson
Added following support:
446
    /* Print the data, if any */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
447
    if(ret == 0){
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
448
      /* EOF */
13 by Björn Påhlsson
Added following support:
449
      break;
450
    }
451
    if(ret < 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
452
      perror_plus("gpgme_data_read");
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
453
      plaintext_length = -1;
454
      goto decrypt_end;
13 by Björn Påhlsson
Added following support:
455
    }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
456
    plaintext_length += ret;
13 by Björn Påhlsson
Added following support:
457
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
458
  
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
459
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
460
    fprintf(stderr, "Mandos plugin mandos-client: "
461
	    "Decrypted password is: ");
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
462
    for(ssize_t i = 0; i < plaintext_length; i++){
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
463
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
464
    }
465
    fprintf(stderr, "\n");
466
  }
467
  
468
 decrypt_end:
469
  
470
  /* Delete the GPGME cryptotext data buffer */
471
  gpgme_data_release(dh_crypto);
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
472
  
473
  /* Delete the GPGME plaintext data buffer */
13 by Björn Påhlsson
Added following support:
474
  gpgme_data_release(dh_plain);
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
475
  return plaintext_length;
13 by Björn Påhlsson
Added following support:
476
}
477
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
478
static const char * safer_gnutls_strerror(int value){
267 by Teddy Hogeborn
* plugins.d/mandos-client.c: Only comment changes.
479
  const char *ret = gnutls_strerror(value); /* Spurious warning from
480
					       -Wunreachable-code */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
481
  if(ret == NULL)
13 by Björn Påhlsson
Added following support:
482
    ret = "(unknown)";
483
  return ret;
484
}
485
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
486
/* GnuTLS log function callback */
36 by Teddy Hogeborn
* TODO: Converted to org-mode style
487
static void debuggnutls(__attribute__((unused)) int level,
488
			const char* string){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
489
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
13 by Björn Påhlsson
Added following support:
490
}
491
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
492
static int init_gnutls_global(const char *pubkeyfilename,
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
493
			      const char *seckeyfilename){
13 by Björn Påhlsson
Added following support:
494
  int ret;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
495
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
496
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
497
    fprintf(stderr, "Mandos plugin mandos-client: "
498
	    "Initializing GnuTLS\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
499
  }
24.1.29 by Björn Påhlsson
Added more header file comments
500
  
501
  ret = gnutls_global_init();
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
502
  if(ret != GNUTLS_E_SUCCESS){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
503
    fprintf(stderr, "Mandos plugin mandos-client: "
504
	    "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
13 by Björn Påhlsson
Added following support:
505
    return -1;
506
  }
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
507
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
508
  if(debug){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
509
    /* "Use a log level over 10 to enable all debugging options."
510
     * - GnuTLS manual
511
     */
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
512
    gnutls_global_set_log_level(11);
513
    gnutls_global_set_log_function(debuggnutls);
514
  }
515
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
516
  /* OpenPGP credentials */
484 by Teddy Hogeborn
* Makefile (plugins.d/mandos-client): Bug fix: Put $^ before all
517
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
518
  if(ret != GNUTLS_E_SUCCESS){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
519
    fprintf(stderr, "Mandos plugin mandos-client: "
520
	    "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
521
    gnutls_global_deinit();
13 by Björn Påhlsson
Added following support:
522
    return -1;
523
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
524
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
525
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
526
    fprintf(stderr, "Mandos plugin mandos-client: "
527
	    "Attempting to use OpenPGP public key %s and"
147 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Improved wording
528
	    " secret key %s as GnuTLS credentials\n", pubkeyfilename,
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
529
	    seckeyfilename);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
530
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
531
  
13 by Björn Påhlsson
Added following support:
532
  ret = gnutls_certificate_set_openpgp_key_file
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
533
    (mc.cred, pubkeyfilename, seckeyfilename,
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
534
     GNUTLS_OPENPGP_FMT_BASE64);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
535
  if(ret != GNUTLS_E_SUCCESS){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
536
    fprintf(stderr,
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
537
	    "Mandos plugin mandos-client: "
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
538
	    "Error[%d] while reading the OpenPGP key pair ('%s',"
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
539
	    " '%s')\n", ret, pubkeyfilename, seckeyfilename);
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
540
    fprintf(stderr, "Mandos plugin mandos-client: "
541
	    "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
24.1.20 by Björn Påhlsson
mandosclient
542
    goto globalfail;
13 by Björn Påhlsson
Added following support:
543
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
544
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
545
  /* GnuTLS server initialization */
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
546
  ret = gnutls_dh_params_init(&mc.dh_params);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
547
  if(ret != GNUTLS_E_SUCCESS){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
548
    fprintf(stderr, "Mandos plugin mandos-client: "
549
	    "Error in GnuTLS DH parameter initialization:"
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
550
	    " %s\n", safer_gnutls_strerror(ret));
24.1.20 by Björn Påhlsson
mandosclient
551
    goto globalfail;
13 by Björn Påhlsson
Added following support:
552
  }
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
553
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
554
  if(ret != GNUTLS_E_SUCCESS){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
555
    fprintf(stderr, "Mandos plugin mandos-client: "
556
	    "Error in GnuTLS prime generation: %s\n",
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
557
	    safer_gnutls_strerror(ret));
24.1.20 by Björn Påhlsson
mandosclient
558
    goto globalfail;
13 by Björn Påhlsson
Added following support:
559
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
560
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
561
  gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
562
  
24.1.13 by Björn Påhlsson
mandosclient
563
  return 0;
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
564
  
24.1.20 by Björn Påhlsson
mandosclient
565
 globalfail:
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
566
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
567
  gnutls_certificate_free_credentials(mc.cred);
24.1.26 by Björn Påhlsson
tally count of used symbols
568
  gnutls_global_deinit();
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
569
  gnutls_dh_params_deinit(mc.dh_params);
24.1.20 by Björn Påhlsson
mandosclient
570
  return -1;
24.1.13 by Björn Påhlsson
mandosclient
571
}
572
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
573
static int init_gnutls_session(gnutls_session_t *session){
24.1.13 by Björn Påhlsson
mandosclient
574
  int ret;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
575
  /* GnuTLS session creation */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
576
  do {
577
    ret = gnutls_init(session, GNUTLS_SERVER);
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
578
    if(quit_now){
579
      return -1;
580
    }
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
581
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
582
  if(ret != GNUTLS_E_SUCCESS){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
583
    fprintf(stderr, "Mandos plugin mandos-client: "
584
	    "Error in GnuTLS session initialization: %s\n",
13 by Björn Påhlsson
Added following support:
585
	    safer_gnutls_strerror(ret));
586
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
587
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
588
  {
589
    const char *err;
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
590
    do {
591
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
592
      if(quit_now){
593
	gnutls_deinit(*session);
594
	return -1;
595
      }
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
596
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
597
    if(ret != GNUTLS_E_SUCCESS){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
598
      fprintf(stderr, "Mandos plugin mandos-client: "
599
	      "Syntax error at: %s\n", err);
600
      fprintf(stderr, "Mandos plugin mandos-client: "
601
	      "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
602
      gnutls_deinit(*session);
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
603
      return -1;
604
    }
13 by Björn Påhlsson
Added following support:
605
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
606
  
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
607
  do {
608
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
609
				 mc.cred);
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
610
    if(quit_now){
611
      gnutls_deinit(*session);
612
      return -1;
613
    }
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
614
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
615
  if(ret != GNUTLS_E_SUCCESS){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
616
    fprintf(stderr, "Mandos plugin mandos-client: "
617
	    "Error setting GnuTLS credentials: %s\n",
13 by Björn Påhlsson
Added following support:
618
	    safer_gnutls_strerror(ret));
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
619
    gnutls_deinit(*session);
13 by Björn Påhlsson
Added following support:
620
    return -1;
621
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
622
  
13 by Björn Påhlsson
Added following support:
623
  /* ignore client certificate if any. */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
624
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
13 by Björn Påhlsson
Added following support:
625
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
626
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
13 by Björn Påhlsson
Added following support:
627
  
628
  return 0;
629
}
630
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
631
/* Avahi log function callback */
36 by Teddy Hogeborn
* TODO: Converted to org-mode style
632
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
633
		      __attribute__((unused)) const char *txt){}
13 by Björn Påhlsson
Added following support:
634
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
635
/* Called when a Mandos server is found */
36 by Teddy Hogeborn
* TODO: Converted to org-mode style
636
static int start_mandos_communication(const char *ip, uint16_t port,
24.1.9 by Björn Påhlsson
not working midwork...
637
				      AvahiIfIndex if_index,
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
638
				      int af){
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
639
  int ret, tcp_sd = -1;
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
640
  ssize_t sret;
304 by Teddy Hogeborn
Four new interrelated features:
641
  union {
642
    struct sockaddr_in in;
643
    struct sockaddr_in6 in6;
644
  } to;
13 by Björn Påhlsson
Added following support:
645
  char *buffer = NULL;
372 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Bug fix:
646
  char *decrypted_buffer = NULL;
13 by Björn Påhlsson
Added following support:
647
  size_t buffer_length = 0;
648
  size_t buffer_capacity = 0;
24.1.10 by Björn Påhlsson
merge commit
649
  size_t written;
372 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Bug fix:
650
  int retval = -1;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
651
  gnutls_session_t session;
304 by Teddy Hogeborn
Four new interrelated features:
652
  int pf;			/* Protocol family */
653
  
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
654
  errno = 0;
655
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
656
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
657
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
658
    return -1;
659
  }
660
  
304 by Teddy Hogeborn
Four new interrelated features:
661
  switch(af){
662
  case AF_INET6:
663
    pf = PF_INET6;
664
    break;
665
  case AF_INET:
666
    pf = PF_INET;
667
    break;
668
  default:
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
669
    fprintf(stderr, "Mandos plugin mandos-client: "
670
	    "Bad address family: %d\n", af);
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
671
    errno = EINVAL;
304 by Teddy Hogeborn
Four new interrelated features:
672
    return -1;
673
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
674
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
675
  ret = init_gnutls_session(&session);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
676
  if(ret != 0){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
677
    return -1;
678
  }
679
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
680
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
681
    fprintf(stderr, "Mandos plugin mandos-client: "
682
	    "Setting up a TCP connection to %s, port %" PRIu16
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
683
	    "\n", ip, port);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
684
  }
13 by Björn Påhlsson
Added following support:
685
  
304 by Teddy Hogeborn
Four new interrelated features:
686
  tcp_sd = socket(pf, SOCK_STREAM, 0);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
687
  if(tcp_sd < 0){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
688
    int e = errno;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
689
    perror_plus("socket");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
690
    errno = e;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
691
    goto mandos_end;
692
  }
693
  
694
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
695
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
696
    goto mandos_end;
13 by Björn Påhlsson
Added following support:
697
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
698
  
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
699
  memset(&to, 0, sizeof(to));
304 by Teddy Hogeborn
Four new interrelated features:
700
  if(af == AF_INET6){
325 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): "sin6_family"
701
    to.in6.sin6_family = (sa_family_t)af;
304 by Teddy Hogeborn
Four new interrelated features:
702
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
703
  } else {			/* IPv4 */
704
    to.in.sin_family = (sa_family_t)af;
705
    ret = inet_pton(af, ip, &to.in.sin_addr);
706
  }
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
707
  if(ret < 0 ){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
708
    int e = errno;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
709
    perror_plus("inet_pton");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
710
    errno = e;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
711
    goto mandos_end;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
712
  }
13 by Björn Påhlsson
Added following support:
713
  if(ret == 0){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
714
    int e = errno;
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
715
    fprintf(stderr, "Mandos plugin mandos-client: "
716
	    "Bad address: %s\n", ip);
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
717
    errno = e;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
718
    goto mandos_end;
13 by Björn Påhlsson
Added following support:
719
  }
304 by Teddy Hogeborn
Four new interrelated features:
720
  if(af == AF_INET6){
721
    to.in6.sin6_port = htons(port); /* Spurious warnings from
722
				       -Wconversion and
723
				       -Wunreachable-code */
724
    
725
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
726
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
727
				-Wunreachable-code*/
304 by Teddy Hogeborn
Four new interrelated features:
728
      if(if_index == AVAHI_IF_UNSPEC){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
729
	fprintf(stderr, "Mandos plugin mandos-client: "
730
		"An IPv6 link-local address is incomplete"
304 by Teddy Hogeborn
Four new interrelated features:
731
		" without a network interface\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
732
	errno = EINVAL;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
733
	goto mandos_end;
304 by Teddy Hogeborn
Four new interrelated features:
734
      }
735
      /* Set the network interface number as scope */
736
      to.in6.sin6_scope_id = (uint32_t)if_index;
737
    }
738
  } else {
739
    to.in.sin_port = htons(port); /* Spurious warnings from
267 by Teddy Hogeborn
* plugins.d/mandos-client.c: Only comment changes.
740
				     -Wconversion and
741
				     -Wunreachable-code */
304 by Teddy Hogeborn
Four new interrelated features:
742
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
743
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
744
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
745
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
746
    goto mandos_end;
747
  }
748
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
749
  if(debug){
304 by Teddy Hogeborn
Four new interrelated features:
750
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
751
      char interface[IF_NAMESIZE];
752
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
753
	perror_plus("if_indextoname");
304 by Teddy Hogeborn
Four new interrelated features:
754
      } else {
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
755
	fprintf(stderr, "Mandos plugin mandos-client: "
756
		"Connection to: %s%%%s, port %" PRIu16 "\n",
304 by Teddy Hogeborn
Four new interrelated features:
757
		ip, interface, port);
758
      }
759
    } else {
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
760
      fprintf(stderr, "Mandos plugin mandos-client: "
761
	      "Connection to: %s, port %" PRIu16 "\n", ip, port);
304 by Teddy Hogeborn
Four new interrelated features:
762
    }
763
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
764
		 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
765
    const char *pcret;
766
    if(af == AF_INET6){
767
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
768
			sizeof(addrstr));
769
    } else {
770
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
771
			sizeof(addrstr));
772
    }
773
    if(pcret == NULL){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
774
      perror_plus("inet_ntop");
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
775
    } else {
776
      if(strcmp(addrstr, ip) != 0){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
777
	fprintf(stderr, "Mandos plugin mandos-client: "
778
		"Canonical address form: %s\n", addrstr);
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
779
      }
780
    }
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
781
  }
13 by Björn Påhlsson
Added following support:
782
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
783
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
784
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
785
    goto mandos_end;
786
  }
787
  
304 by Teddy Hogeborn
Four new interrelated features:
788
  if(af == AF_INET6){
789
    ret = connect(tcp_sd, &to.in6, sizeof(to));
790
  } else {
791
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
792
  }
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
793
  if(ret < 0){
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
794
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
795
      int e = errno;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
796
      perror_plus("connect");
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
797
      errno = e;
798
    }
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
799
    goto mandos_end;
800
  }
801
  
802
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
803
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
804
    goto mandos_end;
13 by Björn Påhlsson
Added following support:
805
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
806
  
24.1.12 by Björn Påhlsson
merge +
807
  const char *out = mandos_protocol_version;
24.1.10 by Björn Påhlsson
merge commit
808
  written = 0;
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
809
  while(true){
24.1.10 by Björn Påhlsson
merge commit
810
    size_t out_size = strlen(out);
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
811
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
812
					out_size - written));
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
813
    if(ret == -1){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
814
      int e = errno;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
815
      perror_plus("write");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
816
      errno = e;
24.1.12 by Björn Påhlsson
merge +
817
      goto mandos_end;
24.1.10 by Björn Påhlsson
merge commit
818
    }
24.1.12 by Björn Påhlsson
merge +
819
    written += (size_t)ret;
24.1.10 by Björn Påhlsson
merge commit
820
    if(written < out_size){
821
      continue;
822
    } else {
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
823
      if(out == mandos_protocol_version){
24.1.10 by Björn Påhlsson
merge commit
824
	written = 0;
825
	out = "\r\n";
826
      } else {
827
	break;
828
      }
829
    }
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
830
  
831
    if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
832
      errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
833
      goto mandos_end;
834
    }
24.1.10 by Björn Påhlsson
merge commit
835
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
836
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
837
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
838
    fprintf(stderr, "Mandos plugin mandos-client: "
839
	    "Establishing TLS session with %s\n", ip);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
840
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
841
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
842
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
843
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
844
    goto mandos_end;
845
  }
846
  
485 by Teddy Hogeborn
Merge from Björn.
847
  /* Spurious warning from -Wint-to-pointer-cast */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
848
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
849
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
850
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
851
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
852
    goto mandos_end;
853
  }
854
  
363 by Teddy Hogeborn
* plugin-runner.c: Minor stylistic changes.
855
  do {
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
856
    ret = gnutls_handshake(session);
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
857
    if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
858
      errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
859
      goto mandos_end;
860
    }
24.1.29 by Björn Påhlsson
Added more header file comments
861
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
13 by Björn Påhlsson
Added following support:
862
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
863
  if(ret != GNUTLS_E_SUCCESS){
25 by Teddy Hogeborn
* mandos-clients.conf ([DEFAULT]): New section.
864
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
865
      fprintf(stderr, "Mandos plugin mandos-client: "
866
	      "*** GnuTLS Handshake failed ***\n");
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
867
      gnutls_perror(ret);
25 by Teddy Hogeborn
* mandos-clients.conf ([DEFAULT]): New section.
868
    }
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
869
    errno = EPROTO;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
870
    goto mandos_end;
13 by Björn Påhlsson
Added following support:
871
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
872
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
873
  /* Read OpenPGP packet that contains the wanted password */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
874
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
875
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
876
    fprintf(stderr, "Mandos plugin mandos-client: "
877
	    "Retrieving OpenPGP encrypted password from %s\n", ip);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
878
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
879
  
13 by Björn Påhlsson
Added following support:
880
  while(true){
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
881
    
882
    if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
883
      errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
884
      goto mandos_end;
885
    }
886
    
24.1.132 by Björn Påhlsson
Fixed a bug in fallback handling
887
    buffer_capacity = incbuffer(&buffer, buffer_length,
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
888
				buffer_capacity);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
889
    if(buffer_capacity == 0){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
890
      int e = errno;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
891
      perror_plus("incbuffer");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
892
      errno = e;
24.1.12 by Björn Påhlsson
merge +
893
      goto mandos_end;
13 by Björn Påhlsson
Added following support:
894
    }
895
    
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
896
    if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
897
      errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
898
      goto mandos_end;
899
    }
900
    
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
901
    sret = gnutls_record_recv(session, buffer+buffer_length,
902
			      BUFFER_SIZE);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
903
    if(sret == 0){
13 by Björn Påhlsson
Added following support:
904
      break;
905
    }
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
906
    if(sret < 0){
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
907
      switch(sret){
13 by Björn Påhlsson
Added following support:
908
      case GNUTLS_E_INTERRUPTED:
909
      case GNUTLS_E_AGAIN:
910
	break;
911
      case GNUTLS_E_REHANDSHAKE:
363 by Teddy Hogeborn
* plugin-runner.c: Minor stylistic changes.
912
	do {
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
913
	  ret = gnutls_handshake(session);
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
914
	  
915
	  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
916
	    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
917
	    goto mandos_end;
918
	  }
24.1.29 by Björn Påhlsson
Added more header file comments
919
	} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
920
	if(ret < 0){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
921
	  fprintf(stderr, "Mandos plugin mandos-client: "
922
		  "*** GnuTLS Re-handshake failed ***\n");
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
923
	  gnutls_perror(ret);
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
924
	  errno = EPROTO;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
925
	  goto mandos_end;
13 by Björn Påhlsson
Added following support:
926
	}
927
	break;
928
      default:
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
929
	fprintf(stderr, "Mandos plugin mandos-client: "
930
		"Unknown error while reading data from"
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
931
		" encrypted session with Mandos server\n");
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
932
	gnutls_bye(session, GNUTLS_SHUT_RDWR);
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
933
	errno = EIO;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
934
	goto mandos_end;
13 by Björn Påhlsson
Added following support:
935
      }
936
    } else {
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
937
      buffer_length += (size_t) sret;
13 by Björn Påhlsson
Added following support:
938
    }
939
  }
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
940
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
941
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
942
    fprintf(stderr, "Mandos plugin mandos-client: "
943
	    "Closing TLS session\n");
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
944
  }
945
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
946
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
947
    errno = EINTR;
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
948
    goto mandos_end;
949
  }
950
  
951
  do {
952
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
953
    if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
954
      errno = EINTR;
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
955
      goto mandos_end;
956
    }
957
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
958
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
959
  if(buffer_length > 0){
362 by Teddy Hogeborn
* plugin-runner.c (getplugin, add_environment, main): Handle EINTR
960
    ssize_t decrypted_buffer_size;
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
961
    decrypted_buffer_size = pgp_packet_decrypt(buffer, buffer_length,
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
962
					       &decrypted_buffer);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
963
    if(decrypted_buffer_size >= 0){
361 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gpgme): Move variable "ret" into the
964
      
24.1.10 by Björn Påhlsson
merge commit
965
      written = 0;
28 by Teddy Hogeborn
* server.conf: New file.
966
      while(written < (size_t) decrypted_buffer_size){
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
967
	if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
968
	  errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
969
	  goto mandos_end;
970
	}
971
	
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
972
	ret = (int)fwrite(decrypted_buffer + written, 1,
973
			  (size_t)decrypted_buffer_size - written,
974
			  stdout);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
975
	if(ret == 0 and ferror(stdout)){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
976
	  int e = errno;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
977
	  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
978
	    fprintf(stderr, "Mandos plugin mandos-client: "
979
		    "Error writing encrypted data: %s\n",
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
980
		    strerror(errno));
981
	  }
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
982
	  errno = e;
372 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Bug fix:
983
	  goto mandos_end;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
984
	}
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
985
	written += (size_t)ret;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
986
      }
372 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Bug fix:
987
      retval = 0;
13 by Björn Påhlsson
Added following support:
988
    }
989
  }
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
990
  
991
  /* Shutdown procedure */
992
  
993
 mandos_end:
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
994
  {
995
    int e = errno;
996
    free(decrypted_buffer);
997
    free(buffer);
998
    if(tcp_sd >= 0){
999
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
1000
    }
1001
    if(ret == -1){
1002
      if(e == 0){
1003
	e = errno;
1004
      }
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1005
      perror_plus("close");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1006
    }
1007
    gnutls_deinit(session);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1008
    errno = e;
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1009
    if(quit_now){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1010
      errno = EINTR;
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1011
      retval = -1;
1012
    }
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
1013
  }
13 by Björn Påhlsson
Added following support:
1014
  return retval;
1015
}
1016
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
1017
static void resolve_callback(AvahiSServiceResolver *r,
1018
			     AvahiIfIndex interface,
304 by Teddy Hogeborn
Four new interrelated features:
1019
			     AvahiProtocol proto,
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
1020
			     AvahiResolverEvent event,
1021
			     const char *name,
1022
			     const char *type,
1023
			     const char *domain,
1024
			     const char *host_name,
1025
			     const AvahiAddress *address,
1026
			     uint16_t port,
1027
			     AVAHI_GCC_UNUSED AvahiStringList *txt,
1028
			     AVAHI_GCC_UNUSED AvahiLookupResultFlags
1029
			     flags,
307 by Teddy Hogeborn
Merge from Björn:
1030
			     AVAHI_GCC_UNUSED void* userdata){
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
1031
  assert(r);
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
1032
  
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1033
  /* Called whenever a service has been resolved successfully or
1034
     timed out */
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
1035
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1036
  if(quit_now){
1037
    return;
1038
  }
1039
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1040
  switch(event){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1041
  default:
1042
  case AVAHI_RESOLVER_FAILURE:
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1043
    fprintf(stderr, "Mandos plugin mandos-client: "
1044
	    "(Avahi Resolver) Failed to resolve service '%s'"
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1045
	    " of type '%s' in domain '%s': %s\n", name, type, domain,
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1046
	    avahi_strerror(avahi_server_errno(mc.server)));
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1047
    break;
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
1048
    
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1049
  case AVAHI_RESOLVER_FOUND:
1050
    {
1051
      char ip[AVAHI_ADDRESS_STR_MAX];
1052
      avahi_address_snprint(ip, sizeof(ip), address);
1053
      if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1054
	fprintf(stderr, "Mandos plugin mandos-client: "
1055
		"Mandos server \"%s\" found on %s (%s, %"
268 by Teddy Hogeborn
Fixes for sscanf usage:
1056
		PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1057
		ip, (intmax_t)interface, port);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1058
      }
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1059
      int ret = start_mandos_communication(ip, port, interface,
304 by Teddy Hogeborn
Four new interrelated features:
1060
					   avahi_proto_to_af(proto));
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
1061
      if(ret == 0){
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1062
	avahi_simple_poll_quit(mc.simple_poll);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1063
      } else {
1064
	ret = add_server(ip, port, interface,
1065
			 avahi_proto_to_af(proto));
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1066
      }
13 by Björn Påhlsson
Added following support:
1067
    }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1068
  }
1069
  avahi_s_service_resolver_free(r);
13 by Björn Påhlsson
Added following support:
1070
}
1071
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1072
static void browse_callback(AvahiSServiceBrowser *b,
1073
			    AvahiIfIndex interface,
1074
			    AvahiProtocol protocol,
1075
			    AvahiBrowserEvent event,
1076
			    const char *name,
1077
			    const char *type,
1078
			    const char *domain,
1079
			    AVAHI_GCC_UNUSED AvahiLookupResultFlags
1080
			    flags,
307 by Teddy Hogeborn
Merge from Björn:
1081
			    AVAHI_GCC_UNUSED void* userdata){
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
1082
  assert(b);
24.1.9 by Björn Påhlsson
not working midwork...
1083
  
1084
  /* Called whenever a new services becomes available on the LAN or
1085
     is removed from the LAN */
1086
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
1087
  if(quit_now){
1088
    return;
1089
  }
1090
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1091
  switch(event){
24.1.9 by Björn Påhlsson
not working midwork...
1092
  default:
1093
  case AVAHI_BROWSER_FAILURE:
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1094
    
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1095
    fprintf(stderr, "Mandos plugin mandos-client: "
1096
	    "(Avahi browser) %s\n",
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1097
	    avahi_strerror(avahi_server_errno(mc.server)));
1098
    avahi_simple_poll_quit(mc.simple_poll);
24.1.9 by Björn Påhlsson
not working midwork...
1099
    return;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1100
    
24.1.9 by Björn Påhlsson
not working midwork...
1101
  case AVAHI_BROWSER_NEW:
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1102
    /* We ignore the returned Avahi resolver object. In the callback
1103
       function we free it. If the Avahi server is terminated before
1104
       the callback function is called the Avahi server will free the
1105
       resolver for us. */
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1106
    
313 by Teddy Hogeborn
* plugins.d/mandos-client.c (browse_callback, main): Do not require
1107
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1108
				    name, type, domain, protocol, 0,
1109
				    resolve_callback, NULL) == NULL)
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1110
      fprintf(stderr, "Mandos plugin mandos-client: "
1111
	      "Avahi: Failed to resolve service '%s': %s\n",
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1112
	      name, avahi_strerror(avahi_server_errno(mc.server)));
24.1.9 by Björn Påhlsson
not working midwork...
1113
    break;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1114
    
24.1.9 by Björn Påhlsson
not working midwork...
1115
  case AVAHI_BROWSER_REMOVE:
1116
    break;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1117
    
24.1.9 by Björn Påhlsson
not working midwork...
1118
  case AVAHI_BROWSER_ALL_FOR_NOW:
1119
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1120
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1121
      fprintf(stderr, "Mandos plugin mandos-client: "
1122
	      "No Mandos server found, still searching...\n");
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1123
    }
24.1.9 by Björn Påhlsson
not working midwork...
1124
    break;
1125
  }
13 by Björn Påhlsson
Added following support:
1126
}
1127
485 by Teddy Hogeborn
Merge from Björn.
1128
/* Signal handler that stops main loop after SIGTERM */
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1129
static void handle_sigterm(int sig){
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1130
  if(quit_now){
1131
    return;
1132
  }
1133
  quit_now = 1;
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1134
  signal_received = sig;
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1135
  int old_errno = errno;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1136
  /* set main loop to exit */
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1137
  if(mc.simple_poll != NULL){
1138
    avahi_simple_poll_quit(mc.simple_poll);
1139
  }
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1140
  errno = old_errno;
1141
}
1142
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1143
bool get_flags(const char *ifname, struct ifreq *ifr){
505.2.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1144
  int ret;
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1145
  
505.2.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1146
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1147
  if(s < 0){
1148
    perror_plus("socket");
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1149
    return false;
505.2.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1150
  }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1151
  strcpy(ifr->ifr_name, ifname);
1152
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
505.2.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1153
  if(ret == -1){
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1154
    if(debug){
505.2.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1155
      perror_plus("ioctl SIOCGIFFLAGS");
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1156
    }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1157
    return false;
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1158
  }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1159
  return true;
1160
}
1161
1162
bool good_flags(const char *ifname, const struct ifreq *ifr){
1163
  
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1164
  /* Reject the loopback device */
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1165
  if(ifr->ifr_flags & IFF_LOOPBACK){
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1166
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1167
      fprintf(stderr, "Mandos plugin mandos-client: "
1168
	      "Rejecting loopback interface \"%s\"\n", ifname);
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1169
    }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1170
    return false;
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1171
  }
1172
  /* Accept point-to-point devices only if connect_to is specified */
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1173
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1174
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1175
      fprintf(stderr, "Mandos plugin mandos-client: "
1176
	      "Accepting point-to-point interface \"%s\"\n", ifname);
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1177
    }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1178
    return true;
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1179
  }
1180
  /* Otherwise, reject non-broadcast-capable devices */
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1181
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1182
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1183
      fprintf(stderr, "Mandos plugin mandos-client: "
1184
	      "Rejecting non-broadcast interface \"%s\"\n", ifname);
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1185
    }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1186
    return false;
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1187
  }
481 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Reject non-ARP
1188
  /* Reject non-ARP interfaces (including dummy interfaces) */
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1189
  if(ifr->ifr_flags & IFF_NOARP){
481 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Reject non-ARP
1190
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1191
      fprintf(stderr, "Mandos plugin mandos-client: "
1192
	      "Rejecting non-ARP interface \"%s\"\n", ifname);
481 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Reject non-ARP
1193
    }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1194
    return false;
481 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Reject non-ARP
1195
  }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1196
  
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1197
  /* Accept this device */
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1198
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1199
    fprintf(stderr, "Mandos plugin mandos-client: "
1200
	    "Interface \"%s\" is good\n", ifname);
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1201
  }
1202
  return true;
1203
}
1204
1205
/* 
1206
 * This function determines if a directory entry in /sys/class/net
1207
 * corresponds to an acceptable network device.
1208
 * (This function is passed to scandir(3) as a filter function.)
1209
 */
1210
int good_interface(const struct dirent *if_entry){
1211
  if(if_entry->d_name[0] == '.'){
1212
    return 0;
1213
  }
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1214
  
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1215
  struct ifreq ifr;
1216
  if(not get_flags(if_entry->d_name, &ifr)){
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1217
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1218
      fprintf(stderr, "Mandos plugin mandos-client: "
1219
	      "Failed to get flags for interface \"%s\"\n",
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1220
	      if_entry->d_name);
1221
    }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1222
    return 0;
1223
  }
1224
  
1225
  if(not good_flags(if_entry->d_name, &ifr)){
1226
    return 0;
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1227
  }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1228
  return 1;
1229
}
1230
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1231
/* 
1232
 * This function determines if a directory entry in /sys/class/net
1233
 * corresponds to an acceptable network device which is up.
1234
 * (This function is passed to scandir(3) as a filter function.)
1235
 */
1236
int up_interface(const struct dirent *if_entry){
1237
  if(if_entry->d_name[0] == '.'){
1238
    return 0;
1239
  }
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1240
  
1241
  struct ifreq ifr;
1242
  if(not get_flags(if_entry->d_name, &ifr)){
1243
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1244
      fprintf(stderr, "Mandos plugin mandos-client: "
1245
	      "Failed to get flags for interface \"%s\"\n",
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1246
	      if_entry->d_name);
1247
    }
1248
    return 0;
1249
  }
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1250
  
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1251
  /* Reject down interfaces */
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1252
  if(not (ifr.ifr_flags & IFF_UP)){
1253
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1254
      fprintf(stderr, "Mandos plugin mandos-client: "
1255
	      "Rejecting down interface \"%s\"\n",
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1256
	      if_entry->d_name);
1257
    }
1258
    return 0;
1259
  }
1260
  
1261
  /* Reject non-running interfaces */
1262
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1263
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1264
      fprintf(stderr, "Mandos plugin mandos-client: "
1265
	      "Rejecting non-running interface \"%s\"\n",
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1266
	      if_entry->d_name);
1267
    }
1268
    return 0;
1269
  }
1270
  
1271
  if(not good_flags(if_entry->d_name, &ifr)){
1272
    return 0;
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1273
  }
1274
  return 1;
1275
}
1276
24.1.172 by Björn Påhlsson
using scandir instead of readdir
1277
int notdotentries(const struct dirent *direntry){
1278
  /* Skip "." and ".." */
1279
  if(direntry->d_name[0] == '.'
1280
     and (direntry->d_name[1] == '\0'
1281
	  or (direntry->d_name[1] == '.'
1282
	      and direntry->d_name[2] == '\0'))){
1283
    return 0;
1284
  }
1285
  return 1;
1286
}
1287
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1288
/* Is this directory entry a runnable program? */
1289
int runnable_hook(const struct dirent *direntry){
1290
  int ret;
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
1291
  size_t sret;
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1292
  struct stat st;
1293
  
1294
  if((direntry->d_name)[0] == '\0'){
1295
    /* Empty name? */
1296
    return 0;
1297
  }
1298
  
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
1299
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1300
		"abcdefghijklmnopqrstuvwxyz"
1301
		"0123456789"
1302
		"_-");
1303
  if((direntry->d_name)[sret] != '\0'){
1304
    /* Contains non-allowed characters */
1305
    if(debug){
1306
      fprintf(stderr, "Mandos plugin mandos-client: "
1307
	      "Ignoring hook \"%s\" with bad name\n",
1308
	      direntry->d_name);
1309
    }
1310
    return 0;
1311
  }
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1312
  
505.3.4 by teddy at bsnet
* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
1313
  char *fullname = NULL;
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
1314
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
505.3.4 by teddy at bsnet
* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
1315
  if(ret < 0){
1316
    perror_plus("asprintf");
1317
    return 0;
1318
  }
1319
  
1320
  ret = stat(fullname, &st);
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1321
  if(ret == -1){
1322
    if(debug){
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
1323
      perror_plus("Could not stat hook");
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1324
    }
1325
    return 0;
1326
  }
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1327
  if(not (S_ISREG(st.st_mode))){
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1328
    /* Not a regular file */
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
1329
    if(debug){
1330
      fprintf(stderr, "Mandos plugin mandos-client: "
1331
	      "Ignoring hook \"%s\" - not a file\n",
1332
	      direntry->d_name);
1333
    }
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1334
    return 0;
1335
  }
1336
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1337
    /* Not executable */
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
1338
    if(debug){
1339
      fprintf(stderr, "Mandos plugin mandos-client: "
1340
	      "Ignoring hook \"%s\" - not executable\n",
1341
	      direntry->d_name);
1342
    }
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1343
    return 0;
1344
  }
1345
  return 1;
1346
}
1347
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1348
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1349
  int ret;
1350
  struct timespec now;
1351
  struct timespec waited_time;
1352
  intmax_t block_time;
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
1353
  
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1354
  while(true){
1355
    if(mc.current_server == NULL){
1356
      if (debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1357
	fprintf(stderr, "Mandos plugin mandos-client: "
485 by Teddy Hogeborn
Merge from Björn.
1358
		"Wait until first server is found. No timeout!\n");
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1359
      }
1360
      ret = avahi_simple_poll_iterate(s, -1);
1361
    } else {
1362
      if (debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1363
	fprintf(stderr, "Mandos plugin mandos-client: "
1364
		"Check current_server if we should run it,"
485 by Teddy Hogeborn
Merge from Björn.
1365
		" or wait\n");
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1366
      }
1367
      /* the current time */
1368
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1369
      if(ret == -1){
1370
	perror_plus("clock_gettime");
1371
	return -1;
1372
      }
1373
      /* Calculating in ms how long time between now and server
1374
	 who we visted longest time ago. Now - last seen.  */
485 by Teddy Hogeborn
Merge from Björn.
1375
      waited_time.tv_sec = (now.tv_sec
1376
			    - mc.current_server->last_seen.tv_sec);
1377
      waited_time.tv_nsec = (now.tv_nsec
1378
			     - mc.current_server->last_seen.tv_nsec);
1379
      /* total time is 10s/10,000ms.
1380
	 Converting to s from ms by dividing by 1,000,
1381
	 and ns to ms by dividing by 1,000,000. */
1382
      block_time = ((retry_interval
1383
		     - ((intmax_t)waited_time.tv_sec * 1000))
1384
		    - ((intmax_t)waited_time.tv_nsec / 1000000));
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
1385
      
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1386
      if (debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1387
	fprintf(stderr, "Mandos plugin mandos-client: "
1388
		"Blocking for %" PRIdMAX " ms\n", block_time);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1389
      }
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
1390
      
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1391
      if(block_time <= 0){
1392
	ret = start_mandos_communication(mc.current_server->ip,
485 by Teddy Hogeborn
Merge from Björn.
1393
					 mc.current_server->port,
1394
					 mc.current_server->if_index,
1395
					 mc.current_server->af);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1396
	if(ret == 0){
1397
	  avahi_simple_poll_quit(mc.simple_poll);
1398
	  return 0;
1399
	}
485 by Teddy Hogeborn
Merge from Björn.
1400
	ret = clock_gettime(CLOCK_MONOTONIC,
1401
			    &mc.current_server->last_seen);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1402
	if(ret == -1){
1403
	  perror_plus("clock_gettime");
1404
	  return -1;
1405
	}
1406
	mc.current_server = mc.current_server->next;
485 by Teddy Hogeborn
Merge from Björn.
1407
	block_time = 0; 	/* Call avahi to find new Mandos
1408
				   servers, but don't block */
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1409
      }
1410
      
1411
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1412
    }
1413
    if(ret != 0){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1414
      if (ret > 0 or errno != EINTR){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1415
	return (ret != 1) ? ret : 0;
1416
      }
1417
    }
1418
  }
1419
}
1420
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1421
int main(int argc, char *argv[]){
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1422
  AvahiSServiceBrowser *sb = NULL;
1423
  int error;
1424
  int ret;
1425
  intmax_t tmpmax;
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1426
  char *tmp;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1427
  int exitcode = EXIT_SUCCESS;
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1428
  const char *interface = "";
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1429
  struct ifreq network;
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
1430
  int sd = -1;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1431
  bool take_down_interface = false;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1432
  uid_t uid;
1433
  gid_t gid;
1434
  char tempdir[] = "/tmp/mandosXXXXXX";
1435
  bool tempdir_created = false;
1436
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1437
  const char *seckey = PATHDIR "/" SECKEY;
1438
  const char *pubkey = PATHDIR "/" PUBKEY;
1439
  
1440
  bool gnutls_initialized = false;
1441
  bool gpgme_initialized = false;
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1442
  float delay = 2.5f;
485 by Teddy Hogeborn
Merge from Björn.
1443
  double retry_interval = 10; /* 10s between trying a server and
1444
				 retrying the same server again */
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1445
  
369 by Teddy Hogeborn
* init.d-mandos (Required-Start, Required-Stop): Bug fix: Added
1446
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1447
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1448
  
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1449
  uid = getuid();
1450
  gid = getgid();
1451
  
1452
  /* Lower any group privileges we might have, just to be safe */
1453
  errno = 0;
1454
  ret = setgid(gid);
1455
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1456
    perror_plus("setgid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1457
  }
1458
  
1459
  /* Lower user privileges (temporarily) */
1460
  errno = 0;
1461
  ret = seteuid(uid);
1462
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1463
    perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1464
  }
1465
  
1466
  if(quit_now){
1467
    goto end;
1468
  }
1469
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1470
  {
1471
    struct argp_option options[] = {
1472
      { .name = "debug", .key = 128,
1473
	.doc = "Debug mode", .group = 3 },
1474
      { .name = "connect", .key = 'c',
1475
	.arg = "ADDRESS:PORT",
1476
	.doc = "Connect directly to a specific Mandos server",
1477
	.group = 1 },
1478
      { .name = "interface", .key = 'i',
1479
	.arg = "NAME",
304 by Teddy Hogeborn
Four new interrelated features:
1480
	.doc = "Network interface that will be used to search for"
1481
	" Mandos servers",
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1482
	.group = 1 },
1483
      { .name = "seckey", .key = 's',
1484
	.arg = "FILE",
1485
	.doc = "OpenPGP secret key file base name",
1486
	.group = 1 },
1487
      { .name = "pubkey", .key = 'p',
1488
	.arg = "FILE",
1489
	.doc = "OpenPGP public key file base name",
1490
	.group = 2 },
1491
      { .name = "dh-bits", .key = 129,
1492
	.arg = "BITS",
1493
	.doc = "Bit length of the prime number used in the"
1494
	" Diffie-Hellman key exchange",
1495
	.group = 2 },
1496
      { .name = "priority", .key = 130,
1497
	.arg = "STRING",
1498
	.doc = "GnuTLS priority string for the TLS handshake",
1499
	.group = 1 },
1500
      { .name = "delay", .key = 131,
1501
	.arg = "SECONDS",
1502
	.doc = "Maximum delay to wait for interface startup",
1503
	.group = 2 },
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1504
      { .name = "retry", .key = 132,
1505
	.arg = "SECONDS",
1506
	.doc = "Retry interval used when denied by the mandos server",
1507
	.group = 2 },
505.3.4 by teddy at bsnet
* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
1508
      { .name = "network-hook-dir", .key = 133,
1509
	.arg = "DIR",
1510
	.doc = "Directory where network hooks are located",
1511
	.group = 2 },
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1512
      /*
1513
       * These reproduce what we would get without ARGP_NO_HELP
1514
       */
1515
      { .name = "help", .key = '?',
1516
	.doc = "Give this help list", .group = -1 },
1517
      { .name = "usage", .key = -3,
1518
	.doc = "Give a short usage message", .group = -1 },
1519
      { .name = "version", .key = 'V',
1520
	.doc = "Print program version", .group = -1 },
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1521
      { .name = NULL }
1522
    };
1523
    
1524
    error_t parse_opt(int key, char *arg,
1525
		      struct argp_state *state){
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1526
      errno = 0;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1527
      switch(key){
1528
      case 128:			/* --debug */
1529
	debug = true;
1530
	break;
1531
      case 'c':			/* --connect */
1532
	connect_to = arg;
1533
	break;
1534
      case 'i':			/* --interface */
1535
	interface = arg;
1536
	break;
1537
      case 's':			/* --seckey */
1538
	seckey = arg;
1539
	break;
1540
      case 'p':			/* --pubkey */
1541
	pubkey = arg;
1542
	break;
1543
      case 129:			/* --dh-bits */
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1544
	errno = 0;
1545
	tmpmax = strtoimax(arg, &tmp, 10);
1546
	if(errno != 0 or tmp == arg or *tmp != '\0'
1547
	   or tmpmax != (typeof(mc.dh_bits))tmpmax){
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1548
	  argp_error(state, "Bad number of DH bits");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1549
	}
1550
	mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1551
	break;
1552
      case 130:			/* --priority */
1553
	mc.priority = arg;
1554
	break;
1555
      case 131:			/* --delay */
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1556
	errno = 0;
1557
	delay = strtof(arg, &tmp);
1558
	if(errno != 0 or tmp == arg or *tmp != '\0'){
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1559
	  argp_error(state, "Bad delay");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1560
	}
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1561
      case 132:			/* --retry */
1562
	errno = 0;
1563
	retry_interval = strtod(arg, &tmp);
1564
	if(errno != 0 or tmp == arg or *tmp != '\0'
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
1565
	   or (retry_interval * 1000) > INT_MAX
1566
	   or retry_interval < 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1567
	  argp_error(state, "Bad retry interval");
1568
	}
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1569
	break;
505.3.4 by teddy at bsnet
* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
1570
      case 133:			/* --network-hook-dir */
1571
	hookdir = arg;
1572
	break;
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1573
	/*
1574
	 * These reproduce what we would get without ARGP_NO_HELP
1575
	 */
1576
      case '?':			/* --help */
1577
	argp_state_help(state, state->out_stream,
1578
			(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1579
			& ~(unsigned int)ARGP_HELP_EXIT_OK);
1580
      case -3:			/* --usage */
1581
	argp_state_help(state, state->out_stream,
1582
			ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1583
      case 'V':			/* --version */
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1584
	fprintf(state->out_stream, "Mandos plugin mandos-client: ");
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1585
	fprintf(state->out_stream, "%s\n", argp_program_version);
1586
	exit(argp_err_exit_status);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1587
	break;
1588
      default:
1589
	return ARGP_ERR_UNKNOWN;
1590
      }
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1591
      return errno;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1592
    }
1593
    
1594
    struct argp argp = { .options = options, .parser = parse_opt,
1595
			 .args_doc = "",
1596
			 .doc = "Mandos client -- Get and decrypt"
1597
			 " passwords from a Mandos server" };
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1598
    ret = argp_parse(&argp, argc, argv,
1599
		     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1600
    switch(ret){
1601
    case 0:
1602
      break;
1603
    case ENOMEM:
1604
    default:
1605
      errno = ret;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1606
      perror_plus("argp_parse");
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1607
      exitcode = EX_OSERR;
1608
      goto end;
1609
    case EINVAL:
1610
      exitcode = EX_USAGE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1611
      goto end;
1612
    }
1613
  }
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1614
    
1615
  {
1616
    /* Work around Debian bug #633582:
1617
       <http://bugs.debian.org/633582> */
1618
    struct stat st;
1619
    
1620
    /* Re-raise priviliges */
1621
    errno = 0;
1622
    ret = seteuid(0);
1623
    if(ret == -1){
1624
      perror_plus("seteuid");
1625
    }
1626
    
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1627
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1628
      int seckey_fd = open(seckey, O_RDONLY);
1629
      if(seckey_fd == -1){
1630
	perror_plus("open");
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1631
      } else {
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1632
	ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1633
	if(ret == -1){
1634
	  perror_plus("fstat");
1635
	} else {
1636
	  if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1637
	    ret = fchown(seckey_fd, uid, gid);
1638
	    if(ret == -1){
1639
	      perror_plus("fchown");
1640
	    }
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1641
	  }
1642
	}
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1643
	TEMP_FAILURE_RETRY(close(seckey_fd));
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1644
      }
1645
    }
1646
    
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1647
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1648
      int pubkey_fd = open(pubkey, O_RDONLY);
1649
      if(pubkey_fd == -1){
1650
	perror_plus("open");
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1651
      } else {
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1652
	ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1653
	if(ret == -1){
1654
	  perror_plus("fstat");
1655
	} else {
1656
	  if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1657
	    ret = fchown(pubkey_fd, uid, gid);
1658
	    if(ret == -1){
1659
	      perror_plus("fchown");
1660
	    }
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1661
	  }
1662
	}
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1663
	TEMP_FAILURE_RETRY(close(pubkey_fd));
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1664
      }
1665
    }
1666
    
1667
    /* Lower privileges */
1668
    errno = 0;
1669
    ret = seteuid(uid);
1670
    if(ret == -1){
1671
      perror_plus("seteuid");
1672
    }
1673
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1674
  
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1675
  /* Find network hooks and run them */
1676
  {
1677
    struct dirent **direntries;
1678
    struct dirent *direntry;
505.3.4 by teddy at bsnet
* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
1679
    int numhooks = scandir(hookdir, &direntries, runnable_hook,
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1680
			   alphasort);
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1681
    if(numhooks == -1){
1682
      perror_plus("scandir");
1683
    } else {
1684
      int devnull = open("/dev/null", O_RDONLY);
1685
      for(int i = 0; i < numhooks; i++){
1686
	direntry = direntries[0];
1687
	char *fullname = NULL;
505.3.4 by teddy at bsnet
* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
1688
	ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1689
	if(ret < 0){
505.3.2 by teddy at bsnet
* plugins.d/mandos-client.c (main): Add "DELAY" environment variable.
1690
	  perror_plus("asprintf");
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1691
	  continue;
505.3.2 by teddy at bsnet
* plugins.d/mandos-client.c (main): Add "DELAY" environment variable.
1692
	}
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1693
	pid_t hook_pid = fork();
1694
	if(hook_pid == 0){
1695
	  /* Child */
1696
	  dup2(devnull, STDIN_FILENO);
1697
	  close(devnull);
1698
	  dup2(STDERR_FILENO, STDOUT_FILENO);
505.3.7 by teddy at bsnet
* initramfs-tools-hook: Install network hooks (and any required files)
1699
	  ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1700
	  if(ret == -1){
1701
	    perror_plus("setenv");
1702
	    exit(1);
1703
	  }
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1704
	  ret = setenv("DEVICE", interface, 1);
1705
	  if(ret == -1){
1706
	    perror_plus("setenv");
1707
	    exit(1);
1708
	  }
1709
	  ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1710
	  if(ret == -1){
1711
	    perror_plus("setenv");
1712
	    exit(1);
1713
	  }
1714
	  ret = setenv("MODE", "start", 1);
1715
	  if(ret == -1){
1716
	    perror_plus("setenv");
1717
	    exit(1);
1718
	  }
1719
	  char *delaystring;
1720
	  ret = asprintf(&delaystring, "%f", delay);
1721
	  if(ret == -1){
1722
	    perror_plus("asprintf");
1723
	    exit(1);
1724
	  }
1725
	  ret = setenv("DELAY", delaystring, 1);
1726
	  if(ret == -1){
1727
	    free(delaystring);
1728
	    perror_plus("setenv");
1729
	    exit(1);
1730
	  }
505.3.2 by teddy at bsnet
* plugins.d/mandos-client.c (main): Add "DELAY" environment variable.
1731
	  free(delaystring);
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1732
	  ret = execl(fullname, direntry->d_name, "start", NULL);
1733
	  perror_plus("execl");
1734
	} else {
1735
	  int status;
1736
	  if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1737
	    perror_plus("waitpid");
1738
	    free(fullname);
1739
	    continue;
1740
	  }
1741
	  if(WIFEXITED(status)){
1742
	    if(WEXITSTATUS(status) != 0){
1743
	      fprintf(stderr, "Mandos plugin mandos-client: "
1744
		      "Warning: network hook \"%s\" exited"
1745
		      " with status %d\n", direntry->d_name,
1746
		      WEXITSTATUS(status));
1747
	      free(fullname);
1748
	      continue;
1749
	    }
1750
	  } else if(WIFSIGNALED(status)){
1751
	    fprintf(stderr, "Mandos plugin mandos-client: "
1752
		    "Warning: network hook \"%s\" died by"
1753
		    " signal %d\n", direntry->d_name,
1754
		    WTERMSIG(status));
1755
	    free(fullname);
1756
	    continue;
1757
	  } else {
1758
	    fprintf(stderr, "Mandos plugin mandos-client: "
1759
		    "Warning: network hook \"%s\" crashed\n",
1760
		    direntry->d_name);
1761
	    free(fullname);
1762
	    continue;
1763
	  }
1764
	}
1765
	free(fullname);
1766
	if(quit_now){
1767
	  goto end;
1768
	}
1769
      }
1770
      close(devnull);
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1771
    }
1772
  }
1773
  
24.1.135 by Björn Påhlsson
Earlier signal handling
1774
  if(not debug){
1775
    avahi_set_log_function(empty_log);
1776
  }
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1777
  
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1778
  if(interface[0] == '\0'){
1779
    struct dirent **direntries;
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1780
    /* First look for interfaces that are up */
1781
    ret = scandir(sys_class_net, &direntries, up_interface,
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1782
		  alphasort);
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1783
    if(ret == 0){
1784
      /* No up interfaces, look for any good interfaces */
1785
      free(direntries);
1786
      ret = scandir(sys_class_net, &direntries, good_interface,
1787
		    alphasort);
1788
    }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1789
    if(ret >= 1){
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1790
      /* Pick the first interface returned */
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1791
      interface = strdup(direntries[0]->d_name);
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1792
      if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1793
	fprintf(stderr, "Mandos plugin mandos-client: "
1794
		"Using interface \"%s\"\n", interface);
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1795
      }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1796
      if(interface == NULL){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1797
	perror_plus("malloc");
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1798
	free(direntries);
1799
	exitcode = EXIT_FAILURE;
1800
	goto end;
1801
      }
1802
      free(direntries);
1803
    } else {
1804
      free(direntries);
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1805
      fprintf(stderr, "Mandos plugin mandos-client: "
1806
	      "Could not find a network interface\n");
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1807
      exitcode = EXIT_FAILURE;
1808
      goto end;
1809
    }
1810
  }
309 by Teddy Hogeborn
Merge from Björn:
1811
  
24.1.135 by Björn Påhlsson
Earlier signal handling
1812
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1813
     from the signal handler */
1814
  /* Initialize the pseudo-RNG for Avahi */
1815
  srand((unsigned int) time(NULL));
1816
  mc.simple_poll = avahi_simple_poll_new();
1817
  if(mc.simple_poll == NULL){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1818
    fprintf(stderr, "Mandos plugin mandos-client: "
1819
	    "Avahi: Failed to create simple poll object.\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1820
    exitcode = EX_UNAVAILABLE;
24.1.135 by Björn Påhlsson
Earlier signal handling
1821
    goto end;
1822
  }
309 by Teddy Hogeborn
Merge from Björn:
1823
  
24.1.135 by Björn Påhlsson
Earlier signal handling
1824
  sigemptyset(&sigterm_action.sa_mask);
309 by Teddy Hogeborn
Merge from Björn:
1825
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1826
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1827
    perror_plus("sigaddset");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1828
    exitcode = EX_OSERR;
309 by Teddy Hogeborn
Merge from Björn:
1829
    goto end;
1830
  }
1831
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1832
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1833
    perror_plus("sigaddset");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1834
    exitcode = EX_OSERR;
309 by Teddy Hogeborn
Merge from Björn:
1835
    goto end;
1836
  }
24.1.135 by Björn Påhlsson
Earlier signal handling
1837
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1838
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1839
    perror_plus("sigaddset");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1840
    exitcode = EX_OSERR;
24.1.135 by Björn Påhlsson
Earlier signal handling
1841
    goto end;
1842
  }
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1843
  /* Need to check if the handler is SIG_IGN before handling:
1844
     | [[info:libc:Initial Signal Actions]] |
1845
     | [[info:libc:Basic Signal Handling]]  |
1846
  */
1847
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1848
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1849
    perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1850
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1851
  }
1852
  if(old_sigterm_action.sa_handler != SIG_IGN){
1853
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1854
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1855
      perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1856
      exitcode = EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1857
      goto end;
1858
    }
1859
  }
1860
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1861
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1862
    perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1863
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1864
  }
1865
  if(old_sigterm_action.sa_handler != SIG_IGN){
1866
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1867
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1868
      perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1869
      exitcode = EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1870
      goto end;
1871
    }
1872
  }
1873
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1874
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1875
    perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1876
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1877
  }
1878
  if(old_sigterm_action.sa_handler != SIG_IGN){
1879
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1880
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1881
      perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1882
      exitcode = EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1883
      goto end;
1884
    }
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1885
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1886
  
1887
  /* If the interface is down, bring it up */
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1888
  if(strcmp(interface, "none") != 0){
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1889
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1890
    if(if_index == 0){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1891
      fprintf(stderr, "Mandos plugin mandos-client: "
1892
	      "No such interface: \"%s\"\n", interface);
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1893
      exitcode = EX_UNAVAILABLE;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1894
      goto end;
1895
    }
1896
    
1897
    if(quit_now){
1898
      goto end;
1899
    }
1900
    
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1901
    /* Re-raise priviliges */
1902
    errno = 0;
1903
    ret = seteuid(0);
1904
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1905
      perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1906
    }
1907
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1908
#ifdef __linux__
1909
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1910
       messages about the network interface to mess up the prompt */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1911
    ret = klogctl(8, NULL, 5);
304 by Teddy Hogeborn
Four new interrelated features:
1912
    bool restore_loglevel = true;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1913
    if(ret == -1){
304 by Teddy Hogeborn
Four new interrelated features:
1914
      restore_loglevel = false;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1915
      perror_plus("klogctl");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1916
    }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1917
#endif	/* __linux__ */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1918
    
1919
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1920
    if(sd < 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1921
      perror_plus("socket");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1922
      exitcode = EX_OSERR;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1923
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1924
      if(restore_loglevel){
1925
	ret = klogctl(7, NULL, 0);
1926
	if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1927
	  perror_plus("klogctl");
304 by Teddy Hogeborn
Four new interrelated features:
1928
	}
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1929
      }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1930
#endif	/* __linux__ */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1931
      /* Lower privileges */
1932
      errno = 0;
1933
      ret = seteuid(uid);
1934
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1935
	perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1936
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1937
      goto end;
1938
    }
1939
    strcpy(network.ifr_name, interface);
1940
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1941
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1942
      perror_plus("ioctl SIOCGIFFLAGS");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1943
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1944
      if(restore_loglevel){
1945
	ret = klogctl(7, NULL, 0);
1946
	if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1947
	  perror_plus("klogctl");
304 by Teddy Hogeborn
Four new interrelated features:
1948
	}
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1949
      }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1950
#endif	/* __linux__ */
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1951
      exitcode = EX_OSERR;
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1952
      /* Lower privileges */
1953
      errno = 0;
1954
      ret = seteuid(uid);
1955
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1956
	perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1957
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1958
      goto end;
1959
    }
1960
    if((network.ifr_flags & IFF_UP) == 0){
1961
      network.ifr_flags |= IFF_UP;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1962
      take_down_interface = true;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1963
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1964
      if(ret == -1){
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1965
	take_down_interface = false;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1966
	perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1967
	exitcode = EX_OSERR;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1968
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1969
	if(restore_loglevel){
1970
	  ret = klogctl(7, NULL, 0);
1971
	  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1972
	    perror_plus("klogctl");
304 by Teddy Hogeborn
Four new interrelated features:
1973
	  }
24.1.124 by Björn Påhlsson
Added lower kernel loglevel to reduce clutter on system console.
1974
	}
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1975
#endif	/* __linux__ */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1976
	/* Lower privileges */
1977
	errno = 0;
1978
	ret = seteuid(uid);
1979
	if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1980
	  perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1981
	}
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
1982
	goto end;
1983
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1984
    }
485 by Teddy Hogeborn
Merge from Björn.
1985
    /* Sleep checking until interface is running.
1986
       Check every 0.25s, up to total time of delay */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1987
    for(int i=0; i < delay * 4; i++){
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
1988
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1989
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1990
	perror_plus("ioctl SIOCGIFFLAGS");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1991
      } else if(network.ifr_flags & IFF_RUNNING){
1992
	break;
1993
      }
1994
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1995
      ret = nanosleep(&sleeptime, NULL);
1996
      if(ret == -1 and errno != EINTR){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1997
	perror_plus("nanosleep");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1998
      }
1999
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2000
    if(not take_down_interface){
2001
      /* We won't need the socket anymore */
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2002
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2003
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2004
	perror_plus("close");
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2005
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2006
    }
2007
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
2008
    if(restore_loglevel){
2009
      /* Restores kernel loglevel to default */
2010
      ret = klogctl(7, NULL, 0);
2011
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2012
	perror_plus("klogctl");
304 by Teddy Hogeborn
Four new interrelated features:
2013
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2014
    }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
2015
#endif	/* __linux__ */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
2016
    /* Lower privileges */
2017
    errno = 0;
2018
    if(take_down_interface){
2019
      /* Lower privileges */
2020
      ret = seteuid(uid);
2021
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2022
	perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
2023
      }
2024
    } else {
2025
      /* Lower privileges permanently */
2026
      ret = setuid(uid);
2027
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2028
	perror_plus("setuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
2029
      }
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2030
    }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2031
  }
2032
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2033
  if(quit_now){
2034
    goto end;
2035
  }
2036
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
2037
  ret = init_gnutls_global(pubkey, seckey);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2038
  if(ret == -1){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2039
    fprintf(stderr, "Mandos plugin mandos-client: "
2040
	    "init_gnutls_global failed\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2041
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2042
    goto end;
2043
  } else {
2044
    gnutls_initialized = true;
2045
  }
2046
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2047
  if(quit_now){
2048
    goto end;
2049
  }
2050
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2051
  if(mkdtemp(tempdir) == NULL){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2052
    perror_plus("mkdtemp");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2053
    goto end;
2054
  }
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2055
  tempdir_created = true;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2056
  
2057
  if(quit_now){
2058
    goto end;
2059
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2060
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
2061
  if(not init_gpgme(pubkey, seckey, tempdir)){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2062
    fprintf(stderr, "Mandos plugin mandos-client: "
2063
	    "init_gpgme failed\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2064
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2065
    goto end;
2066
  } else {
2067
    gpgme_initialized = true;
2068
  }
2069
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2070
  if(quit_now){
2071
    goto end;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2072
  }
2073
  
2074
  if(connect_to != NULL){
2075
    /* Connect directly, do not use Zeroconf */
2076
    /* (Mainly meant for debugging) */
2077
    char *address = strrchr(connect_to, ':');
2078
    if(address == NULL){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2079
      fprintf(stderr, "Mandos plugin mandos-client: "
2080
	      "No colon in address\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2081
      exitcode = EX_USAGE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2082
      goto end;
2083
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2084
    
2085
    if(quit_now){
2086
      goto end;
2087
    }
2088
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2089
    uint16_t port;
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
2090
    errno = 0;
2091
    tmpmax = strtoimax(address+1, &tmp, 10);
2092
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2093
       or tmpmax != (uint16_t)tmpmax){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2094
      fprintf(stderr, "Mandos plugin mandos-client: "
2095
	      "Bad port number\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2096
      exitcode = EX_USAGE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2097
      goto end;
2098
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2099
  
2100
    if(quit_now){
2101
      goto end;
2102
    }
2103
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2104
    port = (uint16_t)tmpmax;
2105
    *address = '\0';
304 by Teddy Hogeborn
Four new interrelated features:
2106
    /* Colon in address indicates IPv6 */
2107
    int af;
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
2108
    if(strchr(connect_to, ':') != NULL){
304 by Teddy Hogeborn
Four new interrelated features:
2109
      af = AF_INET6;
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
2110
      /* Accept [] around IPv6 address - see RFC 5952 */
2111
      if(connect_to[0] == '[' and address[-1] == ']')
2112
	{
2113
	  connect_to++;
2114
	  address[-1] = '\0';
2115
	}
304 by Teddy Hogeborn
Four new interrelated features:
2116
    } else {
2117
      af = AF_INET;
2118
    }
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
2119
    address = connect_to;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2120
    
2121
    if(quit_now){
2122
      goto end;
2123
    }
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
2124
    
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
2125
    while(not quit_now){
2126
      ret = start_mandos_communication(address, port, if_index, af);
2127
      if(quit_now or ret == 0){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2128
	break;
2129
      }
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
2130
      if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2131
	fprintf(stderr, "Mandos plugin mandos-client: "
2132
		"Retrying in %d seconds\n", (int)retry_interval);
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
2133
      }
2134
      sleep((int)retry_interval);
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
2135
    }
2136
    
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
2137
    if (not quit_now){
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2138
      exitcode = EXIT_SUCCESS;
2139
    }
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
2140
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2141
    goto end;
2142
  }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2143
  
2144
  if(quit_now){
2145
    goto end;
2146
  }
2147
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2148
  {
2149
    AvahiServerConfig config;
2150
    /* Do not publish any local Zeroconf records */
2151
    avahi_server_config_init(&config);
2152
    config.publish_hinfo = 0;
2153
    config.publish_addresses = 0;
2154
    config.publish_workstation = 0;
2155
    config.publish_domain = 0;
2156
    
2157
    /* Allocate a new server */
2158
    mc.server = avahi_server_new(avahi_simple_poll_get
2159
				 (mc.simple_poll), &config, NULL,
2160
				 NULL, &error);
2161
    
2162
    /* Free the Avahi configuration data */
2163
    avahi_server_config_free(&config);
2164
  }
2165
  
2166
  /* Check if creating the Avahi server object succeeded */
2167
  if(mc.server == NULL){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2168
    fprintf(stderr, "Mandos plugin mandos-client: "
2169
	    "Failed to create Avahi server: %s\n",
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2170
	    avahi_strerror(error));
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2171
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2172
    goto end;
2173
  }
2174
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2175
  if(quit_now){
2176
    goto end;
2177
  }
2178
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2179
  /* Create the Avahi service browser */
2180
  sb = avahi_s_service_browser_new(mc.server, if_index,
313 by Teddy Hogeborn
* plugins.d/mandos-client.c (browse_callback, main): Do not require
2181
				   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
2182
				   NULL, 0, browse_callback, NULL);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2183
  if(sb == NULL){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2184
    fprintf(stderr, "Mandos plugin mandos-client: "
2185
	    "Failed to create service browser: %s\n",
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2186
	    avahi_strerror(avahi_server_errno(mc.server)));
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2187
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2188
    goto end;
2189
  }
307 by Teddy Hogeborn
Merge from Björn:
2190
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2191
  if(quit_now){
2192
    goto end;
2193
  }
2194
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2195
  /* Run the main loop */
2196
  
2197
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2198
    fprintf(stderr, "Mandos plugin mandos-client: "
2199
	    "Starting Avahi loop search\n");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2200
  }
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2201
485 by Teddy Hogeborn
Merge from Björn.
2202
  ret = avahi_loop_with_timeout(mc.simple_poll,
2203
				(int)(retry_interval * 1000));
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2204
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2205
    fprintf(stderr, "Mandos plugin mandos-client: "
2206
	    "avahi_loop_with_timeout exited %s\n",
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2207
	    (ret == 0) ? "successfully" : "with error");
2208
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2209
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
2210
 end:
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2211
  
2212
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2213
    fprintf(stderr, "Mandos plugin mandos-client: "
2214
	    "%s exiting\n", argv[0]);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2215
  }
2216
  
2217
  /* Cleanup things */
2218
  if(sb != NULL)
2219
    avahi_s_service_browser_free(sb);
2220
  
2221
  if(mc.server != NULL)
2222
    avahi_server_free(mc.server);
2223
  
2224
  if(mc.simple_poll != NULL)
2225
    avahi_simple_poll_free(mc.simple_poll);
2226
  
2227
  if(gnutls_initialized){
2228
    gnutls_certificate_free_credentials(mc.cred);
2229
    gnutls_global_deinit();
2230
    gnutls_dh_params_deinit(mc.dh_params);
2231
  }
2232
  
2233
  if(gpgme_initialized){
2234
    gpgme_release(mc.ctx);
2235
  }
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2236
485 by Teddy Hogeborn
Merge from Björn.
2237
  /* Cleans up the circular linked list of Mandos servers the client
2238
     has seen */
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2239
  if(mc.current_server != NULL){
2240
    mc.current_server->prev->next = NULL;
2241
    while(mc.current_server != NULL){
2242
      server *next = mc.current_server->next;
2243
      free(mc.current_server);
2244
      mc.current_server = next;
2245
    }
2246
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2247
  
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
2248
  /* XXX run network hooks "stop" here  */
2249
  
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2250
  /* Take down the network interface */
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2251
  if(take_down_interface){
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2252
    /* Re-raise priviliges */
2253
    errno = 0;
2254
    ret = seteuid(0);
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2255
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2256
      perror_plus("seteuid");
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2257
    }
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2258
    if(geteuid() == 0){
2259
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2260
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2261
	perror_plus("ioctl SIOCGIFFLAGS");
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2262
      } else if(network.ifr_flags & IFF_UP){
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
2263
	network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2264
	ret = ioctl(sd, SIOCSIFFLAGS, &network);
2265
	if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2266
	  perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2267
	}
2268
      }
2269
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2270
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2271
	perror_plus("close");
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2272
      }
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
2273
      /* Lower privileges permanently */
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2274
      errno = 0;
2275
      ret = setuid(uid);
2276
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2277
	perror_plus("setuid");
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2278
      }
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2279
    }
2280
  }
2281
  
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2282
  /* Removes the GPGME temp directory and all files inside */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2283
  if(tempdir_created){
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2284
    struct dirent **direntries = NULL;
2285
    struct dirent *direntry = NULL;
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
2286
    int numentries = scandir(tempdir, &direntries, notdotentries,
2287
			     alphasort);
2288
    if (numentries > 0){
2289
      for(int i = 0; i < numentries; i++){
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2290
	direntry = direntries[i];
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2291
	char *fullname = NULL;
2292
	ret = asprintf(&fullname, "%s/%s", tempdir,
2293
		       direntry->d_name);
2294
	if(ret < 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2295
	  perror_plus("asprintf");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2296
	  continue;
2297
	}
2298
	ret = remove(fullname);
2299
	if(ret == -1){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2300
	  fprintf(stderr, "Mandos plugin mandos-client: "
2301
		  "remove(\"%s\"): %s\n", fullname, strerror(errno));
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2302
	}
2303
	free(fullname);
2304
      }
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2305
    }
2306
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
2307
    /* need to clean even if 0 because man page doesn't specify */
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2308
    free(direntries);
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
2309
    if (numentries == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2310
      perror_plus("scandir");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2311
    }
2312
    ret = rmdir(tempdir);
2313
    if(ret == -1 and errno != ENOENT){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2314
      perror_plus("rmdir");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2315
    }
2316
  }
2317
  
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
2318
  if(quit_now){
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
2319
    sigemptyset(&old_sigterm_action.sa_mask);
2320
    old_sigterm_action.sa_handler = SIG_DFL;
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
2321
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2322
					    &old_sigterm_action,
2323
					    NULL));
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
2324
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2325
      perror_plus("sigaction");
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
2326
    }
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
2327
    do {
2328
      ret = raise(signal_received);
2329
    } while(ret != 0 and errno == EINTR);
2330
    if(ret != 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2331
      perror_plus("raise");
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
2332
      abort();
2333
    }
2334
    TEMP_FAILURE_RETRY(pause());
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
2335
  }
2336
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2337
  return exitcode;
13 by Björn Påhlsson
Added following support:
2338
}