179
179
va_start (ap, format);
181
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
182
program_invocation_short_name));
181
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
183
182
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
263
262
rc = gpgme_data_new_from_fd(&pgp_data, fd);
264
263
if(rc != GPG_ERR_NO_ERROR){
265
264
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
266
gpgme_strsource(rc), gpgme_strerror(rc));
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
271
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
273
gpgme_strsource(rc), gpgme_strerror(rc));
272
gpgme_strsource(rc), gpgme_strerror(rc));
291
290
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
292
291
if(rc != GPG_ERR_NO_ERROR){
293
292
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
294
gpgme_strsource(rc), gpgme_strerror(rc));
293
gpgme_strsource(rc), gpgme_strerror(rc));
299
298
rc = gpgme_get_engine_info(&engine_info);
300
299
if(rc != GPG_ERR_NO_ERROR){
301
300
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
302
gpgme_strsource(rc), gpgme_strerror(rc));
301
gpgme_strsource(rc), gpgme_strerror(rc));
305
304
while(engine_info != NULL){
320
318
rc = gpgme_new(&(mc.ctx));
321
319
if(rc != GPG_ERR_NO_ERROR){
322
320
fprintf_plus(stderr, "Mandos plugin mandos-client: "
323
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
321
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
355
353
if(rc != GPG_ERR_NO_ERROR){
356
354
fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
357
gpgme_strsource(rc), gpgme_strerror(rc));
355
gpgme_strsource(rc), gpgme_strerror(rc));
362
360
rc = gpgme_data_new(&dh_plain);
363
361
if(rc != GPG_ERR_NO_ERROR){
364
362
fprintf_plus(stderr, "Mandos plugin mandos-client: "
365
"bad gpgme_data_new: %s: %s\n",
366
gpgme_strsource(rc), gpgme_strerror(rc));
363
"bad gpgme_data_new: %s: %s\n",
364
gpgme_strsource(rc), gpgme_strerror(rc));
367
365
gpgme_data_release(dh_crypto);
373
371
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
374
372
if(rc != GPG_ERR_NO_ERROR){
375
373
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
376
gpgme_strsource(rc), gpgme_strerror(rc));
374
gpgme_strsource(rc), gpgme_strerror(rc));
377
375
plaintext_length = -1;
379
377
gpgme_decrypt_result_t result;
382
380
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
384
382
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
385
result->unsupported_algorithm);
383
result->unsupported_algorithm);
386
384
fprintf_plus(stderr, "Wrong key usage: %u\n",
387
result->wrong_key_usage);
385
result->wrong_key_usage);
388
386
if(result->file_name != NULL){
389
387
fprintf_plus(stderr, "File name: %s\n", result->file_name);
392
390
recipient = result->recipients;
393
391
while(recipient != NULL){
394
392
fprintf_plus(stderr, "Public key algorithm: %s\n",
395
gpgme_pubkey_algo_name
396
(recipient->pubkey_algo));
393
gpgme_pubkey_algo_name(recipient->pubkey_algo));
397
394
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
398
395
fprintf_plus(stderr, "Secret key available: %s\n",
399
recipient->status == GPG_ERR_NO_SECKEY
396
recipient->status == GPG_ERR_NO_SECKEY
401
398
recipient = recipient->next;
485
482
ret = gnutls_global_init();
486
483
if(ret != GNUTLS_E_SUCCESS){
487
fprintf_plus(stderr, "GnuTLS global_init: %s\n",
488
safer_gnutls_strerror(ret));
484
fprintf_plus(stderr, "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
500
496
/* OpenPGP credentials */
501
497
ret = gnutls_certificate_allocate_credentials(&mc.cred);
502
498
if(ret != GNUTLS_E_SUCCESS){
503
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
504
safer_gnutls_strerror(ret));
499
fprintf_plus(stderr, "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
505
500
gnutls_global_deinit();
510
505
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
511
" secret key %s as GnuTLS credentials\n",
506
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
516
510
ret = gnutls_certificate_set_openpgp_key_file
520
514
fprintf_plus(stderr,
521
515
"Error[%d] while reading the OpenPGP key pair ('%s',"
522
516
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
523
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
524
safer_gnutls_strerror(ret));
517
fprintf_plus(stderr, "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
528
521
/* GnuTLS server initialization */
529
522
ret = gnutls_dh_params_init(&mc.dh_params);
530
523
if(ret != GNUTLS_E_SUCCESS){
531
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
532
" initialization: %s\n",
533
safer_gnutls_strerror(ret));
524
fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
525
" %s\n", safer_gnutls_strerror(ret));
536
528
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
537
529
if(ret != GNUTLS_E_SUCCESS){
538
530
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
539
safer_gnutls_strerror(ret));
531
safer_gnutls_strerror(ret));
563
555
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
564
556
if(ret != GNUTLS_E_SUCCESS){
566
"Error in GnuTLS session initialization: %s\n",
567
safer_gnutls_strerror(ret));
557
fprintf_plus(stderr, "Error in GnuTLS session initialization: %s\n",
558
safer_gnutls_strerror(ret));
578
569
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
579
570
if(ret != GNUTLS_E_SUCCESS){
580
571
fprintf_plus(stderr, "Syntax error at: %s\n", err);
581
fprintf_plus(stderr, "GnuTLS error: %s\n",
582
safer_gnutls_strerror(ret));
572
fprintf_plus(stderr, "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
583
573
gnutls_deinit(*session);
595
585
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
596
586
if(ret != GNUTLS_E_SUCCESS){
597
587
fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
598
safer_gnutls_strerror(ret));
588
safer_gnutls_strerror(ret));
599
589
gnutls_deinit(*session);
660
fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
661
PRIu16 "\n", ip, port);
650
fprintf_plus(stderr, "Setting up a TCP connection to %s, port %" PRIu16
664
654
tcp_sd = socket(pf, SOCK_STREAM, 0);
703
693
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
704
694
-Wunreachable-code*/
705
695
if(if_index == AVAHI_IF_UNSPEC){
706
fprintf_plus(stderr, "An IPv6 link-local address is"
707
" incomplete without a network interface\n");
696
fprintf_plus(stderr, "An IPv6 link-local address is incomplete"
697
" without a network interface\n");
728
718
if(if_indextoname((unsigned int)if_index, interface) == NULL){
729
719
perror_plus("if_indextoname");
731
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
732
"\n", ip, interface, port);
721
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
722
ip, interface, port);
735
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
725
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n", ip, port);
738
727
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
739
728
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1011
998
case AVAHI_RESOLVER_FAILURE:
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)));
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)));
1018
1004
case AVAHI_RESOLVER_FOUND:
1021
1007
avahi_address_snprint(ip, sizeof(ip), address);
1023
1009
fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1024
PRIdMAX ") on port %" PRIu16 "\n", name,
1025
host_name, ip, (intmax_t)interface, port);
1010
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1011
ip, (intmax_t)interface, port);
1027
1013
int ret = start_mandos_communication(ip, port, interface,
1028
1014
avahi_proto_to_af(proto));
1061
1047
case AVAHI_BROWSER_FAILURE:
1063
1049
fprintf_plus(stderr, "(Avahi browser) %s\n",
1064
avahi_strerror(avahi_server_errno(mc.server)));
1050
avahi_strerror(avahi_server_errno(mc.server)));
1065
1051
avahi_simple_poll_quit(mc.simple_poll);
1074
1060
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1075
1061
name, type, domain, protocol, 0,
1076
1062
resolve_callback, NULL) == NULL)
1077
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1079
avahi_strerror(avahi_server_errno(mc.server)));
1063
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s': %s\n",
1064
name, avahi_strerror(avahi_server_errno(mc.server)));
1082
1067
case AVAHI_BROWSER_REMOVE:
1085
1070
case AVAHI_BROWSER_ALL_FOR_NOW:
1086
1071
case AVAHI_BROWSER_CACHE_EXHAUSTED:
1088
fprintf_plus(stderr, "No Mandos server found, still"
1073
fprintf_plus(stderr, "No Mandos server found, still searching...\n");
1131
1115
/* Reject the loopback device */
1132
1116
if(ifr->ifr_flags & IFF_LOOPBACK){
1134
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1118
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n", ifname);
1139
1122
/* Accept point-to-point devices only if connect_to is specified */
1140
1123
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1142
fprintf_plus(stderr, "Accepting point-to-point interface"
1143
" \"%s\"\n", ifname);
1125
fprintf_plus(stderr, "Accepting point-to-point interface \"%s\"\n", ifname);
1147
1129
/* Otherwise, reject non-broadcast-capable devices */
1148
1130
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1150
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1151
" \"%s\"\n", ifname);
1132
fprintf_plus(stderr, "Rejecting non-broadcast interface \"%s\"\n", ifname);
1155
1136
/* Reject non-ARP interfaces (including dummy interfaces) */
1156
1137
if(ifr->ifr_flags & IFF_NOARP){
1158
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1139
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1181
1161
struct ifreq ifr;
1182
1162
if(not get_flags(if_entry->d_name, &ifr)){
1184
fprintf_plus(stderr, "Failed to get flags for interface "
1185
"\"%s\"\n", if_entry->d_name);
1164
fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1206
1186
struct ifreq ifr;
1207
1187
if(not get_flags(if_entry->d_name, &ifr)){
1209
fprintf_plus(stderr, "Failed to get flags for interface "
1210
"\"%s\"\n", if_entry->d_name);
1189
fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1314
1294
if(mc.current_server == NULL){
1316
fprintf_plus(stderr, "Wait until first server is found."
1296
fprintf_plus(stderr, "Wait until first server is found. No timeout!\n");
1319
1298
ret = avahi_simple_poll_iterate(s, -1);
1322
fprintf_plus(stderr, "Check current_server if we should run"
1301
fprintf_plus(stderr, "Check current_server if we should run it,"
1325
1304
/* the current time */
1326
1305
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[i];
1392
char *fullname = NULL;
1393
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1395
perror_plus("asprintf");
1399
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1402
pid_t hook_pid = fork();
1405
dup2(devnull, STDIN_FILENO);
1407
dup2(STDERR_FILENO, STDOUT_FILENO);
1408
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1410
perror_plus("setenv");
1413
ret = setenv("DEVICE", interface, 1);
1415
perror_plus("setenv");
1418
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1420
perror_plus("setenv");
1423
ret = setenv("MODE", mode, 1);
1425
perror_plus("setenv");
1429
ret = asprintf(&delaystring, "%f", delay);
1431
perror_plus("asprintf");
1434
ret = setenv("DELAY", delaystring, 1);
1437
perror_plus("setenv");
1441
ret = execl(fullname, direntry->d_name, mode, NULL);
1442
perror_plus("execl");
1445
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1446
perror_plus("waitpid");
1450
if(WIFEXITED(status)){
1451
if(WEXITSTATUS(status) != 0){
1452
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1453
" with status %d\n", direntry->d_name,
1454
WEXITSTATUS(status));
1458
} else if(WIFSIGNALED(status)){
1459
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1460
" signal %d\n", direntry->d_name,
1465
fprintf_plus(stderr, "Warning: network hook \"%s\""
1466
" crashed\n", direntry->d_name);
1481
1357
int main(int argc, char *argv[]){
1482
1358
AvahiSServiceBrowser *sb = NULL;
1641
1517
argp_state_help(state, state->out_stream,
1642
1518
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1643
1519
case 'V': /* --version */
1644
fprintf_plus(state->out_stream,
1645
"Mandos plugin mandos-client: ");
1520
fprintf_plus(state->out_stream, "Mandos plugin mandos-client: ");
1646
1521
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1647
1522
exit(argp_err_exit_status);
1738
/* Run network hooks */
1611
/* Find network hooks and run them */
1741
/* Re-raise priviliges */
1745
perror_plus("seteuid");
1748
if(not run_network_hooks("start", interface, delay)){
1752
/* Lower privileges */
1756
perror_plus("seteuid");
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",
1800
1746
srand((unsigned int) time(NULL));
1801
1747
mc.simple_poll = avahi_simple_poll_new();
1802
1748
if(mc.simple_poll == NULL){
1803
fprintf_plus(stderr,
1804
"Avahi: Failed to create simple poll object.\n");
1749
fprintf_plus(stderr, "Avahi: Failed to create simple poll object.\n");
1805
1750
exitcode = EX_UNAVAILABLE;
2153
2105
NULL, 0, browse_callback, NULL);
2154
2106
if(sb == NULL){
2155
2107
fprintf_plus(stderr, "Failed to create service browser: %s\n",
2156
avahi_strerror(avahi_server_errno(mc.server)));
2108
avahi_strerror(avahi_server_errno(mc.server)));
2157
2109
exitcode = EX_UNAVAILABLE;
2215
/* Re-raise priviliges */
2221
perror_plus("seteuid");
2224
/* Run network hooks */
2225
if(not run_network_hooks("stop", interface, delay)){
2229
/* Take down the network interface */
2230
if(take_down_interface and geteuid() == 0){
2167
/* XXX run network hooks "stop" here */
2169
/* Take down the network interface */
2170
if(take_down_interface){
2171
/* Re-raise priviliges */
2175
perror_plus("seteuid");
2231
2178
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2233
2180
perror_plus("ioctl SIOCGIFFLAGS");