62
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
64
64
#include <assert.h> /* assert() */
65
#include <errno.h> /* perror(), errno */
65
#include <errno.h> /* perror(), errno,
66
program_invocation_short_name */
66
67
#include <time.h> /* nanosleep(), time(), sleep() */
67
68
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
69
SIOCSIFFLAGS, if_indextoname(),
73
74
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
74
75
getuid(), getgid(), seteuid(),
75
76
setgid(), pause() */
76
#include <arpa/inet.h> /* inet_pton(), htons */
77
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
77
78
#include <iso646.h> /* not, or, and */
78
79
#include <argp.h> /* struct argp_option, error_t, struct
79
80
argp_state, struct argp,
122
123
#define PATHDIR "/conf/conf.d/mandos"
123
124
#define SECKEY "seckey.txt"
124
125
#define PUBKEY "pubkey.txt"
126
#define HOOKDIR "/lib/mandos/network-hooks.d"
126
128
bool debug = false;
127
129
static const char mandos_protocol_version[] = "1";
128
130
const char *argp_program_version = "mandos-client " VERSION;
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
131
const char *argp_program_bug_address = "<mandos@recompile.se>";
130
132
static const char sys_class_net[] = "/sys/class/net";
131
133
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;
133
146
/* Used for passing in values through the Avahi callback functions */
135
148
AvahiSimplePoll *simple_poll;
139
152
gnutls_dh_params_t dh_params;
140
153
const char *priority;
155
server *current_server;
142
156
} mandos_context;
144
158
/* global context so signal handler can reach it*/
145
159
mandos_context mc = { .simple_poll = NULL, .server = NULL,
146
160
.dh_bits = 1024, .priority = "SECURE256"
147
":!CTYPE-X.509:+CTYPE-OPENPGP" };
161
":!CTYPE-X.509:+CTYPE-OPENPGP",
162
.current_server = NULL };
149
164
sig_atomic_t quit_now = 0;
150
165
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);
174
int fprintf_plus(FILE *stream, const char *format, ...){
176
va_start (ap, format);
178
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
179
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
153
183
* Make additional room in "buffer" for at least BUFFER_SIZE more
154
184
* bytes. "buffer_capacity" is how much is currently allocated,
166
196
return buffer_capacity;
199
/* Add server to set of servers to retry periodically */
200
int add_server(const char *ip, uint16_t port,
201
AvahiIfIndex if_index,
204
server *new_server = malloc(sizeof(server));
205
if(new_server == NULL){
206
perror_plus("malloc");
209
*new_server = (server){ .ip = strdup(ip),
211
.if_index = if_index,
213
if(new_server->ip == NULL){
214
perror_plus("strdup");
217
/* Special case of first server */
218
if (mc.current_server == NULL){
219
new_server->next = new_server;
220
new_server->prev = new_server;
221
mc.current_server = new_server;
222
/* Place the new server last in the list */
224
new_server->next = mc.current_server;
225
new_server->prev = mc.current_server->prev;
226
new_server->prev->next = new_server;
227
mc.current_server->prev = new_server;
229
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
231
perror_plus("clock_gettime");
170
238
* Initialize GPGME.
425
493
/* OpenPGP credentials */
426
gnutls_certificate_allocate_credentials(&mc.cred);
494
ret = gnutls_certificate_allocate_credentials(&mc.cred);
427
495
if(ret != GNUTLS_E_SUCCESS){
428
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
496
fprintf(stderr, "GnuTLS memory error: %s\n",
432
497
safer_gnutls_strerror(ret));
433
498
gnutls_global_deinit();
1019
1088
signal_received = sig;
1020
1089
int old_errno = errno;
1090
/* set main loop to exit */
1021
1091
if(mc.simple_poll != NULL){
1022
1092
avahi_simple_poll_quit(mc.simple_poll);
1024
1094
errno = old_errno;
1097
bool get_flags(const char *ifname, struct ifreq *ifr){
1100
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1102
perror_plus("socket");
1105
strcpy(ifr->ifr_name, ifname);
1106
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1109
perror_plus("ioctl SIOCGIFFLAGS");
1116
bool good_flags(const char *ifname, const struct ifreq *ifr){
1118
/* Reject the loopback device */
1119
if(ifr->ifr_flags & IFF_LOOPBACK){
1121
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1126
/* Accept point-to-point devices only if connect_to is specified */
1127
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1129
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1134
/* Otherwise, reject non-broadcast-capable devices */
1135
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1137
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1142
/* Reject non-ARP interfaces (including dummy interfaces) */
1143
if(ifr->ifr_flags & IFF_NOARP){
1145
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1150
/* Accept this device */
1152
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1028
1158
* This function determines if a directory entry in /sys/class/net
1029
1159
* corresponds to an acceptable network device.
1030
1160
* (This function is passed to scandir(3) as a filter function.)
1032
1162
int good_interface(const struct dirent *if_entry){
1164
if(if_entry->d_name[0] == '.'){
1169
if(not get_flags(if_entry->d_name, &ifr)){
1173
if(not good_flags(if_entry->d_name, &ifr)){
1180
* This function determines if a directory entry in /sys/class/net
1181
* corresponds to an acceptable network device which is up.
1182
* (This function is passed to scandir(3) as a filter function.)
1184
int up_interface(const struct dirent *if_entry){
1034
1186
char *flagname = NULL;
1035
1187
if(if_entry->d_name[0] == '.'){
1038
1190
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1039
1191
if_entry->d_name);
1193
perror_plus("asprintf");
1044
1196
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1045
1197
if(flags_fd == -1){
1198
perror_plus("open");
1047
1199
free(flagname);
1050
1202
free(flagname);
1051
1203
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1052
1204
/* read line from flags_fd */
1053
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1205
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1054
1206
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1055
1207
flagstring[(size_t)to_read] = '\0';
1056
1208
if(flagstring == NULL){
1209
perror_plus("malloc");
1058
1210
close(flags_fd);
1290
int notdotentries(const struct dirent *direntry){
1291
/* Skip "." and ".." */
1292
if(direntry->d_name[0] == '.'
1293
and (direntry->d_name[1] == '\0'
1294
or (direntry->d_name[1] == '.'
1295
and direntry->d_name[2] == '\0'))){
1301
/* Is this directory entry a runnable program? */
1302
int runnable_hook(const struct dirent *direntry){
1306
if((direntry->d_name)[0] == '\0'){
1311
/* Save pointer to last character */
1312
char *end = strchr(direntry->d_name, '\0')-1;
1319
if(((direntry->d_name)[0] == '#')
1321
/* Temporary #name# */
1325
/* XXX more rules here */
1327
ret = stat(direntry->d_name, &st);
1330
perror_plus("Could not stat plugin");
1334
if(not (st.st_mode & S_ISREG)){
1335
/* Not a regular file */
1338
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1339
/* Not executable */
1345
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1347
struct timespec now;
1348
struct timespec waited_time;
1349
intmax_t block_time;
1352
if(mc.current_server == NULL){
1355
"Wait until first server is found. No timeout!\n");
1357
ret = avahi_simple_poll_iterate(s, -1);
1360
fprintf(stderr, "Check current_server if we should run it,"
1363
/* the current time */
1364
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1366
perror_plus("clock_gettime");
1369
/* Calculating in ms how long time between now and server
1370
who we visted longest time ago. Now - last seen. */
1371
waited_time.tv_sec = (now.tv_sec
1372
- mc.current_server->last_seen.tv_sec);
1373
waited_time.tv_nsec = (now.tv_nsec
1374
- mc.current_server->last_seen.tv_nsec);
1375
/* total time is 10s/10,000ms.
1376
Converting to s from ms by dividing by 1,000,
1377
and ns to ms by dividing by 1,000,000. */
1378
block_time = ((retry_interval
1379
- ((intmax_t)waited_time.tv_sec * 1000))
1380
- ((intmax_t)waited_time.tv_nsec / 1000000));
1383
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1386
if(block_time <= 0){
1387
ret = start_mandos_communication(mc.current_server->ip,
1388
mc.current_server->port,
1389
mc.current_server->if_index,
1390
mc.current_server->af);
1392
avahi_simple_poll_quit(mc.simple_poll);
1395
ret = clock_gettime(CLOCK_MONOTONIC,
1396
&mc.current_server->last_seen);
1398
perror_plus("clock_gettime");
1401
mc.current_server = mc.current_server->next;
1402
block_time = 0; /* Call avahi to find new Mandos
1403
servers, but don't block */
1406
ret = avahi_simple_poll_iterate(s, (int)block_time);
1409
if (ret > 0 or errno != EINTR) {
1410
return (ret != 1) ? ret : 0;
1132
1416
int main(int argc, char *argv[]){
1133
1417
AvahiSServiceBrowser *sb = NULL;
1603
/* Work around Debian bug #633582:
1604
<http://bugs.debian.org/633582> */
1607
/* Re-raise priviliges */
1611
perror_plus("seteuid");
1614
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1615
int seckey_fd = open(seckey, O_RDONLY);
1616
if(seckey_fd == -1){
1617
perror_plus("open");
1619
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1621
perror_plus("fstat");
1623
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1624
ret = fchown(seckey_fd, uid, gid);
1626
perror_plus("fchown");
1630
TEMP_FAILURE_RETRY(close(seckey_fd));
1634
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1635
int pubkey_fd = open(pubkey, O_RDONLY);
1636
if(pubkey_fd == -1){
1637
perror_plus("open");
1639
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1641
perror_plus("fstat");
1643
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1644
ret = fchown(pubkey_fd, uid, gid);
1646
perror_plus("fchown");
1650
TEMP_FAILURE_RETRY(close(pubkey_fd));
1654
/* Lower privileges */
1658
perror_plus("seteuid");
1662
/* Find network hooks and run them */
1664
struct dirent **direntries;
1665
struct dirent *direntry;
1666
int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1668
int devnull = open("/dev/null", O_RDONLY);
1669
for(int i = 0; i < numhooks; i++){
1670
direntry = direntries[0];
1671
char *fullname = NULL;
1672
ret = asprintf(&fullname, "%s/%s", tempdir,
1675
perror_plus("asprintf");
1678
pid_t hook_pid = fork();
1681
dup2(devnull, STDIN_FILENO);
1683
dup2(STDERR_FILENO, STDOUT_FILENO);
1684
setenv("DEVICE", interface, 1);
1685
setenv("VERBOSE", debug ? "1" : "0", 1);
1686
setenv("MODE", "start", 1);
1687
/* setenv( XXX more here */
1688
ret = execl(fullname, direntry->d_name, "start");
1689
perror_plus("execl");
1305
1700
avahi_set_log_function(empty_log);
1308
1703
if(interface[0] == '\0'){
1309
1704
struct dirent **direntries;
1310
1705
ret = scandir(sys_class_net, &direntries, good_interface,
1367
1762
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1369
perror("sigaction");
1764
perror_plus("sigaction");
1370
1765
return EX_OSERR;
1372
1767
if(old_sigterm_action.sa_handler != SIG_IGN){
1373
1768
ret = sigaction(SIGINT, &sigterm_action, NULL);
1375
perror("sigaction");
1770
perror_plus("sigaction");
1376
1771
exitcode = EX_OSERR;
1380
1775
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1382
perror("sigaction");
1777
perror_plus("sigaction");
1383
1778
return EX_OSERR;
1385
1780
if(old_sigterm_action.sa_handler != SIG_IGN){
1386
1781
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1388
perror("sigaction");
1783
perror_plus("sigaction");
1389
1784
exitcode = EX_OSERR;
1393
1788
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1395
perror("sigaction");
1790
perror_plus("sigaction");
1396
1791
return EX_OSERR;
1398
1793
if(old_sigterm_action.sa_handler != SIG_IGN){
1399
1794
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1401
perror("sigaction");
1796
perror_plus("sigaction");
1402
1797
exitcode = EX_OSERR;
1497
1892
ret = seteuid(uid);
1894
perror_plus("seteuid");
1504
/* sleep checking until interface is running */
1899
/* Sleep checking until interface is running.
1900
Check every 0.25s, up to total time of delay */
1505
1901
for(int i=0; i < delay * 4; i++){
1506
1902
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1508
perror("ioctl SIOCGIFFLAGS");
1904
perror_plus("ioctl SIOCGIFFLAGS");
1509
1905
} else if(network.ifr_flags & IFF_RUNNING){
1512
1908
struct timespec sleeptime = { .tv_nsec = 250000000 };
1513
1909
ret = nanosleep(&sleeptime, NULL);
1514
1910
if(ret == -1 and errno != EINTR){
1515
perror("nanosleep");
1911
perror_plus("nanosleep");
1518
1914
if(not take_down_interface){
1519
1915
/* We won't need the socket anymore */
1520
1916
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1918
perror_plus("close");
1525
1921
#ifdef __linux__
1619
2014
port = (uint16_t)tmpmax;
1620
2015
*address = '\0';
1621
address = connect_to;
1622
2016
/* Colon in address indicates IPv6 */
1624
if(strchr(address, ':') != NULL){
2018
if(strchr(connect_to, ':') != NULL){
2020
/* Accept [] around IPv6 address - see RFC 5952 */
2021
if(connect_to[0] == '[' and address[-1] == ']')
2029
address = connect_to;
1634
2035
while(not quit_now){
1635
2036
ret = start_mandos_communication(address, port, if_index, af);
1636
2037
if(quit_now or ret == 0){
2041
fprintf(stderr, "Retrying in %d seconds\n",
2042
(int)retry_interval);
2044
sleep((int)retry_interval);
1642
2047
if (not quit_now){
1643
2048
exitcode = EXIT_SUCCESS;
1736
2159
ret = seteuid(0);
2161
perror_plus("seteuid");
1740
2163
if(geteuid() == 0){
1741
2164
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1743
perror("ioctl SIOCGIFFLAGS");
2166
perror_plus("ioctl SIOCGIFFLAGS");
1744
2167
} else if(network.ifr_flags & IFF_UP) {
1745
2168
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1746
2169
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1748
perror("ioctl SIOCSIFFLAGS -IFF_UP");
2171
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1751
2174
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2176
perror_plus("close");
1755
2178
/* Lower privileges permanently */
1757
2180
ret = setuid(uid);
2182
perror_plus("setuid");
1764
/* Removes the temp directory used by GPGME */
2187
/* Removes the GPGME temp directory and all files inside */
1765
2188
if(tempdir_created){
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'))){
2189
struct dirent **direntries = NULL;
2190
struct dirent *direntry = NULL;
2191
int numentries = scandir(tempdir, &direntries, notdotentries,
2193
if (numentries > 0){
2194
for(int i = 0; i < numentries; i++){
2195
direntry = direntries[i];
1786
2196
char *fullname = NULL;
1787
2197
ret = asprintf(&fullname, "%s/%s", tempdir,
1788
2198
direntry->d_name);
2200
perror_plus("asprintf");
1793
2203
ret = remove(fullname);