/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:
34
34
 
35
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
36
36
 
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 */
 
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() */
44
60
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
45
 
                                   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 */
46
73
 
 
74
/* Avahi */
 
75
/* All Avahi types, constants and functions
 
76
 Avahi*, avahi_*,
 
77
 AVAHI_* */
47
78
#include <avahi-core/core.h>
48
79
#include <avahi-core/lookup.h>
49
80
#include <avahi-core/log.h>
51
82
#include <avahi-common/malloc.h>
52
83
#include <avahi-common/error.h>
53
84
 
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 */
 
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 */
68
92
 
69
93
/* GPGME */
70
 
#include <errno.h>              /* perror() */
71
 
#include <gpgme.h>
72
 
 
73
 
/* getopt_long */
74
 
#include <getopt.h>
 
94
#include <gpgme.h>              /* All GPGME types, constants and functions
 
95
                                   gpgme_*
 
96
                                   GPGME_PROTOCOL_OpenPGP,
 
97
                                   GPG_ERR_NO_* */
75
98
 
76
99
#define BUFFER_SIZE 256
77
100
 
 
101
bool debug = false;
78
102
static const char *keydir = "/conf/conf.d/mandos";
79
 
static const char *pubkeyfile = "pubkey.txt";
80
 
static const char *seckeyfile = "seckey.txt";
81
 
 
82
 
bool debug = false;
83
 
 
84
 
const char mandos_protocol_version[] = "1";
85
 
 
86
 
/* 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 */
87
108
typedef struct {
88
109
  AvahiSimplePoll *simple_poll;
89
110
  AvahiServer *server;
90
111
  gnutls_certificate_credentials_t cred;
91
112
  unsigned int dh_bits;
 
113
  gnutls_dh_params_t dh_params;
92
114
  const char *priority;
93
115
} mandos_context;
94
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
 */
95
122
size_t adjustbuffer(char **buffer, size_t buffer_length,
96
123
                  size_t buffer_capacity){
97
124
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
202
229
    } else {
203
230
      fprintf(stderr, "Unsupported algorithm: %s\n",
204
231
              result->unsupported_algorithm);
205
 
      fprintf(stderr, "Wrong key usage: %d\n",
 
232
      fprintf(stderr, "Wrong key usage: %u\n",
206
233
              result->wrong_key_usage);
207
234
      if(result->file_name != NULL){
208
235
        fprintf(stderr, "File name: %s\n", result->file_name);
232
259
  
233
260
  *plaintext = NULL;
234
261
  while(true){
235
 
    plaintext_capacity = adjustbuffer(plaintext, (size_t)plaintext_length,
 
262
    plaintext_capacity = adjustbuffer(plaintext,
 
263
                                      (size_t)plaintext_length,
236
264
                                      plaintext_capacity);
237
265
    if (plaintext_capacity == 0){
238
266
        perror("adjustbuffer");
286
314
  fprintf(stderr, "GnuTLS: %s", string);
287
315
}
288
316
 
289
 
static int initgnutls(mandos_context *mc, gnutls_session_t *session,
290
 
                      gnutls_dh_params_t *dh_params){
 
317
static int init_gnutls_global(mandos_context *mc,
 
318
                              const char *pubkeyfile,
 
319
                              const char *seckeyfile){
291
320
  int ret;
292
321
  
293
322
  if(debug){
294
323
    fprintf(stderr, "Initializing GnuTLS\n");
295
324
  }
296
 
 
297
 
  if ((ret = gnutls_global_init ())
298
 
      != GNUTLS_E_SUCCESS) {
 
325
  
 
326
  ret = gnutls_global_init();
 
327
  if (ret != GNUTLS_E_SUCCESS) {
299
328
    fprintf (stderr, "GnuTLS global_init: %s\n",
300
329
             safer_gnutls_strerror(ret));
301
330
    return -1;
310
339
  }
311
340
  
312
341
  /* OpenPGP credentials */
313
 
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
314
 
      != GNUTLS_E_SUCCESS) {
 
342
  gnutls_certificate_allocate_credentials(&mc->cred);
 
343
  if (ret != GNUTLS_E_SUCCESS){
315
344
    fprintf (stderr, "GnuTLS memory error: %s\n",
316
345
             safer_gnutls_strerror(ret));
 
346
    gnutls_global_deinit ();
317
347
    return -1;
318
348
  }
319
349
  
331
361
            " '%s')\n", ret, pubkeyfile, seckeyfile);
332
362
    fprintf(stdout, "The GnuTLS error is: %s\n",
333
363
            safer_gnutls_strerror(ret));
334
 
    return -1;
 
364
    goto globalfail;
335
365
  }
336
366
  
337
367
  /* GnuTLS server initialization */
338
 
  ret = gnutls_dh_params_init(dh_params);
 
368
  ret = gnutls_dh_params_init(&mc->dh_params);
339
369
  if (ret != GNUTLS_E_SUCCESS) {
340
370
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
341
371
             " %s\n", safer_gnutls_strerror(ret));
342
 
    return -1;
 
372
    goto globalfail;
343
373
  }
344
 
  ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
 
374
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
345
375
  if (ret != GNUTLS_E_SUCCESS) {
346
376
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
347
377
             safer_gnutls_strerror(ret));
348
 
    return -1;
 
378
    goto globalfail;
349
379
  }
350
380
  
351
 
  gnutls_certificate_set_dh_params(mc->cred, *dh_params);
352
 
  
 
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;
353
396
  /* GnuTLS session creation */
354
397
  ret = gnutls_init(session, GNUTLS_SERVER);
355
398
  if (ret != GNUTLS_E_SUCCESS){
364
407
      fprintf(stderr, "Syntax error at: %s\n", err);
365
408
      fprintf(stderr, "GnuTLS error: %s\n",
366
409
              safer_gnutls_strerror(ret));
 
410
      gnutls_deinit (*session);
367
411
      return -1;
368
412
    }
369
413
  }
373
417
  if (ret != GNUTLS_E_SUCCESS) {
374
418
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
375
419
            safer_gnutls_strerror(ret));
 
420
    gnutls_deinit (*session);
376
421
    return -1;
377
422
  }
378
423
  
394
439
                                      AvahiIfIndex if_index,
395
440
                                      mandos_context *mc){
396
441
  int ret, tcp_sd;
397
 
  struct sockaddr_in6 to;
 
442
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
398
443
  char *buffer = NULL;
399
444
  char *decrypted_buffer;
400
445
  size_t buffer_length = 0;
404
449
  int retval = 0;
405
450
  char interface[IF_NAMESIZE];
406
451
  gnutls_session_t session;
407
 
  gnutls_dh_params_t dh_params;
408
452
  
409
 
  ret = initgnutls (mc, &session, &dh_params);
 
453
  ret = init_gnutls_session (mc, &session);
410
454
  if (ret != 0){
411
455
    return -1;
412
456
  }
413
457
  
414
458
  if(debug){
415
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
416
 
            ip, port);
 
459
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
 
460
            "\n", ip, port);
417
461
  }
