]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-util.c
Merge pull request #26458 from yuwata/core-network-namespace-remount-sysfs
[thirdparty/systemd.git] / src / core / dbus-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "bus-polkit.h"
4 #include "bus-util.h"
5 #include "dbus-util.h"
6 #include "escape.h"
7 #include "parse-util.h"
8 #include "path-util.h"
9 #include "unit-printf.h"
10 #include "user-util.h"
11 #include "unit.h"
12
13 int bus_property_get_triggered_unit(
14 sd_bus *bus,
15 const char *path,
16 const char *interface,
17 const char *property,
18 sd_bus_message *reply,
19 void *userdata,
20 sd_bus_error *error) {
21
22 Unit *u = userdata, *trigger;
23
24 assert(bus);
25 assert(reply);
26 assert(u);
27
28 trigger = UNIT_TRIGGER(u);
29
30 return sd_bus_message_append(reply, "s", trigger ? trigger->id : NULL);
31 }
32
33 BUS_DEFINE_SET_TRANSIENT(mode_t, "u", uint32_t, mode_t, "%040o");
34 BUS_DEFINE_SET_TRANSIENT(unsigned, "u", uint32_t, unsigned, "%" PRIu32);
35
36 static inline bool valid_user_group_name_or_id_relaxed(const char *u) {
37 return valid_user_group_name(u, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX);
38 }
39
40 BUS_DEFINE_SET_TRANSIENT_STRING_WITH_CHECK(user_relaxed, valid_user_group_name_or_id_relaxed);
41 BUS_DEFINE_SET_TRANSIENT_STRING_WITH_CHECK(path, path_is_absolute);
42
43 int bus_set_transient_string(
44 Unit *u,
45 const char *name,
46 char **p,
47 sd_bus_message *message,
48 UnitWriteFlags flags,
49 sd_bus_error *error) {
50
51 const char *v;
52 int r;
53
54 assert(p);
55
56 r = sd_bus_message_read(message, "s", &v);
57 if (r < 0)
58 return r;
59
60 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
61 r = free_and_strdup(p, empty_to_null(v));
62 if (r < 0)
63 return r;
64
65 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name,
66 "%s=%s", name, strempty(v));
67 }
68
69 return 1;
70 }
71
72 int bus_set_transient_bool(
73 Unit *u,
74 const char *name,
75 bool *p,
76 sd_bus_message *message,
77 UnitWriteFlags flags,
78 sd_bus_error *error) {
79
80 int v, r;
81
82 assert(p);
83
84 r = sd_bus_message_read(message, "b", &v);
85 if (r < 0)
86 return r;
87
88 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
89 *p = v;
90 unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(v));
91 }
92
93 return 1;
94 }
95
96 int bus_set_transient_tristate(
97 Unit *u,
98 const char *name,
99 int *p,
100 sd_bus_message *message,
101 UnitWriteFlags flags,
102 sd_bus_error *error) {
103
104 int v, r;
105
106 assert(p);
107
108 r = sd_bus_message_read(message, "b", &v);
109 if (r < 0)
110 return r;
111
112 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
113 *p = v;
114 unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(v));
115 }
116
117 return 1;
118 }
119
120 int bus_set_transient_usec_internal(
121 Unit *u,
122 const char *name,
123 usec_t *p,
124 bool fix_0,
125 sd_bus_message *message,
126 UnitWriteFlags flags,
127 sd_bus_error *error) {
128
129 uint64_t v;
130 int r;
131
132 assert(p);
133
134 r = sd_bus_message_read(message, "t", &v);
135 if (r < 0)
136 return r;
137
138 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
139 if (fix_0)
140 *p = v != 0 ? v: USEC_INFINITY;
141 else
142 *p = v;
143
144 char *n = strndupa_safe(name, strlen(name) - 4);
145 unit_write_settingf(u, flags, name, "%sSec=%s", n, FORMAT_TIMESPAN(v, USEC_PER_MSEC));
146 }
147
148 return 1;
149 }
150
151 int bus_verify_manage_units_async_full(
152 Unit *u,
153 const char *verb,
154 int capability,
155 const char *polkit_message,
156 bool interactive,
157 sd_bus_message *call,
158 sd_bus_error *error) {
159
160 const char *details[9] = {
161 "unit", u->id,
162 "verb", verb,
163 };
164
165 if (polkit_message) {
166 details[4] = "polkit.message";
167 details[5] = polkit_message;
168 details[6] = "polkit.gettext_domain";
169 details[7] = GETTEXT_PACKAGE;
170 }
171
172 return bus_verify_polkit_async(
173 call,
174 capability,
175 "org.freedesktop.systemd1.manage-units",
176 details,
177 interactive,
178 UID_INVALID,
179 &u->manager->polkit_registry,
180 error);
181 }
182
183 /* ret_format_str is an accumulator, so if it has any pre-existing content, new options will be appended to it */
184 int bus_read_mount_options(
185 sd_bus_message *message,
186 sd_bus_error *error,
187 MountOptions **ret_options,
188 char **ret_format_str,
189 const char *separator) {
190
191 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
192 _cleanup_free_ char *format_str = NULL;
193 const char *mount_options, *partition;
194 int r;
195
196 assert(message);
197 assert(ret_options);
198 assert(separator);
199
200 r = sd_bus_message_enter_container(message, 'a', "(ss)");
201 if (r < 0)
202 return r;
203
204 while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) {
205 _cleanup_free_ char *escaped = NULL;
206 _cleanup_free_ MountOptions *o = NULL;
207 PartitionDesignator partition_designator;
208
209 if (chars_intersect(mount_options, WHITESPACE))
210 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
211 "Invalid mount options string, contains whitespace character(s): %s", mount_options);
212
213 partition_designator = partition_designator_from_string(partition);
214 if (partition_designator < 0)
215 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid partition name %s", partition);
216
217 /* Need to store the options with the escapes, so that they can be parsed again */
218 escaped = shell_escape(mount_options, ":");
219 if (!escaped)
220 return -ENOMEM;
221
222 if (!strextend_with_separator(&format_str, separator, partition, ":", escaped))
223 return -ENOMEM;
224
225 o = new(MountOptions, 1);
226 if (!o)
227 return -ENOMEM;
228 *o = (MountOptions) {
229 .partition_designator = partition_designator,
230 .options = strdup(mount_options),
231 };
232 if (!o->options)
233 return -ENOMEM;
234 LIST_APPEND(mount_options, options, TAKE_PTR(o));
235 }
236 if (r < 0)
237 return r;
238
239 r = sd_bus_message_exit_container(message);
240 if (r < 0)
241 return r;
242
243 if (options) {
244 if (ret_format_str) {
245 char *final = strjoin(*ret_format_str, !isempty(*ret_format_str) ? separator : "", format_str);
246 if (!final)
247 return -ENOMEM;
248 free_and_replace(*ret_format_str, final);
249 }
250 LIST_JOIN(mount_options, *ret_options, options);
251 }
252
253 return 0;
254 }
255
256 int bus_property_get_activation_details(
257 sd_bus *bus,
258 const char *path,
259 const char *interface,
260 const char *property,
261 sd_bus_message *reply,
262 void *userdata,
263 sd_bus_error *error) {
264
265 ActivationDetails **details = ASSERT_PTR(userdata);
266 _cleanup_strv_free_ char **pairs = NULL;
267 int r;
268
269 assert(reply);
270
271 r = activation_details_append_pair(*details, &pairs);
272 if (r < 0)
273 return r;
274
275 r = sd_bus_message_open_container(reply, 'a', "(ss)");
276 if (r < 0)
277 return r;
278
279 STRV_FOREACH_PAIR(key, value, pairs) {
280 r = sd_bus_message_append(reply, "(ss)", *key, *value);
281 if (r < 0)
282 return r;
283 }
284
285 return sd_bus_message_close_container(reply);
286 }