9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2008-2011 Teddy Hogeborn
13
* Copyright © 2008-2011 Björn Påhlsson
12
* Copyright © 2008-2010 Teddy Hogeborn
13
* Copyright © 2008-2010 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
program_invocation_short_name */
65
#include <errno.h> /* perror(), errno */
67
66
#include <time.h> /* nanosleep(), time(), sleep() */
68
67
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
69
68
SIOCSIFFLAGS, if_indextoname(),
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,
127
126
bool debug = false;
128
127
static const char mandos_protocol_version[] = "1";
129
128
const char *argp_program_version = "mandos-client " VERSION;
130
const char *argp_program_bug_address = "<mandos@recompile.se>";
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
131
130
static const char sys_class_net[] = "/sys/class/net";
132
131
char *connect_to = NULL;
134
/* Doubly linked list that need to be circularly linked when used */
135
typedef struct server{
138
AvahiIfIndex if_index;
140
struct timespec last_seen;
145
133
/* Used for passing in values through the Avahi callback functions */
147
135
AvahiSimplePoll *simple_poll;
151
139
gnutls_dh_params_t dh_params;
152
140
const char *priority;
154
server *current_server;
155
142
} mandos_context;
157
144
/* global context so signal handler can reach it*/
158
145
mandos_context mc = { .simple_poll = NULL, .server = NULL,
159
146
.dh_bits = 1024, .priority = "SECURE256"
160
":!CTYPE-X.509:+CTYPE-OPENPGP",
161
.current_server = NULL };
147
":!CTYPE-X.509:+CTYPE-OPENPGP" };
163
149
sig_atomic_t quit_now = 0;
164
150
int signal_received = 0;
166
/* Function to use when printing errors */
167
void perror_plus(const char *print_text){
168
fprintf(stderr, "Mandos plugin %s: ",
169
program_invocation_short_name);
174
153
* Make additional room in "buffer" for at least BUFFER_SIZE more
175
154
* bytes. "buffer_capacity" is how much is currently allocated,
187
166
return buffer_capacity;
190
/* Add server to set of servers to retry periodically */
191
int add_server(const char *ip, uint16_t port,
192
AvahiIfIndex if_index,
195
server *new_server = malloc(sizeof(server));
196
if(new_server == NULL){
197
perror_plus("malloc");
200
*new_server = (server){ .ip = strdup(ip),
202
.if_index = if_index,
204
if(new_server->ip == NULL){
205
perror_plus("strdup");
208
/* Special case of first server */
209
if (mc.current_server == NULL){
210
new_server->next = new_server;
211
new_server->prev = new_server;
212
mc.current_server = new_server;
213
/* Place the new server last in the list */
215
new_server->next = mc.current_server;
216
new_server->prev = mc.current_server->prev;
217
new_server->prev->next = new_server;
218
mc.current_server->prev = new_server;
220
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
222
perror_plus("clock_gettime");
229
170
* Initialize GPGME.
484
425
/* OpenPGP credentials */
485
ret = gnutls_certificate_allocate_credentials(&mc.cred);
426
gnutls_certificate_allocate_credentials(&mc.cred);
486
427
if(ret != GNUTLS_E_SUCCESS){
487
fprintf(stderr, "GnuTLS memory error: %s\n",
428
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
488
432
safer_gnutls_strerror(ret));
489
433
gnutls_global_deinit();
707
651
if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
708
652
char interface[IF_NAMESIZE];
709
653
if(if_indextoname((unsigned int)if_index, interface) == NULL){
710
perror_plus("if_indextoname");
654
perror("if_indextoname");
712
656
fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
713
657
ip, interface, port);
1093
1032
int good_interface(const struct dirent *if_entry){
1034
char *flagname = NULL;
1035
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1096
1041
if(if_entry->d_name[0] == '.'){
1099
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1101
perror_plus("socket");
1105
strcpy(ifr.ifr_name, if_entry->d_name);
1106
ret = ioctl(s, SIOCGIFFLAGS, &ifr);
1044
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1049
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1050
/* read line from flags_fd */
1051
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1052
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1053
flagstring[(size_t)to_read] = '\0';
1054
if(flagstring == NULL){
1060
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1077
tmpmax = strtoimax(flagstring, &tmp, 0);
1078
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1079
and not (isspace(*tmp)))
1080
or tmpmax != (ifreq_flags)tmpmax){
1109
perror_plus("ioctl SIOCGIFFLAGS");
1082
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1083
flagstring, if_entry->d_name);
1089
ifreq_flags flags = (ifreq_flags)tmpmax;
1113
1090
/* Reject the loopback device */
1114
if(ifr.ifr_flags & IFF_LOOPBACK){
1091
if(flags & IFF_LOOPBACK){
1116
1093
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1117
1094
if_entry->d_name);
1129
1106
/* Otherwise, reject non-broadcast-capable devices */
1130
if(not (ifr.ifr_flags & IFF_BROADCAST)){
1107
if(not (flags & IFF_BROADCAST)){
1132
1109
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1133
1110
if_entry->d_name);
1137
/* Reject non-ARP interfaces (including dummy interfaces) */
1138
if(ifr.ifr_flags & IFF_NOARP){
1140
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1145
1114
/* Accept this device */
1147
1116
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1153
int notdotentries(const struct dirent *direntry){
1154
/* Skip "." and ".." */
1155
if(direntry->d_name[0] == '.'
1156
and (direntry->d_name[1] == '\0'
1157
or (direntry->d_name[1] == '.'
1158
and direntry->d_name[2] == '\0'))){
1164
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1166
struct timespec now;
1167
struct timespec waited_time;
1168
intmax_t block_time;
1171
if(mc.current_server == NULL){
1174
"Wait until first server is found. No timeout!\n");
1176
ret = avahi_simple_poll_iterate(s, -1);
1179
fprintf(stderr, "Check current_server if we should run it,"
1182
/* the current time */
1183
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1185
perror_plus("clock_gettime");
1188
/* Calculating in ms how long time between now and server
1189
who we visted longest time ago. Now - last seen. */
1190
waited_time.tv_sec = (now.tv_sec
1191
- mc.current_server->last_seen.tv_sec);
1192
waited_time.tv_nsec = (now.tv_nsec
1193
- mc.current_server->last_seen.tv_nsec);
1194
/* total time is 10s/10,000ms.
1195
Converting to s from ms by dividing by 1,000,
1196
and ns to ms by dividing by 1,000,000. */
1197
block_time = ((retry_interval
1198
- ((intmax_t)waited_time.tv_sec * 1000))
1199
- ((intmax_t)waited_time.tv_nsec / 1000000));
1202
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1205
if(block_time <= 0){
1206
ret = start_mandos_communication(mc.current_server->ip,
1207
mc.current_server->port,
1208
mc.current_server->if_index,
1209
mc.current_server->af);
1211
avahi_simple_poll_quit(mc.simple_poll);
1214
ret = clock_gettime(CLOCK_MONOTONIC,
1215
&mc.current_server->last_seen);
1217
perror_plus("clock_gettime");
1220
mc.current_server = mc.current_server->next;
1221
block_time = 0; /* Call avahi to find new Mandos
1222
servers, but don't block */
1225
ret = avahi_simple_poll_iterate(s, (int)block_time);
1228
if (ret > 0 or errno != EINTR) {
1229
return (ret != 1) ? ret : 0;
1235
1122
int main(int argc, char *argv[]){
1236
1123
AvahiSServiceBrowser *sb = NULL;
1254
1141
bool gnutls_initialized = false;
1255
1142
bool gpgme_initialized = false;
1256
1143
float delay = 2.5f;
1257
double retry_interval = 10; /* 10s between trying a server and
1258
retrying the same server again */
1260
1145
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1261
1146
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1368
1249
if(errno != 0 or tmp == arg or *tmp != '\0'){
1369
1250
argp_error(state, "Bad delay");
1371
case 132: /* --retry */
1373
retry_interval = strtod(arg, &tmp);
1374
if(errno != 0 or tmp == arg or *tmp != '\0'
1375
or (retry_interval * 1000) > INT_MAX
1376
or retry_interval < 0){
1377
argp_error(state, "Bad retry interval");
1381
1254
* These reproduce what we would get without ARGP_NO_HELP
1422
/* Work around Debian bug #633582:
1423
<http://bugs.debian.org/633582> */
1426
/* Re-raise priviliges */
1430
perror_plus("seteuid");
1433
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1434
int seckey_fd = open(seckey, O_RDONLY);
1435
if(seckey_fd == -1){
1436
perror_plus("open");
1438
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1440
perror_plus("fstat");
1442
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1443
ret = fchown(seckey_fd, uid, gid);
1445
perror_plus("fchown");
1449
TEMP_FAILURE_RETRY(close(seckey_fd));
1453
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1454
int pubkey_fd = open(pubkey, O_RDONLY);
1455
if(pubkey_fd == -1){
1456
perror_plus("open");
1458
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1460
perror_plus("fstat");
1462
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1463
ret = fchown(pubkey_fd, uid, gid);
1465
perror_plus("fchown");
1469
TEMP_FAILURE_RETRY(close(pubkey_fd));
1473
/* Lower privileges */
1477
perror_plus("seteuid");
1482
1295
avahi_set_log_function(empty_log);
1521
1334
sigemptyset(&sigterm_action.sa_mask);
1522
1335
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1524
perror_plus("sigaddset");
1337
perror("sigaddset");
1525
1338
exitcode = EX_OSERR;
1528
1341
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1530
perror_plus("sigaddset");
1343
perror("sigaddset");
1531
1344
exitcode = EX_OSERR;
1534
1347
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1536
perror_plus("sigaddset");
1349
perror("sigaddset");
1537
1350
exitcode = EX_OSERR;
1544
1357
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1546
perror_plus("sigaction");
1359
perror("sigaction");
1547
1360
return EX_OSERR;
1549
1362
if(old_sigterm_action.sa_handler != SIG_IGN){
1550
1363
ret = sigaction(SIGINT, &sigterm_action, NULL);
1552
perror_plus("sigaction");
1365
perror("sigaction");
1553
1366
exitcode = EX_OSERR;
1557
1370
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1559
perror_plus("sigaction");
1372
perror("sigaction");
1560
1373
return EX_OSERR;
1562
1375
if(old_sigterm_action.sa_handler != SIG_IGN){
1563
1376
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1565
perror_plus("sigaction");
1378
perror("sigaction");
1566
1379
exitcode = EX_OSERR;
1570
1383
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1572
perror_plus("sigaction");
1385
perror("sigaction");
1573
1386
return EX_OSERR;
1575
1388
if(old_sigterm_action.sa_handler != SIG_IGN){
1576
1389
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1578
perror_plus("sigaction");
1391
perror("sigaction");
1579
1392
exitcode = EX_OSERR;
1629
1442
ret = seteuid(uid);
1631
perror_plus("seteuid");
1635
1448
strcpy(network.ifr_name, interface);
1636
1449
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1638
perror_plus("ioctl SIOCGIFFLAGS");
1451
perror("ioctl SIOCGIFFLAGS");
1639
1452
#ifdef __linux__
1640
1453
if(restore_loglevel){
1641
1454
ret = klogctl(7, NULL, 0);
1643
perror_plus("klogctl");
1646
1459
#endif /* __linux__ */
1659
1472
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1661
1474
take_down_interface = false;
1662
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1475
perror("ioctl SIOCSIFFLAGS +IFF_UP");
1663
1476
exitcode = EX_OSERR;
1664
1477
#ifdef __linux__
1665
1478
if(restore_loglevel){
1666
1479
ret = klogctl(7, NULL, 0);
1668
perror_plus("klogctl");
1671
1484
#endif /* __linux__ */
1674
1487
ret = seteuid(uid);
1676
perror_plus("seteuid");
1681
/* Sleep checking until interface is running.
1682
Check every 0.25s, up to total time of delay */
1494
/* sleep checking until interface is running */
1683
1495
for(int i=0; i < delay * 4; i++){
1684
1496
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1686
perror_plus("ioctl SIOCGIFFLAGS");
1498
perror("ioctl SIOCGIFFLAGS");
1687
1499
} else if(network.ifr_flags & IFF_RUNNING){
1690
1502
struct timespec sleeptime = { .tv_nsec = 250000000 };
1691
1503
ret = nanosleep(&sleeptime, NULL);
1692
1504
if(ret == -1 and errno != EINTR){
1693
perror_plus("nanosleep");
1505
perror("nanosleep");
1696
1508
if(not take_down_interface){
1697
1509
/* We won't need the socket anymore */
1698
1510
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1700
perror_plus("close");
1703
1515
#ifdef __linux__
1796
1609
port = (uint16_t)tmpmax;
1797
1610
*address = '\0';
1611
address = connect_to;
1798
1612
/* Colon in address indicates IPv6 */
1800
if(strchr(connect_to, ':') != NULL){
1614
if(strchr(address, ':') != NULL){
1802
/* Accept [] around IPv6 address - see RFC 5952 */
1803
if(connect_to[0] == '[' and address[-1] == ']')
1811
address = connect_to;
1817
1624
while(not quit_now){
1818
1625
ret = start_mandos_communication(address, port, if_index, af);
1819
1626
if(quit_now or ret == 0){
1823
fprintf(stderr, "Retrying in %d seconds\n",
1824
(int)retry_interval);
1826
sleep((int)retry_interval);
1829
1632
if (not quit_now){
1830
1633
exitcode = EXIT_SUCCESS;
1888
1691
fprintf(stderr, "Starting Avahi loop search\n");
1891
ret = avahi_loop_with_timeout(mc.simple_poll,
1892
(int)(retry_interval * 1000));
1894
fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1895
(ret == 0) ? "successfully" : "with error");
1694
avahi_simple_poll_loop(mc.simple_poll);
1920
1718
if(gpgme_initialized){
1921
1719
gpgme_release(mc.ctx);
1924
/* Cleans up the circular linked list of Mandos servers the client
1926
if(mc.current_server != NULL){
1927
mc.current_server->prev->next = NULL;
1928
while(mc.current_server != NULL){
1929
server *next = mc.current_server->next;
1930
free(mc.current_server);
1931
mc.current_server = next;
1935
1722
/* Take down the network interface */
1936
1723
if(take_down_interface){
1939
1726
ret = seteuid(0);
1941
perror_plus("seteuid");
1943
1730
if(geteuid() == 0){
1944
1731
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1946
perror_plus("ioctl SIOCGIFFLAGS");
1733
perror("ioctl SIOCGIFFLAGS");
1947
1734
} else if(network.ifr_flags & IFF_UP) {
1948
1735
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1949
1736
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1951
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1738
perror("ioctl SIOCSIFFLAGS -IFF_UP");
1954
1741
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1956
perror_plus("close");
1958
1745
/* Lower privileges permanently */
1960
1747
ret = setuid(uid);
1962
perror_plus("setuid");
1967
/* Removes the GPGME temp directory and all files inside */
1754
/* Removes the temp directory used by GPGME */
1968
1755
if(tempdir_created){
1969
struct dirent **direntries = NULL;
1970
struct dirent *direntry = NULL;
1971
int numentries = scandir(tempdir, &direntries, notdotentries,
1973
if (numentries > 0){
1974
for(int i = 0; i < numentries; i++){
1975
direntry = direntries[i];
1757
struct dirent *direntry;
1758
d = opendir(tempdir);
1760
if(errno != ENOENT){
1765
direntry = readdir(d);
1766
if(direntry == NULL){
1769
/* Skip "." and ".." */
1770
if(direntry->d_name[0] == '.'
1771
and (direntry->d_name[1] == '\0'
1772
or (direntry->d_name[1] == '.'
1773
and direntry->d_name[2] == '\0'))){
1976
1776
char *fullname = NULL;
1977
1777
ret = asprintf(&fullname, "%s/%s", tempdir,
1978
1778
direntry->d_name);
1980
perror_plus("asprintf");
1983
1783
ret = remove(fullname);