650
620
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
651
621
__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 remove_arg[] = "remove";
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 = 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
int helper_fd = (int)TEMP_FAILURE_RETRY(openat(helperdir_fd,
780
TEMP_FAILURE_RETRY(close(helperdir_fd));
782
#pragma GCC diagnostic push
783
#pragma GCC diagnostic ignored "-Wcast-qual"
785
if(fexecve(helper_fd, (char *const [])
786
{ helper, add ? add_arg : remove_arg, (char *)address,
787
interface, NULL }, environ) == -1){
789
#pragma GCC diagnostic pop
791
perror_plus("fexecve");
803
pret = waitpid(pid, &status, 0);
804
if(pret == -1 and errno == EINTR and quit_now){
805
int errno_raising = 0;
806
if((errno = raise_privileges()) != 0){
807
errno_raising = errno;
808
perror_plus("Failed to raise privileges in order to"
809
" kill helper program");
811
if(kill(pid, SIGTERM) == -1){
814
if((errno_raising == 0) and (errno = lower_privileges()) != 0){
815
perror_plus("Failed to lower privileges after killing"
820
} while(pret == -1 and errno == EINTR);
822
perror_plus("waitpid");
825
if(WIFEXITED(status)){
826
if(WEXITSTATUS(status) != 0){
827
fprintf_plus(stderr, "Error: iprouteadddel exited"
828
" with status %d\n", WEXITSTATUS(status));
833
if(WIFSIGNALED(status)){
834
fprintf_plus(stderr, "Error: iprouteadddel died by"
835
" signal %d\n", WTERMSIG(status));
838
fprintf_plus(stderr, "Error: iprouteadddel crashed\n");
842
__attribute__((nonnull, warn_unused_result))
843
static bool add_local_route(const char *address,
844
AvahiIfIndex if_index){
845
return add_remove_local_route(true, address, if_index);
848
__attribute__((nonnull, warn_unused_result))
849
static bool remove_local_route(const char *address,
850
AvahiIfIndex if_index){
851
return add_remove_local_route(false, address, if_index);
854
623
/* Called when a Mandos server is found */
855
__attribute__((nonnull, warn_unused_result))
856
624
static int start_mandos_communication(const char *ip, in_port_t port,
857
625
AvahiIfIndex if_index,
858
626
int af, mandos_context *mc){
859
627
int ret, tcp_sd = -1;
861
struct sockaddr_storage to;
630
struct sockaddr_in in;
631
struct sockaddr_in6 in6;
862
633
char *buffer = NULL;
863
634
char *decrypted_buffer = NULL;
864
635
size_t buffer_length = 0;
1002
775
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
1003
776
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1005
ret = getnameinfo((struct sockaddr *)&to,
1006
sizeof(struct sockaddr_in6),
1007
addrstr, sizeof(addrstr), NULL, 0,
1010
ret = getnameinfo((struct sockaddr *)&to,
1011
sizeof(struct sockaddr_in),
1012
addrstr, sizeof(addrstr), NULL, 0,
1015
if(ret == EAI_SYSTEM){
1016
perror_plus("getnameinfo");
1017
} else if(ret != 0) {
1018
fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
1019
} else if(strcmp(addrstr, ip) != 0){
1020
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
1031
ret = connect(tcp_sd, (struct sockaddr *)&to,
1032
sizeof(struct sockaddr_in6));
1034
ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
1035
sizeof(struct sockaddr_in));
1038
if(errno == ENETUNREACH
1039
and if_index != AVAHI_IF_UNSPEC
1040
and connect_to == NULL
1041
and not route_added and
1042
((af == AF_INET6 and not
1043
IN6_IS_ADDR_LINKLOCAL(&(((struct sockaddr_in6 *)
1045
or (af == AF_INET and
1046
/* Not a a IPv4LL address */
1047
(ntohl(((struct sockaddr_in *)&to)->sin_addr.s_addr)
1048
& 0xFFFF0000L) != 0xA9FE0000L))){
1049
/* Work around Avahi bug - Avahi does not announce link-local
1050
addresses if it has a global address, so local hosts with
1051
*only* a link-local address (e.g. Mandos clients) cannot
1052
connect to a Mandos server announced by Avahi on a server
1053
host with a global address. Work around this by retrying
1054
with an explicit route added with the server's address.
1056
Avahi bug reference:
1057
http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1058
https://bugs.debian.org/587961
1061
route_added = add_local_route(ip, if_index);
1067
if(errno != ECONNREFUSED or debug){
1069
perror_plus("connect");
779
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
782
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
786
perror_plus("inet_ntop");
788
if(strcmp(addrstr, ip) != 0){
789
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
800
ret = connect(tcp_sd, &to.in6, sizeof(to));
802
ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
805
if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
807
perror_plus("connect");
1082
818
const char *out = mandos_protocol_version;
1695
1422
ret = avahi_simple_poll_iterate(s, (int)block_time);
1698
if(ret > 0 or errno != EINTR){
1425
if (ret > 0 or errno != EINTR){
1699
1426
return (ret != 1) ? ret : 0;
1705
__attribute__((nonnull))
1706
void run_network_hooks(const char *mode, const char *interface,
1432
/* Set effective uid to 0, return errno */
1433
error_t raise_privileges(void){
1434
error_t old_errno = errno;
1435
error_t ret_errno = 0;
1436
if(seteuid(0) == -1){
1438
perror_plus("seteuid");
1444
/* Set effective and real user ID to 0. Return errno. */
1445
error_t raise_privileges_permanently(void){
1446
error_t old_errno = errno;
1447
error_t ret_errno = raise_privileges();
1452
if(setuid(0) == -1){
1454
perror_plus("seteuid");
1460
/* Set effective user ID to unprivileged saved user ID */
1461
error_t lower_privileges(void){
1462
error_t old_errno = errno;
1463
error_t ret_errno = 0;
1464
if(seteuid(uid) == -1){
1466
perror_plus("seteuid");
1472
/* Lower privileges permanently */
1473
error_t lower_privileges_permanently(void){
1474
error_t old_errno = errno;
1475
error_t ret_errno = 0;
1476
if(setuid(uid) == -1){
1478
perror_plus("setuid");
1484
bool run_network_hooks(const char *mode, const char *interface,
1707
1485
const float delay){
1708
struct dirent **direntries = NULL;
1709
if(hookdir_fd == -1){
1710
hookdir_fd = open(hookdir, O_RDONLY | O_DIRECTORY | O_PATH
1712
if(hookdir_fd == -1){
1713
if(errno == ENOENT){
1715
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1716
" found\n", hookdir);
1719
perror_plus("open");
1725
#if __GLIBC_PREREQ(2, 15)
1726
int numhooks = scandirat(hookdir_fd, ".", &direntries,
1727
runnable_hook, alphasort);
1728
#else /* not __GLIBC_PREREQ(2, 15) */
1729
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1731
#endif /* not __GLIBC_PREREQ(2, 15) */
1732
#else /* not __GLIBC__ */
1733
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1735
#endif /* not __GLIBC__ */
1486
struct dirent **direntries;
1487
struct dirent *direntry;
1489
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1736
1491
if(numhooks == -1){
1737
perror_plus("scandir");
1740
struct dirent *direntry;
1742
int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
1744
perror_plus("open(\"/dev/null\", O_RDONLY)");
1747
for(int i = 0; i < numhooks; i++){
1748
direntry = direntries[i];
1750
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1492
if(errno == ENOENT){
1494
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1495
" found\n", hookdir);
1498
perror_plus("scandir");
1753
pid_t hook_pid = fork();
1756
/* Raise privileges */
1757
errno = raise_privileges_permanently();
1759
perror_plus("Failed to raise privileges");
1766
perror_plus("setgid");
1769
/* Reset supplementary groups */
1771
ret = setgroups(0, NULL);
1773
perror_plus("setgroups");
1776
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1778
perror_plus("setenv");
1781
ret = setenv("DEVICE", interface, 1);
1783
perror_plus("setenv");
1786
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1788
perror_plus("setenv");
1791
ret = setenv("MODE", mode, 1);
1793
perror_plus("setenv");
1797
ret = asprintf(&delaystring, "%f", (double)delay);
1501
int devnull = open("/dev/null", O_RDONLY);
1502
for(int i = 0; i < numhooks; i++){
1503
direntry = direntries[i];
1504
char *fullname = NULL;
1505
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1799
1507
perror_plus("asprintf");
1802
ret = setenv("DELAY", delaystring, 1);
1511
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1514
pid_t hook_pid = fork();
1517
/* Raise privileges */
1518
raise_privileges_permanently();
1523
perror_plus("setgid");
1525
/* Reset supplementary groups */
1527
ret = setgroups(0, NULL);
1529
perror_plus("setgroups");
1531
dup2(devnull, STDIN_FILENO);
1533
dup2(STDERR_FILENO, STDOUT_FILENO);
1534
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1536
perror_plus("setenv");
1539
ret = setenv("DEVICE", interface, 1);
1541
perror_plus("setenv");
1544
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1546
perror_plus("setenv");
1549
ret = setenv("MODE", mode, 1);
1551
perror_plus("setenv");
1555
ret = asprintf(&delaystring, "%f", delay);
1557
perror_plus("asprintf");
1560
ret = setenv("DELAY", delaystring, 1);
1563
perror_plus("setenv");
1804
1566
free(delaystring);
1805
perror_plus("setenv");
1809
if(connect_to != NULL){
1810
ret = setenv("CONNECT", connect_to, 1);
1812
perror_plus("setenv");
1816
int hook_fd = TEMP_FAILURE_RETRY(openat(hookdir_fd,
1820
perror_plus("openat");
1821
_exit(EXIT_FAILURE);
1823
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1824
perror_plus("close");
1825
_exit(EXIT_FAILURE);
1827
ret = dup2(devnull, STDIN_FILENO);
1829
perror_plus("dup2(devnull, STDIN_FILENO)");
1832
ret = close(devnull);
1834
perror_plus("close");
1837
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1839
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1842
if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1844
perror_plus("fexecve");
1845
_exit(EXIT_FAILURE);
1849
perror_plus("fork");
1854
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1855
perror_plus("waitpid");
1859
if(WIFEXITED(status)){
1860
if(WEXITSTATUS(status) != 0){
1861
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1862
" with status %d\n", direntry->d_name,
1863
WEXITSTATUS(status));
1867
} else if(WIFSIGNALED(status)){
1868
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1869
" signal %d\n", direntry->d_name,
1567
if(connect_to != NULL){
1568
ret = setenv("CONNECT", connect_to, 1);
1570
perror_plus("setenv");
1574
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1575
perror_plus("execl");
1576
_exit(EXIT_FAILURE);
1874
fprintf_plus(stderr, "Warning: network hook \"%s\""
1875
" crashed\n", direntry->d_name);
1881
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1887
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1888
perror_plus("close");
1580
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1581
perror_plus("waitpid");
1585
if(WIFEXITED(status)){
1586
if(WEXITSTATUS(status) != 0){
1587
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1588
" with status %d\n", direntry->d_name,
1589
WEXITSTATUS(status));
1593
} else if(WIFSIGNALED(status)){
1594
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1595
" signal %d\n", direntry->d_name,
1600
fprintf_plus(stderr, "Warning: network hook \"%s\""
1601
" crashed\n", direntry->d_name);
1608
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1895
__attribute__((nonnull, warn_unused_result))
1896
1617
error_t bring_up_interface(const char *const interface,
1897
1618
const float delay){
1898
1620
error_t old_errno = errno;
1621
error_t ret_errno = 0;
1622
int ret, ret_setflags;
1900
1623
struct ifreq network;
1901
1624
unsigned int if_index = if_nametoindex(interface);
1902
1625
if(if_index == 0){
2764
2437
mc.current_server->prev->next = NULL;
2765
2438
while(mc.current_server != NULL){
2766
2439
server *next = mc.current_server->next;
2768
#pragma GCC diagnostic push
2769
#pragma GCC diagnostic ignored "-Wcast-qual"
2771
free((char *)(mc.current_server->ip));
2773
#pragma GCC diagnostic pop
2775
2440
free(mc.current_server);
2776
2441
mc.current_server = next;
2780
/* Re-raise privileges */
2445
/* Re-raise priviliges */
2782
ret_errno = raise_privileges();
2785
perror_plus("Failed to raise privileges");
2788
/* Run network hooks */
2789
run_network_hooks("stop", interfaces_hooks != NULL ?
2790
interfaces_hooks : "", delay);
2792
/* Take down the network interfaces which were brought up */
2794
char *interface = NULL;
2795
while((interface=argz_next(interfaces_to_take_down,
2796
interfaces_to_take_down_size,
2798
ret_errno = take_down_interface(interface);
2801
perror_plus("Failed to take down interface");
2804
if(debug and (interfaces_to_take_down == NULL)){
2805
fprintf_plus(stderr, "No interfaces needed to be taken"
2811
ret_errno = lower_privileges_permanently();
2814
perror_plus("Failed to lower privileges permanently");
2449
/* Run network hooks */
2450
run_network_hooks("stop", interfaces_hooks != NULL ?
2451
interfaces_hooks : "", delay);
2453
/* Take down the network interfaces which were brought up */
2455
char *interface = NULL;
2456
while((interface=argz_next(interfaces_to_take_down,
2457
interfaces_to_take_down_size,
2459
ret_errno = take_down_interface(interface);
2462
perror_plus("Failed to take down interface");
2465
if(debug and (interfaces_to_take_down == NULL)){
2466
fprintf_plus(stderr, "No interfaces needed to be taken"
2471
lower_privileges_permanently();
2818
2474
free(interfaces_to_take_down);
2819
2475
free(interfaces_hooks);
2821
2477
/* Removes the GPGME temp directory and all files inside */
2822
if(tempdir != NULL){
2478
if(tempdir_created){
2823
2479
struct dirent **direntries = NULL;
2824
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
2828
if(tempdir_fd == -1){
2829
perror_plus("open");
2832
#if __GLIBC_PREREQ(2, 15)
2833
int numentries = scandirat(tempdir_fd, ".", &direntries,
2834
notdotentries, alphasort);
2835
#else /* not __GLIBC_PREREQ(2, 15) */
2836
int numentries = scandir(tempdir, &direntries, notdotentries,
2838
#endif /* not __GLIBC_PREREQ(2, 15) */
2839
#else /* not __GLIBC__ */
2840
int numentries = scandir(tempdir, &direntries, notdotentries,
2842
#endif /* not __GLIBC__ */
2843
if(numentries >= 0){
2844
for(int i = 0; i < numentries; i++){
2845
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2847
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2848
" \"%s\", 0): %s\n", tempdir,
2849
direntries[i]->d_name, strerror(errno));
2851
free(direntries[i]);
2854
/* need to clean even if 0 because man page doesn't specify */
2856
if(numentries == -1){
2857
perror_plus("scandir");
2859
ret = rmdir(tempdir);
2860
if(ret == -1 and errno != ENOENT){
2861
perror_plus("rmdir");
2480
struct dirent *direntry = NULL;
2481
int numentries = scandir(tempdir, &direntries, notdotentries,
2483
if (numentries > 0){
2484
for(int i = 0; i < numentries; i++){
2485
direntry = direntries[i];
2486
char *fullname = NULL;
2487
ret = asprintf(&fullname, "%s/%s", tempdir,
2490
perror_plus("asprintf");
2493
ret = remove(fullname);
2495
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2864
TEMP_FAILURE_RETRY(close(tempdir_fd));
2502
/* need to clean even if 0 because man page doesn't specify */
2504
if (numentries == -1){
2505
perror_plus("scandir");
2507
ret = rmdir(tempdir);
2508
if(ret == -1 and errno != ENOENT){
2509
perror_plus("rmdir");