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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-16 20:32:58 UTC
  • mfrom: (24.1.55 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080816203258-eq4by31vdgxkhs9g
* mandos-keygen.xml: New man page for mandos-keygen(8).

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);
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) {
 
342
  gnutls_certificate_allocate_credentials(&mc->cred);
 
343
  if (ret != GNUTLS_E_SUCCESS){
376
344
    fprintf (stderr, "GnuTLS memory error: %s\n",
377
345
             safer_gnutls_strerror(ret));
378
346
    gnutls_global_deinit ();
381
349
  
382
350
  if(debug){
383
351
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
384
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
385
 
            seckeyfile);
 
352
            " and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
 
353
            seckeyfilename);
386
354
  }
387
355
  
388
356
  ret = gnutls_certificate_set_openpgp_key_file
389
 
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
 
357
    (mc->cred, pubkeyfilename, seckeyfilename,
 
358
     GNUTLS_OPENPGP_FMT_BASE64);
390
359
  if (ret != GNUTLS_E_SUCCESS) {
391
360
    fprintf(stderr,
392
361
            "Error[%d] while reading the OpenPGP key pair ('%s',"
393
 
            " '%s')\n", ret, pubkeyfile, seckeyfile);
 
362
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
394
363
    fprintf(stdout, "The GnuTLS error is: %s\n",
395
364
            safer_gnutls_strerror(ret));
396
365
    goto globalfail;
488
457
  }
489
458
  
490
459
  if(debug){
491
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
492
 
            ip, port);
 
460
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
 
461
            "\n", ip, port);
493
462
  }
494
463
  
495
464
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
506
475
    fprintf(stderr, "Binding to interface %s\n", interface);
507
476
  }
508
477
  
509
 
  memset(&to,0,sizeof(to));     /* Spurious warning */
 
478
  memset(&to, 0, sizeof(to));   /* Spurious warning */
510
479
  to.in6.sin6_family = AF_INET6;
511
480
  /* It would be nice to have a way to detect if we were passed an
512
481
     IPv4 address here.   Now we assume an IPv6 address. */
519
488
    fprintf(stderr, "Bad address: %s\n", ip);
520
489
    return -1;
521
490
  }
522
 
  to.in6.sin6_port = htons(port);       /* Spurious warning */
 
491
  to.in6.sin6_port = htons(port); /* Spurious warning */
523
492
  
524
493
  to.in6.sin6_scope_id = (uint32_t)if_index;
525
494
  
526
495
  if(debug){
527
 
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
 
496
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
497
            port);
528
498
    char addrstr[INET6_ADDRSTRLEN] = "";
529
499
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
530
500
                 sizeof(addrstr)) == NULL){
571
541
  }
572
542
  
573
543
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
574
 
  
575
 
  ret = gnutls_handshake (session);
 
