/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/password-request.c

  • Committer: Teddy Hogeborn
  • Date: 2008-09-03 17:34:29 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080903173429-db2mjtddf7mgbx8z
* plugins.d/password-request.xml (OVERVIEW): Refer to
                                             password-prompt(8) by
                                             name.
  (SECURITY): Improved wording.  Add paragraph about insecurity of
              ping.
  (SEE ALSO): Add references to cryptsetup(8) and crypttab(5).
              Changed to be a <variablelist> and added text.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#define _LARGEFILE_SOURCE
33
33
#define _FILE_OFFSET_BITS 64
34
34
 
35
 
#include <stdio.h>
36
 
#include <assert.h>
37
 
#include <stdlib.h>
38
 
#include <time.h>
39
 
#include <net/if.h>             /* if_nametoindex */
40
 
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
41
 
                                   SIOCSIFFLAGS */
 
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
 
36
 
 
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
 
38
                                   stdout, ferror() */
 
39
#include <stdint.h>             /* uint16_t, uint32_t */
 
40
#include <stddef.h>             /* NULL, size_t, ssize_t */
 
41
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
 
42
                                   srand() */
 
43
#include <stdbool.h>            /* bool, true */
 
44
#include <string.h>             /* memset(), strcmp(), strlen(),
 
45
                                   strerror(), asprintf(), strcpy() */
 
46
#include <sys/ioctl.h>          /* ioctl */
 
47
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
 
48
                                   sockaddr_in6, PF_INET6,
 
49
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
 
50
                                   uid_t, gid_t */
 
51
#include <inttypes.h>           /* PRIu16 */
 
52
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
 
53
                                   struct in6_addr, inet_pton(),
 
54
                                   connect() */
 
55
#include <assert.h>             /* assert() */
 
56
#include <errno.h>              /* perror(), errno */
 
57
#include <time.h>               /* time() */
42
58
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
43
 
                                   SIOCSIFFLAGS */
 
59
                                   SIOCSIFFLAGS, if_indextoname(),
 
60
                                   if_nametoindex(), IF_NAMESIZE */
 
61
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
 
62
                                   getuid(), getgid(), setuid(),
 
63
                                   setgid() */
 
64
#include <netinet/in.h>
 
65
#include <arpa/inet.h>          /* inet_pton(), htons */
 
66
#include <iso646.h>             /* not, and */
 
67
#include <argp.h>               /* struct argp_option, error_t, struct
 
68
                                   argp_state, struct argp,
 
69
                                   argp_parse(), ARGP_KEY_ARG,
 
70
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
44
71
 
 
72
/* Avahi */
 
73
/* All Avahi types, constants and functions
 
74
 Avahi*, avahi_*,
 
75
 AVAHI_* */
45
76
#include <avahi-core/core.h>
46
77
#include <avahi-core/lookup.h>
47
78
#include <avahi-core/log.h>
49
80
#include <avahi-common/malloc.h>
50
81
#include <avahi-common/error.h>
51
82
 
52
 
/* Mandos client part */
53
 
#include <sys/types.h>          /* socket(), inet_pton() */
54
 
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
55
 
                                   struct in6_addr, inet_pton() */
56
 
#include <gnutls/gnutls.h>      /* All GnuTLS stuff */
57
 
#include <gnutls/openpgp.h>     /* GnuTLS with openpgp stuff */
58
 
 
59
 
#include <unistd.h>             /* close() */
60
 
#include <netinet/in.h>
61
 
#include <stdbool.h>            /* true */
62
 
#include <string.h>             /* memset */
63
 
#include <arpa/inet.h>          /* inet_pton() */
64
 
#include <iso646.h>             /* not */
65
 
#include <net/if.h>             /* IF_NAMESIZE */
 
83
/* GnuTLS */
 
84
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and
 
85
                                   functions:
 
86
                                   gnutls_*
 
87
                                   init_gnutls_session(),
 
88
                                   GNUTLS_* */
 
89
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
 
90
                                   GNUTLS_OPENPGP_FMT_BASE64 */
66
91
 
67
92
/* GPGME */
68
 
#include <errno.h>              /* perror() */
69
 
#include <gpgme.h>
70
 
 
71
 
/* getopt_long */
72
 
#include <getopt.h>
 
93
#include <gpgme.h>              /* All GPGME types, constants and
 
94
                                   functions:
 
95
                                   gpgme_*
 
96
                                   GPGME_PROTOCOL_OpenPGP,
 
97
                                   GPG_ERR_NO_* */
73
98
 
