/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

tally count of used symbols
return codes from functions checked.

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