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
42
42
#include <stdio.h> /* fprintf(), stderr, fwrite(),
43
43
stdout, ferror(), remove() */
44
#include <stdint.h> /* uint16_t, uint32_t, intptr_t */
44
#include <stdint.h> /* uint16_t, uint32_t */
45
45
#include <stddef.h> /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h> /* free(), EXIT_SUCCESS, srand(),
47
47
strtof(), abort() */
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
173
170
/* Function to use when printing errors */
174
171
void perror_plus(const char *print_text){
176
172
fprintf(stderr, "Mandos plugin %s: ",
177
173
program_invocation_short_name);
179
174
perror(print_text);
182
__attribute__((format (gnu_printf, 2, 3)))
183
177
int fprintf_plus(FILE *stream, const char *format, ...){
185
179
va_start (ap, format);
209
203
/* Add server to set of servers to retry periodically */
210
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,
213
207
server *new_server = malloc(sizeof(server));
214
208
if(new_server == NULL){
215
209
perror_plus("malloc");
218
212
*new_server = (server){ .ip = strdup(ip),
826
/* This casting via intptr_t is to eliminate warning about casting
827
an int to a pointer type. This is exactly how the GnuTLS Guile
828
function "set-session-transport-fd!" does it. */
829
gnutls_transport_set_ptr(session,
830
(gnutls_transport_ptr_t)(intptr_t)tcp_sd);
820
/* Spurious warning from -Wint-to-pointer-cast */
821
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
1039
1030
avahi_simple_poll_quit(mc.simple_poll);
1041
if(not add_server(ip, port, interface,
1042
avahi_proto_to_af(proto))){
1043
fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1032
ret = add_server(ip, port, interface,
1033
avahi_proto_to_af(proto));
1395
/* Set effective uid to 0, return errno */
1396
int raise_privileges(void){
1397
int old_errno = errno;
1400
if(seteuid(0) == -1){
1401
perror_plus("seteuid");
1408
/* Set effective and real user ID to 0. Return errno. */
1409
int raise_privileges_permanently(void){
1410
int old_errno = errno;
1411
int ret_errno = raise_privileges();
1417
if(setuid(0) == -1){
1418
perror_plus("seteuid");
1425
/* Set effective user ID to unprivileged saved user ID */
1426
int lower_privileges(void){
1427
int old_errno = errno;
1430
if(seteuid(uid) == -1){
1431
perror_plus("seteuid");
1438
1383
bool run_network_hooks(const char *mode, const char *interface,
1439
1384
const float delay){
1440
1385
struct dirent **direntries;
1461
1406
pid_t hook_pid = fork();
1462
1407
if(hook_pid == 0){
1464
/* Raise privileges */
1465
raise_privileges_permanently();
1470
perror_plus("setgid");
1472
/* Reset supplementary groups */
1474
ret = setgroups(0, NULL);
1476
perror_plus("setgroups");
1478
1409
dup2(devnull, STDIN_FILENO);
1479
1410
close(devnull);
1480
1411
dup2(STDERR_FILENO, STDOUT_FILENO);
1481
1412
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1483
1414
perror_plus("setenv");
1486
1417
ret = setenv("DEVICE", interface, 1);
1488
1419
perror_plus("setenv");
1491
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1422
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1493
1424
perror_plus("setenv");
1496
1427
ret = setenv("MODE", mode, 1);
1498
1429
perror_plus("setenv");
1501
1432
char *delaystring;
1502
1433
ret = asprintf(&delaystring, "%f", delay);
1504
1435
perror_plus("asprintf");
1507
1438
ret = setenv("DELAY", delaystring, 1);
1509
1440
free(delaystring);
1510
1441
perror_plus("setenv");
1513
1444
free(delaystring);
1514
if(connect_to != NULL){
1515
ret = setenv("CONNECT", connect_to, 1);
1517
perror_plus("setenv");
1521
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1522
perror_plus("execl");
1523
_exit(EXIT_FAILURE);
1445
ret = execl(fullname, direntry->d_name, mode, NULL);
1446
perror_plus("execl");
1527
1449
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1564
int bring_up_interface(const char * const interface, const float delay){
1566
int old_errno = errno;
1569
struct ifreq network;
1570
AvahiIfIndex if_index = (AvahiIfIndex)if_nametoindex(interface);
1572
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1582
/* Re-raise priviliges */
1586
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1587
messages about the network interface to mess up the prompt */
1588
ret = klogctl(8, NULL, 5);
1589
bool restore_loglevel = true;
1591
restore_loglevel = false;
1592
perror_plus("klogctl");
1594
#endif /* __linux__ */
1596
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1599
perror_plus("socket");
1601
if(restore_loglevel){
1602
ret = klogctl(7, NULL, 0);
1604
perror_plus("klogctl");
1607
#endif /* __linux__ */
1608
/* Lower privileges */
1613
strcpy(network.ifr_name, interface);
1614
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1617
perror_plus("ioctl SIOCGIFFLAGS");
1619
if(restore_loglevel){
1620
ret = klogctl(7, NULL, 0);
1622
perror_plus("klogctl");
1625
#endif /* __linux__ */
1626
/* Lower privileges */
1631
if((network.ifr_flags & IFF_UP) == 0){
1632
network.ifr_flags |= IFF_UP;
1633
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1636
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1638
if(restore_loglevel){
1639
ret = klogctl(7, NULL, 0);
1641
perror_plus("klogctl");
1644
#endif /* __linux__ */
1645
/* Lower privileges */
1651
/* Sleep checking until interface is running.
1652
Check every 0.25s, up to total time of delay */
1653
for(int i=0; i < delay * 4; i++){
1654
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1656
perror_plus("ioctl SIOCGIFFLAGS");
1657
} else if(network.ifr_flags & IFF_RUNNING){
1660
struct timespec sleeptime = { .tv_nsec = 250000000 };
1661
ret = nanosleep(&sleeptime, NULL);
1662
if(ret == -1 and errno != EINTR){
1663
perror_plus("nanosleep");
1666
/* Close the socket */
1667
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1669
perror_plus("close");
1672
if(restore_loglevel){
1673
/* Restores kernel loglevel to default */
1674
ret = klogctl(7, NULL, 0);
1676
perror_plus("klogctl");
1679
#endif /* __linux__ */
1680
/* Lower privileges */
1686
1486
int main(int argc, char *argv[]){
1687
1487
AvahiSServiceBrowser *sb = NULL;
1844
1646
argp_state_help(state, state->out_stream,
1845
1647
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1846
1648
case 'V': /* --version */
1649
fprintf_plus(state->out_stream,
1650
"Mandos plugin mandos-client: ");
1847
1651
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1848
1652
exit(argp_err_exit_status);
1878
1682
/* Work around Debian bug #633582:
1879
1683
<http://bugs.debian.org/633582> */
1881
1686
/* Re-raise priviliges */
1882
if(raise_privileges() == 0){
1885
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1886
int seckey_fd = open(seckey, O_RDONLY);
1887
if(seckey_fd == -1){
1888
perror_plus("open");
1890
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1892
perror_plus("fstat");
1894
if(S_ISREG(st.st_mode)
1895
and st.st_uid == 0 and st.st_gid == 0){
1896
ret = fchown(seckey_fd, uid, gid);
1898
perror_plus("fchown");
1902
TEMP_FAILURE_RETRY(close(seckey_fd));
1906
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1907
int pubkey_fd = open(pubkey, O_RDONLY);
1908
if(pubkey_fd == -1){
1909
perror_plus("open");
1911
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1913
perror_plus("fstat");
1915
if(S_ISREG(st.st_mode)
1916
and st.st_uid == 0 and st.st_gid == 0){
1917
ret = fchown(pubkey_fd, uid, gid);
1919
perror_plus("fchown");
1923
TEMP_FAILURE_RETRY(close(pubkey_fd));
1690
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)){
1927
1757
/* Lower privileges */
1929
1759
ret = seteuid(uid);
2052
1877
/* If the interface is down, bring it up */
2053
if((interface[0] != '\0') and (strcmp(interface, "none") != 0)){
2054
ret = bring_up_interface(interface, delay);
2057
perror_plus("Failed to bring up interface");
1878
if(strcmp(interface, "none") != 0){
1879
if_index = (AvahiIfIndex) if_nametoindex(interface);
1881
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1882
exitcode = EX_UNAVAILABLE;
1890
/* Re-raise priviliges */
1894
perror_plus("seteuid");
1898
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1899
messages about the network interface to mess up the prompt */
1900
ret = klogctl(8, NULL, 5);
1901
bool restore_loglevel = true;
1903
restore_loglevel = false;
1904
perror_plus("klogctl");
1906
#endif /* __linux__ */
1908
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1910
perror_plus("socket");
1911
exitcode = EX_OSERR;
1913
if(restore_loglevel){
1914
ret = klogctl(7, NULL, 0);
1916
perror_plus("klogctl");
1919
#endif /* __linux__ */
1920
/* Lower privileges */
1924
perror_plus("seteuid");
1928
strcpy(network.ifr_name, interface);
1929
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1931
perror_plus("ioctl SIOCGIFFLAGS");
1933
if(restore_loglevel){
1934
ret = klogctl(7, NULL, 0);
1936
perror_plus("klogctl");
1939
#endif /* __linux__ */
1940
exitcode = EX_OSERR;
1941
/* Lower privileges */
1945
perror_plus("seteuid");
1949
if((network.ifr_flags & IFF_UP) == 0){
1950
network.ifr_flags |= IFF_UP;
1951
take_down_interface = true;
1952
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1954
take_down_interface = false;
1955
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1956
exitcode = EX_OSERR;
1958
if(restore_loglevel){
1959
ret = klogctl(7, NULL, 0);
1961
perror_plus("klogctl");
1964
#endif /* __linux__ */
1965
/* Lower privileges */
1969
perror_plus("seteuid");
1974
/* Sleep checking until interface is running.
1975
Check every 0.25s, up to total time of delay */
1976
for(int i=0; i < delay * 4; i++){
1977
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1979
perror_plus("ioctl SIOCGIFFLAGS");
1980
} else if(network.ifr_flags & IFF_RUNNING){
1983
struct timespec sleeptime = { .tv_nsec = 250000000 };
1984
ret = nanosleep(&sleeptime, NULL);
1985
if(ret == -1 and errno != EINTR){
1986
perror_plus("nanosleep");
1989
if(not take_down_interface){
1990
/* We won't need the socket anymore */
1991
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1993
perror_plus("close");
1997
if(restore_loglevel){
1998
/* Restores kernel loglevel to default */
1999
ret = klogctl(7, NULL, 0);
2001
perror_plus("klogctl");
2004
#endif /* __linux__ */
2005
/* Lower privileges */
2007
/* Lower privileges */
2010
perror_plus("seteuid");