/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,
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){
 
301
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
302
      != GNUTLS_E_SUCCESS) {
344
303
    fprintf (stderr, "GnuTLS memory error: %s\n",
345
304
             safer_gnutls_strerror(ret));
346
 
    gnutls_global_deinit ();
347
305
    return -1;
348
306
  }
349
307
  
350
308
  if(debug){
351
309
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
352
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
353
 
            seckeyfilename);
 
310
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
311
            seckeyfile);
354
312
  }
355
313
  
356
314
  ret = gnutls_certificate_set_openpgp_key_file
357
 
    (mc->cred, pubkeyfilename, seckeyfilename,
358
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
315
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
359
316
  if (ret != GNUTLS_E_SUCCESS) {
360
317
    fprintf(stderr,
361
318
            "Error[%d] while reading the OpenPGP key pair ('%s',"
362
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
319
            " '%s')\n", ret, pubkeyfile, seckeyfile);
363
320
    fprintf(stdout, "The GnuTLS error is: %s\n",
364
321
            safer_gnutls_strerror(ret));
365
 
    goto globalfail;
 
322
    return -1;
366
323
  }
367
324
  
368
325
  /* GnuTLS server initialization */
369
 
  ret = gnutls_dh_params_init(&mc->dh_params);
 
326
  ret = gnutls_dh_params_init(dh_params);
370
327
  if (ret != GNUTLS_E_SUCCESS) {
371
328
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
372
329
             " %s\n", safer_gnutls_strerror(ret));
373
 
    goto globalfail;
 
330
    return -1;
374
331
  }
375
 
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
 
332
  ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
376
333
  if (ret != GNUTLS_E_SUCCESS) {
377
334
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
378
335
             safer_gnutls_strerror(ret));
379
 
    goto globalfail;
 
336
    return -1;
380
337
  }
381
338
  
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;
 
339
  gnutls_certificate_set_dh_params(mc->cred, *dh_params);
 
340
  
397
341
  /* GnuTLS session creation */
398
342
  ret = gnutls_init(session, GNUTLS_SERVER);
399
343
  if (ret != GNUTLS_E_SUCCESS){
408
352
      fprintf(stderr, "Syntax error at: %s\n", err);
409
353
      fprintf(stderr, "GnuTLS error: %s\n",
410
354
              safer_gnutls_strerror(ret));
411
 
      gnutls_deinit (*session);
412
355
      return -1;
413
356
    }
414
357
  }
418
361
  if (ret != GNUTLS_E_SUCCESS) {
419
362
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
420
363
            safer_gnutls_strerror(ret));
421
 
    gnutls_deinit (*session);
422
364
    return -1;
423
365
  }
424
366
  
440
382
                                      AvahiIfIndex if_index,
441
383
                                      mandos_context *mc){
442
384
  int ret, tcp_sd;
443
 
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
 
385
  struct sockaddr_in6 to;
444
386
  char *buffer = NULL;
445
387
  char *decrypted_buffer;
446
388
  size_t buffer_length = 0;
447
389
  size_t buffer_capacity = 0;
448
390
  ssize_t decrypted_buffer_size;
449
 
  size_t written;
 
391
  size_t written = 0;
450
392
  int retval = 0;
451
393
  char interface[IF_NAMESIZE];
452
394
  gnutls_session_t session;
 
395
  gnutls_dh_params_t dh_params;
453
396
  
454
 
  ret = init_gnutls_session (mc, &session);
 
397
  ret = initgnutls (mc, &session, &dh_params);
455
398
  if (ret != 0){
456
399
    return -1;
457
400
  }
458
401
  
459
402
  if(debug){
460
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
461
 
            "\n", ip, port);
 
403
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
 
404
            ip, port);
462
405
  }
463
406
  
464
407
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
475
418
    fprintf(stderr, "Binding to interface %s\n", interface);
476
419
  }
477
420
  
478
 
  memset(&to, 0, sizeof(to));   /* Spurious warning */
479
 
  to.in6.sin6_family = AF_INET6;
 
421
  memset(&to,0,sizeof(to));     /* Spurious warning */
 
422
  to.sin6_family = AF_INET6;
480
423
  /* It would be nice to have a way to detect if we were passed an
481
424
     IPv4 address here.   Now we assume an IPv6 address. */
482
 
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
 
425
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
483
426
  if (ret < 0 ){
484
427
    perror("inet_pton");
485
428
    return -1;
488
431
    fprintf(stderr, "Bad address: %s\n", ip);
489
432
    return -1;
490
433
  }
491
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
434
  to.sin6_port = htons(port);   /* Spurious warning */
492
435
  
493
 
  to.in6.sin6_scope_id = (uint32_t)if_index;
 
436
  to.sin6_scope_id = (uint32_t)if_index;
494
437
  
495
438
  if(debug){
496
 
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
497
 
            port);
 
439
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
498
440
    char addrstr[INET6_ADDRSTRLEN] = "";
499
 
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
 
441
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
500
442
                 sizeof(addrstr)) == NULL){
501
443
      perror("inet_ntop");
502
444
    } else {
506
448
    }
507
449
  }
508
450
  
509
 
  ret = connect(tcp_sd, &to.in, sizeof(to));
 
451
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
510
452
  if (ret < 0){
511
453
    perror("connect");
512
454
    return -1;
513
455
  }
514
 
 
515
 
  const char *out = mandos_protocol_version;
516
 
  written = 0;
517
 
  while (true){
518
 
    size_t out_size = strlen(out);
519
 
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
520
 
                                   out_size - written));
521
 
    if (ret == -1){
522
 
      perror("write");
523
 
      retval = -1;
524
 
      goto mandos_end;
525
 
    }