74
99
#define BUFFER_SIZE 256
75
100
 
 
101
bool debug = false;
76
102
static const char *keydir = "/conf/conf.d/mandos";
77
 
static const char *pubkeyfile = "pubkey.txt";
78
 
static const char *seckeyfile = "seckey.txt";
79
 
 
80
 
bool debug = false;
81
 
 
82
 
/* Used for passing in values through all the callback functions */
 
103
static const char mandos_protocol_version[] = "1";
 
104
const char *argp_program_version = "password-request 1.0";
 
105
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
106
 
 
107
/* Used for passing in values through the Avahi callback functions */
83
108
typedef struct {
84
109
  AvahiSimplePoll *simple_poll;
85
110
  AvahiServer *server;
86
111
  gnutls_certificate_credentials_t cred;
87
112
  unsigned int dh_bits;
 
113
  gnutls_dh_params_t dh_params;
88
114
  const char *priority;
89
115
} mandos_context;
90
116
 
 
117
/*
 
118
 * Make room in "buffer" for at least BUFFER_SIZE additional bytes.
 
119
 * "buffer_capacity" is how much is currently allocated,
 
120
 * "buffer_length" is how much is already used.
 
121
 */
 
122
size_t adjustbuffer(char **buffer, size_t buffer_length,
 
123
                  size_t buffer_capacity){
 
124
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
 
125
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
 
126
    if (buffer == NULL){
 
127
      return 0;
 
128
    }
 
129
    buffer_capacity += BUFFER_SIZE;
 
130
  }
 
131
  return buffer_capacity;
 
132
}
 
133
 
91
134
/* 
92
135
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
93
136
 * Returns -1 on error
171
214
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
172
215
            gpgme_strsource(rc), gpgme_strerror(rc));
173
216
    plaintext_length = -1;
 
217
    if (debug){
 
218
      gpgme_decrypt_result_t result;
 
219
      result = gpgme_op_decrypt_result(ctx);
 
220
      if (result == NULL){
 
221
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
222
      } else {
 
223
        fprintf(stderr, "Unsupported algorithm: %s\n",
 
224
                result->unsupported_algorithm);
 
225
        fprintf(stderr, "Wrong key usage: %u\n",
 
226
                result->wrong_key_usage);
 
227
        if(result->file_name != NULL){
 
228
          fprintf(stderr, "File name: %s\n", result->file_name);
 
229
        }
 
230
        gpgme_recipient_t recipient;
 
231
        recipient = result->recipients;
 
232
        if(recipient){
 
233
          while(recipient != NULL){
 
234
            fprintf(stderr, "Public key algorithm: %s\n",
 
235
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
236
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
237
            fprintf(stderr, "Secret key available: %s\n",
 
238
                    recipient->status == GPG_ERR_NO_SECKEY
 
239
                    ? "No" : "Yes");
 
240
            recipient = recipient->next;
 
241
          }
 
242
        }
 
243
      }
 
244
    }
174
245
    goto decrypt_end;
175
246
  }
176
247
  
178
249
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
179
250
  }
180
251
  
181
 
  if (debug){
182
 
    gpgme_decrypt_result_t result;
183
 
    result = gpgme_op_decrypt_result(ctx);
184
 
    if (result == NULL){
185
 
      fprintf(stderr, "gpgme_op_decrypt_result failed\n");
186
 
    } else {
187
 
      fprintf(stderr, "Unsupported algorithm: %s\n",
188
 
              result->unsupported_algorithm);
189
 
      fprintf(stderr, "Wrong key usage: %d\n",
190
 
              result->wrong_key_usage);
191
 
      if(result->file_name != NULL){
192
 
        fprintf(stderr, "File name: %s\n", result->file_name);
193
 
      }
194
 
      gpgme_recipient_t recipient;
195
 
      recipient = result->recipients;
196
 
      if(recipient){
197
 
        while(recipient != NULL){
198
 
          fprintf(stderr, "Public key algorithm: %s\n",
199
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
200
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
201
 
          fprintf(stderr, "Secret key available: %s\n",
202
 
                  recipient->status == GPG_ERR_NO_SECKEY
203
 
                  ? "No" : "Yes");
204
 
          recipient = recipient->next;
205
 
        }
206
 
      }
207
 
    }
208
 
  }
209
 
  
210
252
  /* Seek back to the beginning of the GPGME plaintext data buffer */
211
253
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
212
254
    perror("pgpme_data_seek");
216
258
  
217
259
  *plaintext = NULL;
