1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* policy.c Bus security policy
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
28 #include <dbus/dbus-list.h>
29 #include <dbus/dbus-hash.h>
30 #include <dbus/dbus-internals.h>
33 bus_policy_rule_new (BusPolicyRuleType type,
38 rule = dbus_new0 (BusPolicyRule, 1);
48 case BUS_POLICY_RULE_USER:
49 rule->d.user.uid = DBUS_UID_UNSET;
51 case BUS_POLICY_RULE_GROUP:
52 rule->d.group.gid = DBUS_GID_UNSET;
54 case BUS_POLICY_RULE_SEND:
55 rule->d.send.message_type = DBUS_MESSAGE_TYPE_INVALID;
57 case BUS_POLICY_RULE_RECEIVE:
58 rule->d.receive.message_type = DBUS_MESSAGE_TYPE_INVALID;
59 /* allow rules default to TRUE (only requested replies allowed)
60 * deny rules default to FALSE (only unrequested replies denied)
62 rule->d.receive.requested_reply = rule->allow;
64 case BUS_POLICY_RULE_OWN:
72 bus_policy_rule_ref (BusPolicyRule *rule)
74 _dbus_assert (rule->refcount > 0);
82 bus_policy_rule_unref (BusPolicyRule *rule)
84 _dbus_assert (rule->refcount > 0);
88 if (rule->refcount == 0)
92 case BUS_POLICY_RULE_SEND:
93 dbus_free (rule->d.send.path);
94 dbus_free (rule->d.send.interface);
95 dbus_free (rule->d.send.member);
96 dbus_free (rule->d.send.error);
97 dbus_free (rule->d.send.destination);
99 case BUS_POLICY_RULE_RECEIVE:
100 dbus_free (rule->d.receive.path);
101 dbus_free (rule->d.receive.interface);
102 dbus_free (rule->d.receive.member);
103 dbus_free (rule->d.receive.error);
104 dbus_free (rule->d.receive.origin);
106 case BUS_POLICY_RULE_OWN:
107 dbus_free (rule->d.own.service_name);
109 case BUS_POLICY_RULE_USER:
111 case BUS_POLICY_RULE_GROUP:
123 DBusList *default_rules; /**< Default policy rules */
124 DBusList *mandatory_rules; /**< Mandatory policy rules */
125 DBusHashTable *rules_by_uid; /**< per-UID policy rules */
126 DBusHashTable *rules_by_gid; /**< per-GID policy rules */
130 free_rule_func (void *data,
133 BusPolicyRule *rule = data;
135 bus_policy_rule_unref (rule);
139 free_rule_list_func (void *data)
141 DBusList **list = data;
143 if (list == NULL) /* DBusHashTable is on crack */
146 _dbus_list_foreach (list, free_rule_func, NULL);
148 _dbus_list_clear (list);
154 bus_policy_new (void)
158 policy = dbus_new0 (BusPolicy, 1);
162 policy->refcount = 1;
164 policy->rules_by_uid = _dbus_hash_table_new (DBUS_HASH_ULONG,
166 free_rule_list_func);
167 if (policy->rules_by_uid == NULL)
170 policy->rules_by_gid = _dbus_hash_table_new (DBUS_HASH_ULONG,
172 free_rule_list_func);
173 if (policy->rules_by_gid == NULL)
179 bus_policy_unref (policy);
184 bus_policy_ref (BusPolicy *policy)
186 _dbus_assert (policy->refcount > 0);
188 policy->refcount += 1;
194 bus_policy_unref (BusPolicy *policy)
196 _dbus_assert (policy->refcount > 0);
198 policy->refcount -= 1;
200 if (policy->refcount == 0)
202 _dbus_list_foreach (&policy->default_rules, free_rule_func, NULL);
203 _dbus_list_clear (&policy->default_rules);
205 _dbus_list_foreach (&policy->mandatory_rules, free_rule_func, NULL);
206 _dbus_list_clear (&policy->mandatory_rules);
208 if (policy->rules_by_uid)
210 _dbus_hash_table_unref (policy->rules_by_uid);
211 policy->rules_by_uid = NULL;
214 if (policy->rules_by_gid)
216 _dbus_hash_table_unref (policy->rules_by_gid);
217 policy->rules_by_gid = NULL;
225 add_list_to_client (DBusList **list,
226 BusClientPolicy *client)
230 link = _dbus_list_get_first_link (list);
233 BusPolicyRule *rule = link->data;
234 link = _dbus_list_get_next_link (list, link);
238 case BUS_POLICY_RULE_USER:
239 case BUS_POLICY_RULE_GROUP:
240 /* These aren't per-connection policies */
243 case BUS_POLICY_RULE_OWN:
244 case BUS_POLICY_RULE_SEND:
245 case BUS_POLICY_RULE_RECEIVE:
246 /* These are per-connection */
247 if (!bus_client_policy_append_rule (client, rule))
257 bus_policy_create_client_policy (BusPolicy *policy,
258 DBusConnection *connection,
261 BusClientPolicy *client;
264 _dbus_assert (dbus_connection_get_is_authenticated (connection));
265 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
267 client = bus_client_policy_new ();
271 if (!add_list_to_client (&policy->default_rules,
275 /* we avoid the overhead of looking up user's groups
276 * if we don't have any group rules anyway
278 if (_dbus_hash_table_get_n_entries (policy->rules_by_gid) > 0)
280 unsigned long *groups;
284 if (!bus_connection_get_groups (connection, &groups, &n_groups, error))
292 list = _dbus_hash_table_lookup_ulong (policy->rules_by_gid,
297 if (!add_list_to_client (list, client))
310 if (!dbus_connection_get_unix_user (connection, &uid))
312 dbus_set_error (error, DBUS_ERROR_FAILED,
313 "No user ID known for connection, cannot determine security policy\n");
317 if (_dbus_hash_table_get_n_entries (policy->rules_by_uid) > 0)
321 list = _dbus_hash_table_lookup_ulong (policy->rules_by_uid,
326 if (!add_list_to_client (list, client))
331 if (!add_list_to_client (&policy->mandatory_rules,
335 bus_client_policy_optimize (client);
342 _DBUS_ASSERT_ERROR_IS_SET (error);
344 bus_client_policy_unref (client);
349 list_allows_user (dbus_bool_t def,
352 const unsigned long *group_ids,
360 link = _dbus_list_get_first_link (list);
363 BusPolicyRule *rule = link->data;
364 link = _dbus_list_get_next_link (list, link);
366 if (rule->type == BUS_POLICY_RULE_USER)
368 _dbus_verbose ("List %p user rule uid="DBUS_UID_FORMAT"\n",
369 list, rule->d.user.uid);
371 if (rule->d.user.uid == DBUS_UID_UNSET)
373 else if (rule->d.user.uid != uid)
376 else if (rule->type == BUS_POLICY_RULE_GROUP)
378 _dbus_verbose ("List %p group rule uid="DBUS_UID_FORMAT"\n",
379 list, rule->d.user.uid);
381 if (rule->d.group.gid == DBUS_GID_UNSET)
388 while (i < n_group_ids)
390 if (rule->d.group.gid == group_ids[i])
395 if (i == n_group_ids)
402 allowed = rule->allow;
409 bus_policy_allow_user (BusPolicy *policy,
410 DBusUserDatabase *user_database,
414 unsigned long *group_ids;
417 /* On OOM or error we always reject the user */
418 if (!_dbus_user_database_get_groups (user_database,
419 uid, &group_ids, &n_group_ids, NULL))
421 _dbus_verbose ("Did not get any groups for UID %lu\n",
428 allowed = list_allows_user (allowed,
429 &policy->default_rules,
431 group_ids, n_group_ids);
433 allowed = list_allows_user (allowed,
434 &policy->mandatory_rules,
436 group_ids, n_group_ids);
438 dbus_free (group_ids);
440 _dbus_verbose ("UID %lu allowed = %d\n", uid, allowed);
446 bus_policy_append_default_rule (BusPolicy *policy,
449 if (!_dbus_list_append (&policy->default_rules, rule))
452 bus_policy_rule_ref (rule);
458 bus_policy_append_mandatory_rule (BusPolicy *policy,
461 if (!_dbus_list_append (&policy->mandatory_rules, rule))
464 bus_policy_rule_ref (rule);
470 get_list (DBusHashTable *hash,
475 list = _dbus_hash_table_lookup_ulong (hash, key);
479 list = dbus_new0 (DBusList*, 1);
483 if (!_dbus_hash_table_insert_ulong (hash, key, list))
494 bus_policy_append_user_rule (BusPolicy *policy,
500 list = get_list (policy->rules_by_uid, uid);
505 if (!_dbus_list_append (list, rule))
508 bus_policy_rule_ref (rule);
514 bus_policy_append_group_rule (BusPolicy *policy,
520 list = get_list (policy->rules_by_gid, gid);
525 if (!_dbus_list_append (list, rule))
528 bus_policy_rule_ref (rule);
534 append_copy_of_policy_list (DBusList **list,
535 DBusList **to_append)
542 /* Preallocate all our links */
543 link = _dbus_list_get_first_link (to_append);
546 if (!_dbus_list_append (&tmp_list, link->data))
548 _dbus_list_clear (&tmp_list);
552 link = _dbus_list_get_next_link (to_append, link);
555 /* Now append them */
556 while ((link = _dbus_list_pop_first_link (&tmp_list)))
558 bus_policy_rule_ref (link->data);
559 _dbus_list_append_link (list, link);
566 merge_id_hash (DBusHashTable *dest,
567 DBusHashTable *to_absorb)
571 _dbus_hash_iter_init (to_absorb, &iter);
572 while (_dbus_hash_iter_next (&iter))
574 unsigned long id = _dbus_hash_iter_get_ulong_key (&iter);
575 DBusList **list = _dbus_hash_iter_get_value (&iter);
576 DBusList **target = get_list (dest, id);
581 if (!append_copy_of_policy_list (target, list))
589 bus_policy_merge (BusPolicy *policy,
590 BusPolicy *to_absorb)
592 /* Not properly atomic, but as used for configuration files
593 * we don't rely on it.
595 if (!append_copy_of_policy_list (&policy->default_rules,
596 &to_absorb->default_rules))
599 if (!append_copy_of_policy_list (&policy->mandatory_rules,
600 &to_absorb->mandatory_rules))
603 if (!merge_id_hash (policy->rules_by_uid,
604 to_absorb->rules_by_uid))
607 if (!merge_id_hash (policy->rules_by_gid,
608 to_absorb->rules_by_gid))
614 struct BusClientPolicy
622 bus_client_policy_new (void)
624 BusClientPolicy *policy;
626 policy = dbus_new0 (BusClientPolicy, 1);
630 policy->refcount = 1;
636 bus_client_policy_ref (BusClientPolicy *policy)
638 _dbus_assert (policy->refcount > 0);
640 policy->refcount += 1;
646 rule_unref_foreach (void *data,
649 BusPolicyRule *rule = data;
651 bus_policy_rule_unref (rule);
655 bus_client_policy_unref (BusClientPolicy *policy)
657 _dbus_assert (policy->refcount > 0);
659 policy->refcount -= 1;
661 if (policy->refcount == 0)
663 _dbus_list_foreach (&policy->rules,
667 _dbus_list_clear (&policy->rules);
674 remove_rules_by_type_up_to (BusClientPolicy *policy,
675 BusPolicyRuleType type,
680 link = _dbus_list_get_first_link (&policy->rules);
681 while (link != up_to)
683 BusPolicyRule *rule = link->data;
684 DBusList *next = _dbus_list_get_next_link (&policy->rules, link);
686 if (rule->type == type)
688 _dbus_list_remove_link (&policy->rules, link);
689 bus_policy_rule_unref (rule);
697 bus_client_policy_optimize (BusClientPolicy *policy)
701 /* The idea here is that if we have:
703 * <allow send_interface="foo.bar"/>
704 * <deny send_interface="*"/>
706 * (for example) the deny will always override the allow. So we
707 * delete the allow. Ditto for deny followed by allow, etc. This is
708 * a dumb thing to put in a config file, but the <include> feature
709 * of files allows for an "inheritance and override" pattern where
710 * it could make sense. If an included file wants to "start over"
711 * with a blanket deny, no point keeping the rules from the parent
715 _dbus_verbose ("Optimizing policy with %d rules\n",
716 _dbus_list_get_length (&policy->rules));
718 link = _dbus_list_get_first_link (&policy->rules);
723 dbus_bool_t remove_preceding;
725 next = _dbus_list_get_next_link (&policy->rules, link);
728 remove_preceding = FALSE;
730 _dbus_assert (rule != NULL);
734 case BUS_POLICY_RULE_SEND:
736 rule->d.send.message_type == DBUS_MESSAGE_TYPE_INVALID &&
737 rule->d.send.path == NULL &&
738 rule->d.send.interface == NULL &&
739 rule->d.send.member == NULL &&
740 rule->d.send.error == NULL &&
741 rule->d.send.destination == NULL;
743 case BUS_POLICY_RULE_RECEIVE:
745 rule->d.receive.message_type == DBUS_MESSAGE_TYPE_INVALID &&
746 rule->d.receive.path == NULL &&
747 rule->d.receive.interface == NULL &&
748 rule->d.receive.member == NULL &&
749 rule->d.receive.error == NULL &&
750 rule->d.receive.origin == NULL;
752 case BUS_POLICY_RULE_OWN:
754 rule->d.own.service_name == NULL;
756 case BUS_POLICY_RULE_USER:
757 case BUS_POLICY_RULE_GROUP:
758 _dbus_assert_not_reached ("invalid rule");
762 if (remove_preceding)
763 remove_rules_by_type_up_to (policy, rule->type,
769 _dbus_verbose ("After optimization, policy has %d rules\n",
770 _dbus_list_get_length (&policy->rules));
774 bus_client_policy_append_rule (BusClientPolicy *policy,
777 _dbus_verbose ("Appending rule %p with type %d to policy %p\n",
778 rule, rule->type, policy);
780 if (!_dbus_list_append (&policy->rules, rule))
783 bus_policy_rule_ref (rule);
789 bus_client_policy_check_can_send (BusClientPolicy *policy,
790 BusRegistry *registry,
791 DBusConnection *receiver,
792 DBusMessage *message)
797 /* policy->rules is in the order the rules appeared
798 * in the config file, i.e. last rule that applies wins
801 _dbus_verbose (" (policy) checking send rules\n");
804 link = _dbus_list_get_first_link (&policy->rules);
807 BusPolicyRule *rule = link->data;
809 link = _dbus_list_get_next_link (&policy->rules, link);
811 /* Rule is skipped if it specifies a different
812 * message name from the message, or a different
813 * destination from the message
816 if (rule->type != BUS_POLICY_RULE_SEND)
818 _dbus_verbose (" (policy) skipping non-send rule\n");
822 if (rule->d.send.message_type != DBUS_MESSAGE_TYPE_INVALID)
824 if (dbus_message_get_type (message) != rule->d.send.message_type)
826 _dbus_verbose (" (policy) skipping rule for different message type\n");
831 if (rule->d.send.path != NULL)
833 if (dbus_message_get_path (message) != NULL &&
834 strcmp (dbus_message_get_path (message),
835 rule->d.send.path) != 0)
837 _dbus_verbose (" (policy) skipping rule for different path\n");
842 if (rule->d.send.interface != NULL)
844 if (dbus_message_get_interface (message) != NULL &&
845 strcmp (dbus_message_get_interface (message),
846 rule->d.send.interface) != 0)
848 _dbus_verbose (" (policy) skipping rule for different interface\n");
853 if (rule->d.send.member != NULL)
855 if (dbus_message_get_member (message) != NULL &&
856 strcmp (dbus_message_get_member (message),
857 rule->d.send.member) != 0)
859 _dbus_verbose (" (policy) skipping rule for different member\n");
864 if (rule->d.send.error != NULL)
866 if (dbus_message_get_error_name (message) != NULL &&
867 strcmp (dbus_message_get_error_name (message),
868 rule->d.send.error) != 0)
870 _dbus_verbose (" (policy) skipping rule for different error name\n");
875 if (rule->d.send.destination != NULL)
877 /* receiver can be NULL for messages that are sent to the
878 * message bus itself, we check the strings in that case as
879 * built-in services don't have a DBusConnection but messages
880 * to them have a destination service name.
882 if (receiver == NULL)
884 if (!dbus_message_has_destination (message,
885 rule->d.send.destination))
887 _dbus_verbose (" (policy) skipping rule because message dest is not %s\n",
888 rule->d.send.destination);
897 _dbus_string_init_const (&str, rule->d.send.destination);
899 service = bus_registry_lookup (registry, &str);
902 _dbus_verbose (" (policy) skipping rule because dest %s doesn't exist\n",
903 rule->d.send.destination);
907 if (!bus_service_has_owner (service, receiver))
909 _dbus_verbose (" (policy) skipping rule because dest %s isn't owned by receiver\n",
910 rule->d.send.destination);
917 allowed = rule->allow;
919 _dbus_verbose (" (policy) used rule, allow now = %d\n",
926 /* See docs on what the args mean on bus_context_check_security_policy()
930 bus_client_policy_check_can_receive (BusClientPolicy *policy,
931 BusRegistry *registry,
932 dbus_bool_t requested_reply,
933 DBusConnection *sender,
934 DBusConnection *addressed_recipient,
935 DBusConnection *proposed_recipient,
936 DBusMessage *message)
940 dbus_bool_t eavesdropping;
943 addressed_recipient != proposed_recipient &&
944 dbus_message_get_destination (message) != NULL;
946 /* policy->rules is in the order the rules appeared
947 * in the config file, i.e. last rule that applies wins
950 _dbus_verbose (" (policy) checking receive rules, eavesdropping = %d\n", eavesdropping);
953 link = _dbus_list_get_first_link (&policy->rules);
956 BusPolicyRule *rule = link->data;
958 link = _dbus_list_get_next_link (&policy->rules, link);
960 if (rule->type != BUS_POLICY_RULE_RECEIVE)
962 _dbus_verbose (" (policy) skipping non-receive rule\n");
966 if (rule->d.receive.message_type != DBUS_MESSAGE_TYPE_INVALID)
968 if (dbus_message_get_type (message) != rule->d.receive.message_type)
970 _dbus_verbose (" (policy) skipping rule for different message type\n");
975 /* for allow, eavesdrop=false means the rule doesn't apply when
976 * eavesdropping. eavesdrop=true means always allow.
978 if (eavesdropping && rule->allow && !rule->d.receive.eavesdrop)
980 _dbus_verbose (" (policy) skipping allow rule since it doesn't apply to eavesdropping\n");
984 /* for deny, eavesdrop=true means the rule applies only when
985 * eavesdropping; eavesdrop=false means always deny.
987 if (!eavesdropping && !rule->allow && rule->d.receive.eavesdrop)
989 _dbus_verbose (" (policy) skipping deny rule since it only applies to eavesdropping\n");
993 /* If it's a reply, the requested_reply flag kicks in */
994 if (dbus_message_get_reply_serial (message) != 0)
996 /* for allow, requested_reply=true means the rule applies
997 * only when reply was requested. requested_reply=false means
1000 if (!requested_reply && rule->allow && rule->d.receive.requested_reply)
1002 _dbus_verbose (" (policy) skipping allow rule since it only applies to requested replies\n");
1006 /* for deny, requested_reply=false means the rule applies only
1007 * when the reply was not requested. requested_reply=true means the
1008 * rule always applies.
1010 if (requested_reply && !rule->allow && !rule->d.receive.requested_reply)
1012 _dbus_verbose (" (policy) skipping deny rule since it only applies to unrequested replies\n");
1017 if (rule->d.receive.path != NULL)
1019 if (dbus_message_get_path (message) != NULL &&
1020 strcmp (dbus_message_get_path (message),
1021 rule->d.receive.path) != 0)
1023 _dbus_verbose (" (policy) skipping rule for different path\n");
1028 if (rule->d.receive.interface != NULL)
1030 if (dbus_message_get_interface (message) != NULL &&
1031 strcmp (dbus_message_get_interface (message),
1032 rule->d.receive.interface) != 0)
1034 _dbus_verbose (" (policy) skipping rule for different interface\n");
1039 if (rule->d.receive.member != NULL)
1041 if (dbus_message_get_member (message) != NULL &&
1042 strcmp (dbus_message_get_member (message),
1043 rule->d.receive.member) != 0)
1045 _dbus_verbose (" (policy) skipping rule for different member\n");
1050 if (rule->d.receive.error != NULL)
1052 if (dbus_message_get_error_name (message) != NULL &&
1053 strcmp (dbus_message_get_error_name (message),
1054 rule->d.receive.error) != 0)
1056 _dbus_verbose (" (policy) skipping rule for different error name\n");
1061 if (rule->d.receive.origin != NULL)
1063 /* sender can be NULL for messages that originate from the
1064 * message bus itself, we check the strings in that case as
1065 * built-in services don't have a DBusConnection but will
1066 * still set the sender on their messages.
1070 if (!dbus_message_has_sender (message,
1071 rule->d.receive.origin))
1073 _dbus_verbose (" (policy) skipping rule because message sender is not %s\n",
1074 rule->d.receive.origin);
1080 BusService *service;
1083 _dbus_string_init_const (&str, rule->d.receive.origin);
1085 service = bus_registry_lookup (registry, &str);
1087 if (service == NULL)
1089 _dbus_verbose (" (policy) skipping rule because origin %s doesn't exist\n",
1090 rule->d.receive.origin);
1094 if (!bus_service_has_owner (service, sender))
1096 _dbus_verbose (" (policy) skipping rule because origin %s isn't owned by sender\n",
1097 rule->d.receive.origin);
1104 allowed = rule->allow;
1106 _dbus_verbose (" (policy) used rule, allow now = %d\n",
1114 bus_client_policy_check_can_own (BusClientPolicy *policy,
1115 DBusConnection *connection,
1116 const DBusString *service_name)
1119 dbus_bool_t allowed;
1121 /* policy->rules is in the order the rules appeared
1122 * in the config file, i.e. last rule that applies wins
1126 link = _dbus_list_get_first_link (&policy->rules);
1127 while (link != NULL)
1129 BusPolicyRule *rule = link->data;
1131 link = _dbus_list_get_next_link (&policy->rules, link);
1133 /* Rule is skipped if it specifies a different service name from
1137 if (rule->type != BUS_POLICY_RULE_OWN)
1140 if (rule->d.own.service_name != NULL)
1142 if (!_dbus_string_equal_c_str (service_name,
1143 rule->d.own.service_name))
1148 allowed = rule->allow;
1154 #ifdef DBUS_BUILD_TESTS
1157 bus_policy_test (const DBusString *test_data_dir)
1159 /* This doesn't do anything for now because I decided to do it in
1160 * dispatch.c instead by having some of the clients in dispatch.c
1161 * have particular policies applied to them.
1167 #endif /* DBUS_BUILD_TESTS */