/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/mandosclient.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-03 16:05:52 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080803160552-mvqy5klry3wn57x1
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
                                         own code block.
  (start_mandos_communication): Moved call to "initgnutls" to
                                beginning.  Renamed label "exit" to
                                "mandos_end".  Bug fix: set return
                                code if failure to realloc buffer.
                                Close TLS session as early as
                                possible.
  (resolve_callback): Also print interface number when debugging.
  (main): Moved "debug_int" and "config" into their own respective
          code blocks.

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(), 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() */
 
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 */
58
42
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
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 */
 
43
                                   SIOCSIFFLAGS */
71
44
 
72
 
/* Avahi */
73
 
/* All Avahi types, constants and functions
74
 
 Avahi*, avahi_*,
75
 
 AVAHI_* */
76
45
#include <avahi-core/core.h>
77
46
#include <avahi-core/lookup.h>
78
47
#include <avahi-core/log.h>
80
49
#include <avahi-common/malloc.h>
81
50
#include <avahi-common/error.h>
82
51
 
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 */
 
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 */
91
66
 
92
67
/* GPGME */
93
 
#include <gpgme.h>              /* All GPGME types, constants and
94
 
                                   functions:
95
 
                                   gpgme_*
96
 
                                   GPGME_PROTOCOL_OpenPGP,
97
 
                                   GPG_ERR_NO_* */
 
68
#include <errno.h>              /* perror() */
 
69
#include <gpgme.h>
 
70
 
 
71
/* getopt_long */
 
72
#include <getopt.h>
98
73
 
99
74
#define BUFFER_SIZE 256
100
75
 
 
76
static const char *keydir = "/conf/conf.d/mandos";
 
77
static const char *pubkeyfile = "pubkey.txt";
 
78
static const char *seckeyfile = "seckey.txt";
 
79
 
101
80
bool debug = false;
102
 
static const char *keydir = "/conf/conf.d/mandos";
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
81
 
107
 
/* Used for passing in values through the Avahi callback functions */
 
82
/* Used for passing in values through all the callback functions */
108
83
typedef struct {
109
84
  AvahiSimplePoll *simple_poll;
110
85
  AvahiServer *server;
111
86
  gnutls_certificate_credentials_t cred;
112
87
  unsigned int dh_bits;
113
 
  gnutls_dh_params_t dh_params;
114
88
  const char *priority;
115
89
} mandos_context;
116
90
 
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
 
 
134
91
/* 
135
92
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
136
93
 * Returns -1 on error
229
186
    } else {
230
187
      fprintf(stderr, "Unsupported algorithm: %s\n",
231
188
              result->unsupported_algorithm);
232
 
      fprintf(stderr, "Wrong key usage: %u\n",
 
189
      fprintf(stderr, "Wrong key usage: %d\n",
233
190
              result->wrong_key_usage);
234
191
      if(result->file_name != NULL){
235
192
        fprintf(stderr, "File name: %s\n", result->file_name);
259
216
  
260
217
  *plaintext = NULL;
261
218
  while(true){
262
 
    plaintext_capacity = adjustbuffer(plaintext,
263
 
                                      (size_t)plaintext_length,
264
 
                                      plaintext_capacity);
265
 
    if (plaintext_capacity == 0){
266
 
        perror("adjustbuffer");
 
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");
267
225
        plaintext_length = -1;
268
226
        goto decrypt_end;
 
227
      }
 
228
      plaintext_capacity += BUFFER_SIZE;
269
229
    }
270
230
    
271
231
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
302
262
}
303
263
 
304
264
static const char * safer_gnutls_strerror (int value) {
305
 
  const char *ret = gnutls_strerror (value); /* Spurious warning */
 
265
  const char *ret = gnutls_strerror (value);
306
266
  if (ret == NULL)
307
267
    ret = "(unknown)";
308
268
  return ret;
314
274
  fprintf(stderr, "GnuTLS: %s", string);
315
275
}
316
276
 
317
 
