732
733
if(if_indextoname((unsigned int)if_index, interface) == NULL){
733
734
perror_plus("if_indextoname");
735
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
736
"\n", ip, interface, port);
736
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
737
"\n", ip, interface, (uintmax_t)port);
739
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
740
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
741
ip, (uintmax_t)port);
742
743
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
743
744
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1028
1034
PRIdMAX ") on port %" PRIu16 "\n", name,
1029
1035
host_name, ip, (intmax_t)interface, port);
1031
int ret = start_mandos_communication(ip, port, interface,
1037
int ret = start_mandos_communication(ip, (in_port_t)port,
1032
1039
avahi_proto_to_af(proto));
1034
1041
avahi_simple_poll_quit(mc.simple_poll);
1036
if(not add_server(ip, port, interface,
1043
if(not add_server(ip, (in_port_t)port, interface,
1037
1044
avahi_proto_to_af(proto))){
1038
1045
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1039
1046
" list\n", name);
1204
* This function determines if a directory entry in /sys/class/net
1205
* corresponds to an acceptable network device which is up.
1206
* (This function is passed to scandir(3) as a filter function.)
1208
int up_interface(const struct dirent *if_entry){
1209
if(if_entry->d_name[0] == '.'){
1214
if(not get_flags(if_entry->d_name, &ifr)){
1216
fprintf_plus(stderr, "Failed to get flags for interface "
1217
"\"%s\"\n", if_entry->d_name);
1222
/* Reject down interfaces */
1223
if(not (ifr.ifr_flags & IFF_UP)){
1225
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1231
/* Reject non-running interfaces */
1232
if(not (ifr.ifr_flags & IFF_RUNNING)){
1234
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1240
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);
1246
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");
1390
1445
bool run_network_hooks(const char *mode, const char *interface,
1391
1446
const float delay){
1392
1447
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",
1526
1750
int main(int argc, char *argv[]){
1527
1751
AvahiSServiceBrowser *sb = NULL;
1530
1754
intmax_t tmpmax;
1532
1756
int exitcode = EXIT_SUCCESS;
1533
const char *interface = "";
1534
struct ifreq network;
1536
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;
1539
1761
char tempdir[] = "/tmp/mandosXXXXXX";
1540
1762
bool tempdir_created = false;
1541
1763
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1542
1764
const char *seckey = PATHDIR "/" SECKEY;
1543
1765
const char *pubkey = PATHDIR "/" PUBKEY;
1766
char *interfaces_hooks = NULL;
1767
size_t interfaces_hooks_size = 0;
1545
1769
bool gnutls_initialized = false;
1546
1770
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);
1782
2022
/* Run network hooks */
1783
if(not run_network_hooks("start", interface, delay)){
2025
if(interfaces != NULL){
2026
interfaces_hooks = malloc(interfaces_size);
2027
if(interfaces_hooks == NULL){
2028
perror_plus("malloc");
2031
memcpy(interfaces_hooks, interfaces, interfaces_size);
2032
interfaces_hooks_size = interfaces_size;
2033
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2036
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2037
interfaces_hooks : "", delay)){
1788
2043
avahi_set_log_function(empty_log);
1791
if(interface[0] == '\0'){
1792
struct dirent **direntries;
1793
/* First look for interfaces that are up */
1794
ret = scandir(sys_class_net, &direntries, up_interface,
1797
/* No up interfaces, look for any good interfaces */
1799
ret = scandir(sys_class_net, &direntries, good_interface,
1803
/* Pick the first interface returned */
1804
interface = strdup(direntries[0]->d_name);
1806
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1808
if(interface == NULL){
1809
perror_plus("malloc");
1811
exitcode = EXIT_FAILURE;
1817
fprintf_plus(stderr, "Could not find a network interface\n");
1818
exitcode = EXIT_FAILURE;
1823
2046
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1824
2047
from the signal handler */
1825
2048
/* Initialize the pseudo-RNG for Avahi */
1898
/* If the interface is down, bring it up */
1899
if(strcmp(interface, "none") != 0){
1900
if_index = (AvahiIfIndex) if_nametoindex(interface);
1902
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1903
exitcode = EX_UNAVAILABLE;
1911
/* Re-raise priviliges */
1915
perror_plus("seteuid");
1919
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1920
messages about the network interface to mess up the prompt */
1921
ret = klogctl(8, NULL, 5);
1922
bool restore_loglevel = true;
1924
restore_loglevel = false;
1925
perror_plus("klogctl");
1927
#endif /* __linux__ */
1929
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1931
perror_plus("socket");
1932
exitcode = EX_OSERR;
1934
if(restore_loglevel){
1935
ret = klogctl(7, NULL, 0);
1937
perror_plus("klogctl");
1940
#endif /* __linux__ */
1941
/* Lower privileges */
1945
perror_plus("seteuid");
1949
strcpy(network.ifr_name, interface);
1950
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1952
perror_plus("ioctl SIOCGIFFLAGS");
1954
if(restore_loglevel){
1955
ret = klogctl(7, NULL, 0);
1957
perror_plus("klogctl");
1960
#endif /* __linux__ */
1961
exitcode = EX_OSERR;
1962
/* Lower privileges */
1966
perror_plus("seteuid");
1970
if((network.ifr_flags & IFF_UP) == 0){
1971
network.ifr_flags |= IFF_UP;
1972
take_down_interface = true;
1973
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1975
take_down_interface = false;
1976
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1977
exitcode = EX_OSERR;
1979
if(restore_loglevel){
1980
ret = klogctl(7, NULL, 0);
1982
perror_plus("klogctl");
1985
#endif /* __linux__ */
1986
/* Lower privileges */
1990
perror_plus("seteuid");
1995
/* Sleep checking until interface is running.
1996
Check every 0.25s, up to total time of delay */
1997
for(int i=0; i < delay * 4; i++){
1998
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2000
perror_plus("ioctl SIOCGIFFLAGS");
2001
} else if(network.ifr_flags & IFF_RUNNING){
2004
struct timespec sleeptime = { .tv_nsec = 250000000 };
2005
ret = nanosleep(&sleeptime, NULL);
2006
if(ret == -1 and errno != EINTR){
2007
perror_plus("nanosleep");
2010
if(not take_down_interface){
2011
/* We won't need the socket anymore */
2012
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2014
perror_plus("close");
2018
if(restore_loglevel){
2019
/* Restores kernel loglevel to default */
2020
ret = klogctl(7, NULL, 0);
2022
perror_plus("klogctl");
2025
#endif /* __linux__ */
2026
/* Lower privileges */
2028
/* Lower privileges */
2031
perror_plus("seteuid");
2121
/* If no interfaces were specified, make a list */
2122
if(interfaces == NULL){
2123
struct dirent **direntries;
2124
/* Look for any good interfaces */
2125
ret = scandir(sys_class_net, &direntries, good_interface,
2128
/* Add all found interfaces to interfaces list */
2129
for(int i = 0; i < ret; ++i){
2130
ret_errno = argz_add(&interfaces, &interfaces_size,
2131
direntries[i]->d_name);
2133
perror_plus("argz_add");
2137
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2138
direntries[i]->d_name);
2144
fprintf_plus(stderr, "Could not find a network interface\n");
2145
exitcode = EXIT_FAILURE;
2150
/* If we only got one interface, explicitly use only that one */
2151
if(argz_count(interfaces, interfaces_size) == 1){
2153
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2156
if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2159
/* Bring up interfaces which are down */
2160
if(not (argz_count(interfaces, interfaces_size) == 1
2161
and strcmp(interfaces, "none") == 0)){
2162
char *interface = NULL;
2163
while((interface = argz_next(interfaces, interfaces_size,
2165
bool interface_was_up = interface_is_up(interface);
2166
ret = bring_up_interface(interface, delay);
2167
if(not interface_was_up){
2170
perror_plus("Failed to bring up interface");
2172
ret_errno = argz_add(&interfaces_to_take_down,
2173
&interfaces_to_take_down_size,
2180
interfaces_size = 0;
2181
if(debug and (interfaces_to_take_down == NULL)){
2182
fprintf_plus(stderr, "No interfaces were brought up\n");
2241
/* Run network hooks */
2242
run_network_hooks("stop", interface, delay);
2244
2393
/* Re-raise priviliges */
2249
perror_plus("seteuid");
2252
/* Take down the network interface */
2253
if(take_down_interface and geteuid() == 0){
2254
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2256
perror_plus("ioctl SIOCGIFFLAGS");
2257
} else if(network.ifr_flags & IFF_UP){
2258
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2259
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2261
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2397
/* Run network hooks */
2398
run_network_hooks("stop", interfaces_hooks != NULL ?
2399
interfaces_hooks : "", delay);
2401
/* Take down the network interfaces which were brought up */
2403
char *interface = NULL;
2404
while((interface=argz_next(interfaces_to_take_down,
2405
interfaces_to_take_down_size,
2407
ret_errno = take_down_interface(interface);
2410
perror_plus("Failed to take down interface");
2264
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2266
perror_plus("close");
2413
if(debug and (interfaces_to_take_down == NULL)){
2414
fprintf_plus(stderr, "No interfaces needed to be taken"
2270
/* Lower privileges permanently */
2274
perror_plus("setuid");
2419
lower_privileges_permanently();
2422
free(interfaces_to_take_down);
2423
free(interfaces_hooks);
2277
2425
/* Removes the GPGME temp directory and all files inside */
2278
2426
if(tempdir_created){