]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/specifier.c
shared/specifier: provide proper error messages when specifiers fail to read files
[thirdparty/systemd.git] / src / shared / specifier.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <stdlib.h>
7 #include <sys/utsname.h>
8
9 #include "sd-id128.h"
10
11 #include "alloc-util.h"
12 #include "architecture.h"
13 #include "chase-symlinks.h"
14 #include "fd-util.h"
15 #include "format-util.h"
16 #include "fs-util.h"
17 #include "hostname-util.h"
18 #include "id128-util.h"
19 #include "macro.h"
20 #include "os-util.h"
21 #include "path-util.h"
22 #include "specifier.h"
23 #include "string-util.h"
24 #include "strv.h"
25 #include "user-util.h"
26
27 /*
28 * Generic infrastructure for replacing %x style specifiers in
29 * strings. Will call a callback for each replacement.
30 */
31
32 /* Any ASCII character or digit: our pool of potential specifiers,
33 * and "%" used for escaping. */
34 #define POSSIBLE_SPECIFIERS ALPHANUMERICAL "%"
35
36 int specifier_printf(const char *text, size_t max_length, const Specifier table[], const char *root, const void *userdata, char **ret) {
37 _cleanup_free_ char *result = NULL;
38 bool percent = false;
39 size_t l;
40 char *t;
41 int r;
42
43 assert(ret);
44 assert(text);
45 assert(table);
46
47 l = strlen(text);
48 if (!GREEDY_REALLOC(result, l + 1))
49 return -ENOMEM;
50 t = result;
51
52 for (const char *f = text; *f != '\0'; f++, l--) {
53 if (percent) {
54 percent = false;
55
56 if (*f == '%')
57 *(t++) = '%';
58 else {
59 const Specifier *i;
60
61 for (i = table; i->specifier; i++)
62 if (i->specifier == *f)
63 break;
64
65 if (i->lookup) {
66 _cleanup_free_ char *w = NULL;
67 size_t k, j;
68
69 r = i->lookup(i->specifier, i->data, root, userdata, &w);
70 if (r < 0)
71 return r;
72 if (isempty(w))
73 continue;
74
75 j = t - result;
76 k = strlen(w);
77
78 if (!GREEDY_REALLOC(result, j + k + l + 1))
79 return -ENOMEM;
80 memcpy(result + j, w, k);
81 t = result + j + k;
82 } else if (strchr(POSSIBLE_SPECIFIERS, *f))
83 /* Oops, an unknown specifier. */
84 return -EBADSLT;
85 else {
86 *(t++) = '%';
87 *(t++) = *f;
88 }
89 }
90 } else if (*f == '%')
91 percent = true;
92 else
93 *(t++) = *f;
94
95 if ((size_t) (t - result) > max_length)
96 return -ENAMETOOLONG;
97 }
98
99 /* If string ended with a stray %, also end with % */
100 if (percent) {
101 *(t++) = '%';
102 if ((size_t) (t - result) > max_length)
103 return -ENAMETOOLONG;
104 }
105 *(t++) = 0;
106
107 *ret = TAKE_PTR(result);
108 return 0;
109 }
110
111 /* Generic handler for simple string replacements */
112
113 int specifier_string(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
114 char *n = NULL;
115
116 assert(ret);
117
118 if (!isempty(data)) {
119 n = strdup(data);
120 if (!n)
121 return -ENOMEM;
122 }
123
124 *ret = n;
125 return 0;
126 }
127
128 int specifier_real_path(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
129 const char *path = data;
130
131 assert(ret);
132
133 if (!path)
134 return -ENOENT;
135
136 return chase_symlinks(path, root, 0, ret, NULL);
137 }
138
139 int specifier_real_directory(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
140 _cleanup_free_ char *path = NULL;
141 int r;
142
143 assert(ret);
144
145 r = specifier_real_path(specifier, data, root, userdata, &path);
146 if (r < 0)
147 return r;
148
149 assert(path);
150 return path_extract_directory(path, ret);
151 }
152
153 int specifier_machine_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
154 sd_id128_t id;
155 char *n;
156 int r;
157
158 assert(ret);
159
160 if (root) {
161 _cleanup_close_ int fd = -1;
162
163 fd = chase_symlinks_and_open("/etc/machine-id", root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL);
164 if (fd < 0)
165 /* Translate error for missing os-release file to EUNATCH. */
166 return fd == -ENOENT ? -EUNATCH : fd;
167
168 r = id128_read_fd(fd, ID128_PLAIN, &id);
169 } else
170 r = sd_id128_get_machine(&id);
171 if (r < 0)
172 return r;
173
174 n = new(char, SD_ID128_STRING_MAX);
175 if (!n)
176 return -ENOMEM;
177
178 *ret = sd_id128_to_string(id, n);
179 return 0;
180 }
181
182 int specifier_boot_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
183 sd_id128_t id;
184 char *n;
185 int r;
186
187 assert(ret);
188
189 r = sd_id128_get_boot(&id);
190 if (r < 0)
191 return r;
192
193 n = new(char, SD_ID128_STRING_MAX);
194 if (!n)
195 return -ENOMEM;
196
197 *ret = sd_id128_to_string(id, n);
198 return 0;
199 }
200
201 int specifier_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
202 char *n;
203
204 assert(ret);
205
206 n = gethostname_malloc();
207 if (!n)
208 return -ENOMEM;
209
210 *ret = n;
211 return 0;
212 }
213
214 int specifier_short_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
215 char *n;
216
217 assert(ret);
218
219 n = gethostname_short_malloc();
220 if (!n)
221 return -ENOMEM;
222
223 *ret = n;
224 return 0;
225 }
226
227 int specifier_pretty_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
228 char *n = NULL;
229
230 assert(ret);
231
232 if (get_pretty_hostname(&n) < 0) {
233 n = gethostname_short_malloc();
234 if (!n)
235 return -ENOMEM;
236 }
237
238 *ret = n;
239 return 0;
240 }
241
242 int specifier_kernel_release(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
243 struct utsname uts;
244 char *n;
245
246 assert(ret);
247
248 if (uname(&uts) < 0)
249 return -errno;
250
251 n = strdup(uts.release);
252 if (!n)
253 return -ENOMEM;
254
255 *ret = n;
256 return 0;
257 }
258
259 int specifier_architecture(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
260 char *t;
261
262 assert(ret);
263
264 t = strdup(architecture_to_string(uname_architecture()));
265 if (!t)
266 return -ENOMEM;
267
268 *ret = t;
269 return 0;
270 }
271
272 /* Note: fields in /etc/os-release might quite possibly be missing, even if everything is entirely valid
273 * otherwise. We'll return an empty value or NULL in that case from the functions below. But if the
274 * os-release file is missing, we'll return -EUNATCH. This means that something is seriously wrong with the
275 * installation. */
276
277 static int parse_os_release_specifier(const char *root, const char *id, char **ret) {
278 int r;
279
280 assert(ret);
281
282 /* Translate error for missing os-release file to EUNATCH. */
283 r = parse_os_release(root, id, ret);
284 return r == -ENOENT ? -EUNATCH : r;
285 }
286
287 int specifier_os_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
288 return parse_os_release_specifier(root, "ID", ret);
289 }
290
291 int specifier_os_version_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
292 return parse_os_release_specifier(root, "VERSION_ID", ret);
293 }
294
295 int specifier_os_build_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
296 return parse_os_release_specifier(root, "BUILD_ID", ret);
297 }
298
299 int specifier_os_variant_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
300 return parse_os_release_specifier(root, "VARIANT_ID", ret);
301 }
302
303 int specifier_os_image_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
304 return parse_os_release_specifier(root, "IMAGE_ID", ret);
305 }
306
307 int specifier_os_image_version(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
308 return parse_os_release_specifier(root, "IMAGE_VERSION", ret);
309 }
310
311 int specifier_group_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
312 char *t;
313
314 assert(ret);
315
316 t = gid_to_name(getgid());
317 if (!t)
318 return -ENOMEM;
319
320 *ret = t;
321 return 0;
322 }
323
324 int specifier_group_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
325 assert(ret);
326
327 if (asprintf(ret, UID_FMT, getgid()) < 0)
328 return -ENOMEM;
329
330 return 0;
331 }
332
333 int specifier_user_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
334 char *t;
335
336 assert(ret);
337
338 /* If we are UID 0 (root), this will not result in NSS, otherwise it might. This is good, as we want to be able
339 * to run this in PID 1, where our user ID is 0, but where NSS lookups are not allowed.
340
341 * We don't use getusername_malloc() here, because we don't want to look at $USER, to remain consistent with
342 * specifer_user_id() below.
343 */
344
345 t = uid_to_name(getuid());
346 if (!t)
347 return -ENOMEM;
348
349 *ret = t;
350 return 0;
351 }
352
353 int specifier_user_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
354 assert(ret);
355
356 if (asprintf(ret, UID_FMT, getuid()) < 0)
357 return -ENOMEM;
358
359 return 0;
360 }
361
362 int specifier_user_home(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
363 assert(ret);
364
365 /* On PID 1 (which runs as root) this will not result in NSS,
366 * which is good. See above */
367
368 return get_home_dir(ret);
369 }
370
371 int specifier_user_shell(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
372 assert(ret);
373
374 /* On PID 1 (which runs as root) this will not result in NSS,
375 * which is good. See above */
376
377 return get_shell(ret);
378 }
379
380 int specifier_tmp_dir(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
381 const char *p;
382 char *copy;
383 int r;
384
385 assert(ret);
386
387 if (root) /* If root dir is set, don't honour $TMP or similar */
388 p = "/tmp";
389 else {
390 r = tmp_dir(&p);
391 if (r < 0)
392 return r;
393 }
394 copy = strdup(p);
395 if (!copy)
396 return -ENOMEM;
397
398 *ret = copy;
399 return 0;
400 }
401
402 int specifier_var_tmp_dir(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
403 const char *p;
404 char *copy;
405 int r;
406
407 assert(ret);
408
409 if (root)
410 p = "/var/tmp";
411 else {
412 r = var_tmp_dir(&p);
413 if (r < 0)
414 return r;
415 }
416 copy = strdup(p);
417 if (!copy)
418 return -ENOMEM;
419
420 *ret = copy;
421 return 0;
422 }
423
424 int specifier_escape_strv(char **l, char ***ret) {
425 char **z, **p, **q;
426
427 assert(ret);
428
429 if (strv_isempty(l)) {
430 *ret = NULL;
431 return 0;
432 }
433
434 z = new(char*, strv_length(l)+1);
435 if (!z)
436 return -ENOMEM;
437
438 for (p = l, q = z; *p; p++, q++) {
439
440 *q = specifier_escape(*p);
441 if (!*q) {
442 strv_free(z);
443 return -ENOMEM;
444 }
445 }
446
447 *q = NULL;
448 *ret = z;
449
450 return 0;
451 }
452
453 const Specifier system_and_tmp_specifier_table[] = {
454 COMMON_SYSTEM_SPECIFIERS,
455 COMMON_TMP_SPECIFIERS,
456 {}
457 };