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-misc.h>
34 #include <dbus/dbus-sysdeps.h>
39 /* we ignore policies for unknown groups/users */
54 unsigned int had_content : 1;
60 unsigned int ignore_missing : 1;
61 unsigned int if_selinux_enabled : 1;
62 unsigned int selinux_root_relative : 1;
68 unsigned long gid_uid_or_at_console;
82 * Parser for bus configuration file.
84 struct BusConfigParser
86 int refcount; /**< Reference count */
88 DBusString basedir; /**< Directory we resolve paths relative to */
90 DBusList *stack; /**< stack of Element */
92 char *user; /**< user to run as */
94 char *servicehelper; /**< location of the setuid helper */
96 char *bus_type; /**< Message bus type */
98 DBusList *listen_on; /**< List of addresses to listen to */
100 DBusList *mechanisms; /**< Auth mechanisms */
102 DBusList *service_dirs; /**< Directories to look for session services in */
104 DBusList *conf_dirs; /**< Directories to look for policy configuration in */
106 BusPolicy *policy; /**< Security policy */
108 BusLimits limits; /**< Limits */
110 char *pidfile; /**< PID file */
112 DBusList *included_files; /**< Included files stack */
114 DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
116 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
118 unsigned int syslog : 1; /**< TRUE to enable syslog */
119 unsigned int keep_umask : 1; /**< TRUE to keep original umask when forking */
121 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
123 unsigned int allow_anonymous : 1; /**< TRUE to allow anonymous connections */
127 push_element (BusConfigParser *parser,
132 _dbus_assert (type != ELEMENT_NONE);
134 e = dbus_new0 (Element, 1);
138 if (!_dbus_list_append (&parser->stack, e))
150 element_free (Element *e)
152 if (e->type == ELEMENT_LIMIT)
153 dbus_free (e->d.limit.name);
159 pop_element (BusConfigParser *parser)
163 e = _dbus_list_pop_last (&parser->stack);
169 peek_element (BusConfigParser *parser)
173 e = _dbus_list_get_last (&parser->stack);
179 top_element_type (BusConfigParser *parser)
183 e = _dbus_list_get_last (&parser->stack);
192 merge_service_context_hash (DBusHashTable *dest,
202 _dbus_hash_iter_init (from, &iter);
203 while (_dbus_hash_iter_next (&iter))
205 const char *service = _dbus_hash_iter_get_string_key (&iter);
206 const char *context = _dbus_hash_iter_get_value (&iter);
208 service_copy = _dbus_strdup (service);
209 if (service_copy == NULL)
211 context_copy = _dbus_strdup (context);
212 if (context_copy == NULL)
215 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
226 dbus_free (service_copy);
229 dbus_free (context_copy);
235 service_dirs_find_dir (DBusList **service_dirs,
240 _dbus_assert (dir != NULL);
242 for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
244 const char *link_dir;
246 link_dir = (const char *)link->data;
247 if (strcmp (dir, link_dir) == 0)
255 service_dirs_append_unique_or_free (DBusList **service_dirs,
258 if (!service_dirs_find_dir (service_dirs, dir))
259 return _dbus_list_append (service_dirs, dir);
266 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
269 if (!service_dirs_find_dir (service_dirs, dir_link->data))
271 _dbus_list_append_link (service_dirs, dir_link);
275 dbus_free (dir_link->data);
276 _dbus_list_free_link (dir_link);
281 merge_included (BusConfigParser *parser,
282 BusConfigParser *included,
287 if (!bus_policy_merge (parser->policy,
294 if (!merge_service_context_hash (parser->service_context_table,
295 included->service_context_table))
301 if (included->user != NULL)
303 dbus_free (parser->user);
304 parser->user = included->user;
305 included->user = NULL;
308 if (included->bus_type != NULL)
310 dbus_free (parser->bus_type);
311 parser->bus_type = included->bus_type;
312 included->bus_type = NULL;
318 if (included->keep_umask)
319 parser->keep_umask = TRUE;
321 if (included->pidfile != NULL)
323 dbus_free (parser->pidfile);
324 parser->pidfile = included->pidfile;
325 included->pidfile = NULL;
328 if (included->servicehelper != NULL)
330 dbus_free (parser->servicehelper);
331 parser->servicehelper = included->servicehelper;
332 included->servicehelper = NULL;
335 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
336 _dbus_list_append_link (&parser->listen_on, link);
338 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
339 _dbus_list_append_link (&parser->mechanisms, link);
341 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
342 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
344 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
345 _dbus_list_append_link (&parser->conf_dirs, link);
351 seen_include (BusConfigParser *parser,
352 const DBusString *file)
356 iter = parser->included_files;
359 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
362 iter = _dbus_list_get_next_link (&parser->included_files, iter);
369 bus_config_parser_new (const DBusString *basedir,
370 dbus_bool_t is_toplevel,
371 const BusConfigParser *parent)
373 BusConfigParser *parser;
375 parser = dbus_new0 (BusConfigParser, 1);
379 parser->is_toplevel = !!is_toplevel;
381 if (!_dbus_string_init (&parser->basedir))
387 if (((parser->policy = bus_policy_new ()) == NULL) ||
388 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
389 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
391 dbus_free)) == NULL))
394 bus_policy_unref (parser->policy);
396 _dbus_string_free (&parser->basedir);
404 /* Initialize the parser's limits from the parent. */
405 parser->limits = parent->limits;
407 /* Use the parent's list of included_files to avoid
408 circular inclusions. */
409 parser->included_files = parent->included_files;
414 /* Make up some numbers! woot! */
415 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 127;
416 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 127;
417 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
419 /* We set relatively conservative values here since due to the
420 way SCM_RIGHTS works we need to preallocate an array for the
421 maximum number of file descriptors we can receive. Picking a
422 high value here thus translates directly to more memory
424 parser->limits.max_incoming_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS*4;
425 parser->limits.max_outgoing_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS*4;
426 parser->limits.max_message_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS;
428 /* Making this long means the user has to wait longer for an error
429 * message if something screws up, but making it too short means
430 * they might see a false failure.
432 parser->limits.activation_timeout = 25000; /* 25 seconds */
434 /* Making this long risks making a DOS attack easier, but too short
435 * and legitimate auth will fail. If interactive auth (ask user for
436 * password) is allowed, then potentially it has to be quite long.
438 parser->limits.auth_timeout = 30000; /* 30 seconds */
440 parser->limits.max_incomplete_connections = 64;
441 parser->limits.max_connections_per_user = 256;
443 /* Note that max_completed_connections / max_connections_per_user
444 * is the number of users that would have to work together to
445 * DOS all the other users.
447 parser->limits.max_completed_connections = 2048;
449 parser->limits.max_pending_activations = 512;
450 parser->limits.max_services_per_connection = 512;
452 /* For this one, keep in mind that it isn't only the memory used
453 * by the match rules, but slowdown from linearly walking a big
454 * list of them. A client adding more than this is almost
455 * certainly a bad idea for that reason, and should change to a
456 * smaller number of wider-net match rules - getting every last
457 * message to the bus is probably better than having a thousand
460 parser->limits.max_match_rules_per_connection = 512;
462 parser->limits.reply_timeout = -1; /* never */
464 /* this is effectively a limit on message queue size for messages
465 * that require a reply
467 parser->limits.max_replies_per_connection = 1024*8;
470 parser->refcount = 1;
476 bus_config_parser_ref (BusConfigParser *parser)
478 _dbus_assert (parser->refcount > 0);
480 parser->refcount += 1;
486 bus_config_parser_unref (BusConfigParser *parser)
488 _dbus_assert (parser->refcount > 0);
490 parser->refcount -= 1;
492 if (parser->refcount == 0)
494 while (parser->stack != NULL)
495 pop_element (parser);
497 dbus_free (parser->user);
498 dbus_free (parser->servicehelper);
499 dbus_free (parser->bus_type);
500 dbus_free (parser->pidfile);
502 _dbus_list_foreach (&parser->listen_on,
503 (DBusForeachFunction) dbus_free,
506 _dbus_list_clear (&parser->listen_on);
508 _dbus_list_foreach (&parser->service_dirs,
509 (DBusForeachFunction) dbus_free,
512 _dbus_list_clear (&parser->service_dirs);
514 _dbus_list_foreach (&parser->conf_dirs,
515 (DBusForeachFunction) dbus_free,
518 _dbus_list_clear (&parser->conf_dirs);
520 _dbus_list_foreach (&parser->mechanisms,
521 (DBusForeachFunction) dbus_free,
524 _dbus_list_clear (&parser->mechanisms);
526 _dbus_string_free (&parser->basedir);
529 bus_policy_unref (parser->policy);
531 if (parser->service_context_table)
532 _dbus_hash_table_unref (parser->service_context_table);
539 bus_config_parser_check_doctype (BusConfigParser *parser,
543 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
545 if (strcmp (doctype, "busconfig") != 0)
547 dbus_set_error (error,
549 "Configuration file has the wrong document type %s",
564 locate_attributes (BusConfigParser *parser,
565 const char *element_name,
566 const char **attribute_names,
567 const char **attribute_values,
569 const char *first_attribute_name,
570 const char **first_attribute_retloc,
578 LocateAttr attrs[MAX_ATTRS];
582 _dbus_assert (first_attribute_name != NULL);
583 _dbus_assert (first_attribute_retloc != NULL);
588 attrs[0].name = first_attribute_name;
589 attrs[0].retloc = first_attribute_retloc;
590 *first_attribute_retloc = NULL;
592 va_start (args, first_attribute_retloc);
594 name = va_arg (args, const char*);
595 retloc = va_arg (args, const char**);
599 _dbus_assert (retloc != NULL);
600 _dbus_assert (n_attrs < MAX_ATTRS);
602 attrs[n_attrs].name = name;
603 attrs[n_attrs].retloc = retloc;
607 name = va_arg (args, const char*);
608 retloc = va_arg (args, const char**);
614 while (attribute_names[i])
623 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
625 retloc = attrs[j].retloc;
629 dbus_set_error (error, DBUS_ERROR_FAILED,
630 "Attribute \"%s\" repeated twice on the same <%s> element",
631 attrs[j].name, element_name);
636 *retloc = attribute_values[i];
645 dbus_set_error (error, DBUS_ERROR_FAILED,
646 "Attribute \"%s\" is invalid on <%s> element in this context",
647 attribute_names[i], element_name);
660 check_no_attributes (BusConfigParser *parser,
661 const char *element_name,
662 const char **attribute_names,
663 const char **attribute_values,
666 if (attribute_names[0] != NULL)
668 dbus_set_error (error, DBUS_ERROR_FAILED,
669 "Attribute \"%s\" is invalid on <%s> element in this context",
670 attribute_names[0], element_name);
678 start_busconfig_child (BusConfigParser *parser,
679 const char *element_name,
680 const char **attribute_names,
681 const char **attribute_values,
684 ElementType element_type;
686 element_type = bus_config_parser_element_name_to_type (element_name);
688 if (element_type == ELEMENT_USER)
690 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
693 if (push_element (parser, ELEMENT_USER) == NULL)
701 else if (element_type == ELEMENT_CONFIGTYPE)
703 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
706 if (push_element (parser, ELEMENT_CONFIGTYPE) == NULL)
714 else if (element_type == ELEMENT_FORK)
716 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
719 if (push_element (parser, ELEMENT_FORK) == NULL)
729 else if (element_type == ELEMENT_SYSLOG)
731 if (!check_no_attributes (parser, "syslog", attribute_names, attribute_values, error))
734 if (push_element (parser, ELEMENT_SYSLOG) == NULL)
740 parser->syslog = TRUE;
744 else if (element_type == ELEMENT_KEEP_UMASK)
746 if (!check_no_attributes (parser, "keep_umask", attribute_names, attribute_values, error))
749 if (push_element (parser, ELEMENT_KEEP_UMASK) == NULL)
755 parser->keep_umask = TRUE;
759 else if (element_type == ELEMENT_PIDFILE)
761 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
764 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
772 else if (element_type == ELEMENT_LISTEN)
774 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
777 if (push_element (parser, ELEMENT_LISTEN) == NULL)
785 else if (element_type == ELEMENT_AUTH)
787 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
790 if (push_element (parser, ELEMENT_AUTH) == NULL)
798 else if (element_type == ELEMENT_SERVICEHELPER)
800 if (!check_no_attributes (parser, "servicehelper", attribute_names, attribute_values, error))
803 if (push_element (parser, ELEMENT_SERVICEHELPER) == NULL)
811 else if (element_type == ELEMENT_INCLUDEDIR)
813 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
816 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
824 else if (element_type == ELEMENT_STANDARD_SESSION_SERVICEDIRS)
830 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
833 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
839 if (!_dbus_get_standard_session_servicedirs (&dirs))
845 while ((link = _dbus_list_pop_first_link (&dirs)))
846 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
850 else if (element_type == ELEMENT_STANDARD_SYSTEM_SERVICEDIRS)
856 if (!check_no_attributes (parser, "standard_system_servicedirs", attribute_names, attribute_values, error))
859 if (push_element (parser, ELEMENT_STANDARD_SYSTEM_SERVICEDIRS) == NULL)
865 if (!_dbus_get_standard_system_servicedirs (&dirs))
871 while ((link = _dbus_list_pop_first_link (&dirs)))
872 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
876 else if (element_type == ELEMENT_ALLOW_ANONYMOUS)
878 if (!check_no_attributes (parser, "allow_anonymous", attribute_names, attribute_values, error))
881 if (push_element (parser, ELEMENT_ALLOW_ANONYMOUS) == NULL)
887 parser->allow_anonymous = TRUE;
890 else if (element_type == ELEMENT_SERVICEDIR)
892 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
895 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
903 else if (element_type == ELEMENT_INCLUDE)
906 const char *if_selinux_enabled;
907 const char *ignore_missing;
908 const char *selinux_root_relative;
910 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
916 e->d.include.ignore_missing = FALSE;
917 e->d.include.if_selinux_enabled = FALSE;
918 e->d.include.selinux_root_relative = FALSE;
920 if (!locate_attributes (parser, "include",
924 "ignore_missing", &ignore_missing,
925 "if_selinux_enabled", &if_selinux_enabled,
926 "selinux_root_relative", &selinux_root_relative,
930 if (ignore_missing != NULL)
932 if (strcmp (ignore_missing, "yes") == 0)
933 e->d.include.ignore_missing = TRUE;
934 else if (strcmp (ignore_missing, "no") == 0)
935 e->d.include.ignore_missing = FALSE;
938 dbus_set_error (error, DBUS_ERROR_FAILED,
939 "ignore_missing attribute must have value \"yes\" or \"no\"");
944 if (if_selinux_enabled != NULL)
946 if (strcmp (if_selinux_enabled, "yes") == 0)
947 e->d.include.if_selinux_enabled = TRUE;
948 else if (strcmp (if_selinux_enabled, "no") == 0)
949 e->d.include.if_selinux_enabled = FALSE;
952 dbus_set_error (error, DBUS_ERROR_FAILED,
953 "if_selinux_enabled attribute must have value"
954 " \"yes\" or \"no\"");
959 if (selinux_root_relative != NULL)
961 if (strcmp (selinux_root_relative, "yes") == 0)
962 e->d.include.selinux_root_relative = TRUE;
963 else if (strcmp (selinux_root_relative, "no") == 0)
964 e->d.include.selinux_root_relative = FALSE;
967 dbus_set_error (error, DBUS_ERROR_FAILED,
968 "selinux_root_relative attribute must have value"
969 " \"yes\" or \"no\"");
976 else if (element_type == ELEMENT_POLICY)
982 const char *at_console;
984 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
990 e->d.policy.type = POLICY_IGNORED;
992 if (!locate_attributes (parser, "policy",
999 "at_console", &at_console,
1003 if (((context && user) ||
1004 (context && group) ||
1005 (context && at_console)) ||
1007 (user && at_console)) ||
1008 (group && at_console) ||
1009 !(context || user || group || at_console))
1011 dbus_set_error (error, DBUS_ERROR_FAILED,
1012 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
1016 if (context != NULL)
1018 if (strcmp (context, "default") == 0)
1020 e->d.policy.type = POLICY_DEFAULT;
1022 else if (strcmp (context, "mandatory") == 0)
1024 e->d.policy.type = POLICY_MANDATORY;
1028 dbus_set_error (error, DBUS_ERROR_FAILED,
1029 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
1034 else if (user != NULL)
1036 DBusString username;
1037 _dbus_string_init_const (&username, user);
1039 if (_dbus_parse_unix_user_from_config (&username,
1040 &e->d.policy.gid_uid_or_at_console))
1041 e->d.policy.type = POLICY_USER;
1043 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
1046 else if (group != NULL)
1048 DBusString group_name;
1049 _dbus_string_init_const (&group_name, group);
1051 if (_dbus_parse_unix_group_from_config (&group_name,
1052 &e->d.policy.gid_uid_or_at_console))
1053 e->d.policy.type = POLICY_GROUP;
1055 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
1058 else if (at_console != NULL)
1061 t = (strcmp (at_console, "true") == 0);
1062 if (t || strcmp (at_console, "false") == 0)
1064 e->d.policy.gid_uid_or_at_console = t;
1065 e->d.policy.type = POLICY_CONSOLE;
1069 dbus_set_error (error, DBUS_ERROR_FAILED,
1070 "Unknown value \"%s\" for at_console in message bus configuration file",
1078 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1083 else if (element_type == ELEMENT_LIMIT)
1088 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1090 BUS_SET_OOM (error);
1094 if (!locate_attributes (parser, "limit",
1104 dbus_set_error (error, DBUS_ERROR_FAILED,
1105 "<limit> element must have a \"name\" attribute");
1109 e->d.limit.name = _dbus_strdup (name);
1110 if (e->d.limit.name == NULL)
1112 BUS_SET_OOM (error);
1118 else if (element_type == ELEMENT_SELINUX)
1120 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1123 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1125 BUS_SET_OOM (error);
1133 dbus_set_error (error, DBUS_ERROR_FAILED,
1134 "Element <%s> not allowed inside <%s> in configuration file",
1135 element_name, "busconfig");
1141 append_rule_from_element (BusConfigParser *parser,
1142 const char *element_name,
1143 const char **attribute_names,
1144 const char **attribute_values,
1149 const char *send_interface;
1150 const char *send_member;
1151 const char *send_error;
1152 const char *send_destination;
1153 const char *send_path;
1154 const char *send_type;
1155 const char *receive_interface;
1156 const char *receive_member;
1157 const char *receive_error;
1158 const char *receive_sender;
1159 const char *receive_path;
1160 const char *receive_type;
1161 const char *eavesdrop;
1162 const char *send_requested_reply;
1163 const char *receive_requested_reply;
1165 const char *own_prefix;
1169 BusPolicyRule *rule;
1171 if (!locate_attributes (parser, element_name,
1175 "send_interface", &send_interface,
1176 "send_member", &send_member,
1177 "send_error", &send_error,
1178 "send_destination", &send_destination,
1179 "send_path", &send_path,
1180 "send_type", &send_type,
1181 "receive_interface", &receive_interface,
1182 "receive_member", &receive_member,
1183 "receive_error", &receive_error,
1184 "receive_sender", &receive_sender,
1185 "receive_path", &receive_path,
1186 "receive_type", &receive_type,
1187 "eavesdrop", &eavesdrop,
1188 "send_requested_reply", &send_requested_reply,
1189 "receive_requested_reply", &receive_requested_reply,
1191 "own_prefix", &own_prefix,
1198 if (!(send_interface || send_member || send_error || send_destination ||
1199 send_type || send_path ||
1200 receive_interface || receive_member || receive_error || receive_sender ||
1201 receive_type || receive_path || eavesdrop ||
1202 send_requested_reply || receive_requested_reply ||
1203 own || own_prefix || user || group))
1205 dbus_set_error (error, DBUS_ERROR_FAILED,
1206 "Element <%s> must have one or more attributes",
1211 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1212 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1214 dbus_set_error (error, DBUS_ERROR_FAILED,
1215 "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.",
1220 /* Allowed combinations of elements are:
1222 * base, must be all send or all receive:
1225 * interface + member
1228 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1229 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1231 * user, group, own, own_prefix must occur alone
1233 * Pretty sure the below stuff is broken, FIXME think about it more.
1236 if ((send_interface && (send_error ||
1237 receive_interface ||
1241 receive_requested_reply ||
1242 own || own_prefix ||
1246 (send_member && (send_error ||
1247 receive_interface ||
1251 receive_requested_reply ||
1252 own || own_prefix ||
1256 (send_error && (receive_interface ||
1260 receive_requested_reply ||
1261 own || own_prefix ||
1265 (send_destination && (receive_interface ||
1269 receive_requested_reply ||
1270 own || own_prefix ||
1274 (send_type && (receive_interface ||
1278 receive_requested_reply ||
1279 own || own_prefix ||
1283 (send_path && (receive_interface ||
1287 receive_requested_reply ||
1288 own || own_prefix ||
1292 (send_requested_reply && (receive_interface ||
1296 receive_requested_reply ||
1297 own || own_prefix ||
1301 (receive_interface && (receive_error ||
1302 own || own_prefix ||
1306 (receive_member && (receive_error ||
1307 own || own_prefix ||
1311 (receive_error && (own || own_prefix ||
1315 (eavesdrop && (own || own_prefix ||
1319 (receive_requested_reply && (own || own_prefix ||
1323 (own && (own_prefix || user || group)) ||
1325 (own_prefix && (own || user || group)) ||
1329 dbus_set_error (error, DBUS_ERROR_FAILED,
1330 "Invalid combination of attributes on element <%s>",
1337 /* In BusPolicyRule, NULL represents wildcard.
1338 * In the config file, '*' represents it.
1340 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1342 if (send_interface || send_member || send_error || send_destination ||
1343 send_path || send_type || send_requested_reply)
1347 if (IS_WILDCARD (send_interface))
1348 send_interface = NULL;
1349 if (IS_WILDCARD (send_member))
1351 if (IS_WILDCARD (send_error))
1353 if (IS_WILDCARD (send_destination))
1354 send_destination = NULL;
1355 if (IS_WILDCARD (send_path))
1357 if (IS_WILDCARD (send_type))
1360 message_type = DBUS_MESSAGE_TYPE_INVALID;
1361 if (send_type != NULL)
1363 message_type = dbus_message_type_from_string (send_type);
1364 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1366 dbus_set_error (error, DBUS_ERROR_FAILED,
1367 "Bad message type \"%s\"",
1374 !(strcmp (eavesdrop, "true") == 0 ||
1375 strcmp (eavesdrop, "false") == 0))
1377 dbus_set_error (error, DBUS_ERROR_FAILED,
1378 "Bad value \"%s\" for %s attribute, must be true or false",
1379 "eavesdrop", eavesdrop);
1383 if (send_requested_reply &&
1384 !(strcmp (send_requested_reply, "true") == 0 ||
1385 strcmp (send_requested_reply, "false") == 0))
1387 dbus_set_error (error, DBUS_ERROR_FAILED,
1388 "Bad value \"%s\" for %s attribute, must be true or false",
1389 "send_requested_reply", send_requested_reply);
1393 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1398 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1401 rule->d.send.log = (strcmp (log, "true") == 0);
1403 if (send_requested_reply)
1404 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1406 rule->d.send.message_type = message_type;
1407 rule->d.send.path = _dbus_strdup (send_path);
1408 rule->d.send.interface = _dbus_strdup (send_interface);
1409 rule->d.send.member = _dbus_strdup (send_member);
1410 rule->d.send.error = _dbus_strdup (send_error);
1411 rule->d.send.destination = _dbus_strdup (send_destination);
1412 if (send_path && rule->d.send.path == NULL)
1414 if (send_interface && rule->d.send.interface == NULL)
1416 if (send_member && rule->d.send.member == NULL)
1418 if (send_error && rule->d.send.error == NULL)
1420 if (send_destination && rule->d.send.destination == NULL)
1423 else if (receive_interface || receive_member || receive_error || receive_sender ||
1424 receive_path || receive_type || eavesdrop || receive_requested_reply)
1428 if (IS_WILDCARD (receive_interface))
1429 receive_interface = NULL;
1430 if (IS_WILDCARD (receive_member))
1431 receive_member = NULL;
1432 if (IS_WILDCARD (receive_error))
1433 receive_error = NULL;
1434 if (IS_WILDCARD (receive_sender))
1435 receive_sender = NULL;
1436 if (IS_WILDCARD (receive_path))
1437 receive_path = NULL;
1438 if (IS_WILDCARD (receive_type))
1439 receive_type = NULL;
1441 message_type = DBUS_MESSAGE_TYPE_INVALID;
1442 if (receive_type != NULL)
1444 message_type = dbus_message_type_from_string (receive_type);
1445 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1447 dbus_set_error (error, DBUS_ERROR_FAILED,
1448 "Bad message type \"%s\"",
1456 !(strcmp (eavesdrop, "true") == 0 ||
1457 strcmp (eavesdrop, "false") == 0))
1459 dbus_set_error (error, DBUS_ERROR_FAILED,
1460 "Bad value \"%s\" for %s attribute, must be true or false",
1461 "eavesdrop", eavesdrop);
1465 if (receive_requested_reply &&
1466 !(strcmp (receive_requested_reply, "true") == 0 ||
1467 strcmp (receive_requested_reply, "false") == 0))
1469 dbus_set_error (error, DBUS_ERROR_FAILED,
1470 "Bad value \"%s\" for %s attribute, must be true or false",
1471 "receive_requested_reply", receive_requested_reply);
1475 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1480 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1482 if (receive_requested_reply)
1483 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1485 rule->d.receive.message_type = message_type;
1486 rule->d.receive.path = _dbus_strdup (receive_path);
1487 rule->d.receive.interface = _dbus_strdup (receive_interface);
1488 rule->d.receive.member = _dbus_strdup (receive_member);
1489 rule->d.receive.error = _dbus_strdup (receive_error);
1490 rule->d.receive.origin = _dbus_strdup (receive_sender);
1492 if (receive_path && rule->d.receive.path == NULL)
1494 if (receive_interface && rule->d.receive.interface == NULL)
1496 if (receive_member && rule->d.receive.member == NULL)
1498 if (receive_error && rule->d.receive.error == NULL)
1500 if (receive_sender && rule->d.receive.origin == NULL)
1503 else if (own || own_prefix)
1505 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1511 if (IS_WILDCARD (own))
1514 rule->d.own.prefix = 0;
1515 rule->d.own.service_name = _dbus_strdup (own);
1516 if (own && rule->d.own.service_name == NULL)
1521 rule->d.own.prefix = 1;
1522 rule->d.own.service_name = _dbus_strdup (own_prefix);
1523 if (rule->d.own.service_name == NULL)
1529 if (IS_WILDCARD (user))
1531 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1535 rule->d.user.uid = DBUS_UID_UNSET;
1539 DBusString username;
1542 _dbus_string_init_const (&username, user);
1544 if (_dbus_parse_unix_user_from_config (&username, &uid))
1546 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1550 rule->d.user.uid = uid;
1554 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1555 user, element_name);
1561 if (IS_WILDCARD (group))
1563 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1567 rule->d.group.gid = DBUS_GID_UNSET;
1571 DBusString groupname;
1574 _dbus_string_init_const (&groupname, group);
1576 if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1578 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1582 rule->d.group.gid = gid;
1586 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1587 group, element_name);
1592 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1598 pe = peek_element (parser);
1599 _dbus_assert (pe != NULL);
1600 _dbus_assert (pe->type == ELEMENT_POLICY);
1602 switch (pe->d.policy.type)
1604 case POLICY_IGNORED:
1605 /* drop the rule on the floor */
1608 case POLICY_DEFAULT:
1609 if (!bus_policy_append_default_rule (parser->policy, rule))
1612 case POLICY_MANDATORY:
1613 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1617 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1619 dbus_set_error (error, DBUS_ERROR_FAILED,
1620 "<%s> rule cannot be per-user because it has bus-global semantics",
1625 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1630 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1632 dbus_set_error (error, DBUS_ERROR_FAILED,
1633 "<%s> rule cannot be per-group because it has bus-global semantics",
1638 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1644 case POLICY_CONSOLE:
1645 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1651 bus_policy_rule_unref (rule);
1658 BUS_SET_OOM (error);
1661 bus_policy_rule_unref (rule);
1666 start_policy_child (BusConfigParser *parser,
1667 const char *element_name,
1668 const char **attribute_names,
1669 const char **attribute_values,
1672 if (strcmp (element_name, "allow") == 0)
1674 if (!append_rule_from_element (parser, element_name,
1675 attribute_names, attribute_values,
1679 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1681 BUS_SET_OOM (error);
1687 else if (strcmp (element_name, "deny") == 0)
1689 if (!append_rule_from_element (parser, element_name,
1690 attribute_names, attribute_values,
1694 if (push_element (parser, ELEMENT_DENY) == NULL)
1696 BUS_SET_OOM (error);
1704 dbus_set_error (error, DBUS_ERROR_FAILED,
1705 "Element <%s> not allowed inside <%s> in configuration file",
1706 element_name, "policy");
1712 start_selinux_child (BusConfigParser *parser,
1713 const char *element_name,
1714 const char **attribute_names,
1715 const char **attribute_values,
1722 context_copy = NULL;
1724 if (strcmp (element_name, "associate") == 0)
1727 const char *context;
1729 if (!locate_attributes (parser, "associate",
1734 "context", &context,
1738 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1740 BUS_SET_OOM (error);
1744 if (own == NULL || context == NULL)
1746 dbus_set_error (error, DBUS_ERROR_FAILED,
1747 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1751 own_copy = _dbus_strdup (own);
1752 if (own_copy == NULL)
1754 context_copy = _dbus_strdup (context);
1755 if (context_copy == NULL)
1758 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1759 own_copy, context_copy))
1766 dbus_set_error (error, DBUS_ERROR_FAILED,
1767 "Element <%s> not allowed inside <%s> in configuration file",
1768 element_name, "selinux");
1774 dbus_free (own_copy);
1777 dbus_free (context_copy);
1779 BUS_SET_OOM (error);
1784 bus_config_parser_start_element (BusConfigParser *parser,
1785 const char *element_name,
1786 const char **attribute_names,
1787 const char **attribute_values,
1792 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1794 /* printf ("START: %s\n", element_name); */
1796 t = top_element_type (parser);
1798 if (t == ELEMENT_NONE)
1800 if (strcmp (element_name, "busconfig") == 0)
1802 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1805 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1807 BUS_SET_OOM (error);
1815 dbus_set_error (error, DBUS_ERROR_FAILED,
1816 "Unknown element <%s> at root of configuration file",
1821 else if (t == ELEMENT_BUSCONFIG)
1823 return start_busconfig_child (parser, element_name,
1824 attribute_names, attribute_values,
1827 else if (t == ELEMENT_POLICY)
1829 return start_policy_child (parser, element_name,
1830 attribute_names, attribute_values,
1833 else if (t == ELEMENT_SELINUX)
1835 return start_selinux_child (parser, element_name,
1836 attribute_names, attribute_values,
1841 dbus_set_error (error, DBUS_ERROR_FAILED,
1842 "Element <%s> is not allowed in this context",
1849 set_limit (BusConfigParser *parser,
1854 dbus_bool_t must_be_positive;
1855 dbus_bool_t must_be_int;
1857 must_be_int = FALSE;
1858 must_be_positive = FALSE;
1860 if (strcmp (name, "max_incoming_bytes") == 0)
1862 must_be_positive = TRUE;
1863 parser->limits.max_incoming_bytes = value;
1865 else if (strcmp (name, "max_incoming_unix_fds") == 0)
1867 must_be_positive = TRUE;
1868 parser->limits.max_incoming_unix_fds = value;
1870 else if (strcmp (name, "max_outgoing_bytes") == 0)
1872 must_be_positive = TRUE;
1873 parser->limits.max_outgoing_bytes = value;
1875 else if (strcmp (name, "max_outgoing_unix_fds") == 0)
1877 must_be_positive = TRUE;
1878 parser->limits.max_outgoing_unix_fds = value;
1880 else if (strcmp (name, "max_message_size") == 0)
1882 must_be_positive = TRUE;
1883 parser->limits.max_message_size = value;
1885 else if (strcmp (name, "max_message_unix_fds") == 0)
1887 must_be_positive = TRUE;
1888 parser->limits.max_message_unix_fds = value;
1890 else if (strcmp (name, "service_start_timeout") == 0)
1892 must_be_positive = TRUE;
1894 parser->limits.activation_timeout = value;
1896 else if (strcmp (name, "auth_timeout") == 0)
1898 must_be_positive = TRUE;
1900 parser->limits.auth_timeout = value;
1902 else if (strcmp (name, "reply_timeout") == 0)
1904 must_be_positive = TRUE;
1906 parser->limits.reply_timeout = value;
1908 else if (strcmp (name, "max_completed_connections") == 0)
1910 must_be_positive = TRUE;
1912 parser->limits.max_completed_connections = value;
1914 else if (strcmp (name, "max_incomplete_connections") == 0)
1916 must_be_positive = TRUE;
1918 parser->limits.max_incomplete_connections = value;
1920 else if (strcmp (name, "max_connections_per_user") == 0)
1922 must_be_positive = TRUE;
1924 parser->limits.max_connections_per_user = value;
1926 else if (strcmp (name, "max_pending_service_starts") == 0)
1928 must_be_positive = TRUE;
1930 parser->limits.max_pending_activations = value;
1932 else if (strcmp (name, "max_names_per_connection") == 0)
1934 must_be_positive = TRUE;
1936 parser->limits.max_services_per_connection = value;
1938 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1940 must_be_positive = TRUE;
1942 parser->limits.max_match_rules_per_connection = value;
1944 else if (strcmp (name, "max_replies_per_connection") == 0)
1946 must_be_positive = TRUE;
1948 parser->limits.max_replies_per_connection = value;
1952 dbus_set_error (error, DBUS_ERROR_FAILED,
1953 "There is no limit called \"%s\"\n",
1958 if (must_be_positive && value < 0)
1960 dbus_set_error (error, DBUS_ERROR_FAILED,
1961 "<limit name=\"%s\"> must be a positive number\n",
1967 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1969 dbus_set_error (error, DBUS_ERROR_FAILED,
1970 "<limit name=\"%s\"> value is too large\n",
1979 bus_config_parser_end_element (BusConfigParser *parser,
1980 const char *element_name,
1987 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1989 /* printf ("END: %s\n", element_name); */
1991 t = top_element_type (parser);
1993 if (t == ELEMENT_NONE)
1995 /* should probably be an assertion failure but
1996 * being paranoid about XML parsers
1998 dbus_set_error (error, DBUS_ERROR_FAILED,
1999 "XML parser ended element with no element on the stack");
2003 n = bus_config_parser_element_type_to_name (t);
2004 _dbus_assert (n != NULL);
2005 if (strcmp (n, element_name) != 0)
2007 /* should probably be an assertion failure but
2008 * being paranoid about XML parsers
2010 dbus_set_error (error, DBUS_ERROR_FAILED,
2011 "XML element <%s> ended but topmost element on the stack was <%s>",
2016 e = peek_element (parser);
2017 _dbus_assert (e != NULL);
2022 _dbus_assert_not_reached ("element in stack has no type");
2025 case ELEMENT_INCLUDE:
2027 case ELEMENT_CONFIGTYPE:
2028 case ELEMENT_LISTEN:
2029 case ELEMENT_PIDFILE:
2031 case ELEMENT_SERVICEDIR:
2032 case ELEMENT_SERVICEHELPER:
2033 case ELEMENT_INCLUDEDIR:
2035 if (!e->had_content)
2037 dbus_set_error (error, DBUS_ERROR_FAILED,
2038 "XML element <%s> was expected to have content inside it",
2039 bus_config_parser_element_type_to_name (e->type));
2043 if (e->type == ELEMENT_LIMIT)
2045 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
2051 case ELEMENT_BUSCONFIG:
2052 case ELEMENT_POLICY:
2056 case ELEMENT_SYSLOG:
2057 case ELEMENT_KEEP_UMASK:
2058 case ELEMENT_SELINUX:
2059 case ELEMENT_ASSOCIATE:
2060 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2061 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2062 case ELEMENT_ALLOW_ANONYMOUS:
2066 pop_element (parser);
2072 all_whitespace (const DBusString *str)
2076 _dbus_string_skip_white (str, 0, &i);
2078 return i == _dbus_string_get_length (str);
2082 make_full_path (const DBusString *basedir,
2083 const DBusString *filename,
2084 DBusString *full_path)
2086 if (_dbus_path_is_absolute (filename))
2088 return _dbus_string_copy (filename, 0, full_path, 0);
2092 if (!_dbus_string_copy (basedir, 0, full_path, 0))
2095 if (!_dbus_concat_dir_and_file (full_path, filename))
2103 include_file (BusConfigParser *parser,
2104 const DBusString *filename,
2105 dbus_bool_t ignore_missing,
2108 /* FIXME good test case for this would load each config file in the
2109 * test suite both alone, and as an include, and check
2110 * that the result is the same
2112 BusConfigParser *included;
2113 const char *filename_str;
2114 DBusError tmp_error;
2116 dbus_error_init (&tmp_error);
2118 filename_str = _dbus_string_get_const_data (filename);
2120 /* Check to make sure this file hasn't already been included. */
2121 if (seen_include (parser, filename))
2123 dbus_set_error (error, DBUS_ERROR_FAILED,
2124 "Circular inclusion of file '%s'",
2129 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2131 BUS_SET_OOM (error);
2135 /* Since parser is passed in as the parent, included
2136 inherits parser's limits. */
2137 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2139 _dbus_list_pop_last (&parser->included_files);
2141 if (included == NULL)
2143 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2145 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2148 dbus_error_free (&tmp_error);
2153 dbus_move_error (&tmp_error, error);
2159 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2161 if (!merge_included (parser, included, error))
2163 bus_config_parser_unref (included);
2167 /* Copy included's limits back to parser. */
2168 parser->limits = included->limits;
2170 bus_config_parser_unref (included);
2176 servicehelper_path (BusConfigParser *parser,
2177 const DBusString *filename,
2180 const char *filename_str;
2181 char *servicehelper;
2183 filename_str = _dbus_string_get_const_data (filename);
2185 /* copy to avoid overwriting with NULL on OOM */
2186 servicehelper = _dbus_strdup (filename_str);
2189 if (servicehelper == NULL)
2191 BUS_SET_OOM (error);
2195 /* save the latest servicehelper only if not OOM */
2196 dbus_free (parser->servicehelper);
2197 parser->servicehelper = servicehelper;
2199 /* We don't check whether the helper exists; instead we
2200 * would just fail to ever activate anything if it doesn't.
2201 * This allows an admin to fix the problem if it doesn't exist.
2202 * It also allows the parser test suite to successfully parse
2203 * test cases without installing the helper. ;-)
2210 include_dir (BusConfigParser *parser,
2211 const DBusString *dirname,
2214 DBusString filename;
2216 DBusError tmp_error;
2220 if (!_dbus_string_init (&filename))
2222 BUS_SET_OOM (error);
2228 dir = _dbus_directory_open (dirname, error);
2233 dbus_error_init (&tmp_error);
2234 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2236 DBusString full_path;
2238 if (!_dbus_string_init (&full_path))
2240 BUS_SET_OOM (error);
2244 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2246 BUS_SET_OOM (error);
2247 _dbus_string_free (&full_path);
2251 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2253 BUS_SET_OOM (error);
2254 _dbus_string_free (&full_path);
2258 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2260 if (!include_file (parser, &full_path, TRUE, error))
2262 if (dbus_error_is_set (error))
2264 /* We log to syslog unconditionally here, because this is
2265 * the configuration parser, so we don't yet know whether
2266 * this bus is going to want to write to syslog! (There's
2267 * also some layer inversion going on, if we want to use
2268 * the bus context.) */
2269 _dbus_system_log (DBUS_SYSTEM_LOG_INFO,
2270 "Encountered error '%s' while parsing '%s'\n",
2272 _dbus_string_get_const_data (&full_path));
2273 dbus_error_free (error);
2278 _dbus_string_free (&full_path);
2281 if (dbus_error_is_set (&tmp_error))
2283 dbus_move_error (&tmp_error, error);
2288 if (!_dbus_string_copy_data (dirname, &s))
2290 BUS_SET_OOM (error);
2294 if (!_dbus_list_append (&parser->conf_dirs, s))
2297 BUS_SET_OOM (error);
2304 _dbus_string_free (&filename);
2307 _dbus_directory_close (dir);
2313 bus_config_parser_content (BusConfigParser *parser,
2314 const DBusString *content,
2319 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2325 _dbus_string_get_const_data (content, &c_str);
2327 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2331 e = peek_element (parser);
2334 dbus_set_error (error, DBUS_ERROR_FAILED,
2335 "Text content outside of any XML element in configuration file");
2338 else if (e->had_content)
2340 _dbus_assert_not_reached ("Element had multiple content blocks");
2344 switch (top_element_type (parser))
2347 _dbus_assert_not_reached ("element at top of stack has no type");
2350 case ELEMENT_BUSCONFIG:
2351 case ELEMENT_POLICY:
2355 case ELEMENT_SYSLOG:
2356 case ELEMENT_KEEP_UMASK:
2357 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2358 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2359 case ELEMENT_ALLOW_ANONYMOUS:
2360 case ELEMENT_SELINUX:
2361 case ELEMENT_ASSOCIATE:
2362 if (all_whitespace (content))
2366 dbus_set_error (error, DBUS_ERROR_FAILED,
2367 "No text content expected inside XML element %s in configuration file",
2368 bus_config_parser_element_type_to_name (top_element_type (parser)));
2372 case ELEMENT_PIDFILE:
2376 e->had_content = TRUE;
2378 if (!_dbus_string_copy_data (content, &s))
2381 dbus_free (parser->pidfile);
2382 parser->pidfile = s;
2386 case ELEMENT_INCLUDE:
2388 DBusString full_path, selinux_policy_root;
2390 e->had_content = TRUE;
2392 if (e->d.include.if_selinux_enabled
2393 && !bus_selinux_enabled ())
2396 if (!_dbus_string_init (&full_path))
2399 if (e->d.include.selinux_root_relative)
2401 if (!bus_selinux_get_policy_root ())
2403 dbus_set_error (error, DBUS_ERROR_FAILED,
2404 "Could not determine SELinux policy root for relative inclusion");
2405 _dbus_string_free (&full_path);
2408 _dbus_string_init_const (&selinux_policy_root,
2409 bus_selinux_get_policy_root ());
2410 if (!make_full_path (&selinux_policy_root, content, &full_path))
2412 _dbus_string_free (&full_path);
2416 else if (!make_full_path (&parser->basedir, content, &full_path))
2418 _dbus_string_free (&full_path);
2422 if (!include_file (parser, &full_path,
2423 e->d.include.ignore_missing, error))
2425 _dbus_string_free (&full_path);
2429 _dbus_string_free (&full_path);
2433 case ELEMENT_SERVICEHELPER:
2435 DBusString full_path;
2437 e->had_content = TRUE;
2439 if (!_dbus_string_init (&full_path))
2442 if (!make_full_path (&parser->basedir, content, &full_path))
2444 _dbus_string_free (&full_path);
2448 if (!servicehelper_path (parser, &full_path, error))
2450 _dbus_string_free (&full_path);
2454 _dbus_string_free (&full_path);
2458 case ELEMENT_INCLUDEDIR:
2460 DBusString full_path;
2462 e->had_content = TRUE;
2464 if (!_dbus_string_init (&full_path))
2467 if (!make_full_path (&parser->basedir, content, &full_path))
2469 _dbus_string_free (&full_path);
2473 if (!include_dir (parser, &full_path, error))
2475 _dbus_string_free (&full_path);
2479 _dbus_string_free (&full_path);
2487 e->had_content = TRUE;
2489 if (!_dbus_string_copy_data (content, &s))
2492 dbus_free (parser->user);
2497 case ELEMENT_CONFIGTYPE:
2501 e->had_content = TRUE;
2503 if (!_dbus_string_copy_data (content, &s))
2506 dbus_free (parser->bus_type);
2507 parser->bus_type = s;
2511 case ELEMENT_LISTEN:
2515 e->had_content = TRUE;
2517 if (!_dbus_string_copy_data (content, &s))
2520 if (!_dbus_list_append (&parser->listen_on,
2533 e->had_content = TRUE;
2535 if (!_dbus_string_copy_data (content, &s))
2538 if (!_dbus_list_append (&parser->mechanisms,
2547 case ELEMENT_SERVICEDIR:
2550 DBusString full_path;
2552 e->had_content = TRUE;
2554 if (!_dbus_string_init (&full_path))
2557 if (!make_full_path (&parser->basedir, content, &full_path))
2559 _dbus_string_free (&full_path);
2563 if (!_dbus_string_copy_data (&full_path, &s))
2565 _dbus_string_free (&full_path);
2569 /* _only_ extra session directories can be specified */
2570 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2572 _dbus_string_free (&full_path);
2577 _dbus_string_free (&full_path);
2585 e->had_content = TRUE;
2588 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2590 dbus_set_error (error, DBUS_ERROR_FAILED,
2591 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2596 e->d.limit.value = val;
2598 _dbus_verbose ("Loaded value %ld for limit %s\n",
2605 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2609 BUS_SET_OOM (error);
2614 bus_config_parser_finished (BusConfigParser *parser,
2617 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2619 if (parser->stack != NULL)
2621 dbus_set_error (error, DBUS_ERROR_FAILED,
2622 "Element <%s> was not closed in configuration file",
2623 bus_config_parser_element_type_to_name (top_element_type (parser)));
2628 if (parser->is_toplevel && parser->listen_on == NULL)
2630 dbus_set_error (error, DBUS_ERROR_FAILED,
2631 "Configuration file needs one or more <listen> elements giving addresses");
2639 bus_config_parser_get_user (BusConfigParser *parser)
2641 return parser->user;
2645 bus_config_parser_get_type (BusConfigParser *parser)
2647 return parser->bus_type;
2651 bus_config_parser_get_addresses (BusConfigParser *parser)
2653 return &parser->listen_on;
2657 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2659 return &parser->mechanisms;
2663 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2665 return &parser->service_dirs;
2669 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2671 return &parser->conf_dirs;
2675 bus_config_parser_get_fork (BusConfigParser *parser)
2677 return parser->fork;
2681 bus_config_parser_get_syslog (BusConfigParser *parser)
2683 return parser->syslog;
2687 bus_config_parser_get_keep_umask (BusConfigParser *parser)
2689 return parser->keep_umask;
2693 bus_config_parser_get_allow_anonymous (BusConfigParser *parser)
2695 return parser->allow_anonymous;
2699 bus_config_parser_get_pidfile (BusConfigParser *parser)
2701 return parser->pidfile;
2705 bus_config_parser_get_servicehelper (BusConfigParser *parser)
2707 return parser->servicehelper;
2711 bus_config_parser_steal_policy (BusConfigParser *parser)
2715 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2717 policy = parser->policy;
2719 parser->policy = NULL;
2724 /* Overwrite any limits that were set in the configuration file */
2726 bus_config_parser_get_limits (BusConfigParser *parser,
2729 *limits = parser->limits;
2733 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2735 DBusHashTable *table;
2737 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2739 table = parser->service_context_table;
2741 parser->service_context_table = NULL;
2746 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
2757 do_check_own_rules (BusPolicy *policy)
2761 dbus_bool_t allowed;
2763 {"org.freedesktop", FALSE},
2764 {"org.freedesktop.ManySystem", FALSE},
2765 {"org.freedesktop.ManySystems", TRUE},
2766 {"org.freedesktop.ManySystems.foo", TRUE},
2767 {"org.freedesktop.ManySystems.foo.bar", TRUE},
2768 {"org.freedesktop.ManySystems2", FALSE},
2769 {"org.freedesktop.ManySystems2.foo", FALSE},
2770 {"org.freedesktop.ManySystems2.foo.bar", FALSE},
2775 while (checks[i].name)
2777 DBusString service_name;
2780 if (!_dbus_string_init (&service_name))
2781 _dbus_assert_not_reached ("couldn't init string");
2782 if (!_dbus_string_append (&service_name, checks[i].name))
2783 _dbus_assert_not_reached ("couldn't append string");
2785 ret = bus_policy_check_can_own (policy, &service_name);
2786 printf (" Check name %s: %s\n", checks[i].name,
2787 ret ? "allowed" : "not allowed");
2788 if (checks[i].allowed && !ret)
2790 _dbus_warn ("Cannot own %s\n", checks[i].name);
2793 if (!checks[i].allowed && ret)
2795 _dbus_warn ("Can own %s\n", checks[i].name);
2798 _dbus_string_free (&service_name);
2807 do_load (const DBusString *full_path,
2809 dbus_bool_t oom_possible,
2810 dbus_bool_t check_own_rules)
2812 BusConfigParser *parser;
2815 dbus_error_init (&error);
2817 parser = bus_config_load (full_path, TRUE, NULL, &error);
2820 _DBUS_ASSERT_ERROR_IS_SET (&error);
2823 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2825 _dbus_verbose ("Failed to load valid file due to OOM\n");
2826 dbus_error_free (&error);
2829 else if (validity == VALID)
2831 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2834 dbus_error_free (&error);
2839 dbus_error_free (&error);
2845 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2847 if (check_own_rules && do_check_own_rules (parser->policy) == FALSE)
2852 bus_config_parser_unref (parser);
2854 if (validity == INVALID)
2856 _dbus_warn ("Accepted invalid file\n");
2866 const DBusString *full_path;
2868 dbus_bool_t check_own_rules;
2872 check_loader_oom_func (void *data)
2874 LoaderOomData *d = data;
2876 return do_load (d->full_path, d->validity, TRUE, d->check_own_rules);
2880 process_test_valid_subdir (const DBusString *test_base_dir,
2884 DBusString test_directory;
2885 DBusString filename;
2893 if (!_dbus_string_init (&test_directory))
2894 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2896 _dbus_string_init_const (&filename, subdir);
2898 if (!_dbus_string_copy (test_base_dir, 0,
2899 &test_directory, 0))
2900 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2902 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2903 _dbus_assert_not_reached ("couldn't allocate full path");
2905 _dbus_string_free (&filename);
2906 if (!_dbus_string_init (&filename))
2907 _dbus_assert_not_reached ("didn't allocate filename string\n");
2909 dbus_error_init (&error);
2910 dir = _dbus_directory_open (&test_directory, &error);
2913 _dbus_warn ("Could not open %s: %s\n",
2914 _dbus_string_get_const_data (&test_directory),
2916 dbus_error_free (&error);
2920 if (validity == VALID)
2921 printf ("Testing valid files:\n");
2922 else if (validity == INVALID)
2923 printf ("Testing invalid files:\n");
2925 printf ("Testing unknown files:\n");
2928 while (_dbus_directory_get_next_file (dir, &filename, &error))
2930 DBusString full_path;
2933 if (!_dbus_string_init (&full_path))
2934 _dbus_assert_not_reached ("couldn't init string");
2936 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2937 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2939 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2940 _dbus_assert_not_reached ("couldn't concat file to dir");
2942 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2944 _dbus_verbose ("Skipping non-.conf file %s\n",
2945 _dbus_string_get_const_data (&filename));
2946 _dbus_string_free (&full_path);
2950 printf (" %s\n", _dbus_string_get_const_data (&filename));
2952 _dbus_verbose (" expecting %s\n",
2953 validity == VALID ? "valid" :
2954 (validity == INVALID ? "invalid" :
2955 (validity == UNKNOWN ? "unknown" : "???")));
2957 d.full_path = &full_path;
2958 d.validity = validity;
2959 d.check_own_rules = _dbus_string_ends_with_c_str (&full_path,
2960 "check-own-rules.conf");
2962 /* FIXME hackaround for an expat problem, see
2963 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2964 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2966 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2967 if (!check_loader_oom_func (&d))
2968 _dbus_assert_not_reached ("test failed");
2970 _dbus_string_free (&full_path);
2973 if (dbus_error_is_set (&error))
2975 _dbus_warn ("Could not get next file in %s: %s\n",
2976 _dbus_string_get_const_data (&test_directory),
2978 dbus_error_free (&error);
2987 _dbus_directory_close (dir);
2988 _dbus_string_free (&test_directory);
2989 _dbus_string_free (&filename);
2995 bools_equal (dbus_bool_t a,
3002 strings_equal_or_both_null (const char *a,
3005 if (a == NULL || b == NULL)
3008 return !strcmp (a, b);
3012 elements_equal (const Element *a,
3015 if (a->type != b->type)
3018 if (!bools_equal (a->had_content, b->had_content))
3024 case ELEMENT_INCLUDE:
3025 if (!bools_equal (a->d.include.ignore_missing,
3026 b->d.include.ignore_missing))
3030 case ELEMENT_POLICY:
3031 if (a->d.policy.type != b->d.policy.type)
3033 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
3038 if (strcmp (a->d.limit.name, b->d.limit.name))
3040 if (a->d.limit.value != b->d.limit.value)
3054 lists_of_elements_equal (DBusList *a,
3063 while (ia != NULL && ib != NULL)
3065 if (elements_equal (ia->data, ib->data))
3067 ia = _dbus_list_get_next_link (&a, ia);
3068 ib = _dbus_list_get_next_link (&b, ib);
3071 return ia == NULL && ib == NULL;
3075 lists_of_c_strings_equal (DBusList *a,
3084 while (ia != NULL && ib != NULL)
3086 if (strcmp (ia->data, ib->data))
3088 ia = _dbus_list_get_next_link (&a, ia);
3089 ib = _dbus_list_get_next_link (&b, ib);
3092 return ia == NULL && ib == NULL;
3096 limits_equal (const BusLimits *a,
3100 (a->max_incoming_bytes == b->max_incoming_bytes
3101 || a->max_incoming_unix_fds == b->max_incoming_unix_fds
3102 || a->max_outgoing_bytes == b->max_outgoing_bytes
3103 || a->max_outgoing_unix_fds == b->max_outgoing_unix_fds
3104 || a->max_message_size == b->max_message_size
3105 || a->max_message_unix_fds == b->max_message_unix_fds
3106 || a->activation_timeout == b->activation_timeout
3107 || a->auth_timeout == b->auth_timeout
3108 || a->max_completed_connections == b->max_completed_connections
3109 || a->max_incomplete_connections == b->max_incomplete_connections
3110 || a->max_connections_per_user == b->max_connections_per_user
3111 || a->max_pending_activations == b->max_pending_activations
3112 || a->max_services_per_connection == b->max_services_per_connection
3113 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
3114 || a->max_replies_per_connection == b->max_replies_per_connection
3115 || a->reply_timeout == b->reply_timeout);
3119 config_parsers_equal (const BusConfigParser *a,
3120 const BusConfigParser *b)
3122 if (!_dbus_string_equal (&a->basedir, &b->basedir))
3125 if (!lists_of_elements_equal (a->stack, b->stack))
3128 if (!strings_equal_or_both_null (a->user, b->user))
3131 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
3134 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
3137 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
3140 /* FIXME: compare policy */
3142 /* FIXME: compare service selinux ID table */
3144 if (! limits_equal (&a->limits, &b->limits))
3147 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
3150 if (! bools_equal (a->fork, b->fork))
3153 if (! bools_equal (a->keep_umask, b->keep_umask))
3156 if (! bools_equal (a->is_toplevel, b->is_toplevel))
3163 all_are_equiv (const DBusString *target_directory)
3165 DBusString filename;
3167 BusConfigParser *first_parser;
3168 BusConfigParser *parser;
3174 first_parser = NULL;
3178 if (!_dbus_string_init (&filename))
3179 _dbus_assert_not_reached ("didn't allocate filename string");
3181 dbus_error_init (&error);
3182 dir = _dbus_directory_open (target_directory, &error);
3185 _dbus_warn ("Could not open %s: %s\n",
3186 _dbus_string_get_const_data (target_directory),
3188 dbus_error_free (&error);
3192 printf ("Comparing equivalent files:\n");
3195 while (_dbus_directory_get_next_file (dir, &filename, &error))
3197 DBusString full_path;
3199 if (!_dbus_string_init (&full_path))
3200 _dbus_assert_not_reached ("couldn't init string");
3202 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
3203 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3205 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3206 _dbus_assert_not_reached ("couldn't concat file to dir");
3208 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3210 _dbus_verbose ("Skipping non-.conf file %s\n",
3211 _dbus_string_get_const_data (&filename));
3212 _dbus_string_free (&full_path);
3216 printf (" %s\n", _dbus_string_get_const_data (&filename));
3218 parser = bus_config_load (&full_path, TRUE, NULL, &error);
3222 _dbus_warn ("Could not load file %s: %s\n",
3223 _dbus_string_get_const_data (&full_path),
3225 _dbus_string_free (&full_path);
3226 dbus_error_free (&error);
3229 else if (first_parser == NULL)
3231 _dbus_string_free (&full_path);
3232 first_parser = parser;
3236 _dbus_string_free (&full_path);
3237 equal = config_parsers_equal (first_parser, parser);
3238 bus_config_parser_unref (parser);
3247 _dbus_string_free (&filename);
3249 bus_config_parser_unref (first_parser);
3251 _dbus_directory_close (dir);
3258 process_test_equiv_subdir (const DBusString *test_base_dir,
3261 DBusString test_directory;
3262 DBusString filename;
3271 if (!_dbus_string_init (&test_directory))
3272 _dbus_assert_not_reached ("didn't allocate test_directory");
3274 _dbus_string_init_const (&filename, subdir);
3276 if (!_dbus_string_copy (test_base_dir, 0,
3277 &test_directory, 0))
3278 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3280 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3281 _dbus_assert_not_reached ("couldn't allocate full path");
3283 _dbus_string_free (&filename);
3284 if (!_dbus_string_init (&filename))
3285 _dbus_assert_not_reached ("didn't allocate filename string");
3287 dbus_error_init (&error);
3288 dir = _dbus_directory_open (&test_directory, &error);
3291 _dbus_warn ("Could not open %s: %s\n",
3292 _dbus_string_get_const_data (&test_directory),
3294 dbus_error_free (&error);
3298 while (_dbus_directory_get_next_file (dir, &filename, &error))
3300 DBusString full_path;
3302 /* Skip CVS's magic directories! */
3303 if (_dbus_string_equal_c_str (&filename, "CVS"))
3306 if (!_dbus_string_init (&full_path))
3307 _dbus_assert_not_reached ("couldn't init string");
3309 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3310 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3312 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3313 _dbus_assert_not_reached ("couldn't concat file to dir");
3315 equal = all_are_equiv (&full_path);
3316 _dbus_string_free (&full_path);
3325 _dbus_string_free (&test_directory);
3326 _dbus_string_free (&filename);
3328 _dbus_directory_close (dir);
3334 static const char *test_session_service_dir_matches[] =
3337 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3338 "/testusr/testlocal/testshare/dbus-1/services",
3339 "/testusr/testshare/dbus-1/services",
3340 DBUS_DATADIR"/dbus-1/services",
3342 /* will be filled in test_default_session_servicedirs() */
3351 test_default_session_servicedirs (void)
3359 const char *common_progs;
3362 if (_dbus_get_install_root(buffer, sizeof(buffer)))
3364 strcat(buffer,DBUS_DATADIR);
3365 strcat(buffer,"/dbus-1/services");
3366 test_session_service_dir_matches[0] = buffer;
3370 /* On Unix we don't actually use this variable, but it's easier to handle the
3371 * deallocation if we always allocate it, whether needed or not */
3372 if (!_dbus_string_init (&progs))
3373 _dbus_assert_not_reached ("OOM allocating progs");
3376 common_progs = _dbus_getenv ("CommonProgramFiles");
3380 if (!_dbus_string_append (&progs, common_progs))
3382 _dbus_string_free (&progs);
3386 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3388 _dbus_string_free (&progs);
3391 test_session_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3396 printf ("Testing retrieving the default session service directories\n");
3397 if (!_dbus_get_standard_session_servicedirs (&dirs))
3398 _dbus_assert_not_reached ("couldn't get stardard dirs");
3400 /* make sure our defaults end with share/dbus-1/service */
3401 while ((link = _dbus_list_pop_first_link (&dirs)))
3405 printf (" default service dir: %s\n", (char *)link->data);
3406 _dbus_string_init_const (&path, (char *)link->data);
3407 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/services"))
3409 printf ("error with default session service directories\n");
3410 dbus_free (link->data);
3411 _dbus_list_free_link (link);
3412 _dbus_string_free (&progs);
3416 dbus_free (link->data);
3417 _dbus_list_free_link (link);
3421 if (!dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3422 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3424 if (!dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3425 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3427 if (!_dbus_get_standard_session_servicedirs (&dirs))
3428 _dbus_assert_not_reached ("couldn't get stardard dirs");
3430 /* make sure we read and parse the env variable correctly */
3432 while ((link = _dbus_list_pop_first_link (&dirs)))
3434 printf (" test service dir: %s\n", (char *)link->data);
3435 if (test_session_service_dir_matches[i] == NULL)
3437 printf ("more directories parsed than in match set\n");
3438 dbus_free (link->data);
3439 _dbus_list_free_link (link);
3440 _dbus_string_free (&progs);
3444 if (strcmp (test_session_service_dir_matches[i],
3445 (char *)link->data) != 0)
3447 printf ("%s directory does not match %s in the match set\n",
3449 test_session_service_dir_matches[i]);
3450 dbus_free (link->data);
3451 _dbus_list_free_link (link);
3452 _dbus_string_free (&progs);
3458 dbus_free (link->data);
3459 _dbus_list_free_link (link);
3462 if (test_session_service_dir_matches[i] != NULL)
3464 printf ("extra data %s in the match set was not matched\n",
3465 test_session_service_dir_matches[i]);
3467 _dbus_string_free (&progs);
3471 _dbus_string_free (&progs);
3475 static const char *test_system_service_dir_matches[] =
3478 "/usr/local/share/dbus-1/system-services",
3479 "/usr/share/dbus-1/system-services",
3481 DBUS_DATADIR"/dbus-1/system-services",
3483 "/lib/dbus-1/system-services",
3493 test_default_system_servicedirs (void)
3499 const char *common_progs;
3503 /* On Unix we don't actually use this variable, but it's easier to handle the
3504 * deallocation if we always allocate it, whether needed or not */
3505 if (!_dbus_string_init (&progs))
3506 _dbus_assert_not_reached ("OOM allocating progs");
3509 common_progs = _dbus_getenv ("CommonProgramFiles");
3513 if (!_dbus_string_append (&progs, common_progs))
3515 _dbus_string_free (&progs);
3519 if (!_dbus_string_append (&progs, "/dbus-1/system-services"))
3521 _dbus_string_free (&progs);
3524 test_system_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3529 printf ("Testing retrieving the default system service directories\n");
3530 if (!_dbus_get_standard_system_servicedirs (&dirs))
3531 _dbus_assert_not_reached ("couldn't get stardard dirs");
3533 /* make sure our defaults end with share/dbus-1/system-service */
3534 while ((link = _dbus_list_pop_first_link (&dirs)))
3538 printf (" default service dir: %s\n", (char *)link->data);
3539 _dbus_string_init_const (&path, (char *)link->data);
3540 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/system-services"))
3542 printf ("error with default system service directories\n");
3543 dbus_free (link->data);
3544 _dbus_list_free_link (link);
3545 _dbus_string_free (&progs);
3549 dbus_free (link->data);
3550 _dbus_list_free_link (link);
3554 if (!dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3555 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3557 if (!dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3558 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3560 if (!_dbus_get_standard_system_servicedirs (&dirs))
3561 _dbus_assert_not_reached ("couldn't get stardard dirs");
3563 /* make sure we read and parse the env variable correctly */
3565 while ((link = _dbus_list_pop_first_link (&dirs)))
3567 printf (" test service dir: %s\n", (char *)link->data);
3568 if (test_system_service_dir_matches[i] == NULL)
3570 printf ("more directories parsed than in match set\n");
3571 dbus_free (link->data);
3572 _dbus_list_free_link (link);
3573 _dbus_string_free (&progs);
3577 if (strcmp (test_system_service_dir_matches[i],
3578 (char *)link->data) != 0)
3580 printf ("%s directory does not match %s in the match set\n",
3582 test_system_service_dir_matches[i]);
3583 dbus_free (link->data);
3584 _dbus_list_free_link (link);
3585 _dbus_string_free (&progs);
3591 dbus_free (link->data);
3592 _dbus_list_free_link (link);
3595 if (test_system_service_dir_matches[i] != NULL)
3597 printf ("extra data %s in the match set was not matched\n",
3598 test_system_service_dir_matches[i]);
3600 _dbus_string_free (&progs);
3604 _dbus_string_free (&progs);
3609 bus_config_parser_test (const DBusString *test_data_dir)
3611 if (test_data_dir == NULL ||
3612 _dbus_string_get_length (test_data_dir) == 0)
3614 printf ("No test data\n");
3618 if (!test_default_session_servicedirs())
3622 printf("default system service dir skipped\n");
3624 if (!test_default_system_servicedirs())
3628 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3631 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3634 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3640 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */