32
32
#define _LARGEFILE_SOURCE
33
33
#define _FILE_OFFSET_BITS 64
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY() */
37
#include <stdio.h> /* fprintf(), stderr, fwrite(),
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,
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,
47
45
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
48
sockaddr_in6, PF_INET6,
49
SOCK_STREAM, INET6_ADDRSTRLEN,
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(),
55
#include <assert.h> /* assert() */
56
#include <errno.h> /* perror(), errno */
57
#include <time.h> /* time() */
51
#include <errno.h> /* perror() */
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(),
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,
73
/* All Avahi types, constants and functions
76
#include <avahi-core/core.h>
65
#include <avahi-core/core.h> /* AvahiSimplePoll, AvahiServer,
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>
84
#include <gnutls/gnutls.h> /* All GnuTLS types, constants and
74
#include <gnutls/gnutls.h> /* gnutls_certificate_credentials_t,
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(),
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,
96
gnutls_perror(), gnutls_bye(),
87
97
init_gnutls_session(),
89
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
90
GNUTLS_OPENPGP_FMT_BASE64 */
99
GNUTLS_CRD_CERTIFICATE,
101
GNUTLS_E_INTERRUPTED,
103
GNUTLS_E_REHANDSHAKE,
105
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
106
GNUTLS_OPENPGP_FMT_BASE64 */
93
#include <gpgme.h> /* All GPGME types, constants and
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(),
115
gpgme_get_engine_info(),
116
gpgme_set_engine_info(),
117
gpgme_data_new_from_mem(),
118
gpgme_data_new(), gpgme_new(),
120
gpgme_decrypt_result_t,
121
gpgme_op_decrypt_result(),
123
gpgme_pubkey_algo_name(),
96
127
GPGME_PROTOCOL_OpenPGP,
129
GPG_ERR_NO_SECKEY, */
99
131
#define BUFFER_SIZE 256
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>";
107
139
/* Used for passing in values through the Avahi callback functions */
214
246
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
215
247
gpgme_strsource(rc), gpgme_strerror(rc));
216
248
plaintext_length = -1;
218
gpgme_decrypt_result_t result;
219
result = gpgme_op_decrypt_result(ctx);
221
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
223
fprintf(stderr, "Unsupported algorithm: %s\n",
224
result->unsupported_algorithm);
225
fprintf(stderr, "Wrong key usage: %u\n",
226
result->wrong_key_usage);
227
if(result->file_name != NULL){
228
fprintf(stderr, "File name: %s\n", result->file_name);
230
gpgme_recipient_t recipient;
231
recipient = result->recipients;
233
while(recipient != NULL){
234
fprintf(stderr, "Public key algorithm: %s\n",
235
gpgme_pubkey_algo_name(recipient->pubkey_algo));
236
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
237
fprintf(stderr, "Secret key available: %s\n",
238
recipient->status == GPG_ERR_NO_SECKEY
240
recipient = recipient->next;
245
249
goto decrypt_end;
249
253
fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
257
gpgme_decrypt_result_t result;
258
result = gpgme_op_decrypt_result(ctx);
260
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
262
fprintf(stderr, "Unsupported algorithm: %s\n",
263
result->unsupported_algorithm);
264
fprintf(stderr, "Wrong key usage: %d\n",
265
result->wrong_key_usage);
266
if(result->file_name != NULL){
267
fprintf(stderr, "File name: %s\n", result->file_name);
269
gpgme_recipient_t recipient;
270
recipient = result->recipients;
272
while(recipient != NULL){
273
fprintf(stderr, "Public key algorithm: %s\n",
274
gpgme_pubkey_algo_name(recipient->pubkey_algo));
275
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
276
fprintf(stderr, "Secret key available: %s\n",
277
recipient->status == GPG_ERR_NO_SECKEY
279
recipient = recipient->next;
252
285
/* Seek back to the beginning of the GPGME plaintext data buffer */
253
286
if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
254
287
perror("pgpme_data_seek");
340
373
/* OpenPGP credentials */
341
gnutls_certificate_allocate_credentials(&mc->cred);
342
if (ret != GNUTLS_E_SUCCESS){
343
fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
374
if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
375
!= GNUTLS_E_SUCCESS) {
376
fprintf (stderr, "GnuTLS memory error: %s\n",
345
377
safer_gnutls_strerror(ret));
346
378
gnutls_global_deinit ();
351
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
352
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
383
fprintf(stderr, "Attempting to use OpenPGP certificate %s"
384
" and keyfile %s as GnuTLS credentials\n", pubkeyfile,
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) {
361
392
"Error[%d] while reading the OpenPGP key pair ('%s',"
362
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
363
fprintf(stderr, "The GnuTLS error is: %s\n",
393
" '%s')\n", ret, pubkeyfile, seckeyfile);
394
fprintf(stdout, "The GnuTLS error is: %s\n",
364
395
safer_gnutls_strerror(ret));
782
812
{ .name = "debug", .key = 128,
783
813
.doc = "Debug mode", .group = 3 },
784
814
{ .name = "connect", .key = 'c',
785
.arg = "ADDRESS:PORT",
786
.doc = "Connect directly to a specific Mandos server",
816
.doc = "Connect directly to a sepcified mandos server",
788
818
{ .name = "interface", .key = 'i',
790
.doc = "Interface that will be used to search for Mandos"
820
.doc = "Interface that Avahi will conntect through",
793
822
{ .name = "keydir", .key = 'd',
795
.doc = "Directory to read the OpenPGP key files from",
824
.doc = "Directory where the openpgp keyring is",
797
826
{ .name = "seckey", .key = 's',
799
.doc = "OpenPGP secret key file base name",
828
.doc = "Secret openpgp key for gnutls authentication",
801
830
{ .name = "pubkey", .key = 'p',
803
.doc = "OpenPGP public key file base name",
832
.doc = "Public openpgp key for gnutls authentication",
805
834
{ .name = "dh-bits", .key = 129,
807
.doc = "Bit length of the prime number used in the"
808
" Diffie-Hellman key exchange",
836
.doc = "dh-bits to use in gnutls communication",
810
838
{ .name = "priority", .key = 130,
812
.doc = "GnuTLS priority string for the TLS handshake",
840
.doc = "GNUTLS priority", .group = 1 },
817
845
error_t parse_opt (int key, char *arg,
818
846
struct argp_state *state) {
819
847
/* Get the INPUT argument from `argp_parse', which we know is
820
848
a pointer to our plugin list pointer. */
822
case 128: /* --debug */
825
case 'c': /* --connect */
826
854
connect_to = arg;
828
case 'i': /* --interface */
831
case 'd': /* --keydir */
834
case 's': /* --seckey */
837
case 'p': /* --pubkey */
840
case 129: /* --dh-bits */
842
870
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
845
873
exit(EXIT_FAILURE);
848
case 130: /* --priority */
849
877
mc.priority = arg;
851
879
case ARGP_KEY_ARG:
852
880
argp_usage (state);
856
885
return ARGP_ERR_UNKNOWN;
861
890
struct argp argp = { .options = options, .parser = parse_opt,
863
892
.doc = "Mandos client -- Get and decrypt"
864
" passwords from a 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;
873
pubkeyfilename = combinepath(keydir, pubkeyname);
874
if (pubkeyfilename == NULL){
875
perror("combinepath");
876
exitcode = EXIT_FAILURE;
880
seckeyfilename = combinepath(keydir, seckeyname);
881
if (seckeyfilename == NULL){
882
perror("combinepath");
883
exitcode = EXIT_FAILURE;
887
ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
893
" passwords from mandos server" };
894
argp_parse (&argp, argc, argv, 0, 0, NULL);
897
pubkeyfile = combinepath(keydir, pubkeyfile);
898
if (pubkeyfile == NULL){
899
perror("combinepath");
900
exitcode = EXIT_FAILURE;
904
seckeyfile = combinepath(keydir, seckeyfile);
905
if (seckeyfile == NULL){
906
perror("combinepath");
910
ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
889
fprintf(stderr, "init_gnutls_global failed\n");
890
exitcode = EXIT_FAILURE;
912
fprintf(stderr, "init_gnutls_global\n");
893
915
gnutls_initalized = true;
896
/* If the interface is down, bring it up */
898
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
901
exitcode = EXIT_FAILURE;
904
strcpy(network.ifr_name, interface);
905
ret = ioctl(sd, SIOCGIFFLAGS, &network);
907
perror("ioctl SIOCGIFFLAGS");
908
exitcode = EXIT_FAILURE;
911
if((network.ifr_flags & IFF_UP) == 0){
912
network.ifr_flags |= IFF_UP;
913
ret = ioctl(sd, SIOCSIFFLAGS, &network);
915
perror("ioctl SIOCSIFFLAGS");
916
exitcode = EXIT_FAILURE;
926
921
ret = setuid(uid);
928
923
perror("setuid");