/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 03:33:56 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080803033356-6aemgj0g0hoz91ow
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
                                                 On debug, show
                                                 decrypted plaintext
                                                 in hexadecimal.  Free
                                                 the GPGME data
                                                 buffers even on
                                                 errors.

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
35
#include <stdio.h>
38
36
#include <assert.h>
39
37
#include <stdlib.h>
51
49
#include <avahi-common/malloc.h>
52
50
#include <avahi-common/error.h>
53
51
 
54
 
/* Mandos client part */
 
52
//mandos client part
55
53
#include <sys/types.h>          /* socket(), inet_pton() */
56
54
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
57
55
                                   struct in6_addr, inet_pton() */
64
62
#include <string.h>             /* memset */
65
63
#include <arpa/inet.h>          /* inet_pton() */
66
64
#include <iso646.h>             /* not */
67
 
#include <net/if.h>             /* IF_NAMESIZE */
68
65
 
69
 
/* GPGME */
 
66
// gpgme
70
67
#include <errno.h>              /* perror() */
71
68
#include <gpgme.h>
72
69
 
73
 
/* getopt_long */
 
70
// getopt_long
74
71
#include <getopt.h>
75
72
 
76
73
#define BUFFER_SIZE 256
81
78
 
82
79
bool debug = false;
83
80
 
84
 
const char mandos_protocol_version[] = "1";
85
 
 
86
81
/* Used for passing in values through all the callback functions */
87
82
typedef struct {
88
83
  AvahiSimplePoll *simple_poll;
89
84
  AvahiServer *server;
90
85
  gnutls_certificate_credentials_t cred;
91
86
  unsigned int dh_bits;
92
 
  gnutls_dh_params_t dh_params;
93
87
  const char *priority;
94
88
} mandos_context;
95
89
 
96
 
size_t adjustbuffer(char **buffer, size_t buffer_length,
97
 
                  size_t buffer_capacity){
98
 
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
99
 
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
100
 
    if (buffer == NULL){
101
 
      return 0;
102
 
    }
103
 
    buffer_capacity += BUFFER_SIZE;
104
 
  }
105
 
  return buffer_capacity;
106
 
}
107
 
 
108
90
/* 
109
91
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
110
92
 * Returns -1 on error
117
99
  gpgme_ctx_t ctx;
118
100
  gpgme_error_t rc;
119
101
  ssize_t ret;
120
 
  size_t plaintext_capacity = 0;
 
102
  ssize_t plaintext_capacity = 0;
121
103
  ssize_t plaintext_length = 0;
122
104
  gpgme_engine_info_t engine_info;
123
105
  
233
215
  
234
216
  *plaintext = NULL;
235
217
  while(true){
236
 
    plaintext_capacity = adjustbuffer(plaintext, (size_t)plaintext_length,
237
 
                                      plaintext_capacity);
238
 
    if (plaintext_capacity == 0){
239
 
        perror("adjustbuffer");
 
218
    if (plaintext_length + BUFFER_SIZE > plaintext_capacity){
 
219
      *plaintext = realloc(*plaintext,
 
220
                            (unsigned int)plaintext_capacity
 
221
                            + BUFFER_SIZE);
 
222
      if (*plaintext == NULL){
 
223
        perror("realloc");
240
224
        plaintext_length = -1;
241
225
        goto decrypt_end;
 
226
      }
 
227
      plaintext_capacity += BUFFER_SIZE;
242
228
    }
243
229
    
244
230
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
258
244
 
259
245
  if(debug){
260
246
    fprintf(stderr, "Decrypted password is: ");
261
 
    for(ssize_t i = 0; i < plaintext_length; i++){
 
247
    for(size_t i = 0; i < plaintext_length; i++){
262
248
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
263
249
    }
264
250
    fprintf(stderr, "\n");
281
267
  return ret;
282
268
}
283
269
 
284
 
/* GnuTLS log function callback */
285
270
static void debuggnutls(__attribute__((unused)) int level,
286
271
                        const char* string){
287
 
  fprintf(stderr, "GnuTLS: %s", string);
 
272
  fprintf(stderr, "%s", string);
288
273
}
289
274
 
