1413
bool run_network_hooks(const char *mode, const char *interface,
1415
struct dirent **direntries;
1416
struct dirent *direntry;
1418
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1421
perror_plus("scandir");
1423
int devnull = open("/dev/null", O_RDONLY);
1424
for(int i = 0; i < numhooks; i++){
1425
direntry = direntries[0];
1426
char *fullname = NULL;
1427
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1429
perror_plus("asprintf");
1432
pid_t hook_pid = fork();
1435
dup2(devnull, STDIN_FILENO);
1437
dup2(STDERR_FILENO, STDOUT_FILENO);
1438
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1440
perror_plus("setenv");
1443
ret = setenv("DEVICE", interface, 1);
1445
perror_plus("setenv");
1448
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1450
perror_plus("setenv");
1453
ret = setenv("MODE", mode, 1);
1455
perror_plus("setenv");
1459
ret = asprintf(&delaystring, "%f", delay);
1461
perror_plus("asprintf");
1464
ret = setenv("DELAY", delaystring, 1);
1467
perror_plus("setenv");
1471
ret = execl(fullname, direntry->d_name, mode, NULL);
1472
perror_plus("execl");
1475
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1476
perror_plus("waitpid");
1480
if(WIFEXITED(status)){
1481
if(WEXITSTATUS(status) != 0){
1482
fprintf(stderr, "Mandos plugin mandos-client: "
1483
"Warning: network hook \"%s\" exited"
1484
" with status %d\n", direntry->d_name,
1485
WEXITSTATUS(status));
1489
} else if(WIFSIGNALED(status)){
1490
fprintf(stderr, "Mandos plugin mandos-client: "
1491
"Warning: network hook \"%s\" died by"
1492
" signal %d\n", direntry->d_name,
1497
fprintf(stderr, "Mandos plugin mandos-client: "
1498
"Warning: network hook \"%s\" crashed\n",
1413
1514
int main(int argc, char *argv[]){
1414
1515
AvahiSServiceBrowser *sb = NULL;
1667
/* Find network hooks and run them */
1768
/* Run network hooks */
1669
struct dirent **direntries;
1670
struct dirent *direntry;
1671
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1674
perror_plus("scandir");
1676
int devnull = open("/dev/null", O_RDONLY);
1677
for(int i = 0; i < numhooks; i++){
1678
direntry = direntries[0];
1679
char *fullname = NULL;
1680
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1682
perror_plus("asprintf");
1685
pid_t hook_pid = fork();
1688
dup2(devnull, STDIN_FILENO);
1690
dup2(STDERR_FILENO, STDOUT_FILENO);
1691
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1693
perror_plus("setenv");
1696
ret = setenv("DEVICE", interface, 1);
1698
perror_plus("setenv");
1701
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1703
perror_plus("setenv");
1706
ret = setenv("MODE", "start", 1);
1708
perror_plus("setenv");
1712
ret = asprintf(&delaystring, "%f", delay);
1714
perror_plus("asprintf");
1717
ret = setenv("DELAY", delaystring, 1);
1720
perror_plus("setenv");
1724
ret = execl(fullname, direntry->d_name, "start", NULL);
1725
perror_plus("execl");
1728
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1729
perror_plus("waitpid");
1733
if(WIFEXITED(status)){
1734
if(WEXITSTATUS(status) != 0){
1735
fprintf(stderr, "Mandos plugin mandos-client: "
1736
"Warning: network hook \"%s\" exited"
1737
" with status %d\n", direntry->d_name,
1738
WEXITSTATUS(status));
1742
} else if(WIFSIGNALED(status)){
1743
fprintf(stderr, "Mandos plugin mandos-client: "
1744
"Warning: network hook \"%s\" died by"
1745
" signal %d\n", direntry->d_name,
1750
fprintf(stderr, "Mandos plugin mandos-client: "
1751
"Warning: network hook \"%s\" crashed\n",
1770
/* Re-raise priviliges */
1774
perror_plus("seteuid");
1776
if(not run_network_hooks("start", interface, delay)){
1779
/* Lower privileges */
1783
perror_plus("seteuid");
2240
/* XXX run network hooks "stop" here */
2242
/* Take down the network interface */
2243
if(take_down_interface){
2244
/* Re-raise priviliges */
2253
/* Re-raise priviliges */
2246
2256
ret = seteuid(0);
2248
2258
perror_plus("seteuid");
2260
/* Run network hooks */
2261
if(not run_network_hooks("stop", interface, delay)){
2265
/* Take down the network interface */
2266
if(take_down_interface and geteuid() == 0){
2251
2267
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2253
2269
perror_plus("ioctl SIOCGIFFLAGS");