/mandos/trunk

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

« back to all changes in this revision

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

Added configuration files support for mandos-client
Removed plus argument support for mandos-client

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#define _LARGEFILE_SOURCE
33
33
#define _FILE_OFFSET_BITS 64
34
34
 
35
 
#include <stdio.h>
36
 
#include <assert.h>
37
 
#include <stdlib.h>
38
 
#include <time.h>
39
 
#include <net/if.h>             /* if_nametoindex */
40
 
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
41
 
                                   SIOCSIFFLAGS */
 
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
 
36
 
 
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(), stdout,
 
38
                                   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(), memcpy(), strcpy() */
 
46
#include <sys/ioctl.h>          /* ioctl */
 
47
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
 
48
                                   sockaddr_in6, PF_INET6,
 
49
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
 
50
                                   uid_t, gid_t */
 
51
#include <inttypes.h>           /* PRIu16 */
 
52
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
 
53
                                   struct in6_addr, inet_pton(),
 
54
                                   connect() */
 
55
#include <assert.h>             /* assert() */
 
56
#include <errno.h>              /* perror(), errno */
 
57
#include <time.h>               /* time() */
42
58
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
43
 
                                   SIOCSIFFLAGS */
 
59
                                   SIOCSIFFLAGS, if_indextoname(),
 
60
                                   if_nametoindex(), IF_NAMESIZE */
 
61
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
 
62
                                   getuid(), getgid(), setuid(),
 
63
                                   setgid() */
 
64
#include <netinet/in.h>
 
65
#include <arpa/inet.h>          /* inet_pton(), htons */
 
66
#include <iso646.h>             /* not, and */
 
67
#include <argp.h>               /* struct argp_option, error_t, struct
 
68
                                   argp_state, struct argp,
 
69
                                   argp_parse(), ARGP_KEY_ARG,
 
70
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
44
71
 
 
72
/* Avahi */
 
73
/* All Avahi types, constants and functions
 
74
 Avahi*, avahi_*,
 
75
 AVAHI_* */
45
76
#include <avahi-core/core.h>
46
77
#include <avahi-core/lookup.h>
47
78
#include <avahi-core/log.h>
49
80
#include <avahi-common/malloc.h>
50
81
#include <avahi-common/error.h>
51
82
 
52
 
/* Mandos client part */
53
 
#include <sys/types.h>          /* socket(), inet_pton() */
54
 
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
55
 
                                   struct in6_addr, inet_pton() */
56
 
#include <gnutls/gnutls.h>      /* All GnuTLS stuff */
57
 
#include <gnutls/openpgp.h>     /* GnuTLS with openpgp stuff */
58
 
 
59
 
#include <unistd.h>             /* close() */
60
 
#include <netinet/in.h>
61
 
#include <stdbool.h>            /* true */
62
 
#include <string.h>             /* memset */
63
 
#include <arpa/inet.h>          /* inet_pton() */
64
 
#include <iso646.h>             /* not */
65
 
#include <net/if.h>             /* IF_NAMESIZE */
 
83
/* GnuTLS */
 
84
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and functions
 
85
                                   gnutls_*
 
86
                                   init_gnutls_session(),
 
87
                                   GNUTLS_* */
 
88
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
 
89
                                   GNUTLS_OPENPGP_FMT_BASE64 */
66
90
 
67
91
/* GPGME */
68
 
#include <errno.h>              /* perror() */
69
 
#include <gpgme.h>
70
 
 
71
 
/* getopt_long */
72
 
#include <getopt.h>
 
92
#include <gpgme.h>              /* All GPGME types, constants and functions
 
93
                                   gpgme_*
 
94
                                   GPGME_PROTOCOL_OpenPGP,
 
95
                                   GPG_ERR_NO_* */
73
96
 
74
97
#define BUFFER_SIZE 256
75
98
 
 
99
bool debug = false;
76
100
static const char *keydir = "/conf/conf.d/mandos";
77
 
static const char *pubkeyfile = "pubkey.txt";
78
 
static const char *seckeyfile = "seckey.txt";
79
 
 
80
 
bool debug = false;
81
 
 
82
 
