Debian Codegraph @

src/launch/config.c

raw
1 /*
2 * D-Bus XML Config Parser
3 */
4
5 #include <c-list.h>
6 #include <c-stdaux.h>
7 #include <expat.h>
8 #include <stdlib.h>
9 #include <sys/auxv.h>
10 #include "dbus/protocol.h"
11 #include "launch/config.h"
12 #include "launch/nss-cache.h"
13 #include "util/common.h"
14 #include "util/dirwatch.h"
15 #include "util/error.h"
16 #include "util/fs.h"
17 #include "util/selinux.h"
18 #include "util/string.h"
19
20 static_assert(__builtin_types_compatible_p(XML_Char, char),
21 "Missing UTF-8 support in expat");
22
23 /* Print an error message to stderr including file+line information */
24 #define CONFIG_ERR(_state, _intro, _outro, ...) \
25 fprintf(stderr, \
26 _intro " in %s +%lu" _outro "\n", \
27 (_state)->file->path, \
28 XML_GetCurrentLineNumber( \
29 c_container_of((_state), ConfigParser, state)->xml), \
30 ## __VA_ARGS__)
31
32 /**
33 * config_path_new() - XXX
34 */
35 int config_path_new(ConfigPath **filep, ConfigPath *parent, const char *prefix, const char *path) {
36 _c_cleanup_(config_path_unrefp) ConfigPath *file = NULL;
37 size_t n_path, n_prefix;
38 char *t;
39
40 n_path = strlen(path);
41 n_prefix = 0;
42
43 /* prepend parent-path if @path is relative */
44 if (path[0] != '/') {
45 if (prefix) {
46 n_prefix = strlen(prefix);
47 } else if (parent) {
48 if (parent->is_dir) {
49 prefix = parent->path;
50 n_prefix = strlen(parent->path);
51 } else {
52 t = strrchr(parent->path, '/');
53 if (t) {
54 prefix = parent->path;
55 n_prefix = t - parent->path;
56 }
57 }
58 }
59 }
60
61 file = calloc(1, sizeof(*file) + n_prefix + 1 + n_path + 1);
62 if (!file)
63 return error_origin(-ENOMEM);
64
65 *file = (ConfigPath)CONFIG_FILE_NULL(*file);
66 file->parent = config_path_ref(parent);
67
68 if (n_prefix) {
69 c_memcpy(file->path, prefix, n_prefix);
70 file->path[n_prefix] = '/';
71 c_memcpy(file->path + n_prefix + 1, path, n_path + 1);
72 } else {
73 c_memcpy(file->path, path, n_path + 1);
74 }
75
76 *filep = file;
77 file = NULL;
78 return 0;
79 }
80
81 static int config_path_new_dir(ConfigPath **filep, ConfigPath *parent, const char *path) {
82 int r;
83
84 r = config_path_new(filep, parent, NULL, path);
85 if (!r)
86 (*filep)->is_dir = true;
87
88 return r;
89 }
90
91 /**
92 * config_path_ref() - XXX
93 */
94 ConfigPath *config_path_ref(ConfigPath *file) {
95 if (file)
96 ++file->n_refs;
97 return file;
98 }
99
100 /**
101 * config_path_unref() - XXX
102 */
103 ConfigPath *config_path_unref(ConfigPath *file) {
104 ConfigPath *parent;
105
106 if (!file || --file->n_refs)
107 return NULL;
108
109 parent = file->parent;
110 free(file);
111
112 return config_path_unref(parent);
113 }
114
115 /**
116 * config_node_new() - XXX
117 */
118 int config_node_new(ConfigNode **nodep, ConfigNode *parent, unsigned int type) {
119 _c_cleanup_(config_node_freep) ConfigNode *node = NULL;
120
121 node = calloc(1, sizeof(*node));
122 if (!node)
123 return error_origin(-ENOMEM);
124
125 *node = (ConfigNode)CONFIG_NODE_NULL(*node);
126 node->parent = parent;
127 if (parent)
128 ++parent->n_children;
129 node->type = type;
130
131 switch (node->type) {
132 case CONFIG_NODE_ALLOW:
133 case CONFIG_NODE_DENY:
134 node->allow_deny.max_fds = UINT64_MAX;
135 break;
136 }
137
138 node->cdata = strdup("");
139 if (!node->cdata)
140 return error_origin(-ENOMEM);
141
142 *nodep = node;
143 node = NULL;
144 return 0;
145 }
146
147 /**
148 * config_node_free() - XXX
149 */
150 ConfigNode *config_node_free(ConfigNode *node) {
151 if (!node)
152 return NULL;
153
154 switch (node->type) {
155 case CONFIG_NODE_TYPE:
156 free(node->bustype.name);
157 break;
158 case CONFIG_NODE_INCLUDEDIR:
159 config_path_unref(node->includedir.dir);
160 break;
161 case CONFIG_NODE_INCLUDE:
162 config_path_unref(node->include.file);
163 break;
164 case CONFIG_NODE_SERVICEDIR:
165 free(node->servicedir.path);
166 break;
167 case CONFIG_NODE_ALLOW:
168 case CONFIG_NODE_DENY:
169 free(node->allow_deny.send_interface);
170 free(node->allow_deny.send_member);
171 free(node->allow_deny.send_error);
172 free(node->allow_deny.send_destination);
173 free(node->allow_deny.send_path);
174 free(node->allow_deny.recv_interface);
175 free(node->allow_deny.recv_member);
176 free(node->allow_deny.recv_error);
177 free(node->allow_deny.recv_sender);
178 free(node->allow_deny.recv_path);
179 free(node->allow_deny.own);
180 free(node->allow_deny.own_prefix);
181 break;
182 case CONFIG_NODE_ASSOCIATE:
183 free(node->associate.own);
184 free(node->associate.context);
185 break;
186 }
187
188 free(node->cdata);
189 config_path_unref(node->path);
190
191 c_assert(!node->n_children);
192 if (node->parent)
193 --node->parent->n_children;
194
195 c_list_unlink(&node->include_link);
196 c_list_unlink(&node->root_link);
197 free(node);
198
199 return NULL;
200 }
201
202 /**
203 * config_root_new() - XXX
204 */
205 int config_root_new(ConfigRoot **rootp) {
206 _c_cleanup_(config_root_freep) ConfigRoot *root = NULL;
207
208 root = calloc(1, sizeof(*root));
209 if (!root)
210 return error_origin(-ENOMEM);
211
212 *root = (ConfigRoot)CONFIG_ROOT_NULL(*root);
213
214 *rootp = root;
215 root = NULL;
216 return 0;
217 }
218
219 /**
220 * config_root_free() - XXX
221 */
222 ConfigRoot *config_root_free(ConfigRoot *root) {
223 ConfigNode *i_node;
224
225 if (!root)
226 return NULL;
227
228 while ((i_node = c_list_last_entry(&root->node_list, ConfigNode, root_link)))
229 config_node_free(i_node);
230
231 c_assert(c_list_is_empty(&root->node_list));
232 c_assert(c_list_is_empty(&root->include_list));
233
234 free(root);
235
236 return NULL;
237 }
238
239 static int config_parser_attrs_include(ConfigState *state, ConfigNode *node, const XML_Char **attrs) {
240 const char *k, *v;
241
242 while (*attrs) {
243 k = *(attrs++);
244 v = *(attrs++);
245
246 if (!strcmp(k, "ignore_missing")) {
247 if (!strcmp(v, "yes"))
248 node->include.ignore_missing = true;
249 else if (!strcmp(v, "no"))
250 node->include.ignore_missing = false;
251 else
252 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
253 } else if (!strcmp(k, "if_selinux_enabled")) {
254 if (!strcmp(v, "yes"))
255 node->include.if_selinux_enabled = true;
256 else if (!strcmp(v, "no"))
257 node->include.if_selinux_enabled = false;
258 else
259 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
260 } else if (!strcmp(k, "selinux_root_relative")) {
261 if (!strcmp(v, "yes"))
262 node->include.selinux_root_relative = true;
263 else if (!strcmp(v, "no"))
264 node->include.selinux_root_relative = false;
265 else
266 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
267 } else {
268 CONFIG_ERR(state, "Unknown attribute", ": %s=\"%s\"", k, v);
269 }
270 }
271
272 return 0;
273 }
274
275 static int config_parser_attrs_policy(ConfigState *state, ConfigNode *node, const XML_Char **attrs) {
276 const char *k, *v;
277 int r;
278
279 while (*attrs) {
280 k = *(attrs++);
281 v = *(attrs++);
282
283 if (!strcmp(k, "user")) {
284 if (node->policy.context)
285 CONFIG_ERR(state, "Conflicting attributes", "");
286
287 r = nss_cache_get_uid(state->nss, &node->policy.id, NULL, v);
288 if (r) {
289 if (r == NSS_CACHE_E_INVALID_NAME) {
290 CONFIG_ERR(state, "Invalid user-name", ": %s=\"%s\"", k, v);
291 node->policy.id = (uint32_t)-1;
292 } else {
293 return error_fold(r);
294 }
295 }
296
297 node->policy.context = CONFIG_POLICY_USER;
298 } else if (!strcmp(k, "group")) {
299 if (node->policy.context)
300 CONFIG_ERR(state, "Conflicting attributes", "");
301
302 r = nss_cache_get_gid(state->nss, &node->policy.id, v);
303 if (r) {
304 if (r == NSS_CACHE_E_INVALID_NAME) {
305 CONFIG_ERR(state, "Invalid group-name", ": %s=\"%s\"", k, v);
306 node->policy.id = (uint32_t)-1;
307 } else {
308 return error_fold(r);
309 }
310 }
311
312 node->policy.context = CONFIG_POLICY_GROUP;
313 } else if (!strcmp(k, "context")) {
314 if (node->policy.context)
315 CONFIG_ERR(state, "Conflicting attributes", "");
316
317 if (!strcmp(v, "mandatory"))
318 node->policy.context = CONFIG_POLICY_MANDATORY;
319 else if (!strcmp(v, "default"))
320 node->policy.context = CONFIG_POLICY_DEFAULT;
321 else
322 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
323 } else if (!strcmp(k, "at_console")) {
324 if (node->policy.context)
325 CONFIG_ERR(state, "Conflicting attributes", "");
326
327 if (!strcmp(v, "true"))
328 node->policy.context = CONFIG_POLICY_AT_CONSOLE;
329 else if (!strcmp(v, "false"))
330 node->policy.context = CONFIG_POLICY_NO_CONSOLE;
331 else
332 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
333 } else {
334 CONFIG_ERR(state, "Unknown attribute", ": %s=\"%s\"", k, v);
335 }
336 }
337
338 if (!node->policy.context)
339 CONFIG_ERR(state, "Missing attribute", "");
340
341 return 0;
342 }
343
344 static int config_parser_attrs_limit(ConfigState *state, ConfigNode *node, const XML_Char **attrs) {
345 const char *k, *v;
346
347 while (*attrs) {
348 k = *(attrs++);
349 v = *(attrs++);
350
351 if (!strcmp(k, "name")) {
352 if (!strcmp(v, "max_incoming_bytes"))
353 node->limit.name = CONFIG_LIMIT_MAX_INCOMING_BYTES;
354 else if (!strcmp(v, "max_incoming_unix_fds"))
355 node->limit.name = CONFIG_LIMIT_MAX_INCOMING_UNIX_FDS;
356 else if (!strcmp(v, "max_outgoing_bytes"))
357 node->limit.name = CONFIG_LIMIT_MAX_OUTGOING_BYTES;
358 else if (!strcmp(v, "max_outgoing_unix_fds"))
359 node->limit.name = CONFIG_LIMIT_MAX_OUTGOING_UNIX_FDS;
360 else if (!strcmp(v, "max_message_size"))
361 node->limit.name = CONFIG_LIMIT_MAX_MESSAGE_SIZE;
362 else if (!strcmp(v, "max_message_unix_fds"))
363 node->limit.name = CONFIG_LIMIT_MAX_MESSAGE_UNIX_FDS;
364 else if (!strcmp(v, "service_start_timeout"))
365 node->limit.name = CONFIG_LIMIT_SERVICE_START_TIMEOUT;
366 else if (!strcmp(v, "auth_timeout"))
367 node->limit.name = CONFIG_LIMIT_AUTH_TIMEOUT;
368 else if (!strcmp(v, "pending_fd_timeout"))
369 node->limit.name = CONFIG_LIMIT_PENDING_FD_TIMEOUT;
370 else if (!strcmp(v, "max_completed_connections"))
371 node->limit.name = CONFIG_LIMIT_MAX_COMPLETED_CONNECTIONS;
372 else if (!strcmp(v, "max_incomplete_connections"))
373 node->limit.name = CONFIG_LIMIT_MAX_INCOMPLETE_CONNECTIONS;
374 else if (!strcmp(v, "max_connections_per_user"))
375 node->limit.name = CONFIG_LIMIT_MAX_CONNECTIONS_PER_USER;
376 else if (!strcmp(v, "max_pending_service_starts"))
377 node->limit.name = CONFIG_LIMIT_MAX_PENDING_SERVICE_STARTS;
378 else if (!strcmp(v, "max_names_per_connection"))
379 node->limit.name = CONFIG_LIMIT_MAX_NAMES_PER_CONNECTION;
380 else if (!strcmp(v, "max_match_rules_per_connection"))
381 node->limit.name = CONFIG_LIMIT_MAX_MATCH_RULES_PER_CONNECTION;
382 else if (!strcmp(v, "max_replies_per_connection"))
383 node->limit.name = CONFIG_LIMIT_MAX_REPLIES_PER_CONNECTION;
384 else if (!strcmp(v, "max_containers_per_user"))
385 node->limit.name = CONFIG_LIMIT_MAX_CONTAINERS_PER_USER;
386 else if (!strcmp(v, "max_containers"))
387 node->limit.name = CONFIG_LIMIT_MAX_CONTAINERS;
388 else if (!strcmp(v, "max_connections_per_container"))
389 node->limit.name = CONFIG_LIMIT_MAX_CONNECTIONS_PER_CONTAINER;
390 else if (!strcmp(v, "max_container_metadata_bytes"))
391 node->limit.name = CONFIG_LIMIT_MAX_CONTAINER_METADATA_BYTES;
392 else if (!strcmp(v, "reply_timeout"))
393 node->limit.name = CONFIG_LIMIT_REPLY_TIMEOUT;
394 else
395 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
396 } else {
397 CONFIG_ERR(state, "Unknown attribute", ": %s=\"%s\"", k, v);
398 }
399 }
400
401 if (!node->limit.name)
402 CONFIG_ERR(state, "Required attribute 'name' missing", "");
403
404 return 0;
405 }
406
407 static int config_parser_attrs_apparmor(ConfigState *state, ConfigNode *node, const XML_Char **attrs) {
408 const char *k, *v;
409
410 while (*attrs) {
411 k = *(attrs++);
412 v = *(attrs++);
413
414 if (!strcmp(k, "mode")) {
415 if (!strcmp(v, "enabled"))
416 node->apparmor.mode = CONFIG_APPARMOR_ENABLED;
417 else if (!strcmp(v, "disabled"))
418 node->apparmor.mode = CONFIG_APPARMOR_DISABLED;
419 else if (!strcmp(v, "required"))
420 node->apparmor.mode = CONFIG_APPARMOR_REQUIRED;
421 else
422 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
423 } else {
424 CONFIG_ERR(state, "Unknown attribute", ": %s=\"%s\"", k, v);
425 }
426 }
427
428 return 0;
429 }
430
431 static int config_parser_attrs_allow_deny(ConfigState *state, ConfigNode *node, const XML_Char **attrs) {
432 const char *k, *v;
433 char *t;
434 int r;
435
436 while (*attrs) {
437 k = *(attrs++);
438 v = *(attrs++);
439
440 if (!strcmp(k, "send_interface")) {
441 t = strdup(v);
442 if (!t)
443 return error_origin(-ENOMEM);
444
445 free(node->allow_deny.send_interface);
446 node->allow_deny.send_interface = t;
447 } else if (!strcmp(k, "send_member")) {
448 t = strdup(v);
449 if (!t)
450 return error_origin(-ENOMEM);
451
452 free(node->allow_deny.send_member);
453 node->allow_deny.send_member = t;
454 } else if (!strcmp(k, "send_error")) {
455 t = strdup(v);
456 if (!t)
457 return error_origin(-ENOMEM);
458
459 free(node->allow_deny.send_error);
460 node->allow_deny.send_error = t;
461 } else if (!strcmp(k, "send_destination")) {
462 t = strdup(v);
463 if (!t)
464 return error_origin(-ENOMEM);
465
466 free(node->allow_deny.send_destination);
467 node->allow_deny.send_destination = t;
468 } else if (!strcmp(k, "send_path")) {
469 t = strdup(v);
470 if (!t)
471 return error_origin(-ENOMEM);
472
473 free(node->allow_deny.send_path);
474 node->allow_deny.send_path = t;
475 } else if (!strcmp(k, "send_type")) {
476 if (!strcmp(v, "method_call"))
477 node->allow_deny.send_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
478 else if (!strcmp(v, "method_return"))
479 node->allow_deny.send_type = DBUS_MESSAGE_TYPE_METHOD_RETURN;
480 else if (!strcmp(v, "signal"))
481 node->allow_deny.send_type = DBUS_MESSAGE_TYPE_SIGNAL;
482 else if (!strcmp(v, "error"))
483 node->allow_deny.send_type = DBUS_MESSAGE_TYPE_ERROR;
484 else
485 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
486 } else if (!strcmp(k, "send_broadcast")) {
487 if (!strcmp(v, "false"))
488 node->allow_deny.send_broadcast = UTIL_TRISTATE_NO;
489 else if (!strcmp(v, "true"))
490 node->allow_deny.send_broadcast = UTIL_TRISTATE_YES;
491 else
492 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
493 } else if (!strcmp(k, "receive_interface")) {
494 t = strdup(v);
495 if (!t)
496 return error_origin(-ENOMEM);
497
498 free(node->allow_deny.recv_interface);
499 node->allow_deny.recv_interface = t;
500 } else if (!strcmp(k, "receive_member")) {
501 t = strdup(v);
502 if (!t)
503 return error_origin(-ENOMEM);
504
505 free(node->allow_deny.recv_member);
506 node->allow_deny.recv_member = t;
507 } else if (!strcmp(k, "receive_error")) {
508 t = strdup(v);
509 if (!t)
510 return error_origin(-ENOMEM);
511
512 free(node->allow_deny.recv_error);
513 node->allow_deny.recv_error = t;
514 } else if (!strcmp(k, "receive_sender")) {
515 t = strdup(v);
516 if (!t)
517 return error_origin(-ENOMEM);
518
519 free(node->allow_deny.recv_sender);
520 node->allow_deny.recv_sender = t;
521 } else if (!strcmp(k, "receive_path")) {
522 t = strdup(v);
523 if (!t)
524 return error_origin(-ENOMEM);
525
526 free(node->allow_deny.recv_path);
527 node->allow_deny.recv_path = t;
528 } else if (!strcmp(k, "receive_type")) {
529 if (!strcmp(v, "method_call"))
530 node->allow_deny.recv_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
531 else if (!strcmp(v, "method_return"))
532 node->allow_deny.recv_type = DBUS_MESSAGE_TYPE_METHOD_RETURN;
533 else if (!strcmp(v, "signal"))
534 node->allow_deny.recv_type = DBUS_MESSAGE_TYPE_SIGNAL;
535 else if (!strcmp(v, "error"))
536 node->allow_deny.recv_type = DBUS_MESSAGE_TYPE_ERROR;
537 else
538 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
539 } else if (!strcmp(k, "own")) {
540 t = strdup(v);
541 if (!t)
542 return error_origin(-ENOMEM);
543
544 free(node->allow_deny.own);
545 node->allow_deny.own = t;
546 } else if (!strcmp(k, "own_prefix")) {
547 t = strdup(v);
548 if (!t)
549 return error_origin(-ENOMEM);
550
551 free(node->allow_deny.own_prefix);
552 node->allow_deny.own_prefix = t;
553 } else if (!strcmp(k, "min_fds")) {
554 r = util_strtou64(&node->allow_deny.min_fds, v);
555 if (r)
556 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
557 } else if (!strcmp(k, "max_fds")) {
558 r = util_strtou64(&node->allow_deny.max_fds, v);
559 if (r)
560 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
561 } else if (!strcmp(k, "user")) {
562 if (!strcmp(v, "*")) {
563 node->allow_deny.uid = (uint32_t)-1;
564 node->allow_deny.user = true;
565 } else {
566 r = nss_cache_get_uid(state->nss, &node->allow_deny.uid, NULL, v);
567 if (r) {
568 if (r == NSS_CACHE_E_INVALID_NAME) {
569 CONFIG_ERR(state, "Invalid user-name", ": %s=\"%s\"", k, v);
570 continue;
571 }
572
573 return error_fold(r);
574 }
575
576 node->allow_deny.user = true;
577 }
578 } else if (!strcmp(k, "group")) {
579 if (!strcmp(v, "*")) {
580 node->allow_deny.gid = (uint32_t)-1;
581 node->allow_deny.group = true;
582 } else {
583 r = nss_cache_get_gid(state->nss, &node->allow_deny.gid, v);
584 if (r) {
585 if (r == NSS_CACHE_E_INVALID_NAME) {
586 CONFIG_ERR(state, "Invalid group-name", ": %s=\"%s\"", k, v);
587 continue;
588 }
589
590 return error_fold(r);
591 }
592
593 node->allow_deny.group = true;
594 }
595 } else if (!strcmp(k, "send_requested_reply")) {
596 if (!strcmp(v, "true"))
597 node->allow_deny.send_requested_reply = UTIL_TRISTATE_YES;
598 else if (!strcmp(v, "false"))
599 node->allow_deny.send_requested_reply = UTIL_TRISTATE_NO;
600 else
601 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
602 } else if (!strcmp(k, "receive_requested_reply")) {
603 if (!strcmp(v, "true"))
604 node->allow_deny.recv_requested_reply = UTIL_TRISTATE_YES;
605 else if (!strcmp(v, "false"))
606 node->allow_deny.recv_requested_reply = UTIL_TRISTATE_NO;
607 else
608 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
609 } else if (!strcmp(k, "eavesdrop")) {
610 if (!strcmp(v, "true"))
611 node->allow_deny.eavesdrop = UTIL_TRISTATE_YES;
612 else if (!strcmp(v, "false"))
613 node->allow_deny.eavesdrop = UTIL_TRISTATE_NO;
614 else
615 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
616 } else if (!strcmp(k, "log")) {
617 if (!strcmp(v, "true"))
618 node->allow_deny.log = true;
619 else if (!strcmp(v, "false"))
620 node->allow_deny.log = false;
621 else
622 CONFIG_ERR(state, "Invalid value", ": %s=\"%s\"", k, v);
623 } else {
624 CONFIG_ERR(state, "Unknown attribute", ": %s=\"%s\"", k, v);
625 }
626 }
627
628 return 0;
629 }
630
631 static int config_parser_attrs_associate(ConfigState *state, ConfigNode *node, const XML_Char **attrs) {
632 const char *k, *v;
633 char *t;
634
635 while (*attrs) {
636 k = *(attrs++);
637 v = *(attrs++);
638
639 if (!strcmp(k, "own")) {
640 t = strdup(v);
641 if (!t)
642 return error_origin(-ENOMEM);
643
644 free(node->associate.own);
645 node->associate.own = t;
646 } else if (!strcmp(k, "context")) {
647 t = strdup(v);
648 if (!t)
649 return error_origin(-ENOMEM);
650
651 free(node->associate.context);
652 node->associate.context = t;
653 } else {
654 CONFIG_ERR(state, "Unknown attribute", ": %s=\"%s\"", k, v);
655 }
656 }
657
658 if (!node->associate.own)
659 CONFIG_ERR(state, "Required attribute 'own' missing", "");
660 if (!node->associate.context)
661 CONFIG_ERR(state, "Required attribute 'context' missing", "");
662
663 return 0;
664 }
665
666 static int config_parser_attrs_default(ConfigState *state, ConfigNode *node, const XML_Char **attrs) {
667 const char *k, *v;
668
669 while (*attrs) {
670 k = *(attrs++);
671 v = *(attrs++);
672
673 CONFIG_ERR(state, "Unknown attribute", ": %s=\"%s\"", k, v);
674 }
675
676 return 0;
677 }
678
679 static void config_parser_begin_fn(void *userdata, const XML_Char *name, const XML_Char **attrs) {
680 _c_cleanup_(config_node_freep) ConfigNode *node = NULL;
681 ConfigState *state = userdata;
682 int r = 0;
683
684 c_assert(state->current);
685
686 /*
687 * Whenever we hit a fatal error, we remember it in @state and simply
688 * shortcut everything. However, when we just hit unknown tags, we
689 * track our nesting depth and ignore anything underneath that tag, but
690 * continue parsing once we are back at a known state.
691 */
692 if (state->error)
693 return;
694 if (state->n_failed) {
695 ++state->n_failed;
696 return;
697 }
698
699 if (!strcmp(name, "busconfig")) {
700
701 if (state->n_depth)
702 goto failed;
703
704 r = config_node_new(&node, state->current, CONFIG_NODE_BUSCONFIG);
705 if (r)
706 goto failed;
707
708 r = config_parser_attrs_default(state, node, attrs);
709 if (r)
710 goto failed;
711
712 } else if (!strcmp(name, "user")) {
713
714 if (state->current->type != CONFIG_NODE_BUSCONFIG)
715 goto failed;
716
717 r = config_node_new(&node, state->current, CONFIG_NODE_USER);
718 if (r)
719 goto failed;
720
721 r = config_parser_attrs_default(state, node, attrs);
722 if (r)
723 goto failed;
724
725 } else if (!strcmp(name, "type")) {
726
727 if (state->current->type != CONFIG_NODE_BUSCONFIG)
728 goto failed;
729
730 r = config_node_new(&node, state->current, CONFIG_NODE_TYPE);
731 if (r)
732 goto failed;
733
734 r = config_parser_attrs_default(state, node, attrs);
735 if (r)
736 goto failed;
737
738 } else if (!strcmp(name, "fork")) {
739
740 if (state->current->type != CONFIG_NODE_BUSCONFIG)
741 goto failed;
742
743 r = config_node_new(&node, state->current, CONFIG_NODE_FORK);
744 if (r)
745 goto failed;
746
747 r = config_parser_attrs_default(state, node, attrs);
748 if (r)
749 goto failed;
750
751 } else if (!strcmp(name, "syslog")) {
752
753 if (state->current->type != CONFIG_NODE_BUSCONFIG)
754 goto failed;
755
756 r = config_node_new(&node, state->current, CONFIG_NODE_SYSLOG);
757 if (r)
758 goto failed;
759
760 r = config_parser_attrs_default(state, node, attrs);
761 if (r)
762 goto failed;
763
764 } else if (!strcmp(name, "keep_umask")) {
765
766 if (state->current->type != CONFIG_NODE_BUSCONFIG)
767 goto failed;
768
769 r = config_node_new(&node, state->current, CONFIG_NODE_KEEP_UMASK);
770 if (r)
771 goto failed;
772
773 r = config_parser_attrs_default(state, node, attrs);
774 if (r)
775 goto failed;
776
777 } else if (!strcmp(name, "listen")) {
778
779 if (state->current->type != CONFIG_NODE_BUSCONFIG)
780 goto failed;
781
782 r = config_node_new(&node, state->current, CONFIG_NODE_LISTEN);
783 if (r)
784 goto failed;
785
786 r = config_parser_attrs_default(state, node, attrs);
787 if (r)
788 goto failed;
789
790 } else if (!strcmp(name, "pidfile")) {
791
792 if (state->current->type != CONFIG_NODE_BUSCONFIG)
793 goto failed;
794
795 r = config_node_new(&node, state->current, CONFIG_NODE_PIDFILE);
796 if (r)
797 goto failed;
798
799 r = config_parser_attrs_default(state, node, attrs);
800 if (r)
801 goto failed;
802
803 } else if (!strcmp(name, "includedir")) {
804
805 if (state->current->type != CONFIG_NODE_BUSCONFIG)
806 goto failed;
807
808 r = config_node_new(&node, state->current, CONFIG_NODE_INCLUDEDIR);
809 if (r)
810 goto failed;
811
812 r = config_parser_attrs_default(state, node, attrs);
813 if (r)
814 goto failed;
815
816 } else if (!strcmp(name, "standard_session_servicedirs")) {
817
818 if (state->current->type != CONFIG_NODE_BUSCONFIG)
819 goto failed;
820
821 r = config_node_new(&node, state->current, CONFIG_NODE_STANDARD_SESSION_SERVICEDIRS);
822 if (r)
823 goto failed;
824
825 r = config_parser_attrs_default(state, node, attrs);
826 if (r)
827 goto failed;
828
829 } else if (!strcmp(name, "standard_system_servicedirs")) {
830
831 if (state->current->type != CONFIG_NODE_BUSCONFIG)
832 goto failed;
833
834 r = config_node_new(&node, state->current, CONFIG_NODE_STANDARD_SYSTEM_SERVICEDIRS);
835 if (r)
836 goto failed;
837
838 r = config_parser_attrs_default(state, node, attrs);
839 if (r)
840 goto failed;
841
842 } else if (!strcmp(name, "servicedir")) {
843
844 if (state->current->type != CONFIG_NODE_BUSCONFIG)
845 goto failed;
846
847 r = config_node_new(&node, state->current, CONFIG_NODE_SERVICEDIR);
848 if (r)
849 goto failed;
850
851 r = config_parser_attrs_default(state, node, attrs);
852 if (r)
853 goto failed;
854
855 } else if (!strcmp(name, "servicehelper")) {
856
857 if (state->current->type != CONFIG_NODE_BUSCONFIG)
858 goto failed;
859
860 r = config_node_new(&node, state->current, CONFIG_NODE_SERVICEHELPER);
861 if (r)
862 goto failed;
863
864 r = config_parser_attrs_default(state, node, attrs);
865 if (r)
866 goto failed;
867
868 } else if (!strcmp(name, "auth")) {
869
870 if (state->current->type != CONFIG_NODE_BUSCONFIG)
871 goto failed;
872
873 r = config_node_new(&node, state->current, CONFIG_NODE_AUTH);
874 if (r)
875 goto failed;
876
877 r = config_parser_attrs_default(state, node, attrs);
878 if (r)
879 goto failed;
880
881 } else if (!strcmp(name, "include")) {
882
883 if (state->current->type != CONFIG_NODE_BUSCONFIG)
884 goto failed;
885
886 r = config_node_new(&node, state->current, CONFIG_NODE_INCLUDE);
887 if (r)
888 goto failed;
889
890 r = config_parser_attrs_include(state, node, attrs);
891 if (r)
892 goto failed;
893
894 } else if (!strcmp(name, "policy")) {
895
896 if (state->current->type != CONFIG_NODE_BUSCONFIG)
897 goto failed;
898
899 r = config_node_new(&node, state->current, CONFIG_NODE_POLICY);
900 if (r)
901 goto failed;
902
903 r = config_parser_attrs_policy(state, node, attrs);
904 if (r)
905 goto failed;
906
907 } else if (!strcmp(name, "limit")) {
908
909 if (state->current->type != CONFIG_NODE_BUSCONFIG)
910 goto failed;
911
912 r = config_node_new(&node, state->current, CONFIG_NODE_LIMIT);
913 if (r)
914 goto failed;
915
916 r = config_parser_attrs_limit(state, node, attrs);
917 if (r)
918 goto failed;
919
920 } else if (!strcmp(name, "selinux")) {
921
922 if (state->current->type != CONFIG_NODE_BUSCONFIG)
923 goto failed;
924
925 r = config_node_new(&node, state->current, CONFIG_NODE_SELINUX);
926 if (r)
927 goto failed;
928
929 r = config_parser_attrs_default(state, node, attrs);
930 if (r)
931 goto failed;
932
933 } else if (!strcmp(name, "apparmor")) {
934
935 if (state->current->type != CONFIG_NODE_BUSCONFIG)
936 goto failed;
937
938 r = config_node_new(&node, state->current, CONFIG_NODE_APPARMOR);
939 if (r)
940 goto failed;
941
942 r = config_parser_attrs_apparmor(state, node, attrs);
943 if (r)
944 goto failed;
945
946 } else if (!strcmp(name, "allow")) {
947
948 if (state->current->type != CONFIG_NODE_POLICY)
949 goto failed;
950
951 r = config_node_new(&node, state->current, CONFIG_NODE_ALLOW);
952 if (r)
953 goto failed;
954
955 r = config_parser_attrs_allow_deny(state, node, attrs);
956 if (r)
957 goto failed;
958
959 } else if (!strcmp(name, "deny")) {
960
961 if (state->current->type != CONFIG_NODE_POLICY)
962 goto failed;
963
964 r = config_node_new(&node, state->current, CONFIG_NODE_DENY);
965 if (r)
966 goto failed;
967
968 r = config_parser_attrs_allow_deny(state, node, attrs);
969 if (r)
970 goto failed;
971
972 } else if (!strcmp(name, "associate")) {
973
974 if (state->current->type != CONFIG_NODE_SELINUX)
975 goto failed;
976
977 r = config_node_new(&node, state->current, CONFIG_NODE_ASSOCIATE);
978 if (r)
979 goto failed;
980
981 r = config_parser_attrs_associate(state, node, attrs);
982 if (r)
983 goto failed;
984
985 } else {
986 CONFIG_ERR(state, "Unknown element", ": %s", name);
987 goto failed;
988 }
989
990 c_assert(node);
991 c_assert(node->parent);
992
993 c_list_link_after(&state->last->root_link, &node->root_link);
994 state->current = node;
995 state->last = node;
996 ++state->n_depth;
997
998 node->path = config_path_ref(state->file);
999 node->file = node->path->path;
1000 node->lineno = XML_GetCurrentLineNumber(c_container_of(state, ConfigParser, state)->xml);
1001
1002 node = NULL;
1003 return;
1004
1005 failed:
1006 if (r)
1007 state->error = error_trace(r);
1008 else
1009 ++state->n_failed;
1010 }
1011
1012 static void config_parser_end_fn(void *userdata, const XML_Char *name) {
1013 ConfigState *state = userdata;
1014 int r;
1015
1016 /*
1017 * Shortcut on errors. This is orthogonal to the begin-handler. See
1018 * there for details.
1019 */
1020 if (state->error)
1021 return;
1022 if (state->n_failed) {
1023 --state->n_failed;
1024 return;
1025 }
1026
1027 /*
1028 * Before exiting a node, we verify that we got a valid dataset and
1029 * that all mandatory data was given.
1030 */
1031 switch (state->current->type) {
1032 case CONFIG_NODE_TYPE:
1033 state->current->bustype.name = strdup(state->current->cdata);
1034 if (!state->current->bustype.name) {
1035 state->error = error_origin(-ENOMEM);
1036 return;
1037 }
1038
1039 break;
1040
1041 case CONFIG_NODE_USER:
1042 state->current->user.valid = false;
1043 r = nss_cache_get_uid(state->nss,
1044 &state->current->user.uid,
1045 &state->current->user.gid,
1046 state->current->cdata);
1047 if (r) {
1048 if (r == NSS_CACHE_E_INVALID_NAME) {
1049 CONFIG_ERR(state, "Invalid user-name", ": <user>%s</user>", state->current->cdata);
1050 break;
1051 }
1052
1053 state->error = error_fold(r);
1054 return;
1055 } else {
1056 state->current->user.valid = true;
1057 }
1058
1059 break;
1060
1061 case CONFIG_NODE_INCLUDEDIR: {
1062 _c_cleanup_(fs_dirlist_freep) FsDirlist *list = NULL;
1063 _c_cleanup_(c_closedirp) DIR *dir = NULL;
1064 static const char suffix[] = ".conf";
1065 struct dirent *de;
1066 size_t i, n;
1067
1068 r = config_path_new_dir(&state->current->includedir.dir,
1069 state->file,
1070 state->current->cdata);
1071 if (r) {
1072 state->error = error_trace(r);
1073 return;
1074 }
1075
1076 dir = opendir(state->current->includedir.dir->path);
1077 if (!dir) {
1078 if (errno == ENOENT || errno == ENOTDIR)
1079 break;
1080
1081 if (errno == EACCES || errno == EPERM) {
1082 CONFIG_ERR(state, "Access denied", ": %s",
1083 state->current->includedir.dir->path);
1084 }
1085
1086 state->error = error_origin(-errno);
1087 return;
1088 }
1089
1090 r = dirwatch_add(state->dirwatch, state->current->includedir.dir->path);
1091 if (r) {
1092 state->error = error_fold(r);
1093 return;
1094 }
1095
1096 r = fs_dir_list(dir, &list, 0);
1097 if (r) {
1098 state->error = error_fold(r);
1099 return;
1100 }
1101
1102 for (i = 0; i < list->n_entries; ++i) {
1103 _c_cleanup_(config_node_freep) ConfigNode *node = NULL;
1104
1105 de = list->entries[i];
1106 n = strlen(de->d_name);
1107
1108 if (n <= strlen(suffix))
1109 continue;
1110 if (strcmp(de->d_name + n - strlen(suffix), suffix))
1111 continue;
1112
1113 r = config_node_new(&node, state->current, CONFIG_NODE_INCLUDE);
1114 if (r) {
1115 state->error = error_trace(r);
1116 return;
1117 }
1118
1119 r = config_path_new(&node->include.file,
1120 state->current->includedir.dir,
1121 NULL,
1122 de->d_name);
1123 if (r) {
1124 state->error = error_trace(r);
1125 return;
1126 }
1127
1128 c_list_link_after(&state->last->root_link, &node->root_link);
1129 c_list_link_tail(&state->root->include_list, &node->include_link);
1130 state->last = node;
1131 node = NULL;
1132 }
1133
1134 break;
1135 }
1136
1137 case CONFIG_NODE_INCLUDE: {
1138 r = config_path_new(&state->current->include.file,
1139 state->file,
1140 state->current->include.selinux_root_relative ?
1141 bus_selinux_policy_root() :
1142 NULL,
1143 state->current->cdata);
1144 if (r) {
1145 state->error = error_trace(r);
1146 return;
1147 }
1148
1149 c_list_link_tail(&state->root->include_list, &state->current->include_link);
1150 break;
1151 }
1152
1153 case CONFIG_NODE_SERVICEDIR: {
1154 state->current->servicedir.path = strdup(state->current->cdata);
1155 if (!state->current->servicedir.path) {
1156 state->error = error_origin(-ENOMEM);
1157 return;
1158 }
1159
1160 break;
1161 }
1162
1163 case CONFIG_NODE_LIMIT:
1164 r = util_strtou64(&state->current->limit.value, state->current->cdata);
1165 if (r)
1166 CONFIG_ERR(state, "Invalid limit value", ": %s", state->current->cdata);
1167
1168 break;
1169
1170 case CONFIG_NODE_LISTEN:
1171 case CONFIG_NODE_PIDFILE:
1172 case CONFIG_NODE_SERVICEHELPER:
1173 case CONFIG_NODE_AUTH:
1174 /* XXX: Not yet implemented. */
1175 break;
1176
1177 case CONFIG_NODE_BUSCONFIG:
1178 case CONFIG_NODE_FORK:
1179 case CONFIG_NODE_SYSLOG:
1180 case CONFIG_NODE_KEEP_UMASK:
1181 case CONFIG_NODE_STANDARD_SESSION_SERVICEDIRS:
1182 case CONFIG_NODE_STANDARD_SYSTEM_SERVICEDIRS:
1183 case CONFIG_NODE_POLICY:
1184 case CONFIG_NODE_SELINUX:
1185 case CONFIG_NODE_APPARMOR:
1186 case CONFIG_NODE_ALLOW:
1187 case CONFIG_NODE_DENY:
1188 case CONFIG_NODE_ASSOCIATE:
1189 /* fallthrough */
1190 default:
1191 if (state->current->cdata &&
1192 strspn(state->current->cdata, " \r\t\n") < state->current->n_cdata)
1193 CONFIG_ERR(state, "Unknown character value", ": <%s>#CDATA</%s>", name, name);
1194
1195 break;
1196 }
1197
1198 /*
1199 * Verify our state is correct and then traverse one level up the tree.
1200 * That is, set the parent as the new current node and decrement the
1201 * depth.
1202 */
1203 c_assert(state->n_depth);
1204 c_assert(state->current);
1205 c_assert(state->current->parent);
1206
1207 --state->n_depth;
1208 state->current = state->current->parent;
1209 }
1210
1211 static void config_parser_blob_fn(void *userdata, const XML_Char *data, int n_data) {
1212 ConfigState *state = userdata;
1213 char *t;
1214
1215 /* Shortcut on errors. Simply bail out and skip the handler. */
1216 if (state->error || state->n_failed)
1217 return;
1218
1219 /* Append @data to existing @cdata */
1220 t = malloc(state->current->n_cdata + n_data + 1);
1221 if (!t) {
1222 state->error = error_origin(-ENOMEM);
1223 return;
1224 }
1225
1226 c_memcpy(t, state->current->cdata, state->current->n_cdata);
1227 c_memcpy(t + state->current->n_cdata, data, n_data);
1228 t[state->current->n_cdata + n_data] = 0;
1229 free(state->current->cdata);
1230 state->current->cdata = t;
1231 state->current->n_cdata += n_data;
1232 }
1233
1234 /**
1235 * config_parser_init() - XXX
1236 */
1237 void config_parser_init(ConfigParser *parser) {
1238 void *random;
1239
1240 *parser = (ConfigParser)CONFIG_PARSER_NULL(*parser);
1241
1242 parser->xml = XML_ParserCreate(NULL);
1243 c_assert(parser->xml);
1244
1245 /*
1246 * The hash-tables of libexpat require a reliable random seed.
1247 * Depending on libexpat compilation flags, this might end up using
1248 * `/dev/urandom` and thus block until random-initialization is
1249 * finished. We avoid this hidden dependency and instead use the
1250 * entropy provided via `AT_RANDOM`. Hence, entropy availability is
1251 * tightly coupled to process startup, and it is the job of the
1252 * service manager to order processes accordingly.
1253 */
1254 random = (void *)getauxval(AT_RANDOM);
1255 c_assert(random);
1256 c_memcpy(&parser->salt, random, sizeof(parser->salt));
1257 }
1258
1259 /**
1260 * config_parser_deinit() - XXX
1261 */
1262 void config_parser_deinit(ConfigParser *parser) {
1263 if (parser->xml)
1264 XML_ParserFree(parser->xml);
1265
1266 *parser = (ConfigParser)CONFIG_PARSER_NULL(*parser);
1267 }
1268
1269 static int config_parser_include(ConfigParser *parser, ConfigRoot *root, ConfigNode *node, NSSCache *nss_cache, Dirwatch *dirwatch) {
1270 _c_cleanup_(c_closep) int fd = -1;
1271 char buffer[CONFIG_PARSER_BUFFER_MAX];
1272 ConfigPath *i_file;
1273 ssize_t len;
1274 int r;
1275
1276 c_assert(node->type == CONFIG_NODE_INCLUDE);
1277 c_assert(node->include.file);
1278
1279 c_memset(&parser->state, 0, sizeof(parser->state));
1280 parser->state.nss = nss_cache;
1281 parser->state.dirwatch = dirwatch;
1282 parser->state.file = node->include.file;
1283 parser->state.root = root;
1284 parser->state.current = node;
1285 parser->state.last = node;
1286
1287 /* ignore selinux files if selinux is disabled */
1288 if (node->include.if_selinux_enabled && !bus_selinux_is_enabled())
1289 return 0;
1290
1291 /* ignore recursive inclusions */
1292 for (i_file = node->include.file->parent; i_file; i_file = i_file->parent) {
1293 if (!strcmp(node->include.file->path, i_file->path)) {
1294 CONFIG_ERR(&parser->state, "Recursive inclusion", "");
1295 return 0;
1296 }
1297 }
1298
1299 XML_ParserReset(parser->xml, NULL);
1300 XML_SetHashSalt(parser->xml, parser->salt);
1301 XML_SetUserData(parser->xml, &parser->state);
1302 XML_SetElementHandler(parser->xml, config_parser_begin_fn, config_parser_end_fn);
1303 XML_SetCharacterDataHandler(parser->xml, config_parser_blob_fn);
1304
1305 r = open(node->include.file->path, O_RDONLY | O_CLOEXEC | O_NOCTTY);
1306 if (r < 0) {
1307 if (errno == ENOENT || errno == ENOTDIR) {
1308 if (node->include.ignore_missing)
1309 return 0;
1310
1311 CONFIG_ERR(&parser->state, "Missing configuration file",
1312 ": %s", node->include.file->path);
1313 return CONFIG_E_INVALID;
1314 } else if (errno == EACCES || errno == EPERM) {
1315 CONFIG_ERR(&parser->state, "Access denied",
1316 ": %s", node->include.file->path);
1317 return CONFIG_E_INVALID;
1318 }
1319
1320 return error_origin(-errno);
1321 }
1322 fd = r;
1323
1324 do {
1325 len = read(fd, buffer, sizeof(buffer));
1326 if (len < 0)
1327 return error_origin(-errno);
1328
1329 r = XML_Parse(parser->xml, buffer, len, len ? XML_FALSE : XML_TRUE);
1330 if (r != XML_STATUS_OK) {
1331 CONFIG_ERR(&parser->state, "Invalid XML", ": %s",
1332 XML_ErrorString(XML_GetErrorCode(parser->xml)));
1333 return CONFIG_E_INVALID;
1334 }
1335
1336 if (parser->state.error)
1337 return error_trace(parser->state.error);
1338 } while (len);
1339
1340 c_assert(!parser->state.n_depth);
1341 c_assert(!parser->state.n_failed);
1342
1343 return 0;
1344 }
1345
1346 /**
1347 * config_parser_read() - XXX
1348 */
1349 int config_parser_read(ConfigParser *parser, ConfigRoot **rootp, const char *path, NSSCache *nss_cache, Dirwatch *dirwatch) {
1350 _c_cleanup_(config_root_freep) ConfigRoot *root = NULL;
1351 _c_cleanup_(config_path_unrefp) ConfigPath *file = NULL;
1352 ConfigNode *node;
1353 int r;
1354
1355 /*
1356 * First create a fresh root config entry which we will use to link all
1357 * parsed nodes to. This is what is returned in the end to the caller.
1358 */
1359 r = config_root_new(&root);
1360 if (r)
1361 return error_trace(r);
1362
1363 /*
1364 * Create a fake <include>@path</include> node on the root entry, which
1365 * serves as starting point.
1366 */
1367 r = config_path_new(&file, NULL, NULL, path);
1368 if (r)
1369 return error_trace(r);
1370
1371 r = config_node_new(&node, NULL, CONFIG_NODE_INCLUDE);
1372 if (r)
1373 return error_trace(r);
1374
1375 node->include.file = config_path_ref(file);
1376 c_list_link_front(&root->node_list, &node->root_link);
1377 c_list_link_front(&root->include_list, &node->include_link);
1378
1379 /*
1380 * Now for as long as we find include-nodes linked on
1381 * @root->include_list, we call into config_parser_include(). This will
1382 * fill in all the contents of the include.
1383 */
1384 while ((node = c_list_first_entry(&root->include_list, ConfigNode, include_link))) {
1385 c_list_unlink(&node->include_link);
1386
1387 r = config_parser_include(parser, root, node, nss_cache, dirwatch);
1388 if (r)
1389 return error_trace(r);
1390 }
1391
1392 *rootp = root;
1393 root = NULL;
1394 return 0;
1395 }