/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 21:45:14 UTC
  • mfrom: (24.1.13 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080803214514-plvvkrpoitwsrxeg
Merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
#include <arpa/inet.h>          /* inet_pton() */
66
66
#include <iso646.h>             /* not */
67
67
#include <net/if.h>             /* IF_NAMESIZE */
68
 
#include <argp.h>               /* struct argp_option,
69
 
                                   struct argp_state, struct argp,
70
 
                                   argp_parse() */
 
68
 
71
69
/* GPGME */
72
70
#include <errno.h>              /* perror() */
73
71
#include <gpgme.h>
74
72
 
 
73
/* getopt_long */
 
74
#include <getopt.h>
 
75
 
75
76
#define BUFFER_SIZE 256
76
77
 
 
78
static const char *keydir = "/conf/conf.d/mandos";
 
79
static const char *pubkeyfile = "pubkey.txt";
 
80
static const char *seckeyfile = "seckey.txt";
 
81
 
77
82
bool debug = false;
78
 
static const char *keydir = "/conf/conf.d/mandos";
79
 
static const char mandos_protocol_version[] = "1";
80
 
const char *argp_program_version = "mandosclient 0.9";
81
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
82
 
 
83
 
/* Used for passing in values through the Avahi callback functions */
 
83
 
 
84
const char mandos_protocol_version[] = "1";
 
85
 
 
86
/* Used for passing in values through all the callback functions */
84
87
typedef struct {
85
88
  AvahiSimplePoll *simple_poll;
86
89
  AvahiServer *server;
90
93
  const char *priority;
91
94
} mandos_context;
92
95
 
93
 
/*
94
 
 * Make room in "buffer" for at least BUFFER_SIZE additional bytes.
95
 
 * "buffer_capacity" is how much is currently allocated,
96
 
 * "buffer_length" is how much is already used.
97
 
 */
98
96
size_t adjustbuffer(char **buffer, size_t buffer_length,
99
97
                  size_t buffer_capacity){
100
98
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
235
233
  
236
234
  *plaintext = NULL;
237
235
  while(true){
238
 
    plaintext_capacity = adjustbuffer(plaintext,
239
 
                                      (size_t)plaintext_length,
 
236
    plaintext_capacity = adjustbuffer(plaintext, (size_t)plaintext_length,
240
237
                                      plaintext_capacity);
241
238
    if (plaintext_capacity == 0){
242
239
        perror("adjustbuffer");
290
287
  fprintf(stderr, "GnuTLS: %s", string);
291
288
}
292
289
 
293
 
static int init_gnutls_global(mandos_context *mc,
294
 
                              const char *pubkeyfile,
295
 
                              const char *seckeyfile){
 
290
static int init_gnutls_global(mandos_context *mc){
296
291
  int ret;
297
292
  
298
293
  if(debug){
319
314
      != GNUTLS_E_SUCCESS) {
320
315
    fprintf (stderr, "GnuTLS memory error: %s\n",
321
316
             safer_gnutls_strerror(ret));
322
 
    gnutls_global_deinit ();
323
317
    return -1;
324
318
  }
325
319
  
337
331
            " '%s')\n", ret, pubkeyfile, seckeyfile);
338
332
    fprintf(stdout, "The GnuTLS error is: %s\n",
339
333
            safer_gnutls_strerror(ret));
340
 
    goto globalfail;
 
334
    return -1;
341
335
  }
342
336
  
343
337
  /* GnuTLS server initialization */
345
339
  if (ret != GNUTLS_E_SUCCESS) {
346
340
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
347
341
             " %s\n", safer_gnutls_strerror(ret));
348
 
    goto globalfail;
 
342
    return -1;
349
343
  }
350
344
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
351
345
  if (ret != GNUTLS_E_SUCCESS) {
352
346
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
353
347
             safer_gnutls_strerror(ret));
354
 
    goto globalfail;
 
348
    return -1;
355
349
  }
356
350
  
357
351
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
358
352
 
359
353
  return 0;
360
 
 
361
 
 globalfail:
362
 
 
363
 
  gnutls_certificate_free_credentials (mc->cred);
364
 
  gnutls_global_deinit ();
365
 
  return -1;
366
 
 
367
354
}
368
355
 
369
 
static int init_gnutls_session(mandos_context *mc,
370
 
                               gnutls_session_t *session){
 
356
static int init_gnutls_session(mandos_context *mc, gnutls_session_t *session){
371
357
  int ret;
372
358
  /* GnuTLS session creation */
373
359
  ret = gnutls_init(session, GNUTLS_SERVER);
383
369
      fprintf(stderr, "Syntax error at: %s\n", err);
384
370
      fprintf(stderr, "GnuTLS error: %s\n",
385
371
              safer_gnutls_strerror(ret));
386
 
      gnutls_deinit (*session);
387
372
      return -1;
388
373
    }
389
374
  }
393
378
  if (ret != GNUTLS_E_SUCCESS) {
394
379
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
395
380
            safer_gnutls_strerror(ret));
396
 
    gnutls_deinit (*session);
397
381
    return -1;
398
382
  }
