/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

* plugins.d/mandos-client.c (good_interface): Add error message.
  (up_interface): Use get_flags() and good_flags().  Also check the
                  IFF_RUNNING flags.
  (runnable_hook): Bug fix: Add NULL to execl() call.
  (main): Use network interfaces which are up and running in
          preference to any other interfaces.

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
                                   sockaddr_in6, PF_INET6,
54
54
                                   SOCK_STREAM, uid_t, gid_t, open(),
55
55
                                   opendir(), DIR */
56
 
#include <sys/stat.h>           /* open() */
 
56
#include <sys/stat.h>           /* open(), S_ISREG */
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect() */
59
59
#include <fcntl.h>              /* open() */
123
123
#define PATHDIR "/conf/conf.d/mandos"
124
124
#define SECKEY "seckey.txt"
125
125
#define PUBKEY "pubkey.txt"
 
126
#define HOOKDIR "/lib/mandos/network-hooks.d"
126
127
 
127
128
bool debug = false;
128
129
static const char mandos_protocol_version[] = "1";
1085
1086
  errno = old_errno;
1086
1087
}
1087
1088
 
1088
 
/* 
1089
 
 * This function determines if a directory entry in /sys/class/net
1090
 
 * corresponds to an acceptable network device.
1091
 
 * (This function is passed to scandir(3) as a filter function.)
1092
 
 */
1093
 
int good_interface(const struct dirent *if_entry){
1094
 
  ssize_t ssret;
1095
 
  char *flagname = NULL;
1096
 
  if(if_entry->d_name[0] == '.'){
1097
 
    return 0;
1098
 
  }
1099
 
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1100
 
                     if_entry->d_name);
1101
 
  if(ret < 0){
1102
 
    perror_plus("asprintf");
1103
 
    return 0;
1104
 
  }
1105
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1106
 
  if(flags_fd == -1){
1107
 
    perror_plus("open");
1108
 
    free(flagname);
1109
 
    return 0;
1110
 
  }
1111
 
  free(flagname);
1112
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1113
 
  /* read line from flags_fd */
1114
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1115
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1116
 
  flagstring[(size_t)to_read] = '\0';
1117
 
  if(flagstring == NULL){
1118
 
    perror_plus("malloc");
1119
 
    close(flags_fd);
1120
 
    return 0;
1121
 
  }
1122
 
  while(to_read > 0){
1123
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1124
 
                                             (size_t)to_read));
1125
 
    if(ssret == -1){
1126
 
      perror_plus("read");
1127
 
      free(flagstring);
1128
 
      close(flags_fd);
1129
 
      return 0;
1130
 
    }
1131
 
    to_read -= ssret;
1132
 
    if(ssret == 0){
1133
 
      break;
1134
 
    }
1135
 
  }
1136
 
  close(flags_fd);
1137
 
  intmax_t tmpmax;
1138
 
  char *tmp;
1139
 
  errno = 0;
1140
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1141
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1142
 
                                         and not (isspace(*tmp)))
1143
 
     or tmpmax != (ifreq_flags)tmpmax){
 
1089
bool get_flags(const char *ifname, struct ifreq *ifr){
 
1090
  int ret;
 
1091
  
 
1092
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1093
  if(s < 0){
 
1094
    perror_plus("socket");
 
1095
    return false;
 
1096
  }
 
1097
  strcpy(ifr->ifr_name, ifname);
 
1098
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
 
1099
  if(ret == -1){
1144
1100
    if(debug){
1145
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1146
 
              flagstring, if_entry->d_name);
 
1101
      perror_plus("ioctl SIOCGIFFLAGS");
1147
1102
    }
1148
 
    free(flagstring);
1149
 
    return 0;
 
1103
    return false;
1150
1104
  }
1151
 
  free(flagstring);
1152
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1105
  return true;
 
1106
}
 
