650
650
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
651
651
__attribute__((unused)) const char *txt){}
653
/* Set effective uid to 0, return errno */
654
__attribute__((warn_unused_result))
655
error_t raise_privileges(void){
656
error_t old_errno = errno;
657
error_t ret_errno = 0;
658
if(seteuid(0) == -1){
665
/* Set effective and real user ID to 0. Return errno. */
666
__attribute__((warn_unused_result))
667
error_t raise_privileges_permanently(void){
668
error_t old_errno = errno;
669
error_t ret_errno = raise_privileges();
681
/* Set effective user ID to unprivileged saved user ID */
682
__attribute__((warn_unused_result))
683
error_t lower_privileges(void){
684
error_t old_errno = errno;
685
error_t ret_errno = 0;
686
if(seteuid(uid) == -1){
693
/* Lower privileges permanently */
694
__attribute__((warn_unused_result))
695
error_t lower_privileges_permanently(void){
696
error_t old_errno = errno;
697
error_t ret_errno = 0;
698
if(setuid(uid) == -1){
705
/* Helper function to add_local_route() and remove_local_route() */
706
__attribute__((nonnull, warn_unused_result))
707
static bool add_remove_local_route(const bool add,
709
AvahiIfIndex if_index){
711
char helper[] = "mandos-client-iprouteadddel";
712
char add_arg[] = "add";
713
char remove_arg[] = "remove";
714
char *pluginhelperdir = getenv("MANDOSPLUGINHELPERDIR");
715
if(pluginhelperdir == NULL){
717
fprintf_plus(stderr, "MANDOSPLUGINHELPERDIR environment"
718
" variable not set; cannot run helper\n");
723
char interface[IF_NAMESIZE];
724
if(if_indextoname((unsigned int)if_index, interface) == NULL){
725
perror_plus("if_indextoname");
729
int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
731
perror_plus("open(\"/dev/null\", O_RDONLY)");
737
/* Raise privileges */
738
errno = raise_privileges_permanently();
740
perror_plus("Failed to raise privileges");
741
/* _exit(EX_NOPERM); */
747
perror_plus("setgid");
750
/* Reset supplementary groups */
752
ret = setgroups(0, NULL);
754
perror_plus("setgroups");
758
ret = dup2(devnull, STDIN_FILENO);
760
perror_plus("dup2(devnull, STDIN_FILENO)");
763
ret = close(devnull);
765
perror_plus("close");
768
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
770
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
773
int helperdir_fd = (int)TEMP_FAILURE_RETRY(open(pluginhelperdir,
778
int helper_fd = (int)TEMP_FAILURE_RETRY(openat(helperdir_fd,
780
TEMP_FAILURE_RETRY(close(helperdir_fd));
782
#pragma GCC diagnostic push
783
#pragma GCC diagnostic ignored "-Wcast-qual"
785
if(fexecve(helper_fd, (char *const [])
786
{ helper, add ? add_arg : remove_arg, (char *)address,
787
interface, NULL }, environ) == -1){
789
#pragma GCC diagnostic pop
791
perror_plus("fexecve");
803
pret = waitpid(pid, &status, 0);
804
if(pret == -1 and errno == EINTR and quit_now){
805
int errno_raising = 0;
806
if((errno = raise_privileges()) != 0){
807
errno_raising = errno;
808
perror_plus("Failed to raise privileges in order to"
809
" kill helper program");
811
if(kill(pid, SIGTERM) == -1){
814
if((errno_raising == 0) and (errno = lower_privileges()) != 0){
815
perror_plus("Failed to lower privileges after killing"
820
} while(pret == -1 and errno == EINTR);
822
perror_plus("waitpid");
825
if(WIFEXITED(status)){
826
if(WEXITSTATUS(status) != 0){
827
fprintf_plus(stderr, "Error: iprouteadddel exited"
828
" with status %d\n", WEXITSTATUS(status));
833
if(WIFSIGNALED(status)){
834
fprintf_plus(stderr, "Error: iprouteadddel died by"
835
" signal %d\n", WTERMSIG(status));
838
fprintf_plus(stderr, "Error: iprouteadddel crashed\n");
842
__attribute__((nonnull, warn_unused_result))
843
static bool add_local_route(const char *address,
844
AvahiIfIndex if_index){
845
return add_remove_local_route(true, address, if_index);
848
__attribute__((nonnull, warn_unused_result))
849
static bool remove_local_route(const char *address,
850
AvahiIfIndex if_index){
851
return add_remove_local_route(false, address, if_index);
653
854
/* Called when a Mandos server is found */
654
855
__attribute__((nonnull, warn_unused_result))
655
856
static int start_mandos_communication(const char *ip, in_port_t port,
824
1026
goto mandos_end;
828
ret = connect(tcp_sd, (struct sockaddr *)&to,
829
sizeof(struct sockaddr_in6));
831
ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
832
sizeof(struct sockaddr_in));
835
if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
837
perror_plus("connect");
1031
ret = connect(tcp_sd, (struct sockaddr *)&to,
1032
sizeof(struct sockaddr_in6));
1034
ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
1035
sizeof(struct sockaddr_in));
1038
if(errno == ENETUNREACH
1039
and if_index != AVAHI_IF_UNSPEC
1040
and connect_to == NULL
1041
and not route_added and
1042
((af == AF_INET6 and not
1043
IN6_IS_ADDR_LINKLOCAL(&(((struct sockaddr_in6 *)
1045
or (af == AF_INET and
1046
/* Not a a IPv4LL address */
1047
(ntohl(((struct sockaddr_in *)&to)->sin_addr.s_addr)
1048
& 0xFFFF0000L) != 0xA9FE0000L))){
1049
/* Work around Avahi bug - Avahi does not announce link-local
1050
addresses if it has a global address, so local hosts with
1051
*only* a link-local address (e.g. Mandos clients) cannot
1052
connect to a Mandos server announced by Avahi on a server
1053
host with a global address. Work around this by retrying
1054
with an explicit route added with the server's address.
1056
Avahi bug reference:
1057
http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1058
https://bugs.debian.org/587961
1061
route_added = add_local_route(ip, if_index);
1067
if(errno != ECONNREFUSED or debug){
1069
perror_plus("connect");
848
1082
const char *out = mandos_protocol_version;
1465
/* Set effective uid to 0, return errno */
1466
__attribute__((warn_unused_result))
1467
error_t raise_privileges(void){
1468
error_t old_errno = errno;
1469
error_t ret_errno = 0;
1470
if(seteuid(0) == -1){
1477
/* Set effective and real user ID to 0. Return errno. */
1478
__attribute__((warn_unused_result))
1479
error_t raise_privileges_permanently(void){
1480
error_t old_errno = errno;
1481
error_t ret_errno = raise_privileges();
1486
if(setuid(0) == -1){
1493
/* Set effective user ID to unprivileged saved user ID */
1494
__attribute__((warn_unused_result))
1495
error_t lower_privileges(void){
1496
error_t old_errno = errno;
1497
error_t ret_errno = 0;
1498
if(seteuid(uid) == -1){
1505
/* Lower privileges permanently */
1506
__attribute__((warn_unused_result))
1507
error_t lower_privileges_permanently(void){
1508
error_t old_errno = errno;
1509
error_t ret_errno = 0;
1510
if(setuid(uid) == -1){
1517
1705
__attribute__((nonnull))
1518
1706
void run_network_hooks(const char *mode, const char *interface,
1519
1707
const float delay){
1520
1708
struct dirent **direntries = NULL;
1521
1709
if(hookdir_fd == -1){
1522
hookdir_fd = open(hookdir, O_RDONLY);
1710
hookdir_fd = open(hookdir, O_RDONLY | O_DIRECTORY | O_PATH
1523
1712
if(hookdir_fd == -1){
1524
1713
if(errno == ENOENT){