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,
50
uid_t, gid_t, open(), opendir(), DIR */
51
#include <sys/stat.h> /* open() */
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 <fcntl.h> /* open() */
56
#include <dirent.h> /* opendir(), struct dirent, readdir() */
57
#include <inttypes.h> /* PRIu16 */
58
#include <assert.h> /* assert() */
59
#include <errno.h> /* perror(), errno */
60
#include <time.h> /* time() */
51
#include <errno.h> /* perror() */
61
53
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
62
54
SIOCSIFFLAGS, if_indextoname(),
63
55
if_nametoindex(), IF_NAMESIZE */
56
#include <unistd.h> /* close(), SEEK_SET, off_t, write()*/
64
57
#include <netinet/in.h>
65
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
66
getuid(), getgid(), setuid(),
68
58
#include <arpa/inet.h> /* inet_pton(), htons */
69
#include <iso646.h> /* not, and */
70
#include <argp.h> /* struct argp_option, error_t, struct
71
argp_state, struct argp,
72
argp_parse(), ARGP_KEY_ARG,
73
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
59
#include <iso646.h> /* not */
60
#include <argp.h> /* struct argp_option,
61
struct argp_state, struct argp,
76
/* All Avahi types, constants and functions
79
#include <avahi-core/core.h>
65
#include <avahi-core/core.h> /* AvahiSimplePoll, AvahiServer,
80
67
#include <avahi-core/lookup.h>
81
#include <avahi-core/log.h>
68
#include <avahi-core/log.h> /* AvahiLogLevel */
82
69
#include <avahi-common/simple-watch.h>
83
70
#include <avahi-common/malloc.h>
84
71
#include <avahi-common/error.h>
87
#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(),
90
97
init_gnutls_session(),
92
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
93
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 */
96
#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(),
99
127
GPGME_PROTOCOL_OpenPGP,
129
GPG_ERR_NO_SECKEY, */
102
131
#define BUFFER_SIZE 256
104
#define PATHDIR "/conf/conf.d/mandos"
105
#define SECKEY "seckey.txt"
106
#define PUBKEY "pubkey.txt"
108
133
bool debug = false;
134
static const char *keydir = "/conf/conf.d/mandos";
109
135
static const char mandos_protocol_version[] = "1";
110
const char *argp_program_version = "mandos-client " VERSION;
136
const char *argp_program_version = "mandosclient 0.9";
111
137
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
113
139
/* Used for passing in values through the Avahi callback functions */
167
* Decrypt OpenPGP data using keyrings in HOMEDIR.
168
* Returns -1 on error
144
static bool init_gpgme(mandos_context *mc, const char *seckey,
145
const char *pubkey, const char *tempdir){
170
static ssize_t pgp_packet_decrypt (const char *cryptotext,
173
const char *homedir){
174
gpgme_data_t dh_crypto, dh_plain;
147
176
gpgme_error_t rc;
178
size_t plaintext_capacity = 0;
179
ssize_t plaintext_length = 0;
148
180
gpgme_engine_info_t engine_info;
152
* Helper function to insert pub and seckey to the enigne keyring.
154
bool import_key(const char *filename){
156
gpgme_data_t pgp_data;
158
fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
164
rc = gpgme_data_new_from_fd(&pgp_data, fd);
165
if (rc != GPG_ERR_NO_ERROR){
166
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
167
gpgme_strsource(rc), gpgme_strerror(rc));
171
rc = gpgme_op_import(mc->ctx, pgp_data);
172
if (rc != GPG_ERR_NO_ERROR){
173
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
174
gpgme_strsource(rc), gpgme_strerror(rc));
178
ret = TEMP_FAILURE_RETRY(close(fd));
182
gpgme_data_release(pgp_data);
187
fprintf(stderr, "Initialize gpgme\n");
183
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
193
189
if (rc != GPG_ERR_NO_ERROR){
194
190
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
195
191
gpgme_strsource(rc), gpgme_strerror(rc));
199
/* Set GPGME home directory for the OpenPGP engine only */
195
/* Set GPGME home directory for the OpenPGP engine only */
200
196
rc = gpgme_get_engine_info (&engine_info);
201
197
if (rc != GPG_ERR_NO_ERROR){
202
198
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
203
199
gpgme_strsource(rc), gpgme_strerror(rc));
206
202
while(engine_info != NULL){
207
203
if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
208
204
gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
209
engine_info->file_name, tempdir);
205
engine_info->file_name, homedir);
212
208
engine_info = engine_info->next;
214
210
if(engine_info == NULL){
215
fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
219
/* Create new GPGME "context" */
220
rc = gpgme_new(&(mc->ctx));
221
if (rc != GPG_ERR_NO_ERROR){
222
fprintf(stderr, "bad gpgme_new: %s: %s\n",
223
gpgme_strsource(rc), gpgme_strerror(rc));
227
if (not import_key(pubkey) or not import_key(seckey)){
235
* Decrypt OpenPGP data.
236
* Returns -1 on error
238
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
239
const char *cryptotext,
242
gpgme_data_t dh_crypto, dh_plain;
245
size_t plaintext_capacity = 0;
246
ssize_t plaintext_length = 0;
249
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
211
fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
252
215
/* Create new GPGME data buffer from memory cryptotext */
233
/* Create new GPGME "context" */
234
rc = gpgme_new(&ctx);
235
if (rc != GPG_ERR_NO_ERROR){
236
fprintf(stderr, "bad gpgme_new: %s: %s\n",
237
gpgme_strsource(rc), gpgme_strerror(rc));
238
plaintext_length = -1;
270
242
/* Decrypt data from the cryptotext data buffer to the plaintext
272
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
244
rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
273
245
if (rc != GPG_ERR_NO_ERROR){
274
246
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
275
247
gpgme_strsource(rc), gpgme_strerror(rc));
276
248
plaintext_length = -1;
278
gpgme_decrypt_result_t result;
279
result = gpgme_op_decrypt_result(mc->ctx);
281
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
283
fprintf(stderr, "Unsupported algorithm: %s\n",
284
result->unsupported_algorithm);
285
fprintf(stderr, "Wrong key usage: %u\n",
286
result->wrong_key_usage);
287
if(result->file_name != NULL){
288
fprintf(stderr, "File name: %s\n", result->file_name);
290
gpgme_recipient_t recipient;
291
recipient = result->recipients;
293
while(recipient != NULL){
294
fprintf(stderr, "Public key algorithm: %s\n",
295
gpgme_pubkey_algo_name(recipient->pubkey_algo));
296
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
297
fprintf(stderr, "Secret key available: %s\n",
298
recipient->status == GPG_ERR_NO_SECKEY
300
recipient = recipient->next;
305
249
goto decrypt_end;
309
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;
312
285
/* Seek back to the beginning of the GPGME plaintext data buffer */
313
286
if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
314
perror("gpgme_data_seek");
287
perror("pgpme_data_seek");
315
288
plaintext_length = -1;
316
289
goto decrypt_end;
400
373
/* OpenPGP credentials */
401
gnutls_certificate_allocate_credentials(&mc->cred);
402
if (ret != GNUTLS_E_SUCCESS){
403
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",
405
377
safer_gnutls_strerror(ret));
406
378
gnutls_global_deinit ();
411
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
412
" 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,
416
388
ret = gnutls_certificate_set_openpgp_key_file
417
(mc->cred, pubkeyfilename, seckeyfilename,
418
GNUTLS_OPENPGP_FMT_BASE64);
389
(mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
419
390
if (ret != GNUTLS_E_SUCCESS) {
421
392
"Error[%d] while reading the OpenPGP key pair ('%s',"
422
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
423
fprintf(stderr, "The GnuTLS error is: %s\n",
393
" '%s')\n", ret, pubkeyfile, seckeyfile);
394
fprintf(stdout, "The GnuTLS error is: %s\n",
424
395
safer_gnutls_strerror(ret));
820
802
char *connect_to = NULL;
821
char tempdir[] = "/tmp/mandosXXXXXX";
822
803
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
823
const char *seckey = PATHDIR "/" SECKEY;
824
const char *pubkey = PATHDIR "/" PUBKEY;
804
const char *pubkeyfile = "pubkey.txt";
805
const char *seckeyfile = "seckey.txt";
826
806
mandos_context mc = { .simple_poll = NULL, .server = NULL,
827
.dh_bits = 1024, .priority = "SECURE256"
828
":!CTYPE-X.509:+CTYPE-OPENPGP" };
807
.dh_bits = 1024, .priority = "SECURE256"};
829
808
bool gnutls_initalized = false;
830
bool gpgme_initalized = false;
833
811
struct argp_option options[] = {
834
812
{ .name = "debug", .key = 128,
835
813
.doc = "Debug mode", .group = 3 },
836
814
{ .name = "connect", .key = 'c',
837
.arg = "ADDRESS:PORT",
838
.doc = "Connect directly to a specific Mandos server",
816
.doc = "Connect directly to a sepcified mandos server",
840
818
{ .name = "interface", .key = 'i',
842
.doc = "Interface that will be used to search for Mandos"
820
.doc = "Interface that Avahi will conntect through",
822
{ .name = "keydir", .key = 'd',
824
.doc = "Directory where the openpgp keyring is",
845
826
{ .name = "seckey", .key = 's',
847
.doc = "OpenPGP secret key file base name",
828
.doc = "Secret openpgp key for gnutls authentication",
849
830
{ .name = "pubkey", .key = 'p',
851
.doc = "OpenPGP public key file base name",
832
.doc = "Public openpgp key for gnutls authentication",
853
834
{ .name = "dh-bits", .key = 129,
855
.doc = "Bit length of the prime number used in the"
856
" Diffie-Hellman key exchange",
836
.doc = "dh-bits to use in gnutls communication",
858
838
{ .name = "priority", .key = 130,
860
.doc = "GnuTLS priority string for the TLS handshake",
840
.doc = "GNUTLS priority", .group = 1 },
865
845
error_t parse_opt (int key, char *arg,
866
846
struct argp_state *state) {
867
847
/* Get the INPUT argument from `argp_parse', which we know is
868
848
a pointer to our plugin list pointer. */
870
case 128: /* --debug */
873
case 'c': /* --connect */
874
854
connect_to = arg;
876
case 'i': /* --interface */
879
case 's': /* --seckey */
882
case 'p': /* --pubkey */
885
case 129: /* --dh-bits */
887
870
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
890
873
exit(EXIT_FAILURE);
893
case 130: /* --priority */
894
877
mc.priority = arg;
896
879
case ARGP_KEY_ARG:
897
880
argp_usage (state);
901
885
return ARGP_ERR_UNKNOWN;
906
890
struct argp argp = { .options = options, .parser = parse_opt,
908
892
.doc = "Mandos client -- Get and decrypt"
909
" passwords from a Mandos server" };
910
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
911
if (ret == ARGP_ERR_UNKNOWN){
912
fprintf(stderr, "Unknown error while parsing arguments\n");
913
exitcode = EXIT_FAILURE;
918
/* If the interface is down, bring it up */
920
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
923
exitcode = EXIT_FAILURE;
926
strcpy(network.ifr_name, interface);
927
ret = ioctl(sd, SIOCGIFFLAGS, &network);
929
perror("ioctl SIOCGIFFLAGS");
930
exitcode = EXIT_FAILURE;
933
if((network.ifr_flags & IFF_UP) == 0){
934
network.ifr_flags |= IFF_UP;
935
ret = ioctl(sd, SIOCSIFFLAGS, &network);
937
perror("ioctl SIOCSIFFLAGS");
938
exitcode = EXIT_FAILURE;
942
ret = TEMP_FAILURE_RETRY(close(sd));
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);
912
fprintf(stderr, "init_gnutls_global\n");
915
gnutls_initalized = true;
951
921
ret = setuid(uid);
953
923
perror("setuid");