static int init_gnutls_global(mandos_context *mc,
318
 
                              const char *pubkeyfilename,
319
 
                              const char *seckeyfilename){
 
277
static int initgnutls(mandos_context *mc, gnutls_session_t *session,
 
278
                      gnutls_dh_params_t *dh_params){
320
279
  int ret;
321
280
  
322
281
  if(debug){
323
282
    fprintf(stderr, "Initializing GnuTLS\n");
324
283
  }
325
 
  
326
 
  ret = gnutls_global_init();
327
 
  if (ret != GNUTLS_E_SUCCESS) {
 
284
 
 
285
  if ((ret = gnutls_global_init ())
 
286
      != GNUTLS_E_SUCCESS) {
328
287
    fprintf (stderr, "GnuTLS global_init: %s\n",
329
288
             safer_gnutls_strerror(ret));
330
289
    return -1;
339
298
  }
340
299
  
341
300
  /* OpenPGP credentials */
342
 
  gnutls_certificate_allocate_credentials(&mc->cred);
343
 
  if (ret != GNUTLS_E_SUCCESS){
344
 
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
345
 
                                                     warning */
 
301
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
302
      != GNUTLS_E_SUCCESS) {
 
303
    fprintf (stderr, "GnuTLS memory error: %s\n",
346
304
             safer_gnutls_strerror(ret));
347
 
    gnutls_global_deinit ();
348
305
    return -1;
349
306
  }
350
307
  
351
308
  if(debug){
352
309
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
353
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
354
 
            seckeyfilename);
 
310
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
311
            seckeyfile);
355
312
  }
356
313
  
357
314
  ret = gnutls_certificate_set_openpgp_key_file
358
 
    (mc->cred, pubkeyfilename, seckeyfilename,
359
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
315
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
360
316
  if (ret != GNUTLS_E_SUCCESS) {
361
317
    fprintf(stderr,
362
318
            "Error[%d] while reading the OpenPGP key pair ('%s',"
363
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
319
            " '%s')\n", ret, pubkeyfile, seckeyfile);
364
320
    fprintf(stdout, "The GnuTLS error is: %s\n",
365
321
            safer_gnutls_strerror(ret));
366
 
    goto globalfail;
 
322
    return -1;
367
323
  }
368
324
  
369
325
  /* GnuTLS server initialization */
370
 
  ret = gnutls_dh_params_init(&mc->dh_params);
 
326
  ret = gnutls_dh_params_init(dh_params);
371
327
  if (ret != GNUTLS_E_SUCCESS) {
372
328
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
373
329
             " %s\n", safer_gnutls_strerror(ret));
374
 
    goto globalfail;
 
330
    return -1;
375
331
  }
376
 
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
 
332
  ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
377
333
  if (ret != GNUTLS_E_SUCCESS) {
378
334
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
379
335
             safer_gnutls_strerror(ret));
380
 
    goto globalfail;
 
336
    return -1;
381
337
  }
382
338
  
383
 
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
384
 
 
385
 
  return 0;
386
 
 
387
 
 globalfail:
388
 
 
389
 
  gnutls_certificate_free_credentials(mc->cred);
390
 
  gnutls_global_deinit();
391
 
  return -1;
392
 
 
393
 
}
394
 
 
395
 
static int init_gnutls_session(mandos_context *mc,
396
 
                               gnutls_session_t *session){
397
 
  int ret;
 
339
  gnutls_certificate_set_dh_params(mc->cred, *dh_params);
 
340
  
398
341
  /* GnuTLS session creation */
399
342
  ret = gnutls_init(session, GNUTLS_SERVER);
400
343
  if (ret != GNUTLS_E_SUCCESS){
409
352
      fprintf(stderr, "Syntax error at: %s\n", err);
410
353
      fprintf(stderr, "GnuTLS error: %s\n",
411
354
              safer_gnutls_strerror(ret));
412
 
      gnutls_deinit (*session);
413
355
      return -1;
414
356
    }
415
357
  }
419
361
  if (ret != GNUTLS_E_SUCCESS) {
420
362
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
421
363
            safer_gnutls_strerror(ret));
422
 
    gnutls_deinit (*session);
423
364
    return -1;
424
365
  }
425
366
  
441
382
                                      AvahiIfIndex if_index,
442
383
                                      mandos_context *mc){
443
384
  int ret, tcp_sd;
444
 
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
 
385
  struct sockaddr_in6 to;
445
386
  char *buffer = NULL;
446
387
  char *decrypted_buffer;
447
388
  size_t buffer_length = 0;
448
389
  size_t buffer_capacity = 0;
449
390
  ssize_t decrypted_buffer_size;
450
 
  size_t written;
 
391
  size_t written = 0;
451
392
  int retval = 0;
452
393
  char interface[IF_NAMESIZE];
453
394
  gnutls_session_t session;
 
395
  gnutls_dh_params_t dh_params;
454
396
  
455
 
  ret = init_gnutls_session (mc, &session);
 
397
  ret = initgnutls (mc, &session, &dh_params);
456
398
  if (ret != 0){
457
399
    return -1;
458
400
  }
459
401
  
460
402
  if(debug){
461
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
462
 
            "\n", ip, port);
 
403
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
 
404
            ip, port);
