780
728
if(if_indextoname((unsigned int)if_index, interface) == NULL){
781
729
perror_plus("if_indextoname");
783
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
784
"\n", ip, interface, (uintmax_t)port);
731
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
732
"\n", ip, interface, port);
787
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
788
ip, (uintmax_t)port);
735
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
790
738
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
791
739
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
792
741
if(af == AF_INET6){
793
ret = getnameinfo((struct sockaddr *)&to,
794
sizeof(struct sockaddr_in6),
795
addrstr, sizeof(addrstr), NULL, 0,
742
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
798
ret = getnameinfo((struct sockaddr *)&to,
799
sizeof(struct sockaddr_in),
800
addrstr, sizeof(addrstr), NULL, 0,
745
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
803
if(ret == EAI_SYSTEM){
804
perror_plus("getnameinfo");
805
} else if(ret != 0) {
806
fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
807
} else if(strcmp(addrstr, ip) != 0){
808
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
749
perror_plus("inet_ntop");
751
if(strcmp(addrstr, ip) != 0){
752
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
1444
1369
ret = avahi_simple_poll_iterate(s, (int)block_time);
1447
if(ret > 0 or errno != EINTR){
1372
if (ret > 0 or errno != EINTR){
1448
1373
return (ret != 1) ? ret : 0;
1454
/* Set effective uid to 0, return errno */
1455
__attribute__((warn_unused_result))
1456
error_t raise_privileges(void){
1457
error_t old_errno = errno;
1458
error_t ret_errno = 0;
1459
if(seteuid(0) == -1){
1461
perror_plus("seteuid");
1467
/* Set effective and real user ID to 0. Return errno. */
1468
__attribute__((warn_unused_result))
1469
error_t raise_privileges_permanently(void){
1470
error_t old_errno = errno;
1471
error_t ret_errno = raise_privileges();
1476
if(setuid(0) == -1){
1478
perror_plus("seteuid");
1484
/* Set effective user ID to unprivileged saved user ID */
1485
__attribute__((warn_unused_result))
1486
error_t lower_privileges(void){
1487
error_t old_errno = errno;
1488
error_t ret_errno = 0;
1489
if(seteuid(uid) == -1){
1491
perror_plus("seteuid");
1497
/* Lower privileges permanently */
1498
__attribute__((warn_unused_result))
1499
error_t lower_privileges_permanently(void){
1500
error_t old_errno = errno;
1501
error_t ret_errno = 0;
1502
if(setuid(uid) == -1){
1504
perror_plus("setuid");
1510
__attribute__((nonnull))
1511
void run_network_hooks(const char *mode, const char *interface,
1379
bool run_network_hooks(const char *mode, const char *interface,
1512
1380
const float delay){
1513
struct dirent **direntries = NULL;
1514
if(hookdir_fd == -1){
1515
hookdir_fd = open(hookdir, O_RDONLY);
1516
if(hookdir_fd == -1){
1517
if(errno == ENOENT){
1519
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1520
" found\n", hookdir);
1523
perror_plus("open");
1529
#if __GLIBC_PREREQ(2, 15)
1530
int numhooks = scandirat(hookdir_fd, ".", &direntries,
1531
runnable_hook, alphasort);
1532
#else /* not __GLIBC_PREREQ(2, 15) */
1533
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1535
#endif /* not __GLIBC_PREREQ(2, 15) */
1536
#else /* not __GLIBC__ */
1537
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1539
#endif /* not __GLIBC__ */
1381
struct dirent **direntries;
1382
struct dirent *direntry;
1384
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1540
1386
if(numhooks == -1){
1541
1387
perror_plus("scandir");
1544
struct dirent *direntry;
1546
int devnull = open("/dev/null", O_RDONLY);
1547
for(int i = 0; i < numhooks; i++){
1548
direntry = direntries[i];
1550
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1553
pid_t hook_pid = fork();
1556
/* Raise privileges */
1557
if(raise_privileges_permanently() != 0){
1558
perror_plus("Failed to raise privileges");
1565
perror_plus("setgid");
1568
/* Reset supplementary groups */
1570
ret = setgroups(0, NULL);
1572
perror_plus("setgroups");
1575
ret = dup2(devnull, STDIN_FILENO);
1577
perror_plus("dup2(devnull, STDIN_FILENO)");
1580
ret = close(devnull);
1582
perror_plus("close");
1585
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1587
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1590
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1592
perror_plus("setenv");
1595
ret = setenv("DEVICE", interface, 1);
1597
perror_plus("setenv");
1600
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1602
perror_plus("setenv");
1605
ret = setenv("MODE", mode, 1);
1607
perror_plus("setenv");
1611
ret = asprintf(&delaystring, "%f", (double)delay);
1389
int devnull = open("/dev/null", O_RDONLY);
1390
for(int i = 0; i < numhooks; i++){
1391
direntry = direntries[0];
1392
char *fullname = NULL;
1393
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1613
1395
perror_plus("asprintf");
1616
ret = setenv("DELAY", delaystring, 1);
1398
pid_t hook_pid = fork();
1401
dup2(devnull, STDIN_FILENO);
1403
dup2(STDERR_FILENO, STDOUT_FILENO);
1404
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1406
perror_plus("setenv");
1409
ret = setenv("DEVICE", interface, 1);
1411
perror_plus("setenv");
1414
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1416
perror_plus("setenv");
1419
ret = setenv("MODE", mode, 1);
1421
perror_plus("setenv");
1425
ret = asprintf(&delaystring, "%f", delay);
1427
perror_plus("asprintf");
1430
ret = setenv("DELAY", delaystring, 1);
1433
perror_plus("setenv");
1618
1436
free(delaystring);
1619
perror_plus("setenv");
1623
if(connect_to != NULL){
1624
ret = setenv("CONNECT", connect_to, 1);
1626
perror_plus("setenv");
1630
int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1632
perror_plus("openat");
1633
_exit(EXIT_FAILURE);
1635
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1636
perror_plus("close");
1637
_exit(EXIT_FAILURE);
1639
if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1641
perror_plus("fexecve");
1642
_exit(EXIT_FAILURE);
1646
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1647
perror_plus("waitpid");
1650
if(WIFEXITED(status)){
1651
if(WEXITSTATUS(status) != 0){
1652
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1653
" with status %d\n", direntry->d_name,
1654
WEXITSTATUS(status));
1657
} else if(WIFSIGNALED(status)){
1658
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1659
" signal %d\n", direntry->d_name,
1663
fprintf_plus(stderr, "Warning: network hook \"%s\""
1664
" crashed\n", direntry->d_name);
1669
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1674
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1675
perror_plus("close");
1682
__attribute__((nonnull, warn_unused_result))
1683
error_t bring_up_interface(const char *const interface,
1685
error_t old_errno = errno;
1687
struct ifreq network;
1688
unsigned int if_index = if_nametoindex(interface);
1690
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1700
if(not interface_is_up(interface)){
1701
error_t ret_errno = 0, ioctl_errno = 0;
1702
if(not get_flags(interface, &network)){
1704
fprintf_plus(stderr, "Failed to get flags for interface "
1705
"\"%s\"\n", interface);
1709
network.ifr_flags |= IFF_UP; /* set flag */
1711
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1714
perror_plus("socket");
1720
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1722
perror_plus("close");
1729
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1733
/* Raise privileges */
1734
ret_errno = raise_privileges();
1736
perror_plus("Failed to raise privileges");
1741
bool restore_loglevel = false;
1743
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1744
messages about the network interface to mess up the prompt */
1745
ret_linux = klogctl(8, NULL, 5);
1746
if(ret_linux == -1){
1747
perror_plus("klogctl");
1749
restore_loglevel = true;
1752
#endif /* __linux__ */
1753
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1754
ioctl_errno = errno;
1756
if(restore_loglevel){
1757
ret_linux = klogctl(7, NULL, 0);
1758
if(ret_linux == -1){
1759
perror_plus("klogctl");
1762
#endif /* __linux__ */
1764
/* If raise_privileges() succeeded above */
1766
/* Lower privileges */
1767
ret_errno = lower_privileges();
1770
perror_plus("Failed to lower privileges");
1774
/* Close the socket */
1775
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1777
perror_plus("close");
1780
if(ret_setflags == -1){
1781
errno = ioctl_errno;
1782
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1787
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1791
/* Sleep checking until interface is running.
1792
Check every 0.25s, up to total time of delay */
1793
for(int i=0; i < delay * 4; i++){
1794
if(interface_is_running(interface)){
1797
struct timespec sleeptime = { .tv_nsec = 250000000 };
1798
ret = nanosleep(&sleeptime, NULL);
1799
if(ret == -1 and errno != EINTR){
1800
perror_plus("nanosleep");
1808
__attribute__((nonnull, warn_unused_result))
1809
error_t take_down_interface(const char *const interface){
1810
error_t old_errno = errno;
1811
struct ifreq network;
1812
unsigned int if_index = if_nametoindex(interface);
1814
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1818
if(interface_is_up(interface)){
1819
error_t ret_errno = 0, ioctl_errno = 0;
1820
if(not get_flags(interface, &network) and debug){
1822
fprintf_plus(stderr, "Failed to get flags for interface "
1823
"\"%s\"\n", interface);
1827
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1829
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1832
perror_plus("socket");
1838
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1842
/* Raise privileges */
1843
ret_errno = raise_privileges();
1845
perror_plus("Failed to raise privileges");
1848
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1849
ioctl_errno = errno;
1851
/* If raise_privileges() succeeded above */
1853
/* Lower privileges */
1854
ret_errno = lower_privileges();
1857
perror_plus("Failed to lower privileges");
1861
/* Close the socket */
1862
int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1864
perror_plus("close");
1867
if(ret_setflags == -1){
1868
errno = ioctl_errno;
1869
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1874
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1437
ret = execl(fullname, direntry->d_name, mode, NULL);
1438
perror_plus("execl");
1441
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1442
perror_plus("waitpid");
1446
if(WIFEXITED(status)){
1447
if(WEXITSTATUS(status) != 0){
1448
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1449
" with status %d\n", direntry->d_name,
1450
WEXITSTATUS(status));
1454
} else if(WIFSIGNALED(status)){
1455
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1456
" signal %d\n", direntry->d_name,
1461
fprintf_plus(stderr, "Warning: network hook \"%s\""
1462
" crashed\n", direntry->d_name);
1882
1477
int main(int argc, char *argv[]){
1883
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1884
.priority = "SECURE256:!CTYPE-X.509:"
1885
"+CTYPE-OPENPGP", .current_server = NULL,
1886
.interfaces = NULL, .interfaces_size = 0 };
1887
1478
AvahiSServiceBrowser *sb = NULL;
1890
1481
intmax_t tmpmax;
1892
1483
int exitcode = EXIT_SUCCESS;
1893
char *interfaces_to_take_down = NULL;
1894
size_t interfaces_to_take_down_size = 0;
1895
char run_tempdir[] = "/run/tmp/mandosXXXXXX";
1896
char old_tempdir[] = "/tmp/mandosXXXXXX";
1897
char *tempdir = NULL;
1484
const char *interface = "";
1485
struct ifreq network;
1487
bool take_down_interface = false;
1490
char tempdir[] = "/tmp/mandosXXXXXX";
1491
bool tempdir_created = false;
1898
1492
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1899
1493
const char *seckey = PATHDIR "/" SECKEY;
1900
1494
const char *pubkey = PATHDIR "/" PUBKEY;
1901
char *interfaces_hooks = NULL;
1903
1496
bool gnutls_initialized = false;
1904
1497
bool gpgme_initialized = false;
2082
1673
/* Work around Debian bug #633582:
2083
1674
<http://bugs.debian.org/633582> */
2085
/* Re-raise privileges */
2086
ret_errno = raise_privileges();
2089
perror_plus("Failed to raise privileges");
2093
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2094
int seckey_fd = open(seckey, O_RDONLY);
2095
if(seckey_fd == -1){
2096
perror_plus("open");
2098
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
2100
perror_plus("fstat");
2102
if(S_ISREG(st.st_mode)
2103
and st.st_uid == 0 and st.st_gid == 0){
2104
ret = fchown(seckey_fd, uid, gid);
2106
perror_plus("fchown");
2110
TEMP_FAILURE_RETRY(close(seckey_fd));
2114
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
2115
int pubkey_fd = open(pubkey, O_RDONLY);
2116
if(pubkey_fd == -1){
2117
perror_plus("open");
2119
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
2121
perror_plus("fstat");
2123
if(S_ISREG(st.st_mode)
2124
and st.st_uid == 0 and st.st_gid == 0){
2125
ret = fchown(pubkey_fd, uid, gid);
2127
perror_plus("fchown");
2131
TEMP_FAILURE_RETRY(close(pubkey_fd));
2135
/* Lower privileges */
2136
ret_errno = lower_privileges();
2139
perror_plus("Failed to lower privileges");
2144
/* Remove invalid interface names (except "none") */
2146
char *interface = NULL;
2147
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2149
if(strcmp(interface, "none") != 0
2150
and if_nametoindex(interface) == 0){
2151
if(interface[0] != '\0'){
2152
fprintf_plus(stderr, "Not using nonexisting interface"
2153
" \"%s\"\n", interface);
2155
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
1677
/* Re-raise priviliges */
1681
perror_plus("seteuid");
1684
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1685
int seckey_fd = open(seckey, O_RDONLY);
1686
if(seckey_fd == -1){
1687
perror_plus("open");
1689
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1691
perror_plus("fstat");
1693
if(S_ISREG(st.st_mode)
1694
and st.st_uid == 0 and st.st_gid == 0){
1695
ret = fchown(seckey_fd, uid, gid);
1697
perror_plus("fchown");
1701
TEMP_FAILURE_RETRY(close(seckey_fd));
1705
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1706
int pubkey_fd = open(pubkey, O_RDONLY);
1707
if(pubkey_fd == -1){
1708
perror_plus("open");
1710
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1712
perror_plus("fstat");
1714
if(S_ISREG(st.st_mode)
1715
and st.st_uid == 0 and st.st_gid == 0){
1716
ret = fchown(pubkey_fd, uid, gid);
1718
perror_plus("fchown");
1722
TEMP_FAILURE_RETRY(close(pubkey_fd));
1726
/* Lower privileges */
1730
perror_plus("seteuid");
2161
1734
/* Run network hooks */
2163
if(mc.interfaces != NULL){
2164
interfaces_hooks = malloc(mc.interfaces_size);
2165
if(interfaces_hooks == NULL){
2166
perror_plus("malloc");
2169
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2170
argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2172
run_network_hooks("start", interfaces_hooks != NULL ?
2173
interfaces_hooks : "", delay);
1736
/* Re-raise priviliges */
1740
perror_plus("seteuid");
1742
if(not run_network_hooks("start", interface, delay)){
1745
/* Lower privileges */
1749
perror_plus("seteuid");
2177
1754
avahi_set_log_function(empty_log);
1757
if(interface[0] == '\0'){
1758
struct dirent **direntries;
1759
/* First look for interfaces that are up */
1760
ret = scandir(sys_class_net, &direntries, up_interface,
1763
/* No up interfaces, look for any good interfaces */
1765
ret = scandir(sys_class_net, &direntries, good_interface,
1769
/* Pick the first interface returned */
1770
interface = strdup(direntries[0]->d_name);
1772
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1774
if(interface == NULL){
1775
perror_plus("malloc");
1777
exitcode = EXIT_FAILURE;
1783
fprintf_plus(stderr, "Could not find a network interface\n");
1784
exitcode = EXIT_FAILURE;
2180
1789
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
2181
1790
from the signal handler */
2182
1791
/* Initialize the pseudo-RNG for Avahi */
2183
1792
srand((unsigned int) time(NULL));
2184
simple_poll = avahi_simple_poll_new();
2185
if(simple_poll == NULL){
1793
mc.simple_poll = avahi_simple_poll_new();
1794
if(mc.simple_poll == NULL){
2186
1795
fprintf_plus(stderr,
2187
1796
"Avahi: Failed to create simple poll object.\n");
2188
1797
exitcode = EX_UNAVAILABLE;
2255
/* If no interfaces were specified, make a list */
2256
if(mc.interfaces == NULL){
2257
struct dirent **direntries = NULL;
2258
/* Look for any good interfaces */
2259
ret = scandir(sys_class_net, &direntries, good_interface,
2262
/* Add all found interfaces to interfaces list */
2263
for(int i = 0; i < ret; ++i){
2264
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2265
direntries[i]->d_name);
2268
perror_plus("argz_add");
2272
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2273
direntries[i]->d_name);
2281
fprintf_plus(stderr, "Could not find a network interface\n");
2282
exitcode = EXIT_FAILURE;
2287
/* Bring up interfaces which are down, and remove any "none"s */
2289
char *interface = NULL;
2290
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2292
/* If interface name is "none", stop bringing up interfaces.
2293
Also remove all instances of "none" from the list */
2294
if(strcmp(interface, "none") == 0){
2295
argz_delete(&mc.interfaces, &mc.interfaces_size,
2298
while((interface = argz_next(mc.interfaces,
2299
mc.interfaces_size, interface))){
2300
if(strcmp(interface, "none") == 0){
2301
argz_delete(&mc.interfaces, &mc.interfaces_size,
1864
/* If the interface is down, bring it up */
1865
if(strcmp(interface, "none") != 0){
1866
if_index = (AvahiIfIndex) if_nametoindex(interface);
1868
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1869
exitcode = EX_UNAVAILABLE;
1877
/* Re-raise priviliges */
1881
perror_plus("seteuid");
1885
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1886
messages about the network interface to mess up the prompt */
1887
ret = klogctl(8, NULL, 5);
1888
bool restore_loglevel = true;
1890
restore_loglevel = false;
1891
perror_plus("klogctl");
1893
#endif /* __linux__ */
1895
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1897
perror_plus("socket");
1898
exitcode = EX_OSERR;
1900
if(restore_loglevel){
1901
ret = klogctl(7, NULL, 0);
1903
perror_plus("klogctl");
1906
#endif /* __linux__ */
1907
/* Lower privileges */
1911
perror_plus("seteuid");
1915
strcpy(network.ifr_name, interface);
1916
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1918
perror_plus("ioctl SIOCGIFFLAGS");
1920
if(restore_loglevel){
1921
ret = klogctl(7, NULL, 0);
1923
perror_plus("klogctl");
1926
#endif /* __linux__ */
1927
exitcode = EX_OSERR;
1928
/* Lower privileges */
1932
perror_plus("seteuid");
1936
if((network.ifr_flags & IFF_UP) == 0){
1937
network.ifr_flags |= IFF_UP;
1938
take_down_interface = true;
1939
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1941
take_down_interface = false;
1942
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1943
exitcode = EX_OSERR;
1945
if(restore_loglevel){
1946
ret = klogctl(7, NULL, 0);
1948
perror_plus("klogctl");
1951
#endif /* __linux__ */
1952
/* Lower privileges */
1956
perror_plus("seteuid");
1961
/* Sleep checking until interface is running.
1962
Check every 0.25s, up to total time of delay */
1963
for(int i=0; i < delay * 4; i++){
1964
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1966
perror_plus("ioctl SIOCGIFFLAGS");
1967
} else if(network.ifr_flags & IFF_RUNNING){
2308
bool interface_was_up = interface_is_up(interface);
2309
errno = bring_up_interface(interface, delay);
2310
if(not interface_was_up){
2312
perror_plus("Failed to bring up interface");
2314
errno = argz_add(&interfaces_to_take_down,
2315
&interfaces_to_take_down_size,
2318
perror_plus("argz_add");
2323
if(debug and (interfaces_to_take_down == NULL)){
2324
fprintf_plus(stderr, "No interfaces were brought up\n");
2328
/* If we only got one interface, explicitly use only that one */
2329
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2331
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2334
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
1970
struct timespec sleeptime = { .tv_nsec = 250000000 };
1971
ret = nanosleep(&sleeptime, NULL);
1972
if(ret == -1 and errno != EINTR){
1973
perror_plus("nanosleep");
1976
if(not take_down_interface){
1977
/* We won't need the socket anymore */
1978
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1980
perror_plus("close");
1984
if(restore_loglevel){
1985
/* Restores kernel loglevel to default */
1986
ret = klogctl(7, NULL, 0);
1988
perror_plus("klogctl");
1991
#endif /* __linux__ */
1992
/* Lower privileges */
1994
/* Lower privileges */
1997
perror_plus("seteuid");
2341
ret = init_gnutls_global(pubkey, seckey, &mc);
2005
ret = init_gnutls_global(pubkey, seckey);
2343
2007
fprintf_plus(stderr, "init_gnutls_global failed\n");
2344
2008
exitcode = EX_UNAVAILABLE;
2555
/* Re-raise privileges */
2207
/* Re-raise priviliges */
2557
ret_errno = raise_privileges();
2559
perror_plus("Failed to raise privileges");
2562
/* Run network hooks */
2563
run_network_hooks("stop", interfaces_hooks != NULL ?
2564
interfaces_hooks : "", delay);
2566
/* Take down the network interfaces which were brought up */
2568
char *interface = NULL;
2569
while((interface=argz_next(interfaces_to_take_down,
2570
interfaces_to_take_down_size,
2572
ret_errno = take_down_interface(interface);
2575
perror_plus("Failed to take down interface");
2578
if(debug and (interfaces_to_take_down == NULL)){
2579
fprintf_plus(stderr, "No interfaces needed to be taken"
2212
perror_plus("seteuid");
2214
/* Run network hooks */
2215
if(not run_network_hooks("stop", interface, delay)){
2585
ret_errno = lower_privileges_permanently();
2587
perror_plus("Failed to lower privileges permanently");
2219
/* Take down the network interface */
2220
if(take_down_interface and geteuid() == 0){
2221
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2223
perror_plus("ioctl SIOCGIFFLAGS");
2224
} else if(network.ifr_flags & IFF_UP){
2225
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2226
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2228
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2231
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2233
perror_plus("close");
2591
free(interfaces_to_take_down);
2592
free(interfaces_hooks);
2237
/* Lower privileges permanently */
2241
perror_plus("setuid");
2594
2244
/* Removes the GPGME temp directory and all files inside */
2595
if(tempdir != NULL){
2245
if(tempdir_created){
2596
2246
struct dirent **direntries = NULL;
2597
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2599
if(tempdir_fd == -1){
2600
perror_plus("open");
2603
#if __GLIBC_PREREQ(2, 15)
2604
int numentries = scandirat(tempdir_fd, ".", &direntries,
2605
notdotentries, alphasort);
2606
#else /* not __GLIBC_PREREQ(2, 15) */
2607
int numentries = scandir(tempdir, &direntries, notdotentries,
2609
#endif /* not __GLIBC_PREREQ(2, 15) */
2610
#else /* not __GLIBC__ */
2611
int numentries = scandir(tempdir, &direntries, notdotentries,
2613
#endif /* not __GLIBC__ */
2614
if(numentries >= 0){
2615
for(int i = 0; i < numentries; i++){
2616
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2618
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2619
" \"%s\", 0): %s\n", tempdir,
2620
direntries[i]->d_name, strerror(errno));
2624
/* need to clean even if 0 because man page doesn't specify */
2626
if(numentries == -1){
2627
perror_plus("scandir");
2629
ret = rmdir(tempdir);
2630
if(ret == -1 and errno != ENOENT){
2631
perror_plus("rmdir");
2247
struct dirent *direntry = NULL;
2248
int numentries = scandir(tempdir, &direntries, notdotentries,
2250
if (numentries > 0){
2251
for(int i = 0; i < numentries; i++){
2252
direntry = direntries[i];
2253
char *fullname = NULL;
2254
ret = asprintf(&fullname, "%s/%s", tempdir,
2257
perror_plus("asprintf");
2260
ret = remove(fullname);
2262
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2634
TEMP_FAILURE_RETRY(close(tempdir_fd));
2269
/* need to clean even if 0 because man page doesn't specify */
2271
if (numentries == -1){
2272
perror_plus("scandir");
2274
ret = rmdir(tempdir);
2275
if(ret == -1 and errno != ENOENT){
2276
perror_plus("rmdir");