/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:
34
34
 
35
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
36
36
 
37
 
#include <stdio.h>              /* fprintf(), stderr, fwrite(), stdout, ferror() */
38
 
#include <stdint.h>             /* uint16_t, uint32_t */
39
 
#include <stddef.h>             /* NULL, size_t, ssize_t */
40
 
#include <stdlib.h>             /* free() */
41
 
#include <stdbool.h>            /* bool, true */
42
 
#include <string.h>             /* memset(), strcmp(), strlen, strerror() */
43
 
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
44
 
                                   SIOCSIFFLAGS */
45
 
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
46
 
                                   sockaddr_in6, PF_INET6, SOCK_STREAM, INET6_ADDRSTRLEN */
47
 
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
48
 
                                   struct in6_addr, inet_pton(),
49
 
                                   connect() */
 
37
#include <stdio.h>
50
38
#include <assert.h>
51
 
#include <errno.h>              /* perror() */
 
39
#include <stdlib.h>
52
40
#include <time.h>
 
41
#include <net/if.h>             /* if_nametoindex */
 
42
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
 
43
                                   SIOCSIFFLAGS */
53
44
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
54
 
                                   SIOCSIFFLAGS, if_indextoname(),
55
 
                                   if_nametoindex(), IF_NAMESIZE */
56
 
#include <unistd.h>             /* close(), SEEK_SET, off_t, write()*/
57
 
#include <netinet/in.h>
58
 
#include <arpa/inet.h>          /* inet_pton(), htons */
59
 
#include <iso646.h>             /* not */
60
 
#include <argp.h>               /* struct argp_option,
61
 
                                   struct argp_state, struct argp,
62
 
                                   argp_parse() */
 
45
                                   SIOCSIFFLAGS */
63
46
 
64
 
/* Avahi */
65
 
#include <avahi-core/core.h>    /* AvahiSimplePoll, AvahiServer,
66
 
                                   AvahiIfIndex */
 
47
#include <avahi-core/core.h>
67
48
#include <avahi-core/lookup.h>
68
 
#include <avahi-core/log.h>     /* AvahiLogLevel */
 
49
#include <avahi-core/log.h>
69
50
#include <avahi-common/simple-watch.h>
70
51
#include <avahi-common/malloc.h>
71
52
#include <avahi-common/error.h>
72
53
 
73
 
/* GnuTLS */
74
 
#include <gnutls/gnutls.h>      /* gnutls_certificate_credentials_t,
75
 
                                   gnutls_dh_params_t,
76
 
                                   gnutls_strerror(),
77
 
                                   gnutls_global_init(),
78
 
                                   gnutls_global_set_log_level(),
79
 
                                   gnutls_global_set_log_function(),
80
 
                                   gnutls_certificate_allocate_credentials(),
81
 
                                   gnutls_global_deinit(),
82
 
                                   gnutls_dh_params_init(),
83
 
                                   gnutls_dh_params_generate(),
84
 
                                   gnutls_certificate_set_dh_params(),
85
 
                                   gnutls_certificate_free_credentials(),
86
 
                                   gnutls_session_t, gnutls_init(),
87
 
                                   gnutls_priority_set_direct(),
88
 
                                   gnutls_deinit(),
89
 
                                   gnutls_credentials_set(),
90
 
                                   gnutls_certificate_server_set_request(),
91
 
                                   gnutls_dh_set_prime_bits(),
92
 
                                   gnutls_transport_set_ptr(),
93
 
                                   gnutls_transport_ptr_t,
94
 
                                   gnutls_handshake(),
95
 
                                   gnutls_record_recv()
96
 
                                   gnutls_perror(), gnutls_bye(),
97
 
                                   init_gnutls_session(),
98
 
                                   GNUTLS_E_SUCCESS,
99
 
                                   GNUTLS_CRD_CERTIFICATE,
100
 
                                   GNUTLS_CERT_IGNORE,
101
 
                                   GNUTLS_E_INTERRUPTED,
102
 
                                   GNUTLS_E_AGAIN,
103
 
                                   GNUTLS_E_REHANDSHAKE,
104
 
                                   GNUTLS_SHUT_RDWR, */
105
 
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
106
 
                               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 */
 
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 */
107
68
 
108
69
/* GPGME */
109
 
#include <gpgme.h>              /* gpgme_data_t, gpgme_ctx_t,
110
 
                                   gpgme_error_t, gpgme_engine_info_t,
