]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-internal.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-internal.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Lennart Poettering
6 ***/
7
8 #include "alloc-util.h"
9 #include "bus-internal.h"
10 #include "bus-message.h"
11 #include "hexdecoct.h"
12 #include "string-util.h"
13
14 bool object_path_is_valid(const char *p) {
15 const char *q;
16 bool slash;
17
18 if (!p)
19 return false;
20
21 if (p[0] != '/')
22 return false;
23
24 if (p[1] == 0)
25 return true;
26
27 for (slash = true, q = p+1; *q; q++)
28 if (*q == '/') {
29 if (slash)
30 return false;
31
32 slash = true;
33 } else {
34 bool good;
35
36 good =
37 (*q >= 'a' && *q <= 'z') ||
38 (*q >= 'A' && *q <= 'Z') ||
39 (*q >= '0' && *q <= '9') ||
40 *q == '_';
41
42 if (!good)
43 return false;
44
45 slash = false;
46 }
47
48 if (slash)
49 return false;
50
51 return true;
52 }
53
54 char* object_path_startswith(const char *a, const char *b) {
55 const char *p;
56
57 if (!object_path_is_valid(a) ||
58 !object_path_is_valid(b))
59 return NULL;
60
61 if (streq(b, "/"))
62 return (char*) a + 1;
63
64 p = startswith(a, b);
65 if (!p)
66 return NULL;
67
68 if (*p == 0)
69 return (char*) p;
70
71 if (*p == '/')
72 return (char*) p + 1;
73
74 return NULL;
75 }
76
77 bool interface_name_is_valid(const char *p) {
78 const char *q;
79 bool dot, found_dot = false;
80
81 if (isempty(p))
82 return false;
83
84 for (dot = true, q = p; *q; q++)
85 if (*q == '.') {
86 if (dot)
87 return false;
88
89 found_dot = dot = true;
90 } else {
91 bool good;
92
93 good =
94 (*q >= 'a' && *q <= 'z') ||
95 (*q >= 'A' && *q <= 'Z') ||
96 (!dot && *q >= '0' && *q <= '9') ||
97 *q == '_';
98
99 if (!good)
100 return false;
101
102 dot = false;
103 }
104
105 if (q - p > 255)
106 return false;
107
108 if (dot)
109 return false;
110
111 if (!found_dot)
112 return false;
113
114 return true;
115 }
116
117 bool service_name_is_valid(const char *p) {
118 const char *q;
119 bool dot, found_dot = false, unique;
120
121 if (isempty(p))
122 return false;
123
124 unique = p[0] == ':';
125
126 for (dot = true, q = unique ? p+1 : p; *q; q++)
127 if (*q == '.') {
128 if (dot)
129 return false;
130
131 found_dot = dot = true;
132 } else {
133 bool good;
134
135 good =
136 (*q >= 'a' && *q <= 'z') ||
137 (*q >= 'A' && *q <= 'Z') ||
138 ((!dot || unique) && *q >= '0' && *q <= '9') ||
139 IN_SET(*q, '_', '-');
140
141 if (!good)
142 return false;
143
144 dot = false;
145 }
146
147 if (q - p > 255)
148 return false;
149
150 if (dot)
151 return false;
152
153 if (!found_dot)
154 return false;
155
156 return true;
157 }
158
159 char* service_name_startswith(const char *a, const char *b) {
160 const char *p;
161
162 if (!service_name_is_valid(a) ||
163 !service_name_is_valid(b))
164 return NULL;
165
166 p = startswith(a, b);
167 if (!p)
168 return NULL;
169
170 if (*p == 0)
171 return (char*) p;
172
173 if (*p == '.')
174 return (char*) p + 1;
175
176 return NULL;
177 }
178
179 bool member_name_is_valid(const char *p) {
180 const char *q;
181
182 if (isempty(p))
183 return false;
184
185 for (q = p; *q; q++) {
186 bool good;
187
188 good =
189 (*q >= 'a' && *q <= 'z') ||
190 (*q >= 'A' && *q <= 'Z') ||
191 (*q >= '0' && *q <= '9') ||
192 *q == '_';
193
194 if (!good)
195 return false;
196 }
197
198 if (q - p > 255)
199 return false;
200
201 return true;
202 }
203
204 /*
205 * Complex pattern match
206 * This checks whether @a is a 'complex-prefix' of @b, or @b is a
207 * 'complex-prefix' of @a, based on strings that consist of labels with @c as
208 * spearator. This function returns true if:
209 * - both strings are equal
210 * - either is a prefix of the other and ends with @c
211 * The second rule makes sure that either string needs to be fully included in
212 * the other, and the string which is considered the prefix needs to end with a
213 * separator.
214 */
215 static bool complex_pattern_check(char c, const char *a, const char *b) {
216 bool separator = false;
217
218 if (!a && !b)
219 return true;
220
221 if (!a || !b)
222 return false;
223
224 for (;;) {
225 if (*a != *b)
226 return (separator && (*a == 0 || *b == 0));
227
228 if (*a == 0)
229 return true;
230
231 separator = *a == c;
232
233 a++, b++;
234 }
235 }
236
237 bool namespace_complex_pattern(const char *pattern, const char *value) {
238 return complex_pattern_check('.', pattern, value);
239 }
240
241 bool path_complex_pattern(const char *pattern, const char *value) {
242 return complex_pattern_check('/', pattern, value);
243 }
244
245 /*
246 * Simple pattern match
247 * This checks whether @a is a 'simple-prefix' of @b, based on strings that
248 * consist of labels with @c as separator. This function returns true, if:
249 * - if @a and @b are equal
250 * - if @a is a prefix of @b, and the first following character in @b (or the
251 * last character in @a) is @c
252 * The second rule basically makes sure that if @a is a prefix of @b, then @b
253 * must follow with a new label separated by @c. It cannot extend the label.
254 */
255 static bool simple_pattern_check(char c, const char *a, const char *b) {
256 bool separator = false;
257
258 if (!a && !b)
259 return true;
260
261 if (!a || !b)
262 return false;
263
264 for (;;) {
265 if (*a != *b)
266 return *a == 0 && (*b == c || separator);
267
268 if (*a == 0)
269 return true;
270
271 separator = *a == c;
272
273 a++, b++;
274 }
275 }
276
277 bool namespace_simple_pattern(const char *pattern, const char *value) {
278 return simple_pattern_check('.', pattern, value);
279 }
280
281 bool path_simple_pattern(const char *pattern, const char *value) {
282 return simple_pattern_check('/', pattern, value);
283 }
284
285 int bus_message_type_from_string(const char *s, uint8_t *u) {
286 if (streq(s, "signal"))
287 *u = SD_BUS_MESSAGE_SIGNAL;
288 else if (streq(s, "method_call"))
289 *u = SD_BUS_MESSAGE_METHOD_CALL;
290 else if (streq(s, "error"))
291 *u = SD_BUS_MESSAGE_METHOD_ERROR;
292 else if (streq(s, "method_return"))
293 *u = SD_BUS_MESSAGE_METHOD_RETURN;
294 else
295 return -EINVAL;
296
297 return 0;
298 }
299
300 const char *bus_message_type_to_string(uint8_t u) {
301 if (u == SD_BUS_MESSAGE_SIGNAL)
302 return "signal";
303 else if (u == SD_BUS_MESSAGE_METHOD_CALL)
304 return "method_call";
305 else if (u == SD_BUS_MESSAGE_METHOD_ERROR)
306 return "error";
307 else if (u == SD_BUS_MESSAGE_METHOD_RETURN)
308 return "method_return";
309 else
310 return NULL;
311 }
312
313 char *bus_address_escape(const char *v) {
314 const char *a;
315 char *r, *b;
316
317 r = new(char, strlen(v)*3+1);
318 if (!r)
319 return NULL;
320
321 for (a = v, b = r; *a; a++) {
322
323 if ((*a >= '0' && *a <= '9') ||
324 (*a >= 'a' && *a <= 'z') ||
325 (*a >= 'A' && *a <= 'Z') ||
326 strchr("_-/.", *a))
327 *(b++) = *a;
328 else {
329 *(b++) = '%';
330 *(b++) = hexchar(*a >> 4);
331 *(b++) = hexchar(*a & 0xF);
332 }
333 }
334
335 *b = 0;
336 return r;
337 }
338
339 int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error) {
340 assert(m);
341
342 if (r < 0) {
343 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
344 sd_bus_reply_method_errno(m, r, error);
345
346 } else if (sd_bus_error_is_set(error)) {
347 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
348 sd_bus_reply_method_error(m, error);
349 } else
350 return r;
351
352 log_debug("Failed to process message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " signature=%s error-name=%s error-message=%s: %s",
353 bus_message_type_to_string(m->header->type),
354 strna(sd_bus_message_get_sender(m)),
355 strna(sd_bus_message_get_destination(m)),
356 strna(sd_bus_message_get_path(m)),
357 strna(sd_bus_message_get_interface(m)),
358 strna(sd_bus_message_get_member(m)),
359 BUS_MESSAGE_COOKIE(m),
360 m->reply_cookie,
361 strna(m->root_container.signature),
362 strna(m->error.name),
363 strna(m->error.message),
364 bus_error_message(error, r));
365
366 return 1;
367 }