463
405
  }
464
406
  
465
407
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
476
418
    fprintf(stderr, "Binding to interface %s\n", interface);
477
419
  }
478
420
  
479
 
  memset(&to, 0, sizeof(to));
480
 
  to.in6.sin6_family = AF_INET6;
 
421
  memset(&to,0,sizeof(to));     /* Spurious warning */
 
422
  to.sin6_family = AF_INET6;
481
423
  /* It would be nice to have a way to detect if we were passed an
482
424
     IPv4 address here.   Now we assume an IPv6 address. */
483
 
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
 
425
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
484
426
  if (ret < 0 ){
485
427
    perror("inet_pton");
486
428
    return -1;
489
431
    fprintf(stderr, "Bad address: %s\n", ip);
490
432
    return -1;
491
433
  }
492
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
434
  to.sin6_port = htons(port);   /* Spurious warning */
493
435
  
494
 
  to.in6.sin6_scope_id = (uint32_t)if_index;
 
436
  to.sin6_scope_id = (uint32_t)if_index;
495
437
  
496
438
  if(debug){
497
 
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
498
 
            port);
 
439
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
499
440
    char addrstr[INET6_ADDRSTRLEN] = "";
500
 
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
 
441
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
501
442
                 sizeof(addrstr)) == NULL){
502
443
      perror("inet_ntop");
503
444
    } else {
507
448
    }
508
449
  }
509
450
  
510
 
  ret = connect(tcp_sd, &to.in, sizeof(to));
 
451
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
511
452
  if (ret < 0){
512
453
    perror("connect");
513
454
    return -1;
514
455
  }
515
 
 
516
 
  const char *out = mandos_protocol_version;
517
 
  written = 0;
518
 
  while (true){
519
 
    size_t out_size = strlen(out);
520
 
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
521
 
                                   out_size - written));
522
 
    if (ret == -1){
523
 
      perror("write");
524
 
      retval = -1;
525
 
      goto mandos_end;
526
 
    }
527
 
    written += (size_t)ret;
528
 
    if(written < out_size){
529
 
      continue;
530
 
    } else {
531
 
      if (out == mandos_protocol_version){
532
 
        written = 0;
533
 
        out = "\r\n";
534
 
      } else {
535
 
        break;
536
 
      }
537
 
    }
538
 
  }
539
 
 
 
456
  
540
457
  if(debug){
541
458
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
542
459
  }
543
460
  
544
461
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
545
 
 
546
 
  do{
547
 
    ret = gnutls_handshake (session);
548
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
462
  
 
463
  ret = gnutls_handshake (session);
549
464
  
550
465
  if (ret != GNUTLS_E_SUCCESS){
551
466
    if(debug){
564
479
  }
565
480
 
566
481
  while(true){
567
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
568
 
                                   buffer_capacity);
569
 
    if (buffer_capacity == 0){
570
 
      perror("adjustbuffer");
571
 
      retval = -1;
572
 
      goto mandos_end;
 
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;
573
490
    }
574
491
    
575
492
    ret = gnutls_record_recv(session, buffer+buffer_length,
583
500
      case GNUTLS_E_AGAIN:
584
501
        break;
585
502
      case GNUTLS_E_REHANDSHAKE:
586
 
        do{
587
 
          ret = gnutls_handshake (session);
588
 
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
503
        ret = gnutls_handshake (session);
589
504
        if (ret < 0){
590
505
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
591
506
          gnutls_perror (ret);
617
532
                                               &decrypted_buffer,
618
533
                                               keydir);
619
534
    if (decrypted_buffer_size >= 0){
620
 
      written = 0;
621
535
      while(written < (size_t) decrypted_buffer_size){
622
536
        ret = (int)fwrite (decrypted_buffer + written, 1,
623
537
                           (size_t)decrypted_buffer_size - written,
644
558
  free(buffer);
645
559
  close(tcp_sd);
646
560
  gnutls_deinit (session);
 
561
  gnutls_certificate_free_credentials (mc->cred);
 
562
  gnutls_global_deinit ();
647
563
  return retval;
648
564
}
649
565
 
662
578
                             flags,
663
579
                             void* userdata) {
664
580
  mandos_context *mc = userdata;
665
 
  assert(r);
 
581
  assert(r);                    /* Spurious warning */
666
582
  
667
583
  /* Called whenever a service has been resolved successfully or
668
584
     timed out */
680
596
      char ip[AVAHI_ADDRESS_STR_MAX];
681
597
      avahi_address_snprint(ip, sizeof(ip), address);
682
598
      if(debug){
683
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
684
 
                PRIu16 ") on port %d\n", name, host_name, ip,
685
 
                interface, port);
 
599
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
 
600
                " port %d\n", name, host_name, ip, interface, port);
686
601
      }
687
602
      int ret = start_mandos_communication(ip, port, interface, mc);
688
603
      if (ret == 0){
689
 
        avahi_simple_poll_quit(mc->simple_poll);
 
604
        exit(EXIT_SUCCESS);
690
605
      }
691
606
    }
692
607
  }
704
619
                             flags,
705
620
                             void* userdata) {
706
621
  mandos_context *mc = userdata;
707
 
  assert(b);
 
622
  assert(b);                    /* Spurious warning */
708
623
  
709
624
  /* Called whenever a new services becomes available on the LAN or
710
625
     is removed from the LAN */
746
661
 
747
662
/* Combines file name and path and returns the malloced new
748
663
   string. some sane checks could/should be added */
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){
 
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){
753
669
    return NULL;
754
670
  }
 
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';
755
679
  return tmp;
756
680
}
757
681
 
