Debian Codegraph @

src/shared/osc-context.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/auxv.h>
4
5 #include "escape.h"
6 #include "format-util.h"
7 #include "hostname-setup.h"
8 #include "id128-util.h"
9 #include "osc-context.h"
10 #include "pidfd-util.h"
11 #include "process-util.h"
12 #include "string-util.h"
13 #include "strv.h"
14 #include "terminal-util.h"
15 #include "user-util.h"
16
17 /* This currently generates open sequences for OSC 3008 types "boot", "container", "vm", "elevate", "chpriv",
18 * "subcontext", "session", "service".
19 *
20 * NB: Not generated by systemd: "remote" (would have to be generated from the SSH client), "app". */
21
22 static int strextend_escaped(char **s, const char *prefix, const char *value) {
23 assert(s);
24 assert(value);
25
26 if (!strextend(s, prefix))
27 return -ENOMEM;
28
29 _cleanup_free_ char *e = xescape(value, ";\\");
30 if (!e)
31 return -ENOMEM;
32
33 if (!strextend(s, e))
34 return -ENOMEM;
35
36 return 0;
37 }
38
39 static int osc_append_identity(char **s) {
40 int r;
41
42 assert(s);
43
44 _cleanup_free_ char *u = getusername_malloc();
45 if (u) {
46 r = strextend_escaped(s, ";user=", u);
47 if (r < 0)
48 return r;
49 }
50
51 _cleanup_free_ char *h = gethostname_malloc();
52 if (h) {
53 r = strextend_escaped(s, ";hostname=", h);
54 if (r < 0)
55 return r;
56 }
57
58 sd_id128_t id;
59 if (sd_id128_get_machine(&id) >= 0) {
60 r = strextendf(s, ";machineid=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id));
61 if (r < 0)
62 return r;
63 }
64
65 if (sd_id128_get_boot(&id) >= 0) {
66 r = strextendf(s, ";bootid=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id));
67 if (r < 0)
68 return r;
69 }
70
71 r = strextendf(s, ";pid=" PID_FMT, getpid_cached());
72 if (r < 0)
73 return r;
74
75 uint64_t pidfdid;
76 if (pidfd_get_inode_id_self_cached(&pidfdid) >= 0) {
77 r = strextendf(s, ";pidfdid=%" PRIu64, pidfdid);
78 if (r < 0)
79 return r;
80 }
81
82 r = strextend_escaped(s, ";comm=", program_invocation_short_name);
83 if (r < 0)
84 return r;
85
86 return 0;
87 }
88
89 static void osc_context_default_id(sd_id128_t *ret_id) {
90
91 /* Usually we only want one context ID per tool. Since we don't want to store the ID let's just hash
92 * one from process credentials */
93
94 struct {
95 const char label[32];
96 uint64_t pidfdid;
97 uint8_t auxval[16];
98 pid_t pid;
99 } data = {
100 .label = "systemd context hash bytes v1",
101 .pid = getpid_cached(),
102 };
103
104 assert(ret_id);
105
106 memcpy(data.auxval, ULONG_TO_PTR(getauxval(AT_RANDOM)), sizeof(data.auxval));
107 (void) pidfd_get_inode_id_self_cached(&data.pidfdid);
108
109 *ret_id = id128_digest(&data, sizeof(data));
110 }
111
112 static int osc_context_intro_raw(sd_id128_t id, char **ret_seq) {
113 int r;
114
115 assert(ret_seq);
116
117 _cleanup_free_ char *seq = NULL;
118 if (asprintf(&seq, ANSI_OSC "3008;start=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id)) < 0)
119 return -ENOMEM;
120
121 r = osc_append_identity(&seq);
122 if (r < 0)
123 return r;
124
125 *ret_seq = TAKE_PTR(seq);
126 return 0;
127 }
128
129 static int osc_context_intro(char **ret_seq, sd_id128_t *ret_context_id) {
130 int r;
131
132 assert(ret_seq);
133
134 /* If the user passed us a buffer for the context ID generate a randomized one, since we have a place
135 * to store it. The user should pass the ID back to osc_context_close() later on. If the user did not
136 * pass us a buffer, we'll use a session ID hashed from process properties that remain stable as long
137 * our process exists. It hence also remains stable across reexec and similar. */
138 sd_id128_t id;
139 if (ret_context_id) {
140 r = sd_id128_randomize(&id);
141 if (r < 0)
142 return r;
143 } else
144 osc_context_default_id(&id);
145
146 r = osc_context_intro_raw(id, ret_seq);
147 if (r < 0)
148 return r;
149
150 if (ret_context_id)
151 *ret_context_id = id;
152
153 return 0;
154 }
155
156 static int osc_context_outro(char *_seq, sd_id128_t id, char **ret_seq, sd_id128_t *ret_context_id) {
157 _cleanup_free_ char *seq = TAKE_PTR(_seq); /* We take possession of the string no matter what */
158
159 if (ret_seq)
160 *ret_seq = TAKE_PTR(seq);
161 else {
162 fputs(seq, stdout);
163 fflush(stdout);
164 }
165
166 if (ret_context_id)
167 *ret_context_id = id;
168
169 return 0;
170 }
171
172 int osc_context_open_boot(char **ret_seq) {
173 int r;
174
175 _cleanup_free_ char *seq = NULL;
176 r = osc_context_intro(&seq, /* ret_context_id= */ NULL);
177 if (r < 0)
178 return r;
179
180 if (!strextend(&seq, ";type=boot" ANSI_ST))
181 return -ENOMEM;
182
183 return osc_context_outro(TAKE_PTR(seq), /* context_id= */ SD_ID128_NULL, ret_seq, /* ret_context_id= */ NULL);
184 }
185
186 int osc_context_open_container(const char *name, char **ret_seq, sd_id128_t *ret_context_id) {
187 int r;
188
189 _cleanup_free_ char *seq = NULL;
190 sd_id128_t id = SD_ID128_NULL;
191 r = osc_context_intro(&seq, ret_context_id ? &id : NULL);
192 if (r < 0)
193 return r;
194
195 if (name) {
196 r = strextend_escaped(&seq, ";container=", name);
197 if (r < 0)
198 return r;
199 }
200
201 if (!strextend(&seq, ";type=container" ANSI_ST))
202 return -ENOMEM;
203
204 return osc_context_outro(TAKE_PTR(seq), id, ret_seq, ret_context_id);
205 }
206
207 int osc_context_open_vm(const char *name, char **ret_seq, sd_id128_t *ret_context_id) {
208 int r;
209
210 assert(name);
211
212 _cleanup_free_ char *seq = NULL;
213 sd_id128_t id = SD_ID128_NULL;
214 r = osc_context_intro(&seq, ret_context_id ? &id : NULL);
215 if (r < 0)
216 return r;
217
218 r = strextend_escaped(&seq, ";vm=", name);
219 if (r < 0)
220 return r;
221
222 if (!strextend(&seq, ";type=vm" ANSI_ST))
223 return -ENOMEM;
224
225 return osc_context_outro(TAKE_PTR(seq), id, ret_seq, ret_context_id);
226 }
227
228 int osc_context_open_chpriv(const char *target_user, char **ret_seq, sd_id128_t *ret_context_id) {
229 int r;
230
231 assert(target_user);
232
233 _cleanup_free_ char *seq = NULL;
234 sd_id128_t id = SD_ID128_NULL;
235 r = osc_context_intro(&seq, ret_context_id ? &id : NULL);
236 if (r < 0)
237 return r;
238
239 if (is_this_me(target_user) > 0) {
240 if (!strextend(&seq, ";type=subcontext" ANSI_ST))
241 return -ENOMEM;
242 } else if (STR_IN_SET(target_user, "root", "0")) {
243 if (!strextend(&seq, ";type=elevate" ANSI_ST))
244 return -ENOMEM;
245 } else {
246 r = strextend_escaped(&seq, ";targetuser=", target_user);
247 if (r < 0)
248 return r;
249
250 if (!strextend(&seq, ";type=chpriv" ANSI_ST))
251 return -ENOMEM;
252 }
253
254 return osc_context_outro(TAKE_PTR(seq), id, ret_seq, ret_context_id);
255 }
256
257 int osc_context_open_session(const char *user, const char *session_id, char **ret_seq, sd_id128_t *ret_context_id) {
258 int r;
259
260 _cleanup_free_ char *seq = NULL;
261 sd_id128_t id = SD_ID128_NULL;
262 r = osc_context_intro(&seq, ret_context_id ? &id : NULL);
263 if (r < 0)
264 return r;
265
266 if (user) {
267 r = strextend_escaped(&seq, ";targetuser=", user);
268 if (r < 0)
269 return r;
270 }
271
272 if (session_id) {
273 r = strextend_escaped(&seq, ";sessionid=", session_id);
274 if (r < 0)
275 return r;
276 }
277
278 if (!strextend(&seq, ";type=session" ANSI_ST))
279 return -ENOMEM;
280
281 return osc_context_outro(TAKE_PTR(seq), id, ret_seq, ret_context_id);
282 }
283
284 int osc_context_open_service(const char *unit, sd_id128_t invocation_id, char **ret_seq) {
285 int r;
286
287 sd_id128_t id = SD_ID128_NULL;
288 r = osc_context_id_from_invocation_id(invocation_id, &id);
289 if (r < 0)
290 return r;
291
292 _cleanup_free_ char *seq = NULL;
293 r = osc_context_intro_raw(id, &seq);
294 if (r < 0)
295 return r;
296
297 if (unit) {
298 r = strextend_escaped(&seq, ";servicename=", unit);
299 if (r < 0)
300 return r;
301 }
302
303 r = strextendf(&seq, ";invocationid=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(invocation_id));
304 if (r < 0)
305 return r;
306
307 if (!strextend(&seq, ";type=service" ANSI_ST))
308 return -ENOMEM;
309
310 return osc_context_outro(TAKE_PTR(seq), id, ret_seq, /* ret_context_id= */ NULL);
311 }
312
313 int osc_context_close(sd_id128_t id, char **ret_seq) {
314
315 if (sd_id128_is_null(id)) {
316 /* nil uuid: no session opened */
317 if (ret_seq)
318 *ret_seq = NULL;
319 return 0;
320 }
321
322 if (sd_id128_is_allf(id)) /* max uuid: default session opened */
323 osc_context_default_id(&id);
324
325 _cleanup_free_ char *seq = NULL;
326 if (asprintf(&seq, ANSI_OSC "3008;end=" SD_ID128_FORMAT_STR ANSI_ST, SD_ID128_FORMAT_VAL(id)) < 0)
327 return -ENOMEM;
328
329 return osc_context_outro(TAKE_PTR(seq), SD_ID128_NULL, ret_seq, /* ret_context_id= */ NULL);
330 }
331
332 int osc_context_id_from_invocation_id(sd_id128_t id, sd_id128_t *ret) {
333 static const sd_id128_t app_id = SD_ID128_MAKE(5d,63,a5,8d,96,fd,45,d0,a0,f0,63,50,fc,d8,9a,cd);
334
335 assert(ret);
336
337 /* Hash the context id from the invocation id, so that we don't have to store the context id (as long
338 * as we have the invocation ID). We don't use the invocation ID literally in order not to make it too
339 * easy to cancel our service context because the invocation ID is reported literally via the
340 * $INVOCATION_ID env var. */
341 return sd_id128_get_app_specific(id, app_id, ret);
342 }