163
159
server *current_server;
164
160
} mandos_context;
166
/* global so signal handler can reach it*/
167
AvahiSimplePoll *simple_poll;
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 };
169
168
sig_atomic_t quit_now = 0;
170
169
int signal_received = 0;
172
171
/* Function to use when printing errors */
173
172
void perror_plus(const char *print_text){
175
173
fprintf(stderr, "Mandos plugin %s: ",
176
174
program_invocation_short_name);
178
175
perror(print_text);
225
222
/* Special case of first server */
226
if(*current_server == NULL){
223
if (mc.current_server == NULL){
227
224
new_server->next = new_server;
228
225
new_server->prev = new_server;
229
*current_server = new_server;
226
mc.current_server = new_server;
230
227
/* Place the new server last in the list */
232
new_server->next = *current_server;
233
new_server->prev = (*current_server)->prev;
229
new_server->next = mc.current_server;
230
new_server->prev = mc.current_server->prev;
234
231
new_server->prev->next = new_server;
235
(*current_server)->prev = new_server;
232
mc.current_server->prev = new_server;
237
ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
234
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
239
236
perror_plus("clock_gettime");
376
373
/* Decrypt data from the cryptotext data buffer to the plaintext
378
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
375
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
379
376
if(rc != GPG_ERR_NO_ERROR){
380
377
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
381
378
gpgme_strsource(rc), gpgme_strerror(rc));
382
379
plaintext_length = -1;
384
381
gpgme_decrypt_result_t result;
385
result = gpgme_op_decrypt_result(mc->ctx);
382
result = gpgme_op_decrypt_result(mc.ctx);
386
383
if(result == NULL){
387
384
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
533
530
/* GnuTLS server initialization */
534
ret = gnutls_dh_params_init(&mc->dh_params);
531
ret = gnutls_dh_params_init(&mc.dh_params);
535
532
if(ret != GNUTLS_E_SUCCESS){
536
533
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
537
534
" initialization: %s\n",
538
535
safer_gnutls_strerror(ret));
541
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
538
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
542
539
if(ret != GNUTLS_E_SUCCESS){
543
540
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
544
541
safer_gnutls_strerror(ret));
548
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
545
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
554
gnutls_certificate_free_credentials(mc->cred);
551
gnutls_certificate_free_credentials(mc.cred);
555
552
gnutls_global_deinit();
556
gnutls_dh_params_deinit(mc->dh_params);
553
gnutls_dh_params_deinit(mc.dh_params);
560
static int init_gnutls_session(gnutls_session_t *session,
557
static int init_gnutls_session(gnutls_session_t *session){
563
559
/* GnuTLS session creation */
731
730
if(if_indextoname((unsigned int)if_index, interface) == NULL){
732
731
perror_plus("if_indextoname");
734
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
735
"\n", ip, interface, (uintmax_t)port);
733
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
734
"\n", ip, interface, port);
738
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
739
ip, (uintmax_t)port);
737
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
741
740
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
742
741
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1033
1026
PRIdMAX ") on port %" PRIu16 "\n", name,
1034
1027
host_name, ip, (intmax_t)interface, port);
1036
int ret = start_mandos_communication(ip, (in_port_t)port,
1038
avahi_proto_to_af(proto),
1029
int ret = start_mandos_communication(ip, port, interface,
1030
avahi_proto_to_af(proto));
1041
avahi_simple_poll_quit(simple_poll);
1032
avahi_simple_poll_quit(mc.simple_poll);
1043
if(not add_server(ip, (in_port_t)port, interface,
1044
avahi_proto_to_af(proto),
1045
&((mandos_context*)mc)->current_server)){
1034
if(not add_server(ip, port, interface,
1035
avahi_proto_to_af(proto))){
1046
1036
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1047
1037
" list\n", name);
1089
1076
the callback function is called the Avahi server will free the
1090
1077
resolver for us. */
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)
1079
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1080
name, type, domain, protocol, 0,
1081
resolve_callback, NULL) == NULL)
1096
1082
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1098
avahi_strerror(avahi_server_errno
1099
(((mandos_context*)mc)->server)));
1084
avahi_strerror(avahi_server_errno(mc.server)));
1102
1087
case AVAHI_BROWSER_REMOVE:
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);
1202
* This function determines if a directory entry in /sys/class/net
1203
* corresponds to an acceptable network device which is up.
1204
* (This function is passed to scandir(3) as a filter function.)
1206
int up_interface(const struct dirent *if_entry){
1207
if(if_entry->d_name[0] == '.'){
1212
if(not get_flags(if_entry->d_name, &ifr)){
1214
fprintf_plus(stderr, "Failed to get flags for interface "
1215
"\"%s\"\n", if_entry->d_name);
1220
/* Reject down interfaces */
1221
if(not (ifr.ifr_flags & IFF_UP)){
1223
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1229
/* Reject non-running interfaces */
1230
if(not (ifr.ifr_flags & IFF_RUNNING)){
1232
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1238
if(not good_flags(if_entry->d_name, &ifr)){
1253
1244
int notdotentries(const struct dirent *direntry){
1368
1358
if(block_time <= 0){
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);
1359
ret = start_mandos_communication(mc.current_server->ip,
1360
mc.current_server->port,
1361
mc.current_server->if_index,
1362
mc.current_server->af);
1374
avahi_simple_poll_quit(s);
1364
avahi_simple_poll_quit(mc.simple_poll);
1377
1367
ret = clock_gettime(CLOCK_MONOTONIC,
1378
&mc->current_server->last_seen);
1368
&mc.current_server->last_seen);
1380
1370
perror_plus("clock_gettime");
1383
mc->current_server = mc->current_server->next;
1373
mc.current_server = mc.current_server->next;
1384
1374
block_time = 0; /* Call avahi to find new Mandos
1385
1375
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");
1450
1388
bool run_network_hooks(const char *mode, const char *interface,
1451
1389
const float delay){
1452
1390
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",
1755
1517
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 };
1759
1518
AvahiSServiceBrowser *sb = NULL;
1762
1521
intmax_t tmpmax;
1764
1523
int exitcode = EXIT_SUCCESS;
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;
1524
const char *interface = "";
1525
struct ifreq network;
1527
bool take_down_interface = false;
1769
1530
char tempdir[] = "/tmp/mandosXXXXXX";
1770
1531
bool tempdir_created = false;
1771
1532
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1772
1533
const char *seckey = PATHDIR "/" SECKEY;
1773
1534
const char *pubkey = PATHDIR "/" PUBKEY;
1774
char *interfaces_hooks = NULL;
1775
size_t interfaces_hooks_size = 0;
1777
1536
bool gnutls_initialized = false;
1778
1537
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);
2030
1773
/* Run network hooks */
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)){
1774
if(not run_network_hooks("start", interface, delay)){
2051
1779
avahi_set_log_function(empty_log);
1782
if(interface[0] == '\0'){
1783
struct dirent **direntries;
1784
/* First look for interfaces that are up */
1785
ret = scandir(sys_class_net, &direntries, up_interface,
1788
/* No up interfaces, look for any good interfaces */
1790
ret = scandir(sys_class_net, &direntries, good_interface,
1794
/* Pick the first interface returned */
1795
interface = strdup(direntries[0]->d_name);
1797
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1799
if(interface == NULL){
1800
perror_plus("malloc");
1802
exitcode = EXIT_FAILURE;
1808
fprintf_plus(stderr, "Could not find a network interface\n");
1809
exitcode = EXIT_FAILURE;
2054
1814
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
2055
1815
from the signal handler */
2056
1816
/* Initialize the pseudo-RNG for Avahi */
2057
1817
srand((unsigned int) time(NULL));
2058
simple_poll = avahi_simple_poll_new();
2059
if(simple_poll == NULL){
1818
mc.simple_poll = avahi_simple_poll_new();
1819
if(mc.simple_poll == NULL){
2060
1820
fprintf_plus(stderr,
2061
1821
"Avahi: Failed to create simple poll object.\n");
2062
1822
exitcode = EX_UNAVAILABLE;
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");
1889
/* If the interface is down, bring it up */
1890
if(strcmp(interface, "none") != 0){
1891
if_index = (AvahiIfIndex) if_nametoindex(interface);
1893
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1894
exitcode = EX_UNAVAILABLE;
1902
/* Re-raise priviliges */
1906
perror_plus("seteuid");
1910
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1911
messages about the network interface to mess up the prompt */
1912
ret = klogctl(8, NULL, 5);
1913
bool restore_loglevel = true;
1915
restore_loglevel = false;
1916
perror_plus("klogctl");
1918
#endif /* __linux__ */
1920
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1922
perror_plus("socket");
1923
exitcode = EX_OSERR;
1925
if(restore_loglevel){
1926
ret = klogctl(7, NULL, 0);
1928
perror_plus("klogctl");
1931
#endif /* __linux__ */
1932
/* Lower privileges */
1936
perror_plus("seteuid");
1940
strcpy(network.ifr_name, interface);
1941
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1943
perror_plus("ioctl SIOCGIFFLAGS");
1945
if(restore_loglevel){
1946
ret = klogctl(7, NULL, 0);
1948
perror_plus("klogctl");
1951
#endif /* __linux__ */
1952
exitcode = EX_OSERR;
1953
/* Lower privileges */
1957
perror_plus("seteuid");
1961
if((network.ifr_flags & IFF_UP) == 0){
1962
network.ifr_flags |= IFF_UP;
1963
take_down_interface = true;
1964
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1966
take_down_interface = false;
1967
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1968
exitcode = EX_OSERR;
1970
if(restore_loglevel){
1971
ret = klogctl(7, NULL, 0);
1973
perror_plus("klogctl");
1976
#endif /* __linux__ */
1977
/* Lower privileges */
1981
perror_plus("seteuid");
1986
/* Sleep checking until interface is running.
1987
Check every 0.25s, up to total time of delay */
1988
for(int i=0; i < delay * 4; i++){
1989
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1991
perror_plus("ioctl SIOCGIFFLAGS");
1992
} else if(network.ifr_flags & IFF_RUNNING){
1995
struct timespec sleeptime = { .tv_nsec = 250000000 };
1996
ret = nanosleep(&sleeptime, NULL);
1997
if(ret == -1 and errno != EINTR){
1998
perror_plus("nanosleep");
2001
if(not take_down_interface){
2002
/* We won't need the socket anymore */
2003
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2005
perror_plus("close");
2009
if(restore_loglevel){
2010
/* Restores kernel loglevel to default */
2011
ret = klogctl(7, NULL, 0);
2013
perror_plus("klogctl");
2016
#endif /* __linux__ */
2017
/* Lower privileges */
2019
/* Lower privileges */
2022
perror_plus("seteuid");
2232
/* Run network hooks */
2233
run_network_hooks("stop", interface, delay);
2402
2235
/* Re-raise priviliges */
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");
2240
perror_plus("seteuid");
2243
/* Take down the network interface */
2244
if(take_down_interface and geteuid() == 0){
2245
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2247
perror_plus("ioctl SIOCGIFFLAGS");
2248
} else if(network.ifr_flags & IFF_UP){
2249
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2250
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2252
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2422
if(debug and (interfaces_to_take_down == NULL)){
2423
fprintf_plus(stderr, "No interfaces needed to be taken"
2255
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2257
perror_plus("close");
2428
lower_privileges_permanently();
2431
free(interfaces_to_take_down);
2432
free(interfaces_hooks);
2261
/* Lower privileges permanently */
2265
perror_plus("setuid");
2434
2268
/* Removes the GPGME temp directory and all files inside */
2435
2269
if(tempdir_created){