418
462
  
419
463
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
431
475
  }
432
476
  
433
477
  memset(&to,0,sizeof(to));     /* Spurious warning */
434
 
  to.sin6_family = AF_INET6;
 
478
  to.in6.sin6_family = AF_INET6;
435
479
  /* It would be nice to have a way to detect if we were passed an
436
480
     IPv4 address here.   Now we assume an IPv6 address. */
437
 
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
 
481
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
438
482
  if (ret < 0 ){
439
483
    perror("inet_pton");
440
484
    return -1;
443
487
    fprintf(stderr, "Bad address: %s\n", ip);
444
488
    return -1;
445
489
  }
446
 
  to.sin6_port = htons(port);   /* Spurious warning */
 
490
  to.in6.sin6_port = htons(port);       /* Spurious warning */
447
491
  
448
 
  to.sin6_scope_id = (uint32_t)if_index;
 
492
  to.in6.sin6_scope_id = (uint32_t)if_index;
449
493
  
450
494
  if(debug){
451
 
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
 
495
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
496
            port);
452
497
    char addrstr[INET6_ADDRSTRLEN] = "";
453
 
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
 
498
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
454
499
                 sizeof(addrstr)) == NULL){
455
500
      perror("inet_ntop");
456
501
    } else {
460
505
    }
461
506
  }
462
507
  
463
 
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
 
508
  ret = connect(tcp_sd, &to.in, sizeof(to));
464
509
  if (ret < 0){
465
510
    perror("connect");
466
511
    return -1;
495
540
  }
496
541
  
497
542
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
498
 
  
499
 
  ret = gnutls_handshake (session);
 
