/mandos/release

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

« back to all changes in this revision

Viewing changes to plugins.d/password-request.c

  • Committer: Teddy Hogeborn
  • Date: 2008-09-03 17:11:32 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080903171132-usevhlf0gi9rxqc3
* plugins.d/password-request.c (init_gnutls_global): Improved wording
                                                     in debug message.
                                                     Bug fix: Make
                                                     debug message
                                                     print to stderr,
                                                     not stdout.

* plugins.d/password-request.xml (SECURITY): Add 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
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
 
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
36
36
 
37
 
#include <stdio.h>
38
 
#include <assert.h>
39
 
#include <stdlib.h>
40
 
#include <time.h>
41
 
#include <net/if.h>             /* if_nametoindex */
42
 
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
43
 
                                   SIOCSIFFLAGS */
 
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() */
44
58
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
45
 
                                   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 */
46
71
 
 
72
/* Avahi */
 
73
/* All Avahi types, constants and functions
 
74
 Avahi*, avahi_*,
 
75
 AVAHI_* */
47
76
#include <avahi-core/core.h>
48
77
#include <avahi-core/lookup.h>
49
78
#include <avahi-core/log.h>
51
80
#include <avahi-common/malloc.h>
52
81
#include <avahi-common/error.h>
53
82
 
54
 
/* Mandos client part */
55
 
#include <sys/types.h>          /* socket(), inet_pton() */
56
 
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
57
 
                                   struct in6_addr, inet_pton() */
58
 
#include <gnutls/gnutls.h>      /* All GnuTLS stuff */
59
 
#include <gnutls/openpgp.h>     /* GnuTLS with openpgp stuff */
60
 
 
61
 
#include <unistd.h>             /* close() */
62
 
#include <netinet/in.h>
63
 
#include <stdbool.h>            /* true */
64
 
#include <string.h>             /* memset */
65
 
#include <arpa/inet.h>          /* inet_pton() */
66
 
#include <iso646.h>             /* not */
67
 
#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 */
68
91
 
69
92
/* GPGME */
70
 
#include <errno.h>              /* perror() */
71
 
#include <gpgme.h>
72
 
 
73
 
/* getopt_long */
74
 
#include <getopt.h>
 
93
#include <gpgme.h>              /* All GPGME types, constants and
 
94
                                   functions:
 
95
                                   gpgme_*
 
96
                                   GPGME_PROTOCOL_OpenPGP,
 
97
                                   GPG_ERR_NO_* */
75
98
 
76
99
#define BUFFER_SIZE 256
77
100
 
 
101
bool debug = false;
78
102
static const char *keydir = "/conf/conf.d/mandos";
79
 
static const char *pubkeyfile = "pubkey.txt";
80
 
static const char *seckeyfile = "seckey.txt";
81
 
 
82
 
bool debug = false;
83
 
 
84
 
const char mandos_protocol_version[] = "1";
85
 
 
86
 
/* 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 */
87
108
typedef struct {
88
109
  AvahiSimplePoll *simple_poll;
89
110
  AvahiServer *server;
90
111
  gnutls_certificate_credentials_t cred;
91
112
  unsigned int dh_bits;
 
113
  gnutls_dh_params_t dh_params;
92
114
  const char *priority;
93
115
} mandos_context;
94
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
 */
95
122
size_t adjustbuffer(char **buffer, size_t buffer_length,
96
123
                  size_t buffer_capacity){
97
124
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
187
214
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
188
215
            gpgme_strsource(rc), gpgme_strerror(rc));
189
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
    }
190
245
    goto decrypt_end;
191
246
  }
192
247
  
194
249
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
195
250
  }
196
251
  
