1517
1513
__attribute__((nonnull))
1518
1514
void run_network_hooks(const char *mode, const char *interface,
1519
1515
const float delay){
1520
struct dirent **direntries = NULL;
1521
if(hookdir_fd == -1){
1522
hookdir_fd = open(hookdir, O_RDONLY);
1523
if(hookdir_fd == -1){
1524
if(errno == ENOENT){
1526
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1527
" found\n", hookdir);
1530
perror_plus("open");
1536
#if __GLIBC_PREREQ(2, 15)
1537
int numhooks = scandirat(hookdir_fd, ".", &direntries,
1538
runnable_hook, alphasort);
1539
#else /* not __GLIBC_PREREQ(2, 15) */
1540
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1542
#endif /* not __GLIBC_PREREQ(2, 15) */
1543
#else /* not __GLIBC__ */
1544
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1546
#endif /* not __GLIBC__ */
1516
struct dirent **direntries;
1517
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1547
1519
if(numhooks == -1){
1548
perror_plus("scandir");
1551
struct dirent *direntry;
1553
int devnull = open("/dev/null", O_RDONLY);
1554
for(int i = 0; i < numhooks; i++){
1555
direntry = direntries[i];
1557
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1520
if(errno == ENOENT){
1522
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1523
" found\n", hookdir);
1526
perror_plus("scandir");
1560
pid_t hook_pid = fork();
1563
/* Raise privileges */
1564
errno = raise_privileges_permanently();
1566
perror_plus("Failed to raise privileges");
1573
perror_plus("setgid");
1576
/* Reset supplementary groups */
1578
ret = setgroups(0, NULL);
1580
perror_plus("setgroups");
1583
ret = dup2(devnull, STDIN_FILENO);
1585
perror_plus("dup2(devnull, STDIN_FILENO)");
1588
ret = close(devnull);
1590
perror_plus("close");
1593
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1595
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1598
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1600
perror_plus("setenv");
1603
ret = setenv("DEVICE", interface, 1);
1605
perror_plus("setenv");
1608
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1610
perror_plus("setenv");
1613
ret = setenv("MODE", mode, 1);
1615
perror_plus("setenv");
1619
ret = asprintf(&delaystring, "%f", (double)delay);
1529
struct dirent *direntry;
1531
int devnull = open("/dev/null", O_RDONLY);
1532
for(int i = 0; i < numhooks; i++){
1533
direntry = direntries[i];
1534
char *fullname = NULL;
1535
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1621
1537
perror_plus("asprintf");
1624
ret = setenv("DELAY", delaystring, 1);
1541
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1544
pid_t hook_pid = fork();
1547
/* Raise privileges */
1548
if(raise_privileges_permanently() != 0){
1549
perror_plus("Failed to raise privileges");
1556
perror_plus("setgid");
1559
/* Reset supplementary groups */
1561
ret = setgroups(0, NULL);
1563
perror_plus("setgroups");
1566
ret = dup2(devnull, STDIN_FILENO);
1568
perror_plus("dup2(devnull, STDIN_FILENO)");
1571
ret = close(devnull);
1573
perror_plus("close");
1576
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1578
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1581
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1583
perror_plus("setenv");
1586
ret = setenv("DEVICE", interface, 1);
1588
perror_plus("setenv");
1591
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1593
perror_plus("setenv");
1596
ret = setenv("MODE", mode, 1);
1598
perror_plus("setenv");
1602
ret = asprintf(&delaystring, "%f", (double)delay);
1604
perror_plus("asprintf");
1607
ret = setenv("DELAY", delaystring, 1);
1610
perror_plus("setenv");
1626
1613
free(delaystring);
1627
perror_plus("setenv");
1631
if(connect_to != NULL){
1632
ret = setenv("CONNECT", connect_to, 1);
1634
perror_plus("setenv");
1638
int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1640
perror_plus("openat");
1641
_exit(EXIT_FAILURE);
1643
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1644
perror_plus("close");
1645
_exit(EXIT_FAILURE);
1647
if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1649
perror_plus("fexecve");
1650
_exit(EXIT_FAILURE);
1654
perror_plus("fork");
1659
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1660
perror_plus("waitpid");
1664
if(WIFEXITED(status)){
1665
if(WEXITSTATUS(status) != 0){
1666
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1667
" with status %d\n", direntry->d_name,
1668
WEXITSTATUS(status));
1672
} else if(WIFSIGNALED(status)){
1673
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1674
" signal %d\n", direntry->d_name,
1614
if(connect_to != NULL){
1615
ret = setenv("CONNECT", connect_to, 1);
1617
perror_plus("setenv");
1621
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1622
perror_plus("execl");
1623
_exit(EXIT_FAILURE);
1679
fprintf_plus(stderr, "Warning: network hook \"%s\""
1680
" crashed\n", direntry->d_name);
1686
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1692
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1693
perror_plus("close");
1627
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1628
perror_plus("waitpid");
1632
if(WIFEXITED(status)){
1633
if(WEXITSTATUS(status) != 0){
1634
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1635
" with status %d\n", direntry->d_name,
1636
WEXITSTATUS(status));
1640
} else if(WIFSIGNALED(status)){
1641
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1642
" signal %d\n", direntry->d_name,
1647
fprintf_plus(stderr, "Warning: network hook \"%s\""
1648
" crashed\n", direntry->d_name);
1655
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1700
1663
__attribute__((nonnull, warn_unused_result))
2624
2559
free(interfaces_hooks);
2626
2561
/* Removes the GPGME temp directory and all files inside */
2627
if(tempdir != NULL){
2562
if(tempdir_created){
2628
2563
struct dirent **direntries = NULL;
2629
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2631
if(tempdir_fd == -1){
2632
perror_plus("open");
2635
#if __GLIBC_PREREQ(2, 15)
2636
int numentries = scandirat(tempdir_fd, ".", &direntries,
2637
notdotentries, alphasort);
2638
#else /* not __GLIBC_PREREQ(2, 15) */
2639
int numentries = scandir(tempdir, &direntries, notdotentries,
2641
#endif /* not __GLIBC_PREREQ(2, 15) */
2642
#else /* not __GLIBC__ */
2643
int numentries = scandir(tempdir, &direntries, notdotentries,
2645
#endif /* not __GLIBC__ */
2646
if(numentries >= 0){
2647
for(int i = 0; i < numentries; i++){
2648
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2650
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2651
" \"%s\", 0): %s\n", tempdir,
2652
direntries[i]->d_name, strerror(errno));
2654
free(direntries[i]);
2657
/* need to clean even if 0 because man page doesn't specify */
2659
if(numentries == -1){
2660
perror_plus("scandir");
2662
ret = rmdir(tempdir);
2663
if(ret == -1 and errno != ENOENT){
2664
perror_plus("rmdir");
2564
struct dirent *direntry = NULL;
2565
int numentries = scandir(tempdir, &direntries, notdotentries,
2568
for(int i = 0; i < numentries; i++){
2569
direntry = direntries[i];
2570
char *fullname = NULL;
2571
ret = asprintf(&fullname, "%s/%s", tempdir,
2574
perror_plus("asprintf");
2577
ret = remove(fullname);
2579
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2667
TEMP_FAILURE_RETRY(close(tempdir_fd));
2586
/* need to clean even if 0 because man page doesn't specify */
2588
if(numentries == -1){
2589
perror_plus("scandir");
2591
ret = rmdir(tempdir);
2592
if(ret == -1 and errno != ENOENT){
2593
perror_plus("rmdir");