/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: Teddy Hogeborn
  • Date: 2008-08-07 21:45:41 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080807214541-pyg8itw6kphz1dy5
* plugbasedclient.c: Renamed to "mandos-client.c".  All users changed.

* plugins.d/mandosclient.c: Renamed to "plugins.d/password-request.c".
                            All users changed.

* plugins.d/passprompt.c: Renamed to "plugins.d/password-prompt.c".
                          All users changed.

* server.conf: Renamed to "mandos.conf".  All users changed.

* server.py: Renamed to "mandos".
  (daemon): Have default values for arguments. Caller changed.

* Makefile (distclean, mostlyclean, maintainer-clean): New aliases for
                                                       "clean".
  (check, run-client, run-server): New.

* network-protocol.txt: New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
#include <arpa/inet.h>          /* inet_pton() */
66
66
#include <iso646.h>             /* not */
67
67
#include <net/if.h>             /* IF_NAMESIZE */
68
 
 
 
68
#include <argp.h>               /* struct argp_option,
 
69
                                   struct argp_state, struct argp,
 
70
                                   argp_parse() */
69
71
/* GPGME */
70
72
#include <errno.h>              /* perror() */
71
73
#include <gpgme.h>
72
74
 
73
 
/* getopt_long */
74
 
#include <getopt.h>
75
 
 
76
75
#define BUFFER_SIZE 256
77
76
 
 
77
bool debug = false;
78
78
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 */
 
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
 
 
83
/* Used for passing in values through the Avahi callback functions */
87
84
typedef struct {
88
85
  AvahiSimplePoll *simple_poll;
89
86
  AvahiServer *server;
93
90
  const char *priority;
94
91
} mandos_context;
95
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
 */
96
98
size_t adjustbuffer(char **buffer, size_t buffer_length,
97
99
                  size_t buffer_capacity){
98
100
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
233
235
  
234
236
  *plaintext = NULL;
235
237
  while(true){
236
 
    plaintext_capacity = adjustbuffer(plaintext, (size_t)plaintext_length,
 
238
    plaintext_capacity = adjustbuffer(plaintext,
 
239
                                      (size_t)plaintext_length,
237
240
                                      plaintext_capacity);
238
241
    if (plaintext_capacity == 0){
239
242
        perror("adjustbuffer");
287
290
  fprintf(stderr, "GnuTLS: %s", string);
288
291
}
289
292
 
290
 
static int init_gnutls_global(mandos_context *mc){
 
293
static int init_gnutls_global(mandos_context *mc,
 
294
                              const char *pubkeyfile,
 
295
                              const char *seckeyfile){
291
296
  int ret;
292
297
  
293
298
  if(debug){
314
319
      != GNUTLS_E_SUCCESS) {
315
320
    fprintf (stderr, "GnuTLS memory error: %s\n",
316
321
             safer_gnutls_strerror(ret));
 
322
    gnutls_global_deinit ();
317
323
    return -1;
318
324
  }
319
325
  
331
337
            " '%s')\n", ret, pubkeyfile, seckeyfile);
332
338
    fprintf(stdout, "The GnuTLS error is: %s\n",
333
339
            safer_gnutls_strerror(ret));
334
 
    return -1;
 
340
    goto globalfail;
335
341
  }
336
342
  
337
343
  /* GnuTLS server initialization */
339
345
  if (ret != GNUTLS_E_SUCCESS) {
340
346
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
341
347
             " %s\n", safer_gnutls_strerror(ret));
342
 
    return -1;
 
348
    goto globalfail;
343
349
  }
344
350
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
345
351
  if (ret != GNUTLS_E_SUCCESS) {
346
352
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
347
353
             safer_gnutls_strerror(ret));
348
 
    return -1;
 
354
    goto globalfail;
349
355
  }
350
356
  
351
357
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
352
358
 
353
359
  return 0;
 
360
 
 
361
 globalfail:
 
362
 
 
363
  gnutls_certificate_free_credentials (mc->cred);
 
364
  gnutls_global_deinit ();
 
365
  return -1;
 
366
 
354
367
}
355
368
 
356
 
static int init_gnutls_session(mandos_context *mc, gnutls_session_t *session){
 
369
static int init_gnutls_session(mandos_context *mc,
 
370
                               gnutls_session_t *session){
357
371
  int ret;
358
372
  /* GnuTLS session creation */
359
373
  ret = gnutls_init(session, GNUTLS_SERVER);
369
383
      fprintf(stderr, "Syntax error at: %s\n", err);
370
384
      fprintf(stderr, "GnuTLS error: %s\n",
371
385
              safer_gnutls_strerror(ret));
 
386
      gnutls_deinit (*session);
372
387
      return -1;
373
388
    }
374
389
  }
378
393
  if (ret != GNUTLS_E_SUCCESS) {
379
394
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
380
395
            safer_gnutls_strerror(ret));
 
396
    gnutls_deinit (*session);
381
397
    return -1;
382
398
  }
383
399
  
399
415
                                      AvahiIfIndex if_index,
