/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/password-request.c

  • Committer: Björn Påhlsson
  • Date: 2008-08-10 00:21:08 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 59.
  • Revision ID: belorn@braxen-20080810002108-ye1e88yto19lsctt
name change for argp

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>
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 */
 
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() */
44
59
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
45
 
                                   SIOCSIFFLAGS */
 
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 */
46
72
 
 
73
/* Avahi */
 
74
/* All Avahi types, constants and functions
 
75
 Avahi*, avahi_*,
 
76
 AVAHI_* */
47
77
#include <avahi-core/core.h>
48
78
#include <avahi-core/lookup.h>
49
79
#include <avahi-core/log.h>
51
81
#include <avahi-common/malloc.h>
52
82
#include <avahi-common/error.h>
53
83
 
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 */
 
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 */
68
91
 
69
92
/* GPGME */
70
 
#include <errno.h>              /* perror() */
71
 
#include <gpgme.h>
72
 
 
73
 
/* getopt_long */
74
 
#include <getopt.h>
 
93
#include <gpgme.h>              /* All GPGME types, constants and functions
 
94
                                   gpgme_*
 
95
                                   GPGME_PROTOCOL_OpenPGP,
 
96
                                   GPG_ERR_NO_* */
75
97
 
76
98
#define BUFFER_SIZE 256
77
99
 
 
100
bool debug = false;
78
101
static const char *keydir = "/conf/conf.d/mandos";
79
 
static const char *pubkeyfile = "pubkey.txt";
80
 
static const char *seckeyfile = "seckey.txt";
81
 
 
82
 
bool debug = false;
83
 
 
84
 
const char mandos_protocol_version[] = "1";
85
 
 
86
 
/* Used for passing in values through all the callback functions */
 
102
static const char mandos_protocol_version[] = "1";
 
103
const char *argp_program_version = "password-request 1.0";
 
104
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
105
 
 
106
/* Used for passing in values through the Avahi callback functions */
87
107
typedef struct {
88
108
  AvahiSimplePoll *simple_poll;
89
109
  AvahiServer *server;
93
113
  const char *priority;
94
114
} mandos_context;
95
115
 
 
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
 */
96
121
size_t adjustbuffer(char **buffer, size_t buffer_length,
97
122
                  size_t buffer_capacity){
98
123
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
233
258
  
234
259
  *plaintext = NULL;
235
260
  while(true){
236
 
    plaintext_capacity = adjustbuffer(plaintext, (size_t)plaintext_length,
 
261
    plaintext_capacity = adjustbuffer(plaintext,
 
262
                                      (size_t)plaintext_length,
237
263
                                      plaintext_capacity);
238
264
    if (plaintext_capacity == 0){
239
265
        perror("adjustbuffer");
287
313
  fprintf(stderr, "GnuTLS: %s", string);
288
314
}
289
315
 
290
 
static int init_gnutls_global(mandos_context *mc){
 
316
static int init_gnutls_global(mandos_context *mc,
 
317
                              const char *pubkeyfile,
 
318
                              const char *seckeyfile){
291
319
  int ret;
292
320
  
293
321
  if(debug){
294
322
    fprintf(stderr, "Initializing GnuTLS\n");
295
323
  }
296
 
 
297
 
  if ((ret = gnutls_global_init ())
298
 
      != GNUTLS_E_SUCCESS) {
 
324
  
 
325
  ret = gnutls_global_init();
 
326
  if (ret != GNUTLS_E_SUCCESS) {
299
327
    fprintf (stderr, "GnuTLS global_init: %s\n",
300
328
             safer_gnutls_strerror(ret));
301
329
    return -1;
310
338
  }
311
339
  
312
340
  /* OpenPGP credentials */
313
 
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
314
 
      != GNUTLS_E_SUCCESS) {
 
341
  gnutls_certificate_allocate_credentials(&mc->cred);
 
342
  if (ret != GNUTLS_E_SUCCESS){
315
343
    fprintf (stderr, "GnuTLS memory error: %s\n",
316
344
             safer_gnutls_strerror(ret));
 
345
    gnutls_global_deinit ();
317
346
    return -1;
318
347
  }
319
348
  
331
360
            " '%s')\n", ret, pubkeyfile, seckeyfile);
332
361
    fprintf(stdout, "The GnuTLS error is: %s\n",
333
362
            safer_gnutls_strerror(ret));
334
 
    return -1;
 
363
    goto globalfail;
335
364
  }
336
365
  
337
366
  /* GnuTLS server initialization */
339
368
  if (ret != GNUTLS_E_SUCCESS) {
340
369
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
341
370
             " %s\n", safer_gnutls_strerror(ret));
342
 
    return -1;
 
371
    goto globalfail;
343
372
  }
344
373
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
345
374
  if (ret != GNUTLS_E_SUCCESS) {
346
375
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
347
376
             safer_gnutls_strerror(ret));
348
 
    return -1;
 
377
    goto globalfail;
349
378
  }
350
379
  
351
380
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
352
381
 
353
382
  return 0;
 
383
 
 
384
 globalfail:
 
385
 
 
386
  gnutls_certificate_free_credentials(mc->cred);
 
387
  gnutls_global_deinit();
 
388
  return -1;
 
389
 
354
390
}
355
391
 
356
 
static int init_gnutls_session(mandos_context *mc, gnutls_session_t *session){
 
392
static int init_gnutls_session(mandos_context *mc,
 
393
                               gnutls_session_t *session){
357
394
  int ret;
358
395
  /* GnuTLS session creation */
359
396
  ret = gnutls_init(session, GNUTLS_SERVER);
369
406
      fprintf(stderr, "Syntax error at: %s\n", err);
370
407
      fprintf(stderr, "GnuTLS error: %s\n",
371
408
              safer_gnutls_strerror(ret));
 
409
      gnutls_deinit (*session);
372
410
      return -1;
373
411
    }
374
412
  }
378
416
  if (ret != GNUTLS_E_SUCCESS) {
379
417
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
380
418
            safer_gnutls_strerror(ret));
 
419
    gnutls_deinit (*session);
381
420
    return -1;
382
421
  }
