/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
35
#include <stdio.h>
38
36
#include <assert.h>
39
37
#include <stdlib.h>
65
63
#include <arpa/inet.h>          /* inet_pton() */
66
64
#include <iso646.h>             /* not */
67
65
#include <net/if.h>             /* IF_NAMESIZE */
68
 
#include <argp.h>               /* struct argp_option,
69
 
                                   struct argp_state, struct argp,
70
 
                                   argp_parse() */
 
66
 
71
67
/* GPGME */
72
68
#include <errno.h>              /* perror() */
73
69
#include <gpgme.h>
74
70
 
 
71
/* getopt_long */
 
72
#include <getopt.h>
 
73
 
75
74
#define BUFFER_SIZE 256
76
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
 
77
80
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
81
 
83
 
/* Used for passing in values through the Avahi callback functions */
 
82
/* Used for passing in values through all the callback functions */
84
83
typedef struct {
85
84
  AvahiSimplePoll *simple_poll;
86
85
  AvahiServer *server;
87
86
  gnutls_certificate_credentials_t cred;
88
87
  unsigned int dh_bits;
89
 
  gnutls_dh_params_t dh_params;
90
88
  const char *priority;
91
89
} mandos_context;
92
90
 
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
 
size_t adjustbuffer(char **buffer, size_t buffer_length,
99
 
                  size_t buffer_capacity){
100
 
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
101
 
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
102
 
    if (buffer == NULL){
103
 
      return 0;
104
 
    }
105
 
    buffer_capacity += BUFFER_SIZE;
106
 
  }
107
 
  return buffer_capacity;
108
 
}
109
 
 
110
91
/* 
111
92
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
112
93
 * Returns -1 on error
235
216
  
236
217
  *plaintext = NULL;
237
218
  while(true){
238
 
    plaintext_capacity = adjustbuffer(plaintext,
239
 
                                      (size_t)plaintext_length,
240
 
                                      plaintext_capacity);
241
 
    if (plaintext_capacity == 0){
242
 
        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");
243
225
        plaintext_length = -1;
244
226
        goto decrypt_end;
 
227
      }
 
228
      plaintext_capacity += BUFFER_SIZE;
245
229
    }
246
230
    
247
231
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
290
274
  fprintf(stderr, "GnuTLS: %s", string);
291
275
}
292
276
 
293
 
static int init_gnutls_global(mandos_context *mc,
294
 
                              const char *pubkeyfile,
295
 
                              const char *seckeyfile){
 
277
static int initgnutls(mandos_context *mc, gnutls_session_t *session,
 
278
                      gnutls_dh_params_t *dh_params){
296
279
  int ret;
297
280
  
298
281
  if(debug){
319
302
      != GNUTLS_E_SUCCESS) {
320
303
    fprintf (stderr, "GnuTLS memory error: %s\n",
321
304
             safer_gnutls_strerror(ret));
322
 
    gnutls_global_deinit ();
323
305
    return -1;
324
306
  }
325
307
  
337
319
            " '%s')\n", ret, pubkeyfile, seckeyfile);
338
320
    fprintf(stdout, "The GnuTLS error is: %s\n",
339
321
            safer_gnutls_strerror(ret));
340
 
    goto globalfail;
 
322
    return -1;
341
323
  }
342
324
  
343
325
  /* GnuTLS server initialization */
344
 
  ret = gnutls_dh_params_init(&mc->dh_params);
 
326
  ret = gnutls_dh_params_init(dh_params);
345
327
  if (ret != GNUTLS_E_SUCCESS) {
346
328
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
347
329
             " %s\n", safer_gnutls_strerror(ret));
348
 
    goto globalfail;
 
330
    return -1;
349
331
  }
350
 
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
 
332
  ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
351
333
  if (ret != GNUTLS_E_SUCCESS) {
352
334
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
353
335
             safer_gnutls_strerror(ret));
354
 
    goto globalfail;
 
336
    return -1;
355
337
  }
356
338
  
357
 
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
358
 
 
359
 
  return 0;
360
 
 
361
 
 globalfail:
362
 
 
363
 
  gnutls_certificate_free_credentials (mc->cred);
364
 
  gnutls_global_deinit ();
365
 
  return -1;
366
 
 
367
 
}
368
 
 
369
 
static int init_gnutls_session(mandos_context *mc,
370
 
                               gnutls_session_t *session){
371
 
  int ret;
 
339
  gnutls_certificate_set_dh_params(mc->cred, *dh_params);
 
340
  
372
341
  /* GnuTLS session creation */
373
342
  ret = gnutls_init(session, GNUTLS_SERVER);
374
343
  if (ret != GNUTLS_E_SUCCESS){
383
352
      fprintf(stderr, "Syntax error at: %s\n", err);
384
353
      fprintf(stderr, "GnuTLS error: %s\n",
385
354
              safer_gnutls_strerror(ret));
386
 
      gnutls_deinit (*session);
387
355
      return -1;
388
356
    }
389
357
  }