764
688
    const char *interface = "eth0";
765
689
    struct ifreq network;
766
690
    int sd;
767
 
    uid_t uid;
768
 
    gid_t gid;
769
691
    char *connect_to = NULL;
770
692
    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";
775
693
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
776
694
                          .dh_bits = 1024, .priority = "SECURE256"};
777
 
    bool gnutls_initalized = false;
778
695
    
779
696
    {
780
 
      struct argp_option options[] = {
781
 
        { .name = "debug", .key = 128,
782
 
          .doc = "Debug mode", .group = 3 },
783
 
        { .name = "connect", .key = 'c',
784
 
          .arg = "IP",
785
 
          .doc = "Connect directly to a sepcified mandos server",
786
 
          .group = 1 },
787
 
        { .name = "interface", .key = 'i',
788
 
          .arg = "INTERFACE",
789
 
          .doc = "Interface that Avahi will conntect through",
790
 
          .group = 1 },
791
 
        { .name = "keydir", .key = 'd',
792
 
          .arg = "KEYDIR",
793
 
          .doc = "Directory where the openpgp keyring is",
794
 
          .group = 1 },
795
 
        { .name = "seckey", .key = 's',
796
 
          .arg = "SECKEY",
797
 
          .doc = "Secret openpgp key for gnutls authentication",
798
 
          .group = 1 },
799
 
        { .name = "pubkey", .key = 'p',
800
 
          .arg = "PUBKEY",
801
 
          .doc = "Public openpgp key for gnutls authentication",
802
 
          .group = 2 },
803
 
        { .name = "dh-bits", .key = 129,
804
 
          .arg = "BITS",
805
 
          .doc = "dh-bits to use in gnutls communication",
806
 
          .group = 2 },
807
 
        { .name = "priority", .key = 130,
808
 
          .arg = "PRIORITY",
809
 
          .doc = "GNUTLS priority", .group = 1 },
810
 
        { .name = NULL }
811
 
      };
812
 
 
813
 
      
814
 
      error_t parse_opt (int key, char *arg,
815
 
                         struct argp_state *state) {
816
 
        /* Get the INPUT argument from `argp_parse', which we know is
817
 
           a pointer to our plugin list pointer. */
818
 
        switch (key) {
819
 
        case 128:
820
 
          debug = true;
 
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;
821
724
          break;
822
725
        case 'c':
823
 
          connect_to = arg;
824
 
          break;
825
 
        case 'i':
826
 
          interface = arg;
 
726
          connect_to = optarg;
827
727
          break;
828
728
        case 'd':
829
 
          keydir = arg;
 
729
          keydir = optarg;
 
730
          break;
 
731
        case 'p':
 
732
          pubkeyfile = optarg;
830
733
          break;
831
734
        case 's':
832
 
          seckeyname = arg;
833
 
          break;
834
 
        case 'p':
835
 
          pubkeyname = arg;
836
 
          break;
837
 
        case 129:
 
735
          seckeyfile = optarg;
 
736
          break;
 
737
        case 'D':
838
738
          errno = 0;
839
 
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
 
739
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
840
740
          if (errno){
841
741
            perror("strtol");
842
742
            exit(EXIT_FAILURE);
843
743
          }
844
744
          break;
845
 
        case 130:
846
 
          mc.priority = arg;
847
 
          break;
848
 
        case ARGP_KEY_ARG:
849
 
          argp_usage (state);
850
 
        case ARGP_KEY_END:
851
 
          break;
 
745
        case 'P':
 
746
          mc.priority = optarg;
 
747
          break;
 
748
        case '?':
852
749
        default:
853
 
          return ARGP_ERR_UNKNOWN;
854
 
        }
855
 
        return 0;
856
 
      }
857
 
 
858
 
      struct argp argp = { .options = options, .parser = parse_opt,
859
 
                           .args_doc = "",
860
 
                           .doc = "Mandos client -- Get and decrypt"
861
 
                           " passwords from mandos server" };
862
 
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
863
 
      if (ret == ARGP_ERR_UNKNOWN){
864
 
        fprintf(stderr, "Unknown error while parsing arguments\n");
865
 
        exitcode = EXIT_FAILURE;
866
 
        goto end;
867
 
      }
868
 
    }