400
416
                                      mandos_context *mc){
401
417
  int ret, tcp_sd;
402
 
  struct sockaddr_in6 to;
 
418
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
403
419
  char *buffer = NULL;
404
420
  char *decrypted_buffer;
405
421
  size_t buffer_length = 0;
409
425
  int retval = 0;
410
426
  char interface[IF_NAMESIZE];
411
427
  gnutls_session_t session;
412
 
  gnutls_dh_params_t dh_params;
413
428
  
414
429
  ret = init_gnutls_session (mc, &session);
415
430
  if (ret != 0){
436
451
  }
437
452
  
438
453
  memset(&to,0,sizeof(to));     /* Spurious warning */
439
 
  to.sin6_family = AF_INET6;
 
454
  to.in6.sin6_family = AF_INET6;
440
455
  /* It would be nice to have a way to detect if we were passed an
441
456
     IPv4 address here.   Now we assume an IPv6 address. */
442
 
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
 
457
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
443
458
  if (ret < 0 ){
444
459
    perror("inet_pton");
445
460
    return -1;
448
463
    fprintf(stderr, "Bad address: %s\n", ip);
449
464
    return -1;
450
465
  }
451
 
  to.sin6_port = htons(port);   /* Spurious warning */
 
466
  to.in6.sin6_port = htons(port);       /* Spurious warning */
452
467
  
453
 
  to.sin6_scope_id = (uint32_t)if_index;
 
468
  to.in6.sin6_scope_id = (uint32_t)if_index;
454
469
  
455
470
  if(debug){
456
471
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
457
472
    char addrstr[INET6_ADDRSTRLEN] = "";
458
 
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
 
473
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
459
474
                 sizeof(addrstr)) == NULL){
460
475
      perror("inet_ntop");
461
476
    } else {
465
480
    }
466
481
  }
467
482
  
468
 
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
 
483
  ret = connect(tcp_sd, &to.in, sizeof(to));
469
484
  if (ret < 0){
470
485
    perror("connect");
471
486
    return -1;
520
535
  }
521
536
 
522
537
  while(true){
523
 
    buffer_capacity = adjustbuffer(&buffer, buffer_length, buffer_capacity);
 
538
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
 
539
                                   buffer_capacity);
524
540
    if (buffer_capacity == 0){
525
541
      perror("adjustbuffer");
526
542
      retval = -1;
597
613
  free(buffer);
598
614
  close(tcp_sd);
599
615
  gnutls_deinit (session);
600
 
  gnutls_certificate_free_credentials (mc->cred);
601
 
  gnutls_global_deinit ();
602
616
  return retval;
603
617
}
604
618
 
731
745
    gid_t gid;
732
746
    char *connect_to = NULL;
733
747
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
 
748
    const char *pubkeyfile = "pubkey.txt";
 
749
    const char *seckeyfile = "seckey.txt";
734
750
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
735
751
                          .dh_bits = 1024, .priority = "SECURE256"};
 
752
    bool gnutls_initalized = false;
736
753
    
737
754
    {
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){
 
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;
757
796
          break;
758
 
        }
759
 
      
760
 
        switch(ret){
761
 
        case 0:
 
797
        case 'c':
 
798
          connect_to = arg;
762
799
          break;
763
800
        case 'i':
764
 
          interface = optarg;
765
 
          break;
766
 
        case 'c':
767
 
          connect_to = optarg;
 
801
          interface = arg;
768
802
          break;
769
803
        case 'd':
770
 
          keydir = optarg;
 
804
          keydir = arg;
 
805
          break;
 
806
        case 's':
 
807
          seckeyfile = arg;
771
808
          break;
772
809
        case 'p':
773
 
          pubkeyfile = optarg;
774
 
          break;
775
 
        case 's':
776
 
          seckeyfile = optarg;
777
 
          break;
778
 
        case 'D':
 
810
          pubkeyfile = arg;
 
811
          break;
 
812
        case 129:
779
813
          errno = 0;
780
 
          mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
 
814
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
781
815
          if (errno){
782
816
            perror("strtol");
783
817
            exit(EXIT_FAILURE);
784
818
          }
785
819
          break;
786
 
        case 'P':
787
 
          mc.priority = optarg;
788
 
          break;
789
 
        case '?':
 
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;
790
828
        default:
791
 
          /* getopt_long() has already printed a message about the
792
 
             unrcognized option, so just exit. */
793
 
          exit(EXIT_FAILURE);
 
829
          return ARGP_ERR_UNKNOWN;
794
830
        }
 
831
        return 0;
795
832
      }
796
 
      /* Set the global debug flag from the temporary int */
797
 
      debug = debug_int ? true : false;
 
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);
798
839
    }
799
 
    
 
840
      
800
841
    pubkeyfile = combinepath(keydir, pubkeyfile);
801
842
    if (pubkeyfile == NULL){
802
843
      perror("combinepath");
810
851
      goto end;
811
852
    }
812
853
 
813
 
    ret = init_gnutls_global(&mc);
 
854
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
814
855
    if (ret == -1){
815
856
      fprintf(stderr, "init_gnutls_global\n");
816
857
      goto end;
 
858
    } else {
 
859
      gnutls_initalized = true;
817
860
    }
818
861
 
819
862
    uid = getuid();
968
1011
        avahi_simple_poll_free(mc.simple_poll);
969
1012
    free(pubkeyfile);
970
1013
    free(seckeyfile);
 
1014
 
 
1015
    if (gnutls_initalized){
 
1016
      gnutls_certificate_free_credentials (mc.cred);
 
1017
      gnutls_global_deinit ();
 
1018
    }
971
1019
    
972
1020
    return exitcode;
973
1021
}