1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "alloc-util.h"
11 #include "hexdecoct.h"
13 #include "id128-util.h"
16 #include "missing_syscall.h"
17 #include "random-util.h"
18 #include "stat-util.h"
19 #include "user-util.h"
21 _public_
char *sd_id128_to_string(sd_id128_t id
, char s
[_SD_ARRAY_STATIC SD_ID128_STRING_MAX
]) {
24 assert_return(s
, NULL
);
26 for (size_t n
= 0; n
< sizeof(sd_id128_t
); n
++) {
27 s
[k
++] = hexchar(id
.bytes
[n
] >> 4);
28 s
[k
++] = hexchar(id
.bytes
[n
] & 0xF);
31 assert(k
== SD_ID128_STRING_MAX
- 1);
37 _public_
char *sd_id128_to_uuid_string(sd_id128_t id
, char s
[_SD_ARRAY_STATIC SD_ID128_UUID_STRING_MAX
]) {
40 assert_return(s
, NULL
);
42 /* Similar to sd_id128_to_string() but formats the result as UUID instead of plain hex chars */
44 for (size_t n
= 0; n
< sizeof(sd_id128_t
); n
++) {
46 if (IN_SET(n
, 4, 6, 8, 10))
49 s
[k
++] = hexchar(id
.bytes
[n
] >> 4);
50 s
[k
++] = hexchar(id
.bytes
[n
] & 0xF);
53 assert(k
== SD_ID128_UUID_STRING_MAX
- 1);
59 _public_
int sd_id128_from_string(const char *s
, sd_id128_t
*ret
) {
64 assert_return(s
, -EINVAL
);
66 for (n
= 0, i
= 0; n
< sizeof(sd_id128_t
);) {
70 /* Is this a GUID? Then be nice, and skip over
75 else if (IN_SET(i
, 13, 18, 23)) {
85 a
= unhexchar(s
[i
++]);
89 b
= unhexchar(s
[i
++]);
93 t
.bytes
[n
++] = (a
<< 4) | b
;
96 if (i
!= (is_guid
? SD_ID128_UUID_STRING_MAX
: SD_ID128_STRING_MAX
) - 1)
107 _public_
int sd_id128_string_equal(const char *s
, sd_id128_t id
) {
114 /* Checks if the specified string matches a valid string representation of the specified 128 bit ID/uuid */
116 r
= sd_id128_from_string(s
, &parsed
);
120 return sd_id128_equal(parsed
, id
);
123 _public_
int sd_id128_get_machine(sd_id128_t
*ret
) {
124 static thread_local sd_id128_t saved_machine_id
= {};
127 if (sd_id128_is_null(saved_machine_id
)) {
128 r
= id128_read("/etc/machine-id", ID128_FORMAT_PLAIN
, &saved_machine_id
);
132 if (sd_id128_is_null(saved_machine_id
))
137 *ret
= saved_machine_id
;
141 _public_
int sd_id128_get_boot(sd_id128_t
*ret
) {
142 static thread_local sd_id128_t saved_boot_id
= {};
145 if (sd_id128_is_null(saved_boot_id
)) {
146 r
= id128_read("/proc/sys/kernel/random/boot_id", ID128_FORMAT_UUID
, &saved_boot_id
);
147 if (r
== -ENOENT
&& proc_mounted() == 0)
152 if (sd_id128_is_null(saved_boot_id
))
157 *ret
= saved_boot_id
;
161 static int get_invocation_from_keyring(sd_id128_t
*ret
) {
162 _cleanup_free_
char *description
= NULL
;
163 char *d
, *p
, *g
, *u
, *e
;
171 #define MAX_PERMS ((unsigned long) (KEY_POS_VIEW|KEY_POS_READ|KEY_POS_SEARCH| \
172 KEY_USR_VIEW|KEY_USR_READ|KEY_USR_SEARCH))
176 key
= request_key("user", "invocation_id", NULL
, 0);
178 /* Keyring support not available? No invocation key stored? */
179 if (IN_SET(errno
, ENOSYS
, ENOKEY
))
186 description
= new(char, sz
);
190 c
= keyctl(KEYCTL_DESCRIBE
, key
, (unsigned long) description
, sz
, 0);
194 if ((size_t) c
<= sz
)
201 /* The kernel returns a final NUL in the string, verify that. */
202 assert(description
[c
-1] == 0);
204 /* Chop off the final description string */
205 d
= strrchr(description
, ';');
210 /* Look for the permissions */
211 p
= strrchr(description
, ';');
216 perms
= strtoul(p
+ 1, &e
, 16);
219 if (e
== p
+ 1) /* Read at least one character */
221 if (e
!= d
) /* Must reached the end */
224 if ((perms
& ~MAX_PERMS
) != 0)
229 /* Look for the group ID */
230 g
= strrchr(description
, ';');
233 r
= parse_gid(g
+ 1, &gid
);
240 /* Look for the user ID */
241 u
= strrchr(description
, ';');
244 r
= parse_uid(u
+ 1, &uid
);
250 c
= keyctl(KEYCTL_READ
, key
, (unsigned long) ret
, sizeof(sd_id128_t
), 0);
253 if (c
!= sizeof(sd_id128_t
))
259 static int get_invocation_from_environment(sd_id128_t
*ret
) {
265 e
= secure_getenv("INVOCATION_ID");
269 r
= sd_id128_from_string(e
, ret
);
270 return r
== -EINVAL
? -EUCLEAN
: r
;
273 _public_
int sd_id128_get_invocation(sd_id128_t
*ret
) {
274 static thread_local sd_id128_t saved_invocation_id
= {};
277 if (sd_id128_is_null(saved_invocation_id
)) {
278 /* We first check the environment. The environment variable is primarily relevant for user
279 * services, and sufficiently safe as long as no privilege boundary is involved. */
280 r
= get_invocation_from_environment(&saved_invocation_id
);
282 /* The kernel keyring is relevant for system services (as for user services we don't
283 * store the invocation ID in the keyring, as there'd be no trust benefit in that). */
284 r
= get_invocation_from_keyring(&saved_invocation_id
);
290 *ret
= saved_invocation_id
;
294 _public_
int sd_id128_randomize(sd_id128_t
*ret
) {
297 assert_return(ret
, -EINVAL
);
299 random_bytes(&t
, sizeof(t
));
301 /* Turn this into a valid v4 UUID, to be nice. Note that we
302 * only guarantee this for newly generated UUIDs, not for
303 * pre-existing ones. */
305 *ret
= id128_make_v4_uuid(t
);
309 static int get_app_specific(sd_id128_t base
, sd_id128_t app_id
, sd_id128_t
*ret
) {
310 uint8_t hmac
[SHA256_DIGEST_SIZE
];
315 hmac_sha256(&base
, sizeof(base
), &app_id
, sizeof(app_id
), hmac
);
317 /* Take only the first half. */
318 memcpy(&result
, hmac
, MIN(sizeof(hmac
), sizeof(result
)));
320 *ret
= id128_make_v4_uuid(result
);
324 _public_
int sd_id128_get_machine_app_specific(sd_id128_t app_id
, sd_id128_t
*ret
) {
328 assert_return(ret
, -EINVAL
);
330 r
= sd_id128_get_machine(&id
);
334 return get_app_specific(id
, app_id
, ret
);
337 _public_
int sd_id128_get_boot_app_specific(sd_id128_t app_id
, sd_id128_t
*ret
) {
341 assert_return(ret
, -EINVAL
);
343 r
= sd_id128_get_boot(&id
);
347 return get_app_specific(id
, app_id
, ret
);