/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-18 23:55:28 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080818235528-dn628nlbrtzl7z4f
* Makefile: Bug fix: fixed creation of man pages for section 5 pages.

* mandos (main): Changed from requiring "[server]" in mandos.conf(5)
                 to requiring "[DEFAULT]".

* mandos.conf ([server]): Renamed to "[DEFAULT]".

* mandos.conf.xml: Removed <?xml-stylesheet>.  New entity "&OVERVIEW;"
                   referring to "overview.xml".
  (DESCRIPTION): Updated to specify the syntax more precisely.  Use
                 <varname> around the option names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#define _LARGEFILE_SOURCE
33
33
#define _FILE_OFFSET_BITS 64
34
34
 
35
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
 
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
36
36
 
37
 
#include <stdio.h>              /* fprintf(), stderr, fwrite(), stdout, ferror() */
 
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
 
38
                                   stdout, ferror() */
38
39
#include <stdint.h>             /* uint16_t, uint32_t */
39
40
#include <stddef.h>             /* NULL, size_t, ssize_t */
40
 
#include <stdlib.h>             /* free() */
 
41
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
 
42
                                   srand() */
41
43
#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 */
 
44
#include <string.h>             /* memset(), strcmp(), strlen(),
 
45
                                   strerror(), asprintf(), strcpy() */
 
46
#include <sys/ioctl.h>          /* ioctl */
45
47
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
46
 
                                   sockaddr_in6, PF_INET6, SOCK_STREAM, INET6_ADDRSTRLEN */
 
48
                                   sockaddr_in6, PF_INET6,
 
49
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
 
50
                                   uid_t, gid_t */
 
51
#include <inttypes.h>           /* PRIu16 */
47
52
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
48
53
                                   struct in6_addr, inet_pton(),
49
54
                                   connect() */
50
 
#include <assert.h>
51
 
#include <errno.h>              /* perror() */
52
 
#include <time.h>
 
55
#include <assert.h>             /* assert() */
 
56
#include <errno.h>              /* perror(), errno */
 
57
#include <time.h>               /* time() */
53
58
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
54
59
                                   SIOCSIFFLAGS, if_indextoname(),
55
60
                                   if_nametoindex(), IF_NAMESIZE */
56
 
#include <unistd.h>             /* close(), SEEK_SET, off_t, write()*/
 
61
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
 
62
                                   getuid(), getgid(), setuid(),
 
63
                                   setgid() */
57
64
#include <netinet/in.h>
58
65
#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() */
 
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 */
63
71
 
64
72
/* Avahi */
65
 
#include <avahi-core/core.h>    /* AvahiSimplePoll, AvahiServer,
66
 
                                   AvahiIfIndex */
 
73
/* All Avahi types, constants and functions
 
74
 Avahi*, avahi_*,
 
75
 AVAHI_* */
 
76
#include <avahi-core/core.h>
67
77
#include <avahi-core/lookup.h>
68
 
#include <avahi-core/log.h>     /* AvahiLogLevel */
 
78
#include <avahi-core/log.h>
69
79
#include <avahi-common/simple-watch.h>
70
80
#include <avahi-common/malloc.h>
71
81
#include <avahi-common/error.h>
72
82
 
73
83
/* 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(),
 
84
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and
 
85
                                   functions:
 
86
                                   gnutls_*
97
87
                                   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 */
 
88
                                   GNUTLS_* */
 
89
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
 
90
                                   GNUTLS_OPENPGP_FMT_BASE64 */
107
91
 
108
92
/* 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()
 
93
#include <gpgme.h>              /* All GPGME types, constants and
 
94
                                   functions:
 
95
                                   gpgme_*
127
96
                                   GPGME_PROTOCOL_OpenPGP,
128
 
                                   GPG_ERR_NO_ERROR,
129
 
                                   GPG_ERR_NO_SECKEY, */
 
97
                                   GPG_ERR_NO_* */
130
98
 
131
99
#define BUFFER_SIZE 256
132
100
 
133
101
bool debug = false;
134
102
static const char *keydir = "/conf/conf.d/mandos";
135
103
static const char mandos_protocol_version[] = "1";
136
 
const char *argp_program_version = "mandosclient 0.9";
 
104
const char *argp_program_version = "password-request 1.0";
137
105
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
138
106
 
