187
201
return buffer_capacity;
190
int add_server(const char *ip, uint16_t port,
191
AvahiIfIndex if_index,
204
/* Add server to set of servers to retry periodically */
205
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
194
208
server *new_server = malloc(sizeof(server));
195
209
if(new_server == NULL){
196
210
perror_plus("malloc");
199
213
*new_server = (server){ .ip = strdup(ip),
201
.if_index = if_index,
215
.if_index = if_index,
203
217
if(new_server->ip == NULL){
204
218
perror_plus("strdup");
207
/* unique case of first server */
221
/* Special case of first server */
208
222
if (mc.current_server == NULL){
209
223
new_server->next = new_server;
210
224
new_server->prev = new_server;
211
225
mc.current_server = new_server;
212
/* Placing the new server last in the list */
226
/* Place the new server last in the list */
214
228
new_server->next = mc.current_server;
215
229
new_server->prev = mc.current_server->prev;
250
264
rc = gpgme_data_new_from_fd(&pgp_data, fd);
251
265
if(rc != GPG_ERR_NO_ERROR){
252
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
253
gpgme_strsource(rc), gpgme_strerror(rc));
266
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
267
gpgme_strsource(rc), gpgme_strerror(rc));
257
271
rc = gpgme_op_import(mc.ctx, pgp_data);
258
272
if(rc != GPG_ERR_NO_ERROR){
259
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
260
gpgme_strsource(rc), gpgme_strerror(rc));
273
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
274
gpgme_strsource(rc), gpgme_strerror(rc));
273
fprintf(stderr, "Initializing GPGME\n");
287
fprintf_plus(stderr, "Initializing GPGME\n");
277
291
gpgme_check_version(NULL);
278
292
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
279
293
if(rc != GPG_ERR_NO_ERROR){
280
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
281
gpgme_strsource(rc), gpgme_strerror(rc));
294
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
295
gpgme_strsource(rc), gpgme_strerror(rc));
285
/* Set GPGME home directory for the OpenPGP engine only */
299
/* Set GPGME home directory for the OpenPGP engine only */
286
300
rc = gpgme_get_engine_info(&engine_info);
287
301
if(rc != GPG_ERR_NO_ERROR){
288
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
289
gpgme_strsource(rc), gpgme_strerror(rc));
302
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
303
gpgme_strsource(rc), gpgme_strerror(rc));
292
306
while(engine_info != NULL){
331
347
ssize_t plaintext_length = 0;
334
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
350
fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
337
353
/* Create new GPGME data buffer from memory cryptotext */
338
354
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
340
356
if(rc != GPG_ERR_NO_ERROR){
341
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
342
gpgme_strsource(rc), gpgme_strerror(rc));
357
fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
358
gpgme_strsource(rc), gpgme_strerror(rc));
346
362
/* Create new empty GPGME data buffer for the plaintext */
347
363
rc = gpgme_data_new(&dh_plain);
348
364
if(rc != GPG_ERR_NO_ERROR){
349
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
350
gpgme_strsource(rc), gpgme_strerror(rc));
365
fprintf_plus(stderr, "Mandos plugin mandos-client: "
366
"bad gpgme_data_new: %s: %s\n",
367
gpgme_strsource(rc), gpgme_strerror(rc));
351
368
gpgme_data_release(dh_crypto);
357
374
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
358
375
if(rc != GPG_ERR_NO_ERROR){
359
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
360
gpgme_strsource(rc), gpgme_strerror(rc));
376
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
377
gpgme_strsource(rc), gpgme_strerror(rc));
361
378
plaintext_length = -1;
363
380
gpgme_decrypt_result_t result;
364
381
result = gpgme_op_decrypt_result(mc.ctx);
365
382
if(result == NULL){
366
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
383
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
368
fprintf(stderr, "Unsupported algorithm: %s\n",
369
result->unsupported_algorithm);
370
fprintf(stderr, "Wrong key usage: %u\n",
371
result->wrong_key_usage);
385
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
386
result->unsupported_algorithm);
387
fprintf_plus(stderr, "Wrong key usage: %u\n",
388
result->wrong_key_usage);
372
389
if(result->file_name != NULL){
373
fprintf(stderr, "File name: %s\n", result->file_name);
390
fprintf_plus(stderr, "File name: %s\n", result->file_name);
375
392
gpgme_recipient_t recipient;
376
393
recipient = result->recipients;
377
394
while(recipient != NULL){
378
fprintf(stderr, "Public key algorithm: %s\n",
379
gpgme_pubkey_algo_name(recipient->pubkey_algo));
380
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
381
fprintf(stderr, "Secret key available: %s\n",
382
recipient->status == GPG_ERR_NO_SECKEY
395
fprintf_plus(stderr, "Public key algorithm: %s\n",
396
gpgme_pubkey_algo_name
397
(recipient->pubkey_algo));
398
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
399
fprintf_plus(stderr, "Secret key available: %s\n",
400
recipient->status == GPG_ERR_NO_SECKEY
384
402
recipient = recipient->next;
483
501
/* OpenPGP credentials */
484
502
ret = gnutls_certificate_allocate_credentials(&mc.cred);
485
503
if(ret != GNUTLS_E_SUCCESS){
486
fprintf(stderr, "GnuTLS memory error: %s\n",
487
safer_gnutls_strerror(ret));
504
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
505
safer_gnutls_strerror(ret));
488
506
gnutls_global_deinit();
493
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
494
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
511
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
512
" secret key %s as GnuTLS credentials\n",
498
517
ret = gnutls_certificate_set_openpgp_key_file
499
518
(mc.cred, pubkeyfilename, seckeyfilename,
500
519
GNUTLS_OPENPGP_FMT_BASE64);
501
520
if(ret != GNUTLS_E_SUCCESS){
503
"Error[%d] while reading the OpenPGP key pair ('%s',"
504
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
505
fprintf(stderr, "The GnuTLS error is: %s\n",
506
safer_gnutls_strerror(ret));
522
"Error[%d] while reading the OpenPGP key pair ('%s',"
523
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
524
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
525
safer_gnutls_strerror(ret));
510
529
/* GnuTLS server initialization */
511
530
ret = gnutls_dh_params_init(&mc.dh_params);
512
531
if(ret != GNUTLS_E_SUCCESS){
513
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
514
" %s\n", safer_gnutls_strerror(ret));
532
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
533
" initialization: %s\n",
534
safer_gnutls_strerror(ret));
517
537
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
518
538
if(ret != GNUTLS_E_SUCCESS){
519
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
520
safer_gnutls_strerror(ret));
539
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
540
safer_gnutls_strerror(ret));
708
729
if(if_indextoname((unsigned int)if_index, interface) == NULL){
709
730
perror_plus("if_indextoname");
711
fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
712
ip, interface, port);
732
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
733
"\n", ip, interface, port);
715
fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
736
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
718
739
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
719
740
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
999
1021
char ip[AVAHI_ADDRESS_STR_MAX];
1000
1022
avahi_address_snprint(ip, sizeof(ip), address);
1002
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
1003
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1004
ip, (intmax_t)interface, port);
1024
fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1025
PRIdMAX ") on port %" PRIu16 "\n", name,
1026
host_name, ip, (intmax_t)interface, port);
1006
1028
int ret = start_mandos_communication(ip, port, interface,
1007
1029
avahi_proto_to_af(proto));
1009
1031
avahi_simple_poll_quit(mc.simple_poll);
1011
ret = add_server(ip, port, interface,
1012
avahi_proto_to_af(proto));
1033
if(not add_server(ip, port, interface,
1034
avahi_proto_to_af(proto))){
1035
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1084
1111
errno = old_errno;
1088
* This function determines if a directory entry in /sys/class/net
1089
* corresponds to an acceptable network device.
1090
* (This function is passed to scandir(3) as a filter function.)
1092
int good_interface(const struct dirent *if_entry){
1094
char *flagname = NULL;
1095
if(if_entry->d_name[0] == '.'){
1098
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1101
perror_plus("asprintf");
1104
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1106
perror_plus("open");
1111
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1112
/* read line from flags_fd */
1113
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1114
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
flagstring[(size_t)to_read] = '\0';
1116
if(flagstring == NULL){
1117
perror_plus("malloc");
1122
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1125
perror_plus("read");
1139
tmpmax = strtoimax(flagstring, &tmp, 0);
1140
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1141
and not (isspace(*tmp)))
1142
or tmpmax != (ifreq_flags)tmpmax){
1114
bool get_flags(const char *ifname, struct ifreq *ifr){
1117
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1119
perror_plus("socket");
1122
strcpy(ifr->ifr_name, ifname);
1123
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1144
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1145
flagstring, if_entry->d_name);
1126
perror_plus("ioctl SIOCGIFFLAGS");
1151
ifreq_flags flags = (ifreq_flags)tmpmax;
1133
bool good_flags(const char *ifname, const struct ifreq *ifr){
1152
1135
/* Reject the loopback device */
1153
if(flags & IFF_LOOPBACK){
1136
if(ifr->ifr_flags & IFF_LOOPBACK){
1155
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1138
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1160
1143
/* Accept point-to-point devices only if connect_to is specified */
1161
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1144
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1163
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1146
fprintf_plus(stderr, "Accepting point-to-point interface"
1147
" \"%s\"\n", ifname);
1168
1151
/* Otherwise, reject non-broadcast-capable devices */
1169
if(not (flags & IFF_BROADCAST)){
1152
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1171
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1154
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1155
" \"%s\"\n", ifname);
1176
1159
/* Reject non-ARP interfaces (including dummy interfaces) */
1177
if(flags & IFF_NOARP){
1160
if(ifr->ifr_flags & IFF_NOARP){
1179
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1162
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1184
1168
/* Accept this device */
1186
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1170
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1176
* This function determines if a directory entry in /sys/class/net
1177
* corresponds to an acceptable network device.
1178
* (This function is passed to scandir(3) as a filter function.)
1180
int good_interface(const struct dirent *if_entry){
1181
if(if_entry->d_name[0] == '.'){
1186
if(not get_flags(if_entry->d_name, &ifr)){
1188
fprintf_plus(stderr, "Failed to get flags for interface "
1189
"\"%s\"\n", if_entry->d_name);
1194
if(not good_flags(if_entry->d_name, &ifr)){
1201
* This function determines if a directory entry in /sys/class/net
1202
* corresponds to an acceptable network device which is up.
1203
* (This function is passed to scandir(3) as a filter function.)
1205
int up_interface(const struct dirent *if_entry){
1206
if(if_entry->d_name[0] == '.'){
1211
if(not get_flags(if_entry->d_name, &ifr)){
1213
fprintf_plus(stderr, "Failed to get flags for interface "
1214
"\"%s\"\n", if_entry->d_name);
1219
/* Reject down interfaces */
1220
if(not (ifr.ifr_flags & IFF_UP)){
1222
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1228
/* Reject non-running interfaces */
1229
if(not (ifr.ifr_flags & IFF_RUNNING)){
1231
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1237
if(not good_flags(if_entry->d_name, &ifr)){
1254
/* Is this directory entry a runnable program? */
1255
int runnable_hook(const struct dirent *direntry){
1260
if((direntry->d_name)[0] == '\0'){
1265
sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1266
"abcdefghijklmnopqrstuvwxyz"
1269
if((direntry->d_name)[sret] != '\0'){
1270
/* Contains non-allowed characters */
1272
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1278
char *fullname = NULL;
1279
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1281
perror_plus("asprintf");
1285
ret = stat(fullname, &st);
1288
perror_plus("Could not stat hook");
1292
if(not (S_ISREG(st.st_mode))){
1293
/* Not a regular file */
1295
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1300
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1301
/* Not executable */
1303
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1309
fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1203
1315
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1205
1317
struct timespec now;
1206
1318
struct timespec waited_time;
1207
1319
intmax_t block_time;
1210
1322
if(mc.current_server == NULL){
1213
"Wait until first server is found. No timeout!\n");
1324
fprintf_plus(stderr, "Wait until first server is found."
1215
1327
ret = avahi_simple_poll_iterate(s, -1);
1218
fprintf(stderr, "Check current_server if we should run it,"
1330
fprintf_plus(stderr, "Check current_server if we should run"
1221
1333
/* the current time */
1222
1334
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1264
1377
ret = avahi_simple_poll_iterate(s, (int)block_time);
1267
if (ret > 0 or errno != EINTR) {
1380
if (ret > 0 or errno != EINTR){
1268
1381
return (ret != 1) ? ret : 0;
1387
bool run_network_hooks(const char *mode, const char *interface,
1389
struct dirent **direntries;
1390
struct dirent *direntry;
1392
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1395
perror_plus("scandir");
1397
int devnull = open("/dev/null", O_RDONLY);
1398
for(int i = 0; i < numhooks; i++){
1399
direntry = direntries[i];
1400
char *fullname = NULL;
1401
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1403
perror_plus("asprintf");
1407
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1410
pid_t hook_pid = fork();
1413
/* Raise privileges */
1417
perror_plus("seteuid");
1419
/* Raise privileges even more */
1423
perror_plus("setuid");
1429
perror_plus("setgid");
1431
/* Reset supplementary groups */
1433
ret = setgroups(0, NULL);
1435
perror_plus("setgroups");
1437
dup2(devnull, STDIN_FILENO);
1439
dup2(STDERR_FILENO, STDOUT_FILENO);
1440
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1442
perror_plus("setenv");
1445
ret = setenv("DEVICE", interface, 1);
1447
perror_plus("setenv");
1450
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1452
perror_plus("setenv");
1455
ret = setenv("MODE", mode, 1);
1457
perror_plus("setenv");
1461
ret = asprintf(&delaystring, "%f", delay);
1463
perror_plus("asprintf");
1466
ret = setenv("DELAY", delaystring, 1);
1469
perror_plus("setenv");
1473
ret = execl(fullname, direntry->d_name, mode, NULL);
1474
perror_plus("execl");
1477
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1478
perror_plus("waitpid");
1482
if(WIFEXITED(status)){
1483
if(WEXITSTATUS(status) != 0){
1484
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1485
" with status %d\n", direntry->d_name,
1486
WEXITSTATUS(status));
1490
} else if(WIFSIGNALED(status)){
1491
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1492
" signal %d\n", direntry->d_name,
1497
fprintf_plus(stderr, "Warning: network hook \"%s\""
1498
" crashed\n", direntry->d_name);
1505
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1274
1514
int main(int argc, char *argv[]){
1275
1515
AvahiSServiceBrowser *sb = NULL;
1460
1708
/* Work around Debian bug #633582:
1461
1709
<http://bugs.debian.org/633582> */
1464
1711
/* Re-raise priviliges */
1466
1713
ret = seteuid(0);
1468
1715
perror_plus("seteuid");
1471
int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
1472
if(seckey_fd == -1){
1473
perror_plus("open");
1475
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1477
perror_plus("fstat");
1479
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1480
ret = fchown(seckey_fd, uid, gid);
1482
perror_plus("fchown");
1486
TEMP_FAILURE_RETRY(close(seckey_fd));
1489
int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
1490
if(pubkey_fd == -1){
1491
perror_plus("open");
1493
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1495
perror_plus("fstat");
1497
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1498
ret = fchown(pubkey_fd, uid, gid);
1500
perror_plus("fchown");
1504
TEMP_FAILURE_RETRY(close(pubkey_fd));
1507
/* Lower privileges */
1511
perror_plus("seteuid");
1719
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1720
int seckey_fd = open(seckey, O_RDONLY);
1721
if(seckey_fd == -1){
1722
perror_plus("open");
1724
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1726
perror_plus("fstat");
1728
if(S_ISREG(st.st_mode)
1729
and st.st_uid == 0 and st.st_gid == 0){
1730
ret = fchown(seckey_fd, uid, gid);
1732
perror_plus("fchown");
1736
TEMP_FAILURE_RETRY(close(seckey_fd));
1740
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1741
int pubkey_fd = open(pubkey, O_RDONLY);
1742
if(pubkey_fd == -1){
1743
perror_plus("open");
1745
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1747
perror_plus("fstat");
1749
if(S_ISREG(st.st_mode)
1750
and st.st_uid == 0 and st.st_gid == 0){
1751
ret = fchown(pubkey_fd, uid, gid);
1753
perror_plus("fchown");
1757
TEMP_FAILURE_RETRY(close(pubkey_fd));
1761
/* Lower privileges */
1765
perror_plus("seteuid");
1770
/* Run network hooks */
1771
if(not run_network_hooks("start", interface, delay)){
1516
1776
avahi_set_log_function(empty_log);
1519
1779
if(interface[0] == '\0'){
1520
1780
struct dirent **direntries;
1521
ret = scandir(sys_class_net, &direntries, good_interface,
1781
/* First look for interfaces that are up */
1782
ret = scandir(sys_class_net, &direntries, up_interface,
1785
/* No up interfaces, look for any good interfaces */
1787
ret = scandir(sys_class_net, &direntries, good_interface,
1524
/* Pick the first good interface */
1791
/* Pick the first interface returned */
1525
1792
interface = strdup(direntries[0]->d_name);
1527
fprintf(stderr, "Using interface \"%s\"\n", interface);
1794
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1529
1796
if(interface == NULL){
1530
1797
perror_plus("malloc");