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
312
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
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
407
319
recipient = recipient->next;
506
418
/* OpenPGP credentials */
507
ret = gnutls_certificate_allocate_credentials(&mc.cred);
419
gnutls_certificate_allocate_credentials(&mc.cred);
508
420
if(ret != GNUTLS_E_SUCCESS){
509
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
510
safer_gnutls_strerror(ret));
421
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
425
safer_gnutls_strerror(ret));
511
426
gnutls_global_deinit();
516
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
517
" secret key %s as GnuTLS credentials\n",
431
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
432
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
522
436
ret = gnutls_certificate_set_openpgp_key_file
523
437
(mc.cred, pubkeyfilename, seckeyfilename,
524
438
GNUTLS_OPENPGP_FMT_BASE64);
525
439
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));
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));
534
448
/* GnuTLS server initialization */
535
449
ret = gnutls_dh_params_init(&mc.dh_params);
536
450
if(ret != GNUTLS_E_SUCCESS){
537
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
538
" initialization: %s\n",
539
safer_gnutls_strerror(ret));
451
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
452
" %s\n", safer_gnutls_strerror(ret));
542
455
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
543
456
if(ret != GNUTLS_E_SUCCESS){
544
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
545
safer_gnutls_strerror(ret));
457
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
458
safer_gnutls_strerror(ret));
561
474
static int init_gnutls_session(gnutls_session_t *session){
563
476
/* GnuTLS session creation */
565
ret = gnutls_init(session, GNUTLS_SERVER);
569
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
477
ret = gnutls_init(session, GNUTLS_SERVER);
570
478
if(ret != GNUTLS_E_SUCCESS){
572
"Error in GnuTLS session initialization: %s\n",
573
safer_gnutls_strerror(ret));
479
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
480
safer_gnutls_strerror(ret));
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);
485
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
585
486
if(ret != GNUTLS_E_SUCCESS){
586
fprintf_plus(stderr, "Syntax error at: %s\n", err);
587
fprintf_plus(stderr, "GnuTLS error: %s\n",
588
safer_gnutls_strerror(ret));
487
fprintf(stderr, "Syntax error at: %s\n", err);
488
fprintf(stderr, "GnuTLS error: %s\n",
489
safer_gnutls_strerror(ret));
589
490
gnutls_deinit(*session);
595
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
598
gnutls_deinit(*session);
601
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
495
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
602
497
if(ret != GNUTLS_E_SUCCESS){
603
fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
604
safer_gnutls_strerror(ret));
498
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
499
safer_gnutls_strerror(ret));
605
500
gnutls_deinit(*session);
609
504
/* ignore client certificate if any. */
610
gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
505
gnutls_certificate_server_set_request(*session,
612
508
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
1113
966
signal_received = sig;
1114
967
int old_errno = errno;
1115
/* set main loop to exit */
1116
968
if(mc.simple_poll != NULL){
1117
969
avahi_simple_poll_quit(mc.simple_poll);
1119
971
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
974
int main(int argc, char *argv[]){
1687
975
AvahiSServiceBrowser *sb = NULL;
1820
1071
delay = strtof(arg, &tmp);
1821
1072
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);
1073
fprintf(stderr, "Bad delay\n");
1851
1082
return ARGP_ERR_UNKNOWN;
1856
1087
struct argp argp = { .options = options, .parser = parse_opt,
1857
1088
.args_doc = "",
1858
1089
.doc = "Mandos client -- Get and decrypt"
1859
1090
" 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)){
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;
1942
1100
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
1103
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1978
1104
from the signal handler */
1979
1105
/* Initialize the pseudo-RNG for Avahi */
1980
1106
srand((unsigned int) time(NULL));
1981
1107
mc.simple_poll = avahi_simple_poll_new();
1982
1108
if(mc.simple_poll == NULL){
1983
fprintf_plus(stderr,
1984
"Avahi: Failed to create simple poll object.\n");
1985
exitcode = EX_UNAVAILABLE;
1109
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1110
exitcode = EXIT_FAILURE;
1989
1114
sigemptyset(&sigterm_action.sa_mask);
1990
1115
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1992
perror_plus("sigaddset");
1993
exitcode = EX_OSERR;
1117
perror("sigaddset");
1118
exitcode = EXIT_FAILURE;
1996
1121
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1998
perror_plus("sigaddset");
1999
exitcode = EX_OSERR;
1123
perror("sigaddset");
1124
exitcode = EXIT_FAILURE;
2002
1127
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
2004
perror_plus("sigaddset");
2005
exitcode = EX_OSERR;
1129
perror("sigaddset");
1130
exitcode = EXIT_FAILURE;
2008
1133
/* Need to check if the handler is SIG_IGN before handling:
2012
1137
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
2014
perror_plus("sigaction");
1139
perror("sigaction");
1140
return EXIT_FAILURE;
2017
1142
if(old_sigterm_action.sa_handler != SIG_IGN){
2018
1143
ret = sigaction(SIGINT, &sigterm_action, NULL);
2020
perror_plus("sigaction");
2021
exitcode = EX_OSERR;
1145
perror("sigaction");
1146
exitcode = EXIT_FAILURE;
2025
1150
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
2027
perror_plus("sigaction");
1152
perror("sigaction");
1153
return EXIT_FAILURE;
2030
1155
if(old_sigterm_action.sa_handler != SIG_IGN){
2031
1156
ret = sigaction(SIGHUP, &sigterm_action, NULL);
2033
perror_plus("sigaction");
2034
exitcode = EX_OSERR;
1158
perror("sigaction");
1159
exitcode = EXIT_FAILURE;
2038
1163
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
2040
perror_plus("sigaction");
1165
perror("sigaction");
1166
return EXIT_FAILURE;
2043
1168
if(old_sigterm_action.sa_handler != SIG_IGN){
2044
1169
ret = sigaction(SIGTERM, &sigterm_action, NULL);
2046
perror_plus("sigaction");
2047
exitcode = EX_OSERR;
1171
perror("sigaction");
1172
exitcode = EXIT_FAILURE;
2052
1177
/* 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");
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__ */
1288
/* Drop any group privileges we might have, just to be safe */
1295
/* Drop user privileges */
1297
/* Will we need privileges later? */
1298
if(take_down_interface){
1299
/* Drop user privileges temporarily */
1305
/* Drop user privileges permanently */
2254
1484
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;
1487
/* Take down the network interface */
1488
if(take_down_interface){
1489
/* Re-raise priviliges */
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
1496
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 */
1498
perror("ioctl SIOCGIFFLAGS");
1499
} else if(network.ifr_flags & IFF_UP) {
1500
network.ifr_flags &= ~IFF_UP; /* clear flag */
2282
1501
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2284
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1503
perror("ioctl SIOCSIFFLAGS");
2287
1506
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2289
perror_plus("close");
1510
/* Lower privileges, permanently this time */
2293
/* Lower privileges permanently */
2297
perror_plus("setuid");
2300
/* Removes the GPGME temp directory and all files inside */
1519
/* Removes the temp directory used by GPGME */
2301
1520
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];
1522
struct dirent *direntry;
1523
d = opendir(tempdir);
1525
if(errno != ENOENT){
1530
direntry = readdir(d);
1531
if(direntry == NULL){
1534
/* Skip "." and ".." */
1535
if(direntry->d_name[0] == '.'
1536
and (direntry->d_name[1] == '\0'
1537
or (direntry->d_name[1] == '.'
1538
and direntry->d_name[2] == '\0'))){
2309
1541
char *fullname = NULL;
2310
1542
ret = asprintf(&fullname, "%s/%s", tempdir,
2311
1543
direntry->d_name);
2313
perror_plus("asprintf");
2316
1548
ret = remove(fullname);
2318
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
1550
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2321
1553
free(fullname);
2325
/* need to clean even if 0 because man page doesn't specify */
2327
if (numentries == -1){
2328
perror_plus("scandir");
2330
1557
ret = rmdir(tempdir);
2331
1558
if(ret == -1 and errno != ENOENT){
2332
perror_plus("rmdir");
2337
1564
sigemptyset(&old_sigterm_action.sa_mask);
2338
1565
old_sigterm_action.sa_handler = SIG_DFL;
2339
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2340
&old_sigterm_action,
1566
ret = sigaction(signal_received, &old_sigterm_action, NULL);
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());
1568
perror("sigaction");
1570
raise(signal_received);
2355
1573
return exitcode;