218
260
  while(true){
219
 
    if (plaintext_length + BUFFER_SIZE
220
 
        > (ssize_t) plaintext_capacity){
221
 
      *plaintext = realloc(*plaintext, plaintext_capacity
222
 
                            + BUFFER_SIZE);
223
 
      if (*plaintext == NULL){
224
 
        perror("realloc");
 
261
    plaintext_capacity = adjustbuffer(plaintext,
 
262
                                      (size_t)plaintext_length,
 
263
                                      plaintext_capacity);
 
264
    if (plaintext_capacity == 0){
 
265
        perror("adjustbuffer");
225
266
        plaintext_length = -1;
226
267
        goto decrypt_end;
227
 
      }
228
 
      plaintext_capacity += BUFFER_SIZE;
229
268
    }
230
269
    
231
270
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
242
281
    }
243
282
    plaintext_length += ret;
244
283
  }
245
 
 
 
284
  
246
285
  if(debug){
247
286
    fprintf(stderr, "Decrypted password is: ");
248
287
    for(ssize_t i = 0; i < plaintext_length; i++){
262
301
}
263
302
 
264
303
static const char * safer_gnutls_strerror (int value) {
265
 
  const char *ret = gnutls_strerror (value);
 
304
  const char *ret = gnutls_strerror (value); /* Spurious warning */
266
305
  if (ret == NULL)
267
306
    ret = "(unknown)";
268
307
  return ret;
274
313
  fprintf(stderr, "GnuTLS: %s", string);
275
314
}
276
315
 
277
 
static int initgnutls(mandos_context *mc, gnutls_session_t *session,
278
 
                      gnutls_dh_params_t *dh_params){
 
316
static int init_gnutls_global(mandos_context *mc,
 
317
                              const char *pubkeyfilename,
 
318
                              const char *seckeyfilename){
279
319
  int ret;
280
320
  
281
321
  if(debug){
282
322
    fprintf(stderr, "Initializing GnuTLS\n");
283
323
  }
284
 
 
285
 
  if ((ret = gnutls_global_init ())
286
 
      != GNUTLS_E_SUCCESS) {
 
324
  
 
325
  ret = gnutls_global_init();
 
326
  if (ret != GNUTLS_E_SUCCESS) {
287
327
    fprintf (stderr, "GnuTLS global_init: %s\n",
288
328
             safer_gnutls_strerror(ret));
289
329
    return -1;
298
338
  }
299
339
  
300
340
  /* OpenPGP credentials */
301
 
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
302
 
      != GNUTLS_E_SUCCESS) {
303
 
    fprintf (stderr, "GnuTLS memory error: %s\n",
 
341
  gnutls_certificate_allocate_credentials(&mc->cred);
 
342
  if (ret != GNUTLS_E_SUCCESS){
 
343
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
 
344
                                                     warning */
304
345
             safer_gnutls_strerror(ret));
 
346
    gnutls_global_deinit ();
305
347
    return -1;
306
348
  }
307
349
  
308
350
  if(debug){
309
 
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
310
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
311
 
            seckeyfile);
 
351
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
 
352
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
 
353
            seckeyfilename);
312
354
  }
313
355
  
314
356
  ret = gnutls_certificate_set_openpgp_key_file
315
 
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
 
357
    (mc->cred, pubkeyfilename, seckeyfilename,
 
358
     GNUTLS_OPENPGP_FMT_BASE64);
316
359
  if (ret != GNUTLS_E_SUCCESS) {
317
360
    fprintf(stderr,
318
361
            "Error[%d] while reading the OpenPGP key pair ('%s',"
319
 
            " '%s')\n", ret, pubkeyfile, seckeyfile);
320
 
    fprintf(stdout, "The GnuTLS error is: %s\n",
 
362
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
363
    fprintf(stderr, "The GnuTLS error is: %s\n",
321
364
            safer_gnutls_strerror(ret));
322
 
    return -1;
 
365
    goto globalfail;
323
366
  }
324
367
  
325
368
  /* GnuTLS server initialization */
326
 
  ret = gnutls_dh_params_init(dh_params);
 
369
  ret = gnutls_dh_params_init(&mc->dh_params);
327
370
  if (ret != GNUTLS_E_SUCCESS) {
328
371
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
329
372
             " %s\n", safer_gnutls_strerror(ret));
330
 
    return -1;
 
373
    goto globalfail;
331
374
  }
332
 
  ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
 
375
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
333
376
  if (ret != GNUTLS_E_SUCCESS) {
334
377
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
335
378
             safer_gnutls_strerror(ret));
336
 
    return -1;
 
379
    goto globalfail;
337
380
  }
338
381
  
339
 
  gnutls_certificate_set_dh_params(mc->cred, *dh_params);
340
 
  
 
382
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
 
383
  
 
384
  return 0;
 
385
  
 
386
 globalfail:
 
387
  
 
388
  gnutls_certificate_free_credentials(mc->cred);
 
389
  gnutls_global_deinit();
 
390
  return -1;
 
391
}
 
