650
616
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
651
617
__attribute__((unused)) const char *txt){}
653
/* Set effective uid to 0, return errno */
654
__attribute__((warn_unused_result))
655
error_t raise_privileges(void){
656
error_t old_errno = errno;
657
error_t ret_errno = 0;
658
if(seteuid(0) == -1){
665
/* Set effective and real user ID to 0. Return errno. */
666
__attribute__((warn_unused_result))
667
error_t raise_privileges_permanently(void){
668
error_t old_errno = errno;
669
error_t ret_errno = raise_privileges();
681
/* Set effective user ID to unprivileged saved user ID */
682
__attribute__((warn_unused_result))
683
error_t lower_privileges(void){
684
error_t old_errno = errno;
685
error_t ret_errno = 0;
686
if(seteuid(uid) == -1){
693
/* Lower privileges permanently */
694
__attribute__((warn_unused_result))
695
error_t lower_privileges_permanently(void){
696
error_t old_errno = errno;
697
error_t ret_errno = 0;
698
if(setuid(uid) == -1){
705
/* Helper function to add_local_route() and remove_local_route() */
706
__attribute__((nonnull, warn_unused_result))
707
static bool add_remove_local_route(const bool add,
709
AvahiIfIndex if_index){
711
char helper[] = "mandos-client-iprouteadddel";
712
char add_arg[] = "add";
713
char delete_arg[] = "delete";
714
char *pluginhelperdir = getenv("MANDOSPLUGINHELPERDIR");
715
if(pluginhelperdir == NULL){
717
fprintf_plus(stderr, "MANDOSPLUGINHELPERDIR environment"
718
" variable not set; cannot run helper\n");
723
char interface[IF_NAMESIZE];
724
if(if_indextoname((unsigned int)if_index, interface) == NULL){
725
perror_plus("if_indextoname");
729
int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
731
perror_plus("open(\"/dev/null\", O_RDONLY)");
737
/* Raise privileges */
738
errno = raise_privileges_permanently();
740
perror_plus("Failed to raise privileges");
741
/* _exit(EX_NOPERM); */
747
perror_plus("setgid");
750
/* Reset supplementary groups */
752
ret = setgroups(0, NULL);
754
perror_plus("setgroups");
758
ret = dup2(devnull, STDIN_FILENO);
760
perror_plus("dup2(devnull, STDIN_FILENO)");
763
ret = (int)TEMP_FAILURE_RETRY(close(devnull));
765
perror_plus("close");
768
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
770
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
773
int helperdir_fd = (int)TEMP_FAILURE_RETRY(open(pluginhelperdir,
778
if(helperdir_fd == -1){
780
_exit(EX_UNAVAILABLE);
782
int helper_fd = (int)TEMP_FAILURE_RETRY(openat(helperdir_fd,
785
perror_plus("openat");
786
_exit(EX_UNAVAILABLE);
788
TEMP_FAILURE_RETRY(close(helperdir_fd));
790
#pragma GCC diagnostic push
791
#pragma GCC diagnostic ignored "-Wcast-qual"
793
if(fexecve(helper_fd, (char *const [])
794
{ helper, add ? add_arg : delete_arg, (char *)address,
795
interface, NULL }, environ) == -1){
797
#pragma GCC diagnostic pop
799
perror_plus("fexecve");
811
pret = waitpid(pid, &status, 0);
812
if(pret == -1 and errno == EINTR and quit_now){
813
int errno_raising = 0;
814
if((errno = raise_privileges()) != 0){
815
errno_raising = errno;
816
perror_plus("Failed to raise privileges in order to"
817
" kill helper program");
819
if(kill(pid, SIGTERM) == -1){
822
if((errno_raising == 0) and (errno = lower_privileges()) != 0){
823
perror_plus("Failed to lower privileges after killing"
828
} while(pret == -1 and errno == EINTR);
830
perror_plus("waitpid");
833
if(WIFEXITED(status)){
834
if(WEXITSTATUS(status) != 0){
835
fprintf_plus(stderr, "Error: iprouteadddel exited"
836
" with status %d\n", WEXITSTATUS(status));
841
if(WIFSIGNALED(status)){
842
fprintf_plus(stderr, "Error: iprouteadddel died by"
843
" signal %d\n", WTERMSIG(status));
846
fprintf_plus(stderr, "Error: iprouteadddel crashed\n");
850
__attribute__((nonnull, warn_unused_result))
851
static bool add_local_route(const char *address,
852
AvahiIfIndex if_index){
853
return add_remove_local_route(true, address, if_index);
856
__attribute__((nonnull, warn_unused_result))
857
static bool remove_local_route(const char *address,
858
AvahiIfIndex if_index){
859
return add_remove_local_route(false, address, if_index);
862
619
/* Called when a Mandos server is found */
863
__attribute__((nonnull, warn_unused_result))
864
static int start_mandos_communication(const char *ip, in_port_t port,
620
static int start_mandos_communication(const char *ip, uint16_t port,
865
621
AvahiIfIndex if_index,
866
int af, mandos_context *mc){
867
623
int ret, tcp_sd = -1;
869
struct sockaddr_storage to;
626
struct sockaddr_in in;
627
struct sockaddr_in6 in6;
870
629
char *buffer = NULL;
871
630
char *decrypted_buffer = NULL;
872
631
size_t buffer_length = 0;
1000
732
if(if_indextoname((unsigned int)if_index, interface) == NULL){
1001
733
perror_plus("if_indextoname");
1003
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
1004
"\n", ip, interface, (uintmax_t)port);
735
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
736
"\n", ip, interface, port);
1007
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
1008
ip, (uintmax_t)port);
739
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
1010
742
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
1011
743
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1013
ret = getnameinfo((struct sockaddr *)&to,
1014
sizeof(struct sockaddr_in6),
1015
addrstr, sizeof(addrstr), NULL, 0,
1018
ret = getnameinfo((struct sockaddr *)&to,
1019
sizeof(struct sockaddr_in),
1020
addrstr, sizeof(addrstr), NULL, 0,
1023
if(ret == EAI_SYSTEM){
1024
perror_plus("getnameinfo");
1025
} else if(ret != 0) {
1026
fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
1027
} else if(strcmp(addrstr, ip) != 0){
1028
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
1039
ret = connect(tcp_sd, (struct sockaddr *)&to,
1040
sizeof(struct sockaddr_in6));
1042
ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
1043
sizeof(struct sockaddr_in));
1046
if(errno == ENETUNREACH
1047
and if_index != AVAHI_IF_UNSPEC
1048
and connect_to == NULL
1049
and not route_added and
1050
((af == AF_INET6 and not
1051
IN6_IS_ADDR_LINKLOCAL(&(((struct sockaddr_in6 *)
1053
or (af == AF_INET and
1054
/* Not a a IPv4LL address */
1055
(ntohl(((struct sockaddr_in *)&to)->sin_addr.s_addr)
1056
& 0xFFFF0000L) != 0xA9FE0000L))){
1057
/* Work around Avahi bug - Avahi does not announce link-local
1058
addresses if it has a global address, so local hosts with
1059
*only* a link-local address (e.g. Mandos clients) cannot
1060
connect to a Mandos server announced by Avahi on a server
1061
host with a global address. Work around this by retrying
1062
with an explicit route added with the server's address.
1064
Avahi bug reference:
1065
http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1066
https://bugs.debian.org/587961
1069
route_added = add_local_route(ip, if_index);
1075
if(errno != ECONNREFUSED or debug){
1077
perror_plus("connect");
746
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
749
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
753
perror_plus("inet_ntop");
755
if(strcmp(addrstr, ip) != 0){
756
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
767
ret = connect(tcp_sd, &to.in6, sizeof(to));
769
ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
772
if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
774
perror_plus("connect");
1090
785
const char *out = mandos_protocol_version;
1703
1380
ret = avahi_simple_poll_iterate(s, (int)block_time);
1706
if(ret > 0 or errno != EINTR){
1383
if (ret > 0 or errno != EINTR){
1707
1384
return (ret != 1) ? ret : 0;
1713
__attribute__((nonnull))
1714
void run_network_hooks(const char *mode, const char *interface,
1390
bool run_network_hooks(const char *mode, const char *interface,
1715
1391
const float delay){
1716
struct dirent **direntries = NULL;
1717
if(hookdir_fd == -1){
1718
hookdir_fd = open(hookdir, O_RDONLY | O_DIRECTORY | O_PATH
1720
if(hookdir_fd == -1){
1721
if(errno == ENOENT){
1723
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1724
" found\n", hookdir);
1727
perror_plus("open");
1733
#if __GLIBC_PREREQ(2, 15)
1734
int numhooks = scandirat(hookdir_fd, ".", &direntries,
1735
runnable_hook, alphasort);
1736
#else /* not __GLIBC_PREREQ(2, 15) */
1737
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1739
#endif /* not __GLIBC_PREREQ(2, 15) */
1740
#else /* not __GLIBC__ */
1741
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1743
#endif /* not __GLIBC__ */
1392
struct dirent **direntries;
1393
struct dirent *direntry;
1395
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1744
1397
if(numhooks == -1){
1745
1398
perror_plus("scandir");
1748
struct dirent *direntry;
1750
int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
1752
perror_plus("open(\"/dev/null\", O_RDONLY)");
1755
for(int i = 0; i < numhooks; i++){
1756
direntry = direntries[i];
1758
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1761
pid_t hook_pid = fork();
1764
/* Raise privileges */
1765
errno = raise_privileges_permanently();
1767
perror_plus("Failed to raise privileges");
1774
perror_plus("setgid");
1777
/* Reset supplementary groups */
1779
ret = setgroups(0, NULL);
1781
perror_plus("setgroups");
1784
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1786
perror_plus("setenv");
1789
ret = setenv("DEVICE", interface, 1);
1791
perror_plus("setenv");
1794
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1796
perror_plus("setenv");
1799
ret = setenv("MODE", mode, 1);
1801
perror_plus("setenv");
1805
ret = asprintf(&delaystring, "%f", (double)delay);
1400
int devnull = open("/dev/null", O_RDONLY);
1401
for(int i = 0; i < numhooks; i++){
1402
direntry = direntries[i];
1403
char *fullname = NULL;
1404
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1807
1406
perror_plus("asprintf");
1810
ret = setenv("DELAY", delaystring, 1);
1410
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1413
pid_t hook_pid = fork();
1416
/* Raise privileges */
1420
perror_plus("seteuid");
1422
/* Raise privileges even more */
1426
perror_plus("setuid");
1432
perror_plus("setgid");
1434
/* Reset supplementary groups */
1436
ret = setgroups(0, NULL);
1438
perror_plus("setgroups");
1440
dup2(devnull, STDIN_FILENO);
1442
dup2(STDERR_FILENO, STDOUT_FILENO);
1443
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1445
perror_plus("setenv");
1448
ret = setenv("DEVICE", interface, 1);
1450
perror_plus("setenv");
1453
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1455
perror_plus("setenv");
1458
ret = setenv("MODE", mode, 1);
1460
perror_plus("setenv");
1464
ret = asprintf(&delaystring, "%f", delay);
1466
perror_plus("asprintf");
1469
ret = setenv("DELAY", delaystring, 1);
1472
perror_plus("setenv");
1812
1475
free(delaystring);
1813
perror_plus("setenv");
1817
if(connect_to != NULL){
1818
ret = setenv("CONNECT", connect_to, 1);
1820
perror_plus("setenv");
1824
int hook_fd = (int)TEMP_FAILURE_RETRY(openat(hookdir_fd,
1828
perror_plus("openat");
1829
_exit(EXIT_FAILURE);
1831
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1832
perror_plus("close");
1833
_exit(EXIT_FAILURE);
1835
ret = dup2(devnull, STDIN_FILENO);
1837
perror_plus("dup2(devnull, STDIN_FILENO)");
1840
ret = (int)TEMP_FAILURE_RETRY(close(devnull));
1842
perror_plus("close");
1845
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1847
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1850
if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1852
perror_plus("fexecve");
1853
_exit(EXIT_FAILURE);
1857
perror_plus("fork");
1862
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1863
perror_plus("waitpid");
1867
if(WIFEXITED(status)){
1868
if(WEXITSTATUS(status) != 0){
1869
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1870
" with status %d\n", direntry->d_name,
1871
WEXITSTATUS(status));
1875
} else if(WIFSIGNALED(status)){
1876
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1877
" signal %d\n", direntry->d_name,
1882
fprintf_plus(stderr, "Warning: network hook \"%s\""
1883
" crashed\n", direntry->d_name);
1889
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1895
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1896
perror_plus("close");
1903
__attribute__((nonnull, warn_unused_result))
1904
error_t bring_up_interface(const char *const interface,
1906
error_t old_errno = errno;
1908
struct ifreq network;
1909
unsigned int if_index = if_nametoindex(interface);
1911
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1921
if(not interface_is_up(interface)){
1922
error_t ret_errno = 0, ioctl_errno = 0;
1923
if(not get_flags(interface, &network)){
1925
fprintf_plus(stderr, "Failed to get flags for interface "
1926
"\"%s\"\n", interface);
1930
network.ifr_flags |= IFF_UP; /* set flag */
1932
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1935
perror_plus("socket");
1941
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1943
perror_plus("close");
1950
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1954
/* Raise privileges */
1955
ret_errno = raise_privileges();
1958
perror_plus("Failed to raise privileges");
1963
bool restore_loglevel = false;
1965
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1966
messages about the network interface to mess up the prompt */
1967
ret_linux = klogctl(8, NULL, 5);
1968
if(ret_linux == -1){
1969
perror_plus("klogctl");
1971
restore_loglevel = true;
1974
#endif /* __linux__ */
1975
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1976
ioctl_errno = errno;
1978
if(restore_loglevel){
1979
ret_linux = klogctl(7, NULL, 0);
1980
if(ret_linux == -1){
1981
perror_plus("klogctl");
1984
#endif /* __linux__ */
1986
/* If raise_privileges() succeeded above */
1988
/* Lower privileges */
1989
ret_errno = lower_privileges();
1992
perror_plus("Failed to lower privileges");
1996
/* Close the socket */
1997
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1999
perror_plus("close");
2002
if(ret_setflags == -1){
2003
errno = ioctl_errno;
2004
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
2009
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
2013
/* Sleep checking until interface is running.
2014
Check every 0.25s, up to total time of delay */
2015
for(int i=0; i < delay * 4; i++){
2016
if(interface_is_running(interface)){
2019
struct timespec sleeptime = { .tv_nsec = 250000000 };
2020
ret = nanosleep(&sleeptime, NULL);
2021
if(ret == -1 and errno != EINTR){
2022
perror_plus("nanosleep");
2030
__attribute__((nonnull, warn_unused_result))
2031
error_t take_down_interface(const char *const interface){
2032
error_t old_errno = errno;
2033
struct ifreq network;
2034
unsigned int if_index = if_nametoindex(interface);
2036
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
2040
if(interface_is_up(interface)){
2041
error_t ret_errno = 0, ioctl_errno = 0;
2042
if(not get_flags(interface, &network) and debug){
2044
fprintf_plus(stderr, "Failed to get flags for interface "
2045
"\"%s\"\n", interface);
2049
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2051
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
2054
perror_plus("socket");
2060
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
2064
/* Raise privileges */
2065
ret_errno = raise_privileges();
2068
perror_plus("Failed to raise privileges");
2071
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
2072
ioctl_errno = errno;
2074
/* If raise_privileges() succeeded above */
2076
/* Lower privileges */
2077
ret_errno = lower_privileges();
2080
perror_plus("Failed to lower privileges");
2084
/* Close the socket */
2085
int ret = (int)TEMP_FAILURE_RETRY(close(sd));
2087
perror_plus("close");
2090
if(ret_setflags == -1){
2091
errno = ioctl_errno;
2092
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2097
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1476
if(connect_to != NULL){
1477
ret = setenv("CONNECT", connect_to, 1);
1479
perror_plus("setenv");
1483
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1484
perror_plus("execl");
1485
_exit(EXIT_FAILURE);
1489
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1490
perror_plus("waitpid");
1494
if(WIFEXITED(status)){
1495
if(WEXITSTATUS(status) != 0){
1496
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1497
" with status %d\n", direntry->d_name,
1498
WEXITSTATUS(status));
1502
} else if(WIFSIGNALED(status)){
1503
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1504
" signal %d\n", direntry->d_name,
1509
fprintf_plus(stderr, "Warning: network hook \"%s\""
1510
" crashed\n", direntry->d_name);
1517
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
2105
1526
int main(int argc, char *argv[]){
2106
mandos_context mc = { .server = NULL, .dh_bits = 1024,
2107
.priority = "SECURE256:!CTYPE-X.509:"
2108
"+CTYPE-OPENPGP", .current_server = NULL,
2109
.interfaces = NULL, .interfaces_size = 0 };
2110
1527
AvahiSServiceBrowser *sb = NULL;
2113
1530
intmax_t tmpmax;
2115
1532
int exitcode = EXIT_SUCCESS;
2116
char *interfaces_to_take_down = NULL;
2117
size_t interfaces_to_take_down_size = 0;
2118
char run_tempdir[] = "/run/tmp/mandosXXXXXX";
2119
char old_tempdir[] = "/tmp/mandosXXXXXX";
2120
char *tempdir = NULL;
1533
const char *interface = "";
1534
struct ifreq network;
1536
bool take_down_interface = false;
1539
char tempdir[] = "/tmp/mandosXXXXXX";
1540
bool tempdir_created = false;
2121
1541
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
2122
1542
const char *seckey = PATHDIR "/" SECKEY;
2123
1543
const char *pubkey = PATHDIR "/" PUBKEY;
2124
char *interfaces_hooks = NULL;
2126
1545
bool gnutls_initialized = false;
2127
1546
bool gpgme_initialized = false;
2354
1769
TEMP_FAILURE_RETRY(close(pubkey_fd));
2358
1773
/* Lower privileges */
2359
ret_errno = lower_privileges();
2362
perror_plus("Failed to lower privileges");
2367
/* Remove invalid interface names (except "none") */
2369
char *interface = NULL;
2370
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2372
if(strcmp(interface, "none") != 0
2373
and if_nametoindex(interface) == 0){
2374
if(interface[0] != '\0'){
2375
fprintf_plus(stderr, "Not using nonexisting interface"
2376
" \"%s\"\n", interface);
2378
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
1777
perror_plus("seteuid");
2384
1782
/* Run network hooks */
2386
if(mc.interfaces != NULL){
2387
interfaces_hooks = malloc(mc.interfaces_size);
2388
if(interfaces_hooks == NULL){
2389
perror_plus("malloc");
2392
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2393
argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2395
run_network_hooks("start", interfaces_hooks != NULL ?
2396
interfaces_hooks : "", delay);
1783
if(not run_network_hooks("start", interface, delay)){
2400
1788
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;
2403
1823
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
2404
1824
from the signal handler */
2405
1825
/* Initialize the pseudo-RNG for Avahi */
2406
1826
srand((unsigned int) time(NULL));
2407
simple_poll = avahi_simple_poll_new();
2408
if(simple_poll == NULL){
1827
mc.simple_poll = avahi_simple_poll_new();
1828
if(mc.simple_poll == NULL){
2409
1829
fprintf_plus(stderr,
2410
1830
"Avahi: Failed to create simple poll object.\n");
2411
1831
exitcode = EX_UNAVAILABLE;
2478
/* If no interfaces were specified, make a list */
2479
if(mc.interfaces == NULL){
2480
struct dirent **direntries = NULL;
2481
/* Look for any good interfaces */
2482
ret = scandir(sys_class_net, &direntries, good_interface,
2485
/* Add all found interfaces to interfaces list */
2486
for(int i = 0; i < ret; ++i){
2487
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2488
direntries[i]->d_name);
2491
perror_plus("argz_add");
2492
free(direntries[i]);
2496
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2497
direntries[i]->d_name);
2499
free(direntries[i]);
2506
fprintf_plus(stderr, "Could not find a network interface\n");
2507
exitcode = EXIT_FAILURE;
2512
/* Bring up interfaces which are down, and remove any "none"s */
2514
char *interface = NULL;
2515
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2517
/* If interface name is "none", stop bringing up interfaces.
2518
Also remove all instances of "none" from the list */
2519
if(strcmp(interface, "none") == 0){
2520
argz_delete(&mc.interfaces, &mc.interfaces_size,
2523
while((interface = argz_next(mc.interfaces,
2524
mc.interfaces_size, interface))){
2525
if(strcmp(interface, "none") == 0){
2526
argz_delete(&mc.interfaces, &mc.interfaces_size,
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){
2533
bool interface_was_up = interface_is_up(interface);
2534
errno = bring_up_interface(interface, delay);
2535
if(not interface_was_up){
2537
perror_plus("Failed to bring up interface");
2539
errno = argz_add(&interfaces_to_take_down,
2540
&interfaces_to_take_down_size,
2543
perror_plus("argz_add");
2548
if(debug and (interfaces_to_take_down == NULL)){
2549
fprintf_plus(stderr, "No interfaces were brought up\n");
2553
/* If we only got one interface, explicitly use only that one */
2554
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2556
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2559
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
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");
2566
ret = init_gnutls_global(pubkey, seckey, &mc);
2039
ret = init_gnutls_global(pubkey, seckey);
2568
2041
fprintf_plus(stderr, "init_gnutls_global failed\n");
2569
2042
exitcode = EX_UNAVAILABLE;
2772
2233
mc.current_server->prev->next = NULL;
2773
2234
while(mc.current_server != NULL){
2774
2235
server *next = mc.current_server->next;
2776
#pragma GCC diagnostic push
2777
#pragma GCC diagnostic ignored "-Wcast-qual"
2779
free((char *)(mc.current_server->ip));
2781
#pragma GCC diagnostic pop
2783
2236
free(mc.current_server);
2784
2237
mc.current_server = next;
2788
/* Re-raise privileges */
2241
/* Run network hooks */
2242
run_network_hooks("stop", interface, delay);
2244
/* Re-raise priviliges */
2790
ret_errno = raise_privileges();
2793
perror_plus("Failed to raise privileges");
2796
/* Run network hooks */
2797
run_network_hooks("stop", interfaces_hooks != NULL ?
2798
interfaces_hooks : "", delay);
2800
/* Take down the network interfaces which were brought up */
2802
char *interface = NULL;
2803
while((interface=argz_next(interfaces_to_take_down,
2804
interfaces_to_take_down_size,
2806
ret_errno = take_down_interface(interface);
2809
perror_plus("Failed to take down interface");
2812
if(debug and (interfaces_to_take_down == NULL)){
2813
fprintf_plus(stderr, "No interfaces needed to be taken"
2249
perror_plus("seteuid");
2819
ret_errno = lower_privileges_permanently();
2822
perror_plus("Failed to lower privileges permanently");
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");
2264
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2266
perror_plus("close");
2826
free(interfaces_to_take_down);
2827
free(interfaces_hooks);
2270
/* Lower privileges permanently */
2274
perror_plus("setuid");
2829
2277
/* Removes the GPGME temp directory and all files inside */
2830
if(tempdir != NULL){
2278
if(tempdir_created){
2831
2279
struct dirent **direntries = NULL;
2832
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
2836
if(tempdir_fd == -1){
2837
perror_plus("open");
2840
#if __GLIBC_PREREQ(2, 15)
2841
int numentries = scandirat(tempdir_fd, ".", &direntries,
2842
notdotentries, alphasort);
2843
#else /* not __GLIBC_PREREQ(2, 15) */
2844
int numentries = scandir(tempdir, &direntries, notdotentries,
2846
#endif /* not __GLIBC_PREREQ(2, 15) */
2847
#else /* not __GLIBC__ */
2848
int numentries = scandir(tempdir, &direntries, notdotentries,
2850
#endif /* not __GLIBC__ */
2851
if(numentries >= 0){
2852
for(int i = 0; i < numentries; i++){
2853
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2855
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2856
" \"%s\", 0): %s\n", tempdir,
2857
direntries[i]->d_name, strerror(errno));
2859
free(direntries[i]);
2862
/* need to clean even if 0 because man page doesn't specify */
2864
if(numentries == -1){
2865
perror_plus("scandir");
2867
ret = rmdir(tempdir);
2868
if(ret == -1 and errno != ENOENT){
2869
perror_plus("rmdir");
2280
struct dirent *direntry = NULL;
2281
int numentries = scandir(tempdir, &direntries, notdotentries,
2283
if (numentries > 0){
2284
for(int i = 0; i < numentries; i++){
2285
direntry = direntries[i];
2286
char *fullname = NULL;
2287
ret = asprintf(&fullname, "%s/%s", tempdir,
2290
perror_plus("asprintf");
2293
ret = remove(fullname);
2295
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2872
TEMP_FAILURE_RETRY(close(tempdir_fd));
2302
/* need to clean even if 0 because man page doesn't specify */
2304
if (numentries == -1){
2305
perror_plus("scandir");
2307
ret = rmdir(tempdir);
2308
if(ret == -1 and errno != ENOENT){
2309
perror_plus("rmdir");