9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2008-2013 Teddy Hogeborn
13
* Copyright © 2008-2013 Björn Påhlsson
12
* Copyright © 2008-2012 Teddy Hogeborn
13
* Copyright © 2008-2012 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
161
162
const char *priority;
163
164
server *current_server;
165
size_t interfaces_size;
166
165
} mandos_context;
168
/* global so signal handler can reach it*/
169
AvahiSimplePoll *simple_poll;
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 };
171
173
sig_atomic_t quit_now = 0;
172
174
int signal_received = 0;
188
190
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
189
191
program_invocation_short_name));
190
return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
192
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
210
212
/* Add server to set of servers to retry periodically */
211
213
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
212
int af, server **current_server){
214
216
server *new_server = malloc(sizeof(server));
215
217
if(new_server == NULL){
227
229
/* Special case of first server */
228
if(*current_server == NULL){
230
if (mc.current_server == NULL){
229
231
new_server->next = new_server;
230
232
new_server->prev = new_server;
231
*current_server = new_server;
233
mc.current_server = new_server;
232
234
/* Place the new server last in the list */
234
new_server->next = *current_server;
235
new_server->prev = (*current_server)->prev;
236
new_server->next = mc.current_server;
237
new_server->prev = mc.current_server->prev;
236
238
new_server->prev->next = new_server;
237
(*current_server)->prev = new_server;
239
mc.current_server->prev = new_server;
239
ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
241
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
241
243
perror_plus("clock_gettime");
248
250
* Initialize GPGME.
250
252
static bool init_gpgme(const char *seckey, const char *pubkey,
251
const char *tempdir, mandos_context *mc){
253
const char *tempdir){
252
254
gpgme_error_t rc;
253
255
gpgme_engine_info_t engine_info;
256
259
* Helper function to insert pub and seckey to the engine keyring.
276
rc = gpgme_op_import(mc->ctx, pgp_data);
279
rc = gpgme_op_import(mc.ctx, pgp_data);
277
280
if(rc != GPG_ERR_NO_ERROR){
278
281
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
279
282
gpgme_strsource(rc), gpgme_strerror(rc));
325
328
/* Create new GPGME "context" */
326
rc = gpgme_new(&(mc->ctx));
329
rc = gpgme_new(&(mc.ctx));
327
330
if(rc != GPG_ERR_NO_ERROR){
328
331
fprintf_plus(stderr, "Mandos plugin mandos-client: "
329
332
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
378
380
/* Decrypt data from the cryptotext data buffer to the plaintext
380
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
382
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
381
383
if(rc != GPG_ERR_NO_ERROR){
382
384
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
383
385
gpgme_strsource(rc), gpgme_strerror(rc));
384
386
plaintext_length = -1;
386
388
gpgme_decrypt_result_t result;
387
result = gpgme_op_decrypt_result(mc->ctx);
389
result = gpgme_op_decrypt_result(mc.ctx);
388
390
if(result == NULL){
389
391
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
507
508
/* OpenPGP credentials */
508
ret = gnutls_certificate_allocate_credentials(&mc->cred);
509
ret = gnutls_certificate_allocate_credentials(&mc.cred);
509
510
if(ret != GNUTLS_E_SUCCESS){
510
511
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
511
512
safer_gnutls_strerror(ret));
523
524
ret = gnutls_certificate_set_openpgp_key_file
524
(mc->cred, pubkeyfilename, seckeyfilename,
525
(mc.cred, pubkeyfilename, seckeyfilename,
525
526
GNUTLS_OPENPGP_FMT_BASE64);
526
527
if(ret != GNUTLS_E_SUCCESS){
527
528
fprintf_plus(stderr,
535
536
/* GnuTLS server initialization */
536
ret = gnutls_dh_params_init(&mc->dh_params);
537
ret = gnutls_dh_params_init(&mc.dh_params);
537
538
if(ret != GNUTLS_E_SUCCESS){
538
539
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
539
540
" initialization: %s\n",
540
541
safer_gnutls_strerror(ret));
543
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
544
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
544
545
if(ret != GNUTLS_E_SUCCESS){
545
546
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
546
547
safer_gnutls_strerror(ret));
550
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
551
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
556
gnutls_certificate_free_credentials(mc->cred);
557
gnutls_certificate_free_credentials(mc.cred);
557
558
gnutls_global_deinit();
558
gnutls_dh_params_deinit(mc->dh_params);
559
gnutls_dh_params_deinit(mc.dh_params);
562
static int init_gnutls_session(gnutls_session_t *session,
563
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
/* 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);
662
ret = init_gnutls_session(&session);
1054
1022
fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1055
1023
"'%s' of type '%s' in domain '%s': %s\n", name, type,
1057
avahi_strerror(avahi_server_errno
1058
(((mandos_context*)mc)->server)));
1025
avahi_strerror(avahi_server_errno(mc.server)));
1061
1028
case AVAHI_RESOLVER_FOUND:
1070
1037
int ret = start_mandos_communication(ip, (in_port_t)port,
1072
avahi_proto_to_af(proto),
1039
avahi_proto_to_af(proto));
1075
avahi_simple_poll_quit(simple_poll);
1041
avahi_simple_poll_quit(mc.simple_poll);
1077
1043
if(not add_server(ip, (in_port_t)port, interface,
1078
avahi_proto_to_af(proto),
1079
&((mandos_context*)mc)->current_server)){
1044
avahi_proto_to_af(proto))){
1080
1045
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1081
1046
" list\n", name);
1112
1077
case AVAHI_BROWSER_FAILURE:
1114
1079
fprintf_plus(stderr, "(Avahi browser) %s\n",
1115
avahi_strerror(avahi_server_errno
1116
(((mandos_context*)mc)->server)));
1117
avahi_simple_poll_quit(simple_poll);
1080
avahi_strerror(avahi_server_errno(mc.server)));
1081
avahi_simple_poll_quit(mc.simple_poll);
1120
1084
case AVAHI_BROWSER_NEW:
1123
1087
the callback function is called the Avahi server will free the
1124
1088
resolver for us. */
1126
if(avahi_s_service_resolver_new(((mandos_context*)mc)->server,
1127
interface, protocol, name, type,
1128
domain, protocol, 0,
1129
resolve_callback, mc) == NULL)
1090
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1091
name, type, domain, protocol, 0,
1092
resolve_callback, NULL) == NULL)
1130
1093
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1132
avahi_strerror(avahi_server_errno
1133
(((mandos_context*)mc)->server)));
1095
avahi_strerror(avahi_server_errno(mc.server)));
1136
1098
case AVAHI_BROWSER_REMOVE:
1359
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1360
mandos_context *mc){
1321
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1362
1323
struct timespec now;
1363
1324
struct timespec waited_time;
1364
1325
intmax_t block_time;
1367
if(mc->current_server == NULL){
1328
if(mc.current_server == NULL){
1369
1330
fprintf_plus(stderr, "Wait until first server is found."
1370
1331
" No timeout!\n");
1384
1345
/* Calculating in ms how long time between now and server
1385
1346
who we visted longest time ago. Now - last seen. */
1386
1347
waited_time.tv_sec = (now.tv_sec
1387
- mc->current_server->last_seen.tv_sec);
1348
- mc.current_server->last_seen.tv_sec);
1388
1349
waited_time.tv_nsec = (now.tv_nsec
1389
- mc->current_server->last_seen.tv_nsec);
1350
- mc.current_server->last_seen.tv_nsec);
1390
1351
/* total time is 10s/10,000ms.
1391
1352
Converting to s from ms by dividing by 1,000,
1392
1353
and ns to ms by dividing by 1,000,000. */
1402
1363
if(block_time <= 0){
1403
ret = start_mandos_communication(mc->current_server->ip,
1404
mc->current_server->port,
1405
mc->current_server->if_index,
1406
mc->current_server->af, mc);
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);
1408
avahi_simple_poll_quit(s);
1369
avahi_simple_poll_quit(mc.simple_poll);
1411
1372
ret = clock_gettime(CLOCK_MONOTONIC,
1412
&mc->current_server->last_seen);
1373
&mc.current_server->last_seen);
1414
1375
perror_plus("clock_gettime");
1417
mc->current_server = mc->current_server->next;
1378
mc.current_server = mc.current_server->next;
1418
1379
block_time = 0; /* Call avahi to find new Mandos
1419
1380
servers, but don't block */
1489
1450
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1491
1452
if(numhooks == -1){
1492
if(errno == ENOENT){
1494
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1495
" found\n", hookdir);
1498
perror_plus("scandir");
1453
perror_plus("scandir");
1501
1455
int devnull = open("/dev/null", O_RDONLY);
1502
1456
for(int i = 0; i < numhooks; i++){
1789
1743
int main(int argc, char *argv[]){
1790
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1791
.priority = "SECURE256:!CTYPE-X.509:"
1792
"+CTYPE-OPENPGP", .current_server = NULL,
1793
.interfaces = NULL, .interfaces_size = 0 };
1794
1744
AvahiSServiceBrowser *sb = NULL;
1795
1745
error_t ret_errno;
1797
1747
intmax_t tmpmax;
1799
1749
int exitcode = EXIT_SUCCESS;
1750
char *interfaces = NULL;
1751
size_t interfaces_size = 0;
1800
1752
char *interfaces_to_take_down = NULL;
1801
1753
size_t interfaces_to_take_down_size = 0;
1802
1754
char tempdir[] = "/tmp/mandosXXXXXX";
1902
1854
connect_to = arg;
1904
1856
case 'i': /* --interface */
1905
ret_errno = argz_add_sep(&mc.interfaces, &mc.interfaces_size,
1857
ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
1907
1859
if(ret_errno != 0){
1908
1860
argp_error(state, "%s", strerror(ret_errno));
2038
1990
/* Lower privileges */
1994
perror_plus("seteuid");
2043
/* Remove invalid interface names (except "none") */
1999
/* Remove empty interface names */
2045
2001
char *interface = NULL;
2046
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2002
while((interface = argz_next(interfaces, interfaces_size,
2048
if(strcmp(interface, "none") != 0
2049
and if_nametoindex(interface) == 0){
2050
if(interface[0] != '\0'){
2004
if(if_nametoindex(interface) == 0){
2005
if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2051
2006
fprintf_plus(stderr, "Not using nonexisting interface"
2052
2007
" \"%s\"\n", interface);
2054
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
2009
argz_delete(&interfaces, &interfaces_size, interface);
2055
2010
interface = NULL;
2060
2015
/* Run network hooks */
2062
if(mc.interfaces != NULL){
2063
interfaces_hooks = malloc(mc.interfaces_size);
2064
if(interfaces_hooks == NULL){
2065
perror_plus("malloc");
2068
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2069
interfaces_hooks_size = mc.interfaces_size;
2070
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2017
ret_errno = argz_append(&interfaces_hooks, &interfaces_hooks_size,
2018
interfaces, interfaces_size);
2021
perror_plus("argz_append");
2073
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2074
interfaces_hooks : "", delay)){
2024
argz_stringify(interfaces_hooks, interfaces_hooks_size, (int)',');
2025
if(not run_network_hooks("start", interfaces_hooks, delay)){
2084
2035
from the signal handler */
2085
2036
/* Initialize the pseudo-RNG for Avahi */
2086
2037
srand((unsigned int) time(NULL));
2087
simple_poll = avahi_simple_poll_new();
2088
if(simple_poll == NULL){
2038
mc.simple_poll = avahi_simple_poll_new();
2039
if(mc.simple_poll == NULL){
2089
2040
fprintf_plus(stderr,
2090
2041
"Avahi: Failed to create simple poll object.\n");
2091
2042
exitcode = EX_UNAVAILABLE;
2158
2109
/* If no interfaces were specified, make a list */
2159
if(mc.interfaces == NULL){
2110
if(interfaces == NULL){
2160
2111
struct dirent **direntries;
2161
2112
/* Look for any good interfaces */
2162
2113
ret = scandir(sys_class_net, &direntries, good_interface,
2165
2116
/* Add all found interfaces to interfaces list */
2166
2117
for(int i = 0; i < ret; ++i){
2167
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2118
ret_errno = argz_add(&interfaces, &interfaces_size,
2168
2119
direntries[i]->d_name);
2169
2120
if(ret_errno != 0){
2170
2121
perror_plus("argz_add");
2187
/* Bring up interfaces which are down, and remove any "none"s */
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)){
2189
2150
char *interface = NULL;
2190
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2151
while((interface = argz_next(interfaces, interfaces_size,
2192
/* If interface name is "none", stop bringing up interfaces.
2193
Also remove all instances of "none" from the list */
2194
if(strcmp(interface, "none") == 0){
2195
argz_delete(&mc.interfaces, &mc.interfaces_size,
2198
while((interface = argz_next(mc.interfaces,
2199
mc.interfaces_size, interface))){
2200
if(strcmp(interface, "none") == 0){
2201
argz_delete(&mc.interfaces, &mc.interfaces_size,
2208
2153
bool interface_was_up = interface_is_up(interface);
2209
2154
ret = bring_up_interface(interface, delay);
2210
2155
if(not interface_was_up){
2168
interfaces_size = 0;
2221
2169
if(debug and (interfaces_to_take_down == NULL)){
2222
2170
fprintf_plus(stderr, "No interfaces were brought up\n");
2226
/* If we only got one interface, explicitly use only that one */
2227
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2229
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2232
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
2239
ret = init_gnutls_global(pubkey, seckey, &mc);
2178
ret = init_gnutls_global(pubkey, seckey);
2241
2180
fprintf_plus(stderr, "init_gnutls_global failed\n");
2242
2181
exitcode = EX_UNAVAILABLE;
2262
if(not init_gpgme(pubkey, seckey, tempdir, &mc)){
2201
if(not init_gpgme(pubkey, seckey, tempdir)){
2263
2202
fprintf_plus(stderr, "init_gpgme failed\n");
2264
2203
exitcode = EX_UNAVAILABLE;
2324
2263
while(not quit_now){
2325
ret = start_mandos_communication(address, port, if_index, af,
2264
ret = start_mandos_communication(address, port, if_index, af);
2327
2265
if(quit_now or ret == 0){
2355
2293
config.publish_domain = 0;
2357
2295
/* Allocate a new server */
2358
mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2359
&config, NULL, NULL, &ret_errno);
2296
mc.server = avahi_server_new(avahi_simple_poll_get
2297
(mc.simple_poll), &config, NULL,
2361
2300
/* Free the Avahi configuration data */
2362
2301
avahi_server_config_free(&config);
2377
2316
/* Create the Avahi service browser */
2378
2317
sb = avahi_s_service_browser_new(mc.server, if_index,
2379
2318
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2380
NULL, 0, browse_callback,
2319
NULL, 0, browse_callback, NULL);
2382
2320
if(sb == NULL){
2383
2321
fprintf_plus(stderr, "Failed to create service browser: %s\n",
2384
2322
avahi_strerror(avahi_server_errno(mc.server)));
2396
2334
fprintf_plus(stderr, "Starting Avahi loop search\n");
2399
ret = avahi_loop_with_timeout(simple_poll,
2400
(int)(retry_interval * 1000), &mc);
2337
ret = avahi_loop_with_timeout(mc.simple_poll,
2338
(int)(retry_interval * 1000));
2402
2340
fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2403
2341
(ret == 0) ? "successfully" : "with error");
2412
2350
/* Cleanup things */
2413
free(mc.interfaces);
2416
2352
avahi_s_service_browser_free(sb);
2418
2354
if(mc.server != NULL)
2419
2355
avahi_server_free(mc.server);
2421
if(simple_poll != NULL)
2422
avahi_simple_poll_free(simple_poll);
2357
if(mc.simple_poll != NULL)
2358
avahi_simple_poll_free(mc.simple_poll);
2424
2360
if(gnutls_initialized){
2425
2361
gnutls_certificate_free_credentials(mc.cred);
2447
2383
raise_privileges();
2449
2385
/* Run network hooks */
2450
run_network_hooks("stop", interfaces_hooks != NULL ?
2451
interfaces_hooks : "", delay);
2386
run_network_hooks("stop", interfaces_hooks, delay);
2453
2388
/* Take down the network interfaces which were brought up */