Debian Codegraph @

src/core/dbus-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "bus-polkit.h"
4 #include "dbus-util.h"
5 #include "dissect-image.h"
6 #include "escape.h"
7 #include "manager.h"
8 #include "path-util.h"
9 #include "reboot-util.h"
10 #include "strv.h"
11 #include "unit.h"
12 #include "user-util.h"
13
14 int bus_property_get_triggered_unit(
15 sd_bus *bus,
16 const char *path,
17 const char *interface,
18 const char *property,
19 sd_bus_message *reply,
20 void *userdata,
21 sd_bus_error *reterr_error) {
22
23 Unit *u = userdata, *trigger;
24
25 assert(bus);
26 assert(reply);
27 assert(u);
28
29 trigger = UNIT_TRIGGER(u);
30
31 return sd_bus_message_append(reply, "s", trigger ? trigger->id : NULL);
32 }
33
34 BUS_DEFINE_SET_TRANSIENT(mode_t, "u", uint32_t, mode_t, "%04o");
35 BUS_DEFINE_SET_TRANSIENT(unsigned, "u", uint32_t, unsigned, "%" PRIu32);
36
37 static bool valid_user_group_name_or_id_relaxed(const char *u) {
38 return valid_user_group_name(u, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX);
39 }
40
41 BUS_DEFINE_SET_TRANSIENT_STRING_WITH_CHECK(user_relaxed, valid_user_group_name_or_id_relaxed);
42 BUS_DEFINE_SET_TRANSIENT_STRING_WITH_CHECK(path, path_is_absolute);
43 BUS_DEFINE_SET_TRANSIENT_STRING_WITH_CHECK(reboot_parameter, reboot_parameter_is_valid);
44
45 int bus_set_transient_string(
46 Unit *u,
47 const char *name,
48 char **p,
49 sd_bus_message *message,
50 UnitWriteFlags flags,
51 sd_bus_error *reterr_error) {
52
53 const char *v;
54 int r;
55
56 assert(p);
57
58 r = sd_bus_message_read(message, "s", &v);
59 if (r < 0)
60 return r;
61
62 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
63 r = free_and_strdup(p, empty_to_null(v));
64 if (r < 0)
65 return r;
66
67 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name,
68 "%s=%s", name, strempty(v));
69 }
70
71 return 1;
72 }
73
74 int bus_set_transient_bool(
75 Unit *u,
76 const char *name,
77 bool *p,
78 sd_bus_message *message,
79 UnitWriteFlags flags,
80 sd_bus_error *reterr_error) {
81
82 int v, r;
83
84 assert(p);
85
86 r = sd_bus_message_read(message, "b", &v);
87 if (r < 0)
88 return r;
89
90 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
91 *p = v;
92 unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(v));
93 }
94
95 return 1;
96 }
97
98 int bus_set_transient_tristate(
99 Unit *u,
100 const char *name,
101 int *p,
102 sd_bus_message *message,
103 UnitWriteFlags flags,
104 sd_bus_error *reterr_error) {
105
106 int v, r;
107
108 assert(p);
109
110 r = sd_bus_message_read(message, "b", &v);
111 if (r < 0)
112 return r;
113
114 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
115 *p = v;
116 unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(v));
117 }
118
119 return 1;
120 }
121
122 static int bus_set_transient_usec_internal(
123 Unit *u,
124 const char *name,
125 usec_t *p,
126 bool fix_0,
127 sd_bus_message *message,
128 UnitWriteFlags flags,
129 sd_bus_error *reterr_error) {
130
131 uint64_t v;
132 int r;
133
134 assert(p);
135
136 r = sd_bus_message_read(message, "t", &v);
137 if (r < 0)
138 return r;
139
140 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
141 if (fix_0)
142 *p = v != 0 ? v: USEC_INFINITY;
143 else
144 *p = v;
145
146 char *n = strndupa_safe(name, strlen(name) - 4);
147 unit_write_settingf(u, flags, name, "%sSec=%s", n, FORMAT_TIMESPAN(v, USEC_PER_MSEC));
148 }
149
150 return 1;
151 }
152
153 int bus_set_transient_usec(Unit *u, const char *name, usec_t *p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *reterr_error) {
154 return bus_set_transient_usec_internal(u, name, p, false, message, flags, reterr_error);
155 }
156
157 int bus_set_transient_usec_fix_0(Unit *u, const char *name, usec_t *p, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *reterr_error) {
158 return bus_set_transient_usec_internal(u, name, p, true, message, flags, reterr_error);
159 }
160
161 int bus_verify_manage_units_async_impl(
162 Manager *manager,
163 const char *id,
164 const char *verb,
165 const char *polkit_message,
166 sd_bus_message *call,
167 sd_bus_error *reterr_error) {
168
169 const char *details[9];
170 size_t n_details = 0;
171
172 assert(manager);
173 assert(call);
174
175 if (id) {
176 details[n_details++] = "unit";
177 details[n_details++] = id;
178 }
179
180 if (verb) {
181 details[n_details++] = "verb";
182 details[n_details++] = verb;
183 };
184
185 if (polkit_message) {
186 details[n_details++] = "polkit.message";
187 details[n_details++] = polkit_message;
188 details[n_details++] = "polkit.gettext_domain";
189 details[n_details++] = GETTEXT_PACKAGE;
190 }
191
192 assert(n_details < ELEMENTSOF(details));
193 details[n_details] = NULL;
194
195 return bus_verify_polkit_async(
196 call,
197 "org.freedesktop.systemd1.manage-units",
198 n_details > 0 ? details : NULL,
199 &manager->polkit_registry,
200 reterr_error);
201 }
202
203 int bus_verify_manage_units_async_full(Unit *u, const char *verb, const char *polkit_message, sd_bus_message *call, sd_bus_error *reterr_error) {
204 assert(u);
205 return bus_verify_manage_units_async_impl(u->manager, u->id, verb, polkit_message, call, reterr_error);
206 }
207
208 int bus_verify_manage_units_async(Manager *manager, sd_bus_message *call, sd_bus_error *reterr_error) {
209 return bus_verify_manage_units_async_impl(manager, NULL, NULL, NULL, call, reterr_error);
210 }
211
212 int bus_verify_manage_unit_files_async(Manager *m, sd_bus_message *call, sd_bus_error *reterr_error) {
213 assert(m);
214 assert(call);
215
216 return bus_verify_polkit_async(
217 call,
218 "org.freedesktop.systemd1.manage-unit-files",
219 /* details= */ NULL,
220 &m->polkit_registry,
221 reterr_error);
222 }
223
224 int bus_verify_reload_daemon_async(Manager *m, sd_bus_message *call, sd_bus_error *reterr_error) {
225 assert(m);
226 assert(call);
227
228 return bus_verify_polkit_async(
229 call,
230 "org.freedesktop.systemd1.reload-daemon",
231 /* details= */ NULL,
232 &m->polkit_registry, reterr_error);
233 }
234
235 int bus_verify_set_environment_async(Manager *m, sd_bus_message *call, sd_bus_error *reterr_error) {
236 assert(m);
237 assert(call);
238
239 return bus_verify_polkit_async(
240 call,
241 "org.freedesktop.systemd1.set-environment",
242 /* details= */ NULL,
243 &m->polkit_registry,
244 reterr_error);
245 }
246
247 int bus_verify_bypass_dump_ratelimit_async(Manager *m, sd_bus_message *call, sd_bus_error *reterr_error) {
248 assert(m);
249 assert(call);
250
251 return bus_verify_polkit_async(
252 call,
253 "org.freedesktop.systemd1.bypass-dump-ratelimit",
254 /* details= */ NULL,
255 &m->polkit_registry,
256 reterr_error);
257 }
258
259 /* in_out_format_str is an accumulator, so if it has any pre-existing content it will be read, and new
260 * options will be appended to it */
261 int bus_read_mount_options(
262 sd_bus_message *message,
263 sd_bus_error *reterr_error,
264 MountOptions **ret_options,
265 char **in_out_format_str,
266 const char *separator) {
267
268 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
269 _cleanup_free_ char *format_str = NULL;
270 int r;
271
272 assert(message);
273 assert(ret_options);
274 assert(!in_out_format_str == !separator);
275
276 r = sd_bus_message_enter_container(message, 'a', "(ss)");
277 if (r < 0)
278 return r;
279
280 const char *partition, *mount_options;
281 while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) {
282 PartitionDesignator partition_designator;
283
284 if (chars_intersect(mount_options, WHITESPACE))
285 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
286 "Invalid mount options string, contains whitespace character(s): %s", mount_options);
287
288 partition_designator = partition_designator_from_string(partition);
289 if (partition_designator < 0)
290 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid partition name %s", partition);
291
292 if (!options) {
293 options = new0(MountOptions, 1);
294 if (!options)
295 return -ENOMEM;
296 }
297
298 r = free_and_strdup(&options->options[partition_designator], mount_options);
299 if (r < 0)
300 return r;
301
302 if (in_out_format_str && !isempty(mount_options)) {
303 /* Need to store the options with the escapes, so that they can be parsed again */
304 _cleanup_free_ char *escaped = NULL;
305
306 escaped = shell_escape(mount_options, ":");
307 if (!escaped)
308 return -ENOMEM;
309
310 r = strextendf_with_separator(&format_str, separator, "%s:%s", partition, escaped);
311 if (r < 0)
312 return r;
313 }
314 }
315 if (r < 0)
316 return r;
317
318 r = sd_bus_message_exit_container(message);
319 if (r < 0)
320 return r;
321
322 if (in_out_format_str && !strextend_with_separator(in_out_format_str, separator, format_str))
323 return -ENOMEM;
324
325 *ret_options = TAKE_PTR(options);
326
327 return 0;
328 }
329
330 int bus_property_get_activation_details(
331 sd_bus *bus,
332 const char *path,
333 const char *interface,
334 const char *property,
335 sd_bus_message *reply,
336 void *userdata,
337 sd_bus_error *reterr_error) {
338
339 ActivationDetails **details = ASSERT_PTR(userdata);
340 _cleanup_strv_free_ char **pairs = NULL;
341 int r;
342
343 assert(reply);
344
345 r = activation_details_append_pair(*details, &pairs);
346 if (r < 0)
347 return r;
348
349 r = sd_bus_message_open_container(reply, 'a', "(ss)");
350 if (r < 0)
351 return r;
352
353 STRV_FOREACH_PAIR(key, value, pairs) {
354 r = sd_bus_message_append(reply, "(ss)", *key, *value);
355 if (r < 0)
356 return r;
357 }
358
359 return sd_bus_message_close_container(reply);
360 }