236
224
perror_plus("strdup");
239
ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
241
perror_plus("clock_gettime");
244
227
/* Special case of first server */
245
228
if(*current_server == NULL){
246
229
new_server->next = new_server;
247
230
new_server->prev = new_server;
248
231
*current_server = new_server;
232
/* Place the new server last in the list */
250
/* Place the new server last in the list */
251
234
new_server->next = *current_server;
252
235
new_server->prev = (*current_server)->prev;
253
236
new_server->prev->next = new_server;
254
237
(*current_server)->prev = new_server;
239
ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
241
perror_plus("clock_gettime");
260
248
* Initialize GPGME.
262
__attribute__((nonnull, warn_unused_result))
263
static bool init_gpgme(const char * const seckey,
264
const char * const pubkey,
265
const char * const tempdir,
250
static bool init_gpgme(const char *seckey, const char *pubkey,
251
const char *tempdir, mandos_context *mc){
267
252
gpgme_error_t rc;
268
253
gpgme_engine_info_t engine_info;
271
256
* Helper function to insert pub and seckey to the engine keyring.
273
bool import_key(const char * const filename){
258
bool import_key(const char *filename){
276
261
gpgme_data_t pgp_data;
790
775
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
791
776
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
792
778
if(af == AF_INET6){
793
ret = getnameinfo((struct sockaddr *)&to,
794
sizeof(struct sockaddr_in6),
795
addrstr, sizeof(addrstr), NULL, 0,
779
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
798
ret = getnameinfo((struct sockaddr *)&to,
799
sizeof(struct sockaddr_in),
800
addrstr, sizeof(addrstr), NULL, 0,
782
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
803
if(ret == EAI_SYSTEM){
804
perror_plus("getnameinfo");
805
} else if(ret != 0) {
806
fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
807
} else if(strcmp(addrstr, ip) != 0){
808
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
786
perror_plus("inet_ntop");
788
if(strcmp(addrstr, ip) != 0){
789
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
1507
1481
return ret_errno;
1512
* Based on the example in the GNU LibC manual chapter 13.13 "File
1513
* Descriptor Flags".
1514
| [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
1516
__attribute__((warn_unused_result))
1517
static int set_cloexec_flag(int fd){
1518
int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
1519
/* If reading the flags failed, return error indication now. */
1523
/* Store modified flag word in the descriptor. */
1524
return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
1527
#endif /* not O_CLOEXEC */
1529
__attribute__((nonnull))
1530
void run_network_hooks(const char *mode, const char *interface,
1484
bool run_network_hooks(const char *mode, const char *interface,
1531
1485
const float delay){
1532
1486
struct dirent **direntries;
1533
if(hookdir_fd == -1){
1534
hookdir_fd = open(hookdir, O_RDONLY |
1537
#else /* not O_CLOEXEC */
1539
#endif /* not O_CLOEXEC */
1541
if(hookdir_fd == -1){
1542
if(errno == ENOENT){
1544
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1545
" found\n", hookdir);
1548
perror_plus("open");
1553
if(set_cloexec_flag(hookdir_fd) < 0){
1554
perror_plus("set_cloexec_flag");
1555
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1556
perror_plus("close");
1562
#endif /* not O_CLOEXEC */
1565
#if __GLIBC_PREREQ(2, 15)
1566
int numhooks = scandirat(hookdir_fd, ".", &direntries,
1567
runnable_hook, alphasort);
1568
#else /* not __GLIBC_PREREQ(2, 15) */
1569
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1571
#endif /* not __GLIBC_PREREQ(2, 15) */
1572
#else /* not __GLIBC__ */
1573
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1575
#endif /* not __GLIBC__ */
1487
struct dirent *direntry;
1489
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1576
1491
if(numhooks == -1){
1577
perror_plus("scandir");
1580
struct dirent *direntry;
1582
int devnull = open("/dev/null", O_RDONLY);
1583
for(int i = 0; i < numhooks; i++){
1584
direntry = direntries[i];
1586
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1492
if(errno == ENOENT){
1494
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1495
" found\n", hookdir);
1498
perror_plus("scandir");
1589
pid_t hook_pid = fork();
1592
/* Raise privileges */
1593
if(raise_privileges_permanently() != 0){
1594
perror_plus("Failed to raise privileges");
1601
perror_plus("setgid");
1604
/* Reset supplementary groups */
1606
ret = setgroups(0, NULL);
1608
perror_plus("setgroups");
1611
ret = dup2(devnull, STDIN_FILENO);
1613
perror_plus("dup2(devnull, STDIN_FILENO)");
1616
ret = close(devnull);
1618
perror_plus("close");
1621
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1623
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1626
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1628
perror_plus("setenv");
1631
ret = setenv("DEVICE", interface, 1);
1633
perror_plus("setenv");
1636
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1638
perror_plus("setenv");
1641
ret = setenv("MODE", mode, 1);
1643
perror_plus("setenv");
1647
ret = asprintf(&delaystring, "%f", (double)delay);
1501
int devnull = open("/dev/null", O_RDONLY);
1502
for(int i = 0; i < numhooks; i++){
1503
direntry = direntries[i];
1504
char *fullname = NULL;
1505
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1649
1507
perror_plus("asprintf");
1652
ret = setenv("DELAY", delaystring, 1);
1511
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1514
pid_t hook_pid = fork();
1517
/* Raise privileges */
1518
raise_privileges_permanently();
1523
perror_plus("setgid");
1525
/* Reset supplementary groups */
1527
ret = setgroups(0, NULL);
1529
perror_plus("setgroups");
1531
dup2(devnull, STDIN_FILENO);
1533
dup2(STDERR_FILENO, STDOUT_FILENO);
1534
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1536
perror_plus("setenv");
1539
ret = setenv("DEVICE", interface, 1);
1541
perror_plus("setenv");
1544
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1546
perror_plus("setenv");
1549
ret = setenv("MODE", mode, 1);
1551
perror_plus("setenv");
1555
ret = asprintf(&delaystring, "%f", delay);
1557
perror_plus("asprintf");
1560
ret = setenv("DELAY", delaystring, 1);
1563
perror_plus("setenv");
1654
1566
free(delaystring);
1655
perror_plus("setenv");
1659
if(connect_to != NULL){
1660
ret = setenv("CONNECT", connect_to, 1);
1662
perror_plus("setenv");
1666
if(fexecve(hookdir_fd, (char *const [])
1667
{ direntry->d_name, NULL }, environ) == -1){
1668
perror_plus("fexecve");
1669
_exit(EXIT_FAILURE);
1673
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1674
perror_plus("waitpid");
1677
if(WIFEXITED(status)){
1678
if(WEXITSTATUS(status) != 0){
1679
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1680
" with status %d\n", direntry->d_name,
1681
WEXITSTATUS(status));
1684
} else if(WIFSIGNALED(status)){
1685
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1686
" signal %d\n", direntry->d_name,
1567
if(connect_to != NULL){
1568
ret = setenv("CONNECT", connect_to, 1);
1570
perror_plus("setenv");
1574
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1575
perror_plus("execl");
1576
_exit(EXIT_FAILURE);
1690
fprintf_plus(stderr, "Warning: network hook \"%s\""
1691
" crashed\n", direntry->d_name);
1696
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1700
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1701
perror_plus("close");
1580
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1581
perror_plus("waitpid");
1585
if(WIFEXITED(status)){
1586
if(WEXITSTATUS(status) != 0){
1587
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1588
" with status %d\n", direntry->d_name,
1589
WEXITSTATUS(status));
1593
} else if(WIFSIGNALED(status)){
1594
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1595
" signal %d\n", direntry->d_name,
1600
fprintf_plus(stderr, "Warning: network hook \"%s\""
1601
" crashed\n", direntry->d_name);
1608
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1708
__attribute__((nonnull, warn_unused_result))
1709
1617
error_t bring_up_interface(const char *const interface,
1710
1618
const float delay){
1711
1620
error_t old_errno = errno;
1621
error_t ret_errno = 0;
1622
int ret, ret_setflags;
1713
1623
struct ifreq network;
1714
1624
unsigned int if_index = if_nametoindex(interface);
1715
1625
if(if_index == 0){
2579
/* Re-raise privileges */
2445
/* Re-raise priviliges */
2581
ret_errno = raise_privileges();
2583
perror_plus("Failed to raise privileges");
2586
/* Run network hooks */
2587
run_network_hooks("stop", interfaces_hooks != NULL ?
2588
interfaces_hooks : "", delay);
2590
/* Take down the network interfaces which were brought up */
2592
char *interface = NULL;
2593
while((interface=argz_next(interfaces_to_take_down,
2594
interfaces_to_take_down_size,
2596
ret_errno = take_down_interface(interface);
2599
perror_plus("Failed to take down interface");
2602
if(debug and (interfaces_to_take_down == NULL)){
2603
fprintf_plus(stderr, "No interfaces needed to be taken"
2609
ret_errno = lower_privileges_permanently();
2611
perror_plus("Failed to lower privileges permanently");
2449
/* Run network hooks */
2450
run_network_hooks("stop", interfaces_hooks != NULL ?
2451
interfaces_hooks : "", delay);
2453
/* Take down the network interfaces which were brought up */
2455
char *interface = NULL;
2456
while((interface=argz_next(interfaces_to_take_down,
2457
interfaces_to_take_down_size,
2459
ret_errno = take_down_interface(interface);
2462
perror_plus("Failed to take down interface");
2465
if(debug and (interfaces_to_take_down == NULL)){
2466
fprintf_plus(stderr, "No interfaces needed to be taken"
2471
lower_privileges_permanently();
2615
2474
free(interfaces_to_take_down);
2616
2475
free(interfaces_hooks);
2618
2477
/* Removes the GPGME temp directory and all files inside */
2619
if(tempdir != NULL){
2478
if(tempdir_created){
2620
2479
struct dirent **direntries = NULL;
2621
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY));
2622
if(tempdir_fd == -1){
2623
perror_plus("open");
2625
int numentries = scandir(tempdir, &direntries, notdotentries,
2628
for(int i = 0; i < numentries; i++){
2629
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2631
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2632
" \"%s\", 0): %s\n", tempdir,
2633
direntries[i]->d_name, strerror(errno));
2637
/* need to clean even if 0 because man page doesn't specify */
2639
if(numentries == -1){
2640
perror_plus("scandir");
2642
ret = rmdir(tempdir);
2643
if(ret == -1 and errno != ENOENT){
2644
perror_plus("rmdir");
2480
struct dirent *direntry = NULL;
2481
int numentries = scandir(tempdir, &direntries, notdotentries,
2483
if (numentries > 0){
2484
for(int i = 0; i < numentries; i++){
2485
direntry = direntries[i];
2486
char *fullname = NULL;
2487
ret = asprintf(&fullname, "%s/%s", tempdir,
2490
perror_plus("asprintf");
2493
ret = remove(fullname);
2495
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2647
TEMP_FAILURE_RETRY(close(tempdir_fd));
2502
/* need to clean even if 0 because man page doesn't specify */
2504
if (numentries == -1){
2505
perror_plus("scandir");
2507
ret = rmdir(tempdir);
2508
if(ret == -1 and errno != ENOENT){
2509
perror_plus("rmdir");