543
 
 
544
  do{
 
545
    ret = gnutls_handshake (session);
 
546
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
500
547
  
501
548
  if (ret != GNUTLS_E_SUCCESS){
502
549
    if(debug){
515
562
  }
516
563
 
517
564
  while(true){
518
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length, buffer_capacity);
 
565
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
 
566
                                   buffer_capacity);
519
567
    if (buffer_capacity == 0){
520
568
      perror("adjustbuffer");
521
569
      retval = -1;
533
581
      case GNUTLS_E_AGAIN:
534
582
        break;
535
583
      case GNUTLS_E_REHANDSHAKE:
536
 
        ret = gnutls_handshake (session);
 
584
        do{
 
585
          ret = gnutls_handshake (session);
 
586
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
537
587
        if (ret < 0){
538
588
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
539
589
          gnutls_perror (ret);
592
642
  free(buffer);
593
643
  close(tcp_sd);
594
644
  gnutls_deinit (session);
595
 
  gnutls_certificate_free_credentials (mc->cred);
596
 
  gnutls_global_deinit ();
597
645
  return retval;
598
646
}
599
647
 
630
678
      char ip[AVAHI_ADDRESS_STR_MAX];
631
679
      avahi_address_snprint(ip, sizeof(ip), address);
632
680
      if(debug){
633
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
634
 
                " 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);
635
684
      }
636
685
      int ret = start_mandos_communication(ip, port, interface, mc);
637
686
      if (ret == 0){
722
771
    const char *interface = "eth0";
723
772
    struct ifreq network;
724
773
    int sd;
 
774
    uid_t uid;
 
775
    gid_t gid;
725
776
    char *connect_to = NULL;
726
777
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
 
778
    const char *pubkeyfile = "pubkey.txt";
 
779
    const char *seckeyfile = "seckey.txt";
727
780
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
728
781
                          .dh_bits = 1024, .priority = "SECURE256"};
 
782
    bool gnutls_initalized = false;
729
783
    
730
784
    {
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){
 
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;
750
826
          break;
751
 
        }
752
 
      
753
 
        switch(ret){
754
 
        case 0:
 
827
        case 'c':
 
828
          connect_to = arg;
755
829
          break;
756
830
        case 'i':
757
 
          interface = optarg;
758
 
          break;
759
 
        case 'c':
760
 
          connect_to = optarg;
 
831
          interface = arg;
761
832
          break;
762
833
        case 'd':
763
 
          keydir = optarg;
 
834
          keydir = arg;
 
835
          break;
 
836
        case 's':
 
837
          seckeyfile = arg;
764
838
          break;
765
839
        case 'p':
766
 
          pubkeyfile = optarg;
767
 
          break;
768
 
        case 's':
769
 
          seckeyfile = optarg;
770
 
          break;
771
 
        case 'D':
 
840
          pubkeyfile = arg;
 
841
          break;
 
842
        case 129:
772
843
          errno = 0;
773
 
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
 
844
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
774
845
          if (errno){
775
846
            perror("strtol");
776
847
            exit(EXIT_FAILURE);
777
848
          }
778
849
          break;
779
 
        case 'P':
780
 
          mc.priority = optarg;
781
 
          break;
782
 
        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;
783
858
        default:
784
 
          /* getopt_long() has already printed a message about the
785
 
             unrcognized option, so just exit. */
786
 
          exit(EXIT_FAILURE);
 
859
          return ARGP_ERR_UNKNOWN;
787
860
        }
788
 
      }
789
 
      /* Set the global debug flag from the temporary int */
790
 
      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
      }
791
874
    }
792
 
    
 
875
      
793
876
    pubkeyfile = combinepath(keydir, pubkeyfile);
794
877
    if (pubkeyfile == NULL){
795
878
      perror("combinepath");
802
885
      perror("combinepath");
803
886
      goto end;
804
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
    }
805
909
    
806
910
    if_index = (AvahiIfIndex) if_nametoindex(interface);
807
911
    if(if_index == 0){
815
919
      char *address = strrchr(connect_to, ':');
816
920
      if(address == NULL){
817
921
        fprintf(stderr, "No colon in address\n");
818
 
        exit(EXIT_FAILURE);
 
922
        exitcode = EXIT_FAILURE;
 
923
        goto end;
819
924
      }
820
925
      errno = 0;
821
926
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
822
927
      if(errno){
823
928
        perror("Bad port number");
824
 
        exit(EXIT_FAILURE);
 
929
        exitcode = EXIT_FAILURE;
 
930
        goto end;
825
931
      }
826
932
      *address = '\0';
827
933
      address = connect_to;
828
934
      ret = start_mandos_communication(address, port, if_index, &mc);
829
935
      if(ret < 0){
830
 
        exit(EXIT_FAILURE);
 
936
        exitcode = EXIT_FAILURE;
831
937
      } else {
832
 
        exit(EXIT_SUCCESS);
 
938
        exitcode = EXIT_SUCCESS;
833
939
      }
 
940
      goto end;
834
941
    }
835
942
    
836
943
    /* If the interface is down, bring it up */
939
1046
        avahi_simple_poll_free(mc.simple_poll);
940
1047
    free(pubkeyfile);
941
1048
    free(seckeyfile);
 
1049
 
 
1050
    if (gnutls_initalized){
 
1051
      gnutls_certificate_free_credentials(mc.cred);
 
1052
      gnutls_global_deinit ();
 
1053
    }
942
1054
    
943
1055
    return exitcode;
944
1056
}