72
71
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
74
73
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
75
getuid(), getgid(), seteuid(),
77
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
74
getuid(), getgid(), setuid(),
76
#include <arpa/inet.h> /* inet_pton(), htons */
78
77
#include <iso646.h> /* not, or, and */
79
78
#include <argp.h> /* struct argp_option, error_t, struct
80
79
argp_state, struct argp,
81
80
argp_parse(), ARGP_KEY_ARG,
82
81
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
83
82
#include <signal.h> /* sigemptyset(), sigaddset(),
84
sigaction(), SIGTERM, sig_atomic_t,
86
#include <sysexits.h> /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
87
EX_NOHOST, EX_IOERR, EX_PROTOCOL */
88
#include <sys/wait.h> /* waitpid(), WIFEXITED(),
89
WEXITSTATUS(), WTERMSIG() */
83
sigaction(), SIGTERM, sigaction,
92
87
#include <sys/klog.h> /* klogctl() */
199
159
return buffer_capacity;
202
/* Add server to set of servers to retry periodically */
203
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
206
server *new_server = malloc(sizeof(server));
207
if(new_server == NULL){
208
perror_plus("malloc");
211
*new_server = (server){ .ip = strdup(ip),
213
.if_index = if_index,
215
if(new_server->ip == NULL){
216
perror_plus("strdup");
219
/* Special case of first server */
220
if (mc.current_server == NULL){
221
new_server->next = new_server;
222
new_server->prev = new_server;
223
mc.current_server = new_server;
224
/* Place the new server last in the list */
226
new_server->next = mc.current_server;
227
new_server->prev = mc.current_server->prev;
228
new_server->prev->next = new_server;
229
mc.current_server->prev = new_server;
231
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
233
perror_plus("clock_gettime");
240
163
* Initialize GPGME.
242
static bool init_gpgme(const char *seckey, const char *pubkey,
243
const char *tempdir){
165
static bool init_gpgme(const char *seckey,
166
const char *pubkey, const char *tempdir){
244
168
gpgme_error_t rc;
245
169
gpgme_engine_info_t engine_info;
249
173
* Helper function to insert pub and seckey to the engine keyring.
251
175
bool import_key(const char *filename){
254
177
gpgme_data_t pgp_data;
256
179
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
262
185
rc = gpgme_data_new_from_fd(&pgp_data, fd);
263
186
if(rc != GPG_ERR_NO_ERROR){
264
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
187
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
265
188
gpgme_strsource(rc), gpgme_strerror(rc));
269
192
rc = gpgme_op_import(mc.ctx, pgp_data);
270
193
if(rc != GPG_ERR_NO_ERROR){
271
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
194
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
272
195
gpgme_strsource(rc), gpgme_strerror(rc));
276
199
ret = (int)TEMP_FAILURE_RETRY(close(fd));
278
perror_plus("close");
280
203
gpgme_data_release(pgp_data);
285
fprintf_plus(stderr, "Initializing GPGME\n");
208
fprintf(stderr, "Initializing GPGME\n");
289
212
gpgme_check_version(NULL);
290
213
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
291
214
if(rc != GPG_ERR_NO_ERROR){
292
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
215
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
293
216
gpgme_strsource(rc), gpgme_strerror(rc));
297
/* Set GPGME home directory for the OpenPGP engine only */
220
/* Set GPGME home directory for the OpenPGP engine only */
298
221
rc = gpgme_get_engine_info(&engine_info);
299
222
if(rc != GPG_ERR_NO_ERROR){
300
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
223
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
301
224
gpgme_strsource(rc), gpgme_strerror(rc));
371
292
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
372
293
if(rc != GPG_ERR_NO_ERROR){
373
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
294
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
374
295
gpgme_strsource(rc), gpgme_strerror(rc));
375
296
plaintext_length = -1;
377
298
gpgme_decrypt_result_t result;
378
299
result = gpgme_op_decrypt_result(mc.ctx);
379
300
if(result == NULL){
380
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
301
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
382
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
303
fprintf(stderr, "Unsupported algorithm: %s\n",
383
304
result->unsupported_algorithm);
384
fprintf_plus(stderr, "Wrong key usage: %u\n",
305
fprintf(stderr, "Wrong key usage: %u\n",
385
306
result->wrong_key_usage);
386
307
if(result->file_name != NULL){
387
fprintf_plus(stderr, "File name: %s\n", result->file_name);
308
fprintf(stderr, "File name: %s\n", result->file_name);
389
310
gpgme_recipient_t recipient;
390
311
recipient = result->recipients;
391
while(recipient != NULL){
392
fprintf_plus(stderr, "Public key algorithm: %s\n",
393
gpgme_pubkey_algo_name(recipient->pubkey_algo));
394
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
395
fprintf_plus(stderr, "Secret key available: %s\n",
396
recipient->status == GPG_ERR_NO_SECKEY
398
recipient = recipient->next;
313
while(recipient != NULL){
314
fprintf(stderr, "Public key algorithm: %s\n",
315
gpgme_pubkey_algo_name(recipient->pubkey_algo));
316
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
317
fprintf(stderr, "Secret key available: %s\n",
318
recipient->status == GPG_ERR_NO_SECKEY
320
recipient = recipient->next;
511
439
(mc.cred, pubkeyfilename, seckeyfilename,
512
440
GNUTLS_OPENPGP_FMT_BASE64);
513
441
if(ret != GNUTLS_E_SUCCESS){
515
"Error[%d] while reading the OpenPGP key pair ('%s',"
516
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
517
fprintf_plus(stderr, "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
443
"Error[%d] while reading the OpenPGP key pair ('%s',"
444
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
445
fprintf(stderr, "The GnuTLS error is: %s\n",
446
safer_gnutls_strerror(ret));
521
450
/* GnuTLS server initialization */
522
451
ret = gnutls_dh_params_init(&mc.dh_params);
523
452
if(ret != GNUTLS_E_SUCCESS){
524
fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
453
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
525
454
" %s\n", safer_gnutls_strerror(ret));
528
457
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
529
458
if(ret != GNUTLS_E_SUCCESS){
530
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
459
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
531
460
safer_gnutls_strerror(ret));
547
476
static int init_gnutls_session(gnutls_session_t *session){
549
478
/* GnuTLS session creation */
551
ret = gnutls_init(session, GNUTLS_SERVER);
555
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
479
ret = gnutls_init(session, GNUTLS_SERVER);
556
480
if(ret != GNUTLS_E_SUCCESS){
557
fprintf_plus(stderr, "Error in GnuTLS session initialization: %s\n",
481
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
558
482
safer_gnutls_strerror(ret));
564
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
566
gnutls_deinit(*session);
569
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
487
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
570
488
if(ret != GNUTLS_E_SUCCESS){
571
fprintf_plus(stderr, "Syntax error at: %s\n", err);
572
fprintf_plus(stderr, "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
489
fprintf(stderr, "Syntax error at: %s\n", err);
490
fprintf(stderr, "GnuTLS error: %s\n",
491
safer_gnutls_strerror(ret));
573
492
gnutls_deinit(*session);
579
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
582
gnutls_deinit(*session);
585
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
497
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
586
499
if(ret != GNUTLS_E_SUCCESS){
587
fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
500
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
588
501
safer_gnutls_strerror(ret));
589
502
gnutls_deinit(*session);
593
506
/* ignore client certificate if any. */
594
gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
507
gnutls_certificate_server_set_request(*session,
596
510
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
1070
871
case AVAHI_BROWSER_ALL_FOR_NOW:
1071
872
case AVAHI_BROWSER_CACHE_EXHAUSTED:
1073
fprintf_plus(stderr, "No Mandos server found, still searching...\n");
874
fprintf(stderr, "No Mandos server found, still searching...\n");
1079
/* Signal handler that stops main loop after SIGTERM */
1080
static void handle_sigterm(int sig){
880
sig_atomic_t quit_now = 0;
882
/* stop main loop after sigterm has been called */
883
static void handle_sigterm(__attribute__((unused)) int sig){
1085
signal_received = sig;
1086
888
int old_errno = errno;
1087
/* set main loop to exit */
1088
889
if(mc.simple_poll != NULL){
1089
890
avahi_simple_poll_quit(mc.simple_poll);
1091
892
errno = old_errno;
1094
bool get_flags(const char *ifname, struct ifreq *ifr){
1097
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1099
perror_plus("socket");
1102
strcpy(ifr->ifr_name, ifname);
1103
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1106
perror_plus("ioctl SIOCGIFFLAGS");
1113
bool good_flags(const char *ifname, const struct ifreq *ifr){
1115
/* Reject the loopback device */
1116
if(ifr->ifr_flags & IFF_LOOPBACK){
1118
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n", ifname);
1122
/* Accept point-to-point devices only if connect_to is specified */
1123
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1125
fprintf_plus(stderr, "Accepting point-to-point interface \"%s\"\n", ifname);
1129
/* Otherwise, reject non-broadcast-capable devices */
1130
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1132
fprintf_plus(stderr, "Rejecting non-broadcast interface \"%s\"\n", ifname);
1136
/* Reject non-ARP interfaces (including dummy interfaces) */
1137
if(ifr->ifr_flags & IFF_NOARP){
1139
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1144
/* Accept this device */
1146
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1152
* This function determines if a directory entry in /sys/class/net
1153
* corresponds to an acceptable network device.
1154
* (This function is passed to scandir(3) as a filter function.)
1156
int good_interface(const struct dirent *if_entry){
1157
if(if_entry->d_name[0] == '.'){
1162
if(not get_flags(if_entry->d_name, &ifr)){
1164
fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1170
if(not good_flags(if_entry->d_name, &ifr)){
1177
* This function determines if a directory entry in /sys/class/net
1178
* corresponds to an acceptable network device which is up.
1179
* (This function is passed to scandir(3) as a filter function.)
1181
int up_interface(const struct dirent *if_entry){
1182
if(if_entry->d_name[0] == '.'){
1187
if(not get_flags(if_entry->d_name, &ifr)){
1189
fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1195
/* Reject down interfaces */
1196
if(not (ifr.ifr_flags & IFF_UP)){
1198
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1204
/* Reject non-running interfaces */
1205
if(not (ifr.ifr_flags & IFF_RUNNING)){
1207
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1213
if(not good_flags(if_entry->d_name, &ifr)){
1219
int notdotentries(const struct dirent *direntry){
1220
/* Skip "." and ".." */
1221
if(direntry->d_name[0] == '.'
1222
and (direntry->d_name[1] == '\0'
1223
or (direntry->d_name[1] == '.'
1224
and direntry->d_name[2] == '\0'))){
1230
/* Is this directory entry a runnable program? */
1231
int runnable_hook(const struct dirent *direntry){
1236
if((direntry->d_name)[0] == '\0'){
1241
sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1242
"abcdefghijklmnopqrstuvwxyz"
1245
if((direntry->d_name)[sret] != '\0'){
1246
/* Contains non-allowed characters */
1248
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1254
char *fullname = NULL;
1255
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1257
perror_plus("asprintf");
1261
ret = stat(fullname, &st);
1264
perror_plus("Could not stat hook");
1268
if(not (S_ISREG(st.st_mode))){
1269
/* Not a regular file */
1271
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1276
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1277
/* Not executable */
1279
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1287
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1289
struct timespec now;
1290
struct timespec waited_time;
1291
intmax_t block_time;
1294
if(mc.current_server == NULL){
1296
fprintf_plus(stderr, "Wait until first server is found. No timeout!\n");
1298
ret = avahi_simple_poll_iterate(s, -1);
1301
fprintf_plus(stderr, "Check current_server if we should run it,"
1304
/* the current time */
1305
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1307
perror_plus("clock_gettime");
1310
/* Calculating in ms how long time between now and server
1311
who we visted longest time ago. Now - last seen. */
1312
waited_time.tv_sec = (now.tv_sec
1313
- mc.current_server->last_seen.tv_sec);
1314
waited_time.tv_nsec = (now.tv_nsec
1315
- mc.current_server->last_seen.tv_nsec);
1316
/* total time is 10s/10,000ms.
1317
Converting to s from ms by dividing by 1,000,
1318
and ns to ms by dividing by 1,000,000. */
1319
block_time = ((retry_interval
1320
- ((intmax_t)waited_time.tv_sec * 1000))
1321
- ((intmax_t)waited_time.tv_nsec / 1000000));
1324
fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1327
if(block_time <= 0){
1328
ret = start_mandos_communication(mc.current_server->ip,
1329
mc.current_server->port,
1330
mc.current_server->if_index,
1331
mc.current_server->af);
1333
avahi_simple_poll_quit(mc.simple_poll);
1336
ret = clock_gettime(CLOCK_MONOTONIC,
1337
&mc.current_server->last_seen);
1339
perror_plus("clock_gettime");
1342
mc.current_server = mc.current_server->next;
1343
block_time = 0; /* Call avahi to find new Mandos
1344
servers, but don't block */
1347
ret = avahi_simple_poll_iterate(s, (int)block_time);
1350
if (ret > 0 or errno != EINTR){
1351
return (ret != 1) ? ret : 0;
1357
895
int main(int argc, char *argv[]){
1358
896
AvahiSServiceBrowser *sb = NULL;
1493
991
delay = strtof(arg, &tmp);
1494
992
if(errno != 0 or tmp == arg or *tmp != '\0'){
1495
argp_error(state, "Bad delay");
1497
case 132: /* --retry */
1499
retry_interval = strtod(arg, &tmp);
1500
if(errno != 0 or tmp == arg or *tmp != '\0'
1501
or (retry_interval * 1000) > INT_MAX
1502
or retry_interval < 0){
1503
argp_error(state, "Bad retry interval");
1506
case 133: /* --network-hook-dir */
1510
* These reproduce what we would get without ARGP_NO_HELP
1512
case '?': /* --help */
1513
argp_state_help(state, state->out_stream,
1514
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1515
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1516
case -3: /* --usage */
1517
argp_state_help(state, state->out_stream,
1518
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1519
case 'V': /* --version */
1520
fprintf_plus(state->out_stream, "Mandos plugin mandos-client: ");
1521
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1522
exit(argp_err_exit_status);
993
fprintf(stderr, "Bad delay\n");
1525
1002
return ARGP_ERR_UNKNOWN;
1530
1007
struct argp argp = { .options = options, .parser = parse_opt,
1531
1008
.args_doc = "",
1532
1009
.doc = "Mandos client -- Get and decrypt"
1533
1010
" passwords from a Mandos server" };
1534
ret = argp_parse(&argp, argc, argv,
1535
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1542
perror_plus("argp_parse");
1543
exitcode = EX_OSERR;
1546
exitcode = EX_USAGE;
1552
/* Work around Debian bug #633582:
1553
<http://bugs.debian.org/633582> */
1556
/* Re-raise priviliges */
1560
perror_plus("seteuid");
1563
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1564
int seckey_fd = open(seckey, O_RDONLY);
1565
if(seckey_fd == -1){
1566
perror_plus("open");
1568
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1570
perror_plus("fstat");
1572
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1573
ret = fchown(seckey_fd, uid, gid);
1575
perror_plus("fchown");
1579
TEMP_FAILURE_RETRY(close(seckey_fd));
1583
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1584
int pubkey_fd = open(pubkey, O_RDONLY);
1585
if(pubkey_fd == -1){
1586
perror_plus("open");
1588
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1590
perror_plus("fstat");
1592
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1593
ret = fchown(pubkey_fd, uid, gid);
1595
perror_plus("fchown");
1599
TEMP_FAILURE_RETRY(close(pubkey_fd));
1603
/* Lower privileges */
1607
perror_plus("seteuid");
1611
/* Find network hooks and run them */
1613
struct dirent **direntries;
1614
struct dirent *direntry;
1615
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1618
perror_plus("scandir");
1620
int devnull = open("/dev/null", O_RDONLY);
1621
for(int i = 0; i < numhooks; i++){
1622
direntry = direntries[0];
1623
char *fullname = NULL;
1624
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1626
perror_plus("asprintf");
1629
pid_t hook_pid = fork();
1632
dup2(devnull, STDIN_FILENO);
1634
dup2(STDERR_FILENO, STDOUT_FILENO);
1635
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1637
perror_plus("setenv");
1640
ret = setenv("DEVICE", interface, 1);
1642
perror_plus("setenv");
1645
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1647
perror_plus("setenv");
1650
ret = setenv("MODE", "start", 1);
1652
perror_plus("setenv");
1656
ret = asprintf(&delaystring, "%f", delay);
1658
perror_plus("asprintf");
1661
ret = setenv("DELAY", delaystring, 1);
1664
perror_plus("setenv");
1668
ret = execl(fullname, direntry->d_name, "start", NULL);
1669
perror_plus("execl");
1672
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1673
perror_plus("waitpid");
1677
if(WIFEXITED(status)){
1678
if(WEXITSTATUS(status) != 0){
1679
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1680
" with status %d\n", direntry->d_name,
1681
WEXITSTATUS(status));
1685
} else if(WIFSIGNALED(status)){
1686
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1687
" signal %d\n", direntry->d_name,
1692
fprintf_plus(stderr, "Warning: network hook \"%s\" crashed\n",
1011
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1012
if(ret == ARGP_ERR_UNKNOWN){
1013
fprintf(stderr, "Unknown error while parsing arguments\n");
1014
exitcode = EXIT_FAILURE;
1708
1020
avahi_set_log_function(empty_log);
1711
if(interface[0] == '\0'){
1712
struct dirent **direntries;
1713
/* First look for interfaces that are up */
1714
ret = scandir(sys_class_net, &direntries, up_interface,
1717
/* No up interfaces, look for any good interfaces */
1719
ret = scandir(sys_class_net, &direntries, good_interface,
1723
/* Pick the first interface returned */
1724
interface = strdup(direntries[0]->d_name);
1726
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1728
if(interface == NULL){
1729
perror_plus("malloc");
1731
exitcode = EXIT_FAILURE;
1737
fprintf_plus(stderr, "Could not find a network interface\n");
1738
exitcode = EXIT_FAILURE;
1743
1023
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1744
1024
from the signal handler */
1745
1025
/* Initialize the pseudo-RNG for Avahi */
1746
1026
srand((unsigned int) time(NULL));
1747
1027
mc.simple_poll = avahi_simple_poll_new();
1748
1028
if(mc.simple_poll == NULL){
1749
fprintf_plus(stderr, "Avahi: Failed to create simple poll object.\n");
1750
exitcode = EX_UNAVAILABLE;
1029
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1030
exitcode = EXIT_FAILURE;
1754
1034
sigemptyset(&sigterm_action.sa_mask);
1755
1035
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1757
perror_plus("sigaddset");
1758
exitcode = EX_OSERR;
1037
perror("sigaddset");
1038
exitcode = EXIT_FAILURE;
1761
1041
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1763
perror_plus("sigaddset");
1764
exitcode = EX_OSERR;
1043
perror("sigaddset");
1044
exitcode = EXIT_FAILURE;
1767
1047
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1769
perror_plus("sigaddset");
1770
exitcode = EX_OSERR;
1773
/* Need to check if the handler is SIG_IGN before handling:
1774
| [[info:libc:Initial Signal Actions]] |
1775
| [[info:libc:Basic Signal Handling]] |
1777
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1779
perror_plus("sigaction");
1782
if(old_sigterm_action.sa_handler != SIG_IGN){
1783
ret = sigaction(SIGINT, &sigterm_action, NULL);
1785
perror_plus("sigaction");
1786
exitcode = EX_OSERR;
1790
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1792
perror_plus("sigaction");
1795
if(old_sigterm_action.sa_handler != SIG_IGN){
1796
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1798
perror_plus("sigaction");
1799
exitcode = EX_OSERR;
1803
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1805
perror_plus("sigaction");
1808
if(old_sigterm_action.sa_handler != SIG_IGN){
1809
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1811
perror_plus("sigaction");
1812
exitcode = EX_OSERR;
1049
perror("sigaddset");
1050
exitcode = EXIT_FAILURE;
1053
ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1055
perror("sigaction");
1056
exitcode = EXIT_FAILURE;
1817
1060
/* If the interface is down, bring it up */
1818
if(strcmp(interface, "none") != 0){
1819
if_index = (AvahiIfIndex) if_nametoindex(interface);
1821
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1822
exitcode = EX_UNAVAILABLE;
1830
/* Re-raise priviliges */
1834
perror_plus("seteuid");
1061
if(interface[0] != '\0'){
1837
1062
#ifdef __linux__
1838
1063
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1839
messages about the network interface to mess up the prompt */
1064
messages to mess up the prompt */
1840
1065
ret = klogctl(8, NULL, 5);
1841
1066
bool restore_loglevel = true;
1843
1068
restore_loglevel = false;
1844
perror_plus("klogctl");
1846
1071
#endif /* __linux__ */
1848
1073
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1850
perror_plus("socket");
1851
exitcode = EX_OSERR;
1076
exitcode = EXIT_FAILURE;
1852
1077
#ifdef __linux__
1853
1078
if(restore_loglevel){
1854
1079
ret = klogctl(7, NULL, 0);
1856
perror_plus("klogctl");
1859
1084
#endif /* __linux__ */
1860
/* Lower privileges */
1864
perror_plus("seteuid");
1868
1087
strcpy(network.ifr_name, interface);
1869
1088
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1871
perror_plus("ioctl SIOCGIFFLAGS");
1090
perror("ioctl SIOCGIFFLAGS");
1872
1091
#ifdef __linux__
1873
1092
if(restore_loglevel){
1874
1093
ret = klogctl(7, NULL, 0);
1876
perror_plus("klogctl");
1879
1098
#endif /* __linux__ */
1880
exitcode = EX_OSERR;
1881
/* Lower privileges */
1885
perror_plus("seteuid");
1099
exitcode = EXIT_FAILURE;
1889
1102
if((network.ifr_flags & IFF_UP) == 0){
1890
1103
network.ifr_flags |= IFF_UP;
1891
take_down_interface = true;
1892
1104
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1894
take_down_interface = false;
1895
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1896
exitcode = EX_OSERR;
1106
perror("ioctl SIOCSIFFLAGS");
1107
exitcode = EXIT_FAILURE;
1897
1108
#ifdef __linux__
1898
1109
if(restore_loglevel){
1899
1110
ret = klogctl(7, NULL, 0);
1901
perror_plus("klogctl");
1904
1115
#endif /* __linux__ */
1905
/* Lower privileges */
1909
perror_plus("seteuid");
1914
/* Sleep checking until interface is running.
1915
Check every 0.25s, up to total time of delay */
1119
/* sleep checking until interface is running */
1916
1120
for(int i=0; i < delay * 4; i++){
1917
1121
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1919
perror_plus("ioctl SIOCGIFFLAGS");
1123
perror("ioctl SIOCGIFFLAGS");
1920
1124
} else if(network.ifr_flags & IFF_RUNNING){
1923
1127
struct timespec sleeptime = { .tv_nsec = 250000000 };
1924
1128
ret = nanosleep(&sleeptime, NULL);
1925
1129
if(ret == -1 and errno != EINTR){
1926
perror_plus("nanosleep");
1130
perror("nanosleep");
1929
if(not take_down_interface){
1930
/* We won't need the socket anymore */
1931
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1933
perror_plus("close");
1133
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1936
1137
#ifdef __linux__
1937
1138
if(restore_loglevel){
1938
1139
/* Restores kernel loglevel to default */
1939
1140
ret = klogctl(7, NULL, 0);
1941
perror_plus("klogctl");
1944
1145
#endif /* __linux__ */
1945
/* Lower privileges */
1947
if(take_down_interface){
1948
/* Lower privileges */
1951
perror_plus("seteuid");
1954
/* Lower privileges permanently */
1957
perror_plus("setuid");
1966
1162
ret = init_gnutls_global(pubkey, seckey);
1968
fprintf_plus(stderr, "init_gnutls_global failed\n");
1969
exitcode = EX_UNAVAILABLE;
1164
fprintf(stderr, "init_gnutls_global failed\n");
1165
exitcode = EXIT_FAILURE;
1972
1168
gnutls_initialized = true;
1979
1171
if(mkdtemp(tempdir) == NULL){
1980
perror_plus("mkdtemp");
1983
1175
tempdir_created = true;
1989
1177
if(not init_gpgme(pubkey, seckey, tempdir)){
1990
fprintf_plus(stderr, "init_gpgme failed\n");
1991
exitcode = EX_UNAVAILABLE;
1178
fprintf(stderr, "init_gpgme failed\n");
1179
exitcode = EXIT_FAILURE;
1994
1182
gpgme_initialized = true;
1185
if(interface[0] != '\0'){
1186
if_index = (AvahiIfIndex) if_nametoindex(interface);
1188
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1189
exitcode = EXIT_FAILURE;
2001
1194
if(connect_to != NULL){
2003
1196
/* (Mainly meant for debugging) */
2004
1197
char *address = strrchr(connect_to, ':');
2005
1198
if(address == NULL){
2006
fprintf_plus(stderr, "No colon in address\n");
2007
exitcode = EX_USAGE;
1199
fprintf(stderr, "No colon in address\n");
1200
exitcode = EXIT_FAILURE;
2017
1205
tmpmax = strtoimax(address+1, &tmp, 10);
2018
1206
if(errno != 0 or tmp == address+1 or *tmp != '\0'
2019
1207
or tmpmax != (uint16_t)tmpmax){
2020
fprintf_plus(stderr, "Bad port number\n");
2021
exitcode = EX_USAGE;
1208
fprintf(stderr, "Bad port number\n");
1209
exitcode = EXIT_FAILURE;
2029
1212
port = (uint16_t)tmpmax;
2030
1213
*address = '\0';
1214
address = connect_to;
2031
1215
/* Colon in address indicates IPv6 */
2033
if(strchr(connect_to, ':') != NULL){
1217
if(strchr(address, ':') != NULL){
2035
/* Accept [] around IPv6 address - see RFC 5952 */
2036
if(connect_to[0] == '[' and address[-1] == ']')
2044
address = connect_to;
2050
while(not quit_now){
2051
ret = start_mandos_communication(address, port, if_index, af);
2052
if(quit_now or ret == 0){
2056
fprintf_plus(stderr, "Retrying in %d seconds\n", (int)retry_interval);
2058
sleep((int)retry_interval);
1222
ret = start_mandos_communication(address, port, if_index, af);
1224
exitcode = EXIT_FAILURE;
2062
1226
exitcode = EXIT_SUCCESS;
2073
1232
AvahiServerConfig config;
2074
1233
/* Do not publish any local Zeroconf records */
2152
1298
if(gpgme_initialized){
2153
1299
gpgme_release(mc.ctx);
2156
/* Cleans up the circular linked list of Mandos servers the client
2158
if(mc.current_server != NULL){
2159
mc.current_server->prev->next = NULL;
2160
while(mc.current_server != NULL){
2161
server *next = mc.current_server->next;
2162
free(mc.current_server);
2163
mc.current_server = next;
2167
/* XXX run network hooks "stop" here */
2169
/* Take down the network interface */
2170
if(take_down_interface){
2171
/* Re-raise priviliges */
2175
perror_plus("seteuid");
2178
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2180
perror_plus("ioctl SIOCGIFFLAGS");
2181
} else if(network.ifr_flags & IFF_UP){
2182
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2183
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2185
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2188
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2190
perror_plus("close");
2192
/* Lower privileges permanently */
2196
perror_plus("setuid");
2201
/* Removes the GPGME temp directory and all files inside */
1302
/* Removes the temp directory used by GPGME */
2202
1303
if(tempdir_created){
2203
struct dirent **direntries = NULL;
2204
struct dirent *direntry = NULL;
2205
int numentries = scandir(tempdir, &direntries, notdotentries,
2207
if (numentries > 0){
2208
for(int i = 0; i < numentries; i++){
2209
direntry = direntries[i];
1305
struct dirent *direntry;
1306
d = opendir(tempdir);
1308
if(errno != ENOENT){
1313
direntry = readdir(d);
1314
if(direntry == NULL){
1317
/* Skip "." and ".." */
1318
if(direntry->d_name[0] == '.'
1319
and (direntry->d_name[1] == '\0'
1320
or (direntry->d_name[1] == '.'
1321
and direntry->d_name[2] == '\0'))){
2210
1324
char *fullname = NULL;
2211
1325
ret = asprintf(&fullname, "%s/%s", tempdir,
2212
1326
direntry->d_name);
2214
perror_plus("asprintf");
2217
1331
ret = remove(fullname);
2219
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname, strerror(errno));
1333
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2221
1336
free(fullname);
2225
/* need to clean even if 0 because man page doesn't specify */
2227
if (numentries == -1){
2228
perror_plus("scandir");
2230
1340
ret = rmdir(tempdir);
2231
1341
if(ret == -1 and errno != ENOENT){
2232
perror_plus("rmdir");
2237
sigemptyset(&old_sigterm_action.sa_mask);
2238
old_sigterm_action.sa_handler = SIG_DFL;
2239
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2240
&old_sigterm_action,
2243
perror_plus("sigaction");
2246
ret = raise(signal_received);
2247
} while(ret != 0 and errno == EINTR);
2249
perror_plus("raise");
2252
TEMP_FAILURE_RETRY(pause());
2255
1346
return exitcode;