Debian Codegraph @

src/libsystemd/sd-bus/bus-internal.c

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