197
 
  if (debug){
198
 
    gpgme_decrypt_result_t result;
199
 
    result = gpgme_op_decrypt_result(ctx);
200
 
    if (result == NULL){
201
 
      fprintf(stderr, "gpgme_op_decrypt_result failed\n");
202
 
    } else {
203
 
      fprintf(stderr, "Unsupported algorithm: %s\n",
204
 
              result->unsupported_algorithm);
205
 
      fprintf(stderr, "Wrong key usage: %d\n",
206
 
              result->wrong_key_usage);
207
 
      if(result->file_name != NULL){
208
 
        fprintf(stderr, "File name: %s\n", result->file_name);
209
 
      }
210
 
      gpgme_recipient_t recipient;
211
 
      recipient = result->recipients;
212
 
      if(recipient){
213
 
        while(recipient != NULL){
214
 
          fprintf(stderr, "Public key algorithm: %s\n",
215
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
216
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
217
 
          fprintf(stderr, "Secret key available: %s\n",
218
 
                  recipient->status == GPG_ERR_NO_SECKEY
219
 
                  ? "No" : "Yes");
220
 
          recipient = recipient->next;
221
 
        }
222
 
      }
223
 
    }
224
 
  }
225
 
  
226
252
  /* Seek back to the beginning of the GPGME plaintext data buffer */
227
253
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
228
254
    perror("pgpme_data_seek");
232
258
  
233
259
  *plaintext = NULL;
234
260
  while(true){
235
 
    plaintext_capacity = adjustbuffer(plaintext, (size_t)plaintext_length,
 
261
    plaintext_capacity = adjustbuffer(plaintext,
 
262
                                      (size_t)plaintext_length,
236
263
                                      plaintext_capacity);
237
264
    if (plaintext_capacity == 0){
238
265
        perror("adjustbuffer");
254
281
    }
255
282
    plaintext_length += ret;
256
283
  }
257
 
 
 
284
  
258
285
  if(debug){
259
286
    fprintf(stderr, "Decrypted password is: ");
260
287
    for(ssize_t i = 0; i < plaintext_length; i++){
274
301
}
275
302
 
276
303
static const char * safer_gnutls_strerror (int value) {
277
 
  const char *ret = gnutls_strerror (value);
 
304
  const char *ret = gnutls_strerror (value); /* Spurious warning */
278
305
  if (ret == NULL)
279
306
    ret = "(unknown)";
280
307
  return ret;
286
313
  fprintf(stderr, "GnuTLS: %s", string);
287
314
}
288
315
 
289
 
static int initgnutls(mandos_context *mc, gnutls_session_t *session,
290
 
                      gnutls_dh_params_t *dh_params){
 
316
static int init_gnutls_global(mandos_context *mc,
 
317
                              const char *pubkeyfilename,
 
318
                              const char *seckeyfilename){
291
319
  int ret;
292
320
  
293
321
  if(debug){
294
322
    fprintf(stderr, "Initializing GnuTLS\n");
295
323
  }
296
 
 
297
 
  if ((ret = gnutls_global_init ())
298
 
      != GNUTLS_E_SUCCESS) {
 
324
  
 
325
  ret = gnutls_global_init();
 
326
  if (ret != GNUTLS_E_SUCCESS) {
299
327
    fprintf (stderr, "GnuTLS global_init: %s\n",
300
328
             safer_gnutls_strerror(ret));
301
329
    return -1;
310
338
  }
311
339
  
312
340
  /* OpenPGP credentials */
313
 
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
314
 
      != GNUTLS_E_SUCCESS) {
315
 
    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 */
316
345
             safer_gnutls_strerror(ret));
 
346
    gnutls_global_deinit ();
317
347
    return -1;
318
348
  }
319
349
  
320
350
  if(debug){
321
 
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
322
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
323
 
            seckeyfile);
 
351
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
 
352
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
 
353
            seckeyfilename);
324
354
  }
325
355
  
326
356
  ret = gnutls_certificate_set_openpgp_key_file
327
 
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
 
357
    (mc->cred, pubkeyfilename, seckeyfilename,
 
358
     GNUTLS_OPENPGP_FMT_BASE64);
328
359
  if (ret != GNUTLS_E_SUCCESS) {
329
360
    fprintf(stderr,
330
361
            "Error[%d] while reading the OpenPGP key pair ('%s',"
331
 
            " '%s')\n", ret, pubkeyfile, seckeyfile);
332
 
    fprintf(stdout, "The GnuTLS error is: %s\n",
 
362
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
363
    fprintf(stderr, "The GnuTLS error is: %s\n",
333
364
            safer_gnutls_strerror(ret));
334
 
    return -1;
 
365
    goto globalfail;
335
366
  }
336
367
  
337
368
  /* GnuTLS server initialization */
338
 
  ret = gnutls_dh_params_init(dh_params);
 
369
  ret = gnutls_dh_params_init(&mc->dh_params);
339
370
  if (ret != GNUTLS_E_SUCCESS) {
340
371
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
341
372
             " %s\n", safer_gnutls_strerror(ret));
342
 
    return -1;
 
373
    goto globalfail;
343
374
  }
344
 
  ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
 
375
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
345
376
  if (ret != GNUTLS_E_SUCCESS) {
346
377
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
347
378
             safer_gnutls_strerror(ret));
348
 
    return -1;
 
379
    goto globalfail;
349
380
  }
350
381
  
351
 
  gnutls_certificate_set_dh_params(mc->cred, *dh_params);
352
 
  
 
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;
353
396
  /* GnuTLS session creation */
354
397
  ret = gnutls_init(session, GNUTLS_SERVER);
355
398
  if (ret != GNUTLS_E_SUCCESS){
364
407
      fprintf(stderr, "Syntax error at: %s\n", err);
365
408
      fprintf(stderr, "GnuTLS error: %s\n",
366
409
              safer_gnutls_strerror(ret));
 
410
      gnutls_deinit (*session);
367
411
      return -1;
368
412
    }
369
413
  }
