62
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
64
#include <assert.h> /* assert() */
65
64
#include <errno.h> /* perror(), errno,
66
65
program_invocation_short_name */
67
66
#include <time.h> /* nanosleep(), time(), sleep() */
88
87
#include <sys/wait.h> /* waitpid(), WIFEXITED(),
89
88
WEXITSTATUS(), WTERMSIG() */
90
89
#include <grp.h> /* setgroups() */
90
#include <argz.h> /* argz_add_sep(), argz_next(),
91
argz_delete(), argz_append(),
92
argz_stringify(), argz_add(),
93
96
#include <sys/klog.h> /* klogctl() */
161
163
server *current_server;
162
164
} mandos_context;
164
/* global context so signal handler can reach it*/
165
mandos_context mc = { .simple_poll = NULL, .server = NULL,
166
.dh_bits = 1024, .priority = "SECURE256"
167
":!CTYPE-X.509:+CTYPE-OPENPGP",
168
.current_server = NULL };
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 };
170
172
sig_atomic_t quit_now = 0;
171
173
int signal_received = 0;
734
731
if(if_indextoname((unsigned int)if_index, interface) == NULL){
735
732
perror_plus("if_indextoname");
737
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
738
"\n", ip, interface, port);
734
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
735
"\n", ip, interface, (uintmax_t)port);
741
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
738
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
739
ip, (uintmax_t)port);
744
741
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
745
742
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1033
1032
PRIdMAX ") on port %" PRIu16 "\n", name,
1034
1033
host_name, ip, (intmax_t)interface, port);
1036
int ret = start_mandos_communication(ip, port, interface,
1035
int ret = start_mandos_communication(ip, (in_port_t)port,
1037
1037
avahi_proto_to_af(proto));
1039
avahi_simple_poll_quit(mc.simple_poll);
1039
avahi_simple_poll_quit(simple_poll);
1041
if(not add_server(ip, port, interface,
1041
if(not add_server(ip, (in_port_t)port, interface,
1042
1042
avahi_proto_to_af(proto))){
1043
1043
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1044
1044
" list\n", name);
1113
1115
signal_received = sig;
1114
1116
int old_errno = errno;
1115
1117
/* set main loop to exit */
1116
if(mc.simple_poll != NULL){
1117
avahi_simple_poll_quit(mc.simple_poll);
1118
if(simple_poll != NULL){
1119
avahi_simple_poll_quit(simple_poll);
1119
1121
errno = old_errno;
1122
1124
bool get_flags(const char *ifname, struct ifreq *ifr){
1125
1128
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1127
1131
perror_plus("socket");
1130
1135
strcpy(ifr->ifr_name, ifname);
1131
1136
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1134
1140
perror_plus("ioctl SIOCGIFFLAGS");
1209
* This function determines if a directory entry in /sys/class/net
1210
* corresponds to an acceptable network device which is up.
1211
* (This function is passed to scandir(3) as a filter function.)
1213
int up_interface(const struct dirent *if_entry){
1214
if(if_entry->d_name[0] == '.'){
1219
if(not get_flags(if_entry->d_name, &ifr)){
1221
fprintf_plus(stderr, "Failed to get flags for interface "
1222
"\"%s\"\n", if_entry->d_name);
1227
/* Reject down interfaces */
1228
if(not (ifr.ifr_flags & IFF_UP)){
1230
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1236
/* Reject non-running interfaces */
1237
if(not (ifr.ifr_flags & IFF_RUNNING)){
1239
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1245
if(not good_flags(if_entry->d_name, &ifr)){
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);
1251
1247
int notdotentries(const struct dirent *direntry){
1395
1391
/* Set effective uid to 0, return errno */
1396
int raise_privileges(void){
1397
int old_errno = errno;
1392
error_t raise_privileges(void){
1393
error_t old_errno = errno;
1394
error_t ret_errno = 0;
1400
1395
if(seteuid(0) == -1){
1401
1397
perror_plus("seteuid");
1404
1399
errno = old_errno;
1405
1400
return ret_errno;
1408
1403
/* 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();
1404
error_t raise_privileges_permanently(void){
1405
error_t old_errno = errno;
1406
error_t ret_errno = raise_privileges();
1412
1407
if(ret_errno != 0){
1413
1408
errno = old_errno;
1414
1409
return ret_errno;
1417
1411
if(setuid(0) == -1){
1418
1413
perror_plus("seteuid");
1421
1415
errno = old_errno;
1422
1416
return ret_errno;
1425
1419
/* Set effective user ID to unprivileged saved user ID */
1426
int lower_privileges(void){
1427
int old_errno = errno;
1420
error_t lower_privileges(void){
1421
error_t old_errno = errno;
1422
error_t ret_errno = 0;
1430
1423
if(seteuid(uid) == -1){
1431
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");
1434
1439
errno = old_errno;
1435
1440
return ret_errno;
1564
int bring_up_interface(const char * const interface, const float delay){
1576
error_t bring_up_interface(const char *const interface,
1566
int old_errno = errno;
1579
error_t old_errno = errno;
1580
error_t ret_errno = 0;
1581
int ret, ret_setflags;
1569
1582
struct ifreq network;
1570
AvahiIfIndex if_index = (AvahiIfIndex)if_nametoindex(interface);
1583
unsigned int if_index = if_nametoindex(interface);
1571
1584
if(if_index == 0){
1572
1585
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1573
1586
errno = old_errno;
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){
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);
1632
1602
network.ifr_flags |= IFF_UP;
1633
ret = ioctl(sd, SIOCSIFFLAGS, &network);
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){
1636
1658
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 */
1647
1659
errno = old_errno;
1648
1660
return ret_errno;
1663
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1651
1667
/* Sleep checking until interface is running.
1652
1668
Check every 0.25s, up to total time of delay */
1653
1669
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){
1670
if(interface_is_running(interface)){
1660
1673
struct timespec sleeptime = { .tv_nsec = 250000000 };
1663
1676
perror_plus("nanosleep");
1666
/* Close the socket */
1667
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1669
perror_plus("close");
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);
1672
if(restore_loglevel){
1673
/* Restores kernel loglevel to default */
1674
ret = klogctl(7, NULL, 0);
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));
1676
perror_plus("klogctl");
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",
1679
#endif /* __linux__ */
1680
/* Lower privileges */
1682
1744
errno = old_errno;
1686
1748
int main(int argc, char *argv[]){
1687
1749
AvahiSServiceBrowser *sb = NULL;
1690
1752
intmax_t tmpmax;
1692
1754
int exitcode = EXIT_SUCCESS;
1693
const char *interface = "";
1694
struct ifreq network;
1696
bool take_down_interface = false;
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;
1697
1759
char tempdir[] = "/tmp/mandosXXXXXX";
1698
1760
bool tempdir_created = false;
1699
1761
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1700
1762
const char *seckey = PATHDIR "/" SECKEY;
1701
1763
const char *pubkey = PATHDIR "/" PUBKEY;
1764
char *interfaces_hooks = NULL;
1765
size_t interfaces_hooks_size = 0;
1703
1767
bool gnutls_initialized = false;
1704
1768
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);
1936
2020
/* Run network hooks */
1937
if(not run_network_hooks("start", interface, delay)){
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)){
1942
2041
avahi_set_log_function(empty_log);
1945
if(interface[0] == '\0'){
1946
struct dirent **direntries;
1947
/* First look for interfaces that are up */
1948
ret = scandir(sys_class_net, &direntries, up_interface,
1951
/* No up interfaces, look for any good interfaces */
1953
ret = scandir(sys_class_net, &direntries, good_interface,
1957
/* Pick the first interface returned */
1958
interface = strdup(direntries[0]->d_name);
1960
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1962
if(interface == NULL){
1963
perror_plus("malloc");
1965
exitcode = EXIT_FAILURE;
1971
fprintf_plus(stderr, "Could not find a network interface\n");
1972
exitcode = EXIT_FAILURE;
1977
2044
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1978
2045
from the signal handler */
1979
2046
/* Initialize the pseudo-RNG for Avahi */
1980
2047
srand((unsigned int) time(NULL));
1981
mc.simple_poll = avahi_simple_poll_new();
1982
if(mc.simple_poll == NULL){
2048
simple_poll = avahi_simple_poll_new();
2049
if(simple_poll == NULL){
1983
2050
fprintf_plus(stderr,
1984
2051
"Avahi: Failed to create simple poll object.\n");
1985
2052
exitcode = EX_UNAVAILABLE;
2052
/* If the interface is down, bring it up */
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");
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");
2180
2303
config.publish_domain = 0;
2182
2305
/* Allocate a new server */
2183
mc.server = avahi_server_new(avahi_simple_poll_get
2184
(mc.simple_poll), &config, NULL,
2306
mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2307
&config, NULL, NULL, &ret_errno);
2187
2309
/* Free the Avahi configuration data */
2188
2310
avahi_server_config_free(&config);
2221
2343
fprintf_plus(stderr, "Starting Avahi loop search\n");
2224
ret = avahi_loop_with_timeout(mc.simple_poll,
2346
ret = avahi_loop_with_timeout(simple_poll,
2225
2347
(int)(retry_interval * 1000));
2227
2349
fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2268
/* Run network hooks */
2269
run_network_hooks("stop", interface, delay);
2271
2390
/* Re-raise priviliges */
2273
2392
raise_privileges();
2275
/* Take down the network interface */
2276
if(take_down_interface and geteuid() == 0){
2277
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2279
perror_plus("ioctl SIOCGIFFLAGS");
2280
} else if(network.ifr_flags & IFF_UP){
2281
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2282
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2284
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
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");
2287
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2289
perror_plus("close");
2410
if(debug and (interfaces_to_take_down == NULL)){
2411
fprintf_plus(stderr, "No interfaces needed to be taken"
2293
/* Lower privileges permanently */
2297
perror_plus("setuid");
2416
lower_privileges_permanently();
2419
free(interfaces_to_take_down);
2420
free(interfaces_hooks);
2300
2422
/* Removes the GPGME temp directory and all files inside */
2301
2423
if(tempdir_created){