111
 
                                   gpgme_check_version(),
112
 
                                   gpgme_engine_check_version(),
113
 
                                   gpgme_strsource(),
114
 
                                   gpgme_strerror(),
115
 
                                   gpgme_get_engine_info(),
116
 
                                   gpgme_set_engine_info(),
117
 
                                   gpgme_data_new_from_mem(),
118
 
                                   gpgme_data_new(), gpgme_new(),
119
 
                                   gpgme_op_decrypt(),
120
 
                                   gpgme_decrypt_result_t,
121
 
                                   gpgme_op_decrypt_result(),
122
 
                                   gpgme_recipient_t,
123
 
                                   gpgme_pubkey_algo_name(),
124
 
                                   gpgme_data_seek(),
125
 
                                   gpgme_data_read(),
126
 
                                   gpgme_data_release()
127
 
                                   GPGME_PROTOCOL_OpenPGP,
128
 
                                   GPG_ERR_NO_ERROR,
129
 
                                   GPG_ERR_NO_SECKEY, */
 
70
#include <errno.h>              /* perror() */
 
71
#include <gpgme.h>
 
72
 
 
73
/* getopt_long */
 
74
#include <getopt.h>
130
75
 
131
76
#define BUFFER_SIZE 256
132
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
 
133
82
bool debug = false;
134
 
static const char *keydir = "/conf/conf.d/mandos";
135
 
static const char mandos_protocol_version[] = "1";
136
 
const char *argp_program_version = "mandosclient 0.9";
137
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
138
 
 
139
 
/* 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 */
140
87
typedef struct {
141
88
  AvahiSimplePoll *simple_poll;
142
89
  AvahiServer *server;
146
93
  const char *priority;
147
94
} mandos_context;
148
95
 
149
 
/*
150
 
 * Make room in "buffer" for at least BUFFER_SIZE additional bytes.
151
 
 * "buffer_capacity" is how much is currently allocated,
152
 
 * "buffer_length" is how much is already used.
153
 
 */
154
96
size_t adjustbuffer(char **buffer, size_t buffer_length,
155
97
                  size_t buffer_capacity){
156
98
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
291
233
  
292
234
  *plaintext = NULL;
293
235
  while(true){
294
 
    plaintext_capacity = adjustbuffer(plaintext,
295
 
                                      (size_t)plaintext_length,
 
236
    plaintext_capacity = adjustbuffer(plaintext, (size_t)plaintext_length,
296
237
                                      plaintext_capacity);
297
238
    if (plaintext_capacity == 0){
298
239
        perror("adjustbuffer");
346
287
  fprintf(stderr, "GnuTLS: %s", string);
347
288
}
348
289
 
349
 
static int init_gnutls_global(mandos_context *mc,
350
 
                              const char *pubkeyfile,
351
 
                              const char *seckeyfile){
 
290
static int init_gnutls_global(mandos_context *mc){
352
291
  int ret;
353
292
  
354
293
  if(debug){
375
314
      != GNUTLS_E_SUCCESS) {
376
315
    fprintf (stderr, "GnuTLS memory error: %s\n",
377
316
             safer_gnutls_strerror(ret));
378
 
    gnutls_global_deinit ();
379
317
    return -1;
380
318
  }
381
319
  
393
331
            " '%s')\n", ret, pubkeyfile, seckeyfile);
394
332
    fprintf(stdout, "The GnuTLS error is: %s\n",
395
333
            safer_gnutls_strerror(ret));
396
 
    goto globalfail;
 
334
    return -1;
397
335
  }
398
336
  
399
337
  /* GnuTLS server initialization */
401
339
  if (ret != GNUTLS_E_SUCCESS) {
402
340
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
403
341
             " %s\n", safer_gnutls_strerror(ret));
404
 
    goto globalfail;
 
342
    return -1;
405
343
  }
406
344
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
407
345
  if (ret != GNUTLS_E_SUCCESS) {
408
346
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
409
347
             safer_gnutls_strerror(ret));
410
 
    goto globalfail;
 
348
    return -1;
411
349
  }
412
350
  
413
351
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
414
352
 
415
353
  return 0;
416
 
 
417
 
 globalfail:
418
 
 
419
 
  gnutls_certificate_free_credentials(mc->cred);
420
 
  gnutls_global_deinit();
421
 
  return -1;
422
 
 
423
354
}
424
355
 
425
 