1107
 
 
1108
bool good_flags(const char *ifname, const struct ifreq *ifr){
 
1109
  
1153
1110
  /* Reject the loopback device */
1154
 
  if(flags & IFF_LOOPBACK){
 
1111
  if(ifr->ifr_flags & IFF_LOOPBACK){
1155
1112
    if(debug){
1156
1113
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1157
 
              if_entry->d_name);
 
1114
              ifname);
1158
1115
    }
1159
 
    return 0;
 
1116
    return false;
1160
1117
  }
1161
1118
  /* Accept point-to-point devices only if connect_to is specified */
1162
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1119
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1163
1120
    if(debug){
1164
1121
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1165
 
              if_entry->d_name);
 
1122
              ifname);
1166
1123
    }
1167
 
    return 1;
 
1124
    return true;
1168
1125
  }
1169
1126
  /* Otherwise, reject non-broadcast-capable devices */
1170
 
  if(not (flags & IFF_BROADCAST)){
 
1127
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1171
1128
    if(debug){
1172
1129
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1173
 
              if_entry->d_name);
 
1130
              ifname);
1174
1131
    }
1175
 
    return 0;
 
1132
    return false;
1176
1133
  }
1177
1134
  /* Reject non-ARP interfaces (including dummy interfaces) */
1178
 
  if(flags & IFF_NOARP){
 
1135
  if(ifr->ifr_flags & IFF_NOARP){
1179
1136
    if(debug){
1180
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1181
 
              if_entry->d_name);
 
1137
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1182
1138
    }
1183
 
    return 0;
 
1139
    return false;
1184
1140
  }
 
1141
  
1185
1142
  /* Accept this device */
1186
1143
  if(debug){
1187
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1188
 
            if_entry->d_name);
 
1144
    fprintf(stderr, "Interface \"%s\" is good\n", ifname);
 
1145
  }
 
1146
  return true;
 
1147
}
 
1148
 
 
1149
/* 
 
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.)
 
1153
 */
 
1154
int good_interface(const struct dirent *if_entry){
 
1155
  if(if_entry->d_name[0] == '.'){
 
1156
    return 0;
 
1157
  }
 
1158
  
 
1159
  struct ifreq ifr;
 
1160
  if(not get_flags(if_entry->d_name, &ifr)){
 
1161
    if(debug){
 
1162
      fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
 
1163
              if_entry->d_name);
 
1164
    }
 
1165
    return 0;
 
1166
  }
 
1167
  
 
1168
  if(not good_flags(if_entry->d_name, &ifr)){
 
1169
    return 0;
 
1170
  }
 
1171
  return 1;
 
1172
}
 
1173
 
 
1174
/* 
 
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.)
 
1178
 */
 
1179
int up_interface(const struct dirent *if_entry){
 
1180
  if(if_entry->d_name[0] == '.'){
 
1181
    return 0;
 
1182
  }
 
1183
  
 
1184
  struct ifreq ifr;
 
1185
  if(not get_flags(if_entry->d_name, &ifr)){
 
1186
    if(debug){
 
1187
      fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
 
1188
              if_entry->d_name);
 
1189
    }
 
1190
    return 0;
 
1191
  }
 
1192
  
 
1193
  /* Reject down interfaces */
 
1194
  if(not (ifr.ifr_flags & IFF_UP)){
 
1195
    if(debug){
 
1196
      fprintf(stderr, "Rejecting down interface \"%s\"\n",
 
1197
              if_entry->d_name);
 
1198
    }
 
1199
    return 0;
 
1200
  }
 
1201
  
 
1202
  /* Reject non-running interfaces */
 
1203
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1204
    if(debug){
 
1205
      fprintf(stderr, "Rejecting non-running interface \"%s\"\n",
 
1206
              if_entry->d_name);
 
1207
    }
 
1208
    return 0;
 
1209
  }
 
1210
  
 
1211
  if(not good_flags(if_entry->d_name, &ifr)){
 
1212
    return 0;
1189
1213
  }
1190
1214
  return 1;
1191
1215
}
1201
1225
  return 1;
1202
1226
}
1203
1227
 
 
1228
/* Is this directory entry a runnable program? */
 
