506
425
/* OpenPGP credentials */
507
ret = gnutls_certificate_allocate_credentials(&mc.cred);
426
gnutls_certificate_allocate_credentials(&mc.cred);
508
427
if(ret != GNUTLS_E_SUCCESS){
509
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
510
safer_gnutls_strerror(ret));
428
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
432
safer_gnutls_strerror(ret));
511
433
gnutls_global_deinit();
516
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
517
" secret key %s as GnuTLS credentials\n",
438
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
439
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
522
443
ret = gnutls_certificate_set_openpgp_key_file
523
444
(mc.cred, pubkeyfilename, seckeyfilename,
524
445
GNUTLS_OPENPGP_FMT_BASE64);
525
446
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));
448
"Error[%d] while reading the OpenPGP key pair ('%s',"
449
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
450
fprintf(stderr, "The GnuTLS error is: %s\n",
451
safer_gnutls_strerror(ret));
534
455
/* GnuTLS server initialization */
535
456
ret = gnutls_dh_params_init(&mc.dh_params);
536
457
if(ret != GNUTLS_E_SUCCESS){
537
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
538
" initialization: %s\n",
539
safer_gnutls_strerror(ret));
458
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
459
" %s\n", safer_gnutls_strerror(ret));
542
462
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
543
463
if(ret != GNUTLS_E_SUCCESS){
544
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
545
safer_gnutls_strerror(ret));
464
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
465
safer_gnutls_strerror(ret));
1113
1019
signal_received = sig;
1114
1020
int old_errno = errno;
1115
/* set main loop to exit */
1116
1021
if(mc.simple_poll != NULL){
1117
1022
avahi_simple_poll_quit(mc.simple_poll);
1119
1024
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);
1028
* This function determines if a directory entry in /sys/class/net
1029
* corresponds to an acceptable network device.
1030
* (This function is passed to scandir(3) as a filter function.)
1032
int good_interface(const struct dirent *if_entry){
1034
char *flagname = NULL;
1035
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1041
if(if_entry->d_name[0] == '.'){
1044
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1049
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1050
/* read line from flags_fd */
1051
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1052
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1053
flagstring[(size_t)to_read] = '\0';
1054
if(flagstring == NULL){
1060
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1077
tmpmax = strtoimax(flagstring, &tmp, 0);
1078
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1079
and not (isspace(*tmp)))
1080
or tmpmax != (ifreq_flags)tmpmax){
1134
perror_plus("ioctl SIOCGIFFLAGS");
1082
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1083
flagstring, if_entry->d_name);
1141
bool good_flags(const char *ifname, const struct ifreq *ifr){
1089
ifreq_flags flags = (ifreq_flags)tmpmax;
1143
1090
/* Reject the loopback device */
1144
if(ifr->ifr_flags & IFF_LOOPBACK){
1091
if(flags & IFF_LOOPBACK){
1146
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1093
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1151
1098
/* Accept point-to-point devices only if connect_to is specified */
1152
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1099
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1154
fprintf_plus(stderr, "Accepting point-to-point interface"
1155
" \"%s\"\n", ifname);
1101
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1159
1106
/* 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",
1107
if(not (flags & IFF_BROADCAST)){
1109
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1176
1114
/* 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 */
1116
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1684
1122
int main(int argc, char *argv[]){
2010
1357
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
2012
perror_plus("sigaction");
1359
perror("sigaction");
2013
1360
return EX_OSERR;
2015
1362
if(old_sigterm_action.sa_handler != SIG_IGN){
2016
1363
ret = sigaction(SIGINT, &sigterm_action, NULL);
2018
perror_plus("sigaction");
1365
perror("sigaction");
2019
1366
exitcode = EX_OSERR;
2023
1370
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
2025
perror_plus("sigaction");
1372
perror("sigaction");
2026
1373
return EX_OSERR;
2028
1375
if(old_sigterm_action.sa_handler != SIG_IGN){
2029
1376
ret = sigaction(SIGHUP, &sigterm_action, NULL);
2031
perror_plus("sigaction");
1378
perror("sigaction");
2032
1379
exitcode = EX_OSERR;
2036
1383
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
2038
perror_plus("sigaction");
1385
perror("sigaction");
2039
1386
return EX_OSERR;
2041
1388
if(old_sigterm_action.sa_handler != SIG_IGN){
2042
1389
ret = sigaction(SIGTERM, &sigterm_action, NULL);
2044
perror_plus("sigaction");
1391
perror("sigaction");
2045
1392
exitcode = EX_OSERR;
2050
1397
/* If the interface is down, bring it up */
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");
1398
if(strcmp(interface, "none") != 0){
1399
if_index = (AvahiIfIndex) if_nametoindex(interface);
1401
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1402
exitcode = EX_UNAVAILABLE;
1410
/* Re-raise priviliges */
1418
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1419
messages about the network interface to mess up the prompt */
1420
ret = klogctl(8, NULL, 5);
1421
bool restore_loglevel = true;
1423
restore_loglevel = false;
1426
#endif /* __linux__ */
1428
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1431
exitcode = EX_OSERR;
1433
if(restore_loglevel){
1434
ret = klogctl(7, NULL, 0);
1439
#endif /* __linux__ */
1440
/* Lower privileges */
1448
strcpy(network.ifr_name, interface);
1449
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1451
perror("ioctl SIOCGIFFLAGS");
1453
if(restore_loglevel){
1454
ret = klogctl(7, NULL, 0);
1459
#endif /* __linux__ */
1460
exitcode = EX_OSERR;
1461
/* Lower privileges */
1469
if((network.ifr_flags & IFF_UP) == 0){
1470
network.ifr_flags |= IFF_UP;
1471
take_down_interface = true;
1472
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1474
take_down_interface = false;
1475
perror("ioctl SIOCSIFFLAGS +IFF_UP");
1476
exitcode = EX_OSERR;
1478
if(restore_loglevel){
1479
ret = klogctl(7, NULL, 0);
1484
#endif /* __linux__ */
1485
/* Lower privileges */
1494
/* sleep checking until interface is running */
1495
for(int i=0; i < delay * 4; i++){
1496
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1498
perror("ioctl SIOCGIFFLAGS");
1499
} else if(network.ifr_flags & IFF_RUNNING){
1502
struct timespec sleeptime = { .tv_nsec = 250000000 };
1503
ret = nanosleep(&sleeptime, NULL);
1504
if(ret == -1 and errno != EINTR){
1505
perror("nanosleep");
1508
if(not take_down_interface){
1509
/* We won't need the socket anymore */
1510
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1516
if(restore_loglevel){
1517
/* Restores kernel loglevel to default */
1518
ret = klogctl(7, NULL, 0);
1523
#endif /* __linux__ */
1524
/* Lower privileges */
1526
if(take_down_interface){
1527
/* Lower privileges */
1533
/* Lower privileges permanently */