392
 
 
393
static int init_gnutls_session(mandos_context *mc,
 
394
                               gnutls_session_t *session){
 
395
  int ret;
341
396
  /* GnuTLS session creation */
342
397
  ret = gnutls_init(session, GNUTLS_SERVER);
343
398
  if (ret != GNUTLS_E_SUCCESS){
352
407
      fprintf(stderr, "Syntax error at: %s\n", err);
353
408
      fprintf(stderr, "GnuTLS error: %s\n",
354
409
              safer_gnutls_strerror(ret));
 
410
      gnutls_deinit (*session);
355
411
      return -1;
356
412
    }
357
413
  }
361
417
  if (ret != GNUTLS_E_SUCCESS) {
362
418
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
363
419
            safer_gnutls_strerror(ret));
 
420
    gnutls_deinit (*session);
364
421
    return -1;
365
422
  }
366
423
  
382
439
                                      AvahiIfIndex if_index,
383
440
                                      mandos_context *mc){
384
441
  int ret, tcp_sd;
385
 
  struct sockaddr_in6 to;
 
442
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
386
443
  char *buffer = NULL;
387
444
  char *decrypted_buffer;
388
445
  size_t buffer_length = 0;
389
446
  size_t buffer_capacity = 0;
390
447
  ssize_t decrypted_buffer_size;
391
 
  size_t written = 0;
 
448
  size_t written;
392
449
  int retval = 0;
393
450
  char interface[IF_NAMESIZE];
394
451
  gnutls_session_t session;
395
 
  gnutls_dh_params_t dh_params;
396
452
  
397
 
  ret = initgnutls (mc, &session, &dh_params);
 
453
  ret = init_gnutls_session (mc, &session);
398
454
  if (ret != 0){
399
455
    return -1;
400
456
  }
401
457
  
402
458
  if(debug){
403
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
404
 
            ip, port);
 
459
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
 
460
            "\n", ip, port);
405
461
  }
406
462
  
407
463
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
409
465
    perror("socket");
410
466
    return -1;
411
467
  }
412
 
 
 
468
  
413
469
  if(debug){
414
470
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
415
471
      perror("if_indextoname");
418
474
    fprintf(stderr, "Binding to interface %s\n", interface);
419
475
  }
420
476
  
421
 
  memset(&to,0,sizeof(to));     /* Spurious warning */
422
 
  to.sin6_family = AF_INET6;
 
477
  memset(&to, 0, sizeof(to));
 
478
  to.in6.sin6_family = AF_INET6;
423
479
  /* It would be nice to have a way to detect if we were passed an
424
480
     IPv4 address here.   Now we assume an IPv6 address. */
425
 
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
 
481
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
426
482
  if (ret < 0 ){
427
483
    perror("inet_pton");
428
484
    return -1;
431
487
    fprintf(stderr, "Bad address: %s\n", ip);
432
488
    return -1;
433
489
  }
434
 
  to.sin6_port = htons(port);   /* Spurious warning */
 
490
  to.in6.sin6_port = htons(port); /* Spurious warning */
435
491
  
436
 
  to.sin6_scope_id = (uint32_t)if_index;
 
492
  to.in6.sin6_scope_id = (uint32_t)if_index;
437
493
  
438
494
  if(debug){
439
 
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
 
495
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
496
            port);
440
497
    char addrstr[INET6_ADDRSTRLEN] = "";
441
 
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
 
498
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
442
499
                 sizeof(addrstr)) == NULL){
443
500
      perror("inet_ntop");
444
501
    } else {
448
505
    }
449
506
  }
450
507
  
451
 
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
 
508
  ret = connect(tcp_sd, &to.in, sizeof(to));
452
509
  if (ret < 0){
453
510
    perror("connect");
454
511
    return -1;
455
512
  }
456
513
  
 
514
  const char *out = mandos_protocol_version;
 
515
  written = 0;
 
516
  while (true){
 
517
    size_t out_size = strlen(out);
 
518
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
 
519
                                   out_size - written));
 
520
    if (ret == -1){
 
521
      perror("write");
 
522
      retval = -1;
 
523
      goto mandos_end;
 
524
    }
 
