diff options
| author | tv <tv@krebsco.de> | 2026-12-04 01:56:12 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-12-04 15:44:35 +0000 |
| commit | f269fdc2626228534c39292c288a63c19a4c779d (patch) | |
| tree | 48d88407f3c1285b02b0233e8e7f4b1c491dcd23 /modules/slock/validate.c | |
| parent | 6d2f40458c8ffd623f29ff6722cdcd9fc8c57415 (diff) | |
slock: password hash from enstate, else setuid
The hash no longer goes into the store. With enstate, it can come from
the slot secrets/slock.hashed-password: then slock runs as its user, with
that hash. Without a hash, the credential falls back to "none" and
slock runs setuid root, reading /etc/shadow.
A validator for text/x-crypt-hash checks the hash the way slock uses
it, with crypt. The slot is optional, so enstate leaves out a hash the
validator objects to, and slock runs setuid instead of locking with a
hash no password matches.
Diffstat (limited to 'modules/slock/validate.c')
| -rw-r--r-- | modules/slock/validate.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/modules/slock/validate.c b/modules/slock/validate.c new file mode 100644 index 00000000..aabec058 --- /dev/null +++ b/modules/slock/validate.c @@ -0,0 +1,82 @@ +// 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 <crypt.h> +#include <dirent.h> +#include <stdio.h> +#include <string.h> + +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; +} |
