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");
1393
1438
bool run_network_hooks(const char *mode, const char *interface,
1394
1439
const float delay){
1395
1440
struct dirent **direntries;
1564
int bring_up_interface(const char * const interface, const float delay){
1567
struct ifreq network;
1568
AvahiIfIndex if_index = (AvahiIfIndex)if_nametoindex(interface);
1570
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1571
return EX_UNAVAILABLE;
1578
/* Re-raise priviliges */
1582
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1583
messages about the network interface to mess up the prompt */
1584
ret = klogctl(8, NULL, 5);
1585
bool restore_loglevel = true;
1587
restore_loglevel = false;
1588
perror_plus("klogctl");
1590
#endif /* __linux__ */
1592
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1594
perror_plus("socket");
1596
if(restore_loglevel){
1597
ret = klogctl(7, NULL, 0);
1599
perror_plus("klogctl");
1602
#endif /* __linux__ */
1603
/* Lower privileges */
1608
strcpy(network.ifr_name, interface);
1609
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1611
perror_plus("ioctl SIOCGIFFLAGS");
1613
if(restore_loglevel){
1614
ret = klogctl(7, NULL, 0);
1616
perror_plus("klogctl");
1619
#endif /* __linux__ */
1620
/* Lower privileges */
1624
if((network.ifr_flags & IFF_UP) == 0){
1625
network.ifr_flags |= IFF_UP;
1626
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1628
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1630
if(restore_loglevel){
1631
ret = klogctl(7, NULL, 0);
1633
perror_plus("klogctl");
1636
#endif /* __linux__ */
1637
/* Lower privileges */
1642
/* Sleep checking until interface is running.
1643
Check every 0.25s, up to total time of delay */
1644
for(int i=0; i < delay * 4; i++){
1645
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1647
perror_plus("ioctl SIOCGIFFLAGS");
1648
} else if(network.ifr_flags & IFF_RUNNING){
1651
struct timespec sleeptime = { .tv_nsec = 250000000 };
1652
ret = nanosleep(&sleeptime, NULL);
1653
if(ret == -1 and errno != EINTR){
1654
perror_plus("nanosleep");
1657
/* Close the socket */
1658
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1660
perror_plus("close");
1663
if(restore_loglevel){
1664
/* Restores kernel loglevel to default */
1665
ret = klogctl(7, NULL, 0);
1667
perror_plus("klogctl");
1670
#endif /* __linux__ */
1671
/* Lower privileges */
1529
1676
int main(int argc, char *argv[]){
1530
1677
AvahiSServiceBrowser *sb = NULL;
1901
2042
/* If the interface is down, bring it up */
1902
if(strcmp(interface, "none") != 0){
1903
if_index = (AvahiIfIndex) if_nametoindex(interface);
1905
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1906
exitcode = EX_UNAVAILABLE;
1914
/* Re-raise priviliges */
1918
perror_plus("seteuid");
1922
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1923
messages about the network interface to mess up the prompt */
1924
ret = klogctl(8, NULL, 5);
1925
bool restore_loglevel = true;
1927
restore_loglevel = false;
1928
perror_plus("klogctl");
1930
#endif /* __linux__ */
1932
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1934
perror_plus("socket");
1935
exitcode = EX_OSERR;
1937
if(restore_loglevel){
1938
ret = klogctl(7, NULL, 0);
1940
perror_plus("klogctl");
1943
#endif /* __linux__ */
1944
/* Lower privileges */
1948
perror_plus("seteuid");
1952
strcpy(network.ifr_name, interface);
1953
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1955
perror_plus("ioctl SIOCGIFFLAGS");
1957
if(restore_loglevel){
1958
ret = klogctl(7, NULL, 0);
1960
perror_plus("klogctl");
1963
#endif /* __linux__ */
1964
exitcode = EX_OSERR;
1965
/* Lower privileges */
1969
perror_plus("seteuid");
1973
if((network.ifr_flags & IFF_UP) == 0){
1974
network.ifr_flags |= IFF_UP;
1975
take_down_interface = true;
1976
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1978
take_down_interface = false;
1979
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1980
exitcode = EX_OSERR;
1982
if(restore_loglevel){
1983
ret = klogctl(7, NULL, 0);
1985
perror_plus("klogctl");
1988
#endif /* __linux__ */
1989
/* Lower privileges */
1993
perror_plus("seteuid");
1998
/* Sleep checking until interface is running.
1999
Check every 0.25s, up to total time of delay */
2000
for(int i=0; i < delay * 4; i++){
2001
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2003
perror_plus("ioctl SIOCGIFFLAGS");
2004
} else if(network.ifr_flags & IFF_RUNNING){
2007
struct timespec sleeptime = { .tv_nsec = 250000000 };
2008
ret = nanosleep(&sleeptime, NULL);
2009
if(ret == -1 and errno != EINTR){
2010
perror_plus("nanosleep");
2013
if(not take_down_interface){
2014
/* We won't need the socket anymore */
2015
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2017
perror_plus("close");
2021
if(restore_loglevel){
2022
/* Restores kernel loglevel to default */
2023
ret = klogctl(7, NULL, 0);
2025
perror_plus("klogctl");
2028
#endif /* __linux__ */
2029
/* Lower privileges */
2031
/* Lower privileges */
2034
perror_plus("seteuid");
2043
if((interface[0] != '\0') and (strcmp(interface, "none") != 0)){
2044
ret = bring_up_interface(interface, delay);
2047
perror_plus("Failed to bring up interface");