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

merge +
mandosclient
        Added a adjustbuffer function.

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() */
 
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
36
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() */
 
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 */
58
44
#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 */
 
45
                                   SIOCSIFFLAGS */
71
46
 
72
 
/* Avahi */
73
 
/* All Avahi types, constants and functions
74
 
 Avahi*, avahi_*,
75
 
 AVAHI_* */
76
47
#include <avahi-core/core.h>
77
48
#include <avahi-core/lookup.h>
78
49
#include <avahi-core/log.h>
80
51
#include <avahi-common/malloc.h>
81
52
#include <avahi-common/error.h>
82
53
 
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 */
 
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 */
91
68
 
92
69
/* GPGME */
93
 
#include <gpgme.h>              /* All GPGME types, constants and
94
 
                                   functions:
95
 
                                   gpgme_*
96
 
                                   GPGME_PROTOCOL_OpenPGP,
97
 
                                   GPG_ERR_NO_* */
 
70
#include <errno.h>              /* perror() */
 
71
#include <gpgme.h>
 
72
 
 
73
/* getopt_long */
 
74
#include <getopt.h>
98
75
 
99
76
#define BUFFER_SIZE 256
100
77
 
 
78
static const char *keydir = "/conf/conf.d/mandos";
 
79
static const char *pubkeyfile = "pubkey.txt";
 
80
static const char *seckeyfile = "seckey.txt";
 
81
 
101
82
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
 
 
107
 
/* Used for passing in values through the Avahi callback functions */
 
83
 
 
84
const char mandos_protocol_version[] = "1";
 