525
    written += (size_t)ret;
 
526
    if(written < out_size){
 
527
      continue;
 
528
    } else {
 
529
      if (out == mandos_protocol_version){
 
530
        written = 0;
 
531
        out = "\r\n";
 
532
      } else {
 
533
        break;
 
534
      }
 
535
    }
 
536
  }
 
537
  
457
538
  if(debug){
458
539
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
459
540
  }
460
541
  
461
542
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
462
543
  
463
 
  ret = gnutls_handshake (session);
 
544
  do{
 
545
    ret = gnutls_handshake (session);
 
546
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
464
547
  
465
548
  if (ret != GNUTLS_E_SUCCESS){
466
549
    if(debug){
477
560
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
478
561
            ip);
479
562
  }
480
 
 
 
563
  
481
564
  while(true){
482
 
    if (buffer_length + BUFFER_SIZE > buffer_capacity){
483
 
      buffer = realloc(buffer, buffer_capacity + BUFFER_SIZE);
484
 
      if (buffer == NULL){
485
 
        perror("realloc");
486
 
        retval = -1;
487
 
        goto mandos_end;
488
 
      }
489
 
      buffer_capacity += BUFFER_SIZE;
 
565
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
 
566
                                   buffer_capacity);
 
567
    if (buffer_capacity == 0){
 
568
      perror("adjustbuffer");
 
569
      retval = -1;
 
570
      goto mandos_end;
490
571
    }
491
572
    
492
573
    ret = gnutls_record_recv(session, buffer+buffer_length,
500
581
      case GNUTLS_E_AGAIN:
501
582
        break;
502
583
      case GNUTLS_E_REHANDSHAKE:
503
 
        ret = gnutls_handshake (session);
 
584
        do{
 
585
          ret = gnutls_handshake (session);
 
586
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
504
587
        if (ret < 0){
505
588
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
506
589
          gnutls_perror (ret);
532
615
                                               &decrypted_buffer,
533
616
                                               keydir);
534
617
    if (decrypted_buffer_size >= 0){
 
618
      written = 0;
535
619
      while(written < (size_t) decrypted_buffer_size){
536
620
        ret = (int)fwrite (decrypted_buffer + written, 1,
537
621
                           (size_t)decrypted_buffer_size - written,
550
634
    } else {
551
635
      retval = -1;
552
636
    }
 
637
  } else {
 
638
    retval = -1;
553
639
  }
554
640
  
555
641
  /* Shutdown procedure */
558
644
  free(buffer);
559
645
  close(tcp_sd);
560
646
  gnutls_deinit (session);
561
 
  gnutls_certificate_free_credentials (mc->cred);
562
 
  gnutls_global_deinit ();
563
647
  return retval;
564
648
}
565
649
 
578
662
                             flags,
579
663
                             void* userdata) {
580
664
  mandos_context *mc = userdata;
581
 
  assert(r);                    /* Spurious warning */
 
665
  assert(r);
582
666
  
583
667
  /* Called whenever a service has been resolved successfully or
584
668
     timed out */
596
680
      char ip[AVAHI_ADDRESS_STR_MAX];
597
681
      avahi_address_snprint(ip, sizeof(ip), address);
598
682
      if(debug){
599
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
600
 
                " port %d\n", name, host_name, ip, interface, port);
 
683
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
684
                PRIu16 ") on port %d\n", name, host_name, ip,
 
685
                interface, port);
601
686
      }
602
687
      int ret = start_mandos_communication(ip, port, interface, mc);
603
688
      if (ret == 0){
604
 
        exit(EXIT_SUCCESS);
 
689
        avahi_simple_poll_quit(mc->simple_poll);
605
690
      }
606
691
    }
607
692
  }
619
704
                             flags,
