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
161
161
const char *priority;
163
163
server *current_server;
165
size_t interfaces_size;
164
166
} mandos_context;
166
168
/* global so signal handler can reach it*/
167
169
AvahiSimplePoll *simple_poll;
168
mandos_context mc = { .server = NULL, .dh_bits = 1024,
169
.priority = "SECURE256:!CTYPE-X.509:"
170
"+CTYPE-OPENPGP", .current_server = NULL };
172
171
sig_atomic_t quit_now = 0;
173
172
int signal_received = 0;
189
188
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
190
189
program_invocation_short_name));
191
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
190
return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
211
210
/* Add server to set of servers to retry periodically */
212
211
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
212
int af, server **current_server){
215
214
server *new_server = malloc(sizeof(server));
216
215
if(new_server == NULL){
228
227
/* Special case of first server */
229
if (mc.current_server == NULL){
228
if(*current_server == NULL){
230
229
new_server->next = new_server;
231
230
new_server->prev = new_server;
232
mc.current_server = new_server;
231
*current_server = new_server;
233
232
/* Place the new server last in the list */
235
new_server->next = mc.current_server;
236
new_server->prev = mc.current_server->prev;
234
new_server->next = *current_server;
235
new_server->prev = (*current_server)->prev;
237
236
new_server->prev->next = new_server;
238
mc.current_server->prev = new_server;
237
(*current_server)->prev = new_server;
240
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
239
ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
242
241
perror_plus("clock_gettime");
249
248
* Initialize GPGME.
251
250
static bool init_gpgme(const char *seckey, const char *pubkey,
252
const char *tempdir){
251
const char *tempdir, mandos_context *mc){
253
252
gpgme_error_t rc;
254
253
gpgme_engine_info_t engine_info;
277
rc = gpgme_op_import(mc.ctx, pgp_data);
276
rc = gpgme_op_import(mc->ctx, pgp_data);
278
277
if(rc != GPG_ERR_NO_ERROR){
279
278
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
280
279
gpgme_strsource(rc), gpgme_strerror(rc));
326
325
/* Create new GPGME "context" */
327
rc = gpgme_new(&(mc.ctx));
326
rc = gpgme_new(&(mc->ctx));
328
327
if(rc != GPG_ERR_NO_ERROR){
329
328
fprintf_plus(stderr, "Mandos plugin mandos-client: "
330
329
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
378
378
/* Decrypt data from the cryptotext data buffer to the plaintext
380
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
380
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
381
381
if(rc != GPG_ERR_NO_ERROR){
382
382
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
383
383
gpgme_strsource(rc), gpgme_strerror(rc));
384
384
plaintext_length = -1;
386
386
gpgme_decrypt_result_t result;
387
result = gpgme_op_decrypt_result(mc.ctx);
387
result = gpgme_op_decrypt_result(mc->ctx);
388
388
if(result == NULL){
389
389
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
506
507
/* OpenPGP credentials */
507
ret = gnutls_certificate_allocate_credentials(&mc.cred);
508
ret = gnutls_certificate_allocate_credentials(&mc->cred);
508
509
if(ret != GNUTLS_E_SUCCESS){
509
510
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
510
511
safer_gnutls_strerror(ret));
522
523
ret = gnutls_certificate_set_openpgp_key_file
523
(mc.cred, pubkeyfilename, seckeyfilename,
524
(mc->cred, pubkeyfilename, seckeyfilename,
524
525
GNUTLS_OPENPGP_FMT_BASE64);
525
526
if(ret != GNUTLS_E_SUCCESS){
526
527
fprintf_plus(stderr,
534
535
/* GnuTLS server initialization */
535
ret = gnutls_dh_params_init(&mc.dh_params);
536
ret = gnutls_dh_params_init(&mc->dh_params);
536
537
if(ret != GNUTLS_E_SUCCESS){
537
538
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
538
539
" initialization: %s\n",
539
540
safer_gnutls_strerror(ret));
542
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
543
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
543
544
if(ret != GNUTLS_E_SUCCESS){
544
545
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
545
546
safer_gnutls_strerror(ret));
549
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
550
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
555
gnutls_certificate_free_credentials(mc.cred);
556
gnutls_certificate_free_credentials(mc->cred);
556
557
gnutls_global_deinit();
557
gnutls_dh_params_deinit(mc.dh_params);
558
gnutls_dh_params_deinit(mc->dh_params);
561
static int init_gnutls_session(gnutls_session_t *session){
562
static int init_gnutls_session(gnutls_session_t *session,
563
565
/* GnuTLS session creation */
609
611
/* ignore client certificate if any. */
610
612
gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
612
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
614
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
621
623
/* Called when a Mandos server is found */
622
624
static int start_mandos_communication(const char *ip, in_port_t port,
623
625
AvahiIfIndex if_index,
626
int af, mandos_context *mc){
625
627
int ret, tcp_sd = -1;
660
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);
1020
1054
fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1021
1055
"'%s' of type '%s' in domain '%s': %s\n", name, type,
1023
avahi_strerror(avahi_server_errno(mc.server)));
1057
avahi_strerror(avahi_server_errno
1058
(((mandos_context*)mc)->server)));
1026
1061
case AVAHI_RESOLVER_FOUND:
1035
1070
int ret = start_mandos_communication(ip, (in_port_t)port,
1037
avahi_proto_to_af(proto));
1072
avahi_proto_to_af(proto),
1039
1075
avahi_simple_poll_quit(simple_poll);
1041
1077
if(not add_server(ip, (in_port_t)port, interface,
1042
avahi_proto_to_af(proto))){
1078
avahi_proto_to_af(proto),
1079
&((mandos_context*)mc)->current_server)){
1043
1080
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1044
1081
" list\n", name);
1075
1112
case AVAHI_BROWSER_FAILURE:
1077
1114
fprintf_plus(stderr, "(Avahi browser) %s\n",
1078
avahi_strerror(avahi_server_errno(mc.server)));
1115
avahi_strerror(avahi_server_errno
1116
(((mandos_context*)mc)->server)));
1079
1117
avahi_simple_poll_quit(simple_poll);
1085
1123
the callback function is called the Avahi server will free the
1086
1124
resolver for us. */
1088
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1089
name, type, domain, protocol, 0,
1090
resolve_callback, NULL) == NULL)
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)
1091
1130
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1093
avahi_strerror(avahi_server_errno(mc.server)));
1132
avahi_strerror(avahi_server_errno
1133
(((mandos_context*)mc)->server)));
1096
1136
case AVAHI_BROWSER_REMOVE:
1319
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1359
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1360
mandos_context *mc){
1321
1362
struct timespec now;
1322
1363
struct timespec waited_time;
1323
1364
intmax_t block_time;
1326
if(mc.current_server == NULL){
1367
if(mc->current_server == NULL){
1328
1369
fprintf_plus(stderr, "Wait until first server is found."
1329
1370
" No timeout!\n");
1343
1384
/* Calculating in ms how long time between now and server
1344
1385
who we visted longest time ago. Now - last seen. */
1345
1386
waited_time.tv_sec = (now.tv_sec
1346
- mc.current_server->last_seen.tv_sec);
1387
- mc->current_server->last_seen.tv_sec);
1347
1388
waited_time.tv_nsec = (now.tv_nsec
1348
- mc.current_server->last_seen.tv_nsec);
1389
- mc->current_server->last_seen.tv_nsec);
1349
1390
/* total time is 10s/10,000ms.
1350
1391
Converting to s from ms by dividing by 1,000,
1351
1392
and ns to ms by dividing by 1,000,000. */
1361
1402
if(block_time <= 0){
1362
ret = start_mandos_communication(mc.current_server->ip,
1363
mc.current_server->port,
1364
mc.current_server->if_index,
1365
mc.current_server->af);
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);
1367
avahi_simple_poll_quit(simple_poll);
1408
avahi_simple_poll_quit(s);
1370
1411
ret = clock_gettime(CLOCK_MONOTONIC,
1371
&mc.current_server->last_seen);
1412
&mc->current_server->last_seen);
1373
1414
perror_plus("clock_gettime");
1376
mc.current_server = mc.current_server->next;
1417
mc->current_server = mc->current_server->next;
1377
1418
block_time = 0; /* Call avahi to find new Mandos
1378
1419
servers, but don't block */
1748
1789
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 };
1749
1794
AvahiSServiceBrowser *sb = NULL;
1750
1795
error_t ret_errno;
1752
1797
intmax_t tmpmax;
1754
1799
int exitcode = EXIT_SUCCESS;
1755
char *interfaces = NULL;
1756
size_t interfaces_size = 0;
1757
1800
char *interfaces_to_take_down = NULL;
1758
1801
size_t interfaces_to_take_down_size = 0;
1759
1802
char tempdir[] = "/tmp/mandosXXXXXX";
1859
1902
connect_to = arg;
1861
1904
case 'i': /* --interface */
1862
ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
1905
ret_errno = argz_add_sep(&mc.interfaces, &mc.interfaces_size,
1864
1907
if(ret_errno != 0){
1865
1908
argp_error(state, "%s", strerror(ret_errno));
1995
2038
/* Lower privileges */
1999
perror_plus("seteuid");
2004
/* Remove empty interface names */
2043
/* Remove invalid interface names (except "none") */
2006
2045
char *interface = NULL;
2007
while((interface = argz_next(interfaces, interfaces_size,
2046
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2009
if(if_nametoindex(interface) == 0){
2010
if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2048
if(strcmp(interface, "none") != 0
2049
and if_nametoindex(interface) == 0){
2050
if(interface[0] != '\0'){
2011
2051
fprintf_plus(stderr, "Not using nonexisting interface"
2012
2052
" \"%s\"\n", interface);
2014
argz_delete(&interfaces, &interfaces_size, interface);
2054
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
2015
2055
interface = NULL;
2020
2060
/* Run network hooks */
2023
if(interfaces != NULL){
2024
interfaces_hooks = malloc(interfaces_size);
2062
if(mc.interfaces != NULL){
2063
interfaces_hooks = malloc(mc.interfaces_size);
2025
2064
if(interfaces_hooks == NULL){
2026
2065
perror_plus("malloc");
2029
memcpy(interfaces_hooks, interfaces, interfaces_size);
2030
interfaces_hooks_size = interfaces_size;
2068
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2069
interfaces_hooks_size = mc.interfaces_size;
2031
2070
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2119
2158
/* If no interfaces were specified, make a list */
2120
if(interfaces == NULL){
2159
if(mc.interfaces == NULL){
2121
2160
struct dirent **direntries;
2122
2161
/* Look for any good interfaces */
2123
2162
ret = scandir(sys_class_net, &direntries, good_interface,
2126
2165
/* Add all found interfaces to interfaces list */
2127
2166
for(int i = 0; i < ret; ++i){
2128
ret_errno = argz_add(&interfaces, &interfaces_size,
2167
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2129
2168
direntries[i]->d_name);
2130
2169
if(ret_errno != 0){
2131
2170
perror_plus("argz_add");
2148
/* If we only got one interface, explicitly use only that one */
2149
if(argz_count(interfaces, interfaces_size) == 1){
2151
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2154
if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2157
/* Bring up interfaces which are down */
2158
if(not (argz_count(interfaces, interfaces_size) == 1
2159
and strcmp(interfaces, "none") == 0)){
2187
/* Bring up interfaces which are down, and remove any "none"s */
2160
2189
char *interface = NULL;
2161
while((interface = argz_next(interfaces, interfaces_size,
2190
while((interface = argz_next(mc.interfaces, mc.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,
2163
2208
bool interface_was_up = interface_is_up(interface);
2164
2209
ret = bring_up_interface(interface, delay);
2165
2210
if(not interface_was_up){
2178
interfaces_size = 0;
2179
2221
if(debug and (interfaces_to_take_down == NULL)){
2180
2222
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);
2188
ret = init_gnutls_global(pubkey, seckey);
2239
ret = init_gnutls_global(pubkey, seckey, &mc);
2190
2241
fprintf_plus(stderr, "init_gnutls_global failed\n");
2191
2242
exitcode = EX_UNAVAILABLE;
2211
if(not init_gpgme(pubkey, seckey, tempdir)){
2262
if(not init_gpgme(pubkey, seckey, tempdir, &mc)){
2212
2263
fprintf_plus(stderr, "init_gpgme failed\n");
2213
2264
exitcode = EX_UNAVAILABLE;
2273
2324
while(not quit_now){
2274
ret = start_mandos_communication(address, port, if_index, af);
2325
ret = start_mandos_communication(address, port, if_index, af,
2275
2327
if(quit_now or ret == 0){
2325
2377
/* Create the Avahi service browser */
2326
2378
sb = avahi_s_service_browser_new(mc.server, if_index,
2327
2379
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2328
NULL, 0, browse_callback, NULL);
2380
NULL, 0, browse_callback,
2329
2382
if(sb == NULL){
2330
2383
fprintf_plus(stderr, "Failed to create service browser: %s\n",
2331
2384
avahi_strerror(avahi_server_errno(mc.server)));
2346
2399
ret = avahi_loop_with_timeout(simple_poll,
2347
(int)(retry_interval * 1000));
2400
(int)(retry_interval * 1000), &mc);
2349
2402
fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2350
2403
(ret == 0) ? "successfully" : "with error");