174
174
perror(print_text);
177
int fprintf_plus(FILE *stream, const char *format, ...){
179
va_start (ap, format);
181
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
182
program_invocation_short_name));
183
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
187
178
* Make additional room in "buffer" for at least BUFFER_SIZE more
188
179
* bytes. "buffer_capacity" is how much is currently allocated,
189
180
* "buffer_length" is how much is already used.
191
182
size_t incbuffer(char **buffer, size_t buffer_length,
192
size_t buffer_capacity){
183
size_t buffer_capacity){
193
184
if(buffer_length + BUFFER_SIZE > buffer_capacity){
194
185
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
195
186
if(buffer == NULL){
263
255
rc = gpgme_data_new_from_fd(&pgp_data, fd);
264
256
if(rc != GPG_ERR_NO_ERROR){
265
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
266
gpgme_strsource(rc), gpgme_strerror(rc));
257
fprintf(stderr, "Mandos plugin mandos-client: "
258
"bad gpgme_data_new_from_fd: %s: %s\n",
259
gpgme_strsource(rc), gpgme_strerror(rc));
270
263
rc = gpgme_op_import(mc.ctx, pgp_data);
271
264
if(rc != GPG_ERR_NO_ERROR){
272
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
273
gpgme_strsource(rc), gpgme_strerror(rc));
265
fprintf(stderr, "Mandos plugin mandos-client: "
266
"bad gpgme_op_import: %s: %s\n",
267
gpgme_strsource(rc), gpgme_strerror(rc));
286
fprintf_plus(stderr, "Initializing GPGME\n");
280
fprintf(stderr, "Mandos plugin mandos-client: "
281
"Initializing GPGME\n");
290
285
gpgme_check_version(NULL);
291
286
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
292
287
if(rc != GPG_ERR_NO_ERROR){
293
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
294
gpgme_strsource(rc), gpgme_strerror(rc));
288
fprintf(stderr, "Mandos plugin mandos-client: "
289
"bad gpgme_engine_check_version: %s: %s\n",
290
gpgme_strsource(rc), gpgme_strerror(rc));
298
294
/* Set GPGME home directory for the OpenPGP engine only */
299
295
rc = gpgme_get_engine_info(&engine_info);
300
296
if(rc != GPG_ERR_NO_ERROR){
301
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
302
gpgme_strsource(rc), gpgme_strerror(rc));
297
fprintf(stderr, "Mandos plugin mandos-client: "
298
"bad gpgme_get_engine_info: %s: %s\n",
299
gpgme_strsource(rc), gpgme_strerror(rc));
305
302
while(engine_info != NULL){
311
308
engine_info = engine_info->next;
313
310
if(engine_info == NULL){
314
fprintf_plus(stderr, "Could not set GPGME home dir to %s\n",
311
fprintf(stderr, "Mandos plugin mandos-client: "
312
"Could not set GPGME home dir to %s\n", tempdir);
319
316
/* Create new GPGME "context" */
320
317
rc = gpgme_new(&(mc.ctx));
321
318
if(rc != GPG_ERR_NO_ERROR){
322
fprintf_plus(stderr, "Mandos plugin mandos-client: "
323
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
319
fprintf(stderr, "Mandos plugin mandos-client: "
320
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
346
343
ssize_t plaintext_length = 0;
349
fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
346
fprintf(stderr, "Mandos plugin mandos-client: "
347
"Trying to decrypt OpenPGP data\n");
352
350
/* Create new GPGME data buffer from memory cryptotext */
353
351
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
355
353
if(rc != GPG_ERR_NO_ERROR){
356
fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
357
gpgme_strsource(rc), gpgme_strerror(rc));
354
fprintf(stderr, "Mandos plugin mandos-client: "
355
"bad gpgme_data_new_from_mem: %s: %s\n",
356
gpgme_strsource(rc), gpgme_strerror(rc));
361
360
/* Create new empty GPGME data buffer for the plaintext */
362
361
rc = gpgme_data_new(&dh_plain);
363
362
if(rc != GPG_ERR_NO_ERROR){
364
fprintf_plus(stderr, "Mandos plugin mandos-client: "
365
"bad gpgme_data_new: %s: %s\n",
366
gpgme_strsource(rc), gpgme_strerror(rc));
363
fprintf(stderr, "Mandos plugin mandos-client: "
364
"bad gpgme_data_new: %s: %s\n",
365
gpgme_strsource(rc), gpgme_strerror(rc));
367
366
gpgme_data_release(dh_crypto);
373
372
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
374
373
if(rc != GPG_ERR_NO_ERROR){
375
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
376
gpgme_strsource(rc), gpgme_strerror(rc));
374
fprintf(stderr, "Mandos plugin mandos-client: "
375
"bad gpgme_op_decrypt: %s: %s\n",
376
gpgme_strsource(rc), gpgme_strerror(rc));
377
377
plaintext_length = -1;
379
379
gpgme_decrypt_result_t result;
380
380
result = gpgme_op_decrypt_result(mc.ctx);
381
381
if(result == NULL){
382
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
382
fprintf(stderr, "Mandos plugin mandos-client: "
383
"gpgme_op_decrypt_result failed\n");
384
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
385
result->unsupported_algorithm);
386
fprintf_plus(stderr, "Wrong key usage: %u\n",
387
result->wrong_key_usage);
385
fprintf(stderr, "Mandos plugin mandos-client: "
386
"Unsupported algorithm: %s\n",
387
result->unsupported_algorithm);
388
fprintf(stderr, "Mandos plugin mandos-client: "
389
"Wrong key usage: %u\n",
390
result->wrong_key_usage);
388
391
if(result->file_name != NULL){
389
fprintf_plus(stderr, "File name: %s\n", result->file_name);
392
fprintf(stderr, "Mandos plugin mandos-client: "
393
"File name: %s\n", result->file_name);
391
395
gpgme_recipient_t recipient;
392
396
recipient = result->recipients;
393
397
while(recipient != NULL){
394
fprintf_plus(stderr, "Public key algorithm: %s\n",
395
gpgme_pubkey_algo_name
396
(recipient->pubkey_algo));
397
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
398
fprintf_plus(stderr, "Secret key available: %s\n",
399
recipient->status == GPG_ERR_NO_SECKEY
398
fprintf(stderr, "Mandos plugin mandos-client: "
399
"Public key algorithm: %s\n",
400
gpgme_pubkey_algo_name(recipient->pubkey_algo));
401
fprintf(stderr, "Mandos plugin mandos-client: "
402
"Key ID: %s\n", recipient->keyid);
403
fprintf(stderr, "Mandos plugin mandos-client: "
404
"Secret key available: %s\n",
405
recipient->status == GPG_ERR_NO_SECKEY
401
407
recipient = recipient->next;
419
426
*plaintext = NULL;
421
428
plaintext_capacity = incbuffer(plaintext,
422
(size_t)plaintext_length,
429
(size_t)plaintext_length,
424
431
if(plaintext_capacity == 0){
425
perror_plus("incbuffer");
426
plaintext_length = -1;
432
perror_plus("incbuffer");
433
plaintext_length = -1;
430
437
ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
482
fprintf_plus(stderr, "Initializing GnuTLS\n");
490
fprintf(stderr, "Mandos plugin mandos-client: "
491
"Initializing GnuTLS\n");
485
494
ret = gnutls_global_init();
486
495
if(ret != GNUTLS_E_SUCCESS){
487
fprintf_plus(stderr, "GnuTLS global_init: %s\n",
488
safer_gnutls_strerror(ret));
496
fprintf(stderr, "Mandos plugin mandos-client: "
497
"GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
500
509
/* OpenPGP credentials */
501
510
ret = gnutls_certificate_allocate_credentials(&mc.cred);
502
511
if(ret != GNUTLS_E_SUCCESS){
503
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
504
safer_gnutls_strerror(ret));
512
fprintf(stderr, "Mandos plugin mandos-client: "
513
"GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
505
514
gnutls_global_deinit();
510
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
511
" secret key %s as GnuTLS credentials\n",
519
fprintf(stderr, "Mandos plugin mandos-client: "
520
"Attempting to use OpenPGP public key %s and"
521
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
516
525
ret = gnutls_certificate_set_openpgp_key_file
517
526
(mc.cred, pubkeyfilename, seckeyfilename,
518
527
GNUTLS_OPENPGP_FMT_BASE64);
519
528
if(ret != GNUTLS_E_SUCCESS){
521
"Error[%d] while reading the OpenPGP key pair ('%s',"
522
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
523
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
524
safer_gnutls_strerror(ret));
530
"Mandos plugin mandos-client: "
531
"Error[%d] while reading the OpenPGP key pair ('%s',"
532
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
533
fprintf(stderr, "Mandos plugin mandos-client: "
534
"The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
528
538
/* GnuTLS server initialization */
529
539
ret = gnutls_dh_params_init(&mc.dh_params);
530
540
if(ret != GNUTLS_E_SUCCESS){
531
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
532
" initialization: %s\n",
533
safer_gnutls_strerror(ret));
541
fprintf(stderr, "Mandos plugin mandos-client: "
542
"Error in GnuTLS DH parameter initialization:"
543
" %s\n", safer_gnutls_strerror(ret));
536
546
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
537
547
if(ret != GNUTLS_E_SUCCESS){
538
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
539
safer_gnutls_strerror(ret));
548
fprintf(stderr, "Mandos plugin mandos-client: "
549
"Error in GnuTLS prime generation: %s\n",
550
safer_gnutls_strerror(ret));
563
574
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
564
575
if(ret != GNUTLS_E_SUCCESS){
566
"Error in GnuTLS session initialization: %s\n",
567
safer_gnutls_strerror(ret));
576
fprintf(stderr, "Mandos plugin mandos-client: "
577
"Error in GnuTLS session initialization: %s\n",
578
safer_gnutls_strerror(ret));
578
589
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
579
590
if(ret != GNUTLS_E_SUCCESS){
580
fprintf_plus(stderr, "Syntax error at: %s\n", err);
581
fprintf_plus(stderr, "GnuTLS error: %s\n",
582
safer_gnutls_strerror(ret));
591
fprintf(stderr, "Mandos plugin mandos-client: "
592
"Syntax error at: %s\n", err);
593
fprintf(stderr, "Mandos plugin mandos-client: "
594
"GnuTLS error: %s\n", safer_gnutls_strerror(ret));
583
595
gnutls_deinit(*session);
595
607
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
596
608
if(ret != GNUTLS_E_SUCCESS){
597
fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
598
safer_gnutls_strerror(ret));
609
fprintf(stderr, "Mandos plugin mandos-client: "
610
"Error setting GnuTLS credentials: %s\n",
611
safer_gnutls_strerror(ret));
599
612
gnutls_deinit(*session);
702
718
if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
703
719
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
705
721
if(if_index == AVAHI_IF_UNSPEC){
706
fprintf_plus(stderr, "An IPv6 link-local address is"
707
" incomplete without a network interface\n");
722
fprintf(stderr, "Mandos plugin mandos-client: "
723
"An IPv6 link-local address is incomplete"
724
" without a network interface\n");
728
745
if(if_indextoname((unsigned int)if_index, interface) == NULL){
729
746
perror_plus("if_indextoname");
731
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
732
"\n", ip, interface, port);
748
fprintf(stderr, "Mandos plugin mandos-client: "
749
"Connection to: %s%%%s, port %" PRIu16 "\n",
750
ip, interface, port);
735
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
753
fprintf(stderr, "Mandos plugin mandos-client: "
754
"Connection to: %s, port %" PRIu16 "\n", ip, port);
738
756
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
739
757
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
891
912
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
893
fprintf_plus(stderr, "*** GnuTLS Re-handshake failed "
914
fprintf(stderr, "Mandos plugin mandos-client: "
915
"*** GnuTLS Re-handshake failed ***\n");
895
916
gnutls_perror(ret);
901
fprintf_plus(stderr, "Unknown error while reading data from"
902
" encrypted session with Mandos server\n");
922
fprintf(stderr, "Mandos plugin mandos-client: "
923
"Unknown error while reading data from"
924
" encrypted session with Mandos server\n");
903
925
gnutls_bye(session, GNUTLS_SHUT_RDWR);
1011
1036
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)));
1037
fprintf(stderr, "Mandos plugin mandos-client: "
1038
"(Avahi Resolver) Failed to resolve service '%s'"
1039
" of type '%s' in domain '%s': %s\n", name, type, domain,
1040
avahi_strerror(avahi_server_errno(mc.server)));
1018
1043
case AVAHI_RESOLVER_FOUND:
1020
1045
char ip[AVAHI_ADDRESS_STR_MAX];
1021
1046
avahi_address_snprint(ip, sizeof(ip), address);
1023
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);
1048
fprintf(stderr, "Mandos plugin mandos-client: "
1049
"Mandos server \"%s\" found on %s (%s, %"
1050
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1051
ip, (intmax_t)interface, port);
1027
1053
int ret = start_mandos_communication(ip, port, interface,
1028
1054
avahi_proto_to_af(proto));
1074
1101
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1075
1102
name, type, domain, protocol, 0,
1076
1103
resolve_callback, NULL) == NULL)
1077
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1079
avahi_strerror(avahi_server_errno(mc.server)));
1104
fprintf(stderr, "Mandos plugin mandos-client: "
1105
"Avahi: Failed to resolve service '%s': %s\n",
1106
name, avahi_strerror(avahi_server_errno(mc.server)));
1082
1109
case AVAHI_BROWSER_REMOVE:
1131
1158
/* Reject the loopback device */
1132
1159
if(ifr->ifr_flags & IFF_LOOPBACK){
1134
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1161
fprintf(stderr, "Mandos plugin mandos-client: "
1162
"Rejecting loopback interface \"%s\"\n", ifname);
1139
1166
/* Accept point-to-point devices only if connect_to is specified */
1140
1167
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1142
fprintf_plus(stderr, "Accepting point-to-point interface"
1143
" \"%s\"\n", ifname);
1169
fprintf(stderr, "Mandos plugin mandos-client: "
1170
"Accepting point-to-point interface \"%s\"\n", ifname);
1147
1174
/* Otherwise, reject non-broadcast-capable devices */
1148
1175
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1150
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1151
" \"%s\"\n", ifname);
1177
fprintf(stderr, "Mandos plugin mandos-client: "
1178
"Rejecting non-broadcast interface \"%s\"\n", ifname);
1155
1182
/* Reject non-ARP interfaces (including dummy interfaces) */
1156
1183
if(ifr->ifr_flags & IFF_NOARP){
1158
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1185
fprintf(stderr, "Mandos plugin mandos-client: "
1186
"Rejecting non-ARP interface \"%s\"\n", ifname);
1164
1191
/* Accept this device */
1166
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1193
fprintf(stderr, "Mandos plugin mandos-client: "
1194
"Interface \"%s\" is good\n", ifname);
1261
sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1262
"abcdefghijklmnopqrstuvwxyz"
1265
if((direntry->d_name)[sret] != '\0'){
1266
/* Contains non-allowed characters */
1268
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1292
/* Save pointer to last character */
1293
char *end = strchr(direntry->d_name, '\0')-1;
1300
if(((direntry->d_name)[0] == '#')
1302
/* Temporary #name# */
1306
/* XXX more rules here */
1274
1308
char *fullname = NULL;
1275
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1309
ret = asprintf(&fullname, "%s/%s", hookdir,
1277
1312
perror_plus("asprintf");
1281
1316
ret = stat(fullname, &st);
1284
perror_plus("Could not stat hook");
1319
perror_plus("Could not stat plugin");
1288
1323
if(not (S_ISREG(st.st_mode))){
1289
1324
/* Not a regular file */
1291
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1296
1327
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1297
1328
/* Not executable */
1299
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1305
fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1318
1341
if(mc.current_server == NULL){
1320
fprintf_plus(stderr, "Wait until first server is found."
1343
fprintf(stderr, "Mandos plugin mandos-client: "
1344
"Wait until first server is found. No timeout!\n");
1323
1346
ret = avahi_simple_poll_iterate(s, -1);
1326
fprintf_plus(stderr, "Check current_server if we should run"
1349
fprintf(stderr, "Mandos plugin mandos-client: "
1350
"Check current_server if we should run it,"
1329
1353
/* the current time */
1330
1354
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1383
bool run_network_hooks(const char *mode, const char *interface,
1385
struct dirent **direntries;
1386
struct dirent *direntry;
1388
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1391
perror_plus("scandir");
1393
int devnull = open("/dev/null", O_RDONLY);
1394
for(int i = 0; i < numhooks; i++){
1395
direntry = direntries[i];
1396
char *fullname = NULL;
1397
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1399
perror_plus("asprintf");
1403
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1406
pid_t hook_pid = fork();
1409
dup2(devnull, STDIN_FILENO);
1411
dup2(STDERR_FILENO, STDOUT_FILENO);
1412
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1414
perror_plus("setenv");
1417
ret = setenv("DEVICE", interface, 1);
1419
perror_plus("setenv");
1422
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1424
perror_plus("setenv");
1427
ret = setenv("MODE", mode, 1);
1429
perror_plus("setenv");
1433
ret = asprintf(&delaystring, "%f", delay);
1435
perror_plus("asprintf");
1438
ret = setenv("DELAY", delaystring, 1);
1441
perror_plus("setenv");
1445
ret = execl(fullname, direntry->d_name, mode, NULL);
1446
perror_plus("execl");
1449
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1450
perror_plus("waitpid");
1454
if(WIFEXITED(status)){
1455
if(WEXITSTATUS(status) != 0){
1456
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1457
" with status %d\n", direntry->d_name,
1458
WEXITSTATUS(status));
1462
} else if(WIFSIGNALED(status)){
1463
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1464
" signal %d\n", direntry->d_name,
1469
fprintf_plus(stderr, "Warning: network hook \"%s\""
1470
" crashed\n", direntry->d_name);
1477
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1486
1407
int main(int argc, char *argv[]){
1487
1408
AvahiSServiceBrowser *sb = NULL;
1646
1567
argp_state_help(state, state->out_stream,
1647
1568
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1648
1569
case 'V': /* --version */
1649
fprintf_plus(state->out_stream,
1650
"Mandos plugin mandos-client: ");
1651
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1570
fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1571
fprintf(state->out_stream, "%s\n", argp_program_version);
1652
1572
exit(argp_err_exit_status);
1743
/* Run network hooks */
1661
/* Find network hooks and run them */
1746
/* Re-raise priviliges */
1750
perror_plus("seteuid");
1753
if(not run_network_hooks("start", interface, delay)){
1757
/* Lower privileges */
1761
perror_plus("seteuid");
1663
struct dirent **direntries;
1664
struct dirent *direntry;
1665
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1668
perror_plus("scandir");
1670
int devnull = open("/dev/null", O_RDONLY);
1671
for(int i = 0; i < numhooks; i++){
1672
direntry = direntries[0];
1673
char *fullname = NULL;
1674
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1676
perror_plus("asprintf");
1679
pid_t hook_pid = fork();
1682
dup2(devnull, STDIN_FILENO);
1684
dup2(STDERR_FILENO, STDOUT_FILENO);
1685
ret = setenv("DEVICE", interface, 1);
1687
perror_plus("setenv");
1690
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1692
perror_plus("setenv");
1695
ret = setenv("MODE", "start", 1);
1697
perror_plus("setenv");
1701
ret = asprintf(&delaystring, "%f", delay);
1703
perror_plus("asprintf");
1706
ret = setenv("DELAY", delaystring, 1);
1709
perror_plus("setenv");
1713
ret = execl(fullname, direntry->d_name, "start", NULL);
1714
perror_plus("execl");
1717
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1718
perror_plus("waitpid");
1722
if(WIFEXITED(status)){
1723
if(WEXITSTATUS(status) != 0){
1724
fprintf(stderr, "Mandos plugin mandos-client: "
1725
"Warning: network hook \"%s\" exited"
1726
" with status %d\n", direntry->d_name,
1727
WEXITSTATUS(status));
1731
} else if(WIFSIGNALED(status)){
1732
fprintf(stderr, "Mandos plugin mandos-client: "
1733
"Warning: network hook \"%s\" died by"
1734
" signal %d\n", direntry->d_name,
1739
fprintf(stderr, "Mandos plugin mandos-client: "
1740
"Warning: network hook \"%s\" crashed\n",
1878
1869
if(strcmp(interface, "none") != 0){
1879
1870
if_index = (AvahiIfIndex) if_nametoindex(interface);
1880
1871
if(if_index == 0){
1881
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1872
fprintf(stderr, "Mandos plugin mandos-client: "
1873
"No such interface: \"%s\"\n", interface);
1882
1874
exitcode = EX_UNAVAILABLE;
2157
2162
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2158
2163
NULL, 0, browse_callback, NULL);
2159
2164
if(sb == NULL){
2160
fprintf_plus(stderr, "Failed to create service browser: %s\n",
2161
avahi_strerror(avahi_server_errno(mc.server)));
2165
fprintf(stderr, "Mandos plugin mandos-client: "
2166
"Failed to create service browser: %s\n",
2167
avahi_strerror(avahi_server_errno(mc.server)));
2162
2168
exitcode = EX_UNAVAILABLE;
2170
2176
/* Run the main loop */
2173
fprintf_plus(stderr, "Starting Avahi loop search\n");
2179
fprintf(stderr, "Mandos plugin mandos-client: "
2180
"Starting Avahi loop search\n");
2176
2183
ret = avahi_loop_with_timeout(mc.simple_poll,
2177
2184
(int)(retry_interval * 1000));
2179
fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2180
(ret == 0) ? "successfully" : "with error");
2186
fprintf(stderr, "Mandos plugin mandos-client: "
2187
"avahi_loop_with_timeout exited %s\n",
2188
(ret == 0) ? "successfully" : "with error");
2186
fprintf_plus(stderr, "%s exiting\n", argv[0]);
2194
fprintf(stderr, "Mandos plugin mandos-client: "
2195
"%s exiting\n", argv[0]);
2189
2198
/* Cleanup things */
2220
/* Re-raise priviliges */
2226
perror_plus("seteuid");
2229
/* XXX run network hooks "stop" here */
2231
/* Take down the network interface */
2232
if(take_down_interface){
2233
/* Re-raise priviliges */
2237
perror_plus("seteuid");
2230
/* Run network hooks */
2231
run_network_hooks("stop", interface, delay);
2233
/* Take down the network interface */
2234
if(take_down_interface and geteuid() == 0){
2235
2240
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2237
2242
perror_plus("ioctl SIOCGIFFLAGS");