/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 <sys/types.h>          /* socket(), inet_pton(), sockaddr,
48
 
                                   sockaddr_in6, PF_INET6,
49
 
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
50
 
                                   uid_t, gid_t */
51
 
#include <inttypes.h>           /* PRIu16 */
52
 
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
53
 
                                   struct in6_addr, inet_pton(),
54
 
                                   connect() */
55
 
#include <assert.h>             /* assert() */
56
 
#include <errno.h>              /* perror(), errno */
57
 
#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 */
58
44
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
59
 
                                   SIOCSIFFLAGS, if_indextoname(),
60
 
                                   if_nametoindex(), IF_NAMESIZE */
61
 
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
62
 
                                   getuid(), getgid(), setuid(),
63
 
                                   setgid() */
64
 
#include <netinet/in.h>
65
 
#include <arpa/inet.h>          /* inet_pton(), htons */
66
 
#include <iso646.h>             /* not, and */
67
 
#include <argp.h>               /* struct argp_option, error_t, struct
68
 
                                   argp_state, struct argp,
69
 
                                   argp_parse(), ARGP_KEY_ARG,
70
 
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
 
45
                                   SIOCSIFFLAGS */
71
46
 
72
 
/* Avahi */
73
 
/* All Avahi types, constants and functions
74
 
 Avahi*, avahi_*,
75
 
 AVAHI_* */
76
47
#include <avahi-core/core.h>
77
48
#include <avahi-core/lookup.h>
78
49
#include <avahi-core/log.h>
80
51
#include <avahi-common/malloc.h>
81
52
#include <avahi-common/error.h>
82
53
 
83
 
/* GnuTLS */
84
 
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and functions
85
 
                                   gnutls_*
86
 
                                   init_gnutls_session(),
87
 
                                   GNUTLS_* */
88
 
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
89
 
                                   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 */
90
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() */
91
71
/* GPGME */
92
 
#include <gpgme.h>              /* All GPGME types, constants and functions
93
 
                                   gpgme_*
94
 
                                   GPGME_PROTOCOL_OpenPGP,
95
 
                                   GPG_ERR_NO_* */
 
72
#include <errno.h>              /* perror() */
 
73
#include <gpgme.h>
96
74
 
97
75
#define BUFFER_SIZE 256
98
76
 
99
77
bool debug = false;
100
 
static const char *keydir = "/conf/conf.d/mandos";
101
 
static const char mandos_protocol_version[] = "1";
102
 
const char *argp_program_version = "password-request 1.0";
 
78
const char *keydir = "/conf/conf.d/mandos";
 
79
const char *argp_program_version = "mandosclient 0.9";
103
80
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
81
const char mandos_protocol_version[] = "1";
104
82
 
105
 
/* Used for passing in values through the Avahi callback functions */
 
83
/* Used for passing in values through all the callback functions */
106
84
typedef struct {
107
85
  AvahiSimplePoll *simple_poll;
108
86
  AvahiServer *server;
112
90
  const char *priority;
113
91
} mandos_context;
114
92
 
115
 
/*
116
 
 * Make room in "buffer" for at least BUFFER_SIZE additional bytes.
117
 
 * "buffer_capacity" is how much is currently allocated,
118
 
 * "buffer_length" is how much is already used.
119
 
 */