139
107
/* Used for passing in values through the Avahi callback functions */
261
229
    } else {
262
230
      fprintf(stderr, "Unsupported algorithm: %s\n",
263
231
              result->unsupported_algorithm);
264
 
      fprintf(stderr, "Wrong key usage: %d\n",
 
232
      fprintf(stderr, "Wrong key usage: %u\n",
265
233
              result->wrong_key_usage);
266
234
      if(result->file_name != NULL){
267
235
        fprintf(stderr, "File name: %s\n", result->file_name);
334
302
}
335
303
 
336
304
static const char * safer_gnutls_strerror (int value) {
337
 
  const char *ret = gnutls_strerror (value);
 
305
  const char *ret = gnutls_strerror (value); /* Spurious warning */
338
306
  if (ret == NULL)
339
307
    ret = "(unknown)";
340
308
  return ret;
347
315
}
348
316
 
349
317
static int init_gnutls_global(mandos_context *mc,
350
 
                              const char *pubkeyfile,
351
 
                              const char *seckeyfile){
 
318
                              const char *pubkeyfilename,
 
319
                              const char *seckeyfilename){
352
320
  int ret;
353
321
  
354
322
  if(debug){
355
323
    fprintf(stderr, "Initializing GnuTLS\n");
356
324
  }
357
 
 
358
 
  if ((ret = gnutls_global_init ())
359
 
      != GNUTLS_E_SUCCESS) {
 
325
  
 
326
  ret = gnutls_global_init();
 
327
  if (ret != GNUTLS_E_SUCCESS) {
360
328
    fprintf (stderr, "GnuTLS global_init: %s\n",
361
329
             safer_gnutls_strerror(ret));
362
330
    return -1;
371
339
  }
372
340
  
373
341
  /* OpenPGP credentials */
374
 
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
375
 
      != GNUTLS_E_SUCCESS) {
376
 
    fprintf (stderr, "GnuTLS memory error: %s\n",
 
342
  gnutls_certificate_allocate_credentials(&mc->cred);
 
343
  if (ret != GNUTLS_E_SUCCESS){
 
344
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
 
345
                                                     warning */
377
346
             safer_gnutls_strerror(ret));
378
347
    gnutls_global_deinit ();
379
348
    return -1;
381
350
  
382
351
  if(debug){
383
352
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
384
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
385
 
            seckeyfile);
 
353
            " and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
 
354
            seckeyfilename);
386
355
  }
387
356
  
388
357
  ret = gnutls_certificate_set_openpgp_key_file
389
 
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
 
358
    (mc->cred, pubkeyfilename, seckeyfilename,
 
359
     GNUTLS_OPENPGP_FMT_BASE64);
390
360
  if (ret != GNUTLS_E_SUCCESS) {
391
361
    fprintf(stderr,
392
362
            "Error[%d] while reading the OpenPGP key pair ('%s',"
393
 
            " '%s')\n", ret, pubkeyfile, seckeyfile);
 
363
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
394
364
    fprintf(stdout, "The GnuTLS error is: %s\n",
395
365
            safer_gnutls_strerror(ret));
396
366
    goto globalfail;
488
458
  }
489
459
  
490
460
  if(debug){
491
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
492
 
            ip, port);
 
461
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
 
462
            "\n", ip, port);
493
463
  }
494
464
  
495
465
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
506
476
    fprintf(stderr, "Binding to interface %s\n", interface);
507
477
  }
508
478
  
509
 
  memset(&to,0,sizeof(to));     /* Spurious warning */
 
479
  memset(&to, 0, sizeof(to));
510
480
  to.in6.sin6_family = AF_INET6;
511
481
  /* It would be nice to have a way to detect if we were passed an
512
482
     IPv4 address here.   Now we assume an IPv6 address. */
519
489
    fprintf(stderr, "Bad address: %s\n", ip);
520
490
    return -1;
521
491
  }
522
 
  to.in6.sin6_port = htons(port);       /* Spurious warning */
 
492
  to.in6.sin6_port = htons(port); /* Spurious warning */
523
493
  
524
494
  to.in6.sin6_scope_id = (uint32_t)if_index;
525
495
  
526
496
  if(debug){
527
 
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
 
497
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
498
            port);
528
499
    char addrstr[INET6_ADDRSTRLEN] = "";
529
500
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
530
501
                 sizeof(addrstr)) == NULL){
571
542
  }
572
543
  
573
544
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
574
 
  
575
 
  ret = gnutls_handshake (session);
 