373
417
  if (ret != GNUTLS_E_SUCCESS) {
374
418
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
375
419
            safer_gnutls_strerror(ret));
 
420
    gnutls_deinit (*session);
376
421
    return -1;
377
422
  }
378
423
  
394
439
                                      AvahiIfIndex if_index,
395
440
                                      mandos_context *mc){
396
441
  int ret, tcp_sd;
397
 
  struct sockaddr_in6 to;
 
442
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
398
443
  char *buffer = NULL;
399
444
  char *decrypted_buffer;
400
445
  size_t buffer_length = 0;
404
449
  int retval = 0;
405
450
  char interface[IF_NAMESIZE];
406
451
  gnutls_session_t session;
407
 
  gnutls_dh_params_t dh_params;
408
452
  
409
 
  ret = initgnutls (mc, &session, &dh_params);
 
453
  ret = init_gnutls_session (mc, &session);
410
454
  if (ret != 0){
411
455
    return -1;
412
456
  }
413
457
  
414
458
  if(debug){
415
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
416
 
            ip, port);
 
459
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
 
460
            "\n", ip, port);
417
461
  }
418
462
  
419
463
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
421
465
    perror("socket");
422
466
    return -1;
423
467
  }
424
 
 
 
468
  
425
469
  if(debug){
426
470
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
427
471
      perror("if_indextoname");
430
474
    fprintf(stderr, "Binding to interface %s\n", interface);
431
475
  }
432
476
  
433
 
  memset(&to,0,sizeof(to));     /* Spurious warning */
434
 
  to.sin6_family = AF_INET6;
 
477
  memset(&to, 0, sizeof(to));
 
478
  to.in6.sin6_family = AF_INET6;
435
479
  /* It would be nice to have a way to detect if we were passed an
436
480
     IPv4 address here.   Now we assume an IPv6 address. */
437
 
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
 
