71
72
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
73
74
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
74
getuid(), getgid(), setuid(),
76
#include <arpa/inet.h> /* inet_pton(), htons */
75
getuid(), getgid(), seteuid(),
77
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
77
78
#include <iso646.h> /* not, or, and */
78
79
#include <argp.h> /* struct argp_option, error_t, struct
79
80
argp_state, struct argp,
80
81
argp_parse(), ARGP_KEY_ARG,
81
82
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
83
#include <signal.h> /* sigemptyset(), sigaddset(),
83
sigaction(), SIGTERM, sigaction,
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() */
87
92
#include <sys/klog.h> /* klogctl() */
159
199
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");
163
240
* Initialize GPGME.
165
static bool init_gpgme(const char *seckey,
166
const char *pubkey, const char *tempdir){
242
static bool init_gpgme(const char *seckey, const char *pubkey,
243
const char *tempdir){
168
244
gpgme_error_t rc;
169
245
gpgme_engine_info_t engine_info;
173
249
* Helper function to insert pub and seckey to the engine keyring.
175
251
bool import_key(const char *filename){
177
254
gpgme_data_t pgp_data;
179
256
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
185
262
rc = gpgme_data_new_from_fd(&pgp_data, fd);
186
263
if(rc != GPG_ERR_NO_ERROR){
187
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
264
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
188
265
gpgme_strsource(rc), gpgme_strerror(rc));
192
269
rc = gpgme_op_import(mc.ctx, pgp_data);
193
270
if(rc != GPG_ERR_NO_ERROR){
194
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
271
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
195
272
gpgme_strsource(rc), gpgme_strerror(rc));
199
276
ret = (int)TEMP_FAILURE_RETRY(close(fd));
278
perror_plus("close");
203
280
gpgme_data_release(pgp_data);
208
fprintf(stderr, "Initializing GPGME\n");
285
fprintf_plus(stderr, "Initializing GPGME\n");
212
289
gpgme_check_version(NULL);
213
290
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
214
291
if(rc != GPG_ERR_NO_ERROR){
215
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
292
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
216
293
gpgme_strsource(rc), gpgme_strerror(rc));
220
/* Set GPGME home directory for the OpenPGP engine only */
297
/* Set GPGME home directory for the OpenPGP engine only */
221
298
rc = gpgme_get_engine_info(&engine_info);
222
299
if(rc != GPG_ERR_NO_ERROR){
223
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
300
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
224
301
gpgme_strsource(rc), gpgme_strerror(rc));
292
371
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
293
372
if(rc != GPG_ERR_NO_ERROR){
294
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
373
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
295
374
gpgme_strsource(rc), gpgme_strerror(rc));
296
375
plaintext_length = -1;
298
377
gpgme_decrypt_result_t result;
299
378
result = gpgme_op_decrypt_result(mc.ctx);
300
379
if(result == NULL){
301
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
380
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
303
fprintf(stderr, "Unsupported algorithm: %s\n",
382
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
304
383
result->unsupported_algorithm);
305
fprintf(stderr, "Wrong key usage: %u\n",
384
fprintf_plus(stderr, "Wrong key usage: %u\n",
306
385
result->wrong_key_usage);
307
386
if(result->file_name != NULL){
308
fprintf(stderr, "File name: %s\n", result->file_name);
387
fprintf_plus(stderr, "File name: %s\n", result->file_name);
310
389
gpgme_recipient_t recipient;
311
390
recipient = result->recipients;
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;
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;
439
511
(mc.cred, pubkeyfilename, seckeyfilename,
440
512
GNUTLS_OPENPGP_FMT_BASE64);
441
513
if(ret != GNUTLS_E_SUCCESS){
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));
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));
450
521
/* GnuTLS server initialization */
451
522
ret = gnutls_dh_params_init(&mc.dh_params);
452
523
if(ret != GNUTLS_E_SUCCESS){
453
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
524
fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
454
525
" %s\n", safer_gnutls_strerror(ret));
457
528
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
458
529
if(ret != GNUTLS_E_SUCCESS){
459
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
530
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
460
531
safer_gnutls_strerror(ret));
476
547
static int init_gnutls_session(gnutls_session_t *session){
478
549
/* GnuTLS session creation */
479
ret = gnutls_init(session, GNUTLS_SERVER);
551
ret = gnutls_init(session, GNUTLS_SERVER);
555
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
480
556
if(ret != GNUTLS_E_SUCCESS){
481
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
557
fprintf_plus(stderr, "Error in GnuTLS session initialization: %s\n",
482
558
safer_gnutls_strerror(ret));
487
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
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);
488
570
if(ret != GNUTLS_E_SUCCESS){
489
fprintf(stderr, "Syntax error at: %s\n", err);
490
fprintf(stderr, "GnuTLS error: %s\n",
491
safer_gnutls_strerror(ret));
571
fprintf_plus(stderr, "Syntax error at: %s\n", err);
572
fprintf_plus(stderr, "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
492
573
gnutls_deinit(*session);
497
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
579
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
582
gnutls_deinit(*session);
585
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
499
586
if(ret != GNUTLS_E_SUCCESS){
500
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
587
fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
501
588
safer_gnutls_strerror(ret));
502
589
gnutls_deinit(*session);
506
593
/* ignore client certificate if any. */
507
gnutls_certificate_server_set_request(*session,
594
gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
510
596
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
871
1070
case AVAHI_BROWSER_ALL_FOR_NOW:
872
1071
case AVAHI_BROWSER_CACHE_EXHAUSTED:
874
fprintf(stderr, "No Mandos server found, still searching...\n");
1073
fprintf_plus(stderr, "No Mandos server found, still searching...\n");
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){
1079
/* Signal handler that stops main loop after SIGTERM */
1080
static void handle_sigterm(int sig){
1085
signal_received = sig;
888
1086
int old_errno = errno;
1087
/* set main loop to exit */
889
1088
if(mc.simple_poll != NULL){
890
1089
avahi_simple_poll_quit(mc.simple_poll);
892
1091
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;
895
1357
int main(int argc, char *argv[]){
896
1358
AvahiSServiceBrowser *sb = NULL;
991
1493
delay = strtof(arg, &tmp);
992
1494
if(errno != 0 or tmp == arg or *tmp != '\0'){
993
fprintf(stderr, "Bad delay\n");
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);
1002
1525
return ARGP_ERR_UNKNOWN;
1007
1530
struct argp argp = { .options = options, .parser = parse_opt,
1008
1531
.args_doc = "",
1009
1532
.doc = "Mandos client -- Get and decrypt"
1010
1533
" passwords from a Mandos server" };
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;
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",
1020
1708
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;
1023
1743
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1024
1744
from the signal handler */
1025
1745
/* Initialize the pseudo-RNG for Avahi */
1026
1746
srand((unsigned int) time(NULL));
1027
1747
mc.simple_poll = avahi_simple_poll_new();
1028
1748
if(mc.simple_poll == NULL){
1029
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1030
exitcode = EXIT_FAILURE;
1749
fprintf_plus(stderr, "Avahi: Failed to create simple poll object.\n");
1750
exitcode = EX_UNAVAILABLE;
1034
1754
sigemptyset(&sigterm_action.sa_mask);
1035
1755
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1037
perror("sigaddset");
1038
exitcode = EXIT_FAILURE;
1757
perror_plus("sigaddset");
1758
exitcode = EX_OSERR;
1041
1761
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1043
perror("sigaddset");
1044
exitcode = EXIT_FAILURE;
1763
perror_plus("sigaddset");
1764
exitcode = EX_OSERR;
1047
1767
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1049
perror("sigaddset");
1050
exitcode = EXIT_FAILURE;
1053
ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1055
perror("sigaction");
1056
exitcode = EXIT_FAILURE;
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;
1060
1817
/* If the interface is down, bring it up */
1061
if(interface[0] != '\0'){
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");
1062
1837
#ifdef __linux__
1063
1838
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1064
messages to mess up the prompt */
1839
messages about the network interface to mess up the prompt */
1065
1840
ret = klogctl(8, NULL, 5);
1066
1841
bool restore_loglevel = true;
1068
1843
restore_loglevel = false;
1844
perror_plus("klogctl");
1071
1846
#endif /* __linux__ */
1073
1848
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1076
exitcode = EXIT_FAILURE;
1850
perror_plus("socket");
1851
exitcode = EX_OSERR;
1077
1852
#ifdef __linux__
1078
1853
if(restore_loglevel){
1079
1854
ret = klogctl(7, NULL, 0);
1856
perror_plus("klogctl");
1084
1859
#endif /* __linux__ */
1860
/* Lower privileges */
1864
perror_plus("seteuid");
1087
1868
strcpy(network.ifr_name, interface);
1088
1869
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1090
perror("ioctl SIOCGIFFLAGS");
1871
perror_plus("ioctl SIOCGIFFLAGS");
1091
1872
#ifdef __linux__
1092
1873
if(restore_loglevel){
1093
1874
ret = klogctl(7, NULL, 0);
1876
perror_plus("klogctl");
1098
1879
#endif /* __linux__ */
1099
exitcode = EXIT_FAILURE;
1880
exitcode = EX_OSERR;
1881
/* Lower privileges */
1885
perror_plus("seteuid");
1102
1889
if((network.ifr_flags & IFF_UP) == 0){
1103
1890
network.ifr_flags |= IFF_UP;
1891
take_down_interface = true;
1104
1892
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1106
perror("ioctl SIOCSIFFLAGS");
1107
exitcode = EXIT_FAILURE;
1894
take_down_interface = false;
1895
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1896
exitcode = EX_OSERR;
1108
1897
#ifdef __linux__
1109
1898
if(restore_loglevel){
1110
1899
ret = klogctl(7, NULL, 0);
1901
perror_plus("klogctl");
1115
1904
#endif /* __linux__ */
1905
/* Lower privileges */
1909
perror_plus("seteuid");
1119
/* sleep checking until interface is running */
1914
/* Sleep checking until interface is running.
1915
Check every 0.25s, up to total time of delay */
1120
1916
for(int i=0; i < delay * 4; i++){
1121
1917
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1123
perror("ioctl SIOCGIFFLAGS");
1919
perror_plus("ioctl SIOCGIFFLAGS");
1124
1920
} else if(network.ifr_flags & IFF_RUNNING){
1127
1923
struct timespec sleeptime = { .tv_nsec = 250000000 };
1128
1924
ret = nanosleep(&sleeptime, NULL);
1129
1925
if(ret == -1 and errno != EINTR){
1130
perror("nanosleep");
1926
perror_plus("nanosleep");
1133
ret = (int)TEMP_FAILURE_RETRY(close(sd));
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");
1137
1936
#ifdef __linux__
1138
1937
if(restore_loglevel){
1139
1938
/* Restores kernel loglevel to default */
1140
1939
ret = klogctl(7, NULL, 0);
1941
perror_plus("klogctl");
1145
1944
#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");
1162
1966
ret = init_gnutls_global(pubkey, seckey);
1164
fprintf(stderr, "init_gnutls_global failed\n");
1165
exitcode = EXIT_FAILURE;
1968
fprintf_plus(stderr, "init_gnutls_global failed\n");
1969
exitcode = EX_UNAVAILABLE;
1168
1972
gnutls_initialized = true;
1171
1979
if(mkdtemp(tempdir) == NULL){
1980
perror_plus("mkdtemp");
1175
1983
tempdir_created = true;
1177
1989
if(not init_gpgme(pubkey, seckey, tempdir)){
1178
fprintf(stderr, "init_gpgme failed\n");
1179
exitcode = EXIT_FAILURE;
1990
fprintf_plus(stderr, "init_gpgme failed\n");
1991
exitcode = EX_UNAVAILABLE;
1182
1994
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;
1194
2001
if(connect_to != NULL){
1196
2003
/* (Mainly meant for debugging) */
1197
2004
char *address = strrchr(connect_to, ':');
1198
2005
if(address == NULL){
1199
fprintf(stderr, "No colon in address\n");
1200
exitcode = EXIT_FAILURE;
2006
fprintf_plus(stderr, "No colon in address\n");
2007
exitcode = EX_USAGE;
1205
2017
tmpmax = strtoimax(address+1, &tmp, 10);
1206
2018
if(errno != 0 or tmp == address+1 or *tmp != '\0'
1207
2019
or tmpmax != (uint16_t)tmpmax){
1208
fprintf(stderr, "Bad port number\n");
1209
exitcode = EXIT_FAILURE;
2020
fprintf_plus(stderr, "Bad port number\n");
2021
exitcode = EX_USAGE;
1212
2029
port = (uint16_t)tmpmax;
1213
2030
*address = '\0';
1214
address = connect_to;
1215
2031
/* Colon in address indicates IPv6 */
1217
if(strchr(address, ':') != NULL){
2033
if(strchr(connect_to, ':') != NULL){
2035
/* Accept [] around IPv6 address - see RFC 5952 */
2036
if(connect_to[0] == '[' and address[-1] == ']')
1222
ret = start_mandos_communication(address, port, if_index, af);
1224
exitcode = EXIT_FAILURE;
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);
1226
2062
exitcode = EXIT_SUCCESS;
1232
2073
AvahiServerConfig config;
1233
2074
/* Do not publish any local Zeroconf records */
1298
2152
if(gpgme_initialized){
1299
2153
gpgme_release(mc.ctx);
1302
/* Removes the temp directory used by GPGME */
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 */
1303
2202
if(tempdir_created){
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'))){
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];
1324
2210
char *fullname = NULL;
1325
2211
ret = asprintf(&fullname, "%s/%s", tempdir,
1326
2212
direntry->d_name);
2214
perror_plus("asprintf");
1331
2217
ret = remove(fullname);
1333
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2219
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname, strerror(errno));
1336
2221
free(fullname);
2225
/* need to clean even if 0 because man page doesn't specify */
2227
if (numentries == -1){
2228
perror_plus("scandir");
1340
2230
ret = rmdir(tempdir);
1341
2231
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());
1346
2255
return exitcode;