393
361
  if (ret != GNUTLS_E_SUCCESS) {
394
362
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
395
363
            safer_gnutls_strerror(ret));
396
 
    gnutls_deinit (*session);
397
364
    return -1;
398
365
  }
399
366
  
415
382
                                      AvahiIfIndex if_index,
416
383
                                      mandos_context *mc){
417
384
  int ret, tcp_sd;
418
 
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
 
385
  struct sockaddr_in6 to;
419
386
  char *buffer = NULL;
420
387
  char *decrypted_buffer;
421
388
  size_t buffer_length = 0;
422
389
  size_t buffer_capacity = 0;
423
390
  ssize_t decrypted_buffer_size;
424
 
  size_t written;
 
391
  size_t written = 0;
425
392
  int retval = 0;
426
393
  char interface[IF_NAMESIZE];
427
394
  gnutls_session_t session;
 
395
  gnutls_dh_params_t dh_params;
428
396
  
429
 
  ret = init_gnutls_session (mc, &session);
 
397
  ret = initgnutls (mc, &session, &dh_params);
430
398
  if (ret != 0){
431
399
    return -1;
432
400
  }
451
419
  }
452
420
  
453
421
  memset(&to,0,sizeof(to));     /* Spurious warning */
454
 
  to.in6.sin6_family = AF_INET6;
 
422
  to.sin6_family = AF_INET6;
455
423
  /* It would be nice to have a way to detect if we were passed an
456
424
     IPv4 address here.   Now we assume an IPv6 address. */
457
 
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
 
425
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
458
426
  if (ret < 0 ){
459
427
    perror("inet_pton");
460
428
    return -1;
463
431
    fprintf(stderr, "Bad address: %s\n", ip);
464
432
    return -1;
465
433
  }
466
 
  to.in6.sin6_port = htons(port);       /* Spurious warning */
 
434
  to.sin6_port = htons(port);   /* Spurious warning */
467
435
  
468
 
  to.in6.sin6_scope_id = (uint32_t)if_index;
 
436
  to.sin6_scope_id = (uint32_t)if_index;
469
437
  
470
438
  if(debug){
471
439
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
472
440
    char addrstr[INET6_ADDRSTRLEN] = "";
473
 
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
 
441
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
474
442
                 sizeof(addrstr)) == NULL){
475
443
      perror("inet_ntop");
476
444
    } else {
480
448
    }
481
449
  }
482
450
  
483
 
  ret = connect(tcp_sd, &to.in, sizeof(to));
 
451
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
484
452
  if (ret < 0){
485
453
    perror("connect");
486
454
    return -1;
487
455
  }
488
 
 
489
 
  const char *out = mandos_protocol_version;
490
 
  written = 0;
491
 
  while (true){
492
 
    size_t out_size = strlen(out);
493
 
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
494
 
                                   out_size - written));
495
 
    if (ret == -1){
496
 
      perror("write");
497
 
      retval = -1;
498
 
      goto mandos_end;
499
 
    }
500
 
    written += (size_t)ret;
501
 
    if(written < out_size){
502
 
      continue;
503
 
    } else {
504
 
      if (out == mandos_protocol_version){
505
 
        written = 0;
506
 
        out = "\r\n";
507
 
      } else {
508
 
        break;
509
 
      }
510
 
    }
511
 
  }
512
 
 
 
456
  
513
457
  if(debug){
514
458
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
515
459
  }
535
479
  }
536
480
 
537
481
  while(true){
538
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
539
 
                                   buffer_capacity);
540
 
    if (buffer_capacity == 0){
541
 
      perror("adjustbuffer");
542
 
      retval = -1;
543
 
      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;
544
490
    }
545
491
    
546
492
    ret = gnutls_record_recv(session, buffer+buffer_length,
586
532
                                               &decrypted_buffer,
587
533
                                               keydir);