544
 
 
545
  do{
 
546
    ret = gnutls_handshake (session);
 
547
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
576
548
  
577
549
  if (ret != GNUTLS_E_SUCCESS){
578
550
    if(debug){
610
582
      case GNUTLS_E_AGAIN:
611
583
        break;
612
584
      case GNUTLS_E_REHANDSHAKE:
613
 
        ret = gnutls_handshake (session);
 
585
        do{
 
586
          ret = gnutls_handshake (session);
 
587
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
614
588
        if (ret < 0){
615
589
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
616
590
          gnutls_perror (ret);
705
679
      char ip[AVAHI_ADDRESS_STR_MAX];
706
680
      avahi_address_snprint(ip, sizeof(ip), address);
707
681
      if(debug){
708
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
709
 
                " port %d\n", name, host_name, ip, interface, port);
 
682
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
683
                PRIu16 ") on port %d\n", name, host_name, ip,
 
684
                interface, port);
710
685
      }
711
686
      int ret = start_mandos_communication(ip, port, interface, mc);
712
687
      if (ret == 0){
713
 
        exit(EXIT_SUCCESS);
 
688
        avahi_simple_poll_quit(mc->simple_poll);
714
689
      }
715
690
    }
716
691
  }
770
745
 
771
746
/* Combines file name and path and returns the malloced new
772
747
   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){
 
748
static char *combinepath(const char *first, const char *second){
 
749
  char *tmp;
 
750
  int ret = asprintf(&tmp, "%s/%s", first, second);
 
751
  if(ret < 0){
778
752
    return NULL;
779
753
  }
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
754
  return tmp;
789
755
}
790
756
 
801
767
    gid_t gid;
802
768
    char *connect_to = NULL;
803
769
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
804
 
    const char *pubkeyfile = "pubkey.txt";
805
 
    const char *seckeyfile = "seckey.txt";
 
770
    char *pubkeyfilename = NULL;
 
771
    char *seckeyfilename = NULL;
 
772
    const char *pubkeyname = "pubkey.txt";
 
773
    const char *seckeyname = "seckey.txt";
806
774
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
807
775
                          .dh_bits = 1024, .priority = "SECURE256"};
808
776
    bool gnutls_initalized = false;
860
828
          keydir = arg;
861
829
          break;
862
830
        case 's':
863
 
          seckeyfile = arg;
 
831
          seckeyname = arg;
864
832
          break;
865
833
        case 'p':
866
 
          pubkeyfile = arg;
 
834
          pubkeyname = arg;
867
835
          break;
868
836
        case 129:
869
837
          errno = 0;
878
846
          break;
879
847
        case ARGP_KEY_ARG:
880
848
          argp_usage (state);
 
849
        case ARGP_KEY_END:
881
850
          break;
882
 
          case ARGP_KEY_END:
883
 
            break;
884
851
        default:
885
852
          return ARGP_ERR_UNKNOWN;
886
853
        }
891
858
                           .args_doc = "",
892
859
                           .doc = "Mandos client -- Get and decrypt"
893
860
                           " passwords from mandos server" };
894
 
      argp_parse (&argp, argc, argv, 0, 0, NULL);
 
861
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
862
      if (ret == ARGP_ERR_UNKNOWN){
 
863
        fprintf(stderr, "Unknown error while parsing arguments\n");
 
864
        exitcode = EXIT_FAILURE;
 
865
        goto end;
 
866
      }
895
867
    }
896
868
      
897
 
    pubkeyfile = combinepath(keydir, pubkeyfile);
898
 
    if (pubkeyfile == NULL){
 
869
    pubkeyfilename = combinepath(keydir, pubkeyname);
 
870
    if (pubkeyfilename == NULL){
899
871
      perror("combinepath");
900
872
      exitcode = EXIT_FAILURE;
901
873
      goto end;
902
874
    }
903
875
    
904
 
    seckeyfile = combinepath(keydir, seckeyfile);
905
 
    if (seckeyfile == NULL){
 
876
    seckeyfilename = combinepath(keydir, seckeyname);
 
877
    if (seckeyfilename == NULL){
906
878
      perror("combinepath");
 
879
      exitcode = EXIT_FAILURE;
907
880
      goto end;
908
881
    }
909
882
 
910
 
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
 
883
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
911
884
    if (ret == -1){
912
 
      fprintf(stderr, "init_gnutls_global\n");
 
885
      fprintf(stderr, "init_gnutls_global failed\n");
 
886
      exitcode = EXIT_FAILURE;
913
887
      goto end;
914
888
    } else {
915
889
      gnutls_initalized = true;
916
890
    }
917
 
 
 
891
    
 
892
    /* If the interface is down, bring it up */
 
893
    {
 
894
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
895
      if(sd < 0) {
 
896
        perror("socket");
 
897
        exitcode = EXIT_FAILURE;
 
898
        goto end;
 
899
      }
 
900
      strcpy(network.ifr_name, interface); /* Spurious warning */
 
901
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
902
      if(ret == -1){
 
903
        perror("ioctl SIOCGIFFLAGS");
 
904
        exitcode = EXIT_FAILURE;
 
905
        goto end;
 
906
      }
 
907
      if((network.ifr_flags & IFF_UP) == 0){
 
908
        network.ifr_flags |= IFF_UP;
 
909
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
910
        if(ret == -1){
 
911
          perror("ioctl SIOCSIFFLAGS");
 
912
          exitcode = EXIT_FAILURE;
 
913
          goto end;
 
914
        }
 
915
      }
 
916
      close(sd);
 
917
    }
 
918
    
918
919
    uid = getuid();
919
920
    gid = getgid();
920
 
 
 
921
    
921
922
    ret = setuid(uid);
922
923
    if (ret == -1){
923
924
      perror("setuid");
961
962
      goto end;
962
963
    }
963
964
    
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
965
    if (not debug){
992
966
      avahi_set_log_function(empty_log);
993
967
    }
1065
1039
 
1066
1040
    if (mc.simple_poll != NULL)
1067
1041
        avahi_simple_poll_free(mc.simple_poll);
1068
 
    free(pubkeyfile);
1069
 
    free(seckeyfile);
 
1042
    free(pubkeyfilename);
 
1043
    free(seckeyfilename);
1070
1044
 
1071
1045
    if (gnutls_initalized){
1072
 
      gnutls_certificate_free_credentials (mc.cred);
 
1046
      gnutls_certificate_free_credentials(mc.cred);
1073
1047
      gnutls_global_deinit ();
1074
1048
    }
1075
1049