290
 
static int init_gnutls_global(mandos_context *mc){
 
275
static int initgnutls(mandos_context *mc, gnutls_session_t *session,
 
276
                      gnutls_dh_params_t *dh_params){
 
277
  const char *err;
291
278
  int ret;
292
279
  
293
280
  if(debug){
296
283
 
297
284
  if ((ret = gnutls_global_init ())
298
285
      != GNUTLS_E_SUCCESS) {
299
 
    fprintf (stderr, "GnuTLS global_init: %s\n",
300
 
             safer_gnutls_strerror(ret));
 
286
    fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
301
287
    return -1;
302
288
  }
303
289
  
304
290
  if (debug){
305
 
    /* "Use a log level over 10 to enable all debugging options."
306
 
     * - GnuTLS manual
307
 
     */
308
291
    gnutls_global_set_log_level(11);
309
292
    gnutls_global_set_log_function(debuggnutls);
310
293
  }
311
294
  
312
 
  /* OpenPGP credentials */
 
295
  /* openpgp credentials */
313
296
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
314
297
      != GNUTLS_E_SUCCESS) {
315
 
    fprintf (stderr, "GnuTLS memory error: %s\n",
 
298
    fprintf (stderr, "memory error: %s\n",
316
299
             safer_gnutls_strerror(ret));
317
300
    return -1;
318
301
  }
326
309
  ret = gnutls_certificate_set_openpgp_key_file
327
310
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
328
311
  if (ret != GNUTLS_E_SUCCESS) {
329
 
    fprintf(stderr,
330
 
            "Error[%d] while reading the OpenPGP key pair ('%s',"
331
 
            " '%s')\n", ret, pubkeyfile, seckeyfile);
332
 
    fprintf(stdout, "The GnuTLS error is: %s\n",
 
312
    fprintf
 
313
      (stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
 
314
       " '%s')\n",
 
315
       ret, pubkeyfile, seckeyfile);
 
316
    fprintf(stdout, "The Error is: %s\n",
333
317
            safer_gnutls_strerror(ret));
334
318
    return -1;
335
319
  }
336
320
  
337
 
  /* GnuTLS server initialization */
338
 
  ret = gnutls_dh_params_init(&mc->dh_params);
339
 
  if (ret != GNUTLS_E_SUCCESS) {
340
 
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
341
 
             " %s\n", safer_gnutls_strerror(ret));
342
 
    return -1;
343
 
  }
344
 
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
345
 
  if (ret != GNUTLS_E_SUCCESS) {
346
 
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
347
 
             safer_gnutls_strerror(ret));
348
 
    return -1;
349
 
  }
350
 
  
351
 
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
352
 
 
353
 
  return 0;
354
 
}
355
 
 
356
 
static int init_gnutls_session(mandos_context *mc, gnutls_session_t *session){
357
 
  int ret;
358
 
  /* GnuTLS session creation */
359
 
  ret = gnutls_init(session, GNUTLS_SERVER);
360
 
  if (ret != GNUTLS_E_SUCCESS){
 
321
  //GnuTLS server initialization
 
322
  if ((ret = gnutls_dh_params_init(dh_params))
 
323
      != GNUTLS_E_SUCCESS) {
 
324
    fprintf (stderr, "Error in dh parameter initialization: %s\n",
 
325
             safer_gnutls_strerror(ret));
 
326
    return -1;
 
327
  }
 
328
  
 
329
  if ((ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits))
 
330
      != GNUTLS_E_SUCCESS) {
 
331
    fprintf (stderr, "Error in prime generation: %s\n",
 
332
             safer_gnutls_strerror(ret));
 
333
    return -1;
 
334
  }
 
335
  
 
336
  gnutls_certificate_set_dh_params(mc->cred, *dh_params);
 
337
  
 
338
  // GnuTLS session creation
 
339
  if ((ret = gnutls_init(session, GNUTLS_SERVER))
 
340
      != GNUTLS_E_SUCCESS){
361
341
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
362
342
            safer_gnutls_strerror(ret));
363
343
  }
364
344
  
365
 
  {
366
 
    const char *err;
367
 
    ret = gnutls_priority_set_direct(*session, mc->priority, &err);
368
 
    if (ret != GNUTLS_E_SUCCESS) {
369
 
      fprintf(stderr, "Syntax error at: %s\n", err);
370
 
      fprintf(stderr, "GnuTLS error: %s\n",
371
 
              safer_gnutls_strerror(ret));
372
 
      return -1;
373
 
    }
 
345
  if ((ret = gnutls_priority_set_direct(*session, mc->priority, &err))
 
346
      != GNUTLS_E_SUCCESS) {
 
347
    fprintf(stderr, "Syntax error at: %s\n", err);
 
348
    fprintf(stderr, "GnuTLS error: %s\n",
 
349
            safer_gnutls_strerror(ret));
 
350
    return -1;
374
351
  }
375
352
  
376
 
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
377
 
                               mc->cred);
378
 
  if (ret != GNUTLS_E_SUCCESS) {
379
 
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
 
353
  if ((ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
354
                                    mc->cred))
 
355
      != GNUTLS_E_SUCCESS) {
 
356
    fprintf(stderr, "Error setting a credentials set: %s\n",
380
357
            safer_gnutls_strerror(ret));
381
358
    return -1;
382
359
  }
390
367
  return 0;
391
368
}
392
369
 
