163
159
server *current_server;
164
160
} mandos_context;
166
/* global so signal handler can reach it*/
167
AvahiSimplePoll *simple_poll;
168
mandos_context mc = { .server = NULL, .dh_bits = 1024,
169
.priority = "SECURE256:!CTYPE-X.509:"
170
"+CTYPE-OPENPGP", .current_server = NULL };
162
/* global context so signal handler can reach it*/
163
mandos_context mc = { .simple_poll = NULL, .server = NULL,
164
.dh_bits = 1024, .priority = "SECURE256"
165
":!CTYPE-X.509:+CTYPE-OPENPGP",
166
.current_server = NULL };
172
168
sig_atomic_t quit_now = 0;
173
169
int signal_received = 0;
175
171
/* Function to use when printing errors */
176
172
void perror_plus(const char *print_text){
178
173
fprintf(stderr, "Mandos plugin %s: ",
179
174
program_invocation_short_name);
181
175
perror(print_text);
184
__attribute__((format (gnu_printf, 2, 3)))
185
178
int fprintf_plus(FILE *stream, const char *format, ...){
187
180
va_start (ap, format);
731
729
if(if_indextoname((unsigned int)if_index, interface) == NULL){
732
730
perror_plus("if_indextoname");
734
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
735
"\n", ip, interface, (uintmax_t)port);
732
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
733
"\n", ip, interface, port);
738
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
739
ip, (uintmax_t)port);
736
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
741
739
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
742
740
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1032
1025
PRIdMAX ") on port %" PRIu16 "\n", name,
1033
1026
host_name, ip, (intmax_t)interface, port);
1035
int ret = start_mandos_communication(ip, (in_port_t)port,
1028
int ret = start_mandos_communication(ip, port, interface,
1037
1029
avahi_proto_to_af(proto));
1039
avahi_simple_poll_quit(simple_poll);
1031
avahi_simple_poll_quit(mc.simple_poll);
1041
if(not add_server(ip, (in_port_t)port, interface,
1042
avahi_proto_to_af(proto))){
1043
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1033
ret = add_server(ip, port, interface,
1034
avahi_proto_to_af(proto));
1216
* This function determines if a network interface is up.
1218
bool interface_is_up(const char *interface){
1220
if(not get_flags(interface, &ifr)){
1222
fprintf_plus(stderr, "Failed to get flags for interface "
1223
"\"%s\"\n", interface);
1228
return (bool)(ifr.ifr_flags & IFF_UP);
1232
* This function determines if a network interface is running
1234
bool interface_is_running(const char *interface){
1236
if(not get_flags(interface, &ifr)){
1238
fprintf_plus(stderr, "Failed to get flags for interface "
1239
"\"%s\"\n", interface);
1244
return (bool)(ifr.ifr_flags & IFF_RUNNING);
1198
* This function determines if a directory entry in /sys/class/net
1199
* corresponds to an acceptable network device which is up.
1200
* (This function is passed to scandir(3) as a filter function.)
1202
int up_interface(const struct dirent *if_entry){
1203
if(if_entry->d_name[0] == '.'){
1208
if(not get_flags(if_entry->d_name, &ifr)){
1210
fprintf_plus(stderr, "Failed to get flags for interface "
1211
"\"%s\"\n", if_entry->d_name);
1216
/* Reject down interfaces */
1217
if(not (ifr.ifr_flags & IFF_UP)){
1219
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1225
/* Reject non-running interfaces */
1226
if(not (ifr.ifr_flags & IFF_RUNNING)){
1228
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1234
if(not good_flags(if_entry->d_name, &ifr)){
1247
1240
int notdotentries(const struct dirent *direntry){
1391
/* Set effective uid to 0, return errno */
1392
error_t raise_privileges(void){
1393
error_t old_errno = errno;
1394
error_t ret_errno = 0;
1395
if(seteuid(0) == -1){
1397
perror_plus("seteuid");
1403
/* Set effective and real user ID to 0. Return errno. */
1404
error_t raise_privileges_permanently(void){
1405
error_t old_errno = errno;
1406
error_t ret_errno = raise_privileges();
1411
if(setuid(0) == -1){
1413
perror_plus("seteuid");
1419
/* Set effective user ID to unprivileged saved user ID */
1420
error_t lower_privileges(void){
1421
error_t old_errno = errno;
1422
error_t ret_errno = 0;
1423
if(seteuid(uid) == -1){
1425
perror_plus("seteuid");
1431
/* Lower privileges permanently */
1432
error_t lower_privileges_permanently(void){
1433
error_t old_errno = errno;
1434
error_t ret_errno = 0;
1435
if(setuid(uid) == -1){
1437
perror_plus("setuid");
1443
1384
bool run_network_hooks(const char *mode, const char *interface,
1444
1385
const float delay){
1445
1386
struct dirent **direntries;
1576
error_t bring_up_interface(const char *const interface,
1579
error_t old_errno = errno;
1580
error_t ret_errno = 0;
1581
int ret, ret_setflags;
1582
struct ifreq network;
1583
unsigned int if_index = if_nametoindex(interface);
1585
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1595
if(not interface_is_up(interface)){
1596
if(not get_flags(interface, &network) and debug){
1598
fprintf_plus(stderr, "Failed to get flags for interface "
1599
"\"%s\"\n", interface);
1602
network.ifr_flags |= IFF_UP;
1604
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1607
perror_plus("socket");
1619
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1623
/* Raise priviliges */
1627
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1628
messages about the network interface to mess up the prompt */
1629
int ret_linux = klogctl(8, NULL, 5);
1630
bool restore_loglevel = true;
1631
if(ret_linux == -1){
1632
restore_loglevel = false;
1633
perror_plus("klogctl");
1635
#endif /* __linux__ */
1636
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1639
if(restore_loglevel){
1640
ret_linux = klogctl(7, NULL, 0);
1641
if(ret_linux == -1){
1642
perror_plus("klogctl");
1645
#endif /* __linux__ */
1647
/* Lower privileges */
1650
/* Close the socket */
1651
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1653
perror_plus("close");
1656
if(ret_setflags == -1){
1658
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1663
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1667
/* Sleep checking until interface is running.
1668
Check every 0.25s, up to total time of delay */
1669
for(int i=0; i < delay * 4; i++){
1670
if(interface_is_running(interface)){
1673
struct timespec sleeptime = { .tv_nsec = 250000000 };
1674
ret = nanosleep(&sleeptime, NULL);
1675
if(ret == -1 and errno != EINTR){
1676
perror_plus("nanosleep");
1684
error_t take_down_interface(const char *const interface){
1686
error_t old_errno = errno;
1687
error_t ret_errno = 0;
1688
int ret, ret_setflags;
1689
struct ifreq network;
1690
unsigned int if_index = if_nametoindex(interface);
1692
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1696
if(interface_is_up(interface)){
1697
if(not get_flags(interface, &network) and debug){
1699
fprintf_plus(stderr, "Failed to get flags for interface "
1700
"\"%s\"\n", interface);
1703
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1705
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1708
perror_plus("socket");
1714
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1718
/* Raise priviliges */
1721
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1724
/* Lower privileges */
1727
/* Close the socket */
1728
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1730
perror_plus("close");
1733
if(ret_setflags == -1){
1735
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1740
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1748
1513
int main(int argc, char *argv[]){
1749
1514
AvahiSServiceBrowser *sb = NULL;
1752
1517
intmax_t tmpmax;
1754
1519
int exitcode = EXIT_SUCCESS;
1755
char *interfaces = NULL;
1756
size_t interfaces_size = 0;
1757
char *interfaces_to_take_down = NULL;
1758
size_t interfaces_to_take_down_size = 0;
1520
const char *interface = "";
1521
struct ifreq network;
1523
bool take_down_interface = false;
1759
1526
char tempdir[] = "/tmp/mandosXXXXXX";
1760
1527
bool tempdir_created = false;
1761
1528
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1762
1529
const char *seckey = PATHDIR "/" SECKEY;
1763
1530
const char *pubkey = PATHDIR "/" PUBKEY;
1764
char *interfaces_hooks = NULL;
1765
size_t interfaces_hooks_size = 0;
1767
1532
bool gnutls_initialized = false;
1768
1533
bool gpgme_initialized = false;
2004
/* Remove empty interface names */
2006
char *interface = NULL;
2007
while((interface = argz_next(interfaces, interfaces_size,
2009
if(if_nametoindex(interface) == 0){
2010
if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2011
fprintf_plus(stderr, "Not using nonexisting interface"
2012
" \"%s\"\n", interface);
2014
argz_delete(&interfaces, &interfaces_size, interface);
2020
1771
/* Run network hooks */
2023
if(interfaces != NULL){
2024
interfaces_hooks = malloc(interfaces_size);
2025
if(interfaces_hooks == NULL){
2026
perror_plus("malloc");
2029
memcpy(interfaces_hooks, interfaces, interfaces_size);
2030
interfaces_hooks_size = interfaces_size;
2031
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2034
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2035
interfaces_hooks : "", delay)){
1772
if(not run_network_hooks("start", interface, delay)){
2041
1777
avahi_set_log_function(empty_log);
1780
if(interface[0] == '\0'){
1781
struct dirent **direntries;
1782
/* First look for interfaces that are up */
1783
ret = scandir(sys_class_net, &direntries, up_interface,
1786
/* No up interfaces, look for any good interfaces */
1788
ret = scandir(sys_class_net, &direntries, good_interface,
1792
/* Pick the first interface returned */
1793
interface = strdup(direntries[0]->d_name);
1795
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1797
if(interface == NULL){
1798
perror_plus("malloc");
1800
exitcode = EXIT_FAILURE;
1806
fprintf_plus(stderr, "Could not find a network interface\n");
1807
exitcode = EXIT_FAILURE;
2044
1812
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
2045
1813
from the signal handler */
2046
1814
/* Initialize the pseudo-RNG for Avahi */
2047
1815
srand((unsigned int) time(NULL));
2048
simple_poll = avahi_simple_poll_new();
2049
if(simple_poll == NULL){
1816
mc.simple_poll = avahi_simple_poll_new();
1817
if(mc.simple_poll == NULL){
2050
1818
fprintf_plus(stderr,
2051
1819
"Avahi: Failed to create simple poll object.\n");
2052
1820
exitcode = EX_UNAVAILABLE;
2119
/* If no interfaces were specified, make a list */
2120
if(interfaces == NULL){
2121
struct dirent **direntries;
2122
/* Look for any good interfaces */
2123
ret = scandir(sys_class_net, &direntries, good_interface,
2126
/* Add all found interfaces to interfaces list */
2127
for(int i = 0; i < ret; ++i){
2128
ret_errno = argz_add(&interfaces, &interfaces_size,
2129
direntries[i]->d_name);
2131
perror_plus("argz_add");
2135
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2136
direntries[i]->d_name);
2142
fprintf_plus(stderr, "Could not find a network interface\n");
2143
exitcode = EXIT_FAILURE;
2148
/* If we only got one interface, explicitly use only that one */
2149
if(argz_count(interfaces, interfaces_size) == 1){
2151
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2154
if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2157
/* Bring up interfaces which are down */
2158
if(not (argz_count(interfaces, interfaces_size) == 1
2159
and strcmp(interfaces, "none") == 0)){
2160
char *interface = NULL;
2161
while((interface = argz_next(interfaces, interfaces_size,
2163
bool interface_was_up = interface_is_up(interface);
2164
ret = bring_up_interface(interface, delay);
2165
if(not interface_was_up){
2168
perror_plus("Failed to bring up interface");
2170
ret_errno = argz_add(&interfaces_to_take_down,
2171
&interfaces_to_take_down_size,
2178
interfaces_size = 0;
2179
if(debug and (interfaces_to_take_down == NULL)){
2180
fprintf_plus(stderr, "No interfaces were brought up\n");
1887
/* If the interface is down, bring it up */
1888
if(strcmp(interface, "none") != 0){
1889
if_index = (AvahiIfIndex) if_nametoindex(interface);
1891
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1892
exitcode = EX_UNAVAILABLE;
1900
/* Re-raise priviliges */
1904
perror_plus("seteuid");
1908
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1909
messages about the network interface to mess up the prompt */
1910
ret = klogctl(8, NULL, 5);
1911
bool restore_loglevel = true;
1913
restore_loglevel = false;
1914
perror_plus("klogctl");
1916
#endif /* __linux__ */
1918
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1920
perror_plus("socket");
1921
exitcode = EX_OSERR;
1923
if(restore_loglevel){
1924
ret = klogctl(7, NULL, 0);
1926
perror_plus("klogctl");
1929
#endif /* __linux__ */
1930
/* Lower privileges */
1934
perror_plus("seteuid");
1938
strcpy(network.ifr_name, interface);
1939
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1941
perror_plus("ioctl SIOCGIFFLAGS");
1943
if(restore_loglevel){
1944
ret = klogctl(7, NULL, 0);
1946
perror_plus("klogctl");
1949
#endif /* __linux__ */
1950
exitcode = EX_OSERR;
1951
/* Lower privileges */
1955
perror_plus("seteuid");
1959
if((network.ifr_flags & IFF_UP) == 0){
1960
network.ifr_flags |= IFF_UP;
1961
take_down_interface = true;
1962
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1964
take_down_interface = false;
1965
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1966
exitcode = EX_OSERR;
1968
if(restore_loglevel){
1969
ret = klogctl(7, NULL, 0);
1971
perror_plus("klogctl");
1974
#endif /* __linux__ */
1975
/* Lower privileges */
1979
perror_plus("seteuid");
1984
/* Sleep checking until interface is running.
1985
Check every 0.25s, up to total time of delay */
1986
for(int i=0; i < delay * 4; i++){
1987
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1989
perror_plus("ioctl SIOCGIFFLAGS");
1990
} else if(network.ifr_flags & IFF_RUNNING){
1993
struct timespec sleeptime = { .tv_nsec = 250000000 };
1994
ret = nanosleep(&sleeptime, NULL);
1995
if(ret == -1 and errno != EINTR){
1996
perror_plus("nanosleep");
1999
if(not take_down_interface){
2000
/* We won't need the socket anymore */
2001
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2003
perror_plus("close");
2007
if(restore_loglevel){
2008
/* Restores kernel loglevel to default */
2009
ret = klogctl(7, NULL, 0);
2011
perror_plus("klogctl");
2014
#endif /* __linux__ */
2015
/* Lower privileges */
2017
/* Lower privileges */
2020
perror_plus("seteuid");
2230
/* Run network hooks */
2231
run_network_hooks("stop", interface, delay);
2390
2233
/* Re-raise priviliges */
2394
/* Run network hooks */
2395
run_network_hooks("stop", interfaces_hooks != NULL ?
2396
interfaces_hooks : "", delay);
2398
/* Take down the network interfaces which were brought up */
2400
char *interface = NULL;
2401
while((interface=argz_next(interfaces_to_take_down,
2402
interfaces_to_take_down_size,
2404
ret_errno = take_down_interface(interface);
2407
perror_plus("Failed to take down interface");
2238
perror_plus("seteuid");
2241
/* Take down the network interface */
2242
if(take_down_interface and geteuid() == 0){
2243
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2245
perror_plus("ioctl SIOCGIFFLAGS");
2246
} else if(network.ifr_flags & IFF_UP){
2247
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2248
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2250
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2410
if(debug and (interfaces_to_take_down == NULL)){
2411
fprintf_plus(stderr, "No interfaces needed to be taken"
2253
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2255
perror_plus("close");
2416
lower_privileges_permanently();
2419
free(interfaces_to_take_down);
2420
free(interfaces_hooks);
2259
/* Lower privileges permanently */
2263
perror_plus("setuid");
2422
2266
/* Removes the GPGME temp directory and all files inside */
2423
2267
if(tempdir_created){