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() */
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);
824
/* Spurious warning from -Wint-to-pointer-cast */
825
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
1395
/* Set effective uid to 0, return errno */
1396
error_t raise_privileges(void){
1397
error_t old_errno = errno;
1398
error_t ret_errno = 0;
1399
if(seteuid(0) == -1){
1401
perror_plus("seteuid");
1407
/* Set effective and real user ID to 0. Return errno. */
1408
error_t raise_privileges_permanently(void){
1409
error_t old_errno = errno;
1410
error_t ret_errno = raise_privileges();
1415
if(setuid(0) == -1){
1417
perror_plus("seteuid");
1423
/* Set effective user ID to unprivileged saved user ID */
1424
error_t lower_privileges(void){
1425
error_t old_errno = errno;
1426
error_t ret_errno = 0;
1427
if(seteuid(uid) == -1){
1429
perror_plus("seteuid");
1435
1390
bool run_network_hooks(const char *mode, const char *interface,
1436
1391
const float delay){
1437
1392
struct dirent **direntries;
1561
int bring_up_interface(const char *const interface,
1564
int old_errno = errno;
1567
struct ifreq network;
1568
AvahiIfIndex if_index = (AvahiIfIndex)if_nametoindex(interface);
1570
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1580
/* Re-raise priviliges */
1584
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1585
messages about the network interface to mess up the prompt */
1586
ret = klogctl(8, NULL, 5);
1587
bool restore_loglevel = true;
1589
restore_loglevel = false;
1590
perror_plus("klogctl");
1592
#endif /* __linux__ */
1594
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1597
perror_plus("socket");
1599
if(restore_loglevel){
1600
ret = klogctl(7, NULL, 0);
1602
perror_plus("klogctl");
1605
#endif /* __linux__ */
1606
/* Lower privileges */
1611
strcpy(network.ifr_name, interface);
1612
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1615
perror_plus("ioctl SIOCGIFFLAGS");
1617
if(restore_loglevel){
1618
ret = klogctl(7, NULL, 0);
1620
perror_plus("klogctl");
1623
#endif /* __linux__ */
1624
/* Lower privileges */
1629
if((network.ifr_flags & IFF_UP) == 0){
1630
network.ifr_flags |= IFF_UP;
1631
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1634
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1636
if(restore_loglevel){
1637
ret = klogctl(7, NULL, 0);
1639
perror_plus("klogctl");
1642
#endif /* __linux__ */
1643
/* Lower privileges */
1649
/* Sleep checking until interface is running.
1650
Check every 0.25s, up to total time of delay */
1651
for(int i=0; i < delay * 4; i++){
1652
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1654
perror_plus("ioctl SIOCGIFFLAGS");
1655
} else if(network.ifr_flags & IFF_RUNNING){
1658
struct timespec sleeptime = { .tv_nsec = 250000000 };
1659
ret = nanosleep(&sleeptime, NULL);
1660
if(ret == -1 and errno != EINTR){
1661
perror_plus("nanosleep");
1664
/* Close the socket */
1665
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1667
perror_plus("close");
1670
if(restore_loglevel){
1671
/* Restores kernel loglevel to default */
1672
ret = klogctl(7, NULL, 0);
1674
perror_plus("klogctl");
1677
#endif /* __linux__ */
1678
/* Lower privileges */
1684
1526
int main(int argc, char *argv[]){
1685
1527
AvahiSServiceBrowser *sb = NULL;
2050
1898
/* If the interface is down, bring it up */
2051
if((interface[0] != '\0') and (strcmp(interface, "none") != 0)){
2052
ret = bring_up_interface(interface, delay);
2055
perror_plus("Failed to bring up interface");
1899
if(strcmp(interface, "none") != 0){
1900
if_index = (AvahiIfIndex) if_nametoindex(interface);
1902
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1903
exitcode = EX_UNAVAILABLE;
1911
/* Re-raise priviliges */
1915
perror_plus("seteuid");
1919
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1920
messages about the network interface to mess up the prompt */
1921
ret = klogctl(8, NULL, 5);
1922
bool restore_loglevel = true;
1924
restore_loglevel = false;
1925
perror_plus("klogctl");
1927
#endif /* __linux__ */
1929
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1931
perror_plus("socket");
1932
exitcode = EX_OSERR;
1934
if(restore_loglevel){
1935
ret = klogctl(7, NULL, 0);
1937
perror_plus("klogctl");
1940
#endif /* __linux__ */
1941
/* Lower privileges */
1945
perror_plus("seteuid");
1949
strcpy(network.ifr_name, interface);
1950
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1952
perror_plus("ioctl SIOCGIFFLAGS");
1954
if(restore_loglevel){
1955
ret = klogctl(7, NULL, 0);
1957
perror_plus("klogctl");
1960
#endif /* __linux__ */
1961
exitcode = EX_OSERR;
1962
/* Lower privileges */
1966
perror_plus("seteuid");
1970
if((network.ifr_flags & IFF_UP) == 0){
1971
network.ifr_flags |= IFF_UP;
1972
take_down_interface = true;
1973
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1975
take_down_interface = false;
1976
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1977
exitcode = EX_OSERR;
1979
if(restore_loglevel){
1980
ret = klogctl(7, NULL, 0);
1982
perror_plus("klogctl");
1985
#endif /* __linux__ */
1986
/* Lower privileges */
1990
perror_plus("seteuid");
1995
/* Sleep checking until interface is running.
1996
Check every 0.25s, up to total time of delay */
1997
for(int i=0; i < delay * 4; i++){
1998
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2000
perror_plus("ioctl SIOCGIFFLAGS");
2001
} else if(network.ifr_flags & IFF_RUNNING){
2004
struct timespec sleeptime = { .tv_nsec = 250000000 };
2005
ret = nanosleep(&sleeptime, NULL);
2006
if(ret == -1 and errno != EINTR){
2007
perror_plus("nanosleep");
2010
if(not take_down_interface){
2011
/* We won't need the socket anymore */
2012
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2014
perror_plus("close");
2018
if(restore_loglevel){
2019
/* Restores kernel loglevel to default */
2020
ret = klogctl(7, NULL, 0);
2022
perror_plus("klogctl");
2025
#endif /* __linux__ */
2026
/* Lower privileges */
2028
/* Lower privileges */
2031
perror_plus("seteuid");