393
 
/* Avahi log function callback */
394
370
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
395
371
                      __attribute__((unused)) const char *txt){}
396
372
 
397
 
/* Called when a Mandos server is found */
398
373
static int start_mandos_communication(const char *ip, uint16_t port,
399
374
                                      AvahiIfIndex if_index,
400
375
                                      mandos_context *mc){
405
380
  size_t buffer_length = 0;
406
381
  size_t buffer_capacity = 0;
407
382
  ssize_t decrypted_buffer_size;
408
 
  size_t written;
 
383
  size_t written = 0;
409
384
  int retval = 0;
410
385
  char interface[IF_NAMESIZE];
411
386
  gnutls_session_t session;
412
387
  gnutls_dh_params_t dh_params;
413
388
  
414
 
  ret = init_gnutls_session (mc, &session);
415
 
  if (ret != 0){
416
 
    return -1;
417
 
  }
418
 
  
419
389
  if(debug){
420
390
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
421
391
            ip, port);
437
407
  
438
408
  memset(&to,0,sizeof(to));     /* Spurious warning */
439
409
  to.sin6_family = AF_INET6;
440
 
  /* It would be nice to have a way to detect if we were passed an
441
 
     IPv4 address here.   Now we assume an IPv6 address. */
442
410
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
443
411
  if (ret < 0 ){
444
412
    perror("inet_pton");
470
438
    perror("connect");
471
439
    return -1;
472
440
  }
473
 
 
474
 
  const char *out = mandos_protocol_version;
475
 
  written = 0;
476
 
  while (true){
477
 
    size_t out_size = strlen(out);
478
 
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
479
 
                                   out_size - written));
480
 
    if (ret == -1){
481
 
      perror("write");
482
 
      retval = -1;
483
 
      goto mandos_end;
484
 
    }
485
 
    written += (size_t)ret;
486
 
    if(written < out_size){
487
 
      continue;
488
 
    } else {
489
 
      if (out == mandos_protocol_version){
490
 
        written = 0;
491
 
        out = "\r\n";
492
 
      } else {
493
 
        break;
494
 
      }
495
 
    }
 
441
  
 
442
  ret = initgnutls (mc, &session, &dh_params);
 
443
  if (ret != 0){
 
444
    retval = -1;
 
445
    return -1;
496
446
  }
497
 
 
 
447
  
 
448
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
 
449
  
498
450
  if(debug){
499
451
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
500
452
  }
501
453
  