481
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
438
482
  if (ret < 0 ){
439
483
    perror("inet_pton");
440
484
    return -1;
443
487
    fprintf(stderr, "Bad address: %s\n", ip);
444
488
    return -1;
445
489
  }
446
 
  to.sin6_port = htons(port);   /* Spurious warning */
 
490
  to.in6.sin6_port = htons(port); /* Spurious warning */
447
491
  
448
 
  to.sin6_scope_id = (uint32_t)if_index;
 
492
  to.in6.sin6_scope_id = (uint32_t)if_index;
449
493
  
450
494
  if(debug){
451
 
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
 
495
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
496
            port);
452
497
    char addrstr[INET6_ADDRSTRLEN] = "";
453
 
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
 
498
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
454
499
                 sizeof(addrstr)) == NULL){
455
500
      perror("inet_ntop");
456
501
    } else {
460
505
    }
461
506
  }
462
507
  
463
 
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
 
508
  ret = connect(tcp_sd, &to.in, sizeof(to));
464
509
  if (ret < 0){
465
510
    perror("connect");
466
511
    return -1;
467
512
  }
468
 
 
 
513
  
469
514
  const char *out = mandos_protocol_version;
470
515
  written = 0;
471
516
  while (true){
489
534
      }
490
535
    }
491
536
  }
492
 
 
 
537
  
493
538
  if(debug){
494
539
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
495
540
  }
496
541
  
497
542
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
498
543
  
499
 
  ret = gnutls_handshake (session);
 
544
  do{
 
545
    ret = gnutls_handshake (session);
 
546
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
500
547
  
501
548
  if (ret != GNUTLS_E_SUCCESS){
502
549
    if(debug){
513
560
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
514
561
            ip);
515
562
  }
516
 
 
 
563
  
517
564
  while(true){
518
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length, buffer_capacity);
 
565
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
 
566
                                   buffer_capacity);
519
567
    if (buffer_capacity == 0){
520
568
      perror("adjustbuffer");
521
569
      retval = -1;
533
581
      case GNUTLS_E_AGAIN:
534
582
        break;
535
583
      case GNUTLS_E_REHANDSHAKE:
536
 
        ret = gnutls_handshake (session);
 
584
        do{
 
585
          ret = gnutls_handshake (session);
 
586
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
537
587
        if (ret < 0){
538
588
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
539
589
          gnutls_perror (ret);
584
634
    } else {
585
635
      retval = -1;
586
636
    }
 
637
  } else {
 
638
    retval = -1;
587
639
  }
588
640
  
589
641
  /* Shutdown procedure */
592
644
  free(buffer);
593
645
  close(tcp_sd);
594
646
  gnutls_deinit (session);
595
 
  gnutls_certificate_free_credentials (mc->cred);
596
 
  gnutls_global_deinit ();
597
647
  return retval;
598
648
}
599
649
 
612
662
                             flags,
613
663
                             void* userdata) {
614
664
  mandos_context *mc = userdata;
615
 
  assert(r);                    /* Spurious warning */
 
665
  assert(r);
616
666
  
617
667
  /* Called whenever a service has been resolved successfully or
618
668
     timed out */
630
680
      char ip[AVAHI_ADDRESS_STR_MAX];
631
681
      avahi_address_snprint(ip, sizeof(ip), address);
632
682
      if(debug){
633
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
634
 
                " 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);
635
686
      }
636
687
      int ret = start_mandos_communication(ip, port, interface, mc);
637
688
      if (ret == 0){
638
 
        exit(EXIT_SUCCESS);
 
689
        avahi_simple_poll_quit(mc->simple_poll);
639
690
      }
640
691
    }
641
692
  }
653
704
                             flags,
