/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

Intermediate commit - this does *not* work yet.

* plugins.d/mandos-client.c (HOOKDIR): New.
  (up_interface): New.
  (runnable_hook): New.
  (main): Run network hooks with "start" argument.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 * along with this program.  If not, see
27
27
 * <http://www.gnu.org/licenses/>.
28
28
 * 
29
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
29
 * Contact the authors at <mandos@recompile.se>.
30
30
 */
31
31
 
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
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";
129
130
const char *argp_program_version = "mandos-client " VERSION;
130
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
131
const char *argp_program_bug_address = "<mandos@recompile.se>";
131
132
static const char sys_class_net[] = "/sys/class/net";
132
133
char *connect_to = NULL;
133
134
 
1085
1086
  errno = old_errno;
1086
1087
}
1087
1088
 
 
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){
 
1100
    if(debug){
 
1101
      perror_plus("ioctl SIOCGIFFLAGS");
 
1102
    }
 
1103
    return false;
 
1104
  }
 
1105
  return true;
 
1106
}
 
1107
 
 
1108
bool good_flags(const char *ifname, const struct ifreq *ifr){
 
1109
  
 
1110
  /* Reject the loopback device */
 
1111
  if(ifr->ifr_flags & IFF_LOOPBACK){
 
1112
    if(debug){
 
1113
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1114
              ifname);
 
1115
    }
 
1116
    return false;
 
1117
  }
 
1118
  /* Accept point-to-point devices only if connect_to is specified */
 
1119
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
 
1120
    if(debug){
 
1121
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1122
              ifname);
 
1123
    }
 
1124
    return true;
 
1125
  }
 
1126
  /* Otherwise, reject non-broadcast-capable devices */
 
1127
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
 
1128
    if(debug){
 
1129
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1130
              ifname);
 
1131
    }
 
1132
    return false;
 
1133
  }
 
1134
  /* Reject non-ARP interfaces (including dummy interfaces) */
 
1135
  if(ifr->ifr_flags & IFF_NOARP){
 
1136
    if(debug){
 
1137
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1138
    }
 
1139
    return false;
 
1140
  }
 
1141
  
 
1142
  /* Accept this device */
 
1143
  if(debug){
 
1144
    fprintf(stderr, "Interface \"%s\" is good\n", ifname);
 
1145
  }
 
1146
  return true;
 
1147
}
 
1148
 
1088
1149
/* 
1089
1150
 * This function determines if a directory entry in /sys/class/net
1090
1151
 * corresponds to an acceptable network device.
1091
1152
 * (This function is passed to scandir(3) as a filter function.)
1092
1153
 */
1093
1154
int good_interface(const struct dirent *if_entry){
 
1155
  int ret;
 
1156
  if(if_entry->d_name[0] == '.'){
 
1157
    return 0;
 
1158
  }
 
1159
  struct ifreq ifr;
 
1160
 
 
1161
  if(not get_flags(if_entry->d_name, &ifr)){
 
1162
    return 0;
 
1163
  }
 
1164
  
 
1165
  if(not good_flags(if_entry->d_name, &ifr)){
 
1166
    return 0;
 
1167
  }
 
1168
  return 1;
 
1169
}
 
1170
 
 
1171
/* 
 
1172
 * This function determines if a directory entry in /sys/class/net
 
1173
 * corresponds to an acceptable network device which is up.
 
1174
 * (This function is passed to scandir(3) as a filter function.)
 
1175
 */
 
1176
int up_interface(const struct dirent *if_entry){
1094
1177
  ssize_t ssret;
1095
1178
  char *flagname = NULL;
1096
1179
  if(if_entry->d_name[0] == '.'){
1158
1241
    }
1159
1242
    return 0;
1160
1243
  }
 
1244
 
 
1245
  /* Reject down interfaces */
 
1246
  if(not (flags & IFF_UP)){
 
1247
    return 0;
 
1248
  }
 
1249
  
1161
1250
  /* Accept point-to-point devices only if connect_to is specified */
