/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

* plugins.d/mandos-client.c (main): Add "DELAY" environment variable.
                                    Check return value from setenv().

Show diffs side-by-side

added added

removed removed

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