526
 
    written += (size_t)ret;
527
 
    if(written < out_size){
528
 
      continue;
529
 
    } else {
530
 
      if (out == mandos_protocol_version){
531
 
        written = 0;
532
 
        out = "\r\n";
533
 
      } else {
534
 
        break;
535
 
      }
536
 
    }
537
 
  }
538
 
 
 
456
  
539
457
  if(debug){
540
458
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
541
459
  }
542
460
  
543
461
  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);
 
462
  
 
463
  ret = gnutls_handshake (session);
548
464
  
549
465
  if (ret != GNUTLS_E_SUCCESS){
550
466
    if(debug){
563
479
  }
564
480
 
565
481
  while(true){
566
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
567
 
                                   buffer_capacity);
568
 
    if (buffer_capacity == 0){
569
 
      perror("adjustbuffer");
570
 
      retval = -1;
571
 
      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;
572
490
    }
573
491
    
574
492
    ret = gnutls_record_recv(session, buffer+buffer_length,
582
500
      case GNUTLS_E_AGAIN:
583
501
        break;
584
502
      case GNUTLS_E_REHANDSHAKE:
585
 
        do{
586
 
          ret = gnutls_handshake (session);
587
 
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
503
        ret = gnutls_handshake (session);
588
504
        if (ret < 0){
589
505
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
590
506
          gnutls_perror (ret);
616
532
                                               &decrypted_buffer,
617
533
                                               keydir);
618
534
    if (decrypted_buffer_size >= 0){
619
 
      written = 0;
620
535
      while(written < (size_t) decrypted_buffer_size){
621
536
        ret = (int)fwrite (decrypted_buffer + written, 1,
622
537
                           (size_t)decrypted_buffer_size - written,
643
558
  free(buffer);
644
559
  close(tcp_sd);
645
560
  gnutls_deinit (session);
 
561
  gnutls_certificate_free_credentials (mc->cred);
 
562
  gnutls_global_deinit ();
646
563
  return retval;
647
564
}
648
565
 
679
596
      char ip[AVAHI_ADDRESS_STR_MAX];
680
597
      avahi_address_snprint(ip, sizeof(ip), address);
681
598
      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);
 
599
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
 
600
                " port %d\n", name, host_name, ip, interface, port);
685
601
      }
686
602
      int ret = start_mandos_communication(ip, port, interface, mc);
687
603
      if (ret == 0){
688
 
        avahi_simple_poll_quit(mc->simple_poll);
 
604
        exit(EXIT_SUCCESS);
689
605
      }
690
606
    }
691
607
  }
745
661
 
746
662
/* Combines file name and path and returns the malloced new
747
663
   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){
 
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){
752
669
    return NULL;
753
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';
754
679
  return tmp;
755
680
}
756
681
 
763
688
    const char *interface = "eth0";
764
689
    struct ifreq network;
765
690
    int sd;
766
 
    uid_t uid;
767
 
    gid_t gid;
768
691
    char *connect_to = NULL;
769
692
    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
693
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
775
694
                          .dh_bits = 1024, .priority = "SECURE256"};
776
 
    bool gnutls_initalized = false;
777
695
    
778
696
    {
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;
 
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;
820
724
          break;
821
725
        case 'c':
822
 
          connect_to = arg;
823
 
          break;
824
 
        case 'i':
825
 
          interface = arg;
 
726
          connect_to = optarg;
826
727
          break;
827
728
        case 'd':
828
 
          keydir = arg;
 
729
          keydir = optarg;
 
730
          break;
 
731
        case 'p':
 
732
          pubkeyfile = optarg;
829
733
          break;
830
734
        case 's':
831
 
          seckeyname = arg;
832
 
          break;
833
 
        case 'p':
834
 
          pubkeyname = arg;
835
 
          break;
836
 
        case 129:
 
735
          seckeyfile = optarg;
 
736
          break;
 
737
        case 'D':
837
738
          errno = 0;
838
 
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
 
739
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
839
740
          if (errno){
840
741
            perror("strtol");
841
742
            exit(EXIT_FAILURE);
842
743
          }
843
744
          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;
 
745
        case 'P':
 
746
          mc.priority = optarg;
 
747
          break;
 
748
        case '?':
851
749
        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");
 
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;
930
770
    }
931
771
    
932
772
    if_index = (AvahiIfIndex) if_nametoindex(interface);
941
781
      char *address = strrchr(connect_to, ':');
942
782
      if(address == NULL){
943
783
        fprintf(stderr, "No colon in address\n");
944
 
        exitcode = EXIT_FAILURE;
945
 
        goto end;
 
784
        exit(EXIT_FAILURE);
946
785
      }
947
786
      errno = 0;
948
787
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
949
788
      if(errno){
950
789
        perror("Bad port number");
951
 
        exitcode = EXIT_FAILURE;
952
 
        goto end;
 
790
        exit(EXIT_FAILURE);
953
791
      }
954
792
      *address = '\0';
955
793
      address = connect_to;
956
794
      ret = start_mandos_communication(address, port, if_index, &mc);
957
795
      if(ret < 0){
958
 
        exitcode = EXIT_FAILURE;
 
796
        exit(EXIT_FAILURE);
959
797
      } else {
960
 
        exitcode = EXIT_SUCCESS;
961
 
      }
962
 
      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);
963
827
    }
964
828
    
965
829
    if (not debug){
1039
903
 
1040
904
    if (mc.simple_poll != NULL)
1041
905
        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
 
    }
 
906
    free(pubkeyfile);
 
907
    free(seckeyfile);
1049
908
    
1050
909
    return exitcode;
1051
910
}