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
56
56
#include <sys/stat.h> /* open(), S_ISREG */
57
57
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
58
inet_pton(), connect(),
58
inet_pton(), connect() */
60
59
#include <fcntl.h> /* open() */
61
60
#include <dirent.h> /* opendir(), struct dirent, readdir()
63
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
64
#include <assert.h> /* assert() */
65
65
#include <errno.h> /* perror(), errno,
66
66
program_invocation_short_name */
67
67
#include <time.h> /* nanosleep(), time(), sleep() */
74
74
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
75
75
getuid(), getgid(), seteuid(),
76
76
setgid(), pause(), _exit() */
77
#include <arpa/inet.h> /* inet_pton(), htons() */
77
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
78
78
#include <iso646.h> /* not, or, and */
79
79
#include <argp.h> /* struct argp_option, error_t, struct
80
80
argp_state, struct argp,
92
92
argz_delete(), argz_append(),
93
93
argz_stringify(), argz_add(),
95
#include <netdb.h> /* getnameinfo(), NI_NUMERICHOST,
96
EAI_SYSTEM, gai_strerror() */
99
97
#include <sys/klog.h> /* klogctl() */
164
163
const char *priority;
166
165
server *current_server;
168
size_t interfaces_size;
169
166
} mandos_context;
171
/* global so signal handler can reach it*/
172
AvahiSimplePoll *simple_poll;
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 };
174
174
sig_atomic_t quit_now = 0;
175
175
int signal_received = 0;
191
191
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
192
192
program_invocation_short_name));
193
return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
193
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
201
201
size_t incbuffer(char **buffer, size_t buffer_length,
202
202
size_t buffer_capacity){
203
203
if(buffer_length + BUFFER_SIZE > buffer_capacity){
204
char *new_buf = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
206
int old_errno = errno;
204
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
213
208
buffer_capacity += BUFFER_SIZE;
215
210
return buffer_capacity;
218
213
/* Add server to set of servers to retry periodically */
219
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
220
int af, server **current_server){
214
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
222
217
server *new_server = malloc(sizeof(server));
223
218
if(new_server == NULL){
232
227
perror_plus("strdup");
235
ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
237
perror_plus("clock_gettime");
240
230
/* Special case of first server */
241
if(*current_server == NULL){
231
if (mc.current_server == NULL){
242
232
new_server->next = new_server;
243
233
new_server->prev = new_server;
244
*current_server = new_server;
234
mc.current_server = new_server;
245
235
/* Place the new server last in the list */
247
new_server->next = *current_server;
248
new_server->prev = (*current_server)->prev;
237
new_server->next = mc.current_server;
238
new_server->prev = mc.current_server->prev;
249
239
new_server->prev->next = new_server;
250
(*current_server)->prev = new_server;
240
mc.current_server->prev = new_server;
242
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
244
perror_plus("clock_gettime");
256
251
* Initialize GPGME.
258
253
static bool init_gpgme(const char *seckey, const char *pubkey,
259
const char *tempdir, mandos_context *mc){
254
const char *tempdir){
260
255
gpgme_error_t rc;
261
256
gpgme_engine_info_t engine_info;
264
260
* Helper function to insert pub and seckey to the engine keyring.
284
rc = gpgme_op_import(mc->ctx, pgp_data);
280
rc = gpgme_op_import(mc.ctx, pgp_data);
285
281
if(rc != GPG_ERR_NO_ERROR){
286
282
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
287
283
gpgme_strsource(rc), gpgme_strerror(rc));
333
329
/* Create new GPGME "context" */
334
rc = gpgme_new(&(mc->ctx));
330
rc = gpgme_new(&(mc.ctx));
335
331
if(rc != GPG_ERR_NO_ERROR){
336
332
fprintf_plus(stderr, "Mandos plugin mandos-client: "
337
333
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
386
381
/* Decrypt data from the cryptotext data buffer to the plaintext
388
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
383
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
389
384
if(rc != GPG_ERR_NO_ERROR){
390
385
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
391
386
gpgme_strsource(rc), gpgme_strerror(rc));
392
387
plaintext_length = -1;
394
389
gpgme_decrypt_result_t result;
395
result = gpgme_op_decrypt_result(mc->ctx);
390
result = gpgme_op_decrypt_result(mc.ctx);
396
391
if(result == NULL){
397
392
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
515
510
/* OpenPGP credentials */
516
ret = gnutls_certificate_allocate_credentials(&mc->cred);
511
ret = gnutls_certificate_allocate_credentials(&mc.cred);
517
512
if(ret != GNUTLS_E_SUCCESS){
518
513
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
519
514
safer_gnutls_strerror(ret));
531
526
ret = gnutls_certificate_set_openpgp_key_file
532
(mc->cred, pubkeyfilename, seckeyfilename,
527
(mc.cred, pubkeyfilename, seckeyfilename,
533
528
GNUTLS_OPENPGP_FMT_BASE64);
534
529
if(ret != GNUTLS_E_SUCCESS){
535
530
fprintf_plus(stderr,
543
538
/* GnuTLS server initialization */
544
ret = gnutls_dh_params_init(&mc->dh_params);
539
ret = gnutls_dh_params_init(&mc.dh_params);
545
540
if(ret != GNUTLS_E_SUCCESS){
546
541
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
547
542
" initialization: %s\n",
548
543
safer_gnutls_strerror(ret));
551
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
546
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
552
547
if(ret != GNUTLS_E_SUCCESS){
553
548
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
554
549
safer_gnutls_strerror(ret));
558
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
553
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
564
gnutls_certificate_free_credentials(mc->cred);
559
gnutls_certificate_free_credentials(mc.cred);
565
560
gnutls_global_deinit();
566
gnutls_dh_params_deinit(mc->dh_params);
561
gnutls_dh_params_deinit(mc.dh_params);
570
static int init_gnutls_session(gnutls_session_t *session,
565
static int init_gnutls_session(gnutls_session_t *session){
573
567
/* GnuTLS session creation */
619
613
/* ignore client certificate if any. */
620
614
gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
622
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
616
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
629
623
__attribute__((unused)) const char *txt){}
631
625
/* Called when a Mandos server is found */
632
static int start_mandos_communication(const char *ip, in_port_t port,
626
static int start_mandos_communication(const char *ip, uint16_t port,
633
627
AvahiIfIndex if_index,
634
int af, mandos_context *mc){
635
629
int ret, tcp_sd = -1;
637
struct sockaddr_storage to;
632
struct sockaddr_in in;
633
struct sockaddr_in6 in6;
638
635
char *buffer = NULL;
639
636
char *decrypted_buffer = NULL;
640
637
size_t buffer_length = 0;
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);
664
ret = init_gnutls_session(&session);
705
670
fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
706
PRIuMAX "\n", ip, (uintmax_t)port);
671
PRIu16 "\n", ip, port);
709
674
tcp_sd = socket(pf, SOCK_STREAM, 0);
722
687
memset(&to, 0, sizeof(to));
723
688
if(af == AF_INET6){
724
((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
725
ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
689
to.in6.sin6_family = (sa_family_t)af;
690
ret = inet_pton(af, ip, &to.in6.sin6_addr);
726
691
} else { /* IPv4 */
727
((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
728
ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
692
to.in.sin_family = (sa_family_t)af;
693
ret = inet_pton(af, ip, &to.in.sin_addr);
742
707
if(af == AF_INET6){
743
((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
744
if(IN6_IS_ADDR_LINKLOCAL
745
(&((struct sockaddr_in6 *)&to)->sin6_addr)){
708
to.in6.sin6_port = htons(port); /* Spurious warnings from
710
-Wunreachable-code */
712
if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
713
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
746
715
if(if_index == AVAHI_IF_UNSPEC){
747
716
fprintf_plus(stderr, "An IPv6 link-local address is"
748
717
" incomplete without a network interface\n");
767
738
if(if_indextoname((unsigned int)if_index, interface) == NULL){
768
739
perror_plus("if_indextoname");
770
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
771
"\n", ip, interface, (uintmax_t)port);
741
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
742
"\n", ip, interface, port);
774
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
775
ip, (uintmax_t)port);
745
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
777
748
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
778
749
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
779
751
if(af == AF_INET6){
780
ret = getnameinfo((struct sockaddr *)&to,
781
sizeof(struct sockaddr_in6),
782
addrstr, sizeof(addrstr), NULL, 0,
752
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
785
ret = getnameinfo((struct sockaddr *)&to,
786
sizeof(struct sockaddr_in),
787
addrstr, sizeof(addrstr), NULL, 0,
755
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
790
if(ret == EAI_SYSTEM){
791
perror_plus("getnameinfo");
792
} else if(ret != 0) {
793
fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
794
} else if(strcmp(addrstr, ip) != 0){
795
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
759
perror_plus("inet_ntop");
761
if(strcmp(addrstr, ip) != 0){
762
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
804
772
if(af == AF_INET6){
805
ret = connect(tcp_sd, (struct sockaddr *)&to,
806
sizeof(struct sockaddr_in6));
773
ret = connect(tcp_sd, &to.in6, sizeof(to));
808
ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
809
sizeof(struct sockaddr_in));
775
ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
812
778
if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
1061
1025
fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1062
1026
"'%s' of type '%s' in domain '%s': %s\n", name, type,
1064
avahi_strerror(avahi_server_errno
1065
(((mandos_context*)mc)->server)));
1028
avahi_strerror(avahi_server_errno(mc.server)));
1068
1031
case AVAHI_RESOLVER_FOUND:
1074
1037
PRIdMAX ") on port %" PRIu16 "\n", name,
1075
1038
host_name, ip, (intmax_t)interface, port);
1077
int ret = start_mandos_communication(ip, (in_port_t)port,
1079
avahi_proto_to_af(proto),
1040
int ret = start_mandos_communication(ip, port, interface,
1041
avahi_proto_to_af(proto));
1082
avahi_simple_poll_quit(simple_poll);
1043
avahi_simple_poll_quit(mc.simple_poll);
1084
if(not add_server(ip, (in_port_t)port, interface,
1085
avahi_proto_to_af(proto),
1086
&((mandos_context*)mc)->current_server)){
1045
if(not add_server(ip, port, interface,
1046
avahi_proto_to_af(proto))){
1087
1047
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1088
1048
" list\n", name);
1119
1077
case AVAHI_BROWSER_FAILURE:
1121
1079
fprintf_plus(stderr, "(Avahi browser) %s\n",
1122
avahi_strerror(avahi_server_errno
1123
(((mandos_context*)mc)->server)));
1124
avahi_simple_poll_quit(simple_poll);
1080
avahi_strerror(avahi_server_errno(mc.server)));
1081
avahi_simple_poll_quit(mc.simple_poll);
1127
1084
case AVAHI_BROWSER_NEW:
1130
1087
the callback function is called the Avahi server will free the
1131
1088
resolver for us. */
1133
if(avahi_s_service_resolver_new(((mandos_context*)mc)->server,
1134
interface, protocol, name, type,
1135
domain, protocol, 0,
1136
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)
1137
1093
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1139
avahi_strerror(avahi_server_errno
1140
(((mandos_context*)mc)->server)));
1095
avahi_strerror(avahi_server_errno(mc.server)));
1143
1098
case AVAHI_BROWSER_REMOVE:
1366
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1367
mandos_context *mc){
1321
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1369
1323
struct timespec now;
1370
1324
struct timespec waited_time;
1371
1325
intmax_t block_time;
1374
if(mc->current_server == NULL){
1328
if(mc.current_server == NULL){
1376
1330
fprintf_plus(stderr, "Wait until first server is found."
1377
1331
" No timeout!\n");
1391
1345
/* Calculating in ms how long time between now and server
1392
1346
who we visted longest time ago. Now - last seen. */
1393
1347
waited_time.tv_sec = (now.tv_sec
1394
- mc->current_server->last_seen.tv_sec);
1348
- mc.current_server->last_seen.tv_sec);
1395
1349
waited_time.tv_nsec = (now.tv_nsec
1396
- mc->current_server->last_seen.tv_nsec);
1350
- mc.current_server->last_seen.tv_nsec);
1397
1351
/* total time is 10s/10,000ms.
1398
1352
Converting to s from ms by dividing by 1,000,
1399
1353
and ns to ms by dividing by 1,000,000. */
1409
1363
if(block_time <= 0){
1410
ret = start_mandos_communication(mc->current_server->ip,
1411
mc->current_server->port,
1412
mc->current_server->if_index,
1413
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);
1415
avahi_simple_poll_quit(s);
1369
avahi_simple_poll_quit(mc.simple_poll);
1418
1372
ret = clock_gettime(CLOCK_MONOTONIC,
1419
&mc->current_server->last_seen);
1373
&mc.current_server->last_seen);
1421
1375
perror_plus("clock_gettime");
1424
mc->current_server = mc->current_server->next;
1378
mc.current_server = mc.current_server->next;
1425
1379
block_time = 0; /* Call avahi to find new Mandos
1426
1380
servers, but don't block */
1491
1445
bool run_network_hooks(const char *mode, const char *interface,
1492
1446
const float delay){
1493
1447
struct dirent **direntries;
1448
struct dirent *direntry;
1494
1450
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1496
1452
if(numhooks == -1){
1497
if(errno == ENOENT){
1499
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1500
" found\n", hookdir);
1503
perror_plus("scandir");
1453
perror_plus("scandir");
1506
struct dirent *direntry;
1508
1455
int devnull = open("/dev/null", O_RDONLY);
1509
1456
for(int i = 0; i < numhooks; i++){
1510
1457
direntry = direntries[i];
1764
1713
/* Raise priviliges */
1765
1714
raise_privileges();
1767
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1716
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1768
1717
ret_errno = errno;
1770
1719
/* Lower privileges */
1771
1720
lower_privileges();
1773
1722
/* Close the socket */
1774
int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1723
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1776
1725
perror_plus("close");
1794
1743
int main(int argc, char *argv[]){
1795
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1796
.priority = "SECURE256:!CTYPE-X.509:"
1797
"+CTYPE-OPENPGP", .current_server = NULL,
1798
.interfaces = NULL, .interfaces_size = 0 };
1799
1744
AvahiSServiceBrowser *sb = NULL;
1800
1745
error_t ret_errno;
1802
1747
intmax_t tmpmax;
1804
1749
int exitcode = EXIT_SUCCESS;
1750
char *interfaces = NULL;
1751
size_t interfaces_size = 0;
1805
1752
char *interfaces_to_take_down = NULL;
1806
1753
size_t interfaces_to_take_down_size = 0;
1807
1754
char tempdir[] = "/tmp/mandosXXXXXX";
1810
1757
const char *seckey = PATHDIR "/" SECKEY;
1811
1758
const char *pubkey = PATHDIR "/" PUBKEY;
1812
1759
char *interfaces_hooks = NULL;
1760
size_t interfaces_hooks_size = 0;
1814
1762
bool gnutls_initialized = false;
1815
1763
bool gpgme_initialized = false;
1906
1854
connect_to = arg;
1908
1856
case 'i': /* --interface */
1909
ret_errno = argz_add_sep(&mc.interfaces, &mc.interfaces_size,
1857
ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
1911
1859
if(ret_errno != 0){
1912
1860
argp_error(state, "%s", strerror(ret_errno));
2042
1990
/* Lower privileges */
1994
perror_plus("seteuid");
2047
/* Remove invalid interface names (except "none") */
1999
/* Remove empty interface names */
2049
2001
char *interface = NULL;
2050
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2002
while((interface = argz_next(interfaces, interfaces_size,
2052
if(strcmp(interface, "none") != 0
2053
and if_nametoindex(interface) == 0){
2054
if(interface[0] != '\0'){
2004
if(if_nametoindex(interface) == 0){
2005
if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2055
2006
fprintf_plus(stderr, "Not using nonexisting interface"
2056
2007
" \"%s\"\n", interface);
2058
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
2009
argz_delete(&interfaces, &interfaces_size, interface);
2059
2010
interface = NULL;
2064
2015
/* Run network hooks */
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
argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2017
ret_errno = argz_append(&interfaces_hooks, &interfaces_hooks_size,
2018
interfaces, interfaces_size);
2021
perror_plus("argz_append");
2075
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2076
interfaces_hooks : "", delay)){
2024
argz_stringify(interfaces_hooks, interfaces_hooks_size, (int)',');
2025
if(not run_network_hooks("start", interfaces_hooks, delay)){
2086
2035
from the signal handler */
2087
2036
/* Initialize the pseudo-RNG for Avahi */
2088
2037
srand((unsigned int) time(NULL));
2089
simple_poll = avahi_simple_poll_new();
2090
if(simple_poll == NULL){
2038
mc.simple_poll = avahi_simple_poll_new();
2039
if(mc.simple_poll == NULL){
2091
2040
fprintf_plus(stderr,
2092
2041
"Avahi: Failed to create simple poll object.\n");
2093
2042
exitcode = EX_UNAVAILABLE;
2160
2109
/* If no interfaces were specified, make a list */
2161
if(mc.interfaces == NULL){
2110
if(interfaces == NULL){
2162
2111
struct dirent **direntries;
2163
2112
/* Look for any good interfaces */
2164
2113
ret = scandir(sys_class_net, &direntries, good_interface,
2167
2116
/* Add all found interfaces to interfaces list */
2168
2117
for(int i = 0; i < ret; ++i){
2169
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2118
ret_errno = argz_add(&interfaces, &interfaces_size,
2170
2119
direntries[i]->d_name);
2171
2120
if(ret_errno != 0){
2173
2121
perror_plus("argz_add");
2190
/* 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)){
2192
2150
char *interface = NULL;
2193
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2151
while((interface = argz_next(interfaces, interfaces_size,
2195
/* If interface name is "none", stop bringing up interfaces.
2196
Also remove all instances of "none" from the list */
2197
if(strcmp(interface, "none") == 0){
2198
argz_delete(&mc.interfaces, &mc.interfaces_size,
2201
while((interface = argz_next(mc.interfaces,
2202
mc.interfaces_size, interface))){
2203
if(strcmp(interface, "none") == 0){
2204
argz_delete(&mc.interfaces, &mc.interfaces_size,
2211
2153
bool interface_was_up = interface_is_up(interface);
2212
2154
ret = bring_up_interface(interface, delay);
2213
2155
if(not interface_was_up){
2218
2160
ret_errno = argz_add(&interfaces_to_take_down,
2219
2161
&interfaces_to_take_down_size,
2223
perror_plus("argz_add");
2168
interfaces_size = 0;
2228
2169
if(debug and (interfaces_to_take_down == NULL)){
2229
2170
fprintf_plus(stderr, "No interfaces were brought up\n");
2233
/* If we only got one interface, explicitly use only that one */
2234
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2236
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2239
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
2246
ret = init_gnutls_global(pubkey, seckey, &mc);
2178
ret = init_gnutls_global(pubkey, seckey);
2248
2180
fprintf_plus(stderr, "init_gnutls_global failed\n");
2249
2181
exitcode = EX_UNAVAILABLE;
2269
if(not init_gpgme(pubkey, seckey, tempdir, &mc)){
2201
if(not init_gpgme(pubkey, seckey, tempdir)){
2270
2202
fprintf_plus(stderr, "init_gpgme failed\n");
2271
2203
exitcode = EX_UNAVAILABLE;
2298
2230
tmpmax = strtoimax(address+1, &tmp, 10);
2299
2231
if(errno != 0 or tmp == address+1 or *tmp != '\0'
2300
or tmpmax != (in_port_t)tmpmax){
2232
or tmpmax != (uint16_t)tmpmax){
2301
2233
fprintf_plus(stderr, "Bad port number\n");
2302
2234
exitcode = EX_USAGE;
2310
port = (in_port_t)tmpmax;
2242
port = (uint16_t)tmpmax;
2311
2243
*address = '\0';
2312
2244
/* Colon in address indicates IPv6 */
2331
2263
while(not quit_now){
2332
ret = start_mandos_communication(address, port, if_index, af,
2264
ret = start_mandos_communication(address, port, if_index, af);
2334
2265
if(quit_now or ret == 0){
2362
2293
config.publish_domain = 0;
2364
2295
/* Allocate a new server */
2365
mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2366
&config, NULL, NULL, &ret_errno);
2296
mc.server = avahi_server_new(avahi_simple_poll_get
2297
(mc.simple_poll), &config, NULL,
2368
2300
/* Free the Avahi configuration data */
2369
2301
avahi_server_config_free(&config);
2384
2316
/* Create the Avahi service browser */
2385
2317
sb = avahi_s_service_browser_new(mc.server, if_index,
2386
2318
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2387
NULL, 0, browse_callback,
2319
NULL, 0, browse_callback, NULL);
2389
2320
if(sb == NULL){
2390
2321
fprintf_plus(stderr, "Failed to create service browser: %s\n",
2391
2322
avahi_strerror(avahi_server_errno(mc.server)));
2403
2334
fprintf_plus(stderr, "Starting Avahi loop search\n");
2406
ret = avahi_loop_with_timeout(simple_poll,
2407
(int)(retry_interval * 1000), &mc);
2337
ret = avahi_loop_with_timeout(mc.simple_poll,
2338
(int)(retry_interval * 1000));
2409
2340
fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2410
2341
(ret == 0) ? "successfully" : "with error");
2419
2350
/* Cleanup things */
2420
free(mc.interfaces);
2423
2352
avahi_s_service_browser_free(sb);
2425
2354
if(mc.server != NULL)
2426
2355
avahi_server_free(mc.server);
2428
if(simple_poll != NULL)
2429
avahi_simple_poll_free(simple_poll);
2357
if(mc.simple_poll != NULL)
2358
avahi_simple_poll_free(mc.simple_poll);
2431
2360
if(gnutls_initialized){
2432
2361
gnutls_certificate_free_credentials(mc.cred);
2454
2383
raise_privileges();
2456
2385
/* Run network hooks */
2457
run_network_hooks("stop", interfaces_hooks != NULL ?
2458
interfaces_hooks : "", delay);
2386
run_network_hooks("stop", interfaces_hooks, delay);
2460
2388
/* Take down the network interfaces which were brought up */