399
383
  
415
399
                                      AvahiIfIndex if_index,
416
400
                                      mandos_context *mc){
417
401
  int ret, tcp_sd;
418
 
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
 
402
  struct sockaddr_in6 to;
419
403
  char *buffer = NULL;
420
404
  char *decrypted_buffer;
421
405
  size_t buffer_length = 0;
425
409
  int retval = 0;
426
410
  char interface[IF_NAMESIZE];
427
411
  gnutls_session_t session;
 
412
  gnutls_dh_params_t dh_params;
428
413
  
429
414
  ret = init_gnutls_session (mc, &session);
430
415
  if (ret != 0){
451
436
  }
452
437
  
453
438
  memset(&to,0,sizeof(to));     /* Spurious warning */
454
 
  to.in6.sin6_family = AF_INET6;
 
439
  to.sin6_family = AF_INET6;
455
440
  /* It would be nice to have a way to detect if we were passed an
456
441
     IPv4 address here.   Now we assume an IPv6 address. */
457
 
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
 
442
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
458
443
  if (ret < 0 ){
459
444
    perror("inet_pton");
460
445
    return -1;
463
448
    fprintf(stderr, "Bad address: %s\n", ip);
464
449
    return -1;
465
450
  }
466
 
  to.in6.sin6_port = htons(port);       /* Spurious warning */
 
451
  to.sin6_port = htons(port);   /* Spurious warning */
467
452
  
468
 
  to.in6.sin6_scope_id = (uint32_t)if_index;
 
453
  to.sin6_scope_id = (uint32_t)if_index;
469
454
  
470
455
  if(debug){
471
456
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
472
457
    char addrstr[INET6_ADDRSTRLEN] = "";
473
 
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
 
458
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
474
459
                 sizeof(addrstr)) == NULL){
475
460
      perror("inet_ntop");
476
461
    } else {
480
465
    }
481
466
  }
482
467
  
483
 
  ret = connect(tcp_sd, &to.in, sizeof(to));
 
468
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
484
469
  if (ret < 0){
485
470
    perror("connect");
486
471
    return -1;
535
520
  }
536
521
 
537
522
  while(true){
538
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
539
 
                                   buffer_capacity);
 
523
    buffer_capacity = adjustbuffer(&buffer, buffer_length, buffer_capacity);
540
524
    if (buffer_capacity == 0){
541
525
      perror("adjustbuffer");
542
526
      retval = -1;
613
597
  free(buffer);
614
598
  close(tcp_sd);
615
599
  gnutls_deinit (session);
 
600
  gnutls_certificate_free_credentials (mc->cred);
 
601
  gnutls_global_deinit ();
616
602
  return retval;
617
603
}
618
604
 
745
731
    gid_t gid;
746
732
    char *connect_to = NULL;
747
733
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
748
 
    const char *pubkeyfile = "pubkey.txt";
749
 
    const char *seckeyfile = "seckey.txt";
750
734
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
751
735
                          .dh_bits = 1024, .priority = "SECURE256"};
752
 
    bool gnutls_initalized = false;
753
736
    