1229
int runnable_hook(const struct dirent *direntry){
 
1230
  int ret;
 
1231
  struct stat st;
 
1232
  
 
1233
  if((direntry->d_name)[0] == '\0'){
 
1234
    /* Empty name? */
 
1235
    return 0;
 
1236
  }
 
1237
  
 
1238
  /* Save pointer to last character */
 
1239
  char *end = strchr(direntry->d_name, '\0')-1;
 
1240
  
 
1241
  if(*end == '~'){
 
1242
    /* Backup name~ */
 
1243
    return 0;
 
1244
  }
 
1245
  
 
1246
  if(((direntry->d_name)[0] == '#')
 
1247
     and (*end == '#')){
 
1248
    /* Temporary #name# */
 
1249
    return 0;
 
1250
  }
 
1251
  
 
1252
  /* XXX more rules here */
 
1253
  
 
1254
  ret = stat(direntry->d_name, &st);
 
1255
  if(ret == -1){
 
1256
    if(debug){
 
1257
      perror_plus("Could not stat plugin");
 
1258
    }
 
1259
    return 0;
 
1260
  }
 
1261
  if(not (S_ISREG(st.st_mode))){
 
1262
    /* Not a regular file */
 
1263
    return 0;
 
1264
  }
 
1265
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
 
1266
    /* Not executable */
 
1267
    return 0;
 
1268
  }
 
1269
  return 1;
 
1270
}
 
1271
 
1204
1272
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1205
1273
  int ret;
1206
1274
  struct timespec now;
1518
1586
    }
1519
1587
  }
1520
1588
  
 
1589
  /* Find network hooks and run them */
 
1590
  {
 
1591
    struct dirent **direntries;
 
1592
    struct dirent *direntry;
 
1593
    int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
 
1594
                           alphasort);
 
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,
 
1600
                     direntry->d_name);
 
1601
      if(ret < 0){
 
1602
        perror_plus("asprintf");
 
1603
        continue;
 
1604
      }
 
1605
      pid_t hook_pid = fork();
 
1606
      if(hook_pid == 0){
 
1607
        /* Child */
 
1608
        dup2(devnull, STDIN_FILENO);
 
1609
        close(devnull);
 
1610
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1611
        setenv("DEVICE", interface, 1);
 
1612
        setenv("VERBOSE", debug ? "1" : "0", 1);
 
1613
        setenv("MODE", "start", 1);
 
1614
        /* setenv( XXX more here */
 
1615
        ret = execl(fullname, direntry->d_name, "start", NULL);
 
1616
        perror_plus("execl");
 
1617
      }
 
1618
      free(fullname);
 
1619
      if(quit_now){
 
1620
        goto end;
 
1621
      }
 
1622
    }
 
1623
    close(devnull);
 
1624
  }
 
1625
  
1521
1626
  if(not debug){
1522
1627
    avahi_set_log_function(empty_log);
1523
1628
  }
1524
 
 
 
1629
  
1525
1630
  if(interface[0] == '\0'){
1526
1631
    struct dirent **direntries;
1527
 
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1632
    /* First look for interfaces that are up */
 
1633
    ret = scandir(sys_class_net, &direntries, up_interface,
1528
1634
                  alphasort);
 
1635
    if(ret == 0){
 
1636
      /* No up interfaces, look for any good interfaces */
 
1637
      free(direntries);
 
1638
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1639
                    alphasort);
 
1640
    }
1529
1641
    if(ret >= 1){
1530
 
      /* Pick the first good interface */
 
1642
      /* Pick the first interface returned */
1531
1643
      interface = strdup(direntries[0]->d_name);
1532
1644
      if(debug){
1533
1645
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1869
1981
    if (not quit_now){
1870
1982
      exitcode = EXIT_SUCCESS;
1871
1983
    }
1872
 
 
 
1984
    
1873
1985
    goto end;
1874
1986
  }
1875
1987
  
1972
2084
    }
1973
2085
  }
1974
2086
  
 
2087
  /* XXX run network hooks "stop" here  */
 
2088
  
1975
2089
  /* Take down the network interface */
1976
2090
  if(take_down_interface){
1977
2091
    /* Re-raise priviliges */