262
262
rc = gpgme_data_new_from_fd(&pgp_data, fd);
263
263
if(rc != GPG_ERR_NO_ERROR){
264
fprintf(stderr, "Mandos plugin mandos-client: "
265
"bad gpgme_data_new_from_fd: %s: %s\n",
264
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
266
265
gpgme_strsource(rc), gpgme_strerror(rc));
270
269
rc = gpgme_op_import(mc.ctx, pgp_data);
271
270
if(rc != GPG_ERR_NO_ERROR){
272
fprintf(stderr, "Mandos plugin mandos-client: "
273
"bad gpgme_op_import: %s: %s\n",
271
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
274
272
gpgme_strsource(rc), gpgme_strerror(rc));
287
fprintf(stderr, "Mandos plugin mandos-client: "
288
"Initializing GPGME\n");
285
fprintf_plus(stderr, "Initializing GPGME\n");
292
289
gpgme_check_version(NULL);
293
290
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
294
291
if(rc != GPG_ERR_NO_ERROR){
295
fprintf(stderr, "Mandos plugin mandos-client: "
296
"bad gpgme_engine_check_version: %s: %s\n",
292
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
297
293
gpgme_strsource(rc), gpgme_strerror(rc));
301
297
/* Set GPGME home directory for the OpenPGP engine only */
302
298
rc = gpgme_get_engine_info(&engine_info);
303
299
if(rc != GPG_ERR_NO_ERROR){
304
fprintf(stderr, "Mandos plugin mandos-client: "
305
"bad gpgme_get_engine_info: %s: %s\n",
300
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
306
301
gpgme_strsource(rc), gpgme_strerror(rc));
315
310
engine_info = engine_info->next;
317
312
if(engine_info == NULL){
318
fprintf(stderr, "Mandos plugin mandos-client: "
319
"Could not set GPGME home dir to %s\n", tempdir);
313
fprintf_plus(stderr, "Could not set GPGME home dir to %s\n", tempdir);
323
317
/* Create new GPGME "context" */
324
318
rc = gpgme_new(&(mc.ctx));
325
319
if(rc != GPG_ERR_NO_ERROR){
326
fprintf(stderr, "Mandos plugin mandos-client: "
320
fprintf_plus(stderr, "Mandos plugin mandos-client: "
327
321
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
328
322
gpgme_strerror(rc));
350
344
ssize_t plaintext_length = 0;
353
fprintf(stderr, "Mandos plugin mandos-client: "
354
"Trying to decrypt OpenPGP data\n");
347
fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
357
350
/* Create new GPGME data buffer from memory cryptotext */
358
351
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
360
353
if(rc != GPG_ERR_NO_ERROR){
361
fprintf(stderr, "Mandos plugin mandos-client: "
362
"bad gpgme_data_new_from_mem: %s: %s\n",
354
fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
363
355
gpgme_strsource(rc), gpgme_strerror(rc));
367
359
/* Create new empty GPGME data buffer for the plaintext */
368
360
rc = gpgme_data_new(&dh_plain);
369
361
if(rc != GPG_ERR_NO_ERROR){
370
fprintf(stderr, "Mandos plugin mandos-client: "
362
fprintf_plus(stderr, "Mandos plugin mandos-client: "
371
363
"bad gpgme_data_new: %s: %s\n",
372
364
gpgme_strsource(rc), gpgme_strerror(rc));
373
365
gpgme_data_release(dh_crypto);
379
371
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
380
372
if(rc != GPG_ERR_NO_ERROR){
381
fprintf(stderr, "Mandos plugin mandos-client: "
382
"bad gpgme_op_decrypt: %s: %s\n",
373
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
383
374
gpgme_strsource(rc), gpgme_strerror(rc));
384
375
plaintext_length = -1;
386
377
gpgme_decrypt_result_t result;
387
378
result = gpgme_op_decrypt_result(mc.ctx);
388
379
if(result == NULL){
389
fprintf(stderr, "Mandos plugin mandos-client: "
390
"gpgme_op_decrypt_result failed\n");
380
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
392
fprintf(stderr, "Mandos plugin mandos-client: "
393
"Unsupported algorithm: %s\n",
382
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
394
383
result->unsupported_algorithm);
395
fprintf(stderr, "Mandos plugin mandos-client: "
396
"Wrong key usage: %u\n",
384
fprintf_plus(stderr, "Wrong key usage: %u\n",
397
385
result->wrong_key_usage);
398
386
if(result->file_name != NULL){
399
fprintf(stderr, "Mandos plugin mandos-client: "
400
"File name: %s\n", result->file_name);
387
fprintf_plus(stderr, "File name: %s\n", result->file_name);
402
389
gpgme_recipient_t recipient;
403
390
recipient = result->recipients;
404
391
while(recipient != NULL){
405
fprintf(stderr, "Mandos plugin mandos-client: "
406
"Public key algorithm: %s\n",
392
fprintf_plus(stderr, "Public key algorithm: %s\n",
407
393
gpgme_pubkey_algo_name(recipient->pubkey_algo));
408
fprintf(stderr, "Mandos plugin mandos-client: "
409
"Key ID: %s\n", recipient->keyid);
410
fprintf(stderr, "Mandos plugin mandos-client: "
411
"Secret key available: %s\n",
394
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
395
fprintf_plus(stderr, "Secret key available: %s\n",
412
396
recipient->status == GPG_ERR_NO_SECKEY
414
398
recipient = recipient->next;
486
468
/* GnuTLS log function callback */
487
469
static void debuggnutls(__attribute__((unused)) int level,
488
470
const char* string){
489
fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
471
fprintf_plus(stderr, "GnuTLS: %s", string);
492
474
static int init_gnutls_global(const char *pubkeyfilename,
497
fprintf(stderr, "Mandos plugin mandos-client: "
498
"Initializing GnuTLS\n");
479
fprintf_plus(stderr, "Initializing GnuTLS\n");
501
482
ret = gnutls_global_init();
502
483
if(ret != GNUTLS_E_SUCCESS){
503
fprintf(stderr, "Mandos plugin mandos-client: "
504
"GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
484
fprintf_plus(stderr, "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
516
496
/* OpenPGP credentials */
517
497
ret = gnutls_certificate_allocate_credentials(&mc.cred);
518
498
if(ret != GNUTLS_E_SUCCESS){
519
fprintf(stderr, "Mandos plugin mandos-client: "
520
"GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
499
fprintf_plus(stderr, "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
521
500
gnutls_global_deinit();
526
fprintf(stderr, "Mandos plugin mandos-client: "
527
"Attempting to use OpenPGP public key %s and"
505
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
528
506
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
533
511
(mc.cred, pubkeyfilename, seckeyfilename,
534
512
GNUTLS_OPENPGP_FMT_BASE64);
535
513
if(ret != GNUTLS_E_SUCCESS){
537
"Mandos plugin mandos-client: "
538
"Error[%d] while reading the OpenPGP key pair ('%s',"
539
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
540
fprintf(stderr, "Mandos plugin mandos-client: "
541
"The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
515
"Error[%d] while reading the OpenPGP key pair ('%s',"
516
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
517
fprintf_plus(stderr, "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
545
521
/* GnuTLS server initialization */
546
522
ret = gnutls_dh_params_init(&mc.dh_params);
547
523
if(ret != GNUTLS_E_SUCCESS){
548
fprintf(stderr, "Mandos plugin mandos-client: "
549
"Error in GnuTLS DH parameter initialization:"
524
fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
550
525
" %s\n", safer_gnutls_strerror(ret));
553
528
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
554
529
if(ret != GNUTLS_E_SUCCESS){
555
fprintf(stderr, "Mandos plugin mandos-client: "
556
"Error in GnuTLS prime generation: %s\n",
530
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
557
531
safer_gnutls_strerror(ret));
581
555
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
582
556
if(ret != GNUTLS_E_SUCCESS){
583
fprintf(stderr, "Mandos plugin mandos-client: "
584
"Error in GnuTLS session initialization: %s\n",
557
fprintf_plus(stderr, "Error in GnuTLS session initialization: %s\n",
585
558
safer_gnutls_strerror(ret));
596
569
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
597
570
if(ret != GNUTLS_E_SUCCESS){
598
fprintf(stderr, "Mandos plugin mandos-client: "
599
"Syntax error at: %s\n", err);
600
fprintf(stderr, "Mandos plugin mandos-client: "
601
"GnuTLS error: %s\n", safer_gnutls_strerror(ret));
571
fprintf_plus(stderr, "Syntax error at: %s\n", err);
572
fprintf_plus(stderr, "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
602
573
gnutls_deinit(*session);
614
585
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
615
586
if(ret != GNUTLS_E_SUCCESS){
616
fprintf(stderr, "Mandos plugin mandos-client: "
617
"Error setting GnuTLS credentials: %s\n",
587
fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
618
588
safer_gnutls_strerror(ret));
619
589
gnutls_deinit(*session);
726
693
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
727
694
-Wunreachable-code*/
728
695
if(if_index == AVAHI_IF_UNSPEC){
729
fprintf(stderr, "Mandos plugin mandos-client: "
730
"An IPv6 link-local address is incomplete"
696
fprintf_plus(stderr, "An IPv6 link-local address is incomplete"
731
697
" without a network interface\n");
752
718
if(if_indextoname((unsigned int)if_index, interface) == NULL){
753
719
perror_plus("if_indextoname");
755
fprintf(stderr, "Mandos plugin mandos-client: "
756
"Connection to: %s%%%s, port %" PRIu16 "\n",
721
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
757
722
ip, interface, port);
760
fprintf(stderr, "Mandos plugin mandos-client: "
761
"Connection to: %s, port %" PRIu16 "\n", ip, port);
725
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n", ip, port);
763
727
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
764
728
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
774
738
perror_plus("inet_ntop");
776
740
if(strcmp(addrstr, ip) != 0){
777
fprintf(stderr, "Mandos plugin mandos-client: "
778
"Canonical address form: %s\n", addrstr);
741
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
919
879
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
921
fprintf(stderr, "Mandos plugin mandos-client: "
922
"*** GnuTLS Re-handshake failed ***\n");
881
fprintf_plus(stderr, "*** GnuTLS Re-handshake failed ***\n");
923
882
gnutls_perror(ret);
929
fprintf(stderr, "Mandos plugin mandos-client: "
930
"Unknown error while reading data from"
888
fprintf_plus(stderr, "Unknown error while reading data from"
931
889
" encrypted session with Mandos server\n");
932
890
gnutls_bye(session, GNUTLS_SHUT_RDWR);
1042
998
case AVAHI_RESOLVER_FAILURE:
1043
fprintf(stderr, "Mandos plugin mandos-client: "
1044
"(Avahi Resolver) Failed to resolve service '%s'"
999
fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
1045
1000
" of type '%s' in domain '%s': %s\n", name, type, domain,
1046
1001
avahi_strerror(avahi_server_errno(mc.server)));
1051
1006
char ip[AVAHI_ADDRESS_STR_MAX];
1052
1007
avahi_address_snprint(ip, sizeof(ip), address);
1054
fprintf(stderr, "Mandos plugin mandos-client: "
1055
"Mandos server \"%s\" found on %s (%s, %"
1009
fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1056
1010
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1057
1011
ip, (intmax_t)interface, port);
1107
1060
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1108
1061
name, type, domain, protocol, 0,
1109
1062
resolve_callback, NULL) == NULL)
1110
fprintf(stderr, "Mandos plugin mandos-client: "
1111
"Avahi: Failed to resolve service '%s': %s\n",
1063
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s': %s\n",
1112
1064
name, avahi_strerror(avahi_server_errno(mc.server)));
1118
1070
case AVAHI_BROWSER_ALL_FOR_NOW:
1119
1071
case AVAHI_BROWSER_CACHE_EXHAUSTED:
1121
fprintf(stderr, "Mandos plugin mandos-client: "
1122
"No Mandos server found, still searching...\n");
1073
fprintf_plus(stderr, "No Mandos server found, still searching...\n");
1164
1115
/* Reject the loopback device */
1165
1116
if(ifr->ifr_flags & IFF_LOOPBACK){
1167
fprintf(stderr, "Mandos plugin mandos-client: "
1168
"Rejecting loopback interface \"%s\"\n", ifname);
1118
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n", ifname);
1172
1122
/* Accept point-to-point devices only if connect_to is specified */
1173
1123
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1175
fprintf(stderr, "Mandos plugin mandos-client: "
1176
"Accepting point-to-point interface \"%s\"\n", ifname);
1125
fprintf_plus(stderr, "Accepting point-to-point interface \"%s\"\n", ifname);
1180
1129
/* Otherwise, reject non-broadcast-capable devices */
1181
1130
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1183
fprintf(stderr, "Mandos plugin mandos-client: "
1184
"Rejecting non-broadcast interface \"%s\"\n", ifname);
1132
fprintf_plus(stderr, "Rejecting non-broadcast interface \"%s\"\n", ifname);
1188
1136
/* Reject non-ARP interfaces (including dummy interfaces) */
1189
1137
if(ifr->ifr_flags & IFF_NOARP){
1191
fprintf(stderr, "Mandos plugin mandos-client: "
1192
"Rejecting non-ARP interface \"%s\"\n", ifname);
1139
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1197
1144
/* Accept this device */
1199
fprintf(stderr, "Mandos plugin mandos-client: "
1200
"Interface \"%s\" is good\n", ifname);
1146
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1261
1204
/* Reject non-running interfaces */
1262
1205
if(not (ifr.ifr_flags & IFF_RUNNING)){
1264
fprintf(stderr, "Mandos plugin mandos-client: "
1265
"Rejecting non-running interface \"%s\"\n",
1207
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1266
1208
if_entry->d_name);
1303
1245
if((direntry->d_name)[sret] != '\0'){
1304
1246
/* Contains non-allowed characters */
1306
fprintf(stderr, "Mandos plugin mandos-client: "
1307
"Ignoring hook \"%s\" with bad name\n",
1248
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1308
1249
direntry->d_name);
1355
1294
if(mc.current_server == NULL){
1357
fprintf(stderr, "Mandos plugin mandos-client: "
1358
"Wait until first server is found. No timeout!\n");
1296
fprintf_plus(stderr, "Wait until first server is found. No timeout!\n");
1360
1298
ret = avahi_simple_poll_iterate(s, -1);
1363
fprintf(stderr, "Mandos plugin mandos-client: "
1364
"Check current_server if we should run it,"
1301
fprintf_plus(stderr, "Check current_server if we should run it,"
1367
1304
/* the current time */
1581
1517
argp_state_help(state, state->out_stream,
1582
1518
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1583
1519
case 'V': /* --version */
1584
fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1585
fprintf(state->out_stream, "%s\n", argp_program_version);
1520
fprintf_plus(state->out_stream, "Mandos plugin mandos-client: ");
1521
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1586
1522
exit(argp_err_exit_status);
1741
1677
if(WIFEXITED(status)){
1742
1678
if(WEXITSTATUS(status) != 0){
1743
fprintf(stderr, "Mandos plugin mandos-client: "
1744
"Warning: network hook \"%s\" exited"
1679
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1745
1680
" with status %d\n", direntry->d_name,
1746
1681
WEXITSTATUS(status));
1747
1682
free(fullname);
1750
1685
} else if(WIFSIGNALED(status)){
1751
fprintf(stderr, "Mandos plugin mandos-client: "
1752
"Warning: network hook \"%s\" died by"
1686
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1753
1687
" signal %d\n", direntry->d_name,
1754
1688
WTERMSIG(status));
1755
1689
free(fullname);
1758
fprintf(stderr, "Mandos plugin mandos-client: "
1759
"Warning: network hook \"%s\" crashed\n",
1692
fprintf_plus(stderr, "Warning: network hook \"%s\" crashed\n",
1760
1693
direntry->d_name);
1761
1694
free(fullname);
1790
1723
/* Pick the first interface returned */
1791
1724
interface = strdup(direntries[0]->d_name);
1793
fprintf(stderr, "Mandos plugin mandos-client: "
1794
"Using interface \"%s\"\n", interface);
1726
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1796
1728
if(interface == NULL){
1797
1729
perror_plus("malloc");
1815
1746
srand((unsigned int) time(NULL));
1816
1747
mc.simple_poll = avahi_simple_poll_new();
1817
1748
if(mc.simple_poll == NULL){
1818
fprintf(stderr, "Mandos plugin mandos-client: "
1819
"Avahi: Failed to create simple poll object.\n");
1749
fprintf_plus(stderr, "Avahi: Failed to create simple poll object.\n");
1820
1750
exitcode = EX_UNAVAILABLE;
1888
1818
if(strcmp(interface, "none") != 0){
1889
1819
if_index = (AvahiIfIndex) if_nametoindex(interface);
1890
1820
if(if_index == 0){
1891
fprintf(stderr, "Mandos plugin mandos-client: "
1892
"No such interface: \"%s\"\n", interface);
1821
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1893
1822
exitcode = EX_UNAVAILABLE;
2166
2090
/* Check if creating the Avahi server object succeeded */
2167
2091
if(mc.server == NULL){
2168
fprintf(stderr, "Mandos plugin mandos-client: "
2169
"Failed to create Avahi server: %s\n",
2092
fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2170
2093
avahi_strerror(error));
2171
2094
exitcode = EX_UNAVAILABLE;
2181
2104
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2182
2105
NULL, 0, browse_callback, NULL);
2183
2106
if(sb == NULL){
2184
fprintf(stderr, "Mandos plugin mandos-client: "
2185
"Failed to create service browser: %s\n",
2107
fprintf_plus(stderr, "Failed to create service browser: %s\n",
2186
2108
avahi_strerror(avahi_server_errno(mc.server)));
2187
2109
exitcode = EX_UNAVAILABLE;
2195
2117
/* Run the main loop */
2198
fprintf(stderr, "Mandos plugin mandos-client: "
2199
"Starting Avahi loop search\n");
2120
fprintf_plus(stderr, "Starting Avahi loop search\n");
2202
2123
ret = avahi_loop_with_timeout(mc.simple_poll,
2203
2124
(int)(retry_interval * 1000));
2205
fprintf(stderr, "Mandos plugin mandos-client: "
2206
"avahi_loop_with_timeout exited %s\n",
2126
fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2207
2127
(ret == 0) ? "successfully" : "with error");
2213
fprintf(stderr, "Mandos plugin mandos-client: "
2214
"%s exiting\n", argv[0]);
2133
fprintf_plus(stderr, "%s exiting\n", argv[0]);
2217
2136
/* Cleanup things */