/* Used for passing in values through all the callback functions */
 
101
static const char mandos_protocol_version[] = "1";
 
102
const char *argp_program_version = "password-request 1.0";
 
103
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
104
 
 
105
/* Used for passing in values through the Avahi callback functions */
83
106
typedef struct {
84
107
  AvahiSimplePoll *simple_poll;
85
108
  AvahiServer *server;
86
109
  gnutls_certificate_credentials_t cred;
87
110
  unsigned int dh_bits;
 
111
  gnutls_dh_params_t dh_params;
88
112
  const char *priority;
89
113
} mandos_context;
90
114
 
 
115
/*
 
116
 * Make room in "buffer" for at least BUFFER_SIZE additional bytes.
 
117
 * "buffer_capacity" is how much is currently allocated,
 
118
 * "buffer_length" is how much is already used.
 
119
 */
 
120
size_t adjustbuffer(char **buffer, size_t buffer_length,
 
121
                  size_t buffer_capacity){
 
122
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
 
123
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
 
124
    if (buffer == NULL){
 
125
      return 0;
 
126
    }
 
127
    buffer_capacity += BUFFER_SIZE;
 
128
  }
 
129
  return buffer_capacity;
 
130
}
 
131
 
91
132
/* 
92
133
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
93
134
 * Returns -1 on error
186
227
    } else {
187
228
      fprintf(stderr, "Unsupported algorithm: %s\n",
188
229
              result->unsupported_algorithm);
189
 
      fprintf(stderr, "Wrong key usage: %d\n",
 
230
      fprintf(stderr, "Wrong key usage: %u\n",
190
231
              result->wrong_key_usage);
191
232
      if(result->file_name != NULL){
192
233
        fprintf(stderr, "File name: %s\n", result->file_name);
216
257
  
217
258
  *plaintext = NULL;
218
259
  while(true){
219
 
    if (plaintext_length + BUFFER_SIZE
220
 
        > (ssize_t) plaintext_capacity){
221
 
      *plaintext = realloc(*plaintext, plaintext_capacity
222
 
                            + BUFFER_SIZE);
223
 
      if (*plaintext == NULL){
224
 
        perror("realloc");
 
260
    plaintext_capacity = adjustbuffer(plaintext,
 
261
                                      (size_t)plaintext_length,
 
262
                                      plaintext_capacity);
 
263
    if (plaintext_capacity == 0){
 
264
        perror("adjustbuffer");
225
265
        plaintext_length = -1;
226
266
        goto decrypt_end;
227
 
      }
228
 
      plaintext_capacity += BUFFER_SIZE;
229
267
    }
230
268
    
231
269
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
274
312
  fprintf(stderr, "GnuTLS: %s", string);
275
313
}
276
314
 
277
 
static int initgnutls(mandos_context *mc, gnutls_session_t *session,
278
 
                      gnutls_dh_params_t *dh_params){
 
315
static int init_gnutls_global(mandos_context *mc,
 
316
                              const char *pubkeyfile,
 
317
                              const char *seckeyfile){
279
318
  int ret;
280
319
  
281
320
  if(debug){
282
321
    fprintf(stderr, "Initializing GnuTLS\n");
283
322
  }
284
 
 
285
 
  if ((ret = gnutls_global_init ())
286
 
      != GNUTLS_E_SUCCESS) {
 
323
  
 
324
  ret = gnutls_global_init();
 
325
  if (ret != GNUTLS_E_SUCCESS) {
287
326
    fprintf (stderr, "GnuTLS global_init: %s\n",
288
327
             safer_gnutls_strerror(ret));
289
328
    return -1;
298
337
  }
299
338
  
300
339
  /* OpenPGP credentials */
301
 
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
302
 
      != GNUTLS_E_SUCCESS) {
 
340
  gnutls_certificate_allocate_credentials(&mc->cred);
 
341
  if (ret != GNUTLS_E_SUCCESS){
303
342
    fprintf (stderr, "GnuTLS memory error: %s\n",
304
343
             safer_gnutls_strerror(ret));
 
344
    gnutls_global_deinit ();
305
345
    return -1;
306
346
  }
307
347
  
319
359
            " '%s')\n", ret, pubkeyfile, seckeyfile);
