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(),
76
setgid(), pause(), _exit() */
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() */
90
#include <grp.h> /* setgroups() */
87
93
#include <sys/klog.h> /* klogctl() */
173
252
* Helper function to insert pub and seckey to the engine keyring.
175
254
bool import_key(const char *filename){
177
257
gpgme_data_t pgp_data;
179
259
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
185
265
rc = gpgme_data_new_from_fd(&pgp_data, fd);
186
266
if(rc != GPG_ERR_NO_ERROR){
187
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
188
gpgme_strsource(rc), gpgme_strerror(rc));
267
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
268
gpgme_strsource(rc), gpgme_strerror(rc));
192
272
rc = gpgme_op_import(mc.ctx, pgp_data);
193
273
if(rc != GPG_ERR_NO_ERROR){
194
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
195
gpgme_strsource(rc), gpgme_strerror(rc));
274
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
275
gpgme_strsource(rc), gpgme_strerror(rc));
199
279
ret = (int)TEMP_FAILURE_RETRY(close(fd));
281
perror_plus("close");
203
283
gpgme_data_release(pgp_data);
208
fprintf(stderr, "Initializing GPGME\n");
288
fprintf_plus(stderr, "Initializing GPGME\n");
212
292
gpgme_check_version(NULL);
213
293
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
214
294
if(rc != GPG_ERR_NO_ERROR){
215
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
216
gpgme_strsource(rc), gpgme_strerror(rc));
295
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
296
gpgme_strsource(rc), gpgme_strerror(rc));
220
/* Set GPGME home directory for the OpenPGP engine only */
300
/* Set GPGME home directory for the OpenPGP engine only */
221
301
rc = gpgme_get_engine_info(&engine_info);
222
302
if(rc != GPG_ERR_NO_ERROR){
223
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
224
gpgme_strsource(rc), gpgme_strerror(rc));
303
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
304
gpgme_strsource(rc), gpgme_strerror(rc));
227
307
while(engine_info != NULL){
292
375
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
293
376
if(rc != GPG_ERR_NO_ERROR){
294
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
295
gpgme_strsource(rc), gpgme_strerror(rc));
377
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
378
gpgme_strsource(rc), gpgme_strerror(rc));
296
379
plaintext_length = -1;
298
381
gpgme_decrypt_result_t result;
299
382
result = gpgme_op_decrypt_result(mc.ctx);
300
383
if(result == NULL){
301
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
384
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
303
fprintf(stderr, "Unsupported algorithm: %s\n",
304
result->unsupported_algorithm);
305
fprintf(stderr, "Wrong key usage: %u\n",
306
result->wrong_key_usage);
386
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
387
result->unsupported_algorithm);
388
fprintf_plus(stderr, "Wrong key usage: %u\n",
389
result->wrong_key_usage);
307
390
if(result->file_name != NULL){
308
fprintf(stderr, "File name: %s\n", result->file_name);
391
fprintf_plus(stderr, "File name: %s\n", result->file_name);
310
393
gpgme_recipient_t recipient;
311
394
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;
395
while(recipient != NULL){
396
fprintf_plus(stderr, "Public key algorithm: %s\n",
397
gpgme_pubkey_algo_name
398
(recipient->pubkey_algo));
399
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
400
fprintf_plus(stderr, "Secret key available: %s\n",
401
recipient->status == GPG_ERR_NO_SECKEY
403
recipient = recipient->next;
420
502
/* OpenPGP credentials */
421
gnutls_certificate_allocate_credentials(&mc.cred);
503
ret = gnutls_certificate_allocate_credentials(&mc.cred);
422
504
if(ret != GNUTLS_E_SUCCESS){
423
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
427
safer_gnutls_strerror(ret));
505
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
506
safer_gnutls_strerror(ret));
428
507
gnutls_global_deinit();
433
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
434
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
512
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
513
" secret key %s as GnuTLS credentials\n",
438
518
ret = gnutls_certificate_set_openpgp_key_file
439
519
(mc.cred, pubkeyfilename, seckeyfilename,
440
520
GNUTLS_OPENPGP_FMT_BASE64);
441
521
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));
523
"Error[%d] while reading the OpenPGP key pair ('%s',"
524
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
525
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
526
safer_gnutls_strerror(ret));
450
530
/* GnuTLS server initialization */
451
531
ret = gnutls_dh_params_init(&mc.dh_params);
452
532
if(ret != GNUTLS_E_SUCCESS){
453
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
454
" %s\n", safer_gnutls_strerror(ret));
533
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
534
" initialization: %s\n",
535
safer_gnutls_strerror(ret));
457
538
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
458
539
if(ret != GNUTLS_E_SUCCESS){
459
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
460
safer_gnutls_strerror(ret));
540
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
541
safer_gnutls_strerror(ret));
476
557
static int init_gnutls_session(gnutls_session_t *session){
478
559
/* GnuTLS session creation */
479
ret = gnutls_init(session, GNUTLS_SERVER);
561
ret = gnutls_init(session, GNUTLS_SERVER);
565
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
480
566
if(ret != GNUTLS_E_SUCCESS){
481
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
482
safer_gnutls_strerror(ret));
568
"Error in GnuTLS session initialization: %s\n",
569
safer_gnutls_strerror(ret));
487
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
575
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
577
gnutls_deinit(*session);
580
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
488
581
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));
582
fprintf_plus(stderr, "Syntax error at: %s\n", err);
583
fprintf_plus(stderr, "GnuTLS error: %s\n",
584
safer_gnutls_strerror(ret));
492
585
gnutls_deinit(*session);
497
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
591
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
594
gnutls_deinit(*session);
597
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
499
598
if(ret != GNUTLS_E_SUCCESS){
500
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
501
safer_gnutls_strerror(ret));
599
fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
600
safer_gnutls_strerror(ret));
502
601
gnutls_deinit(*session);
506
605
/* ignore client certificate if any. */
507
gnutls_certificate_server_set_request(*session,
606
gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
510
608
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
871
1090
case AVAHI_BROWSER_ALL_FOR_NOW:
872
1091
case AVAHI_BROWSER_CACHE_EXHAUSTED:
874
fprintf(stderr, "No Mandos server found, still searching...\n");
1093
fprintf_plus(stderr, "No Mandos server found, still"
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){
1100
/* Signal handler that stops main loop after SIGTERM */
1101
static void handle_sigterm(int sig){
1106
signal_received = sig;
888
1107
int old_errno = errno;
1108
/* set main loop to exit */
889
1109
if(mc.simple_poll != NULL){
890
1110
avahi_simple_poll_quit(mc.simple_poll);
892
1112
errno = old_errno;
1115
bool get_flags(const char *ifname, struct ifreq *ifr){
1118
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1120
perror_plus("socket");
1123
strcpy(ifr->ifr_name, ifname);
1124
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1127
perror_plus("ioctl SIOCGIFFLAGS");
1134
bool good_flags(const char *ifname, const struct ifreq *ifr){
1136
/* Reject the loopback device */
1137
if(ifr->ifr_flags & IFF_LOOPBACK){
1139
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1144
/* Accept point-to-point devices only if connect_to is specified */
1145
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1147
fprintf_plus(stderr, "Accepting point-to-point interface"
1148
" \"%s\"\n", ifname);
1152
/* Otherwise, reject non-broadcast-capable devices */
1153
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1155
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1156
" \"%s\"\n", ifname);
1160
/* Reject non-ARP interfaces (including dummy interfaces) */
1161
if(ifr->ifr_flags & IFF_NOARP){
1163
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1169
/* Accept this device */
1171
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1177
* This function determines if a directory entry in /sys/class/net
1178
* corresponds to an acceptable network device.
1179
* (This function is passed to scandir(3) as a filter function.)
1181
int good_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 "
1190
"\"%s\"\n", if_entry->d_name);
1195
if(not good_flags(if_entry->d_name, &ifr)){
1202
* This function determines if a directory entry in /sys/class/net
1203
* corresponds to an acceptable network device which is up.
1204
* (This function is passed to scandir(3) as a filter function.)
1206
int up_interface(const struct dirent *if_entry){
1207
if(if_entry->d_name[0] == '.'){
1212
if(not get_flags(if_entry->d_name, &ifr)){
1214
fprintf_plus(stderr, "Failed to get flags for interface "
1215
"\"%s\"\n", if_entry->d_name);
1220
/* Reject down interfaces */
1221
if(not (ifr.ifr_flags & IFF_UP)){
1223
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1229
/* Reject non-running interfaces */
1230
if(not (ifr.ifr_flags & IFF_RUNNING)){
1232
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1238
if(not good_flags(if_entry->d_name, &ifr)){
1244
int notdotentries(const struct dirent *direntry){
1245
/* Skip "." and ".." */
1246
if(direntry->d_name[0] == '.'
1247
and (direntry->d_name[1] == '\0'
1248
or (direntry->d_name[1] == '.'
1249
and direntry->d_name[2] == '\0'))){
1255
/* Is this directory entry a runnable program? */
1256
int runnable_hook(const struct dirent *direntry){
1261
if((direntry->d_name)[0] == '\0'){
1266
sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1267
"abcdefghijklmnopqrstuvwxyz"
1270
if((direntry->d_name)[sret] != '\0'){
1271
/* Contains non-allowed characters */
1273
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1279
char *fullname = NULL;
1280
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1282
perror_plus("asprintf");
1286
ret = stat(fullname, &st);
1289
perror_plus("Could not stat hook");
1293
if(not (S_ISREG(st.st_mode))){
1294
/* Not a regular file */
1296
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1301
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1302
/* Not executable */
1304
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1310
fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1316
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1318
struct timespec now;
1319
struct timespec waited_time;
1320
intmax_t block_time;
1323
if(mc.current_server == NULL){
1325
fprintf_plus(stderr, "Wait until first server is found."
1328
ret = avahi_simple_poll_iterate(s, -1);
1331
fprintf_plus(stderr, "Check current_server if we should run"
1334
/* the current time */
1335
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1337
perror_plus("clock_gettime");
1340
/* Calculating in ms how long time between now and server
1341
who we visted longest time ago. Now - last seen. */
1342
waited_time.tv_sec = (now.tv_sec
1343
- mc.current_server->last_seen.tv_sec);
1344
waited_time.tv_nsec = (now.tv_nsec
1345
- mc.current_server->last_seen.tv_nsec);
1346
/* total time is 10s/10,000ms.
1347
Converting to s from ms by dividing by 1,000,
1348
and ns to ms by dividing by 1,000,000. */
1349
block_time = ((retry_interval
1350
- ((intmax_t)waited_time.tv_sec * 1000))
1351
- ((intmax_t)waited_time.tv_nsec / 1000000));
1354
fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1358
if(block_time <= 0){
1359
ret = start_mandos_communication(mc.current_server->ip,
1360
mc.current_server->port,
1361
mc.current_server->if_index,
1362
mc.current_server->af);
1364
avahi_simple_poll_quit(mc.simple_poll);
1367
ret = clock_gettime(CLOCK_MONOTONIC,
1368
&mc.current_server->last_seen);
1370
perror_plus("clock_gettime");
1373
mc.current_server = mc.current_server->next;
1374
block_time = 0; /* Call avahi to find new Mandos
1375
servers, but don't block */
1378
ret = avahi_simple_poll_iterate(s, (int)block_time);
1381
if (ret > 0 or errno != EINTR){
1382
return (ret != 1) ? ret : 0;
1388
bool run_network_hooks(const char *mode, const char *interface,
1390
struct dirent **direntries;
1391
struct dirent *direntry;
1393
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1396
perror_plus("scandir");
1398
int devnull = open("/dev/null", O_RDONLY);
1399
for(int i = 0; i < numhooks; i++){
1400
direntry = direntries[i];
1401
char *fullname = NULL;
1402
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1404
perror_plus("asprintf");
1408
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1411
pid_t hook_pid = fork();
1414
/* Raise privileges */
1418
perror_plus("seteuid");
1420
/* Raise privileges even more */
1424
perror_plus("setuid");
1430
perror_plus("setgid");
1432
/* Reset supplementary groups */
1434
ret = setgroups(0, NULL);
1436
perror_plus("setgroups");
1438
dup2(devnull, STDIN_FILENO);
1440
dup2(STDERR_FILENO, STDOUT_FILENO);
1441
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1443
perror_plus("setenv");
1446
ret = setenv("DEVICE", interface, 1);
1448
perror_plus("setenv");
1451
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1453
perror_plus("setenv");
1456
ret = setenv("MODE", mode, 1);
1458
perror_plus("setenv");
1462
ret = asprintf(&delaystring, "%f", delay);
1464
perror_plus("asprintf");
1467
ret = setenv("DELAY", delaystring, 1);
1470
perror_plus("setenv");
1474
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1475
perror_plus("execl");
1476
_exit(EXIT_FAILURE);
1480
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1481
perror_plus("waitpid");
1485
if(WIFEXITED(status)){
1486
if(WEXITSTATUS(status) != 0){
1487
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1488
" with status %d\n", direntry->d_name,
1489
WEXITSTATUS(status));
1493
} else if(WIFSIGNALED(status)){
1494
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1495
" signal %d\n", direntry->d_name,
1500
fprintf_plus(stderr, "Warning: network hook \"%s\""
1501
" crashed\n", direntry->d_name);
1508
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
895
1517
int main(int argc, char *argv[]){
896
1518
AvahiSServiceBrowser *sb = NULL;
991
1653
delay = strtof(arg, &tmp);
992
1654
if(errno != 0 or tmp == arg or *tmp != '\0'){
993
fprintf(stderr, "Bad delay\n");
1655
argp_error(state, "Bad delay");
1657
case 132: /* --retry */
1659
retry_interval = strtod(arg, &tmp);
1660
if(errno != 0 or tmp == arg or *tmp != '\0'
1661
or (retry_interval * 1000) > INT_MAX
1662
or retry_interval < 0){
1663
argp_error(state, "Bad retry interval");
1666
case 133: /* --network-hook-dir */
1670
* These reproduce what we would get without ARGP_NO_HELP
1672
case '?': /* --help */
1673
argp_state_help(state, state->out_stream,
1674
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1675
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1676
case -3: /* --usage */
1677
argp_state_help(state, state->out_stream,
1678
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1679
case 'V': /* --version */
1680
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1681
exit(argp_err_exit_status);
1002
1684
return ARGP_ERR_UNKNOWN;
1007
1689
struct argp argp = { .options = options, .parser = parse_opt,
1008
1690
.args_doc = "",
1009
1691
.doc = "Mandos client -- Get and decrypt"
1010
1692
" 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;
1693
ret = argp_parse(&argp, argc, argv,
1694
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1701
perror_plus("argp_parse");
1702
exitcode = EX_OSERR;
1705
exitcode = EX_USAGE;
1711
/* Work around Debian bug #633582:
1712
<http://bugs.debian.org/633582> */
1714
/* Re-raise priviliges */
1718
perror_plus("seteuid");
1722
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1723
int seckey_fd = open(seckey, O_RDONLY);
1724
if(seckey_fd == -1){
1725
perror_plus("open");
1727
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1729
perror_plus("fstat");
1731
if(S_ISREG(st.st_mode)
1732
and st.st_uid == 0 and st.st_gid == 0){
1733
ret = fchown(seckey_fd, uid, gid);
1735
perror_plus("fchown");
1739
TEMP_FAILURE_RETRY(close(seckey_fd));
1743
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1744
int pubkey_fd = open(pubkey, O_RDONLY);
1745
if(pubkey_fd == -1){
1746
perror_plus("open");
1748
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1750
perror_plus("fstat");
1752
if(S_ISREG(st.st_mode)
1753
and st.st_uid == 0 and st.st_gid == 0){
1754
ret = fchown(pubkey_fd, uid, gid);
1756
perror_plus("fchown");
1760
TEMP_FAILURE_RETRY(close(pubkey_fd));
1764
/* Lower privileges */
1768
perror_plus("seteuid");
1773
/* Run network hooks */
1774
if(not run_network_hooks("start", interface, delay)){
1020
1779
avahi_set_log_function(empty_log);
1782
if(interface[0] == '\0'){
1783
struct dirent **direntries;
1784
/* First look for interfaces that are up */
1785
ret = scandir(sys_class_net, &direntries, up_interface,
1788
/* No up interfaces, look for any good interfaces */
1790
ret = scandir(sys_class_net, &direntries, good_interface,
1794
/* Pick the first interface returned */
1795
interface = strdup(direntries[0]->d_name);
1797
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1799
if(interface == NULL){
1800
perror_plus("malloc");
1802
exitcode = EXIT_FAILURE;
1808
fprintf_plus(stderr, "Could not find a network interface\n");
1809
exitcode = EXIT_FAILURE;
1023
1814
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1024
1815
from the signal handler */
1025
1816
/* Initialize the pseudo-RNG for Avahi */
1026
1817
srand((unsigned int) time(NULL));
1027
1818
mc.simple_poll = avahi_simple_poll_new();
1028
1819
if(mc.simple_poll == NULL){
1029
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1030
exitcode = EXIT_FAILURE;
1820
fprintf_plus(stderr,
1821
"Avahi: Failed to create simple poll object.\n");
1822
exitcode = EX_UNAVAILABLE;
1034
1826
sigemptyset(&sigterm_action.sa_mask);
1035
1827
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1037
perror("sigaddset");
1038
exitcode = EXIT_FAILURE;
1829
perror_plus("sigaddset");
1830
exitcode = EX_OSERR;
1041
1833
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1043
perror("sigaddset");
1044
exitcode = EXIT_FAILURE;
1835
perror_plus("sigaddset");
1836
exitcode = EX_OSERR;
1047
1839
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;
1841
perror_plus("sigaddset");
1842
exitcode = EX_OSERR;
1845
/* Need to check if the handler is SIG_IGN before handling:
1846
| [[info:libc:Initial Signal Actions]] |
1847
| [[info:libc:Basic Signal Handling]] |
1849
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1851
perror_plus("sigaction");
1854
if(old_sigterm_action.sa_handler != SIG_IGN){
1855
ret = sigaction(SIGINT, &sigterm_action, NULL);
1857
perror_plus("sigaction");
1858
exitcode = EX_OSERR;
1862
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1864
perror_plus("sigaction");
1867
if(old_sigterm_action.sa_handler != SIG_IGN){
1868
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1870
perror_plus("sigaction");
1871
exitcode = EX_OSERR;
1875
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1877
perror_plus("sigaction");
1880
if(old_sigterm_action.sa_handler != SIG_IGN){
1881
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1883
perror_plus("sigaction");
1884
exitcode = EX_OSERR;
1060
1889
/* If the interface is down, bring it up */
1061
if(interface[0] != '\0'){
1890
if(strcmp(interface, "none") != 0){
1891
if_index = (AvahiIfIndex) if_nametoindex(interface);
1893
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1894
exitcode = EX_UNAVAILABLE;
1902
/* Re-raise priviliges */
1906
perror_plus("seteuid");
1062
1909
#ifdef __linux__
1063
1910
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1064
messages to mess up the prompt */
1911
messages about the network interface to mess up the prompt */
1065
1912
ret = klogctl(8, NULL, 5);
1066
1913
bool restore_loglevel = true;
1068
1915
restore_loglevel = false;
1916
perror_plus("klogctl");
1071
1918
#endif /* __linux__ */
1073
1920
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1076
exitcode = EXIT_FAILURE;
1922
perror_plus("socket");
1923
exitcode = EX_OSERR;
1077
1924
#ifdef __linux__
1078
1925
if(restore_loglevel){
1079
1926
ret = klogctl(7, NULL, 0);
1928
perror_plus("klogctl");
1084
1931
#endif /* __linux__ */
1932
/* Lower privileges */
1936
perror_plus("seteuid");
1087
1940
strcpy(network.ifr_name, interface);
1088
1941
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1090
perror("ioctl SIOCGIFFLAGS");
1943
perror_plus("ioctl SIOCGIFFLAGS");
1091
1944
#ifdef __linux__
1092
1945
if(restore_loglevel){
1093
1946
ret = klogctl(7, NULL, 0);
1948
perror_plus("klogctl");
1098
1951
#endif /* __linux__ */
1099
exitcode = EXIT_FAILURE;
1952
exitcode = EX_OSERR;
1953
/* Lower privileges */
1957
perror_plus("seteuid");
1102
1961
if((network.ifr_flags & IFF_UP) == 0){
1103
1962
network.ifr_flags |= IFF_UP;
1963
take_down_interface = true;
1104
1964
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1106
perror("ioctl SIOCSIFFLAGS");
1107
exitcode = EXIT_FAILURE;
1966
take_down_interface = false;
1967
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1968
exitcode = EX_OSERR;
1108
1969
#ifdef __linux__
1109
1970
if(restore_loglevel){
1110
1971
ret = klogctl(7, NULL, 0);
1973
perror_plus("klogctl");
1115
1976
#endif /* __linux__ */
1977
/* Lower privileges */
1981
perror_plus("seteuid");
1119
/* sleep checking until interface is running */
1986
/* Sleep checking until interface is running.
1987
Check every 0.25s, up to total time of delay */
1120
1988
for(int i=0; i < delay * 4; i++){
1121
1989
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1123
perror("ioctl SIOCGIFFLAGS");
1991
perror_plus("ioctl SIOCGIFFLAGS");
1124
1992
} else if(network.ifr_flags & IFF_RUNNING){
1127
1995
struct timespec sleeptime = { .tv_nsec = 250000000 };
1128
1996
ret = nanosleep(&sleeptime, NULL);
1129
1997
if(ret == -1 and errno != EINTR){
1130
perror("nanosleep");
1998
perror_plus("nanosleep");
1133
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2001
if(not take_down_interface){
2002
/* We won't need the socket anymore */
2003
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2005
perror_plus("close");
1137
2008
#ifdef __linux__
1138
2009
if(restore_loglevel){
1139
2010
/* Restores kernel loglevel to default */
1140
2011
ret = klogctl(7, NULL, 0);
2013
perror_plus("klogctl");
1145
2016
#endif /* __linux__ */
2017
/* Lower privileges */
2019
/* Lower privileges */
2022
perror_plus("seteuid");
1162
2030
ret = init_gnutls_global(pubkey, seckey);
1164
fprintf(stderr, "init_gnutls_global failed\n");
1165
exitcode = EXIT_FAILURE;
2032
fprintf_plus(stderr, "init_gnutls_global failed\n");
2033
exitcode = EX_UNAVAILABLE;
1168
2036
gnutls_initialized = true;
1171
2043
if(mkdtemp(tempdir) == NULL){
2044
perror_plus("mkdtemp");
1175
2047
tempdir_created = true;
1177
2053
if(not init_gpgme(pubkey, seckey, tempdir)){
1178
fprintf(stderr, "init_gpgme failed\n");
1179
exitcode = EXIT_FAILURE;
2054
fprintf_plus(stderr, "init_gpgme failed\n");
2055
exitcode = EX_UNAVAILABLE;
1182
2058
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
2065
if(connect_to != NULL){
1299
2218
gpgme_release(mc.ctx);
1302
/* Removes the temp directory used by GPGME */
2221
/* Cleans up the circular linked list of Mandos servers the client
2223
if(mc.current_server != NULL){
2224
mc.current_server->prev->next = NULL;
2225
while(mc.current_server != NULL){
2226
server *next = mc.current_server->next;
2227
free(mc.current_server);
2228
mc.current_server = next;
2232
/* Run network hooks */
2233
run_network_hooks("stop", interface, delay);
2235
/* Re-raise priviliges */
2240
perror_plus("seteuid");
2243
/* Take down the network interface */
2244
if(take_down_interface and geteuid() == 0){
2245
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2247
perror_plus("ioctl SIOCGIFFLAGS");
2248
} else if(network.ifr_flags & IFF_UP){
2249
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2250
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2252
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2255
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2257
perror_plus("close");
2261
/* Lower privileges permanently */
2265
perror_plus("setuid");
2268
/* Removes the GPGME temp directory and all files inside */
1303
2269
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'))){
2270
struct dirent **direntries = NULL;
2271
struct dirent *direntry = NULL;
2272
int numentries = scandir(tempdir, &direntries, notdotentries,
2274
if (numentries > 0){
2275
for(int i = 0; i < numentries; i++){
2276
direntry = direntries[i];
1324
2277
char *fullname = NULL;
1325
2278
ret = asprintf(&fullname, "%s/%s", tempdir,
1326
2279
direntry->d_name);
2281
perror_plus("asprintf");
1331
2284
ret = remove(fullname);
1333
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2286
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
1336
2289
free(fullname);
2293
/* need to clean even if 0 because man page doesn't specify */
2295
if (numentries == -1){
2296
perror_plus("scandir");
1340
2298
ret = rmdir(tempdir);
1341
2299
if(ret == -1 and errno != ENOENT){
2300
perror_plus("rmdir");
2305
sigemptyset(&old_sigterm_action.sa_mask);
2306
old_sigterm_action.sa_handler = SIG_DFL;
2307
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2308
&old_sigterm_action,
2311
perror_plus("sigaction");
2314
ret = raise(signal_received);
2315
} while(ret != 0 and errno == EINTR);
2317
perror_plus("raise");
2320
TEMP_FAILURE_RETRY(pause());
1346
2323
return exitcode;