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() */
1406
1407
pid_t hook_pid = fork();
1407
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");
1409
1434
dup2(devnull, STDIN_FILENO);
1410
1435
close(devnull);
1411
1436
dup2(STDERR_FILENO, STDOUT_FILENO);
1412
1437
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1414
1439
perror_plus("setenv");
1417
1442
ret = setenv("DEVICE", interface, 1);
1419
1444
perror_plus("setenv");
1422
1447
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1424
1449
perror_plus("setenv");
1427
1452
ret = setenv("MODE", mode, 1);
1429
1454
perror_plus("setenv");
1432
1457
char *delaystring;
1433
1458
ret = asprintf(&delaystring, "%f", delay);
1435
1460
perror_plus("asprintf");
1438
1463
ret = setenv("DELAY", delaystring, 1);
1440
1465
free(delaystring);
1441
1466
perror_plus("setenv");
1444
1469
free(delaystring);
1445
1470
ret = execl(fullname, direntry->d_name, mode, NULL);
1682
1707
/* Work around Debian bug #633582:
1683
1708
<http://bugs.debian.org/633582> */
1686
1710
/* Re-raise priviliges */
1688
1712
ret = seteuid(0);
1690
1714
perror_plus("seteuid");
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)){
1718
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1719
int seckey_fd = open(seckey, O_RDONLY);
1720
if(seckey_fd == -1){
1721
perror_plus("open");
1723
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1725
perror_plus("fstat");
1727
if(S_ISREG(st.st_mode)
1728
and st.st_uid == 0 and st.st_gid == 0){
1729
ret = fchown(seckey_fd, uid, gid);
1731
perror_plus("fchown");
1735
TEMP_FAILURE_RETRY(close(seckey_fd));
1739
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1740
int pubkey_fd = open(pubkey, O_RDONLY);
1741
if(pubkey_fd == -1){
1742
perror_plus("open");
1744
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1746
perror_plus("fstat");
1748
if(S_ISREG(st.st_mode)
1749
and st.st_uid == 0 and st.st_gid == 0){
1750
ret = fchown(pubkey_fd, uid, gid);
1752
perror_plus("fchown");
1756
TEMP_FAILURE_RETRY(close(pubkey_fd));
1757
1760
/* Lower privileges */
1759
1762
ret = seteuid(uid);
2228
/* Run network hooks */
2229
run_network_hooks("stop", interface, delay);
2220
2231
/* Re-raise priviliges */
2226
perror_plus("seteuid");
2236
perror_plus("seteuid");
2230
/* Run network hooks */
2231
run_network_hooks("stop", interface, delay);
2233
2239
/* Take down the network interface */
2234
2240
if(take_down_interface and geteuid() == 0){
2235
2241
ret = ioctl(sd, SIOCGIFFLAGS, &network);