9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2008,2009 Teddy Hogeborn
13
* Copyright © 2008,2009 Björn Påhlsson
12
* Copyright © 2008-2011 Teddy Hogeborn
13
* Copyright © 2008-2011 Björn Påhlsson
15
15
* This program is free software: you can redistribute it and/or
16
16
* modify it under the terms of the GNU General Public License as
62
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
64
64
#include <assert.h> /* assert() */
65
#include <errno.h> /* perror(), errno */
66
#include <time.h> /* nanosleep(), time() */
65
#include <errno.h> /* perror(), errno, program_invocation_short_name */
66
#include <time.h> /* nanosleep(), time(), sleep() */
67
67
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
68
SIOCSIFFLAGS, if_indextoname(),
69
69
if_nametoindex(), IF_NAMESIZE */
130
130
static const char sys_class_net[] = "/sys/class/net";
131
131
char *connect_to = NULL;
133
/* Doubly linked list that need to be circular linked when ever used */
134
typedef struct server{
137
AvahiIfIndex if_index;
139
struct timespec last_seen;
133
144
/* Used for passing in values through the Avahi callback functions */
135
146
AvahiSimplePoll *simple_poll;
139
150
gnutls_dh_params_t dh_params;
140
151
const char *priority;
153
server *current_server;
142
154
} mandos_context;
144
156
/* global context so signal handler can reach it*/
145
157
mandos_context mc = { .simple_poll = NULL, .server = NULL,
146
158
.dh_bits = 1024, .priority = "SECURE256"
147
":!CTYPE-X.509:+CTYPE-OPENPGP" };
159
":!CTYPE-X.509:+CTYPE-OPENPGP", .current_server = NULL };
149
161
sig_atomic_t quit_now = 0;
150
162
int signal_received = 0;
164
/* Function to use when printing errors */
165
void perror_plus(const char *print_text){
166
fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
153
171
* Make additional room in "buffer" for at least BUFFER_SIZE more
154
172
* bytes. "buffer_capacity" is how much is currently allocated,
166
184
return buffer_capacity;
187
int add_server(const char *ip, uint16_t port,
188
AvahiIfIndex if_index,
191
server *new_server = malloc(sizeof(server));
192
if(new_server == NULL){
193
perror_plus("malloc");
196
*new_server = (server){ .ip = strdup(ip),
198
.if_index = if_index,
200
if(new_server->ip == NULL){
201
perror_plus("strdup");
204
/* uniqe case of first server */
205
if (mc.current_server == NULL){
206
new_server->next = new_server;
207
new_server->prev = new_server;
208
mc.current_server = new_server;
209
/* Placing the new server last in the list */
211
new_server->next = mc.current_server;
212
new_server->prev = mc.current_server->prev;
213
new_server->prev->next = new_server;
214
mc.current_server->prev = new_server;
216
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
218
perror_plus("clock_gettime");
170
225
* Initialize GPGME.
337
392
/* Seek back to the beginning of the GPGME plaintext data buffer */
338
393
if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
339
perror("gpgme_data_seek");
394
perror_plus("gpgme_data_seek");
340
395
plaintext_length = -1;
341
396
goto decrypt_end;
651
706
if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
652
707
char interface[IF_NAMESIZE];
653
708
if(if_indextoname((unsigned int)if_index, interface) == NULL){
654
perror("if_indextoname");
709
perror_plus("if_indextoname");
656
711
fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
657
712
ip, interface, port);
949
1007
avahi_proto_to_af(proto));
951
1009
avahi_simple_poll_quit(mc.simple_poll);
1011
ret = add_server(ip, port, interface,
1012
avahi_proto_to_af(proto));
1030
1092
int good_interface(const struct dirent *if_entry){
1032
1094
char *flagname = NULL;
1095
if(if_entry->d_name[0] == '.'){
1033
1098
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1034
1099
if_entry->d_name);
1039
if(if_entry->d_name[0] == '.'){
1101
perror_plus("asprintf");
1042
1104
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1043
1105
if(flags_fd == -1){
1106
perror_plus("open");
1047
1111
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1048
1112
/* read line from flags_fd */
1049
1113
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1050
1114
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1051
1115
flagstring[(size_t)to_read] = '\0';
1052
1116
if(flagstring == NULL){
1117
perror_plus("malloc");
1054
1118
close(flags_fd);
1176
/* Reject non-ARP interfaces (including dummy interfaces) */
1177
if(flags & IFF_NOARP){
1179
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1112
1184
/* Accept this device */
1114
1186
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1192
int notdotentries(const struct dirent *direntry){
1193
/* Skip "." and ".." */
1194
if(direntry->d_name[0] == '.'
1195
and (direntry->d_name[1] == '\0'
1196
or (direntry->d_name[1] == '.'
1197
and direntry->d_name[2] == '\0'))){
1203
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1205
struct timespec now;
1206
struct timespec waited_time;
1207
intmax_t block_time;
1210
if(mc.current_server == NULL){
1212
fprintf(stderr, "Wait until first server is found. No timeout!\n");
1214
ret = avahi_simple_poll_iterate(s, -1);
1217
fprintf(stderr, "Check current_server if we should run it, or wait\n");
1219
/* the current time */
1220
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1222
perror_plus("clock_gettime");
1225
/* Calculating in ms how long time between now and server
1226
who we visted longest time ago. Now - last seen. */
1227
waited_time.tv_sec = now.tv_sec - mc.current_server->last_seen.tv_sec;
1228
waited_time.tv_nsec = now.tv_nsec - mc.current_server->last_seen.tv_nsec;
1229
/* total time is 10s/10000ms. Converting to s to ms by 1000/s, and ns to ms by divind by 1000000. */
1230
block_time = (retry_interval - ((intmax_t)waited_time.tv_sec * 1000)) - ((intmax_t)waited_time.tv_nsec / 1000000);
1233
fprintf(stderr, "Blocking for %ld ms\n", block_time);
1236
if(block_time <= 0){
1237
ret = start_mandos_communication(mc.current_server->ip,
1238
mc.current_server->port,
1239
mc.current_server->if_index,
1240
mc.current_server->af);
1242
avahi_simple_poll_quit(mc.simple_poll);
1245
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
1247
perror_plus("clock_gettime");
1250
mc.current_server = mc.current_server->next;
1251
block_time = 0; /* call avahi to find new mandos servers, but dont block */
1254
ret = avahi_simple_poll_iterate(s, (int)block_time);
1257
if (ret > 0 or errno != EINTR) {
1258
return (ret != 1) ? ret : 0;
1120
1264
int main(int argc, char *argv[]){
1121
1265
AvahiSServiceBrowser *sb = NULL;
1139
1283
bool gnutls_initialized = false;
1140
1284
bool gpgme_initialized = false;
1141
1285
float delay = 2.5f;
1286
double retry_interval = 10; /* 10s between retrying a server and checking again*/
1143
1288
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1144
1289
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1198
1343
.arg = "SECONDS",
1199
1344
.doc = "Maximum delay to wait for interface startup",
1346
{ .name = "retry", .key = 132,
1348
.doc = "Retry interval used when denied by the mandos server",
1202
1351
* These reproduce what we would get without ARGP_NO_HELP
1247
1396
if(errno != 0 or tmp == arg or *tmp != '\0'){
1248
1397
argp_error(state, "Bad delay");
1399
case 132: /* --retry */
1401
retry_interval = strtod(arg, &tmp);
1402
if(errno != 0 or tmp == arg or *tmp != '\0'
1403
or (retry_interval * 1000) > INT_MAX){
1404
argp_error(state, "Bad retry interval");
1252
1408
* These reproduce what we would get without ARGP_NO_HELP
1332
1488
sigemptyset(&sigterm_action.sa_mask);
1333
1489
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1335
perror("sigaddset");
1491
perror_plus("sigaddset");
1336
1492
exitcode = EX_OSERR;
1339
1495
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1341
perror("sigaddset");
1497
perror_plus("sigaddset");
1342
1498
exitcode = EX_OSERR;
1345
1501
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1347
perror("sigaddset");
1503
perror_plus("sigaddset");
1348
1504
exitcode = EX_OSERR;
1355
1511
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1357
perror("sigaction");
1513
perror_plus("sigaction");
1358
1514
return EX_OSERR;
1360
1516
if(old_sigterm_action.sa_handler != SIG_IGN){
1361
1517
ret = sigaction(SIGINT, &sigterm_action, NULL);
1363
perror("sigaction");
1519
perror_plus("sigaction");
1364
1520
exitcode = EX_OSERR;
1368
1524
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1370
perror("sigaction");
1526
perror_plus("sigaction");
1371
1527
return EX_OSERR;
1373
1529
if(old_sigterm_action.sa_handler != SIG_IGN){
1374
1530
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1376
perror("sigaction");
1532
perror_plus("sigaction");
1377
1533
exitcode = EX_OSERR;
1381
1537
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1383
perror("sigaction");
1539
perror_plus("sigaction");
1384
1540
return EX_OSERR;
1386
1542
if(old_sigterm_action.sa_handler != SIG_IGN){
1387
1543
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1389
perror("sigaction");
1545
perror_plus("sigaction");
1390
1546
exitcode = EX_OSERR;
1419
1575
bool restore_loglevel = true;
1421
1577
restore_loglevel = false;
1578
perror_plus("klogctl");
1424
1580
#endif /* __linux__ */
1426
1582
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1584
perror_plus("socket");
1429
1585
exitcode = EX_OSERR;
1430
1586
#ifdef __linux__
1431
1587
if(restore_loglevel){
1432
1588
ret = klogctl(7, NULL, 0);
1590
perror_plus("klogctl");
1437
1593
#endif /* __linux__ */
1440
1596
ret = seteuid(uid);
1598
perror_plus("seteuid");
1446
1602
strcpy(network.ifr_name, interface);
1447
1603
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1449
perror("ioctl SIOCGIFFLAGS");
1605
perror_plus("ioctl SIOCGIFFLAGS");
1450
1606
#ifdef __linux__
1451
1607
if(restore_loglevel){
1452
1608
ret = klogctl(7, NULL, 0);
1610
perror_plus("klogctl");
1457
1613
#endif /* __linux__ */
1470
1626
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1472
1628
take_down_interface = false;
1473
perror("ioctl SIOCSIFFLAGS +IFF_UP");
1629
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1474
1630
exitcode = EX_OSERR;
1475
1631
#ifdef __linux__
1476
1632
if(restore_loglevel){
1477
1633
ret = klogctl(7, NULL, 0);
1635
perror_plus("klogctl");
1482
1638
#endif /* __linux__ */
1485
1641
ret = seteuid(uid);
1643
perror_plus("seteuid");
1492
/* sleep checking until interface is running */
1648
/* sleep checking until interface is running. Check every 0.25s, up to total time of delay */
1493
1649
for(int i=0; i < delay * 4; i++){
1494
1650
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1496
perror("ioctl SIOCGIFFLAGS");
1652
perror_plus("ioctl SIOCGIFFLAGS");
1497
1653
} else if(network.ifr_flags & IFF_RUNNING){
1500
1656
struct timespec sleeptime = { .tv_nsec = 250000000 };
1501
1657
ret = nanosleep(&sleeptime, NULL);
1502
1658
if(ret == -1 and errno != EINTR){
1503
perror("nanosleep");
1659
perror_plus("nanosleep");
1506
1662
if(not take_down_interface){
1507
1663
/* We won't need the socket anymore */
1508
1664
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1666
perror_plus("close");
1513
1669
#ifdef __linux__
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;
1777
while(not quit_now){
1778
ret = start_mandos_communication(address, port, if_index, af);
1779
if(quit_now or ret == 0){
1782
sleep((int)retry_interval or 1);
1644
1786
exitcode = EXIT_SUCCESS;
1701
1844
fprintf(stderr, "Starting Avahi loop search\n");
1704
avahi_simple_poll_loop(mc.simple_poll);
1847
ret = avahi_loop_with_timeout(mc.simple_poll, (int)(retry_interval * 1000));
1849
fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1850
(ret == 0) ? "successfully" : "with error");
1728
1875
if(gpgme_initialized){
1729
1876
gpgme_release(mc.ctx);
1879
/* cleans up the circular linked list of mandos servers the client has seen */
1880
if(mc.current_server != NULL){
1881
mc.current_server->prev->next = NULL;
1882
while(mc.current_server != NULL){
1883
server *next = mc.current_server->next;
1884
free(mc.current_server);
1885
mc.current_server = next;
1732
1889
/* Take down the network interface */
1733
1890
if(take_down_interface){
1736
1893
ret = seteuid(0);
1895
perror_plus("seteuid");
1740
1897
if(geteuid() == 0){
1741
1898
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1743
perror("ioctl SIOCGIFFLAGS");
1900
perror_plus("ioctl SIOCGIFFLAGS");
1744
1901
} else if(network.ifr_flags & IFF_UP) {
1745
1902
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1746
1903
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1748
perror("ioctl SIOCSIFFLAGS -IFF_UP");
1905
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1751
1908
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1910
perror_plus("close");
1755
1912
/* Lower privileges permanently */
1757
1914
ret = setuid(uid);
1916
perror_plus("setuid");
1764
/* Removes the temp directory used by GPGME */
1921
/* Removes the GPGME temp directory and all files inside */
1765
1922
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'))){
1923
struct dirent **direntries = NULL;
1924
struct dirent *direntry = NULL;
1925
ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1927
for(int i = 0; i < ret; i++){
1928
direntry = direntries[i];
1786
1929
char *fullname = NULL;
1787
1930
ret = asprintf(&fullname, "%s/%s", tempdir,
1788
1931
direntry->d_name);
1933
perror_plus("asprintf");
1793
1936
ret = remove(fullname);