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() */
93
92
#include <sys/klog.h> /* klogctl() */
1407
1406
pid_t hook_pid = fork();
1408
1407
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());
1436
1409
dup2(devnull, STDIN_FILENO);
1437
1410
close(devnull);
1438
1411
dup2(STDERR_FILENO, STDOUT_FILENO);
1439
1412
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1441
1414
perror_plus("setenv");
1444
1417
ret = setenv("DEVICE", interface, 1);
1446
1419
perror_plus("setenv");
1449
1422
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1451
1424
perror_plus("setenv");
1454
1427
ret = setenv("MODE", mode, 1);
1456
1429
perror_plus("setenv");
1459
1432
char *delaystring;
1460
1433
ret = asprintf(&delaystring, "%f", delay);
1462
1435
perror_plus("asprintf");
1465
1438
ret = setenv("DELAY", delaystring, 1);
1467
1440
free(delaystring);
1468
1441
perror_plus("setenv");
1471
1444
free(delaystring);
1472
1445
ret = execl(fullname, direntry->d_name, mode, NULL);
1709
1682
/* Work around Debian bug #633582:
1710
1683
<http://bugs.debian.org/633582> */
1712
1686
/* Re-raise priviliges */
1714
1688
ret = seteuid(0);
1716
1690
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));
1693
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1694
int seckey_fd = open(seckey, O_RDONLY);
1695
if(seckey_fd == -1){
1696
perror_plus("open");
1698
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1700
perror_plus("fstat");
1702
if(S_ISREG(st.st_mode)
1703
and st.st_uid == 0 and st.st_gid == 0){
1704
ret = fchown(seckey_fd, uid, gid);
1706
perror_plus("fchown");
1710
TEMP_FAILURE_RETRY(close(seckey_fd));
1714
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1715
int pubkey_fd = open(pubkey, O_RDONLY);
1716
if(pubkey_fd == -1){
1717
perror_plus("open");
1719
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1721
perror_plus("fstat");
1723
if(S_ISREG(st.st_mode)
1724
and st.st_uid == 0 and st.st_gid == 0){
1725
ret = fchown(pubkey_fd, uid, gid);
1727
perror_plus("fchown");
1731
TEMP_FAILURE_RETRY(close(pubkey_fd));
1735
/* Lower privileges */
1739
perror_plus("seteuid");
1743
/* Run network hooks */
1746
/* Re-raise priviliges */
1750
perror_plus("seteuid");
1753
if(not run_network_hooks("start", interface, delay)){
1762
1757
/* Lower privileges */
1764
1759
ret = seteuid(uid);
2230
/* Run network hooks */
2231
run_network_hooks("stop", interface, delay);
2233
2220
/* Re-raise priviliges */
2238
perror_plus("seteuid");
2226
perror_plus("seteuid");
2230
/* Run network hooks */
2231
run_network_hooks("stop", interface, delay);
2241
2233
/* Take down the network interface */
2242
2234
if(take_down_interface and geteuid() == 0){
2243
2235
ret = ioctl(sd, SIOCGIFFLAGS, &network);