383
422
  
399
438
                                      AvahiIfIndex if_index,
400
439
                                      mandos_context *mc){
401
440
  int ret, tcp_sd;
402
 
  struct sockaddr_in6 to;
 
441
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
403
442
  char *buffer = NULL;
404
443
  char *decrypted_buffer;
405
444
  size_t buffer_length = 0;
409
448
  int retval = 0;
410
449
  char interface[IF_NAMESIZE];
411
450
  gnutls_session_t session;
412
 
  gnutls_dh_params_t dh_params;
413
451
  
414
452
  ret = init_gnutls_session (mc, &session);
415
453
  if (ret != 0){
436
474
  }
437
475
  
438
476
  memset(&to,0,sizeof(to));     /* Spurious warning */
439
 
  to.sin6_family = AF_INET6;
 
477
  to.in6.sin6_family = AF_INET6;
440
478
  /* It would be nice to have a way to detect if we were passed an
441
479
     IPv4 address here.   Now we assume an IPv6 address. */
442
 
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
 
480
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
443
481
  if (ret < 0 ){
444
482
    perror("inet_pton");
445
483
    return -1;
448
486
    fprintf(stderr, "Bad address: %s\n", ip);
449
487
    return -1;
450
488
  }
451
 
  to.sin6_port = htons(port);   /* Spurious warning */
 
489
  to.in6.sin6_port = htons(port);       /* Spurious warning */
452
490
  
453
 
  to.sin6_scope_id = (uint32_t)if_index;
 
491
  to.in6.sin6_scope_id = (uint32_t)if_index;
454
492
  
455
493
  if(debug){
456
494
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
457
495
    char addrstr[INET6_ADDRSTRLEN] = "";
458
 
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
 
496
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
459
497
                 sizeof(addrstr)) == NULL){
460
498
      perror("inet_ntop");
461
499
    } else {
465
503
    }
466
504
  }
467
505
  
468
 
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
 
506
  ret = connect(tcp_sd, &to.in, sizeof(to));
469
507
  if (ret < 0){
470
508
    perror("connect");
471
509
    return -1;
500
538
  }
501
539
  
502
540
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
503
 
  
504
 
  ret = gnutls_handshake (session);
 
