1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* config-parser.c XML-library-agnostic configuration file parser
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config-parser.h"
27 #include <dbus/dbus-list.h>
28 #include <dbus/dbus-internals.h>
52 /* we ignore policies for unknown groups/users */
66 unsigned int had_content : 1;
72 unsigned int ignore_missing : 1;
78 unsigned long gid_or_uid;
91 struct BusConfigParser
95 DBusString basedir; /**< Directory we resolve paths relative to */
97 DBusList *stack; /**< stack of Element */
99 char *user; /**< user to run as */
101 char *bus_type; /**< Message bus type */
103 DBusList *listen_on; /**< List of addresses to listen to */
105 DBusList *mechanisms; /**< Auth mechanisms */
107 DBusList *service_dirs; /**< Directories to look for services in */
109 BusPolicy *policy; /**< Security policy */
111 BusLimits limits; /**< Limits */
113 char *pidfile; /**< PID file */
115 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
117 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
121 element_type_to_name (ElementType type)
127 case ELEMENT_BUSCONFIG:
129 case ELEMENT_INCLUDE:
147 case ELEMENT_PIDFILE:
149 case ELEMENT_SERVICEDIR:
151 case ELEMENT_INCLUDEDIR:
157 _dbus_assert_not_reached ("bad element type");
163 push_element (BusConfigParser *parser,
168 _dbus_assert (type != ELEMENT_NONE);
170 e = dbus_new0 (Element, 1);
174 if (!_dbus_list_append (&parser->stack, e))
186 element_free (Element *e)
188 if (e->type == ELEMENT_LIMIT)
189 dbus_free (e->d.limit.name);
195 pop_element (BusConfigParser *parser)
199 e = _dbus_list_pop_last (&parser->stack);
205 peek_element (BusConfigParser *parser)
209 e = _dbus_list_get_last (&parser->stack);
215 top_element_type (BusConfigParser *parser)
219 e = _dbus_list_get_last (&parser->stack);
228 merge_included (BusConfigParser *parser,
229 BusConfigParser *included,
234 if (!bus_policy_merge (parser->policy,
241 if (included->user != NULL)
243 dbus_free (parser->user);
244 parser->user = included->user;
245 included->user = NULL;
248 if (included->bus_type != NULL)
250 dbus_free (parser->bus_type);
251 parser->bus_type = included->bus_type;
252 included->bus_type = NULL;
258 if (included->pidfile != NULL)
260 dbus_free (parser->pidfile);
261 parser->pidfile = included->pidfile;
262 included->pidfile = NULL;
265 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
266 _dbus_list_append_link (&parser->listen_on, link);
268 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
269 _dbus_list_append_link (&parser->mechanisms, link);
271 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
272 _dbus_list_append_link (&parser->service_dirs, link);
278 bus_config_parser_new (const DBusString *basedir,
279 dbus_bool_t is_toplevel)
281 BusConfigParser *parser;
283 parser = dbus_new0 (BusConfigParser, 1);
287 parser->is_toplevel = !!is_toplevel;
289 if (!_dbus_string_init (&parser->basedir))
295 if (((parser->policy = bus_policy_new ()) == NULL) ||
296 !_dbus_string_copy (basedir, 0, &parser->basedir, 0))
299 bus_policy_unref (parser->policy);
301 _dbus_string_free (&parser->basedir);
306 /* Make up some numbers! woot! */
307 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
308 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
309 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
311 /* Making this long means the user has to wait longer for an error
312 * message if something screws up, but making it too short means
313 * they might see a false failure.
315 parser->limits.activation_timeout = 25000; /* 25 seconds */
317 /* Making this long risks making a DOS attack easier, but too short
318 * and legitimate auth will fail. If interactive auth (ask user for
319 * password) is allowed, then potentially it has to be quite long.
321 parser->limits.auth_timeout = 30000; /* 30 seconds */
323 parser->limits.max_incomplete_connections = 32;
324 parser->limits.max_connections_per_user = 128;
326 /* Note that max_completed_connections / max_connections_per_user
327 * is the number of users that would have to work together to
328 * DOS all the other users.
330 parser->limits.max_completed_connections = 1024;
332 parser->limits.max_pending_activations = 256;
333 parser->limits.max_services_per_connection = 256;
335 parser->refcount = 1;
341 bus_config_parser_ref (BusConfigParser *parser)
343 _dbus_assert (parser->refcount > 0);
345 parser->refcount += 1;
349 bus_config_parser_unref (BusConfigParser *parser)
351 _dbus_assert (parser->refcount > 0);
353 parser->refcount -= 1;
355 if (parser->refcount == 0)
357 while (parser->stack != NULL)
358 pop_element (parser);
360 dbus_free (parser->user);
361 dbus_free (parser->bus_type);
362 dbus_free (parser->pidfile);
364 _dbus_list_foreach (&parser->listen_on,
365 (DBusForeachFunction) dbus_free,
368 _dbus_list_clear (&parser->listen_on);
370 _dbus_list_foreach (&parser->service_dirs,
371 (DBusForeachFunction) dbus_free,
374 _dbus_list_clear (&parser->service_dirs);
376 _dbus_list_foreach (&parser->mechanisms,
377 (DBusForeachFunction) dbus_free,
380 _dbus_list_clear (&parser->mechanisms);
382 _dbus_string_free (&parser->basedir);
385 bus_policy_unref (parser->policy);
392 bus_config_parser_check_doctype (BusConfigParser *parser,
396 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
398 if (strcmp (doctype, "busconfig") != 0)
400 dbus_set_error (error,
402 "Configuration file has the wrong document type %s",
417 locate_attributes (BusConfigParser *parser,
418 const char *element_name,
419 const char **attribute_names,
420 const char **attribute_values,
422 const char *first_attribute_name,
423 const char **first_attribute_retloc,
431 LocateAttr attrs[MAX_ATTRS];
435 _dbus_assert (first_attribute_name != NULL);
436 _dbus_assert (first_attribute_retloc != NULL);
441 attrs[0].name = first_attribute_name;
442 attrs[0].retloc = first_attribute_retloc;
443 *first_attribute_retloc = NULL;
445 va_start (args, first_attribute_retloc);
447 name = va_arg (args, const char*);
448 retloc = va_arg (args, const char**);
452 _dbus_assert (retloc != NULL);
453 _dbus_assert (n_attrs < MAX_ATTRS);
455 attrs[n_attrs].name = name;
456 attrs[n_attrs].retloc = retloc;
460 name = va_arg (args, const char*);
461 retloc = va_arg (args, const char**);
470 while (attribute_names[i])
479 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
481 retloc = attrs[j].retloc;
485 dbus_set_error (error, DBUS_ERROR_FAILED,
486 "Attribute \"%s\" repeated twice on the same <%s> element",
487 attrs[j].name, element_name);
492 *retloc = attribute_values[i];
501 dbus_set_error (error, DBUS_ERROR_FAILED,
502 "Attribute \"%s\" is invalid on <%s> element in this context",
503 attribute_names[i], element_name);
516 check_no_attributes (BusConfigParser *parser,
517 const char *element_name,
518 const char **attribute_names,
519 const char **attribute_values,
522 if (attribute_names[0] != NULL)
524 dbus_set_error (error, DBUS_ERROR_FAILED,
525 "Attribute \"%s\" is invalid on <%s> element in this context",
526 attribute_names[0], element_name);
534 start_busconfig_child (BusConfigParser *parser,
535 const char *element_name,
536 const char **attribute_names,
537 const char **attribute_values,
540 if (strcmp (element_name, "user") == 0)
542 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
545 if (push_element (parser, ELEMENT_USER) == NULL)
553 else if (strcmp (element_name, "type") == 0)
555 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
558 if (push_element (parser, ELEMENT_TYPE) == NULL)
566 else if (strcmp (element_name, "fork") == 0)
568 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
571 if (push_element (parser, ELEMENT_FORK) == NULL)
581 else if (strcmp (element_name, "pidfile") == 0)
583 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
586 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
594 else if (strcmp (element_name, "listen") == 0)
596 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
599 if (push_element (parser, ELEMENT_LISTEN) == NULL)
607 else if (strcmp (element_name, "auth") == 0)
609 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
612 if (push_element (parser, ELEMENT_AUTH) == NULL)
620 else if (strcmp (element_name, "includedir") == 0)
622 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
625 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
633 else if (strcmp (element_name, "servicedir") == 0)
635 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
638 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
646 else if (strcmp (element_name, "include") == 0)
649 const char *ignore_missing;
651 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
657 e->d.include.ignore_missing = FALSE;
659 if (!locate_attributes (parser, "include",
663 "ignore_missing", &ignore_missing,
667 if (ignore_missing != NULL)
669 if (strcmp (ignore_missing, "yes") == 0)
670 e->d.include.ignore_missing = TRUE;
671 else if (strcmp (ignore_missing, "no") == 0)
672 e->d.include.ignore_missing = FALSE;
675 dbus_set_error (error, DBUS_ERROR_FAILED,
676 "ignore_missing attribute must have value \"yes\" or \"no\"");
683 else if (strcmp (element_name, "policy") == 0)
690 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
696 e->d.policy.type = POLICY_IGNORED;
698 if (!locate_attributes (parser, "policy",
708 if (((context && user) ||
709 (context && group)) ||
711 !(context || user || group))
713 dbus_set_error (error, DBUS_ERROR_FAILED,
714 "<policy> element must have exactly one of (context|user|group) attributes");
720 if (strcmp (context, "default") == 0)
722 e->d.policy.type = POLICY_DEFAULT;
724 else if (strcmp (context, "mandatory") == 0)
726 e->d.policy.type = POLICY_MANDATORY;
730 dbus_set_error (error, DBUS_ERROR_FAILED,
731 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
736 else if (user != NULL)
739 _dbus_string_init_const (&username, user);
741 if (_dbus_get_user_id (&username,
742 &e->d.policy.gid_or_uid))
743 e->d.policy.type = POLICY_USER;
745 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
748 else if (group != NULL)
750 DBusString group_name;
751 _dbus_string_init_const (&group_name, group);
753 if (_dbus_get_group_id (&group_name,
754 &e->d.policy.gid_or_uid))
755 e->d.policy.type = POLICY_GROUP;
757 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
762 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
767 else if (strcmp (element_name, "limit") == 0)
772 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
778 if (!locate_attributes (parser, "limit",
788 dbus_set_error (error, DBUS_ERROR_FAILED,
789 "<limit> element must have a \"name\" attribute");
793 e->d.limit.name = _dbus_strdup (name);
794 if (e->d.limit.name == NULL)
804 dbus_set_error (error, DBUS_ERROR_FAILED,
805 "Element <%s> not allowed inside <%s> in configuration file",
806 element_name, "busconfig");
812 message_type_from_string (const char *type_str)
814 if (strcmp (type_str, "method_call") == 0)
815 return DBUS_MESSAGE_TYPE_METHOD_CALL;
816 if (strcmp (type_str, "method_return") == 0)
817 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
818 else if (strcmp (type_str, "signal") == 0)
819 return DBUS_MESSAGE_TYPE_SIGNAL;
820 else if (strcmp (type_str, "error") == 0)
821 return DBUS_MESSAGE_TYPE_ERROR;
823 return DBUS_MESSAGE_TYPE_INVALID;
827 append_rule_from_element (BusConfigParser *parser,
828 const char *element_name,
829 const char **attribute_names,
830 const char **attribute_values,
834 const char *send_interface;
835 const char *send_member;
836 const char *send_error;
837 const char *send_service;
838 const char *send_path;
839 const char *send_type;
840 const char *receive_interface;
841 const char *receive_member;
842 const char *receive_error;
843 const char *receive_service;
844 const char *receive_path;
845 const char *receive_type;
851 if (!locate_attributes (parser, element_name,
855 "send_interface", &send_interface,
856 "send_member", &send_member,
857 "send_error", &send_error,
858 "send_service", &send_service,
859 "send_path", &send_path,
860 "send_type", &send_type,
861 "receive_interface", &receive_interface,
862 "receive_member", &receive_member,
863 "receive_error", &receive_error,
864 "receive_service", &receive_service,
865 "receive_path", &receive_path,
866 "receive_type", &receive_type,
873 if (!(send_interface || send_member || send_error || send_service ||
874 send_type || send_path ||
875 receive_interface || receive_member || receive_error || receive_service ||
876 receive_type || receive_path ||
877 own || user || group))
879 dbus_set_error (error, DBUS_ERROR_FAILED,
880 "Element <%s> must have one or more attributes",
885 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
886 (receive_member && (receive_interface == NULL && receive_path == NULL)))
888 dbus_set_error (error, DBUS_ERROR_FAILED,
889 "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.",
894 /* Allowed combinations of elements are:
896 * base, must be all send or all receive:
902 * base send_ can combine with send_service, send_path, send_type
903 * base receive_ with receive_service, receive_path, receive_type
905 * user, group, own must occur alone
908 if (((send_interface && send_error) ||
909 (send_interface && receive_interface) ||
910 (send_interface && receive_member) ||
911 (send_interface && receive_error) ||
912 (send_interface && receive_service) ||
913 (send_interface && own) ||
914 (send_interface && user) ||
915 (send_interface && group)) ||
917 ((send_member && send_error) ||
918 (send_member && receive_interface) ||
919 (send_member && receive_member) ||
920 (send_member && receive_error) ||
921 (send_member && receive_service) ||
922 (send_member && own) ||
923 (send_member && user) ||
924 (send_member && group)) ||
926 ((send_error && receive_interface) ||
927 (send_error && receive_member) ||
928 (send_error && receive_error) ||
929 (send_error && receive_service) ||
930 (send_error && own) ||
931 (send_error && user) ||
932 (send_error && group)) ||
934 ((send_service && receive_interface) ||
935 (send_service && receive_member) ||
936 (send_service && receive_error) ||
937 (send_service && receive_service) ||
938 (send_service && own) ||
939 (send_service && user) ||
940 (send_service && group)) ||
942 ((send_type && receive_interface) ||
943 (send_type && receive_member) ||
944 (send_type && receive_error) ||
945 (send_type && receive_service) ||
946 (send_type && own) ||
947 (send_type && user) ||
948 (send_type && group)) ||
950 ((send_path && receive_interface) ||
951 (send_path && receive_member) ||
952 (send_path && receive_error) ||
953 (send_path && receive_service) ||
954 (send_path && own) ||
955 (send_path && user) ||
956 (send_path && group)) ||
958 ((receive_interface && receive_error) ||
959 (receive_interface && own) ||
960 (receive_interface && user) ||
961 (receive_interface && group)) ||
963 ((receive_member && receive_error) ||
964 (receive_member && own) ||
965 (receive_member && user) ||
966 (receive_member && group)) ||
968 ((receive_error && own) ||
969 (receive_error && user) ||
970 (receive_error && group)) ||
977 dbus_set_error (error, DBUS_ERROR_FAILED,
978 "Invalid combination of attributes on element <%s>, "
979 "only send_foo/send_service or receive_foo/receive_service may be paired",
986 /* In BusPolicyRule, NULL represents wildcard.
987 * In the config file, '*' represents it.
989 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
991 if (send_interface || send_member || send_error || send_service ||
992 send_path || send_type)
996 if (IS_WILDCARD (send_interface))
997 send_interface = NULL;
998 if (IS_WILDCARD (send_member))
1000 if (IS_WILDCARD (send_error))
1002 if (IS_WILDCARD (send_service))
1003 send_service = NULL;
1004 if (IS_WILDCARD (send_path))
1006 if (IS_WILDCARD (send_type))
1009 message_type = DBUS_MESSAGE_TYPE_INVALID;
1010 if (send_type != NULL)
1012 message_type = message_type_from_string (send_type);
1013 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1015 dbus_set_error (error, DBUS_ERROR_FAILED,
1016 "Bad message type \"%s\"",
1022 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1026 rule->d.send.message_type = message_type;
1027 rule->d.send.path = _dbus_strdup (send_path);
1028 rule->d.send.interface = _dbus_strdup (send_interface);
1029 rule->d.send.member = _dbus_strdup (send_member);
1030 rule->d.send.error = _dbus_strdup (send_error);
1031 rule->d.send.destination = _dbus_strdup (send_service);
1032 if (send_path && rule->d.send.path == NULL)
1034 if (send_interface && rule->d.send.interface == NULL)
1036 if (send_member && rule->d.send.member == NULL)
1038 if (send_error && rule->d.send.error == NULL)
1040 if (send_service && rule->d.send.destination == NULL)
1043 else if (receive_interface || receive_member || receive_error || receive_service ||
1044 receive_path || receive_type)
1048 if (IS_WILDCARD (receive_interface))
1049 receive_interface = NULL;
1050 if (IS_WILDCARD (receive_member))
1051 receive_member = NULL;
1052 if (IS_WILDCARD (receive_error))
1053 receive_error = NULL;
1054 if (IS_WILDCARD (receive_service))
1055 receive_service = NULL;
1056 if (IS_WILDCARD (receive_path))
1057 receive_path = NULL;
1058 if (IS_WILDCARD (receive_type))
1059 receive_type = NULL;
1062 message_type = DBUS_MESSAGE_TYPE_INVALID;
1063 if (receive_type != NULL)
1065 message_type = message_type_from_string (receive_type);
1066 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1068 dbus_set_error (error, DBUS_ERROR_FAILED,
1069 "Bad message type \"%s\"",
1075 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1079 rule->d.receive.message_type = message_type;
1080 rule->d.receive.path = _dbus_strdup (receive_path);
1081 rule->d.receive.interface = _dbus_strdup (receive_interface);
1082 rule->d.receive.member = _dbus_strdup (receive_member);
1083 rule->d.receive.error = _dbus_strdup (receive_error);
1084 rule->d.receive.origin = _dbus_strdup (receive_service);
1085 if (receive_path && rule->d.receive.path == NULL)
1087 if (receive_interface && rule->d.receive.interface == NULL)
1089 if (receive_member && rule->d.receive.member == NULL)
1091 if (receive_error && rule->d.receive.error == NULL)
1093 if (receive_service && rule->d.receive.origin == NULL)
1098 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1102 if (IS_WILDCARD (own))
1105 rule->d.own.service_name = _dbus_strdup (own);
1106 if (own && rule->d.own.service_name == NULL)
1111 if (IS_WILDCARD (user))
1113 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1117 rule->d.user.uid = DBUS_UID_UNSET;
1121 DBusString username;
1124 _dbus_string_init_const (&username, user);
1126 if (_dbus_get_user_id (&username, &uid))
1128 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1132 rule->d.user.uid = uid;
1136 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1137 user, element_name);
1143 if (IS_WILDCARD (group))
1145 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1149 rule->d.group.gid = DBUS_GID_UNSET;
1153 DBusString groupname;
1156 _dbus_string_init_const (&groupname, group);
1158 if (_dbus_get_user_id (&groupname, &gid))
1160 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1164 rule->d.group.gid = gid;
1168 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1169 group, element_name);
1174 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1180 pe = peek_element (parser);
1181 _dbus_assert (pe != NULL);
1182 _dbus_assert (pe->type == ELEMENT_POLICY);
1184 switch (pe->d.policy.type)
1186 case POLICY_IGNORED:
1187 /* drop the rule on the floor */
1190 case POLICY_DEFAULT:
1191 if (!bus_policy_append_default_rule (parser->policy, rule))
1194 case POLICY_MANDATORY:
1195 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1199 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1201 dbus_set_error (error, DBUS_ERROR_FAILED,
1202 "<%s> rule cannot be per-user because it has bus-global semantics",
1207 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1212 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1214 dbus_set_error (error, DBUS_ERROR_FAILED,
1215 "<%s> rule cannot be per-group because it has bus-global semantics",
1220 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1226 bus_policy_rule_unref (rule);
1233 BUS_SET_OOM (error);
1236 bus_policy_rule_unref (rule);
1241 start_policy_child (BusConfigParser *parser,
1242 const char *element_name,
1243 const char **attribute_names,
1244 const char **attribute_values,
1247 if (strcmp (element_name, "allow") == 0)
1249 if (!append_rule_from_element (parser, element_name,
1250 attribute_names, attribute_values,
1254 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1256 BUS_SET_OOM (error);
1262 else if (strcmp (element_name, "deny") == 0)
1264 if (!append_rule_from_element (parser, element_name,
1265 attribute_names, attribute_values,
1269 if (push_element (parser, ELEMENT_DENY) == NULL)
1271 BUS_SET_OOM (error);
1279 dbus_set_error (error, DBUS_ERROR_FAILED,
1280 "Element <%s> not allowed inside <%s> in configuration file",
1281 element_name, "policy");
1287 bus_config_parser_start_element (BusConfigParser *parser,
1288 const char *element_name,
1289 const char **attribute_names,
1290 const char **attribute_values,
1295 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1297 /* printf ("START: %s\n", element_name); */
1299 t = top_element_type (parser);
1301 if (t == ELEMENT_NONE)
1303 if (strcmp (element_name, "busconfig") == 0)
1305 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1308 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1310 BUS_SET_OOM (error);
1318 dbus_set_error (error, DBUS_ERROR_FAILED,
1319 "Unknown element <%s> at root of configuration file",
1324 else if (t == ELEMENT_BUSCONFIG)
1326 return start_busconfig_child (parser, element_name,
1327 attribute_names, attribute_values,
1330 else if (t == ELEMENT_POLICY)
1332 return start_policy_child (parser, element_name,
1333 attribute_names, attribute_values,
1338 dbus_set_error (error, DBUS_ERROR_FAILED,
1339 "Element <%s> is not allowed in this context",
1346 set_limit (BusConfigParser *parser,
1351 dbus_bool_t must_be_positive;
1352 dbus_bool_t must_be_int;
1354 must_be_int = FALSE;
1355 must_be_positive = FALSE;
1357 if (strcmp (name, "max_incoming_bytes") == 0)
1359 must_be_positive = TRUE;
1360 parser->limits.max_incoming_bytes = value;
1362 else if (strcmp (name, "max_outgoing_bytes") == 0)
1364 must_be_positive = TRUE;
1365 parser->limits.max_outgoing_bytes = value;
1367 else if (strcmp (name, "max_message_size") == 0)
1369 must_be_positive = TRUE;
1370 parser->limits.max_message_size = value;
1372 else if (strcmp (name, "activation_timeout") == 0)
1374 must_be_positive = TRUE;
1376 parser->limits.activation_timeout = value;
1378 else if (strcmp (name, "auth_timeout") == 0)
1380 must_be_positive = TRUE;
1382 parser->limits.auth_timeout = value;
1384 else if (strcmp (name, "max_completed_connections") == 0)
1386 must_be_positive = TRUE;
1388 parser->limits.max_completed_connections = value;
1390 else if (strcmp (name, "max_incomplete_connections") == 0)
1392 must_be_positive = TRUE;
1394 parser->limits.max_incomplete_connections = value;
1396 else if (strcmp (name, "max_connections_per_user") == 0)
1398 must_be_positive = TRUE;
1400 parser->limits.max_connections_per_user = value;
1402 else if (strcmp (name, "max_pending_activations") == 0)
1404 must_be_positive = TRUE;
1406 parser->limits.max_pending_activations = value;
1408 else if (strcmp (name, "max_services_per_connection") == 0)
1410 must_be_positive = TRUE;
1412 parser->limits.max_services_per_connection = value;
1416 dbus_set_error (error, DBUS_ERROR_FAILED,
1417 "There is no limit called \"%s\"\n",
1422 if (must_be_positive && value < 0)
1424 dbus_set_error (error, DBUS_ERROR_FAILED,
1425 "<limit name=\"%s\"> must be a positive number\n",
1431 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1433 dbus_set_error (error, DBUS_ERROR_FAILED,
1434 "<limit name=\"%s\"> value is too large\n",
1443 bus_config_parser_end_element (BusConfigParser *parser,
1444 const char *element_name,
1451 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1453 /* printf ("END: %s\n", element_name); */
1455 t = top_element_type (parser);
1457 if (t == ELEMENT_NONE)
1459 /* should probably be an assertion failure but
1460 * being paranoid about XML parsers
1462 dbus_set_error (error, DBUS_ERROR_FAILED,
1463 "XML parser ended element with no element on the stack");
1467 n = element_type_to_name (t);
1468 _dbus_assert (n != NULL);
1469 if (strcmp (n, element_name) != 0)
1471 /* should probably be an assertion failure but
1472 * being paranoid about XML parsers
1474 dbus_set_error (error, DBUS_ERROR_FAILED,
1475 "XML element <%s> ended but topmost element on the stack was <%s>",
1480 e = peek_element (parser);
1481 _dbus_assert (e != NULL);
1486 _dbus_assert_not_reached ("element in stack has no type");
1489 case ELEMENT_INCLUDE:
1492 case ELEMENT_LISTEN:
1493 case ELEMENT_PIDFILE:
1495 case ELEMENT_SERVICEDIR:
1496 case ELEMENT_INCLUDEDIR:
1498 if (!e->had_content)
1500 dbus_set_error (error, DBUS_ERROR_FAILED,
1501 "XML element <%s> was expected to have content inside it",
1502 element_type_to_name (e->type));
1506 if (e->type == ELEMENT_LIMIT)
1508 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1514 case ELEMENT_BUSCONFIG:
1515 case ELEMENT_POLICY:
1522 pop_element (parser);
1528 all_whitespace (const DBusString *str)
1532 _dbus_string_skip_white (str, 0, &i);
1534 return i == _dbus_string_get_length (str);
1538 make_full_path (const DBusString *basedir,
1539 const DBusString *filename,
1540 DBusString *full_path)
1542 if (_dbus_path_is_absolute (filename))
1544 return _dbus_string_copy (filename, 0, full_path, 0);
1548 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1551 if (!_dbus_concat_dir_and_file (full_path, filename))
1559 include_file (BusConfigParser *parser,
1560 const DBusString *filename,
1561 dbus_bool_t ignore_missing,
1564 /* FIXME good test case for this would load each config file in the
1565 * test suite both alone, and as an include, and check
1566 * that the result is the same
1568 BusConfigParser *included;
1569 DBusError tmp_error;
1571 dbus_error_init (&tmp_error);
1572 included = bus_config_load (filename, FALSE, &tmp_error);
1573 if (included == NULL)
1575 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1577 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1580 dbus_error_free (&tmp_error);
1585 dbus_move_error (&tmp_error, error);
1591 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1593 if (!merge_included (parser, included, error))
1595 bus_config_parser_unref (included);
1599 bus_config_parser_unref (included);
1605 include_dir (BusConfigParser *parser,
1606 const DBusString *dirname,
1609 DBusString filename;
1611 DBusError tmp_error;
1614 if (!_dbus_string_init (&filename))
1616 BUS_SET_OOM (error);
1622 dir = _dbus_directory_open (dirname, error);
1627 dbus_error_init (&tmp_error);
1628 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1630 DBusString full_path;
1632 if (!_dbus_string_init (&full_path))
1634 BUS_SET_OOM (error);
1638 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1640 BUS_SET_OOM (error);
1641 _dbus_string_free (&full_path);
1645 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1647 BUS_SET_OOM (error);
1648 _dbus_string_free (&full_path);
1652 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1654 if (!include_file (parser, &full_path, TRUE, error))
1656 _dbus_string_free (&full_path);
1661 _dbus_string_free (&full_path);
1664 if (dbus_error_is_set (&tmp_error))
1666 dbus_move_error (&tmp_error, error);
1673 _dbus_string_free (&filename);
1676 _dbus_directory_close (dir);
1682 bus_config_parser_content (BusConfigParser *parser,
1683 const DBusString *content,
1688 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1694 _dbus_string_get_const_data (content, &c_str);
1696 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1700 e = peek_element (parser);
1703 dbus_set_error (error, DBUS_ERROR_FAILED,
1704 "Text content outside of any XML element in configuration file");
1707 else if (e->had_content)
1709 _dbus_assert_not_reached ("Element had multiple content blocks");
1713 switch (top_element_type (parser))
1716 _dbus_assert_not_reached ("element at top of stack has no type");
1719 case ELEMENT_BUSCONFIG:
1720 case ELEMENT_POLICY:
1724 if (all_whitespace (content))
1728 dbus_set_error (error, DBUS_ERROR_FAILED,
1729 "No text content expected inside XML element %s in configuration file",
1730 element_type_to_name (top_element_type (parser)));
1734 case ELEMENT_PIDFILE:
1738 e->had_content = TRUE;
1740 if (!_dbus_string_copy_data (content, &s))
1743 dbus_free (parser->pidfile);
1744 parser->pidfile = s;
1748 case ELEMENT_INCLUDE:
1750 DBusString full_path;
1752 e->had_content = TRUE;
1754 if (!_dbus_string_init (&full_path))
1757 if (!make_full_path (&parser->basedir, content, &full_path))
1759 _dbus_string_free (&full_path);
1763 if (!include_file (parser, &full_path,
1764 e->d.include.ignore_missing, error))
1766 _dbus_string_free (&full_path);
1770 _dbus_string_free (&full_path);
1774 case ELEMENT_INCLUDEDIR:
1776 DBusString full_path;
1778 e->had_content = TRUE;
1780 if (!_dbus_string_init (&full_path))
1783 if (!make_full_path (&parser->basedir, content, &full_path))
1785 _dbus_string_free (&full_path);
1789 if (!include_dir (parser, &full_path, error))
1791 _dbus_string_free (&full_path);
1795 _dbus_string_free (&full_path);
1803 e->had_content = TRUE;
1805 if (!_dbus_string_copy_data (content, &s))
1808 dbus_free (parser->user);
1817 e->had_content = TRUE;
1819 if (!_dbus_string_copy_data (content, &s))
1822 dbus_free (parser->bus_type);
1823 parser->bus_type = s;
1827 case ELEMENT_LISTEN:
1831 e->had_content = TRUE;
1833 if (!_dbus_string_copy_data (content, &s))
1836 if (!_dbus_list_append (&parser->listen_on,
1849 e->had_content = TRUE;
1851 if (!_dbus_string_copy_data (content, &s))
1854 if (!_dbus_list_append (&parser->mechanisms,
1863 case ELEMENT_SERVICEDIR:
1866 DBusString full_path;
1868 e->had_content = TRUE;
1870 if (!_dbus_string_init (&full_path))
1873 if (!make_full_path (&parser->basedir, content, &full_path))
1875 _dbus_string_free (&full_path);
1879 if (!_dbus_string_copy_data (&full_path, &s))
1881 _dbus_string_free (&full_path);
1885 if (!_dbus_list_append (&parser->service_dirs, s))
1887 _dbus_string_free (&full_path);
1892 _dbus_string_free (&full_path);
1900 e->had_content = TRUE;
1903 if (!_dbus_string_parse_int (content, 0, &val, NULL))
1905 dbus_set_error (error, DBUS_ERROR_FAILED,
1906 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
1911 e->d.limit.value = val;
1913 _dbus_verbose ("Loaded value %ld for limit %s\n",
1920 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1924 BUS_SET_OOM (error);
1929 bus_config_parser_finished (BusConfigParser *parser,
1932 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1934 if (parser->stack != NULL)
1936 dbus_set_error (error, DBUS_ERROR_FAILED,
1937 "Element <%s> was not closed in configuration file",
1938 element_type_to_name (top_element_type (parser)));
1943 if (parser->is_toplevel && parser->listen_on == NULL)
1945 dbus_set_error (error, DBUS_ERROR_FAILED,
1946 "Configuration file needs one or more <listen> elements giving addresses");
1954 bus_config_parser_get_user (BusConfigParser *parser)
1956 return parser->user;
1960 bus_config_parser_get_type (BusConfigParser *parser)
1962 return parser->bus_type;
1966 bus_config_parser_get_addresses (BusConfigParser *parser)
1968 return &parser->listen_on;
1972 bus_config_parser_get_mechanisms (BusConfigParser *parser)
1974 return &parser->mechanisms;
1978 bus_config_parser_get_service_dirs (BusConfigParser *parser)
1980 return &parser->service_dirs;
1984 bus_config_parser_get_fork (BusConfigParser *parser)
1986 return parser->fork;
1990 bus_config_parser_get_pidfile (BusConfigParser *parser)
1992 return parser->pidfile;
1996 bus_config_parser_steal_policy (BusConfigParser *parser)
2000 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2002 policy = parser->policy;
2004 parser->policy = NULL;
2009 /* Overwrite any limits that were set in the configuration file */
2011 bus_config_parser_get_limits (BusConfigParser *parser,
2014 *limits = parser->limits;
2017 #ifdef DBUS_BUILD_TESTS
2028 do_load (const DBusString *full_path,
2030 dbus_bool_t oom_possible)
2032 BusConfigParser *parser;
2035 dbus_error_init (&error);
2037 parser = bus_config_load (full_path, TRUE, &error);
2040 _DBUS_ASSERT_ERROR_IS_SET (&error);
2043 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2045 _dbus_verbose ("Failed to load valid file due to OOM\n");
2046 dbus_error_free (&error);
2049 else if (validity == VALID)
2051 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2054 dbus_error_free (&error);
2059 dbus_error_free (&error);
2065 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2067 bus_config_parser_unref (parser);
2069 if (validity == INVALID)
2071 _dbus_warn ("Accepted invalid file\n");
2081 const DBusString *full_path;
2086 check_loader_oom_func (void *data)
2088 LoaderOomData *d = data;
2090 return do_load (d->full_path, d->validity, TRUE);
2094 process_test_subdir (const DBusString *test_base_dir,
2098 DBusString test_directory;
2099 DBusString filename;
2107 if (!_dbus_string_init (&test_directory))
2108 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2110 _dbus_string_init_const (&filename, subdir);
2112 if (!_dbus_string_copy (test_base_dir, 0,
2113 &test_directory, 0))
2114 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2116 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2117 _dbus_assert_not_reached ("couldn't allocate full path");
2119 _dbus_string_free (&filename);
2120 if (!_dbus_string_init (&filename))
2121 _dbus_assert_not_reached ("didn't allocate filename string\n");
2123 dbus_error_init (&error);
2124 dir = _dbus_directory_open (&test_directory, &error);
2127 _dbus_warn ("Could not open %s: %s\n",
2128 _dbus_string_get_const_data (&test_directory),
2130 dbus_error_free (&error);
2134 printf ("Testing:\n");
2137 while (_dbus_directory_get_next_file (dir, &filename, &error))
2139 DBusString full_path;
2142 if (!_dbus_string_init (&full_path))
2143 _dbus_assert_not_reached ("couldn't init string");
2145 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2146 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2148 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2149 _dbus_assert_not_reached ("couldn't concat file to dir");
2151 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2153 _dbus_verbose ("Skipping non-.conf file %s\n",
2154 _dbus_string_get_const_data (&filename));
2155 _dbus_string_free (&full_path);
2159 printf (" %s\n", _dbus_string_get_const_data (&filename));
2161 _dbus_verbose (" expecting %s\n",
2162 validity == VALID ? "valid" :
2163 (validity == INVALID ? "invalid" :
2164 (validity == UNKNOWN ? "unknown" : "???")));
2166 d.full_path = &full_path;
2167 d.validity = validity;
2168 if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d))
2169 _dbus_assert_not_reached ("test failed");
2171 _dbus_string_free (&full_path);
2174 if (dbus_error_is_set (&error))
2176 _dbus_warn ("Could not get next file in %s: %s\n",
2177 _dbus_string_get_const_data (&test_directory),
2179 dbus_error_free (&error);
2188 _dbus_directory_close (dir);
2189 _dbus_string_free (&test_directory);
2190 _dbus_string_free (&filename);
2196 bus_config_parser_test (const DBusString *test_data_dir)
2198 if (test_data_dir == NULL ||
2199 _dbus_string_get_length (test_data_dir) == 0)
2201 printf ("No test data\n");
2205 if (!process_test_subdir (test_data_dir, "valid-config-files", VALID))
2211 #endif /* DBUS_BUILD_TESTS */