85
 
 
86
/* Used for passing in values through all the callback functions */
108
87
typedef struct {
109
88
  AvahiSimplePoll *simple_poll;
110
89
  AvahiServer *server;
111
90
  gnutls_certificate_credentials_t cred;
112
91
  unsigned int dh_bits;
113
 
  gnutls_dh_params_t dh_params;
114
92
  const char *priority;
115
93
} mandos_context;
116
94
 
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
95
size_t adjustbuffer(char **buffer, size_t buffer_length,
123
96
                  size_t buffer_capacity){
124
97
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
229
202
    } else {
230
203
      fprintf(stderr, "Unsupported algorithm: %s\n",
231
204
              result->unsupported_algorithm);
232
 
      fprintf(stderr, "Wrong key usage: %u\n",
 
205
      fprintf(stderr, "Wrong key usage: %d\n",
233
206
              result->wrong_key_usage);
234
207
      if(result->file_name != NULL){
235
208
        fprintf(stderr, "File name: %s\n", result->file_name);
259
232
  
260
233
  *plaintext = NULL;
261
234
  while(true){
262
 
    plaintext_capacity = adjustbuffer(plaintext,
263
 
                                      (size_t)plaintext_length,
 
235
    plaintext_capacity = adjustbuffer(plaintext, (size_t)plaintext_length,
264
236
                                      plaintext_capacity);
265
237
    if (plaintext_capacity == 0){
266
238
        perror("adjustbuffer");
314
286
  fprintf(stderr, "GnuTLS: %s", string);
315
287
}
316
288
 
317
 
static int init_gnutls_global(mandos_context *mc,
318
 
                              const char *pubkeyfilename,
319
 
                              const char *seckeyfilename){
 
289
static int initgnutls(mandos_context *mc, gnutls_session_t *session,
 
290
                      gnutls_dh_params_t *dh_params){
320
291
  int ret;
321
292
  
322
293
  if(debug){
323
294
    fprintf(stderr, "Initializing GnuTLS\n");
324
295
  }
325
 
  
326
 
  ret = gnutls_global_init();
327
 
  if (ret != GNUTLS_E_SUCCESS) {
 
296
 
 
297
  if ((ret = gnutls_global_init ())
 
298
      != GNUTLS_E_SUCCESS) {
328
299
    fprintf (stderr, "GnuTLS global_init: %s\n",
329
300
             safer_gnutls_strerror(ret));
330
301
    return -1;
339
310
  }
340
311
  
341
312
  /* OpenPGP credentials */
342
 
  gnutls_certificate_allocate_credentials(&mc->cred);
343
 
  if (ret != GNUTLS_E_SUCCESS){
 
313
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
314
      != GNUTLS_E_SUCCESS) {
344
315
    fprintf (stderr, "GnuTLS memory error: %s\n",
345
316
             safer_gnutls_strerror(ret));
346
 
    gnutls_global_deinit ();
347
317
    return -1;
348
318
  }
349
319
  
350
320
  if(debug){
351
321
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
352
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
353
 
            seckeyfilename);
 
322
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
323
            seckeyfile);
354
324
  }
355
325
  
356
326
  ret = gnutls_certificate_set_openpgp_key_file
357
 
    (mc->cred, pubkeyfilename, seckeyfilename,
358
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
327
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
359
328
  if (ret != GNUTLS_E_SUCCESS) {
360
329
    fprintf(stderr,
361
330
            "Error[%d] while reading the OpenPGP key pair ('%s',"
362
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
331
            " '%s')\n", ret, pubkeyfile, seckeyfile);
363
332
    fprintf(stdout, "The GnuTLS error is: %s\n",
364
333
            safer_gnutls_strerror(ret));
365
 
    goto globalfail;
 
334
    return -1;
366
335
  }
367
336
  
368
337
  /* GnuTLS server initialization */
369
 
  ret = gnutls_dh_params_init(&mc->dh_params);
 
338
  ret = gnutls_dh_params_init(dh_params);
370
339
  if (ret != GNUTLS_E_SUCCESS) {
371
340
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
372
341
             " %s\n", safer_gnutls_strerror(ret));
373
 
    goto globalfail;
 
342
    return -1;
374
343
  }
375
 
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
 
344
  ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
376
345
  if (ret != GNUTLS_E_SUCCESS) {
377
346
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
378
347
             safer_gnutls_strerror(ret));
379
 
    goto globalfail;
 
348
    return -1;
380
349
  }
381
350
  
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
 
 
394
 
static int init_gnutls_session(mandos_context *mc,
395
 
                               gnutls_session_t *session){
396
 
  int ret;
 
351
  gnutls_certificate_set_dh_params(mc->cred, *dh_params);
 
352
  
397
353
  /* GnuTLS session creation */
398
354
  ret = gnutls_init(session, GNUTLS_SERVER);
399
355
  if (ret != GNUTLS_E_SUCCESS){
408
364
      fprintf(stderr, "Syntax error at: %s\n", err);
409
365
      fprintf(stderr, "GnuTLS error: %s\n",
410
366
              safer_gnutls_strerror(ret));
411
 
      gnutls_deinit (*session);
412
367
      return -1;
413
368
    }
414
369
  }
418
373
  if (ret != GNUTLS_E_SUCCESS) {
419
374
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
420
375
            safer_gnutls_strerror(ret));
421
 
    gnutls_deinit (*session);
422
376
    return -1;
423
377
  }
424
378
  
440
394
                                      AvahiIfIndex if_index,
441
395
                                      mandos_context *mc){
442
396
  int ret, tcp_sd;
443
 
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
 
397
  struct sockaddr_in6 to;
444
398
  char *buffer = NULL;
445
399
  char *decrypted_buffer;
446
400
  size_t buffer_length = 0;
450
404
  int retval = 0;
451
405
  char interface[IF_NAMESIZE];
452
406
  gnutls_session_t session;
 
407
  gnutls_dh_params_t dh_params;
453
408
  
454
 
  ret = init_gnutls_session (mc, &session);
 
409
  ret = initgnutls (mc, &session, &dh_params);
455
410
  if (ret != 0){
456
411
    return -1;
457
412
  }
458
413
  
459
414
  if(debug){
460
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
461
 
            "\n", ip, port);
 
415
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
 
416
            ip, port);
462
417
  }
463
418
  
464
419
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
475
430
    fprintf(stderr, "Binding to interface %s\n", interface);
476
431
  }
477
432
  
478
 
  memset(&to, 0, sizeof(to));   /* Spurious warning */
479
 
  to.in6.sin6_family = AF_INET6;
 
433
  memset(&to,0,sizeof(to));     /* Spurious warning */
 
434
  to.sin6_family = AF_INET6;
480
435
  /* It would be nice to have a way to detect if we were passed an
481
436
     IPv4 address here.   Now we assume an IPv6 address. */
482
 
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
 
437
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
483
438
  if (ret < 0 ){
484
439
    perror("inet_pton");
485
440
    return -1;
488
443
    fprintf(stderr, "Bad address: %s\n", ip);
489
444
    return -1;
490
445
  }
491
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
446
  to.sin6_port = htons(port);   /* Spurious warning */
492
447
  
493
 
  to.in6.sin6_scope_id = (uint32_t)if_index;
 
448
  to.sin6_scope_id = (uint32_t)if_index;
494
449
  
495
450
  if(debug){
496
 
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
497
 
            port);
 
451
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
498
452
    char addrstr[INET6_ADDRSTRLEN] = "";
499
 
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
 
453
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
500
454
                 sizeof(addrstr)) == NULL){
501
455
      perror("inet_ntop");
502
456
    } else {
506
460
    }