static int init_gnutls_session(mandos_context *mc,
426
 
                               gnutls_session_t *session){
 
356
static int init_gnutls_session(mandos_context *mc, gnutls_session_t *session){
427
357
  int ret;
428
358
  /* GnuTLS session creation */
429
359
  ret = gnutls_init(session, GNUTLS_SERVER);
439
369
      fprintf(stderr, "Syntax error at: %s\n", err);
440
370
      fprintf(stderr, "GnuTLS error: %s\n",
441
371
              safer_gnutls_strerror(ret));
442
 
      gnutls_deinit (*session);
443
372
      return -1;
444
373
    }
445
374
  }
449
378
  if (ret != GNUTLS_E_SUCCESS) {
450
379
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
451
380
            safer_gnutls_strerror(ret));
452
 
    gnutls_deinit (*session);
453
381
    return -1;
454
382
  }
455
383
  
471
399
                                      AvahiIfIndex if_index,
472
400
                                      mandos_context *mc){
473
401
  int ret, tcp_sd;
474
 
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
 
402
  struct sockaddr_in6 to;
475
403
  char *buffer = NULL;
476
404
  char *decrypted_buffer;
477
405
  size_t buffer_length = 0;
481
409
  int retval = 0;
482
410
  char interface[IF_NAMESIZE];
483
411
  gnutls_session_t session;
 
412
  gnutls_dh_params_t dh_params;
484
413
  
485
414
  ret = init_gnutls_session (mc, &session);
486
415
  if (ret != 0){
507
436
  }
508
437
  
509
438
  memset(&to,0,sizeof(to));     /* Spurious warning */
510
 
  to.in6.sin6_family = AF_INET6;
 
439
  to.sin6_family = AF_INET6;
511
440
  /* It would be nice to have a way to detect if we were passed an
512
441
     IPv4 address here.   Now we assume an IPv6 address. */
513
 
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
 
442
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
514
443
  if (ret < 0 ){
515
444
    perror("inet_pton");
516
445
    return -1;
519
448
    fprintf(stderr, "Bad address: %s\n", ip);
520
449
    return -1;
521
450
  }
522
 
  to.in6.sin6_port = htons(port);       /* Spurious warning */
 
451
  to.sin6_port = htons(port);   /* Spurious warning */
523
452
  
524
 
  to.in6.sin6_scope_id = (uint32_t)if_index;
 
453
  to.sin6_scope_id = (uint32_t)if_index;
525
454
  
526
455
  if(debug){
527
456
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
528
457
    char addrstr[INET6_ADDRSTRLEN] = "";
529
 
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
 
458
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
530
459
                 sizeof(addrstr)) == NULL){
531
460
      perror("inet_ntop");
532
461
    } else {
536
465
    }
537
466
  }
538
467
  
539
 
  ret = connect(tcp_sd, &to.in, sizeof(to));
 
468
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
540
469
  if (ret < 0){
541
470
    perror("connect");
542
471
    return -1;
591
520
  }
592
521
 
593
522
  while(true){
594
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
595
 
                                   buffer_capacity);
 
523
    buffer_capacity = adjustbuffer(&buffer, buffer_length, buffer_capacity);
596
524
    if (buffer_capacity == 0){
597
525
      perror("adjustbuffer");
598
526
      retval = -1;
669
597
  free(buffer);
670
598
  close(tcp_sd);
671
599
  gnutls_deinit (session);
 
600
  gnutls_certificate_free_credentials (mc->cred);
 
601
  gnutls_global_deinit ();
672
602
  return retval;
673
603
}
674
604
 
801
731
    gid_t gid;
802
732
    char *connect_to = NULL;
803
733
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
804
 
    const char *pubkeyfile = "pubkey.txt";
805
 
    const char *seckeyfile = "seckey.txt";
806
734
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
807
735
                          .dh_bits = 1024, .priority = "SECURE256"};
808
 
    bool gnutls_initalized = false;
809
736
    
