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), nonnull))
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,
180
199
* "buffer_length" is how much is already used.
201
__attribute__((nonnull, warn_unused_result))
182
202
size_t incbuffer(char **buffer, size_t buffer_length,
183
203
size_t buffer_capacity){
184
204
if(buffer_length + BUFFER_SIZE > buffer_capacity){
185
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
205
char *new_buf = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
207
int old_errno = errno;
189
214
buffer_capacity += BUFFER_SIZE;
191
216
return buffer_capacity;
194
219
/* Add server to set of servers to retry periodically */
195
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
220
__attribute__((nonnull, warn_unused_result))
221
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
222
int af, server **current_server){
198
224
server *new_server = malloc(sizeof(server));
199
225
if(new_server == NULL){
200
226
perror_plus("malloc");
203
229
*new_server = (server){ .ip = strdup(ip),
207
233
if(new_server->ip == NULL){
208
234
perror_plus("strdup");
237
ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
239
perror_plus("clock_gettime");
211
242
/* Special case of first server */
212
if (mc.current_server == NULL){
243
if(*current_server == NULL){
213
244
new_server->next = new_server;
214
245
new_server->prev = new_server;
215
mc.current_server = new_server;
216
/* Place the new server last in the list */
246
*current_server = new_server;
218
new_server->next = mc.current_server;
219
new_server->prev = mc.current_server->prev;
248
/* Place the new server last in the list */
249
new_server->next = *current_server;
250
new_server->prev = (*current_server)->prev;
220
251
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");
252
(*current_server)->prev = new_server;
232
258
* Initialize GPGME.
234
static bool init_gpgme(const char *seckey, const char *pubkey,
235
const char *tempdir){
260
__attribute__((nonnull, warn_unused_result))
261
static bool init_gpgme(const char * const seckey,
262
const char * const pubkey,
263
const char * const tempdir,
236
265
gpgme_error_t rc;
237
266
gpgme_engine_info_t engine_info;
241
269
* Helper function to insert pub and seckey to the engine keyring.
243
bool import_key(const char *filename){
271
bool import_key(const char * const filename){
246
274
gpgme_data_t pgp_data;
342
367
ssize_t plaintext_length = 0;
345
fprintf(stderr, "Mandos plugin mandos-client: "
346
"Trying to decrypt OpenPGP data\n");
370
fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
349
373
/* Create new GPGME data buffer from memory cryptotext */
350
374
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
352
376
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));
377
fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
378
gpgme_strsource(rc), gpgme_strerror(rc));
359
382
/* Create new empty GPGME data buffer for the plaintext */
360
383
rc = gpgme_data_new(&dh_plain);
361
384
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));
385
fprintf_plus(stderr, "Mandos plugin mandos-client: "
386
"bad gpgme_data_new: %s: %s\n",
387
gpgme_strsource(rc), gpgme_strerror(rc));
365
388
gpgme_data_release(dh_crypto);
369
392
/* Decrypt data from the cryptotext data buffer to the plaintext
371
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
394
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
372
395
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));
396
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
397
gpgme_strsource(rc), gpgme_strerror(rc));
376
398
plaintext_length = -1;
378
400
gpgme_decrypt_result_t result;
379
result = gpgme_op_decrypt_result(mc.ctx);
401
result = gpgme_op_decrypt_result(mc->ctx);
380
402
if(result == NULL){
381
fprintf(stderr, "Mandos plugin mandos-client: "
382
"gpgme_op_decrypt_result failed\n");
403
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);
405
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
406
result->unsupported_algorithm);
407
fprintf_plus(stderr, "Wrong key usage: %u\n",
408
result->wrong_key_usage);
390
409
if(result->file_name != NULL){
391
fprintf(stderr, "Mandos plugin mandos-client: "
392
"File name: %s\n", result->file_name);
410
fprintf_plus(stderr, "File name: %s\n", result->file_name);
394
412
gpgme_recipient_t recipient;
395
413
recipient = result->recipients;
396
414
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
415
fprintf_plus(stderr, "Public key algorithm: %s\n",
416
gpgme_pubkey_algo_name
417
(recipient->pubkey_algo));
418
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
419
fprintf_plus(stderr, "Secret key available: %s\n",
420
recipient->status == GPG_ERR_NO_SECKEY
406
422
recipient = recipient->next;
508
524
/* OpenPGP credentials */
509
ret = gnutls_certificate_allocate_credentials(&mc.cred);
525
ret = gnutls_certificate_allocate_credentials(&mc->cred);
510
526
if(ret != GNUTLS_E_SUCCESS){
511
fprintf(stderr, "Mandos plugin mandos-client: "
512
"GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
527
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
528
safer_gnutls_strerror(ret));
513
529
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,
534
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
535
" secret key %s as GnuTLS credentials\n",
524
540
ret = gnutls_certificate_set_openpgp_key_file
525
(mc.cred, pubkeyfilename, seckeyfilename,
541
(mc->cred, pubkeyfilename, seckeyfilename,
526
542
GNUTLS_OPENPGP_FMT_BASE64);
527
543
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));
545
"Error[%d] while reading the OpenPGP key pair ('%s',"
546
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
547
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
548
safer_gnutls_strerror(ret));
537
552
/* GnuTLS server initialization */
538
ret = gnutls_dh_params_init(&mc.dh_params);
553
ret = gnutls_dh_params_init(&mc->dh_params);
539
554
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));
555
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
556
" initialization: %s\n",
557
safer_gnutls_strerror(ret));
545
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
560
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
546
561
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));
562
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
563
safer_gnutls_strerror(ret));
553
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
567
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
559
gnutls_certificate_free_credentials(mc.cred);
573
gnutls_certificate_free_credentials(mc->cred);
560
574
gnutls_global_deinit();
561
gnutls_dh_params_deinit(mc.dh_params);
575
gnutls_dh_params_deinit(mc->dh_params);
565
static int init_gnutls_session(gnutls_session_t *session){
579
__attribute__((nonnull, warn_unused_result))
580
static int init_gnutls_session(gnutls_session_t *session,
567
583
/* GnuTLS session creation */
744
778
if(if_indextoname((unsigned int)if_index, interface) == NULL){
745
779
perror_plus("if_indextoname");
747
fprintf(stderr, "Mandos plugin mandos-client: "
748
"Connection to: %s%%%s, port %" PRIu16 "\n",
749
ip, interface, port);
781
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
782
"\n", ip, interface, (uintmax_t)port);
752
fprintf(stderr, "Mandos plugin mandos-client: "
753
"Connection to: %s, port %" PRIu16 "\n", ip, port);
785
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
786
ip, (uintmax_t)port);
755
788
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
756
789
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
758
790
if(af == AF_INET6){
759
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
791
ret = getnameinfo((struct sockaddr *)&to,
792
sizeof(struct sockaddr_in6),
793
addrstr, sizeof(addrstr), NULL, 0,
762
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
796
ret = getnameinfo((struct sockaddr *)&to,
797
sizeof(struct sockaddr_in),
798
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);
801
if(ret == EAI_SYSTEM){
802
perror_plus("getnameinfo");
803
} else if(ret != 0) {
804
fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
805
} else if(strcmp(addrstr, ip) != 0){
806
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
1043
1082
char ip[AVAHI_ADDRESS_STR_MAX];
1044
1083
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);
1085
fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1086
PRIdMAX ") on port %" PRIu16 "\n", name,
1087
host_name, ip, (intmax_t)interface, port);
1051
int ret = start_mandos_communication(ip, port, interface,
1052
avahi_proto_to_af(proto));
1089
int ret = start_mandos_communication(ip, (in_port_t)port,
1091
avahi_proto_to_af(proto),
1054
avahi_simple_poll_quit(mc.simple_poll);
1094
avahi_simple_poll_quit(simple_poll);
1056
ret = add_server(ip, port, interface,
1057
avahi_proto_to_af(proto));
1096
if(not add_server(ip, (in_port_t)port, interface,
1097
avahi_proto_to_af(proto),
1098
&((mandos_context*)mc)->current_server)){
1099
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1126
1174
signal_received = sig;
1127
1175
int old_errno = errno;
1128
1176
/* set main loop to exit */
1129
if(mc.simple_poll != NULL){
1130
avahi_simple_poll_quit(mc.simple_poll);
1177
if(simple_poll != NULL){
1178
avahi_simple_poll_quit(simple_poll);
1132
1180
errno = old_errno;
1183
__attribute__((nonnull, warn_unused_result))
1135
1184
bool get_flags(const char *ifname, struct ifreq *ifr){
1138
1188
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1140
1191
perror_plus("socket");
1143
1195
strcpy(ifr->ifr_name, ifname);
1144
1196
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1147
1200
perror_plus("ioctl SIOCGIFFLAGS");
1208
__attribute__((nonnull, warn_unused_result))
1154
1209
bool good_flags(const char *ifname, const struct ifreq *ifr){
1156
1211
/* Reject the loopback device */
1157
1212
if(ifr->ifr_flags & IFF_LOOPBACK){
1159
fprintf(stderr, "Mandos plugin mandos-client: "
1160
"Rejecting loopback interface \"%s\"\n", ifname);
1214
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1164
1219
/* Accept point-to-point devices only if connect_to is specified */
1165
1220
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);
1222
fprintf_plus(stderr, "Accepting point-to-point interface"
1223
" \"%s\"\n", ifname);
1172
1227
/* Otherwise, reject non-broadcast-capable devices */
1173
1228
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1175
fprintf(stderr, "Mandos plugin mandos-client: "
1176
"Rejecting non-broadcast interface \"%s\"\n", ifname);
1230
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1231
" \"%s\"\n", ifname);
1180
1235
/* Reject non-ARP interfaces (including dummy interfaces) */
1181
1236
if(ifr->ifr_flags & IFF_NOARP){
1183
fprintf(stderr, "Mandos plugin mandos-client: "
1184
"Rejecting non-ARP interface \"%s\"\n", ifname);
1238
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1189
1244
/* Accept this device */
1191
fprintf(stderr, "Mandos plugin mandos-client: "
1192
"Interface \"%s\" is good\n", ifname);
1246
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1207
1262
struct ifreq ifr;
1208
1263
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)){
1265
fprintf_plus(stderr, "Failed to get flags for interface "
1266
"\"%s\"\n", if_entry->d_name);
1271
if(not good_flags(if_entry->d_name, &ifr)){
1278
* This function determines if a network interface is up.
1280
__attribute__((nonnull, warn_unused_result))
1281
bool interface_is_up(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_UP);
1295
* This function determines if a network interface is running
1297
__attribute__((nonnull, warn_unused_result))
1298
bool interface_is_running(const char *interface){
1300
if(not get_flags(interface, &ifr)){
1302
fprintf_plus(stderr, "Failed to get flags for interface "
1303
"\"%s\"\n", interface);
1308
return (bool)(ifr.ifr_flags & IFF_RUNNING);
1311
__attribute__((nonnull, pure, warn_unused_result))
1269
1312
int notdotentries(const struct dirent *direntry){
1270
1313
/* Skip "." and ".." */
1271
1314
if(direntry->d_name[0] == '.'
1319
1362
if(not (S_ISREG(st.st_mode))){
1320
1363
/* Not a regular file */
1322
fprintf(stderr, "Mandos plugin mandos-client: "
1323
"Ignoring hook \"%s\" - not a file\n",
1365
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1328
1370
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1329
1371
/* Not executable */
1331
fprintf(stderr, "Mandos plugin mandos-client: "
1332
"Ignoring hook \"%s\" - not executable\n",
1373
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1379
fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1340
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1385
__attribute__((nonnull, warn_unused_result))
1386
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1387
mandos_context *mc){
1342
1389
struct timespec now;
1343
1390
struct timespec waited_time;
1344
1391
intmax_t block_time;
1347
if(mc.current_server == NULL){
1349
fprintf(stderr, "Mandos plugin mandos-client: "
1350
"Wait until first server is found. No timeout!\n");
1394
if(mc->current_server == NULL){
1396
fprintf_plus(stderr, "Wait until first server is found."
1352
1399
ret = avahi_simple_poll_iterate(s, -1);
1355
fprintf(stderr, "Mandos plugin mandos-client: "
1356
"Check current_server if we should run it,"
1402
fprintf_plus(stderr, "Check current_server if we should run"
1359
1405
/* the current time */
1360
1406
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1403
1449
ret = avahi_simple_poll_iterate(s, (int)block_time);
1406
if (ret > 0 or errno != EINTR){
1452
if(ret > 0 or errno != EINTR){
1407
1453
return (ret != 1) ? ret : 0;
1459
/* Set effective uid to 0, return errno */
1460
__attribute__((warn_unused_result))
1461
error_t raise_privileges(void){
1462
error_t old_errno = errno;
1463
error_t ret_errno = 0;
1464
if(seteuid(0) == -1){
1466
perror_plus("seteuid");
1472
/* Set effective and real user ID to 0. Return errno. */
1473
__attribute__((warn_unused_result))
1474
error_t raise_privileges_permanently(void){
1475
error_t old_errno = errno;
1476
error_t ret_errno = raise_privileges();
1481
if(setuid(0) == -1){
1483
perror_plus("seteuid");
1489
/* Set effective user ID to unprivileged saved user ID */
1490
__attribute__((warn_unused_result))
1491
error_t lower_privileges(void){
1492
error_t old_errno = errno;
1493
error_t ret_errno = 0;
1494
if(seteuid(uid) == -1){
1496
perror_plus("seteuid");
1502
/* Lower privileges permanently */
1503
__attribute__((warn_unused_result))
1504
error_t lower_privileges_permanently(void){
1505
error_t old_errno = errno;
1506
error_t ret_errno = 0;
1507
if(setuid(uid) == -1){
1509
perror_plus("setuid");
1515
__attribute__((nonnull))
1516
void run_network_hooks(const char *mode, const char *interface,
1518
struct dirent **direntries;
1519
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1522
if(errno == ENOENT){
1524
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1525
" found\n", hookdir);
1528
perror_plus("scandir");
1531
struct dirent *direntry;
1533
int devnull = open("/dev/null", O_RDONLY);
1534
for(int i = 0; i < numhooks; i++){
1535
direntry = direntries[i];
1536
char *fullname = NULL;
1537
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1539
perror_plus("asprintf");
1543
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1546
pid_t hook_pid = fork();
1549
/* Raise privileges */
1550
if(raise_privileges_permanently() != 0){
1551
perror_plus("Failed to raise privileges");
1558
perror_plus("setgid");
1561
/* Reset supplementary groups */
1563
ret = setgroups(0, NULL);
1565
perror_plus("setgroups");
1568
ret = dup2(devnull, STDIN_FILENO);
1570
perror_plus("dup2(devnull, STDIN_FILENO)");
1573
ret = close(devnull);
1575
perror_plus("close");
1578
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1580
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1583
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1585
perror_plus("setenv");
1588
ret = setenv("DEVICE", interface, 1);
1590
perror_plus("setenv");
1593
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1595
perror_plus("setenv");
1598
ret = setenv("MODE", mode, 1);
1600
perror_plus("setenv");
1604
ret = asprintf(&delaystring, "%f", (double)delay);
1606
perror_plus("asprintf");
1609
ret = setenv("DELAY", delaystring, 1);
1612
perror_plus("setenv");
1616
if(connect_to != NULL){
1617
ret = setenv("CONNECT", connect_to, 1);
1619
perror_plus("setenv");
1623
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1624
perror_plus("execl");
1625
_exit(EXIT_FAILURE);
1629
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1630
perror_plus("waitpid");
1634
if(WIFEXITED(status)){
1635
if(WEXITSTATUS(status) != 0){
1636
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1637
" with status %d\n", direntry->d_name,
1638
WEXITSTATUS(status));
1642
} else if(WIFSIGNALED(status)){
1643
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1644
" signal %d\n", direntry->d_name,
1649
fprintf_plus(stderr, "Warning: network hook \"%s\""
1650
" crashed\n", direntry->d_name);
1657
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1665
__attribute__((nonnull, warn_unused_result))
1666
error_t bring_up_interface(const char *const interface,
1668
error_t old_errno = errno;
1670
struct ifreq network;
1671
unsigned int if_index = if_nametoindex(interface);
1673
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1683
if(not interface_is_up(interface)){
1684
error_t ret_errno = 0, ioctl_errno = 0;
1685
if(not get_flags(interface, &network)){
1687
fprintf_plus(stderr, "Failed to get flags for interface "
1688
"\"%s\"\n", interface);
1692
network.ifr_flags |= IFF_UP; /* set flag */
1694
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1697
perror_plus("socket");
1703
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1705
perror_plus("close");
1712
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1716
/* Raise privileges */
1717
ret_errno = raise_privileges();
1719
perror_plus("Failed to raise privileges");
1724
bool restore_loglevel = false;
1726
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1727
messages about the network interface to mess up the prompt */
1728
ret_linux = klogctl(8, NULL, 5);
1729
if(ret_linux == -1){
1730
perror_plus("klogctl");
1732
restore_loglevel = true;
1735
#endif /* __linux__ */
1736
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1737
ioctl_errno = errno;
1739
if(restore_loglevel){
1740
ret_linux = klogctl(7, NULL, 0);
1741
if(ret_linux == -1){
1742
perror_plus("klogctl");
1745
#endif /* __linux__ */
1747
/* If raise_privileges() succeeded above */
1749
/* Lower privileges */
1750
ret_errno = lower_privileges();
1753
perror_plus("Failed to lower privileges");
1757
/* Close the socket */
1758
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1760
perror_plus("close");
1763
if(ret_setflags == -1){
1764
errno = ioctl_errno;
1765
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1770
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1774
/* Sleep checking until interface is running.
1775
Check every 0.25s, up to total time of delay */
1776
for(int i=0; i < delay * 4; i++){
1777
if(interface_is_running(interface)){
1780
struct timespec sleeptime = { .tv_nsec = 250000000 };
1781
ret = nanosleep(&sleeptime, NULL);
1782
if(ret == -1 and errno != EINTR){
1783
perror_plus("nanosleep");
1791
__attribute__((nonnull, warn_unused_result))
1792
error_t take_down_interface(const char *const interface){
1793
error_t old_errno = errno;
1794
struct ifreq network;
1795
unsigned int if_index = if_nametoindex(interface);
1797
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1801
if(interface_is_up(interface)){
1802
error_t ret_errno = 0, ioctl_errno = 0;
1803
if(not get_flags(interface, &network) and debug){
1805
fprintf_plus(stderr, "Failed to get flags for interface "
1806
"\"%s\"\n", interface);
1810
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1812
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1815
perror_plus("socket");
1821
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1825
/* Raise privileges */
1826
ret_errno = raise_privileges();
1828
perror_plus("Failed to raise privileges");
1831
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1832
ioctl_errno = errno;
1834
/* If raise_privileges() succeeded above */
1836
/* Lower privileges */
1837
ret_errno = lower_privileges();
1840
perror_plus("Failed to lower privileges");
1844
/* Close the socket */
1845
int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1847
perror_plus("close");
1850
if(ret_setflags == -1){
1851
errno = ioctl_errno;
1852
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1857
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1413
1865
int main(int argc, char *argv[]){
1866
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1867
.priority = "SECURE256:!CTYPE-X.509:"
1868
"+CTYPE-OPENPGP", .current_server = NULL,
1869
.interfaces = NULL, .interfaces_size = 0 };
1414
1870
AvahiSServiceBrowser *sb = NULL;
1417
1873
intmax_t tmpmax;
1419
1875
int exitcode = EXIT_SUCCESS;
1420
const char *interface = "";
1421
struct ifreq network;
1423
bool take_down_interface = false;
1426
char tempdir[] = "/tmp/mandosXXXXXX";
1427
bool tempdir_created = false;
1876
char *interfaces_to_take_down = NULL;
1877
size_t interfaces_to_take_down_size = 0;
1878
char run_tempdir[] = "/run/tmp/mandosXXXXXX";
1879
char old_tempdir[] = "/tmp/mandosXXXXXX";
1880
char *tempdir = NULL;
1428
1881
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1429
1882
const char *seckey = PATHDIR "/" SECKEY;
1430
1883
const char *pubkey = PATHDIR "/" PUBKEY;
1884
char *interfaces_hooks = NULL;
1432
1886
bool gnutls_initialized = false;
1433
1887
bool gpgme_initialized = false;
1608
2065
/* Work around Debian bug #633582:
1609
2066
<http://bugs.debian.org/633582> */
1612
/* 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");
2068
/* Re-raise privileges */
2069
ret_errno = raise_privileges();
2072
perror_plus("Failed to raise privileges");
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("DEVICE", interface, 1);
1693
perror_plus("setenv");
1696
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1698
perror_plus("setenv");
1701
ret = setenv("MODE", "start", 1);
1703
perror_plus("setenv");
1707
ret = asprintf(&delaystring, "%f", delay);
1709
perror_plus("asprintf");
1712
ret = setenv("DELAY", delaystring, 1);
1715
perror_plus("setenv");
1719
ret = execl(fullname, direntry->d_name, "start", NULL);
1720
perror_plus("execl");
1723
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1724
perror_plus("waitpid");
1728
if(WIFEXITED(status)){
1729
if(WEXITSTATUS(status) != 0){
1730
fprintf(stderr, "Mandos plugin mandos-client: "
1731
"Warning: network hook \"%s\" exited"
1732
" with status %d\n", direntry->d_name,
1733
WEXITSTATUS(status));
1737
} else if(WIFSIGNALED(status)){
1738
fprintf(stderr, "Mandos plugin mandos-client: "
1739
"Warning: network hook \"%s\" died by"
1740
" signal %d\n", direntry->d_name,
1745
fprintf(stderr, "Mandos plugin mandos-client: "
1746
"Warning: network hook \"%s\" crashed\n",
2076
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2077
int seckey_fd = open(seckey, O_RDONLY);
2078
if(seckey_fd == -1){
2079
perror_plus("open");
2081
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
2083
perror_plus("fstat");
2085
if(S_ISREG(st.st_mode)
2086
and st.st_uid == 0 and st.st_gid == 0){
2087
ret = fchown(seckey_fd, uid, gid);
2089
perror_plus("fchown");
2093
TEMP_FAILURE_RETRY(close(seckey_fd));
2097
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
2098
int pubkey_fd = open(pubkey, O_RDONLY);
2099
if(pubkey_fd == -1){
2100
perror_plus("open");
2102
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
2104
perror_plus("fstat");
2106
if(S_ISREG(st.st_mode)
2107
and st.st_uid == 0 and st.st_gid == 0){
2108
ret = fchown(pubkey_fd, uid, gid);
2110
perror_plus("fchown");
2114
TEMP_FAILURE_RETRY(close(pubkey_fd));
2118
/* Lower privileges */
2119
ret_errno = lower_privileges();
2122
perror_plus("Failed to lower privileges");
2127
/* Remove invalid interface names (except "none") */
2129
char *interface = NULL;
2130
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2132
if(strcmp(interface, "none") != 0
2133
and if_nametoindex(interface) == 0){
2134
if(interface[0] != '\0'){
2135
fprintf_plus(stderr, "Not using nonexisting interface"
2136
" \"%s\"\n", interface);
2138
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
2144
/* Run network hooks */
2146
if(mc.interfaces != NULL){
2147
interfaces_hooks = malloc(mc.interfaces_size);
2148
if(interfaces_hooks == NULL){
2149
perror_plus("malloc");
2152
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2153
argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2155
run_network_hooks("start", interfaces_hooks != NULL ?
2156
interfaces_hooks : "", delay);
1762
2160
avahi_set_log_function(empty_log);
1765
if(interface[0] == '\0'){
1766
struct dirent **direntries;
1767
/* First look for interfaces that are up */
1768
ret = scandir(sys_class_net, &direntries, up_interface,
1771
/* No up interfaces, look for any good interfaces */
1773
ret = scandir(sys_class_net, &direntries, good_interface,
1777
/* Pick the first interface returned */
1778
interface = strdup(direntries[0]->d_name);
1780
fprintf(stderr, "Mandos plugin mandos-client: "
1781
"Using interface \"%s\"\n", interface);
1783
if(interface == NULL){
1784
perror_plus("malloc");
1786
exitcode = EXIT_FAILURE;
1792
fprintf(stderr, "Mandos plugin mandos-client: "
1793
"Could not find a network interface\n");
1794
exitcode = EXIT_FAILURE;
1799
2163
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1800
2164
from the signal handler */
1801
2165
/* Initialize the pseudo-RNG for Avahi */
1802
2166
srand((unsigned int) time(NULL));
1803
mc.simple_poll = avahi_simple_poll_new();
1804
if(mc.simple_poll == NULL){
1805
fprintf(stderr, "Mandos plugin mandos-client: "
1806
"Avahi: Failed to create simple poll object.\n");
2167
simple_poll = avahi_simple_poll_new();
2168
if(simple_poll == NULL){
2169
fprintf_plus(stderr,
2170
"Avahi: Failed to create simple poll object.\n");
1807
2171
exitcode = EX_UNAVAILABLE;
1874
/* If the interface is down, bring it up */
1875
if(strcmp(interface, "none") != 0){
1876
if_index = (AvahiIfIndex) if_nametoindex(interface);
1878
fprintf(stderr, "Mandos plugin mandos-client: "
1879
"No such interface: \"%s\"\n", interface);
1880
exitcode = EX_UNAVAILABLE;
1888
/* Re-raise priviliges */
1892
perror_plus("seteuid");
1896
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1897
messages about the network interface to mess up the prompt */
1898
ret = klogctl(8, NULL, 5);
1899
bool restore_loglevel = true;
1901
restore_loglevel = false;
1902
perror_plus("klogctl");
1904
#endif /* __linux__ */
1906
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1908
perror_plus("socket");
1909
exitcode = EX_OSERR;
1911
if(restore_loglevel){
1912
ret = klogctl(7, NULL, 0);
1914
perror_plus("klogctl");
1917
#endif /* __linux__ */
1918
/* Lower privileges */
1922
perror_plus("seteuid");
1926
strcpy(network.ifr_name, interface);
1927
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1929
perror_plus("ioctl SIOCGIFFLAGS");
1931
if(restore_loglevel){
1932
ret = klogctl(7, NULL, 0);
1934
perror_plus("klogctl");
1937
#endif /* __linux__ */
1938
exitcode = EX_OSERR;
1939
/* Lower privileges */
1943
perror_plus("seteuid");
1947
if((network.ifr_flags & IFF_UP) == 0){
1948
network.ifr_flags |= IFF_UP;
1949
take_down_interface = true;
1950
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1952
take_down_interface = false;
1953
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1954
exitcode = EX_OSERR;
1956
if(restore_loglevel){
1957
ret = klogctl(7, NULL, 0);
1959
perror_plus("klogctl");
2238
/* If no interfaces were specified, make a list */
2239
if(mc.interfaces == NULL){
2240
struct dirent **direntries;
2241
/* Look for any good interfaces */
2242
ret = scandir(sys_class_net, &direntries, good_interface,
2245
/* Add all found interfaces to interfaces list */
2246
for(int i = 0; i < ret; ++i){
2247
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2248
direntries[i]->d_name);
2251
perror_plus("argz_add");
2255
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2256
direntries[i]->d_name);
2262
fprintf_plus(stderr, "Could not find a network interface\n");
2263
exitcode = EXIT_FAILURE;
2268
/* Bring up interfaces which are down, and remove any "none"s */
2270
char *interface = NULL;
2271
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2273
/* If interface name is "none", stop bringing up interfaces.
2274
Also remove all instances of "none" from the list */
2275
if(strcmp(interface, "none") == 0){
2276
argz_delete(&mc.interfaces, &mc.interfaces_size,
2279
while((interface = argz_next(mc.interfaces,
2280
mc.interfaces_size, interface))){
2281
if(strcmp(interface, "none") == 0){
2282
argz_delete(&mc.interfaces, &mc.interfaces_size,
1962
#endif /* __linux__ */
1963
/* Lower privileges */
1967
perror_plus("seteuid");
1972
/* Sleep checking until interface is running.
1973
Check every 0.25s, up to total time of delay */
1974
for(int i=0; i < delay * 4; i++){
1975
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1977
perror_plus("ioctl SIOCGIFFLAGS");
1978
} else if(network.ifr_flags & IFF_RUNNING){
1981
struct timespec sleeptime = { .tv_nsec = 250000000 };
1982
ret = nanosleep(&sleeptime, NULL);
1983
if(ret == -1 and errno != EINTR){
1984
perror_plus("nanosleep");
1987
if(not take_down_interface){
1988
/* We won't need the socket anymore */
1989
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1991
perror_plus("close");
1995
if(restore_loglevel){
1996
/* Restores kernel loglevel to default */
1997
ret = klogctl(7, NULL, 0);
1999
perror_plus("klogctl");
2002
#endif /* __linux__ */
2003
/* Lower privileges */
2005
if(take_down_interface){
2006
/* Lower privileges */
2009
perror_plus("seteuid");
2012
/* Lower privileges permanently */
2015
perror_plus("setuid");
2289
bool interface_was_up = interface_is_up(interface);
2290
errno = bring_up_interface(interface, delay);
2291
if(not interface_was_up){
2293
perror_plus("Failed to bring up interface");
2295
errno = argz_add(&interfaces_to_take_down,
2296
&interfaces_to_take_down_size,
2299
perror_plus("argz_add");
2304
if(debug and (interfaces_to_take_down == NULL)){
2305
fprintf_plus(stderr, "No interfaces were brought up\n");
2309
/* If we only got one interface, explicitly use only that one */
2310
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2312
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2315
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
2024
ret = init_gnutls_global(pubkey, seckey);
2322
ret = init_gnutls_global(pubkey, seckey, &mc);
2026
fprintf(stderr, "Mandos plugin mandos-client: "
2027
"init_gnutls_global failed\n");
2324
fprintf_plus(stderr, "init_gnutls_global failed\n");
2028
2325
exitcode = EX_UNAVAILABLE;
2235
/* XXX run network hooks "stop" here */
2237
/* Take down the network interface */
2238
if(take_down_interface){
2239
/* Re-raise priviliges */
2243
perror_plus("seteuid");
2536
/* Re-raise privileges */
2538
ret_errno = raise_privileges();
2540
perror_plus("Failed to raise privileges");
2543
/* Run network hooks */
2544
run_network_hooks("stop", interfaces_hooks != NULL ?
2545
interfaces_hooks : "", delay);
2547
/* Take down the network interfaces which were brought up */
2549
char *interface = NULL;
2550
while((interface=argz_next(interfaces_to_take_down,
2551
interfaces_to_take_down_size,
2553
ret_errno = take_down_interface(interface);
2556
perror_plus("Failed to take down interface");
2559
if(debug and (interfaces_to_take_down == NULL)){
2560
fprintf_plus(stderr, "No interfaces needed to be taken"
2246
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2248
perror_plus("ioctl SIOCGIFFLAGS");
2249
} else if(network.ifr_flags & IFF_UP){
2250
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2251
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2253
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2256
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2258
perror_plus("close");
2260
/* Lower privileges permanently */
2264
perror_plus("setuid");
2566
ret_errno = lower_privileges_permanently();
2568
perror_plus("Failed to lower privileges permanently");
2572
free(interfaces_to_take_down);
2573
free(interfaces_hooks);
2269
2575
/* Removes the GPGME temp directory and all files inside */
2270
if(tempdir_created){
2576
if(tempdir != NULL){
2271
2577
struct dirent **direntries = NULL;
2272
2578
struct dirent *direntry = NULL;
2273
2579
int numentries = scandir(tempdir, &direntries, notdotentries,
2275
if (numentries > 0){
2276
2582
for(int i = 0; i < numentries; i++){
2277
2583
direntry = direntries[i];
2278
2584
char *fullname = NULL;