9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2008-2011 Teddy Hogeborn
13
* Copyright © 2008-2011 Björn Påhlsson
12
* Copyright © 2008-2012 Teddy Hogeborn
13
* Copyright © 2008-2012 Björn Påhlsson
15
15
* This program is free software: you can redistribute it and/or
16
16
* modify it under the terms of the GNU General Public License as
42
42
#include <stdio.h> /* fprintf(), stderr, fwrite(),
43
43
stdout, ferror(), remove() */
44
#include <stdint.h> /* uint16_t, uint32_t */
44
#include <stdint.h> /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h> /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h> /* free(), EXIT_SUCCESS, srand(),
47
47
strtof(), abort() */
74
74
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
75
75
getuid(), getgid(), seteuid(),
76
setgid(), pause(), _exit() */
77
77
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
78
78
#include <iso646.h> /* not, or, and */
79
79
#include <argp.h> /* struct argp_option, error_t, struct
170
173
/* Function to use when printing errors */
171
174
void perror_plus(const char *print_text){
172
176
fprintf(stderr, "Mandos plugin %s: ",
173
177
program_invocation_short_name);
174
179
perror(print_text);
182
__attribute__((format (gnu_printf, 2, 3)))
177
183
int fprintf_plus(FILE *stream, const char *format, ...){
179
185
va_start (ap, format);
203
209
/* Add server to set of servers to retry periodically */
204
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
210
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
207
213
server *new_server = malloc(sizeof(server));
208
214
if(new_server == NULL){
209
215
perror_plus("malloc");
212
218
*new_server = (server){ .ip = strdup(ip),
820
/* Spurious warning from -Wint-to-pointer-cast */
821
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
826
/* This casting via intptr_t is to eliminate warning about casting
827
an int to a pointer type. This is exactly how the GnuTLS Guile
828
function "set-session-transport-fd!" does it. */
829
gnutls_transport_set_ptr(session,
830
(gnutls_transport_ptr_t)(intptr_t)tcp_sd);
1030
1039
avahi_simple_poll_quit(mc.simple_poll);
1032
ret = add_server(ip, port, interface,
1033
avahi_proto_to_af(proto));
1041
if(not add_server(ip, port, interface,
1042
avahi_proto_to_af(proto))){
1043
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1395
/* Set effective uid to 0, return errno */
1396
int raise_privileges(void){
1397
int old_errno = errno;
1400
if(seteuid(0) == -1){
1401
perror_plus("seteuid");
1408
/* Set effective and real user ID to 0. Return errno. */
1409
int raise_privileges_permanently(void){
1410
int old_errno = errno;
1411
int ret_errno = raise_privileges();
1417
if(setuid(0) == -1){
1418
perror_plus("seteuid");
1425
/* Set effective user ID to unprivileged saved user ID */
1426
int lower_privileges(void){
1427
int old_errno = errno;
1430
if(seteuid(uid) == -1){
1431
perror_plus("seteuid");
1379
1438
bool run_network_hooks(const char *mode, const char *interface,
1380
1439
const float delay){
1381
1440
struct dirent **direntries;
1389
1448
int devnull = open("/dev/null", O_RDONLY);
1390
1449
for(int i = 0; i < numhooks; i++){
1391
direntry = direntries[0];
1450
direntry = direntries[i];
1392
1451
char *fullname = NULL;
1393
1452
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1395
1454
perror_plus("asprintf");
1458
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1398
1461
pid_t hook_pid = fork();
1399
1462
if(hook_pid == 0){
1464
/* Raise privileges */
1465
raise_privileges_permanently();
1470
perror_plus("setgid");
1472
/* Reset supplementary groups */
1474
ret = setgroups(0, NULL);
1476
perror_plus("setgroups");
1401
1478
dup2(devnull, STDIN_FILENO);
1402
1479
close(devnull);
1403
1480
dup2(STDERR_FILENO, STDOUT_FILENO);
1404
1481
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1406
1483
perror_plus("setenv");
1409
1486
ret = setenv("DEVICE", interface, 1);
1411
1488
perror_plus("setenv");
1414
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1491
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1416
1493
perror_plus("setenv");
1419
1496
ret = setenv("MODE", mode, 1);
1421
1498
perror_plus("setenv");
1424
1501
char *delaystring;
1425
1502
ret = asprintf(&delaystring, "%f", delay);
1427
1504
perror_plus("asprintf");
1430
1507
ret = setenv("DELAY", delaystring, 1);
1432
1509
free(delaystring);
1433
1510
perror_plus("setenv");
1436
1513
free(delaystring);
1437
ret = execl(fullname, direntry->d_name, mode, NULL);
1438
perror_plus("execl");
1514
if(connect_to != NULL){
1515
ret = setenv("CONNECT", connect_to, 1);
1517
perror_plus("setenv");
1521
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1522
perror_plus("execl");
1523
_exit(EXIT_FAILURE);
1441
1527
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1564
int bring_up_interface(const char * const interface, const float delay){
1566
int old_errno = errno;
1569
struct ifreq network;
1570
AvahiIfIndex if_index = (AvahiIfIndex)if_nametoindex(interface);
1572
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1582
/* Re-raise priviliges */
1586
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1587
messages about the network interface to mess up the prompt */
1588
ret = klogctl(8, NULL, 5);
1589
bool restore_loglevel = true;
1591
restore_loglevel = false;
1592
perror_plus("klogctl");
1594
#endif /* __linux__ */
1596
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1599
perror_plus("socket");
1601
if(restore_loglevel){
1602
ret = klogctl(7, NULL, 0);
1604
perror_plus("klogctl");
1607
#endif /* __linux__ */
1608
/* Lower privileges */
1613
strcpy(network.ifr_name, interface);
1614
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1617
perror_plus("ioctl SIOCGIFFLAGS");
1619
if(restore_loglevel){
1620
ret = klogctl(7, NULL, 0);
1622
perror_plus("klogctl");
1625
#endif /* __linux__ */
1626
/* Lower privileges */
1631
if((network.ifr_flags & IFF_UP) == 0){
1632
network.ifr_flags |= IFF_UP;
1633
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1636
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1638
if(restore_loglevel){
1639
ret = klogctl(7, NULL, 0);
1641
perror_plus("klogctl");
1644
#endif /* __linux__ */
1645
/* Lower privileges */
1651
/* Sleep checking until interface is running.
1652
Check every 0.25s, up to total time of delay */
1653
for(int i=0; i < delay * 4; i++){
1654
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1656
perror_plus("ioctl SIOCGIFFLAGS");
1657
} else if(network.ifr_flags & IFF_RUNNING){
1660
struct timespec sleeptime = { .tv_nsec = 250000000 };
1661
ret = nanosleep(&sleeptime, NULL);
1662
if(ret == -1 and errno != EINTR){
1663
perror_plus("nanosleep");
1666
/* Close the socket */
1667
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1669
perror_plus("close");
1672
if(restore_loglevel){
1673
/* Restores kernel loglevel to default */
1674
ret = klogctl(7, NULL, 0);
1676
perror_plus("klogctl");
1679
#endif /* __linux__ */
1680
/* Lower privileges */
1477
1686
int main(int argc, char *argv[]){
1478
1687
AvahiSServiceBrowser *sb = NULL;
1637
1844
argp_state_help(state, state->out_stream,
1638
1845
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1639
1846
case 'V': /* --version */
1640
fprintf_plus(state->out_stream,
1641
"Mandos plugin mandos-client: ");
1642
1847
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1643
1848
exit(argp_err_exit_status);
1673
1878
/* Work around Debian bug #633582:
1674
1879
<http://bugs.debian.org/633582> */
1677
1881
/* 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");
1882
if(raise_privileges() == 0){
1885
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1886
int seckey_fd = open(seckey, O_RDONLY);
1887
if(seckey_fd == -1){
1888
perror_plus("open");
1890
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1892
perror_plus("fstat");
1894
if(S_ISREG(st.st_mode)
1895
and st.st_uid == 0 and st.st_gid == 0){
1896
ret = fchown(seckey_fd, uid, gid);
1898
perror_plus("fchown");
1902
TEMP_FAILURE_RETRY(close(seckey_fd));
1906
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1907
int pubkey_fd = open(pubkey, O_RDONLY);
1908
if(pubkey_fd == -1){
1909
perror_plus("open");
1911
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1913
perror_plus("fstat");
1915
if(S_ISREG(st.st_mode)
1916
and st.st_uid == 0 and st.st_gid == 0){
1917
ret = fchown(pubkey_fd, uid, gid);
1919
perror_plus("fchown");
1923
TEMP_FAILURE_RETRY(close(pubkey_fd));
1927
/* Lower privileges */
1931
perror_plus("seteuid");
1734
1936
/* Run network hooks */
1736
/* Re-raise priviliges */
1740
perror_plus("seteuid");
1742
if(not run_network_hooks("start", interface, delay)){
1745
/* Lower privileges */
1749
perror_plus("seteuid");
1937
if(not run_network_hooks("start", interface, delay)){
1864
2052
/* 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){
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");
2053
if((interface[0] != '\0') and (strcmp(interface, "none") != 0)){
2054
ret = bring_up_interface(interface, delay);
2057
perror_plus("Failed to bring up interface");