48
48
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
49
49
sockaddr_in6, PF_INET6,
50
50
SOCK_STREAM, INET6_ADDRSTRLEN,
51
uid_t, gid_t, open(), opendir(), DIR */
51
uid_t, gid_t, open(), opendir(),
52
53
#include <sys/stat.h> /* open() */
53
54
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
54
55
struct in6_addr, inet_pton(),
56
57
#include <fcntl.h> /* open() */
57
#include <dirent.h> /* opendir(), struct dirent, readdir() */
58
#include <inttypes.h> /* PRIu16, SCNu16 */
58
#include <dirent.h> /* opendir(), struct dirent, readdir()
60
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
59
61
#include <assert.h> /* assert() */
60
62
#include <errno.h> /* perror(), errno */
61
#include <time.h> /* time() */
63
#include <time.h> /* time(), nanosleep() */
62
64
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
63
65
SIOCSIFFLAGS, if_indextoname(),
64
66
if_nametoindex(), IF_NAMESIZE */
72
74
argp_state, struct argp,
73
75
argp_parse(), ARGP_KEY_ARG,
74
76
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
77
#include <sys/klog.h> /* klogctl() */
77
80
/* All Avahi types, constants and functions
91
94
init_gnutls_session(),
93
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
96
#include <gnutls/openpgp.h>
97
/* gnutls_certificate_set_openpgp_key_file(),
94
98
GNUTLS_OPENPGP_FMT_BASE64 */
130
134
size_t adjustbuffer(char **buffer, size_t buffer_length,
131
135
size_t buffer_capacity){
132
if (buffer_length + BUFFER_SIZE > buffer_capacity){
136
if(buffer_length + BUFFER_SIZE > buffer_capacity){
133
137
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
137
141
buffer_capacity += BUFFER_SIZE;
165
169
rc = gpgme_data_new_from_fd(&pgp_data, fd);
166
if (rc != GPG_ERR_NO_ERROR){
170
if(rc != GPG_ERR_NO_ERROR){
167
171
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
168
172
gpgme_strsource(rc), gpgme_strerror(rc));
172
176
rc = gpgme_op_import(mc->ctx, pgp_data);
173
if (rc != GPG_ERR_NO_ERROR){
177
if(rc != GPG_ERR_NO_ERROR){
174
178
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
175
179
gpgme_strsource(rc), gpgme_strerror(rc));
188
192
fprintf(stderr, "Initialize gpgme\n");
192
196
gpgme_check_version(NULL);
193
197
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
194
if (rc != GPG_ERR_NO_ERROR){
198
if(rc != GPG_ERR_NO_ERROR){
195
199
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
196
200
gpgme_strsource(rc), gpgme_strerror(rc));
200
204
/* Set GPGME home directory for the OpenPGP engine only */
201
rc = gpgme_get_engine_info (&engine_info);
202
if (rc != GPG_ERR_NO_ERROR){
205
rc = gpgme_get_engine_info(&engine_info);
206
if(rc != GPG_ERR_NO_ERROR){
203
207
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
204
208
gpgme_strsource(rc), gpgme_strerror(rc));
220
224
/* Create new GPGME "context" */
221
225
rc = gpgme_new(&(mc->ctx));
222
if (rc != GPG_ERR_NO_ERROR){
226
if(rc != GPG_ERR_NO_ERROR){
223
227
fprintf(stderr, "bad gpgme_new: %s: %s\n",
224
228
gpgme_strsource(rc), gpgme_strerror(rc));
228
if (not import_key(pubkey) or not import_key(seckey)){
232
if(not import_key(pubkey) or not import_key(seckey)){
236
240
* Decrypt OpenPGP data.
237
241
* Returns -1 on error
239
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
240
const char *cryptotext,
243
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
244
const char *cryptotext,
243
247
gpgme_data_t dh_crypto, dh_plain;
244
248
gpgme_error_t rc;
246
250
size_t plaintext_capacity = 0;
247
251
ssize_t plaintext_length = 0;
250
254
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
253
257
/* Create new GPGME data buffer from memory cryptotext */
254
258
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
256
if (rc != GPG_ERR_NO_ERROR){
260
if(rc != GPG_ERR_NO_ERROR){
257
261
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
258
262
gpgme_strsource(rc), gpgme_strerror(rc));
262
266
/* Create new empty GPGME data buffer for the plaintext */
263
267
rc = gpgme_data_new(&dh_plain);
264
if (rc != GPG_ERR_NO_ERROR){
268
if(rc != GPG_ERR_NO_ERROR){
265
269
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
266
270
gpgme_strsource(rc), gpgme_strerror(rc));
267
271
gpgme_data_release(dh_crypto);
271
275
/* Decrypt data from the cryptotext data buffer to the plaintext
273
277
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
274
if (rc != GPG_ERR_NO_ERROR){
278
if(rc != GPG_ERR_NO_ERROR){
275
279
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
276
280
gpgme_strsource(rc), gpgme_strerror(rc));
277
281
plaintext_length = -1;
279
283
gpgme_decrypt_result_t result;
280
284
result = gpgme_op_decrypt_result(mc->ctx);
282
286
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
284
288
fprintf(stderr, "Unsupported algorithm: %s\n",
313
317
/* Seek back to the beginning of the GPGME plaintext data buffer */
314
if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
318
if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
315
319
perror("gpgme_data_seek");
316
320
plaintext_length = -1;
317
321
goto decrypt_end;
322
326
plaintext_capacity = adjustbuffer(plaintext,
323
327
(size_t)plaintext_length,
324
328
plaintext_capacity);
325
if (plaintext_capacity == 0){
329
if(plaintext_capacity == 0){
326
330
perror("adjustbuffer");
327
331
plaintext_length = -1;
328
332
goto decrypt_end;
361
365
return plaintext_length;
364
static const char * safer_gnutls_strerror (int value) {
365
const char *ret = gnutls_strerror (value); /* Spurious warning */
368
static const char * safer_gnutls_strerror(int value) {
369
const char *ret = gnutls_strerror(value); /* Spurious warning from
370
-Wunreachable-code */
367
372
ret = "(unknown)";
386
391
ret = gnutls_global_init();
387
if (ret != GNUTLS_E_SUCCESS) {
388
fprintf (stderr, "GnuTLS global_init: %s\n",
389
safer_gnutls_strerror(ret));
392
if(ret != GNUTLS_E_SUCCESS) {
393
fprintf(stderr, "GnuTLS global_init: %s\n",
394
safer_gnutls_strerror(ret));
394
399
/* "Use a log level over 10 to enable all debugging options."
395
400
* - GnuTLS manual
401
406
/* OpenPGP credentials */
402
407
gnutls_certificate_allocate_credentials(&mc->cred);
403
if (ret != GNUTLS_E_SUCCESS){
404
fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
406
safer_gnutls_strerror(ret));
407
gnutls_global_deinit ();
408
if(ret != GNUTLS_E_SUCCESS){
409
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
413
safer_gnutls_strerror(ret));
414
gnutls_global_deinit();
417
424
ret = gnutls_certificate_set_openpgp_key_file
418
425
(mc->cred, pubkeyfilename, seckeyfilename,
419
426
GNUTLS_OPENPGP_FMT_BASE64);
420
if (ret != GNUTLS_E_SUCCESS) {
427
if(ret != GNUTLS_E_SUCCESS) {
422
429
"Error[%d] while reading the OpenPGP key pair ('%s',"
423
430
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
429
436
/* GnuTLS server initialization */
430
437
ret = gnutls_dh_params_init(&mc->dh_params);
431
if (ret != GNUTLS_E_SUCCESS) {
432
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
433
" %s\n", safer_gnutls_strerror(ret));
438
if(ret != GNUTLS_E_SUCCESS) {
439
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
440
" %s\n", safer_gnutls_strerror(ret));
436
443
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
437
if (ret != GNUTLS_E_SUCCESS) {
438
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
439
safer_gnutls_strerror(ret));
444
if(ret != GNUTLS_E_SUCCESS) {
445
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
446
safer_gnutls_strerror(ret));
458
465
/* GnuTLS session creation */
459
466
ret = gnutls_init(session, GNUTLS_SERVER);
460
if (ret != GNUTLS_E_SUCCESS){
467
if(ret != GNUTLS_E_SUCCESS){
461
468
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
462
469
safer_gnutls_strerror(ret));
467
474
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
468
if (ret != GNUTLS_E_SUCCESS) {
475
if(ret != GNUTLS_E_SUCCESS) {
469
476
fprintf(stderr, "Syntax error at: %s\n", err);
470
477
fprintf(stderr, "GnuTLS error: %s\n",
471
478
safer_gnutls_strerror(ret));
472
gnutls_deinit (*session);
479
gnutls_deinit(*session);
477
484
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
479
if (ret != GNUTLS_E_SUCCESS) {
486
if(ret != GNUTLS_E_SUCCESS) {
480
487
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
481
488
safer_gnutls_strerror(ret));
482
gnutls_deinit (*session);
489
gnutls_deinit(*session);
486
493
/* ignore client certificate if any. */
487
gnutls_certificate_server_set_request (*session,
494
gnutls_certificate_server_set_request(*session,
490
gnutls_dh_set_prime_bits (*session, mc->dh_bits);
497
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
513
520
char interface[IF_NAMESIZE];
514
521
gnutls_session_t session;
516
ret = init_gnutls_session (mc, &session);
523
ret = init_gnutls_session(mc, &session);
550
557
fprintf(stderr, "Bad address: %s\n", ip);
553
to.in6.sin6_port = htons(port); /* Spurious warning */
560
to.in6.sin6_port = htons(port); /* Spurious warnings from
562
-Wunreachable-code */
555
564
to.in6.sin6_scope_id = (uint32_t)if_index;
571
580
ret = connect(tcp_sd, &to.in, sizeof(to));
573
582
perror("connect");
577
586
const char *out = mandos_protocol_version;
580
589
size_t out_size = strlen(out);
581
590
ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
582
591
out_size - written));
602
611
fprintf(stderr, "Establishing TLS session with %s\n", ip);
605
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
614
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
608
ret = gnutls_handshake (session);
617
ret = gnutls_handshake(session);
609
618
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
611
if (ret != GNUTLS_E_SUCCESS){
620
if(ret != GNUTLS_E_SUCCESS){
613
622
fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
636
645
sret = gnutls_record_recv(session, buffer+buffer_length,
643
652
case GNUTLS_E_INTERRUPTED:
644
653
case GNUTLS_E_AGAIN:
646
655
case GNUTLS_E_REHANDSHAKE:
648
ret = gnutls_handshake (session);
657
ret = gnutls_handshake(session);
649
658
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
651
660
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
658
667
fprintf(stderr, "Unknown error while reading data from"
659
668
" encrypted session with Mandos server\n");
661
gnutls_bye (session, GNUTLS_SHUT_RDWR);
670
gnutls_bye(session, GNUTLS_SHUT_RDWR);
670
679
fprintf(stderr, "Closing TLS session\n");
673
gnutls_bye (session, GNUTLS_SHUT_RDWR);
682
gnutls_bye(session, GNUTLS_SHUT_RDWR);
675
if (buffer_length > 0){
684
if(buffer_length > 0){
676
685
decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
678
687
&decrypted_buffer);
679
if (decrypted_buffer_size >= 0){
688
if(decrypted_buffer_size >= 0){
681
690
while(written < (size_t) decrypted_buffer_size){
682
ret = (int)fwrite (decrypted_buffer + written, 1,
683
(size_t)decrypted_buffer_size - written,
691
ret = (int)fwrite(decrypted_buffer + written, 1,
692
(size_t)decrypted_buffer_size - written,
685
694
if(ret == 0 and ferror(stdout)){
687
696
fprintf(stderr, "Error writing encrypted data: %s\n",
746
755
avahi_address_snprint(ip, sizeof(ip), address);
748
757
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
749
PRIu16 ") on port %d\n", name, host_name, ip,
758
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
759
ip, (intmax_t)interface, port);
752
761
int ret = start_mandos_communication(ip, port, interface, mc);
754
763
avahi_simple_poll_quit(mc->simple_poll);
789
798
the callback function is called the Avahi server will free the
790
799
resolver for us. */
792
if (!(avahi_s_service_resolver_new(mc->server, interface,
801
if(!(avahi_s_service_resolver_new(mc->server, interface,
793
802
protocol, name, type, domain,
794
803
AVAHI_PROTO_INET6, 0,
795
804
resolve_callback, mc)))
862
874
.doc = "GnuTLS priority string for the TLS handshake",
876
{ .name = "delay", .key = 131,
878
.doc = "Maximum delay to wait for interface startup",
867
error_t parse_opt (int key, char *arg,
868
struct argp_state *state) {
883
error_t parse_opt(int key, char *arg,
884
struct argp_state *state) {
870
886
case 128: /* --debug */
885
901
case 129: /* --dh-bits */
886
ret = sscanf(arg, "%u", &mc.dh_bits);
887
if(ret == 0 or mc.dh_bits == 0){
902
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
903
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
904
or arg[numchars] != '\0'){
888
905
fprintf(stderr, "Bad number of DH bits\n");
889
906
exit(EXIT_FAILURE);
908
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
892
910
case 130: /* --priority */
893
911
mc.priority = arg;
913
case 131: /* --delay */
914
ret = sscanf(arg, "%lf%n", &delay, &numchars);
915
if(ret < 1 or arg[numchars] != '\0'){
916
fprintf(stderr, "Bad delay\n");
895
920
case ARGP_KEY_ARG:
897
922
case ARGP_KEY_END:
907
932
.doc = "Mandos client -- Get and decrypt"
908
933
" passwords from a Mandos server" };
909
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
910
if (ret == ARGP_ERR_UNKNOWN){
934
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
935
if(ret == ARGP_ERR_UNKNOWN){
911
936
fprintf(stderr, "Unknown error while parsing arguments\n");
912
937
exitcode = EXIT_FAILURE;
917
942
/* If the interface is down, bring it up */
944
// Lower kernel loglevel to KERN_NOTICE to avoid
945
// KERN_INFO messages to mess up the prompt
946
ret = klogctl(8, NULL, 5);
919
951
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
921
953
perror("socket");
922
954
exitcode = EXIT_FAILURE;
955
ret = klogctl(7, NULL, 0);
925
961
strcpy(network.ifr_name, interface);
926
962
ret = ioctl(sd, SIOCGIFFLAGS, &network);
928
964
perror("ioctl SIOCGIFFLAGS");
965
ret = klogctl(7, NULL, 0);
929
969
exitcode = EXIT_FAILURE;
936
976
perror("ioctl SIOCSIFFLAGS");
937
977
exitcode = EXIT_FAILURE;
978
ret = klogctl(7, NULL, 0);
985
// sleep checking until interface is running
986
for(int i=0; i < delay * 4; i++){
987
ret = ioctl(sd, SIOCGIFFLAGS, &network);
989
perror("ioctl SIOCGIFFLAGS");
990
} else if(network.ifr_flags & IFF_RUNNING){
993
struct timespec sleeptime = { .tv_nsec = 250000000 };
994
nanosleep(&sleeptime, NULL);
941
996
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1000
// Restores kernel loglevel to default
1001
ret = klogctl(7, NULL, 0);
950
1010
ret = setuid(uid);
952
1012
perror("setuid");
957
1017
perror("setgid");
960
1020
ret = init_gnutls_global(&mc, pubkey, seckey);
962
1022
fprintf(stderr, "init_gnutls_global failed\n");
963
1023
exitcode = EXIT_FAILURE;
999
ret = sscanf(address+1, "%" SCNu16, &port);
1000
if(ret == 0 or port == 0){
1059
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1060
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1061
or address[numchars+1] != '\0'){
1001
1062
fprintf(stderr, "Bad port number\n");
1002
1063
exitcode = EXIT_FAILURE;
1066
port = (uint16_t)tmpmax;
1005
1067
*address = '\0';
1006
1068
address = connect_to;
1007
1069
ret = start_mandos_communication(address, port, if_index, &mc);
1023
1085
/* Allocate main Avahi loop object */
1024
1086
mc.simple_poll = avahi_simple_poll_new();
1025
if (mc.simple_poll == NULL) {
1087
if(mc.simple_poll == NULL) {
1026
1088
fprintf(stderr, "Avahi: Failed to create simple poll"
1028
1090
exitcode = EXIT_FAILURE;
1070
1132
/* Run the main loop */
1073
1135
fprintf(stderr, "Starting Avahi loop search\n");
1076
1138
avahi_simple_poll_loop(mc.simple_poll);
1081
1143
fprintf(stderr, "%s exiting\n", argv[0]);
1084
1146
/* Cleanup things */
1086
1148
avahi_s_service_browser_free(sb);
1088
if (mc.server != NULL)
1150
if(mc.server != NULL)
1089
1151
avahi_server_free(mc.server);
1091
if (mc.simple_poll != NULL)
1153
if(mc.simple_poll != NULL)
1092
1154
avahi_simple_poll_free(mc.simple_poll);
1094
if (gnutls_initalized){
1156
if(gnutls_initalized){
1095
1157
gnutls_certificate_free_credentials(mc.cred);
1096
gnutls_global_deinit ();
1158
gnutls_global_deinit();
1097
1159
gnutls_dh_params_deinit(mc.dh_params);
1116
1178
if(direntry == NULL){
1119
if (direntry->d_type == DT_REG){
1181
if(direntry->d_type == DT_REG){
1120
1182
char *fullname = NULL;
1121
1183
ret = asprintf(&fullname, "%s/%s", tempdir,
1122
1184
direntry->d_name);