62
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
64
64
#include <assert.h> /* assert() */
65
#include <errno.h> /* perror(), errno,
66
program_invocation_short_name */
67
#include <time.h> /* nanosleep(), time(), sleep() */
65
#include <errno.h> /* perror(), errno */
66
#include <time.h> /* nanosleep(), time() */
68
67
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
69
68
SIOCSIFFLAGS, if_indextoname(),
70
69
if_nametoindex(), IF_NAMESIZE */
74
73
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
75
74
getuid(), getgid(), seteuid(),
76
75
setgid(), pause() */
77
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
76
#include <arpa/inet.h> /* inet_pton(), htons */
78
77
#include <iso646.h> /* not, or, and */
79
78
#include <argp.h> /* struct argp_option, error_t, struct
80
79
argp_state, struct argp,
123
122
#define PATHDIR "/conf/conf.d/mandos"
124
123
#define SECKEY "seckey.txt"
125
124
#define PUBKEY "pubkey.txt"
126
#define HOOKDIR "/lib/mandos/network-hooks.d"
128
126
bool debug = false;
129
127
static const char mandos_protocol_version[] = "1";
130
128
const char *argp_program_version = "mandos-client " VERSION;
131
const char *argp_program_bug_address = "<mandos@recompile.se>";
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
132
130
static const char sys_class_net[] = "/sys/class/net";
133
131
char *connect_to = NULL;
135
/* Doubly linked list that need to be circularly linked when used */
136
typedef struct server{
139
AvahiIfIndex if_index;
141
struct timespec last_seen;
146
133
/* Used for passing in values through the Avahi callback functions */
148
135
AvahiSimplePoll *simple_poll;
152
139
gnutls_dh_params_t dh_params;
153
140
const char *priority;
155
server *current_server;
156
142
} mandos_context;
158
144
/* global context so signal handler can reach it*/
159
145
mandos_context mc = { .simple_poll = NULL, .server = NULL,
160
146
.dh_bits = 1024, .priority = "SECURE256"
161
":!CTYPE-X.509:+CTYPE-OPENPGP",
162
.current_server = NULL };
147
":!CTYPE-X.509:+CTYPE-OPENPGP" };
164
149
sig_atomic_t quit_now = 0;
165
150
int signal_received = 0;
167
/* Function to use when printing errors */
168
void perror_plus(const char *print_text){
169
fprintf(stderr, "Mandos plugin %s: ",
170
program_invocation_short_name);
175
153
* Make additional room in "buffer" for at least BUFFER_SIZE more
176
154
* bytes. "buffer_capacity" is how much is currently allocated,
188
166
return buffer_capacity;
191
/* Add server to set of servers to retry periodically */
192
int add_server(const char *ip, uint16_t port,
193
AvahiIfIndex if_index,
196
server *new_server = malloc(sizeof(server));
197
if(new_server == NULL){
198
perror_plus("malloc");
201
*new_server = (server){ .ip = strdup(ip),
203
.if_index = if_index,
205
if(new_server->ip == NULL){
206
perror_plus("strdup");
209
/* Special case of first server */
210
if (mc.current_server == NULL){
211
new_server->next = new_server;
212
new_server->prev = new_server;
213
mc.current_server = new_server;
214
/* Place the new server last in the list */
216
new_server->next = mc.current_server;
217
new_server->prev = mc.current_server->prev;
218
new_server->prev->next = new_server;
219
mc.current_server->prev = new_server;
221
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
223
perror_plus("clock_gettime");
230
170
* Initialize GPGME.
485
425
/* OpenPGP credentials */
486
ret = gnutls_certificate_allocate_credentials(&mc.cred);
426
gnutls_certificate_allocate_credentials(&mc.cred);
487
427
if(ret != GNUTLS_E_SUCCESS){
488
fprintf(stderr, "GnuTLS memory error: %s\n",
428
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
489
432
safer_gnutls_strerror(ret));
490
433
gnutls_global_deinit();
1080
1017
signal_received = sig;
1081
1018
int old_errno = errno;
1082
/* set main loop to exit */
1083
1019
if(mc.simple_poll != NULL){
1084
1020
avahi_simple_poll_quit(mc.simple_poll);
1086
1022
errno = old_errno;
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
1026
* This function determines if a directory entry in /sys/class/net
1151
1027
* corresponds to an acceptable network device.
1152
1028
* (This function is passed to scandir(3) as a filter function.)
1154
1030
int good_interface(const struct dirent *if_entry){
1156
if(if_entry->d_name[0] == '.'){
1161
if(not get_flags(if_entry->d_name, &ifr)){
1165
if(not good_flags(if_entry->d_name, &ifr)){
1172
* This function determines if a directory entry in /sys/class/net
1173
* corresponds to an acceptable network device which is up.
1174
* (This function is passed to scandir(3) as a filter function.)
1176
int up_interface(const struct dirent *if_entry){
1178
1032
char *flagname = NULL;
1179
if(if_entry->d_name[0] == '.'){
1182
1033
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1183
1034
if_entry->d_name);
1185
perror_plus("asprintf");
1039
if(if_entry->d_name[0] == '.'){
1188
1042
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1189
1043
if(flags_fd == -1){
1190
perror_plus("open");
1195
1047
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1196
1048
/* read line from flags_fd */
1197
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1049
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1198
1050
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1199
1051
flagstring[(size_t)to_read] = '\0';
1200
1052
if(flagstring == NULL){
1201
perror_plus("malloc");
1202
1054
close(flags_fd);
1282
int notdotentries(const struct dirent *direntry){
1283
/* Skip "." and ".." */
1284
if(direntry->d_name[0] == '.'
1285
and (direntry->d_name[1] == '\0'
1286
or (direntry->d_name[1] == '.'
1287
and direntry->d_name[2] == '\0'))){
1293
/* Is this directory entry a runnable program? */
1294
int runnable_hook(const struct dirent *direntry){
1298
if((direntry->d_name)[0] == '\0'){
1303
/* Save pointer to last character */
1304
char *end = strchr(direntry->d_name, '\0')-1;
1311
if(((direntry->d_name)[0] == '#')
1313
/* Temporary #name# */
1317
/* XXX more rules here */
1319
ret = stat(direntry->d_name, &st);
1322
perror_plus("Could not stat plugin");
1326
if(not (st.st_mode & S_ISREG)){
1327
/* Not a regular file */
1330
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1331
/* Not executable */
1337
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1339
struct timespec now;
1340
struct timespec waited_time;
1341
intmax_t block_time;
1344
if(mc.current_server == NULL){
1347
"Wait until first server is found. No timeout!\n");
1349
ret = avahi_simple_poll_iterate(s, -1);
1352
fprintf(stderr, "Check current_server if we should run it,"
1355
/* the current time */
1356
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1358
perror_plus("clock_gettime");
1361
/* Calculating in ms how long time between now and server
1362
who we visted longest time ago. Now - last seen. */
1363
waited_time.tv_sec = (now.tv_sec
1364
- mc.current_server->last_seen.tv_sec);
1365
waited_time.tv_nsec = (now.tv_nsec
1366
- mc.current_server->last_seen.tv_nsec);
1367
/* total time is 10s/10,000ms.
1368
Converting to s from ms by dividing by 1,000,
1369
and ns to ms by dividing by 1,000,000. */
1370
block_time = ((retry_interval
1371
- ((intmax_t)waited_time.tv_sec * 1000))
1372
- ((intmax_t)waited_time.tv_nsec / 1000000));
1375
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1378
if(block_time <= 0){
1379
ret = start_mandos_communication(mc.current_server->ip,
1380
mc.current_server->port,
1381
mc.current_server->if_index,
1382
mc.current_server->af);
1384
avahi_simple_poll_quit(mc.simple_poll);
1387
ret = clock_gettime(CLOCK_MONOTONIC,
1388
&mc.current_server->last_seen);
1390
perror_plus("clock_gettime");
1393
mc.current_server = mc.current_server->next;
1394
block_time = 0; /* Call avahi to find new Mandos
1395
servers, but don't block */
1398
ret = avahi_simple_poll_iterate(s, (int)block_time);
1401
if (ret > 0 or errno != EINTR) {
1402
return (ret != 1) ? ret : 0;
1408
1120
int main(int argc, char *argv[]){
1409
1121
AvahiSServiceBrowser *sb = NULL;
1595
/* Work around Debian bug #633582:
1596
<http://bugs.debian.org/633582> */
1599
/* Re-raise priviliges */
1603
perror_plus("seteuid");
1606
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1607
int seckey_fd = open(seckey, O_RDONLY);
1608
if(seckey_fd == -1){
1609
perror_plus("open");
1611
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1613
perror_plus("fstat");
1615
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1616
ret = fchown(seckey_fd, uid, gid);
1618
perror_plus("fchown");
1622
TEMP_FAILURE_RETRY(close(seckey_fd));
1626
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1627
int pubkey_fd = open(pubkey, O_RDONLY);
1628
if(pubkey_fd == -1){
1629
perror_plus("open");
1631
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1633
perror_plus("fstat");
1635
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1636
ret = fchown(pubkey_fd, uid, gid);
1638
perror_plus("fchown");
1642
TEMP_FAILURE_RETRY(close(pubkey_fd));
1646
/* Lower privileges */
1650
perror_plus("seteuid");
1654
/* Find network hooks and run them */
1656
struct dirent **direntries;
1657
struct dirent *direntry;
1658
int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1660
int devnull = open("/dev/null", O_RDONLY);
1661
for(int i = 0; i < numhooks; i++){
1662
direntry = direntries[0];
1663
char *fullname = NULL;
1664
ret = asprintf(&fullname, "%s/%s", tempdir,
1667
perror_plus("asprintf");
1670
pid_t hook_pid = fork();
1673
dup2(devnull, STDIN_FILENO);
1675
dup2(STDERR_FILENO, STDOUT_FILENO);
1676
setenv("DEVICE", interface, 1);
1677
setenv("VERBOSE", debug ? "1" : "0", 1);
1678
setenv("MODE", "start", 1);
1679
/* setenv( XXX more here */
1680
ret = execl(fullname, direntry->d_name, "start");
1681
perror_plus("execl");
1692
1293
avahi_set_log_function(empty_log);
1695
1296
if(interface[0] == '\0'){
1696
1297
struct dirent **direntries;
1697
1298
ret = scandir(sys_class_net, &direntries, good_interface,
1754
1355
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1756
perror_plus("sigaction");
1357
perror("sigaction");
1757
1358
return EX_OSERR;
1759
1360
if(old_sigterm_action.sa_handler != SIG_IGN){
1760
1361
ret = sigaction(SIGINT, &sigterm_action, NULL);
1762
perror_plus("sigaction");
1363
perror("sigaction");
1763
1364
exitcode = EX_OSERR;
1767
1368
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1769
perror_plus("sigaction");
1370
perror("sigaction");
1770
1371
return EX_OSERR;
1772
1373
if(old_sigterm_action.sa_handler != SIG_IGN){
1773
1374
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1775
perror_plus("sigaction");
1376
perror("sigaction");
1776
1377
exitcode = EX_OSERR;
1780
1381
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1782
perror_plus("sigaction");
1383
perror("sigaction");
1783
1384
return EX_OSERR;
1785
1386
if(old_sigterm_action.sa_handler != SIG_IGN){
1786
1387
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1788
perror_plus("sigaction");
1389
perror("sigaction");
1789
1390
exitcode = EX_OSERR;
1884
1485
ret = seteuid(uid);
1886
perror_plus("seteuid");
1891
/* Sleep checking until interface is running.
1892
Check every 0.25s, up to total time of delay */
1492
/* sleep checking until interface is running */
1893
1493
for(int i=0; i < delay * 4; i++){
1894
1494
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1896
perror_plus("ioctl SIOCGIFFLAGS");
1496
perror("ioctl SIOCGIFFLAGS");
1897
1497
} else if(network.ifr_flags & IFF_RUNNING){
1900
1500
struct timespec sleeptime = { .tv_nsec = 250000000 };
1901
1501
ret = nanosleep(&sleeptime, NULL);
1902
1502
if(ret == -1 and errno != EINTR){
1903
perror_plus("nanosleep");
1503
perror("nanosleep");
1906
1506
if(not take_down_interface){
1907
1507
/* We won't need the socket anymore */
1908
1508
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1910
perror_plus("close");
1913
1513
#ifdef __linux__
2006
1607
port = (uint16_t)tmpmax;
2007
1608
*address = '\0';
1609
address = connect_to;
2008
1610
/* Colon in address indicates IPv6 */
2010
if(strchr(connect_to, ':') != NULL){
1612
if(strchr(address, ':') != NULL){
2012
/* Accept [] around IPv6 address - see RFC 5952 */
2013
if(connect_to[0] == '[' and address[-1] == ']')
2021
address = connect_to;
2027
while(not quit_now){
2028
ret = start_mandos_communication(address, port, if_index, af);
2029
if(quit_now or ret == 0){
2033
fprintf(stderr, "Retrying in %d seconds\n",
2034
(int)retry_interval);
2036
sleep((int)retry_interval);
1622
ret = start_mandos_communication(address, port, if_index, af);
1628
exitcode = EX_NOHOST;
1631
exitcode = EX_USAGE;
1634
exitcode = EX_IOERR;
1637
exitcode = EX_PROTOCOL;
1640
exitcode = EX_OSERR;
2040
1644
exitcode = EXIT_SUCCESS;
2151
1736
ret = seteuid(0);
2153
perror_plus("seteuid");
2155
1740
if(geteuid() == 0){
2156
1741
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2158
perror_plus("ioctl SIOCGIFFLAGS");
1743
perror("ioctl SIOCGIFFLAGS");
2159
1744
} else if(network.ifr_flags & IFF_UP) {
2160
1745
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2161
1746
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2163
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1748
perror("ioctl SIOCSIFFLAGS -IFF_UP");
2166
1751
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2168
perror_plus("close");
2170
1755
/* Lower privileges permanently */
2172
1757
ret = setuid(uid);
2174
perror_plus("setuid");
2179
/* Removes the GPGME temp directory and all files inside */
1764
/* Removes the temp directory used by GPGME */
2180
1765
if(tempdir_created){
2181
struct dirent **direntries = NULL;
2182
struct dirent *direntry = NULL;
2183
int numentries = scandir(tempdir, &direntries, notdotentries,
2185
if (numentries > 0){
2186
for(int i = 0; i < numentries; i++){
2187
direntry = direntries[i];
1767
struct dirent *direntry;
1768
d = opendir(tempdir);
1770
if(errno != ENOENT){
1775
direntry = readdir(d);
1776
if(direntry == NULL){
1779
/* Skip "." and ".." */
1780
if(direntry->d_name[0] == '.'
1781
and (direntry->d_name[1] == '\0'
1782
or (direntry->d_name[1] == '.'
1783
and direntry->d_name[2] == '\0'))){
2188
1786
char *fullname = NULL;
2189
1787
ret = asprintf(&fullname, "%s/%s", tempdir,
2190
1788
direntry->d_name);
2192
perror_plus("asprintf");
2195
1793
ret = remove(fullname);