320
360
    fprintf(stdout, "The GnuTLS error is: %s\n",
321
361
            safer_gnutls_strerror(ret));
322
 
    return -1;
 
362
    goto globalfail;
323
363
  }
324
364
  
325
365
  /* GnuTLS server initialization */
326
 
  ret = gnutls_dh_params_init(dh_params);
 
366
  ret = gnutls_dh_params_init(&mc->dh_params);
327
367
  if (ret != GNUTLS_E_SUCCESS) {
328
368
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
329
369
             " %s\n", safer_gnutls_strerror(ret));
330
 
    return -1;
 
370
    goto globalfail;
331
371
  }
332
 
  ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
 
372
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
333
373
  if (ret != GNUTLS_E_SUCCESS) {
334
374
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
335
375
             safer_gnutls_strerror(ret));
336
 
    return -1;
 
376
    goto globalfail;
337
377
  }
338
378
  
339
 
  gnutls_certificate_set_dh_params(mc->cred, *dh_params);
340
 
  
 
379
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
 
380
 
 
381
  return 0;
 
382
 
 
383
 globalfail:
 
384
 
 
385
  gnutls_certificate_free_credentials(mc->cred);
 
386
  gnutls_global_deinit();
 
387
  return -1;
 
388
 
 
389
}
 
390
 
 
391
static int init_gnutls_session(mandos_context *mc,
 
392
                               gnutls_session_t *session){
 
393
  int ret;
341
394
  /* GnuTLS session creation */
342
395
  ret = gnutls_init(session, GNUTLS_SERVER);
343
396
  if (ret != GNUTLS_E_SUCCESS){
352
405
      fprintf(stderr, "Syntax error at: %s\n", err);
353
406
      fprintf(stderr, "GnuTLS error: %s\n",
354
407
              safer_gnutls_strerror(ret));
 
408
      gnutls_deinit (*session);
355
409
      return -1;
356
410
    }
357
411
  }
361
415
  if (ret != GNUTLS_E_SUCCESS) {
362
416
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
363
417
            safer_gnutls_strerror(ret));
 
418
    gnutls_deinit (*session);
364
419
    return -1;
365
420
  }
366
421
  
382
437
                                      AvahiIfIndex if_index,
383
438
                                      mandos_context *mc){
384
439
  int ret, tcp_sd;
385
 
  struct sockaddr_in6 to;
 
440
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
386
441
  char *buffer = NULL;
387
442
  char *decrypted_buffer;
388
443
  size_t buffer_length = 0;
389
444
  size_t buffer_capacity = 0;
390
445
  ssize_t decrypted_buffer_size;
391
 
  size_t written = 0;
 
446
  size_t written;
392
447
  int retval = 0;
393
448
  char interface[IF_NAMESIZE];
394
449
  gnutls_session_t session;
395
 
  gnutls_dh_params_t dh_params;
396
450
  
397
 
  ret = initgnutls (mc, &session, &dh_params);
 
451
  ret = init_gnutls_session (mc, &session);
398
452
  if (ret != 0){
399
453
    return -1;
400
454
  }
401
455
  
402
456
  if(debug){
403
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
404
 
            ip, port);
 
457
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
 
458
            "\n", ip, port);
405
459
  }
406
460
  
407
461
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
419
473
  }
420
474
  
421
475
  memset(&to,0,sizeof(to));     /* Spurious warning */
422
 
  to.sin6_family = AF_INET6;
 
476
  to.in6.sin6_family = AF_INET6;
423
477
  /* It would be nice to have a way to detect if we were passed an
424
478
     IPv4 address here.   Now we assume an IPv6 address. */
425
 
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
 
479
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
426
480
  if (ret < 0 ){
427
481
    perror("inet_pton");
428
482
    return -1;
431
485
    fprintf(stderr, "Bad address: %s\n", ip);
432
486
    return -1;
433
487
  }
434
 
  to.sin6_port = htons(port);   /* Spurious warning */
 
488
  to.in6.sin6_port = htons(port); /* Spurious warning */