541
 
 
542
  do{
 
543
    ret = gnutls_handshake (session);
 
544
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
505
545
  
506
546
  if (ret != GNUTLS_E_SUCCESS){
507
547
    if(debug){
520
560
  }
521
561
 
522
562
  while(true){
523
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length, buffer_capacity);
 
563
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
 
564
                                   buffer_capacity);
524
565
    if (buffer_capacity == 0){
525
566
      perror("adjustbuffer");
526
567
      retval = -1;
538
579
      case GNUTLS_E_AGAIN:
539
580
        break;
540
581
      case GNUTLS_E_REHANDSHAKE:
541
 
        ret = gnutls_handshake (session);
 
582
        do{
 
583
          ret = gnutls_handshake (session);
 
584
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
542
585
        if (ret < 0){
543
586
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
544
587
          gnutls_perror (ret);
597
640
  free(buffer);
598
641
  close(tcp_sd);
599
642
  gnutls_deinit (session);
600
 
  gnutls_certificate_free_credentials (mc->cred);
601
 
  gnutls_global_deinit ();
602
643
  return retval;
603
644
}
604
645
 
731
772
    gid_t gid;
732
773
    char *connect_to = NULL;
733
774
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
 
775
    const char *pubkeyfile = "pubkey.txt";
 
776
    const char *seckeyfile = "seckey.txt";
734
777
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
735
778
                          .dh_bits = 1024, .priority = "SECURE256"};
 
779
    bool gnutls_initalized = false;
736
780
    
737
781
    {
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){
 
782
      struct argp_option options[] = {
 
783
        { .name = "debug", .key = 128,
 
784
          .doc = "Debug mode", .group = 3 },
 
785
        { .name = "connect", .key = 'c',
 
786
          .arg = "IP",
 
787
          .doc = "Connect directly to a sepcified mandos server",
 
788
          .group = 1 },
 
789
        { .name = "interface", .key = 'i',
 
790
          .arg = "INTERFACE",
 
791
          .doc = "Interface that Avahi will conntect through",
 
792
          .group = 1 },
 
793
        { .name = "keydir", .key = 'd',
 
794
          .arg = "KEYDIR",
 
795
          .doc = "Directory where the openpgp keyring is",
 
796
          .group = 1 },
 
797
        { .name = "seckey", .key = 's',
 
798
          .arg = "SECKEY",
 
799
          .doc = "Secret openpgp key for gnutls authentication",
 
800
          .group = 1 },
 
801
        { .name = "pubkey", .key = 'p',
 
802
          .arg = "PUBKEY",
 
803
          .doc = "Public openpgp key for gnutls authentication",
 
804
          .group = 2 },
 
805
        { .name = "dh-bits", .key = 129,
 
806
          .arg = "BITS",
 
807
          .doc = "dh-bits to use in gnutls communication",
 
808
          .group = 2 },
 
809
        { .name = "priority", .key = 130,
 
810
          .arg = "PRIORITY",
 
811
          .doc = "GNUTLS priority", .group = 1 },
 
812
        { .name = NULL }
 
813
      };
 
814
 
 
815
      
 
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. */
 
820
        switch (key) {
 
821
        case 128:
 
822
          debug = true;
757
823
          break;
758
 
        }
759
 
      
760
 
        switch(ret){
761
 
        case 0:
 
824
        case 'c':
 
825
          connect_to = arg;
762
826
          break;
763
827
        case 'i':
764
 
          interface = optarg;
765
 
          break;
766
 
        case 'c':
767
 
          connect_to = optarg;
 
828
          interface = arg;
768
829
          break;
769
830
        case 'd':
770
 
          keydir = optarg;
 
831
          keydir = arg;
 
832
          break;
 
833
        case 's':
 
834
          seckeyfile = arg;
771
835
          break;
772
836
        case 'p':
773
 
          pubkeyfile = optarg;
774
 
          break;
775
 
        case 's':
776
 
          seckeyfile = optarg;
777
 
          break;
778
 
        case 'D':
 
837
          pubkeyfile = arg;
 
838
          break;
 
839
        case 129:
779
840
          errno = 0;
780
 
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
 
841
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
781
842
          if (errno){
782
843
            perror("strtol");
783
844
            exit(EXIT_FAILURE);
784
845
          }
785
846
          break;
786
 
        case 'P':
787
 
          mc.priority = optarg;
788
 
          break;
789
 
        case '?':
 
847
        case 130:
 
848
          mc.priority = arg;
 
849
          break;
 
850
        case ARGP_KEY_ARG:
 
851
          argp_usage (state);
 
852
          break;
 
853
          case ARGP_KEY_END:
 
854
            break;
790
855
        default:
791
 
          /* getopt_long() has already printed a message about the
792
 
             unrcognized option, so just exit. */
793
 
          exit(EXIT_FAILURE);
 
856
          return ARGP_ERR_UNKNOWN;
794
857
        }
795
 
      }
796
 
      /* Set the global debug flag from the temporary int */
797
 
      debug = debug_int ? true : false;
 
858
        return 0;
 
859
      }
 
860
 
 
861
      struct argp argp = { .options = options, .parser = parse_opt,
 
862
                           .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
      }
798
871
    }
799
 
    
 
872
      
800
873
    pubkeyfile = combinepath(keydir, pubkeyfile);
801
874
    if (pubkeyfile == NULL){
802
875
      perror("combinepath");
810
883
      goto end;
811
884
    }
812
885
 
813
 
    ret = init_gnutls_global(&mc);
 
886
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
814
887
    if (ret == -1){
815
888
      fprintf(stderr, "init_gnutls_global\n");
816
889
      goto end;
 
890
    } else {
 
891
      gnutls_initalized = true;
817
892
    }
818
893
 
819
894
    uid = getuid();
968
1043
        avahi_simple_poll_free(mc.simple_poll);
969
1044
    free(pubkeyfile);
970
1045
    free(seckeyfile);
 
1046
 
 
1047
    if (gnutls_initalized){
 
1048
      gnutls_certificate_free_credentials(mc.cred);
 
1049
      gnutls_global_deinit ();
 
1050
    }
971
1051
    
972
1052
    return exitcode;
973
1053
}