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
235
234
if(new_server->ip == NULL){
236
235
perror_plus("strdup");
240
238
ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
242
240
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
254
243
/* Special case of first server */
1509
1500
error_t ret_errno = 0;
1510
1501
if(setuid(uid) == -1){
1511
1502
ret_errno = errno;
1503
perror_plus("setuid");
1513
1505
errno = old_errno;
1514
1506
return ret_errno;
1511
* Based on the example in the GNU LibC manual chapter 13.13 "File
1512
* Descriptor Flags".
1513
| [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
1515
__attribute__((warn_unused_result))
1516
static int set_cloexec_flag(int fd){
1517
int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
1518
/* If reading the flags failed, return error indication now. */
1522
/* Store modified flag word in the descriptor. */
1523
return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
1526
#endif /* not O_CLOEXEC */
1517
1528
__attribute__((nonnull))
1518
1529
void run_network_hooks(const char *mode, const char *interface,
1519
1530
const float delay){
1520
struct dirent **direntries = NULL;
1531
struct dirent **direntries;
1521
1532
if(hookdir_fd == -1){
1522
hookdir_fd = open(hookdir, O_RDONLY);
1533
hookdir_fd = open(hookdir, O_RDONLY |
1536
#else /* not O_CLOEXEC */
1538
#endif /* not O_CLOEXEC */
1523
1540
if(hookdir_fd == -1){
1524
1541
if(errno == ENOENT){
1552
if(set_cloexec_flag(hookdir_fd) < 0){
1553
perror_plus("set_cloexec_flag");
1554
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1555
perror_plus("close");
1561
#endif /* not O_CLOEXEC */
1535
1563
#ifdef __GLIBC__
1536
1564
#if __GLIBC_PREREQ(2, 15)
1635
1662
_exit(EX_OSERR);
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 },
1665
if(fexecve(hookdir_fd, (char *const [])
1666
{ direntry->d_name, NULL }, environ) == -1){
1649
1667
perror_plus("fexecve");
1650
1668
_exit(EXIT_FAILURE);
1661
1678
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1662
1679
" with status %d\n", direntry->d_name,
1663
1680
WEXITSTATUS(status));
1667
1683
} else if(WIFSIGNALED(status)){
1668
1684
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1669
1685
" signal %d\n", direntry->d_name,
1670
1686
WTERMSIG(status));
1674
1689
fprintf_plus(stderr, "Warning: network hook \"%s\""
1675
1690
" crashed\n", direntry->d_name);
2270
2280
/* If no interfaces were specified, make a list */
2271
2281
if(mc.interfaces == NULL){
2272
struct dirent **direntries = NULL;
2282
struct dirent **direntries;
2273
2283
/* Look for any good interfaces */
2274
2284
ret = scandir(sys_class_net, &direntries, good_interface,
2564
2570
mc.current_server->prev->next = NULL;
2565
2571
while(mc.current_server != NULL){
2566
2572
server *next = mc.current_server->next;
2568
#pragma GCC diagnostic push
2569
#pragma GCC diagnostic ignored "-Wcast-qual"
2571
free((char *)(mc.current_server->ip));
2573
#pragma GCC diagnostic pop
2575
2573
free(mc.current_server);
2576
2574
mc.current_server = next;
2621
2617
/* Removes the GPGME temp directory and all files inside */
2622
2618
if(tempdir != NULL){
2623
2619
struct dirent **direntries = NULL;
2624
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2626
if(tempdir_fd == -1){
2627
perror_plus("open");
2630
#if __GLIBC_PREREQ(2, 15)
2631
int numentries = scandirat(tempdir_fd, ".", &direntries,
2632
notdotentries, alphasort);
2633
#else /* not __GLIBC_PREREQ(2, 15) */
2634
int numentries = scandir(tempdir, &direntries, notdotentries,
2636
#endif /* not __GLIBC_PREREQ(2, 15) */
2637
#else /* not __GLIBC__ */
2638
int numentries = scandir(tempdir, &direntries, notdotentries,
2640
#endif /* not __GLIBC__ */
2641
if(numentries >= 0){
2642
for(int i = 0; i < numentries; i++){
2643
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2645
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2646
" \"%s\", 0): %s\n", tempdir,
2647
direntries[i]->d_name, strerror(errno));
2649
free(direntries[i]);
2652
/* need to clean even if 0 because man page doesn't specify */
2654
if(numentries == -1){
2655
perror_plus("scandir");
2657
ret = rmdir(tempdir);
2658
if(ret == -1 and errno != ENOENT){
2659
perror_plus("rmdir");
2620
struct dirent *direntry = NULL;
2621
int numentries = scandir(tempdir, &direntries, notdotentries,
2624
for(int i = 0; i < numentries; i++){
2625
direntry = direntries[i];
2626
char *fullname = NULL;
2627
ret = asprintf(&fullname, "%s/%s", tempdir,
2630
perror_plus("asprintf");
2633
ret = remove(fullname);
2635
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2662
TEMP_FAILURE_RETRY(close(tempdir_fd));
2642
/* need to clean even if 0 because man page doesn't specify */
2644
if(numentries == -1){
2645
perror_plus("scandir");
2647
ret = rmdir(tempdir);
2648
if(ret == -1 and errno != ENOENT){
2649
perror_plus("rmdir");