1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* config-parser.c XML-library-agnostic configuration file parser
4 * Copyright (C) 2003, 2004 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "config-parser-common.h"
26 #include "config-parser.h"
31 #include <dbus/dbus-list.h>
32 #include <dbus/dbus-internals.h>
33 #include <dbus/dbus-sysdeps.h>
38 /* we ignore policies for unknown groups/users */
53 unsigned int had_content : 1;
59 unsigned int ignore_missing : 1;
60 unsigned int if_selinux_enabled : 1;
61 unsigned int selinux_root_relative : 1;
67 unsigned long gid_uid_or_at_console;
81 * Parser for bus configuration file.
83 struct BusConfigParser
85 int refcount; /**< Reference count */
87 DBusString basedir; /**< Directory we resolve paths relative to */
89 DBusList *stack; /**< stack of Element */
91 char *user; /**< user to run as */
93 char *servicehelper; /**< location of the setuid helper */
95 char *bus_type; /**< Message bus type */
97 DBusList *listen_on; /**< List of addresses to listen to */
99 DBusList *mechanisms; /**< Auth mechanisms */
101 DBusList *service_dirs; /**< Directories to look for session services in */
103 DBusList *conf_dirs; /**< Directories to look for policy configuration in */
105 BusPolicy *policy; /**< Security policy */
107 BusLimits limits; /**< Limits */
109 char *pidfile; /**< PID file */
111 DBusList *included_files; /**< Included files stack */
113 DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
115 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
117 unsigned int syslog : 1; /**< TRUE to enable syslog */
118 unsigned int keep_umask : 1; /**< TRUE to keep original umask when forking */
120 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
122 unsigned int allow_anonymous : 1; /**< TRUE to allow anonymous connections */
126 push_element (BusConfigParser *parser,
131 _dbus_assert (type != ELEMENT_NONE);
133 e = dbus_new0 (Element, 1);
137 if (!_dbus_list_append (&parser->stack, e))
149 element_free (Element *e)
151 if (e->type == ELEMENT_LIMIT)
152 dbus_free (e->d.limit.name);
158 pop_element (BusConfigParser *parser)
162 e = _dbus_list_pop_last (&parser->stack);
168 peek_element (BusConfigParser *parser)
172 e = _dbus_list_get_last (&parser->stack);
178 top_element_type (BusConfigParser *parser)
182 e = _dbus_list_get_last (&parser->stack);
191 merge_service_context_hash (DBusHashTable *dest,
201 _dbus_hash_iter_init (from, &iter);
202 while (_dbus_hash_iter_next (&iter))
204 const char *service = _dbus_hash_iter_get_string_key (&iter);
205 const char *context = _dbus_hash_iter_get_value (&iter);
207 service_copy = _dbus_strdup (service);
208 if (service_copy == NULL)
210 context_copy = _dbus_strdup (context);
211 if (context_copy == NULL)
214 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
225 dbus_free (service_copy);
228 dbus_free (context_copy);
234 service_dirs_find_dir (DBusList **service_dirs,
239 _dbus_assert (dir != NULL);
241 for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
243 const char *link_dir;
245 link_dir = (const char *)link->data;
246 if (strcmp (dir, link_dir) == 0)
254 service_dirs_append_unique_or_free (DBusList **service_dirs,
257 if (!service_dirs_find_dir (service_dirs, dir))
258 return _dbus_list_append (service_dirs, dir);
265 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
268 if (!service_dirs_find_dir (service_dirs, dir_link->data))
270 _dbus_list_append_link (service_dirs, dir_link);
274 dbus_free (dir_link->data);
275 _dbus_list_free_link (dir_link);
280 merge_included (BusConfigParser *parser,
281 BusConfigParser *included,
286 if (!bus_policy_merge (parser->policy,
293 if (!merge_service_context_hash (parser->service_context_table,
294 included->service_context_table))
300 if (included->user != NULL)
302 dbus_free (parser->user);
303 parser->user = included->user;
304 included->user = NULL;
307 if (included->bus_type != NULL)
309 dbus_free (parser->bus_type);
310 parser->bus_type = included->bus_type;
311 included->bus_type = NULL;
317 if (included->keep_umask)
318 parser->keep_umask = TRUE;
320 if (included->pidfile != NULL)
322 dbus_free (parser->pidfile);
323 parser->pidfile = included->pidfile;
324 included->pidfile = NULL;
327 if (included->servicehelper != NULL)
329 dbus_free (parser->servicehelper);
330 parser->servicehelper = included->servicehelper;
331 included->servicehelper = NULL;
334 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
335 _dbus_list_append_link (&parser->listen_on, link);
337 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
338 _dbus_list_append_link (&parser->mechanisms, link);
340 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
341 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
343 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
344 _dbus_list_append_link (&parser->conf_dirs, link);
350 seen_include (BusConfigParser *parser,
351 const DBusString *file)
355 iter = parser->included_files;
358 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
361 iter = _dbus_list_get_next_link (&parser->included_files, iter);
368 bus_config_parser_new (const DBusString *basedir,
369 dbus_bool_t is_toplevel,
370 const BusConfigParser *parent)
372 BusConfigParser *parser;
374 parser = dbus_new0 (BusConfigParser, 1);
378 parser->is_toplevel = !!is_toplevel;
380 if (!_dbus_string_init (&parser->basedir))
386 if (((parser->policy = bus_policy_new ()) == NULL) ||
387 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
388 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
390 dbus_free)) == NULL))
393 bus_policy_unref (parser->policy);
395 _dbus_string_free (&parser->basedir);
403 /* Initialize the parser's limits from the parent. */
404 parser->limits = parent->limits;
406 /* Use the parent's list of included_files to avoid
407 circular inclusions. */
408 parser->included_files = parent->included_files;
413 /* Make up some numbers! woot! */
414 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 127;
415 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 127;
416 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
418 /* We set relatively conservative values here since due to the
419 way SCM_RIGHTS works we need to preallocate an array for the
420 maximum number of file descriptors we can receive. Picking a
421 high value here thus translates directly to more memory
423 parser->limits.max_incoming_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS*4;
424 parser->limits.max_outgoing_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS*4;
425 parser->limits.max_message_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS;
427 /* Making this long means the user has to wait longer for an error
428 * message if something screws up, but making it too short means
429 * they might see a false failure.
431 parser->limits.activation_timeout = 25000; /* 25 seconds */
433 /* Making this long risks making a DOS attack easier, but too short
434 * and legitimate auth will fail. If interactive auth (ask user for
435 * password) is allowed, then potentially it has to be quite long.
437 parser->limits.auth_timeout = 30000; /* 30 seconds */
439 parser->limits.max_incomplete_connections = 64;
440 parser->limits.max_connections_per_user = 256;
442 /* Note that max_completed_connections / max_connections_per_user
443 * is the number of users that would have to work together to
444 * DOS all the other users.
446 parser->limits.max_completed_connections = 2048;
448 parser->limits.max_pending_activations = 512;
449 parser->limits.max_services_per_connection = 512;
451 /* For this one, keep in mind that it isn't only the memory used
452 * by the match rules, but slowdown from linearly walking a big
453 * list of them. A client adding more than this is almost
454 * certainly a bad idea for that reason, and should change to a
455 * smaller number of wider-net match rules - getting every last
456 * message to the bus is probably better than having a thousand
459 parser->limits.max_match_rules_per_connection = 512;
461 parser->limits.reply_timeout = -1; /* never */
463 /* this is effectively a limit on message queue size for messages
464 * that require a reply
466 parser->limits.max_replies_per_connection = 1024*8;
469 parser->refcount = 1;
475 bus_config_parser_ref (BusConfigParser *parser)
477 _dbus_assert (parser->refcount > 0);
479 parser->refcount += 1;
485 bus_config_parser_unref (BusConfigParser *parser)
487 _dbus_assert (parser->refcount > 0);
489 parser->refcount -= 1;
491 if (parser->refcount == 0)
493 while (parser->stack != NULL)
494 pop_element (parser);
496 dbus_free (parser->user);
497 dbus_free (parser->servicehelper);
498 dbus_free (parser->bus_type);
499 dbus_free (parser->pidfile);
501 _dbus_list_foreach (&parser->listen_on,
502 (DBusForeachFunction) dbus_free,
505 _dbus_list_clear (&parser->listen_on);
507 _dbus_list_foreach (&parser->service_dirs,
508 (DBusForeachFunction) dbus_free,
511 _dbus_list_clear (&parser->service_dirs);
513 _dbus_list_foreach (&parser->conf_dirs,
514 (DBusForeachFunction) dbus_free,
517 _dbus_list_clear (&parser->conf_dirs);
519 _dbus_list_foreach (&parser->mechanisms,
520 (DBusForeachFunction) dbus_free,
523 _dbus_list_clear (&parser->mechanisms);
525 _dbus_string_free (&parser->basedir);
528 bus_policy_unref (parser->policy);
530 if (parser->service_context_table)
531 _dbus_hash_table_unref (parser->service_context_table);
538 bus_config_parser_check_doctype (BusConfigParser *parser,
542 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
544 if (strcmp (doctype, "busconfig") != 0)
546 dbus_set_error (error,
548 "Configuration file has the wrong document type %s",
563 locate_attributes (BusConfigParser *parser,
564 const char *element_name,
565 const char **attribute_names,
566 const char **attribute_values,
568 const char *first_attribute_name,
569 const char **first_attribute_retloc,
577 LocateAttr attrs[MAX_ATTRS];
581 _dbus_assert (first_attribute_name != NULL);
582 _dbus_assert (first_attribute_retloc != NULL);
587 attrs[0].name = first_attribute_name;
588 attrs[0].retloc = first_attribute_retloc;
589 *first_attribute_retloc = NULL;
591 va_start (args, first_attribute_retloc);
593 name = va_arg (args, const char*);
594 retloc = va_arg (args, const char**);
598 _dbus_assert (retloc != NULL);
599 _dbus_assert (n_attrs < MAX_ATTRS);
601 attrs[n_attrs].name = name;
602 attrs[n_attrs].retloc = retloc;
606 name = va_arg (args, const char*);
607 retloc = va_arg (args, const char**);
613 while (attribute_names[i])
622 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
624 retloc = attrs[j].retloc;
628 dbus_set_error (error, DBUS_ERROR_FAILED,
629 "Attribute \"%s\" repeated twice on the same <%s> element",
630 attrs[j].name, element_name);
635 *retloc = attribute_values[i];
644 dbus_set_error (error, DBUS_ERROR_FAILED,
645 "Attribute \"%s\" is invalid on <%s> element in this context",
646 attribute_names[i], element_name);
659 check_no_attributes (BusConfigParser *parser,
660 const char *element_name,
661 const char **attribute_names,
662 const char **attribute_values,
665 if (attribute_names[0] != NULL)
667 dbus_set_error (error, DBUS_ERROR_FAILED,
668 "Attribute \"%s\" is invalid on <%s> element in this context",
669 attribute_names[0], element_name);
677 start_busconfig_child (BusConfigParser *parser,
678 const char *element_name,
679 const char **attribute_names,
680 const char **attribute_values,
683 ElementType element_type;
685 element_type = bus_config_parser_element_name_to_type (element_name);
687 if (element_type == ELEMENT_USER)
689 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
692 if (push_element (parser, ELEMENT_USER) == NULL)
700 else if (element_type == ELEMENT_CONFIGTYPE)
702 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
705 if (push_element (parser, ELEMENT_CONFIGTYPE) == NULL)
713 else if (element_type == ELEMENT_FORK)
715 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
718 if (push_element (parser, ELEMENT_FORK) == NULL)
728 else if (element_type == ELEMENT_SYSLOG)
730 if (!check_no_attributes (parser, "syslog", attribute_names, attribute_values, error))
733 if (push_element (parser, ELEMENT_SYSLOG) == NULL)
739 parser->syslog = TRUE;
743 else if (element_type == ELEMENT_KEEP_UMASK)
745 if (!check_no_attributes (parser, "keep_umask", attribute_names, attribute_values, error))
748 if (push_element (parser, ELEMENT_KEEP_UMASK) == NULL)
754 parser->keep_umask = TRUE;
758 else if (element_type == ELEMENT_PIDFILE)
760 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
763 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
771 else if (element_type == ELEMENT_LISTEN)
773 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
776 if (push_element (parser, ELEMENT_LISTEN) == NULL)
784 else if (element_type == ELEMENT_AUTH)
786 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
789 if (push_element (parser, ELEMENT_AUTH) == NULL)
797 else if (element_type == ELEMENT_SERVICEHELPER)
799 if (!check_no_attributes (parser, "servicehelper", attribute_names, attribute_values, error))
802 if (push_element (parser, ELEMENT_SERVICEHELPER) == NULL)
810 else if (element_type == ELEMENT_INCLUDEDIR)
812 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
815 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
823 else if (element_type == ELEMENT_STANDARD_SESSION_SERVICEDIRS)
829 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
832 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
838 if (!_dbus_get_standard_session_servicedirs (&dirs))
844 while ((link = _dbus_list_pop_first_link (&dirs)))
845 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
849 else if (element_type == ELEMENT_STANDARD_SYSTEM_SERVICEDIRS)
855 if (!check_no_attributes (parser, "standard_system_servicedirs", attribute_names, attribute_values, error))
858 if (push_element (parser, ELEMENT_STANDARD_SYSTEM_SERVICEDIRS) == NULL)
864 if (!_dbus_get_standard_system_servicedirs (&dirs))
870 while ((link = _dbus_list_pop_first_link (&dirs)))
871 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
875 else if (element_type == ELEMENT_ALLOW_ANONYMOUS)
877 if (!check_no_attributes (parser, "allow_anonymous", attribute_names, attribute_values, error))
880 if (push_element (parser, ELEMENT_ALLOW_ANONYMOUS) == NULL)
886 parser->allow_anonymous = TRUE;
889 else if (element_type == ELEMENT_SERVICEDIR)
891 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
894 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
902 else if (element_type == ELEMENT_INCLUDE)
905 const char *if_selinux_enabled;
906 const char *ignore_missing;
907 const char *selinux_root_relative;
909 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
915 e->d.include.ignore_missing = FALSE;
916 e->d.include.if_selinux_enabled = FALSE;
917 e->d.include.selinux_root_relative = FALSE;
919 if (!locate_attributes (parser, "include",
923 "ignore_missing", &ignore_missing,
924 "if_selinux_enabled", &if_selinux_enabled,
925 "selinux_root_relative", &selinux_root_relative,
929 if (ignore_missing != NULL)
931 if (strcmp (ignore_missing, "yes") == 0)
932 e->d.include.ignore_missing = TRUE;
933 else if (strcmp (ignore_missing, "no") == 0)
934 e->d.include.ignore_missing = FALSE;
937 dbus_set_error (error, DBUS_ERROR_FAILED,
938 "ignore_missing attribute must have value \"yes\" or \"no\"");
943 if (if_selinux_enabled != NULL)
945 if (strcmp (if_selinux_enabled, "yes") == 0)
946 e->d.include.if_selinux_enabled = TRUE;
947 else if (strcmp (if_selinux_enabled, "no") == 0)
948 e->d.include.if_selinux_enabled = FALSE;
951 dbus_set_error (error, DBUS_ERROR_FAILED,
952 "if_selinux_enabled attribute must have value"
953 " \"yes\" or \"no\"");
958 if (selinux_root_relative != NULL)
960 if (strcmp (selinux_root_relative, "yes") == 0)
961 e->d.include.selinux_root_relative = TRUE;
962 else if (strcmp (selinux_root_relative, "no") == 0)
963 e->d.include.selinux_root_relative = FALSE;
966 dbus_set_error (error, DBUS_ERROR_FAILED,
967 "selinux_root_relative attribute must have value"
968 " \"yes\" or \"no\"");
975 else if (element_type == ELEMENT_POLICY)
981 const char *at_console;
983 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
989 e->d.policy.type = POLICY_IGNORED;
991 if (!locate_attributes (parser, "policy",
998 "at_console", &at_console,
1002 if (((context && user) ||
1003 (context && group) ||
1004 (context && at_console)) ||
1006 (user && at_console)) ||
1007 (group && at_console) ||
1008 !(context || user || group || at_console))
1010 dbus_set_error (error, DBUS_ERROR_FAILED,
1011 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
1015 if (context != NULL)
1017 if (strcmp (context, "default") == 0)
1019 e->d.policy.type = POLICY_DEFAULT;
1021 else if (strcmp (context, "mandatory") == 0)
1023 e->d.policy.type = POLICY_MANDATORY;
1027 dbus_set_error (error, DBUS_ERROR_FAILED,
1028 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
1033 else if (user != NULL)
1035 DBusString username;
1036 _dbus_string_init_const (&username, user);
1038 if (_dbus_parse_unix_user_from_config (&username,
1039 &e->d.policy.gid_uid_or_at_console))
1040 e->d.policy.type = POLICY_USER;
1042 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
1045 else if (group != NULL)
1047 DBusString group_name;
1048 _dbus_string_init_const (&group_name, group);
1050 if (_dbus_parse_unix_group_from_config (&group_name,
1051 &e->d.policy.gid_uid_or_at_console))
1052 e->d.policy.type = POLICY_GROUP;
1054 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
1057 else if (at_console != NULL)
1060 t = (strcmp (at_console, "true") == 0);
1061 if (t || strcmp (at_console, "false") == 0)
1063 e->d.policy.gid_uid_or_at_console = t;
1064 e->d.policy.type = POLICY_CONSOLE;
1068 dbus_set_error (error, DBUS_ERROR_FAILED,
1069 "Unknown value \"%s\" for at_console in message bus configuration file",
1077 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1082 else if (element_type == ELEMENT_LIMIT)
1087 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1089 BUS_SET_OOM (error);
1093 if (!locate_attributes (parser, "limit",
1103 dbus_set_error (error, DBUS_ERROR_FAILED,
1104 "<limit> element must have a \"name\" attribute");
1108 e->d.limit.name = _dbus_strdup (name);
1109 if (e->d.limit.name == NULL)
1111 BUS_SET_OOM (error);
1117 else if (element_type == ELEMENT_SELINUX)
1119 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1122 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1124 BUS_SET_OOM (error);
1132 dbus_set_error (error, DBUS_ERROR_FAILED,
1133 "Element <%s> not allowed inside <%s> in configuration file",
1134 element_name, "busconfig");
1140 append_rule_from_element (BusConfigParser *parser,
1141 const char *element_name,
1142 const char **attribute_names,
1143 const char **attribute_values,
1148 const char *send_interface;
1149 const char *send_member;
1150 const char *send_error;
1151 const char *send_destination;
1152 const char *send_path;
1153 const char *send_type;
1154 const char *receive_interface;
1155 const char *receive_member;
1156 const char *receive_error;
1157 const char *receive_sender;
1158 const char *receive_path;
1159 const char *receive_type;
1160 const char *eavesdrop;
1161 const char *send_requested_reply;
1162 const char *receive_requested_reply;
1164 const char *own_prefix;
1168 BusPolicyRule *rule;
1170 if (!locate_attributes (parser, element_name,
1174 "send_interface", &send_interface,
1175 "send_member", &send_member,
1176 "send_error", &send_error,
1177 "send_destination", &send_destination,
1178 "send_path", &send_path,
1179 "send_type", &send_type,
1180 "receive_interface", &receive_interface,
1181 "receive_member", &receive_member,
1182 "receive_error", &receive_error,
1183 "receive_sender", &receive_sender,
1184 "receive_path", &receive_path,
1185 "receive_type", &receive_type,
1186 "eavesdrop", &eavesdrop,
1187 "send_requested_reply", &send_requested_reply,
1188 "receive_requested_reply", &receive_requested_reply,
1190 "own_prefix", &own_prefix,
1197 if (!(send_interface || send_member || send_error || send_destination ||
1198 send_type || send_path ||
1199 receive_interface || receive_member || receive_error || receive_sender ||
1200 receive_type || receive_path || eavesdrop ||
1201 send_requested_reply || receive_requested_reply ||
1202 own || own_prefix || user || group))
1204 dbus_set_error (error, DBUS_ERROR_FAILED,
1205 "Element <%s> must have one or more attributes",
1210 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1211 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1213 dbus_set_error (error, DBUS_ERROR_FAILED,
1214 "On element <%s>, if you specify a member you must specify an interface or a path. Keep in mind that not all messages have an interface field.",
1219 /* Allowed combinations of elements are:
1221 * base, must be all send or all receive:
1224 * interface + member
1227 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1228 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1230 * user, group, own, own_prefix must occur alone
1232 * Pretty sure the below stuff is broken, FIXME think about it more.
1235 if ((send_interface && (send_error ||
1236 receive_interface ||
1240 receive_requested_reply ||
1241 own || own_prefix ||
1245 (send_member && (send_error ||
1246 receive_interface ||
1250 receive_requested_reply ||
1251 own || own_prefix ||
1255 (send_error && (receive_interface ||
1259 receive_requested_reply ||
1260 own || own_prefix ||
1264 (send_destination && (receive_interface ||
1268 receive_requested_reply ||
1269 own || own_prefix ||
1273 (send_type && (receive_interface ||
1277 receive_requested_reply ||
1278 own || own_prefix ||
1282 (send_path && (receive_interface ||
1286 receive_requested_reply ||
1287 own || own_prefix ||
1291 (send_requested_reply && (receive_interface ||
1295 receive_requested_reply ||
1296 own || own_prefix ||
1300 (receive_interface && (receive_error ||
1301 own || own_prefix ||
1305 (receive_member && (receive_error ||
1306 own || own_prefix ||
1310 (receive_error && (own || own_prefix ||
1314 (eavesdrop && (own || own_prefix ||
1318 (receive_requested_reply && (own || own_prefix ||
1322 (own && (own_prefix || user || group)) ||
1324 (own_prefix && (own || user || group)) ||
1328 dbus_set_error (error, DBUS_ERROR_FAILED,
1329 "Invalid combination of attributes on element <%s>",
1336 /* In BusPolicyRule, NULL represents wildcard.
1337 * In the config file, '*' represents it.
1339 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1341 if (send_interface || send_member || send_error || send_destination ||
1342 send_path || send_type || send_requested_reply)
1346 if (IS_WILDCARD (send_interface))
1347 send_interface = NULL;
1348 if (IS_WILDCARD (send_member))
1350 if (IS_WILDCARD (send_error))
1352 if (IS_WILDCARD (send_destination))
1353 send_destination = NULL;
1354 if (IS_WILDCARD (send_path))
1356 if (IS_WILDCARD (send_type))
1359 message_type = DBUS_MESSAGE_TYPE_INVALID;
1360 if (send_type != NULL)
1362 message_type = dbus_message_type_from_string (send_type);
1363 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1365 dbus_set_error (error, DBUS_ERROR_FAILED,
1366 "Bad message type \"%s\"",
1373 !(strcmp (eavesdrop, "true") == 0 ||
1374 strcmp (eavesdrop, "false") == 0))
1376 dbus_set_error (error, DBUS_ERROR_FAILED,
1377 "Bad value \"%s\" for %s attribute, must be true or false",
1378 "eavesdrop", eavesdrop);
1382 if (send_requested_reply &&
1383 !(strcmp (send_requested_reply, "true") == 0 ||
1384 strcmp (send_requested_reply, "false") == 0))
1386 dbus_set_error (error, DBUS_ERROR_FAILED,
1387 "Bad value \"%s\" for %s attribute, must be true or false",
1388 "send_requested_reply", send_requested_reply);
1392 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1397 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1400 rule->d.send.log = (strcmp (log, "true") == 0);
1402 if (send_requested_reply)
1403 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1405 rule->d.send.message_type = message_type;
1406 rule->d.send.path = _dbus_strdup (send_path);
1407 rule->d.send.interface = _dbus_strdup (send_interface);
1408 rule->d.send.member = _dbus_strdup (send_member);
1409 rule->d.send.error = _dbus_strdup (send_error);
1410 rule->d.send.destination = _dbus_strdup (send_destination);
1411 if (send_path && rule->d.send.path == NULL)
1413 if (send_interface && rule->d.send.interface == NULL)
1415 if (send_member && rule->d.send.member == NULL)
1417 if (send_error && rule->d.send.error == NULL)
1419 if (send_destination && rule->d.send.destination == NULL)
1422 else if (receive_interface || receive_member || receive_error || receive_sender ||
1423 receive_path || receive_type || eavesdrop || receive_requested_reply)
1427 if (IS_WILDCARD (receive_interface))
1428 receive_interface = NULL;
1429 if (IS_WILDCARD (receive_member))
1430 receive_member = NULL;
1431 if (IS_WILDCARD (receive_error))
1432 receive_error = NULL;
1433 if (IS_WILDCARD (receive_sender))
1434 receive_sender = NULL;
1435 if (IS_WILDCARD (receive_path))
1436 receive_path = NULL;
1437 if (IS_WILDCARD (receive_type))
1438 receive_type = NULL;
1440 message_type = DBUS_MESSAGE_TYPE_INVALID;
1441 if (receive_type != NULL)
1443 message_type = dbus_message_type_from_string (receive_type);
1444 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1446 dbus_set_error (error, DBUS_ERROR_FAILED,
1447 "Bad message type \"%s\"",
1455 !(strcmp (eavesdrop, "true") == 0 ||
1456 strcmp (eavesdrop, "false") == 0))
1458 dbus_set_error (error, DBUS_ERROR_FAILED,
1459 "Bad value \"%s\" for %s attribute, must be true or false",
1460 "eavesdrop", eavesdrop);
1464 if (receive_requested_reply &&
1465 !(strcmp (receive_requested_reply, "true") == 0 ||
1466 strcmp (receive_requested_reply, "false") == 0))
1468 dbus_set_error (error, DBUS_ERROR_FAILED,
1469 "Bad value \"%s\" for %s attribute, must be true or false",
1470 "receive_requested_reply", receive_requested_reply);
1474 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1479 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1481 if (receive_requested_reply)
1482 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1484 rule->d.receive.message_type = message_type;
1485 rule->d.receive.path = _dbus_strdup (receive_path);
1486 rule->d.receive.interface = _dbus_strdup (receive_interface);
1487 rule->d.receive.member = _dbus_strdup (receive_member);
1488 rule->d.receive.error = _dbus_strdup (receive_error);
1489 rule->d.receive.origin = _dbus_strdup (receive_sender);
1491 if (receive_path && rule->d.receive.path == NULL)
1493 if (receive_interface && rule->d.receive.interface == NULL)
1495 if (receive_member && rule->d.receive.member == NULL)
1497 if (receive_error && rule->d.receive.error == NULL)
1499 if (receive_sender && rule->d.receive.origin == NULL)
1502 else if (own || own_prefix)
1504 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1510 if (IS_WILDCARD (own))
1513 rule->d.own.prefix = 0;
1514 rule->d.own.service_name = _dbus_strdup (own);
1515 if (own && rule->d.own.service_name == NULL)
1520 rule->d.own.prefix = 1;
1521 rule->d.own.service_name = _dbus_strdup (own_prefix);
1522 if (rule->d.own.service_name == NULL)
1528 if (IS_WILDCARD (user))
1530 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1534 rule->d.user.uid = DBUS_UID_UNSET;
1538 DBusString username;
1541 _dbus_string_init_const (&username, user);
1543 if (_dbus_parse_unix_user_from_config (&username, &uid))
1545 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1549 rule->d.user.uid = uid;
1553 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1554 user, element_name);
1560 if (IS_WILDCARD (group))
1562 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1566 rule->d.group.gid = DBUS_GID_UNSET;
1570 DBusString groupname;
1573 _dbus_string_init_const (&groupname, group);
1575 if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1577 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1581 rule->d.group.gid = gid;
1585 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1586 group, element_name);
1591 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1597 pe = peek_element (parser);
1598 _dbus_assert (pe != NULL);
1599 _dbus_assert (pe->type == ELEMENT_POLICY);
1601 switch (pe->d.policy.type)
1603 case POLICY_IGNORED:
1604 /* drop the rule on the floor */
1607 case POLICY_DEFAULT:
1608 if (!bus_policy_append_default_rule (parser->policy, rule))
1611 case POLICY_MANDATORY:
1612 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1616 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1618 dbus_set_error (error, DBUS_ERROR_FAILED,
1619 "<%s> rule cannot be per-user because it has bus-global semantics",
1624 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1629 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1631 dbus_set_error (error, DBUS_ERROR_FAILED,
1632 "<%s> rule cannot be per-group because it has bus-global semantics",
1637 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1643 case POLICY_CONSOLE:
1644 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1650 bus_policy_rule_unref (rule);
1657 BUS_SET_OOM (error);
1660 bus_policy_rule_unref (rule);
1665 start_policy_child (BusConfigParser *parser,
1666 const char *element_name,
1667 const char **attribute_names,
1668 const char **attribute_values,
1671 if (strcmp (element_name, "allow") == 0)
1673 if (!append_rule_from_element (parser, element_name,
1674 attribute_names, attribute_values,
1678 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1680 BUS_SET_OOM (error);
1686 else if (strcmp (element_name, "deny") == 0)
1688 if (!append_rule_from_element (parser, element_name,
1689 attribute_names, attribute_values,
1693 if (push_element (parser, ELEMENT_DENY) == NULL)
1695 BUS_SET_OOM (error);
1703 dbus_set_error (error, DBUS_ERROR_FAILED,
1704 "Element <%s> not allowed inside <%s> in configuration file",
1705 element_name, "policy");
1711 start_selinux_child (BusConfigParser *parser,
1712 const char *element_name,
1713 const char **attribute_names,
1714 const char **attribute_values,
1721 context_copy = NULL;
1723 if (strcmp (element_name, "associate") == 0)
1726 const char *context;
1728 if (!locate_attributes (parser, "associate",
1733 "context", &context,
1737 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1739 BUS_SET_OOM (error);
1743 if (own == NULL || context == NULL)
1745 dbus_set_error (error, DBUS_ERROR_FAILED,
1746 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1750 own_copy = _dbus_strdup (own);
1751 if (own_copy == NULL)
1753 context_copy = _dbus_strdup (context);
1754 if (context_copy == NULL)
1757 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1758 own_copy, context_copy))
1765 dbus_set_error (error, DBUS_ERROR_FAILED,
1766 "Element <%s> not allowed inside <%s> in configuration file",
1767 element_name, "selinux");
1773 dbus_free (own_copy);
1776 dbus_free (context_copy);
1778 BUS_SET_OOM (error);
1783 bus_config_parser_start_element (BusConfigParser *parser,
1784 const char *element_name,
1785 const char **attribute_names,
1786 const char **attribute_values,
1791 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1793 /* printf ("START: %s\n", element_name); */
1795 t = top_element_type (parser);
1797 if (t == ELEMENT_NONE)
1799 if (strcmp (element_name, "busconfig") == 0)
1801 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1804 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1806 BUS_SET_OOM (error);
1814 dbus_set_error (error, DBUS_ERROR_FAILED,
1815 "Unknown element <%s> at root of configuration file",
1820 else if (t == ELEMENT_BUSCONFIG)
1822 return start_busconfig_child (parser, element_name,
1823 attribute_names, attribute_values,
1826 else if (t == ELEMENT_POLICY)
1828 return start_policy_child (parser, element_name,
1829 attribute_names, attribute_values,
1832 else if (t == ELEMENT_SELINUX)
1834 return start_selinux_child (parser, element_name,
1835 attribute_names, attribute_values,
1840 dbus_set_error (error, DBUS_ERROR_FAILED,
1841 "Element <%s> is not allowed in this context",
1848 set_limit (BusConfigParser *parser,
1853 dbus_bool_t must_be_positive;
1854 dbus_bool_t must_be_int;
1856 must_be_int = FALSE;
1857 must_be_positive = FALSE;
1859 if (strcmp (name, "max_incoming_bytes") == 0)
1861 must_be_positive = TRUE;
1862 parser->limits.max_incoming_bytes = value;
1864 else if (strcmp (name, "max_incoming_unix_fds") == 0)
1866 must_be_positive = TRUE;
1867 parser->limits.max_incoming_unix_fds = value;
1869 else if (strcmp (name, "max_outgoing_bytes") == 0)
1871 must_be_positive = TRUE;
1872 parser->limits.max_outgoing_bytes = value;
1874 else if (strcmp (name, "max_outgoing_unix_fds") == 0)
1876 must_be_positive = TRUE;
1877 parser->limits.max_outgoing_unix_fds = value;
1879 else if (strcmp (name, "max_message_size") == 0)
1881 must_be_positive = TRUE;
1882 parser->limits.max_message_size = value;
1884 else if (strcmp (name, "max_message_unix_fds") == 0)
1886 must_be_positive = TRUE;
1887 parser->limits.max_message_unix_fds = value;
1889 else if (strcmp (name, "service_start_timeout") == 0)
1891 must_be_positive = TRUE;
1893 parser->limits.activation_timeout = value;
1895 else if (strcmp (name, "auth_timeout") == 0)
1897 must_be_positive = TRUE;
1899 parser->limits.auth_timeout = value;
1901 else if (strcmp (name, "reply_timeout") == 0)
1903 must_be_positive = TRUE;
1905 parser->limits.reply_timeout = value;
1907 else if (strcmp (name, "max_completed_connections") == 0)
1909 must_be_positive = TRUE;
1911 parser->limits.max_completed_connections = value;
1913 else if (strcmp (name, "max_incomplete_connections") == 0)
1915 must_be_positive = TRUE;
1917 parser->limits.max_incomplete_connections = value;
1919 else if (strcmp (name, "max_connections_per_user") == 0)
1921 must_be_positive = TRUE;
1923 parser->limits.max_connections_per_user = value;
1925 else if (strcmp (name, "max_pending_service_starts") == 0)
1927 must_be_positive = TRUE;
1929 parser->limits.max_pending_activations = value;
1931 else if (strcmp (name, "max_names_per_connection") == 0)
1933 must_be_positive = TRUE;
1935 parser->limits.max_services_per_connection = value;
1937 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1939 must_be_positive = TRUE;
1941 parser->limits.max_match_rules_per_connection = value;
1943 else if (strcmp (name, "max_replies_per_connection") == 0)
1945 must_be_positive = TRUE;
1947 parser->limits.max_replies_per_connection = value;
1951 dbus_set_error (error, DBUS_ERROR_FAILED,
1952 "There is no limit called \"%s\"\n",
1957 if (must_be_positive && value < 0)
1959 dbus_set_error (error, DBUS_ERROR_FAILED,
1960 "<limit name=\"%s\"> must be a positive number\n",
1966 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1968 dbus_set_error (error, DBUS_ERROR_FAILED,
1969 "<limit name=\"%s\"> value is too large\n",
1978 bus_config_parser_end_element (BusConfigParser *parser,
1979 const char *element_name,
1986 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1988 /* printf ("END: %s\n", element_name); */
1990 t = top_element_type (parser);
1992 if (t == ELEMENT_NONE)
1994 /* should probably be an assertion failure but
1995 * being paranoid about XML parsers
1997 dbus_set_error (error, DBUS_ERROR_FAILED,
1998 "XML parser ended element with no element on the stack");
2002 n = bus_config_parser_element_type_to_name (t);
2003 _dbus_assert (n != NULL);
2004 if (strcmp (n, element_name) != 0)
2006 /* should probably be an assertion failure but
2007 * being paranoid about XML parsers
2009 dbus_set_error (error, DBUS_ERROR_FAILED,
2010 "XML element <%s> ended but topmost element on the stack was <%s>",
2015 e = peek_element (parser);
2016 _dbus_assert (e != NULL);
2021 _dbus_assert_not_reached ("element in stack has no type");
2024 case ELEMENT_INCLUDE:
2026 case ELEMENT_CONFIGTYPE:
2027 case ELEMENT_LISTEN:
2028 case ELEMENT_PIDFILE:
2030 case ELEMENT_SERVICEDIR:
2031 case ELEMENT_SERVICEHELPER:
2032 case ELEMENT_INCLUDEDIR:
2034 if (!e->had_content)
2036 dbus_set_error (error, DBUS_ERROR_FAILED,
2037 "XML element <%s> was expected to have content inside it",
2038 bus_config_parser_element_type_to_name (e->type));
2042 if (e->type == ELEMENT_LIMIT)
2044 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
2050 case ELEMENT_BUSCONFIG:
2051 case ELEMENT_POLICY:
2055 case ELEMENT_SYSLOG:
2056 case ELEMENT_KEEP_UMASK:
2057 case ELEMENT_SELINUX:
2058 case ELEMENT_ASSOCIATE:
2059 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2060 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2061 case ELEMENT_ALLOW_ANONYMOUS:
2065 pop_element (parser);
2071 all_whitespace (const DBusString *str)
2075 _dbus_string_skip_white (str, 0, &i);
2077 return i == _dbus_string_get_length (str);
2081 make_full_path (const DBusString *basedir,
2082 const DBusString *filename,
2083 DBusString *full_path)
2085 if (_dbus_path_is_absolute (filename))
2087 return _dbus_string_copy (filename, 0, full_path, 0);
2091 if (!_dbus_string_copy (basedir, 0, full_path, 0))
2094 if (!_dbus_concat_dir_and_file (full_path, filename))
2102 include_file (BusConfigParser *parser,
2103 const DBusString *filename,
2104 dbus_bool_t ignore_missing,
2107 /* FIXME good test case for this would load each config file in the
2108 * test suite both alone, and as an include, and check
2109 * that the result is the same
2111 BusConfigParser *included;
2112 const char *filename_str;
2113 DBusError tmp_error;
2115 dbus_error_init (&tmp_error);
2117 filename_str = _dbus_string_get_const_data (filename);
2119 /* Check to make sure this file hasn't already been included. */
2120 if (seen_include (parser, filename))
2122 dbus_set_error (error, DBUS_ERROR_FAILED,
2123 "Circular inclusion of file '%s'",
2128 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2130 BUS_SET_OOM (error);
2134 /* Since parser is passed in as the parent, included
2135 inherits parser's limits. */
2136 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2138 _dbus_list_pop_last (&parser->included_files);
2140 if (included == NULL)
2142 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2144 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2147 dbus_error_free (&tmp_error);
2152 dbus_move_error (&tmp_error, error);
2158 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2160 if (!merge_included (parser, included, error))
2162 bus_config_parser_unref (included);
2166 /* Copy included's limits back to parser. */
2167 parser->limits = included->limits;
2169 bus_config_parser_unref (included);
2175 servicehelper_path (BusConfigParser *parser,
2176 const DBusString *filename,
2179 const char *filename_str;
2180 char *servicehelper;
2182 filename_str = _dbus_string_get_const_data (filename);
2184 /* copy to avoid overwriting with NULL on OOM */
2185 servicehelper = _dbus_strdup (filename_str);
2188 if (servicehelper == NULL)
2190 BUS_SET_OOM (error);
2194 /* save the latest servicehelper only if not OOM */
2195 dbus_free (parser->servicehelper);
2196 parser->servicehelper = servicehelper;
2198 /* We don't check whether the helper exists; instead we
2199 * would just fail to ever activate anything if it doesn't.
2200 * This allows an admin to fix the problem if it doesn't exist.
2201 * It also allows the parser test suite to successfully parse
2202 * test cases without installing the helper. ;-)
2209 include_dir (BusConfigParser *parser,
2210 const DBusString *dirname,
2213 DBusString filename;
2215 DBusError tmp_error;
2219 if (!_dbus_string_init (&filename))
2221 BUS_SET_OOM (error);
2227 dir = _dbus_directory_open (dirname, error);
2232 dbus_error_init (&tmp_error);
2233 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2235 DBusString full_path;
2237 if (!_dbus_string_init (&full_path))
2239 BUS_SET_OOM (error);
2243 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2245 BUS_SET_OOM (error);
2246 _dbus_string_free (&full_path);
2250 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2252 BUS_SET_OOM (error);
2253 _dbus_string_free (&full_path);
2257 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2259 if (!include_file (parser, &full_path, TRUE, error))
2261 if (dbus_error_is_set (error))
2263 /* We log to syslog unconditionally here, because this is
2264 * the configuration parser, so we don't yet know whether
2265 * this bus is going to want to write to syslog! (There's
2266 * also some layer inversion going on, if we want to use
2267 * the bus context.) */
2268 _dbus_system_log (DBUS_SYSTEM_LOG_INFO,
2269 "Encountered error '%s' while parsing '%s'\n",
2271 _dbus_string_get_const_data (&full_path));
2272 dbus_error_free (error);
2277 _dbus_string_free (&full_path);
2280 if (dbus_error_is_set (&tmp_error))
2282 dbus_move_error (&tmp_error, error);
2287 if (!_dbus_string_copy_data (dirname, &s))
2289 BUS_SET_OOM (error);
2293 if (!_dbus_list_append (&parser->conf_dirs, s))
2296 BUS_SET_OOM (error);
2303 _dbus_string_free (&filename);
2306 _dbus_directory_close (dir);
2312 bus_config_parser_content (BusConfigParser *parser,
2313 const DBusString *content,
2318 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2324 _dbus_string_get_const_data (content, &c_str);
2326 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2330 e = peek_element (parser);
2333 dbus_set_error (error, DBUS_ERROR_FAILED,
2334 "Text content outside of any XML element in configuration file");
2337 else if (e->had_content)
2339 _dbus_assert_not_reached ("Element had multiple content blocks");
2343 switch (top_element_type (parser))
2346 _dbus_assert_not_reached ("element at top of stack has no type");
2349 case ELEMENT_BUSCONFIG:
2350 case ELEMENT_POLICY:
2354 case ELEMENT_SYSLOG:
2355 case ELEMENT_KEEP_UMASK:
2356 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2357 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2358 case ELEMENT_ALLOW_ANONYMOUS:
2359 case ELEMENT_SELINUX:
2360 case ELEMENT_ASSOCIATE:
2361 if (all_whitespace (content))
2365 dbus_set_error (error, DBUS_ERROR_FAILED,
2366 "No text content expected inside XML element %s in configuration file",
2367 bus_config_parser_element_type_to_name (top_element_type (parser)));
2371 case ELEMENT_PIDFILE:
2375 e->had_content = TRUE;
2377 if (!_dbus_string_copy_data (content, &s))
2380 dbus_free (parser->pidfile);
2381 parser->pidfile = s;
2385 case ELEMENT_INCLUDE:
2387 DBusString full_path, selinux_policy_root;
2389 e->had_content = TRUE;
2391 if (e->d.include.if_selinux_enabled
2392 && !bus_selinux_enabled ())
2395 if (!_dbus_string_init (&full_path))
2398 if (e->d.include.selinux_root_relative)
2400 if (!bus_selinux_get_policy_root ())
2402 dbus_set_error (error, DBUS_ERROR_FAILED,
2403 "Could not determine SELinux policy root for relative inclusion");
2404 _dbus_string_free (&full_path);
2407 _dbus_string_init_const (&selinux_policy_root,
2408 bus_selinux_get_policy_root ());
2409 if (!make_full_path (&selinux_policy_root, content, &full_path))
2411 _dbus_string_free (&full_path);
2415 else if (!make_full_path (&parser->basedir, content, &full_path))
2417 _dbus_string_free (&full_path);
2421 if (!include_file (parser, &full_path,
2422 e->d.include.ignore_missing, error))
2424 _dbus_string_free (&full_path);
2428 _dbus_string_free (&full_path);
2432 case ELEMENT_SERVICEHELPER:
2434 DBusString full_path;
2436 e->had_content = TRUE;
2438 if (!_dbus_string_init (&full_path))
2441 if (!make_full_path (&parser->basedir, content, &full_path))
2443 _dbus_string_free (&full_path);
2447 if (!servicehelper_path (parser, &full_path, error))
2449 _dbus_string_free (&full_path);
2453 _dbus_string_free (&full_path);
2457 case ELEMENT_INCLUDEDIR:
2459 DBusString full_path;
2461 e->had_content = TRUE;
2463 if (!_dbus_string_init (&full_path))
2466 if (!make_full_path (&parser->basedir, content, &full_path))
2468 _dbus_string_free (&full_path);
2472 if (!include_dir (parser, &full_path, error))
2474 _dbus_string_free (&full_path);
2478 _dbus_string_free (&full_path);
2486 e->had_content = TRUE;
2488 if (!_dbus_string_copy_data (content, &s))
2491 dbus_free (parser->user);
2496 case ELEMENT_CONFIGTYPE:
2500 e->had_content = TRUE;
2502 if (!_dbus_string_copy_data (content, &s))
2505 dbus_free (parser->bus_type);
2506 parser->bus_type = s;
2510 case ELEMENT_LISTEN:
2514 e->had_content = TRUE;
2516 if (!_dbus_string_copy_data (content, &s))
2519 if (!_dbus_list_append (&parser->listen_on,
2532 e->had_content = TRUE;
2534 if (!_dbus_string_copy_data (content, &s))
2537 if (!_dbus_list_append (&parser->mechanisms,
2546 case ELEMENT_SERVICEDIR:
2549 DBusString full_path;
2551 e->had_content = TRUE;
2553 if (!_dbus_string_init (&full_path))
2556 if (!make_full_path (&parser->basedir, content, &full_path))
2558 _dbus_string_free (&full_path);
2562 if (!_dbus_string_copy_data (&full_path, &s))
2564 _dbus_string_free (&full_path);
2568 /* _only_ extra session directories can be specified */
2569 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2571 _dbus_string_free (&full_path);
2576 _dbus_string_free (&full_path);
2584 e->had_content = TRUE;
2587 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2589 dbus_set_error (error, DBUS_ERROR_FAILED,
2590 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2595 e->d.limit.value = val;
2597 _dbus_verbose ("Loaded value %ld for limit %s\n",
2604 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2608 BUS_SET_OOM (error);
2613 bus_config_parser_finished (BusConfigParser *parser,
2616 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2618 if (parser->stack != NULL)
2620 dbus_set_error (error, DBUS_ERROR_FAILED,
2621 "Element <%s> was not closed in configuration file",
2622 bus_config_parser_element_type_to_name (top_element_type (parser)));
2627 if (parser->is_toplevel && parser->listen_on == NULL)
2629 dbus_set_error (error, DBUS_ERROR_FAILED,
2630 "Configuration file needs one or more <listen> elements giving addresses");
2638 bus_config_parser_get_user (BusConfigParser *parser)
2640 return parser->user;
2644 bus_config_parser_get_type (BusConfigParser *parser)
2646 return parser->bus_type;
2650 bus_config_parser_get_addresses (BusConfigParser *parser)
2652 return &parser->listen_on;
2656 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2658 return &parser->mechanisms;
2662 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2664 return &parser->service_dirs;
2668 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2670 return &parser->conf_dirs;
2674 bus_config_parser_get_fork (BusConfigParser *parser)
2676 return parser->fork;
2680 bus_config_parser_get_syslog (BusConfigParser *parser)
2682 return parser->syslog;
2686 bus_config_parser_get_keep_umask (BusConfigParser *parser)
2688 return parser->keep_umask;
2692 bus_config_parser_get_allow_anonymous (BusConfigParser *parser)
2694 return parser->allow_anonymous;
2698 bus_config_parser_get_pidfile (BusConfigParser *parser)
2700 return parser->pidfile;
2704 bus_config_parser_get_servicehelper (BusConfigParser *parser)
2706 return parser->servicehelper;
2710 bus_config_parser_steal_policy (BusConfigParser *parser)
2714 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2716 policy = parser->policy;
2718 parser->policy = NULL;
2723 /* Overwrite any limits that were set in the configuration file */
2725 bus_config_parser_get_limits (BusConfigParser *parser,
2728 *limits = parser->limits;
2732 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2734 DBusHashTable *table;
2736 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2738 table = parser->service_context_table;
2740 parser->service_context_table = NULL;
2745 #ifdef DBUS_BUILD_TESTS
2756 do_check_own_rules (BusPolicy *policy)
2760 dbus_bool_t allowed;
2762 {"org.freedesktop", FALSE},
2763 {"org.freedesktop.ManySystem", FALSE},
2764 {"org.freedesktop.ManySystems", TRUE},
2765 {"org.freedesktop.ManySystems.foo", TRUE},
2766 {"org.freedesktop.ManySystems.foo.bar", TRUE},
2767 {"org.freedesktop.ManySystems2", FALSE},
2768 {"org.freedesktop.ManySystems2.foo", FALSE},
2769 {"org.freedesktop.ManySystems2.foo.bar", FALSE},
2774 while (checks[i].name)
2776 DBusString service_name;
2779 if (!_dbus_string_init (&service_name))
2780 _dbus_assert_not_reached ("couldn't init string");
2781 if (!_dbus_string_append (&service_name, checks[i].name))
2782 _dbus_assert_not_reached ("couldn't append string");
2784 ret = bus_policy_check_can_own (policy, &service_name);
2785 printf (" Check name %s: %s\n", checks[i].name,
2786 ret ? "allowed" : "not allowed");
2787 if (checks[i].allowed && !ret)
2789 _dbus_warn ("Cannot own %s\n", checks[i].name);
2792 if (!checks[i].allowed && ret)
2794 _dbus_warn ("Can own %s\n", checks[i].name);
2797 _dbus_string_free (&service_name);
2806 do_load (const DBusString *full_path,
2808 dbus_bool_t oom_possible,
2809 dbus_bool_t check_own_rules)
2811 BusConfigParser *parser;
2814 dbus_error_init (&error);
2816 parser = bus_config_load (full_path, TRUE, NULL, &error);
2819 _DBUS_ASSERT_ERROR_IS_SET (&error);
2822 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2824 _dbus_verbose ("Failed to load valid file due to OOM\n");
2825 dbus_error_free (&error);
2828 else if (validity == VALID)
2830 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2833 dbus_error_free (&error);
2838 dbus_error_free (&error);
2844 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2846 if (check_own_rules && do_check_own_rules (parser->policy) == FALSE)
2851 bus_config_parser_unref (parser);
2853 if (validity == INVALID)
2855 _dbus_warn ("Accepted invalid file\n");
2865 const DBusString *full_path;
2867 dbus_bool_t check_own_rules;
2871 check_loader_oom_func (void *data)
2873 LoaderOomData *d = data;
2875 return do_load (d->full_path, d->validity, TRUE, d->check_own_rules);
2879 process_test_valid_subdir (const DBusString *test_base_dir,
2883 DBusString test_directory;
2884 DBusString filename;
2892 if (!_dbus_string_init (&test_directory))
2893 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2895 _dbus_string_init_const (&filename, subdir);
2897 if (!_dbus_string_copy (test_base_dir, 0,
2898 &test_directory, 0))
2899 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2901 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2902 _dbus_assert_not_reached ("couldn't allocate full path");
2904 _dbus_string_free (&filename);
2905 if (!_dbus_string_init (&filename))
2906 _dbus_assert_not_reached ("didn't allocate filename string\n");
2908 dbus_error_init (&error);
2909 dir = _dbus_directory_open (&test_directory, &error);
2912 _dbus_warn ("Could not open %s: %s\n",
2913 _dbus_string_get_const_data (&test_directory),
2915 dbus_error_free (&error);
2919 if (validity == VALID)
2920 printf ("Testing valid files:\n");
2921 else if (validity == INVALID)
2922 printf ("Testing invalid files:\n");
2924 printf ("Testing unknown files:\n");
2927 while (_dbus_directory_get_next_file (dir, &filename, &error))
2929 DBusString full_path;
2932 if (!_dbus_string_init (&full_path))
2933 _dbus_assert_not_reached ("couldn't init string");
2935 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2936 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2938 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2939 _dbus_assert_not_reached ("couldn't concat file to dir");
2941 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2943 _dbus_verbose ("Skipping non-.conf file %s\n",
2944 _dbus_string_get_const_data (&filename));
2945 _dbus_string_free (&full_path);
2949 printf (" %s\n", _dbus_string_get_const_data (&filename));
2951 _dbus_verbose (" expecting %s\n",
2952 validity == VALID ? "valid" :
2953 (validity == INVALID ? "invalid" :
2954 (validity == UNKNOWN ? "unknown" : "???")));
2956 d.full_path = &full_path;
2957 d.validity = validity;
2958 d.check_own_rules = _dbus_string_ends_with_c_str (&full_path,
2959 "check-own-rules.conf");
2961 /* FIXME hackaround for an expat problem, see
2962 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2963 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2965 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2966 if (!check_loader_oom_func (&d))
2967 _dbus_assert_not_reached ("test failed");
2969 _dbus_string_free (&full_path);
2972 if (dbus_error_is_set (&error))
2974 _dbus_warn ("Could not get next file in %s: %s\n",
2975 _dbus_string_get_const_data (&test_directory),
2977 dbus_error_free (&error);
2986 _dbus_directory_close (dir);
2987 _dbus_string_free (&test_directory);
2988 _dbus_string_free (&filename);
2994 bools_equal (dbus_bool_t a,
3001 strings_equal_or_both_null (const char *a,
3004 if (a == NULL || b == NULL)
3007 return !strcmp (a, b);
3011 elements_equal (const Element *a,
3014 if (a->type != b->type)
3017 if (!bools_equal (a->had_content, b->had_content))
3023 case ELEMENT_INCLUDE:
3024 if (!bools_equal (a->d.include.ignore_missing,
3025 b->d.include.ignore_missing))
3029 case ELEMENT_POLICY:
3030 if (a->d.policy.type != b->d.policy.type)
3032 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
3037 if (strcmp (a->d.limit.name, b->d.limit.name))
3039 if (a->d.limit.value != b->d.limit.value)
3053 lists_of_elements_equal (DBusList *a,
3062 while (ia != NULL && ib != NULL)
3064 if (elements_equal (ia->data, ib->data))
3066 ia = _dbus_list_get_next_link (&a, ia);
3067 ib = _dbus_list_get_next_link (&b, ib);
3070 return ia == NULL && ib == NULL;
3074 lists_of_c_strings_equal (DBusList *a,
3083 while (ia != NULL && ib != NULL)
3085 if (strcmp (ia->data, ib->data))
3087 ia = _dbus_list_get_next_link (&a, ia);
3088 ib = _dbus_list_get_next_link (&b, ib);
3091 return ia == NULL && ib == NULL;
3095 limits_equal (const BusLimits *a,
3099 (a->max_incoming_bytes == b->max_incoming_bytes
3100 || a->max_incoming_unix_fds == b->max_incoming_unix_fds
3101 || a->max_outgoing_bytes == b->max_outgoing_bytes
3102 || a->max_outgoing_unix_fds == b->max_outgoing_unix_fds
3103 || a->max_message_size == b->max_message_size
3104 || a->max_message_unix_fds == b->max_message_unix_fds
3105 || a->activation_timeout == b->activation_timeout
3106 || a->auth_timeout == b->auth_timeout
3107 || a->max_completed_connections == b->max_completed_connections
3108 || a->max_incomplete_connections == b->max_incomplete_connections
3109 || a->max_connections_per_user == b->max_connections_per_user
3110 || a->max_pending_activations == b->max_pending_activations
3111 || a->max_services_per_connection == b->max_services_per_connection
3112 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
3113 || a->max_replies_per_connection == b->max_replies_per_connection
3114 || a->reply_timeout == b->reply_timeout);
3118 config_parsers_equal (const BusConfigParser *a,
3119 const BusConfigParser *b)
3121 if (!_dbus_string_equal (&a->basedir, &b->basedir))
3124 if (!lists_of_elements_equal (a->stack, b->stack))
3127 if (!strings_equal_or_both_null (a->user, b->user))
3130 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
3133 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
3136 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
3139 /* FIXME: compare policy */
3141 /* FIXME: compare service selinux ID table */
3143 if (! limits_equal (&a->limits, &b->limits))
3146 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
3149 if (! bools_equal (a->fork, b->fork))
3152 if (! bools_equal (a->keep_umask, b->keep_umask))
3155 if (! bools_equal (a->is_toplevel, b->is_toplevel))
3162 all_are_equiv (const DBusString *target_directory)
3164 DBusString filename;
3166 BusConfigParser *first_parser;
3167 BusConfigParser *parser;
3173 first_parser = NULL;
3177 if (!_dbus_string_init (&filename))
3178 _dbus_assert_not_reached ("didn't allocate filename string");
3180 dbus_error_init (&error);
3181 dir = _dbus_directory_open (target_directory, &error);
3184 _dbus_warn ("Could not open %s: %s\n",
3185 _dbus_string_get_const_data (target_directory),
3187 dbus_error_free (&error);
3191 printf ("Comparing equivalent files:\n");
3194 while (_dbus_directory_get_next_file (dir, &filename, &error))
3196 DBusString full_path;
3198 if (!_dbus_string_init (&full_path))
3199 _dbus_assert_not_reached ("couldn't init string");
3201 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
3202 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3204 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3205 _dbus_assert_not_reached ("couldn't concat file to dir");
3207 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3209 _dbus_verbose ("Skipping non-.conf file %s\n",
3210 _dbus_string_get_const_data (&filename));
3211 _dbus_string_free (&full_path);
3215 printf (" %s\n", _dbus_string_get_const_data (&filename));
3217 parser = bus_config_load (&full_path, TRUE, NULL, &error);
3221 _dbus_warn ("Could not load file %s: %s\n",
3222 _dbus_string_get_const_data (&full_path),
3224 _dbus_string_free (&full_path);
3225 dbus_error_free (&error);
3228 else if (first_parser == NULL)
3230 _dbus_string_free (&full_path);
3231 first_parser = parser;
3235 _dbus_string_free (&full_path);
3236 equal = config_parsers_equal (first_parser, parser);
3237 bus_config_parser_unref (parser);
3246 _dbus_string_free (&filename);
3248 bus_config_parser_unref (first_parser);
3250 _dbus_directory_close (dir);
3257 process_test_equiv_subdir (const DBusString *test_base_dir,
3260 DBusString test_directory;
3261 DBusString filename;
3270 if (!_dbus_string_init (&test_directory))
3271 _dbus_assert_not_reached ("didn't allocate test_directory");
3273 _dbus_string_init_const (&filename, subdir);
3275 if (!_dbus_string_copy (test_base_dir, 0,
3276 &test_directory, 0))
3277 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3279 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3280 _dbus_assert_not_reached ("couldn't allocate full path");
3282 _dbus_string_free (&filename);
3283 if (!_dbus_string_init (&filename))
3284 _dbus_assert_not_reached ("didn't allocate filename string");
3286 dbus_error_init (&error);
3287 dir = _dbus_directory_open (&test_directory, &error);
3290 _dbus_warn ("Could not open %s: %s\n",
3291 _dbus_string_get_const_data (&test_directory),
3293 dbus_error_free (&error);
3297 while (_dbus_directory_get_next_file (dir, &filename, &error))
3299 DBusString full_path;
3301 /* Skip CVS's magic directories! */
3302 if (_dbus_string_equal_c_str (&filename, "CVS"))
3305 if (!_dbus_string_init (&full_path))
3306 _dbus_assert_not_reached ("couldn't init string");
3308 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3309 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3311 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3312 _dbus_assert_not_reached ("couldn't concat file to dir");
3314 equal = all_are_equiv (&full_path);
3315 _dbus_string_free (&full_path);
3324 _dbus_string_free (&test_directory);
3325 _dbus_string_free (&filename);
3327 _dbus_directory_close (dir);
3333 static const char *test_session_service_dir_matches[] =
3336 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3337 "/testusr/testlocal/testshare/dbus-1/services",
3338 "/testusr/testshare/dbus-1/services",
3339 DBUS_DATADIR"/dbus-1/services",
3341 /* will be filled in test_default_session_servicedirs() */
3350 test_default_session_servicedirs (void)
3358 const char *common_progs;
3361 if (_dbus_get_install_root(buffer, sizeof(buffer)))
3363 strcat(buffer,DBUS_DATADIR);
3364 strcat(buffer,"/dbus-1/services");
3365 test_session_service_dir_matches[0] = buffer;
3369 /* On Unix we don't actually use this variable, but it's easier to handle the
3370 * deallocation if we always allocate it, whether needed or not */
3371 if (!_dbus_string_init (&progs))
3372 _dbus_assert_not_reached ("OOM allocating progs");
3375 common_progs = _dbus_getenv ("CommonProgramFiles");
3379 if (!_dbus_string_append (&progs, common_progs))
3381 _dbus_string_free (&progs);
3385 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3387 _dbus_string_free (&progs);
3390 test_session_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3395 printf ("Testing retrieving the default session service directories\n");
3396 if (!_dbus_get_standard_session_servicedirs (&dirs))
3397 _dbus_assert_not_reached ("couldn't get stardard dirs");
3399 /* make sure our defaults end with share/dbus-1/service */
3400 while ((link = _dbus_list_pop_first_link (&dirs)))
3404 printf (" default service dir: %s\n", (char *)link->data);
3405 _dbus_string_init_const (&path, (char *)link->data);
3406 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/services"))
3408 printf ("error with default session service directories\n");
3409 dbus_free (link->data);
3410 _dbus_list_free_link (link);
3411 _dbus_string_free (&progs);
3415 dbus_free (link->data);
3416 _dbus_list_free_link (link);
3420 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3421 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3423 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3424 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3426 if (!_dbus_get_standard_session_servicedirs (&dirs))
3427 _dbus_assert_not_reached ("couldn't get stardard dirs");
3429 /* make sure we read and parse the env variable correctly */
3431 while ((link = _dbus_list_pop_first_link (&dirs)))
3433 printf (" test service dir: %s\n", (char *)link->data);
3434 if (test_session_service_dir_matches[i] == NULL)
3436 printf ("more directories parsed than in match set\n");
3437 dbus_free (link->data);
3438 _dbus_list_free_link (link);
3439 _dbus_string_free (&progs);
3443 if (strcmp (test_session_service_dir_matches[i],
3444 (char *)link->data) != 0)
3446 printf ("%s directory does not match %s in the match set\n",
3448 test_session_service_dir_matches[i]);
3449 dbus_free (link->data);
3450 _dbus_list_free_link (link);
3451 _dbus_string_free (&progs);
3457 dbus_free (link->data);
3458 _dbus_list_free_link (link);
3461 if (test_session_service_dir_matches[i] != NULL)
3463 printf ("extra data %s in the match set was not matched\n",
3464 test_session_service_dir_matches[i]);
3466 _dbus_string_free (&progs);
3470 _dbus_string_free (&progs);
3474 static const char *test_system_service_dir_matches[] =
3477 "/usr/local/share/dbus-1/system-services",
3478 "/usr/share/dbus-1/system-services",
3480 DBUS_DATADIR"/dbus-1/system-services",
3482 "/lib/dbus-1/system-services",
3492 test_default_system_servicedirs (void)
3498 const char *common_progs;
3502 /* On Unix we don't actually use this variable, but it's easier to handle the
3503 * deallocation if we always allocate it, whether needed or not */
3504 if (!_dbus_string_init (&progs))
3505 _dbus_assert_not_reached ("OOM allocating progs");
3508 common_progs = _dbus_getenv ("CommonProgramFiles");
3512 if (!_dbus_string_append (&progs, common_progs))
3514 _dbus_string_free (&progs);
3518 if (!_dbus_string_append (&progs, "/dbus-1/system-services"))
3520 _dbus_string_free (&progs);
3523 test_system_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3528 printf ("Testing retrieving the default system service directories\n");
3529 if (!_dbus_get_standard_system_servicedirs (&dirs))
3530 _dbus_assert_not_reached ("couldn't get stardard dirs");
3532 /* make sure our defaults end with share/dbus-1/system-service */
3533 while ((link = _dbus_list_pop_first_link (&dirs)))
3537 printf (" default service dir: %s\n", (char *)link->data);
3538 _dbus_string_init_const (&path, (char *)link->data);
3539 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/system-services"))
3541 printf ("error with default system service directories\n");
3542 dbus_free (link->data);
3543 _dbus_list_free_link (link);
3544 _dbus_string_free (&progs);
3548 dbus_free (link->data);
3549 _dbus_list_free_link (link);
3553 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3554 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3556 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3557 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3559 if (!_dbus_get_standard_system_servicedirs (&dirs))
3560 _dbus_assert_not_reached ("couldn't get stardard dirs");
3562 /* make sure we read and parse the env variable correctly */
3564 while ((link = _dbus_list_pop_first_link (&dirs)))
3566 printf (" test service dir: %s\n", (char *)link->data);
3567 if (test_system_service_dir_matches[i] == NULL)
3569 printf ("more directories parsed than in match set\n");
3570 dbus_free (link->data);
3571 _dbus_list_free_link (link);
3572 _dbus_string_free (&progs);
3576 if (strcmp (test_system_service_dir_matches[i],
3577 (char *)link->data) != 0)
3579 printf ("%s directory does not match %s in the match set\n",
3581 test_system_service_dir_matches[i]);
3582 dbus_free (link->data);
3583 _dbus_list_free_link (link);
3584 _dbus_string_free (&progs);
3590 dbus_free (link->data);
3591 _dbus_list_free_link (link);
3594 if (test_system_service_dir_matches[i] != NULL)
3596 printf ("extra data %s in the match set was not matched\n",
3597 test_system_service_dir_matches[i]);
3599 _dbus_string_free (&progs);
3603 _dbus_string_free (&progs);
3608 bus_config_parser_test (const DBusString *test_data_dir)
3610 if (test_data_dir == NULL ||
3611 _dbus_string_get_length (test_data_dir) == 0)
3613 printf ("No test data\n");
3617 if (!test_default_session_servicedirs())
3621 printf("default system service dir skipped\n");
3623 if (!test_default_system_servicedirs())
3627 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3630 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3633 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3639 #endif /* DBUS_BUILD_TESTS */