26
26
* along with this program. If not, see
27
27
* <http://www.gnu.org/licenses/>.
29
* Contact the authors at <mandos@fukt.bsnet.se>.
29
* Contact the authors at <mandos@recompile.se>.
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
62
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
64
64
#include <assert.h> /* assert() */
65
#include <errno.h> /* perror(), errno, program_invocation_short_name */
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,
126
127
bool debug = false;
127
128
static const char mandos_protocol_version[] = "1";
128
129
const char *argp_program_version = "mandos-client " VERSION;
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
130
const char *argp_program_bug_address = "<mandos@recompile.se>";
130
131
static const char sys_class_net[] = "/sys/class/net";
131
132
char *connect_to = NULL;
133
/* Doubly linked list that need to be circular linked when ever used */
134
/* Doubly linked list that need to be circularly linked when used */
134
135
typedef struct server{
156
157
/* global context so signal handler can reach it*/
157
158
mandos_context mc = { .simple_poll = NULL, .server = NULL,
158
159
.dh_bits = 1024, .priority = "SECURE256"
159
":!CTYPE-X.509:+CTYPE-OPENPGP", .current_server = NULL };
160
":!CTYPE-X.509:+CTYPE-OPENPGP",
161
.current_server = NULL };
161
163
sig_atomic_t quit_now = 0;
162
164
int signal_received = 0;
164
166
/* Function to use when printing errors */
165
167
void perror_plus(const char *print_text){
166
fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
168
fprintf(stderr, "Mandos plugin %s: ",
169
program_invocation_short_name);
167
170
perror(print_text);
200
204
if(new_server->ip == NULL){
201
205
perror_plus("strdup");
204
/* uniqe case of first server */
208
/* Special case of first server */
205
209
if (mc.current_server == NULL){
206
210
new_server->next = new_server;
207
211
new_server->prev = new_server;
208
212
mc.current_server = new_server;
209
/* Placing the new server last in the list */
213
/* Place the new server last in the list */
211
215
new_server->next = mc.current_server;
212
216
new_server->prev = mc.current_server->prev;
282
/* Set GPGME home directory for the OpenPGP engine only */
286
/* Set GPGME home directory for the OpenPGP engine only */
283
287
rc = gpgme_get_engine_info(&engine_info);
284
288
if(rc != GPG_ERR_NO_ERROR){
285
289
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
480
484
/* OpenPGP credentials */
481
gnutls_certificate_allocate_credentials(&mc.cred);
485
ret = gnutls_certificate_allocate_credentials(&mc.cred);
482
486
if(ret != GNUTLS_E_SUCCESS){
483
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
487
fprintf(stderr, "GnuTLS memory error: %s\n",
487
488
safer_gnutls_strerror(ret));
488
489
gnutls_global_deinit();
1110
1111
free(flagname);
1111
1112
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1112
1113
/* read line from flags_fd */
1113
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1114
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1114
1115
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
1116
flagstring[(size_t)to_read] = '\0';
1116
1117
if(flagstring == NULL){
1205
1206
struct timespec now;
1206
1207
struct timespec waited_time;
1207
1208
intmax_t block_time;
1210
1211
if(mc.current_server == NULL){
1212
fprintf(stderr, "Wait until first server is found. No timeout!\n");
1214
"Wait until first server is found. No timeout!\n");
1214
1216
ret = avahi_simple_poll_iterate(s, -1);
1217
fprintf(stderr, "Check current_server if we should run it, or wait\n");
1219
fprintf(stderr, "Check current_server if we should run it,"
1219
1222
/* the current time */
1220
1223
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1225
1228
/* Calculating in ms how long time between now and server
1226
1229
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);
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));
1233
fprintf(stderr, "Blocking for %ld ms\n", block_time);
1242
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1236
1245
if(block_time <= 0){
1237
1246
ret = start_mandos_communication(mc.current_server->ip,
1238
mc.current_server->port,
1239
mc.current_server->if_index,
1240
mc.current_server->af);
1247
mc.current_server->port,
1248
mc.current_server->if_index,
1249
mc.current_server->af);
1242
1251
avahi_simple_poll_quit(mc.simple_poll);
1245
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
1254
ret = clock_gettime(CLOCK_MONOTONIC,
1255
&mc.current_server->last_seen);
1247
1257
perror_plus("clock_gettime");
1250
1260
mc.current_server = mc.current_server->next;
1251
block_time = 0; /* call avahi to find new mandos servers, but dont block */
1261
block_time = 0; /* Call avahi to find new Mandos
1262
servers, but don't block */
1254
1265
ret = avahi_simple_poll_iterate(s, (int)block_time);
1283
1294
bool gnutls_initialized = false;
1284
1295
bool gpgme_initialized = false;
1285
1296
float delay = 2.5f;
1286
double retry_interval = 10; /* 10s between retrying a server and checking again*/
1297
double retry_interval = 10; /* 10s between trying a server and
1298
retrying the same server again */
1288
1300
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1289
1301
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1401
1413
retry_interval = strtod(arg, &tmp);
1402
1414
if(errno != 0 or tmp == arg or *tmp != '\0'
1403
or (retry_interval * 1000) > INT_MAX){
1415
or (retry_interval * 1000) > INT_MAX
1416
or retry_interval < 0){
1404
1417
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");
1449
1522
avahi_set_log_function(empty_log);
1648
/* sleep checking until interface is running. Check every 0.25s, up to total time of delay */
1721
/* Sleep checking until interface is running.
1722
Check every 0.25s, up to total time of delay */
1649
1723
for(int i=0; i < delay * 4; i++){
1650
1724
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1762
1836
port = (uint16_t)tmpmax;
1763
1837
*address = '\0';
1764
address = connect_to;
1765
1838
/* Colon in address indicates IPv6 */
1767
if(strchr(address, ':') != NULL){
1840
if(strchr(connect_to, ':') != NULL){
1842
/* Accept [] around IPv6 address - see RFC 5952 */
1843
if(connect_to[0] == '[' and address[-1] == ']')
1851
address = connect_to;
1777
1857
while(not quit_now){
1778
1858
ret = start_mandos_communication(address, port, if_index, af);
1779
1859
if(quit_now or ret == 0){
1782
sleep((int)retry_interval or 1);
1863
fprintf(stderr, "Retrying in %d seconds\n",
1864
(int)retry_interval);
1866
sleep((int)retry_interval);
1785
1869
if (not quit_now){
1786
1870
exitcode = EXIT_SUCCESS;
1844
1928
fprintf(stderr, "Starting Avahi loop search\n");
1847
ret = avahi_loop_with_timeout(mc.simple_poll, (int)(retry_interval * 1000));
1931
ret = avahi_loop_with_timeout(mc.simple_poll,
1932
(int)(retry_interval * 1000));
1849
1934
fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1850
1935
(ret == 0) ? "successfully" : "with error");
1876
1961
gpgme_release(mc.ctx);
1879
/* cleans up the circular linked list of mandos servers the client has seen */
1964
/* Cleans up the circular linked list of Mandos servers the client
1880
1966
if(mc.current_server != NULL){
1881
1967
mc.current_server->prev->next = NULL;
1882
1968
while(mc.current_server != NULL){
1922
2008
if(tempdir_created){
1923
2009
struct dirent **direntries = NULL;
1924
2010
struct dirent *direntry = NULL;
1925
ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1927
for(int i = 0; i < ret; i++){
2011
int numentries = scandir(tempdir, &direntries, notdotentries,
2013
if (numentries > 0){
2014
for(int i = 0; i < numentries; i++){
1928
2015
direntry = direntries[i];
1929
2016
char *fullname = NULL;
1930
2017
ret = asprintf(&fullname, "%s/%s", tempdir,