summaryrefslogtreecommitdiffstats
path: root/src/pwcache.c
blob: 5026dee9ad5ef9a28b8a1fee8b2481d94b5868a5 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD

#include "pwcache.h"
#include "darray.h"
#include "trie.h"
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/** Represents cache hits for negative results. */
static void *MISSING = &MISSING;

/** Callback type for bfs_getent(). */
typedef void *bfs_getent_fn(const void *key, void *ent, void *buf, size_t bufsize);

/** Shared scaffolding for get{pw,gr}{nam,?id}_r(). */
static void *bfs_getent(struct trie_leaf *leaf, bfs_getent_fn *fn, const void *key, size_t entsize, size_t bufsize) {
	if (leaf->value) {
		errno = 0;
		return leaf->value == MISSING ? NULL : leaf->value;
	}

	void *buf = NULL;
	while (true) {
		void *result = buf;
		buf = realloc(buf, entsize + bufsize);
		if (!buf) {
			free(result);
			return NULL;
		}

		result = fn(key, buf, (char *)buf + entsize, bufsize);
		if (result) {
			leaf->value = result;
			return result;
		} else if (errno == 0) {
			free(buf);
			leaf->value = MISSING;
			return NULL;
		} else if (errno == ERANGE) {
			bufsize *= 2;
		} else {
			free(buf);
			return NULL;
		}
	}
}

/** Flush a single cache. */
static void bfs_pwcache_flush(struct trie *trie) {
	TRIE_FOR_EACH(trie, leaf) {
		if (leaf->value != MISSING) {
			free(leaf->value);
		}
		trie_remove(trie, leaf);
	}
}

struct bfs_users {
	/** Initial buffer size for getpw*_r(). */
	size_t bufsize;
	/** A map from usernames to entries. */
	struct trie by_name;
	/** A map from UIDs to entries. */
	struct trie by_uid;
};

struct bfs_users *bfs_users_new(void) {
	struct bfs_users *users = malloc(sizeof(*users));
	if (!users) {
		return NULL;
	}

	long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
	if (bufsize > 0) {
		users->bufsize = bufsize;
	} else {
		users->bufsize = 1024;
	}

	trie_init(&users->by_name);
	trie_init(&users->by_uid);
	return users;
}

/** bfs_getent() callback for getpwnam_r(). */
static void *bfs_getpwnam_impl(const void *key, void *ent, void *buf, size_t bufsize) {
	struct passwd *result;
	errno = getpwnam_r(key, ent, buf, bufsize, &result);
	return result;
}

const struct passwd *bfs_getpwnam(struct bfs_users *users, const char *name) {
	struct trie_leaf *leaf = trie_insert_str(&users->by_name, name);
	if (!leaf) {
		return NULL;
	}

	return bfs_getent(leaf, bfs_getpwnam_impl, name, sizeof(struct passwd), users->bufsize);
}

/** bfs_getent() callback for getpwuid_r(). */
static void *bfs_getpwuid_impl(const void *key, void *ent, void *buf, size_t bufsize) {
	struct passwd *result;
	errno = getpwuid_r(*(const uid_t *)key, ent, buf, bufsize, &result);
	return result;
}

const struct passwd *bfs_getpwuid(struct bfs_users *users, uid_t uid) {
	struct trie_leaf *leaf = trie_insert_mem(&users->by_uid, &uid, sizeof(uid));
	if (!leaf) {
		return NULL;
	}

	return bfs_getent(leaf, bfs_getpwuid_impl, &uid, sizeof(struct passwd), users->bufsize);
}

void bfs_users_flush(struct bfs_users *users) {
	bfs_pwcache_flush(&users->by_name);
	bfs_pwcache_flush(&users->by_uid);
}

void bfs_users_free(struct bfs_users *users) {
	if (users) {
		bfs_users_flush(users);
		trie_destroy(&users->by_uid);
		trie_destroy(&users->by_name);
		free(users);
	}
}

struct bfs_groups {
	/** Initial buffer size for getgr*_r(). */
	size_t bufsize;
	/** A map from group names to entries. */
	struct trie by_name;
	/** A map from GIDs to entries. */
	struct trie by_gid;
};

struct bfs_groups *bfs_groups_new(void) {
	struct bfs_groups *groups = malloc(sizeof(*groups));
	if (!groups) {
		return NULL;
	}

	long bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
	if (bufsize > 0) {
		groups->bufsize = bufsize;
	} else {
		groups->bufsize = 1024;
	}

	trie_init(&groups->by_name);
	trie_init(&groups->by_gid);
	return groups;
}

/** bfs_getent() callback for getgrnam_r(). */
static void *bfs_getgrnam_impl(const void *key, void *ent, void *buf, size_t bufsize) {
	struct group *result;
	errno = getgrnam_r(key, ent, buf, bufsize, &result);
	return result;
}

const struct group *bfs_getgrnam(struct bfs_groups *groups, const char *name) {
	struct trie_leaf *leaf = trie_insert_str(&groups->by_name, name);
	if (!leaf) {
		return NULL;
	}

	return bfs_getent(leaf, bfs_getgrnam_impl, name, sizeof(struct group), groups->bufsize);
}

/** bfs_getent() callback for getgrgid_r(). */
static void *bfs_getgrgid_impl(const void *key, void *ent, void *buf, size_t bufsize) {
	struct group *result;
	errno = getgrgid_r(*(const gid_t *)key, ent, buf, bufsize, &result);
	return result;
}

const struct group *bfs_getgrgid(struct bfs_groups *groups, gid_t gid) {
	struct trie_leaf *leaf = trie_insert_mem(&groups->by_gid, &gid, sizeof(gid));
	if (!leaf) {
		return NULL;
	}

	return bfs_getent(leaf, bfs_getgrgid_impl, &gid, sizeof(struct group), groups->bufsize);
}

void bfs_groups_flush(struct bfs_groups *groups) {
	bfs_pwcache_flush(&groups->by_name);
	bfs_pwcache_flush(&groups->by_gid);
}

void bfs_groups_free(struct bfs_groups *groups) {
	if (groups) {
		bfs_groups_flush(groups);
		trie_destroy(&groups->by_gid);
		trie_destroy(&groups->by_name);
		free(groups);
	}
}