/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-08-10 16:13:23 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080810161323-wrh1rbpdrq0otuhf
* mandos (console): Define handler globally.
  (main): If in debug mode, also reduce consol log level.  Remove
          console log handler before daemonizing.

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