26
26
* along with this program. If not, see
27
27
* <http://www.gnu.org/licenses/>.
29
* Contact the authors at <mandos@fukt.bsnet.se>.
29
* Contact the authors at <mandos@recompile.se>.
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
#ifndef _LARGEFILE_SOURCE
33
34
#define _LARGEFILE_SOURCE
36
#ifndef _FILE_OFFSET_BITS
34
37
#define _FILE_OFFSET_BITS 64
36
40
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
38
42
#include <stdio.h> /* fprintf(), stderr, fwrite(),
39
stdout, ferror(), sscanf(),
43
stdout, ferror(), remove() */
41
44
#include <stdint.h> /* uint16_t, uint32_t */
42
45
#include <stddef.h> /* NULL, size_t, ssize_t */
43
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
45
#include <stdbool.h> /* bool, true */
46
#include <stdlib.h> /* free(), EXIT_SUCCESS, srand(),
48
#include <stdbool.h> /* bool, false, true */
46
49
#include <string.h> /* memset(), strcmp(), strlen(),
47
50
strerror(), asprintf(), strcpy() */
48
#include <sys/ioctl.h> /* ioctl */
51
#include <sys/ioctl.h> /* ioctl */
49
52
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
50
53
sockaddr_in6, PF_INET6,
51
SOCK_STREAM, INET6_ADDRSTRLEN,
52
uid_t, gid_t, open(), opendir(),
54
#include <sys/stat.h> /* open() */
54
SOCK_STREAM, uid_t, gid_t, open(),
56
#include <sys/stat.h> /* open(), S_ISREG */
55
57
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
56
struct in6_addr, inet_pton(),
58
inet_pton(), connect() */
58
59
#include <fcntl.h> /* open() */
59
60
#include <dirent.h> /* opendir(), struct dirent, readdir()
61
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
62
64
#include <assert.h> /* assert() */
63
#include <errno.h> /* perror(), errno */
64
#include <time.h> /* nanosleep(), time() */
65
#include <errno.h> /* perror(), errno,
66
program_invocation_short_name */
67
#include <time.h> /* nanosleep(), time(), sleep() */
65
68
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
66
69
SIOCSIFFLAGS, if_indextoname(),
67
70
if_nametoindex(), IF_NAMESIZE */
68
#include <netinet/in.h>
71
#include <netinet/in.h> /* IN6_IS_ADDR_LINKLOCAL,
72
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
69
74
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
70
getuid(), getgid(), setuid(),
72
#include <arpa/inet.h> /* inet_pton(), htons */
73
#include <iso646.h> /* not, and, or */
75
getuid(), getgid(), seteuid(),
77
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
78
#include <iso646.h> /* not, or, and */
74
79
#include <argp.h> /* struct argp_option, error_t, struct
75
80
argp_state, struct argp,
76
81
argp_parse(), ARGP_KEY_ARG,
77
82
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
83
#include <signal.h> /* sigemptyset(), sigaddset(),
84
sigaction(), SIGTERM, sig_atomic_t,
86
#include <sysexits.h> /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
87
EX_NOHOST, EX_IOERR, EX_PROTOCOL */
79
90
#include <sys/klog.h> /* klogctl() */
91
#endif /* __linux__ */
83
94
/* All Avahi types, constants and functions
509
597
/* Called when a Mandos server is found */
510
598
static int start_mandos_communication(const char *ip, uint16_t port,
511
599
AvahiIfIndex if_index,
601
int ret, tcp_sd = -1;
515
union { struct sockaddr in; struct sockaddr_in6 in6; } to;
604
struct sockaddr_in in;
605
struct sockaddr_in6 in6;
516
607
char *buffer = NULL;
517
char *decrypted_buffer;
608
char *decrypted_buffer = NULL;
518
609
size_t buffer_length = 0;
519
610
size_t buffer_capacity = 0;
520
ssize_t decrypted_buffer_size;
523
char interface[IF_NAMESIZE];
524
613
gnutls_session_t session;
526
ret = init_gnutls_session(mc, &session);
614
int pf; /* Protocol family */
631
fprintf(stderr, "Bad address family: %d\n", af);
636
ret = init_gnutls_session(&session);
532
fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
642
fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
536
tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
646
tcp_sd = socket(pf, SOCK_STREAM, 0);
649
perror_plus("socket");
543
if(if_indextoname((unsigned int)if_index, interface) == NULL){
544
perror("if_indextoname");
547
fprintf(stderr, "Binding to interface %s\n", interface);
550
659
memset(&to, 0, sizeof(to));
551
to.in6.sin6_family = AF_INET6;
552
/* It would be nice to have a way to detect if we were passed an
553
IPv4 address here. Now we assume an IPv6 address. */
554
ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
661
to.in6.sin6_family = (sa_family_t)af;
662
ret = inet_pton(af, ip, &to.in6.sin6_addr);
664
to.in.sin_family = (sa_family_t)af;
665
ret = inet_pton(af, ip, &to.in.sin_addr);
669
perror_plus("inet_pton");
560
675
fprintf(stderr, "Bad address: %s\n", ip);
563
to.in6.sin6_port = htons(port); /* Spurious warnings from
680
to.in6.sin6_port = htons(port); /* Spurious warnings from
682
-Wunreachable-code */
684
if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
685
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
687
if(if_index == AVAHI_IF_UNSPEC){
688
fprintf(stderr, "An IPv6 link-local address is incomplete"
689
" without a network interface\n");
693
/* Set the network interface number as scope */
694
to.in6.sin6_scope_id = (uint32_t)if_index;
697
to.in.sin_port = htons(port); /* Spurious warnings from
565
699
-Wunreachable-code */
567
to.in6.sin6_scope_id = (uint32_t)if_index;
570
fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
572
char addrstr[INET6_ADDRSTRLEN] = "";
573
if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
574
sizeof(addrstr)) == NULL){
708
if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
709
char interface[IF_NAMESIZE];
710
if(if_indextoname((unsigned int)if_index, interface) == NULL){
711
perror_plus("if_indextoname");
713
fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
714
ip, interface, port);
717
fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
720
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
721
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
724
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
727
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
731
perror_plus("inet_ntop");
577
733
if(strcmp(addrstr, ip) != 0){
578
734
fprintf(stderr, "Canonical address form: %s\n", addrstr);
1074
/* Signal handler that stops main loop after SIGTERM */
1075
static void handle_sigterm(int sig){
1080
signal_received = sig;
1081
int old_errno = errno;
1082
/* set main loop to exit */
1083
if(mc.simple_poll != NULL){
1084
avahi_simple_poll_quit(mc.simple_poll);
1089
bool get_flags(const char *ifname, struct ifreq *ifr){
1092
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1094
perror_plus("socket");
1097
strcpy(ifr->ifr_name, ifname);
1098
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1101
perror_plus("ioctl SIOCGIFFLAGS");
1108
bool good_flags(const char *ifname, const struct ifreq *ifr){
1110
/* Reject the loopback device */
1111
if(ifr->ifr_flags & IFF_LOOPBACK){
1113
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1118
/* Accept point-to-point devices only if connect_to is specified */
1119
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1121
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1126
/* Otherwise, reject non-broadcast-capable devices */
1127
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1129
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1134
/* Reject non-ARP interfaces (including dummy interfaces) */
1135
if(ifr->ifr_flags & IFF_NOARP){
1137
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1142
/* Accept this device */
1144
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1150
* This function determines if a directory entry in /sys/class/net
1151
* corresponds to an acceptable network device.
1152
* (This function is passed to scandir(3) as a filter function.)
1154
int good_interface(const struct dirent *if_entry){
1155
if(if_entry->d_name[0] == '.'){
1160
if(not get_flags(if_entry->d_name, &ifr)){
1162
fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1168
if(not good_flags(if_entry->d_name, &ifr)){
1175
* This function determines if a directory entry in /sys/class/net
1176
* corresponds to an acceptable network device which is up.
1177
* (This function is passed to scandir(3) as a filter function.)
1179
int up_interface(const struct dirent *if_entry){
1180
if(if_entry->d_name[0] == '.'){
1185
if(not get_flags(if_entry->d_name, &ifr)){
1187
fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1193
/* Reject down interfaces */
1194
if(not (ifr.ifr_flags & IFF_UP)){
1196
fprintf(stderr, "Rejecting down interface \"%s\"\n",
1202
/* Reject non-running interfaces */
1203
if(not (ifr.ifr_flags & IFF_RUNNING)){
1205
fprintf(stderr, "Rejecting non-running interface \"%s\"\n",
1211
if(not good_flags(if_entry->d_name, &ifr)){
1217
int notdotentries(const struct dirent *direntry){
1218
/* Skip "." and ".." */
1219
if(direntry->d_name[0] == '.'
1220
and (direntry->d_name[1] == '\0'
1221
or (direntry->d_name[1] == '.'
1222
and direntry->d_name[2] == '\0'))){
1228
/* Is this directory entry a runnable program? */
1229
int runnable_hook(const struct dirent *direntry){
1233
if((direntry->d_name)[0] == '\0'){
1238
/* Save pointer to last character */
1239
char *end = strchr(direntry->d_name, '\0')-1;
1246
if(((direntry->d_name)[0] == '#')
1248
/* Temporary #name# */
1252
/* XXX more rules here */
1254
ret = stat(direntry->d_name, &st);
1257
perror_plus("Could not stat plugin");
1261
if(not (S_ISREG(st.st_mode))){
1262
/* Not a regular file */
1265
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1266
/* Not executable */
1272
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1274
struct timespec now;
1275
struct timespec waited_time;
1276
intmax_t block_time;
1279
if(mc.current_server == NULL){
1282
"Wait until first server is found. No timeout!\n");
1284
ret = avahi_simple_poll_iterate(s, -1);
1287
fprintf(stderr, "Check current_server if we should run it,"
1290
/* the current time */
1291
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1293
perror_plus("clock_gettime");
1296
/* Calculating in ms how long time between now and server
1297
who we visted longest time ago. Now - last seen. */
1298
waited_time.tv_sec = (now.tv_sec
1299
- mc.current_server->last_seen.tv_sec);
1300
waited_time.tv_nsec = (now.tv_nsec
1301
- mc.current_server->last_seen.tv_nsec);
1302
/* total time is 10s/10,000ms.
1303
Converting to s from ms by dividing by 1,000,
1304
and ns to ms by dividing by 1,000,000. */
1305
block_time = ((retry_interval
1306
- ((intmax_t)waited_time.tv_sec * 1000))
1307
- ((intmax_t)waited_time.tv_nsec / 1000000));
1310
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1313
if(block_time <= 0){
1314
ret = start_mandos_communication(mc.current_server->ip,
1315
mc.current_server->port,
1316
mc.current_server->if_index,
1317
mc.current_server->af);
1319
avahi_simple_poll_quit(mc.simple_poll);
1322
ret = clock_gettime(CLOCK_MONOTONIC,
1323
&mc.current_server->last_seen);
1325
perror_plus("clock_gettime");
1328
mc.current_server = mc.current_server->next;
1329
block_time = 0; /* Call avahi to find new Mandos
1330
servers, but don't block */
1333
ret = avahi_simple_poll_iterate(s, (int)block_time);
1336
if (ret > 0 or errno != EINTR) {
1337
return (ret != 1) ? ret : 0;
824
1343
int main(int argc, char *argv[]){
825
1344
AvahiSServiceBrowser *sb = NULL;
828
1347
intmax_t tmpmax;
830
1349
int exitcode = EXIT_SUCCESS;
831
const char *interface = "eth0";
1350
const char *interface = "";
832
1351
struct ifreq network;
1353
bool take_down_interface = false;
836
char *connect_to = NULL;
837
1356
char tempdir[] = "/tmp/mandosXXXXXX";
838
1357
bool tempdir_created = false;
839
1358
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
840
1359
const char *seckey = PATHDIR "/" SECKEY;
841
1360
const char *pubkey = PATHDIR "/" PUBKEY;
843
mandos_context mc = { .simple_poll = NULL, .server = NULL,
844
.dh_bits = 1024, .priority = "SECURE256"
845
":!CTYPE-X.509:+CTYPE-OPENPGP" };
846
1362
bool gnutls_initialized = false;
847
1363
bool gpgme_initialized = false;
1365
double retry_interval = 10; /* 10s between trying a server and
1366
retrying the same server again */
1368
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1369
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1374
/* Lower any group privileges we might have, just to be safe */
1378
perror_plus("setgid");
1381
/* Lower user privileges (temporarily) */
1385
perror_plus("seteuid");
851
1393
struct argp_option options[] = {
915
1471
mc.priority = arg;
917
1473
case 131: /* --delay */
918
ret = sscanf(arg, "%lf%n", &delay, &numchars);
919
if(ret < 1 or arg[numchars] != '\0'){
920
fprintf(stderr, "Bad delay\n");
1475
delay = strtof(arg, &tmp);
1476
if(errno != 0 or tmp == arg or *tmp != '\0'){
1477
argp_error(state, "Bad delay");
1479
case 132: /* --retry */
1481
retry_interval = strtod(arg, &tmp);
1482
if(errno != 0 or tmp == arg or *tmp != '\0'
1483
or (retry_interval * 1000) > INT_MAX
1484
or retry_interval < 0){
1485
argp_error(state, "Bad retry interval");
1489
* These reproduce what we would get without ARGP_NO_HELP
1491
case '?': /* --help */
1492
argp_state_help(state, state->out_stream,
1493
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1494
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1495
case -3: /* --usage */
1496
argp_state_help(state, state->out_stream,
1497
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1498
case 'V': /* --version */
1499
fprintf(state->out_stream, "%s\n", argp_program_version);
1500
exit(argp_err_exit_status);
929
1503
return ARGP_ERR_UNKNOWN;
934
1508
struct argp argp = { .options = options, .parser = parse_opt,
936
1510
.doc = "Mandos client -- Get and decrypt"
937
1511
" passwords from a Mandos server" };
938
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
939
if(ret == ARGP_ERR_UNKNOWN){
940
fprintf(stderr, "Unknown error while parsing arguments\n");
1512
ret = argp_parse(&argp, argc, argv,
1513
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1520
perror_plus("argp_parse");
1521
exitcode = EX_OSERR;
1524
exitcode = EX_USAGE;
1530
/* Work around Debian bug #633582:
1531
<http://bugs.debian.org/633582> */
1534
/* Re-raise priviliges */
1538
perror_plus("seteuid");
1541
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1542
int seckey_fd = open(seckey, O_RDONLY);
1543
if(seckey_fd == -1){
1544
perror_plus("open");
1546
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1548
perror_plus("fstat");
1550
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1551
ret = fchown(seckey_fd, uid, gid);
1553
perror_plus("fchown");
1557
TEMP_FAILURE_RETRY(close(seckey_fd));
1561
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1562
int pubkey_fd = open(pubkey, O_RDONLY);
1563
if(pubkey_fd == -1){
1564
perror_plus("open");
1566
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1568
perror_plus("fstat");
1570
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1571
ret = fchown(pubkey_fd, uid, gid);
1573
perror_plus("fchown");
1577
TEMP_FAILURE_RETRY(close(pubkey_fd));
1581
/* Lower privileges */
1585
perror_plus("seteuid");
1589
/* Find network hooks and run them */
1591
struct dirent **direntries;
1592
struct dirent *direntry;
1593
int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1595
int devnull = open("/dev/null", O_RDONLY);
1596
for(int i = 0; i < numhooks; i++){
1597
direntry = direntries[0];
1598
char *fullname = NULL;
1599
ret = asprintf(&fullname, "%s/%s", tempdir,
1602
perror_plus("asprintf");
1605
pid_t hook_pid = fork();
1608
dup2(devnull, STDIN_FILENO);
1610
dup2(STDERR_FILENO, STDOUT_FILENO);
1611
setenv("DEVICE", interface, 1);
1612
setenv("VERBOSE", debug ? "1" : "0", 1);
1613
setenv("MODE", "start", 1);
1614
/* setenv( XXX more here */
1615
ret = execl(fullname, direntry->d_name, "start", NULL);
1616
perror_plus("execl");
1627
avahi_set_log_function(empty_log);
1630
if(interface[0] == '\0'){
1631
struct dirent **direntries;
1632
/* First look for interfaces that are up */
1633
ret = scandir(sys_class_net, &direntries, up_interface,
1636
/* No up interfaces, look for any good interfaces */
1638
ret = scandir(sys_class_net, &direntries, good_interface,
1642
/* Pick the first interface returned */
1643
interface = strdup(direntries[0]->d_name);
1645
fprintf(stderr, "Using interface \"%s\"\n", interface);
1647
if(interface == NULL){
1648
perror_plus("malloc");
1650
exitcode = EXIT_FAILURE;
1656
fprintf(stderr, "Could not find a network interface\n");
941
1657
exitcode = EXIT_FAILURE;
1662
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1663
from the signal handler */
1664
/* Initialize the pseudo-RNG for Avahi */
1665
srand((unsigned int) time(NULL));
1666
mc.simple_poll = avahi_simple_poll_new();
1667
if(mc.simple_poll == NULL){
1668
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1669
exitcode = EX_UNAVAILABLE;
1673
sigemptyset(&sigterm_action.sa_mask);
1674
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1676
perror_plus("sigaddset");
1677
exitcode = EX_OSERR;
1680
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1682
perror_plus("sigaddset");
1683
exitcode = EX_OSERR;
1686
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1688
perror_plus("sigaddset");
1689
exitcode = EX_OSERR;
1692
/* Need to check if the handler is SIG_IGN before handling:
1693
| [[info:libc:Initial Signal Actions]] |
1694
| [[info:libc:Basic Signal Handling]] |
1696
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1698
perror_plus("sigaction");
1701
if(old_sigterm_action.sa_handler != SIG_IGN){
1702
ret = sigaction(SIGINT, &sigterm_action, NULL);
1704
perror_plus("sigaction");
1705
exitcode = EX_OSERR;
1709
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1711
perror_plus("sigaction");
1714
if(old_sigterm_action.sa_handler != SIG_IGN){
1715
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1717
perror_plus("sigaction");
1718
exitcode = EX_OSERR;
1722
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1724
perror_plus("sigaction");
1727
if(old_sigterm_action.sa_handler != SIG_IGN){
1728
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1730
perror_plus("sigaction");
1731
exitcode = EX_OSERR;
946
1736
/* If the interface is down, bring it up */
1737
if(strcmp(interface, "none") != 0){
1738
if_index = (AvahiIfIndex) if_nametoindex(interface);
1740
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1741
exitcode = EX_UNAVAILABLE;
1749
/* Re-raise priviliges */
1753
perror_plus("seteuid");
948
1756
#ifdef __linux__
949
1757
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
950
messages to mess up the prompt */
1758
messages about the network interface to mess up the prompt */
951
1759
ret = klogctl(8, NULL, 5);
1760
bool restore_loglevel = true;
1762
restore_loglevel = false;
1763
perror_plus("klogctl");
1765
#endif /* __linux__ */
957
1767
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
960
exitcode = EXIT_FAILURE;
1769
perror_plus("socket");
1770
exitcode = EX_OSERR;
961
1771
#ifdef __linux__
962
ret = klogctl(7, NULL, 0);
1772
if(restore_loglevel){
1773
ret = klogctl(7, NULL, 0);
1775
perror_plus("klogctl");
1778
#endif /* __linux__ */
1779
/* Lower privileges */
1783
perror_plus("seteuid");
969
1787
strcpy(network.ifr_name, interface);
970
1788
ret = ioctl(sd, SIOCGIFFLAGS, &network);
972
perror("ioctl SIOCGIFFLAGS");
1790
perror_plus("ioctl SIOCGIFFLAGS");
973
1791
#ifdef __linux__
974
ret = klogctl(7, NULL, 0);
1792
if(restore_loglevel){
1793
ret = klogctl(7, NULL, 0);
1795
perror_plus("klogctl");
1798
#endif /* __linux__ */
1799
exitcode = EX_OSERR;
1800
/* Lower privileges */
1804
perror_plus("seteuid");
979
exitcode = EXIT_FAILURE;
982
1808
if((network.ifr_flags & IFF_UP) == 0){
983
1809
network.ifr_flags |= IFF_UP;
1810
take_down_interface = true;
984
1811
ret = ioctl(sd, SIOCSIFFLAGS, &network);
986
perror("ioctl SIOCSIFFLAGS");
987
exitcode = EXIT_FAILURE;
1813
take_down_interface = false;
1814
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1815
exitcode = EX_OSERR;
988
1816
#ifdef __linux__
989
ret = klogctl(7, NULL, 0);
1817
if(restore_loglevel){
1818
ret = klogctl(7, NULL, 0);
1820
perror_plus("klogctl");
1823
#endif /* __linux__ */
1824
/* Lower privileges */
1828
perror_plus("seteuid");
997
/* sleep checking until interface is running */
1833
/* Sleep checking until interface is running.
1834
Check every 0.25s, up to total time of delay */
998
1835
for(int i=0; i < delay * 4; i++){
999
1836
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1001
perror("ioctl SIOCGIFFLAGS");
1838
perror_plus("ioctl SIOCGIFFLAGS");
1002
1839
} else if(network.ifr_flags & IFF_RUNNING){
1005
1842
struct timespec sleeptime = { .tv_nsec = 250000000 };
1006
1843
ret = nanosleep(&sleeptime, NULL);
1007
1844
if(ret == -1 and errno != EINTR){
1008
perror("nanosleep");
1845
perror_plus("nanosleep");
1011
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1848
if(not take_down_interface){
1849
/* We won't need the socket anymore */
1850
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1852
perror_plus("close");
1015
1855
#ifdef __linux__
1016
/* Restores kernel loglevel to default */
1017
ret = klogctl(7, NULL, 0);
1037
ret = init_gnutls_global(&mc, pubkey, seckey);
1856
if(restore_loglevel){
1857
/* Restores kernel loglevel to default */
1858
ret = klogctl(7, NULL, 0);
1860
perror_plus("klogctl");
1863
#endif /* __linux__ */
1864
/* Lower privileges */
1866
if(take_down_interface){
1867
/* Lower privileges */
1870
perror_plus("seteuid");
1873
/* Lower privileges permanently */
1876
perror_plus("setuid");
1885
ret = init_gnutls_global(pubkey, seckey);
1039
1887
fprintf(stderr, "init_gnutls_global failed\n");
1040
exitcode = EXIT_FAILURE;
1888
exitcode = EX_UNAVAILABLE;
1043
1891
gnutls_initialized = true;
1046
1898
if(mkdtemp(tempdir) == NULL){
1899
perror_plus("mkdtemp");
1050
1902
tempdir_created = true;
1052
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1908
if(not init_gpgme(pubkey, seckey, tempdir)){
1053
1909
fprintf(stderr, "init_gpgme failed\n");
1054
exitcode = EXIT_FAILURE;
1910
exitcode = EX_UNAVAILABLE;
1057
1913
gpgme_initialized = true;
1060
if_index = (AvahiIfIndex) if_nametoindex(interface);
1062
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1063
exitcode = EXIT_FAILURE;