156
164
const char *priority;
158
166
server *current_server;
168
size_t interfaces_size;
159
169
} mandos_context;
161
/* global context so signal handler can reach it*/
162
mandos_context mc = { .simple_poll = NULL, .server = NULL,
163
.dh_bits = 1024, .priority = "SECURE256"
164
":!CTYPE-X.509:+CTYPE-OPENPGP",
165
.current_server = NULL };
171
/* global so signal handler can reach it*/
172
AvahiSimplePoll *simple_poll;
167
174
sig_atomic_t quit_now = 0;
168
175
int signal_received = 0;
170
177
/* Function to use when printing errors */
171
178
void perror_plus(const char *print_text){
172
180
fprintf(stderr, "Mandos plugin %s: ",
173
181
program_invocation_short_name);
174
183
perror(print_text);
186
__attribute__((format (gnu_printf, 2, 3)))
187
int fprintf_plus(FILE *stream, const char *format, ...){
189
va_start (ap, format);
191
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
192
program_invocation_short_name));
193
return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
178
197
* Make additional room in "buffer" for at least BUFFER_SIZE more
179
198
* bytes. "buffer_capacity" is how much is currently allocated,
207
231
if(new_server->ip == NULL){
208
232
perror_plus("strdup");
235
ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
237
perror_plus("clock_gettime");
211
240
/* Special case of first server */
212
if (mc.current_server == NULL){
241
if(*current_server == NULL){
213
242
new_server->next = new_server;
214
243
new_server->prev = new_server;
215
mc.current_server = new_server;
244
*current_server = new_server;
216
245
/* Place the new server last in the list */
218
new_server->next = mc.current_server;
219
new_server->prev = mc.current_server->prev;
247
new_server->next = *current_server;
248
new_server->prev = (*current_server)->prev;
220
249
new_server->prev->next = new_server;
221
mc.current_server->prev = new_server;
223
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
225
perror_plus("clock_gettime");
250
(*current_server)->prev = new_server;
232
256
* Initialize GPGME.
234
258
static bool init_gpgme(const char *seckey, const char *pubkey,
235
const char *tempdir){
259
const char *tempdir, mandos_context *mc){
236
260
gpgme_error_t rc;
237
261
gpgme_engine_info_t engine_info;
241
264
* Helper function to insert pub and seckey to the engine keyring.
279
fprintf(stderr, "Mandos plugin mandos-client: "
280
"Initializing GPGME\n");
300
fprintf_plus(stderr, "Initializing GPGME\n");
284
304
gpgme_check_version(NULL);
285
305
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
286
306
if(rc != GPG_ERR_NO_ERROR){
287
fprintf(stderr, "Mandos plugin mandos-client: "
288
"bad gpgme_engine_check_version: %s: %s\n",
289
gpgme_strsource(rc), gpgme_strerror(rc));
307
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
308
gpgme_strsource(rc), gpgme_strerror(rc));
293
312
/* Set GPGME home directory for the OpenPGP engine only */
294
313
rc = gpgme_get_engine_info(&engine_info);
295
314
if(rc != GPG_ERR_NO_ERROR){
296
fprintf(stderr, "Mandos plugin mandos-client: "
297
"bad gpgme_get_engine_info: %s: %s\n",
298
gpgme_strsource(rc), gpgme_strerror(rc));
315
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
316
gpgme_strsource(rc), gpgme_strerror(rc));
301
319
while(engine_info != NULL){
342
361
ssize_t plaintext_length = 0;
345
fprintf(stderr, "Mandos plugin mandos-client: "
346
"Trying to decrypt OpenPGP data\n");
364
fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
349
367
/* Create new GPGME data buffer from memory cryptotext */
350
368
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
352
370
if(rc != GPG_ERR_NO_ERROR){
353
fprintf(stderr, "Mandos plugin mandos-client: "
354
"bad gpgme_data_new_from_mem: %s: %s\n",
355
gpgme_strsource(rc), gpgme_strerror(rc));
371
fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
372
gpgme_strsource(rc), gpgme_strerror(rc));
359
376
/* Create new empty GPGME data buffer for the plaintext */
360
377
rc = gpgme_data_new(&dh_plain);
361
378
if(rc != GPG_ERR_NO_ERROR){
362
fprintf(stderr, "Mandos plugin mandos-client: "
363
"bad gpgme_data_new: %s: %s\n",
364
gpgme_strsource(rc), gpgme_strerror(rc));
379
fprintf_plus(stderr, "Mandos plugin mandos-client: "
380
"bad gpgme_data_new: %s: %s\n",
381
gpgme_strsource(rc), gpgme_strerror(rc));
365
382
gpgme_data_release(dh_crypto);
369
386
/* Decrypt data from the cryptotext data buffer to the plaintext
371
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
388
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
372
389
if(rc != GPG_ERR_NO_ERROR){
373
fprintf(stderr, "Mandos plugin mandos-client: "
374
"bad gpgme_op_decrypt: %s: %s\n",
375
gpgme_strsource(rc), gpgme_strerror(rc));
390
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
391
gpgme_strsource(rc), gpgme_strerror(rc));
376
392
plaintext_length = -1;
378
394
gpgme_decrypt_result_t result;
379
result = gpgme_op_decrypt_result(mc.ctx);
395
result = gpgme_op_decrypt_result(mc->ctx);
380
396
if(result == NULL){
381
fprintf(stderr, "Mandos plugin mandos-client: "
382
"gpgme_op_decrypt_result failed\n");
397
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
384
fprintf(stderr, "Mandos plugin mandos-client: "
385
"Unsupported algorithm: %s\n",
386
result->unsupported_algorithm);
387
fprintf(stderr, "Mandos plugin mandos-client: "
388
"Wrong key usage: %u\n",
389
result->wrong_key_usage);
399
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
400
result->unsupported_algorithm);
401
fprintf_plus(stderr, "Wrong key usage: %u\n",
402
result->wrong_key_usage);
390
403
if(result->file_name != NULL){
391
fprintf(stderr, "Mandos plugin mandos-client: "
392
"File name: %s\n", result->file_name);
404
fprintf_plus(stderr, "File name: %s\n", result->file_name);
394
406
gpgme_recipient_t recipient;
395
407
recipient = result->recipients;
396
408
while(recipient != NULL){
397
fprintf(stderr, "Mandos plugin mandos-client: "
398
"Public key algorithm: %s\n",
399
gpgme_pubkey_algo_name(recipient->pubkey_algo));
400
fprintf(stderr, "Mandos plugin mandos-client: "
401
"Key ID: %s\n", recipient->keyid);
402
fprintf(stderr, "Mandos plugin mandos-client: "
403
"Secret key available: %s\n",
404
recipient->status == GPG_ERR_NO_SECKEY
409
fprintf_plus(stderr, "Public key algorithm: %s\n",
410
gpgme_pubkey_algo_name
411
(recipient->pubkey_algo));
412
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
413
fprintf_plus(stderr, "Secret key available: %s\n",
414
recipient->status == GPG_ERR_NO_SECKEY
406
416
recipient = recipient->next;
508
515
/* OpenPGP credentials */
509
ret = gnutls_certificate_allocate_credentials(&mc.cred);
516
ret = gnutls_certificate_allocate_credentials(&mc->cred);
510
517
if(ret != GNUTLS_E_SUCCESS){
511
fprintf(stderr, "Mandos plugin mandos-client: "
512
"GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
518
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
519
safer_gnutls_strerror(ret));
513
520
gnutls_global_deinit();
518
fprintf(stderr, "Mandos plugin mandos-client: "
519
"Attempting to use OpenPGP public key %s and"
520
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
525
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
526
" secret key %s as GnuTLS credentials\n",
524
531
ret = gnutls_certificate_set_openpgp_key_file
525
(mc.cred, pubkeyfilename, seckeyfilename,
532
(mc->cred, pubkeyfilename, seckeyfilename,
526
533
GNUTLS_OPENPGP_FMT_BASE64);
527
534
if(ret != GNUTLS_E_SUCCESS){
529
"Mandos plugin mandos-client: "
530
"Error[%d] while reading the OpenPGP key pair ('%s',"
531
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
532
fprintf(stderr, "Mandos plugin mandos-client: "
533
"The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
536
"Error[%d] while reading the OpenPGP key pair ('%s',"
537
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
538
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
539
safer_gnutls_strerror(ret));
537
543
/* GnuTLS server initialization */
538
ret = gnutls_dh_params_init(&mc.dh_params);
544
ret = gnutls_dh_params_init(&mc->dh_params);
539
545
if(ret != GNUTLS_E_SUCCESS){
540
fprintf(stderr, "Mandos plugin mandos-client: "
541
"Error in GnuTLS DH parameter initialization:"
542
" %s\n", safer_gnutls_strerror(ret));
546
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
547
" initialization: %s\n",
548
safer_gnutls_strerror(ret));
545
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
551
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
546
552
if(ret != GNUTLS_E_SUCCESS){
547
fprintf(stderr, "Mandos plugin mandos-client: "
548
"Error in GnuTLS prime generation: %s\n",
549
safer_gnutls_strerror(ret));
553
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
554
safer_gnutls_strerror(ret));
553
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
558
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
559
gnutls_certificate_free_credentials(mc.cred);
564
gnutls_certificate_free_credentials(mc->cred);
560
565
gnutls_global_deinit();
561
gnutls_dh_params_deinit(mc.dh_params);
566
gnutls_dh_params_deinit(mc->dh_params);
565
static int init_gnutls_session(gnutls_session_t *session){
570
static int init_gnutls_session(gnutls_session_t *session,
567
573
/* GnuTLS session creation */
661
fprintf(stderr, "Mandos plugin mandos-client: "
662
"Bad address family: %d\n", af);
662
fprintf_plus(stderr, "Bad address family: %d\n", af);
667
ret = init_gnutls_session(&session);
667
/* If the interface is specified and we have a list of interfaces */
668
if(if_index != AVAHI_IF_UNSPEC and mc->interfaces != NULL){
669
/* Check if the interface is one of the interfaces we are using */
672
char *interface = NULL;
673
while((interface=argz_next(mc->interfaces, mc->interfaces_size,
675
if(if_nametoindex(interface) == (unsigned int)if_index){
682
/* This interface does not match any in the list, so we don't
683
connect to the server */
685
char interface[IF_NAMESIZE];
686
if(if_indextoname((unsigned int)if_index, interface) == NULL){
687
perror_plus("if_indextoname");
689
fprintf_plus(stderr, "Skipping server on non-used interface"
691
if_indextoname((unsigned int)if_index,
699
ret = init_gnutls_session(&session, mc);
673
fprintf(stderr, "Mandos plugin mandos-client: "
674
"Setting up a TCP connection to %s, port %" PRIu16
705
fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
706
PRIuMAX "\n", ip, (uintmax_t)port);
678
709
tcp_sd = socket(pf, SOCK_STREAM, 0);
744
767
if(if_indextoname((unsigned int)if_index, interface) == NULL){
745
768
perror_plus("if_indextoname");
747
fprintf(stderr, "Mandos plugin mandos-client: "
748
"Connection to: %s%%%s, port %" PRIu16 "\n",
749
ip, interface, port);
770
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
771
"\n", ip, interface, (uintmax_t)port);
752
fprintf(stderr, "Mandos plugin mandos-client: "
753
"Connection to: %s, port %" PRIu16 "\n", ip, port);
774
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
775
ip, (uintmax_t)port);
755
777
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
756
778
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
758
779
if(af == AF_INET6){
759
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
780
ret = getnameinfo((struct sockaddr *)&to,
781
sizeof(struct sockaddr_in6),
782
addrstr, sizeof(addrstr), NULL, 0,
762
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
785
ret = getnameinfo((struct sockaddr *)&to,
786
sizeof(struct sockaddr_in),
787
addrstr, sizeof(addrstr), NULL, 0,
766
perror_plus("inet_ntop");
768
if(strcmp(addrstr, ip) != 0){
769
fprintf(stderr, "Mandos plugin mandos-client: "
770
"Canonical address form: %s\n", addrstr);
790
if(ret == EAI_SYSTEM){
791
perror_plus("getnameinfo");
792
} else if(ret != 0) {
793
fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
794
} else if(strcmp(addrstr, ip) != 0){
795
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
1043
1070
char ip[AVAHI_ADDRESS_STR_MAX];
1044
1071
avahi_address_snprint(ip, sizeof(ip), address);
1046
fprintf(stderr, "Mandos plugin mandos-client: "
1047
"Mandos server \"%s\" found on %s (%s, %"
1048
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1049
ip, (intmax_t)interface, port);
1073
fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1074
PRIdMAX ") on port %" PRIu16 "\n", name,
1075
host_name, ip, (intmax_t)interface, port);
1051
int ret = start_mandos_communication(ip, port, interface,
1052
avahi_proto_to_af(proto));
1077
int ret = start_mandos_communication(ip, (in_port_t)port,
1079
avahi_proto_to_af(proto),
1054
avahi_simple_poll_quit(mc.simple_poll);
1082
avahi_simple_poll_quit(simple_poll);
1056
ret = add_server(ip, port, interface,
1057
avahi_proto_to_af(proto));
1084
if(not add_server(ip, (in_port_t)port, interface,
1085
avahi_proto_to_af(proto),
1086
&((mandos_context*)mc)->current_server)){
1087
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1156
1197
/* Reject the loopback device */
1157
1198
if(ifr->ifr_flags & IFF_LOOPBACK){
1159
fprintf(stderr, "Mandos plugin mandos-client: "
1160
"Rejecting loopback interface \"%s\"\n", ifname);
1200
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1164
1205
/* Accept point-to-point devices only if connect_to is specified */
1165
1206
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1167
fprintf(stderr, "Mandos plugin mandos-client: "
1168
"Accepting point-to-point interface \"%s\"\n", ifname);
1208
fprintf_plus(stderr, "Accepting point-to-point interface"
1209
" \"%s\"\n", ifname);
1172
1213
/* Otherwise, reject non-broadcast-capable devices */
1173
1214
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1175
fprintf(stderr, "Mandos plugin mandos-client: "
1176
"Rejecting non-broadcast interface \"%s\"\n", ifname);
1216
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1217
" \"%s\"\n", ifname);
1180
1221
/* Reject non-ARP interfaces (including dummy interfaces) */
1181
1222
if(ifr->ifr_flags & IFF_NOARP){
1183
fprintf(stderr, "Mandos plugin mandos-client: "
1184
"Rejecting non-ARP interface \"%s\"\n", ifname);
1224
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1189
1230
/* Accept this device */
1191
fprintf(stderr, "Mandos plugin mandos-client: "
1192
"Interface \"%s\" is good\n", ifname);
1232
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1207
1247
struct ifreq ifr;
1208
1248
if(not get_flags(if_entry->d_name, &ifr)){
1210
fprintf(stderr, "Mandos plugin mandos-client: "
1211
"Failed to get flags for interface \"%s\"\n",
1217
if(not good_flags(if_entry->d_name, &ifr)){
1224
* This function determines if a directory entry in /sys/class/net
1225
* corresponds to an acceptable network device which is up.
1226
* (This function is passed to scandir(3) as a filter function.)
1228
int up_interface(const struct dirent *if_entry){
1229
if(if_entry->d_name[0] == '.'){
1234
if(not get_flags(if_entry->d_name, &ifr)){
1236
fprintf(stderr, "Mandos plugin mandos-client: "
1237
"Failed to get flags for interface \"%s\"\n",
1243
/* Reject down interfaces */
1244
if(not (ifr.ifr_flags & IFF_UP)){
1246
fprintf(stderr, "Mandos plugin mandos-client: "
1247
"Rejecting down interface \"%s\"\n",
1253
/* Reject non-running interfaces */
1254
if(not (ifr.ifr_flags & IFF_RUNNING)){
1256
fprintf(stderr, "Mandos plugin mandos-client: "
1257
"Rejecting non-running interface \"%s\"\n",
1263
if(not good_flags(if_entry->d_name, &ifr)){
1250
fprintf_plus(stderr, "Failed to get flags for interface "
1251
"\"%s\"\n", if_entry->d_name);
1256
if(not good_flags(if_entry->d_name, &ifr)){
1263
* This function determines if a network interface is up.
1265
bool interface_is_up(const char *interface){
1267
if(not get_flags(interface, &ifr)){
1269
fprintf_plus(stderr, "Failed to get flags for interface "
1270
"\"%s\"\n", interface);
1275
return (bool)(ifr.ifr_flags & IFF_UP);
1279
* This function determines if a network interface is running
1281
bool interface_is_running(const char *interface){
1283
if(not get_flags(interface, &ifr)){
1285
fprintf_plus(stderr, "Failed to get flags for interface "
1286
"\"%s\"\n", interface);
1291
return (bool)(ifr.ifr_flags & IFF_RUNNING);
1269
1294
int notdotentries(const struct dirent *direntry){
1319
1343
if(not (S_ISREG(st.st_mode))){
1320
1344
/* Not a regular file */
1322
fprintf(stderr, "Mandos plugin mandos-client: "
1323
"Ignoring hook \"%s\" - not a file\n",
1346
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1328
1351
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1329
1352
/* Not executable */
1331
fprintf(stderr, "Mandos plugin mandos-client: "
1332
"Ignoring hook \"%s\" - not executable\n",
1354
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1360
fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1340
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1366
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1367
mandos_context *mc){
1342
1369
struct timespec now;
1343
1370
struct timespec waited_time;
1344
1371
intmax_t block_time;
1347
if(mc.current_server == NULL){
1374
if(mc->current_server == NULL){
1349
fprintf(stderr, "Mandos plugin mandos-client: "
1350
"Wait until first server is found. No timeout!\n");
1376
fprintf_plus(stderr, "Wait until first server is found."
1352
1379
ret = avahi_simple_poll_iterate(s, -1);
1355
fprintf(stderr, "Mandos plugin mandos-client: "
1356
"Check current_server if we should run it,"
1382
fprintf_plus(stderr, "Check current_server if we should run"
1359
1385
/* the current time */
1360
1386
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1439
/* Set effective uid to 0, return errno */
1440
error_t raise_privileges(void){
1441
error_t old_errno = errno;
1442
error_t ret_errno = 0;
1443
if(seteuid(0) == -1){
1445
perror_plus("seteuid");
1451
/* Set effective and real user ID to 0. Return errno. */
1452
error_t raise_privileges_permanently(void){
1453
error_t old_errno = errno;
1454
error_t ret_errno = raise_privileges();
1459
if(setuid(0) == -1){
1461
perror_plus("seteuid");
1467
/* Set effective user ID to unprivileged saved user ID */
1468
error_t lower_privileges(void){
1469
error_t old_errno = errno;
1470
error_t ret_errno = 0;
1471
if(seteuid(uid) == -1){
1473
perror_plus("seteuid");
1479
/* Lower privileges permanently */
1480
error_t lower_privileges_permanently(void){
1481
error_t old_errno = errno;
1482
error_t ret_errno = 0;
1483
if(setuid(uid) == -1){
1485
perror_plus("setuid");
1491
bool run_network_hooks(const char *mode, const char *interface,
1493
struct dirent **direntries;
1494
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1497
if(errno == ENOENT){
1499
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1500
" found\n", hookdir);
1503
perror_plus("scandir");
1506
struct dirent *direntry;
1508
int devnull = open("/dev/null", O_RDONLY);
1509
for(int i = 0; i < numhooks; i++){
1510
direntry = direntries[i];
1511
char *fullname = NULL;
1512
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1514
perror_plus("asprintf");
1518
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1521
pid_t hook_pid = fork();
1524
/* Raise privileges */
1525
raise_privileges_permanently();
1530
perror_plus("setgid");
1532
/* Reset supplementary groups */
1534
ret = setgroups(0, NULL);
1536
perror_plus("setgroups");
1538
dup2(devnull, STDIN_FILENO);
1540
dup2(STDERR_FILENO, STDOUT_FILENO);
1541
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1543
perror_plus("setenv");
1546
ret = setenv("DEVICE", interface, 1);
1548
perror_plus("setenv");
1551
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1553
perror_plus("setenv");
1556
ret = setenv("MODE", mode, 1);
1558
perror_plus("setenv");
1562
ret = asprintf(&delaystring, "%f", delay);
1564
perror_plus("asprintf");
1567
ret = setenv("DELAY", delaystring, 1);
1570
perror_plus("setenv");
1574
if(connect_to != NULL){
1575
ret = setenv("CONNECT", connect_to, 1);
1577
perror_plus("setenv");
1581
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1582
perror_plus("execl");
1583
_exit(EXIT_FAILURE);
1587
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1588
perror_plus("waitpid");
1592
if(WIFEXITED(status)){
1593
if(WEXITSTATUS(status) != 0){
1594
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1595
" with status %d\n", direntry->d_name,
1596
WEXITSTATUS(status));
1600
} else if(WIFSIGNALED(status)){
1601
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1602
" signal %d\n", direntry->d_name,
1607
fprintf_plus(stderr, "Warning: network hook \"%s\""
1608
" crashed\n", direntry->d_name);
1615
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1624
error_t bring_up_interface(const char *const interface,
1627
error_t old_errno = errno;
1628
error_t ret_errno = 0;
1629
int ret, ret_setflags;
1630
struct ifreq network;
1631
unsigned int if_index = if_nametoindex(interface);
1633
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1643
if(not interface_is_up(interface)){
1644
if(not get_flags(interface, &network) and debug){
1646
fprintf_plus(stderr, "Failed to get flags for interface "
1647
"\"%s\"\n", interface);
1650
network.ifr_flags |= IFF_UP;
1652
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1655
perror_plus("socket");
1667
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1671
/* Raise priviliges */
1675
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1676
messages about the network interface to mess up the prompt */
1677
int ret_linux = klogctl(8, NULL, 5);
1678
bool restore_loglevel = true;
1679
if(ret_linux == -1){
1680
restore_loglevel = false;
1681
perror_plus("klogctl");
1683
#endif /* __linux__ */
1684
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1687
if(restore_loglevel){
1688
ret_linux = klogctl(7, NULL, 0);
1689
if(ret_linux == -1){
1690
perror_plus("klogctl");
1693
#endif /* __linux__ */
1695
/* Lower privileges */
1698
/* Close the socket */
1699
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1701
perror_plus("close");
1704
if(ret_setflags == -1){
1706
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1711
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
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
if(interface_is_running(interface)){
1721
struct timespec sleeptime = { .tv_nsec = 250000000 };
1722
ret = nanosleep(&sleeptime, NULL);
1723
if(ret == -1 and errno != EINTR){
1724
perror_plus("nanosleep");
1732
error_t take_down_interface(const char *const interface){
1733
error_t old_errno = errno;
1734
struct ifreq network;
1735
unsigned int if_index = if_nametoindex(interface);
1737
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1741
if(interface_is_up(interface)){
1742
error_t ret_errno = 0;
1743
if(not get_flags(interface, &network) and debug){
1745
fprintf_plus(stderr, "Failed to get flags for interface "
1746
"\"%s\"\n", interface);
1749
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1751
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1754
perror_plus("socket");
1760
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1764
/* Raise priviliges */
1767
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1770
/* Lower privileges */
1773
/* Close the socket */
1774
int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1776
perror_plus("close");
1779
if(ret_setflags == -1){
1781
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1786
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1413
1794
int main(int argc, char *argv[]){
1795
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1796
.priority = "SECURE256:!CTYPE-X.509:"
1797
"+CTYPE-OPENPGP", .current_server = NULL,
1798
.interfaces = NULL, .interfaces_size = 0 };
1414
1799
AvahiSServiceBrowser *sb = NULL;
1417
1802
intmax_t tmpmax;
1419
1804
int exitcode = EXIT_SUCCESS;
1420
const char *interface = "";
1421
struct ifreq network;
1423
bool take_down_interface = false;
1805
char *interfaces_to_take_down = NULL;
1806
size_t interfaces_to_take_down_size = 0;
1426
1807
char tempdir[] = "/tmp/mandosXXXXXX";
1427
1808
bool tempdir_created = false;
1428
1809
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1429
1810
const char *seckey = PATHDIR "/" SECKEY;
1430
1811
const char *pubkey = PATHDIR "/" PUBKEY;
1812
char *interfaces_hooks = NULL;
1432
1814
bool gnutls_initialized = false;
1433
1815
bool gpgme_initialized = false;
1608
1993
/* Work around Debian bug #633582:
1609
1994
<http://bugs.debian.org/633582> */
1612
1996
/* Re-raise priviliges */
1616
perror_plus("seteuid");
1619
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1620
int seckey_fd = open(seckey, O_RDONLY);
1621
if(seckey_fd == -1){
1622
perror_plus("open");
1624
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1626
perror_plus("fstat");
1628
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1629
ret = fchown(seckey_fd, uid, gid);
1631
perror_plus("fchown");
1635
TEMP_FAILURE_RETRY(close(seckey_fd));
1639
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1640
int pubkey_fd = open(pubkey, O_RDONLY);
1641
if(pubkey_fd == -1){
1642
perror_plus("open");
1644
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1646
perror_plus("fstat");
1648
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1649
ret = fchown(pubkey_fd, uid, gid);
1651
perror_plus("fchown");
1655
TEMP_FAILURE_RETRY(close(pubkey_fd));
1659
/* Lower privileges */
1663
perror_plus("seteuid");
1667
/* Find network hooks and run them */
1669
struct dirent **direntries;
1670
struct dirent *direntry;
1671
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1674
perror_plus("scandir");
1676
int devnull = open("/dev/null", O_RDONLY);
1677
for(int i = 0; i < numhooks; i++){
1678
direntry = direntries[0];
1679
char *fullname = NULL;
1680
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1682
perror_plus("asprintf");
1685
pid_t hook_pid = fork();
1688
dup2(devnull, STDIN_FILENO);
1690
dup2(STDERR_FILENO, STDOUT_FILENO);
1691
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1693
perror_plus("setenv");
1696
ret = setenv("DEVICE", interface, 1);
1698
perror_plus("setenv");
1701
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1703
perror_plus("setenv");
1706
ret = setenv("MODE", "start", 1);
1708
perror_plus("setenv");
1712
ret = asprintf(&delaystring, "%f", delay);
1714
perror_plus("asprintf");
1717
ret = setenv("DELAY", delaystring, 1);
1720
perror_plus("setenv");
1724
ret = execl(fullname, direntry->d_name, "start", NULL);
1725
perror_plus("execl");
1728
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1729
perror_plus("waitpid");
1733
if(WIFEXITED(status)){
1734
if(WEXITSTATUS(status) != 0){
1735
fprintf(stderr, "Mandos plugin mandos-client: "
1736
"Warning: network hook \"%s\" exited"
1737
" with status %d\n", direntry->d_name,
1738
WEXITSTATUS(status));
1742
} else if(WIFSIGNALED(status)){
1743
fprintf(stderr, "Mandos plugin mandos-client: "
1744
"Warning: network hook \"%s\" died by"
1745
" signal %d\n", direntry->d_name,
1750
fprintf(stderr, "Mandos plugin mandos-client: "
1751
"Warning: network hook \"%s\" crashed\n",
1997
if(raise_privileges() == 0){
2000
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2001
int seckey_fd = open(seckey, O_RDONLY);
2002
if(seckey_fd == -1){
2003
perror_plus("open");
2005
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
2007
perror_plus("fstat");
2009
if(S_ISREG(st.st_mode)
2010
and st.st_uid == 0 and st.st_gid == 0){
2011
ret = fchown(seckey_fd, uid, gid);
2013
perror_plus("fchown");
2017
TEMP_FAILURE_RETRY(close(seckey_fd));
2021
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
2022
int pubkey_fd = open(pubkey, O_RDONLY);
2023
if(pubkey_fd == -1){
2024
perror_plus("open");
2026
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
2028
perror_plus("fstat");
2030
if(S_ISREG(st.st_mode)
2031
and st.st_uid == 0 and st.st_gid == 0){
2032
ret = fchown(pubkey_fd, uid, gid);
2034
perror_plus("fchown");
2038
TEMP_FAILURE_RETRY(close(pubkey_fd));
2042
/* Lower privileges */
2047
/* Remove invalid interface names (except "none") */
2049
char *interface = NULL;
2050
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2052
if(strcmp(interface, "none") != 0
2053
and if_nametoindex(interface) == 0){
2054
if(interface[0] != '\0'){
2055
fprintf_plus(stderr, "Not using nonexisting interface"
2056
" \"%s\"\n", interface);
2058
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
2064
/* Run network hooks */
2066
if(mc.interfaces != NULL){
2067
interfaces_hooks = malloc(mc.interfaces_size);
2068
if(interfaces_hooks == NULL){
2069
perror_plus("malloc");
2072
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2073
argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2075
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2076
interfaces_hooks : "", delay)){
1767
2082
avahi_set_log_function(empty_log);
1770
if(interface[0] == '\0'){
1771
struct dirent **direntries;
1772
/* First look for interfaces that are up */
1773
ret = scandir(sys_class_net, &direntries, up_interface,
1776
/* No up interfaces, look for any good interfaces */
1778
ret = scandir(sys_class_net, &direntries, good_interface,
1782
/* Pick the first interface returned */
1783
interface = strdup(direntries[0]->d_name);
1785
fprintf(stderr, "Mandos plugin mandos-client: "
1786
"Using interface \"%s\"\n", interface);
1788
if(interface == NULL){
1789
perror_plus("malloc");
1791
exitcode = EXIT_FAILURE;
1797
fprintf(stderr, "Mandos plugin mandos-client: "
1798
"Could not find a network interface\n");
1799
exitcode = EXIT_FAILURE;
1804
2085
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1805
2086
from the signal handler */
1806
2087
/* Initialize the pseudo-RNG for Avahi */
1807
2088
srand((unsigned int) time(NULL));
1808
mc.simple_poll = avahi_simple_poll_new();
1809
if(mc.simple_poll == NULL){
1810
fprintf(stderr, "Mandos plugin mandos-client: "
1811
"Avahi: Failed to create simple poll object.\n");
2089
simple_poll = avahi_simple_poll_new();
2090
if(simple_poll == NULL){
2091
fprintf_plus(stderr,
2092
"Avahi: Failed to create simple poll object.\n");
1812
2093
exitcode = EX_UNAVAILABLE;
1879
/* If the interface is down, bring it up */
1880
if(strcmp(interface, "none") != 0){
1881
if_index = (AvahiIfIndex) if_nametoindex(interface);
1883
fprintf(stderr, "Mandos plugin mandos-client: "
1884
"No such interface: \"%s\"\n", interface);
1885
exitcode = EX_UNAVAILABLE;
1893
/* Re-raise priviliges */
1897
perror_plus("seteuid");
1901
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1902
messages about the network interface to mess up the prompt */
1903
ret = klogctl(8, NULL, 5);
1904
bool restore_loglevel = true;
1906
restore_loglevel = false;
1907
perror_plus("klogctl");
1909
#endif /* __linux__ */
1911
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1913
perror_plus("socket");
1914
exitcode = EX_OSERR;
1916
if(restore_loglevel){
1917
ret = klogctl(7, NULL, 0);
1919
perror_plus("klogctl");
1922
#endif /* __linux__ */
1923
/* Lower privileges */
1927
perror_plus("seteuid");
1931
strcpy(network.ifr_name, interface);
1932
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1934
perror_plus("ioctl SIOCGIFFLAGS");
1936
if(restore_loglevel){
1937
ret = klogctl(7, NULL, 0);
1939
perror_plus("klogctl");
1942
#endif /* __linux__ */
1943
exitcode = EX_OSERR;
1944
/* Lower privileges */
1948
perror_plus("seteuid");
1952
if((network.ifr_flags & IFF_UP) == 0){
1953
network.ifr_flags |= IFF_UP;
1954
take_down_interface = true;
1955
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1957
take_down_interface = false;
1958
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1959
exitcode = EX_OSERR;
1961
if(restore_loglevel){
1962
ret = klogctl(7, NULL, 0);
1964
perror_plus("klogctl");
2160
/* If no interfaces were specified, make a list */
2161
if(mc.interfaces == NULL){
2162
struct dirent **direntries;
2163
/* Look for any good interfaces */
2164
ret = scandir(sys_class_net, &direntries, good_interface,
2167
/* Add all found interfaces to interfaces list */
2168
for(int i = 0; i < ret; ++i){
2169
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2170
direntries[i]->d_name);
2173
perror_plus("argz_add");
2177
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2178
direntries[i]->d_name);
2184
fprintf_plus(stderr, "Could not find a network interface\n");
2185
exitcode = EXIT_FAILURE;
2190
/* Bring up interfaces which are down, and remove any "none"s */
2192
char *interface = NULL;
2193
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2195
/* If interface name is "none", stop bringing up interfaces.
2196
Also remove all instances of "none" from the list */
2197
if(strcmp(interface, "none") == 0){
2198
argz_delete(&mc.interfaces, &mc.interfaces_size,
2201
while((interface = argz_next(mc.interfaces,
2202
mc.interfaces_size, interface))){
2203
if(strcmp(interface, "none") == 0){
2204
argz_delete(&mc.interfaces, &mc.interfaces_size,
1967
#endif /* __linux__ */
1968
/* Lower privileges */
1972
perror_plus("seteuid");
1977
/* Sleep checking until interface is running.
1978
Check every 0.25s, up to total time of delay */
1979
for(int i=0; i < delay * 4; i++){
1980
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1982
perror_plus("ioctl SIOCGIFFLAGS");
1983
} else if(network.ifr_flags & IFF_RUNNING){
1986
struct timespec sleeptime = { .tv_nsec = 250000000 };
1987
ret = nanosleep(&sleeptime, NULL);
1988
if(ret == -1 and errno != EINTR){
1989
perror_plus("nanosleep");
1992
if(not take_down_interface){
1993
/* We won't need the socket anymore */
1994
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1996
perror_plus("close");
2000
if(restore_loglevel){
2001
/* Restores kernel loglevel to default */
2002
ret = klogctl(7, NULL, 0);
2004
perror_plus("klogctl");
2007
#endif /* __linux__ */
2008
/* Lower privileges */
2010
if(take_down_interface){
2011
/* Lower privileges */
2014
perror_plus("seteuid");
2017
/* Lower privileges permanently */
2020
perror_plus("setuid");
2211
bool interface_was_up = interface_is_up(interface);
2212
ret = bring_up_interface(interface, delay);
2213
if(not interface_was_up){
2216
perror_plus("Failed to bring up interface");
2218
ret_errno = argz_add(&interfaces_to_take_down,
2219
&interfaces_to_take_down_size,
2223
perror_plus("argz_add");
2228
if(debug and (interfaces_to_take_down == NULL)){
2229
fprintf_plus(stderr, "No interfaces were brought up\n");
2233
/* If we only got one interface, explicitly use only that one */
2234
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2236
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2239
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
2029
ret = init_gnutls_global(pubkey, seckey);
2246
ret = init_gnutls_global(pubkey, seckey, &mc);
2031
fprintf(stderr, "Mandos plugin mandos-client: "
2032
"init_gnutls_global failed\n");
2248
fprintf_plus(stderr, "init_gnutls_global failed\n");
2033
2249
exitcode = EX_UNAVAILABLE;
2187
2400
/* Run the main loop */
2190
fprintf(stderr, "Mandos plugin mandos-client: "
2191
"Starting Avahi loop search\n");
2403
fprintf_plus(stderr, "Starting Avahi loop search\n");
2194
ret = avahi_loop_with_timeout(mc.simple_poll,
2195
(int)(retry_interval * 1000));
2406
ret = avahi_loop_with_timeout(simple_poll,
2407
(int)(retry_interval * 1000), &mc);
2197
fprintf(stderr, "Mandos plugin mandos-client: "
2198
"avahi_loop_with_timeout exited %s\n",
2199
(ret == 0) ? "successfully" : "with error");
2409
fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2410
(ret == 0) ? "successfully" : "with error");
2205
fprintf(stderr, "Mandos plugin mandos-client: "
2206
"%s exiting\n", argv[0]);
2416
fprintf_plus(stderr, "%s exiting\n", argv[0]);
2209
2419
/* Cleanup things */
2420
free(mc.interfaces);
2211
2423
avahi_s_service_browser_free(sb);
2213
2425
if(mc.server != NULL)
2214
2426
avahi_server_free(mc.server);
2216
if(mc.simple_poll != NULL)
2217
avahi_simple_poll_free(mc.simple_poll);
2428
if(simple_poll != NULL)
2429
avahi_simple_poll_free(simple_poll);
2219
2431
if(gnutls_initialized){
2220
2432
gnutls_certificate_free_credentials(mc.cred);
2240
/* XXX run network hooks "stop" here */
2242
/* Take down the network interface */
2243
if(take_down_interface){
2244
/* Re-raise priviliges */
2248
perror_plus("seteuid");
2251
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2253
perror_plus("ioctl SIOCGIFFLAGS");
2254
} else if(network.ifr_flags & IFF_UP){
2255
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2256
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2258
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2452
/* Re-raise priviliges */
2456
/* Run network hooks */
2457
run_network_hooks("stop", interfaces_hooks != NULL ?
2458
interfaces_hooks : "", delay);
2460
/* Take down the network interfaces which were brought up */
2462
char *interface = NULL;
2463
while((interface=argz_next(interfaces_to_take_down,
2464
interfaces_to_take_down_size,
2466
ret_errno = take_down_interface(interface);
2469
perror_plus("Failed to take down interface");
2261
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2263
perror_plus("close");
2265
/* Lower privileges permanently */
2269
perror_plus("setuid");
2472
if(debug and (interfaces_to_take_down == NULL)){
2473
fprintf_plus(stderr, "No interfaces needed to be taken"
2478
lower_privileges_permanently();
2481
free(interfaces_to_take_down);
2482
free(interfaces_hooks);
2274
2484
/* Removes the GPGME temp directory and all files inside */
2275
2485
if(tempdir_created){
2276
2486
struct dirent **direntries = NULL;