1510
1515
__attribute__((nonnull))
1511
1516
void run_network_hooks(const char *mode, const char *interface,
1512
1517
const float delay){
1513
struct dirent **direntries = NULL;
1514
if(hookdir_fd == -1){
1515
hookdir_fd = open(hookdir, O_RDONLY);
1516
if(hookdir_fd == -1){
1517
if(errno == ENOENT){
1519
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1520
" found\n", hookdir);
1523
perror_plus("open");
1529
#if __GLIBC_PREREQ(2, 15)
1530
int numhooks = scandirat(hookdir_fd, ".", &direntries,
1531
runnable_hook, alphasort);
1532
#else /* not __GLIBC_PREREQ(2, 15) */
1533
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1535
#endif /* not __GLIBC_PREREQ(2, 15) */
1536
#else /* not __GLIBC__ */
1537
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1539
#endif /* not __GLIBC__ */
1518
struct dirent **direntries;
1519
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1540
1521
if(numhooks == -1){
1541
perror_plus("scandir");
1544
struct dirent *direntry;
1546
int devnull = open("/dev/null", O_RDONLY);
1547
for(int i = 0; i < numhooks; i++){
1548
direntry = direntries[i];
1550
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1522
if(errno == ENOENT){
1524
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1525
" found\n", hookdir);
1528
perror_plus("scandir");
1553
pid_t hook_pid = fork();
1556
/* Raise privileges */
1557
if(raise_privileges_permanently() != 0){
1558
perror_plus("Failed to raise privileges");
1565
perror_plus("setgid");
1568
/* Reset supplementary groups */
1570
ret = setgroups(0, NULL);
1572
perror_plus("setgroups");
1575
ret = dup2(devnull, STDIN_FILENO);
1577
perror_plus("dup2(devnull, STDIN_FILENO)");
1580
ret = close(devnull);
1582
perror_plus("close");
1585
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1587
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1590
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1592
perror_plus("setenv");
1595
ret = setenv("DEVICE", interface, 1);
1597
perror_plus("setenv");
1600
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1602
perror_plus("setenv");
1605
ret = setenv("MODE", mode, 1);
1607
perror_plus("setenv");
1611
ret = asprintf(&delaystring, "%f", (double)delay);
1531
struct dirent *direntry;
1533
int devnull = open("/dev/null", O_RDONLY);
1534
for(int i = 0; i < numhooks; i++){
1535
direntry = direntries[i];
1536
char *fullname = NULL;
1537
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1613
1539
perror_plus("asprintf");
1616
ret = setenv("DELAY", delaystring, 1);
1543
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1546
pid_t hook_pid = fork();
1549
/* Raise privileges */
1550
if(raise_privileges_permanently() != 0){
1551
perror_plus("Failed to raise privileges");
1558
perror_plus("setgid");
1561
/* Reset supplementary groups */
1563
ret = setgroups(0, NULL);
1565
perror_plus("setgroups");
1568
ret = dup2(devnull, STDIN_FILENO);
1570
perror_plus("dup2(devnull, STDIN_FILENO)");
1573
ret = close(devnull);
1575
perror_plus("close");
1578
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1580
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1583
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1585
perror_plus("setenv");
1588
ret = setenv("DEVICE", interface, 1);
1590
perror_plus("setenv");
1593
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1595
perror_plus("setenv");
1598
ret = setenv("MODE", mode, 1);
1600
perror_plus("setenv");
1604
ret = asprintf(&delaystring, "%f", (double)delay);
1606
perror_plus("asprintf");
1609
ret = setenv("DELAY", delaystring, 1);
1612
perror_plus("setenv");
1618
1615
free(delaystring);
1619
perror_plus("setenv");
1623
if(connect_to != NULL){
1624
ret = setenv("CONNECT", connect_to, 1);
1626
perror_plus("setenv");
1630
int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1632
perror_plus("openat");
1633
_exit(EXIT_FAILURE);
1635
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1636
perror_plus("close");
1637
_exit(EXIT_FAILURE);
1639
if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1641
perror_plus("fexecve");
1642
_exit(EXIT_FAILURE);
1646
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1647
perror_plus("waitpid");
1650
if(WIFEXITED(status)){
1651
if(WEXITSTATUS(status) != 0){
1652
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1653
" with status %d\n", direntry->d_name,
1654
WEXITSTATUS(status));
1657
} else if(WIFSIGNALED(status)){
1658
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1659
" signal %d\n", direntry->d_name,
1616
if(connect_to != NULL){
1617
ret = setenv("CONNECT", connect_to, 1);
1619
perror_plus("setenv");
1623
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1624
perror_plus("execl");
1625
_exit(EXIT_FAILURE);
1663
fprintf_plus(stderr, "Warning: network hook \"%s\""
1664
" crashed\n", direntry->d_name);
1669
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1674
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1675
perror_plus("close");
1629
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1630
perror_plus("waitpid");
1634
if(WIFEXITED(status)){
1635
if(WEXITSTATUS(status) != 0){
1636
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1637
" with status %d\n", direntry->d_name,
1638
WEXITSTATUS(status));
1642
} else if(WIFSIGNALED(status)){
1643
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1644
" signal %d\n", direntry->d_name,
1649
fprintf_plus(stderr, "Warning: network hook \"%s\""
1650
" crashed\n", direntry->d_name);
1657
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1682
1665
__attribute__((nonnull, warn_unused_result))
2594
2575
/* Removes the GPGME temp directory and all files inside */
2595
2576
if(tempdir != NULL){
2596
2577
struct dirent **direntries = NULL;
2597
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2599
if(tempdir_fd == -1){
2600
perror_plus("open");
2603
#if __GLIBC_PREREQ(2, 15)
2604
int numentries = scandirat(tempdir_fd, ".", &direntries,
2605
notdotentries, alphasort);
2606
#else /* not __GLIBC_PREREQ(2, 15) */
2607
int numentries = scandir(tempdir, &direntries, notdotentries,
2609
#endif /* not __GLIBC_PREREQ(2, 15) */
2610
#else /* not __GLIBC__ */
2611
int numentries = scandir(tempdir, &direntries, notdotentries,
2613
#endif /* not __GLIBC__ */
2614
if(numentries >= 0){
2615
for(int i = 0; i < numentries; i++){
2616
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2618
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2619
" \"%s\", 0): %s\n", tempdir,
2620
direntries[i]->d_name, strerror(errno));
2624
/* need to clean even if 0 because man page doesn't specify */
2626
if(numentries == -1){
2627
perror_plus("scandir");
2629
ret = rmdir(tempdir);
2630
if(ret == -1 and errno != ENOENT){
2631
perror_plus("rmdir");
2578
struct dirent *direntry = NULL;
2579
int numentries = scandir(tempdir, &direntries, notdotentries,
2582
for(int i = 0; i < numentries; i++){
2583
direntry = direntries[i];
2584
char *fullname = NULL;
2585
ret = asprintf(&fullname, "%s/%s", tempdir,
2588
perror_plus("asprintf");
2591
ret = remove(fullname);
2593
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2634
TEMP_FAILURE_RETRY(close(tempdir_fd));
2600
/* need to clean even if 0 because man page doesn't specify */
2602
if(numentries == -1){
2603
perror_plus("scandir");
2605
ret = rmdir(tempdir);
2606
if(ret == -1 and errno != ENOENT){
2607
perror_plus("rmdir");