/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

mandosclient
        changed to argp

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
#define BUFFER_SIZE 256
76
76
 
77
77
bool debug = false;
78
 
static const char *keydir = "/conf/conf.d/mandos";
79
 
static const char mandos_protocol_version[] = "1";
 
78
const char *keydir = "/conf/conf.d/mandos";
80
79
const char *argp_program_version = "mandosclient 0.9";
81
80
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
81
const char mandos_protocol_version[] = "1";
82
82
 
83
 
/* Used for passing in values through the Avahi callback functions */
 
83
/* Used for passing in values through all the callback functions */
84
84
typedef struct {
85
85
  AvahiSimplePoll *simple_poll;
86
86
  AvahiServer *server;
90
90
  const char *priority;
91
91
} mandos_context;
92
92
 
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
93
size_t adjustbuffer(char **buffer, size_t buffer_length,
99
94
                  size_t buffer_capacity){
100
95
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
235
230
  
236
231
  *plaintext = NULL;
237
232
  while(true){
238
 
    plaintext_capacity = adjustbuffer(plaintext,
239
 
                                      (size_t)plaintext_length,
 
233
    plaintext_capacity = adjustbuffer(plaintext, (size_t)plaintext_length,
240
234
                                      plaintext_capacity);
241
235
    if (plaintext_capacity == 0){
242
236
        perror("adjustbuffer");
358
352
  return 0;
359
353
}
360
354
 
361
 
static int init_gnutls_session(mandos_context *mc,
362
 
                               gnutls_session_t *session){
 
355
static int init_gnutls_session(mandos_context *mc, gnutls_session_t *session){
363
356
  int ret;
364
357
  /* GnuTLS session creation */
365
358
  ret = gnutls_init(session, GNUTLS_SERVER);
405
398
                                      AvahiIfIndex if_index,
406
399
                                      mandos_context *mc){
407
400
  int ret, tcp_sd;
408
 
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
 
401
  struct sockaddr_in6 to;
409
402
  char *buffer = NULL;
410
403
  char *decrypted_buffer;
411
404
  size_t buffer_length = 0;
415
408
  int retval = 0;
416
409
  char interface[IF_NAMESIZE];
417
410
  gnutls_session_t session;
 
411
  gnutls_dh_params_t dh_params;
418
412
  
419
413
  ret = init_gnutls_session (mc, &session);
420
414
  if (ret != 0){
441
435
  }
442
436
  
443
437
  memset(&to,0,sizeof(to));     /* Spurious warning */
444
 
  to.in6.sin6_family = AF_INET6;
 
438
  to.sin6_family = AF_INET6;
445
439
  /* It would be nice to have a way to detect if we were passed an
446
440
     IPv4 address here.   Now we assume an IPv6 address. */
447
 
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
 
441
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
448
442
  if (ret < 0 ){
449
443
    perror("inet_pton");
450
444
    return -1;
453
447
    fprintf(stderr, "Bad address: %s\n", ip);
454
448
    return -1;
455
449
  }
456
 
  to.in6.sin6_port = htons(port);       /* Spurious warning */
 
450
  to.sin6_port = htons(port);   /* Spurious warning */
457
451
  
458
 
  to.in6.sin6_scope_id = (uint32_t)if_index;
 
452
  to.sin6_scope_id = (uint32_t)if_index;
459
453
  
460
454
  if(debug){
461
455
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
462
456
    char addrstr[INET6_ADDRSTRLEN] = "";
463
 
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
 
457
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
464
458
                 sizeof(addrstr)) == NULL){
465
459
      perror("inet_ntop");
466
460
    } else {
470
464
    }
471
465
  }
472
466
  
473
 
  ret = connect(tcp_sd, &to.in, sizeof(to));
 
467
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
474
468
  if (ret < 0){
475
469
    perror("connect");
476
470
    return -1;
525
519
  }
526
520
 
527
521
  while(true){
528
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
529
 
                                   buffer_capacity);
 
522
    buffer_capacity = adjustbuffer(&buffer, buffer_length, buffer_capacity);
530
523
    if (buffer_capacity == 0){
531
524
      perror("adjustbuffer");
532
525
      retval = -1;
748
741
          .doc = "Debug mode", .group = 3 },
749
742
        { .name = "connect", .key = 'c',
750
743
          .arg = "IP",
751
 
          .doc = "Connect directly to a sepcified mandos server",
752
 
          .group = 1 },
 
744
          .doc = "Connect directly to a sepcified mandos server", .group = 1 },
753
745
        { .name = "interface", .key = 'i',
754
746
          .arg = "INTERFACE",
755
 
          .doc = "Interface that Avahi will conntect through",
756
 
          .group = 1 },
 
747
          .doc = "Interface that Avahi will conntect through", .group = 1 },
757
748
        { .name = "keydir", .key = 'd',
758
749
          .arg = "KEYDIR",
759
 
          .doc = "Directory where the openpgp keyring is",
760
 
          .group = 1 },
 
750
          .doc = "Directory where the openpgp keyring is", .group = 1 },
761
751
        { .name = "seckey", .key = 's',
762
752
          .arg = "SECKEY",
763
 
          .doc = "Secret openpgp key for gnutls authentication",
764
 
          .group = 1 },
 
753
          .doc = "Secret openpgp key for gnutls authentication", .group = 1 },
765
754
        { .name = "pubkey", .key = 'p',
766
755
          .arg = "PUBKEY",
767
 
          .doc = "Public openpgp key for gnutls authentication",
768
 
          .group = 2 },
 
756
          .doc = "Public openpgp key for gnutls authentication", .group = 2 },
769
757
        { .name = "dh-bits", .key = 129,
770
758
          .arg = "BITS",
771
 
          .doc = "dh-bits to use in gnutls communication",
772
 
          .group = 2 },
 
759
          .doc = "dh-bits to use in gnutls communication", .group = 2 },
773
760
        { .name = "priority", .key = 130,
774
761
          .arg = "PRIORITY",
775
762
          .doc = "GNUTLS priority", .group = 1 },
777
764
      };
778
765
 
779
766
      
780
 
      error_t parse_opt (int key, char *arg,
781
 
                         struct argp_state *state) {
782
 
        /* Get the INPUT argument from `argp_parse', which we know is
783
 
           a pointer to our plugin list pointer. */
 
767
      error_t parse_opt (int key, char *arg, struct argp_state *state) {
 
768
        /* Get the INPUT argument from `argp_parse', which we know is a
 
769
           pointer to our plugin list pointer. */
784
770
        switch (key) {
785
771
        case 128:
786
772
          debug = true;
824
810
 
825
811
      struct argp argp = { .options = options, .parser = parse_opt,
826
812
                           .args_doc = "",
827
 
                           .doc = "Mandos client -- Get and decrypt"
828
 
                           " passwords from mandos server" };
 
813
                           .doc = "Mandos client -- Get and decrypt passwords from mandos server" };
829
814
      argp_parse (&argp, argc, argv, 0, 0, NULL);
830
815
    }
831
816