502
 
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
503
 
  
504
454
  ret = gnutls_handshake (session);
505
455
  
506
456
  if (ret != GNUTLS_E_SUCCESS){
507
457
    if(debug){
508
 
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
 
458
      fprintf(stderr, "\n*** Handshake failed ***\n");
509
459
      gnutls_perror (ret);
510
460
    }
511
461
    retval = -1;
512
 
    goto mandos_end;
 
462
    goto exit;
513
463
  }
514
464
  
515
 
  /* Read OpenPGP packet that contains the wanted password */
 
465
  //Retrieve OpenPGP packet that contains the wanted password
516
466
  
517
467
  if(debug){
518
468
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
520
470
  }
521
471
 
522
472
  while(true){
523
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length, buffer_capacity);
524
 
    if (buffer_capacity == 0){
525
 
      perror("adjustbuffer");
526
 
      retval = -1;
527
 
      goto mandos_end;
 
473
    if (buffer_length + BUFFER_SIZE > buffer_capacity){
 
474
      buffer = realloc(buffer, buffer_capacity + BUFFER_SIZE);
 
475
      if (buffer == NULL){
 
476
        perror("realloc");
 
477
        goto exit;
 
478
      }
 
479
      buffer_capacity += BUFFER_SIZE;
528
480
    }
529
481
    
530
482
    ret = gnutls_record_recv(session, buffer+buffer_length,
540
492
      case GNUTLS_E_REHANDSHAKE:
541
493
        ret = gnutls_handshake (session);
542
494
        if (ret < 0){
543
 
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
 
495
          fprintf(stderr, "\n*** Handshake failed ***\n");
544
496
          gnutls_perror (ret);
545
497
          retval = -1;
546
 
          goto mandos_end;
 
498
          goto exit;
547
499
        }
548
500
        break;
549
501
      default:
550
502
        fprintf(stderr, "Unknown error while reading data from"
551
 
                " encrypted session with Mandos server\n");
 
503
                " encrypted session with mandos server\n");
552
504
        retval = -1;
553
505
        gnutls_bye (session, GNUTLS_SHUT_RDWR);
554
 
        goto mandos_end;
 
506
        goto exit;
555
507
      }
556
508
    } else {
557
509
      buffer_length += (size_t) ret;
558
510
    }
559
511
  }
560
512
  
561
 
  if(debug){
562
 
    fprintf(stderr, "Closing TLS session\n");
563
 
  }
564
 
  
565
 
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
566
 
  
567
513
  if (buffer_length > 0){
568
514
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
569
515
                                               buffer_length,
570
516
                                               &decrypted_buffer,
571
517
                                               keydir);
572
518
    if (decrypted_buffer_size >= 0){
573
 
      written = 0;
574
519
      while(written < (size_t) decrypted_buffer_size){
575
520
        ret = (int)fwrite (decrypted_buffer + written, 1,
576
521
                           (size_t)decrypted_buffer_size - written,
590
535
      retval = -1;
591
536
    }
592
537
  }
593
 
  
594
 
  /* Shutdown procedure */
595
 
  
596
 
 mandos_end:
 
538
 
 
539
  //shutdown procedure
 
540
 
 
541
  if(debug){
 
542
    fprintf(stderr, "Closing TLS session\n");
 
543
  }
 
544
 
597
545
  free(buffer);
 
546
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
 
547
 exit:
598
548
  close(tcp_sd);
599
549
  gnutls_deinit (session);
600
550
  gnutls_certificate_free_credentials (mc->cred);
625
575
  switch (event) {
626
576
  default:
627
577
  case AVAHI_RESOLVER_FAILURE:
628
 
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
629
 
            " of type '%s' in domain '%s': %s\n", name, type, domain,
 
578
    fprintf(stderr, "(Resolver) Failed to resolve service '%s' of"
 
579
            " type '%s' in domain '%s': %s\n", name, type, domain,
630
580
            avahi_strerror(avahi_server_errno(mc->server)));
631
581
    break;
632
582
    
635
585
      char ip[AVAHI_ADDRESS_STR_MAX];
636
586
      avahi_address_snprint(ip, sizeof(ip), address);
637
587
      if(debug){
638
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
639
 
                " port %d\n", name, host_name, ip, interface, port);
 
588
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s) on"
 
589
                " port %d\n", name, host_name, ip, port);
