/mandos/trunk

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

« back to all changes in this revision

Viewing changes to plugins.d/mandosclient.c

  • 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() */
36
 
 
37
 
#include <stdio.h>              /* fprintf(), stderr, fwrite(), stdout, ferror() */
38
 
#include <stdint.h>             /* uint16_t, uint32_t */
39
 
#include <stddef.h>             /* NULL, size_t, ssize_t */
40
 
#include <stdlib.h>             /* free() */
41
 
#include <stdbool.h>            /* bool, true */
42
 
#include <string.h>             /* memset(), strcmp(), strlen, strerror() */
43
 
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
44
 
                                   SIOCSIFFLAGS */
45
 
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
46
 
                                   sockaddr_in6, PF_INET6, SOCK_STREAM, INET6_ADDRSTRLEN */
47
 
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
48
 
                                   struct in6_addr, inet_pton(),
49
 
                                   connect() */
 
35
#include <stdio.h>
50
36
#include <assert.h>
51
 
#include <errno.h>              /* perror() */
 
37
#include <stdlib.h>
52
38
#include <time.h>
 
39
#include <net/if.h>             /* if_nametoindex */
 
40
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
 
41
                                   SIOCSIFFLAGS */
53
42
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
54
 
                                   SIOCSIFFLAGS, if_indextoname(),
55
 
                                   if_nametoindex(), IF_NAMESIZE */
56
 
#include <unistd.h>             /* close(), SEEK_SET, off_t, write()*/
57
 
#include <netinet/in.h>
58
 
#include <arpa/inet.h>          /* inet_pton(), htons */
59
 
#include <iso646.h>             /* not */
60
 
#include <argp.h>               /* struct argp_option,
61
 
                                   struct argp_state, struct argp,
62
 
                                   argp_parse() */
 
43
                                   SIOCSIFFLAGS */
63
44
 
64
 
/* Avahi */
65
 
#include <avahi-core/core.h>    /* AvahiSimplePoll, AvahiServer,
66
 
                                   AvahiIfIndex */
 
45
#include <avahi-core/core.h>
67
46
#include <avahi-core/lookup.h>
68
 
#include <avahi-core/log.h>     /* AvahiLogLevel */
 
47
#include <avahi-core/log.h>
69
48
#include <avahi-common/simple-watch.h>
70
49
#include <avahi-common/malloc.h>
71
50
#include <avahi-common/error.h>
72
51
 
73
 
/* GnuTLS */
74
 
#include <gnutls/gnutls.h>      /* gnutls_certificate_credentials_t,
75
 
                                   gnutls_dh_params_t,
76
 
                                   gnutls_strerror(),
77
 
                                   gnutls_global_init(),
78
 
                                   gnutls_global_set_log_level(),
79
 
                                   gnutls_global_set_log_function(),
80
 
                                   gnutls_certificate_allocate_credentials(),
81
 
                                   gnutls_global_deinit(),
82
 
                                   gnutls_dh_params_init(),
83
 
                                   gnutls_dh_params_generate(),
84
 
                                   gnutls_certificate_set_dh_params(),
85
 
                                   gnutls_certificate_free_credentials(),
86
 
                                   gnutls_session_t, gnutls_init(),
87
 
                                   gnutls_priority_set_direct(),
88
 
                                   gnutls_deinit(),
89
 
                                   gnutls_credentials_set(),
90
 
                                   gnutls_certificate_server_set_request(),
91
 
                                   gnutls_dh_set_prime_bits(),
92
 
                                   gnutls_transport_set_ptr(),
93
 
                                   gnutls_transport_ptr_t,
94
 
                                   gnutls_handshake(),
95
 
                                   gnutls_record_recv()
96
 
                                   gnutls_perror(), gnutls_bye(),
97
 
                                   init_gnutls_session(),
98
 
                                   GNUTLS_E_SUCCESS,
99
 
                                   GNUTLS_CRD_CERTIFICATE,
100
 
                                   GNUTLS_CERT_IGNORE,
101
 
                                   GNUTLS_E_INTERRUPTED,
102
 
                                   GNUTLS_E_AGAIN,
103
 
                                   GNUTLS_E_REHANDSHAKE,
104
 
                                   GNUTLS_SHUT_RDWR, */
105
 
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
106
 
                               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 */
107
66
 
108
67
/* GPGME */
109
 
