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
62
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
64
#include <assert.h> /* assert() */
65
64
#include <errno.h> /* perror(), errno,
66
65
program_invocation_short_name */
67
66
#include <time.h> /* nanosleep(), time(), sleep() */
163
161
const char *priority;
165
163
server *current_server;
165
size_t interfaces_size;
166
166
} mandos_context;
168
/* global context so signal handler can reach it*/
169
mandos_context mc = { .simple_poll = NULL, .server = NULL,
170
.dh_bits = 1024, .priority = "SECURE256"
171
":!CTYPE-X.509:+CTYPE-OPENPGP",
172
.current_server = NULL };
168
/* global so signal handler can reach it*/
169
AvahiSimplePoll *simple_poll;
174
171
sig_atomic_t quit_now = 0;
175
172
int signal_received = 0;
191
188
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
192
189
program_invocation_short_name));
193
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
190
return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
201
198
size_t incbuffer(char **buffer, size_t buffer_length,
202
199
size_t buffer_capacity){
203
200
if(buffer_length + BUFFER_SIZE > buffer_capacity){
204
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
201
char *new_buf = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
203
int old_errno = errno;
208
210
buffer_capacity += BUFFER_SIZE;
210
212
return buffer_capacity;
213
215
/* Add server to set of servers to retry periodically */
214
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
216
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
217
int af, server **current_server){
217
219
server *new_server = malloc(sizeof(server));
218
220
if(new_server == NULL){
230
232
/* Special case of first server */
231
if (mc.current_server == NULL){
233
if(*current_server == NULL){
232
234
new_server->next = new_server;
233
235
new_server->prev = new_server;
234
mc.current_server = new_server;
236
*current_server = new_server;
235
237
/* Place the new server last in the list */
237
new_server->next = mc.current_server;
238
new_server->prev = mc.current_server->prev;
239
new_server->next = *current_server;
240
new_server->prev = (*current_server)->prev;
239
241
new_server->prev->next = new_server;
240
mc.current_server->prev = new_server;
242
(*current_server)->prev = new_server;
242
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
244
ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
244
246
perror_plus("clock_gettime");
251
253
* Initialize GPGME.
253
255
static bool init_gpgme(const char *seckey, const char *pubkey,
254
const char *tempdir){
256
const char *tempdir, mandos_context *mc){
255
257
gpgme_error_t rc;
256
258
gpgme_engine_info_t engine_info;
260
261
* Helper function to insert pub and seckey to the engine keyring.
280
rc = gpgme_op_import(mc.ctx, pgp_data);
281
rc = gpgme_op_import(mc->ctx, pgp_data);
281
282
if(rc != GPG_ERR_NO_ERROR){
282
283
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
283
284
gpgme_strsource(rc), gpgme_strerror(rc));
329
330
/* Create new GPGME "context" */
330
rc = gpgme_new(&(mc.ctx));
331
rc = gpgme_new(&(mc->ctx));
331
332
if(rc != GPG_ERR_NO_ERROR){
332
333
fprintf_plus(stderr, "Mandos plugin mandos-client: "
333
334
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
381
383
/* Decrypt data from the cryptotext data buffer to the plaintext
383
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
385
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
384
386
if(rc != GPG_ERR_NO_ERROR){
385
387
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
386
388
gpgme_strsource(rc), gpgme_strerror(rc));
387
389
plaintext_length = -1;
389
391
gpgme_decrypt_result_t result;
390
result = gpgme_op_decrypt_result(mc.ctx);
392
result = gpgme_op_decrypt_result(mc->ctx);
391
393
if(result == NULL){
392
394
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
510
512
/* OpenPGP credentials */
511
ret = gnutls_certificate_allocate_credentials(&mc.cred);
513
ret = gnutls_certificate_allocate_credentials(&mc->cred);
512
514
if(ret != GNUTLS_E_SUCCESS){
513
515
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
514
516
safer_gnutls_strerror(ret));
526
528
ret = gnutls_certificate_set_openpgp_key_file
527
(mc.cred, pubkeyfilename, seckeyfilename,
529
(mc->cred, pubkeyfilename, seckeyfilename,
528
530
GNUTLS_OPENPGP_FMT_BASE64);
529
531
if(ret != GNUTLS_E_SUCCESS){
530
532
fprintf_plus(stderr,
538
540
/* GnuTLS server initialization */
539
ret = gnutls_dh_params_init(&mc.dh_params);
541
ret = gnutls_dh_params_init(&mc->dh_params);
540
542
if(ret != GNUTLS_E_SUCCESS){
541
543
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
542
544
" initialization: %s\n",
543
545
safer_gnutls_strerror(ret));
546
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
548
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
547
549
if(ret != GNUTLS_E_SUCCESS){
548
550
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
549
551
safer_gnutls_strerror(ret));
553
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
555
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
559
gnutls_certificate_free_credentials(mc.cred);
561
gnutls_certificate_free_credentials(mc->cred);
560
562
gnutls_global_deinit();
561
gnutls_dh_params_deinit(mc.dh_params);
563
gnutls_dh_params_deinit(mc->dh_params);
565
static int init_gnutls_session(gnutls_session_t *session){
567
static int init_gnutls_session(gnutls_session_t *session,
567
570
/* GnuTLS session creation */
613
616
/* ignore client certificate if any. */
614
617
gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
616
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
619
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
623
626
__attribute__((unused)) const char *txt){}
625
628
/* Called when a Mandos server is found */
626
static int start_mandos_communication(const char *ip, uint16_t port,
629
static int start_mandos_communication(const char *ip, in_port_t port,
627
630
AvahiIfIndex if_index,
631
int af, mandos_context *mc){
629
632
int ret, tcp_sd = -1;
664
ret = init_gnutls_session(&session);
667
/* If the interface is specified and we have a list of interfaces */
668
if(if_index != AVAHI_IF_UNSPEC and mc->interfaces != NULL){
669
/* Check if the interface is one of the interfaces we are using */
672
char *interface = NULL;
673
while((interface=argz_next(mc->interfaces, mc->interfaces_size,
675
if(if_nametoindex(interface) == (unsigned int)if_index){
682
/* This interface does not match any in the list, so we don't
683
connect to the server */
685
char interface[IF_NAMESIZE];
686
if(if_indextoname((unsigned int)if_index, interface) == NULL){
687
perror_plus("if_indextoname");
689
fprintf_plus(stderr, "Skipping server on non-used interface"
691
if_indextoname((unsigned int)if_index,
699
ret = init_gnutls_session(&session, mc);
670
705
fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
671
PRIu16 "\n", ip, port);
706
PRIuMAX "\n", ip, (uintmax_t)port);
674
709
tcp_sd = socket(pf, SOCK_STREAM, 0);
707
742
if(af == AF_INET6){
708
to.in6.sin6_port = htons(port); /* Spurious warnings from
710
-Wunreachable-code */
743
to.in6.sin6_port = htons(port);
745
#pragma GCC diagnostic push
746
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
712
748
if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
713
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
749
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower */
751
#pragma GCC diagnostic pop
715
753
if(if_index == AVAHI_IF_UNSPEC){
716
754
fprintf_plus(stderr, "An IPv6 link-local address is"
717
755
" incomplete without a network interface\n");
738
774
if(if_indextoname((unsigned int)if_index, interface) == NULL){
739
775
perror_plus("if_indextoname");
741
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
742
"\n", ip, interface, port);
777
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
778
"\n", ip, interface, (uintmax_t)port);
745
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
781
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
782
ip, (uintmax_t)port);
748
784
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
749
785
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1009
1045
AVAHI_GCC_UNUSED AvahiStringList *txt,
1010
1046
AVAHI_GCC_UNUSED AvahiLookupResultFlags
1012
AVAHI_GCC_UNUSED void* userdata){
1015
1053
/* Called whenever a service has been resolved successfully or
1025
1063
fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1026
1064
"'%s' of type '%s' in domain '%s': %s\n", name, type,
1028
avahi_strerror(avahi_server_errno(mc.server)));
1066
avahi_strerror(avahi_server_errno
1067
(((mandos_context*)mc)->server)));
1031
1070
case AVAHI_RESOLVER_FOUND:
1037
1076
PRIdMAX ") on port %" PRIu16 "\n", name,
1038
1077
host_name, ip, (intmax_t)interface, port);
1040
int ret = start_mandos_communication(ip, port, interface,
1041
avahi_proto_to_af(proto));
1079
int ret = start_mandos_communication(ip, (in_port_t)port,
1081
avahi_proto_to_af(proto),
1043
avahi_simple_poll_quit(mc.simple_poll);
1084
avahi_simple_poll_quit(simple_poll);
1045
if(not add_server(ip, port, interface,
1046
avahi_proto_to_af(proto))){
1086
if(not add_server(ip, (in_port_t)port, interface,
1087
avahi_proto_to_af(proto),
1088
&((mandos_context*)mc)->current_server)){
1047
1089
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1048
1090
" list\n", name);
1077
1121
case AVAHI_BROWSER_FAILURE:
1079
1123
fprintf_plus(stderr, "(Avahi browser) %s\n",
1080
avahi_strerror(avahi_server_errno(mc.server)));
1081
avahi_simple_poll_quit(mc.simple_poll);
1124
avahi_strerror(avahi_server_errno
1125
(((mandos_context*)mc)->server)));
1126
avahi_simple_poll_quit(simple_poll);
1084
1129
case AVAHI_BROWSER_NEW:
1087
1132
the callback function is called the Avahi server will free the
1088
1133
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)
1135
if(avahi_s_service_resolver_new(((mandos_context*)mc)->server,
1136
interface, protocol, name, type,
1137
domain, protocol, 0,
1138
resolve_callback, mc) == NULL)
1093
1139
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1095
avahi_strerror(avahi_server_errno(mc.server)));
1141
avahi_strerror(avahi_server_errno
1142
(((mandos_context*)mc)->server)));
1098
1145
case AVAHI_BROWSER_REMOVE:
1321
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1368
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1369
mandos_context *mc){
1323
1371
struct timespec now;
1324
1372
struct timespec waited_time;
1325
1373
intmax_t block_time;
1328
if(mc.current_server == NULL){
1376
if(mc->current_server == NULL){
1330
1378
fprintf_plus(stderr, "Wait until first server is found."
1331
1379
" No timeout!\n");
1345
1393
/* Calculating in ms how long time between now and server
1346
1394
who we visted longest time ago. Now - last seen. */
1347
1395
waited_time.tv_sec = (now.tv_sec
1348
- mc.current_server->last_seen.tv_sec);
1396
- mc->current_server->last_seen.tv_sec);
1349
1397
waited_time.tv_nsec = (now.tv_nsec
1350
- mc.current_server->last_seen.tv_nsec);
1398
- mc->current_server->last_seen.tv_nsec);
1351
1399
/* total time is 10s/10,000ms.
1352
1400
Converting to s from ms by dividing by 1,000,
1353
1401
and ns to ms by dividing by 1,000,000. */
1363
1411
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);
1412
ret = start_mandos_communication(mc->current_server->ip,
1413
mc->current_server->port,
1414
mc->current_server->if_index,
1415
mc->current_server->af, mc);
1369
avahi_simple_poll_quit(mc.simple_poll);
1417
avahi_simple_poll_quit(s);
1372
1420
ret = clock_gettime(CLOCK_MONOTONIC,
1373
&mc.current_server->last_seen);
1421
&mc->current_server->last_seen);
1375
1423
perror_plus("clock_gettime");
1378
mc.current_server = mc.current_server->next;
1426
mc->current_server = mc->current_server->next;
1379
1427
block_time = 0; /* Call avahi to find new Mandos
1380
1428
servers, but don't block */
1445
1493
bool run_network_hooks(const char *mode, const char *interface,
1446
1494
const float delay){
1447
1495
struct dirent **direntries;
1448
struct dirent *direntry;
1450
1496
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1452
1498
if(numhooks == -1){
1453
perror_plus("scandir");
1499
if(errno == ENOENT){
1501
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1502
" found\n", hookdir);
1505
perror_plus("scandir");
1508
struct dirent *direntry;
1455
1510
int devnull = open("/dev/null", O_RDONLY);
1456
1511
for(int i = 0; i < numhooks; i++){
1457
1512
direntry = direntries[i];
1698
1751
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1700
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1753
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1702
1755
ret_errno = errno;
1703
1756
perror_plus("socket");
1713
1766
/* Raise priviliges */
1714
1767
raise_privileges();
1716
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1769
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1717
1770
ret_errno = errno;
1719
1772
/* Lower privileges */
1720
1773
lower_privileges();
1722
1775
/* Close the socket */
1723
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1776
int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1725
1778
perror_plus("close");
1743
1796
int main(int argc, char *argv[]){
1797
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1798
.priority = "SECURE256:!CTYPE-X.509:"
1799
"+CTYPE-OPENPGP", .current_server = NULL,
1800
.interfaces = NULL, .interfaces_size = 0 };
1744
1801
AvahiSServiceBrowser *sb = NULL;
1745
1802
error_t ret_errno;
1747
1804
intmax_t tmpmax;
1749
1806
int exitcode = EXIT_SUCCESS;
1750
char *interfaces = NULL;
1751
size_t interfaces_size = 0;
1752
1807
char *interfaces_to_take_down = NULL;
1753
1808
size_t interfaces_to_take_down_size = 0;
1754
1809
char tempdir[] = "/tmp/mandosXXXXXX";
1757
1812
const char *seckey = PATHDIR "/" SECKEY;
1758
1813
const char *pubkey = PATHDIR "/" PUBKEY;
1759
1814
char *interfaces_hooks = NULL;
1760
size_t interfaces_hooks_size = 0;
1762
1816
bool gnutls_initialized = false;
1763
1817
bool gpgme_initialized = false;
1854
1908
connect_to = arg;
1856
1910
case 'i': /* --interface */
1857
ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
1911
ret_errno = argz_add_sep(&mc.interfaces, &mc.interfaces_size,
1859
1913
if(ret_errno != 0){
1860
1914
argp_error(state, "%s", strerror(ret_errno));
1990
2044
/* Lower privileges */
1994
perror_plus("seteuid");
1999
/* Remove empty interface names */
2049
/* Remove invalid interface names (except "none") */
2001
2051
char *interface = NULL;
2002
while((interface = argz_next(interfaces, interfaces_size,
2052
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){
2054
if(strcmp(interface, "none") != 0
2055
and if_nametoindex(interface) == 0){
2056
if(interface[0] != '\0'){
2006
2057
fprintf_plus(stderr, "Not using nonexisting interface"
2007
2058
" \"%s\"\n", interface);
2009
argz_delete(&interfaces, &interfaces_size, interface);
2060
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
2010
2061
interface = NULL;
2015
2066
/* Run network hooks */
2017
ret_errno = argz_append(&interfaces_hooks, &interfaces_hooks_size,
2018
interfaces, interfaces_size);
2021
perror_plus("argz_append");
2068
if(mc.interfaces != NULL){
2069
interfaces_hooks = malloc(mc.interfaces_size);
2070
if(interfaces_hooks == NULL){
2071
perror_plus("malloc");
2074
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2075
argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
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
2175
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)){
2192
/* Bring up interfaces which are down, and remove any "none"s */
2150
2194
char *interface = NULL;
2151
while((interface = argz_next(interfaces, interfaces_size,
2195
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2197
/* If interface name is "none", stop bringing up interfaces.
2198
Also remove all instances of "none" from the list */
2199
if(strcmp(interface, "none") == 0){
2200
argz_delete(&mc.interfaces, &mc.interfaces_size,
2203
while((interface = argz_next(mc.interfaces,
2204
mc.interfaces_size, interface))){
2205
if(strcmp(interface, "none") == 0){
2206
argz_delete(&mc.interfaces, &mc.interfaces_size,
2153
2213
bool interface_was_up = interface_is_up(interface);
2154
2214
ret = bring_up_interface(interface, delay);
2155
2215
if(not interface_was_up){
2160
2220
ret_errno = argz_add(&interfaces_to_take_down,
2161
2221
&interfaces_to_take_down_size,
2225
perror_plus("argz_add");
2168
interfaces_size = 0;
2169
2230
if(debug and (interfaces_to_take_down == NULL)){
2170
2231
fprintf_plus(stderr, "No interfaces were brought up\n");
2235
/* If we only got one interface, explicitly use only that one */
2236
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2238
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2241
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
2178
ret = init_gnutls_global(pubkey, seckey);
2248
ret = init_gnutls_global(pubkey, seckey, &mc);
2180
2250
fprintf_plus(stderr, "init_gnutls_global failed\n");
2181
2251
exitcode = EX_UNAVAILABLE;
2201
if(not init_gpgme(pubkey, seckey, tempdir)){
2271
if(not init_gpgme(pubkey, seckey, tempdir, &mc)){
2202
2272
fprintf_plus(stderr, "init_gpgme failed\n");
2203
2273
exitcode = EX_UNAVAILABLE;
2230
2300
tmpmax = strtoimax(address+1, &tmp, 10);
2231
2301
if(errno != 0 or tmp == address+1 or *tmp != '\0'
2232
or tmpmax != (uint16_t)tmpmax){
2302
or tmpmax != (in_port_t)tmpmax){
2233
2303
fprintf_plus(stderr, "Bad port number\n");
2234
2304
exitcode = EX_USAGE;
2242
port = (uint16_t)tmpmax;
2312
port = (in_port_t)tmpmax;
2243
2313
*address = '\0';
2244
2314
/* Colon in address indicates IPv6 */
2263
2333
while(not quit_now){
2264
ret = start_mandos_communication(address, port, if_index, af);
2334
ret = start_mandos_communication(address, port, if_index, af,
2265
2336
if(quit_now or ret == 0){
2293
2364
config.publish_domain = 0;
2295
2366
/* Allocate a new server */
2296
mc.server = avahi_server_new(avahi_simple_poll_get
2297
(mc.simple_poll), &config, NULL,
2367
mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2368
&config, NULL, NULL, &ret_errno);
2300
2370
/* Free the Avahi configuration data */
2301
2371
avahi_server_config_free(&config);
2316
2386
/* Create the Avahi service browser */
2317
2387
sb = avahi_s_service_browser_new(mc.server, if_index,
2318
2388
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2319
NULL, 0, browse_callback, NULL);
2389
NULL, 0, browse_callback,
2320
2391
if(sb == NULL){
2321
2392
fprintf_plus(stderr, "Failed to create service browser: %s\n",
2322
2393
avahi_strerror(avahi_server_errno(mc.server)));
2334
2405
fprintf_plus(stderr, "Starting Avahi loop search\n");
2337
ret = avahi_loop_with_timeout(mc.simple_poll,
2338
(int)(retry_interval * 1000));
2408
ret = avahi_loop_with_timeout(simple_poll,
2409
(int)(retry_interval * 1000), &mc);
2340
2411
fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2341
2412
(ret == 0) ? "successfully" : "with error");
2350
2421
/* Cleanup things */
2422
free(mc.interfaces);
2352
2425
avahi_s_service_browser_free(sb);
2354
2427
if(mc.server != NULL)
2355
2428
avahi_server_free(mc.server);
2357
if(mc.simple_poll != NULL)
2358
avahi_simple_poll_free(mc.simple_poll);
2430
if(simple_poll != NULL)
2431
avahi_simple_poll_free(simple_poll);
2360
2433
if(gnutls_initialized){
2361
2434
gnutls_certificate_free_credentials(mc.cred);
2383
2456
raise_privileges();
2385
2458
/* Run network hooks */
2386
run_network_hooks("stop", interfaces_hooks, delay);
2459
run_network_hooks("stop", interfaces_hooks != NULL ?
2460
interfaces_hooks : "", delay);
2388
2462
/* Take down the network interfaces which were brought up */