507
461
  }
508
462
  
509
 
  ret = connect(tcp_sd, &to.in, sizeof(to));
 
463
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
510
464
  if (ret < 0){
511
465
    perror("connect");
512
466
    return -1;
541
495
  }
542
496
  
543
497
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
544
 
 
545
 
  do{
546
 
    ret = gnutls_handshake (session);
547
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
498
  
 
499
  ret = gnutls_handshake (session);
548
500
  
549
501
  if (ret != GNUTLS_E_SUCCESS){
550
502
    if(debug){
563
515
  }
564
516
 
565
517
  while(true){
566
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
567
 
                                   buffer_capacity);
 
518
    buffer_capacity = adjustbuffer(&buffer, buffer_length, buffer_capacity);
568
519
    if (buffer_capacity == 0){
569
520
      perror("adjustbuffer");
570
521
      retval = -1;
582
533
      case GNUTLS_E_AGAIN:
583
534
        break;
584
535
      case GNUTLS_E_REHANDSHAKE:
585
 
        do{
586
 
          ret = gnutls_handshake (session);
587
 
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
536
        ret = gnutls_handshake (session);
588
537
        if (ret < 0){
589
538
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
590
539
          gnutls_perror (ret);
643
592
  free(buffer);
644
593
  close(tcp_sd);
645
594
  gnutls_deinit (session);
 
595
  gnutls_certificate_free_credentials (mc->cred);
 
596
  gnutls_global_deinit ();
646
597
  return retval;
647
598
}
648
599
 
679
630
      char ip[AVAHI_ADDRESS_STR_MAX];
680
631
      avahi_address_snprint(ip, sizeof(ip), address);
681
632
      if(debug){
682
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
683
 
                PRIu16 ") on port %d\n", name, host_name, ip,
684
 
                interface, port);
 
633
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
 
634
                " port %d\n", name, host_name, ip, interface, port);
685
635
      }
686
636
      int ret = start_mandos_communication(ip, port, interface, mc);
687
637
      if (ret == 0){
688
 
        avahi_simple_poll_quit(mc->simple_poll);
 
638
        exit(EXIT_SUCCESS);
689
639
      }
690
640
    }
691
641
  }
745
695
 
746
696
/* Combines file name and path and returns the malloced new
747
697
   string. some sane checks could/should be added */
748
 
static char *combinepath(const char *first, const char *second){
749
 
  char *tmp;
750
 
  int ret = asprintf(&tmp, "%s/%s", first, second);
751
 
  if(ret < 0){
 
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){
752
703
    return NULL;
753
704
  }
 
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';
754
713
  return tmp;
755
714
}
756
715
 
763
722
    const char *interface = "eth0";
764
723
    struct ifreq network;
765
724
    int sd;
766
 
    uid_t uid;
767
 
    gid_t gid;
768
725
    char *connect_to = NULL;