435
489
  
436
 
  to.sin6_scope_id = (uint32_t)if_index;
 
490
  to.in6.sin6_scope_id = (uint32_t)if_index;
437
491
  
438
492
  if(debug){
439
 
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
 
493
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
494
            port);
440
495
    char addrstr[INET6_ADDRSTRLEN] = "";
441
 
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
 
496
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
442
497
                 sizeof(addrstr)) == NULL){
443
498
      perror("inet_ntop");
444
499
    } else {
448
503
    }
449
504
  }
450
505
  
451
 
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
 
506
  ret = connect(tcp_sd, &to.in, sizeof(to));
452
507
  if (ret < 0){
453
508
    perror("connect");
454
509
    return -1;
455
510
  }
456
 
  
 
511
 
 
512
  const char *out = mandos_protocol_version;
 
513
  written = 0;
 
514
  while (true){
 
515
    size_t out_size = strlen(out);
 
516
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
 
517
                                   out_size - written));
 
518
    if (ret == -1){
 
519
      perror("write");
 
520
      retval = -1;
 
521
      goto mandos_end;
 
522
    }
 
523
    written += (size_t)ret;
 
524
    if(written < out_size){
 
525
      continue;
 
526
    } else {
 
527
      if (out == mandos_protocol_version){
 
528
        written = 0;
 
529
        out = "\r\n";
 
530
      } else {
 
531
        break;
 
532
      }
 
533
    }
 
534
  }
 
535
 
457
536
  if(debug){
458
537
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
459
538
  }
460
539
  
461
540
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
462
 
  
463
 
  ret = gnutls_handshake (session);
 