#include <gpgme.h>              /* gpgme_data_t, gpgme_ctx_t,
110
 
                                   gpgme_error_t, gpgme_engine_info_t,
111
 
                                   gpgme_check_version(),
112
 
                                   gpgme_engine_check_version(),
113
 
                                   gpgme_strsource(),
114
 
                                   gpgme_strerror(),
115
 
                                   gpgme_get_engine_info(),
116
 
                                   gpgme_set_engine_info(),
117
 
                                   gpgme_data_new_from_mem(),
118
 
                                   gpgme_data_new(), gpgme_new(),
119
 
                                   gpgme_op_decrypt(),
120
 
                                   gpgme_decrypt_result_t,
121
 
                                   gpgme_op_decrypt_result(),
122
 
                                   gpgme_recipient_t,
123
 
                                   gpgme_pubkey_algo_name(),
124
 
                                   gpgme_data_seek(),
125
 
                                   gpgme_data_read(),
126
 
                                   gpgme_data_release()
127
 
                                   GPGME_PROTOCOL_OpenPGP,
128
 
                                   GPG_ERR_NO_ERROR,
129
 
                                   GPG_ERR_NO_SECKEY, */
 
68
#include <errno.h>              /* perror() */
 
69
#include <gpgme.h>
 
70
 
 
71
/* getopt_long */
 
72
#include <getopt.h>
130
73
 
131
74
#define BUFFER_SIZE 256
132
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
 
133
80
bool debug = false;
134
 
static const char *keydir = "/conf/conf.d/mandos";
135
 
static const char mandos_protocol_version[] = "1";
136
 
const char *argp_program_version = "mandosclient 0.9";
137
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
138
81
 
139
 
/* Used for passing in values through the Avahi callback functions */
 
82
/* Used for passing in values through all the callback functions */
140
83
typedef struct {
141
84
  AvahiSimplePoll *simple_poll;
142
85
  AvahiServer *server;
143
86
  gnutls_certificate_credentials_t cred;
144
87
  unsigned int dh_bits;
145
 
  gnutls_dh_params_t dh_params;
146
88
  const char *priority;
147
89
} mandos_context;
148
90
 
149
 
/*
150
 
 * Make room in "buffer" for at least BUFFER_SIZE additional bytes.
151
 
 * "buffer_capacity" is how much is currently allocated,
152
 
 * "buffer_length" is how much is already used.
153
 
 */
154
 
size_t adjustbuffer(char **buffer, size_t buffer_length,
155
 
                  size_t buffer_capacity){
156
 
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
157
 
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
158
 
    if (buffer == NULL){
159
 
      return 0;
160
 
    }
161
 
    buffer_capacity += BUFFER_SIZE;
162
 
  }
163
 
  return buffer_capacity;
164
 
}
165
 
 
166
91
/* 
167
92
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
168
93
 * Returns -1 on error
291
216
  
292
217
  *plaintext = NULL;
293
218
  while(true){
294
 
    plaintext_capacity = adjustbuffer(plaintext,
295
 
                                      (size_t)plaintext_length,
296
 
                                      plaintext_capacity);
297
 
    if (plaintext_capacity == 0){
298
 
        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");
299
225
        plaintext_length = -1;
300
226
        goto decrypt_end;
 
227
      }
 
228
      plaintext_capacity += BUFFER_SIZE;
301
229
    }
302
230
    
303
231
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
346
274
  fprintf(stderr, "GnuTLS: %s", string);
347
275
}
348
276
 
349
 
static int init_gnutls_global(mandos_context *mc,
350
 
                              const char *pubkeyfile,
351
 
                              const char *seckeyfile){
 
277
static int initgnutls(mandos_context *mc, gnutls_session_t *session,
 
278
                      gnutls_dh_params_t *dh_params){
352
279
  int ret;
353
280
  
354
281
  if(debug){
375
302
      != GNUTLS_E_SUCCESS) {
376
303
    fprintf (stderr, "GnuTLS memory error: %s\n",
377
304
             safer_gnutls_strerror(ret));
378
 
    gnutls_global_deinit ();
379
305
    return -1;
380
306
  }
381
307
  
393
319
            " '%s')\n", ret, pubkeyfile, seckeyfile);
394
320
    fprintf(stdout, "The GnuTLS error is: %s\n",
395
321
            safer_gnutls_strerror(ret));
396
 
    goto globalfail;
 
322
    return -1;
397
323
  }
398
324
  
399
325
  /* GnuTLS server initialization */
