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() */
171
170
/* Function to use when printing errors */
172
171
void perror_plus(const char *print_text){
174
172
fprintf(stderr, "Mandos plugin %s: ",
175
173
program_invocation_short_name);
177
174
perror(print_text);
180
__attribute__((format (gnu_printf, 2, 3)))
181
177
int fprintf_plus(FILE *stream, const char *format, ...){
183
179
va_start (ap, format);
207
203
/* Add server to set of servers to retry periodically */
208
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,
211
207
server *new_server = malloc(sizeof(server));
212
208
if(new_server == NULL){
213
209
perror_plus("malloc");
216
212
*new_server = (server){ .ip = strdup(ip),
1034
1030
avahi_simple_poll_quit(mc.simple_poll);
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"
1032
ret = add_server(ip, port, interface,
1033
avahi_proto_to_af(proto));
1413
1406
pid_t hook_pid = fork();
1414
1407
if(hook_pid == 0){
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
1409
dup2(devnull, STDIN_FILENO);
1441
1410
close(devnull);
1442
1411
dup2(STDERR_FILENO, STDOUT_FILENO);
1443
1412
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1445
1414
perror_plus("setenv");
1448
1417
ret = setenv("DEVICE", interface, 1);
1450
1419
perror_plus("setenv");
1453
1422
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1455
1424
perror_plus("setenv");
1458
1427
ret = setenv("MODE", mode, 1);
1460
1429
perror_plus("setenv");
1463
1432
char *delaystring;
1464
1433
ret = asprintf(&delaystring, "%f", delay);
1466
1435
perror_plus("asprintf");
1469
1438
ret = setenv("DELAY", delaystring, 1);
1471
1440
free(delaystring);
1472
1441
perror_plus("setenv");
1475
1444
free(delaystring);
1476
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1477
perror_plus("execl");
1478
_exit(EXIT_FAILURE);
1445
ret = execl(fullname, direntry->d_name, mode, NULL);
1446
perror_plus("execl");
1482
1449
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1679
1646
argp_state_help(state, state->out_stream,
1680
1647
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1681
1648
case 'V': /* --version */
1649
fprintf_plus(state->out_stream,
1650
"Mandos plugin mandos-client: ");
1682
1651
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1683
1652
exit(argp_err_exit_status);
1713
1682
/* Work around Debian bug #633582:
1714
1683
<http://bugs.debian.org/633582> */
1716
1686
/* Re-raise priviliges */
1718
1688
ret = seteuid(0);
1720
1690
perror_plus("seteuid");
1724
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1725
int seckey_fd = open(seckey, O_RDONLY);
1726
if(seckey_fd == -1){
1727
perror_plus("open");
1729
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1731
perror_plus("fstat");
1733
if(S_ISREG(st.st_mode)
1734
and st.st_uid == 0 and st.st_gid == 0){
1735
ret = fchown(seckey_fd, uid, gid);
1737
perror_plus("fchown");
1741
TEMP_FAILURE_RETRY(close(seckey_fd));
1745
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1746
int pubkey_fd = open(pubkey, O_RDONLY);
1747
if(pubkey_fd == -1){
1748
perror_plus("open");
1750
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1752
perror_plus("fstat");
1754
if(S_ISREG(st.st_mode)
1755
and st.st_uid == 0 and st.st_gid == 0){
1756
ret = fchown(pubkey_fd, uid, gid);
1758
perror_plus("fchown");
1762
TEMP_FAILURE_RETRY(close(pubkey_fd));
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)){
1766
1757
/* Lower privileges */
1768
1759
ret = seteuid(uid);
2234
/* Run network hooks */
2235
run_network_hooks("stop", interface, delay);
2237
2220
/* Re-raise priviliges */
2242
perror_plus("seteuid");
2226
perror_plus("seteuid");
2230
/* Run network hooks */
2231
run_network_hooks("stop", interface, delay);
2245
2233
/* Take down the network interface */
2246
2234
if(take_down_interface and geteuid() == 0){
2247
2235
ret = ioctl(sd, SIOCGIFFLAGS, &network);