224
225
/* Special case of first server */
225
if (mc.current_server == NULL){
226
if(*current_server == NULL){
226
227
new_server->next = new_server;
227
228
new_server->prev = new_server;
228
mc.current_server = new_server;
229
*current_server = new_server;
229
230
/* Place the new server last in the list */
231
new_server->next = mc.current_server;
232
new_server->prev = mc.current_server->prev;
232
new_server->next = *current_server;
233
new_server->prev = (*current_server)->prev;
233
234
new_server->prev->next = new_server;
234
mc.current_server->prev = new_server;
235
(*current_server)->prev = new_server;
236
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
237
ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
238
239
perror_plus("clock_gettime");
375
376
/* Decrypt data from the cryptotext data buffer to the plaintext
377
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
378
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
378
379
if(rc != GPG_ERR_NO_ERROR){
379
380
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
380
381
gpgme_strsource(rc), gpgme_strerror(rc));
381
382
plaintext_length = -1;
383
384
gpgme_decrypt_result_t result;
384
result = gpgme_op_decrypt_result(mc.ctx);
385
result = gpgme_op_decrypt_result(mc->ctx);
385
386
if(result == NULL){
386
387
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
532
533
/* GnuTLS server initialization */
533
ret = gnutls_dh_params_init(&mc.dh_params);
534
ret = gnutls_dh_params_init(&mc->dh_params);
534
535
if(ret != GNUTLS_E_SUCCESS){
535
536
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
536
537
" initialization: %s\n",
537
538
safer_gnutls_strerror(ret));
540
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
541
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
541
542
if(ret != GNUTLS_E_SUCCESS){
542
543
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
543
544
safer_gnutls_strerror(ret));
547
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
548
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
553
gnutls_certificate_free_credentials(mc.cred);
554
gnutls_certificate_free_credentials(mc->cred);
554
555
gnutls_global_deinit();
555
gnutls_dh_params_deinit(mc.dh_params);
556
gnutls_dh_params_deinit(mc->dh_params);
559
static int init_gnutls_session(gnutls_session_t *session){
560
static int init_gnutls_session(gnutls_session_t *session,
561
563
/* GnuTLS session creation */
732
731
if(if_indextoname((unsigned int)if_index, interface) == NULL){
733
732
perror_plus("if_indextoname");
735
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
736
"\n", ip, interface, port);
734
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
735
"\n", ip, interface, (uintmax_t)port);
739
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
738
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
739
ip, (uintmax_t)port);
742
741
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
743
742
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1028
1033
PRIdMAX ") on port %" PRIu16 "\n", name,
1029
1034
host_name, ip, (intmax_t)interface, port);
1031
int ret = start_mandos_communication(ip, port, interface,
1032
avahi_proto_to_af(proto));
1036
int ret = start_mandos_communication(ip, (in_port_t)port,
1038
avahi_proto_to_af(proto),
1034
avahi_simple_poll_quit(mc.simple_poll);
1041
avahi_simple_poll_quit(simple_poll);
1036
if(not add_server(ip, port, interface,
1037
avahi_proto_to_af(proto))){
1043
if(not add_server(ip, (in_port_t)port, interface,
1044
avahi_proto_to_af(proto),
1045
&((mandos_context*)mc)->current_server)){
1038
1046
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1039
1047
" list\n", name);
1078
1089
the callback function is called the Avahi server will free the
1079
1090
resolver for us. */
1081
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1082
name, type, domain, protocol, 0,
1083
resolve_callback, NULL) == NULL)
1092
if(avahi_s_service_resolver_new(((mandos_context*)mc)->server,
1093
interface, protocol, name, type,
1094
domain, protocol, 0,
1095
resolve_callback, mc) == NULL)
1084
1096
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1086
avahi_strerror(avahi_server_errno(mc.server)));
1098
avahi_strerror(avahi_server_errno
1099
(((mandos_context*)mc)->server)));
1089
1102
case AVAHI_BROWSER_REMOVE:
1204
* This function determines if a directory entry in /sys/class/net
1205
* corresponds to an acceptable network device which is up.
1206
* (This function is passed to scandir(3) as a filter function.)
1208
int up_interface(const struct dirent *if_entry){
1209
if(if_entry->d_name[0] == '.'){
1214
if(not get_flags(if_entry->d_name, &ifr)){
1216
fprintf_plus(stderr, "Failed to get flags for interface "
1217
"\"%s\"\n", if_entry->d_name);
1222
/* Reject down interfaces */
1223
if(not (ifr.ifr_flags & IFF_UP)){
1225
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1231
/* Reject non-running interfaces */
1232
if(not (ifr.ifr_flags & IFF_RUNNING)){
1234
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1240
if(not good_flags(if_entry->d_name, &ifr)){
1222
* This function determines if a network interface is up.
1224
bool interface_is_up(const char *interface){
1226
if(not get_flags(interface, &ifr)){
1228
fprintf_plus(stderr, "Failed to get flags for interface "
1229
"\"%s\"\n", interface);
1234
return (bool)(ifr.ifr_flags & IFF_UP);
1238
* This function determines if a network interface is running
1240
bool interface_is_running(const char *interface){
1242
if(not get_flags(interface, &ifr)){
1244
fprintf_plus(stderr, "Failed to get flags for interface "
1245
"\"%s\"\n", interface);
1250
return (bool)(ifr.ifr_flags & IFF_RUNNING);
1246
1253
int notdotentries(const struct dirent *direntry){
1360
1368
if(block_time <= 0){
1361
ret = start_mandos_communication(mc.current_server->ip,
1362
mc.current_server->port,
1363
mc.current_server->if_index,
1364
mc.current_server->af);
1369
ret = start_mandos_communication(mc->current_server->ip,
1370
mc->current_server->port,
1371
mc->current_server->if_index,
1372
mc->current_server->af, mc);
1366
avahi_simple_poll_quit(mc.simple_poll);
1374
avahi_simple_poll_quit(s);
1369
1377
ret = clock_gettime(CLOCK_MONOTONIC,
1370
&mc.current_server->last_seen);
1378
&mc->current_server->last_seen);
1372
1380
perror_plus("clock_gettime");
1375
mc.current_server = mc.current_server->next;
1383
mc->current_server = mc->current_server->next;
1376
1384
block_time = 0; /* Call avahi to find new Mandos
1377
1385
servers, but don't block */
1398
/* Set effective uid to 0, return errno */
1399
error_t raise_privileges(void){
1400
error_t old_errno = errno;
1401
error_t ret_errno = 0;
1402
if(seteuid(0) == -1){
1404
perror_plus("seteuid");
1410
/* Set effective and real user ID to 0. Return errno. */
1411
error_t raise_privileges_permanently(void){
1412
error_t old_errno = errno;
1413
error_t ret_errno = raise_privileges();
1418
if(setuid(0) == -1){
1420
perror_plus("seteuid");
1426
/* Set effective user ID to unprivileged saved user ID */
1427
error_t lower_privileges(void){
1428
error_t old_errno = errno;
1429
error_t ret_errno = 0;
1430
if(seteuid(uid) == -1){
1432
perror_plus("seteuid");
1438
/* Lower privileges permanently */
1439
error_t lower_privileges_permanently(void){
1440
error_t old_errno = errno;
1441
error_t ret_errno = 0;
1442
if(setuid(uid) == -1){
1444
perror_plus("setuid");
1390
1450
bool run_network_hooks(const char *mode, const char *interface,
1391
1451
const float delay){
1392
1452
struct dirent **direntries;
1583
error_t bring_up_interface(const char *const interface,
1586
error_t old_errno = errno;
1587
error_t ret_errno = 0;
1588
int ret, ret_setflags;
1589
struct ifreq network;
1590
unsigned int if_index = if_nametoindex(interface);
1592
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1602
if(not interface_is_up(interface)){
1603
if(not get_flags(interface, &network) and debug){
1605
fprintf_plus(stderr, "Failed to get flags for interface "
1606
"\"%s\"\n", interface);
1609
network.ifr_flags |= IFF_UP;
1611
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1614
perror_plus("socket");
1626
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1630
/* Raise priviliges */
1634
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1635
messages about the network interface to mess up the prompt */
1636
int ret_linux = klogctl(8, NULL, 5);
1637
bool restore_loglevel = true;
1638
if(ret_linux == -1){
1639
restore_loglevel = false;
1640
perror_plus("klogctl");
1642
#endif /* __linux__ */
1643
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1646
if(restore_loglevel){
1647
ret_linux = klogctl(7, NULL, 0);
1648
if(ret_linux == -1){
1649
perror_plus("klogctl");
1652
#endif /* __linux__ */
1654
/* Lower privileges */
1657
/* Close the socket */
1658
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1660
perror_plus("close");
1663
if(ret_setflags == -1){
1665
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1670
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1674
/* Sleep checking until interface is running.
1675
Check every 0.25s, up to total time of delay */
1676
for(int i=0; i < delay * 4; i++){
1677
if(interface_is_running(interface)){
1680
struct timespec sleeptime = { .tv_nsec = 250000000 };
1681
ret = nanosleep(&sleeptime, NULL);
1682
if(ret == -1 and errno != EINTR){
1683
perror_plus("nanosleep");
1691
error_t take_down_interface(const char *const interface){
1693
error_t old_errno = errno;
1694
error_t ret_errno = 0;
1695
int ret, ret_setflags;
1696
struct ifreq network;
1697
unsigned int if_index = if_nametoindex(interface);
1699
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1703
if(interface_is_up(interface)){
1704
if(not get_flags(interface, &network) and debug){
1706
fprintf_plus(stderr, "Failed to get flags for interface "
1707
"\"%s\"\n", interface);
1710
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1712
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1715
perror_plus("socket");
1721
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1725
/* Raise priviliges */
1728
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1731
/* Lower privileges */
1734
/* Close the socket */
1735
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1737
perror_plus("close");
1740
if(ret_setflags == -1){
1742
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1747
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1519
1755
int main(int argc, char *argv[]){
1756
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1757
.priority = "SECURE256:!CTYPE-X.509:"
1758
"+CTYPE-OPENPGP", .current_server = NULL };
1520
1759
AvahiSServiceBrowser *sb = NULL;
1523
1762
intmax_t tmpmax;
1525
1764
int exitcode = EXIT_SUCCESS;
1526
const char *interface = "";
1527
struct ifreq network;
1529
bool take_down_interface = false;
1765
char *interfaces = NULL;
1766
size_t interfaces_size = 0;
1767
char *interfaces_to_take_down = NULL;
1768
size_t interfaces_to_take_down_size = 0;
1532
1769
char tempdir[] = "/tmp/mandosXXXXXX";
1533
1770
bool tempdir_created = false;
1534
1771
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1535
1772
const char *seckey = PATHDIR "/" SECKEY;
1536
1773
const char *pubkey = PATHDIR "/" PUBKEY;
1774
char *interfaces_hooks = NULL;
1775
size_t interfaces_hooks_size = 0;
1538
1777
bool gnutls_initialized = false;
1539
1778
bool gpgme_initialized = false;
2014
/* Remove empty interface names */
2016
char *interface = NULL;
2017
while((interface = argz_next(interfaces, interfaces_size,
2019
if(if_nametoindex(interface) == 0){
2020
if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2021
fprintf_plus(stderr, "Not using nonexisting interface"
2022
" \"%s\"\n", interface);
2024
argz_delete(&interfaces, &interfaces_size, interface);
1775
2030
/* Run network hooks */
1776
if(not run_network_hooks("start", interface, delay)){
2033
if(interfaces != NULL){
2034
interfaces_hooks = malloc(interfaces_size);
2035
if(interfaces_hooks == NULL){
2036
perror_plus("malloc");
2039
memcpy(interfaces_hooks, interfaces, interfaces_size);
2040
interfaces_hooks_size = interfaces_size;
2041
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2044
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2045
interfaces_hooks : "", delay)){
1781
2051
avahi_set_log_function(empty_log);
1784
if(interface[0] == '\0'){
1785
struct dirent **direntries;
1786
/* First look for interfaces that are up */
1787
ret = scandir(sys_class_net, &direntries, up_interface,
1790
/* No up interfaces, look for any good interfaces */
1792
ret = scandir(sys_class_net, &direntries, good_interface,
1796
/* Pick the first interface returned */
1797
interface = strdup(direntries[0]->d_name);
1799
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1801
if(interface == NULL){
1802
perror_plus("malloc");
1804
exitcode = EXIT_FAILURE;
1810
fprintf_plus(stderr, "Could not find a network interface\n");
1811
exitcode = EXIT_FAILURE;
1816
2054
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1817
2055
from the signal handler */
1818
2056
/* Initialize the pseudo-RNG for Avahi */
1819
2057
srand((unsigned int) time(NULL));
1820
mc.simple_poll = avahi_simple_poll_new();
1821
if(mc.simple_poll == NULL){
2058
simple_poll = avahi_simple_poll_new();
2059
if(simple_poll == NULL){
1822
2060
fprintf_plus(stderr,
1823
2061
"Avahi: Failed to create simple poll object.\n");
1824
2062
exitcode = EX_UNAVAILABLE;
1891
/* If the interface is down, bring it up */
1892
if(strcmp(interface, "none") != 0){
1893
if_index = (AvahiIfIndex) if_nametoindex(interface);
1895
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1896
exitcode = EX_UNAVAILABLE;
1904
/* Re-raise priviliges */
1908
perror_plus("seteuid");
1912
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1913
messages about the network interface to mess up the prompt */
1914
ret = klogctl(8, NULL, 5);
1915
bool restore_loglevel = true;
1917
restore_loglevel = false;
1918
perror_plus("klogctl");
1920
#endif /* __linux__ */
1922
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1924
perror_plus("socket");
1925
exitcode = EX_OSERR;
1927
if(restore_loglevel){
1928
ret = klogctl(7, NULL, 0);
1930
perror_plus("klogctl");
1933
#endif /* __linux__ */
1934
/* Lower privileges */
1938
perror_plus("seteuid");
1942
strcpy(network.ifr_name, interface);
1943
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1945
perror_plus("ioctl SIOCGIFFLAGS");
1947
if(restore_loglevel){
1948
ret = klogctl(7, NULL, 0);
1950
perror_plus("klogctl");
1953
#endif /* __linux__ */
1954
exitcode = EX_OSERR;
1955
/* Lower privileges */
1959
perror_plus("seteuid");
1963
if((network.ifr_flags & IFF_UP) == 0){
1964
network.ifr_flags |= IFF_UP;
1965
take_down_interface = true;
1966
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1968
take_down_interface = false;
1969
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1970
exitcode = EX_OSERR;
1972
if(restore_loglevel){
1973
ret = klogctl(7, NULL, 0);
1975
perror_plus("klogctl");
1978
#endif /* __linux__ */
1979
/* Lower privileges */
1983
perror_plus("seteuid");
1988
/* Sleep checking until interface is running.
1989
Check every 0.25s, up to total time of delay */
1990
for(int i=0; i < delay * 4; i++){
1991
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1993
perror_plus("ioctl SIOCGIFFLAGS");
1994
} else if(network.ifr_flags & IFF_RUNNING){
1997
struct timespec sleeptime = { .tv_nsec = 250000000 };
1998
ret = nanosleep(&sleeptime, NULL);
1999
if(ret == -1 and errno != EINTR){
2000
perror_plus("nanosleep");
2003
if(not take_down_interface){
2004
/* We won't need the socket anymore */
2005
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2007
perror_plus("close");
2011
if(restore_loglevel){
2012
/* Restores kernel loglevel to default */
2013
ret = klogctl(7, NULL, 0);
2015
perror_plus("klogctl");
2018
#endif /* __linux__ */
2019
/* Lower privileges */
2021
/* Lower privileges */
2024
perror_plus("seteuid");
2129
/* If no interfaces were specified, make a list */
2130
if(interfaces == NULL){
2131
struct dirent **direntries;
2132
/* Look for any good interfaces */
2133
ret = scandir(sys_class_net, &direntries, good_interface,
2136
/* Add all found interfaces to interfaces list */
2137
for(int i = 0; i < ret; ++i){
2138
ret_errno = argz_add(&interfaces, &interfaces_size,
2139
direntries[i]->d_name);
2141
perror_plus("argz_add");
2145
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2146
direntries[i]->d_name);
2152
fprintf_plus(stderr, "Could not find a network interface\n");
2153
exitcode = EXIT_FAILURE;
2158
/* If we only got one interface, explicitly use only that one */
2159
if(argz_count(interfaces, interfaces_size) == 1){
2161
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2164
if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2167
/* Bring up interfaces which are down */
2168
if(not (argz_count(interfaces, interfaces_size) == 1
2169
and strcmp(interfaces, "none") == 0)){
2170
char *interface = NULL;
2171
while((interface = argz_next(interfaces, interfaces_size,
2173
bool interface_was_up = interface_is_up(interface);
2174
ret = bring_up_interface(interface, delay);
2175
if(not interface_was_up){
2178
perror_plus("Failed to bring up interface");
2180
ret_errno = argz_add(&interfaces_to_take_down,
2181
&interfaces_to_take_down_size,
2188
interfaces_size = 0;
2189
if(debug and (interfaces_to_take_down == NULL)){
2190
fprintf_plus(stderr, "No interfaces were brought up\n");
2234
/* Run network hooks */
2235
run_network_hooks("stop", interface, delay);
2237
2402
/* Re-raise priviliges */
2242
perror_plus("seteuid");
2245
/* Take down the network interface */
2246
if(take_down_interface and geteuid() == 0){
2247
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2249
perror_plus("ioctl SIOCGIFFLAGS");
2250
} else if(network.ifr_flags & IFF_UP){
2251
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2252
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2254
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2406
/* Run network hooks */
2407
run_network_hooks("stop", interfaces_hooks != NULL ?
2408
interfaces_hooks : "", delay);
2410
/* Take down the network interfaces which were brought up */
2412
char *interface = NULL;
2413
while((interface=argz_next(interfaces_to_take_down,
2414
interfaces_to_take_down_size,
2416
ret_errno = take_down_interface(interface);
2419
perror_plus("Failed to take down interface");
2257
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2259
perror_plus("close");
2422
if(debug and (interfaces_to_take_down == NULL)){
2423
fprintf_plus(stderr, "No interfaces needed to be taken"
2263
/* Lower privileges permanently */
2267
perror_plus("setuid");
2428
lower_privileges_permanently();
2431
free(interfaces_to_take_down);
2432
free(interfaces_hooks);
2270
2434
/* Removes the GPGME temp directory and all files inside */
2271
2435
if(tempdir_created){