// enstate's validator for text/x-crypt-hash (enstate's docs/design.md, // 5.2): objects to each slot under IN/channels that preload.c couldn't // use, i.e. that isn't one line of at most 4095 bytes that crypt accepts // as a setting. // // usage: validate IN OUT #include #include #include #include static void object(const char *channel, const char *slot, const char *message) { printf("{\"address\": \"%s/%s\", \"message\": \"%s\"}\n", channel, slot, message); } static void check(const char *path, const char *channel, const char *slot) { char hash[4096]; FILE *f = fopen(path, "r"); if (f == NULL) { object(channel, slot, "cannot be read"); return; } size_t n = fread(hash, 1, sizeof hash, f); fclose(f); if (n == sizeof hash) { object(channel, slot, "is longer than 4095 bytes"); return; } if (n > 0 && hash[n - 1] == '\n') { n--; } hash[n] = '\0'; if (n == 0) { object(channel, slot, "is empty"); return; } if (strlen(hash) != n || strchr(hash, '\n') != NULL) { object(channel, slot, "is not one line"); return; } const char *result = crypt("", hash); if (result == NULL || result[0] == '*' || strlen(result) != n) { object(channel, slot, "is not a password hash that crypt accepts"); } } int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "usage: validate IN OUT\n"); return 1; } char path[4096]; snprintf(path, sizeof path, "%s/channels", argv[1]); DIR *channels = opendir(path); if (channels == NULL) { return 0; } struct dirent *channel; while ((channel = readdir(channels)) != NULL) { if (channel->d_name[0] == '.') { continue; } snprintf(path, sizeof path, "%s/channels/%s", argv[1], channel->d_name); DIR *slots = opendir(path); if (slots == NULL) { continue; } struct dirent *slot; while ((slot = readdir(slots)) != NULL) { if (slot->d_name[0] == '.') { continue; } snprintf(path, sizeof path, "%s/channels/%s/%s", argv[1], channel->d_name, slot->d_name); check(path, channel->d_name, slot->d_name); } closedir(slots); } closedir(channels); return 0; }