9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2008-2012 Teddy Hogeborn
13
* Copyright © 2008-2012 Björn Påhlsson
12
* Copyright © 2008-2013 Teddy Hogeborn
13
* Copyright © 2008-2013 Björn Påhlsson
15
15
* This program is free software: you can redistribute it and/or
16
16
* modify it under the terms of the GNU General Public License as
162
161
const char *priority;
164
163
server *current_server;
165
size_t interfaces_size;
165
166
} mandos_context;
167
/* global context so signal handler can reach it*/
168
mandos_context mc = { .simple_poll = NULL, .server = NULL,
169
.dh_bits = 1024, .priority = "SECURE256"
170
":!CTYPE-X.509:+CTYPE-OPENPGP",
171
.current_server = NULL };
168
/* global so signal handler can reach it*/
169
AvahiSimplePoll *simple_poll;
173
171
sig_atomic_t quit_now = 0;
174
172
int signal_received = 0;
190
188
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
191
189
program_invocation_short_name));
192
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
190
return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
212
210
/* Add server to set of servers to retry periodically */
213
211
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
212
int af, server **current_server){
216
214
server *new_server = malloc(sizeof(server));
217
215
if(new_server == NULL){
229
227
/* Special case of first server */
230
if (mc.current_server == NULL){
228
if(*current_server == NULL){
231
229
new_server->next = new_server;
232
230
new_server->prev = new_server;
233
mc.current_server = new_server;
231
*current_server = new_server;
234
232
/* Place the new server last in the list */
236
new_server->next = mc.current_server;
237
new_server->prev = mc.current_server->prev;
234
new_server->next = *current_server;
235
new_server->prev = (*current_server)->prev;
238
236
new_server->prev->next = new_server;
239
mc.current_server->prev = new_server;
237
(*current_server)->prev = new_server;
241
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
239
ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
243
241
perror_plus("clock_gettime");
250
248
* Initialize GPGME.
252
250
static bool init_gpgme(const char *seckey, const char *pubkey,
253
const char *tempdir){
251
const char *tempdir, mandos_context *mc){
254
252
gpgme_error_t rc;
255
253
gpgme_engine_info_t engine_info;
259
256
* Helper function to insert pub and seckey to the engine keyring.
279
rc = gpgme_op_import(mc.ctx, pgp_data);
276
rc = gpgme_op_import(mc->ctx, pgp_data);
280
277
if(rc != GPG_ERR_NO_ERROR){
281
278
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
282
279
gpgme_strsource(rc), gpgme_strerror(rc));
328
325
/* Create new GPGME "context" */
329
rc = gpgme_new(&(mc.ctx));
326
rc = gpgme_new(&(mc->ctx));
330
327
if(rc != GPG_ERR_NO_ERROR){
331
328
fprintf_plus(stderr, "Mandos plugin mandos-client: "
332
329
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
380
378
/* Decrypt data from the cryptotext data buffer to the plaintext
382
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
380
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
383
381
if(rc != GPG_ERR_NO_ERROR){
384
382
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
385
383
gpgme_strsource(rc), gpgme_strerror(rc));
386
384
plaintext_length = -1;
388
386
gpgme_decrypt_result_t result;
389
result = gpgme_op_decrypt_result(mc.ctx);
387
result = gpgme_op_decrypt_result(mc->ctx);
390
388
if(result == NULL){
391
389
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
508
507
/* OpenPGP credentials */
509
ret = gnutls_certificate_allocate_credentials(&mc.cred);
508
ret = gnutls_certificate_allocate_credentials(&mc->cred);
510
509
if(ret != GNUTLS_E_SUCCESS){
511
510
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
512
511
safer_gnutls_strerror(ret));
524
523
ret = gnutls_certificate_set_openpgp_key_file
525
(mc.cred, pubkeyfilename, seckeyfilename,
524
(mc->cred, pubkeyfilename, seckeyfilename,
526
525
GNUTLS_OPENPGP_FMT_BASE64);
527
526
if(ret != GNUTLS_E_SUCCESS){
528
527
fprintf_plus(stderr,
536
535
/* GnuTLS server initialization */
537
ret = gnutls_dh_params_init(&mc.dh_params);
536
ret = gnutls_dh_params_init(&mc->dh_params);
538
537
if(ret != GNUTLS_E_SUCCESS){
539
538
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
540
539
" initialization: %s\n",
541
540
safer_gnutls_strerror(ret));
544
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
543
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
545
544
if(ret != GNUTLS_E_SUCCESS){
546
545
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
547
546
safer_gnutls_strerror(ret));
551
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
550
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
557
gnutls_certificate_free_credentials(mc.cred);
556
gnutls_certificate_free_credentials(mc->cred);
558
557
gnutls_global_deinit();
559
gnutls_dh_params_deinit(mc.dh_params);
558
gnutls_dh_params_deinit(mc->dh_params);
563
static int init_gnutls_session(gnutls_session_t *session){
562
static int init_gnutls_session(gnutls_session_t *session,
565
565
/* GnuTLS session creation */
611
611
/* ignore client certificate if any. */
612
612
gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
614
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
614
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
623
623
/* Called when a Mandos server is found */
624
624
static int start_mandos_communication(const char *ip, in_port_t port,
625
625
AvahiIfIndex if_index,
626
int af, mandos_context *mc){
627
627
int ret, tcp_sd = -1;
662
ret = init_gnutls_session(&session);
662
/* If the interface is specified and we have a list of interfaces */
663
if(if_index != AVAHI_IF_UNSPEC and mc->interfaces != NULL){
664
/* Check if the interface is one of the interfaces we are using */
667
char *interface = NULL;
668
while((interface=argz_next(mc->interfaces, mc->interfaces_size,
670
if(if_nametoindex(interface) == (unsigned int)if_index){
677
/* This interface does not match any in the list, so we don't
678
connect to the server */
680
char interface[IF_NAMESIZE];
681
if(if_indextoname((unsigned int)if_index, interface) == NULL){
682
perror_plus("if_indextoname");
684
fprintf_plus(stderr, "Skipping server on non-used interface"
686
if_indextoname((unsigned int)if_index,
694
ret = init_gnutls_session(&session, mc);
705
737
if(af == AF_INET6){
706
738
to.in6.sin6_port = htons(port);
740
#pragma GCC diagnostic push
741
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
707
743
if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
708
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
744
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower */
746
#pragma GCC diagnostic pop
710
748
if(if_index == AVAHI_IF_UNSPEC){
711
749
fprintf_plus(stderr, "An IPv6 link-local address is"
712
750
" incomplete without a network interface\n");
1022
1058
fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1023
1059
"'%s' of type '%s' in domain '%s': %s\n", name, type,
1025
avahi_strerror(avahi_server_errno(mc.server)));
1061
avahi_strerror(avahi_server_errno
1062
(((mandos_context*)mc)->server)));
1028
1065
case AVAHI_RESOLVER_FOUND:
1037
1074
int ret = start_mandos_communication(ip, (in_port_t)port,
1039
avahi_proto_to_af(proto));
1076
avahi_proto_to_af(proto),
1041
avahi_simple_poll_quit(mc.simple_poll);
1079
avahi_simple_poll_quit(simple_poll);
1043
1081
if(not add_server(ip, (in_port_t)port, interface,
1044
avahi_proto_to_af(proto))){
1082
avahi_proto_to_af(proto),
1083
&((mandos_context*)mc)->current_server)){
1045
1084
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1046
1085
" list\n", name);
1077
1116
case AVAHI_BROWSER_FAILURE:
1079
1118
fprintf_plus(stderr, "(Avahi browser) %s\n",
1080
avahi_strerror(avahi_server_errno(mc.server)));
1081
avahi_simple_poll_quit(mc.simple_poll);
1119
avahi_strerror(avahi_server_errno
1120
(((mandos_context*)mc)->server)));
1121
avahi_simple_poll_quit(simple_poll);
1084
1124
case AVAHI_BROWSER_NEW:
1087
1127
the callback function is called the Avahi server will free the
1088
1128
resolver for us. */
1090
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1091
name, type, domain, protocol, 0,
1092
resolve_callback, NULL) == NULL)
1130
if(avahi_s_service_resolver_new(((mandos_context*)mc)->server,
1131
interface, protocol, name, type,
1132
domain, protocol, 0,
1133
resolve_callback, mc) == NULL)
1093
1134
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1095
avahi_strerror(avahi_server_errno(mc.server)));
1136
avahi_strerror(avahi_server_errno
1137
(((mandos_context*)mc)->server)));
1098
1140
case AVAHI_BROWSER_REMOVE:
1321
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1363
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1364
mandos_context *mc){
1323
1366
struct timespec now;
1324
1367
struct timespec waited_time;
1325
1368
intmax_t block_time;
1328
if(mc.current_server == NULL){
1371
if(mc->current_server == NULL){
1330
1373
fprintf_plus(stderr, "Wait until first server is found."
1331
1374
" No timeout!\n");
1345
1388
/* Calculating in ms how long time between now and server
1346
1389
who we visted longest time ago. Now - last seen. */
1347
1390
waited_time.tv_sec = (now.tv_sec
1348
- mc.current_server->last_seen.tv_sec);
1391
- mc->current_server->last_seen.tv_sec);
1349
1392
waited_time.tv_nsec = (now.tv_nsec
1350
- mc.current_server->last_seen.tv_nsec);
1393
- mc->current_server->last_seen.tv_nsec);
1351
1394
/* total time is 10s/10,000ms.
1352
1395
Converting to s from ms by dividing by 1,000,
1353
1396
and ns to ms by dividing by 1,000,000. */
1363
1406
if(block_time <= 0){
1364
ret = start_mandos_communication(mc.current_server->ip,
1365
mc.current_server->port,
1366
mc.current_server->if_index,
1367
mc.current_server->af);
1407
ret = start_mandos_communication(mc->current_server->ip,
1408
mc->current_server->port,
1409
mc->current_server->if_index,
1410
mc->current_server->af, mc);
1369
avahi_simple_poll_quit(mc.simple_poll);
1412
avahi_simple_poll_quit(s);
1372
1415
ret = clock_gettime(CLOCK_MONOTONIC,
1373
&mc.current_server->last_seen);
1416
&mc->current_server->last_seen);
1375
1418
perror_plus("clock_gettime");
1378
mc.current_server = mc.current_server->next;
1421
mc->current_server = mc->current_server->next;
1379
1422
block_time = 0; /* Call avahi to find new Mandos
1380
1423
servers, but don't block */
1450
1493
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1452
1495
if(numhooks == -1){
1453
perror_plus("scandir");
1496
if(errno == ENOENT){
1498
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1499
" found\n", hookdir);
1502
perror_plus("scandir");
1455
1505
int devnull = open("/dev/null", O_RDONLY);
1456
1506
for(int i = 0; i < numhooks; i++){
1743
1793
int main(int argc, char *argv[]){
1794
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1795
.priority = "SECURE256:!CTYPE-X.509:"
1796
"+CTYPE-OPENPGP", .current_server = NULL,
1797
.interfaces = NULL, .interfaces_size = 0 };
1744
1798
AvahiSServiceBrowser *sb = NULL;
1745
1799
error_t ret_errno;
1747
1801
intmax_t tmpmax;
1749
1803
int exitcode = EXIT_SUCCESS;
1750
char *interfaces = NULL;
1751
size_t interfaces_size = 0;
1752
1804
char *interfaces_to_take_down = NULL;
1753
1805
size_t interfaces_to_take_down_size = 0;
1754
1806
char tempdir[] = "/tmp/mandosXXXXXX";
1854
1906
connect_to = arg;
1856
1908
case 'i': /* --interface */
1857
ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
1909
ret_errno = argz_add_sep(&mc.interfaces, &mc.interfaces_size,
1859
1911
if(ret_errno != 0){
1860
1912
argp_error(state, "%s", strerror(ret_errno));
1990
2042
/* Lower privileges */
1994
perror_plus("seteuid");
1999
/* Remove empty interface names */
2047
/* Remove invalid interface names (except "none") */
2001
2049
char *interface = NULL;
2002
while((interface = argz_next(interfaces, interfaces_size,
2050
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2004
if(if_nametoindex(interface) == 0){
2005
if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2052
if(strcmp(interface, "none") != 0
2053
and if_nametoindex(interface) == 0){
2054
if(interface[0] != '\0'){
2006
2055
fprintf_plus(stderr, "Not using nonexisting interface"
2007
2056
" \"%s\"\n", interface);
2009
argz_delete(&interfaces, &interfaces_size, interface);
2058
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
2010
2059
interface = NULL;
2015
2064
/* Run network hooks */
2017
ret_errno = argz_append(&interfaces_hooks, &interfaces_hooks_size,
2018
interfaces, interfaces_size);
2021
perror_plus("argz_append");
2066
if(mc.interfaces != NULL){
2067
interfaces_hooks = malloc(mc.interfaces_size);
2068
if(interfaces_hooks == NULL){
2069
perror_plus("malloc");
2072
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2073
interfaces_hooks_size = mc.interfaces_size;
2074
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2024
argz_stringify(interfaces_hooks, interfaces_hooks_size, (int)',');
2025
if(not run_network_hooks("start", interfaces_hooks, delay)){
2077
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2078
interfaces_hooks : "", delay)){
2035
2088
from the signal handler */
2036
2089
/* Initialize the pseudo-RNG for Avahi */
2037
2090
srand((unsigned int) time(NULL));
2038
mc.simple_poll = avahi_simple_poll_new();
2039
if(mc.simple_poll == NULL){
2091
simple_poll = avahi_simple_poll_new();
2092
if(simple_poll == NULL){
2040
2093
fprintf_plus(stderr,
2041
2094
"Avahi: Failed to create simple poll object.\n");
2042
2095
exitcode = EX_UNAVAILABLE;
2109
2162
/* If no interfaces were specified, make a list */
2110
if(interfaces == NULL){
2163
if(mc.interfaces == NULL){
2111
2164
struct dirent **direntries;
2112
2165
/* Look for any good interfaces */
2113
2166
ret = scandir(sys_class_net, &direntries, good_interface,
2116
2169
/* Add all found interfaces to interfaces list */
2117
2170
for(int i = 0; i < ret; ++i){
2118
ret_errno = argz_add(&interfaces, &interfaces_size,
2171
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2119
2172
direntries[i]->d_name);
2120
2173
if(ret_errno != 0){
2121
2174
perror_plus("argz_add");
2138
/* If we only got one interface, explicitly use only that one */
2139
if(argz_count(interfaces, interfaces_size) == 1){
2141
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2144
if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2147
/* Bring up interfaces which are down */
2148
if(not (argz_count(interfaces, interfaces_size) == 1
2149
and strcmp(interfaces, "none") == 0)){
2191
/* Bring up interfaces which are down, and remove any "none"s */
2150
2193
char *interface = NULL;
2151
while((interface = argz_next(interfaces, interfaces_size,
2194
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2196
/* If interface name is "none", stop bringing up interfaces.
2197
Also remove all instances of "none" from the list */
2198
if(strcmp(interface, "none") == 0){
2199
argz_delete(&mc.interfaces, &mc.interfaces_size,
2202
while((interface = argz_next(mc.interfaces,
2203
mc.interfaces_size, interface))){
2204
if(strcmp(interface, "none") == 0){
2205
argz_delete(&mc.interfaces, &mc.interfaces_size,
2153
2212
bool interface_was_up = interface_is_up(interface);
2154
2213
ret = bring_up_interface(interface, delay);
2155
2214
if(not interface_was_up){
2168
interfaces_size = 0;
2169
2225
if(debug and (interfaces_to_take_down == NULL)){
2170
2226
fprintf_plus(stderr, "No interfaces were brought up\n");
2230
/* If we only got one interface, explicitly use only that one */
2231
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2233
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2236
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
2178
ret = init_gnutls_global(pubkey, seckey);
2243
ret = init_gnutls_global(pubkey, seckey, &mc);
2180
2245
fprintf_plus(stderr, "init_gnutls_global failed\n");
2181
2246
exitcode = EX_UNAVAILABLE;
2201
if(not init_gpgme(pubkey, seckey, tempdir)){
2266
if(not init_gpgme(pubkey, seckey, tempdir, &mc)){
2202
2267
fprintf_plus(stderr, "init_gpgme failed\n");
2203
2268
exitcode = EX_UNAVAILABLE;
2263
2328
while(not quit_now){
2264
ret = start_mandos_communication(address, port, if_index, af);
2329
ret = start_mandos_communication(address, port, if_index, af,
2265
2331
if(quit_now or ret == 0){
2293
2359
config.publish_domain = 0;
2295
2361
/* Allocate a new server */
2296
mc.server = avahi_server_new(avahi_simple_poll_get
2297
(mc.simple_poll), &config, NULL,
2362
mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2363
&config, NULL, NULL, &ret_errno);
2300
2365
/* Free the Avahi configuration data */
2301
2366
avahi_server_config_free(&config);
2316
2381
/* Create the Avahi service browser */
2317
2382
sb = avahi_s_service_browser_new(mc.server, if_index,
2318
2383
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2319
NULL, 0, browse_callback, NULL);
2384
NULL, 0, browse_callback,
2320
2386
if(sb == NULL){
2321
2387
fprintf_plus(stderr, "Failed to create service browser: %s\n",
2322
2388
avahi_strerror(avahi_server_errno(mc.server)));
2334
2400
fprintf_plus(stderr, "Starting Avahi loop search\n");
2337
ret = avahi_loop_with_timeout(mc.simple_poll,
2338
(int)(retry_interval * 1000));
2403
ret = avahi_loop_with_timeout(simple_poll,
2404
(int)(retry_interval * 1000), &mc);
2340
2406
fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2341
2407
(ret == 0) ? "successfully" : "with error");
2350
2416
/* Cleanup things */
2417
free(mc.interfaces);
2352
2420
avahi_s_service_browser_free(sb);
2354
2422
if(mc.server != NULL)
2355
2423
avahi_server_free(mc.server);
2357
if(mc.simple_poll != NULL)
2358
avahi_simple_poll_free(mc.simple_poll);
2425
if(simple_poll != NULL)
2426
avahi_simple_poll_free(simple_poll);
2360
2428
if(gnutls_initialized){
2361
2429
gnutls_certificate_free_credentials(mc.cred);
2383
2451
raise_privileges();
2385
2453
/* Run network hooks */
2386
run_network_hooks("stop", interfaces_hooks, delay);
2454
run_network_hooks("stop", interfaces_hooks != NULL ?
2455
interfaces_hooks : "", delay);
2388
2457
/* Take down the network interfaces which were brought up */