74
74
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
75
75
getuid(), getgid(), seteuid(),
76
setgid(), pause(), _exit() */
77
77
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
78
78
#include <iso646.h> /* not, or, and */
79
79
#include <argp.h> /* struct argp_option, error_t, struct
87
87
EX_NOHOST, EX_IOERR, EX_PROTOCOL */
88
88
#include <sys/wait.h> /* waitpid(), WIFEXITED(),
89
89
WEXITSTATUS(), WTERMSIG() */
90
#include <grp.h> /* setgroups() */
92
93
#include <sys/klog.h> /* klogctl() */
1389
1394
int devnull = open("/dev/null", O_RDONLY);
1390
1395
for(int i = 0; i < numhooks; i++){
1391
direntry = direntries[0];
1396
direntry = direntries[i];
1392
1397
char *fullname = NULL;
1393
1398
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1395
1400
perror_plus("asprintf");
1404
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1398
1407
pid_t hook_pid = fork();
1399
1408
if(hook_pid == 0){
1410
/* Raise privileges */
1414
perror_plus("seteuid");
1416
/* Raise privileges even more */
1420
perror_plus("setuid");
1426
perror_plus("setgid");
1428
/* Reset supplementary groups */
1430
ret = setgroups(0, NULL);
1432
perror_plus("setgroups");
1434
fprintf_plus(stderr, "Child: getuid() = %d\n", getuid());
1435
fprintf_plus(stderr, "Child: geteuid() = %d\n", geteuid());
1401
1436
dup2(devnull, STDIN_FILENO);
1402
1437
close(devnull);
1403
1438
dup2(STDERR_FILENO, STDOUT_FILENO);
1404
1439
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1406
1441
perror_plus("setenv");
1409
1444
ret = setenv("DEVICE", interface, 1);
1411
1446
perror_plus("setenv");
1414
1449
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1416
1451
perror_plus("setenv");
1419
1454
ret = setenv("MODE", mode, 1);
1421
1456
perror_plus("setenv");
1424
1459
char *delaystring;
1425
1460
ret = asprintf(&delaystring, "%f", delay);
1427
1462
perror_plus("asprintf");
1430
1465
ret = setenv("DELAY", delaystring, 1);
1432
1467
free(delaystring);
1433
1468
perror_plus("setenv");
1436
1471
free(delaystring);
1437
1472
ret = execl(fullname, direntry->d_name, mode, NULL);
1673
1709
/* Work around Debian bug #633582:
1674
1710
<http://bugs.debian.org/633582> */
1677
1712
/* Re-raise priviliges */
1679
1714
ret = seteuid(0);
1681
1716
perror_plus("seteuid");
1684
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1685
int seckey_fd = open(seckey, O_RDONLY);
1686
if(seckey_fd == -1){
1687
perror_plus("open");
1689
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1691
perror_plus("fstat");
1693
if(S_ISREG(st.st_mode)
1694
and st.st_uid == 0 and st.st_gid == 0){
1695
ret = fchown(seckey_fd, uid, gid);
1697
perror_plus("fchown");
1701
TEMP_FAILURE_RETRY(close(seckey_fd));
1705
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1706
int pubkey_fd = open(pubkey, O_RDONLY);
1707
if(pubkey_fd == -1){
1708
perror_plus("open");
1710
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1712
perror_plus("fstat");
1714
if(S_ISREG(st.st_mode)
1715
and st.st_uid == 0 and st.st_gid == 0){
1716
ret = fchown(pubkey_fd, uid, gid);
1718
perror_plus("fchown");
1722
TEMP_FAILURE_RETRY(close(pubkey_fd));
1726
/* Lower privileges */
1730
perror_plus("seteuid");
1720
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1721
int seckey_fd = open(seckey, O_RDONLY);
1722
if(seckey_fd == -1){
1723
perror_plus("open");
1725
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1727
perror_plus("fstat");
1729
if(S_ISREG(st.st_mode)
1730
and st.st_uid == 0 and st.st_gid == 0){
1731
ret = fchown(seckey_fd, uid, gid);
1733
perror_plus("fchown");
1737
TEMP_FAILURE_RETRY(close(seckey_fd));
1741
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1742
int pubkey_fd = open(pubkey, O_RDONLY);
1743
if(pubkey_fd == -1){
1744
perror_plus("open");
1746
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1748
perror_plus("fstat");
1750
if(S_ISREG(st.st_mode)
1751
and st.st_uid == 0 and st.st_gid == 0){
1752
ret = fchown(pubkey_fd, uid, gid);
1754
perror_plus("fchown");
1758
TEMP_FAILURE_RETRY(close(pubkey_fd));
1762
/* Lower privileges */
1766
perror_plus("seteuid");
1734
1771
/* Run network hooks */
1736
/* Re-raise priviliges */
1740
perror_plus("seteuid");
1742
if(not run_network_hooks("start", interface, delay)){
1745
/* Lower privileges */
1749
perror_plus("seteuid");
1772
if(not run_network_hooks("start", interface, delay)){