/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

Fixed unkown -> unknown
updated TODO

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, ferror() */
 
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(), stdout,
 
38
                                   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(), memcpy(), 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 functions
 
85
                                   gnutls_*
97
86
                                   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 */
 
87
                                   GNUTLS_* */
 
88
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
 
89
                                   GNUTLS_OPENPGP_FMT_BASE64 */
107
90
 
108
91
/* 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()
 
92
#include <gpgme.h>              /* All GPGME types, constants and functions
 
93
                                   gpgme_*
127
94
                                   GPGME_PROTOCOL_OpenPGP,
128
 
                                   GPG_ERR_NO_ERROR,
129
 
                                   GPG_ERR_NO_SECKEY, */
 
95
                                   GPG_ERR_NO_* */
130
96
 
131
97
#define BUFFER_SIZE 256
132
98
 
133
99
bool debug = false;
134
100
static const char *keydir = "/conf/conf.d/mandos";
135
101
static const char mandos_protocol_version[] = "1";
136
 
const char *argp_program_version = "mandosclient 0.9";
 
102
const char *argp_program_version = "password-request 1.0";
137
103
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
138
104
 
139
105
/* Used for passing in values through the Avahi callback functions */
261
227
    } else {
262
228
      fprintf(stderr, "Unsupported algorithm: %s\n",
263
229
              result->unsupported_algorithm);
264
 
      fprintf(stderr, "Wrong key usage: %d\n",
 
230
      fprintf(stderr, "Wrong key usage: %u\n",
265
231
              result->wrong_key_usage);
266
232
      if(result->file_name != NULL){
267
233
        fprintf(stderr, "File name: %s\n", result->file_name);
354
320
  if(debug){
355
321
    fprintf(stderr, "Initializing GnuTLS\n");
356
322
  }
357
 
 
358
 
  if ((ret = gnutls_global_init ())
359
 
      != GNUTLS_E_SUCCESS) {
 
323
  
 
324
  ret = gnutls_global_init();
 
325
  if (ret != GNUTLS_E_SUCCESS) {
360
326
    fprintf (stderr, "GnuTLS global_init: %s\n",
361
327
             safer_gnutls_strerror(ret));
362
328
    return -1;
371
337
  }
372
338
  
373
339
  /* OpenPGP credentials */
374
 
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
375
 
      != GNUTLS_E_SUCCESS) {
 
340
  gnutls_certificate_allocate_credentials(&mc->cred);
 
341
  if (ret != GNUTLS_E_SUCCESS){
376
342
    fprintf (stderr, "GnuTLS memory error: %s\n",
377
343
             safer_gnutls_strerror(ret));
378
344
    gnutls_global_deinit ();
488
454
  }
489
455
  
490
456
  if(debug){
491
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
492
 
            ip, port);
 
457
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
 
458
            "\n", ip, port);
493
459
  }
494
460
  
495
461
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
519
485
    fprintf(stderr, "Bad address: %s\n", ip);
520
486
    return -1;
521
487
  }
522
 
  to.in6.sin6_port = htons(port);       /* Spurious warning */
 
488
  to.in6.sin6_port = htons(port); /* Spurious warning */
523
489
  
524
490
  to.in6.sin6_scope_id = (uint32_t)if_index;
525
491
  
526
492
  if(debug){
527
 
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
 
493
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
494
            port);
528
495
    char addrstr[INET6_ADDRSTRLEN] = "";
529
496
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
530
497
                 sizeof(addrstr)) == NULL){
571
538
  }
572
539
  
573
540
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
574
 
  
575
 
  ret = gnutls_handshake (session);
 
541
 
 
542
  do{
 
543
    ret = gnutls_handshake (session);
 
544
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
576
545
  
577
546
  if (ret != GNUTLS_E_SUCCESS){
578
547
    if(debug){
610
579
      case GNUTLS_E_AGAIN:
611
580
        break;
612
581
      case GNUTLS_E_REHANDSHAKE:
613
 
        ret = gnutls_handshake (session);
 
582
        do{
 
583
          ret = gnutls_handshake (session);
 
584
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
614
585
        if (ret < 0){
615
586
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
616
587
          gnutls_perror (ret);
705
676
      char ip[AVAHI_ADDRESS_STR_MAX];
706
677
      avahi_address_snprint(ip, sizeof(ip), address);
707
678
      if(debug){
708
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
709
 
                " port %d\n", name, host_name, ip, interface, port);
 
679
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
680
                PRIu16 ") on port %d\n", name, host_name, ip,
 
681
                interface, port);
710
682
      }
711
683
      int ret = start_mandos_communication(ip, port, interface, mc);
712
684
      if (ret == 0){
891
863
                           .args_doc = "",
892
864
                           .doc = "Mandos client -- Get and decrypt"
893
865
                           " passwords from mandos server" };
894
 
      argp_parse (&argp, argc, argv, 0, 0, NULL);
 
866
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
867
      if (ret == ARGP_ERR_UNKNOWN){
 
868
        fprintf(stderr, "Unknown error while parsing arguments\n");
 
869
        exitcode = EXIT_FAILURE;
 
870
        goto end;
 
871
      }
895
872
    }
896
873
      
897
874
    pubkeyfile = combinepath(keydir, pubkeyfile);
914
891
    } else {
915
892
      gnutls_initalized = true;
916
893
    }
917
 
 
 
894
    
 
895
    /* If the interface is down, bring it up */
 
896
    {
 
897
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
898
      if(sd < 0) {
 
899
        perror("socket");
 
900
        exitcode = EXIT_FAILURE;
 
901
        goto end;
 
902
      }
 
903
      strcpy(network.ifr_name, interface); /* Spurious warning */
 
904
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
905
      if(ret == -1){
 
906
        perror("ioctl SIOCGIFFLAGS");
 
907
        exitcode = EXIT_FAILURE;
 
908
        goto end;
 
909
      }
 
910
      if((network.ifr_flags & IFF_UP) == 0){
 
911
        network.ifr_flags |= IFF_UP;
 
912
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
913
        if(ret == -1){
 
914
          perror("ioctl SIOCSIFFLAGS");
 
915
          exitcode = EXIT_FAILURE;
 
916
          goto end;
 
917
        }
 
918
      }
 
919
      close(sd);
 
920
    }
 
921
    
918
922
    uid = getuid();
919
923
    gid = getgid();
920
 
 
 
924
    
921
925
    ret = setuid(uid);
922
926
    if (ret == -1){
923
927
      perror("setuid");
961
965
      goto end;
962
966
    }
963
967
    
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
968
    if (not debug){
992
969
      avahi_set_log_function(empty_log);
993
970
    }
1069
1046
    free(seckeyfile);
1070
1047
 
1071
1048
    if (gnutls_initalized){
1072
 
      gnutls_certificate_free_credentials (mc.cred);
 
1049
      gnutls_certificate_free_credentials(mc.cred);
1073
1050
      gnutls_global_deinit ();
1074
1051
    }
1075
1052