120
93
size_t adjustbuffer(char **buffer, size_t buffer_length,
121
94
                  size_t buffer_capacity){
122
95
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
227
200
    } else {
228
201
      fprintf(stderr, "Unsupported algorithm: %s\n",
229
202
              result->unsupported_algorithm);
230
 
      fprintf(stderr, "Wrong key usage: %u\n",
 
203
      fprintf(stderr, "Wrong key usage: %d\n",
231
204
              result->wrong_key_usage);
232
205
      if(result->file_name != NULL){
233
206
        fprintf(stderr, "File name: %s\n", result->file_name);
257
230
  
258
231
  *plaintext = NULL;
259
232
  while(true){
260
 
    plaintext_capacity = adjustbuffer(plaintext,
261
 
                                      (size_t)plaintext_length,
 
233
    plaintext_capacity = adjustbuffer(plaintext, (size_t)plaintext_length,
262
234
                                      plaintext_capacity);
263
235
    if (plaintext_capacity == 0){
264
236
        perror("adjustbuffer");
320
292
  if(debug){
321
293
    fprintf(stderr, "Initializing GnuTLS\n");
322
294
  }
323
 
  
324
 
  ret = gnutls_global_init();
325
 
  if (ret != GNUTLS_E_SUCCESS) {
 
295
 
 
296
  if ((ret = gnutls_global_init ())
 
297
      != GNUTLS_E_SUCCESS) {
326
298
    fprintf (stderr, "GnuTLS global_init: %s\n",
327
299
             safer_gnutls_strerror(ret));
328
300
    return -1;
337
309
  }
338
310
  
339
311
  /* OpenPGP credentials */
340
 
  gnutls_certificate_allocate_credentials(&mc->cred);
341
 
  if (ret != GNUTLS_E_SUCCESS){
 
312
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
313
      != GNUTLS_E_SUCCESS) {
342
314
    fprintf (stderr, "GnuTLS memory error: %s\n",
343
315
             safer_gnutls_strerror(ret));
344
 
    gnutls_global_deinit ();
345
316
    return -1;
346
317
  }
347
318
  
359
330
            " '%s')\n", ret, pubkeyfile, seckeyfile);
360
331
    fprintf(stdout, "The GnuTLS error is: %s\n",
361
332
            safer_gnutls_strerror(ret));
362
 
    goto globalfail;
 
333
    return -1;
363
334
  }
364
335
  
365
336
  /* GnuTLS server initialization */
367
338
  if (ret != GNUTLS_E_SUCCESS) {
368
339
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
369
340
             " %s\n", safer_gnutls_strerror(ret));
370
 
    goto globalfail;
 
341
    return -1;
371
342
  }
372
343
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
373
344
  if (ret != GNUTLS_E_SUCCESS) {
374
345
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
375
346
             safer_gnutls_strerror(ret));
376
 
    goto globalfail;
 
347
    return -1;
377
348
  }
378
349
  
379
350
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
380
351
 
381
352
  return 0;
382
 
 
383
 
 globalfail:
384
 
 
385
 
  gnutls_certificate_free_credentials(mc->cred);
386
 
  gnutls_global_deinit();
387
 
  return -1;
388
 
 
389
353
}
390
354
 
391
 
static int init_gnutls_session(mandos_context *mc,
392
 
                               gnutls_session_t *session){
 
355
static int init_gnutls_session(mandos_context *mc, gnutls_session_t *session){
393
356
  int ret;
394
357
  /* GnuTLS session creation */
395
358
  ret = gnutls_init(session, GNUTLS_SERVER);
405
368
      fprintf(stderr, "Syntax error at: %s\n", err);
406
369
      fprintf(stderr, "GnuTLS error: %s\n",
407
370
              safer_gnutls_strerror(ret));
408
 
      gnutls_deinit (*session);
409
371
      return -1;
410
372
    }
411
373
  }
415
377
  if (ret != GNUTLS_E_SUCCESS) {
416
378
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
417
379
            safer_gnutls_strerror(ret));
418
 
    gnutls_deinit (*session);
419
380
    return -1;
420
381
  }
421
382
  
437
398
                                      AvahiIfIndex if_index,
438
399
                                      mandos_context *mc){
439
400
  int ret, tcp_sd;
440
 
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
 
401
  struct sockaddr_in6 to;
441
402
  char *buffer = NULL;
442
403
  char *decrypted_buffer;
443
404
  size_t buffer_length = 0;
447
408
  int retval = 0;
448
409
  char interface[IF_NAMESIZE];
449
410
  gnutls_session_t session;
 
411
  gnutls_dh_params_t dh_params;
450
412
  
451
413
  ret = init_gnutls_session (mc, &session);
452
414
  if (ret != 0){
454
416
  }
455
417
  
456
418
  if(debug){
457
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
458
 
            "\n", ip, port);
 
419
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
 
420
            ip, port);
