163
154
server *current_server;
164
155
} mandos_context;
166
/* global so signal handler can reach it*/
167
AvahiSimplePoll *simple_poll;
157
/* global context so signal handler can reach it*/
158
mandos_context mc = { .simple_poll = NULL, .server = NULL,
159
.dh_bits = 1024, .priority = "SECURE256"
160
":!CTYPE-X.509:+CTYPE-OPENPGP",
161
.current_server = NULL };
169
163
sig_atomic_t quit_now = 0;
170
164
int signal_received = 0;
172
166
/* Function to use when printing errors */
173
167
void perror_plus(const char *print_text){
175
168
fprintf(stderr, "Mandos plugin %s: ",
176
169
program_invocation_short_name);
178
170
perror(print_text);
181
__attribute__((format (gnu_printf, 2, 3)))
182
int fprintf_plus(FILE *stream, const char *format, ...){
184
va_start (ap, format);
186
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
187
program_invocation_short_name));
188
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
192
174
* Make additional room in "buffer" for at least BUFFER_SIZE more
193
175
* bytes. "buffer_capacity" is how much is currently allocated,
194
176
* "buffer_length" is how much is already used.
196
178
size_t incbuffer(char **buffer, size_t buffer_length,
197
size_t buffer_capacity){
179
size_t buffer_capacity){
198
180
if(buffer_length + BUFFER_SIZE > buffer_capacity){
199
181
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
200
182
if(buffer == NULL){
205
187
return buffer_capacity;
208
/* Add server to set of servers to retry periodically */
209
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
210
int af, server **current_server){
190
int add_server(const char *ip, uint16_t port,
191
AvahiIfIndex if_index,
212
194
server *new_server = malloc(sizeof(server));
213
195
if(new_server == NULL){
214
196
perror_plus("malloc");
217
199
*new_server = (server){ .ip = strdup(ip),
219
.if_index = if_index,
201
.if_index = if_index,
221
203
if(new_server->ip == NULL){
222
204
perror_plus("strdup");
225
/* Special case of first server */
226
if(*current_server == NULL){
207
/* unique case of first server */
208
if (mc.current_server == NULL){
227
209
new_server->next = new_server;
228
210
new_server->prev = new_server;
229
*current_server = new_server;
230
/* Place the new server last in the list */
211
mc.current_server = new_server;
212
/* Placing the new server last in the list */
232
new_server->next = *current_server;
233
new_server->prev = (*current_server)->prev;
214
new_server->next = mc.current_server;
215
new_server->prev = mc.current_server->prev;
234
216
new_server->prev->next = new_server;
235
(*current_server)->prev = new_server;
217
mc.current_server->prev = new_server;
237
ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
219
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
239
221
perror_plus("clock_gettime");
246
228
* Initialize GPGME.
248
static bool init_gpgme(const char *seckey, const char *pubkey,
249
const char *tempdir, mandos_context *mc){
230
static bool init_gpgme(const char *seckey,
231
const char *pubkey, const char *tempdir){
250
232
gpgme_error_t rc;
251
233
gpgme_engine_info_t engine_info;
254
237
* Helper function to insert pub and seckey to the engine keyring.
351
331
ssize_t plaintext_length = 0;
354
fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
334
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
357
337
/* Create new GPGME data buffer from memory cryptotext */
358
338
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
360
340
if(rc != GPG_ERR_NO_ERROR){
361
fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
362
gpgme_strsource(rc), gpgme_strerror(rc));
341
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
342
gpgme_strsource(rc), gpgme_strerror(rc));
366
346
/* Create new empty GPGME data buffer for the plaintext */
367
347
rc = gpgme_data_new(&dh_plain);
368
348
if(rc != GPG_ERR_NO_ERROR){
369
fprintf_plus(stderr, "Mandos plugin mandos-client: "
370
"bad gpgme_data_new: %s: %s\n",
371
gpgme_strsource(rc), gpgme_strerror(rc));
349
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
350
gpgme_strsource(rc), gpgme_strerror(rc));
372
351
gpgme_data_release(dh_crypto);
376
355
/* Decrypt data from the cryptotext data buffer to the plaintext
378
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
357
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
379
358
if(rc != GPG_ERR_NO_ERROR){
380
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
381
gpgme_strsource(rc), gpgme_strerror(rc));
359
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
360
gpgme_strsource(rc), gpgme_strerror(rc));
382
361
plaintext_length = -1;
384
363
gpgme_decrypt_result_t result;
385
result = gpgme_op_decrypt_result(mc->ctx);
364
result = gpgme_op_decrypt_result(mc.ctx);
386
365
if(result == NULL){
387
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
366
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
389
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
390
result->unsupported_algorithm);
391
fprintf_plus(stderr, "Wrong key usage: %u\n",
392
result->wrong_key_usage);
368
fprintf(stderr, "Unsupported algorithm: %s\n",
369
result->unsupported_algorithm);
370
fprintf(stderr, "Wrong key usage: %u\n",
371
result->wrong_key_usage);
393
372
if(result->file_name != NULL){
394
fprintf_plus(stderr, "File name: %s\n", result->file_name);
373
fprintf(stderr, "File name: %s\n", result->file_name);
396
375
gpgme_recipient_t recipient;
397
376
recipient = result->recipients;
398
377
while(recipient != NULL){
399
fprintf_plus(stderr, "Public key algorithm: %s\n",
400
gpgme_pubkey_algo_name
401
(recipient->pubkey_algo));
402
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
403
fprintf_plus(stderr, "Secret key available: %s\n",
404
recipient->status == GPG_ERR_NO_SECKEY
378
fprintf(stderr, "Public key algorithm: %s\n",
379
gpgme_pubkey_algo_name(recipient->pubkey_algo));
380
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
381
fprintf(stderr, "Secret key available: %s\n",
382
recipient->status == GPG_ERR_NO_SECKEY
406
384
recipient = recipient->next;
505
483
/* OpenPGP credentials */
506
ret = gnutls_certificate_allocate_credentials(&mc->cred);
484
ret = gnutls_certificate_allocate_credentials(&mc.cred);
507
485
if(ret != GNUTLS_E_SUCCESS){
508
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
509
safer_gnutls_strerror(ret));
486
fprintf(stderr, "GnuTLS memory error: %s\n",
487
safer_gnutls_strerror(ret));
510
488
gnutls_global_deinit();
515
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
516
" secret key %s as GnuTLS credentials\n",
493
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
494
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
521
498
ret = gnutls_certificate_set_openpgp_key_file
522
(mc->cred, pubkeyfilename, seckeyfilename,
499
(mc.cred, pubkeyfilename, seckeyfilename,
523
500
GNUTLS_OPENPGP_FMT_BASE64);
524
501
if(ret != GNUTLS_E_SUCCESS){
526
"Error[%d] while reading the OpenPGP key pair ('%s',"
527
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
528
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
529
safer_gnutls_strerror(ret));
503
"Error[%d] while reading the OpenPGP key pair ('%s',"
504
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
505
fprintf(stderr, "The GnuTLS error is: %s\n",
506
safer_gnutls_strerror(ret));
533
510
/* GnuTLS server initialization */
534
ret = gnutls_dh_params_init(&mc->dh_params);
511
ret = gnutls_dh_params_init(&mc.dh_params);
535
512
if(ret != GNUTLS_E_SUCCESS){
536
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
537
" initialization: %s\n",
538
safer_gnutls_strerror(ret));
513
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
514
" %s\n", safer_gnutls_strerror(ret));
541
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
517
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
542
518
if(ret != GNUTLS_E_SUCCESS){
543
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
544
safer_gnutls_strerror(ret));
519
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
520
safer_gnutls_strerror(ret));
548
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
524
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
554
gnutls_certificate_free_credentials(mc->cred);
530
gnutls_certificate_free_credentials(mc.cred);
555
531
gnutls_global_deinit();
556
gnutls_dh_params_deinit(mc->dh_params);
532
gnutls_dh_params_deinit(mc.dh_params);
560
static int init_gnutls_session(gnutls_session_t *session,
536
static int init_gnutls_session(gnutls_session_t *session){
563
538
/* GnuTLS session creation */
1029
999
char ip[AVAHI_ADDRESS_STR_MAX];
1030
1000
avahi_address_snprint(ip, sizeof(ip), address);
1032
fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1033
PRIdMAX ") on port %" PRIu16 "\n", name,
1034
host_name, ip, (intmax_t)interface, port);
1002
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
1003
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1004
ip, (intmax_t)interface, port);
1036
int ret = start_mandos_communication(ip, (in_port_t)port,
1038
avahi_proto_to_af(proto),
1006
int ret = start_mandos_communication(ip, port, interface,
1007
avahi_proto_to_af(proto));
1041
avahi_simple_poll_quit(simple_poll);
1009
avahi_simple_poll_quit(mc.simple_poll);
1043
if(not add_server(ip, (in_port_t)port, interface,
1044
avahi_proto_to_af(proto),
1045
&((mandos_context*)mc)->current_server)){
1046
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1011
ret = add_server(ip, port, interface,
1012
avahi_proto_to_af(proto));
1121
1078
signal_received = sig;
1122
1079
int old_errno = errno;
1123
1080
/* set main loop to exit */
1124
if(simple_poll != NULL){
1125
avahi_simple_poll_quit(simple_poll);
1081
if(mc.simple_poll != NULL){
1082
avahi_simple_poll_quit(mc.simple_poll);
1127
1084
errno = old_errno;
1130
bool get_flags(const char *ifname, struct ifreq *ifr){
1134
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1137
perror_plus("socket");
1141
strcpy(ifr->ifr_name, ifname);
1142
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1088
* This function determines if a directory entry in /sys/class/net
1089
* corresponds to an acceptable network device.
1090
* (This function is passed to scandir(3) as a filter function.)
1092
int good_interface(const struct dirent *if_entry){
1094
char *flagname = NULL;
1095
if(if_entry->d_name[0] == '.'){
1098
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1101
perror_plus("asprintf");
1104
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1106
perror_plus("open");
1111
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1112
/* read line from flags_fd */
1113
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1114
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
flagstring[(size_t)to_read] = '\0';
1116
if(flagstring == NULL){
1117
perror_plus("malloc");
1122
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1125
perror_plus("read");
1139
tmpmax = strtoimax(flagstring, &tmp, 0);
1140
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1141
and not (isspace(*tmp)))
1142
or tmpmax != (ifreq_flags)tmpmax){
1146
perror_plus("ioctl SIOCGIFFLAGS");
1144
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1145
flagstring, if_entry->d_name);
1154
bool good_flags(const char *ifname, const struct ifreq *ifr){
1151
ifreq_flags flags = (ifreq_flags)tmpmax;
1156
1152
/* Reject the loopback device */
1157
if(ifr->ifr_flags & IFF_LOOPBACK){
1153
if(flags & IFF_LOOPBACK){
1159
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1155
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1164
1160
/* Accept point-to-point devices only if connect_to is specified */
1165
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1161
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1167
fprintf_plus(stderr, "Accepting point-to-point interface"
1168
" \"%s\"\n", ifname);
1163
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1172
1168
/* Otherwise, reject non-broadcast-capable devices */
1173
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1169
if(not (flags & IFF_BROADCAST)){
1175
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1176
" \"%s\"\n", ifname);
1171
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1180
1176
/* Reject non-ARP interfaces (including dummy interfaces) */
1181
if(ifr->ifr_flags & IFF_NOARP){
1177
if(flags & IFF_NOARP){
1183
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1179
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1189
1184
/* Accept this device */
1191
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1197
* This function determines if a directory entry in /sys/class/net
1198
* corresponds to an acceptable network device.
1199
* (This function is passed to scandir(3) as a filter function.)
1201
int good_interface(const struct dirent *if_entry){
1202
if(if_entry->d_name[0] == '.'){
1207
if(not get_flags(if_entry->d_name, &ifr)){
1209
fprintf_plus(stderr, "Failed to get flags for interface "
1210
"\"%s\"\n", if_entry->d_name);
1215
if(not good_flags(if_entry->d_name, &ifr)){
1186
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1222
* This function determines if a network interface is up.
1224
bool interface_is_up(const char *interface){
1226
if(not get_flags(interface, &ifr)){
1228
fprintf_plus(stderr, "Failed to get flags for interface "
1229
"\"%s\"\n", interface);
1234
return (bool)(ifr.ifr_flags & IFF_UP);
1238
* This function determines if a network interface is running
1240
bool interface_is_running(const char *interface){
1242
if(not get_flags(interface, &ifr)){
1244
fprintf_plus(stderr, "Failed to get flags for interface "
1245
"\"%s\"\n", interface);
1250
return (bool)(ifr.ifr_flags & IFF_RUNNING);
1253
1192
int notdotentries(const struct dirent *direntry){
1254
1193
/* Skip "." and ".." */
1255
1194
if(direntry->d_name[0] == '.'
1264
/* Is this directory entry a runnable program? */
1265
int runnable_hook(const struct dirent *direntry){
1270
if((direntry->d_name)[0] == '\0'){
1275
sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1276
"abcdefghijklmnopqrstuvwxyz"
1279
if((direntry->d_name)[sret] != '\0'){
1280
/* Contains non-allowed characters */
1282
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1288
char *fullname = NULL;
1289
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1291
perror_plus("asprintf");
1295
ret = stat(fullname, &st);
1298
perror_plus("Could not stat hook");
1302
if(not (S_ISREG(st.st_mode))){
1303
/* Not a regular file */
1305
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1310
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1311
/* Not executable */
1313
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1319
fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1325
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1326
mandos_context *mc){
1203
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1328
1205
struct timespec now;
1329
1206
struct timespec waited_time;
1330
1207
intmax_t block_time;
1333
if(mc->current_server == NULL){
1210
if(mc.current_server == NULL){
1335
fprintf_plus(stderr, "Wait until first server is found."
1213
"Wait until first server is found. No timeout!\n");
1338
1215
ret = avahi_simple_poll_iterate(s, -1);
1341
fprintf_plus(stderr, "Check current_server if we should run"
1218
fprintf(stderr, "Check current_server if we should run it,"
1344
1221
/* the current time */
1345
1222
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1350
1227
/* Calculating in ms how long time between now and server
1351
1228
who we visted longest time ago. Now - last seen. */
1352
1229
waited_time.tv_sec = (now.tv_sec
1353
- mc->current_server->last_seen.tv_sec);
1230
- mc.current_server->last_seen.tv_sec);
1354
1231
waited_time.tv_nsec = (now.tv_nsec
1355
- mc->current_server->last_seen.tv_nsec);
1232
- mc.current_server->last_seen.tv_nsec);
1356
1233
/* total time is 10s/10,000ms.
1357
1234
Converting to s from ms by dividing by 1,000,
1358
1235
and ns to ms by dividing by 1,000,000. */
1359
1236
block_time = ((retry_interval
1360
1237
- ((intmax_t)waited_time.tv_sec * 1000))
1361
1238
- ((intmax_t)waited_time.tv_nsec / 1000000));
1364
fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1241
fprintf(stderr, "Blocking for %ld ms\n", block_time);
1368
1244
if(block_time <= 0){
1369
ret = start_mandos_communication(mc->current_server->ip,
1370
mc->current_server->port,
1371
mc->current_server->if_index,
1372
mc->current_server->af, mc);
1245
ret = start_mandos_communication(mc.current_server->ip,
1246
mc.current_server->port,
1247
mc.current_server->if_index,
1248
mc.current_server->af);
1374
avahi_simple_poll_quit(s);
1250
avahi_simple_poll_quit(mc.simple_poll);
1377
1253
ret = clock_gettime(CLOCK_MONOTONIC,
1378
&mc->current_server->last_seen);
1254
&mc.current_server->last_seen);
1380
1256
perror_plus("clock_gettime");
1383
mc->current_server = mc->current_server->next;
1259
mc.current_server = mc.current_server->next;
1384
1260
block_time = 0; /* Call avahi to find new Mandos
1385
1261
servers, but don't block */
1388
1264
ret = avahi_simple_poll_iterate(s, (int)block_time);
1391
if (ret > 0 or errno != EINTR){
1267
if (ret > 0 or errno != EINTR) {
1392
1268
return (ret != 1) ? ret : 0;
1398
/* Set effective uid to 0, return errno */
1399
error_t raise_privileges(void){
1400
error_t old_errno = errno;
1401
error_t ret_errno = 0;
1402
if(seteuid(0) == -1){
1404
perror_plus("seteuid");
1410
/* Set effective and real user ID to 0. Return errno. */
1411
error_t raise_privileges_permanently(void){
1412
error_t old_errno = errno;
1413
error_t ret_errno = raise_privileges();
1418
if(setuid(0) == -1){
1420
perror_plus("seteuid");
1426
/* Set effective user ID to unprivileged saved user ID */
1427
error_t lower_privileges(void){
1428
error_t old_errno = errno;
1429
error_t ret_errno = 0;
1430
if(seteuid(uid) == -1){
1432
perror_plus("seteuid");
1438
/* Lower privileges permanently */
1439
error_t lower_privileges_permanently(void){
1440
error_t old_errno = errno;
1441
error_t ret_errno = 0;
1442
if(setuid(uid) == -1){
1444
perror_plus("setuid");
1450
bool run_network_hooks(const char *mode, const char *interface,
1452
struct dirent **direntries;
1453
struct dirent *direntry;
1455
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1458
if(errno == ENOENT){
1460
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1461
" found\n", hookdir);
1464
perror_plus("scandir");
1467
int devnull = open("/dev/null", O_RDONLY);
1468
for(int i = 0; i < numhooks; i++){
1469
direntry = direntries[i];
1470
char *fullname = NULL;
1471
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1473
perror_plus("asprintf");
1477
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1480
pid_t hook_pid = fork();
1483
/* Raise privileges */
1484
raise_privileges_permanently();
1489
perror_plus("setgid");
1491
/* Reset supplementary groups */
1493
ret = setgroups(0, NULL);
1495
perror_plus("setgroups");
1497
dup2(devnull, STDIN_FILENO);
1499
dup2(STDERR_FILENO, STDOUT_FILENO);
1500
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1502
perror_plus("setenv");
1505
ret = setenv("DEVICE", interface, 1);
1507
perror_plus("setenv");
1510
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1512
perror_plus("setenv");
1515
ret = setenv("MODE", mode, 1);
1517
perror_plus("setenv");
1521
ret = asprintf(&delaystring, "%f", delay);
1523
perror_plus("asprintf");
1526
ret = setenv("DELAY", delaystring, 1);
1529
perror_plus("setenv");
1533
if(connect_to != NULL){
1534
ret = setenv("CONNECT", connect_to, 1);
1536
perror_plus("setenv");
1540
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1541
perror_plus("execl");
1542
_exit(EXIT_FAILURE);
1546
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1547
perror_plus("waitpid");
1551
if(WIFEXITED(status)){
1552
if(WEXITSTATUS(status) != 0){
1553
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1554
" with status %d\n", direntry->d_name,
1555
WEXITSTATUS(status));
1559
} else if(WIFSIGNALED(status)){
1560
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1561
" signal %d\n", direntry->d_name,
1566
fprintf_plus(stderr, "Warning: network hook \"%s\""
1567
" crashed\n", direntry->d_name);
1574
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1583
error_t bring_up_interface(const char *const interface,
1586
error_t old_errno = errno;
1587
error_t ret_errno = 0;
1588
int ret, ret_setflags;
1589
struct ifreq network;
1590
unsigned int if_index = if_nametoindex(interface);
1592
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1602
if(not interface_is_up(interface)){
1603
if(not get_flags(interface, &network) and debug){
1605
fprintf_plus(stderr, "Failed to get flags for interface "
1606
"\"%s\"\n", interface);
1609
network.ifr_flags |= IFF_UP;
1611
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1614
perror_plus("socket");
1626
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1630
/* Raise priviliges */
1634
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1635
messages about the network interface to mess up the prompt */
1636
int ret_linux = klogctl(8, NULL, 5);
1637
bool restore_loglevel = true;
1638
if(ret_linux == -1){
1639
restore_loglevel = false;
1640
perror_plus("klogctl");
1642
#endif /* __linux__ */
1643
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1646
if(restore_loglevel){
1647
ret_linux = klogctl(7, NULL, 0);
1648
if(ret_linux == -1){
1649
perror_plus("klogctl");
1652
#endif /* __linux__ */
1654
/* Lower privileges */
1657
/* Close the socket */
1658
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1660
perror_plus("close");
1663
if(ret_setflags == -1){
1665
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1670
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1674
/* Sleep checking until interface is running.
1675
Check every 0.25s, up to total time of delay */
1676
for(int i=0; i < delay * 4; i++){
1677
if(interface_is_running(interface)){
1680
struct timespec sleeptime = { .tv_nsec = 250000000 };
1681
ret = nanosleep(&sleeptime, NULL);
1682
if(ret == -1 and errno != EINTR){
1683
perror_plus("nanosleep");
1691
error_t take_down_interface(const char *const interface){
1693
error_t old_errno = errno;
1694
error_t ret_errno = 0;
1695
int ret, ret_setflags;
1696
struct ifreq network;
1697
unsigned int if_index = if_nametoindex(interface);
1699
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1703
if(interface_is_up(interface)){
1704
if(not get_flags(interface, &network) and debug){
1706
fprintf_plus(stderr, "Failed to get flags for interface "
1707
"\"%s\"\n", interface);
1710
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1712
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1715
perror_plus("socket");
1721
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1725
/* Raise priviliges */
1728
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1731
/* Lower privileges */
1734
/* Close the socket */
1735
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1737
perror_plus("close");
1740
if(ret_setflags == -1){
1742
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1747
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1755
1274
int main(int argc, char *argv[]){
1756
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1757
.priority = "SECURE256:!CTYPE-X.509:"
1758
"+CTYPE-OPENPGP", .current_server = NULL };
1759
1275
AvahiSServiceBrowser *sb = NULL;
1762
1278
intmax_t tmpmax;
1764
1280
int exitcode = EXIT_SUCCESS;
1765
char *interfaces = NULL;
1766
size_t interfaces_size = 0;
1767
char *interfaces_to_take_down = NULL;
1768
size_t interfaces_to_take_down_size = 0;
1281
const char *interface = "";
1282
struct ifreq network;
1284
bool take_down_interface = false;
1769
1287
char tempdir[] = "/tmp/mandosXXXXXX";
1770
1288
bool tempdir_created = false;
1771
1289
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1772
1290
const char *seckey = PATHDIR "/" SECKEY;
1773
1291
const char *pubkey = PATHDIR "/" PUBKEY;
1774
char *interfaces_hooks = NULL;
1775
size_t interfaces_hooks_size = 0;
1777
1293
bool gnutls_initialized = false;
1778
1294
bool gpgme_initialized = false;
1956
1460
/* Work around Debian bug #633582:
1957
1461
<http://bugs.debian.org/633582> */
1959
1464
/* Re-raise priviliges */
1960
if(raise_privileges() == 0){
1963
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1964
int seckey_fd = open(seckey, O_RDONLY);
1965
if(seckey_fd == -1){
1966
perror_plus("open");
1968
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1970
perror_plus("fstat");
1972
if(S_ISREG(st.st_mode)
1973
and st.st_uid == 0 and st.st_gid == 0){
1974
ret = fchown(seckey_fd, uid, gid);
1976
perror_plus("fchown");
1980
TEMP_FAILURE_RETRY(close(seckey_fd));
1984
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1985
int pubkey_fd = open(pubkey, O_RDONLY);
1986
if(pubkey_fd == -1){
1987
perror_plus("open");
1989
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1991
perror_plus("fstat");
1993
if(S_ISREG(st.st_mode)
1994
and st.st_uid == 0 and st.st_gid == 0){
1995
ret = fchown(pubkey_fd, uid, gid);
1997
perror_plus("fchown");
2001
TEMP_FAILURE_RETRY(close(pubkey_fd));
2005
/* Lower privileges */
2009
perror_plus("seteuid");
2014
/* Remove empty interface names */
2016
char *interface = NULL;
2017
while((interface = argz_next(interfaces, interfaces_size,
2019
if(if_nametoindex(interface) == 0){
2020
if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2021
fprintf_plus(stderr, "Not using nonexisting interface"
2022
" \"%s\"\n", interface);
2024
argz_delete(&interfaces, &interfaces_size, interface);
2030
/* Run network hooks */
2033
if(interfaces != NULL){
2034
interfaces_hooks = malloc(interfaces_size);
2035
if(interfaces_hooks == NULL){
2036
perror_plus("malloc");
2039
memcpy(interfaces_hooks, interfaces, interfaces_size);
2040
interfaces_hooks_size = interfaces_size;
2041
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2044
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2045
interfaces_hooks : "", delay)){
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");
2051
1516
avahi_set_log_function(empty_log);
1519
if(interface[0] == '\0'){
1520
struct dirent **direntries;
1521
ret = scandir(sys_class_net, &direntries, good_interface,
1524
/* Pick the first good interface */
1525
interface = strdup(direntries[0]->d_name);
1527
fprintf(stderr, "Using interface \"%s\"\n", interface);
1529
if(interface == NULL){
1530
perror_plus("malloc");
1532
exitcode = EXIT_FAILURE;
1538
fprintf(stderr, "Could not find a network interface\n");
1539
exitcode = EXIT_FAILURE;
2054
1544
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
2055
1545
from the signal handler */
2056
1546
/* Initialize the pseudo-RNG for Avahi */
2057
1547
srand((unsigned int) time(NULL));
2058
simple_poll = avahi_simple_poll_new();
2059
if(simple_poll == NULL){
2060
fprintf_plus(stderr,
2061
"Avahi: Failed to create simple poll object.\n");
1548
mc.simple_poll = avahi_simple_poll_new();
1549
if(mc.simple_poll == NULL){
1550
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
2062
1551
exitcode = EX_UNAVAILABLE;
2129
/* If no interfaces were specified, make a list */
2130
if(interfaces == NULL){
2131
struct dirent **direntries;
2132
/* Look for any good interfaces */
2133
ret = scandir(sys_class_net, &direntries, good_interface,
2136
/* Add all found interfaces to interfaces list */
2137
for(int i = 0; i < ret; ++i){
2138
ret_errno = argz_add(&interfaces, &interfaces_size,
2139
direntries[i]->d_name);
2141
perror_plus("argz_add");
2145
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2146
direntries[i]->d_name);
1618
/* If the interface is down, bring it up */
1619
if(strcmp(interface, "none") != 0){
1620
if_index = (AvahiIfIndex) if_nametoindex(interface);
1622
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1623
exitcode = EX_UNAVAILABLE;
1631
/* Re-raise priviliges */
1635
perror_plus("seteuid");
1639
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1640
messages about the network interface to mess up the prompt */
1641
ret = klogctl(8, NULL, 5);
1642
bool restore_loglevel = true;
1644
restore_loglevel = false;
1645
perror_plus("klogctl");
1647
#endif /* __linux__ */
1649
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1651
perror_plus("socket");
1652
exitcode = EX_OSERR;
1654
if(restore_loglevel){
1655
ret = klogctl(7, NULL, 0);
1657
perror_plus("klogctl");
1660
#endif /* __linux__ */
1661
/* Lower privileges */
1665
perror_plus("seteuid");
1669
strcpy(network.ifr_name, interface);
1670
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1672
perror_plus("ioctl SIOCGIFFLAGS");
1674
if(restore_loglevel){
1675
ret = klogctl(7, NULL, 0);
1677
perror_plus("klogctl");
1680
#endif /* __linux__ */
1681
exitcode = EX_OSERR;
1682
/* Lower privileges */
1686
perror_plus("seteuid");
1690
if((network.ifr_flags & IFF_UP) == 0){
1691
network.ifr_flags |= IFF_UP;
1692
take_down_interface = true;
1693
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1695
take_down_interface = false;
1696
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1697
exitcode = EX_OSERR;
1699
if(restore_loglevel){
1700
ret = klogctl(7, NULL, 0);
1702
perror_plus("klogctl");
1705
#endif /* __linux__ */
1706
/* Lower privileges */
1710
perror_plus("seteuid");
1715
/* Sleep checking until interface is running.
1716
Check every 0.25s, up to total time of delay */
1717
for(int i=0; i < delay * 4; i++){
1718
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1720
perror_plus("ioctl SIOCGIFFLAGS");
1721
} else if(network.ifr_flags & IFF_RUNNING){
1724
struct timespec sleeptime = { .tv_nsec = 250000000 };
1725
ret = nanosleep(&sleeptime, NULL);
1726
if(ret == -1 and errno != EINTR){
1727
perror_plus("nanosleep");
1730
if(not take_down_interface){
1731
/* We won't need the socket anymore */
1732
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1734
perror_plus("close");
1738
if(restore_loglevel){
1739
/* Restores kernel loglevel to default */
1740
ret = klogctl(7, NULL, 0);
1742
perror_plus("klogctl");
1745
#endif /* __linux__ */
1746
/* Lower privileges */
1748
if(take_down_interface){
1749
/* Lower privileges */
1752
perror_plus("seteuid");
2152
fprintf_plus(stderr, "Could not find a network interface\n");
2153
exitcode = EXIT_FAILURE;
2158
/* If we only got one interface, explicitly use only that one */
2159
if(argz_count(interfaces, interfaces_size) == 1){
2161
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2164
if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2167
/* Bring up interfaces which are down */
2168
if(not (argz_count(interfaces, interfaces_size) == 1
2169
and strcmp(interfaces, "none") == 0)){
2170
char *interface = NULL;
2171
while((interface = argz_next(interfaces, interfaces_size,
2173
bool interface_was_up = interface_is_up(interface);
2174
ret = bring_up_interface(interface, delay);
2175
if(not interface_was_up){
2178
perror_plus("Failed to bring up interface");
2180
ret_errno = argz_add(&interfaces_to_take_down,
2181
&interfaces_to_take_down_size,
1755
/* Lower privileges permanently */
1758
perror_plus("setuid");
2188
interfaces_size = 0;
2189
if(debug and (interfaces_to_take_down == NULL)){
2190
fprintf_plus(stderr, "No interfaces were brought up\n");
2198
ret = init_gnutls_global(pubkey, seckey, &mc);
1767
ret = init_gnutls_global(pubkey, seckey);
2200
fprintf_plus(stderr, "init_gnutls_global failed\n");
1769
fprintf(stderr, "init_gnutls_global failed\n");
2201
1770
exitcode = EX_UNAVAILABLE;
2402
/* Re-raise priviliges */
2406
/* Run network hooks */
2407
run_network_hooks("stop", interfaces_hooks != NULL ?
2408
interfaces_hooks : "", delay);
2410
/* Take down the network interfaces which were brought up */
2412
char *interface = NULL;
2413
while((interface=argz_next(interfaces_to_take_down,
2414
interfaces_to_take_down_size,
2416
ret_errno = take_down_interface(interface);
2419
perror_plus("Failed to take down interface");
1959
/* Take down the network interface */
1960
if(take_down_interface){
1961
/* Re-raise priviliges */
1965
perror_plus("seteuid");
1968
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1970
perror_plus("ioctl SIOCGIFFLAGS");
1971
} else if(network.ifr_flags & IFF_UP) {
1972
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1973
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1975
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2422
if(debug and (interfaces_to_take_down == NULL)){
2423
fprintf_plus(stderr, "No interfaces needed to be taken"
1978
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1980
perror_plus("close");
1982
/* Lower privileges permanently */
1986
perror_plus("setuid");
2428
lower_privileges_permanently();
2431
free(interfaces_to_take_down);
2432
free(interfaces_hooks);
2434
1991
/* Removes the GPGME temp directory and all files inside */
2435
1992
if(tempdir_created){
2436
1993
struct dirent **direntries = NULL;
2437
1994
struct dirent *direntry = NULL;
2438
int numentries = scandir(tempdir, &direntries, notdotentries,
2440
if (numentries > 0){
2441
for(int i = 0; i < numentries; i++){
1995
ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1997
for(int i = 0; i < ret; i++){
2442
1998
direntry = direntries[i];
2443
1999
char *fullname = NULL;
2444
2000
ret = asprintf(&fullname, "%s/%s", tempdir,