1084
1086
errno = old_errno;
1088
* This function determines if a directory entry in /sys/class/net
1089
* corresponds to an acceptable network device.
1090
* (This function is passed to scandir(3) as a filter function.)
1092
int good_interface(const struct dirent *if_entry){
1094
char *flagname = NULL;
1095
if(if_entry->d_name[0] == '.'){
1098
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1101
perror_plus("asprintf");
1104
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1106
perror_plus("open");
1111
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1112
/* read line from flags_fd */
1113
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1114
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
flagstring[(size_t)to_read] = '\0';
1116
if(flagstring == NULL){
1117
perror_plus("malloc");
1122
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1125
perror_plus("read");
1139
tmpmax = strtoimax(flagstring, &tmp, 0);
1140
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1141
and not (isspace(*tmp)))
1142
or tmpmax != (ifreq_flags)tmpmax){
1089
bool get_flags(const char *ifname, struct ifreq *ifr){
1092
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1094
perror_plus("socket");
1097
strcpy(ifr->ifr_name, ifname);
1098
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1144
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1145
flagstring, if_entry->d_name);
1101
perror_plus("ioctl SIOCGIFFLAGS");
1151
ifreq_flags flags = (ifreq_flags)tmpmax;
1108
bool good_flags(const char *ifname, const struct ifreq *ifr){
1152
1110
/* Reject the loopback device */
1153
if(flags & IFF_LOOPBACK){
1111
if(ifr->ifr_flags & IFF_LOOPBACK){
1155
1113
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1160
1118
/* Accept point-to-point devices only if connect_to is specified */
1161
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1119
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1163
1121
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1168
1126
/* Otherwise, reject non-broadcast-capable devices */
1169
if(not (flags & IFF_BROADCAST)){
1127
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1171
1129
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1176
1134
/* Reject non-ARP interfaces (including dummy interfaces) */
1177
if(flags & IFF_NOARP){
1135
if(ifr->ifr_flags & IFF_NOARP){
1179
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1137
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1184
1142
/* Accept this device */
1186
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1144
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1150
* This function determines if a directory entry in /sys/class/net
1151
* corresponds to an acceptable network device.
1152
* (This function is passed to scandir(3) as a filter function.)
1154
int good_interface(const struct dirent *if_entry){
1155
if(if_entry->d_name[0] == '.'){
1160
if(not get_flags(if_entry->d_name, &ifr)){
1162
fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1168
if(not good_flags(if_entry->d_name, &ifr)){
1175
* This function determines if a directory entry in /sys/class/net
1176
* corresponds to an acceptable network device which is up.
1177
* (This function is passed to scandir(3) as a filter function.)
1179
int up_interface(const struct dirent *if_entry){
1180
if(if_entry->d_name[0] == '.'){
1185
if(not get_flags(if_entry->d_name, &ifr)){
1187
fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1193
/* Reject down interfaces */
1194
if(not (ifr.ifr_flags & IFF_UP)){
1196
fprintf(stderr, "Rejecting down interface \"%s\"\n",
1202
/* Reject non-running interfaces */
1203
if(not (ifr.ifr_flags & IFF_RUNNING)){
1205
fprintf(stderr, "Rejecting non-running interface \"%s\"\n",
1211
if(not good_flags(if_entry->d_name, &ifr)){
1228
/* Is this directory entry a runnable program? */
1229
int runnable_hook(const struct dirent *direntry){
1233
if((direntry->d_name)[0] == '\0'){
1238
/* Save pointer to last character */
1239
char *end = strchr(direntry->d_name, '\0')-1;
1246
if(((direntry->d_name)[0] == '#')
1248
/* Temporary #name# */
1252
/* XXX more rules here */
1254
ret = stat(direntry->d_name, &st);
1257
perror_plus("Could not stat plugin");
1261
if(not (S_ISREG(st.st_mode))){
1262
/* Not a regular file */
1265
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1266
/* Not executable */
1203
1272
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1205
1274
struct timespec now;
1206
1275
struct timespec waited_time;
1207
1276
intmax_t block_time;
1210
1279
if(mc.current_server == NULL){
1530
/* Work around Debian bug #633582:
1531
<http://bugs.debian.org/633582> */
1534
/* Re-raise priviliges */
1538
perror_plus("seteuid");
1541
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1542
int seckey_fd = open(seckey, O_RDONLY);
1543
if(seckey_fd == -1){
1544
perror_plus("open");
1546
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1548
perror_plus("fstat");
1550
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1551
ret = fchown(seckey_fd, uid, gid);
1553
perror_plus("fchown");
1557
TEMP_FAILURE_RETRY(close(seckey_fd));
1561
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1562
int pubkey_fd = open(pubkey, O_RDONLY);
1563
if(pubkey_fd == -1){
1564
perror_plus("open");
1566
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1568
perror_plus("fstat");
1570
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1571
ret = fchown(pubkey_fd, uid, gid);
1573
perror_plus("fchown");
1577
TEMP_FAILURE_RETRY(close(pubkey_fd));
1581
/* Lower privileges */
1585
perror_plus("seteuid");
1589
/* Find network hooks and run them */
1591
struct dirent **direntries;
1592
struct dirent *direntry;
1593
int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1595
int devnull = open("/dev/null", O_RDONLY);
1596
for(int i = 0; i < numhooks; i++){
1597
direntry = direntries[0];
1598
char *fullname = NULL;
1599
ret = asprintf(&fullname, "%s/%s", tempdir,
1602
perror_plus("asprintf");
1605
pid_t hook_pid = fork();
1608
dup2(devnull, STDIN_FILENO);
1610
dup2(STDERR_FILENO, STDOUT_FILENO);
1611
ret = setenv("DEVICE", interface, 1);
1613
perror_plus("setenv");
1616
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1618
perror_plus("setenv");
1621
ret = setenv("MODE", "start", 1);
1623
perror_plus("setenv");
1627
ret = asprintf(&delaystring, "%f", delay);
1629
perror_plus("asprintf");
1632
ret = setenv("DELAY", delaystring, 1);
1635
perror_plus("setenv");
1639
ret = execl(fullname, direntry->d_name, "start", NULL);
1640
perror_plus("execl");
1460
1651
avahi_set_log_function(empty_log);
1463
1654
if(interface[0] == '\0'){
1464
1655
struct dirent **direntries;
1465
ret = scandir(sys_class_net, &direntries, good_interface,
1656
/* First look for interfaces that are up */
1657
ret = scandir(sys_class_net, &direntries, up_interface,
1660
/* No up interfaces, look for any good interfaces */
1662
ret = scandir(sys_class_net, &direntries, good_interface,
1468
/* Pick the first good interface */
1666
/* Pick the first interface returned */
1469
1667
interface = strdup(direntries[0]->d_name);
1471
1669
fprintf(stderr, "Using interface \"%s\"\n", interface);