545
 
 
546
  do{
 
547
    ret = gnutls_handshake (session);
 
548
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
576
549
  
577
550
  if (ret != GNUTLS_E_SUCCESS){
578
551
    if(debug){
610
583
      case GNUTLS_E_AGAIN:
611
584
        break;
612
585
      case GNUTLS_E_REHANDSHAKE:
613
 
        ret = gnutls_handshake (session);
 
586
        do{
 
587
          ret = gnutls_handshake (session);
 
588
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
614
589
        if (ret < 0){
615
590
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
616
591
          gnutls_perror (ret);
687
662
                             flags,
688
663
                             void* userdata) {
689
664
  mandos_context *mc = userdata;
690
 
  assert(r);                    /* Spurious warning */
 
665
  assert(r);
691
666
  
692
667
  /* Called whenever a service has been resolved successfully or
693
668
     timed out */
705
680
      char ip[AVAHI_ADDRESS_STR_MAX];
706
681
      avahi_address_snprint(ip, sizeof(ip), address);
707
682
      if(debug){
708
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
709
 
                " port %d\n", name, host_name, ip, interface, port);
 
683
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
684
                PRIu16 ") on port %d\n", name, host_name, ip,
 
685
                interface, port);
710
686
      }
711
687
      int ret = start_mandos_communication(ip, port, interface, mc);
712
688
      if (ret == 0){
713
 
        exit(EXIT_SUCCESS);
 
689
        avahi_simple_poll_quit(mc->simple_poll);
714
690
      }
715
691
    }
716
692
  }
728
704
                             flags,
729
705
                             void* userdata) {
730
706
  mandos_context *mc = userdata;
731
 
  assert(b);                    /* Spurious warning */
 
707
  assert(b);
732
708
  
733
709
  /* Called whenever a new services becomes available on the LAN or
734
710
     is removed from the LAN */
770
746
 
771
747
/* Combines file name and path and returns the malloced new
772
748
   string. some sane checks could/should be added */
773
 
static const char *combinepath(const char *first, const char *second){
774
 
  size_t f_len = strlen(first);
775
 
  size_t s_len = strlen(second);
776
 
  char *tmp = malloc(f_len + s_len + 2);
777
 
  if (tmp == NULL){
 
749
static char *combinepath(const char *first, const char *second){
 
750
  char *tmp;
 
751
  int ret = asprintf(&tmp, "%s/%s", first, second);
 
752
  if(ret < 0){
778
753
    return NULL;
779
754
  }
780
 
  if(f_len > 0){
781
 
    memcpy(tmp, first, f_len);  /* Spurious warning */
782
 
  }
783
 
  tmp[f_len] = '/';
784
 
  if(s_len > 0){
785
 
    memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
786
 
  }
787
 
  tmp[f_len + 1 + s_len] = '\0';
788
755
  return tmp;
789
756
}
790
757
 
801
768
    gid_t gid;
802
769
    char *connect_to = NULL;
803
770
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
804
 
    const char *pubkeyfile = "pubkey.txt";
805
 
    const char *seckeyfile = "seckey.txt";
 
771
    char *pubkeyfilename = NULL;
 
772
    char *seckeyfilename = NULL;
 
773
    const char *pubkeyname = "pubkey.txt";
 
774
    const char *seckeyname = "seckey.txt";
806
775
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
807
776
                          .dh_bits = 1024, .priority = "SECURE256"};
808
777
    bool gnutls_initalized = false;
860
829
          keydir = arg;
861
830
          break;
862
831
        case 's':
863
 
          seckeyfile = arg;
 
832
          seckeyname = arg;
864
833
          break;
865
834
        case 'p':
866
 
          pubkeyfile = arg;
 
835
          pubkeyname = arg;
867
836
          break;
868
837
        case 129:
869
838
          errno = 0;
878
847
          break;
879
848
        case ARGP_KEY_ARG:
880
849
          argp_usage (state);
 
850
        case ARGP_KEY_END:
881
851
          break;
882
 
          case ARGP_KEY_END:
883
 
            break;
884
852
        default:
885
853
          return ARGP_ERR_UNKNOWN;
886
854
        }
891
859
                           .args_doc = "",
892
860
                           .doc = "Mandos client -- Get and decrypt"
893
861
                           " passwords from mandos server" };
894
 
      argp_parse (&argp, argc, argv, 0, 0, NULL);
 
