diff options
Diffstat (limited to 'modules/slock/preload.c')
| -rw-r--r-- | modules/slock/preload.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/modules/slock/preload.c b/modules/slock/preload.c new file mode 100644 index 00000000..0b7e81d4 --- /dev/null +++ b/modules/slock/preload.c @@ -0,0 +1,41 @@ +// Preloaded into slock, which then runs as its user instead of root: +// getspnam returns the password hash from the credential hashed-password, +// and dropping privileges does nothing. + +#include <shadow.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static char hash[4096]; + +static struct spwd entry = { + .sp_namp = "", + .sp_pwdp = hash, +}; + +struct spwd *getspnam(const char *name) { + (void) name; + const char *dir = getenv("CREDENTIALS_DIRECTORY"); + char path[4096]; + if (dir == NULL || + snprintf(path, sizeof path, "%s/hashed-password", dir) >= (int) sizeof path) { + return NULL; + } + FILE *f = fopen(path, "r"); + if (f == NULL) { + return NULL; + } + char *line = fgets(hash, sizeof hash, f); + fclose(f); + if (line == NULL) { + return NULL; + } + hash[strcspn(hash, "\n")] = '\0'; + return &entry; +} + +int setgroups(size_t size, const gid_t *list) { (void) size; (void) list; return 0; } +int setgid(gid_t gid) { (void) gid; return 0; } +int setuid(uid_t uid) { (void) uid; return 0; } |