400
 
  ret = gnutls_dh_params_init(&mc->dh_params);
 
326
  ret = gnutls_dh_params_init(dh_params);
401
327
  if (ret != GNUTLS_E_SUCCESS) {
402
328
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
403
329
             " %s\n", safer_gnutls_strerror(ret));
404
 
    goto globalfail;
 
330
    return -1;
405
331
  }
406
 
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
 
332
  ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
407
333
  if (ret != GNUTLS_E_SUCCESS) {
408
334
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
409
335
             safer_gnutls_strerror(ret));
410
 
    goto globalfail;
 
336
    return -1;
411
337
  }
412
338
  
413
 
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
414
 
 
415
 
  return 0;
416
 
 
417
 
 globalfail:
418
 
 
419
 
  gnutls_certificate_free_credentials(mc->cred);
420
 
  gnutls_global_deinit();
421
 
  return -1;
422
 
 
423
 
}
424
 
 
425
 
static int init_gnutls_session(mandos_context *mc,
426
 
                               gnutls_session_t *session){
427
 
  int ret;
 
339
  gnutls_certificate_set_dh_params(mc->cred, *dh_params);
 
340
  
428
341
  /* GnuTLS session creation */
429
342
  ret = gnutls_init(session, GNUTLS_SERVER);
430
343
  if (ret != GNUTLS_E_SUCCESS){
439
352
      fprintf(stderr, "Syntax error at: %s\n", err);
440
353
      fprintf(stderr, "GnuTLS error: %s\n",
441
354
              safer_gnutls_strerror(ret));
442
 
      gnutls_deinit (*session);
443
355
      return -1;
444
356
    }
445
357
  }
449
361
  if (ret != GNUTLS_E_SUCCESS) {
450
362
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
451
363
            safer_gnutls_strerror(ret));
452
 
    gnutls_deinit (*session);
453
364
    return -1;
454
365
  }
455
366
  
471
382
                                      AvahiIfIndex if_index,
472
383
                                      mandos_context *mc){
473
384
  int ret, tcp_sd;
474
 
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
 
385
  struct sockaddr_in6 to;
475
386
  char *buffer = NULL;
476
387
  char *decrypted_buffer;
477
388
  size_t buffer_length = 0;
478
389
  size_t buffer_capacity = 0;
479
390
  ssize_t decrypted_buffer_size;
480
 
  size_t written;
 
391
  size_t written = 0;
481
392
  int retval = 0;
482
393
  char interface[IF_NAMESIZE];
483
394
  gnutls_session_t session;
 
395
  gnutls_dh_params_t dh_params;
484
396
  
485
 
  ret = init_gnutls_session (mc, &session);
 
397
  ret = initgnutls (mc, &session, &dh_params);
486
398
  if (ret != 0){
487
399
    return -1;
488
400
  }
507
419
  }
508
420
  
509
421
  memset(&to,0,sizeof(to));     /* Spurious warning */
510
 
  to.in6.sin6_family = AF_INET6;
 
422
  to.sin6_family = AF_INET6;
511
423
  /* It would be nice to have a way to detect if we were passed an
512
424
     IPv4 address here.   Now we assume an IPv6 address. */
513
 
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
 
425
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
514
426
  if (ret < 0 ){
515
427
    perror("inet_pton");
516
428
    return -1;
519
431
    fprintf(stderr, "Bad address: %s\n", ip);
520
432
    return -1;
521
433
  }
522
 
  to.in6.sin6_port = htons(port);       /* Spurious warning */
 
434
  to.sin6_port = htons(port);   /* Spurious warning */
523
435
  
524
 
  to.in6.sin6_scope_id = (uint32_t)if_index;
 
436
  to.sin6_scope_id = (uint32_t)if_index;
525
437
  
526
438
  if(debug){
527
439
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
528
440
    char addrstr[INET6_ADDRSTRLEN] = "";
529
 
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
 
441
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
530
442
                 sizeof(addrstr)) == NULL){
531
443
      perror("inet_ntop");
532
444
    } else {
536
448
    }
537
449
  }
538
450
  
539
 
  ret = connect(tcp_sd, &to.in, sizeof(to));
 
451
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
540
452
  if (ret < 0){
541
453
    perror("connect");
542
454
    return -1;
543
455
  }
