332
355
ssize_t plaintext_length = 0;
335
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
358
fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
338
361
/* Create new GPGME data buffer from memory cryptotext */
339
362
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
341
364
if(rc != GPG_ERR_NO_ERROR){
342
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
343
gpgme_strsource(rc), gpgme_strerror(rc));
365
fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
366
gpgme_strsource(rc), gpgme_strerror(rc));
347
370
/* Create new empty GPGME data buffer for the plaintext */
348
371
rc = gpgme_data_new(&dh_plain);
349
372
if(rc != GPG_ERR_NO_ERROR){
350
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
351
gpgme_strsource(rc), gpgme_strerror(rc));
373
fprintf_plus(stderr, "Mandos plugin mandos-client: "
374
"bad gpgme_data_new: %s: %s\n",
375
gpgme_strsource(rc), gpgme_strerror(rc));
352
376
gpgme_data_release(dh_crypto);
358
382
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
359
383
if(rc != GPG_ERR_NO_ERROR){
360
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
361
gpgme_strsource(rc), gpgme_strerror(rc));
384
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
385
gpgme_strsource(rc), gpgme_strerror(rc));
362
386
plaintext_length = -1;
364
388
gpgme_decrypt_result_t result;
365
389
result = gpgme_op_decrypt_result(mc.ctx);
366
390
if(result == NULL){
367
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
391
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
369
fprintf(stderr, "Unsupported algorithm: %s\n",
370
result->unsupported_algorithm);
371
fprintf(stderr, "Wrong key usage: %u\n",
372
result->wrong_key_usage);
393
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
394
result->unsupported_algorithm);
395
fprintf_plus(stderr, "Wrong key usage: %u\n",
396
result->wrong_key_usage);
373
397
if(result->file_name != NULL){
374
fprintf(stderr, "File name: %s\n", result->file_name);
398
fprintf_plus(stderr, "File name: %s\n", result->file_name);
376
400
gpgme_recipient_t recipient;
377
401
recipient = result->recipients;
378
402
while(recipient != NULL){
379
fprintf(stderr, "Public key algorithm: %s\n",
380
gpgme_pubkey_algo_name(recipient->pubkey_algo));
381
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
382
fprintf(stderr, "Secret key available: %s\n",
383
recipient->status == GPG_ERR_NO_SECKEY
403
fprintf_plus(stderr, "Public key algorithm: %s\n",
404
gpgme_pubkey_algo_name
405
(recipient->pubkey_algo));
406
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
407
fprintf_plus(stderr, "Secret key available: %s\n",
408
recipient->status == GPG_ERR_NO_SECKEY
385
410
recipient = recipient->next;
484
508
/* OpenPGP credentials */
485
509
ret = gnutls_certificate_allocate_credentials(&mc.cred);
486
510
if(ret != GNUTLS_E_SUCCESS){
487
fprintf(stderr, "GnuTLS memory error: %s\n",
488
safer_gnutls_strerror(ret));
511
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
512
safer_gnutls_strerror(ret));
489
513
gnutls_global_deinit();
494
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
495
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
518
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
519
" secret key %s as GnuTLS credentials\n",
499
524
ret = gnutls_certificate_set_openpgp_key_file
500
525
(mc.cred, pubkeyfilename, seckeyfilename,
501
526
GNUTLS_OPENPGP_FMT_BASE64);
502
527
if(ret != GNUTLS_E_SUCCESS){
504
"Error[%d] while reading the OpenPGP key pair ('%s',"
505
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
506
fprintf(stderr, "The GnuTLS error is: %s\n",
507
safer_gnutls_strerror(ret));
529
"Error[%d] while reading the OpenPGP key pair ('%s',"
530
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
531
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
532
safer_gnutls_strerror(ret));
511
536
/* GnuTLS server initialization */
512
537
ret = gnutls_dh_params_init(&mc.dh_params);
513
538
if(ret != GNUTLS_E_SUCCESS){
514
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
515
" %s\n", safer_gnutls_strerror(ret));
539
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
540
" initialization: %s\n",
541
safer_gnutls_strerror(ret));
518
544
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
519
545
if(ret != GNUTLS_E_SUCCESS){
520
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
521
safer_gnutls_strerror(ret));
546
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
547
safer_gnutls_strerror(ret));
1000
1030
char ip[AVAHI_ADDRESS_STR_MAX];
1001
1031
avahi_address_snprint(ip, sizeof(ip), address);
1003
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
1004
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1005
ip, (intmax_t)interface, port);
1033
fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1034
PRIdMAX ") on port %" PRIu16 "\n", name,
1035
host_name, ip, (intmax_t)interface, port);
1007
int ret = start_mandos_communication(ip, port, interface,
1037
int ret = start_mandos_communication(ip, (in_port_t)port,
1008
1039
avahi_proto_to_af(proto));
1010
1041
avahi_simple_poll_quit(mc.simple_poll);
1012
ret = add_server(ip, port, interface,
1013
avahi_proto_to_af(proto));
1043
if(not add_server(ip, (in_port_t)port, interface,
1044
avahi_proto_to_af(proto))){
1045
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1085
1123
errno = old_errno;
1089
* This function determines if a directory entry in /sys/class/net
1090
* corresponds to an acceptable network device.
1091
* (This function is passed to scandir(3) as a filter function.)
1093
int good_interface(const struct dirent *if_entry){
1126
bool get_flags(const char *ifname, struct ifreq *ifr){
1096
if(if_entry->d_name[0] == '.'){
1099
1130
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1101
1133
perror_plus("socket");
1105
strcpy(ifr.ifr_name, if_entry->d_name);
1106
ret = ioctl(s, SIOCGIFFLAGS, &ifr);
1137
strcpy(ifr->ifr_name, ifname);
1138
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1109
1142
perror_plus("ioctl SIOCGIFFLAGS");
1150
bool good_flags(const char *ifname, const struct ifreq *ifr){
1113
1152
/* Reject the loopback device */
1114
if(ifr.ifr_flags & IFF_LOOPBACK){
1153
if(ifr->ifr_flags & IFF_LOOPBACK){
1116
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1155
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1121
1160
/* Accept point-to-point devices only if connect_to is specified */
1122
if(connect_to != NULL and (ifr.ifr_flags & IFF_POINTOPOINT)){
1161
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1124
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1163
fprintf_plus(stderr, "Accepting point-to-point interface"
1164
" \"%s\"\n", ifname);
1129
1168
/* Otherwise, reject non-broadcast-capable devices */
1130
if(not (ifr.ifr_flags & IFF_BROADCAST)){
1169
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1132
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1171
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1172
" \"%s\"\n", ifname);
1137
1176
/* Reject non-ARP interfaces (including dummy interfaces) */
1138
if(ifr.ifr_flags & IFF_NOARP){
1177
if(ifr->ifr_flags & IFF_NOARP){
1140
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1179
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1145
1185
/* Accept this device */
1147
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1187
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1193
* This function determines if a directory entry in /sys/class/net
1194
* corresponds to an acceptable network device.
1195
* (This function is passed to scandir(3) as a filter function.)
1197
int good_interface(const struct dirent *if_entry){
1198
if(if_entry->d_name[0] == '.'){
1203
if(not get_flags(if_entry->d_name, &ifr)){
1205
fprintf_plus(stderr, "Failed to get flags for interface "
1206
"\"%s\"\n", if_entry->d_name);
1211
if(not good_flags(if_entry->d_name, &ifr)){
1218
* This function determines if a network interface is up.
1220
bool interface_is_up(const char *interface){
1222
if(not get_flags(interface, &ifr)){
1224
fprintf_plus(stderr, "Failed to get flags for interface "
1225
"\"%s\"\n", interface);
1230
return (bool)(ifr.ifr_flags & IFF_UP);
1234
* This function determines if a network interface is running
1236
bool interface_is_running(const char *interface){
1238
if(not get_flags(interface, &ifr)){
1240
fprintf_plus(stderr, "Failed to get flags for interface "
1241
"\"%s\"\n", interface);
1246
return (bool)(ifr.ifr_flags & IFF_RUNNING);
1153
1249
int notdotentries(const struct dirent *direntry){
1154
1250
/* Skip "." and ".." */
1155
1251
if(direntry->d_name[0] == '.'
1225
1383
ret = avahi_simple_poll_iterate(s, (int)block_time);
1228
if (ret > 0 or errno != EINTR) {
1386
if (ret > 0 or errno != EINTR){
1229
1387
return (ret != 1) ? ret : 0;
1393
/* Set effective uid to 0, return errno */
1394
error_t raise_privileges(void){
1395
error_t old_errno = errno;
1396
error_t ret_errno = 0;
1397
if(seteuid(0) == -1){
1399
perror_plus("seteuid");
1405
/* Set effective and real user ID to 0. Return errno. */
1406
error_t raise_privileges_permanently(void){
1407
error_t old_errno = errno;
1408
error_t ret_errno = raise_privileges();
1413
if(setuid(0) == -1){
1415
perror_plus("seteuid");
1421
/* Set effective user ID to unprivileged saved user ID */
1422
error_t lower_privileges(void){
1423
error_t old_errno = errno;
1424
error_t ret_errno = 0;
1425
if(seteuid(uid) == -1){
1427
perror_plus("seteuid");
1433
/* Lower privileges permanently */
1434
error_t lower_privileges_permanently(void){
1435
error_t old_errno = errno;
1436
error_t ret_errno = 0;
1437
if(setuid(uid) == -1){
1439
perror_plus("setuid");
1445
bool run_network_hooks(const char *mode, const char *interface,
1447
struct dirent **direntries;
1448
struct dirent *direntry;
1450
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1453
if(errno == ENOENT){
1455
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1456
" found\n", hookdir);
1459
perror_plus("scandir");
1462
int devnull = open("/dev/null", O_RDONLY);
1463
for(int i = 0; i < numhooks; i++){
1464
direntry = direntries[i];
1465
char *fullname = NULL;
1466
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1468
perror_plus("asprintf");
1472
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1475
pid_t hook_pid = fork();
1478
/* Raise privileges */
1479
raise_privileges_permanently();
1484
perror_plus("setgid");
1486
/* Reset supplementary groups */
1488
ret = setgroups(0, NULL);
1490
perror_plus("setgroups");
1492
dup2(devnull, STDIN_FILENO);
1494
dup2(STDERR_FILENO, STDOUT_FILENO);
1495
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1497
perror_plus("setenv");
1500
ret = setenv("DEVICE", interface, 1);
1502
perror_plus("setenv");
1505
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1507
perror_plus("setenv");
1510
ret = setenv("MODE", mode, 1);
1512
perror_plus("setenv");
1516
ret = asprintf(&delaystring, "%f", delay);
1518
perror_plus("asprintf");
1521
ret = setenv("DELAY", delaystring, 1);
1524
perror_plus("setenv");
1528
if(connect_to != NULL){
1529
ret = setenv("CONNECT", connect_to, 1);
1531
perror_plus("setenv");
1535
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1536
perror_plus("execl");
1537
_exit(EXIT_FAILURE);
1541
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1542
perror_plus("waitpid");
1546
if(WIFEXITED(status)){
1547
if(WEXITSTATUS(status) != 0){
1548
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1549
" with status %d\n", direntry->d_name,
1550
WEXITSTATUS(status));
1554
} else if(WIFSIGNALED(status)){
1555
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1556
" signal %d\n", direntry->d_name,
1561
fprintf_plus(stderr, "Warning: network hook \"%s\""
1562
" crashed\n", direntry->d_name);
1569
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1578
error_t bring_up_interface(const char *const interface,
1581
error_t old_errno = errno;
1582
error_t ret_errno = 0;
1583
int ret, ret_setflags;
1584
struct ifreq network;
1585
unsigned int if_index = if_nametoindex(interface);
1587
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1597
if(not interface_is_up(interface)){
1598
if(not get_flags(interface, &network) and debug){
1600
fprintf_plus(stderr, "Failed to get flags for interface "
1601
"\"%s\"\n", interface);
1604
network.ifr_flags |= IFF_UP;
1606
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1609
perror_plus("socket");
1621
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1625
/* Raise priviliges */
1629
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1630
messages about the network interface to mess up the prompt */
1631
int ret_linux = klogctl(8, NULL, 5);
1632
bool restore_loglevel = true;
1633
if(ret_linux == -1){
1634
restore_loglevel = false;
1635
perror_plus("klogctl");
1637
#endif /* __linux__ */
1638
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1641
if(restore_loglevel){
1642
ret_linux = klogctl(7, NULL, 0);
1643
if(ret_linux == -1){
1644
perror_plus("klogctl");
1647
#endif /* __linux__ */
1649
/* Lower privileges */
1652
/* Close the socket */
1653
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1655
perror_plus("close");
1658
if(ret_setflags == -1){
1660
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1665
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1669
/* Sleep checking until interface is running.
1670
Check every 0.25s, up to total time of delay */
1671
for(int i=0; i < delay * 4; i++){
1672
if(interface_is_running(interface)){
1675
struct timespec sleeptime = { .tv_nsec = 250000000 };
1676
ret = nanosleep(&sleeptime, NULL);
1677
if(ret == -1 and errno != EINTR){
1678
perror_plus("nanosleep");
1686
error_t take_down_interface(const char *const interface){
1688
error_t old_errno = errno;
1689
error_t ret_errno = 0;
1690
int ret, ret_setflags;
1691
struct ifreq network;
1692
unsigned int if_index = if_nametoindex(interface);
1694
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1698
if(interface_is_up(interface)){
1699
if(not get_flags(interface, &network) and debug){
1701
fprintf_plus(stderr, "Failed to get flags for interface "
1702
"\"%s\"\n", interface);
1705
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1707
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1710
perror_plus("socket");
1716
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1720
/* Raise priviliges */
1723
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1726
/* Lower privileges */
1729
/* Close the socket */
1730
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1732
perror_plus("close");
1735
if(ret_setflags == -1){
1737
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1742
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1235
1750
int main(int argc, char *argv[]){
1236
1751
AvahiSServiceBrowser *sb = NULL;
1239
1754
intmax_t tmpmax;
1241
1756
int exitcode = EXIT_SUCCESS;
1242
const char *interface = "";
1243
struct ifreq network;
1245
bool take_down_interface = false;
1757
char *interfaces = NULL;
1758
size_t interfaces_size = 0;
1759
char *interfaces_to_take_down = NULL;
1760
size_t interfaces_to_take_down_size = 0;
1248
1761
char tempdir[] = "/tmp/mandosXXXXXX";
1249
1762
bool tempdir_created = false;
1250
1763
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1251
1764
const char *seckey = PATHDIR "/" SECKEY;
1252
1765
const char *pubkey = PATHDIR "/" PUBKEY;
1766
char *interfaces_hooks = NULL;
1767
size_t interfaces_hooks_size = 0;
1254
1769
bool gnutls_initialized = false;
1255
1770
bool gpgme_initialized = false;
1422
1948
/* Work around Debian bug #633582:
1423
1949
<http://bugs.debian.org/633582> */
1426
1951
/* Re-raise priviliges */
1430
perror_plus("seteuid");
1433
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1434
int seckey_fd = open(seckey, O_RDONLY);
1435
if(seckey_fd == -1){
1436
perror_plus("open");
1438
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1440
perror_plus("fstat");
1442
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1443
ret = fchown(seckey_fd, uid, gid);
1445
perror_plus("fchown");
1449
TEMP_FAILURE_RETRY(close(seckey_fd));
1453
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1454
int pubkey_fd = open(pubkey, O_RDONLY);
1455
if(pubkey_fd == -1){
1456
perror_plus("open");
1458
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1460
perror_plus("fstat");
1462
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1463
ret = fchown(pubkey_fd, uid, gid);
1465
perror_plus("fchown");
1469
TEMP_FAILURE_RETRY(close(pubkey_fd));
1473
/* Lower privileges */
1477
perror_plus("seteuid");
1952
if(raise_privileges() == 0){
1955
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1956
int seckey_fd = open(seckey, O_RDONLY);
1957
if(seckey_fd == -1){
1958
perror_plus("open");
1960
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1962
perror_plus("fstat");
1964
if(S_ISREG(st.st_mode)
1965
and st.st_uid == 0 and st.st_gid == 0){
1966
ret = fchown(seckey_fd, uid, gid);
1968
perror_plus("fchown");
1972
TEMP_FAILURE_RETRY(close(seckey_fd));
1976
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1977
int pubkey_fd = open(pubkey, O_RDONLY);
1978
if(pubkey_fd == -1){
1979
perror_plus("open");
1981
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1983
perror_plus("fstat");
1985
if(S_ISREG(st.st_mode)
1986
and st.st_uid == 0 and st.st_gid == 0){
1987
ret = fchown(pubkey_fd, uid, gid);
1989
perror_plus("fchown");
1993
TEMP_FAILURE_RETRY(close(pubkey_fd));
1997
/* Lower privileges */
2001
perror_plus("seteuid");
2006
/* Remove empty interface names */
2008
char *interface = NULL;
2009
while((interface = argz_next(interfaces, interfaces_size,
2011
if(if_nametoindex(interface) == 0){
2012
if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2013
fprintf_plus(stderr, "Not using nonexisting interface"
2014
" \"%s\"\n", interface);
2016
argz_delete(&interfaces, &interfaces_size, interface);
2022
/* Run network hooks */
2024
ret_errno = argz_append(&interfaces_hooks, &interfaces_hooks_size,
2025
interfaces, interfaces_size);
2028
perror_plus("argz_append");
2031
argz_stringify(interfaces_hooks, interfaces_hooks_size, (int)',');
2032
if(not run_network_hooks("start", interfaces_hooks, delay)){
1482
2038
avahi_set_log_function(empty_log);
1485
if(interface[0] == '\0'){
1486
struct dirent **direntries;
1487
ret = scandir(sys_class_net, &direntries, good_interface,
1490
/* Pick the first good interface */
1491
interface = strdup(direntries[0]->d_name);
1493
fprintf(stderr, "Using interface \"%s\"\n", interface);
1495
if(interface == NULL){
1496
perror_plus("malloc");
1498
exitcode = EXIT_FAILURE;
1504
fprintf(stderr, "Could not find a network interface\n");
1505
exitcode = EXIT_FAILURE;
1510
2041
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1511
2042
from the signal handler */
1584
/* If the interface is down, bring it up */
1585
if(strcmp(interface, "none") != 0){
1586
if_index = (AvahiIfIndex) if_nametoindex(interface);
1588
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1589
exitcode = EX_UNAVAILABLE;
1597
/* Re-raise priviliges */
1601
perror_plus("seteuid");
1605
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1606
messages about the network interface to mess up the prompt */
1607
ret = klogctl(8, NULL, 5);
1608
bool restore_loglevel = true;
1610
restore_loglevel = false;
1611
perror_plus("klogctl");
1613
#endif /* __linux__ */
1615
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1617
perror_plus("socket");
1618
exitcode = EX_OSERR;
1620
if(restore_loglevel){
1621
ret = klogctl(7, NULL, 0);
1623
perror_plus("klogctl");
1626
#endif /* __linux__ */
1627
/* Lower privileges */
1631
perror_plus("seteuid");
1635
strcpy(network.ifr_name, interface);
1636
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1638
perror_plus("ioctl SIOCGIFFLAGS");
1640
if(restore_loglevel){
1641
ret = klogctl(7, NULL, 0);
1643
perror_plus("klogctl");
1646
#endif /* __linux__ */
1647
exitcode = EX_OSERR;
1648
/* Lower privileges */
1652
perror_plus("seteuid");
1656
if((network.ifr_flags & IFF_UP) == 0){
1657
network.ifr_flags |= IFF_UP;
1658
take_down_interface = true;
1659
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1661
take_down_interface = false;
1662
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1663
exitcode = EX_OSERR;
1665
if(restore_loglevel){
1666
ret = klogctl(7, NULL, 0);
1668
perror_plus("klogctl");
1671
#endif /* __linux__ */
1672
/* Lower privileges */
1676
perror_plus("seteuid");
1681
/* Sleep checking until interface is running.
1682
Check every 0.25s, up to total time of delay */
1683
for(int i=0; i < delay * 4; i++){
1684
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1686
perror_plus("ioctl SIOCGIFFLAGS");
1687
} else if(network.ifr_flags & IFF_RUNNING){
1690
struct timespec sleeptime = { .tv_nsec = 250000000 };
1691
ret = nanosleep(&sleeptime, NULL);
1692
if(ret == -1 and errno != EINTR){
1693
perror_plus("nanosleep");
1696
if(not take_down_interface){
1697
/* We won't need the socket anymore */
1698
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1700
perror_plus("close");
1704
if(restore_loglevel){
1705
/* Restores kernel loglevel to default */
1706
ret = klogctl(7, NULL, 0);
1708
perror_plus("klogctl");
1711
#endif /* __linux__ */
1712
/* Lower privileges */
1714
if(take_down_interface){
1715
/* Lower privileges */
1718
perror_plus("seteuid");
2116
/* If no interfaces were specified, make a list */
2117
if(interfaces == NULL){
2118
struct dirent **direntries;
2119
/* Look for any good interfaces */
2120
ret = scandir(sys_class_net, &direntries, good_interface,
2123
/* Add all found interfaces to interfaces list */
2124
for(int i = 0; i < ret; ++i){
2125
ret_errno = argz_add(&interfaces, &interfaces_size,
2126
direntries[i]->d_name);
2128
perror_plus("argz_add");
2132
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2133
direntries[i]->d_name);
1721
/* Lower privileges permanently */
1724
perror_plus("setuid");
2139
fprintf_plus(stderr, "Could not find a network interface\n");
2140
exitcode = EXIT_FAILURE;
2145
/* If we only got one interface, explicitly use only that one */
2146
if(argz_count(interfaces, interfaces_size) == 1){
2148
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2151
if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2154
/* Bring up interfaces which are down */
2155
if(not (argz_count(interfaces, interfaces_size) == 1
2156
and strcmp(interfaces, "none") == 0)){
2157
char *interface = NULL;
2158
while((interface = argz_next(interfaces, interfaces_size,
2160
bool interface_was_up = interface_is_up(interface);
2161
ret = bring_up_interface(interface, delay);
2162
if(not interface_was_up){
2165
perror_plus("Failed to bring up interface");
2167
ret_errno = argz_add(&interfaces_to_take_down,
2168
&interfaces_to_take_down_size,
2175
interfaces_size = 0;
2176
if(debug and (interfaces_to_take_down == NULL)){
2177
fprintf_plus(stderr, "No interfaces were brought up\n");
1935
/* Take down the network interface */
1936
if(take_down_interface){
1937
/* Re-raise priviliges */
1941
perror_plus("seteuid");
1944
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1946
perror_plus("ioctl SIOCGIFFLAGS");
1947
} else if(network.ifr_flags & IFF_UP) {
1948
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1949
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1951
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2388
/* Re-raise priviliges */
2392
/* Run network hooks */
2393
run_network_hooks("stop", interfaces_hooks, delay);
2395
/* Take down the network interfaces which were brought up */
2397
char *interface = NULL;
2398
while((interface=argz_next(interfaces_to_take_down,
2399
interfaces_to_take_down_size,
2401
ret_errno = take_down_interface(interface);
2404
perror_plus("Failed to take down interface");
1954
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1956
perror_plus("close");
1958
/* Lower privileges permanently */
1962
perror_plus("setuid");
2407
if(debug and (interfaces_to_take_down == NULL)){
2408
fprintf_plus(stderr, "No interfaces needed to be taken"
2413
lower_privileges_permanently();
2416
free(interfaces_to_take_down);
2417
free(interfaces_hooks);
1967
2419
/* Removes the GPGME temp directory and all files inside */
1968
2420
if(tempdir_created){
1969
2421
struct dirent **direntries = NULL;