754
737
    {
755
 
      struct argp_option options[] = {
756
 
        { .name = "debug", .key = 128,
757
 
          .doc = "Debug mode", .group = 3 },
758
 
        { .name = "connect", .key = 'c',
759
 
          .arg = "IP",
760
 
          .doc = "Connect directly to a sepcified mandos server",
761
 
          .group = 1 },
762
 
        { .name = "interface", .key = 'i',
763
 
          .arg = "INTERFACE",
764
 
          .doc = "Interface that Avahi will conntect through",
765
 
          .group = 1 },
766
 
        { .name = "keydir", .key = 'd',
767
 
          .arg = "KEYDIR",
768
 
          .doc = "Directory where the openpgp keyring is",
769
 
          .group = 1 },
770
 
        { .name = "seckey", .key = 's',
771
 
          .arg = "SECKEY",
772
 
          .doc = "Secret openpgp key for gnutls authentication",
773
 
          .group = 1 },
774
 
        { .name = "pubkey", .key = 'p',
775
 
          .arg = "PUBKEY",
776
 
          .doc = "Public openpgp key for gnutls authentication",
777
 
          .group = 2 },
778
 
        { .name = "dh-bits", .key = 129,
779
 
          .arg = "BITS",
780
 
          .doc = "dh-bits to use in gnutls communication",
781
 
          .group = 2 },
782
 
        { .name = "priority", .key = 130,
783
 
          .arg = "PRIORITY",
784
 
          .doc = "GNUTLS priority", .group = 1 },
785
 
        { .name = NULL }
786
 
      };
787
 
 
788
 
      
789
 
      error_t parse_opt (int key, char *arg,
790
 
                         struct argp_state *state) {
791
 
        /* Get the INPUT argument from `argp_parse', which we know is
792
 
           a pointer to our plugin list pointer. */
793
 
        switch (key) {
794
 
        case 128:
795
 
          debug = true;
 
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;
796
765
          break;
797
766
        case 'c':
798
 
          connect_to = arg;
799
 
          break;
800
 
        case 'i':
801
 
          interface = arg;
 
767
          connect_to = optarg;
802
768
          break;
803
769
        case 'd':
804
 
          keydir = arg;
 
770
          keydir = optarg;
 
771
          break;
 
772
        case 'p':
 
773
          pubkeyfile = optarg;
805
774
          break;
806
775
        case 's':
807
 
          seckeyfile = arg;
808
 
          break;
809
 
        case 'p':
810
 
          pubkeyfile = arg;
811
 
          break;
812
 
        case 129:
 
776
          seckeyfile = optarg;
 
777
          break;
 
778
        case 'D':
813
779
          errno = 0;
814
 
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
 
780
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
815
781
          if (errno){
816
782
            perror("strtol");
817
783
            exit(EXIT_FAILURE);
818
784
          }
819
785
          break;
820
 
        case 130:
821
 
          mc.priority = arg;
822
 
          break;
823
 
        case ARGP_KEY_ARG:
824
 
          argp_usage (state);
825
 
          break;
826
 
          case ARGP_KEY_END:
827
 
            break;
 
786
        case 'P':
 
787
          mc.priority = optarg;
 
788
          break;
 
789
        case '?':
828
790
        default:
829
 
          return ARGP_ERR_UNKNOWN;
 
791
          /* getopt_long() has already printed a message about the
 
792
             unrcognized option, so just exit. */
 
793
          exit(EXIT_FAILURE);
830
794
        }
831
 
        return 0;
832
795
      }
833
 
 
834
 
      struct argp argp = { .options = options, .parser = parse_opt,
835
 
                           .args_doc = "",
836
 
                           .doc = "Mandos client -- Get and decrypt"
837
 
                           " passwords from mandos server" };
838
 
      argp_parse (&argp, argc, argv, 0, 0, NULL);
 
796
      /* Set the global debug flag from the temporary int */
 
797
      debug = debug_int ? true : false;
839
798
    }
840
 
      
 
799
    
841
800
    pubkeyfile = combinepath(keydir, pubkeyfile);
842
801
    if (pubkeyfile == NULL){
843
802
      perror("combinepath");
851
810
      goto end;
852
811
    }
853
812
 
854
 
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
 
813
    ret = init_gnutls_global(&mc);
855
814
    if (ret == -1){
856
815
      fprintf(stderr, "init_gnutls_global\n");
857
816
      goto end;
858
 
    } else {
859
 
      gnutls_initalized = true;
860
817
    }
861
818
 
862
819
    uid = getuid();
1011
968
        avahi_simple_poll_free(mc.simple_poll);
1012
969
    free(pubkeyfile);
1013
970
    free(seckeyfile);
1014
 
 
1015
 
    if (gnutls_initalized){
1016
 
      gnutls_certificate_free_credentials (mc.cred);
1017
 
      gnutls_global_deinit ();
1018
 
    }
1019
971
    
1020
972
    return exitcode;
1021
973
}