Debian Codegraph @

src/shared/userdb-dropin.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-json.h"
4
5 #include "alloc-util.h"
6 #include "errno-util.h"
7 #include "fd-util.h"
8 #include "fileio.h"
9 #include "format-util.h"
10 #include "group-record.h"
11 #include "path-util.h"
12 #include "stdio-util.h"
13 #include "string-util.h"
14 #include "user-util.h"
15 #include "userdb.h"
16 #include "userdb-dropin.h"
17
18 static int load_user(
19 FILE *f,
20 const char *path,
21 const char *name,
22 uid_t uid,
23 UserDBFlags flags,
24 UserRecord **ret) {
25
26 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
27 _cleanup_(user_record_unrefp) UserRecord *u = NULL;
28 bool have_privileged;
29 int r;
30
31 assert(f);
32
33 r = sd_json_parse_file(f, path, 0, &v, NULL, NULL);
34 if (r < 0)
35 return r;
36
37 if (FLAGS_SET(flags, USERDB_SUPPRESS_SHADOW) || !path || !(name || uid_is_valid(uid)))
38 have_privileged = false;
39 else {
40 _cleanup_(sd_json_variant_unrefp) sd_json_variant *privileged_v = NULL;
41 _cleanup_free_ char *d = NULL, *j = NULL;
42
43 /* Let's load the "privileged" section from a companion file. But only if USERDB_AVOID_SHADOW
44 * is not set. After all, the privileged section kinda takes the role of the data from the
45 * shadow file, hence it makes sense to use the same flag here.
46 *
47 * The general assumption is that whoever provides these records makes the .user file
48 * world-readable, but the .privilege file readable to root and the assigned UID only. But we
49 * won't verify that here, as it would be too late. */
50
51 r = path_extract_directory(path, &d);
52 if (r < 0)
53 return r;
54
55 if (name) {
56 j = strjoin(d, "/", name, ".user-privileged");
57 if (!j)
58 return -ENOMEM;
59 } else {
60 assert(uid_is_valid(uid));
61 if (asprintf(&j, "%s/" UID_FMT ".user-privileged", d, uid) < 0)
62 return -ENOMEM;
63 }
64
65 r = sd_json_parse_file(NULL, j, SD_JSON_PARSE_SENSITIVE, &privileged_v, NULL, NULL);
66 if (ERRNO_IS_NEG_PRIVILEGE(r))
67 have_privileged = false;
68 else if (r == -ENOENT)
69 have_privileged = true; /* if the privileged file doesn't exist, we are complete */
70 else if (r < 0)
71 return r;
72 else {
73 r = sd_json_variant_merge_object(&v, privileged_v);
74 if (r < 0)
75 return r;
76
77 have_privileged = true;
78 }
79 }
80
81 u = user_record_new();
82 if (!u)
83 return -ENOMEM;
84
85 r = user_record_load(
86 u, v,
87 USER_RECORD_REQUIRE_REGULAR|
88 USER_RECORD_ALLOW_PER_MACHINE|
89 USER_RECORD_ALLOW_BINDING|
90 USER_RECORD_ALLOW_SIGNATURE|
91 (have_privileged ? USER_RECORD_ALLOW_PRIVILEGED : 0)|
92 USER_RECORD_PERMISSIVE);
93 if (r < 0)
94 return r;
95
96 if (name && !user_record_matches_user_name(u, name))
97 return -EINVAL;
98
99 if (uid_is_valid(uid) && uid != u->uid)
100 return -EINVAL;
101
102 u->incomplete = !have_privileged;
103
104 if (ret)
105 *ret = TAKE_PTR(u);
106
107 return 0;
108 }
109
110 int dropin_user_record_by_name(const char *name, const char *path, UserDBFlags flags, UserRecord **ret) {
111 _cleanup_free_ char *found_path = NULL;
112 _cleanup_fclose_ FILE *f = NULL;
113 int r;
114
115 assert(name);
116
117 if (path) {
118 f = fopen(path, "re");
119 if (!f)
120 return errno == ENOENT ? -ESRCH : -errno; /* We generally want ESRCH to indicate no such user */
121 } else {
122 const char *j;
123
124 j = strjoina(name, ".user");
125 if (!filename_is_valid(j)) /* Doesn't qualify as valid filename? Then it's definitely not provided as a drop-in */
126 return -ESRCH;
127
128 r = search_and_fopen_nulstr(j, "re", NULL, USERDB_DROPIN_DIR_NULSTR("userdb"), &f, &found_path);
129 if (r == -ENOENT)
130 return -ESRCH;
131 if (r < 0)
132 return r;
133
134 path = found_path;
135 }
136
137 return load_user(f, path, name, UID_INVALID, flags, ret);
138 }
139
140 int dropin_user_record_by_uid(uid_t uid, const char *path, UserDBFlags flags, UserRecord **ret) {
141 _cleanup_free_ char *found_path = NULL;
142 _cleanup_fclose_ FILE *f = NULL;
143 int r;
144
145 assert(uid_is_valid(uid));
146
147 if (path) {
148 f = fopen(path, "re");
149 if (!f)
150 return errno == ENOENT ? -ESRCH : -errno;
151 } else {
152 char buf[DECIMAL_STR_MAX(uid_t) + STRLEN(".user") + 1];
153
154 xsprintf(buf, UID_FMT ".user", uid);
155 /* Note that we don't bother to validate this as a filename, as this is generated from a decimal
156 * integer, i.e. is definitely OK as a filename */
157
158 r = search_and_fopen_nulstr(buf, "re", NULL, USERDB_DROPIN_DIR_NULSTR("userdb"), &f, &found_path);
159 if (r == -ENOENT)
160 return -ESRCH;
161 if (r < 0)
162 return r;
163
164 path = found_path;
165 }
166
167 return load_user(f, path, NULL, uid, flags, ret);
168 }
169
170 static int load_group(
171 FILE *f,
172 const char *path,
173 const char *name,
174 gid_t gid,
175 UserDBFlags flags,
176 GroupRecord **ret) {
177
178 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
179 _cleanup_(group_record_unrefp) GroupRecord *g = NULL;
180 bool have_privileged;
181 int r;
182
183 assert(f);
184
185 r = sd_json_parse_file(f, path, 0, &v, NULL, NULL);
186 if (r < 0)
187 return r;
188
189 if (FLAGS_SET(flags, USERDB_SUPPRESS_SHADOW) || !path || !(name || gid_is_valid(gid)))
190 have_privileged = false;
191 else {
192 _cleanup_(sd_json_variant_unrefp) sd_json_variant *privileged_v = NULL;
193 _cleanup_free_ char *d = NULL, *j = NULL;
194
195 r = path_extract_directory(path, &d);
196 if (r < 0)
197 return r;
198
199 if (name) {
200 j = strjoin(d, "/", name, ".group-privileged");
201 if (!j)
202 return -ENOMEM;
203 } else {
204 assert(gid_is_valid(gid));
205 if (asprintf(&j, "%s/" GID_FMT ".group-privileged", d, gid) < 0)
206 return -ENOMEM;
207 }
208
209 r = sd_json_parse_file(NULL, j, SD_JSON_PARSE_SENSITIVE, &privileged_v, NULL, NULL);
210 if (ERRNO_IS_NEG_PRIVILEGE(r))
211 have_privileged = false;
212 else if (r == -ENOENT)
213 have_privileged = true; /* if the privileged file doesn't exist, we are complete */
214 else if (r < 0)
215 return r;
216 else {
217 r = sd_json_variant_merge_object(&v, privileged_v);
218 if (r < 0)
219 return r;
220
221 have_privileged = true;
222 }
223 }
224
225 g = group_record_new();
226 if (!g)
227 return -ENOMEM;
228
229 r = group_record_load(
230 g, v,
231 USER_RECORD_REQUIRE_REGULAR|
232 USER_RECORD_ALLOW_PER_MACHINE|
233 USER_RECORD_ALLOW_BINDING|
234 USER_RECORD_ALLOW_SIGNATURE|
235 (have_privileged ? USER_RECORD_ALLOW_PRIVILEGED : 0)|
236 USER_RECORD_PERMISSIVE);
237 if (r < 0)
238 return r;
239
240 if (name && !group_record_matches_group_name(g, name))
241 return -EINVAL;
242
243 if (gid_is_valid(gid) && gid != g->gid)
244 return -EINVAL;
245
246 g->incomplete = !have_privileged;
247
248 if (ret)
249 *ret = TAKE_PTR(g);
250
251 return 0;
252 }
253
254 int dropin_group_record_by_name(const char *name, const char *path, UserDBFlags flags, GroupRecord **ret) {
255 _cleanup_free_ char *found_path = NULL;
256 _cleanup_fclose_ FILE *f = NULL;
257 int r;
258
259 assert(name);
260
261 if (path) {
262 f = fopen(path, "re");
263 if (!f)
264 return errno == ENOENT ? -ESRCH : -errno;
265 } else {
266 const char *j;
267
268 j = strjoina(name, ".group");
269 if (!filename_is_valid(j)) /* Doesn't qualify as valid filename? Then it's definitely not provided as a drop-in */
270 return -ESRCH;
271
272 r = search_and_fopen_nulstr(j, "re", NULL, USERDB_DROPIN_DIR_NULSTR("userdb"), &f, &found_path);
273 if (r == -ENOENT)
274 return -ESRCH;
275 if (r < 0)
276 return r;
277
278 path = found_path;
279 }
280
281 return load_group(f, path, name, GID_INVALID, flags, ret);
282 }
283
284 int dropin_group_record_by_gid(gid_t gid, const char *path, UserDBFlags flags, GroupRecord **ret) {
285 _cleanup_free_ char *found_path = NULL;
286 _cleanup_fclose_ FILE *f = NULL;
287 int r;
288
289 assert(gid_is_valid(gid));
290
291 if (path) {
292 f = fopen(path, "re");
293 if (!f)
294 return errno == ENOENT ? -ESRCH : -errno;
295 } else {
296 char buf[DECIMAL_STR_MAX(gid_t) + STRLEN(".group") + 1];
297
298 xsprintf(buf, GID_FMT ".group", gid);
299
300 r = search_and_fopen_nulstr(buf, "re", NULL, USERDB_DROPIN_DIR_NULSTR("userdb"), &f, &found_path);
301 if (r == -ENOENT)
302 return -ESRCH;
303 if (r < 0)
304 return r;
305
306 path = found_path;
307 }
308
309 return load_group(f, path, NULL, gid, flags, ret);
310 }