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, program_invocation_short_name */
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,
157
156
/* global context so signal handler can reach it*/
158
157
mandos_context mc = { .simple_poll = NULL, .server = NULL,
159
158
.dh_bits = 1024, .priority = "SECURE256"
160
":!CTYPE-X.509:+CTYPE-OPENPGP",
161
.current_server = NULL };
159
":!CTYPE-X.509:+CTYPE-OPENPGP", .current_server = NULL };
163
161
sig_atomic_t quit_now = 0;
164
162
int signal_received = 0;
166
164
/* Function to use when printing errors */
167
165
void perror_plus(const char *print_text){
168
fprintf(stderr, "Mandos plugin %s: ",
169
program_invocation_short_name);
166
fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
170
167
perror(print_text);
204
200
if(new_server->ip == NULL){
205
201
perror_plus("strdup");
208
/* Special case of first server */
204
/* uniqe case of first server */
209
205
if (mc.current_server == NULL){
210
206
new_server->next = new_server;
211
207
new_server->prev = new_server;
212
208
mc.current_server = new_server;
213
/* Place the new server last in the list */
209
/* Placing the new server last in the list */
215
211
new_server->next = mc.current_server;
216
212
new_server->prev = mc.current_server->prev;
286
/* Set GPGME home directory for the OpenPGP engine only */
282
/* Set GPGME home directory for the OpenPGP engine only */
287
283
rc = gpgme_get_engine_info(&engine_info);
288
284
if(rc != GPG_ERR_NO_ERROR){
289
285
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
484
480
/* OpenPGP credentials */
485
ret = gnutls_certificate_allocate_credentials(&mc.cred);
481
gnutls_certificate_allocate_credentials(&mc.cred);
486
482
if(ret != GNUTLS_E_SUCCESS){
487
fprintf(stderr, "GnuTLS memory error: %s\n",
483
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
488
487
safer_gnutls_strerror(ret));
489
488
gnutls_global_deinit();
1111
1110
free(flagname);
1112
1111
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1113
1112
/* read line from flags_fd */
1114
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1113
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1115
1114
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1116
1115
flagstring[(size_t)to_read] = '\0';
1117
1116
if(flagstring == NULL){
1206
1205
struct timespec now;
1207
1206
struct timespec waited_time;
1208
1207
intmax_t block_time;
1211
1210
if(mc.current_server == NULL){
1214
"Wait until first server is found. No timeout!\n");
1212
fprintf(stderr, "Wait until first server is found. No timeout!\n");
1216
1214
ret = avahi_simple_poll_iterate(s, -1);
1219
fprintf(stderr, "Check current_server if we should run it,"
1217
fprintf(stderr, "Check current_server if we should run it, or wait\n");
1222
1219
/* the current time */
1223
1220
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1228
1225
/* Calculating in ms how long time between now and server
1229
1226
who we visted longest time ago. Now - last seen. */
1230
waited_time.tv_sec = (now.tv_sec
1231
- mc.current_server->last_seen.tv_sec);
1232
waited_time.tv_nsec = (now.tv_nsec
1233
- mc.current_server->last_seen.tv_nsec);
1234
/* total time is 10s/10,000ms.
1235
Converting to s from ms by dividing by 1,000,
1236
and ns to ms by dividing by 1,000,000. */
1237
block_time = ((retry_interval
1238
- ((intmax_t)waited_time.tv_sec * 1000))
1239
- ((intmax_t)waited_time.tv_nsec / 1000000));
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);
1242
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1233
fprintf(stderr, "Blocking for %ld ms\n", block_time);
1245
1236
if(block_time <= 0){
1246
1237
ret = start_mandos_communication(mc.current_server->ip,
1247
mc.current_server->port,
1248
mc.current_server->if_index,
1249
mc.current_server->af);
1238
mc.current_server->port,
1239
mc.current_server->if_index,
1240
mc.current_server->af);
1251
1242
avahi_simple_poll_quit(mc.simple_poll);
1254
ret = clock_gettime(CLOCK_MONOTONIC,
1255
&mc.current_server->last_seen);
1245
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
1257
1247
perror_plus("clock_gettime");
1260
1250
mc.current_server = mc.current_server->next;
1261
block_time = 0; /* Call avahi to find new Mandos
1262
servers, but don't block */
1251
block_time = 0; /* call avahi to find new mandos servers, but dont block */
1265
1254
ret = avahi_simple_poll_iterate(s, (int)block_time);
1294
1283
bool gnutls_initialized = false;
1295
1284
bool gpgme_initialized = false;
1296
1285
float delay = 2.5f;
1297
double retry_interval = 10; /* 10s between trying a server and
1298
retrying the same server again */
1286
double retry_interval = 10; /* 10s between retrying a server and checking again*/
1300
1288
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1301
1289
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1413
1401
retry_interval = strtod(arg, &tmp);
1414
1402
if(errno != 0 or tmp == arg or *tmp != '\0'
1415
or (retry_interval * 1000) > INT_MAX
1416
or retry_interval < 0){
1403
or (retry_interval * 1000) > INT_MAX){
1417
1404
argp_error(state, "Bad retry interval");
1462
/* Work around Debian bug #633582:
1463
<http://bugs.debian.org/633582> */
1466
/* Re-raise priviliges */
1470
perror_plus("seteuid");
1473
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1474
int seckey_fd = open(seckey, O_RDONLY);
1475
if(seckey_fd == -1){
1476
perror_plus("open");
1478
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1480
perror_plus("fstat");
1482
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1483
ret = fchown(seckey_fd, uid, gid);
1485
perror_plus("fchown");
1489
TEMP_FAILURE_RETRY(close(seckey_fd));
1493
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1494
int pubkey_fd = open(pubkey, O_RDONLY);
1495
if(pubkey_fd == -1){
1496
perror_plus("open");
1498
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1500
perror_plus("fstat");
1502
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1503
ret = fchown(pubkey_fd, uid, gid);
1505
perror_plus("fchown");
1509
TEMP_FAILURE_RETRY(close(pubkey_fd));
1513
/* Lower privileges */
1517
perror_plus("seteuid");
1522
1449
avahi_set_log_function(empty_log);
1721
/* Sleep checking until interface is running.
1722
Check every 0.25s, up to total time of delay */
1648
/* sleep checking until interface is running. Check every 0.25s, up to total time of delay */
1723
1649
for(int i=0; i < delay * 4; i++){
1724
1650
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1836
1762
port = (uint16_t)tmpmax;
1837
1763
*address = '\0';
1764
address = connect_to;
1838
1765
/* Colon in address indicates IPv6 */
1840
if(strchr(connect_to, ':') != NULL){
1767
if(strchr(address, ':') != NULL){
1842
/* Accept [] around IPv6 address - see RFC 5952 */
1843
if(connect_to[0] == '[' and address[-1] == ']')
1851
address = connect_to;
1857
1777
while(not quit_now){
1858
1778
ret = start_mandos_communication(address, port, if_index, af);
1859
1779
if(quit_now or ret == 0){
1863
fprintf(stderr, "Retrying in %d seconds\n",
1864
(int)retry_interval);
1866
sleep((int)retry_interval);
1782
sleep((int)retry_interval or 1);
1869
1785
if (not quit_now){
1870
1786
exitcode = EXIT_SUCCESS;
1928
1844
fprintf(stderr, "Starting Avahi loop search\n");
1931
ret = avahi_loop_with_timeout(mc.simple_poll,
1932
(int)(retry_interval * 1000));
1847
ret = avahi_loop_with_timeout(mc.simple_poll, (int)(retry_interval * 1000));
1934
1849
fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1935
1850
(ret == 0) ? "successfully" : "with error");
1961
1876
gpgme_release(mc.ctx);
1964
/* Cleans up the circular linked list of Mandos servers the client
1879
/* cleans up the circular linked list of mandos servers the client has seen */
1966
1880
if(mc.current_server != NULL){
1967
1881
mc.current_server->prev->next = NULL;
1968
1882
while(mc.current_server != NULL){
2008
1922
if(tempdir_created){
2009
1923
struct dirent **direntries = NULL;
2010
1924
struct dirent *direntry = NULL;
2011
int numentries = scandir(tempdir, &direntries, notdotentries,
2013
if (numentries > 0){
2014
for(int i = 0; i < numentries; i++){
1925
ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1927
for(int i = 0; i < ret; i++){
2015
1928
direntry = direntries[i];
2016
1929
char *fullname = NULL;
2017
1930
ret = asprintf(&fullname, "%s/%s", tempdir,