869
 
      
870
 
    pubkeyfilename = combinepath(keydir, pubkeyname);
871
 
    if (pubkeyfilename == NULL){
872
 
      perror("combinepath");
873
 
      exitcode = EXIT_FAILURE;
874
 
      goto end;
875
 
    }
876
 
    
877
 
    seckeyfilename = combinepath(keydir, seckeyname);
878
 
    if (seckeyfilename == NULL){
879
 
      perror("combinepath");
880
 
      exitcode = EXIT_FAILURE;
881
 
      goto end;
882
 
    }
883
 
 
884
 
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
885
 
    if (ret == -1){
886
 
      fprintf(stderr, "init_gnutls_global failed\n");
887
 
      exitcode = EXIT_FAILURE;
888
 
      goto end;
889
 
    } else {
890
 
      gnutls_initalized = true;
891
 
    }
892
 
    
893
 
    /* If the interface is down, bring it up */
894
 
    {
895
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
896
 
      if(sd < 0) {
897
 
        perror("socket");
898
 
        exitcode = EXIT_FAILURE;
899
 
        goto end;
900
 
      }
901
 
      strcpy(network.ifr_name, interface);
902
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
903
 
      if(ret == -1){
904
 
        perror("ioctl SIOCGIFFLAGS");
905
 
        exitcode = EXIT_FAILURE;
906
 
        goto end;
907
 
      }
908
 
      if((network.ifr_flags & IFF_UP) == 0){
909
 
        network.ifr_flags |= IFF_UP;
910
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
911
 
        if(ret == -1){
912
 
          perror("ioctl SIOCSIFFLAGS");
913
 
          exitcode = EXIT_FAILURE;
914
 
          goto end;
915
 
        }
916
 
      }
917
 
      close(sd);
918
 
    }
919
 
    
920
 
    uid = getuid();
921
 
    gid = getgid();
922
 
    
923
 
    ret = setuid(uid);
924
 
    if (ret == -1){
925
 
      perror("setuid");
926
 
    }
927
 
    
928
 
    setgid(gid);
929
 
    if (ret == -1){
930
 
      perror("setgid");
 
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;
931
770
    }
932
771
    
933
772
    if_index = (AvahiIfIndex) if_nametoindex(interface);
942
781
      char *address = strrchr(connect_to, ':');
943
782
      if(address == NULL){
944
783
        fprintf(stderr, "No colon in address\n");
945
 
        exitcode = EXIT_FAILURE;
946
 
        goto end;
 
784
        exit(EXIT_FAILURE);
947
785
      }
948
786
      errno = 0;
949
787
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
950
788
      if(errno){
951
789
        perror("Bad port number");
952
 
        exitcode = EXIT_FAILURE;
953
 
        goto end;
 
790
        exit(EXIT_FAILURE);
954
791
      }
955
792
      *address = '\0';
956
793
      address = connect_to;
957
794
      ret = start_mandos_communication(address, port, if_index, &mc);
958
795
      if(ret < 0){
959
 
        exitcode = EXIT_FAILURE;
 
796
        exit(EXIT_FAILURE);
960
797
      } else {
961
 
        exitcode = EXIT_SUCCESS;
962
 
      }
963
 
      goto end;
 
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
827
    }
965
828
    
966
829
    if (not debug){
1040
903
 
1041
904
    if (mc.simple_poll != NULL)
1042
905
        avahi_simple_poll_free(mc.simple_poll);
1043
 
    free(pubkeyfilename);
1044
 
    free(seckeyfilename);
1045
 
 
1046
 
    if (gnutls_initalized){
1047
 
      gnutls_certificate_free_credentials(mc.cred);
1048
 
      gnutls_global_deinit ();
1049
 
    }
 
906
    free(pubkeyfile);
 
907
    free(seckeyfile);
1050
908
    
1051
909
    return exitcode;
1052
910
}