40
40
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
42
42
#include <stdio.h> /* fprintf(), stderr, fwrite(),
43
stdout, ferror(), remove() */
44
44
#include <stdint.h> /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h> /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h> /* free(), EXIT_SUCCESS, srand(),
57
57
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
58
58
inet_pton(), connect(),
60
#include <fcntl.h> /* open(), unlinkat() */
60
#include <fcntl.h> /* open() */
61
61
#include <dirent.h> /* opendir(), struct dirent, readdir()
63
63
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
74
74
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
75
75
getuid(), getgid(), seteuid(),
76
setgid(), pause(), _exit(),
76
setgid(), pause(), _exit() */
78
77
#include <arpa/inet.h> /* inet_pton(), htons() */
79
78
#include <iso646.h> /* not, or, and */
80
79
#include <argp.h> /* struct argp_option, error_t, struct
1350
ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
1349
char *fullname = NULL;
1350
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1352
perror_plus("asprintf");
1356
ret = stat(fullname, &st);
1353
1359
perror_plus("Could not stat hook");
1357
1364
if(not (S_ISREG(st.st_mode))){
1358
1365
/* Not a regular file */
1507
1514
return ret_errno;
1519
* Based on the example in the GNU LibC manual chapter 13.13 "File
1520
* Descriptor Flags".
1521
| [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
1523
__attribute__((warn_unused_result))
1524
static int set_cloexec_flag(int fd){
1525
int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
1526
/* If reading the flags failed, return error indication now. */
1530
/* Store modified flag word in the descriptor. */
1531
return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
1534
#endif /* not O_CLOEXEC */
1510
1536
__attribute__((nonnull))
1511
1537
void run_network_hooks(const char *mode, const char *interface,
1512
1538
const float delay){
1513
1539
struct dirent **direntries;
1514
1540
if(hookdir_fd == -1){
1515
hookdir_fd = open(hookdir, O_RDONLY);
1541
hookdir_fd = open(hookdir, O_RDONLY |
1544
#else /* not O_CLOEXEC */
1546
#endif /* not O_CLOEXEC */
1516
1548
if(hookdir_fd == -1){
1517
1549
if(errno == ENOENT){
1560
if(set_cloexec_flag(hookdir_fd) < 0){
1561
perror_plus("set_cloexec_flag");
1562
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1563
perror_plus("close");
1569
#endif /* not O_CLOEXEC */
1528
1571
#ifdef __GLIBC__
1529
1572
#if __GLIBC_PREREQ(2, 15)
1546
1589
int devnull = open("/dev/null", O_RDONLY);
1547
1590
for(int i = 0; i < numhooks; i++){
1548
1591
direntry = direntries[i];
1592
char *fullname = NULL;
1593
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1595
perror_plus("asprintf");
1550
1599
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1551
1600
direntry->d_name);
1627
1676
_exit(EX_OSERR);
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");
1679
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1680
perror_plus("execl");
1642
1681
_exit(EXIT_FAILURE);
1646
1685
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1647
1686
perror_plus("waitpid");
1650
1690
if(WIFEXITED(status)){
1652
1692
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1653
1693
" with status %d\n", direntry->d_name,
1654
1694
WEXITSTATUS(status));
1657
1698
} else if(WIFSIGNALED(status)){
1658
1699
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1659
1700
" signal %d\n", direntry->d_name,
1660
1701
WTERMSIG(status));
1663
1705
fprintf_plus(stderr, "Warning: network hook \"%s\""
1664
1706
" crashed\n", direntry->d_name);
1669
1713
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1670
1714
direntry->d_name);
2254
2298
/* If no interfaces were specified, make a list */
2255
2299
if(mc.interfaces == NULL){
2256
struct dirent **direntries = NULL;
2300
struct dirent **direntries;
2257
2301
/* Look for any good interfaces */
2258
2302
ret = scandir(sys_class_net, &direntries, good_interface,
2591
2635
/* Removes the GPGME temp directory and all files inside */
2592
2636
if(tempdir != NULL){
2593
2637
struct dirent **direntries = NULL;
2594
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2596
if(tempdir_fd == -1){
2597
perror_plus("open");
2600
#if __GLIBC_PREREQ(2, 15)
2601
int numentries = scandirat(tempdir_fd, ".", &direntries,
2602
notdotentries, alphasort);
2603
#else /* not __GLIBC_PREREQ(2, 15) */
2604
int numentries = scandir(tempdir, &direntries, notdotentries,
2606
#endif /* not __GLIBC_PREREQ(2, 15) */
2607
#else /* not __GLIBC__ */
2608
int numentries = scandir(tempdir, &direntries, notdotentries,
2610
#endif /* not __GLIBC__ */
2612
for(int i = 0; i < numentries; i++){
2613
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2615
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2616
" \"%s\", 0): %s\n", tempdir,
2617
direntries[i]->d_name, strerror(errno));
2621
/* need to clean even if 0 because man page doesn't specify */
2623
if(numentries == -1){
2624
perror_plus("scandir");
2626
ret = rmdir(tempdir);
2627
if(ret == -1 and errno != ENOENT){
2628
perror_plus("rmdir");
2638
struct dirent *direntry = NULL;
2639
int numentries = scandir(tempdir, &direntries, notdotentries,
2642
for(int i = 0; i < numentries; i++){
2643
direntry = direntries[i];
2644
char *fullname = NULL;
2645
ret = asprintf(&fullname, "%s/%s", tempdir,
2648
perror_plus("asprintf");
2651
ret = remove(fullname);
2653
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2631
TEMP_FAILURE_RETRY(close(tempdir_fd));
2660
/* need to clean even if 0 because man page doesn't specify */
2662
if(numentries == -1){
2663
perror_plus("scandir");
2665
ret = rmdir(tempdir);
2666
if(ret == -1 and errno != ENOENT){
2667
perror_plus("rmdir");