88
88
#include <sys/wait.h> /* waitpid(), WIFEXITED(),
89
89
WEXITSTATUS(), WTERMSIG() */
90
90
#include <grp.h> /* setgroups() */
91
#include <argz.h> /* argz_add_sep(), argz_next(),
92
argz_delete(), argz_append(),
93
argz_stringify(), argz_add(),
93
97
#include <sys/klog.h> /* klogctl() */
1122
1126
bool get_flags(const char *ifname, struct ifreq *ifr){
1125
1130
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1127
1133
perror_plus("socket");
1130
1137
strcpy(ifr->ifr_name, ifname);
1131
1138
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1134
1142
perror_plus("ioctl SIOCGIFFLAGS");
1209
* This function determines if a directory entry in /sys/class/net
1210
* corresponds to an acceptable network device which is up.
1211
* (This function is passed to scandir(3) as a filter function.)
1213
int up_interface(const struct dirent *if_entry){
1214
if(if_entry->d_name[0] == '.'){
1219
if(not get_flags(if_entry->d_name, &ifr)){
1221
fprintf_plus(stderr, "Failed to get flags for interface "
1222
"\"%s\"\n", if_entry->d_name);
1227
/* Reject down interfaces */
1228
if(not (ifr.ifr_flags & IFF_UP)){
1230
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1236
/* Reject non-running interfaces */
1237
if(not (ifr.ifr_flags & IFF_RUNNING)){
1239
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1245
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);
1251
1249
int notdotentries(const struct dirent *direntry){
1395
1393
/* Set effective uid to 0, return errno */
1396
int raise_privileges(void){
1397
int old_errno = errno;
1394
error_t raise_privileges(void){
1395
error_t old_errno = errno;
1396
error_t ret_errno = 0;
1400
1397
if(seteuid(0) == -1){
1401
1399
perror_plus("seteuid");
1404
1401
errno = old_errno;
1405
1402
return ret_errno;
1408
1405
/* Set effective and real user ID to 0. Return errno. */
1409
int raise_privileges_permanently(void){
1410
int old_errno = errno;
1411
int ret_errno = raise_privileges();
1406
error_t raise_privileges_permanently(void){
1407
error_t old_errno = errno;
1408
error_t ret_errno = raise_privileges();
1412
1409
if(ret_errno != 0){
1413
1410
errno = old_errno;
1414
1411
return ret_errno;
1417
1413
if(setuid(0) == -1){
1418
1415
perror_plus("seteuid");
1421
1417
errno = old_errno;
1422
1418
return ret_errno;
1425
1421
/* Set effective user ID to unprivileged saved user ID */
1426
int lower_privileges(void){
1427
int old_errno = errno;
1422
error_t lower_privileges(void){
1423
error_t old_errno = errno;
1424
error_t ret_errno = 0;
1430
1425
if(seteuid(uid) == -1){
1431
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");
1434
1441
errno = old_errno;
1435
1442
return ret_errno;
1564
int bring_up_interface(const char * const interface, const float delay){
1571
error_t bring_up_interface(const char *const interface,
1566
int old_errno = errno;
1574
error_t old_errno = errno;
1575
error_t ret_errno = 0;
1576
int ret, ret_setflags;
1569
1577
struct ifreq network;
1570
AvahiIfIndex if_index = (AvahiIfIndex)if_nametoindex(interface);
1578
unsigned int if_index = if_nametoindex(interface);
1571
1579
if(if_index == 0){
1572
1580
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1573
1581
errno = old_errno;
1582
/* Re-raise priviliges */
1586
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1587
messages about the network interface to mess up the prompt */
1588
ret = klogctl(8, NULL, 5);
1589
bool restore_loglevel = true;
1591
restore_loglevel = false;
1592
perror_plus("klogctl");
1594
#endif /* __linux__ */
1596
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1599
perror_plus("socket");
1601
if(restore_loglevel){
1602
ret = klogctl(7, NULL, 0);
1604
perror_plus("klogctl");
1607
#endif /* __linux__ */
1608
/* Lower privileges */
1613
strcpy(network.ifr_name, interface);
1614
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1617
perror_plus("ioctl SIOCGIFFLAGS");
1619
if(restore_loglevel){
1620
ret = klogctl(7, NULL, 0);
1622
perror_plus("klogctl");
1625
#endif /* __linux__ */
1626
/* Lower privileges */
1631
if((network.ifr_flags & IFF_UP) == 0){
1590
if(not interface_is_up(interface)){
1591
if(not get_flags(interface, &network) and debug){
1593
fprintf_plus(stderr, "Failed to get flags for interface "
1594
"\"%s\"\n", interface);
1632
1597
network.ifr_flags |= IFF_UP;
1633
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1599
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1602
perror_plus("socket");
1614
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1618
/* Raise priviliges */
1622
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1623
messages about the network interface to mess up the prompt */
1624
int ret_linux = klogctl(8, NULL, 5);
1625
bool restore_loglevel = true;
1626
if(ret_linux == -1){
1627
restore_loglevel = false;
1628
perror_plus("klogctl");
1630
#endif /* __linux__ */
1631
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1634
if(restore_loglevel){
1635
ret_linux = klogctl(7, NULL, 0);
1636
if(ret_linux == -1){
1637
perror_plus("klogctl");
1640
#endif /* __linux__ */
1642
/* Lower privileges */
1645
/* Close the socket */
1646
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1648
perror_plus("close");
1651
if(ret_setflags == -1){
1636
1653
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1638
if(restore_loglevel){
1639
ret = klogctl(7, NULL, 0);
1641
perror_plus("klogctl");
1644
#endif /* __linux__ */
1645
/* Lower privileges */
1647
1654
errno = old_errno;
1648
1655
return ret_errno;
1658
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1651
1662
/* Sleep checking until interface is running.
1652
1663
Check every 0.25s, up to total time of delay */
1653
1664
for(int i=0; i < delay * 4; i++){
1654
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1656
perror_plus("ioctl SIOCGIFFLAGS");
1657
} else if(network.ifr_flags & IFF_RUNNING){
1665
if(interface_is_running(interface)){
1660
1668
struct timespec sleeptime = { .tv_nsec = 250000000 };
1663
1671
perror_plus("nanosleep");
1666
/* Close the socket */
1667
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1669
perror_plus("close");
1679
error_t take_down_interface(const char *const interface){
1681
error_t old_errno = errno;
1682
error_t ret_errno = 0;
1683
int ret, ret_setflags;
1684
struct ifreq network;
1685
unsigned int if_index = if_nametoindex(interface);
1687
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1672
if(restore_loglevel){
1673
/* Restores kernel loglevel to default */
1674
ret = klogctl(7, NULL, 0);
1691
if(interface_is_up(interface)){
1692
if(not get_flags(interface, &network) and debug){
1694
fprintf_plus(stderr, "Failed to get flags for interface "
1695
"\"%s\"\n", interface);
1698
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1700
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1703
perror_plus("socket");
1709
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1713
/* Raise priviliges */
1716
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1719
/* Lower privileges */
1722
/* Close the socket */
1723
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1676
perror_plus("klogctl");
1725
perror_plus("close");
1728
if(ret_setflags == -1){
1730
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1735
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1679
#endif /* __linux__ */
1680
/* Lower privileges */
1682
1739
errno = old_errno;
1686
1743
int main(int argc, char *argv[]){
1687
1744
AvahiSServiceBrowser *sb = NULL;
1690
1747
intmax_t tmpmax;
1692
1749
int exitcode = EXIT_SUCCESS;
1693
const char *interface = "";
1694
struct ifreq network;
1696
bool take_down_interface = false;
1750
char *interfaces = NULL;
1751
size_t interfaces_size = 0;
1752
char *interfaces_to_take_down = NULL;
1753
size_t interfaces_to_take_down_size = 0;
1697
1754
char tempdir[] = "/tmp/mandosXXXXXX";
1698
1755
bool tempdir_created = false;
1699
1756
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1700
1757
const char *seckey = PATHDIR "/" SECKEY;
1701
1758
const char *pubkey = PATHDIR "/" PUBKEY;
1759
char *interfaces_hooks = NULL;
1760
size_t interfaces_hooks_size = 0;
1703
1762
bool gnutls_initialized = false;
1704
1763
bool gpgme_initialized = false;
1999
/* Remove empty interface names */
2001
char *interface = NULL;
2002
while((interface = argz_next(interfaces, interfaces_size,
2004
if(if_nametoindex(interface) == 0){
2005
if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2006
fprintf_plus(stderr, "Not using nonexisting interface"
2007
" \"%s\"\n", interface);
2009
argz_delete(&interfaces, &interfaces_size, interface);
1936
2015
/* Run network hooks */
1937
if(not run_network_hooks("start", interface, delay)){
2017
ret_errno = argz_append(&interfaces_hooks, &interfaces_hooks_size,
2018
interfaces, interfaces_size);
2021
perror_plus("argz_append");
2024
argz_stringify(interfaces_hooks, interfaces_hooks_size, (int)',');
2025
if(not run_network_hooks("start", interfaces_hooks, delay)){
1942
2031
avahi_set_log_function(empty_log);
1945
if(interface[0] == '\0'){
1946
struct dirent **direntries;
1947
/* First look for interfaces that are up */
1948
ret = scandir(sys_class_net, &direntries, up_interface,
1951
/* No up interfaces, look for any good interfaces */
1953
ret = scandir(sys_class_net, &direntries, good_interface,
1957
/* Pick the first interface returned */
1958
interface = strdup(direntries[0]->d_name);
1960
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1962
if(interface == NULL){
1963
perror_plus("malloc");
1965
exitcode = EXIT_FAILURE;
1971
fprintf_plus(stderr, "Could not find a network interface\n");
1972
exitcode = EXIT_FAILURE;
1977
2034
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1978
2035
from the signal handler */
1979
2036
/* Initialize the pseudo-RNG for Avahi */
2052
/* If the interface is down, bring it up */
2053
if((interface[0] != '\0') and (strcmp(interface, "none") != 0)){
2054
ret = bring_up_interface(interface, delay);
2057
perror_plus("Failed to bring up interface");
2109
/* If no interfaces were specified, make a list */
2110
if(interfaces == NULL){
2111
struct dirent **direntries;
2112
/* Look for any good interfaces */
2113
ret = scandir(sys_class_net, &direntries, good_interface,
2116
/* Add all found interfaces to interfaces list */
2117
for(int i = 0; i < ret; ++i){
2118
ret_errno = argz_add(&interfaces, &interfaces_size,
2119
direntries[i]->d_name);
2121
perror_plus("argz_add");
2125
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2126
direntries[i]->d_name);
2132
fprintf_plus(stderr, "Could not find a network interface\n");
2133
exitcode = EXIT_FAILURE;
2138
/* If we only got one interface, explicitly use only that one */
2139
if(argz_count(interfaces, interfaces_size) == 1){
2141
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2144
if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2147
/* Bring up interfaces which are down */
2148
if(not (argz_count(interfaces, interfaces_size) == 1
2149
and strcmp(interfaces, "none") == 0)){
2150
char *interface = NULL;
2151
while((interface = argz_next(interfaces, interfaces_size,
2153
bool interface_was_up = interface_is_up(interface);
2154
ret = bring_up_interface(interface, delay);
2155
if(not interface_was_up){
2158
perror_plus("Failed to bring up interface");
2160
ret_errno = argz_add(&interfaces_to_take_down,
2161
&interfaces_to_take_down_size,
2168
interfaces_size = 0;
2169
if(debug and (interfaces_to_take_down == NULL)){
2170
fprintf_plus(stderr, "No interfaces were brought up\n");
2268
/* Run network hooks */
2269
run_network_hooks("stop", interface, delay);
2271
2381
/* Re-raise priviliges */
2273
2383
raise_privileges();
2275
/* Take down the network interface */
2276
if(take_down_interface and geteuid() == 0){
2277
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2279
perror_plus("ioctl SIOCGIFFLAGS");
2280
} else if(network.ifr_flags & IFF_UP){
2281
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2282
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2284
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2385
/* Run network hooks */
2386
run_network_hooks("stop", interfaces_hooks, delay);
2388
/* Take down the network interfaces which were brought up */
2390
char *interface = NULL;
2391
while((interface=argz_next(interfaces_to_take_down,
2392
interfaces_to_take_down_size,
2394
ret_errno = take_down_interface(interface);
2397
perror_plus("Failed to take down interface");
2287
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2289
perror_plus("close");
2400
if(debug and (interfaces_to_take_down == NULL)){
2401
fprintf_plus(stderr, "No interfaces needed to be taken"
2293
/* Lower privileges permanently */
2297
perror_plus("setuid");
2406
lower_privileges_permanently();
2409
free(interfaces_to_take_down);
2410
free(interfaces_hooks);
2300
2412
/* Removes the GPGME temp directory and all files inside */
2301
2413
if(tempdir_created){