179
263
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
185
269
rc = gpgme_data_new_from_fd(&pgp_data, fd);
186
270
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));
271
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
272
gpgme_strsource(rc), gpgme_strerror(rc));
192
276
rc = gpgme_op_import(mc.ctx, pgp_data);
193
277
if(rc != GPG_ERR_NO_ERROR){
194
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
195
gpgme_strsource(rc), gpgme_strerror(rc));
278
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
279
gpgme_strsource(rc), gpgme_strerror(rc));
199
283
ret = (int)TEMP_FAILURE_RETRY(close(fd));
285
perror_plus("close");
203
287
gpgme_data_release(pgp_data);
208
fprintf(stderr, "Initializing GPGME\n");
292
fprintf_plus(stderr, "Initializing GPGME\n");
212
296
gpgme_check_version(NULL);
213
297
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
214
298
if(rc != GPG_ERR_NO_ERROR){
215
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
216
gpgme_strsource(rc), gpgme_strerror(rc));
299
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
300
gpgme_strsource(rc), gpgme_strerror(rc));
220
/* Set GPGME home directory for the OpenPGP engine only */
304
/* Set GPGME home directory for the OpenPGP engine only */
221
305
rc = gpgme_get_engine_info(&engine_info);
222
306
if(rc != GPG_ERR_NO_ERROR){
223
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
224
gpgme_strsource(rc), gpgme_strerror(rc));
307
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
308
gpgme_strsource(rc), gpgme_strerror(rc));
227
311
while(engine_info != NULL){
292
379
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
293
380
if(rc != GPG_ERR_NO_ERROR){
294
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
295
gpgme_strsource(rc), gpgme_strerror(rc));
381
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
382
gpgme_strsource(rc), gpgme_strerror(rc));
296
383
plaintext_length = -1;
298
385
gpgme_decrypt_result_t result;
299
386
result = gpgme_op_decrypt_result(mc.ctx);
300
387
if(result == NULL){
301
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
388
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);
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);
307
394
if(result->file_name != NULL){
308
fprintf(stderr, "File name: %s\n", result->file_name);
395
fprintf_plus(stderr, "File name: %s\n", result->file_name);
310
397
gpgme_recipient_t recipient;
311
398
recipient = result->recipients;
312
399
while(recipient != NULL){
313
fprintf(stderr, "Public key algorithm: %s\n",
314
gpgme_pubkey_algo_name(recipient->pubkey_algo));
315
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
316
fprintf(stderr, "Secret key available: %s\n",
317
recipient->status == GPG_ERR_NO_SECKEY
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
319
407
recipient = recipient->next;
418
506
/* OpenPGP credentials */
419
gnutls_certificate_allocate_credentials(&mc.cred);
507
ret = gnutls_certificate_allocate_credentials(&mc.cred);
420
508
if(ret != GNUTLS_E_SUCCESS){
421
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
425
safer_gnutls_strerror(ret));
509
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
510
safer_gnutls_strerror(ret));
426
511
gnutls_global_deinit();
431
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
432
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
516
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
517
" secret key %s as GnuTLS credentials\n",
436
522
ret = gnutls_certificate_set_openpgp_key_file
437
523
(mc.cred, pubkeyfilename, seckeyfilename,
438
524
GNUTLS_OPENPGP_FMT_BASE64);
439
525
if(ret != GNUTLS_E_SUCCESS){
441
"Error[%d] while reading the OpenPGP key pair ('%s',"
442
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
443
fprintf(stderr, "The GnuTLS error is: %s\n",
444
safer_gnutls_strerror(ret));
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));
448
534
/* GnuTLS server initialization */
449
535
ret = gnutls_dh_params_init(&mc.dh_params);
450
536
if(ret != GNUTLS_E_SUCCESS){
451
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
452
" %s\n", safer_gnutls_strerror(ret));
537
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
538
" initialization: %s\n",
539
safer_gnutls_strerror(ret));
455
542
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
456
543
if(ret != GNUTLS_E_SUCCESS){
457
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
458
safer_gnutls_strerror(ret));
544
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
545
safer_gnutls_strerror(ret));
474
561
static int init_gnutls_session(gnutls_session_t *session){
476
563
/* GnuTLS session creation */
477
ret = gnutls_init(session, GNUTLS_SERVER);
565
ret = gnutls_init(session, GNUTLS_SERVER);
569
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
478
570
if(ret != GNUTLS_E_SUCCESS){
479
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
480
safer_gnutls_strerror(ret));
572
"Error in GnuTLS session initialization: %s\n",
573
safer_gnutls_strerror(ret));
485
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
579
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
581
gnutls_deinit(*session);
584
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
486
585
if(ret != GNUTLS_E_SUCCESS){
487
fprintf(stderr, "Syntax error at: %s\n", err);
488
fprintf(stderr, "GnuTLS error: %s\n",
489
safer_gnutls_strerror(ret));
586
fprintf_plus(stderr, "Syntax error at: %s\n", err);
587
fprintf_plus(stderr, "GnuTLS error: %s\n",
588
safer_gnutls_strerror(ret));
490
589
gnutls_deinit(*session);
495
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
595
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
598
gnutls_deinit(*session);
601
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
497
602
if(ret != GNUTLS_E_SUCCESS){
498
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
499
safer_gnutls_strerror(ret));
603
fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
604
safer_gnutls_strerror(ret));
500
605
gnutls_deinit(*session);
504
609
/* ignore client certificate if any. */
505
gnutls_certificate_server_set_request(*session,
610
gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
508
612
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
966
1113
signal_received = sig;
967
1114
int old_errno = errno;
1115
/* set main loop to exit */
968
1116
if(mc.simple_poll != NULL){
969
1117
avahi_simple_poll_quit(mc.simple_poll);
971
1119
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
error_t raise_privileges(void){
1397
error_t old_errno = errno;
1398
error_t ret_errno = 0;
1399
if(seteuid(0) == -1){
1401
perror_plus("seteuid");
1407
/* Set effective and real user ID to 0. Return errno. */
1408
error_t raise_privileges_permanently(void){
1409
error_t old_errno = errno;
1410
error_t ret_errno = raise_privileges();
1415
if(setuid(0) == -1){
1417
perror_plus("seteuid");
1423
/* Set effective user ID to unprivileged saved user ID */
1424
error_t lower_privileges(void){
1425
error_t old_errno = errno;
1426
error_t ret_errno = 0;
1427
if(seteuid(uid) == -1){
1429
perror_plus("seteuid");
1435
bool run_network_hooks(const char *mode, const char *interface,
1437
struct dirent **direntries;
1438
struct dirent *direntry;
1440
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1443
perror_plus("scandir");
1445
int devnull = open("/dev/null", O_RDONLY);
1446
for(int i = 0; i < numhooks; i++){
1447
direntry = direntries[i];
1448
char *fullname = NULL;
1449
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1451
perror_plus("asprintf");
1455
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1458
pid_t hook_pid = fork();
1461
/* Raise privileges */
1462
raise_privileges_permanently();
1467
perror_plus("setgid");
1469
/* Reset supplementary groups */
1471
ret = setgroups(0, NULL);
1473
perror_plus("setgroups");
1475
dup2(devnull, STDIN_FILENO);
1477
dup2(STDERR_FILENO, STDOUT_FILENO);
1478
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1480
perror_plus("setenv");
1483
ret = setenv("DEVICE", interface, 1);
1485
perror_plus("setenv");
1488
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1490
perror_plus("setenv");
1493
ret = setenv("MODE", mode, 1);
1495
perror_plus("setenv");
1499
ret = asprintf(&delaystring, "%f", delay);
1501
perror_plus("asprintf");
1504
ret = setenv("DELAY", delaystring, 1);
1507
perror_plus("setenv");
1511
if(connect_to != NULL){
1512
ret = setenv("CONNECT", connect_to, 1);
1514
perror_plus("setenv");
1518
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1519
perror_plus("execl");
1520
_exit(EXIT_FAILURE);
1524
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1525
perror_plus("waitpid");
1529
if(WIFEXITED(status)){
1530
if(WEXITSTATUS(status) != 0){
1531
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1532
" with status %d\n", direntry->d_name,
1533
WEXITSTATUS(status));
1537
} else if(WIFSIGNALED(status)){
1538
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1539
" signal %d\n", direntry->d_name,
1544
fprintf_plus(stderr, "Warning: network hook \"%s\""
1545
" crashed\n", direntry->d_name);
1552
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1561
int bring_up_interface(const char *const interface,
1564
int old_errno = errno;
1567
struct ifreq network;
1568
AvahiIfIndex if_index = (AvahiIfIndex)if_nametoindex(interface);
1570
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1580
/* Re-raise priviliges */
1584
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1585
messages about the network interface to mess up the prompt */
1586
ret = klogctl(8, NULL, 5);
1587
bool restore_loglevel = true;
1589
restore_loglevel = false;
1590
perror_plus("klogctl");
1592
#endif /* __linux__ */
1594
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1597
perror_plus("socket");
1599
if(restore_loglevel){
1600
ret = klogctl(7, NULL, 0);
1602
perror_plus("klogctl");
1605
#endif /* __linux__ */
1606
/* Lower privileges */
1611
strcpy(network.ifr_name, interface);
1612
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1615
perror_plus("ioctl SIOCGIFFLAGS");
1617
if(restore_loglevel){
1618
ret = klogctl(7, NULL, 0);
1620
perror_plus("klogctl");
1623
#endif /* __linux__ */
1624
/* Lower privileges */
1629
if((network.ifr_flags & IFF_UP) == 0){
1630
network.ifr_flags |= IFF_UP;
1631
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1634
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1636
if(restore_loglevel){
1637
ret = klogctl(7, NULL, 0);
1639
perror_plus("klogctl");
1642
#endif /* __linux__ */
1643
/* Lower privileges */
1649
/* Sleep checking until interface is running.
1650
Check every 0.25s, up to total time of delay */
1651
for(int i=0; i < delay * 4; i++){
1652
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1654
perror_plus("ioctl SIOCGIFFLAGS");
1655
} else if(network.ifr_flags & IFF_RUNNING){
1658
struct timespec sleeptime = { .tv_nsec = 250000000 };
1659
ret = nanosleep(&sleeptime, NULL);
1660
if(ret == -1 and errno != EINTR){
1661
perror_plus("nanosleep");
1664
/* Close the socket */
1665
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1667
perror_plus("close");
1670
if(restore_loglevel){
1671
/* Restores kernel loglevel to default */
1672
ret = klogctl(7, NULL, 0);
1674
perror_plus("klogctl");
1677
#endif /* __linux__ */
1678
/* Lower privileges */
974
1684
int main(int argc, char *argv[]){
975
1685
AvahiSServiceBrowser *sb = NULL;
1071
1818
delay = strtof(arg, &tmp);
1072
1819
if(errno != 0 or tmp == arg or *tmp != '\0'){
1073
fprintf(stderr, "Bad delay\n");
1820
argp_error(state, "Bad delay");
1822
case 132: /* --retry */
1824
retry_interval = strtod(arg, &tmp);
1825
if(errno != 0 or tmp == arg or *tmp != '\0'
1826
or (retry_interval * 1000) > INT_MAX
1827
or retry_interval < 0){
1828
argp_error(state, "Bad retry interval");
1831
case 133: /* --network-hook-dir */
1835
* These reproduce what we would get without ARGP_NO_HELP
1837
case '?': /* --help */
1838
argp_state_help(state, state->out_stream,
1839
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1840
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1841
case -3: /* --usage */
1842
argp_state_help(state, state->out_stream,
1843
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1844
case 'V': /* --version */
1845
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1846
exit(argp_err_exit_status);
1082
1849
return ARGP_ERR_UNKNOWN;
1087
1854
struct argp argp = { .options = options, .parser = parse_opt,
1088
1855
.args_doc = "",
1089
1856
.doc = "Mandos client -- Get and decrypt"
1090
1857
" passwords from a Mandos server" };
1091
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1092
if(ret == ARGP_ERR_UNKNOWN){
1093
fprintf(stderr, "Unknown error while parsing arguments\n");
1094
exitcode = EXIT_FAILURE;
1858
ret = argp_parse(&argp, argc, argv,
1859
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1866
perror_plus("argp_parse");
1867
exitcode = EX_OSERR;
1870
exitcode = EX_USAGE;
1876
/* Work around Debian bug #633582:
1877
<http://bugs.debian.org/633582> */
1879
/* Re-raise priviliges */
1880
if(raise_privileges() == 0){
1883
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1884
int seckey_fd = open(seckey, O_RDONLY);
1885
if(seckey_fd == -1){
1886
perror_plus("open");
1888
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1890
perror_plus("fstat");
1892
if(S_ISREG(st.st_mode)
1893
and st.st_uid == 0 and st.st_gid == 0){
1894
ret = fchown(seckey_fd, uid, gid);
1896
perror_plus("fchown");
1900
TEMP_FAILURE_RETRY(close(seckey_fd));
1904
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1905
int pubkey_fd = open(pubkey, O_RDONLY);
1906
if(pubkey_fd == -1){
1907
perror_plus("open");
1909
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1911
perror_plus("fstat");
1913
if(S_ISREG(st.st_mode)
1914
and st.st_uid == 0 and st.st_gid == 0){
1915
ret = fchown(pubkey_fd, uid, gid);
1917
perror_plus("fchown");
1921
TEMP_FAILURE_RETRY(close(pubkey_fd));
1925
/* Lower privileges */
1929
perror_plus("seteuid");
1934
/* Run network hooks */
1935
if(not run_network_hooks("start", interface, delay)){
1100
1940
avahi_set_log_function(empty_log);
1943
if(interface[0] == '\0'){
1944
struct dirent **direntries;
1945
/* First look for interfaces that are up */
1946
ret = scandir(sys_class_net, &direntries, up_interface,
1949
/* No up interfaces, look for any good interfaces */
1951
ret = scandir(sys_class_net, &direntries, good_interface,
1955
/* Pick the first interface returned */
1956
interface = strdup(direntries[0]->d_name);
1958
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1960
if(interface == NULL){
1961
perror_plus("malloc");
1963
exitcode = EXIT_FAILURE;
1969
fprintf_plus(stderr, "Could not find a network interface\n");
1970
exitcode = EXIT_FAILURE;
1103
1975
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1104
1976
from the signal handler */
1105
1977
/* Initialize the pseudo-RNG for Avahi */
1106
1978
srand((unsigned int) time(NULL));
1107
1979
mc.simple_poll = avahi_simple_poll_new();
1108
1980
if(mc.simple_poll == NULL){
1109
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1110
exitcode = EXIT_FAILURE;
1981
fprintf_plus(stderr,
1982
"Avahi: Failed to create simple poll object.\n");
1983
exitcode = EX_UNAVAILABLE;
1114
1987
sigemptyset(&sigterm_action.sa_mask);
1115
1988
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1117
perror("sigaddset");
1118
exitcode = EXIT_FAILURE;
1990
perror_plus("sigaddset");
1991
exitcode = EX_OSERR;
1121
1994
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1123
perror("sigaddset");
1124
exitcode = EXIT_FAILURE;
1996
perror_plus("sigaddset");
1997
exitcode = EX_OSERR;
1127
2000
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1129
perror("sigaddset");
1130
exitcode = EXIT_FAILURE;
2002
perror_plus("sigaddset");
2003
exitcode = EX_OSERR;
1133
2006
/* Need to check if the handler is SIG_IGN before handling:
1137
2010
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1139
perror("sigaction");
1140
return EXIT_FAILURE;
2012
perror_plus("sigaction");
1142
2015
if(old_sigterm_action.sa_handler != SIG_IGN){
1143
2016
ret = sigaction(SIGINT, &sigterm_action, NULL);
1145
perror("sigaction");
1146
exitcode = EXIT_FAILURE;
2018
perror_plus("sigaction");
2019
exitcode = EX_OSERR;
1150
2023
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1152
perror("sigaction");
1153
return EXIT_FAILURE;
2025
perror_plus("sigaction");
1155
2028
if(old_sigterm_action.sa_handler != SIG_IGN){
1156
2029
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1158
perror("sigaction");
1159
exitcode = EXIT_FAILURE;
2031
perror_plus("sigaction");
2032
exitcode = EX_OSERR;
1163
2036
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1165
perror("sigaction");
1166
return EXIT_FAILURE;
2038
perror_plus("sigaction");
1168
2041
if(old_sigterm_action.sa_handler != SIG_IGN){
1169
2042
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1171
perror("sigaction");
1172
exitcode = EXIT_FAILURE;
2044
perror_plus("sigaction");
2045
exitcode = EX_OSERR;
1177
2050
/* If the interface is down, bring it up */
1178
if(interface[0] != '\0'){
1179
if_index = (AvahiIfIndex) if_nametoindex(interface);
1181
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1182
exitcode = EXIT_FAILURE;
1191
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1192
messages to mess up the prompt */
1193
ret = klogctl(8, NULL, 5);
1194
bool restore_loglevel = true;
1196
restore_loglevel = false;
1199
#endif /* __linux__ */
1201
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1204
exitcode = EXIT_FAILURE;
1206
if(restore_loglevel){
1207
ret = klogctl(7, NULL, 0);
1212
#endif /* __linux__ */
1215
strcpy(network.ifr_name, interface);
1216
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1218
perror("ioctl SIOCGIFFLAGS");
1220
if(restore_loglevel){
1221
ret = klogctl(7, NULL, 0);
1226
#endif /* __linux__ */
1227
exitcode = EXIT_FAILURE;
1230
if((network.ifr_flags & IFF_UP) == 0){
1231
network.ifr_flags |= IFF_UP;
1232
take_down_interface = true;
1233
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1235
take_down_interface = false;
1236
perror("ioctl SIOCSIFFLAGS");
1237
exitcode = EXIT_FAILURE;
1239
if(restore_loglevel){
1240
ret = klogctl(7, NULL, 0);
1245
#endif /* __linux__ */
1249
/* sleep checking until interface is running */
1250
for(int i=0; i < delay * 4; i++){
1251
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1253
perror("ioctl SIOCGIFFLAGS");
1254
} else if(network.ifr_flags & IFF_RUNNING){
1257
struct timespec sleeptime = { .tv_nsec = 250000000 };
1258
ret = nanosleep(&sleeptime, NULL);
1259
if(ret == -1 and errno != EINTR){
1260
perror("nanosleep");
1263
if(not take_down_interface){
1264
/* We won't need the socket anymore */
1265
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1271
if(restore_loglevel){
1272
/* Restores kernel loglevel to default */
1273
ret = klogctl(7, NULL, 0);
1278
#endif /* __linux__ */
2051
if((interface[0] != '\0') and (strcmp(interface, "none") != 0)){
2052
ret = bring_up_interface(interface, delay);
2055
perror_plus("Failed to bring up interface");
1471
2252
gpgme_release(mc.ctx);
1474
/* Take down the network interface */
1475
if(take_down_interface){
1476
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1478
perror("ioctl SIOCGIFFLAGS");
1479
} else if(network.ifr_flags & IFF_UP) {
1480
network.ifr_flags &= ~IFF_UP; /* clear flag */
1481
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1483
perror("ioctl SIOCSIFFLAGS");
1486
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1492
/* Removes the temp directory used by GPGME */
2255
/* Cleans up the circular linked list of Mandos servers the client
2257
if(mc.current_server != NULL){
2258
mc.current_server->prev->next = NULL;
2259
while(mc.current_server != NULL){
2260
server *next = mc.current_server->next;
2261
free(mc.current_server);
2262
mc.current_server = next;
2266
/* Run network hooks */
2267
run_network_hooks("stop", interface, delay);
2269
/* Re-raise priviliges */
2273
/* Take down the network interface */
2274
if(take_down_interface and geteuid() == 0){
2275
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2277
perror_plus("ioctl SIOCGIFFLAGS");
2278
} else if(network.ifr_flags & IFF_UP){
2279
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2280
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2282
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2285
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2287
perror_plus("close");
2291
/* Lower privileges permanently */
2295
perror_plus("setuid");
2298
/* Removes the GPGME temp directory and all files inside */
1493
2299
if(tempdir_created){
1495
struct dirent *direntry;
1496
d = opendir(tempdir);
1498
if(errno != ENOENT){
1503
direntry = readdir(d);
1504
if(direntry == NULL){
1507
/* Skip "." and ".." */
1508
if(direntry->d_name[0] == '.'
1509
and (direntry->d_name[1] == '\0'
1510
or (direntry->d_name[1] == '.'
1511
and direntry->d_name[2] == '\0'))){
2300
struct dirent **direntries = NULL;
2301
struct dirent *direntry = NULL;
2302
int numentries = scandir(tempdir, &direntries, notdotentries,
2304
if (numentries > 0){
2305
for(int i = 0; i < numentries; i++){
2306
direntry = direntries[i];
1514
2307
char *fullname = NULL;
1515
2308
ret = asprintf(&fullname, "%s/%s", tempdir,
1516
2309
direntry->d_name);
2311
perror_plus("asprintf");
1521
2314
ret = remove(fullname);
1523
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2316
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
1526
2319
free(fullname);
2323
/* need to clean even if 0 because man page doesn't specify */
2325
if (numentries == -1){
2326
perror_plus("scandir");
1530
2328
ret = rmdir(tempdir);
1531
2329
if(ret == -1 and errno != ENOENT){
2330
perror_plus("rmdir");
1537
2335
sigemptyset(&old_sigterm_action.sa_mask);
1538
2336
old_sigterm_action.sa_handler = SIG_DFL;
1539
ret = sigaction(signal_received, &old_sigterm_action, NULL);
2337
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2338
&old_sigterm_action,
1541
perror("sigaction");
1543
raise(signal_received);
2341
perror_plus("sigaction");
2344
ret = raise(signal_received);
2345
} while(ret != 0 and errno == EINTR);
2347
perror_plus("raise");
2350
TEMP_FAILURE_RETRY(pause());
1546
2353
return exitcode;