179
179
va_start (ap, format);
181
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
181
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
182
program_invocation_short_name));
182
183
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
262
263
rc = gpgme_data_new_from_fd(&pgp_data, fd);
263
264
if(rc != GPG_ERR_NO_ERROR){
264
265
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
265
gpgme_strsource(rc), gpgme_strerror(rc));
266
gpgme_strsource(rc), gpgme_strerror(rc));
269
270
rc = gpgme_op_import(mc.ctx, pgp_data);
270
271
if(rc != GPG_ERR_NO_ERROR){
271
272
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
272
gpgme_strsource(rc), gpgme_strerror(rc));
273
gpgme_strsource(rc), gpgme_strerror(rc));
290
291
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
291
292
if(rc != GPG_ERR_NO_ERROR){
292
293
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
293
gpgme_strsource(rc), gpgme_strerror(rc));
294
gpgme_strsource(rc), gpgme_strerror(rc));
298
299
rc = gpgme_get_engine_info(&engine_info);
299
300
if(rc != GPG_ERR_NO_ERROR){
300
301
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
301
gpgme_strsource(rc), gpgme_strerror(rc));
302
gpgme_strsource(rc), gpgme_strerror(rc));
304
305
while(engine_info != NULL){
318
320
rc = gpgme_new(&(mc.ctx));
319
321
if(rc != GPG_ERR_NO_ERROR){
320
322
fprintf_plus(stderr, "Mandos plugin mandos-client: "
321
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
323
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
353
355
if(rc != GPG_ERR_NO_ERROR){
354
356
fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
355
gpgme_strsource(rc), gpgme_strerror(rc));
357
gpgme_strsource(rc), gpgme_strerror(rc));
360
362
rc = gpgme_data_new(&dh_plain);
361
363
if(rc != GPG_ERR_NO_ERROR){
362
364
fprintf_plus(stderr, "Mandos plugin mandos-client: "
363
"bad gpgme_data_new: %s: %s\n",
364
gpgme_strsource(rc), gpgme_strerror(rc));
365
"bad gpgme_data_new: %s: %s\n",
366
gpgme_strsource(rc), gpgme_strerror(rc));
365
367
gpgme_data_release(dh_crypto);
371
373
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
372
374
if(rc != GPG_ERR_NO_ERROR){
373
375
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
374
gpgme_strsource(rc), gpgme_strerror(rc));
376
gpgme_strsource(rc), gpgme_strerror(rc));
375
377
plaintext_length = -1;
377
379
gpgme_decrypt_result_t result;
380
382
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
382
384
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
383
result->unsupported_algorithm);
385
result->unsupported_algorithm);
384
386
fprintf_plus(stderr, "Wrong key usage: %u\n",
385
result->wrong_key_usage);
387
result->wrong_key_usage);
386
388
if(result->file_name != NULL){
387
389
fprintf_plus(stderr, "File name: %s\n", result->file_name);
390
392
recipient = result->recipients;
391
393
while(recipient != NULL){
392
394
fprintf_plus(stderr, "Public key algorithm: %s\n",
393
gpgme_pubkey_algo_name(recipient->pubkey_algo));
395
gpgme_pubkey_algo_name
396
(recipient->pubkey_algo));
394
397
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
395
398
fprintf_plus(stderr, "Secret key available: %s\n",
396
recipient->status == GPG_ERR_NO_SECKEY
399
recipient->status == GPG_ERR_NO_SECKEY
398
401
recipient = recipient->next;
482
485
ret = gnutls_global_init();
483
486
if(ret != GNUTLS_E_SUCCESS){
484
fprintf_plus(stderr, "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
487
fprintf_plus(stderr, "GnuTLS global_init: %s\n",
488
safer_gnutls_strerror(ret));
496
500
/* OpenPGP credentials */
497
501
ret = gnutls_certificate_allocate_credentials(&mc.cred);
498
502
if(ret != GNUTLS_E_SUCCESS){
499
fprintf_plus(stderr, "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
503
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
504
safer_gnutls_strerror(ret));
500
505
gnutls_global_deinit();
505
510
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
506
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
511
" secret key %s as GnuTLS credentials\n",
510
516
ret = gnutls_certificate_set_openpgp_key_file
514
520
fprintf_plus(stderr,
515
521
"Error[%d] while reading the OpenPGP key pair ('%s',"
516
522
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
517
fprintf_plus(stderr, "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
523
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
524
safer_gnutls_strerror(ret));
521
528
/* GnuTLS server initialization */
522
529
ret = gnutls_dh_params_init(&mc.dh_params);
523
530
if(ret != GNUTLS_E_SUCCESS){
524
fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
525
" %s\n", safer_gnutls_strerror(ret));
531
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
532
" initialization: %s\n",
533
safer_gnutls_strerror(ret));
528
536
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
529
537
if(ret != GNUTLS_E_SUCCESS){
530
538
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
531
safer_gnutls_strerror(ret));
539
safer_gnutls_strerror(ret));
555
563
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
556
564
if(ret != GNUTLS_E_SUCCESS){
557
fprintf_plus(stderr, "Error in GnuTLS session initialization: %s\n",
558
safer_gnutls_strerror(ret));
566
"Error in GnuTLS session initialization: %s\n",
567
safer_gnutls_strerror(ret));
569
578
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
570
579
if(ret != GNUTLS_E_SUCCESS){
571
580
fprintf_plus(stderr, "Syntax error at: %s\n", err);
572
fprintf_plus(stderr, "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
581
fprintf_plus(stderr, "GnuTLS error: %s\n",
582
safer_gnutls_strerror(ret));
573
583
gnutls_deinit(*session);
585
595
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
586
596
if(ret != GNUTLS_E_SUCCESS){
587
597
fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
588
safer_gnutls_strerror(ret));
598
safer_gnutls_strerror(ret));
589
599
gnutls_deinit(*session);
650
fprintf_plus(stderr, "Setting up a TCP connection to %s, port %" PRIu16
660
fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
661
PRIu16 "\n", ip, port);
654
664
tcp_sd = socket(pf, SOCK_STREAM, 0);
693
703
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
694
704
-Wunreachable-code*/
695
705
if(if_index == AVAHI_IF_UNSPEC){
696
fprintf_plus(stderr, "An IPv6 link-local address is incomplete"
697
" without a network interface\n");
706
fprintf_plus(stderr, "An IPv6 link-local address is"
707
" incomplete without a network interface\n");
718
728
if(if_indextoname((unsigned int)if_index, interface) == NULL){
719
729
perror_plus("if_indextoname");
721
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
722
ip, interface, port);
731
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
732
"\n", ip, interface, port);
725
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n", ip, port);
735
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
727
738
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
728
739
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
998
1011
case AVAHI_RESOLVER_FAILURE:
999
fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
1000
" of type '%s' in domain '%s': %s\n", name, type, domain,
1001
avahi_strerror(avahi_server_errno(mc.server)));
1012
fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1013
"'%s' of type '%s' in domain '%s': %s\n", name, type,
1015
avahi_strerror(avahi_server_errno(mc.server)));
1004
1018
case AVAHI_RESOLVER_FOUND:
1007
1021
avahi_address_snprint(ip, sizeof(ip), address);
1009
1023
fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1010
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1011
ip, (intmax_t)interface, port);
1024
PRIdMAX ") on port %" PRIu16 "\n", name,
1025
host_name, ip, (intmax_t)interface, port);
1013
1027
int ret = start_mandos_communication(ip, port, interface,
1014
1028
avahi_proto_to_af(proto));
1047
1061
case AVAHI_BROWSER_FAILURE:
1049
1063
fprintf_plus(stderr, "(Avahi browser) %s\n",
1050
avahi_strerror(avahi_server_errno(mc.server)));
1064
avahi_strerror(avahi_server_errno(mc.server)));
1051
1065
avahi_simple_poll_quit(mc.simple_poll);
1060
1074
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1061
1075
name, type, domain, protocol, 0,
1062
1076
resolve_callback, NULL) == NULL)
1063
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s': %s\n",
1064
name, avahi_strerror(avahi_server_errno(mc.server)));
1077
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1079
avahi_strerror(avahi_server_errno(mc.server)));
1067
1082
case AVAHI_BROWSER_REMOVE:
1070
1085
case AVAHI_BROWSER_ALL_FOR_NOW:
1071
1086
case AVAHI_BROWSER_CACHE_EXHAUSTED:
1073
fprintf_plus(stderr, "No Mandos server found, still searching...\n");
1088
fprintf_plus(stderr, "No Mandos server found, still"
1115
1131
/* Reject the loopback device */
1116
1132
if(ifr->ifr_flags & IFF_LOOPBACK){
1118
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n", ifname);
1134
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1122
1139
/* Accept point-to-point devices only if connect_to is specified */
1123
1140
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1125
fprintf_plus(stderr, "Accepting point-to-point interface \"%s\"\n", ifname);
1142
fprintf_plus(stderr, "Accepting point-to-point interface"
1143
" \"%s\"\n", ifname);
1129
1147
/* Otherwise, reject non-broadcast-capable devices */
1130
1148
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1132
fprintf_plus(stderr, "Rejecting non-broadcast interface \"%s\"\n", ifname);
1150
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1151
" \"%s\"\n", ifname);
1136
1155
/* Reject non-ARP interfaces (including dummy interfaces) */
1137
1156
if(ifr->ifr_flags & IFF_NOARP){
1139
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1158
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1161
1181
struct ifreq ifr;
1162
1182
if(not get_flags(if_entry->d_name, &ifr)){
1164
fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1184
fprintf_plus(stderr, "Failed to get flags for interface "
1185
"\"%s\"\n", if_entry->d_name);
1186
1206
struct ifreq ifr;
1187
1207
if(not get_flags(if_entry->d_name, &ifr)){
1189
fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1209
fprintf_plus(stderr, "Failed to get flags for interface "
1210
"\"%s\"\n", if_entry->d_name);
1294
1314
if(mc.current_server == NULL){
1296
fprintf_plus(stderr, "Wait until first server is found. No timeout!\n");
1316
fprintf_plus(stderr, "Wait until first server is found."
1298
1319
ret = avahi_simple_poll_iterate(s, -1);
1301
fprintf_plus(stderr, "Check current_server if we should run it,"
1322
fprintf_plus(stderr, "Check current_server if we should run"
1304
1325
/* the current time */
1305
1326
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1379
bool run_network_hooks(const char *mode, const char *interface,
1381
struct dirent **direntries;
1382
struct dirent *direntry;
1384
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1387
perror_plus("scandir");
1389
int devnull = open("/dev/null", O_RDONLY);
1390
for(int i = 0; i < numhooks; i++){
1391
direntry = direntries[0];
1392
char *fullname = NULL;
1393
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1395
perror_plus("asprintf");
1398
pid_t hook_pid = fork();
1401
dup2(devnull, STDIN_FILENO);
1403
dup2(STDERR_FILENO, STDOUT_FILENO);
1404
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1406
perror_plus("setenv");
1409
ret = setenv("DEVICE", interface, 1);
1411
perror_plus("setenv");
1414
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1416
perror_plus("setenv");
1419
ret = setenv("MODE", mode, 1);
1421
perror_plus("setenv");
1425
ret = asprintf(&delaystring, "%f", delay);
1427
perror_plus("asprintf");
1430
ret = setenv("DELAY", delaystring, 1);
1433
perror_plus("setenv");
1437
ret = execl(fullname, direntry->d_name, mode, NULL);
1438
perror_plus("execl");
1441
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1442
perror_plus("waitpid");
1446
if(WIFEXITED(status)){
1447
if(WEXITSTATUS(status) != 0){
1448
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1449
" with status %d\n", direntry->d_name,
1450
WEXITSTATUS(status));
1454
} else if(WIFSIGNALED(status)){
1455
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1456
" signal %d\n", direntry->d_name,
1461
fprintf_plus(stderr, "Warning: network hook \"%s\""
1462
" crashed\n", direntry->d_name);
1357
1477
int main(int argc, char *argv[]){
1358
1478
AvahiSServiceBrowser *sb = NULL;
1517
1637
argp_state_help(state, state->out_stream,
1518
1638
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1519
1639
case 'V': /* --version */
1520
fprintf_plus(state->out_stream, "Mandos plugin mandos-client: ");
1640
fprintf_plus(state->out_stream,
1641
"Mandos plugin mandos-client: ");
1521
1642
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1522
1643
exit(argp_err_exit_status);
1611
/* Find network hooks and run them */
1734
/* Run network hooks */
1613
struct dirent **direntries;
1614
struct dirent *direntry;
1615
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1618
perror_plus("scandir");
1620
int devnull = open("/dev/null", O_RDONLY);
1621
for(int i = 0; i < numhooks; i++){
1622
direntry = direntries[0];
1623
char *fullname = NULL;
1624
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1626
perror_plus("asprintf");
1629
pid_t hook_pid = fork();
1632
dup2(devnull, STDIN_FILENO);
1634
dup2(STDERR_FILENO, STDOUT_FILENO);
1635
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1637
perror_plus("setenv");
1640
ret = setenv("DEVICE", interface, 1);
1642
perror_plus("setenv");
1645
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1647
perror_plus("setenv");
1650
ret = setenv("MODE", "start", 1);
1652
perror_plus("setenv");
1656
ret = asprintf(&delaystring, "%f", delay);
1658
perror_plus("asprintf");
1661
ret = setenv("DELAY", delaystring, 1);
1664
perror_plus("setenv");
1668
ret = execl(fullname, direntry->d_name, "start", NULL);
1669
perror_plus("execl");
1672
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1673
perror_plus("waitpid");
1677
if(WIFEXITED(status)){
1678
if(WEXITSTATUS(status) != 0){
1679
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1680
" with status %d\n", direntry->d_name,
1681
WEXITSTATUS(status));
1685
} else if(WIFSIGNALED(status)){
1686
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1687
" signal %d\n", direntry->d_name,
1692
fprintf_plus(stderr, "Warning: network hook \"%s\" crashed\n",
1736
/* Re-raise priviliges */
1740
perror_plus("seteuid");
1742
if(not run_network_hooks("start", interface, delay)){
1745
/* Lower privileges */
1749
perror_plus("seteuid");
1746
1792
srand((unsigned int) time(NULL));
1747
1793
mc.simple_poll = avahi_simple_poll_new();
1748
1794
if(mc.simple_poll == NULL){
1749
fprintf_plus(stderr, "Avahi: Failed to create simple poll object.\n");
1795
fprintf_plus(stderr,
1796
"Avahi: Failed to create simple poll object.\n");
1750
1797
exitcode = EX_UNAVAILABLE;
2105
2145
NULL, 0, browse_callback, NULL);
2106
2146
if(sb == NULL){
2107
2147
fprintf_plus(stderr, "Failed to create service browser: %s\n",
2108
avahi_strerror(avahi_server_errno(mc.server)));
2148
avahi_strerror(avahi_server_errno(mc.server)));
2109
2149
exitcode = EX_UNAVAILABLE;
2167
/* XXX run network hooks "stop" here */
2169
/* Take down the network interface */
2170
if(take_down_interface){
2171
/* Re-raise priviliges */
2207
/* Re-raise priviliges */
2173
2210
ret = seteuid(0);
2175
2212
perror_plus("seteuid");
2214
/* Run network hooks */
2215
if(not run_network_hooks("stop", interface, delay)){
2219
/* Take down the network interface */
2220
if(take_down_interface and geteuid() == 0){
2178
2221
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2180
2223
perror_plus("ioctl SIOCGIFFLAGS");