9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2008-2012 Teddy Hogeborn
13
* Copyright © 2008-2012 Björn Påhlsson
12
* Copyright © 2008-2011 Teddy Hogeborn
13
* Copyright © 2008-2011 Björn Påhlsson
15
15
* This program is free software: you can redistribute it and/or
16
16
* modify it under the terms of the GNU General Public License as
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
1402
pid_t hook_pid = fork();
1414
1403
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
1405
dup2(devnull, STDIN_FILENO);
1441
1406
close(devnull);
1442
1407
dup2(STDERR_FILENO, STDOUT_FILENO);
1443
1408
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1445
1410
perror_plus("setenv");
1448
1413
ret = setenv("DEVICE", interface, 1);
1450
1415
perror_plus("setenv");
1453
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1418
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1455
1420
perror_plus("setenv");
1458
1423
ret = setenv("MODE", mode, 1);
1460
1425
perror_plus("setenv");
1463
1428
char *delaystring;
1464
1429
ret = asprintf(&delaystring, "%f", delay);
1466
1431
perror_plus("asprintf");
1469
1434
ret = setenv("DELAY", delaystring, 1);
1471
1436
free(delaystring);
1472
1437
perror_plus("setenv");
1475
1440
free(delaystring);
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);
1441
ret = execl(fullname, direntry->d_name, mode, NULL);
1442
perror_plus("execl");
1489
1445
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1609
1564
{ .name = "retry", .key = 132,
1610
1565
.arg = "SECONDS",
1611
.doc = "Retry interval used when denied by the Mandos server",
1566
.doc = "Retry interval used when denied by the mandos server",
1613
1568
{ .name = "network-hook-dir", .key = 133,
1686
1641
argp_state_help(state, state->out_stream,
1687
1642
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1688
1643
case 'V': /* --version */
1644
fprintf_plus(state->out_stream,
1645
"Mandos plugin mandos-client: ");
1689
1646
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1690
1647
exit(argp_err_exit_status);
1720
1677
/* Work around Debian bug #633582:
1721
1678
<http://bugs.debian.org/633582> */
1723
1681
/* Re-raise priviliges */
1725
1683
ret = seteuid(0);
1727
1685
perror_plus("seteuid");
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));
1688
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1689
int seckey_fd = open(seckey, O_RDONLY);
1690
if(seckey_fd == -1){
1691
perror_plus("open");
1693
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1695
perror_plus("fstat");
1697
if(S_ISREG(st.st_mode)
1698
and st.st_uid == 0 and st.st_gid == 0){
1699
ret = fchown(seckey_fd, uid, gid);
1701
perror_plus("fchown");
1705
TEMP_FAILURE_RETRY(close(seckey_fd));
1709
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1710
int pubkey_fd = open(pubkey, O_RDONLY);
1711
if(pubkey_fd == -1){
1712
perror_plus("open");
1714
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1716
perror_plus("fstat");
1718
if(S_ISREG(st.st_mode)
1719
and st.st_uid == 0 and st.st_gid == 0){
1720
ret = fchown(pubkey_fd, uid, gid);
1722
perror_plus("fchown");
1726
TEMP_FAILURE_RETRY(close(pubkey_fd));
1730
/* Lower privileges */
1734
perror_plus("seteuid");
1738
/* Run network hooks */
1741
/* Re-raise priviliges */
1745
perror_plus("seteuid");
1748
if(not run_network_hooks("start", interface, delay)){
1773
1752
/* Lower privileges */
1775
1754
ret = seteuid(uid);
2241
/* Run network hooks */
2242
run_network_hooks("stop", interface, delay);
2244
2215
/* Re-raise priviliges */
2249
perror_plus("seteuid");
2221
perror_plus("seteuid");
2224
/* Run network hooks */
2225
if(not run_network_hooks("stop", interface, delay)){
2252
2229
/* Take down the network interface */