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){
1561
int bring_up_interface(const char *const interface,
1571
error_t bring_up_interface(const char *const interface,
1564
int old_errno = errno;
1574
error_t old_errno = errno;
1575
error_t ret_errno = 0;
1576
int ret, ret_setflags;
1567
1577
struct ifreq network;
1568
AvahiIfIndex if_index = (AvahiIfIndex)if_nametoindex(interface);
1578
unsigned int if_index = if_nametoindex(interface);
1569
1579
if(if_index == 0){
1570
1580
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1571
1581
errno = old_errno;
1580
/* Re-raise priviliges */
1584
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1585
messages about the network interface to mess up the prompt */
1586
ret = klogctl(8, NULL, 5);
1587
bool restore_loglevel = true;
1589
restore_loglevel = false;
1590
perror_plus("klogctl");
1592
#endif /* __linux__ */
1594
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1597
perror_plus("socket");
1599
if(restore_loglevel){
1600
ret = klogctl(7, NULL, 0);
1602
perror_plus("klogctl");
1605
#endif /* __linux__ */
1606
/* Lower privileges */
1611
strcpy(network.ifr_name, interface);
1612
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1615
perror_plus("ioctl SIOCGIFFLAGS");
1617
if(restore_loglevel){
1618
ret = klogctl(7, NULL, 0);
1620
perror_plus("klogctl");
1623
#endif /* __linux__ */
1624
/* Lower privileges */
1629
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);
1630
1597
network.ifr_flags |= IFF_UP;
1631
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){
1634
1653
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1636
if(restore_loglevel){
1637
ret = klogctl(7, NULL, 0);
1639
perror_plus("klogctl");
1642
#endif /* __linux__ */
1643
/* Lower privileges */
1645
1654
errno = old_errno;
1646
1655
return ret_errno;
1658
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1649
1662
/* Sleep checking until interface is running.
1650
1663
Check every 0.25s, up to total time of delay */
1651
1664
for(int i=0; i < delay * 4; i++){
1652
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1654
perror_plus("ioctl SIOCGIFFLAGS");
1655
} else if(network.ifr_flags & IFF_RUNNING){
1665
if(interface_is_running(interface)){
1658
1668
struct timespec sleeptime = { .tv_nsec = 250000000 };
1661
1671
perror_plus("nanosleep");
1664
/* Close the socket */
1665
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1667
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);
1670
if(restore_loglevel){
1671
/* Restores kernel loglevel to default */
1672
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));
1674
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",
1677
#endif /* __linux__ */
1678
/* Lower privileges */
1680
1739
errno = old_errno;
1684
1743
int main(int argc, char *argv[]){
1685
1744
AvahiSServiceBrowser *sb = NULL;
1688
1747
intmax_t tmpmax;
1690
1749
int exitcode = EXIT_SUCCESS;
1691
const char *interface = "";
1692
struct ifreq network;
1694
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;
1695
1754
char tempdir[] = "/tmp/mandosXXXXXX";
1696
1755
bool tempdir_created = false;
1697
1756
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1698
1757
const char *seckey = PATHDIR "/" SECKEY;
1699
1758
const char *pubkey = PATHDIR "/" PUBKEY;
1759
char *interfaces_hooks = NULL;
1760
size_t interfaces_hooks_size = 0;
1701
1762
bool gnutls_initialized = false;
1702
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);
1934
2015
/* Run network hooks */
1935
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)){
1940
2031
avahi_set_log_function(empty_log);
1943
if(interface[0] == '\0'){
1944
struct dirent **direntries;
1945
/* First look for interfaces that are up */
1946
ret = scandir(sys_class_net, &direntries, up_interface,
1949
/* No up interfaces, look for any good interfaces */
1951
ret = scandir(sys_class_net, &direntries, good_interface,
1955
/* Pick the first interface returned */
1956
interface = strdup(direntries[0]->d_name);
1958
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1960
if(interface == NULL){
1961
perror_plus("malloc");
1963
exitcode = EXIT_FAILURE;
1969
fprintf_plus(stderr, "Could not find a network interface\n");
1970
exitcode = EXIT_FAILURE;
1975
2034
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1976
2035
from the signal handler */
1977
2036
/* Initialize the pseudo-RNG for Avahi */
2050
/* If the interface is down, bring it up */
2051
if((interface[0] != '\0') and (strcmp(interface, "none") != 0)){
2052
ret = bring_up_interface(interface, delay);
2055
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");
2266
/* Run network hooks */
2267
run_network_hooks("stop", interface, delay);
2269
2381
/* Re-raise priviliges */
2271
2383
raise_privileges();
2273
/* Take down the network interface */
2274
if(take_down_interface and geteuid() == 0){
2275
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2277
perror_plus("ioctl SIOCGIFFLAGS");
2278
} else if(network.ifr_flags & IFF_UP){
2279
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2280
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2282
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");
2285
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2287
perror_plus("close");
2400
if(debug and (interfaces_to_take_down == NULL)){
2401
fprintf_plus(stderr, "No interfaces needed to be taken"
2291
/* Lower privileges permanently */
2295
perror_plus("setuid");
2406
lower_privileges_permanently();
2409
free(interfaces_to_take_down);
2410
free(interfaces_hooks);
2298
2412
/* Removes the GPGME temp directory and all files inside */
2299
2413
if(tempdir_created){