620
705
                             void* userdata) {
621
706
  mandos_context *mc = userdata;
622
 
  assert(b);                    /* Spurious warning */
 
707
  assert(b);
623
708
  
624
709
  /* Called whenever a new services becomes available on the LAN or
625
710
     is removed from the LAN */
661
746
 
662
747
/* Combines file name and path and returns the malloced new
663
748
   string. some sane checks could/should be added */
664
 
static const char *combinepath(const char *first, const char *second){
665
 
  size_t f_len = strlen(first);
666
 
  size_t s_len = strlen(second);
667
 
  char *tmp = malloc(f_len + s_len + 2);
668
 
  if (tmp == NULL){
 
749
static char *combinepath(const char *first, const char *second){
 
750
  char *tmp;
 
751
  int ret = asprintf(&tmp, "%s/%s", first, second);
 
752
  if(ret < 0){
669
753
    return NULL;
670
754
  }
671
 
  if(f_len > 0){
672
 
    memcpy(tmp, first, f_len);  /* Spurious warning */
673
 
  }
674
 
  tmp[f_len] = '/';
675
 
  if(s_len > 0){
676
 
    memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
677
 
  }
678
 
  tmp[f_len + 1 + s_len] = '\0';
679
755
  return tmp;
680
756
}
681
757
 
688
764
    const char *interface = "eth0";
689
765
    struct ifreq network;
690
766
    int sd;
 
767
    uid_t uid;
 
768
    gid_t gid;
691
769
    char *connect_to = NULL;
692
770
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
 
771
    char *pubkeyfilename = NULL;
 
772
    char *seckeyfilename = NULL;
 
773
    const char *pubkeyname = "pubkey.txt";
 
774
    const char *seckeyname = "seckey.txt";
693
775
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
694
 
                          .dh_bits = 1024, .priority = "SECURE256"};
 
776
                          .dh_bits = 1024, .priority = "SECURE256"
 
777
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
778
    bool gnutls_initalized = false;
695
779
    
696
780
    {
697
 
      /* Temporary int to get the address of for getopt_long */
698
 
      int debug_int = debug ? 1 : 0;
699
 
      while (true){
700
 
        struct option long_options[] = {
701
 
          {"debug", no_argument, &debug_int, 1},
702
 
          {"connect", required_argument, NULL, 'c'},
703
 
          {"interface", required_argument, NULL, 'i'},
704
 
          {"keydir", required_argument, NULL, 'd'},
705
 
          {"seckey", required_argument, NULL, 's'},
706
 
          {"pubkey", required_argument, NULL, 'p'},
707
 
          {"dh-bits", required_argument, NULL, 'D'},
708
 
          {"priority", required_argument, NULL, 'P'},
709
 
          {0, 0, 0, 0} };
710
 
      
711
 
        int option_index = 0;
712
 
        ret = getopt_long (argc, argv, "i:", long_options,
713
 
                           &option_index);
714
 
      
715
 
        if (ret == -1){
716
 
          break;
717
 
        }
718
 
      
719
 
        switch(ret){
720
 
        case 0:
721
 
          break;
722
 
        case 'i':
723
 
          interface = optarg;
724
 
          break;
725
 
        case 'c':
726
 
          connect_to = optarg;
727
 
          break;
728
 
        case 'd':
729
 
          keydir = optarg;
730
 
          break;
731
 
        case 'p':
732
 
          pubkeyfile = optarg;
733
 
          break;
734
 
        case 's':
735
 
          seckeyfile = optarg;
736
 
          break;
737
 
        case 'D':
 
781
      struct argp_option options[] = {
 
782
        { .name = "debug", .key = 128,
 
783
          .doc = "Debug mode", .group = 3 },
 
784
        { .name = "connect", .key = 'c',
 
785
          .arg = "ADDRESS:PORT",
 
786
          .doc = "Connect directly to a specific Mandos server",
 
787
          .group = 1 },
 
788
        { .name = "interface", .key = 'i',
 
789
          .arg = "NAME",
 
790
          .doc = "Interface that will be used to search for Mandos"
 
791
          " servers",
 
792
          .group = 1 },
 
793
        { .name = "keydir", .key = 'd',
 
794
          .arg = "DIRECTORY",
 
795
          .doc = "Directory to read the OpenPGP key files from",
 
796
          .group = 1 },
 
797
        { .name = "seckey", .key = 's',
 
798
          .arg = "FILE",
 
799
          .doc = "OpenPGP secret key file base name",
 
800
          .group = 1 },
 
801
        { .name = "pubkey", .key = 'p',
 
802
          .arg = "FILE",
 
803
          .doc = "OpenPGP public key file base name",
 
804
          .group = 2 },
 
805
        { .name = "dh-bits", .key = 129,
 
806
          .arg = "BITS",
 
807
          .doc = "Bit length of the prime number used in the"
 
808
          " Diffie-Hellman key exchange",
 
809
          .group = 2 },
 
810
        { .name = "priority", .key = 130,
 
811
          .arg = "STRING",
 
812
          .doc = "GnuTLS priority string for the TLS handshake",
 
813
          .group = 1 },
 
814
        { .name = NULL }
 
815
      };
 
816
      
 
817
      error_t parse_opt (int key, char *arg,
 
818
                         struct argp_state *state) {
 
819
        /* Get the INPUT argument from `argp_parse', which we know is
 
820
           a pointer to our plugin list pointer. */
 
821
        switch (key) {
 
822
        case 128:               /* --debug */
 
823
          debug = true;
 
824
          break;
 
825
        case 'c':               /* --connect */
 
826
          connect_to = arg;
 
827
          break;
 
828
        case 'i':               /* --interface */
 
829
          interface = arg;
 
830
          break;
 
831
        case 'd':               /* --keydir */
 
832
          keydir = arg;
 
833
          break;
 
834
        case 's':               /* --seckey */
 
835
          seckeyname = arg;
 
836
          break;
 
837
        case 'p':               /* --pubkey */
 
838
          pubkeyname = arg;
 
839
          break;
 
840
        case 129:               /* --dh-bits */
738
841
          errno = 0;
739
 
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
 
842
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
740
843
          if (errno){
741
844
            perror("strtol");
742
845
            exit(EXIT_FAILURE);
743
846
          }
744
847
          break;
745
 
        case 'P':
746
 
          mc.priority = optarg;
747
 
          break;
748
 
        case '?':
 
848
        case 130:               /* --priority */
 
849
          mc.priority = arg;
 
850
          break;
 
851
        case ARGP_KEY_ARG:
 
852
          argp_usage (state);
 
853
        case ARGP_KEY_END:
 
854
          break;
749
855
        default:
750
 
          /* getopt_long() has already printed a message about the
751
 
             unrcognized option, so just exit. */
752
 
          exit(EXIT_FAILURE);
753
 
        }
754
 
      }
755
 
      /* Set the global debug flag from the temporary int */
756
 
      debug = debug_int ? true : false;
757
 
    }
758
 
    
759
 
    pubkeyfile = combinepath(keydir, pubkeyfile);
760
 
    if (pubkeyfile == NULL){
761
 
      perror("combinepath");
762
 
      exitcode = EXIT_FAILURE;
763
 
      goto end;
764
 
    }
765
 
    
766
 
    seckeyfile = combinepath(keydir, seckeyfile);
767
 
    if (seckeyfile == NULL){
768
 
      perror("combinepath");
769
 
      goto end;
 
856
          return ARGP_ERR_UNKNOWN;
 
857
        }
 
858
        return 0;
 
859
      }
 
860
      
 
861
      struct argp argp = { .options = options, .parser = parse_opt,
 
862
                           .args_doc = "",
 
863
                           .doc = "Mandos client -- Get and decrypt"
 
864
                           " passwords from a Mandos server" };
 
865
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
866
      if (ret == ARGP_ERR_UNKNOWN){
 
867
        fprintf(stderr, "Unknown error while parsing arguments\n");
 
868
        exitcode = EXIT_FAILURE;
 
869
        goto end;
 
870
      }
 
871
    }
 
872
    
 
873
    pubkeyfilename = combinepath(keydir, pubkeyname);
 
874
    if (pubkeyfilename == NULL){
 
875
      perror("combinepath");
 
876
      exitcode = EXIT_FAILURE;
 
877
      goto end;
 
878
    }
 
879
    
 
880
    seckeyfilename = combinepath(keydir, seckeyname);
 
881
    if (seckeyfilename == NULL){
 
882
      perror("combinepath");
 
883
      exitcode = EXIT_FAILURE;
 
884
      goto end;
 
885
    }
 
886
    
 
887
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
 
888
    if (ret == -1){
 
889
      fprintf(stderr, "init_gnutls_global failed\n");
 
890
      exitcode = EXIT_FAILURE;
 
891
      goto end;
 
892
    } else {
 
893
      gnutls_initalized = true;
 
894
    }
 
895
    
 
896
    /* If the interface is down, bring it up */
 
897
    {
 
898
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
899
      if(sd < 0) {
 
900
        perror("socket");
 
901
        exitcode = EXIT_FAILURE;
 
902
        goto end;
 
903
      }
 
904
      strcpy(network.ifr_name, interface);
 
905
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
906
      if(ret == -1){
 
907
        perror("ioctl SIOCGIFFLAGS");
 
908
        exitcode = EXIT_FAILURE;
 
909
        goto end;
 
910
      }
 
911
      if((network.ifr_flags & IFF_UP) == 0){
 
912
        network.ifr_flags |= IFF_UP;
 
913
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
914
        if(ret == -1){
 
915
          perror("ioctl SIOCSIFFLAGS");
 
916
          exitcode = EXIT_FAILURE;
 
917
          goto end;
 
918
        }
 
919
      }
 
920
      close(sd);
 
921
    }
 
922
    
 
923
    uid = getuid();
 
924
    gid = getgid();
 
925
    
 
926
    ret = setuid(uid);
 
927
    if (ret == -1){
 
928
      perror("setuid");
 
929
    }
 
930
    
 
931
    setgid(gid);
 
932
    if (ret == -1){
 
933
      perror("setgid");
770
934
    }
771
935
    
772
936
    if_index = (AvahiIfIndex) if_nametoindex(interface);
781
945
      char *address = strrchr(connect_to, ':');
782
946
      if(address == NULL){
783
947
        fprintf(stderr, "No colon in address\n");
784
 
        exit(EXIT_FAILURE);
 
948
        exitcode = EXIT_FAILURE;
 
949
        goto end;
785
950
      }
786
951
      errno = 0;
787
952
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
788
953
      if(errno){
789
954
        perror("Bad port number");
790
 
        exit(EXIT_FAILURE);
 
955
        exitcode = EXIT_FAILURE;
 
956
        goto end;
791
957
      }
792
958
      *address = '\0';
793
959
      address = connect_to;
794
960
      ret = start_mandos_communication(address, port, if_index, &mc);
795
961
      if(ret < 0){
796
 
        exit(EXIT_FAILURE);
 
962
        exitcode = EXIT_FAILURE;
797
963
      } else {
798
 
        exit(EXIT_SUCCESS);
799
 
      }
800
 
    }
801
 
    
802
 
    /* If the interface is down, bring it up */
803
 
    {
804
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
805
 
      if(sd < 0) {
806
 
        perror("socket");
807
 
        exitcode = EXIT_FAILURE;
808
 
        goto end;
809
 
      }
810
 
      strcpy(network.ifr_name, interface); /* Spurious warning */
811
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
812
 
      if(ret == -1){
813
 
        perror("ioctl SIOCGIFFLAGS");
814
 
        exitcode = EXIT_FAILURE;
815
 
        goto end;
816
 
      }
817
 
      if((network.ifr_flags & IFF_UP) == 0){
818
 
        network.ifr_flags |= IFF_UP;
819
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
820
 
        if(ret == -1){
821
 
          perror("ioctl SIOCSIFFLAGS");
822
 
          exitcode = EXIT_FAILURE;
823
 
          goto end;
824
 
        }
825
 
      }
826
 
      close(sd);
 
964
        exitcode = EXIT_SUCCESS;
 
965
      }
 
966
      goto end;
827
967
    }
