159
163
server *current_server;
160
164
} mandos_context;
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 };
166
/* global so signal handler can reach it*/
167
AvahiSimplePoll *simple_poll;
168
169
sig_atomic_t quit_now = 0;
169
170
int signal_received = 0;
171
172
/* Function to use when printing errors */
172
173
void perror_plus(const char *print_text){
173
175
fprintf(stderr, "Mandos plugin %s: ",
174
176
program_invocation_short_name);
175
178
perror(print_text);
181
__attribute__((format (gnu_printf, 2, 3)))
178
182
int fprintf_plus(FILE *stream, const char *format, ...){
180
184
va_start (ap, format);
217
221
if(new_server->ip == NULL){
218
222
perror_plus("strdup");
221
225
/* Special case of first server */
222
if (mc.current_server == NULL){
226
if(*current_server == NULL){
223
227
new_server->next = new_server;
224
228
new_server->prev = new_server;
225
mc.current_server = new_server;
229
*current_server = new_server;
226
230
/* Place the new server last in the list */
228
new_server->next = mc.current_server;
229
new_server->prev = mc.current_server->prev;
232
new_server->next = *current_server;
233
new_server->prev = (*current_server)->prev;
230
234
new_server->prev->next = new_server;
231
mc.current_server->prev = new_server;
235
(*current_server)->prev = new_server;
233
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
237
ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
235
239
perror_plus("clock_gettime");
242
246
* Initialize GPGME.
244
248
static bool init_gpgme(const char *seckey, const char *pubkey,
245
const char *tempdir){
249
const char *tempdir, mandos_context *mc){
246
250
gpgme_error_t rc;
247
251
gpgme_engine_info_t engine_info;
251
254
* Helper function to insert pub and seckey to the engine keyring.
372
376
/* Decrypt data from the cryptotext data buffer to the plaintext
374
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
378
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
375
379
if(rc != GPG_ERR_NO_ERROR){
376
380
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
377
381
gpgme_strsource(rc), gpgme_strerror(rc));
378
382
plaintext_length = -1;
380
384
gpgme_decrypt_result_t result;
381
result = gpgme_op_decrypt_result(mc.ctx);
385
result = gpgme_op_decrypt_result(mc->ctx);
382
386
if(result == NULL){
383
387
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
529
533
/* GnuTLS server initialization */
530
ret = gnutls_dh_params_init(&mc.dh_params);
534
ret = gnutls_dh_params_init(&mc->dh_params);
531
535
if(ret != GNUTLS_E_SUCCESS){
532
536
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
533
537
" initialization: %s\n",
534
538
safer_gnutls_strerror(ret));
537
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
541
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
538
542
if(ret != GNUTLS_E_SUCCESS){
539
543
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
540
544
safer_gnutls_strerror(ret));
544
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
548
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
550
gnutls_certificate_free_credentials(mc.cred);
554
gnutls_certificate_free_credentials(mc->cred);
551
555
gnutls_global_deinit();
552
gnutls_dh_params_deinit(mc.dh_params);
556
gnutls_dh_params_deinit(mc->dh_params);
556
static int init_gnutls_session(gnutls_session_t *session){
560
static int init_gnutls_session(gnutls_session_t *session,
558
563
/* GnuTLS session creation */
729
731
if(if_indextoname((unsigned int)if_index, interface) == NULL){
730
732
perror_plus("if_indextoname");
732
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
733
"\n", ip, interface, port);
734
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
735
"\n", ip, interface, (uintmax_t)port);
736
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
738
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
739
ip, (uintmax_t)port);
739
741
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
740
742
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1025
1033
PRIdMAX ") on port %" PRIu16 "\n", name,
1026
1034
host_name, ip, (intmax_t)interface, port);
1028
int ret = start_mandos_communication(ip, port, interface,
1029
avahi_proto_to_af(proto));
1036
int ret = start_mandos_communication(ip, (in_port_t)port,
1038
avahi_proto_to_af(proto),
1031
avahi_simple_poll_quit(mc.simple_poll);
1041
avahi_simple_poll_quit(simple_poll);
1033
ret = add_server(ip, port, interface,
1034
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)){
1046
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1072
1089
the callback function is called the Avahi server will free the
1073
1090
resolver for us. */
1075
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1076
name, type, domain, protocol, 0,
1077
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)
1078
1096
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1080
avahi_strerror(avahi_server_errno(mc.server)));
1098
avahi_strerror(avahi_server_errno
1099
(((mandos_context*)mc)->server)));
1083
1102
case AVAHI_BROWSER_REMOVE:
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)){
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);
1240
1253
int notdotentries(const struct dirent *direntry){
1354
1368
if(block_time <= 0){
1355
ret = start_mandos_communication(mc.current_server->ip,
1356
mc.current_server->port,
1357
mc.current_server->if_index,
1358
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);
1360
avahi_simple_poll_quit(mc.simple_poll);
1374
avahi_simple_poll_quit(s);
1363
1377
ret = clock_gettime(CLOCK_MONOTONIC,
1364
&mc.current_server->last_seen);
1378
&mc->current_server->last_seen);
1366
1380
perror_plus("clock_gettime");
1369
mc.current_server = mc.current_server->next;
1383
mc->current_server = mc->current_server->next;
1370
1384
block_time = 0; /* Call avahi to find new Mandos
1371
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");
1384
1450
bool run_network_hooks(const char *mode, const char *interface,
1385
1451
const float delay){
1386
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",
1511
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 };
1512
1759
AvahiSServiceBrowser *sb = NULL;
1515
1762
intmax_t tmpmax;
1517
1764
int exitcode = EXIT_SUCCESS;
1518
const char *interface = "";
1519
struct ifreq network;
1521
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;
1524
1769
char tempdir[] = "/tmp/mandosXXXXXX";
1525
1770
bool tempdir_created = false;
1526
1771
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1527
1772
const char *seckey = PATHDIR "/" SECKEY;
1528
1773
const char *pubkey = PATHDIR "/" PUBKEY;
1774
char *interfaces_hooks = NULL;
1775
size_t interfaces_hooks_size = 0;
1530
1777
bool gnutls_initialized = false;
1531
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);
1769
2030
/* Run network hooks */
1770
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)){
1775
2051
avahi_set_log_function(empty_log);
1778
if(interface[0] == '\0'){
1779
struct dirent **direntries;
1780
/* First look for interfaces that are up */
1781
ret = scandir(sys_class_net, &direntries, up_interface,
1784
/* No up interfaces, look for any good interfaces */
1786
ret = scandir(sys_class_net, &direntries, good_interface,
1790
/* Pick the first interface returned */
1791
interface = strdup(direntries[0]->d_name);
1793
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1795
if(interface == NULL){
1796
perror_plus("malloc");
1798
exitcode = EXIT_FAILURE;
1804
fprintf_plus(stderr, "Could not find a network interface\n");
1805
exitcode = EXIT_FAILURE;
1810
2054
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1811
2055
from the signal handler */
1812
2056
/* Initialize the pseudo-RNG for Avahi */
1813
2057
srand((unsigned int) time(NULL));
1814
mc.simple_poll = avahi_simple_poll_new();
1815
if(mc.simple_poll == NULL){
2058
simple_poll = avahi_simple_poll_new();
2059
if(simple_poll == NULL){
1816
2060
fprintf_plus(stderr,
1817
2061
"Avahi: Failed to create simple poll object.\n");
1818
2062
exitcode = EX_UNAVAILABLE;
1885
/* If the interface is down, bring it up */
1886
if(strcmp(interface, "none") != 0){
1887
if_index = (AvahiIfIndex) if_nametoindex(interface);
1889
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1890
exitcode = EX_UNAVAILABLE;
1898
/* Re-raise priviliges */
1902
perror_plus("seteuid");
1906
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1907
messages about the network interface to mess up the prompt */
1908
ret = klogctl(8, NULL, 5);
1909
bool restore_loglevel = true;
1911
restore_loglevel = false;
1912
perror_plus("klogctl");
1914
#endif /* __linux__ */
1916
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1918
perror_plus("socket");
1919
exitcode = EX_OSERR;
1921
if(restore_loglevel){
1922
ret = klogctl(7, NULL, 0);
1924
perror_plus("klogctl");
1927
#endif /* __linux__ */
1928
/* Lower privileges */
1932
perror_plus("seteuid");
1936
strcpy(network.ifr_name, interface);
1937
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1939
perror_plus("ioctl SIOCGIFFLAGS");
1941
if(restore_loglevel){
1942
ret = klogctl(7, NULL, 0);
1944
perror_plus("klogctl");
1947
#endif /* __linux__ */
1948
exitcode = EX_OSERR;
1949
/* Lower privileges */
1953
perror_plus("seteuid");
1957
if((network.ifr_flags & IFF_UP) == 0){
1958
network.ifr_flags |= IFF_UP;
1959
take_down_interface = true;
1960
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1962
take_down_interface = false;
1963
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1964
exitcode = EX_OSERR;
1966
if(restore_loglevel){
1967
ret = klogctl(7, NULL, 0);
1969
perror_plus("klogctl");
1972
#endif /* __linux__ */
1973
/* Lower privileges */
1977
perror_plus("seteuid");
1982
/* Sleep checking until interface is running.
1983
Check every 0.25s, up to total time of delay */
1984
for(int i=0; i < delay * 4; i++){
1985
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1987
perror_plus("ioctl SIOCGIFFLAGS");
1988
} else if(network.ifr_flags & IFF_RUNNING){
1991
struct timespec sleeptime = { .tv_nsec = 250000000 };
1992
ret = nanosleep(&sleeptime, NULL);
1993
if(ret == -1 and errno != EINTR){
1994
perror_plus("nanosleep");
1997
if(not take_down_interface){
1998
/* We won't need the socket anymore */
1999
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2001
perror_plus("close");
2005
if(restore_loglevel){
2006
/* Restores kernel loglevel to default */
2007
ret = klogctl(7, NULL, 0);
2009
perror_plus("klogctl");
2012
#endif /* __linux__ */
2013
/* Lower privileges */
2015
/* Lower privileges */
2018
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");
2228
/* Run network hooks */
2229
run_network_hooks("stop", interface, delay);
2231
2402
/* Re-raise priviliges */
2236
perror_plus("seteuid");
2239
/* Take down the network interface */
2240
if(take_down_interface and geteuid() == 0){
2241
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2243
perror_plus("ioctl SIOCGIFFLAGS");
2244
} else if(network.ifr_flags & IFF_UP){
2245
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2246
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2248
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");
2251
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2253
perror_plus("close");
2422
if(debug and (interfaces_to_take_down == NULL)){
2423
fprintf_plus(stderr, "No interfaces needed to be taken"
2257
/* Lower privileges permanently */
2261
perror_plus("setuid");
2428
lower_privileges_permanently();
2431
free(interfaces_to_take_down);
2432
free(interfaces_hooks);
2264
2434
/* Removes the GPGME temp directory and all files inside */
2265
2435
if(tempdir_created){