733
732
if(if_indextoname((unsigned int)if_index, interface) == NULL){
734
733
perror_plus("if_indextoname");
736
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
737
"\n", ip, interface, (uintmax_t)port);
735
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
736
"\n", ip, interface, port);
740
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
741
ip, (uintmax_t)port);
739
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
743
742
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
744
743
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1034
1031
PRIdMAX ") on port %" PRIu16 "\n", name,
1035
1032
host_name, ip, (intmax_t)interface, port);
1037
int ret = start_mandos_communication(ip, (in_port_t)port,
1034
int ret = start_mandos_communication(ip, port, interface,
1039
1035
avahi_proto_to_af(proto));
1041
1037
avahi_simple_poll_quit(mc.simple_poll);
1043
if(not add_server(ip, (in_port_t)port, interface,
1039
if(not add_server(ip, port, interface,
1044
1040
avahi_proto_to_af(proto))){
1045
1041
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1046
1042
" list\n", name);
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);
1207
* This function determines if a directory entry in /sys/class/net
1208
* corresponds to an acceptable network device which is up.
1209
* (This function is passed to scandir(3) as a filter function.)
1211
int up_interface(const struct dirent *if_entry){
1212
if(if_entry->d_name[0] == '.'){
1217
if(not get_flags(if_entry->d_name, &ifr)){
1219
fprintf_plus(stderr, "Failed to get flags for interface "
1220
"\"%s\"\n", if_entry->d_name);
1225
/* Reject down interfaces */
1226
if(not (ifr.ifr_flags & IFF_UP)){
1228
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1234
/* Reject non-running interfaces */
1235
if(not (ifr.ifr_flags & IFF_RUNNING)){
1237
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1243
if(not good_flags(if_entry->d_name, &ifr)){
1249
1249
int notdotentries(const struct dirent *direntry){
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
1393
bool run_network_hooks(const char *mode, const char *interface,
1446
1394
const float delay){
1447
1395
struct dirent **direntries;
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",
1750
1529
int main(int argc, char *argv[]){
1751
1530
AvahiSServiceBrowser *sb = NULL;
1754
1533
intmax_t tmpmax;
1756
1535
int exitcode = EXIT_SUCCESS;
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;
1536
const char *interface = "";
1537
struct ifreq network;
1539
bool take_down_interface = false;
1761
1542
char tempdir[] = "/tmp/mandosXXXXXX";
1762
1543
bool tempdir_created = false;
1763
1544
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1764
1545
const char *seckey = PATHDIR "/" SECKEY;
1765
1546
const char *pubkey = PATHDIR "/" PUBKEY;
1766
char *interfaces_hooks = NULL;
1767
size_t interfaces_hooks_size = 0;
1769
1548
bool gnutls_initialized = false;
1770
1549
bool gpgme_initialized = false;
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
1785
/* 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)){
1786
if(not run_network_hooks("start", interface, delay)){
2038
1791
avahi_set_log_function(empty_log);
1794
if(interface[0] == '\0'){
1795
struct dirent **direntries;
1796
/* First look for interfaces that are up */
1797
ret = scandir(sys_class_net, &direntries, up_interface,
1800
/* No up interfaces, look for any good interfaces */
1802
ret = scandir(sys_class_net, &direntries, good_interface,
1806
/* Pick the first interface returned */
1807
interface = strdup(direntries[0]->d_name);
1809
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1811
if(interface == NULL){
1812
perror_plus("malloc");
1814
exitcode = EXIT_FAILURE;
1820
fprintf_plus(stderr, "Could not find a network interface\n");
1821
exitcode = EXIT_FAILURE;
2041
1826
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
2042
1827
from the signal handler */
2043
1828
/* Initialize the pseudo-RNG for Avahi */
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);
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");
1901
/* If the interface is down, bring it up */
1902
if(strcmp(interface, "none") != 0){
1903
if_index = (AvahiIfIndex) if_nametoindex(interface);
1905
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1906
exitcode = EX_UNAVAILABLE;
1914
/* Re-raise priviliges */
1918
perror_plus("seteuid");
1922
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1923
messages about the network interface to mess up the prompt */
1924
ret = klogctl(8, NULL, 5);
1925
bool restore_loglevel = true;
1927
restore_loglevel = false;
1928
perror_plus("klogctl");
1930
#endif /* __linux__ */
1932
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1934
perror_plus("socket");
1935
exitcode = EX_OSERR;
1937
if(restore_loglevel){
1938
ret = klogctl(7, NULL, 0);
1940
perror_plus("klogctl");
1943
#endif /* __linux__ */
1944
/* Lower privileges */
1948
perror_plus("seteuid");
1952
strcpy(network.ifr_name, interface);
1953
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1955
perror_plus("ioctl SIOCGIFFLAGS");
1957
if(restore_loglevel){
1958
ret = klogctl(7, NULL, 0);
1960
perror_plus("klogctl");
1963
#endif /* __linux__ */
1964
exitcode = EX_OSERR;
1965
/* Lower privileges */
1969
perror_plus("seteuid");
1973
if((network.ifr_flags & IFF_UP) == 0){
1974
network.ifr_flags |= IFF_UP;
1975
take_down_interface = true;
1976
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1978
take_down_interface = false;
1979
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1980
exitcode = EX_OSERR;
1982
if(restore_loglevel){
1983
ret = klogctl(7, NULL, 0);
1985
perror_plus("klogctl");
1988
#endif /* __linux__ */
1989
/* Lower privileges */
1993
perror_plus("seteuid");
1998
/* Sleep checking until interface is running.
1999
Check every 0.25s, up to total time of delay */
2000
for(int i=0; i < delay * 4; i++){
2001
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2003
perror_plus("ioctl SIOCGIFFLAGS");
2004
} else if(network.ifr_flags & IFF_RUNNING){
2007
struct timespec sleeptime = { .tv_nsec = 250000000 };
2008
ret = nanosleep(&sleeptime, NULL);
2009
if(ret == -1 and errno != EINTR){
2010
perror_plus("nanosleep");
2013
if(not take_down_interface){
2014
/* We won't need the socket anymore */
2015
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2017
perror_plus("close");
2021
if(restore_loglevel){
2022
/* Restores kernel loglevel to default */
2023
ret = klogctl(7, NULL, 0);
2025
perror_plus("klogctl");
2028
#endif /* __linux__ */
2029
/* Lower privileges */
2031
/* Lower privileges */
2034
perror_plus("seteuid");
2244
/* Run network hooks */
2245
run_network_hooks("stop", interface, delay);
2388
2247
/* 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");
2252
perror_plus("seteuid");
2255
/* Take down the network interface */
2256
if(take_down_interface and geteuid() == 0){
2257
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2259
perror_plus("ioctl SIOCGIFFLAGS");
2260
} else if(network.ifr_flags & IFF_UP){
2261
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2262
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2264
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2407
if(debug and (interfaces_to_take_down == NULL)){
2408
fprintf_plus(stderr, "No interfaces needed to be taken"
2267
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2269
perror_plus("close");
2413
lower_privileges_permanently();
2416
free(interfaces_to_take_down);
2417
free(interfaces_hooks);
2273
/* Lower privileges permanently */
2277
perror_plus("setuid");
2419
2280
/* Removes the GPGME temp directory and all files inside */
2420
2281
if(tempdir_created){