266
351
ssize_t plaintext_length = 0;
269
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
354
fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
272
357
/* Create new GPGME data buffer from memory cryptotext */
273
358
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
275
360
if(rc != GPG_ERR_NO_ERROR){
276
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
277
gpgme_strsource(rc), gpgme_strerror(rc));
361
fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
362
gpgme_strsource(rc), gpgme_strerror(rc));
281
366
/* Create new empty GPGME data buffer for the plaintext */
282
367
rc = gpgme_data_new(&dh_plain);
283
368
if(rc != GPG_ERR_NO_ERROR){
284
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
285
gpgme_strsource(rc), gpgme_strerror(rc));
369
fprintf_plus(stderr, "Mandos plugin mandos-client: "
370
"bad gpgme_data_new: %s: %s\n",
371
gpgme_strsource(rc), gpgme_strerror(rc));
286
372
gpgme_data_release(dh_crypto);
290
376
/* Decrypt data from the cryptotext data buffer to the plaintext
292
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
378
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
293
379
if(rc != GPG_ERR_NO_ERROR){
294
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
295
gpgme_strsource(rc), gpgme_strerror(rc));
380
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
381
gpgme_strsource(rc), gpgme_strerror(rc));
296
382
plaintext_length = -1;
298
384
gpgme_decrypt_result_t result;
299
result = gpgme_op_decrypt_result(mc.ctx);
385
result = gpgme_op_decrypt_result(mc->ctx);
300
386
if(result == NULL){
301
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
387
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);
389
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
390
result->unsupported_algorithm);
391
fprintf_plus(stderr, "Wrong key usage: %u\n",
392
result->wrong_key_usage);
307
393
if(result->file_name != NULL){
308
fprintf(stderr, "File name: %s\n", result->file_name);
394
fprintf_plus(stderr, "File name: %s\n", result->file_name);
310
396
gpgme_recipient_t recipient;
311
397
recipient = result->recipients;
312
398
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
399
fprintf_plus(stderr, "Public key algorithm: %s\n",
400
gpgme_pubkey_algo_name
401
(recipient->pubkey_algo));
402
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
403
fprintf_plus(stderr, "Secret key available: %s\n",
404
recipient->status == GPG_ERR_NO_SECKEY
319
406
recipient = recipient->next;
418
505
/* OpenPGP credentials */
419
gnutls_certificate_allocate_credentials(&mc.cred);
506
ret = gnutls_certificate_allocate_credentials(&mc->cred);
420
507
if(ret != GNUTLS_E_SUCCESS){
421
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
425
safer_gnutls_strerror(ret));
508
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
509
safer_gnutls_strerror(ret));
426
510
gnutls_global_deinit();
431
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
432
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
515
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
516
" secret key %s as GnuTLS credentials\n",
436
521
ret = gnutls_certificate_set_openpgp_key_file
437
(mc.cred, pubkeyfilename, seckeyfilename,
522
(mc->cred, pubkeyfilename, seckeyfilename,
438
523
GNUTLS_OPENPGP_FMT_BASE64);
439
524
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));
526
"Error[%d] while reading the OpenPGP key pair ('%s',"
527
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
528
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
529
safer_gnutls_strerror(ret));
448
533
/* GnuTLS server initialization */
449
ret = gnutls_dh_params_init(&mc.dh_params);
534
ret = gnutls_dh_params_init(&mc->dh_params);
450
535
if(ret != GNUTLS_E_SUCCESS){
451
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
452
" %s\n", safer_gnutls_strerror(ret));
536
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
537
" initialization: %s\n",
538
safer_gnutls_strerror(ret));
455
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
541
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
456
542
if(ret != GNUTLS_E_SUCCESS){
457
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
458
safer_gnutls_strerror(ret));
543
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
544
safer_gnutls_strerror(ret));
462
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
548
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
468
gnutls_certificate_free_credentials(mc.cred);
554
gnutls_certificate_free_credentials(mc->cred);
469
555
gnutls_global_deinit();
470
gnutls_dh_params_deinit(mc.dh_params);
556
gnutls_dh_params_deinit(mc->dh_params);
474
static int init_gnutls_session(gnutls_session_t *session){
560
static int init_gnutls_session(gnutls_session_t *session,
476
563
/* GnuTLS session creation */
478
565
ret = gnutls_init(session, GNUTLS_SERVER);
479
569
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
480
570
if(ret != GNUTLS_E_SUCCESS){
481
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
482
safer_gnutls_strerror(ret));
572
"Error in GnuTLS session initialization: %s\n",
573
safer_gnutls_strerror(ret));
488
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
579
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
581
gnutls_deinit(*session);
489
584
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
490
585
if(ret != GNUTLS_E_SUCCESS){
491
fprintf(stderr, "Syntax error at: %s\n", err);
492
fprintf(stderr, "GnuTLS error: %s\n",
493
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));
494
589
gnutls_deinit(*session);
971
1121
signal_received = sig;
972
1122
int old_errno = errno;
973
if(mc.simple_poll != NULL){
974
avahi_simple_poll_quit(mc.simple_poll);
1123
/* set main loop to exit */
1124
if(simple_poll != NULL){
1125
avahi_simple_poll_quit(simple_poll);
1130
bool get_flags(const char *ifname, struct ifreq *ifr){
1134
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1137
perror_plus("socket");
1141
strcpy(ifr->ifr_name, ifname);
1142
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1146
perror_plus("ioctl SIOCGIFFLAGS");
1154
bool good_flags(const char *ifname, const struct ifreq *ifr){
1156
/* Reject the loopback device */
1157
if(ifr->ifr_flags & IFF_LOOPBACK){
1159
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1164
/* Accept point-to-point devices only if connect_to is specified */
1165
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1167
fprintf_plus(stderr, "Accepting point-to-point interface"
1168
" \"%s\"\n", ifname);
1172
/* Otherwise, reject non-broadcast-capable devices */
1173
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1175
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1176
" \"%s\"\n", ifname);
1180
/* Reject non-ARP interfaces (including dummy interfaces) */
1181
if(ifr->ifr_flags & IFF_NOARP){
1183
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1189
/* Accept this device */
1191
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1197
* This function determines if a directory entry in /sys/class/net
1198
* corresponds to an acceptable network device.
1199
* (This function is passed to scandir(3) as a filter function.)
1201
int good_interface(const struct dirent *if_entry){
1202
if(if_entry->d_name[0] == '.'){
1207
if(not get_flags(if_entry->d_name, &ifr)){
1209
fprintf_plus(stderr, "Failed to get flags for interface "
1210
"\"%s\"\n", if_entry->d_name);
1215
if(not good_flags(if_entry->d_name, &ifr)){
1222
* This function determines if a network interface is up.
1224
bool interface_is_up(const char *interface){
1226
if(not get_flags(interface, &ifr)){
1228
fprintf_plus(stderr, "Failed to get flags for interface "
1229
"\"%s\"\n", interface);
1234
return (bool)(ifr.ifr_flags & IFF_UP);
1238
* This function determines if a network interface is running
1240
bool interface_is_running(const char *interface){
1242
if(not get_flags(interface, &ifr)){
1244
fprintf_plus(stderr, "Failed to get flags for interface "
1245
"\"%s\"\n", interface);
1250
return (bool)(ifr.ifr_flags & IFF_RUNNING);
1253
int notdotentries(const struct dirent *direntry){
1254
/* Skip "." and ".." */
1255
if(direntry->d_name[0] == '.'
1256
and (direntry->d_name[1] == '\0'
1257
or (direntry->d_name[1] == '.'
1258
and direntry->d_name[2] == '\0'))){
1264
/* Is this directory entry a runnable program? */
1265
int runnable_hook(const struct dirent *direntry){
1270
if((direntry->d_name)[0] == '\0'){
1275
sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1276
"abcdefghijklmnopqrstuvwxyz"
1279
if((direntry->d_name)[sret] != '\0'){
1280
/* Contains non-allowed characters */
1282
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1288
char *fullname = NULL;
1289
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1291
perror_plus("asprintf");
1295
ret = stat(fullname, &st);
1298
perror_plus("Could not stat hook");
1302
if(not (S_ISREG(st.st_mode))){
1303
/* Not a regular file */
1305
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1310
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1311
/* Not executable */
1313
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1319
fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1325
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1326
mandos_context *mc){
1328
struct timespec now;
1329
struct timespec waited_time;
1330
intmax_t block_time;
1333
if(mc->current_server == NULL){
1335
fprintf_plus(stderr, "Wait until first server is found."
1338
ret = avahi_simple_poll_iterate(s, -1);
1341
fprintf_plus(stderr, "Check current_server if we should run"
1344
/* the current time */
1345
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1347
perror_plus("clock_gettime");
1350
/* Calculating in ms how long time between now and server
1351
who we visted longest time ago. Now - last seen. */
1352
waited_time.tv_sec = (now.tv_sec
1353
- mc->current_server->last_seen.tv_sec);
1354
waited_time.tv_nsec = (now.tv_nsec
1355
- mc->current_server->last_seen.tv_nsec);
1356
/* total time is 10s/10,000ms.
1357
Converting to s from ms by dividing by 1,000,
1358
and ns to ms by dividing by 1,000,000. */
1359
block_time = ((retry_interval
1360
- ((intmax_t)waited_time.tv_sec * 1000))
1361
- ((intmax_t)waited_time.tv_nsec / 1000000));
1364
fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1368
if(block_time <= 0){
1369
ret = start_mandos_communication(mc->current_server->ip,
1370
mc->current_server->port,
1371
mc->current_server->if_index,
1372
mc->current_server->af, mc);
1374
avahi_simple_poll_quit(s);
1377
ret = clock_gettime(CLOCK_MONOTONIC,
1378
&mc->current_server->last_seen);
1380
perror_plus("clock_gettime");
1383
mc->current_server = mc->current_server->next;
1384
block_time = 0; /* Call avahi to find new Mandos
1385
servers, but don't block */
1388
ret = avahi_simple_poll_iterate(s, (int)block_time);
1391
if (ret > 0 or errno != EINTR){
1392
return (ret != 1) ? ret : 0;
1398
/* Set effective uid to 0, return errno */
1399
error_t raise_privileges(void){
1400
error_t old_errno = errno;
1401
error_t ret_errno = 0;
1402
if(seteuid(0) == -1){
1404
perror_plus("seteuid");
1410
/* Set effective and real user ID to 0. Return errno. */
1411
error_t raise_privileges_permanently(void){
1412
error_t old_errno = errno;
1413
error_t ret_errno = raise_privileges();
1418
if(setuid(0) == -1){
1420
perror_plus("seteuid");
1426
/* Set effective user ID to unprivileged saved user ID */
1427
error_t lower_privileges(void){
1428
error_t old_errno = errno;
1429
error_t ret_errno = 0;
1430
if(seteuid(uid) == -1){
1432
perror_plus("seteuid");
1438
/* Lower privileges permanently */
1439
error_t lower_privileges_permanently(void){
1440
error_t old_errno = errno;
1441
error_t ret_errno = 0;
1442
if(setuid(uid) == -1){
1444
perror_plus("setuid");
1450
bool run_network_hooks(const char *mode, const char *interface,
1452
struct dirent **direntries;
1453
struct dirent *direntry;
1455
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1458
if(errno == ENOENT){
1460
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1461
" found\n", hookdir);
1464
perror_plus("scandir");
1467
int devnull = open("/dev/null", O_RDONLY);
1468
for(int i = 0; i < numhooks; i++){
1469
direntry = direntries[i];
1470
char *fullname = NULL;
1471
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1473
perror_plus("asprintf");
1477
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1480
pid_t hook_pid = fork();
1483
/* Raise privileges */
1484
raise_privileges_permanently();
1489
perror_plus("setgid");
1491
/* Reset supplementary groups */
1493
ret = setgroups(0, NULL);
1495
perror_plus("setgroups");
1497
dup2(devnull, STDIN_FILENO);
1499
dup2(STDERR_FILENO, STDOUT_FILENO);
1500
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1502
perror_plus("setenv");
1505
ret = setenv("DEVICE", interface, 1);
1507
perror_plus("setenv");
1510
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1512
perror_plus("setenv");
1515
ret = setenv("MODE", mode, 1);
1517
perror_plus("setenv");
1521
ret = asprintf(&delaystring, "%f", delay);
1523
perror_plus("asprintf");
1526
ret = setenv("DELAY", delaystring, 1);
1529
perror_plus("setenv");
1533
if(connect_to != NULL){
1534
ret = setenv("CONNECT", connect_to, 1);
1536
perror_plus("setenv");
1540
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1541
perror_plus("execl");
1542
_exit(EXIT_FAILURE);
1546
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1547
perror_plus("waitpid");
1551
if(WIFEXITED(status)){
1552
if(WEXITSTATUS(status) != 0){
1553
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1554
" with status %d\n", direntry->d_name,
1555
WEXITSTATUS(status));
1559
} else if(WIFSIGNALED(status)){
1560
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1561
" signal %d\n", direntry->d_name,
1566
fprintf_plus(stderr, "Warning: network hook \"%s\""
1567
" crashed\n", direntry->d_name);
1574
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1583
error_t bring_up_interface(const char *const interface,
1586
error_t old_errno = errno;
1587
error_t ret_errno = 0;
1588
int ret, ret_setflags;
1589
struct ifreq network;
1590
unsigned int if_index = if_nametoindex(interface);
1592
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1602
if(not interface_is_up(interface)){
1603
if(not get_flags(interface, &network) and debug){
1605
fprintf_plus(stderr, "Failed to get flags for interface "
1606
"\"%s\"\n", interface);
1609
network.ifr_flags |= IFF_UP;
1611
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1614
perror_plus("socket");
1626
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1630
/* Raise priviliges */
1634
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1635
messages about the network interface to mess up the prompt */
1636
int ret_linux = klogctl(8, NULL, 5);
1637
bool restore_loglevel = true;
1638
if(ret_linux == -1){
1639
restore_loglevel = false;
1640
perror_plus("klogctl");
1642
#endif /* __linux__ */
1643
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1646
if(restore_loglevel){
1647
ret_linux = klogctl(7, NULL, 0);
1648
if(ret_linux == -1){
1649
perror_plus("klogctl");
1652
#endif /* __linux__ */
1654
/* Lower privileges */
1657
/* Close the socket */
1658
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1660
perror_plus("close");
1663
if(ret_setflags == -1){
1665
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1670
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1674
/* Sleep checking until interface is running.
1675
Check every 0.25s, up to total time of delay */
1676
for(int i=0; i < delay * 4; i++){
1677
if(interface_is_running(interface)){
1680
struct timespec sleeptime = { .tv_nsec = 250000000 };
1681
ret = nanosleep(&sleeptime, NULL);
1682
if(ret == -1 and errno != EINTR){
1683
perror_plus("nanosleep");
1691
error_t take_down_interface(const char *const interface){
1693
error_t old_errno = errno;
1694
error_t ret_errno = 0;
1695
int ret, ret_setflags;
1696
struct ifreq network;
1697
unsigned int if_index = if_nametoindex(interface);
1699
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1703
if(interface_is_up(interface)){
1704
if(not get_flags(interface, &network) and debug){
1706
fprintf_plus(stderr, "Failed to get flags for interface "
1707
"\"%s\"\n", interface);
1710
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1712
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1715
perror_plus("socket");
1721
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1725
/* Raise priviliges */
1728
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1731
/* Lower privileges */
1734
/* Close the socket */
1735
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1737
perror_plus("close");
1740
if(ret_setflags == -1){
1742
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1747
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
979
1755
int main(int argc, char *argv[]){
1756
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1757
.priority = "SECURE256:!CTYPE-X.509:"
1758
"+CTYPE-OPENPGP", .current_server = NULL };
980
1759
AvahiSServiceBrowser *sb = NULL;
983
1762
intmax_t tmpmax;
985
1764
int exitcode = EXIT_SUCCESS;
986
const char *interface = "eth0";
987
struct ifreq network;
989
bool take_down_interface = false;
992
char *connect_to = NULL;
1765
char *interfaces = NULL;
1766
size_t interfaces_size = 0;
1767
char *interfaces_to_take_down = NULL;
1768
size_t interfaces_to_take_down_size = 0;
993
1769
char tempdir[] = "/tmp/mandosXXXXXX";
994
1770
bool tempdir_created = false;
995
1771
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
996
1772
const char *seckey = PATHDIR "/" SECKEY;
997
1773
const char *pubkey = PATHDIR "/" PUBKEY;
1774
char *interfaces_hooks = NULL;
1775
size_t interfaces_hooks_size = 0;
999
1777
bool gnutls_initialized = false;
1000
1778
bool gpgme_initialized = false;
1001
1779
float delay = 2.5f;
1780
double retry_interval = 10; /* 10s between trying a server and
1781
retrying the same server again */
1003
1783
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1004
1784
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1097
1898
delay = strtof(arg, &tmp);
1098
1899
if(errno != 0 or tmp == arg or *tmp != '\0'){
1099
fprintf(stderr, "Bad delay\n");
1900
argp_error(state, "Bad delay");
1902
case 132: /* --retry */
1904
retry_interval = strtod(arg, &tmp);
1905
if(errno != 0 or tmp == arg or *tmp != '\0'
1906
or (retry_interval * 1000) > INT_MAX
1907
or retry_interval < 0){
1908
argp_error(state, "Bad retry interval");
1911
case 133: /* --network-hook-dir */
1915
* These reproduce what we would get without ARGP_NO_HELP
1917
case '?': /* --help */
1918
argp_state_help(state, state->out_stream,
1919
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1920
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1921
case -3: /* --usage */
1922
argp_state_help(state, state->out_stream,
1923
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1924
case 'V': /* --version */
1925
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1926
exit(argp_err_exit_status);
1108
1929
return ARGP_ERR_UNKNOWN;
1113
1934
struct argp argp = { .options = options, .parser = parse_opt,
1114
1935
.args_doc = "",
1115
1936
.doc = "Mandos client -- Get and decrypt"
1116
1937
" passwords from a Mandos server" };
1117
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1118
if(ret == ARGP_ERR_UNKNOWN){
1119
fprintf(stderr, "Unknown error while parsing arguments\n");
1120
exitcode = EXIT_FAILURE;
1938
ret = argp_parse(&argp, argc, argv,
1939
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1946
perror_plus("argp_parse");
1947
exitcode = EX_OSERR;
1950
exitcode = EX_USAGE;
1956
/* Work around Debian bug #633582:
1957
<http://bugs.debian.org/633582> */
1959
/* Re-raise priviliges */
1960
if(raise_privileges() == 0){
1963
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1964
int seckey_fd = open(seckey, O_RDONLY);
1965
if(seckey_fd == -1){
1966
perror_plus("open");
1968
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1970
perror_plus("fstat");
1972
if(S_ISREG(st.st_mode)
1973
and st.st_uid == 0 and st.st_gid == 0){
1974
ret = fchown(seckey_fd, uid, gid);
1976
perror_plus("fchown");
1980
TEMP_FAILURE_RETRY(close(seckey_fd));
1984
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1985
int pubkey_fd = open(pubkey, O_RDONLY);
1986
if(pubkey_fd == -1){
1987
perror_plus("open");
1989
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1991
perror_plus("fstat");
1993
if(S_ISREG(st.st_mode)
1994
and st.st_uid == 0 and st.st_gid == 0){
1995
ret = fchown(pubkey_fd, uid, gid);
1997
perror_plus("fchown");
2001
TEMP_FAILURE_RETRY(close(pubkey_fd));
2005
/* Lower privileges */
2009
perror_plus("seteuid");
2014
/* Remove empty interface names */
2016
char *interface = NULL;
2017
while((interface = argz_next(interfaces, interfaces_size,
2019
if(if_nametoindex(interface) == 0){
2020
if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2021
fprintf_plus(stderr, "Not using nonexisting interface"
2022
" \"%s\"\n", interface);
2024
argz_delete(&interfaces, &interfaces_size, interface);
2030
/* Run network hooks */
2033
if(interfaces != NULL){
2034
interfaces_hooks = malloc(interfaces_size);
2035
if(interfaces_hooks == NULL){
2036
perror_plus("malloc");
2039
memcpy(interfaces_hooks, interfaces, interfaces_size);
2040
interfaces_hooks_size = interfaces_size;
2041
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2044
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2045
interfaces_hooks : "", delay)){
1163
2089
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1165
perror("sigaction");
1166
return EXIT_FAILURE;
2091
perror_plus("sigaction");
1168
2094
if(old_sigterm_action.sa_handler != SIG_IGN){
1169
2095
ret = sigaction(SIGINT, &sigterm_action, NULL);
1171
perror("sigaction");
1172
exitcode = EXIT_FAILURE;
2097
perror_plus("sigaction");
2098
exitcode = EX_OSERR;
1176
2102
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1178
perror("sigaction");
1179
return EXIT_FAILURE;
2104
perror_plus("sigaction");
1181
2107
if(old_sigterm_action.sa_handler != SIG_IGN){
1182
2108
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1184
perror("sigaction");
1185
exitcode = EXIT_FAILURE;
2110
perror_plus("sigaction");
2111
exitcode = EX_OSERR;
1189
2115
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1191
perror("sigaction");
1192
return EXIT_FAILURE;
2117
perror_plus("sigaction");
1194
2120
if(old_sigterm_action.sa_handler != SIG_IGN){
1195
2121
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1197
perror("sigaction");
1198
exitcode = EXIT_FAILURE;
2123
perror_plus("sigaction");
2124
exitcode = EX_OSERR;
1203
/* If the interface is down, bring it up */
1204
if(interface[0] != '\0'){
1205
if_index = (AvahiIfIndex) if_nametoindex(interface);
1207
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1208
exitcode = EXIT_FAILURE;
1216
/* Re-raise priviliges */
1224
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1225
messages to mess up the prompt */
1226
ret = klogctl(8, NULL, 5);
1227
bool restore_loglevel = true;
1229
restore_loglevel = false;
1232
#endif /* __linux__ */
1234
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1237
exitcode = EXIT_FAILURE;
1239
if(restore_loglevel){
1240
ret = klogctl(7, NULL, 0);
1245
#endif /* __linux__ */
1246
/* Lower privileges */
1254
strcpy(network.ifr_name, interface);
1255
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1257
perror("ioctl SIOCGIFFLAGS");
1259
if(restore_loglevel){
1260
ret = klogctl(7, NULL, 0);
1265
#endif /* __linux__ */
1266
exitcode = EXIT_FAILURE;
1267
/* Lower privileges */
1275
if((network.ifr_flags & IFF_UP) == 0){
1276
network.ifr_flags |= IFF_UP;
1277
take_down_interface = true;
1278
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1280
take_down_interface = false;
1281
perror("ioctl SIOCSIFFLAGS");
1282
exitcode = EXIT_FAILURE;
1284
if(restore_loglevel){
1285
ret = klogctl(7, NULL, 0);
1290
#endif /* __linux__ */
1291
/* Lower privileges */
1300
/* sleep checking until interface is running */
1301
for(int i=0; i < delay * 4; i++){
1302
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1304
perror("ioctl SIOCGIFFLAGS");
1305
} else if(network.ifr_flags & IFF_RUNNING){
1308
struct timespec sleeptime = { .tv_nsec = 250000000 };
1309
ret = nanosleep(&sleeptime, NULL);
1310
if(ret == -1 and errno != EINTR){
1311
perror("nanosleep");
1314
if(not take_down_interface){
1315
/* We won't need the socket anymore */
1316
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1322
if(restore_loglevel){
1323
/* Restores kernel loglevel to default */
1324
ret = klogctl(7, NULL, 0);
1329
#endif /* __linux__ */
1330
/* Lower privileges */
1332
if(take_down_interface){
1333
/* Lower privileges */
2129
/* If no interfaces were specified, make a list */
2130
if(interfaces == NULL){
2131
struct dirent **direntries;
2132
/* Look for any good interfaces */
2133
ret = scandir(sys_class_net, &direntries, good_interface,
2136
/* Add all found interfaces to interfaces list */
2137
for(int i = 0; i < ret; ++i){
2138
ret_errno = argz_add(&interfaces, &interfaces_size,
2139
direntries[i]->d_name);
2141
perror_plus("argz_add");
2145
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2146
direntries[i]->d_name);
1339
/* Lower privileges permanently */
2152
fprintf_plus(stderr, "Could not find a network interface\n");
2153
exitcode = EXIT_FAILURE;
2158
/* If we only got one interface, explicitly use only that one */
2159
if(argz_count(interfaces, interfaces_size) == 1){
2161
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2164
if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2167
/* Bring up interfaces which are down */
2168
if(not (argz_count(interfaces, interfaces_size) == 1
2169
and strcmp(interfaces, "none") == 0)){
2170
char *interface = NULL;
2171
while((interface = argz_next(interfaces, interfaces_size,
2173
bool interface_was_up = interface_is_up(interface);
2174
ret = bring_up_interface(interface, delay);
2175
if(not interface_was_up){
2178
perror_plus("Failed to bring up interface");
2180
ret_errno = argz_add(&interfaces_to_take_down,
2181
&interfaces_to_take_down_size,
2188
interfaces_size = 0;
2189
if(debug and (interfaces_to_take_down == NULL)){
2190
fprintf_plus(stderr, "No interfaces were brought up\n");
1351
ret = init_gnutls_global(pubkey, seckey);
2198
ret = init_gnutls_global(pubkey, seckey, &mc);
1353
fprintf(stderr, "init_gnutls_global failed\n");
1354
exitcode = EXIT_FAILURE;
2200
fprintf_plus(stderr, "init_gnutls_global failed\n");
2201
exitcode = EX_UNAVAILABLE;
1357
2204
gnutls_initialized = true;
1519
2388
gpgme_release(mc.ctx);
1522
/* Take down the network interface */
1523
if(take_down_interface){
1524
/* Re-raise priviliges */
2391
/* Cleans up the circular linked list of Mandos servers the client
2393
if(mc.current_server != NULL){
2394
mc.current_server->prev->next = NULL;
2395
while(mc.current_server != NULL){
2396
server *next = mc.current_server->next;
2397
free(mc.current_server);
2398
mc.current_server = next;
1531
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1533
perror("ioctl SIOCGIFFLAGS");
1534
} else if(network.ifr_flags & IFF_UP) {
1535
network.ifr_flags &= ~IFF_UP; /* clear flag */
1536
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1538
perror("ioctl SIOCSIFFLAGS");
2402
/* Re-raise priviliges */
2406
/* Run network hooks */
2407
run_network_hooks("stop", interfaces_hooks != NULL ?
2408
interfaces_hooks : "", delay);
2410
/* Take down the network interfaces which were brought up */
2412
char *interface = NULL;
2413
while((interface=argz_next(interfaces_to_take_down,
2414
interfaces_to_take_down_size,
2416
ret_errno = take_down_interface(interface);
2419
perror_plus("Failed to take down interface");
1541
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1545
/* Lower privileges permanently */
2422
if(debug and (interfaces_to_take_down == NULL)){
2423
fprintf_plus(stderr, "No interfaces needed to be taken"
2428
lower_privileges_permanently();
1554
/* Removes the temp directory used by GPGME */
2431
free(interfaces_to_take_down);
2432
free(interfaces_hooks);
2434
/* Removes the GPGME temp directory and all files inside */
1555
2435
if(tempdir_created){
1557
struct dirent *direntry;
1558
d = opendir(tempdir);
1560
if(errno != ENOENT){
1565
direntry = readdir(d);
1566
if(direntry == NULL){
1569
/* Skip "." and ".." */
1570
if(direntry->d_name[0] == '.'
1571
and (direntry->d_name[1] == '\0'
1572
or (direntry->d_name[1] == '.'
1573
and direntry->d_name[2] == '\0'))){
2436
struct dirent **direntries = NULL;
2437
struct dirent *direntry = NULL;
2438
int numentries = scandir(tempdir, &direntries, notdotentries,
2440
if (numentries > 0){
2441
for(int i = 0; i < numentries; i++){
2442
direntry = direntries[i];
1576
2443
char *fullname = NULL;
1577
2444
ret = asprintf(&fullname, "%s/%s", tempdir,
1578
2445
direntry->d_name);
2447
perror_plus("asprintf");
1583
2450
ret = remove(fullname);
1585
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2452
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
1588
2455
free(fullname);
2459
/* need to clean even if 0 because man page doesn't specify */
2461
if (numentries == -1){
2462
perror_plus("scandir");
1592
2464
ret = rmdir(tempdir);
1593
2465
if(ret == -1 and errno != ENOENT){
2466
perror_plus("rmdir");
1599
2471
sigemptyset(&old_sigterm_action.sa_mask);
1600
2472
old_sigterm_action.sa_handler = SIG_DFL;
1601
ret = sigaction(signal_received, &old_sigterm_action, NULL);
2473
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2474
&old_sigterm_action,
1603
perror("sigaction");
1605
raise(signal_received);
2477
perror_plus("sigaction");
2480
ret = raise(signal_received);
2481
} while(ret != 0 and errno == EINTR);
2483
perror_plus("raise");
2486
TEMP_FAILURE_RETRY(pause());
1608
2489
return exitcode;