/mandos/release

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

« back to all changes in this revision

Viewing changes to plugins.d/mandosclient.c

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
36
36
 
37
 
#include <stdio.h>              /* fprintf(), stderr, fwrite(), stdout,
38
 
                                   ferror() */
39
 
#include <stdint.h>             /* uint16_t, uint32_t */
40
 
#include <stddef.h>             /* NULL, size_t, ssize_t */
41
 
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
42
 
                                   srand() */
43
 
#include <stdbool.h>            /* bool, true */
44
 
#include <string.h>             /* memset(), strcmp(), strlen(),
45
 
                                   strerror(), memcpy(), strcpy() */
46
 
#include <sys/ioctl.h>          /* ioctl */
47
 
#include <net/if.h>             /* ifreq, SIOCGIFFLAGS, SIOCSIFFLAGS,
48
 
                                   IFF_UP */
49
 
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
50
 
                                   sockaddr_in6, PF_INET6,
51
 
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
52
 
                                   uid_t, gid_t */
53
 
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
54
 
                                   struct in6_addr, inet_pton(),
55
 
                                   connect() */
56
 
#include <assert.h>             /* assert() */
57
 
#include <errno.h>              /* perror(), errno */
58
 
#include <time.h>               /* time() */
 
37
#include <stdio.h>
 
38
#include <assert.h>
 
39
#include <stdlib.h>
 
40
#include <time.h>
 
41
#include <net/if.h>             /* if_nametoindex */
 
42
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
 
43
                                   SIOCSIFFLAGS */
59
44
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
60
 
                                   SIOCSIFFLAGS, if_indextoname(),
61
 
                                   if_nametoindex(), IF_NAMESIZE */
62
 
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
63
 
                                   getuid(), getgid(), setuid(),
64
 
                                   setgid() */
65
 
#include <netinet/in.h>
66
 
#include <arpa/inet.h>          /* inet_pton(), htons */
67
 
#include <iso646.h>             /* not, and */
68
 
#include <argp.h>               /* struct argp_option, error_t, struct
69
 
                                   argp_state, struct argp,
70
 
                                   argp_parse(), ARGP_KEY_ARG,
71
 
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
 
45
                                   SIOCSIFFLAGS */
72
46
 
73
 
/* Avahi */
74
 
/* All Avahi types, constants and functions
75
 
 Avahi*, avahi_*,
76
 
 AVAHI_* */
77
47
#include <avahi-core/core.h>
78
48
#include <avahi-core/lookup.h>
79
49
#include <avahi-core/log.h>
81
51
#include <avahi-common/malloc.h>
82
52
#include <avahi-common/error.h>
83
53
 
84
 
/* GnuTLS */
85
 
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and functions
86
 
                                   gnutls_*
87
 
                                   init_gnutls_session(),
88
 
                                   GNUTLS_* */
89
 
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
90
 
                                   GNUTLS_OPENPGP_FMT_BASE64 */
 
54
/* Mandos client part */
 
55
#include <sys/types.h>          /* socket(), inet_pton() */
 
56
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
 
57
                                   struct in6_addr, inet_pton() */
 
58
#include <gnutls/gnutls.h>      /* All GnuTLS stuff */
 
59
#include <gnutls/openpgp.h>     /* GnuTLS with openpgp stuff */
91
60
 
 
61
#include <unistd.h>             /* close() */
 
62
#include <netinet/in.h>
 
63
#include <stdbool.h>            /* true */
 
64
#include <string.h>             /* memset */
 
65
#include <arpa/inet.h>          /* inet_pton() */
 
66
#include <iso646.h>             /* not */
 
67
#include <net/if.h>             /* IF_NAMESIZE */
 
68
#include <argp.h>               /* struct argp_option,
 
69
                                   struct argp_state, struct argp,
 
70
                                   argp_parse() */
92
71
/* GPGME */
93
 
#include <gpgme.h>              /* All GPGME types, constants and functions
94
 
                                   gpgme_*
95
 
                                   GPGME_PROTOCOL_OpenPGP,