769
726
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
770
 
    char *pubkeyfilename = NULL;
771
 
    char *seckeyfilename = NULL;
772
 
    const char *pubkeyname = "pubkey.txt";
773
 
    const char *seckeyname = "seckey.txt";
774
727
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
775
728
                          .dh_bits = 1024, .priority = "SECURE256"};
776
 
    bool gnutls_initalized = false;
777
729
    
778
730
    {
779
 
      struct argp_option options[] = {
780
 
        { .name = "debug", .key = 128,
781
 
          .doc = "Debug mode", .group = 3 },
782
 
        { .name = "connect", .key = 'c',
783
 
          .arg = "IP",
784
 
          .doc = "Connect directly to a sepcified mandos server",
785
 
          .group = 1 },
786
 
        { .name = "interface", .key = 'i',
787
 
          .arg = "INTERFACE",
788
 
          .doc = "Interface that Avahi will conntect through",
789
 
          .group = 1 },
790
 
        { .name = "keydir", .key = 'd',
791
 
          .arg = "KEYDIR",
792
 
          .doc = "Directory where the openpgp keyring is",
793
 
          .group = 1 },
794
 
        { .name = "seckey", .key = 's',
795
 
          .arg = "SECKEY",
796
 
          .doc = "Secret openpgp key for gnutls authentication",
797
 
          .group = 1 },
798
 
        { .name = "pubkey", .key = 'p',
799
 
          .arg = "PUBKEY",
800
 
          .doc = "Public openpgp key for gnutls authentication",
801
 
          .group = 2 },
802
 
        { .name = "dh-bits", .key = 129,
803
 
          .arg = "BITS",
804
 
          .doc = "dh-bits to use in gnutls communication",
805
 
          .group = 2 },
806
 
        { .name = "priority", .key = 130,
807
 
          .arg = "PRIORITY",
808
 
          .doc = "GNUTLS priority", .group = 1 },
809
 
        { .name = NULL }
810
 
      };
811
 
 
812
 
      
813
 
      error_t parse_opt (int key, char *arg,
814
 
                         struct argp_state *state) {
815
 
        /* Get the INPUT argument from `argp_parse', which we know is
816
 
           a pointer to our plugin list pointer. */
817
 
        switch (key) {
818
 
        case 128:
819
 
          debug = true;
 
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;
820
758
          break;
821
759
        case 'c':
822
 
          connect_to = arg;
823
 
          break;
824
 
        case 'i':
825
 
          interface = arg;
 
760
          connect_to = optarg;
826
761
          break;
827
762
        case 'd':
828
 
          keydir = arg;
 
763
          keydir = optarg;
 
764
          break;
 
765
        case 'p':
 
766
          pubkeyfile = optarg;
829
767
          break;
830
768
        case 's':
831
 
          seckeyname = arg;
832
 
          break;
833
 
        case 'p':
834
 
          pubkeyname = arg;
835
 
          break;
836
 
        case 129:
 
769
          seckeyfile = optarg;
 
770
          break;
 
771
        case 'D':
837
772
          errno = 0;
838
 
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
 
773
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
839
774
          if (errno){
840
775
            perror("strtol");
841
776
            exit(EXIT_FAILURE);
842
777
          }
843
778
          break;
844
 
        case 130:
845
 
          mc.priority = arg;
846
 
          break;
847
 
        case ARGP_KEY_ARG:
848
 
          argp_usage (state);
849
 
        case ARGP_KEY_END:
850
 
          break;
 
779
        case 'P':
 
780
          mc.priority = optarg;
 
781
          break;
 
782
        case '?':
851
783
        default:
852
 
          return ARGP_ERR_UNKNOWN;
853
 
        }
854
 
        return 0;
855
 
      }
856
 
 
857
 
      struct argp argp = { .options = options, .parser = parse_opt,
858
 
                           .args_doc = "",
859
 
                           .doc = "Mandos client -- Get and decrypt"
860
 
                           " passwords from mandos server" };
861
 
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
862
 
      if (ret == ARGP_ERR_UNKNOWN){
863
 
        fprintf(stderr, "Unknown error while parsing arguments\n");
864
 
        exitcode = EXIT_FAILURE;
865
 
        goto end;
866
 
      }
867
 
    }
