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);
203
200
if(new_server->ip == NULL){
204
201
perror_plus("strdup");
207
/* unique case of first server */
204
/* uniqe case of first server */
208
205
if (mc.current_server == NULL){
209
206
new_server->next = new_server;
210
207
new_server->prev = new_server;
483
480
/* OpenPGP credentials */
484
ret = gnutls_certificate_allocate_credentials(&mc.cred);
481
gnutls_certificate_allocate_credentials(&mc.cred);
485
482
if(ret != GNUTLS_E_SUCCESS){
486
fprintf(stderr, "GnuTLS memory error: %s\n",
483
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
487
487
safer_gnutls_strerror(ret));
488
488
gnutls_global_deinit();
1110
1110
free(flagname);
1111
1111
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1112
1112
/* read line from flags_fd */
1113
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1113
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1114
1114
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
1115
flagstring[(size_t)to_read] = '\0';
1116
1116
if(flagstring == NULL){
1210
1210
if(mc.current_server == NULL){
1213
"Wait until first server is found. No timeout!\n");
1212
fprintf(stderr, "Wait until first server is found. No timeout!\n");
1215
1214
ret = avahi_simple_poll_iterate(s, -1);
1218
fprintf(stderr, "Check current_server if we should run it,"
1217
fprintf(stderr, "Check current_server if we should run it, or wait\n");
1221
1219
/* the current time */
1222
1220
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1227
1225
/* Calculating in ms how long time between now and server
1228
1226
who we visted longest time ago. Now - last seen. */
1229
waited_time.tv_sec = (now.tv_sec
1230
- mc.current_server->last_seen.tv_sec);
1231
waited_time.tv_nsec = (now.tv_nsec
1232
- mc.current_server->last_seen.tv_nsec);
1233
/* total time is 10s/10,000ms.
1234
Converting to s from ms by dividing by 1,000,
1235
and ns to ms by dividing by 1,000,000. */
1236
block_time = ((retry_interval
1237
- ((intmax_t)waited_time.tv_sec * 1000))
1238
- ((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);
1241
fprintf(stderr, "Blocking for %ld ms\n", block_time);
1233
fprintf(stderr, "Blocking for %ld ms\n", block_time);
1244
1236
if(block_time <= 0){
1245
1237
ret = start_mandos_communication(mc.current_server->ip,
1246
mc.current_server->port,
1247
mc.current_server->if_index,
1248
mc.current_server->af);
1238
mc.current_server->port,
1239
mc.current_server->if_index,
1240
mc.current_server->af);
1250
1242
avahi_simple_poll_quit(mc.simple_poll);
1253
ret = clock_gettime(CLOCK_MONOTONIC,
1254
&mc.current_server->last_seen);
1245
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
1256
1247
perror_plus("clock_gettime");
1259
1250
mc.current_server = mc.current_server->next;
1260
block_time = 0; /* Call avahi to find new Mandos
1261
servers, but don't block */
1251
block_time = 0; /* call avahi to find new mandos servers, but dont block */
1264
1254
ret = avahi_simple_poll_iterate(s, (int)block_time);
1293
1283
bool gnutls_initialized = false;
1294
1284
bool gpgme_initialized = false;
1295
1285
float delay = 2.5f;
1296
double retry_interval = 10; /* 10s between trying a server and
1297
retrying the same server again */
1286
double retry_interval = 10; /* 10s between retrying a server and checking again*/
1299
1288
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1300
1289
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1460
/* Work around Debian bug #633582:
1461
<http://bugs.debian.org/633582> */
1464
/* Re-raise priviliges */
1468
perror_plus("seteuid");
1471
int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
1472
if(seckey_fd == -1){
1473
perror_plus("open");
1475
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1477
perror_plus("fstat");
1479
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1480
ret = fchown(seckey_fd, uid, gid);
1482
perror_plus("fchown");
1486
TEMP_FAILURE_RETRY(close(seckey_fd));
1489
int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
1490
if(pubkey_fd == -1){
1491
perror_plus("open");
1493
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1495
perror_plus("fstat");
1497
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1498
ret = fchown(pubkey_fd, uid, gid);
1500
perror_plus("fchown");
1504
TEMP_FAILURE_RETRY(close(pubkey_fd));
1507
/* Lower privileges */
1511
perror_plus("seteuid");
1516
1449
avahi_set_log_function(empty_log);
1715
/* Sleep checking until interface is running.
1716
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 */
1717
1649
for(int i=0; i < delay * 4; i++){
1718
1650
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1912
1844
fprintf(stderr, "Starting Avahi loop search\n");
1915
ret = avahi_loop_with_timeout(mc.simple_poll,
1916
(int)(retry_interval * 1000));
1847
ret = avahi_loop_with_timeout(mc.simple_poll, (int)(retry_interval * 1000));
1918
1849
fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1919
1850
(ret == 0) ? "successfully" : "with error");
1945
1876
gpgme_release(mc.ctx);
1948
/* 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 */
1950
1880
if(mc.current_server != NULL){
1951
1881
mc.current_server->prev->next = NULL;
1952
1882
while(mc.current_server != NULL){