862
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
863
      if (ret == ARGP_ERR_UNKNOWN){
 
864
        fprintf(stderr, "Unknown error while parsing arguments\n");
 
865
        exitcode = EXIT_FAILURE;
 
866
        goto end;
 
867
      }
895
868
    }
896
869
      
897
 
    pubkeyfile = combinepath(keydir, pubkeyfile);
898
 
    if (pubkeyfile == NULL){
 
870
    pubkeyfilename = combinepath(keydir, pubkeyname);
 
871
    if (pubkeyfilename == NULL){
899
872
      perror("combinepath");
900
873
      exitcode = EXIT_FAILURE;
901
874
      goto end;
902
875
    }
903
876
    
904
 
    seckeyfile = combinepath(keydir, seckeyfile);
905
 
    if (seckeyfile == NULL){
 
877
    seckeyfilename = combinepath(keydir, seckeyname);
 
878
    if (seckeyfilename == NULL){
906
879
      perror("combinepath");
 
880
      exitcode = EXIT_FAILURE;
907
881
      goto end;
908
882
    }
909
883
 
910
 
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
 
884
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
911
885
    if (ret == -1){
912
 
      fprintf(stderr, "init_gnutls_global\n");
 
886
      fprintf(stderr, "init_gnutls_global failed\n");
 
887
      exitcode = EXIT_FAILURE;
913
888
      goto end;
914
889
    } else {
915
890
      gnutls_initalized = true;
916
891
    }
917
 
 
 
892
    
 
893
    /* If the interface is down, bring it up */
 
894
    {
 
895
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
896
      if(sd < 0) {
 
897
        perror("socket");
 
898
        exitcode = EXIT_FAILURE;
 
899
        goto end;
 
900
      }
 
901
      strcpy(network.ifr_name, interface);
 
902
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
903
      if(ret == -1){
 
904
        perror("ioctl SIOCGIFFLAGS");
 
905
        exitcode = EXIT_FAILURE;
 
906
        goto end;
 
907
      }
 
908
      if((network.ifr_flags & IFF_UP) == 0){
 
909
        network.ifr_flags |= IFF_UP;
 
910
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
911
        if(ret == -1){
 
912
          perror("ioctl SIOCSIFFLAGS");
 
913
          exitcode = EXIT_FAILURE;
 
914
          goto end;
 
915
        }
 
916
      }
 
917
      close(sd);
 
918
    }
 
919
    
918
920
    uid = getuid();
919
921
    gid = getgid();
920
 
 
 
922
    
921
923
    ret = setuid(uid);
922
924
    if (ret == -1){
923
925
      perror("setuid");
961
963
      goto end;
962
964
    }
963
965
    
964
 
    /* If the interface is down, bring it up */
965
 
    {
966
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
967
 
      if(sd < 0) {
968
 
        perror("socket");
969
 
        exitcode = EXIT_FAILURE;
970
 
        goto end;
971
 
      }
972
 
      strcpy(network.ifr_name, interface); /* Spurious warning */
973
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
974
 
      if(ret == -1){
975
 
        perror("ioctl SIOCGIFFLAGS");
976
 
        exitcode = EXIT_FAILURE;
977
 
        goto end;
978
 
      }
979
 
      if((network.ifr_flags & IFF_UP) == 0){
980
 
        network.ifr_flags |= IFF_UP;
981
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
982
 
        if(ret == -1){
983
 
          perror("ioctl SIOCSIFFLAGS");
984
 
          exitcode = EXIT_FAILURE;
985
 
          goto end;
986
 
        }
987
 
      }
988
 
      close(sd);
989
 
    }
990
 
    
991
966
    if (not debug){
992
967
      avahi_set_log_function(empty_log);
993
968
    }
1065
1040
 
1066
1041
    if (mc.simple_poll != NULL)
1067
1042
        avahi_simple_poll_free(mc.simple_poll);
1068
 
    free(pubkeyfile);
1069
 
    free(seckeyfile);
 
1043
    free(pubkeyfilename);
 
1044
    free(seckeyfilename);
1070
1045
 
1071
1046
    if (gnutls_initalized){
1072
 
      gnutls_certificate_free_credentials (mc.cred);
 
1047
      gnutls_certificate_free_credentials(mc.cred);
1073
1048
      gnutls_global_deinit ();
1074
1049
    }
1075
1050