640
590
      }
641
591
      int ret = start_mandos_communication(ip, port, interface, mc);
642
592
      if (ret == 0){
667
617
  default:
668
618
  case AVAHI_BROWSER_FAILURE:
669
619
    
670
 
    fprintf(stderr, "(Avahi browser) %s\n",
 
620
    fprintf(stderr, "(Browser) %s\n",
671
621
            avahi_strerror(avahi_server_errno(mc->server)));
672
622
    avahi_simple_poll_quit(mc->simple_poll);
673
623
    return;
674
624
    
675
625
  case AVAHI_BROWSER_NEW:
676
 
    /* We ignore the returned Avahi resolver object. In the callback
677
 
       function we free it. If the Avahi server is terminated before
678
 
       the callback function is called the Avahi server will free the
679
 
       resolver for us. */
 
626
    /* We ignore the returned resolver object. In the callback
 
627
       function we free it. If the server is terminated before
 
628
       the callback function is called the server will free
 
629
       the resolver for us. */
680
630
    
681
631
    if (!(avahi_s_service_resolver_new(mc->server, interface,
682
632
                                       protocol, name, type, domain,
683
633
                                       AVAHI_PROTO_INET6, 0,
684
634
                                       resolve_callback, mc)))
685
 
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
686
 
              name, avahi_strerror(avahi_server_errno(mc->server)));
 
635
      fprintf(stderr, "Failed to resolve service '%s': %s\n", name,
 
636
              avahi_strerror(avahi_server_errno(mc->server)));
687
637
    break;
688
638
    
689
639
  case AVAHI_BROWSER_REMOVE:
691
641
    
692
642
  case AVAHI_BROWSER_ALL_FOR_NOW:
693
643
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
694
 
    if(debug){
695
 
      fprintf(stderr, "No Mandos server found, still searching...\n");
696
 
    }
697
644
    break;
698
645
  }
699
646
}
719
666
}
720
667
 
721
668
 
722
 
int main(int argc, char *argv[]){
 
669
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
 
670
    AvahiServerConfig config;
723
671
    AvahiSServiceBrowser *sb = NULL;
724
672
    int error;
725
673
    int ret;
726
 
    int exitcode = EXIT_SUCCESS;
 
674
    int debug_int;
 
675
    int returncode = EXIT_SUCCESS;
727
676
    const char *interface = "eth0";
728
677
    struct ifreq network;
729
678
    int sd;
730
 
    uid_t uid;
731
 
    gid_t gid;
732
679
    char *connect_to = NULL;
733
680
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
734
681
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
735
682
                          .dh_bits = 1024, .priority = "SECURE256"};
736
683
    
