Debian Codegraph @

src/tmpfiles/offline-passwd.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "chase.h"
5 #include "fd-util.h"
6 #include "hash-funcs.h"
7 #include "hashmap.h"
8 #include "log.h"
9 #include "offline-passwd.h"
10 #include "path-util.h"
11 #include "string-util.h"
12 #include "strv.h"
13 #include "user-util.h"
14
15 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(uid_gid_hash_ops, char, string_hash_func, string_compare_func, free);
16
17 static int open_passwd_file(const char *root, const char *fname, FILE **ret_file) {
18 _cleanup_free_ char *p = NULL;
19 _cleanup_close_ int fd = -EBADF;
20 _cleanup_fclose_ FILE *f = NULL;
21
22 fd = chase_and_open(fname, root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC, &p);
23 if (fd < 0)
24 return fd;
25
26 f = fdopen(fd, "r");
27 if (!f)
28 return -errno;
29
30 TAKE_FD(fd);
31
32 if (DEBUG_LOGGING) {
33 _cleanup_free_ char *bn = NULL;
34
35 (void) path_extract_filename(fname, &bn);
36 log_debug("Reading %s entries from %s...", strna(bn), p);
37 }
38
39 *ret_file = TAKE_PTR(f);
40 return 0;
41 }
42
43 static int populate_uid_cache(const char *root, Hashmap **ret) {
44 _cleanup_hashmap_free_ Hashmap *cache = NULL;
45 int r;
46
47 assert(ret);
48
49 cache = hashmap_new(&uid_gid_hash_ops);
50 if (!cache)
51 return -ENOMEM;
52
53 /* The directory list is hardcoded here: /etc is the standard, and rpm-ostree uses /usr/lib. This
54 * could be made configurable, but I don't see the point right now. */
55
56 FOREACH_STRING(fname, "/etc/passwd", "/usr/lib/passwd") {
57 _cleanup_fclose_ FILE *f = NULL;
58
59 r = open_passwd_file(root, fname, &f);
60 if (r == -ENOENT)
61 continue;
62 if (r < 0)
63 return r;
64
65 struct passwd *pw;
66 while ((r = fgetpwent_sane(f, &pw)) > 0) {
67 _cleanup_free_ char *n = NULL;
68
69 n = strdup(pw->pw_name);
70 if (!n)
71 return -ENOMEM;
72
73 r = hashmap_put(cache, n, UID_TO_PTR(pw->pw_uid));
74 if (IN_SET(r, 0, -EEXIST))
75 continue;
76 if (r < 0)
77 return r;
78 TAKE_PTR(n);
79 }
80 }
81
82 *ret = TAKE_PTR(cache);
83 return 0;
84 }
85
86 static int populate_gid_cache(const char *root, Hashmap **ret) {
87 _cleanup_hashmap_free_ Hashmap *cache = NULL;
88 int r;
89
90 assert(ret);
91
92 cache = hashmap_new(&uid_gid_hash_ops);
93 if (!cache)
94 return -ENOMEM;
95
96 FOREACH_STRING(fname, "/etc/group", "/usr/lib/group") {
97 _cleanup_fclose_ FILE *f = NULL;
98
99 r = open_passwd_file(root, fname, &f);
100 if (r == -ENOENT)
101 continue;
102 if (r < 0)
103 return r;
104
105 struct group *gr;
106 while ((r = fgetgrent_sane(f, &gr)) > 0) {
107 _cleanup_free_ char *n = NULL;
108
109 n = strdup(gr->gr_name);
110 if (!n)
111 return -ENOMEM;
112
113 r = hashmap_put(cache, n, GID_TO_PTR(gr->gr_gid));
114 if (IN_SET(r, 0, -EEXIST))
115 continue;
116 if (r < 0)
117 return r;
118 TAKE_PTR(n);
119 }
120 }
121
122 *ret = TAKE_PTR(cache);
123 return 0;
124 }
125
126 int name_to_uid_offline(
127 const char *root,
128 const char *user,
129 uid_t *ret_uid,
130 Hashmap **cache) {
131
132 void *found;
133 int r;
134
135 assert(user);
136 assert(ret_uid);
137 assert(cache);
138
139 if (!*cache) {
140 r = populate_uid_cache(root, cache);
141 if (r < 0)
142 return r;
143 }
144
145 found = hashmap_get(*cache, user);
146 if (!found)
147 return -ESRCH;
148
149 *ret_uid = PTR_TO_UID(found);
150 return 0;
151 }
152
153 int name_to_gid_offline(
154 const char *root,
155 const char *group,
156 gid_t *ret_gid,
157 Hashmap **cache) {
158
159 void *found;
160 int r;
161
162 assert(group);
163 assert(ret_gid);
164 assert(cache);
165
166 if (!*cache) {
167 r = populate_gid_cache(root, cache);
168 if (r < 0)
169 return r;
170 }
171
172 found = hashmap_get(*cache, group);
173 if (!found)
174 return -ESRCH;
175
176 *ret_gid = PTR_TO_GID(found);
177 return 0;
178 }