summaryrefslogtreecommitdiffstats
path: root/modules/slock/preload.c
blob: 0b7e81d4295b38ca04ef981e91ea5362a663c1e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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; }