1080
1019
signal_received = sig;
1081
1020
int old_errno = errno;
1082
/* set main loop to exit */
1083
1021
if(mc.simple_poll != NULL){
1084
1022
avahi_simple_poll_quit(mc.simple_poll);
1086
1024
errno = old_errno;
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);
1028
* This function determines if a directory entry in /sys/class/net
1029
* corresponds to an acceptable network device.
1030
* (This function is passed to scandir(3) as a filter function.)
1032
int good_interface(const struct dirent *if_entry){
1034
char *flagname = NULL;
1035
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1041
if(if_entry->d_name[0] == '.'){
1044
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1049
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1050
/* read line from flags_fd */
1051
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1052
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1053
flagstring[(size_t)to_read] = '\0';
1054
if(flagstring == NULL){
1060
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1077
tmpmax = strtoimax(flagstring, &tmp, 0);
1078
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1079
and not (isspace(*tmp)))
1080
or tmpmax != (ifreq_flags)tmpmax){
1101
perror_plus("ioctl SIOCGIFFLAGS");
1082
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1083
flagstring, if_entry->d_name);
1108
bool good_flags(const char *ifname, const struct ifreq *ifr){
1089
ifreq_flags flags = (ifreq_flags)tmpmax;
1110
1090
/* Reject the loopback device */
1111
if(ifr->ifr_flags & IFF_LOOPBACK){
1091
if(flags & IFF_LOOPBACK){
1113
1093
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1118
1098
/* Accept point-to-point devices only if connect_to is specified */
1119
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1099
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1121
1101
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1126
1106
/* Otherwise, reject non-broadcast-capable devices */
1127
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1107
if(not (flags & IFF_BROADCAST)){
1129
1109
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1134
/* Reject non-ARP interfaces (including dummy interfaces) */
1135
if(ifr->ifr_flags & IFF_NOARP){
1137
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1142
1114
/* Accept this device */
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)){
1217
int notdotentries(const struct dirent *direntry){
1218
/* Skip "." and ".." */
1219
if(direntry->d_name[0] == '.'
1220
and (direntry->d_name[1] == '\0'
1221
or (direntry->d_name[1] == '.'
1222
and direntry->d_name[2] == '\0'))){
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 */
1272
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1274
struct timespec now;
1275
struct timespec waited_time;
1276
intmax_t block_time;
1279
if(mc.current_server == NULL){
1282
"Wait until first server is found. No timeout!\n");
1284
ret = avahi_simple_poll_iterate(s, -1);
1287
fprintf(stderr, "Check current_server if we should run it,"
1290
/* the current time */
1291
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1293
perror_plus("clock_gettime");
1296
/* Calculating in ms how long time between now and server
1297
who we visted longest time ago. Now - last seen. */
1298
waited_time.tv_sec = (now.tv_sec
1299
- mc.current_server->last_seen.tv_sec);
1300
waited_time.tv_nsec = (now.tv_nsec
1301
- mc.current_server->last_seen.tv_nsec);
1302
/* total time is 10s/10,000ms.
1303
Converting to s from ms by dividing by 1,000,
1304
and ns to ms by dividing by 1,000,000. */
1305
block_time = ((retry_interval
1306
- ((intmax_t)waited_time.tv_sec * 1000))
1307
- ((intmax_t)waited_time.tv_nsec / 1000000));
1310
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1313
if(block_time <= 0){
1314
ret = start_mandos_communication(mc.current_server->ip,
1315
mc.current_server->port,
1316
mc.current_server->if_index,
1317
mc.current_server->af);
1319
avahi_simple_poll_quit(mc.simple_poll);
1322
ret = clock_gettime(CLOCK_MONOTONIC,
1323
&mc.current_server->last_seen);
1325
perror_plus("clock_gettime");
1328
mc.current_server = mc.current_server->next;
1329
block_time = 0; /* Call avahi to find new Mandos
1330
servers, but don't block */
1333
ret = avahi_simple_poll_iterate(s, (int)block_time);
1336
if (ret > 0 or errno != EINTR) {
1337
return (ret != 1) ? ret : 0;
1116
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1343
1122
int main(int argc, char *argv[]){
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");
1651
1295
avahi_set_log_function(empty_log);
1654
1298
if(interface[0] == '\0'){
1655
1299
struct dirent **direntries;
1656
/* First look for interfaces that are up */
1657
ret = scandir(sys_class_net, &direntries, up_interface,
1300
ret = scandir(sys_class_net, &direntries, good_interface,
1660
/* No up interfaces, look for any good interfaces */
1662
ret = scandir(sys_class_net, &direntries, good_interface,
1666
/* Pick the first interface returned */
1303
/* Pick the first good interface */
1667
1304
interface = strdup(direntries[0]->d_name);
1669
1306
fprintf(stderr, "Using interface \"%s\"\n", interface);
1671
1308
if(interface == NULL){
1672
perror_plus("malloc");
1673
1310
free(direntries);
1674
1311
exitcode = EXIT_FAILURE;
2117
1726
ret = seteuid(0);
2119
perror_plus("seteuid");
2121
1730
if(geteuid() == 0){
2122
1731
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2124
perror_plus("ioctl SIOCGIFFLAGS");
1733
perror("ioctl SIOCGIFFLAGS");
2125
1734
} else if(network.ifr_flags & IFF_UP) {
2126
1735
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2127
1736
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2129
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1738
perror("ioctl SIOCSIFFLAGS -IFF_UP");
2132
1741
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2134
perror_plus("close");
2136
1745
/* Lower privileges permanently */
2138
1747
ret = setuid(uid);
2140
perror_plus("setuid");
2145
/* Removes the GPGME temp directory and all files inside */
1754
/* Removes the temp directory used by GPGME */
2146
1755
if(tempdir_created){
2147
struct dirent **direntries = NULL;
2148
struct dirent *direntry = NULL;
2149
int numentries = scandir(tempdir, &direntries, notdotentries,
2151
if (numentries > 0){
2152
for(int i = 0; i < numentries; i++){
2153
direntry = direntries[i];
1757
struct dirent *direntry;
1758
d = opendir(tempdir);
1760
if(errno != ENOENT){
1765
direntry = readdir(d);
1766
if(direntry == NULL){
1769
/* Skip "." and ".." */
1770
if(direntry->d_name[0] == '.'
1771
and (direntry->d_name[1] == '\0'
1772
or (direntry->d_name[1] == '.'
1773
and direntry->d_name[2] == '\0'))){
2154
1776
char *fullname = NULL;
2155
1777
ret = asprintf(&fullname, "%s/%s", tempdir,
2156
1778
direntry->d_name);
2158
perror_plus("asprintf");
2161
1783
ret = remove(fullname);