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
234
235
if(new_server->ip == NULL){
235
236
perror_plus("strdup");
238
240
ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
240
242
perror_plus("clock_gettime");
244
#pragma GCC diagnostic push
245
#pragma GCC diagnostic ignored "-Wcast-qual"
247
free((char *)(new_server->ip));
249
#pragma GCC diagnostic pop
243
254
/* Special case of first server */
1349
char *fullname = NULL;
1350
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1352
perror_plus("asprintf");
1356
ret = stat(fullname, &st);
1361
ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
1359
1364
perror_plus("Could not stat hook");
1364
1368
if(not (S_ISREG(st.st_mode))){
1365
1369
/* Not a regular file */
1508
1509
error_t ret_errno = 0;
1509
1510
if(setuid(uid) == -1){
1510
1511
ret_errno = errno;
1511
perror_plus("setuid");
1513
1513
errno = old_errno;
1514
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 */
1536
1517
__attribute__((nonnull))
1537
1518
void run_network_hooks(const char *mode, const char *interface,
1538
1519
const float delay){
1539
struct dirent **direntries;
1520
struct dirent **direntries = NULL;
1540
1521
if(hookdir_fd == -1){
1541
hookdir_fd = open(hookdir, O_RDONLY |
1544
#else /* not O_CLOEXEC */
1546
#endif /* not O_CLOEXEC */
1522
hookdir_fd = open(hookdir, O_RDONLY);
1548
1523
if(hookdir_fd == -1){
1549
1524
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
1535
#ifdef __GLIBC__
1572
1536
#if __GLIBC_PREREQ(2, 15)
1670
1635
_exit(EX_OSERR);
1673
if(fexecve(hookdir_fd, (char *const [])
1674
{ direntry->d_name, NULL }, environ) == -1){
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 },
1675
1649
perror_plus("fexecve");
1676
1650
_exit(EXIT_FAILURE);
1654
perror_plus("fork");
1680
1659
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1681
1660
perror_plus("waitpid");
1684
1664
if(WIFEXITED(status)){
1686
1666
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1687
1667
" with status %d\n", direntry->d_name,
1688
1668
WEXITSTATUS(status));
1691
1672
} else if(WIFSIGNALED(status)){
1692
1673
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1693
1674
" signal %d\n", direntry->d_name,
1694
1675
WTERMSIG(status));
1697
1679
fprintf_plus(stderr, "Warning: network hook \"%s\""
1698
1680
" crashed\n", direntry->d_name);
2288
2275
/* If no interfaces were specified, make a list */
2289
2276
if(mc.interfaces == NULL){
2290
struct dirent **direntries;
2277
struct dirent **direntries = NULL;
2291
2278
/* Look for any good interfaces */
2292
2279
ret = scandir(sys_class_net, &direntries, good_interface,
2578
2569
mc.current_server->prev->next = NULL;
2579
2570
while(mc.current_server != NULL){
2580
2571
server *next = mc.current_server->next;
2573
#pragma GCC diagnostic push
2574
#pragma GCC diagnostic ignored "-Wcast-qual"
2576
free((char *)(mc.current_server->ip));
2578
#pragma GCC diagnostic pop
2581
2580
free(mc.current_server);
2582
2581
mc.current_server = next;
2625
2626
/* Removes the GPGME temp directory and all files inside */
2626
2627
if(tempdir != NULL){
2627
2628
struct dirent **direntries = NULL;
2628
struct dirent *direntry = NULL;
2629
int numentries = scandir(tempdir, &direntries, notdotentries,
2632
for(int i = 0; i < numentries; i++){
2633
direntry = direntries[i];
2634
char *fullname = NULL;
2635
ret = asprintf(&fullname, "%s/%s", tempdir,
2638
perror_plus("asprintf");
2641
ret = remove(fullname);
2643
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
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");
2650
/* need to clean even if 0 because man page doesn't specify */
2652
if(numentries == -1){
2653
perror_plus("scandir");
2655
ret = rmdir(tempdir);
2656
if(ret == -1 and errno != ENOENT){
2657
perror_plus("rmdir");
2667
TEMP_FAILURE_RETRY(close(tempdir_fd));