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