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