/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

merge

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 */
47
 
#include <net/if.h>             /* ifreq, SIOCGIFFLAGS, SIOCSIFFLAGS,
48
 
                                   IFF_UP */
 
42
#include <string.h>             /* memset(), strcmp(), strlen, strerror() */
 
43
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
 
44
                                   SIOCSIFFLAGS */
49
45
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
50
 
                                   sockaddr_in6, PF_INET6,
51
 
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
52
 
                                   uid_t, gid_t */
 
46
                                   sockaddr_in6, PF_INET6, SOCK_STREAM, INET6_ADDRSTRLEN */
53
47
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
54
48
                                   struct in6_addr, inet_pton(),
55
49
                                   connect() */
56
 
#include <assert.h>             /* assert() */
57
 
#include <errno.h>              /* perror(), errno */
58
 
#include <time.h>               /* time() */
 
50
#include <assert.h>
 
51
#include <errno.h>              /* perror() */
 
52
#include <time.h>
59
53
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
60
54
                                   SIOCSIFFLAGS, if_indextoname(),
61
55
                                   if_nametoindex(), IF_NAMESIZE */
62
 
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
63
 
                                   getuid(), getgid(), setuid(),
64
 
                                   setgid() */
 
56
#include <unistd.h>             /* close(), SEEK_SET, off_t, write()*/
65
57
#include <netinet/in.h>
66
58
#include <arpa/inet.h>          /* inet_pton(), htons */
67
 
#include <iso646.h>             /* not, and */
68
 
#include <argp.h>               /* struct argp_option, error_t, struct
69
 
                                   argp_state, struct argp,
70
 
                                   argp_parse(), ARGP_KEY_ARG,
71
 
                                   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() */
72
63
 
73
64
/* Avahi */
74
 
/* All Avahi types, constants and functions
75
 
 Avahi*, avahi_*,
76
 
 AVAHI_* */
77
 
#include <avahi-core/core.h>
 
65
#include <avahi-core/core.h>    /* AvahiSimplePoll, AvahiServer,
 
66
                                   AvahiIfIndex */
78
67
#include <avahi-core/lookup.h>
79
 
#include <avahi-core/log.h>
 
68
#include <avahi-core/log.h>     /* AvahiLogLevel */
80
69
#include <avahi-common/simple-watch.h>
81
70
#include <avahi-common/malloc.h>
82
71
#include <avahi-common/error.h>
83
72
 
84
73
/* GnuTLS */
85
 
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and 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 functions
94
 
                                   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()
95
127
                                   GPGME_PROTOCOL_OpenPGP,
96
 
                                   GPG_ERR_NO_* */
 
128
                                   GPG_ERR_NO_ERROR,
 
129
                                   GPG_ERR_NO_SECKEY, */
97
130
 
98
131
#define BUFFER_SIZE 256
99
132
 
100
133
bool debug = false;
101
134
static const char *keydir = "/conf/conf.d/mandos";
102
135
static const char mandos_protocol_version[] = "1";
103
 
const char *argp_program_version = "mandosclient 1.0";
 
136
const char *argp_program_version = "mandosclient 0.9";
104
137
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
105
138
 
106
139
/* Used for passing in values through the Avahi callback functions */
321
354
  if(debug){
322
355
    fprintf(stderr, "Initializing GnuTLS\n");
323
356
  }
324
 
  
325
 
  ret = gnutls_global_init();
326
 
  if (ret != GNUTLS_E_SUCCESS) {
 
357
 
 
358
  if ((ret = gnutls_global_init ())
 
359
      != GNUTLS_E_SUCCESS) {
327
360
    fprintf (stderr, "GnuTLS global_init: %s\n",
328
361
             safer_gnutls_strerror(ret));
329
362
    return -1;
338
371
  }
339
372
  
340
373
  /* OpenPGP credentials */
341
 
  gnutls_certificate_allocate_credentials(&mc->cred);
342
 
  if (ret != GNUTLS_E_SUCCESS){
 
374
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
375
      != GNUTLS_E_SUCCESS) {
343
376
    fprintf (stderr, "GnuTLS memory error: %s\n",
344
377
             safer_gnutls_strerror(ret));
345
378
    gnutls_global_deinit ();
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);
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, "Unkown 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);
1045
1069
    free(seckeyfile);
1046
1070
 
1047
1071
    if (gnutls_initalized){
1048
 
      gnutls_certificate_free_credentials(mc.cred);
 
1072
      gnutls_certificate_free_credentials (mc.cred);
1049
1073
      gnutls_global_deinit ();
1050
1074
    }
1051
1075