737
 
    {
738
 
      /* Temporary int to get the address of for getopt_long */
739
 
      int debug_int = debug ? 1 : 0;
740
 
      while (true){
741
 
        struct option long_options[] = {
742
 
          {"debug", no_argument, &debug_int, 1},
743
 
          {"connect", required_argument, NULL, 'c'},
744
 
          {"interface", required_argument, NULL, 'i'},
745
 
          {"keydir", required_argument, NULL, 'd'},
746
 
          {"seckey", required_argument, NULL, 's'},
747
 
          {"pubkey", required_argument, NULL, 'p'},
748
 
          {"dh-bits", required_argument, NULL, 'D'},
749
 
          {"priority", required_argument, NULL, 'P'},
750
 
          {0, 0, 0, 0} };
751
 
      
752
 
        int option_index = 0;
753
 
        ret = getopt_long (argc, argv, "i:", long_options,
754
 
                           &option_index);
755
 
      
756
 
        if (ret == -1){
757
 
          break;
758
 
        }
759
 
      
760
 
        switch(ret){
761
 
        case 0:
762
 
          break;
763
 
        case 'i':
764
 
          interface = optarg;
765
 
          break;
766
 
        case 'c':
767
 
          connect_to = optarg;
768
 
          break;
769
 
        case 'd':
770
 
          keydir = optarg;
771
 
          break;
772
 
        case 'p':
773
 
          pubkeyfile = optarg;
774
 
          break;
775
 
        case 's':
776
 
          seckeyfile = optarg;
777
 
          break;
778
 
        case 'D':
779
 
          errno = 0;
780
 
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
781
 
          if (errno){
782
 
            perror("strtol");
783
 
            exit(EXIT_FAILURE);
784
 
          }
785
 
          break;
786
 
        case 'P':
787
 
          mc.priority = optarg;
788
 
          break;
789
 
        case '?':
790
 
        default:
791
 
          /* getopt_long() has already printed a message about the
792
 
             unrcognized option, so just exit. */
 
684
    debug_int = debug ? 1 : 0;
 
685
    while (true){
 
686
      struct option long_options[] = {
 
687
        {"debug", no_argument, &debug_int, 1},
 
688
        {"connect", required_argument, NULL, 'c'},
 
689
        {"interface", required_argument, NULL, 'i'},
 
690
        {"keydir", required_argument, NULL, 'd'},
 
691
        {"seckey", required_argument, NULL, 's'},
 
692
        {"pubkey", required_argument, NULL, 'p'},
 
693
        {"dh-bits", required_argument, NULL, 'D'},
 
694
        {"priority", required_argument, NULL, 'P'},
 
695
        {0, 0, 0, 0} };
 
696
      
 
697
      int option_index = 0;
 
698
      ret = getopt_long (argc, argv, "i:", long_options,
 
699
                         &option_index);
 
700
      
 
701
      if (ret == -1){
 
702
        break;
 
703
      }
 
704
      
 
705
      switch(ret){
 
706
      case 0:
 
707
        break;
 
708
      case 'i':
 
709
        interface = optarg;
 
710
        break;
 
711
      case 'c':
 
712
        connect_to = optarg;
 
713
        break;
 
714
      case 'd':
 
715
        keydir = optarg;
 
716
        break;
 
717
      case 'p':
 
718
        pubkeyfile = optarg;
 
719
        break;
 
720
      case 's':
 
721
        seckeyfile = optarg;
 
722
        break;
 
723
      case 'D':
 
724
        errno = 0;
 
725
        mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
 
726
        if (errno){
 
727
          perror("strtol");
793
728
          exit(EXIT_FAILURE);
794
729
        }
 
730
        break;
 
731
      case 'P':
 
732
        mc.priority = optarg;
 
733
        break;
 
734
      case '?':
 
735
      default:
 
736
        exit(EXIT_FAILURE);
795
737
      }
796
 
      /* Set the global debug flag from the temporary int */
797
 
      debug = debug_int ? true : false;
798
738
    }
 
739
    debug = debug_int ? true : false;
799
740
    
800
741
    pubkeyfile = combinepath(keydir, pubkeyfile);
801
742
    if (pubkeyfile == NULL){
802
743
      perror("combinepath");
803
 
      exitcode = EXIT_FAILURE;
804
 
      goto end;
 
744
      returncode = EXIT_FAILURE;
 
745
      goto exit;
805
746
    }
806
747
    
807
748
    seckeyfile = combinepath(keydir, seckeyfile);
808
749
    if (seckeyfile == NULL){
809
750
      perror("combinepath");
810
 
      goto end;
811
 
    }
812
 
 
813
 
    ret = init_gnutls_global(&mc);
814
 
    if (ret == -1){
815
 
      fprintf(stderr, "init_gnutls_global\n");
816
 
      goto end;
817
 
    }
818
 
 
819
 
    uid = getuid();
820
 
    gid = getgid();
821
 
 
822
 
    ret = setuid(uid);
823
 
    if (ret == -1){
824
 
      perror("setuid");
825
 
    }
826
 
    
827
 
    setgid(gid);
828
 
    if (ret == -1){
829
 
      perror("setgid");
 
751
      goto exit;
830
752
    }
831
753
    
832
754
    if_index = (AvahiIfIndex) if_nametoindex(interface);
841
763
      char *address = strrchr(connect_to, ':');
842
764
      if(address == NULL){
843
765
        fprintf(stderr, "No colon in address\n");
844
 
        exitcode = EXIT_FAILURE;
845
 
        goto end;
 
766
        exit(EXIT_FAILURE);
846
767
      }
847
768
      errno = 0;
848
769
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
849
770
      if(errno){
850
771
        perror("Bad port number");
851
 
        exitcode = EXIT_FAILURE;
852
 
        goto end;
 
772
        exit(EXIT_FAILURE);
853
773
      }
854
774
      *address = '\0';
855
775
      address = connect_to;
856
776
      ret = start_mandos_communication(address, port, if_index, &mc);
857
777
      if(ret < 0){
858
 
        exitcode = EXIT_FAILURE;
 
778
        exit(EXIT_FAILURE);
859
779
      } else {
860
 
        exitcode = EXIT_SUCCESS;
 
780
        exit(EXIT_SUCCESS);
861
781
      }
862
 
      goto end;
863
782
    }
864
783
    
865
 
    /* If the interface is down, bring it up */
866
 
    {
867
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
868
 
      if(sd < 0) {
869
 
        perror("socket");
870
 
        exitcode = EXIT_FAILURE;
871
 
        goto end;
872
 
      }
873
 
      strcpy(network.ifr_name, interface); /* Spurious warning */
874
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
784
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
785
    if(sd < 0) {
 
786
      perror("socket");
 
787
      returncode = EXIT_FAILURE;
 
788
      goto exit;
 
789
    }
 
790
    strcpy(network.ifr_name, interface); /* Spurious warning */
 
791
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
792
    if(ret == -1){
 
793
      
 
794
      perror("ioctl SIOCGIFFLAGS");
 
795
      returncode = EXIT_FAILURE;
 
796
      goto exit;
 
797
    }
 
798
    if((network.ifr_flags & IFF_UP) == 0){
 
799
      network.ifr_flags |= IFF_UP;
 
800
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
875
801
      if(ret == -1){
876
 
        perror("ioctl SIOCGIFFLAGS");
877
 
        exitcode = EXIT_FAILURE;
878
 
        goto end;
879
 
      }
880
 
      if((network.ifr_flags & IFF_UP) == 0){
881
 
        network.ifr_flags |= IFF_UP;
882
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
883
 
        if(ret == -1){
884
 
          perror("ioctl SIOCSIFFLAGS");
885
 
          exitcode = EXIT_FAILURE;
886
 
          goto end;
887
 
        }
888
 
      }
889
 
      close(sd);
 
802
        perror("ioctl SIOCSIFFLAGS");
 
803
        returncode = EXIT_FAILURE;
 
804
        goto exit;
 
805
      }
890
806
    }
 
807
    close(sd);
891
808
    
892
809
    if (not debug){
893
810
      avahi_set_log_function(empty_log);
894
811
    }
895
812
    
896
 
    /* Initialize the pseudo-RNG for Avahi */
 
813
    /* Initialize the psuedo-RNG */
897
814
    srand((unsigned int) time(NULL));
898
 
    
899
 
    /* Allocate main Avahi loop object */
900
 
    mc.simple_poll = avahi_simple_poll_new();
901
 
    if (mc.simple_poll == NULL) {
902
 
        fprintf(stderr, "Avahi: Failed to create simple poll"
903
 
                " object.\n");
904
 
        exitcode = EXIT_FAILURE;
905
 
        goto end;
906
 
    }
907
 
 
908
 
    {
909
 
      AvahiServerConfig config;
910
 
      /* Do not publish any local Zeroconf records */
911
 
      avahi_server_config_init(&config);
912
 
      config.publish_hinfo = 0;
913
 
      config.publish_addresses = 0;
914
 
      config.publish_workstation = 0;
915
 
      config.publish_domain = 0;
916
 
 
917
 
      /* Allocate a new server */
918
 
      mc.server = avahi_server_new(avahi_simple_poll_get
919
 
                                   (mc.simple_poll), &config, NULL,
920
 
                                   NULL, &error);
921
 
    
922
 
      /* Free the Avahi configuration data */
923
 
      avahi_server_config_free(&config);
924
 
    }
925
 
    
926
 
    /* Check if creating the Avahi server object succeeded */
927
 
    if (mc.server == NULL) {
928
 
        fprintf(stderr, "Failed to create Avahi server: %s\n",
 
815
 
 
816
    /* Allocate main loop object */
 
817
    if (!(mc.simple_poll = avahi_simple_poll_new())) {
 
818
        fprintf(stderr, "Failed to create simple poll object.\n");
 
819
        returncode = EXIT_FAILURE;
 
820
        goto exit;
 
821
    }
 
822
 
 
823
    /* Do not publish any local records */
 
824
    avahi_server_config_init(&config);
 
825
    config.publish_hinfo = 0;
 
826
    config.publish_addresses = 0;
 
827
    config.publish_workstation = 0;
 
828
    config.publish_domain = 0;
 
829
 
 
830
    /* Allocate a new server */
 
831
    mc.server=avahi_server_new(avahi_simple_poll_get(mc.simple_poll),
 
832
                               &config, NULL, NULL, &error);
 
833
    
 
834
    /* Free the configuration data */
 
835
    avahi_server_config_free(&config);
 
836
    
 
837
    /* Check if creating the server object succeeded */
 
838
    if (!mc.server) {
 
839
        fprintf(stderr, "Failed to create server: %s\n",
929
840
                avahi_strerror(error));
930
 
        exitcode = EXIT_FAILURE;
931
 
        goto end;
 
841
        returncode = EXIT_FAILURE;
 
842
        goto exit;
932
843
    }
933
844
    
934
 
    /* Create the Avahi service browser */
 
845
    /* Create the service browser */
935
846
    sb = avahi_s_service_browser_new(mc.server, if_index,
936
847
                                     AVAHI_PROTO_INET6,
937
848
                                     "_mandos._tcp", NULL, 0,
938
849
                                     browse_callback, &mc);
939
 
    if (sb == NULL) {
 
850
    if (!sb) {
940
851
        fprintf(stderr, "Failed to create service browser: %s\n",
941
852
                avahi_strerror(avahi_server_errno(mc.server)));
942
 
        exitcode = EXIT_FAILURE;
943
 
        goto end;
 
853
        returncode = EXIT_FAILURE;
 
854
        goto exit;
944
855
    }
945
856
    
946
857
    /* Run the main loop */
947
858
 
948
859
    if (debug){
949
 
      fprintf(stderr, "Starting Avahi loop search\n");
 
860
      fprintf(stderr, "Starting avahi loop search\n");
950
861
    }
951
862
    
952
863
    avahi_simple_poll_loop(mc.simple_poll);
953
864
    
954
 
 end:
 
865
 exit:
955
866
 
956
867
    if (debug){
957
868
      fprintf(stderr, "%s exiting\n", argv[0]);
958
869
    }
959
870
    
960
871
    /* Cleanup things */
961
 
    if (sb != NULL)
 
872
    if (sb)
962
873
        avahi_s_service_browser_free(sb);
963
874
    
964
 
    if (mc.server != NULL)
 
875
    if (mc.server)
965
876
        avahi_server_free(mc.server);
966
877
 
967
 
    if (mc.simple_poll != NULL)
 
878
    if (mc.simple_poll)
968
879
        avahi_simple_poll_free(mc.simple_poll);
969
880
    free(pubkeyfile);
970
881
    free(seckeyfile);
971
882
    
972
 
    return exitcode;
 
883
    return returncode;
973
884
}