164
155
const char *priority;
166
157
server *current_server;
168
size_t interfaces_size;
169
158
} mandos_context;
171
/* global so signal handler can reach it*/
172
AvahiSimplePoll *simple_poll;
160
/* global context so signal handler can reach it*/
161
mandos_context mc = { .simple_poll = NULL, .server = NULL,
162
.dh_bits = 1024, .priority = "SECURE256"
163
":!CTYPE-X.509:+CTYPE-OPENPGP",
164
.current_server = NULL };
174
166
sig_atomic_t quit_now = 0;
175
167
int signal_received = 0;
177
169
/* Function to use when printing errors */
178
170
void perror_plus(const char *print_text){
180
171
fprintf(stderr, "Mandos plugin %s: ",
181
172
program_invocation_short_name);
183
173
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));
197
177
* Make additional room in "buffer" for at least BUFFER_SIZE more
198
178
* bytes. "buffer_capacity" is how much is currently allocated,
199
179
* "buffer_length" is how much is already used.
201
__attribute__((nonnull, warn_unused_result))
202
181
size_t incbuffer(char **buffer, size_t buffer_length,
203
size_t buffer_capacity){
182
size_t buffer_capacity){
204
183
if(buffer_length + BUFFER_SIZE > buffer_capacity){
205
char *new_buf = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
207
int old_errno = errno;
184
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
214
188
buffer_capacity += BUFFER_SIZE;
216
190
return buffer_capacity;
219
193
/* Add server to set of servers to retry periodically */
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){
194
int add_server(const char *ip, uint16_t port,
195
AvahiIfIndex if_index,
224
198
server *new_server = malloc(sizeof(server));
225
199
if(new_server == NULL){
226
200
perror_plus("malloc");
229
203
*new_server = (server){ .ip = strdup(ip),
231
.if_index = if_index,
205
.if_index = if_index,
233
207
if(new_server->ip == NULL){
234
208
perror_plus("strdup");
237
ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
239
perror_plus("clock_gettime");
242
211
/* Special case of first server */
243
if(*current_server == NULL){
212
if (mc.current_server == NULL){
244
213
new_server->next = new_server;
245
214
new_server->prev = new_server;
246
*current_server = new_server;
215
mc.current_server = new_server;
216
/* Place the new server last in the list */
248
/* Place the new server last in the list */
249
new_server->next = *current_server;
250
new_server->prev = (*current_server)->prev;
218
new_server->next = mc.current_server;
219
new_server->prev = mc.current_server->prev;
251
220
new_server->prev->next = new_server;
252
(*current_server)->prev = 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");
258
232
* Initialize GPGME.
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,
234
static bool init_gpgme(const char *seckey,
235
const char *pubkey, const char *tempdir){
265
236
gpgme_error_t rc;
266
237
gpgme_engine_info_t engine_info;
269
241
* Helper function to insert pub and seckey to the engine keyring.
271
bool import_key(const char * const filename){
243
bool import_key(const char *filename){
274
246
gpgme_data_t pgp_data;
367
342
ssize_t plaintext_length = 0;
370
fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
345
fprintf(stderr, "Mandos plugin mandos-client: "
346
"Trying to decrypt OpenPGP data\n");
373
349
/* Create new GPGME data buffer from memory cryptotext */
374
350
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
376
352
if(rc != GPG_ERR_NO_ERROR){
377
fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
378
gpgme_strsource(rc), gpgme_strerror(rc));
353
fprintf(stderr, "Mandos plugin mandos-client: "
354
"bad gpgme_data_new_from_mem: %s: %s\n",
355
gpgme_strsource(rc), gpgme_strerror(rc));
382
359
/* Create new empty GPGME data buffer for the plaintext */
383
360
rc = gpgme_data_new(&dh_plain);
384
361
if(rc != GPG_ERR_NO_ERROR){
385
fprintf_plus(stderr, "Mandos plugin mandos-client: "
386
"bad gpgme_data_new: %s: %s\n",
387
gpgme_strsource(rc), gpgme_strerror(rc));
362
fprintf(stderr, "Mandos plugin mandos-client: "
363
"bad gpgme_data_new: %s: %s\n",
364
gpgme_strsource(rc), gpgme_strerror(rc));
388
365
gpgme_data_release(dh_crypto);
392
369
/* Decrypt data from the cryptotext data buffer to the plaintext
394
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
371
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
395
372
if(rc != GPG_ERR_NO_ERROR){
396
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
397
gpgme_strsource(rc), gpgme_strerror(rc));
373
fprintf(stderr, "Mandos plugin mandos-client: "
374
"bad gpgme_op_decrypt: %s: %s\n",
375
gpgme_strsource(rc), gpgme_strerror(rc));
398
376
plaintext_length = -1;
400
378
gpgme_decrypt_result_t result;
401
result = gpgme_op_decrypt_result(mc->ctx);
379
result = gpgme_op_decrypt_result(mc.ctx);
402
380
if(result == NULL){
403
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
381
fprintf(stderr, "Mandos plugin mandos-client: "
382
"gpgme_op_decrypt_result failed\n");
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);
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);
409
390
if(result->file_name != NULL){
410
fprintf_plus(stderr, "File name: %s\n", result->file_name);
391
fprintf(stderr, "Mandos plugin mandos-client: "
392
"File name: %s\n", result->file_name);
412
394
gpgme_recipient_t recipient;
413
395
recipient = result->recipients;
414
396
while(recipient != NULL){
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
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
422
406
recipient = recipient->next;
524
508
/* OpenPGP credentials */
525
ret = gnutls_certificate_allocate_credentials(&mc->cred);
509
ret = gnutls_certificate_allocate_credentials(&mc.cred);
526
510
if(ret != GNUTLS_E_SUCCESS){
527
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
528
safer_gnutls_strerror(ret));
511
fprintf(stderr, "Mandos plugin mandos-client: "
512
"GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
529
513
gnutls_global_deinit();
534
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
535
" secret key %s as GnuTLS credentials\n",
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,
540
524
ret = gnutls_certificate_set_openpgp_key_file
541
(mc->cred, pubkeyfilename, seckeyfilename,
525
(mc.cred, pubkeyfilename, seckeyfilename,
542
526
GNUTLS_OPENPGP_FMT_BASE64);
543
527
if(ret != GNUTLS_E_SUCCESS){
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));
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));
552
537
/* GnuTLS server initialization */
553
ret = gnutls_dh_params_init(&mc->dh_params);
538
ret = gnutls_dh_params_init(&mc.dh_params);
554
539
if(ret != GNUTLS_E_SUCCESS){
555
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
556
" initialization: %s\n",
557
safer_gnutls_strerror(ret));
540
fprintf(stderr, "Mandos plugin mandos-client: "
541
"Error in GnuTLS DH parameter initialization:"
542
" %s\n", safer_gnutls_strerror(ret));
560
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
545
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
561
546
if(ret != GNUTLS_E_SUCCESS){
562
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
563
safer_gnutls_strerror(ret));
547
fprintf(stderr, "Mandos plugin mandos-client: "
548
"Error in GnuTLS prime generation: %s\n",
549
safer_gnutls_strerror(ret));
567
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
553
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
573
gnutls_certificate_free_credentials(mc->cred);
559
gnutls_certificate_free_credentials(mc.cred);
574
560
gnutls_global_deinit();
575
gnutls_dh_params_deinit(mc->dh_params);
561
gnutls_dh_params_deinit(mc.dh_params);
579
__attribute__((nonnull, warn_unused_result))
580
static int init_gnutls_session(gnutls_session_t *session,
565
static int init_gnutls_session(gnutls_session_t *session){
583
567
/* GnuTLS session creation */
778
744
if(if_indextoname((unsigned int)if_index, interface) == NULL){
779
745
perror_plus("if_indextoname");
781
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
782
"\n", ip, interface, (uintmax_t)port);
747
fprintf(stderr, "Mandos plugin mandos-client: "
748
"Connection to: %s%%%s, port %" PRIu16 "\n",
749
ip, interface, port);
785
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
786
ip, (uintmax_t)port);
752
fprintf(stderr, "Mandos plugin mandos-client: "
753
"Connection to: %s, port %" PRIu16 "\n", ip, port);
788
755
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
789
756
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
790
758
if(af == AF_INET6){
791
ret = getnameinfo((struct sockaddr *)&to,
792
sizeof(struct sockaddr_in6),
793
addrstr, sizeof(addrstr), NULL, 0,
759
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
796
ret = getnameinfo((struct sockaddr *)&to,
797
sizeof(struct sockaddr_in),
798
addrstr, sizeof(addrstr), NULL, 0,
762
pcret = inet_ntop(af, &(to.in.sin_addr), 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);
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);
1082
1044
char ip[AVAHI_ADDRESS_STR_MAX];
1083
1045
avahi_address_snprint(ip, sizeof(ip), address);
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);
1047
fprintf(stderr, "Mandos plugin mandos-client: "
1048
"Mandos server \"%s\" found on %s (%s, %"
1049
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1050
ip, (intmax_t)interface, port);
1089
int ret = start_mandos_communication(ip, (in_port_t)port,
1091
avahi_proto_to_af(proto),
1052
int ret = start_mandos_communication(ip, port, interface,
1053
avahi_proto_to_af(proto));
1094
avahi_simple_poll_quit(simple_poll);
1055
avahi_simple_poll_quit(mc.simple_poll);
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"
1057
ret = add_server(ip, port, interface,
1058
avahi_proto_to_af(proto));
1174
1127
signal_received = sig;
1175
1128
int old_errno = errno;
1176
1129
/* set main loop to exit */
1177
if(simple_poll != NULL){
1178
avahi_simple_poll_quit(simple_poll);
1130
if(mc.simple_poll != NULL){
1131
avahi_simple_poll_quit(mc.simple_poll);
1180
1133
errno = old_errno;
1183
__attribute__((nonnull, warn_unused_result))
1184
1136
bool get_flags(const char *ifname, struct ifreq *ifr){
1188
1139
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1191
1141
perror_plus("socket");
1195
1144
strcpy(ifr->ifr_name, ifname);
1196
1145
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1200
1148
perror_plus("ioctl SIOCGIFFLAGS");
1208
__attribute__((nonnull, warn_unused_result))
1209
1155
bool good_flags(const char *ifname, const struct ifreq *ifr){
1211
1157
/* Reject the loopback device */
1212
1158
if(ifr->ifr_flags & IFF_LOOPBACK){
1214
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1160
fprintf(stderr, "Mandos plugin mandos-client: "
1161
"Rejecting loopback interface \"%s\"\n", ifname);
1219
1165
/* Accept point-to-point devices only if connect_to is specified */
1220
1166
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1222
fprintf_plus(stderr, "Accepting point-to-point interface"
1223
" \"%s\"\n", ifname);
1168
fprintf(stderr, "Mandos plugin mandos-client: "
1169
"Accepting point-to-point interface \"%s\"\n", ifname);
1227
1173
/* Otherwise, reject non-broadcast-capable devices */
1228
1174
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1230
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1231
" \"%s\"\n", ifname);
1176
fprintf(stderr, "Mandos plugin mandos-client: "
1177
"Rejecting non-broadcast interface \"%s\"\n", ifname);
1235
1181
/* Reject non-ARP interfaces (including dummy interfaces) */
1236
1182
if(ifr->ifr_flags & IFF_NOARP){
1238
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1184
fprintf(stderr, "Mandos plugin mandos-client: "
1185
"Rejecting non-ARP interface \"%s\"\n", ifname);
1244
1190
/* Accept this device */
1246
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1192
fprintf(stderr, "Mandos plugin mandos-client: "
1193
"Interface \"%s\" is good\n", ifname);
1262
1208
struct ifreq ifr;
1263
1209
if(not get_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))
1211
fprintf(stderr, "Mandos plugin mandos-client: "
1212
"Failed to get flags for interface \"%s\"\n",
1218
if(not good_flags(if_entry->d_name, &ifr)){
1225
* This function determines if a directory entry in /sys/class/net
1226
* corresponds to an acceptable network device which is up.
1227
* (This function is passed to scandir(3) as a filter function.)
1229
int up_interface(const struct dirent *if_entry){
1230
if(if_entry->d_name[0] == '.'){
1235
if(not get_flags(if_entry->d_name, &ifr)){
1237
fprintf(stderr, "Mandos plugin mandos-client: "
1238
"Failed to get flags for interface \"%s\"\n",
1244
/* Reject down interfaces */
1245
if(not (ifr.ifr_flags & IFF_UP)){
1247
fprintf(stderr, "Mandos plugin mandos-client: "
1248
"Rejecting down interface \"%s\"\n",
1254
/* Reject non-running interfaces */
1255
if(not (ifr.ifr_flags & IFF_RUNNING)){
1257
fprintf(stderr, "Mandos plugin mandos-client: "
1258
"Rejecting non-running interface \"%s\"\n",
1264
if(not good_flags(if_entry->d_name, &ifr)){
1312
1270
int notdotentries(const struct dirent *direntry){
1313
1271
/* Skip "." and ".." */
1314
1272
if(direntry->d_name[0] == '.'
1335
sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1336
"abcdefghijklmnopqrstuvwxyz"
1339
if((direntry->d_name)[sret] != '\0'){
1340
/* Contains non-allowed characters */
1342
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1348
char *fullname = NULL;
1349
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1351
perror_plus("asprintf");
1355
ret = stat(fullname, &st);
1291
/* Save pointer to last character */
1292
char *end = strchr(direntry->d_name, '\0')-1;
1299
if(((direntry->d_name)[0] == '#')
1301
/* Temporary #name# */
1305
/* XXX more rules here */
1307
ret = stat(direntry->d_name, &st);
1358
perror_plus("Could not stat hook");
1310
perror_plus("Could not stat plugin");
1362
1314
if(not (S_ISREG(st.st_mode))){
1363
1315
/* Not a regular file */
1365
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1370
1318
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1371
1319
/* Not executable */
1373
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1379
fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1385
__attribute__((nonnull, warn_unused_result))
1386
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1387
mandos_context *mc){
1325
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1389
1327
struct timespec now;
1390
1328
struct timespec waited_time;
1391
1329
intmax_t block_time;
1394
if(mc->current_server == NULL){
1396
fprintf_plus(stderr, "Wait until first server is found."
1332
if(mc.current_server == NULL){
1334
fprintf(stderr, "Mandos plugin mandos-client: "
1335
"Wait until first server is found. No timeout!\n");
1399
1337
ret = avahi_simple_poll_iterate(s, -1);
1402
fprintf_plus(stderr, "Check current_server if we should run"
1340
fprintf(stderr, "Mandos plugin mandos-client: "
1341
"Check current_server if we should run it,"
1405
1344
/* the current time */
1406
1345
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1449
1388
ret = avahi_simple_poll_iterate(s, (int)block_time);
1452
if(ret > 0 or errno != EINTR){
1391
if (ret > 0 or errno != EINTR){
1453
1392
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();
1718
bool restore_loglevel = false;
1720
perror_plus("Failed to raise privileges");
1725
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1726
messages about the network interface to mess up the prompt */
1727
ret_linux = klogctl(8, NULL, 5);
1728
if(ret_linux == -1){
1729
perror_plus("klogctl");
1731
restore_loglevel = true;
1734
#endif /* __linux__ */
1735
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1736
ioctl_errno = errno;
1738
if(restore_loglevel){
1739
ret_linux = klogctl(7, NULL, 0);
1740
if(ret_linux == -1){
1741
perror_plus("klogctl");
1744
#endif /* __linux__ */
1746
/* If raise_privileges() succeeded above */
1748
/* Lower privileges */
1749
ret_errno = lower_privileges();
1752
perror_plus("Failed to lower privileges");
1756
/* Close the socket */
1757
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1759
perror_plus("close");
1762
if(ret_setflags == -1){
1763
errno = ioctl_errno;
1764
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1769
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1773
/* Sleep checking until interface is running.
1774
Check every 0.25s, up to total time of delay */
1775
for(int i=0; i < delay * 4; i++){
1776
if(interface_is_running(interface)){
1779
struct timespec sleeptime = { .tv_nsec = 250000000 };
1780
ret = nanosleep(&sleeptime, NULL);
1781
if(ret == -1 and errno != EINTR){
1782
perror_plus("nanosleep");
1790
__attribute__((nonnull, warn_unused_result))
1791
error_t take_down_interface(const char *const interface){
1792
error_t old_errno = errno;
1793
struct ifreq network;
1794
unsigned int if_index = if_nametoindex(interface);
1796
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1800
if(interface_is_up(interface)){
1801
error_t ret_errno = 0, ioctl_errno = 0;
1802
if(not get_flags(interface, &network) and debug){
1804
fprintf_plus(stderr, "Failed to get flags for interface "
1805
"\"%s\"\n", interface);
1809
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1811
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1814
perror_plus("socket");
1820
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1824
/* Raise privileges */
1825
ret_errno = raise_privileges();
1827
perror_plus("Failed to raise privileges");
1829
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1830
ioctl_errno = errno;
1832
/* If raise_privileges() succeeded above */
1834
/* Lower privileges */
1835
ret_errno = lower_privileges();
1838
perror_plus("Failed to lower privileges");
1842
/* Close the socket */
1843
int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1845
perror_plus("close");
1848
if(ret_setflags == -1){
1849
errno = ioctl_errno;
1850
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1855
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1863
1398
int main(int argc, char *argv[]){
1864
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1865
.priority = "SECURE256:!CTYPE-X.509:"
1866
"+CTYPE-OPENPGP", .current_server = NULL,
1867
.interfaces = NULL, .interfaces_size = 0 };
1868
1399
AvahiSServiceBrowser *sb = NULL;
1871
1402
intmax_t tmpmax;
1873
1404
int exitcode = EXIT_SUCCESS;
1874
char *interfaces_to_take_down = NULL;
1875
size_t interfaces_to_take_down_size = 0;
1876
char run_tempdir[] = "/run/tmp/mandosXXXXXX";
1877
char old_tempdir[] = "/tmp/mandosXXXXXX";
1878
char *tempdir = NULL;
1405
const char *interface = "";
1406
struct ifreq network;
1408
bool take_down_interface = false;
1411
char tempdir[] = "/tmp/mandosXXXXXX";
1412
bool tempdir_created = false;
1879
1413
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1880
1414
const char *seckey = PATHDIR "/" SECKEY;
1881
1415
const char *pubkey = PATHDIR "/" PUBKEY;
1882
char *interfaces_hooks = NULL;
1884
1417
bool gnutls_initialized = false;
1885
1418
bool gpgme_initialized = false;
2063
1586
/* Work around Debian bug #633582:
2064
1587
<http://bugs.debian.org/633582> */
2066
/* Re-raise privileges */
2067
ret_errno = raise_privileges();
2070
perror_plus("Failed to raise privileges");
1590
/* Re-raise priviliges */
1594
perror_plus("seteuid");
1597
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1598
int seckey_fd = open(seckey, O_RDONLY);
1599
if(seckey_fd == -1){
1600
perror_plus("open");
1602
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1604
perror_plus("fstat");
1606
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1607
ret = fchown(seckey_fd, uid, gid);
1609
perror_plus("fchown");
1613
TEMP_FAILURE_RETRY(close(seckey_fd));
1617
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1618
int pubkey_fd = open(pubkey, O_RDONLY);
1619
if(pubkey_fd == -1){
1620
perror_plus("open");
1622
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1624
perror_plus("fstat");
1626
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1627
ret = fchown(pubkey_fd, uid, gid);
1629
perror_plus("fchown");
1633
TEMP_FAILURE_RETRY(close(pubkey_fd));
1637
/* Lower privileges */
1641
perror_plus("seteuid");
1645
/* Find network hooks and run them */
1647
struct dirent **direntries;
1648
struct dirent *direntry;
1649
int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1652
perror_plus("scandir");
2074
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2075
int seckey_fd = open(seckey, O_RDONLY);
2076
if(seckey_fd == -1){
2077
perror_plus("open");
2079
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
2081
perror_plus("fstat");
2083
if(S_ISREG(st.st_mode)
2084
and st.st_uid == 0 and st.st_gid == 0){
2085
ret = fchown(seckey_fd, uid, gid);
2087
perror_plus("fchown");
2091
TEMP_FAILURE_RETRY(close(seckey_fd));
2095
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
2096
int pubkey_fd = open(pubkey, O_RDONLY);
2097
if(pubkey_fd == -1){
2098
perror_plus("open");
2100
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
2102
perror_plus("fstat");
2104
if(S_ISREG(st.st_mode)
2105
and st.st_uid == 0 and st.st_gid == 0){
2106
ret = fchown(pubkey_fd, uid, gid);
2108
perror_plus("fchown");
2112
TEMP_FAILURE_RETRY(close(pubkey_fd));
2116
/* Lower privileges */
2117
ret_errno = lower_privileges();
2120
perror_plus("Failed to lower privileges");
2125
/* Remove invalid interface names (except "none") */
2127
char *interface = NULL;
2128
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2130
if(strcmp(interface, "none") != 0
2131
and if_nametoindex(interface) == 0){
2132
if(interface[0] != '\0'){
2133
fprintf_plus(stderr, "Not using nonexisting interface"
2134
" \"%s\"\n", interface);
2136
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
2142
/* Run network hooks */
2144
if(mc.interfaces != NULL){
2145
interfaces_hooks = malloc(mc.interfaces_size);
2146
if(interfaces_hooks == NULL){
2147
perror_plus("malloc");
2150
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2151
argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2153
run_network_hooks("start", interfaces_hooks != NULL ?
2154
interfaces_hooks : "", delay);
1654
int devnull = open("/dev/null", O_RDONLY);
1655
for(int i = 0; i < numhooks; i++){
1656
direntry = direntries[0];
1657
char *fullname = NULL;
1658
ret = asprintf(&fullname, "%s/%s", tempdir,
1661
perror_plus("asprintf");
1664
pid_t hook_pid = fork();
1667
dup2(devnull, STDIN_FILENO);
1669
dup2(STDERR_FILENO, STDOUT_FILENO);
1670
ret = setenv("DEVICE", interface, 1);
1672
perror_plus("setenv");
1675
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1677
perror_plus("setenv");
1680
ret = setenv("MODE", "start", 1);
1682
perror_plus("setenv");
1686
ret = asprintf(&delaystring, "%f", delay);
1688
perror_plus("asprintf");
1691
ret = setenv("DELAY", delaystring, 1);
1694
perror_plus("setenv");
1698
ret = execl(fullname, direntry->d_name, "start", NULL);
1699
perror_plus("execl");
1702
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1703
perror_plus("waitpid");
1707
if(WIFEXITED(status)){
1708
if(WEXITSTATUS(status) != 0){
1709
fprintf(stderr, "Mandos plugin mandos-client: "
1710
"Warning: network hook \"%s\" exited"
1711
" with status %d\n", direntry->d_name,
1712
WEXITSTATUS(status));
1716
} else if(WIFSIGNALED(status)){
1717
fprintf(stderr, "Mandos plugin mandos-client: "
1718
"Warning: network hook \"%s\" died by"
1719
" signal %d\n", direntry->d_name,
1724
fprintf(stderr, "Mandos plugin mandos-client: "
1725
"Warning: network hook \"%s\" crashed\n",
2158
1741
avahi_set_log_function(empty_log);
1744
if(interface[0] == '\0'){
1745
struct dirent **direntries;
1746
/* First look for interfaces that are up */
1747
ret = scandir(sys_class_net, &direntries, up_interface,
1750
/* No up interfaces, look for any good interfaces */
1752
ret = scandir(sys_class_net, &direntries, good_interface,
1756
/* Pick the first interface returned */
1757
interface = strdup(direntries[0]->d_name);
1759
fprintf(stderr, "Mandos plugin mandos-client: "
1760
"Using interface \"%s\"\n", interface);
1762
if(interface == NULL){
1763
perror_plus("malloc");
1765
exitcode = EXIT_FAILURE;
1771
fprintf(stderr, "Mandos plugin mandos-client: "
1772
"Could not find a network interface\n");
1773
exitcode = EXIT_FAILURE;
2161
1778
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
2162
1779
from the signal handler */
2163
1780
/* Initialize the pseudo-RNG for Avahi */
2164
1781
srand((unsigned int) time(NULL));
2165
simple_poll = avahi_simple_poll_new();
2166
if(simple_poll == NULL){
2167
fprintf_plus(stderr,
2168
"Avahi: Failed to create simple poll object.\n");
1782
mc.simple_poll = avahi_simple_poll_new();
1783
if(mc.simple_poll == NULL){
1784
fprintf(stderr, "Mandos plugin mandos-client: "
1785
"Avahi: Failed to create simple poll object.\n");
2169
1786
exitcode = EX_UNAVAILABLE;
2236
/* If no interfaces were specified, make a list */
2237
if(mc.interfaces == NULL){
2238
struct dirent **direntries;
2239
/* Look for any good interfaces */
2240
ret = scandir(sys_class_net, &direntries, good_interface,
2243
/* Add all found interfaces to interfaces list */
2244
for(int i = 0; i < ret; ++i){
2245
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2246
direntries[i]->d_name);
2249
perror_plus("argz_add");
2253
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2254
direntries[i]->d_name);
1853
/* If the interface is down, bring it up */
1854
if(strcmp(interface, "none") != 0){
1855
if_index = (AvahiIfIndex) if_nametoindex(interface);
1857
fprintf(stderr, "Mandos plugin mandos-client: "
1858
"No such interface: \"%s\"\n", interface);
1859
exitcode = EX_UNAVAILABLE;
1867
/* Re-raise priviliges */
1871
perror_plus("seteuid");
1875
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1876
messages about the network interface to mess up the prompt */
1877
ret = klogctl(8, NULL, 5);
1878
bool restore_loglevel = true;
1880
restore_loglevel = false;
1881
perror_plus("klogctl");
1883
#endif /* __linux__ */
1885
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1887
perror_plus("socket");
1888
exitcode = EX_OSERR;
1890
if(restore_loglevel){
1891
ret = klogctl(7, NULL, 0);
1893
perror_plus("klogctl");
1896
#endif /* __linux__ */
1897
/* Lower privileges */
1901
perror_plus("seteuid");
1905
strcpy(network.ifr_name, interface);
1906
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1908
perror_plus("ioctl SIOCGIFFLAGS");
1910
if(restore_loglevel){
1911
ret = klogctl(7, NULL, 0);
1913
perror_plus("klogctl");
1916
#endif /* __linux__ */
1917
exitcode = EX_OSERR;
1918
/* Lower privileges */
1922
perror_plus("seteuid");
1926
if((network.ifr_flags & IFF_UP) == 0){
1927
network.ifr_flags |= IFF_UP;
1928
take_down_interface = true;
1929
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1931
take_down_interface = false;
1932
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1933
exitcode = EX_OSERR;
1935
if(restore_loglevel){
1936
ret = klogctl(7, NULL, 0);
1938
perror_plus("klogctl");
1941
#endif /* __linux__ */
1942
/* Lower privileges */
1946
perror_plus("seteuid");
1951
/* Sleep checking until interface is running.
1952
Check every 0.25s, up to total time of delay */
1953
for(int i=0; i < delay * 4; i++){
1954
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1956
perror_plus("ioctl SIOCGIFFLAGS");
1957
} else if(network.ifr_flags & IFF_RUNNING){
1960
struct timespec sleeptime = { .tv_nsec = 250000000 };
1961
ret = nanosleep(&sleeptime, NULL);
1962
if(ret == -1 and errno != EINTR){
1963
perror_plus("nanosleep");
1966
if(not take_down_interface){
1967
/* We won't need the socket anymore */
1968
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1970
perror_plus("close");
1974
if(restore_loglevel){
1975
/* Restores kernel loglevel to default */
1976
ret = klogctl(7, NULL, 0);
1978
perror_plus("klogctl");
1981
#endif /* __linux__ */
1982
/* Lower privileges */
1984
if(take_down_interface){
1985
/* Lower privileges */
1988
perror_plus("seteuid");
2260
fprintf_plus(stderr, "Could not find a network interface\n");
2261
exitcode = EXIT_FAILURE;
2266
/* Bring up interfaces which are down, and remove any "none"s */
2268
char *interface = NULL;
2269
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2271
/* If interface name is "none", stop bringing up interfaces.
2272
Also remove all instances of "none" from the list */
2273
if(strcmp(interface, "none") == 0){
2274
argz_delete(&mc.interfaces, &mc.interfaces_size,
2277
while((interface = argz_next(mc.interfaces,
2278
mc.interfaces_size, interface))){
2279
if(strcmp(interface, "none") == 0){
2280
argz_delete(&mc.interfaces, &mc.interfaces_size,
2287
bool interface_was_up = interface_is_up(interface);
2288
errno = bring_up_interface(interface, delay);
2289
if(not interface_was_up){
2291
perror_plus("Failed to bring up interface");
2293
errno = argz_add(&interfaces_to_take_down,
2294
&interfaces_to_take_down_size,
2297
perror_plus("argz_add");
2302
if(debug and (interfaces_to_take_down == NULL)){
2303
fprintf_plus(stderr, "No interfaces were brought up\n");
2307
/* If we only got one interface, explicitly use only that one */
2308
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2310
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2313
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
1991
/* Lower privileges permanently */
1994
perror_plus("setuid");
2320
ret = init_gnutls_global(pubkey, seckey, &mc);
2003
ret = init_gnutls_global(pubkey, seckey);
2322
fprintf_plus(stderr, "init_gnutls_global failed\n");
2005
fprintf(stderr, "Mandos plugin mandos-client: "
2006
"init_gnutls_global failed\n");
2323
2007
exitcode = EX_UNAVAILABLE;
2534
/* Re-raise privileges */
2536
ret_errno = raise_privileges();
2538
perror_plus("Failed to raise privileges");
2541
/* Run network hooks */
2542
run_network_hooks("stop", interfaces_hooks != NULL ?
2543
interfaces_hooks : "", delay);
2545
/* Take down the network interfaces which were brought up */
2547
char *interface = NULL;
2548
while((interface=argz_next(interfaces_to_take_down,
2549
interfaces_to_take_down_size,
2551
ret_errno = take_down_interface(interface);
2554
perror_plus("Failed to take down interface");
2557
if(debug and (interfaces_to_take_down == NULL)){
2558
fprintf_plus(stderr, "No interfaces needed to be taken"
2214
/* XXX run network hooks "stop" here */
2216
/* Take down the network interface */
2217
if(take_down_interface){
2218
/* Re-raise priviliges */
2222
perror_plus("seteuid");
2563
ret_errno = lower_privileges_permanently();
2565
perror_plus("Failed to lower privileges permanently");
2225
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2227
perror_plus("ioctl SIOCGIFFLAGS");
2228
} else if(network.ifr_flags & IFF_UP){
2229
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2230
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2232
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2235
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2237
perror_plus("close");
2239
/* Lower privileges permanently */
2243
perror_plus("setuid");
2569
free(interfaces_to_take_down);
2570
free(interfaces_hooks);
2572
2248
/* Removes the GPGME temp directory and all files inside */
2573
if(tempdir != NULL){
2249
if(tempdir_created){
2574
2250
struct dirent **direntries = NULL;
2575
2251
struct dirent *direntry = NULL;
2576
2252
int numentries = scandir(tempdir, &direntries, notdotentries,
2254
if (numentries > 0){
2579
2255
for(int i = 0; i < numentries; i++){
2580
2256
direntry = direntries[i];
2581
2257
char *fullname = NULL;