96
 
                                   GPG_ERR_NO_* */
 
72
#include <errno.h>              /* perror() */
 
73
#include <gpgme.h>
97
74
 
98
75
#define BUFFER_SIZE 256
99
76
 
100
77
bool debug = false;
101
 
static const char *keydir = "/conf/conf.d/mandos";
102
 
static const char mandos_protocol_version[] = "1";
103
 
const char *argp_program_version = "mandosclient 1.0";
 
78
const char *keydir = "/conf/conf.d/mandos";
 
79
const char *argp_program_version = "mandosclient 0.9";
104
80
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
81
const char mandos_protocol_version[] = "1";
105
82
 
106
 
/* Used for passing in values through the Avahi callback functions */
 
83
/* Used for passing in values through all the callback functions */
107
84
typedef struct {
108
85
  AvahiSimplePoll *simple_poll;
109
86
  AvahiServer *server;
113
90
  const char *priority;
114
91
} mandos_context;
115
92
 
116
 
/*
117
 
 * Make room in "buffer" for at least BUFFER_SIZE additional bytes.
118
 
 * "buffer_capacity" is how much is currently allocated,
119
 
 * "buffer_length" is how much is already used.
120
 
 */
121
93
size_t adjustbuffer(char **buffer, size_t buffer_length,
122
94
                  size_t buffer_capacity){
123
95
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
258
230
  
259
231
  *plaintext = NULL;
260
232
  while(true){
261
 
    plaintext_capacity = adjustbuffer(plaintext,
262
 
                                      (size_t)plaintext_length,
 
233
    plaintext_capacity = adjustbuffer(plaintext, (size_t)plaintext_length,
263
234
                                      plaintext_capacity);
264
235
    if (plaintext_capacity == 0){
265
236
        perror("adjustbuffer");
321
292
  if(debug){
322
293
    fprintf(stderr, "Initializing GnuTLS\n");
323
294
  }
324
 
  
325
 
  ret = gnutls_global_init();
326
 
  if (ret != GNUTLS_E_SUCCESS) {
 
295
 
 
296
  if ((ret = gnutls_global_init ())
 
297
      != GNUTLS_E_SUCCESS) {
327
298
    fprintf (stderr, "GnuTLS global_init: %s\n",
328
299
             safer_gnutls_strerror(ret));
329
300
    return -1;
338
309
  }
339
310
  
340
311
  /* OpenPGP credentials */
341
 
  gnutls_certificate_allocate_credentials(&mc->cred);
342
 
  if (ret != GNUTLS_E_SUCCESS){
 
312
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
313
      != GNUTLS_E_SUCCESS) {
343
314
    fprintf (stderr, "GnuTLS memory error: %s\n",
344
315
             safer_gnutls_strerror(ret));
345
 
    gnutls_global_deinit ();
346
316
    return -1;
347
317
  }
348
318
  
360
330
            " '%s')\n", ret, pubkeyfile, seckeyfile);
361
331
    fprintf(stdout, "The GnuTLS error is: %s\n",
362
332
            safer_gnutls_strerror(ret));
363
 
    goto globalfail;
 
333
    return -1;
364
334
  }
365
335
  
366
336
  /* GnuTLS server initialization */
368
338
  if (ret != GNUTLS_E_SUCCESS) {
369
339
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
370
340
             " %s\n", safer_gnutls_strerror(ret));
371
 
    goto globalfail;
 
341
    return -1;
372
342
  }
373
343
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
374
344
  if (ret != GNUTLS_E_SUCCESS) {
375
345
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
376
346
             safer_gnutls_strerror(ret));
377
 
    goto globalfail;
 
347
    return -1;
378
348
  }
379
349
  
380
350
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
381
351
 
382
352
  return 0;
383
 
 
384
 
 globalfail:
385
 
 
386
 
  gnutls_certificate_free_credentials(mc->cred);
387
 
  gnutls_global_deinit();
388
 
  return -1;
389
 
 
390
353
}
391
354
 
392
 
