256
173
* Helper function to insert pub and seckey to the engine keyring.
258
175
bool import_key(const char *filename){
261
177
gpgme_data_t pgp_data;
263
179
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
269
185
rc = gpgme_data_new_from_fd(&pgp_data, fd);
270
186
if(rc != GPG_ERR_NO_ERROR){
271
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
272
gpgme_strsource(rc), gpgme_strerror(rc));
187
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
188
gpgme_strsource(rc), gpgme_strerror(rc));
276
192
rc = gpgme_op_import(mc.ctx, pgp_data);
277
193
if(rc != GPG_ERR_NO_ERROR){
278
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
279
gpgme_strsource(rc), gpgme_strerror(rc));
194
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
195
gpgme_strsource(rc), gpgme_strerror(rc));
283
199
ret = (int)TEMP_FAILURE_RETRY(close(fd));
285
perror_plus("close");
287
203
gpgme_data_release(pgp_data);
292
fprintf_plus(stderr, "Initializing GPGME\n");
208
fprintf(stderr, "Initializing GPGME\n");
296
212
gpgme_check_version(NULL);
297
213
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
298
214
if(rc != GPG_ERR_NO_ERROR){
299
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
300
gpgme_strsource(rc), gpgme_strerror(rc));
215
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
216
gpgme_strsource(rc), gpgme_strerror(rc));
304
/* Set GPGME home directory for the OpenPGP engine only */
220
/* Set GPGME home directory for the OpenPGP engine only */
305
221
rc = gpgme_get_engine_info(&engine_info);
306
222
if(rc != GPG_ERR_NO_ERROR){
307
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
308
gpgme_strsource(rc), gpgme_strerror(rc));
223
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
224
gpgme_strsource(rc), gpgme_strerror(rc));
311
227
while(engine_info != NULL){
379
292
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
380
293
if(rc != GPG_ERR_NO_ERROR){
381
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
382
gpgme_strsource(rc), gpgme_strerror(rc));
294
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
295
gpgme_strsource(rc), gpgme_strerror(rc));
383
296
plaintext_length = -1;
385
298
gpgme_decrypt_result_t result;
386
299
result = gpgme_op_decrypt_result(mc.ctx);
387
300
if(result == NULL){
388
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
301
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
390
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
391
result->unsupported_algorithm);
392
fprintf_plus(stderr, "Wrong key usage: %u\n",
393
result->wrong_key_usage);
303
fprintf(stderr, "Unsupported algorithm: %s\n",
304
result->unsupported_algorithm);
305
fprintf(stderr, "Wrong key usage: %u\n",
306
result->wrong_key_usage);
394
307
if(result->file_name != NULL){
395
fprintf_plus(stderr, "File name: %s\n", result->file_name);
308
fprintf(stderr, "File name: %s\n", result->file_name);
397
310
gpgme_recipient_t recipient;
398
311
recipient = result->recipients;
399
while(recipient != NULL){
400
fprintf_plus(stderr, "Public key algorithm: %s\n",
401
gpgme_pubkey_algo_name
402
(recipient->pubkey_algo));
403
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
404
fprintf_plus(stderr, "Secret key available: %s\n",
405
recipient->status == GPG_ERR_NO_SECKEY
407
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;
506
420
/* OpenPGP credentials */
507
ret = gnutls_certificate_allocate_credentials(&mc.cred);
421
gnutls_certificate_allocate_credentials(&mc.cred);
508
422
if(ret != GNUTLS_E_SUCCESS){
509
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
510
safer_gnutls_strerror(ret));
423
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
427
safer_gnutls_strerror(ret));
511
428
gnutls_global_deinit();
516
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
517
" secret key %s as GnuTLS credentials\n",
433
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
434
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
522
438
ret = gnutls_certificate_set_openpgp_key_file
523
439
(mc.cred, pubkeyfilename, seckeyfilename,
524
440
GNUTLS_OPENPGP_FMT_BASE64);
525
441
if(ret != GNUTLS_E_SUCCESS){
527
"Error[%d] while reading the OpenPGP key pair ('%s',"
528
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
529
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
530
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));
534
450
/* GnuTLS server initialization */
535
451
ret = gnutls_dh_params_init(&mc.dh_params);
536
452
if(ret != GNUTLS_E_SUCCESS){
537
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
538
" initialization: %s\n",
539
safer_gnutls_strerror(ret));
453
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
454
" %s\n", safer_gnutls_strerror(ret));
542
457
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
543
458
if(ret != GNUTLS_E_SUCCESS){
544
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
545
safer_gnutls_strerror(ret));
459
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
460
safer_gnutls_strerror(ret));
1097
871
case AVAHI_BROWSER_ALL_FOR_NOW:
1098
872
case AVAHI_BROWSER_CACHE_EXHAUSTED:
1100
fprintf_plus(stderr, "No Mandos server found, still"
874
fprintf(stderr, "No Mandos server found, still searching...\n");
1107
/* Signal handler that stops main loop after SIGTERM */
1108
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){
1113
signal_received = sig;
1114
888
int old_errno = errno;
1115
/* set main loop to exit */
1116
889
if(mc.simple_poll != NULL){
1117
890
avahi_simple_poll_quit(mc.simple_poll);
1119
892
errno = old_errno;
1122
bool get_flags(const char *ifname, struct ifreq *ifr){
1125
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1127
perror_plus("socket");
1130
strcpy(ifr->ifr_name, ifname);
1131
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1134
perror_plus("ioctl SIOCGIFFLAGS");
1141
bool good_flags(const char *ifname, const struct ifreq *ifr){
1143
/* Reject the loopback device */
1144
if(ifr->ifr_flags & IFF_LOOPBACK){
1146
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1151
/* Accept point-to-point devices only if connect_to is specified */
1152
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1154
fprintf_plus(stderr, "Accepting point-to-point interface"
1155
" \"%s\"\n", ifname);
1159
/* Otherwise, reject non-broadcast-capable devices */
1160
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1162
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1163
" \"%s\"\n", ifname);
1167
/* Reject non-ARP interfaces (including dummy interfaces) */
1168
if(ifr->ifr_flags & IFF_NOARP){
1170
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1176
/* Accept this device */
1178
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1184
* This function determines if a directory entry in /sys/class/net
1185
* corresponds to an acceptable network device.
1186
* (This function is passed to scandir(3) as a filter function.)
1188
int good_interface(const struct dirent *if_entry){
1189
if(if_entry->d_name[0] == '.'){
1194
if(not get_flags(if_entry->d_name, &ifr)){
1196
fprintf_plus(stderr, "Failed to get flags for interface "
1197
"\"%s\"\n", if_entry->d_name);
1202
if(not good_flags(if_entry->d_name, &ifr)){
1209
* This function determines if a directory entry in /sys/class/net
1210
* corresponds to an acceptable network device which is up.
1211
* (This function is passed to scandir(3) as a filter function.)
1213
int up_interface(const struct dirent *if_entry){
1214
if(if_entry->d_name[0] == '.'){
1219
if(not get_flags(if_entry->d_name, &ifr)){
1221
fprintf_plus(stderr, "Failed to get flags for interface "
1222
"\"%s\"\n", if_entry->d_name);
1227
/* Reject down interfaces */
1228
if(not (ifr.ifr_flags & IFF_UP)){
1230
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1236
/* Reject non-running interfaces */
1237
if(not (ifr.ifr_flags & IFF_RUNNING)){
1239
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1245
if(not good_flags(if_entry->d_name, &ifr)){
1251
int notdotentries(const struct dirent *direntry){
1252
/* Skip "." and ".." */
1253
if(direntry->d_name[0] == '.'
1254
and (direntry->d_name[1] == '\0'
1255
or (direntry->d_name[1] == '.'
1256
and direntry->d_name[2] == '\0'))){
1262
/* Is this directory entry a runnable program? */
1263
int runnable_hook(const struct dirent *direntry){
1268
if((direntry->d_name)[0] == '\0'){
1273
sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1274
"abcdefghijklmnopqrstuvwxyz"
1277
if((direntry->d_name)[sret] != '\0'){
1278
/* Contains non-allowed characters */
1280
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1286
char *fullname = NULL;
1287
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1289
perror_plus("asprintf");
1293
ret = stat(fullname, &st);
1296
perror_plus("Could not stat hook");
1300
if(not (S_ISREG(st.st_mode))){
1301
/* Not a regular file */
1303
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1308
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1309
/* Not executable */
1311
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1317
fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1323
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1325
struct timespec now;
1326
struct timespec waited_time;
1327
intmax_t block_time;
1330
if(mc.current_server == NULL){
1332
fprintf_plus(stderr, "Wait until first server is found."
1335
ret = avahi_simple_poll_iterate(s, -1);
1338
fprintf_plus(stderr, "Check current_server if we should run"
1341
/* the current time */
1342
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1344
perror_plus("clock_gettime");
1347
/* Calculating in ms how long time between now and server
1348
who we visted longest time ago. Now - last seen. */
1349
waited_time.tv_sec = (now.tv_sec
1350
- mc.current_server->last_seen.tv_sec);
1351
waited_time.tv_nsec = (now.tv_nsec
1352
- mc.current_server->last_seen.tv_nsec);
1353
/* total time is 10s/10,000ms.
1354
Converting to s from ms by dividing by 1,000,
1355
and ns to ms by dividing by 1,000,000. */
1356
block_time = ((retry_interval
1357
- ((intmax_t)waited_time.tv_sec * 1000))
1358
- ((intmax_t)waited_time.tv_nsec / 1000000));
1361
fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1365
if(block_time <= 0){
1366
ret = start_mandos_communication(mc.current_server->ip,
1367
mc.current_server->port,
1368
mc.current_server->if_index,
1369
mc.current_server->af);
1371
avahi_simple_poll_quit(mc.simple_poll);
1374
ret = clock_gettime(CLOCK_MONOTONIC,
1375
&mc.current_server->last_seen);
1377
perror_plus("clock_gettime");
1380
mc.current_server = mc.current_server->next;
1381
block_time = 0; /* Call avahi to find new Mandos
1382
servers, but don't block */
1385
ret = avahi_simple_poll_iterate(s, (int)block_time);
1388
if (ret > 0 or errno != EINTR){
1389
return (ret != 1) ? ret : 0;
1395
/* Set effective uid to 0, return errno */
1396
int raise_privileges(void){
1397
int old_errno = errno;
1400
if(seteuid(0) == -1){
1401
perror_plus("seteuid");
1408
/* Set effective and real user ID to 0. Return errno. */
1409
int raise_privileges_permanently(void){
1410
int old_errno = errno;
1411
int ret_errno = raise_privileges();
1417
if(setuid(0) == -1){
1418
perror_plus("seteuid");
1425
/* Set effective user ID to unprivileged saved user ID */
1426
int lower_privileges(void){
1427
int old_errno = errno;
1430
if(seteuid(uid) == -1){
1431
perror_plus("seteuid");
1438
bool run_network_hooks(const char *mode, const char *interface,
1440
struct dirent **direntries;
1441
struct dirent *direntry;
1443
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1446
perror_plus("scandir");
1448
int devnull = open("/dev/null", O_RDONLY);
1449
for(int i = 0; i < numhooks; i++){
1450
direntry = direntries[i];
1451
char *fullname = NULL;
1452
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1454
perror_plus("asprintf");
1458
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1461
pid_t hook_pid = fork();
1464
/* Raise privileges */
1465
raise_privileges_permanently();
1470
perror_plus("setgid");
1472
/* Reset supplementary groups */
1474
ret = setgroups(0, NULL);
1476
perror_plus("setgroups");
1478
dup2(devnull, STDIN_FILENO);
1480
dup2(STDERR_FILENO, STDOUT_FILENO);
1481
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1483
perror_plus("setenv");
1486
ret = setenv("DEVICE", interface, 1);
1488
perror_plus("setenv");
1491
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1493
perror_plus("setenv");
1496
ret = setenv("MODE", mode, 1);
1498
perror_plus("setenv");
1502
ret = asprintf(&delaystring, "%f", delay);
1504
perror_plus("asprintf");
1507
ret = setenv("DELAY", delaystring, 1);
1510
perror_plus("setenv");
1514
if(connect_to != NULL){
1515
ret = setenv("CONNECT", connect_to, 1);
1517
perror_plus("setenv");
1521
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1522
perror_plus("execl");
1523
_exit(EXIT_FAILURE);
1527
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1528
perror_plus("waitpid");
1532
if(WIFEXITED(status)){
1533
if(WEXITSTATUS(status) != 0){
1534
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1535
" with status %d\n", direntry->d_name,
1536
WEXITSTATUS(status));
1540
} else if(WIFSIGNALED(status)){
1541
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1542
" signal %d\n", direntry->d_name,
1547
fprintf_plus(stderr, "Warning: network hook \"%s\""
1548
" crashed\n", direntry->d_name);
1555
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1564
int bring_up_interface(const char * const interface, const float delay){
1566
int old_errno = errno;
1569
struct ifreq network;
1570
AvahiIfIndex if_index = (AvahiIfIndex)if_nametoindex(interface);
1572
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1582
/* Re-raise priviliges */
1586
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1587
messages about the network interface to mess up the prompt */
1588
ret = klogctl(8, NULL, 5);
1589
bool restore_loglevel = true;
1591
restore_loglevel = false;
1592
perror_plus("klogctl");
1594
#endif /* __linux__ */
1596
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1599
perror_plus("socket");
1601
if(restore_loglevel){
1602
ret = klogctl(7, NULL, 0);
1604
perror_plus("klogctl");
1607
#endif /* __linux__ */
1608
/* Lower privileges */
1613
strcpy(network.ifr_name, interface);
1614
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1617
perror_plus("ioctl SIOCGIFFLAGS");
1619
if(restore_loglevel){
1620
ret = klogctl(7, NULL, 0);
1622
perror_plus("klogctl");
1625
#endif /* __linux__ */
1626
/* Lower privileges */
1631
if((network.ifr_flags & IFF_UP) == 0){
1632
network.ifr_flags |= IFF_UP;
1633
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1636
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1638
if(restore_loglevel){
1639
ret = klogctl(7, NULL, 0);
1641
perror_plus("klogctl");
1644
#endif /* __linux__ */
1645
/* Lower privileges */
1651
/* Sleep checking until interface is running.
1652
Check every 0.25s, up to total time of delay */
1653
for(int i=0; i < delay * 4; i++){
1654
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1656
perror_plus("ioctl SIOCGIFFLAGS");
1657
} else if(network.ifr_flags & IFF_RUNNING){
1660
struct timespec sleeptime = { .tv_nsec = 250000000 };
1661
ret = nanosleep(&sleeptime, NULL);
1662
if(ret == -1 and errno != EINTR){
1663
perror_plus("nanosleep");
1666
/* Close the socket */
1667
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1669
perror_plus("close");
1672
if(restore_loglevel){
1673
/* Restores kernel loglevel to default */
1674
ret = klogctl(7, NULL, 0);
1676
perror_plus("klogctl");
1679
#endif /* __linux__ */
1680
/* Lower privileges */
1686
895
int main(int argc, char *argv[]){
1687
896
AvahiSServiceBrowser *sb = NULL;
1820
991
delay = strtof(arg, &tmp);
1821
992
if(errno != 0 or tmp == arg or *tmp != '\0'){
1822
argp_error(state, "Bad delay");
1824
case 132: /* --retry */
1826
retry_interval = strtod(arg, &tmp);
1827
if(errno != 0 or tmp == arg or *tmp != '\0'
1828
or (retry_interval * 1000) > INT_MAX
1829
or retry_interval < 0){
1830
argp_error(state, "Bad retry interval");
1833
case 133: /* --network-hook-dir */
1837
* These reproduce what we would get without ARGP_NO_HELP
1839
case '?': /* --help */
1840
argp_state_help(state, state->out_stream,
1841
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1842
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1843
case -3: /* --usage */
1844
argp_state_help(state, state->out_stream,
1845
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1846
case 'V': /* --version */
1847
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1848
exit(argp_err_exit_status);
993
fprintf(stderr, "Bad delay\n");
1851
1002
return ARGP_ERR_UNKNOWN;
1856
1007
struct argp argp = { .options = options, .parser = parse_opt,
1857
1008
.args_doc = "",
1858
1009
.doc = "Mandos client -- Get and decrypt"
1859
1010
" passwords from a Mandos server" };
1860
ret = argp_parse(&argp, argc, argv,
1861
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1868
perror_plus("argp_parse");
1869
exitcode = EX_OSERR;
1872
exitcode = EX_USAGE;
1878
/* Work around Debian bug #633582:
1879
<http://bugs.debian.org/633582> */
1881
/* Re-raise priviliges */
1882
if(raise_privileges() == 0){
1885
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1886
int seckey_fd = open(seckey, O_RDONLY);
1887
if(seckey_fd == -1){
1888
perror_plus("open");
1890
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1892
perror_plus("fstat");
1894
if(S_ISREG(st.st_mode)
1895
and st.st_uid == 0 and st.st_gid == 0){
1896
ret = fchown(seckey_fd, uid, gid);
1898
perror_plus("fchown");
1902
TEMP_FAILURE_RETRY(close(seckey_fd));
1906
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1907
int pubkey_fd = open(pubkey, O_RDONLY);
1908
if(pubkey_fd == -1){
1909
perror_plus("open");
1911
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1913
perror_plus("fstat");
1915
if(S_ISREG(st.st_mode)
1916
and st.st_uid == 0 and st.st_gid == 0){
1917
ret = fchown(pubkey_fd, uid, gid);
1919
perror_plus("fchown");
1923
TEMP_FAILURE_RETRY(close(pubkey_fd));
1927
/* Lower privileges */
1931
perror_plus("seteuid");
1936
/* Run network hooks */
1937
if(not run_network_hooks("start", interface, delay)){
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;
1942
1020
avahi_set_log_function(empty_log);
1945
if(interface[0] == '\0'){
1946
struct dirent **direntries;
1947
/* First look for interfaces that are up */
1948
ret = scandir(sys_class_net, &direntries, up_interface,
1951
/* No up interfaces, look for any good interfaces */
1953
ret = scandir(sys_class_net, &direntries, good_interface,
1957
/* Pick the first interface returned */
1958
interface = strdup(direntries[0]->d_name);
1960
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1962
if(interface == NULL){
1963
perror_plus("malloc");
1965
exitcode = EXIT_FAILURE;
1971
fprintf_plus(stderr, "Could not find a network interface\n");
1972
exitcode = EXIT_FAILURE;
1977
1023
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1978
1024
from the signal handler */
1979
1025
/* Initialize the pseudo-RNG for Avahi */
1980
1026
srand((unsigned int) time(NULL));
1981
1027
mc.simple_poll = avahi_simple_poll_new();
1982
1028
if(mc.simple_poll == NULL){
1983
fprintf_plus(stderr,
1984
"Avahi: Failed to create simple poll object.\n");
1985
exitcode = EX_UNAVAILABLE;
1029
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1030
exitcode = EXIT_FAILURE;
1989
1034
sigemptyset(&sigterm_action.sa_mask);
1990
1035
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1992
perror_plus("sigaddset");
1993
exitcode = EX_OSERR;
1037
perror("sigaddset");
1038
exitcode = EXIT_FAILURE;
1996
1041
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1998
perror_plus("sigaddset");
1999
exitcode = EX_OSERR;
1043
perror("sigaddset");
1044
exitcode = EXIT_FAILURE;
2002
1047
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
2004
perror_plus("sigaddset");
2005
exitcode = EX_OSERR;
2008
/* Need to check if the handler is SIG_IGN before handling:
2009
| [[info:libc:Initial Signal Actions]] |
2010
| [[info:libc:Basic Signal Handling]] |
2012
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
2014
perror_plus("sigaction");
2017
if(old_sigterm_action.sa_handler != SIG_IGN){
2018
ret = sigaction(SIGINT, &sigterm_action, NULL);
2020
perror_plus("sigaction");
2021
exitcode = EX_OSERR;
2025
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
2027
perror_plus("sigaction");
2030
if(old_sigterm_action.sa_handler != SIG_IGN){
2031
ret = sigaction(SIGHUP, &sigterm_action, NULL);
2033
perror_plus("sigaction");
2034
exitcode = EX_OSERR;
2038
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
2040
perror_plus("sigaction");
2043
if(old_sigterm_action.sa_handler != SIG_IGN){
2044
ret = sigaction(SIGTERM, &sigterm_action, NULL);
2046
perror_plus("sigaction");
2047
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;
2052
1060
/* If the interface is down, bring it up */
2053
if((interface[0] != '\0') and (strcmp(interface, "none") != 0)){
2054
ret = bring_up_interface(interface, delay);
2057
perror_plus("Failed to bring up interface");
1061
if(interface[0] != '\0'){
1063
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1064
messages to mess up the prompt */
1065
ret = klogctl(8, NULL, 5);
1066
bool restore_loglevel = true;
1068
restore_loglevel = false;
1071
#endif /* __linux__ */
1073
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1076
exitcode = EXIT_FAILURE;
1078
if(restore_loglevel){
1079
ret = klogctl(7, NULL, 0);
1084
#endif /* __linux__ */
1087
strcpy(network.ifr_name, interface);
1088
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1090
perror("ioctl SIOCGIFFLAGS");
1092
if(restore_loglevel){
1093
ret = klogctl(7, NULL, 0);
1098
#endif /* __linux__ */
1099
exitcode = EXIT_FAILURE;
1102
if((network.ifr_flags & IFF_UP) == 0){
1103
network.ifr_flags |= IFF_UP;
1104
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1106
perror("ioctl SIOCSIFFLAGS");
1107
exitcode = EXIT_FAILURE;
1109
if(restore_loglevel){
1110
ret = klogctl(7, NULL, 0);
1115
#endif /* __linux__ */
1119
/* sleep checking until interface is running */
1120
for(int i=0; i < delay * 4; i++){
1121
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1123
perror("ioctl SIOCGIFFLAGS");
1124
} else if(network.ifr_flags & IFF_RUNNING){
1127
struct timespec sleeptime = { .tv_nsec = 250000000 };
1128
ret = nanosleep(&sleeptime, NULL);
1129
if(ret == -1 and errno != EINTR){
1130
perror("nanosleep");
1133
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1138
if(restore_loglevel){
1139
/* Restores kernel loglevel to default */
1140
ret = klogctl(7, NULL, 0);
1145
#endif /* __linux__ */
2065
1162
ret = init_gnutls_global(pubkey, seckey);
2067
fprintf_plus(stderr, "init_gnutls_global failed\n");
2068
exitcode = EX_UNAVAILABLE;
1164
fprintf(stderr, "init_gnutls_global failed\n");
1165
exitcode = EXIT_FAILURE;
2071
1168
gnutls_initialized = true;
2078
1171
if(mkdtemp(tempdir) == NULL){
2079
perror_plus("mkdtemp");
2082
1175
tempdir_created = true;
2088
1177
if(not init_gpgme(pubkey, seckey, tempdir)){
2089
fprintf_plus(stderr, "init_gpgme failed\n");
2090
exitcode = EX_UNAVAILABLE;
1178
fprintf(stderr, "init_gpgme failed\n");
1179
exitcode = EXIT_FAILURE;
2093
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;
2100
1194
if(connect_to != NULL){
2101
1195
/* Connect directly, do not use Zeroconf */
2102
1196
/* (Mainly meant for debugging) */
2103
1197
char *address = strrchr(connect_to, ':');
2105
1198
if(address == NULL){
2106
fprintf_plus(stderr, "No colon in address\n");
2107
exitcode = EX_USAGE;
1199
fprintf(stderr, "No colon in address\n");
1200
exitcode = EXIT_FAILURE;
2117
1205
tmpmax = strtoimax(address+1, &tmp, 10);
2118
1206
if(errno != 0 or tmp == address+1 or *tmp != '\0'
2119
1207
or tmpmax != (uint16_t)tmpmax){
2120
fprintf_plus(stderr, "Bad port number\n");
2121
exitcode = EX_USAGE;
1208
fprintf(stderr, "Bad port number\n");
1209
exitcode = EXIT_FAILURE;
2129
1212
port = (uint16_t)tmpmax;
2130
1213
*address = '\0';
1214
address = connect_to;
2131
1215
/* Colon in address indicates IPv6 */
2133
if(strchr(connect_to, ':') != NULL){
1217
if(strchr(address, ':') != NULL){
2135
/* Accept [] around IPv6 address - see RFC 5952 */
2136
if(connect_to[0] == '[' and address[-1] == ']')
2144
address = connect_to;
2150
while(not quit_now){
2151
ret = start_mandos_communication(address, port, if_index, af);
2152
if(quit_now or ret == 0){
2156
fprintf_plus(stderr, "Retrying in %d seconds\n",
2157
(int)retry_interval);
2159
sleep((int)retry_interval);
1222
ret = start_mandos_communication(address, port, if_index, af);
1224
exitcode = EXIT_FAILURE;
2163
1226
exitcode = EXIT_SUCCESS;
2174
1232
AvahiServerConfig config;
2175
1233
/* Do not publish any local Zeroconf records */
2254
1299
gpgme_release(mc.ctx);
2257
/* Cleans up the circular linked list of Mandos servers the client
2259
if(mc.current_server != NULL){
2260
mc.current_server->prev->next = NULL;
2261
while(mc.current_server != NULL){
2262
server *next = mc.current_server->next;
2263
free(mc.current_server);
2264
mc.current_server = next;
2268
/* Run network hooks */
2269
run_network_hooks("stop", interface, delay);
2271
/* Re-raise priviliges */
2275
/* Take down the network interface */
2276
if(take_down_interface and geteuid() == 0){
2277
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2279
perror_plus("ioctl SIOCGIFFLAGS");
2280
} else if(network.ifr_flags & IFF_UP){
2281
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2282
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2284
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2287
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2289
perror_plus("close");
2293
/* Lower privileges permanently */
2297
perror_plus("setuid");
2300
/* Removes the GPGME temp directory and all files inside */
1302
/* Removes the temp directory used by GPGME */
2301
1303
if(tempdir_created){
2302
struct dirent **direntries = NULL;
2303
struct dirent *direntry = NULL;
2304
int numentries = scandir(tempdir, &direntries, notdotentries,
2306
if (numentries > 0){
2307
for(int i = 0; i < numentries; i++){
2308
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'))){
2309
1324
char *fullname = NULL;
2310
1325
ret = asprintf(&fullname, "%s/%s", tempdir,
2311
1326
direntry->d_name);
2313
perror_plus("asprintf");
2316
1331
ret = remove(fullname);
2318
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
1333
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2321
1336
free(fullname);
2325
/* need to clean even if 0 because man page doesn't specify */
2327
if (numentries == -1){
2328
perror_plus("scandir");
2330
1340
ret = rmdir(tempdir);
2331
1341
if(ret == -1 and errno != ENOENT){
2332
perror_plus("rmdir");
2337
sigemptyset(&old_sigterm_action.sa_mask);
2338
old_sigterm_action.sa_handler = SIG_DFL;
2339
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2340
&old_sigterm_action,
2343
perror_plus("sigaction");
2346
ret = raise(signal_received);
2347
} while(ret != 0 and errno == EINTR);
2349
perror_plus("raise");
2352
TEMP_FAILURE_RETRY(pause());
2355
1346
return exitcode;