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
1402
pid_t hook_pid = fork();
1408
1403
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
1405
dup2(devnull, STDIN_FILENO);
1435
1406
close(devnull);
1436
1407
dup2(STDERR_FILENO, STDOUT_FILENO);
1437
1408
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1439
1410
perror_plus("setenv");
1442
1413
ret = setenv("DEVICE", interface, 1);
1444
1415
perror_plus("setenv");
1447
1418
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1449
1420
perror_plus("setenv");
1452
1423
ret = setenv("MODE", mode, 1);
1454
1425
perror_plus("setenv");
1457
1428
char *delaystring;
1458
1429
ret = asprintf(&delaystring, "%f", delay);
1460
1431
perror_plus("asprintf");
1463
1434
ret = setenv("DELAY", delaystring, 1);
1465
1436
free(delaystring);
1466
1437
perror_plus("setenv");
1469
1440
free(delaystring);
1470
1441
ret = execl(fullname, direntry->d_name, mode, NULL);
1707
1677
/* Work around Debian bug #633582:
1708
1678
<http://bugs.debian.org/633582> */
1710
1681
/* Re-raise priviliges */
1712
1683
ret = seteuid(0);
1714
1685
perror_plus("seteuid");
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));
1688
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1689
int seckey_fd = open(seckey, O_RDONLY);
1690
if(seckey_fd == -1){
1691
perror_plus("open");
1693
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1695
perror_plus("fstat");
1697
if(S_ISREG(st.st_mode)
1698
and st.st_uid == 0 and st.st_gid == 0){
1699
ret = fchown(seckey_fd, uid, gid);
1701
perror_plus("fchown");
1705
TEMP_FAILURE_RETRY(close(seckey_fd));
1709
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1710
int pubkey_fd = open(pubkey, O_RDONLY);
1711
if(pubkey_fd == -1){
1712
perror_plus("open");
1714
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1716
perror_plus("fstat");
1718
if(S_ISREG(st.st_mode)
1719
and st.st_uid == 0 and st.st_gid == 0){
1720
ret = fchown(pubkey_fd, uid, gid);
1722
perror_plus("fchown");
1726
TEMP_FAILURE_RETRY(close(pubkey_fd));
1730
/* Lower privileges */
1734
perror_plus("seteuid");
1738
/* Run network hooks */
1741
/* Re-raise priviliges */
1745
perror_plus("seteuid");
1748
if(not run_network_hooks("start", interface, delay)){
1760
1752
/* Lower privileges */
1762
1754
ret = seteuid(uid);
2228
/* Run network hooks */
2229
run_network_hooks("stop", interface, delay);
2231
2215
/* Re-raise priviliges */
2236
perror_plus("seteuid");
2221
perror_plus("seteuid");
2224
/* Run network hooks */
2225
if(not run_network_hooks("stop", interface, delay)){
2239
2229
/* Take down the network interface */