459
421
  }
460
422
  
461
423
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
473
435
  }
474
436
  
475
437
  memset(&to,0,sizeof(to));     /* Spurious warning */
476
 
  to.in6.sin6_family = AF_INET6;
 
438
  to.sin6_family = AF_INET6;
477
439
  /* It would be nice to have a way to detect if we were passed an
478
440
     IPv4 address here.   Now we assume an IPv6 address. */
479
 
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
 
441
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
480
442
  if (ret < 0 ){
481
443
    perror("inet_pton");
482
444
    return -1;
485
447
    fprintf(stderr, "Bad address: %s\n", ip);
486
448
    return -1;
487
449
  }
488
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
450
  to.sin6_port = htons(port);   /* Spurious warning */
489
451
  
490
 
  to.in6.sin6_scope_id = (uint32_t)if_index;
 
452
  to.sin6_scope_id = (uint32_t)if_index;
491
453
  
492
454
  if(debug){
493
 
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
494
 
            port);
 
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
 
676
634
      char ip[AVAHI_ADDRESS_STR_MAX];
677
635
      avahi_address_snprint(ip, sizeof(ip), address);
678
636
      if(debug){
679
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
680
 
                PRIu16 ") on port %d\n", name, host_name, ip,
681
 
                interface, port);
 
637
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
 
638
                " port %d\n", name, host_name, ip, interface, port);
682
639
      }
683
640
      int ret = start_mandos_communication(ip, port, interface, mc);
684
641
      if (ret == 0){
685
 
        avahi_simple_poll_quit(mc->simple_poll);
 
642
        exit(EXIT_SUCCESS);
686
643
      }
687
644
    }
688
645
  }
777
734
    const char *seckeyfile = "seckey.txt";
778
735
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
779
736
                          .dh_bits = 1024, .priority = "SECURE256"};
780
 
    bool gnutls_initalized = false;
781
737
    
782
738
    {
783
739
      struct argp_option options[] = {
785
741
          .doc = "Debug mode", .group = 3 },
786
742
        { .name = "connect", .key = 'c',
787
743
          .arg = "IP",
788
 
          .doc = "Connect directly to a sepcified mandos server",
789
 
          .group = 1 },
 
744
          .doc = "Connect directly to a sepcified mandos server", .group = 1 },
790
745
        { .name = "interface", .key = 'i',
791
746
          .arg = "INTERFACE",
792
 
          .doc = "Interface that Avahi will conntect through",
793
 
          .group = 1 },
 
747
          .doc = "Interface that Avahi will conntect through", .group = 1 },
794
748
        { .name = "keydir", .key = 'd',
795
749
          .arg = "KEYDIR",
796
 
          .doc = "Directory where the openpgp keyring is",
797
 
          .group = 1 },
 
750
          .doc = "Directory where the openpgp keyring is", .group = 1 },
798
751
        { .name = "seckey", .key = 's',
799
752
          .arg = "SECKEY",
800
 
          .doc = "Secret openpgp key for gnutls authentication",
801
 
          .group = 1 },
 
753
          .doc = "Secret openpgp key for gnutls authentication", .group = 1 },
802
754
        { .name = "pubkey", .key = 'p',
803
755
          .arg = "PUBKEY",
804
 
          .doc = "Public openpgp key for gnutls authentication",
805
 
          .group = 2 },
 
756
          .doc = "Public openpgp key for gnutls authentication", .group = 2 },
806
757
        { .name = "dh-bits", .key = 129,
807
758
          .arg = "BITS",
808
 
          .doc = "dh-bits to use in gnutls communication",
809
 
          .group = 2 },
 
759
          .doc = "dh-bits to use in gnutls communication", .group = 2 },
810
760
        { .name = "priority", .key = 130,
811
761
          .arg = "PRIORITY",
812
762
          .doc = "GNUTLS priority", .group = 1 },
814
764
      };
815
765
 