static int init_gnutls_session(mandos_context *mc,
393
 
                               gnutls_session_t *session){
 
355
static int init_gnutls_session(mandos_context *mc, gnutls_session_t *session){
394
356
  int ret;
395
357
  /* GnuTLS session creation */
396
358
  ret = gnutls_init(session, GNUTLS_SERVER);
406
368
      fprintf(stderr, "Syntax error at: %s\n", err);
407
369
      fprintf(stderr, "GnuTLS error: %s\n",
408
370
              safer_gnutls_strerror(ret));
409
 
      gnutls_deinit (*session);
410
371
      return -1;
411
372
    }
412
373
  }
416
377
  if (ret != GNUTLS_E_SUCCESS) {
417
378
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
418
379
            safer_gnutls_strerror(ret));
419
 
    gnutls_deinit (*session);
420
380
    return -1;
421
381
  }
422
382
  
438
398
                                      AvahiIfIndex if_index,
439
399
                                      mandos_context *mc){
440
400
  int ret, tcp_sd;
441
 
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
 
401
  struct sockaddr_in6 to;
442
402
  char *buffer = NULL;
443
403
  char *decrypted_buffer;
444
404
  size_t buffer_length = 0;
448
408
  int retval = 0;
449
409
  char interface[IF_NAMESIZE];
450
410
  gnutls_session_t session;
 
411
  gnutls_dh_params_t dh_params;
451
412
  
452
413
  ret = init_gnutls_session (mc, &session);
453
414
  if (ret != 0){
474
435
  }
475
436
  
476
437
  memset(&to,0,sizeof(to));     /* Spurious warning */
477
 
  to.in6.sin6_family = AF_INET6;
 
438
  to.sin6_family = AF_INET6;
478
439
  /* It would be nice to have a way to detect if we were passed an
479
440
     IPv4 address here.   Now we assume an IPv6 address. */
480
 
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
 
441
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
481
442
  if (ret < 0 ){
482
443
    perror("inet_pton");
483
444
    return -1;
486
447
    fprintf(stderr, "Bad address: %s\n", ip);
487
448
    return -1;
488
449
  }
489
 
  to.in6.sin6_port = htons(port);       /* Spurious warning */
 
450
  to.sin6_port = htons(port);   /* Spurious warning */
490
451
  
491
 
  to.in6.sin6_scope_id = (uint32_t)if_index;
 
452
  to.sin6_scope_id = (uint32_t)if_index;
492
453
  
493
454
  if(debug){
494
455
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
495
456
    char addrstr[INET6_ADDRSTRLEN] = "";
496
 
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
 
457
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
497
458
                 sizeof(addrstr)) == NULL){
498
459
      perror("inet_ntop");
499
460
    } else {
503
464
    }
504
465
  }
505
466
  
506
 
  ret = connect(tcp_sd, &to.in, sizeof(to));
 
467
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
507
468
  if (ret < 0){
508
469
    perror("connect");
509
470
    return -1;
538
499
  }
539
500
  