828
968
    
829
969
    if (not debug){
841
981
        exitcode = EXIT_FAILURE;
842
982
        goto end;
843
983
    }
844
 
 
 
984
    
845
985
    {
846
986
      AvahiServerConfig config;
847
987
      /* Do not publish any local Zeroconf records */
850
990
      config.publish_addresses = 0;
851
991
      config.publish_workstation = 0;
852
992
      config.publish_domain = 0;
853
 
 
 
993
      
854
994
      /* Allocate a new server */
855
995
      mc.server = avahi_server_new(avahi_simple_poll_get
856
996
                                   (mc.simple_poll), &config, NULL,
857
997
                                   NULL, &error);
858
 
    
 
998
      
859
999
      /* Free the Avahi configuration data */
860
1000
      avahi_server_config_free(&config);
861
1001
    }
881
1021
    }
882
1022
    
883
1023
    /* Run the main loop */
884
 
 
 
1024
    
885
1025
    if (debug){
886
1026
      fprintf(stderr, "Starting Avahi loop search\n");
887
1027
    }
889
1029
    avahi_simple_poll_loop(mc.simple_poll);
890
1030
    
891
1031
 end:
892
 
 
 
1032
    
893
1033
    if (debug){
894
1034
      fprintf(stderr, "%s exiting\n", argv[0]);
895
1035
    }
900
1040
    
901
1041
    if (mc.server != NULL)
902
1042
        avahi_server_free(mc.server);
903
 
 
 
1043
    
904
1044
    if (mc.simple_poll != NULL)
905
1045
        avahi_simple_poll_free(mc.simple_poll);
906
 
    free(pubkeyfile);
907
 
    free(seckeyfile);
 
1046
    free(pubkeyfilename);
 
1047
    free(seckeyfilename);
 
1048
    
 
1049
    if (gnutls_initalized){
 
1050
      gnutls_certificate_free_credentials(mc.cred);
 
1051
      gnutls_global_deinit ();
 
1052
    }
908
1053
    
909
1054
    return exitcode;
910
1055
}