1085
1084
errno = old_errno;
1088
bool get_flags(const char *ifname, struct ifreq *ifr){
1091
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1093
perror_plus("socket");
1096
strcpy(ifr->ifr_name, ifname);
1097
ret = ioctl(s, SIOCGIFFLAGS, ifr);
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){
1100
perror_plus("ioctl SIOCGIFFLAGS");
1144
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1145
flagstring, if_entry->d_name);
1107
bool good_flags(const char *ifname, const struct ifreq *ifr){
1151
ifreq_flags flags = (ifreq_flags)tmpmax;
1109
1152
/* Reject the loopback device */
1110
if(ifr->ifr_flags & IFF_LOOPBACK){
1153
if(flags & IFF_LOOPBACK){
1112
1155
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1117
1160
/* Accept point-to-point devices only if connect_to is specified */
1118
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1161
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1120
1163
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1125
1168
/* Otherwise, reject non-broadcast-capable devices */
1126
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1169
if(not (flags & IFF_BROADCAST)){
1128
1171
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1133
1176
/* Reject non-ARP interfaces (including dummy interfaces) */
1134
if(ifr->ifr_flags & IFF_NOARP){
1177
if(flags & IFF_NOARP){
1136
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1179
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1141
1184
/* Accept this device */
1143
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1149
* This function determines if a directory entry in /sys/class/net
1150
* corresponds to an acceptable network device.
1151
* (This function is passed to scandir(3) as a filter function.)
1153
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)){
1164
if(not good_flags(if_entry->d_name, &ifr)){
1186
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1447
1468
perror_plus("seteuid");
1450
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1451
int seckey_fd = open(seckey, O_RDONLY);
1452
if(seckey_fd == -1){
1453
perror_plus("open");
1471
int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
1472
if(seckey_fd == -1){
1473
perror_plus("open");
1475
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1477
perror_plus("fstat");
1455
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1457
perror_plus("fstat");
1459
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1460
ret = fchown(seckey_fd, uid, gid);
1462
perror_plus("fchown");
1479
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1480
ret = fchown(seckey_fd, uid, gid);
1482
perror_plus("fchown");
1466
TEMP_FAILURE_RETRY(close(seckey_fd));
1486
TEMP_FAILURE_RETRY(close(seckey_fd));
1470
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1471
int pubkey_fd = open(pubkey, O_RDONLY);
1472
if(pubkey_fd == -1){
1473
perror_plus("open");
1489
int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
1490
if(pubkey_fd == -1){
1491
perror_plus("open");
1493
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1495
perror_plus("fstat");
1475
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1477
perror_plus("fstat");
1479
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1480
ret = fchown(pubkey_fd, uid, gid);
1482
perror_plus("fchown");
1497
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1498
ret = fchown(pubkey_fd, uid, gid);
1500
perror_plus("fchown");
1486
TEMP_FAILURE_RETRY(close(pubkey_fd));
1504
TEMP_FAILURE_RETRY(close(pubkey_fd));
1490
1507
/* Lower privileges */
1813
1830
port = (uint16_t)tmpmax;
1814
1831
*address = '\0';
1832
address = connect_to;
1815
1833
/* Colon in address indicates IPv6 */
1817
if(strchr(connect_to, ':') != NULL){
1835
if(strchr(address, ':') != NULL){
1819
/* Accept [] around IPv6 address - see RFC 5952 */
1820
if(connect_to[0] == '[' and address[-1] == ']')
1828
address = connect_to;
1834
1845
while(not quit_now){
1835
1846
ret = start_mandos_communication(address, port, if_index, af);
1836
1847
if(quit_now or ret == 0){
1840
fprintf(stderr, "Retrying in %d seconds\n",
1841
(int)retry_interval);
1843
sleep((int)retry_interval);
1850
sleep((int)retry_interval or 1);
1846
1853
if (not quit_now){
1847
1854
exitcode = EXIT_SUCCESS;
1985
1992
if(tempdir_created){
1986
1993
struct dirent **direntries = NULL;
1987
1994
struct dirent *direntry = NULL;
1988
int numentries = scandir(tempdir, &direntries, notdotentries,
1990
if (numentries > 0){
1991
for(int i = 0; i < numentries; i++){
1995
ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1997
for(int i = 0; i < ret; i++){
1992
1998
direntry = direntries[i];
1993
1999
char *fullname = NULL;
1994
2000
ret = asprintf(&fullname, "%s/%s", tempdir,