1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "bus-internal.h"
7 #include "bus-message.h"
9 #include "string-util.h"
11 bool object_path_is_valid(const char *p) {
24 for (slash = true, q = p+1; *q; q++)
34 (*q >= 'a' && *q <= 'z') ||
35 (*q >= 'A' && *q <= 'Z') ||
36 (*q >= '0' && *q <= '9') ||
51 char* object_path_startswith(const char *a, const char *b) {
54 if (!object_path_is_valid(a) ||
55 !object_path_is_valid(b))
74 bool interface_name_is_valid(const char *p) {
76 bool dot, found_dot = false;
81 for (dot = true, q = p; *q; q++)
86 found_dot = dot = true;
91 (*q >= 'a' && *q <= 'z') ||
92 (*q >= 'A' && *q <= 'Z') ||
93 (!dot && *q >= '0' && *q <= '9') ||
114 bool service_name_is_valid(const char *p) {
116 bool dot, found_dot = false, unique;
121 unique = p[0] == ':';
123 for (dot = true, q = unique ? p+1 : p; *q; q++)
128 found_dot = dot = true;
133 (*q >= 'a' && *q <= 'z') ||
134 (*q >= 'A' && *q <= 'Z') ||
135 ((!dot || unique) && *q >= '0' && *q <= '9') ||
136 IN_SET(*q, '_', '-');
156 #if 0 /// UNNEEDED by elogind
157 char* service_name_startswith(const char *a, const char *b) {
160 if (!service_name_is_valid(a) ||
161 !service_name_is_valid(b))
164 p = startswith(a, b);
172 return (char*) p + 1;
178 bool member_name_is_valid(const char *p) {
184 for (q = p; *q; q++) {
188 (*q >= 'a' && *q <= 'z') ||
189 (*q >= 'A' && *q <= 'Z') ||
190 (*q >= '0' && *q <= '9') ||
204 * Complex pattern match
205 * This checks whether @a is a 'complex-prefix' of @b, or @b is a
206 * 'complex-prefix' of @a, based on strings that consist of labels with @c as
207 * spearator. This function returns true if:
208 * - both strings are equal
209 * - either is a prefix of the other and ends with @c
210 * The second rule makes sure that either string needs to be fully included in
211 * the other, and the string which is considered the prefix needs to end with a
214 static bool complex_pattern_check(char c, const char *a, const char *b) {
215 bool separator = false;
225 return (separator && (*a == 0 || *b == 0));
236 bool namespace_complex_pattern(const char *pattern, const char *value) {
237 return complex_pattern_check('.', pattern, value);
240 bool path_complex_pattern(const char *pattern, const char *value) {
241 return complex_pattern_check('/', pattern, value);
245 * Simple pattern match
246 * This checks whether @a is a 'simple-prefix' of @b, based on strings that
247 * consist of labels with @c as separator. This function returns true, if:
248 * - if @a and @b are equal
249 * - if @a is a prefix of @b, and the first following character in @b (or the
250 * last character in @a) is @c
251 * The second rule basically makes sure that if @a is a prefix of @b, then @b
252 * must follow with a new label separated by @c. It cannot extend the label.
254 static bool simple_pattern_check(char c, const char *a, const char *b) {
255 bool separator = false;
265 return *a == 0 && (*b == c || separator);
276 bool namespace_simple_pattern(const char *pattern, const char *value) {
277 return simple_pattern_check('.', pattern, value);
280 bool path_simple_pattern(const char *pattern, const char *value) {
281 return simple_pattern_check('/', pattern, value);
284 int bus_message_type_from_string(const char *s, uint8_t *u) {
285 if (streq(s, "signal"))
286 *u = SD_BUS_MESSAGE_SIGNAL;
287 else if (streq(s, "method_call"))
288 *u = SD_BUS_MESSAGE_METHOD_CALL;
289 else if (streq(s, "error"))
290 *u = SD_BUS_MESSAGE_METHOD_ERROR;
291 else if (streq(s, "method_return"))
292 *u = SD_BUS_MESSAGE_METHOD_RETURN;
299 const char *bus_message_type_to_string(uint8_t u) {
300 if (u == SD_BUS_MESSAGE_SIGNAL)
302 else if (u == SD_BUS_MESSAGE_METHOD_CALL)
303 return "method_call";
304 else if (u == SD_BUS_MESSAGE_METHOD_ERROR)
306 else if (u == SD_BUS_MESSAGE_METHOD_RETURN)
307 return "method_return";
312 char *bus_address_escape(const char *v) {
316 r = new(char, strlen(v)*3+1);
320 for (a = v, b = r; *a; a++) {
322 if ((*a >= '0' && *a <= '9') ||
323 (*a >= 'a' && *a <= 'z') ||
324 (*a >= 'A' && *a <= 'Z') ||
329 *(b++) = hexchar(*a >> 4);
330 *(b++) = hexchar(*a & 0xF);
338 int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error) {
342 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
343 sd_bus_reply_method_errno(m, r, error);
345 } else if (sd_bus_error_is_set(error)) {
346 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
347 sd_bus_reply_method_error(m, error);
351 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",
352 bus_message_type_to_string(m->header->type),
353 strna(sd_bus_message_get_sender(m)),
354 strna(sd_bus_message_get_destination(m)),
355 strna(sd_bus_message_get_path(m)),
356 strna(sd_bus_message_get_interface(m)),
357 strna(sd_bus_message_get_member(m)),
358 BUS_MESSAGE_COOKIE(m),
360 strna(m->root_container.signature),
361 strna(m->error.name),
362 strna(m->error.message),
363 bus_error_message(error, r));