654
705
                             void* userdata) {
655
706
  mandos_context *mc = userdata;
656
 
  assert(b);                    /* Spurious warning */
 
707
  assert(b);
657
708
  
658
709
  /* Called whenever a new services becomes available on the LAN or
659
710
     is removed from the LAN */
695
746
 
696
747
/* Combines file name and path and returns the malloced new
697
748
   string. some sane checks could/should be added */
698
 
static const char *combinepath(const char *first, const char *second){
699
 
  size_t f_len = strlen(first);
700
 
  size_t s_len = strlen(second);
701
 
  char *tmp = malloc(f_len + s_len + 2);
702
 
  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){
703
753
    return NULL;
704
754
  }
705
 
  if(f_len > 0){
706
 
    memcpy(tmp, first, f_len);  /* Spurious warning */
707
 
  }
708
 
  tmp[f_len] = '/';
709
 
  if(s_len > 0){
710
 
    memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
711
 
  }
712
 
  tmp[f_len + 1 + s_len] = '\0';
713
755
  return tmp;
714
756
}
715
757
 
722
764
    const char *interface = "eth0";
723
765
    struct ifreq network;
724
766
    int sd;
 
767
    uid_t uid;
 
768
    gid_t gid;
725
769
    char *connect_to = NULL;
726
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";
727
775
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
728
 
                          .dh_bits = 1024, .priority = "SECURE256"};
 
776
                          .dh_bits = 1024, .priority = "SECURE256"
 
777
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
778
    bool gnutls_initalized = false;
729
779
    
730
780
    {
731
 
      /* Temporary int to get the address of for getopt_long */
732
 
      int debug_int = debug ? 1 : 0;
733
 
      while (true){
734
 
        struct option long_options[] = {
735
 
          {"debug", no_argument, &debug_int, 1},
736
 
          {"connect", required_argument, NULL, 'c'},
737
 
          {"interface", required_argument, NULL, 'i'},
738
 
          {"keydir", required_argument, NULL, 'd'},
739
 
          {"seckey", required_argument, NULL, 's'},
740
 
          {"pubkey", required_argument, NULL, 'p'},
741
 
          {"dh-bits", required_argument, NULL, 'D'},
742
 
          {"priority", required_argument, NULL, 'P'},
743
 
          {0, 0, 0, 0} };
744
 
      
745
 
        int option_index = 0;
746
 
        ret = getopt_long (argc, argv, "i:", long_options,
747
 
                           &option_index);
748
 
      
749
 
        if (ret == -1){
750
 
          break;
751
 
        }
752
 
      
753
 
        switch(ret){
754
 
        case 0:
755
 
          break;
756
 
        case 'i':
757
 
          interface = optarg;
758
 
          break;
759
 
        case 'c':
760
 
          connect_to = optarg;
761
 
          break;
762
 
        case 'd':
763
 
          keydir = optarg;
764
 
          break;
765
 
        case 'p':
766
 
          pubkeyfile = optarg;
767
 
          break;
768
 
        case 's':
769
 
          seckeyfile = optarg;
770
 
          break;
771
 
        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 */
772
841
          errno = 0;
773
 
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
 
842
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
774
843
          if (errno){
775
844
            perror("strtol");
776
845
            exit(EXIT_FAILURE);
777
846
          }
778
847
          break;
779
 
        case 'P':
780
 
          mc.priority = optarg;
781
 
          break;
782
 
        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;
783
855
        default:
784
 
          /* getopt_long() has already printed a message about the
785
 
             unrcognized option, so just exit. */
786
 
          exit(EXIT_FAILURE);
787
 
        }
788
 
      }
789
 
      /* Set the global debug flag from the temporary int */
790
 
      debug = debug_int ? true : false;
791
 
    }
792
 
    
793
 
    pubkeyfile = combinepath(keydir, pubkeyfile);
794
 
    if (pubkeyfile == NULL){
795
 
      perror("combinepath");
796
 
      exitcode = EXIT_FAILURE;
797
 
      goto end;
798
 
    }
799
 
    
800
 
    seckeyfile = combinepath(keydir, seckeyfile);
801
 
    if (seckeyfile == NULL){
802
 
      perror("combinepath");
803
 
      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");
804
934
    }