1162
1251
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1163
1252
    if(debug){
1201
1290
  return 1;
1202
1291
}
1203
1292
 
 
1293
/* Is this directory entry a runnable program? */
 
1294
int runnable_hook(const struct dirent *direntry){
 
1295
  int ret;
 
1296
  struct stat st;
 
1297
  
 
1298
  if((direntry->d_name)[0] == '\0'){
 
1299
    /* Empty name? */
 
1300
    return 0;
 
1301
  }
 
1302
  
 
1303
  /* Save pointer to last character */
 
1304
  char *end = strchr(direntry->d_name, '\0')-1;
 
1305
  
 
1306
  if(*end == '~'){
 
1307
    /* Backup name~ */
 
1308
    return 0;
 
1309
  }
 
1310
  
 
1311
  if(((direntry->d_name)[0] == '#')
 
1312
     and (*end == '#')){
 
1313
    /* Temporary #name# */
 
1314
    return 0;
 
1315
  }
 
1316
  
 
1317
  /* XXX more rules here */
 
1318
  
 
1319
  ret = stat(direntry->d_name, &st);
 
1320
  if(ret == -1){
 
1321
    if(debug){
 
1322
      perror_plus("Could not stat plugin");
 
1323
    }
 
1324
    return 0;
 
1325
  }
 
1326
  if(not (st.st_mode & S_ISREG)){
 
1327
    /* Not a regular file */
 
1328
    return 0;
 
1329
  }
 
1330
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
 
1331
    /* Not executable */
 
1332
    return 0;
 
1333
  }
 
1334
  return 1;
 
1335
}
 
1336
 
1204
1337
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1205
1338
  int ret;
1206
1339
  struct timespec now;
1518
1651
    }
1519
1652
  }
1520
1653
  
 
1654
  /* Find network hooks and run them */
 
1655
  {
 
1656
    struct dirent **direntries;
 
1657
    struct dirent *direntry;
 
1658
    int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
 
1659
                           alphasort);
 
1660
    int devnull = open("/dev/null", O_RDONLY);
 
1661
    for(int i = 0; i < numhooks; i++){
 
1662
      direntry = direntries[0];
 
1663
      char *fullname = NULL;
 
1664
      ret = asprintf(&fullname, "%s/%s", tempdir,
 
1665
                     direntry->d_name);
 
1666
      if(ret < 0){
 
1667
        perror_plus("asprintf");
 
1668
        continue;
 
1669
      }
 
1670
      pid_t hook_pid = fork();
 
1671
      if(hook_pid == 0){
 
1672
        /* Child */
 
1673
        dup2(devnull, STDIN_FILENO);
 
1674
        close(devnull);
 
1675
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1676
        setenv("DEVICE", interface, 1);
 
1677
        setenv("VERBOSE", debug ? "1" : "0", 1);
 
1678
        setenv("MODE", "start", 1);
 
1679
        /* setenv( XXX more here */
 
1680
        ret = execl(fullname, direntry->d_name, "start");
 
1681
        perror_plus("execl");
 
1682
      }
 
1683
      free(fullname);
 
1684
      if(quit_now){
 
1685
        goto end;
 
1686
      }
 
1687
    }
 
1688
    close(devnull);
 
1689
  }
 
1690
  
1521
1691
  if(not debug){
1522
1692
    avahi_set_log_function(empty_log);
1523
1693
  }
1524
 
 
 
1694
  
1525
1695
  if(interface[0] == '\0'){
1526
1696
    struct dirent **direntries;
1527
1697
    ret = scandir(sys_class_net, &direntries, good_interface,
1869
2039
    if (not quit_now){
1870
2040
      exitcode = EXIT_SUCCESS;
1871
2041
    }
1872
 
 
 
2042
    
1873
2043
    goto end;
1874
2044
  }
1875
2045
  
1972
2142
    }
1973
2143
  }
1974
2144
  
 
2145
  /* XXX run network hooks "stop" here  */
 
2146
  
1975
2147
  /* Take down the network interface */
1976
2148
  if(take_down_interface){
1977
2149
    /* Re-raise priviliges */