541
 
 
542
  do{
 
543
    ret = gnutls_handshake (session);
 
544
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
464
545
  
465
546
  if (ret != GNUTLS_E_SUCCESS){
466
547
    if(debug){
479
560
  }
480
561
 
481
562
  while(true){
482
 
    if (buffer_length + BUFFER_SIZE > buffer_capacity){
483
 
      buffer = realloc(buffer, buffer_capacity + BUFFER_SIZE);
484
 
      if (buffer == NULL){
485
 
        perror("realloc");
486
 
        retval = -1;
487
 
        goto mandos_end;
488
 
      }
489
 
      buffer_capacity += BUFFER_SIZE;
 
563
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
 
564
                                   buffer_capacity);
 
565
    if (buffer_capacity == 0){
 
566
      perror("adjustbuffer");
 
567
      retval = -1;
 
568
      goto mandos_end;
490
569
    }
491
570
    
492
571
    ret = gnutls_record_recv(session, buffer+buffer_length,
500
579
      case GNUTLS_E_AGAIN:
501
580
        break;
502
581
      case GNUTLS_E_REHANDSHAKE:
503
 
        ret = gnutls_handshake (session);
 
582
        do{
 
583
          ret = gnutls_handshake (session);
 
584
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
504
585
        if (ret < 0){
505
586
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
506
587
          gnutls_perror (ret);
532
613
                                               &decrypted_buffer,
533
614
                                               keydir);
534
615
    if (decrypted_buffer_size >= 0){
 
616
      written = 0;
535
617
      while(written < (size_t) decrypted_buffer_size){
536
618
        ret = (int)fwrite (decrypted_buffer + written, 1,
537
619
                           (size_t)decrypted_buffer_size - written,
558
640
  free(buffer);
559
641
  close(tcp_sd);
560
642
  gnutls_deinit (session);
561
 
  gnutls_certificate_free_credentials (mc->cred);
562
 
  gnutls_global_deinit ();
563
643
  return retval;
564
644
}
565
645
 
596
676
      char ip[AVAHI_ADDRESS_STR_MAX];
597
677
      avahi_address_snprint(ip, sizeof(ip), address);
598
678
      if(debug){
599
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
600
 
                " port %d\n", name, host_name, ip, interface, port);
 
679
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
680
                PRIu16 ") on port %d\n", name, host_name, ip,
 
681
                interface, port);
601
682
      }
602
683
      int ret = start_mandos_communication(ip, port, interface, mc);
603
684
      if (ret == 0){
604
 
        exit(EXIT_SUCCESS);
 
685
        avahi_simple_poll_quit(mc->simple_poll);
605
686
      }
606
687
    }
607
688
  }
688
769
    const char *interface = "eth0";
689
770
    struct ifreq network;
690
771
    int sd;
 
772
    uid_t uid;
 
773
    gid_t gid;
691
774
    char *connect_to = NULL;
692
775
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
 
776
    const char *pubkeyfile = "pubkey.txt";
 
777
    const char *seckeyfile = "seckey.txt";
693
778
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
694
779
                          .dh_bits = 1024, .priority = "SECURE256"};
 
780
    bool gnutls_initalized = false;
695
781
    
696
782
    {
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){
 
783
      struct argp_option options[] = {
 
784
        { .name = "debug", .key = 128,
 
785
          .doc = "Debug mode", .group = 3 },
 
786
        { .name = "connect", .key = 'c',
 
787
          .arg = "IP",
 
788
          .doc = "Connect directly to a sepcified mandos server",
 
789
          .group = 1 },
 
790
        { .name = "interface", .key = 'i',
 
791
          .arg = "INTERFACE",
 
792
          .doc = "Interface that Avahi will conntect through",
 
793
          .group = 1 },
 
794
        { .name = "keydir", .key = 'd',
 
795
          .arg = "KEYDIR",
 
796
          .doc = "Directory where the openpgp keyring is",
 
797
          .group = 1 },
 
798
        { .name = "seckey", .key = 's',
 
799
          .arg = "SECKEY",
 
800
          .doc = "Secret openpgp key for gnutls authentication",
 
801
          .group = 1 },
 
802
        { .name = "pubkey", .key = 'p',
 
803
          .arg = "PUBKEY",
 
804
          .doc = "Public openpgp key for gnutls authentication",
 
805
          .group = 2 },
 
806
        { .name = "dh-bits", .key = 129,
 
807
          .arg = "BITS",
 
808
          .doc = "dh-bits to use in gnutls communication",
 
809
          .group = 2 },
 
810
        { .name = "priority", .key = 130,
 
811
          .arg = "PRIORITY",
 
812
          .doc = "GNUTLS priority", .group = 1 },
 
813
        { .name = NULL }
 
814
      };
 
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:
 
823
          debug = true;
716
824
          break;
717
 
        }
718
 
      
719
 
        switch(ret){
720
 
        case 0:
 
825
        case 'c':
 
826
          connect_to = arg;
721
827
          break;
722
828
        case 'i':
723
 
          interface = optarg;
724
 
          break;
725
 
        case 'c':
726
 
          connect_to = optarg;
 
829
          interface = arg;
727
830
          break;
728
831
        case 'd':
729
 
          keydir = optarg;
 
832
          keydir = arg;
 
833
          break;
 
834
        case 's':
 
835
          seckeyfile = arg;
730
836
          break;
731
837
        case 'p':
732
 
          pubkeyfile = optarg;
733
 
          break;
734
 
        case 's':
735
 
          seckeyfile = optarg;
736
 
          break;
737
 
        case 'D':
 
838
          pubkeyfile = arg;
 
839
          break;
 
840
        case 129:
738
841
          errno = 0;
739
 
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
 
842
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
740
843
          if (errno){
741
844
            perror("strtol");
742
845
            exit(EXIT_FAILURE);
743
846
          }
744
847
          break;
745
 
        case 'P':
746
 
          mc.priority = optarg;
747
 
          break;
748
 
        case '?':
 
848
        case 130:
 
849
          mc.priority = arg;
 
850
          break;
 
851
        case ARGP_KEY_ARG:
 
852
          argp_usage (state);
 
853
        case ARGP_KEY_END:
 
854
          break;
749
855
        default:
750
 
          /* getopt_long() has already printed a message about the
751
 
             unrcognized option, so just exit. */
752
 
          exit(EXIT_FAILURE);
 
856
          return ARGP_ERR_UNKNOWN;
753
857
        }
754
 
      }
755
 
      /* Set the global debug flag from the temporary int */
756
 
      debug = debug_int ? true : false;
 
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 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
      }
