74
74
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
75
75
getuid(), getgid(), seteuid(),
76
setgid(), pause(), _exit() */
77
77
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
78
78
#include <iso646.h> /* not, or, and */
79
79
#include <argp.h> /* struct argp_option, error_t, struct
170
171
/* Function to use when printing errors */
171
172
void perror_plus(const char *print_text){
172
174
fprintf(stderr, "Mandos plugin %s: ",
173
175
program_invocation_short_name);
174
177
perror(print_text);
180
__attribute__((format (gnu_printf, 2, 3)))
177
181
int fprintf_plus(FILE *stream, const char *format, ...){
179
183
va_start (ap, format);
181
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
185
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
186
program_invocation_short_name));
182
187
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
262
267
rc = gpgme_data_new_from_fd(&pgp_data, fd);
263
268
if(rc != GPG_ERR_NO_ERROR){
264
269
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
265
gpgme_strsource(rc), gpgme_strerror(rc));
270
gpgme_strsource(rc), gpgme_strerror(rc));
269
274
rc = gpgme_op_import(mc.ctx, pgp_data);
270
275
if(rc != GPG_ERR_NO_ERROR){
271
276
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
272
gpgme_strsource(rc), gpgme_strerror(rc));
277
gpgme_strsource(rc), gpgme_strerror(rc));
390
396
recipient = result->recipients;
391
397
while(recipient != NULL){
392
398
fprintf_plus(stderr, "Public key algorithm: %s\n",
393
gpgme_pubkey_algo_name(recipient->pubkey_algo));
399
gpgme_pubkey_algo_name
400
(recipient->pubkey_algo));
394
401
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
395
402
fprintf_plus(stderr, "Secret key available: %s\n",
396
recipient->status == GPG_ERR_NO_SECKEY
403
recipient->status == GPG_ERR_NO_SECKEY
398
405
recipient = recipient->next;
496
504
/* OpenPGP credentials */
497
505
ret = gnutls_certificate_allocate_credentials(&mc.cred);
498
506
if(ret != GNUTLS_E_SUCCESS){
499
fprintf_plus(stderr, "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
507
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
508
safer_gnutls_strerror(ret));
500
509
gnutls_global_deinit();
505
514
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
506
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
515
" secret key %s as GnuTLS credentials\n",
510
520
ret = gnutls_certificate_set_openpgp_key_file
514
524
fprintf_plus(stderr,
515
525
"Error[%d] while reading the OpenPGP key pair ('%s',"
516
526
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
517
fprintf_plus(stderr, "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
527
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
528
safer_gnutls_strerror(ret));
521
532
/* GnuTLS server initialization */
522
533
ret = gnutls_dh_params_init(&mc.dh_params);
523
534
if(ret != GNUTLS_E_SUCCESS){
524
fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
525
" %s\n", safer_gnutls_strerror(ret));
535
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
536
" initialization: %s\n",
537
safer_gnutls_strerror(ret));
528
540
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
529
541
if(ret != GNUTLS_E_SUCCESS){
530
542
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
531
safer_gnutls_strerror(ret));
543
safer_gnutls_strerror(ret));
718
732
if(if_indextoname((unsigned int)if_index, interface) == NULL){
719
733
perror_plus("if_indextoname");
721
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
722
ip, interface, port);
735
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
736
"\n", ip, interface, port);
725
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n", ip, port);
739
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
727
742
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
728
743
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
998
1015
case AVAHI_RESOLVER_FAILURE:
999
fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
1000
" of type '%s' in domain '%s': %s\n", name, type, domain,
1001
avahi_strerror(avahi_server_errno(mc.server)));
1016
fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1017
"'%s' of type '%s' in domain '%s': %s\n", name, type,
1019
avahi_strerror(avahi_server_errno(mc.server)));
1004
1022
case AVAHI_RESOLVER_FOUND:
1007
1025
avahi_address_snprint(ip, sizeof(ip), address);
1009
1027
fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1010
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1011
ip, (intmax_t)interface, port);
1028
PRIdMAX ") on port %" PRIu16 "\n", name,
1029
host_name, ip, (intmax_t)interface, port);
1013
1031
int ret = start_mandos_communication(ip, port, interface,
1014
1032
avahi_proto_to_af(proto));
1016
1034
avahi_simple_poll_quit(mc.simple_poll);
1018
ret = add_server(ip, port, interface,
1019
avahi_proto_to_af(proto));
1036
if(not add_server(ip, port, interface,
1037
avahi_proto_to_af(proto))){
1038
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1060
1081
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1061
1082
name, type, domain, protocol, 0,
1062
1083
resolve_callback, NULL) == NULL)
1063
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s': %s\n",
1064
name, avahi_strerror(avahi_server_errno(mc.server)));
1084
fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1086
avahi_strerror(avahi_server_errno(mc.server)));
1067
1089
case AVAHI_BROWSER_REMOVE:
1115
1138
/* Reject the loopback device */
1116
1139
if(ifr->ifr_flags & IFF_LOOPBACK){
1118
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n", ifname);
1141
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1122
1146
/* Accept point-to-point devices only if connect_to is specified */
1123
1147
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1125
fprintf_plus(stderr, "Accepting point-to-point interface \"%s\"\n", ifname);
1149
fprintf_plus(stderr, "Accepting point-to-point interface"
1150
" \"%s\"\n", ifname);
1129
1154
/* Otherwise, reject non-broadcast-capable devices */
1130
1155
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1132
fprintf_plus(stderr, "Rejecting non-broadcast interface \"%s\"\n", ifname);
1157
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1158
" \"%s\"\n", ifname);
1136
1162
/* Reject non-ARP interfaces (including dummy interfaces) */
1137
1163
if(ifr->ifr_flags & IFF_NOARP){
1139
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1165
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1390
bool run_network_hooks(const char *mode, const char *interface,
1392
struct dirent **direntries;
1393
struct dirent *direntry;
1395
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1398
perror_plus("scandir");
1400
int devnull = open("/dev/null", O_RDONLY);
1401
for(int i = 0; i < numhooks; i++){
1402
direntry = direntries[i];
1403
char *fullname = NULL;
1404
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1406
perror_plus("asprintf");
1410
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1413
pid_t hook_pid = fork();
1416
/* Raise privileges */
1420
perror_plus("seteuid");
1422
/* Raise privileges even more */
1426
perror_plus("setuid");
1432
perror_plus("setgid");
1434
/* Reset supplementary groups */
1436
ret = setgroups(0, NULL);
1438
perror_plus("setgroups");
1440
dup2(devnull, STDIN_FILENO);
1442
dup2(STDERR_FILENO, STDOUT_FILENO);
1443
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1445
perror_plus("setenv");
1448
ret = setenv("DEVICE", interface, 1);
1450
perror_plus("setenv");
1453
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1455
perror_plus("setenv");
1458
ret = setenv("MODE", mode, 1);
1460
perror_plus("setenv");
1464
ret = asprintf(&delaystring, "%f", delay);
1466
perror_plus("asprintf");
1469
ret = setenv("DELAY", delaystring, 1);
1472
perror_plus("setenv");
1476
if(connect_to != NULL){
1477
ret = setenv("CONNECT", connect_to, 1);
1479
perror_plus("setenv");
1483
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1484
perror_plus("execl");
1485
_exit(EXIT_FAILURE);
1489
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1490
perror_plus("waitpid");
1494
if(WIFEXITED(status)){
1495
if(WEXITSTATUS(status) != 0){
1496
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1497
" with status %d\n", direntry->d_name,
1498
WEXITSTATUS(status));
1502
} else if(WIFSIGNALED(status)){
1503
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1504
" signal %d\n", direntry->d_name,
1509
fprintf_plus(stderr, "Warning: network hook \"%s\""
1510
" crashed\n", direntry->d_name);
1517
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1357
1526
int main(int argc, char *argv[]){
1358
1527
AvahiSServiceBrowser *sb = NULL;
1552
1720
/* Work around Debian bug #633582:
1553
1721
<http://bugs.debian.org/633582> */
1556
1723
/* Re-raise priviliges */
1558
1725
ret = seteuid(0);
1560
1727
perror_plus("seteuid");
1563
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1564
int seckey_fd = open(seckey, O_RDONLY);
1565
if(seckey_fd == -1){
1566
perror_plus("open");
1568
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1570
perror_plus("fstat");
1572
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1573
ret = fchown(seckey_fd, uid, gid);
1575
perror_plus("fchown");
1579
TEMP_FAILURE_RETRY(close(seckey_fd));
1583
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1584
int pubkey_fd = open(pubkey, O_RDONLY);
1585
if(pubkey_fd == -1){
1586
perror_plus("open");
1588
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1590
perror_plus("fstat");
1592
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1593
ret = fchown(pubkey_fd, uid, gid);
1595
perror_plus("fchown");
1599
TEMP_FAILURE_RETRY(close(pubkey_fd));
1603
/* Lower privileges */
1607
perror_plus("seteuid");
1611
/* Find network hooks and run them */
1613
struct dirent **direntries;
1614
struct dirent *direntry;
1615
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1618
perror_plus("scandir");
1620
int devnull = open("/dev/null", O_RDONLY);
1621
for(int i = 0; i < numhooks; i++){
1622
direntry = direntries[0];
1623
char *fullname = NULL;
1624
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1626
perror_plus("asprintf");
1629
pid_t hook_pid = fork();
1632
dup2(devnull, STDIN_FILENO);
1634
dup2(STDERR_FILENO, STDOUT_FILENO);
1635
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1637
perror_plus("setenv");
1640
ret = setenv("DEVICE", interface, 1);
1642
perror_plus("setenv");
1645
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1647
perror_plus("setenv");
1650
ret = setenv("MODE", "start", 1);
1652
perror_plus("setenv");
1656
ret = asprintf(&delaystring, "%f", delay);
1658
perror_plus("asprintf");
1661
ret = setenv("DELAY", delaystring, 1);
1664
perror_plus("setenv");
1668
ret = execl(fullname, direntry->d_name, "start", NULL);
1669
perror_plus("execl");
1672
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1673
perror_plus("waitpid");
1677
if(WIFEXITED(status)){
1678
if(WEXITSTATUS(status) != 0){
1679
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1680
" with status %d\n", direntry->d_name,
1681
WEXITSTATUS(status));
1685
} else if(WIFSIGNALED(status)){
1686
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1687
" signal %d\n", direntry->d_name,
1692
fprintf_plus(stderr, "Warning: network hook \"%s\" crashed\n",
1731
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1732
int seckey_fd = open(seckey, O_RDONLY);
1733
if(seckey_fd == -1){
1734
perror_plus("open");
1736
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1738
perror_plus("fstat");
1740
if(S_ISREG(st.st_mode)
1741
and st.st_uid == 0 and st.st_gid == 0){
1742
ret = fchown(seckey_fd, uid, gid);
1744
perror_plus("fchown");
1748
TEMP_FAILURE_RETRY(close(seckey_fd));
1752
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1753
int pubkey_fd = open(pubkey, O_RDONLY);
1754
if(pubkey_fd == -1){
1755
perror_plus("open");
1757
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1759
perror_plus("fstat");
1761
if(S_ISREG(st.st_mode)
1762
and st.st_uid == 0 and st.st_gid == 0){
1763
ret = fchown(pubkey_fd, uid, gid);
1765
perror_plus("fchown");
1769
TEMP_FAILURE_RETRY(close(pubkey_fd));
1773
/* Lower privileges */
1777
perror_plus("seteuid");
1782
/* Run network hooks */
1783
if(not run_network_hooks("start", interface, delay)){
1708
1788
avahi_set_log_function(empty_log);