816
766
      
817
 
      error_t parse_opt (int key, char *arg,
818
 
                         struct argp_state *state) {
819
 
        /* Get the INPUT argument from `argp_parse', which we know is
820
 
           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. */
821
770
        switch (key) {
822
771
        case 128:
823
772
          debug = true;
850
799
          break;
851
800
        case ARGP_KEY_ARG:
852
801
          argp_usage (state);
853
 
        case ARGP_KEY_END:
854
802
          break;
 
803
          case ARGP_KEY_END:
 
804
            break;
855
805
        default:
856
806
          return ARGP_ERR_UNKNOWN;
857
807
        }
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, "Unknown 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);
880
824
    seckeyfile = combinepath(keydir, seckeyfile);
881
825
    if (seckeyfile == NULL){
882
826
      perror("combinepath");
883
 
      exitcode = EXIT_FAILURE;
884
827
      goto end;
885
828
    }
886
829
 
887
830
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
888
831
    if (ret == -1){
889
 
      fprintf(stderr, "init_gnutls_global failed\n");
890
 
      exitcode = EXIT_FAILURE;
 
832
      fprintf(stderr, "init_gnutls_global\n");
891
833
      goto end;
892
 
    } else {
893
 
      gnutls_initalized = true;
894
 
    }
895
 
    
896
 
    /* If the interface is down, bring it up */
897
 
    {
898
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
899
 
      if(sd < 0) {
900
 
        perror("socket");
901
 
        exitcode = EXIT_FAILURE;
902
 
        goto end;
903
 
      }
904
 
      strcpy(network.ifr_name, interface); /* Spurious warning */
905
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
906
 
      if(ret == -1){
907
 
        perror("ioctl SIOCGIFFLAGS");
908
 
        exitcode = EXIT_FAILURE;
909
 
        goto end;
910
 
      }
911
 
      if((network.ifr_flags & IFF_UP) == 0){
912
 
        network.ifr_flags |= IFF_UP;
913
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
914
 
        if(ret == -1){
915
 
          perror("ioctl SIOCSIFFLAGS");
916
 
          exitcode = EXIT_FAILURE;
917
 
          goto end;
918
 
        }
919
 
      }
920
 
      close(sd);
921
 
    }
922
 
    
 
834
    }
 
835
 
923
836
    uid = getuid();
924
837
    gid = getgid();
925
 
    
 
838
 
926
839
    ret = setuid(uid);
927
840
    if (ret == -1){
928
841
      perror("setuid");
966
879
      goto end;
967
880
    }
968
881
    
 
882
    /* If the interface is down, bring it up */
 
883
    {
 
884
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
885
      if(sd < 0) {
 
886
        perror("socket");
 
887
        exitcode = EXIT_FAILURE;
 
888
        goto end;
 
889
      }
 
890
      strcpy(network.ifr_name, interface); /* Spurious warning */
 
891
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
892
      if(ret == -1){
 
893
        perror("ioctl SIOCGIFFLAGS");
 
894
        exitcode = EXIT_FAILURE;
 
895
        goto end;
 
896
      }
 
897
      if((network.ifr_flags & IFF_UP) == 0){
 
898
        network.ifr_flags |= IFF_UP;
 
899
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
900
        if(ret == -1){
 
901
          perror("ioctl SIOCSIFFLAGS");
 
902
          exitcode = EXIT_FAILURE;
 
903
          goto end;
 
904
        }
 
905
      }
 
906
      close(sd);
 
907
    }
 
908
    
969
909
    if (not debug){
970
910
      avahi_set_log_function(empty_log);
971
911
    }
1045
985
        avahi_simple_poll_free(mc.simple_poll);
1046
986
    free(pubkeyfile);
1047
987
    free(seckeyfile);
1048
 
 
1049
 
    if (gnutls_initalized){
1050
 
      gnutls_certificate_free_credentials(mc.cred);
1051
 
      gnutls_global_deinit ();
1052
 
    }
1053
988
    
1054
989
    return exitcode;
1055
990
}