757
871
    }
758
 
    
 
872
      
759
873
    pubkeyfile = combinepath(keydir, pubkeyfile);
760
874
    if (pubkeyfile == NULL){
761
875
      perror("combinepath");
766
880
    seckeyfile = combinepath(keydir, seckeyfile);
767
881
    if (seckeyfile == NULL){
768
882
      perror("combinepath");
769
 
      goto end;
 
883
      exitcode = EXIT_FAILURE;
 
884
      goto end;
 
885
    }
 
886
 
 
887
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
 
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); /* Spurious warning */
 
905
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
906
      if(ret == -1){
 
907
        perror("ioctl SIOCGIFFLAGS");
 
908
        exitcode = EXIT_FAILURE;
 
909
        goto end;
 
910
      }
 
911
      if((network.ifr_flags & IFF_UP) == 0){
 
912
        network.ifr_flags |= IFF_UP;
 
913
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
914
        if(ret == -1){
 
915
          perror("ioctl SIOCSIFFLAGS");
 
916
          exitcode = EXIT_FAILURE;
 
917
          goto end;
 
918
        }
 
919
      }
 
920
      close(sd);
 
921
    }
 
922
    
 
923
    uid = getuid();
 
924
    gid = getgid();
 
925
    
 
926
    ret = setuid(uid);
 
927
    if (ret == -1){
 
928
      perror("setuid");
 
929
    }
 
930
    
 
931
    setgid(gid);
 
932
    if (ret == -1){
 
933
      perror("setgid");
770
934
    }
771
935
    
772
936
    if_index = (AvahiIfIndex) if_nametoindex(interface);
781
945
      char *address = strrchr(connect_to, ':');
782
946
      if(address == NULL){
783
947
        fprintf(stderr, "No colon in address\n");
784
 
        exit(EXIT_FAILURE);
 
948
        exitcode = EXIT_FAILURE;
 
949
        goto end;
785
950
      }
786
951
      errno = 0;
787
952
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
788
953
      if(errno){
789
954
        perror("Bad port number");
790
 
        exit(EXIT_FAILURE);
 
955
        exitcode = EXIT_FAILURE;
 
956
        goto end;
791
957
      }
792
958
      *address = '\0';
793
959
      address = connect_to;
794
960
      ret = start_mandos_communication(address, port, if_index, &mc);
795
961
      if(ret < 0){
796
 
        exit(EXIT_FAILURE);
 
962
        exitcode = EXIT_FAILURE;
797
963
      } else {
798
 
        exit(EXIT_SUCCESS);
799
 
      }
800
 
    }
801
 
    
802
 
    /* If the interface is down, bring it up */
803
 
    {
804
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
805
 
      if(sd < 0) {
806
 
        perror("socket");
807
 
        exitcode = EXIT_FAILURE;
808
 
        goto end;
809
 
      }
810
 
      strcpy(network.ifr_name, interface); /* Spurious warning */
811
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
812
 
      if(ret == -1){
813
 
        perror("ioctl SIOCGIFFLAGS");
814
 
        exitcode = EXIT_FAILURE;
815
 
        goto end;
816
 
      }
817
 
      if((network.ifr_flags & IFF_UP) == 0){
818
 
        network.ifr_flags |= IFF_UP;
819
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
820
 
        if(ret == -1){
821
 
          perror("ioctl SIOCSIFFLAGS");
822
 
          exitcode = EXIT_FAILURE;
823
 
          goto end;
824
 
        }
825
 
      }
826
 
      close(sd);
 
964
        exitcode = EXIT_SUCCESS;
 
965
      }
 
966
      goto end;
827
967
    }
828
968
    
829
969
    if (not debug){
905
1045
        avahi_simple_poll_free(mc.simple_poll);
906
1046
    free(pubkeyfile);
907
1047
    free(seckeyfile);
 
1048
 
 
1049
    if (gnutls_initalized){
 
1050
      gnutls_certificate_free_credentials(mc.cred);
 
1051
      gnutls_global_deinit ();
 
1052
    }
908
1053
    
909
1054
    return exitcode;
910
1055
}