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() */
60
#include <fcntl.h> /* open(), unlinkat() */
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(),
77
78
#include <arpa/inet.h> /* inet_pton(), htons() */
78
79
#include <iso646.h> /* not, or, and */
79
80
#include <argp.h> /* struct argp_option, error_t, struct
1349
char *fullname = NULL;
1350
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1352
perror_plus("asprintf");
1356
ret = stat(fullname, &st);
1350
ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
1359
1353
perror_plus("Could not stat hook");
1364
1357
if(not (S_ISREG(st.st_mode))){
1365
1358
/* Not a regular file */
1508
1498
error_t ret_errno = 0;
1509
1499
if(setuid(uid) == -1){
1510
1500
ret_errno = errno;
1511
perror_plus("setuid");
1513
1502
errno = old_errno;
1514
1503
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 */
1536
1506
__attribute__((nonnull))
1537
1507
void run_network_hooks(const char *mode, const char *interface,
1538
1508
const float delay){
1539
struct dirent **direntries;
1509
struct dirent **direntries = NULL;
1540
1510
if(hookdir_fd == -1){
1541
hookdir_fd = open(hookdir, O_RDONLY |
1544
#else /* not O_CLOEXEC */
1546
#endif /* not O_CLOEXEC */
1511
hookdir_fd = open(hookdir, O_RDONLY);
1548
1512
if(hookdir_fd == -1){
1549
1513
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 */
1571
1524
#ifdef __GLIBC__
1572
1525
#if __GLIBC_PREREQ(2, 15)
1589
1542
int devnull = open("/dev/null", O_RDONLY);
1590
1543
for(int i = 0; i < numhooks; i++){
1591
1544
direntry = direntries[i];
1592
char *fullname = NULL;
1593
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1595
perror_plus("asprintf");
1599
1546
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1600
1547
direntry->d_name);
1676
1624
_exit(EX_OSERR);
1679
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1680
perror_plus("execl");
1627
int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1629
perror_plus("openat");
1630
_exit(EXIT_FAILURE);
1632
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1633
perror_plus("close");
1634
_exit(EXIT_FAILURE);
1636
if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1638
perror_plus("fexecve");
1681
1639
_exit(EXIT_FAILURE);
1685
1643
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1686
1644
perror_plus("waitpid");
1690
1647
if(WIFEXITED(status)){
1692
1649
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1693
1650
" with status %d\n", direntry->d_name,
1694
1651
WEXITSTATUS(status));
1698
1654
} else if(WIFSIGNALED(status)){
1699
1655
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1700
1656
" signal %d\n", direntry->d_name,
1701
1657
WTERMSIG(status));
1705
1660
fprintf_plus(stderr, "Warning: network hook \"%s\""
1706
1661
" crashed\n", direntry->d_name);
1713
1666
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1714
1667
direntry->d_name);
1717
1671
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1718
1672
perror_plus("close");
2298
2254
/* If no interfaces were specified, make a list */
2299
2255
if(mc.interfaces == NULL){
2300
struct dirent **direntries;
2256
struct dirent **direntries = NULL;
2301
2257
/* Look for any good interfaces */
2302
2258
ret = scandir(sys_class_net, &direntries, good_interface,
2635
2595
/* Removes the GPGME temp directory and all files inside */
2636
2596
if(tempdir != NULL){
2637
2597
struct dirent **direntries = NULL;
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,
2598
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2600
if(tempdir_fd == -1){
2601
perror_plus("open");
2604
#if __GLIBC_PREREQ(2, 15)
2605
int numentries = scandirat(tempdir_fd, ".", &direntries,
2606
notdotentries, alphasort);
2607
#else /* not __GLIBC_PREREQ(2, 15) */
2608
int numentries = scandir(tempdir, &direntries, notdotentries,
2610
#endif /* not __GLIBC_PREREQ(2, 15) */
2611
#else /* not __GLIBC__ */
2612
int numentries = scandir(tempdir, &direntries, notdotentries,
2614
#endif /* not __GLIBC__ */
2615
if(numentries >= 0){
2616
for(int i = 0; i < numentries; i++){
2617
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2619
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2620
" \"%s\", 0): %s\n", tempdir,
2621
direntries[i]->d_name, strerror(errno));
2625
/* need to clean even if 0 because man page doesn't specify */
2627
if(numentries == -1){
2628
perror_plus("scandir");
2630
ret = rmdir(tempdir);
2631
if(ret == -1 and errno != ENOENT){
2632
perror_plus("rmdir");
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");
2635
TEMP_FAILURE_RETRY(close(tempdir_fd));