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
87
87
EX_NOHOST, EX_IOERR, EX_PROTOCOL */
88
88
#include <sys/wait.h> /* waitpid(), WIFEXITED(),
89
89
WEXITSTATUS(), WTERMSIG() */
90
#include <grp.h> /* setgroups() */
93
92
#include <sys/klog.h> /* klogctl() */
204
203
/* Add server to set of servers to retry periodically */
205
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
204
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
208
207
server *new_server = malloc(sizeof(server));
209
208
if(new_server == NULL){
210
209
perror_plus("malloc");
213
212
*new_server = (server){ .ip = strdup(ip),
1031
1030
avahi_simple_poll_quit(mc.simple_poll);
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"
1032
ret = add_server(ip, port, interface,
1033
avahi_proto_to_af(proto));
1397
1389
int devnull = open("/dev/null", O_RDONLY);
1398
1390
for(int i = 0; i < numhooks; i++){
1399
direntry = direntries[i];
1391
direntry = direntries[0];
1400
1392
char *fullname = NULL;
1401
1393
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1403
1395
perror_plus("asprintf");
1407
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1410
1398
pid_t hook_pid = fork();
1411
1399
if(hook_pid == 0){
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
1401
dup2(devnull, STDIN_FILENO);
1438
1402
close(devnull);
1439
1403
dup2(STDERR_FILENO, STDOUT_FILENO);
1440
1404
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1442
1406
perror_plus("setenv");
1445
1409
ret = setenv("DEVICE", interface, 1);
1447
1411
perror_plus("setenv");
1450
1414
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1452
1416
perror_plus("setenv");
1455
1419
ret = setenv("MODE", mode, 1);
1457
1421
perror_plus("setenv");
1460
1424
char *delaystring;
1461
1425
ret = asprintf(&delaystring, "%f", delay);
1463
1427
perror_plus("asprintf");
1466
1430
ret = setenv("DELAY", delaystring, 1);
1468
1432
free(delaystring);
1469
1433
perror_plus("setenv");
1472
1436
free(delaystring);
1473
1437
ret = execl(fullname, direntry->d_name, mode, NULL);
1674
1637
argp_state_help(state, state->out_stream,
1675
1638
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1676
1639
case 'V': /* --version */
1640
fprintf_plus(state->out_stream,
1641
"Mandos plugin mandos-client: ");
1677
1642
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1678
1643
exit(argp_err_exit_status);
1708
1673
/* Work around Debian bug #633582:
1709
1674
<http://bugs.debian.org/633582> */
1711
1677
/* Re-raise priviliges */
1713
1679
ret = seteuid(0);
1715
1681
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");
1684
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1685
int seckey_fd = open(seckey, O_RDONLY);
1686
if(seckey_fd == -1){
1687
perror_plus("open");
1689
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1691
perror_plus("fstat");
1693
if(S_ISREG(st.st_mode)
1694
and st.st_uid == 0 and st.st_gid == 0){
1695
ret = fchown(seckey_fd, uid, gid);
1697
perror_plus("fchown");
1701
TEMP_FAILURE_RETRY(close(seckey_fd));
1705
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1706
int pubkey_fd = open(pubkey, O_RDONLY);
1707
if(pubkey_fd == -1){
1708
perror_plus("open");
1710
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1712
perror_plus("fstat");
1714
if(S_ISREG(st.st_mode)
1715
and st.st_uid == 0 and st.st_gid == 0){
1716
ret = fchown(pubkey_fd, uid, gid);
1718
perror_plus("fchown");
1722
TEMP_FAILURE_RETRY(close(pubkey_fd));
1726
/* Lower privileges */
1730
perror_plus("seteuid");
1770
1734
/* Run network hooks */
1771
if(not run_network_hooks("start", interface, delay)){
1736
/* Re-raise priviliges */
1740
perror_plus("seteuid");
1742
if(not run_network_hooks("start", interface, delay)){
1745
/* Lower privileges */
1749
perror_plus("seteuid");