588
534
    if (decrypted_buffer_size >= 0){
589
 
      written = 0;
590
535
      while(written < (size_t) decrypted_buffer_size){
591
536
        ret = (int)fwrite (decrypted_buffer + written, 1,
592
537
                           (size_t)decrypted_buffer_size - written,
613
558
  free(buffer);
614
559
  close(tcp_sd);
615
560
  gnutls_deinit (session);
 
561
  gnutls_certificate_free_credentials (mc->cred);
 
562
  gnutls_global_deinit ();
616
563
  return retval;
617
564
}
618
565
 
741
688
    const char *interface = "eth0";
742
689
    struct ifreq network;
743
690
    int sd;
744
 
    uid_t uid;
745
 
    gid_t gid;
746
691
    char *connect_to = NULL;
747
692
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
748
 
    const char *pubkeyfile = "pubkey.txt";
749
 
    const char *seckeyfile = "seckey.txt";
750
693
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
751
694
                          .dh_bits = 1024, .priority = "SECURE256"};
752
 
    bool gnutls_initalized = false;
753
695
    
754
696
    {
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;
 
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;
796
724
          break;
797
725
        case 'c':
798
 
          connect_to = arg;
799
 
          break;
800
 
        case 'i':
801
 
          interface = arg;
 
726
          connect_to = optarg;
802
727
          break;
803
728
        case 'd':
804
 
          keydir = arg;
 
729
          keydir = optarg;
 
730
          break;
 
731
        case 'p':
 
732
          pubkeyfile = optarg;
805
733
          break;
806
734
        case 's':
807
 
          seckeyfile = arg;
808
 
          break;
809
 
        case 'p':
810
 
          pubkeyfile = arg;
811
 
          break;
812
 
        case 129:
 
735
          seckeyfile = optarg;
 
736
          break;
 
737
        case 'D':
813
738
          errno = 0;
814
 
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
 
739
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
815
740
          if (errno){
816
741
            perror("strtol");
817
742
            exit(EXIT_FAILURE);
818
743
          }
819
744
          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;
 
745
        case 'P':
 
746
          mc.priority = optarg;
 
747
          break;
 
748
        case '?':
828
749
        default:
829
 
          return ARGP_ERR_UNKNOWN;
 
750
          /* getopt_long() has already printed a message about the
 
751
             unrcognized option, so just exit. */
 
752
          exit(EXIT_FAILURE);
830
753
        }
831
 
        return 0;
832
754
      }
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);
 
755
      /* Set the global debug flag from the temporary int */
 
756
      debug = debug_int ? true : false;
839
757
    }
840
 
      
 
758
    
841
759
    pubkeyfile = combinepath(keydir, pubkeyfile);
842
760
    if (pubkeyfile == NULL){
843
761
      perror("combinepath");
850
768
      perror("combinepath");
851
769
      goto end;
852
770
    }
853
 
 
854
 
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
855
 
    if (ret == -1){
856
 
      fprintf(stderr, "init_gnutls_global\n");
857
 
      goto end;
858
 
    } else {
859
 
      gnutls_initalized = true;
860
 
    }
861
 
 
862
 
    uid = getuid();
863
 
    gid = getgid();
864
 
 
865
 
    ret = setuid(uid);
866
 
    if (ret == -1){
867
 
      perror("setuid");
868
 
    }
869
 
    
870
 
    setgid(gid);
871
 
    if (ret == -1){
872
 
      perror("setgid");
873
 
    }
874
771
    
875
772
    if_index = (AvahiIfIndex) if_nametoindex(interface);
876
773
    if(if_index == 0){
884
781
      char *address = strrchr(connect_to, ':');
885
782
      if(address == NULL){
886
783
        fprintf(stderr, "No colon in address\n");
887
 
        exitcode = EXIT_FAILURE;
888
 
        goto end;
 
784
        exit(EXIT_FAILURE);
889
785
      }
890
786
      errno = 0;
891
787
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
892
788
      if(errno){
893
789
        perror("Bad port number");
894
 
        exitcode = EXIT_FAILURE;
895
 
        goto end;
 
790
        exit(EXIT_FAILURE);
896
791
      }
897
792
      *address = '\0';
898
793
      address = connect_to;
899
794
      ret = start_mandos_communication(address, port, if_index, &mc);
900
795
      if(ret < 0){
901
 
        exitcode = EXIT_FAILURE;
 
796
        exit(EXIT_FAILURE);
902
797
      } else {
903
 
        exitcode = EXIT_SUCCESS;
 
798
        exit(EXIT_SUCCESS);
904
799
      }
905
 
      goto end;
906
800
    }
907
801
    
908
802
    /* If the interface is down, bring it up */
1011
905
        avahi_simple_poll_free(mc.simple_poll);
1012
906
    free(pubkeyfile);
1013
907
    free(seckeyfile);
1014
 
 
1015
 
    if (gnutls_initalized){
1016
 
      gnutls_certificate_free_credentials (mc.cred);
1017
 
      gnutls_global_deinit ();
1018
 
    }
1019
908
    
1020
909
    return exitcode;
1021
910
}