868
 
      
869
 
    pubkeyfilename = combinepath(keydir, pubkeyname);
870
 
    if (pubkeyfilename == NULL){
871
 
      perror("combinepath");
872
 
      exitcode = EXIT_FAILURE;
873
 
      goto end;
874
 
    }
875
 
    
876
 
    seckeyfilename = combinepath(keydir, seckeyname);
877
 
    if (seckeyfilename == NULL){
878
 
      perror("combinepath");
879
 
      exitcode = EXIT_FAILURE;
880
 
      goto end;
881
 
    }
882
 
 
883
 
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
884
 
    if (ret == -1){
885
 
      fprintf(stderr, "init_gnutls_global failed\n");
886
 
      exitcode = EXIT_FAILURE;
887
 
      goto end;
888
 
    } else {
889
 
      gnutls_initalized = true;
890
 
    }
891
 
    
892
 
    /* If the interface is down, bring it up */
893
 
    {
894
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
895
 
      if(sd < 0) {
896
 
        perror("socket");
897
 
        exitcode = EXIT_FAILURE;
898
 
        goto end;
899
 
      }
900
 
      strcpy(network.ifr_name, interface); /* Spurious warning */
901
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
902
 
      if(ret == -1){
903
 
        perror("ioctl SIOCGIFFLAGS");
904
 
        exitcode = EXIT_FAILURE;
905
 
        goto end;
906
 
      }
907
 
      if((network.ifr_flags & IFF_UP) == 0){
908
 
        network.ifr_flags |= IFF_UP;
909
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
910
 
        if(ret == -1){
911
 
          perror("ioctl SIOCSIFFLAGS");
912
 
          exitcode = EXIT_FAILURE;
913
 
          goto end;
914
 
        }
915
 
      }
916
 
      close(sd);
917
 
    }
918
 
    
919
 
    uid = getuid();
920
 
    gid = getgid();
921
 
    
922
 
    ret = setuid(uid);
923
 
    if (ret == -1){
924
 
      perror("setuid");
925
 
    }
926
 
    
927
 
    setgid(gid);
928
 
    if (ret == -1){
929
 
      perror("setgid");
 
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;
930
804
    }
931
805
    
932
806
    if_index = (AvahiIfIndex) if_nametoindex(interface);
941
815
      char *address = strrchr(connect_to, ':');
942
816
      if(address == NULL){
943
817
        fprintf(stderr, "No colon in address\n");
944
 
        exitcode = EXIT_FAILURE;
945
 
        goto end;
 
818
        exit(EXIT_FAILURE);
946
819
      }
947
820
      errno = 0;
948
821
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
949
822
      if(errno){
950
823
        perror("Bad port number");
951
 
        exitcode = EXIT_FAILURE;
952
 
        goto end;
 
824
        exit(EXIT_FAILURE);
953
825
      }
954
826
      *address = '\0';
955
827
      address = connect_to;
956
828
      ret = start_mandos_communication(address, port, if_index, &mc);
957
829
      if(ret < 0){
958
 
        exitcode = EXIT_FAILURE;
 
830
        exit(EXIT_FAILURE);
959
831
      } else {
960
 
        exitcode = EXIT_SUCCESS;
961
 
      }
962
 
      goto end;
 
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);
963
861
    }
964
862
    
965
863
    if (not debug){
1039
937
 
1040
938
    if (mc.simple_poll != NULL)
1041
939
        avahi_simple_poll_free(mc.simple_poll);
1042
 
    free(pubkeyfilename);
1043
 
    free(seckeyfilename);
1044
 
 
1045
 
    if (gnutls_initalized){
1046
 
      gnutls_certificate_free_credentials(mc.cred);
1047
 
      gnutls_global_deinit ();
1048
 
    }
 
940
    free(pubkeyfile);
 
941
    free(seckeyfile);
1049
942
    
1050
943
    return exitcode;
1051
944
}