805
935
    
806
936
    if_index = (AvahiIfIndex) if_nametoindex(interface);
815
945
      char *address = strrchr(connect_to, ':');
816
946
      if(address == NULL){
817
947
        fprintf(stderr, "No colon in address\n");
818
 
        exit(EXIT_FAILURE);
 
948
        exitcode = EXIT_FAILURE;
 
949
        goto end;
819
950
      }
820
951
      errno = 0;
821
952
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
822
953
      if(errno){
823
954
        perror("Bad port number");
824
 
        exit(EXIT_FAILURE);
 
955
        exitcode = EXIT_FAILURE;
 
956
        goto end;
825
957
      }
826
958
      *address = '\0';
827
959
      address = connect_to;
828
960
      ret = start_mandos_communication(address, port, if_index, &mc);
829
961
      if(ret < 0){
830
 
        exit(EXIT_FAILURE);
 
962
        exitcode = EXIT_FAILURE;
831
963
      } else {
832
 
        exit(EXIT_SUCCESS);
833
 
      }
834
 
    }
835
 
    
836
 
    /* If the interface is down, bring it up */
837
 
    {
838
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
839
 
      if(sd < 0) {
840
 
        perror("socket");
841
 
        exitcode = EXIT_FAILURE;
842
 
        goto end;
843
 
      }
844
 
      strcpy(network.ifr_name, interface); /* Spurious warning */
845
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
846
 
      if(ret == -1){
847
 
        perror("ioctl SIOCGIFFLAGS");
848
 
        exitcode = EXIT_FAILURE;
849
 
        goto end;
850
 
      }
851
 
      if((network.ifr_flags & IFF_UP) == 0){
852
 
        network.ifr_flags |= IFF_UP;
853
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
854
 
        if(ret == -1){
855
 
          perror("ioctl SIOCSIFFLAGS");
856
 
          exitcode = EXIT_FAILURE;
857
 
          goto end;
858
 
        }
859
 
      }
860
 
      close(sd);
 
964
        exitcode = EXIT_SUCCESS;
 
965
      }
 
966
      goto end;
861
967
    }
862
968
    
863
969
    if (not debug){
875
981
        exitcode = EXIT_FAILURE;
876
982
        goto end;
877
983
    }
878
 
 
 
984
    
879
985
    {
880
986
      AvahiServerConfig config;
881
987
      /* Do not publish any local Zeroconf records */
884
990
      config.publish_addresses = 0;
885
991
      config.publish_workstation = 0;
886
992
      config.publish_domain = 0;
887
 
 
 
993
      
888
994
      /* Allocate a new server */
889
995
      mc.server = avahi_server_new(avahi_simple_poll_get
890
996
                                   (mc.simple_poll), &config, NULL,
891
997
                                   NULL, &error);
892
 
    
 
998
      
893
999
      /* Free the Avahi configuration data */
894
1000
      avahi_server_config_free(&config);
895
1001
    }
915
1021
    }
916
1022
    
917
1023
    /* Run the main loop */
918
 
 
 
1024
    
919
1025
    if (debug){
920
1026
      fprintf(stderr, "Starting Avahi loop search\n");
921
1027
    }
923
1029
    avahi_simple_poll_loop(mc.simple_poll);
924
1030
    
925
1031
 end:
926
 
 
 
1032
    
927
1033
    if (debug){
928
1034
      fprintf(stderr, "%s exiting\n", argv[0]);
929
1035
    }
934
1040
    
935
1041
    if (mc.server != NULL)
936
1042
        avahi_server_free(mc.server);
937
 
 
 
1043
    
938
1044
    if (mc.simple_poll != NULL)
939
1045
        avahi_simple_poll_free(mc.simple_poll);
940
 
    free(pubkeyfile);
941
 
    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
    }
942
1053
    
943
1054
    return exitcode;
944
1055
}