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() */
92
93
#include <sys/klog.h> /* klogctl() */
203
204
/* Add server to set of servers to retry periodically */
204
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
205
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
207
208
server *new_server = malloc(sizeof(server));
208
209
if(new_server == NULL){
209
210
perror_plus("malloc");
212
213
*new_server = (server){ .ip = strdup(ip),
1030
1031
avahi_simple_poll_quit(mc.simple_poll);
1032
ret = add_server(ip, port, interface,
1033
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"
1406
1410
pid_t hook_pid = fork();
1407
1411
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");
1409
1437
dup2(devnull, STDIN_FILENO);
1410
1438
close(devnull);
1411
1439
dup2(STDERR_FILENO, STDOUT_FILENO);
1412
1440
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1414
1442
perror_plus("setenv");
1417
1445
ret = setenv("DEVICE", interface, 1);
1419
1447
perror_plus("setenv");
1422
1450
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1424
1452
perror_plus("setenv");
1427
1455
ret = setenv("MODE", mode, 1);
1429
1457
perror_plus("setenv");
1432
1460
char *delaystring;
1433
1461
ret = asprintf(&delaystring, "%f", delay);
1435
1463
perror_plus("asprintf");
1438
1466
ret = setenv("DELAY", delaystring, 1);
1440
1468
free(delaystring);
1441
1469
perror_plus("setenv");
1444
1472
free(delaystring);
1445
1473
ret = execl(fullname, direntry->d_name, mode, NULL);
1646
1674
argp_state_help(state, state->out_stream,
1647
1675
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1648
1676
case 'V': /* --version */
1649
fprintf_plus(state->out_stream,
1650
"Mandos plugin mandos-client: ");
1651
1677
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1652
1678
exit(argp_err_exit_status);
1682
1708
/* Work around Debian bug #633582:
1683
1709
<http://bugs.debian.org/633582> */
1686
1711
/* Re-raise priviliges */
1688
1713
ret = seteuid(0);
1690
1715
perror_plus("seteuid");
1693
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1694
int seckey_fd = open(seckey, O_RDONLY);
1695
if(seckey_fd == -1){
1696
perror_plus("open");
1698
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1700
perror_plus("fstat");
1702
if(S_ISREG(st.st_mode)
1703
and st.st_uid == 0 and st.st_gid == 0){
1704
ret = fchown(seckey_fd, uid, gid);
1706
perror_plus("fchown");
1710
TEMP_FAILURE_RETRY(close(seckey_fd));
1714
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1715
int pubkey_fd = open(pubkey, O_RDONLY);
1716
if(pubkey_fd == -1){
1717
perror_plus("open");
1719
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1721
perror_plus("fstat");
1723
if(S_ISREG(st.st_mode)
1724
and st.st_uid == 0 and st.st_gid == 0){
1725
ret = fchown(pubkey_fd, uid, gid);
1727
perror_plus("fchown");
1731
TEMP_FAILURE_RETRY(close(pubkey_fd));
1735
/* Lower privileges */
1739
perror_plus("seteuid");
1743
/* Run network hooks */
1746
/* Re-raise priviliges */
1750
perror_plus("seteuid");
1753
if(not run_network_hooks("start", interface, delay)){
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));
1757
1761
/* Lower privileges */
1759
1763
ret = seteuid(uid);
2229
/* Run network hooks */
2230
run_network_hooks("stop", interface, delay);
2220
2232
/* Re-raise priviliges */
2226
perror_plus("seteuid");
2237
perror_plus("seteuid");
2230
/* Run network hooks */
2231
run_network_hooks("stop", interface, delay);
2233
2240
/* Take down the network interface */
2234
2241
if(take_down_interface and geteuid() == 0){
2235
2242
ret = ioctl(sd, SIOCGIFFLAGS, &network);