810
737
    {
811
 
      struct argp_option options[] = {
812
 
        { .name = "debug", .key = 128,
813
 
          .doc = "Debug mode", .group = 3 },
814
 
        { .name = "connect", .key = 'c',
815
 
          .arg = "IP",
816
 
          .doc = "Connect directly to a sepcified mandos server",
817
 
          .group = 1 },
818
 
        { .name = "interface", .key = 'i',
819
 
          .arg = "INTERFACE",
820
 
          .doc = "Interface that Avahi will conntect through",
821
 
          .group = 1 },
822
 
        { .name = "keydir", .key = 'd',
823
 
          .arg = "KEYDIR",
824
 
          .doc = "Directory where the openpgp keyring is",
825
 
          .group = 1 },
826
 
        { .name = "seckey", .key = 's',
827
 
          .arg = "SECKEY",
828
 
          .doc = "Secret openpgp key for gnutls authentication",
829
 
          .group = 1 },
830
 
        { .name = "pubkey", .key = 'p',
831
 
          .arg = "PUBKEY",
832
 
          .doc = "Public openpgp key for gnutls authentication",
833
 
          .group = 2 },
834
 
        { .name = "dh-bits", .key = 129,
835
 
          .arg = "BITS",
836
 
          .doc = "dh-bits to use in gnutls communication",
837
 
          .group = 2 },
838
 
        { .name = "priority", .key = 130,
839
 
          .arg = "PRIORITY",
840
 
          .doc = "GNUTLS priority", .group = 1 },
841
 
        { .name = NULL }
842
 
      };
843
 
 
844
 
      
845
 
      error_t parse_opt (int key, char *arg,
846
 
                         struct argp_state *state) {
847
 
        /* Get the INPUT argument from `argp_parse', which we know is
848
 
           a pointer to our plugin list pointer. */
849
 
        switch (key) {
850
 
        case 128:
851
 
          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;
852
765
          break;
853
766
        case 'c':
854
 
          connect_to = arg;
855
 
          break;
856
 
        case 'i':
857
 
          interface = arg;
 
767
          connect_to = optarg;
858
768
          break;
859
769
        case 'd':
860
 
          keydir = arg;
 
770
          keydir = optarg;
 
771
          break;
 
772
        case 'p':
 
773
          pubkeyfile = optarg;
861
774
          break;
862
775
        case 's':
863
 
          seckeyfile = arg;
864
 
          break;
865
 
        case 'p':
866
 
          pubkeyfile = arg;
867
 
          break;
868
 
        case 129:
 
776
          seckeyfile = optarg;
 
777
          break;
 
778
        case 'D':
869
779
          errno = 0;
870
 
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
 
780
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
871
781
          if (errno){
872
782
            perror("strtol");
873
783
            exit(EXIT_FAILURE);
874
784
          }
875
785
          break;
876
 
        case 130:
877
 
          mc.priority = arg;
878
 
          break;
879
 
        case ARGP_KEY_ARG:
880
 
          argp_usage (state);
881
 
          break;
882
 
          case ARGP_KEY_END:
883
 
            break;
 
786
        case 'P':
 
787
          mc.priority = optarg;
 
788
          break;
 
789
        case '?':
884
790
        default:
885
 
          return ARGP_ERR_UNKNOWN;
 
791
          /* getopt_long() has already printed a message about the
 
792
             unrcognized option, so just exit. */
 
793
          exit(EXIT_FAILURE);
886
794
        }
887
 
        return 0;
888
795
      }
889
 
 
890
 
      struct argp argp = { .options = options, .parser = parse_opt,
891
 
                           .args_doc = "",
892
 
                           .doc = "Mandos client -- Get and decrypt"
893
 
                           " passwords from mandos server" };
894
 
      argp_parse (&argp, argc, argv, 0, 0, NULL);
 
796
      /* Set the global debug flag from the temporary int */
 
797
      debug = debug_int ? true : false;
895
798
    }
896
 
      
 
799
    
897
800
    pubkeyfile = combinepath(keydir, pubkeyfile);
898
801
    if (pubkeyfile == NULL){
899
802
      perror("combinepath");
907
810
      goto end;
908
811
    }
909
812
 
910
 
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
 
813
    ret = init_gnutls_global(&mc);
911
814
    if (ret == -1){
912
815
      fprintf(stderr, "init_gnutls_global\n");
913
816
      goto end;
914
 
    } else {
915
 
      gnutls_initalized = true;
916
817
    }
917
818
 
918
819
    uid = getuid();
1067
968
        avahi_simple_poll_free(mc.simple_poll);
1068
969
    free(pubkeyfile);
1069
970
    free(seckeyfile);
1070
 
 
1071
 
    if (gnutls_initalized){
1072
 
      gnutls_certificate_free_credentials (mc.cred);
1073
 
      gnutls_global_deinit ();
1074
 
    }
1075
971
    
1076
972
    return exitcode;
1077
973
}