540
501
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
541
 
 
542
 
  do{
543
 
    ret = gnutls_handshake (session);
544
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
502
  
 
503
  ret = gnutls_handshake (session);
545
504
  
546
505
  if (ret != GNUTLS_E_SUCCESS){
547
506
    if(debug){
560
519
  }
561
520
 
562
521
  while(true){
563
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
564
 
                                   buffer_capacity);
 
522
    buffer_capacity = adjustbuffer(&buffer, buffer_length, buffer_capacity);
565
523
    if (buffer_capacity == 0){
566
524
      perror("adjustbuffer");
567
525
      retval = -1;
579
537
      case GNUTLS_E_AGAIN:
580
538
        break;
581
539
      case GNUTLS_E_REHANDSHAKE:
582
 
        do{
583
 
          ret = gnutls_handshake (session);
584
 
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
540
        ret = gnutls_handshake (session);
585
541
        if (ret < 0){
586
542
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
587
543
          gnutls_perror (ret);
640
596
  free(buffer);
641
597
  close(tcp_sd);
642
598
  gnutls_deinit (session);
 
599
  gnutls_certificate_free_credentials (mc->cred);
 
600
  gnutls_global_deinit ();
643
601
  return retval;
644
602
}
645
603
 
776
734
    const char *seckeyfile = "seckey.txt";
777
735
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
778
736
                          .dh_bits = 1024, .priority = "SECURE256"};
779
 
    bool gnutls_initalized = false;
780
737
    
781
738
    {
782
739
      struct argp_option options[] = {
784
741
          .doc = "Debug mode", .group = 3 },
785
742
        { .name = "connect", .key = 'c',
786
743
          .arg = "IP",
787
 
          .doc = "Connect directly to a sepcified mandos server",
788
 
          .group = 1 },
 
744
          .doc = "Connect directly to a sepcified mandos server", .group = 1 },
789
745
        { .name = "interface", .key = 'i',
790
746
          .arg = "INTERFACE",
791
 
          .doc = "Interface that Avahi will conntect through",
792
 
          .group = 1 },
 
747
          .doc = "Interface that Avahi will conntect through", .group = 1 },
793
748
        { .name = "keydir", .key = 'd',
794
749
          .arg = "KEYDIR",
795
 
          .doc = "Directory where the openpgp keyring is",
796
 
          .group = 1 },
 
750
          .doc = "Directory where the openpgp keyring is", .group = 1 },
797
751
        { .name = "seckey", .key = 's',
798
752
          .arg = "SECKEY",
799
 
          .doc = "Secret openpgp key for gnutls authentication",
800
 
          .group = 1 },
 
753
          .doc = "Secret openpgp key for gnutls authentication", .group = 1 },
801
754
        { .name = "pubkey", .key = 'p',
802
755
          .arg = "PUBKEY",
803
 
          .doc = "Public openpgp key for gnutls authentication",
804
 
          .group = 2 },
 
756
          .doc = "Public openpgp key for gnutls authentication", .group = 2 },
805
757
        { .name = "dh-bits", .key = 129,
806
758
          .arg = "BITS",
807
 
          .doc = "dh-bits to use in gnutls communication",
808
 
          .group = 2 },
 
759
          .doc = "dh-bits to use in gnutls communication", .group = 2 },
809
760
        { .name = "priority", .key = 130,
810
761
          .arg = "PRIORITY",
811
762
          .doc = "GNUTLS priority", .group = 1 },
813
764
      };
814
765
 
815
766
      
816
 
      error_t parse_opt (int key, char *arg,
817
 
                         struct argp_state *state) {
818
 
        /* Get the INPUT argument from `argp_parse', which we know is
819
 
           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. */
820
770
        switch (key) {
821
771
        case 128:
822
772
          debug = true;
860
810
 
861
811
      struct argp argp = { .options = options, .parser = parse_opt,
862
812
                           .args_doc = "",
863
 
                           .doc = "Mandos client -- Get and decrypt"
864
 
                           " passwords from mandos server" };
865
 
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
866
 
      if (ret == ARGP_ERR_UNKNOWN){
867
 
        fprintf(stderr, "Unkown error while parsing arguments\n");
868
 
        exitcode = EXIT_FAILURE;
869
 
        goto end;
870
 
      }
 
813
                           .doc = "Mandos client -- Get and decrypt passwords from mandos server" };
 
814
      argp_parse (&argp, argc, argv, 0, 0, NULL);
871
815
    }
872
816
      
873
817
    pubkeyfile = combinepath(keydir, pubkeyfile);
887
831
    if (ret == -1){
888
832
      fprintf(stderr, "init_gnutls_global\n");
889
833
      goto end;
890
 
    } else {
891
 
      gnutls_initalized = true;
892
834
    }
893
835
 
894
836
    uid = getuid();
1043
985
        avahi_simple_poll_free(mc.simple_poll);
1044
986
    free(pubkeyfile);
1045
987
    free(seckeyfile);
1046
 
 
1047
 
    if (gnutls_initalized){
1048
 
      gnutls_certificate_free_credentials(mc.cred);
1049
 
      gnutls_global_deinit ();
1050
 
    }
1051
988
    
1052
989
    return exitcode;
1053
990
}