Debian Codegraph @

src/launch/nss-cache.c

raw
1 /*
2 * NSS Cache
3 *
4 * Calling out to NSS is potentially slow and prone to deadlocks, so we
5 * maintain a cache to only call out when necessary. This cache does no
6 * invalidation, so should not be long-living.
7 *
8 * The cache can be populated from /etc/{passwd,group}, which means NSS
9 * will be invoked only for users/groups that do not appear in these
10 * files, which should not happen on a well-configured system.
11 *
12 * The root user is hardcoded to always return UID 0, see passwd(5).
13 */
14
15 #include <c-rbtree.h>
16 #include <c-stdaux.h>
17 #include <grp.h>
18 #include <pwd.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include "launch/nss-cache.h"
22 #include "util/error.h"
23
24 typedef struct NSSCacheNode {
25 uint32_t id;
26 CRBNode rb_by_name;
27 CRBNode rb_by_id;
28
29 /*
30 * Note that these structures are not fully allocated. They are plain
31 * copies of the source data, with non-static fields cleared to 0.
32 */
33 union {
34 struct passwd pw;
35 struct group gr;
36 };
37
38 char name[];
39 } NSSCacheNode;
40
41 static int nss_cache_node_new(NSSCacheNode **nodep, const char *name, uint32_t id) {
42 NSSCacheNode *node;
43 size_t n_name;
44
45 name = name ? : "";
46 n_name = strlen(name);
47
48 node = malloc(sizeof(*node) + n_name + 1);
49 if (!node)
50 return error_origin(-ENOMEM);
51
52 node->id = id;
53 node->rb_by_name = (CRBNode)C_RBNODE_INIT(node->rb_by_name);
54 node->rb_by_id = (CRBNode)C_RBNODE_INIT(node->rb_by_id);
55 memcpy(node->name, name, n_name + 1);
56
57 *nodep = node;
58 return 0;
59 }
60
61 static NSSCacheNode *nss_cache_node_free(NSSCacheNode *node) {
62 c_rbnode_unlink(&node->rb_by_name);
63 c_rbnode_unlink(&node->rb_by_id);
64
65 free(node);
66
67 return NULL;
68 }
69
70 void nss_cache_init(NSSCache *cache) {
71 *cache = (NSSCache)NSS_CACHE_INIT;
72 }
73
74 void nss_cache_deinit(NSSCache *cache) {
75 NSSCacheNode *node, *safe;
76
77 c_rbtree_for_each_entry_safe_postorder_unlink(node,
78 safe,
79 &cache->user_tree,
80 rb_by_name)
81 nss_cache_node_free(node);
82
83 c_rbtree_for_each_entry_safe_postorder_unlink(node,
84 safe,
85 &cache->group_tree,
86 rb_by_name)
87 nss_cache_node_free(node);
88
89 c_assert(c_rbtree_is_empty(&cache->user_tree));
90 c_assert(c_rbtree_is_empty(&cache->uid_tree));
91 c_assert(c_rbtree_is_empty(&cache->group_tree));
92 c_assert(c_rbtree_is_empty(&cache->gid_tree));
93 }
94
95 static int nss_cache_node_compare_name(CRBTree *t, void *k, CRBNode *rb) {
96 NSSCacheNode *node = c_rbnode_entry(rb, NSSCacheNode, rb_by_name);
97 const char *name = k;
98
99 return strcmp(name, node->name);
100 }
101
102 static int nss_cache_node_compare_id(CRBTree *t, void *k, CRBNode *rb) {
103 NSSCacheNode *node = c_rbnode_entry(rb, NSSCacheNode, rb_by_id);
104 uint32_t id = (uint32_t)(unsigned long)k;
105
106 if (id > node->id)
107 return 1;
108 else if (id < node->id)
109 return -1;
110 else
111 return 0;
112 }
113
114 static int nss_cache_add(CRBTree *tree_by_name,
115 CRBTree *tree_by_id,
116 NSSCacheNode **nodep,
117 const char *name,
118 uint32_t id) {
119 CRBNode **slot_by_name, **slot_by_id, *parent_by_name, *parent_by_id;
120 NSSCacheNode *node_by_name = NULL, *node_by_id = NULL, *node = NULL;
121 int r;
122
123 /*
124 * Do not cache invalid user/UID or group/GID pairs.
125 */
126 if (!name && id == (uint32_t)-1)
127 return error_origin(-EINVAL);
128
129 if (name) {
130 /*
131 * The user/group is valid, so try to find an existing entry for it.
132 */
133 slot_by_name = c_rbtree_find_slot(tree_by_name,
134 nss_cache_node_compare_name,
135 name,
136 &parent_by_name);
137
138 if (!slot_by_name) {
139 node_by_name = c_rbnode_entry(parent_by_name, NSSCacheNode, rb_by_name);
140 node = node_by_name;
141 }
142 }
143
144 if (id != (uint32_t)-1) {
145 /*
146 * The UID/GID is valid, so try to find an existing entry for it.
147 */
148 slot_by_id = c_rbtree_find_slot(tree_by_id,
149 nss_cache_node_compare_id,
150 (void *)(unsigned long)id,
151 &parent_by_id);
152 if (!slot_by_id) {
153 node_by_id = c_rbnode_entry(parent_by_id, NSSCacheNode, rb_by_id);
154 node = node_by_id;
155 }
156 }
157
158 if (node_by_name != node_by_id &&
159 ((node_by_name && node_by_id) ||
160 (node_by_id && c_rbnode_is_linked(&node_by_id->rb_by_name)) ||
161 (node_by_name && c_rbnode_is_linked(&node_by_name->rb_by_id)))) {
162 /*
163 * If an entry is linked by-name (resp., by-id), and either not linked
164 * at all by-id (resp., by-name), or using a different id (resp., name)
165 * from what we are adding, then that implies a UID/GID conflict which
166 * there is no sane way to handle, other than bypassing the cache. We
167 * therefore do not cach such entries. Worst case, you will end up
168 * calling into NSS all the time.
169 */
170 *nodep = NULL;
171 return 0;
172 }
173
174 if (!node) {
175 /*
176 * No entry was found either by-name, nor by-id. We have to
177 * create a new entry and link it into both trees.
178 *
179 * This is the common case, since the nss-cache is shortlived
180 * and there really shouldn't be any conflicts in UIDs/GIDs.
181 */
182 r = nss_cache_node_new(&node, name, id);
183 if (r)
184 return error_trace(r);
185
186 if (name)
187 c_rbtree_add(tree_by_name,
188 parent_by_name,
189 slot_by_name,
190 &node->rb_by_name);
191 if (id != (uint32_t)-1)
192 c_rbtree_add(tree_by_id,
193 parent_by_id,
194 slot_by_id,
195 &node->rb_by_id);
196
197 }
198
199 *nodep = node;
200 return 0;
201 }
202
203 static int nss_cache_add_user(NSSCache *cache, struct passwd *pw) {
204 NSSCacheNode *node;
205 int r;
206
207 r = nss_cache_add(&cache->user_tree,
208 &cache->uid_tree,
209 &node,
210 pw->pw_name,
211 pw->pw_uid);
212 if (r)
213 return error_trace(r);
214
215 if (node) {
216 c_memset(&node->pw, 0, sizeof(node->pw));
217 node->pw.pw_name = node->name;
218 node->pw.pw_uid = pw->pw_uid;
219 node->pw.pw_gid = pw->pw_gid;
220 }
221
222 return 0;
223 }
224
225 static int nss_cache_add_group(NSSCache *cache, struct group *gr) {
226 NSSCacheNode *node;
227 int r;
228
229 r = nss_cache_add(&cache->group_tree,
230 &cache->gid_tree,
231 &node,
232 gr->gr_name,
233 gr->gr_gid);
234 if (r)
235 return error_trace(r);
236
237 if (node) {
238 c_memset(&node->gr, 0, sizeof(node->gr));
239 node->gr.gr_name = node->name;
240 node->gr.gr_gid = gr->gr_gid;
241 }
242
243 return 0;
244 }
245
246 static int nss_cache_populate_users(NSSCache *cache) {
247 _c_cleanup_(c_fclosep) FILE *passwd = NULL;
248 struct passwd *pw;
249 int r;
250
251 passwd = fopen("/etc/passwd", "re");
252 if (!passwd) {
253 if (errno == ENOENT)
254 return 0;
255
256 return error_origin(-errno);
257 }
258
259 while ((pw = fgetpwent(passwd))) {
260 r = nss_cache_add_user(cache, pw);
261 if (r)
262 return error_trace(r);
263 }
264
265 return 0;
266 }
267
268 static int nss_cache_populate_groups(NSSCache *cache) {
269 _c_cleanup_(c_fclosep) FILE *group = NULL;
270 struct group *gr;
271 int r;
272
273 group = fopen("/etc/group", "re");
274 if (!group) {
275 if (errno == ENOENT)
276 return 0;
277
278 return error_origin(-errno);
279 }
280
281 while ((gr = fgetgrent(group))) {
282 r = nss_cache_add_group(cache, gr);
283 if (r)
284 return error_trace(r);
285 }
286
287 return 0;
288 }
289
290 int nss_cache_populate(NSSCache *cache) {
291 int r;
292
293 r = nss_cache_add_user(cache,
294 &(struct passwd){
295 .pw_name = (char[]){ "root" },
296 .pw_uid = 0,
297 .pw_gid = 0,
298 });
299 if (r)
300 return error_trace(r);
301
302 r = nss_cache_add_group(cache,
303 &(struct group){
304 .gr_name = (char[]){ "root" },
305 .gr_gid = 0,
306 });
307 if (r)
308 return error_trace(r);
309
310 r = nss_cache_populate_users(cache);
311 if (r)
312 return error_trace(r);
313
314 r = nss_cache_populate_groups(cache);
315 if (r)
316 return error_trace(r);
317
318 return 0;
319 }
320
321 int nss_cache_get_uid(NSSCache *cache, uint32_t *uidp, uint32_t *gidp, const char *name) {
322 unsigned long long id;
323 NSSCacheNode *node;
324 struct passwd *pw;
325 bool by_id = false;
326 char *end;
327 int r;
328
329 static_assert(sizeof(uid_t) == sizeof(uint32_t), "uid_t is not 32 bits");
330
331 /* try parsing @name as a numeric ID */
332 errno = 0;
333 id = strtoull(name, &end, 10);
334 if (end != name && *end == '\0' && errno == 0 && id < UINT32_MAX)
335 by_id = true;
336
337 /*
338 * First try a lookup in our cache. If we find an entry, use it and
339 * return it. If not, we have to fall back to NSS lookups below.
340 */
341 if (by_id)
342 node = c_rbtree_find_entry(&cache->uid_tree,
343 nss_cache_node_compare_id,
344 (void *)(unsigned long)id,
345 NSSCacheNode,
346 rb_by_id);
347 else
348 node = c_rbtree_find_entry(&cache->user_tree,
349 nss_cache_node_compare_name,
350 name,
351 NSSCacheNode,
352 rb_by_name);
353 if (node) {
354 pw = &node->pw;
355
356 if (by_id && !pw->pw_name)
357 return NSS_CACHE_E_INVALID_NAME;
358 else if (pw->pw_uid == (uint32_t)-1)
359 return NSS_CACHE_E_INVALID_NAME;
360 } else {
361 fprintf(stderr, "Looking up NSS user entry for '%s'...\n", name);
362
363 if (by_id)
364 pw = getpwuid(id);
365 else
366 pw = getpwnam(name);
367
368 if (pw) {
369 fprintf(stderr, "NSS returned NAME '%s' and UID '%u'\n",
370 pw->pw_name, pw->pw_uid);
371
372 r = nss_cache_add_user(cache, pw);
373 if (r)
374 return r;
375 } else {
376 fprintf(stderr, "NSS returned no entry for '%s'\n",
377 name);
378
379 if (by_id) {
380 r = nss_cache_add_user(cache,
381 &(struct passwd){
382 .pw_name = NULL,
383 .pw_uid = id,
384 .pw_gid = (uint32_t)-1,
385 });
386 if (r)
387 return r;
388 } else {
389 r = nss_cache_add_user(cache,
390 &(struct passwd){
391 .pw_name = (char*)name,
392 .pw_uid = (uint32_t)-1,
393 .pw_gid = (uint32_t)-1,
394 });
395 if (r)
396 return r;
397 }
398
399 return NSS_CACHE_E_INVALID_NAME;
400 }
401 }
402
403 if (uidp)
404 *uidp = pw->pw_uid;
405 if (gidp)
406 *gidp = pw->pw_gid;
407 return 0;
408 }
409
410 int nss_cache_get_gid(NSSCache *cache, uint32_t *gidp, const char *name) {
411 unsigned long long id;
412 NSSCacheNode *node;
413 struct group *gr;
414 bool by_id = false;
415 char *end;
416 int r;
417
418 static_assert(sizeof(gid_t) == sizeof(uint32_t), "gid_t is not 32 bits");
419
420 /* try parsing @name as a numeric ID */
421 errno = 0;
422 id = strtoull(name, &end, 10);
423 if (end != name && *end == '\0' && errno == 0 && id < UINT32_MAX)
424 by_id = true;
425
426 /*
427 * First try a lookup in our cache. If we find an entry, use it and
428 * return it. If not, we have to fall back to NSS lookups below.
429 */
430 if (by_id)
431 node = c_rbtree_find_entry(&cache->gid_tree,
432 nss_cache_node_compare_id,
433 (void *)(unsigned long)id,
434 NSSCacheNode,
435 rb_by_id);
436 else
437 node = c_rbtree_find_entry(&cache->group_tree,
438 nss_cache_node_compare_name,
439 name,
440 NSSCacheNode,
441 rb_by_name);
442 if (node) {
443 gr = &node->gr;
444
445 if (by_id && !gr->gr_name)
446 return NSS_CACHE_E_INVALID_NAME;
447 else if (gr->gr_gid == (uint32_t)-1)
448 return NSS_CACHE_E_INVALID_NAME;
449 } else {
450 fprintf(stderr, "Looking up NSS group entry for '%s'...\n", name);
451
452 if (by_id)
453 gr = getgrgid(id);
454 else
455 gr = getgrnam(name);
456
457 if (gr) {
458 fprintf(stderr, "NSS returned NAME '%s' and GID '%u'\n",
459 gr->gr_name, gr->gr_gid);
460
461 r = nss_cache_add_group(cache, gr);
462 if (r)
463 return r;
464 } else {
465 fprintf(stderr, "NSS returned no entry for '%s'\n",
466 name);
467
468 if (by_id) {
469 r = nss_cache_add_group(cache,
470 &(struct group){
471 .gr_name = NULL,
472 .gr_gid = id,
473 });
474 if (r)
475 return r;
476 } else {
477 r = nss_cache_add_group(cache,
478 &(struct group){
479 .gr_name = (char*) name,
480 .gr_gid = (uint32_t)-1,
481 });
482 if (r)
483 return r;
484 }
485
486 return NSS_CACHE_E_INVALID_NAME;
487 }
488 }
489
490 *gidp = gr->gr_gid;
491 return 0;
492 }
493
494 static int nss_cache_resolve_names(
495 NSSCache *nss_cache,
496 uint32_t **uidsp,
497 size_t *n_uidsp,
498 const char * const *usernames,
499 size_t n_usernames
500 ) {
501 _c_cleanup_(c_freep) uint32_t *uids = NULL;
502 size_t i, n_uids = 0;
503 uid_t uid;
504 int r;
505
506 if (!n_usernames) {
507 *uidsp = NULL;
508 *n_uidsp = 0;
509 return 0;
510 }
511
512 uids = calloc(n_usernames, sizeof(*uids));
513 if (!uids)
514 return error_origin(-ENOMEM);
515
516 for (i = 0; i < n_usernames; ++i) {
517 r = nss_cache_get_uid(nss_cache, &uid, NULL, usernames[i]);
518 if (r) {
519 if (r == NSS_CACHE_E_INVALID_NAME)
520 continue;
521
522 return error_fold(r);
523 }
524
525 uids[n_uids++] = uid;
526 }
527
528 *uidsp = uids;
529 *n_uidsp = n_uids;
530 uids = NULL;
531 return 0;
532 }
533
534 int nss_cache_resolve_system_console_users(NSSCache *nss_cache, uint32_t **uidsp, size_t *n_uidsp) {
535 static const char * const usernames[] = { SYSTEM_CONSOLE_USERS };
536 static const size_t n_usernames = C_ARRAY_SIZE(usernames);
537
538 // We avoid inlining `nss_cache_resolve_names()` here, as GCC will start
539 // complaining about use of `usernames` if it is empty, even though the
540 // function bails out early if it is empty.
541
542 return nss_cache_resolve_names(nss_cache, uidsp, n_uidsp, usernames, n_usernames);
543 }