544
 
 
545
 
  const char *out = mandos_protocol_version;
546
 
  written = 0;
547
 
  while (true){
548
 
    size_t out_size = strlen(out);
549
 
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
550
 
                                   out_size - written));
551
 
    if (ret == -1){
552
 
      perror("write");
553
 
      retval = -1;
554
 
      goto mandos_end;
555
 
    }
556
 
    written += (size_t)ret;
557
 
    if(written < out_size){
558
 
      continue;
559
 
    } else {
560
 
      if (out == mandos_protocol_version){
561
 
        written = 0;
562
 
        out = "\r\n";
563
 
      } else {
564
 
        break;
565
 
      }
566
 
    }
567
 
  }
568
 
 
 
456
  
569
457
  if(debug){
570
458
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
571
459
  }
591
479
  }
592
480
 
593
481
  while(true){
594
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
595
 
                                   buffer_capacity);
596
 
    if (buffer_capacity == 0){
597
 
      perror("adjustbuffer");
598
 
      retval = -1;
599
 
      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;
600
490
    }
601
491
    
602
492
    ret = gnutls_record_recv(session, buffer+buffer_length,
642
532
                                               &decrypted_buffer,
643
533
                                               keydir);
644
534
    if (decrypted_buffer_size >= 0){
645
 
      written = 0;
646
535
      while(written < (size_t) decrypted_buffer_size){
647
536
        ret = (int)fwrite (decrypted_buffer + written, 1,
648
537
                           (size_t)decrypted_buffer_size - written,
669
558
  free(buffer);
670
559
  close(tcp_sd);
671
560
  gnutls_deinit (session);
 
561
  gnutls_certificate_free_credentials (mc->cred);
 
562
  gnutls_global_deinit ();
672
563
  return retval;
673
564
}
674
565
 
797
688
    const char *interface = "eth0";
798
689
    struct ifreq network;
799
690
    int sd;
800
 
    uid_t uid;
801
 
    gid_t gid;
802
691
    char *connect_to = NULL;
803
692
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
804
 
    const char *pubkeyfile = "pubkey.txt";
805
 
    const char *seckeyfile = "seckey.txt";
806
693
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
807
694
                          .dh_bits = 1024, .priority = "SECURE256"};
808
 
    bool gnutls_initalized = false;
809
695
    
810
696
    {
811
 
      struct argp_option options[] = {
812
 
        { .name = "debug", .key = 128,
813
 
          .doc = "Debug mode", .group = 3 },
814
 
        { .name = "connect", .key = 'c',
815
 
          .arg = "IP",
816
 
          .doc = "Connect directly to a sepcified mandos server",
817
 
          .group = 1 },
818
 
        { .name = "interface", .key = 'i',
819
 
          .arg = "INTERFACE",
820
 
          .doc = "Interface that Avahi will conntect through",
821
 
          .group = 1 },
822
 
        { .name = "keydir", .key = 'd',
823
 
          .arg = "KEYDIR",
824
 
          .doc = "Directory where the openpgp keyring is",
825
 
          .group = 1 },
826
 
        { .name = "seckey", .key = 's',
827
 
          .arg = "SECKEY",
828
 
          .doc = "Secret openpgp key for gnutls authentication",
829
 
          .group = 1 },
830
 
        { .name = "pubkey", .key = 'p',
831
 
          .arg = "PUBKEY",
832
 
          .doc = "Public openpgp key for gnutls authentication",
833
 
          .group = 2 },
834
 
        { .name = "dh-bits", .key = 129,
835
 
          .arg = "BITS",
836
 
          .doc = "dh-bits to use in gnutls communication",
837
 
          .group = 2 },
838
 
        { .name = "priority", .key = 130,
839
 
          .arg = "PRIORITY",
840
 
          .doc = "GNUTLS priority", .group = 1 },
841
 
        { .name = NULL }
842
 
      };
843
 
 
844
 
      
845
 
      error_t parse_opt (int key, char *arg,
846
 
                         struct argp_state *state) {
847
 
        /* Get the INPUT argument from `argp_parse', which we know is
848
 
           a pointer to our plugin list pointer. */
849
 
        switch (key) {
850
 
        case 128:
851
 
          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;
852
724
          break;
853
725
        case 'c':
854
 
          connect_to = arg;
855
 
          break;
856
 
        case 'i':
857
 
          interface = arg;
 
726
          connect_to = optarg;
858
727
          break;
859
728
        case 'd':
860
 
          keydir = arg;
 
729
          keydir = optarg;
 
730
          break;
 
731
        case 'p':
 
732
          pubkeyfile = optarg;
861
733
          break;
862
734
        case 's':
863
 
          seckeyfile = arg;
864
 
          break;
865
 
        case 'p':
866
 
          pubkeyfile = arg;
867
 
          break;
868
 
        case 129:
 
735
          seckeyfile = optarg;
 
736
          break;
 
737
        case 'D':
869
738
          errno = 0;
870
 
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
 
739
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
871
740
          if (errno){
872
741
            perror("strtol");
873
742
            exit(EXIT_FAILURE);
874
743
          }
875
744
          break;
876
 
        case 130:
877
 
          mc.priority = arg;
878
 
          break;
879
 
        case ARGP_KEY_ARG:
880
 
          argp_usage (state);
881
 
          break;
882
 
          case ARGP_KEY_END:
883
 
            break;
 
745
        case 'P':
 
746
          mc.priority = optarg;
 
747
          break;
 
748
        case '?':
884
749
        default:
885
 
          return ARGP_ERR_UNKNOWN;
 
750
          /* getopt_long() has already printed a message about the
 
751
             unrcognized option, so just exit. */
 
752
          exit(EXIT_FAILURE);
886
753
        }
887
 
        return 0;
888
754
      }
889
 
 
890
 
      struct argp argp = { .options = options, .parser = parse_opt,
891
 
                           .args_doc = "",
892
 
                           .doc = "Mandos client -- Get and decrypt"
893
 
                           " passwords from mandos server" };
894
 
      argp_parse (&argp, argc, argv, 0, 0, NULL);
 
755
      /* Set the global debug flag from the temporary int */
 
756
      debug = debug_int ? true : false;
895
757
    }
896
 
      
 
758
    
897
759
    pubkeyfile = combinepath(keydir, pubkeyfile);
898
760
    if (pubkeyfile == NULL){
899
761
      perror("combinepath");
906
768
      perror("combinepath");
907
769
      goto end;
908
770
    }
909
 
 
910
 
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
911
 
    if (ret == -1){
912
 
      fprintf(stderr, "init_gnutls_global\n");
913
 
      goto end;
914
 
    } else {
915
 
      gnutls_initalized = true;
916
 
    }
917
 
 
918
 
    uid = getuid();
919
 
    gid = getgid();
920
 
 
921
 
    ret = setuid(uid);
922
 
    if (ret == -1){
923
 
      perror("setuid");
924
 
    }
925
 
    
926
 
    setgid(gid);
927
 
    if (ret == -1){
928
 
      perror("setgid");
929
 
    }
930
771
    
931
772
    if_index = (AvahiIfIndex) if_nametoindex(interface);
932
773
    if(if_index == 0){
940
781
      char *address = strrchr(connect_to, ':');
941
782
      if(address == NULL){
942
783
        fprintf(stderr, "No colon in address\n");
943
 
        exitcode = EXIT_FAILURE;
944
 
        goto end;
 
784
        exit(EXIT_FAILURE);
945
785
      }
946
786
      errno = 0;
947
787
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
948
788
      if(errno){
949
789
        perror("Bad port number");
950
 
        exitcode = EXIT_FAILURE;
951
 
        goto end;
 
790
        exit(EXIT_FAILURE);
952
791
      }
953
792
      *address = '\0';
954
793
      address = connect_to;
955
794
      ret = start_mandos_communication(address, port, if_index, &mc);
956
795
      if(ret < 0){
957
 
        exitcode = EXIT_FAILURE;
 
796
        exit(EXIT_FAILURE);
958
797
      } else {
959
 
        exitcode = EXIT_SUCCESS;
 
798
        exit(EXIT_SUCCESS);
960
799
      }
961
 
      goto end;
962
800
    }
963
801
    
964
802
    /* If the interface is down, bring it up */
1067
905
        avahi_simple_poll_free(mc.simple_poll);
1068
906
    free(pubkeyfile);
1069
907
    free(seckeyfile);
1070
 
 
1071
 
    if (gnutls_initalized){
1072
 
      gnutls_certificate_free_credentials (mc.cred);
1073
 
      gnutls_global_deinit ();
1074
 
    }
1075
908
    
1076
909
    return exitcode;
1077
910
}