bus/policy: reduce number of params in check_ functions
[platform/upstream/dbus.git] / bus / policy.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* policy.c  Bus security policy
3  *
4  * Copyright (C) 2003, 2004  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
24 #include <config.h>
25 #include "check.h"
26 #include "policy.h"
27 #include "services.h"
28 #include "test.h"
29 #include "utils.h"
30 #include <dbus/dbus-list.h>
31 #include <dbus/dbus-hash.h>
32 #include <dbus/dbus-internals.h>
33 #include <dbus/dbus-message-internal.h>
34
35 static dbus_bool_t bus_client_policy_append_rule (BusClientPolicy *policy,
36                                                   BusPolicyRule *rule);
37
38 BusPolicyRule*
39 bus_policy_rule_new (BusPolicyRuleType type,
40                      BusPolicyRuleAccess access)
41 {
42   BusPolicyRule *rule;
43
44   rule = dbus_new0 (BusPolicyRule, 1);
45   if (rule == NULL)
46     return NULL;
47
48   rule->type = type;
49   rule->refcount = 1;
50   rule->access = access;
51
52   switch (rule->type)
53     {
54     case BUS_POLICY_RULE_USER:
55       rule->d.user.uid = DBUS_UID_UNSET;
56       break;
57     case BUS_POLICY_RULE_GROUP:
58       rule->d.group.gid = DBUS_GID_UNSET;
59       break;
60     case BUS_POLICY_RULE_SEND:
61       rule->d.send.message_type = DBUS_MESSAGE_TYPE_INVALID;
62       /* allow rules default to TRUE (only requested replies allowed)
63        * check rules default to TRUE (only requested replies are checked)
64        * deny rules default to FALSE (only unrequested replies denied)
65        */
66       rule->d.send.requested_reply = rule->access != BUS_POLICY_RULE_ACCESS_DENY;
67       break;
68     case BUS_POLICY_RULE_RECEIVE:
69       rule->d.receive.message_type = DBUS_MESSAGE_TYPE_INVALID;
70       /* allow rules default to TRUE (only requested replies allowed)
71        * check rules default to TRUE (only requested replies are checked)
72        * deny rules default to FALSE (only unrequested replies denied)
73        */
74       rule->d.receive.requested_reply = rule->access != BUS_POLICY_RULE_ACCESS_DENY;
75       break;
76     case BUS_POLICY_RULE_OWN:
77       break;
78     default:
79       _dbus_assert_not_reached ("invalid rule");
80     }
81   
82   return rule;
83 }
84
85 BusPolicyRule *
86 bus_policy_rule_ref (BusPolicyRule *rule)
87 {
88   _dbus_assert (rule->refcount > 0);
89
90   rule->refcount += 1;
91
92   return rule;
93 }
94
95 void
96 bus_policy_rule_unref (BusPolicyRule *rule)
97 {
98   _dbus_assert (rule->refcount > 0);
99
100   rule->refcount -= 1;
101   
102   if (rule->refcount == 0)
103     {
104       switch (rule->type)
105         {
106         case BUS_POLICY_RULE_SEND:
107           dbus_free (rule->d.send.path);
108           dbus_free (rule->d.send.interface);
109           dbus_free (rule->d.send.member);
110           dbus_free (rule->d.send.error);
111           dbus_free (rule->d.send.destination);
112           break;
113         case BUS_POLICY_RULE_RECEIVE:
114           dbus_free (rule->d.receive.path);
115           dbus_free (rule->d.receive.interface);
116           dbus_free (rule->d.receive.member);
117           dbus_free (rule->d.receive.error);
118           dbus_free (rule->d.receive.origin);
119           break;
120         case BUS_POLICY_RULE_OWN:
121           dbus_free (rule->d.own.service_name);
122           break;
123         case BUS_POLICY_RULE_USER:
124           break;
125         case BUS_POLICY_RULE_GROUP:
126           break;
127         default:
128           _dbus_assert_not_reached ("invalid rule");
129         }
130
131       dbus_free (rule->privilege);
132       dbus_free (rule);
133     }
134 }
135
136 struct BusPolicy
137 {
138   int refcount;
139
140   DBusList *default_rules;         /**< Default policy rules */
141   DBusList *mandatory_rules;       /**< Mandatory policy rules */
142   DBusHashTable *rules_by_uid;     /**< per-UID policy rules */
143   DBusHashTable *rules_by_gid;     /**< per-GID policy rules */
144   DBusList *at_console_true_rules; /**< console user policy rules where at_console="true"*/
145   DBusList *at_console_false_rules; /**< console user policy rules where at_console="false"*/
146 };
147
148 static void
149 free_rule_func (void *data,
150                 void *user_data)
151 {
152   BusPolicyRule *rule = data;
153
154   bus_policy_rule_unref (rule);
155 }
156
157 static void
158 free_rule_list_func (void *data)
159 {
160   DBusList **list = data;
161
162   if (list == NULL) /* DBusHashTable is on crack */
163     return;
164   
165   _dbus_list_foreach (list, free_rule_func, NULL);
166   
167   _dbus_list_clear (list);
168
169   dbus_free (list);
170 }
171
172 BusPolicy*
173 bus_policy_new (void)
174 {
175   BusPolicy *policy;
176
177   policy = dbus_new0 (BusPolicy, 1);
178   if (policy == NULL)
179     return NULL;
180
181   policy->refcount = 1;
182   
183   policy->rules_by_uid = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
184                                                NULL,
185                                                free_rule_list_func);
186   if (policy->rules_by_uid == NULL)
187     goto failed;
188
189   policy->rules_by_gid = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
190                                                NULL,
191                                                free_rule_list_func);
192   if (policy->rules_by_gid == NULL)
193     goto failed;
194
195   return policy;
196   
197  failed:
198   bus_policy_unref (policy);
199   return NULL;
200 }
201
202 BusPolicy *
203 bus_policy_ref (BusPolicy *policy)
204 {
205   _dbus_assert (policy->refcount > 0);
206
207   policy->refcount += 1;
208
209   return policy;
210 }
211
212 void
213 bus_policy_unref (BusPolicy *policy)
214 {
215   _dbus_assert (policy->refcount > 0);
216
217   policy->refcount -= 1;
218
219   if (policy->refcount == 0)
220     {
221       _dbus_list_foreach (&policy->default_rules, free_rule_func, NULL);
222       _dbus_list_clear (&policy->default_rules);
223
224       _dbus_list_foreach (&policy->mandatory_rules, free_rule_func, NULL);
225       _dbus_list_clear (&policy->mandatory_rules);
226
227       _dbus_list_foreach (&policy->at_console_true_rules, free_rule_func, NULL);
228       _dbus_list_clear (&policy->at_console_true_rules);
229
230       _dbus_list_foreach (&policy->at_console_false_rules, free_rule_func, NULL);
231       _dbus_list_clear (&policy->at_console_false_rules);
232
233       if (policy->rules_by_uid)
234         {
235           _dbus_hash_table_unref (policy->rules_by_uid);
236           policy->rules_by_uid = NULL;
237         }
238
239       if (policy->rules_by_gid)
240         {
241           _dbus_hash_table_unref (policy->rules_by_gid);
242           policy->rules_by_gid = NULL;
243         }
244
245       dbus_free (policy);
246     }
247 }
248
249 static dbus_bool_t
250 add_list_to_client (DBusList        **list,
251                     BusClientPolicy  *client)
252 {
253   DBusList *link;
254
255   link = _dbus_list_get_first_link (list);
256   while (link != NULL)
257     {
258       BusPolicyRule *rule = link->data;
259       link = _dbus_list_get_next_link (list, link);
260
261       switch (rule->type)
262         {
263         case BUS_POLICY_RULE_USER:
264         case BUS_POLICY_RULE_GROUP:
265           /* These aren't per-connection policies */
266           break;
267
268         case BUS_POLICY_RULE_OWN:
269         case BUS_POLICY_RULE_SEND:
270         case BUS_POLICY_RULE_RECEIVE:
271           /* These are per-connection */
272           if (!bus_client_policy_append_rule (client, rule))
273             return FALSE;
274           break;
275
276         default:
277           _dbus_assert_not_reached ("invalid rule");
278         }
279     }
280   
281   return TRUE;
282 }
283
284 BusClientPolicy*
285 bus_policy_create_client_policy (BusPolicy      *policy,
286                                  DBusConnection *connection,
287                                  DBusError      *error)
288 {
289   BusClientPolicy *client;
290   dbus_uid_t uid;
291   dbus_bool_t at_console;
292
293   _dbus_assert (dbus_connection_get_is_authenticated (connection));
294   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
295   
296   client = bus_client_policy_new ();
297   if (client == NULL)
298     goto nomem;
299
300   if (!add_list_to_client (&policy->default_rules,
301                            client))
302     goto nomem;
303
304   /* we avoid the overhead of looking up user's groups
305    * if we don't have any group rules anyway
306    */
307   if (_dbus_hash_table_get_n_entries (policy->rules_by_gid) > 0)
308     {
309       unsigned long *groups;
310       int n_groups;
311       int i;
312       
313       if (!bus_connection_get_unix_groups (connection, &groups, &n_groups, error))
314         goto failed;
315       
316       i = 0;
317       while (i < n_groups)
318         {
319           DBusList **list;
320           
321           list = _dbus_hash_table_lookup_uintptr (policy->rules_by_gid,
322                                                 groups[i]);
323           
324           if (list != NULL)
325             {
326               if (!add_list_to_client (list, client))
327                 {
328                   dbus_free (groups);
329                   goto nomem;
330                 }
331             }
332           
333           ++i;
334         }
335
336       dbus_free (groups);
337     }
338   
339   if (dbus_connection_get_unix_user (connection, &uid))
340     {
341       if (_dbus_hash_table_get_n_entries (policy->rules_by_uid) > 0)
342         {
343           DBusList **list;
344           
345           list = _dbus_hash_table_lookup_uintptr (policy->rules_by_uid,
346                                                 uid);
347           
348           if (list != NULL)
349             {
350               if (!add_list_to_client (list, client))
351                 goto nomem;
352             }
353         }
354
355       /* Add console rules */
356       at_console = _dbus_unix_user_is_at_console (uid, error);
357       
358       if (at_console)
359         {
360           if (!add_list_to_client (&policy->at_console_true_rules, client))
361             goto nomem;
362         }
363       else if (dbus_error_is_set (error) == TRUE)
364         {
365           goto failed;
366         }
367       else if (!add_list_to_client (&policy->at_console_false_rules, client))
368         {
369           goto nomem;
370         }
371     }
372
373   if (!add_list_to_client (&policy->mandatory_rules,
374                            client))
375     goto nomem;
376
377   return client;
378
379  nomem:
380   BUS_SET_OOM (error);
381  failed:
382   _DBUS_ASSERT_ERROR_IS_SET (error);
383   if (client)
384     bus_client_policy_unref (client);
385   return NULL;
386 }
387
388 static dbus_bool_t
389 list_allows_user (dbus_bool_t           def,
390                   DBusList            **list,
391                   unsigned long         uid,
392                   const unsigned long  *group_ids,
393                   int                   n_group_ids)
394 {
395   DBusList *link;
396   dbus_bool_t allowed;
397   
398   allowed = def;
399
400   link = _dbus_list_get_first_link (list);
401   while (link != NULL)
402     {
403       BusPolicyRule *rule = link->data;
404       link = _dbus_list_get_next_link (list, link);
405
406       if (rule->type == BUS_POLICY_RULE_USER)
407         {
408           _dbus_verbose ("List %p user rule uid="DBUS_UID_FORMAT"\n",
409                          list, rule->d.user.uid);
410           
411           if (rule->d.user.uid == DBUS_UID_UNSET)
412             ; /* '*' wildcard */
413           else if (rule->d.user.uid != uid)
414             continue;
415         }
416       else if (rule->type == BUS_POLICY_RULE_GROUP)
417         {
418           _dbus_verbose ("List %p group rule gid="DBUS_GID_FORMAT"\n",
419                          list, rule->d.group.gid);
420           
421           if (rule->d.group.gid == DBUS_GID_UNSET)
422             ;  /* '*' wildcard */
423           else
424             {
425               int i;
426               
427               i = 0;
428               while (i < n_group_ids)
429                 {
430                   if (rule->d.group.gid == group_ids[i])
431                     break;
432                   ++i;
433                 }
434               
435               if (i == n_group_ids)
436                 continue;
437             }
438         }
439       else
440         continue;
441
442       /* We don't intend to support <check user="..." /> and <check group="..." />
443          rules. They are treated like deny.
444       */
445       allowed = rule->access == BUS_POLICY_RULE_ACCESS_ALLOW;
446     }
447   
448   return allowed;
449 }
450
451 dbus_bool_t
452 bus_policy_allow_unix_user (BusPolicy        *policy,
453                             unsigned long     uid)
454 {
455   dbus_bool_t allowed;
456   unsigned long *group_ids;
457   int n_group_ids;
458
459   /* On OOM or error we always reject the user */
460   if (!_dbus_unix_groups_from_uid (uid, &group_ids, &n_group_ids))
461     {
462       _dbus_verbose ("Did not get any groups for UID %lu\n",
463                      uid);
464       return FALSE;
465     }
466
467   /* Default to "user owning bus" can connect */
468   allowed = _dbus_unix_user_is_process_owner (uid);
469
470   allowed = list_allows_user (allowed,
471                               &policy->default_rules,
472                               uid,
473                               group_ids, n_group_ids);
474
475   allowed = list_allows_user (allowed,
476                               &policy->mandatory_rules,
477                               uid,
478                               group_ids, n_group_ids);
479
480   dbus_free (group_ids);
481
482   _dbus_verbose ("UID %lu allowed = %d\n", uid, allowed);
483   
484   return allowed;
485 }
486
487 /* For now this is never actually called because the default
488  * DBusConnection behavior of 'same user that owns the bus can
489  * connect' is all it would do. Set the windows user function in
490  * connection.c if the config file ever supports doing something
491  * interesting here.
492  */
493 dbus_bool_t
494 bus_policy_allow_windows_user (BusPolicy        *policy,
495                                const char       *windows_sid)
496 {
497   /* Windows has no policies here since only the session bus
498    * is really used for now, so just checking that the
499    * connecting person is the same as the bus owner is fine.
500    */
501   return _dbus_windows_user_is_process_owner (windows_sid);
502 }
503
504 dbus_bool_t
505 bus_policy_append_default_rule (BusPolicy      *policy,
506                                 BusPolicyRule  *rule)
507 {
508   if (!_dbus_list_append (&policy->default_rules, rule))
509     return FALSE;
510
511   bus_policy_rule_ref (rule);
512
513   return TRUE;
514 }
515
516 dbus_bool_t
517 bus_policy_append_mandatory_rule (BusPolicy      *policy,
518                                   BusPolicyRule  *rule)
519 {
520   if (!_dbus_list_append (&policy->mandatory_rules, rule))
521     return FALSE;
522
523   bus_policy_rule_ref (rule);
524
525   return TRUE;
526 }
527
528
529
530 static DBusList**
531 get_list (DBusHashTable *hash,
532           unsigned long  key)
533 {
534   DBusList **list;
535
536   list = _dbus_hash_table_lookup_uintptr (hash, key);
537
538   if (list == NULL)
539     {
540       list = dbus_new0 (DBusList*, 1);
541       if (list == NULL)
542         return NULL;
543
544       if (!_dbus_hash_table_insert_uintptr (hash, key, list))
545         {
546           dbus_free (list);
547           return NULL;
548         }
549     }
550
551   return list;
552 }
553
554 dbus_bool_t
555 bus_policy_append_user_rule (BusPolicy      *policy,
556                              dbus_uid_t      uid,
557                              BusPolicyRule  *rule)
558 {
559   DBusList **list;
560
561   list = get_list (policy->rules_by_uid, uid);
562
563   if (list == NULL)
564     return FALSE;
565
566   if (!_dbus_list_append (list, rule))
567     return FALSE;
568
569   bus_policy_rule_ref (rule);
570
571   return TRUE;
572 }
573
574 dbus_bool_t
575 bus_policy_append_group_rule (BusPolicy      *policy,
576                               dbus_gid_t      gid,
577                               BusPolicyRule  *rule)
578 {
579   DBusList **list;
580
581   list = get_list (policy->rules_by_gid, gid);
582
583   if (list == NULL)
584     return FALSE;
585
586   if (!_dbus_list_append (list, rule))
587     return FALSE;
588
589   bus_policy_rule_ref (rule);
590
591   return TRUE;
592 }
593
594 dbus_bool_t
595 bus_policy_append_console_rule (BusPolicy      *policy,
596                                 dbus_bool_t     at_console,
597                                 BusPolicyRule  *rule)
598 {
599   if (at_console)
600     {
601       if (!_dbus_list_append (&policy->at_console_true_rules, rule))
602         return FALSE;
603     }
604     else
605     {
606       if (!_dbus_list_append (&policy->at_console_false_rules, rule))
607         return FALSE;
608     }
609
610   bus_policy_rule_ref (rule);
611
612   return TRUE;
613
614 }
615
616 static dbus_bool_t
617 append_copy_of_policy_list (DBusList **list,
618                             DBusList **to_append)
619 {
620   DBusList *link;
621   DBusList *tmp_list;
622
623   tmp_list = NULL;
624
625   /* Preallocate all our links */
626   link = _dbus_list_get_first_link (to_append);
627   while (link != NULL)
628     {
629       if (!_dbus_list_append (&tmp_list, link->data))
630         {
631           _dbus_list_clear (&tmp_list);
632           return FALSE;
633         }
634       
635       link = _dbus_list_get_next_link (to_append, link);
636     }
637
638   /* Now append them */
639   while ((link = _dbus_list_pop_first_link (&tmp_list)))
640     {
641       bus_policy_rule_ref (link->data);
642       _dbus_list_append_link (list, link);
643     }
644
645   return TRUE;
646 }
647
648 static dbus_bool_t
649 merge_id_hash (DBusHashTable *dest,
650                DBusHashTable *to_absorb)
651 {
652   DBusHashIter iter;
653   
654   _dbus_hash_iter_init (to_absorb, &iter);
655   while (_dbus_hash_iter_next (&iter))
656     {
657       unsigned long id = _dbus_hash_iter_get_uintptr_key (&iter);
658       DBusList **list = _dbus_hash_iter_get_value (&iter);
659       DBusList **target = get_list (dest, id);
660
661       if (target == NULL)
662         return FALSE;
663
664       if (!append_copy_of_policy_list (target, list))
665         return FALSE;
666     }
667
668   return TRUE;
669 }
670
671 dbus_bool_t
672 bus_policy_merge (BusPolicy *policy,
673                   BusPolicy *to_absorb)
674 {
675   /* FIXME Not properly atomic, but as used for configuration files we
676    * don't rely on it quite so much.
677    */
678   
679   if (!append_copy_of_policy_list (&policy->default_rules,
680                                    &to_absorb->default_rules))
681     return FALSE;
682   
683   if (!append_copy_of_policy_list (&policy->mandatory_rules,
684                                    &to_absorb->mandatory_rules))
685     return FALSE;
686
687   if (!append_copy_of_policy_list (&policy->at_console_true_rules,
688                                    &to_absorb->at_console_true_rules))
689     return FALSE;
690
691   if (!append_copy_of_policy_list (&policy->at_console_false_rules,
692                                    &to_absorb->at_console_false_rules))
693     return FALSE;
694
695   if (!merge_id_hash (policy->rules_by_uid,
696                       to_absorb->rules_by_uid))
697     return FALSE;
698   
699   if (!merge_id_hash (policy->rules_by_gid,
700                       to_absorb->rules_by_gid))
701     return FALSE;
702
703   return TRUE;
704 }
705
706 struct BusClientPolicy
707 {
708   int refcount;
709
710   DBusList *rules;
711 };
712
713 BusClientPolicy*
714 bus_client_policy_new (void)
715 {
716   BusClientPolicy *policy;
717
718   policy = dbus_new0 (BusClientPolicy, 1);
719   if (policy == NULL)
720     return NULL;
721
722   policy->refcount = 1;
723
724   return policy;
725 }
726
727 BusClientPolicy *
728 bus_client_policy_ref (BusClientPolicy *policy)
729 {
730   _dbus_assert (policy->refcount > 0);
731
732   policy->refcount += 1;
733
734   return policy;
735 }
736
737 static void
738 rule_unref_foreach (void *data,
739                     void *user_data)
740 {
741   BusPolicyRule *rule = data;
742
743   bus_policy_rule_unref (rule);
744 }
745
746 void
747 bus_client_policy_unref (BusClientPolicy *policy)
748 {
749   _dbus_assert (policy->refcount > 0);
750
751   policy->refcount -= 1;
752
753   if (policy->refcount == 0)
754     {
755       _dbus_list_foreach (&policy->rules,
756                           rule_unref_foreach,
757                           NULL);
758
759       _dbus_list_clear (&policy->rules);
760
761       dbus_free (policy);
762     }
763 }
764
765 static dbus_bool_t
766 bus_client_policy_append_rule (BusClientPolicy *policy,
767                                BusPolicyRule   *rule)
768 {
769   _dbus_verbose ("Appending rule %p with type %d to policy %p\n",
770                  rule, rule->type, policy);
771   
772   if (!_dbus_list_append (&policy->rules, rule))
773     return FALSE;
774
775   bus_policy_rule_ref (rule);
776
777   return TRUE;
778 }
779
780 #define _dbus_string_append_printf_err_check(str, fmt, args...) \
781     if (!_dbus_string_append_printf(str, fmt, ##args)) \
782       { \
783         _dbus_string_free (str); \
784         return FALSE; \
785       }
786
787 static dbus_bool_t
788 bus_policy_rule_to_string (BusPolicyRule *rule,
789                            char **out_rule)
790 {
791   const char *sr;
792   const char *access;
793   const char *dest;
794   const char *msg_type[] = {"Invalid", "method_call", "method_return", "signal", "error"};
795   DBusString str;
796
797   *out_rule = NULL;
798
799   switch (rule->access)
800     {
801     case BUS_POLICY_RULE_ACCESS_ALLOW:
802       access = "allow";
803       break;
804     case BUS_POLICY_RULE_ACCESS_DENY:
805       access = "deny";
806       break;
807     case BUS_POLICY_RULE_ACCESS_CHECK:
808       access = "check";
809       break;
810     }
811
812   if (rule->type == BUS_POLICY_RULE_SEND)
813     {
814       sr = "send";
815       dest = "destination";
816     }
817   else if (rule->type == BUS_POLICY_RULE_RECEIVE)
818     {
819       sr = "receive";
820       dest = "sender";
821     }
822   else
823     return FALSE;
824
825   /* generate xml format */
826   if (!_dbus_string_init (&str))
827     return FALSE;
828
829   _dbus_string_append_printf_err_check (&str, "<%s ", access);
830
831   if (rule->d.send.destination_prefix)
832     {
833       _dbus_string_append_printf_err_check (&str, "%s_destination_prefix=\"%s\" ", sr, rule->d.send.destination);
834     }
835   else if (rule->d.send.destination)
836     {
837       _dbus_string_append_printf_err_check (&str, "%s_%s=\"%s\" ", sr, dest, rule->d.send.destination);
838     }
839
840   if (rule->d.send.path)
841     _dbus_string_append_printf_err_check (&str, "%s_path=\"%s\" ", sr, rule->d.send.path);
842   if (rule->d.send.interface)
843     _dbus_string_append_printf_err_check (&str, "%s_interface=\"%s\" ", sr, rule->d.send.interface);
844   if (rule->d.send.member)
845     _dbus_string_append_printf_err_check (&str, "%s_member=\"%s\" ", sr, rule->d.send.member);
846   if (rule->d.send.message_type)
847     _dbus_string_append_printf_err_check (&str, "%s_type=\"%s\" ", sr, msg_type[rule->d.send.message_type]);
848   if (rule->privilege)
849     _dbus_string_append_printf_err_check (&str, "privilege=\"%s\" ", rule->privilege);
850
851   if (!_dbus_string_append (&str, "/>"))
852     {
853       _dbus_string_free (&str);
854       return FALSE;
855     }
856
857   if (!_dbus_string_steal_data (&str, out_rule))
858     {
859       *out_rule = NULL;
860       _dbus_string_free (&str);
861       return FALSE;
862     }
863
864   _dbus_string_free (&str);
865
866   return TRUE;
867 }
868
869 struct MatchSendRuleParams {
870   BusRegistry    *registry;
871   dbus_bool_t     requested_reply;
872   DBusConnection *receiver;
873   DBusMessage    *message;
874 };
875
876 static dbus_bool_t
877 check_send_rule (const BusPolicyRule  *rule,
878                  const struct MatchSendRuleParams *match_params,
879                  BusResult      *result,
880                  const char    **privilege)
881 {
882   /* Rule is skipped if it specifies a different
883    * message name from the message, or a different
884    * destination from the message
885    */
886   if (rule->type != BUS_POLICY_RULE_SEND)
887     {
888       _dbus_verbose ("  (policy) skipping non-send rule\n");
889       return FALSE;
890     }
891
892   if (rule->d.send.message_type != DBUS_MESSAGE_TYPE_INVALID)
893     {
894       if (dbus_message_get_type (match_params->message) != rule->d.send.message_type)
895         {
896           _dbus_verbose ("  (policy) skipping rule for different message type\n");
897           return FALSE;
898         }
899     }
900
901   /* If it's a reply, the requested_reply flag kicks in */
902   if (dbus_message_get_reply_serial (match_params->message) != 0)
903     {
904       /* for allow or check requested_reply=true means the rule applies
905        * only when reply was requested. requested_reply=false means the
906        * rule always applies
907        */
908       if (!match_params->requested_reply && rule->access != BUS_POLICY_RULE_ACCESS_DENY && rule->d.send.requested_reply && !rule->d.send.eavesdrop)
909         {
910           _dbus_verbose ("  (policy) skipping %s rule since it only applies to requested replies and does not allow eavesdropping\n",
911               rule->access == BUS_POLICY_RULE_ACCESS_ALLOW ? "allow" : "check");
912           return FALSE;
913         }
914
915       /* for deny, requested_reply=false means the rule applies only
916        * when the reply was not requested. requested_reply=true means the
917        * rule always applies.
918        */
919       if (match_params->requested_reply && rule->access == BUS_POLICY_RULE_ACCESS_DENY && !rule->d.send.requested_reply)
920         {
921           _dbus_verbose ("  (policy) skipping deny rule since it only applies to unrequested replies\n");
922           return FALSE;
923         }
924     }
925
926   if (rule->d.send.path != NULL)
927     {
928       if (dbus_message_get_path (match_params->message) != NULL &&
929           strcmp (dbus_message_get_path (match_params->message),
930                   rule->d.send.path) != 0)
931         {
932           _dbus_verbose ("  (policy) skipping rule for different path\n");
933           return FALSE;
934         }
935     }
936
937   if (rule->d.send.interface != NULL)
938     {
939       /* The interface is optional in messages. For allow rules, if the message
940        * has no interface we want to skip the rule (and thus not allow);
941        * for deny rules, if the message has no interface we want to use the
942        * rule (and thus deny). Check rules are meant to be used like allow
943        * rules (they can grant access, but not remove it), so we treat it like
944        * allow here.
945        */
946       dbus_bool_t no_interface;
947
948       no_interface = dbus_message_get_interface (match_params->message) == NULL;
949       
950       if ((no_interface && rule->access != BUS_POLICY_RULE_ACCESS_DENY) ||
951           (!no_interface &&
952            strcmp (dbus_message_get_interface (match_params->message),
953                    rule->d.send.interface) != 0))
954         {
955           _dbus_verbose ("  (policy) skipping rule for different interface\n");
956           return FALSE;
957         }
958     }
959
960   if (rule->d.send.member != NULL)
961     {
962       if (dbus_message_get_member (match_params->message) != NULL &&
963           strcmp (dbus_message_get_member (match_params->message),
964                   rule->d.send.member) != 0)
965         {
966           _dbus_verbose ("  (policy) skipping rule for different member\n");
967           return FALSE;
968         }
969     }
970
971   if (rule->d.send.error != NULL)
972     {
973       if (dbus_message_get_error_name (match_params->message) != NULL &&
974           strcmp (dbus_message_get_error_name (match_params->message),
975                   rule->d.send.error) != 0)
976         {
977           _dbus_verbose ("  (policy) skipping rule for different error name\n");
978           return FALSE;
979         }
980     }
981
982   if (rule->d.send.broadcast != BUS_POLICY_TRISTATE_ANY)
983     {
984       if (dbus_message_get_destination (match_params->message) == NULL &&
985           dbus_message_get_type (match_params->message) == DBUS_MESSAGE_TYPE_SIGNAL)
986         {
987           /* it's a broadcast */
988           if (rule->d.send.broadcast == BUS_POLICY_TRISTATE_FALSE)
989             {
990               _dbus_verbose ("  (policy) skipping rule because message is a broadcast\n");
991               return FALSE;
992             }
993         }
994       /* else it isn't a broadcast: there is some destination */
995       else if (rule->d.send.broadcast == BUS_POLICY_TRISTATE_TRUE)
996         {
997           _dbus_verbose ("  (policy) skipping rule because message is not a broadcast\n");
998           return FALSE;
999         }
1000     }
1001
1002   if (rule->d.send.destination != NULL)
1003     {
1004       if (!rule->d.send.destination_prefix)
1005         {
1006           /* receiver can be NULL for messages that are sent to the
1007            * message bus itself, we check the strings in that case as
1008            * built-in services don't have a DBusConnection but messages
1009            * to them have a destination service name.
1010            *
1011            * Similarly, receiver can be NULL when we're deciding whether
1012            * activation should be allowed; we make the authorization decision
1013            * on the assumption that the activated service will have the
1014            * requested name and no others.
1015            */
1016           if (match_params->receiver == NULL)
1017             {
1018               if (!dbus_message_has_destination (match_params->message,
1019                                                  rule->d.send.destination))
1020                 {
1021                   _dbus_verbose ("  (policy) skipping rule because message dest is not %s\n",
1022                                  rule->d.send.destination);
1023                   return FALSE;
1024                 }
1025             }
1026           else
1027             {
1028               DBusString str;
1029               BusService *service;
1030
1031               _dbus_string_init_const (&str, rule->d.send.destination);
1032
1033               service = bus_registry_lookup (match_params->registry, &str);
1034               if (service == NULL)
1035                 {
1036                   _dbus_verbose ("  (policy) skipping rule because dest %s doesn't exist\n",
1037                                  rule->d.send.destination);
1038                   return FALSE;
1039                 }
1040
1041               if (!bus_service_has_owner (service, match_params->receiver))
1042                 {
1043                   _dbus_verbose ("  (policy) skipping rule because dest %s isn't owned by receiver\n",
1044                                  rule->d.send.destination);
1045                   return FALSE;
1046                 }
1047             }
1048         }
1049       else if (rule->d.send.destination_prefix)
1050         {
1051           /* receiver can be NULL - the same as in !send.destination_prefix */
1052           if (match_params->receiver == NULL)
1053             {
1054               const char *destination = dbus_message_get_destination (match_params->message);
1055               DBusString dest_name;
1056
1057               if (destination == NULL)
1058                 {
1059                   _dbus_verbose ("  (policy) skipping rule because message has no dest\n");
1060                   return FALSE;
1061                 }
1062
1063               _dbus_string_init_const (&dest_name, destination);
1064
1065               if (!_dbus_string_starts_with_words_c_str (&dest_name,
1066                                                          rule->d.send.destination,
1067                                                          '.'))
1068                 {
1069                   _dbus_verbose ("  (policy) skipping rule because message dest doesn't start with %s\n",
1070                                  rule->d.send.destination);
1071                   return FALSE;
1072                 }
1073             }
1074           else
1075             {
1076               if (!bus_connection_is_service_owner_by_prefix (match_params->receiver,
1077                                                               rule->d.send.destination))
1078                 {
1079                   _dbus_verbose ("  (policy) skipping rule because no dest with prefix %s is owned by receiver\n",
1080                                  rule->d.send.destination);
1081                   return FALSE;
1082                 }
1083             }
1084         }
1085     }
1086
1087   if (rule->d.send.min_fds > 0 ||
1088       rule->d.send.max_fds < DBUS_MAXIMUM_MESSAGE_UNIX_FDS)
1089     {
1090       unsigned int n_fds = _dbus_message_get_n_unix_fds (match_params->message);
1091
1092       if (n_fds < rule->d.send.min_fds || n_fds > rule->d.send.max_fds)
1093         {
1094           _dbus_verbose ("  (policy) skipping rule because message has %u fds "
1095                          "and that is outside range [%u,%u]",
1096                          n_fds, rule->d.send.min_fds, rule->d.send.max_fds);
1097           return FALSE;
1098         }
1099     }
1100
1101   /* Use this rule */
1102   switch (rule->access)
1103   {
1104     case BUS_POLICY_RULE_ACCESS_ALLOW:
1105       *result = BUS_RESULT_TRUE;
1106       break;
1107     case BUS_POLICY_RULE_ACCESS_DENY:
1108       *result = BUS_RESULT_FALSE;
1109       break;
1110     case BUS_POLICY_RULE_ACCESS_CHECK:
1111       *result = BUS_RESULT_LATER;
1112       *privilege = rule->privilege;
1113       break;
1114   }
1115
1116   return TRUE;
1117 }
1118
1119 BusResult
1120 bus_client_policy_check_can_send (DBusConnection      *sender,
1121                                   BusClientPolicy     *policy,
1122                                   BusRegistry         *registry,
1123                                   dbus_bool_t          requested_reply,
1124                                   DBusConnection      *addressed_recipient,
1125                                   DBusConnection      *receiver,
1126                                   DBusMessage         *message,
1127                                   dbus_int32_t        *toggles,
1128                                   dbus_bool_t         *log,
1129                                   const char         **privilege_param,
1130                                   BusDeferredMessage **deferred_message,
1131                                   char               **out_rule)
1132 {
1133   DBusList *link;
1134   BusResult result;
1135   const char *privilege;
1136   BusPolicyRule *matched_rule = NULL;
1137   struct MatchSendRuleParams params;
1138
1139   params.registry = registry;
1140   params.requested_reply = requested_reply;
1141   params.receiver = receiver;
1142   params.message = message;
1143
1144   /* policy->rules is in the order the rules appeared
1145    * in the config file, i.e. last rule that applies wins
1146    */
1147
1148   _dbus_verbose ("  (policy) checking send rules\n");
1149   *toggles = 0;
1150
1151   result = BUS_RESULT_FALSE;
1152   link = _dbus_list_get_first_link (&policy->rules);
1153   while (link != NULL)
1154     {
1155       const BusPolicyRule *rule = link->data;
1156
1157       link = _dbus_list_get_next_link (&policy->rules, link);
1158
1159       if (check_send_rule (rule, &params, &result, &privilege))
1160         {
1161           *log = rule->d.send.log;
1162           (*toggles)++;
1163           matched_rule = (BusPolicyRule *)rule;
1164
1165           _dbus_verbose ("  (policy) used rule, result now = %d\n",
1166                          result);
1167         }
1168     }
1169
1170   if (result == BUS_RESULT_LATER)
1171     {
1172       BusContext *context = bus_connection_get_context(sender);
1173       BusCheck *check = bus_context_get_check(context);
1174
1175       result = bus_check_privilege(check, message, sender, addressed_recipient, receiver,
1176           privilege, BUS_DEFERRED_MESSAGE_CHECK_SEND, deferred_message);
1177       if (result == BUS_RESULT_LATER && deferred_message != NULL)
1178         bus_deferred_message_set_policy_check_info(*deferred_message, requested_reply,
1179             *toggles, privilege);
1180     }
1181   else
1182     privilege = NULL;
1183
1184   if (privilege_param != NULL)
1185     *privilege_param = privilege;
1186
1187   if (result == BUS_RESULT_FALSE)
1188     {
1189       if (matched_rule && out_rule)
1190         bus_policy_rule_to_string (matched_rule, out_rule);
1191     }
1192
1193   return result;
1194 }
1195
1196 struct MatchReceiveRuleParams {
1197   BusRegistry    *registry;
1198   dbus_bool_t     requested_reply;
1199   DBusConnection *sender;
1200   DBusMessage    *message;
1201   dbus_bool_t     eavesdropping;
1202 };
1203
1204 static dbus_bool_t
1205 check_receive_rule (const BusPolicyRule  *rule,
1206                     const struct MatchReceiveRuleParams *match_params,
1207                     BusResult      *result,
1208                     const char    **privilege)
1209 {
1210   if (rule->type != BUS_POLICY_RULE_RECEIVE)
1211     {
1212       _dbus_verbose ("  (policy) skipping non-receive rule\n");
1213       return FALSE;
1214     }
1215
1216   if (rule->d.receive.message_type != DBUS_MESSAGE_TYPE_INVALID)
1217     {
1218       if (dbus_message_get_type (match_params->message) != rule->d.receive.message_type)
1219         {
1220           _dbus_verbose ("  (policy) skipping rule for different message type\n");
1221           return FALSE;
1222         }
1223     }
1224
1225
1226   /* for allow or check, eavesdrop=false means the rule doesn't apply when
1227    * eavesdropping. eavesdrop=true means the rule always applies
1228    */
1229   if (match_params->eavesdropping && rule->access != BUS_POLICY_RULE_ACCESS_DENY && !rule->d.receive.eavesdrop)
1230     {
1231       _dbus_verbose ("  (policy) skipping %s rule since it doesn't apply to eavesdropping\n",
1232           rule->access == BUS_POLICY_RULE_ACCESS_ALLOW ? "allow" : "check");
1233       return FALSE;
1234     }
1235
1236   /* for deny, eavesdrop=true means the rule applies only when
1237    * eavesdropping; eavesdrop=false means always deny.
1238    */
1239   if (!match_params->eavesdropping && rule->access == BUS_POLICY_RULE_ACCESS_DENY && rule->d.receive.eavesdrop)
1240     {
1241       _dbus_verbose ("  (policy) skipping deny rule since it only applies to eavesdropping\n");
1242       return FALSE;
1243     }
1244
1245   /* If it's a reply, the requested_reply flag kicks in */
1246   if (dbus_message_get_reply_serial (match_params->message) != 0)
1247     {
1248       /* for allow or check requested_reply=true means the rule applies
1249        * only when reply was requested. requested_reply=false means the
1250        * rule always applies
1251        */
1252       if (!match_params->requested_reply && rule->access != BUS_POLICY_RULE_ACCESS_DENY && rule->d.send.requested_reply && !rule->d.send.eavesdrop)
1253         {
1254           _dbus_verbose ("  (policy) skipping %s rule since it only applies to requested replies and does not allow eavesdropping\n",
1255               rule->access == BUS_POLICY_RULE_ACCESS_DENY ? "allow" : "deny");
1256           return FALSE;
1257         }
1258
1259       /* for deny, requested_reply=false means the rule applies only
1260        * when the reply was not requested. requested_reply=true means the
1261        * rule always applies.
1262        */
1263       if (match_params->requested_reply && rule->access == BUS_POLICY_RULE_ACCESS_DENY && !rule->d.receive.requested_reply)
1264         {
1265           _dbus_verbose ("  (policy) skipping deny rule since it only applies to unrequested replies\n");
1266           return FALSE;
1267         }
1268     }
1269
1270   if (rule->d.receive.path != NULL)
1271     {
1272       if (dbus_message_get_path (match_params->message) != NULL &&
1273           strcmp (dbus_message_get_path (match_params->message),
1274                   rule->d.receive.path) != 0)
1275         {
1276           _dbus_verbose ("  (policy) skipping rule for different path\n");
1277           return FALSE;
1278         }
1279     }
1280
1281   if (rule->d.receive.interface != NULL)
1282     {
1283       /* The interface is optional in messages. For allow rules, if the message
1284        * has no interface we want to skip the rule (and thus not allow);
1285        * for deny rules, if the message has no interface we want to use the
1286        * rule (and thus deny). Check rules are treated like allow rules.
1287        */
1288       dbus_bool_t no_interface;
1289
1290       no_interface = dbus_message_get_interface (match_params->message) == NULL;
1291
1292       if ((no_interface && rule->access != BUS_POLICY_RULE_ACCESS_DENY) ||
1293           (!no_interface &&
1294            strcmp (dbus_message_get_interface (match_params->message),
1295                    rule->d.receive.interface) != 0))
1296         {
1297           _dbus_verbose ("  (policy) skipping rule for different interface\n");
1298           return FALSE;
1299         }
1300     }
1301
1302   if (rule->d.receive.member != NULL)
1303     {
1304       if (dbus_message_get_member (match_params->message) != NULL &&
1305           strcmp (dbus_message_get_member (match_params->message),
1306                   rule->d.receive.member) != 0)
1307         {
1308           _dbus_verbose ("  (policy) skipping rule for different member\n");
1309           return FALSE;
1310         }
1311     }
1312
1313   if (rule->d.receive.error != NULL)
1314     {
1315       if (dbus_message_get_error_name (match_params->message) != NULL &&
1316           strcmp (dbus_message_get_error_name (match_params->message),
1317                   rule->d.receive.error) != 0)
1318         {
1319           _dbus_verbose ("  (policy) skipping rule for different error name\n");
1320           return FALSE;
1321         }
1322     }
1323
1324   if (rule->d.receive.origin != NULL)
1325     {
1326       /* sender can be NULL for messages that originate from the
1327        * message bus itself, we check the strings in that case as
1328        * built-in services don't have a DBusConnection but will
1329        * still set the sender on their messages.
1330        */
1331       if (match_params->sender == NULL)
1332         {
1333           if (!dbus_message_has_sender (match_params->message,
1334                                         rule->d.receive.origin))
1335             {
1336               _dbus_verbose ("  (policy) skipping rule because message sender is not %s\n",
1337                              rule->d.receive.origin);
1338               return FALSE;
1339             }
1340         }
1341       else
1342         {
1343           BusService *service;
1344           DBusString str;
1345
1346           _dbus_string_init_const (&str, rule->d.receive.origin);
1347
1348           service = bus_registry_lookup (match_params->registry, &str);
1349           
1350           if (service == NULL)
1351             {
1352               _dbus_verbose ("  (policy) skipping rule because origin %s doesn't exist\n",
1353                              rule->d.receive.origin);
1354               return FALSE;
1355             }
1356
1357           if (!bus_service_has_owner (service, match_params->sender))
1358             {
1359               _dbus_verbose ("  (policy) skipping rule because origin %s isn't owned by sender\n",
1360                              rule->d.receive.origin);
1361               return FALSE;
1362             }
1363         }
1364     }
1365
1366   if (rule->d.receive.min_fds > 0 ||
1367       rule->d.receive.max_fds < DBUS_MAXIMUM_MESSAGE_UNIX_FDS)
1368     {
1369       unsigned int n_fds = _dbus_message_get_n_unix_fds (match_params->message);
1370
1371       if (n_fds < rule->d.receive.min_fds || n_fds > rule->d.receive.max_fds)
1372         {
1373           _dbus_verbose ("  (policy) skipping rule because message has %u fds "
1374                          "and that is outside range [%u,%u]",
1375                          n_fds, rule->d.receive.min_fds,
1376                          rule->d.receive.max_fds);
1377           return FALSE;
1378         }
1379     }
1380
1381   /* Use this rule */
1382   switch (rule->access)
1383   {
1384     case BUS_POLICY_RULE_ACCESS_ALLOW:
1385       *result = BUS_RESULT_TRUE;
1386       break;
1387     case BUS_POLICY_RULE_ACCESS_DENY:
1388       *result = BUS_RESULT_FALSE;
1389       break;
1390     case BUS_POLICY_RULE_ACCESS_CHECK:
1391       *result = BUS_RESULT_LATER;
1392       *privilege = rule->privilege;
1393       break;
1394   }
1395
1396   return TRUE;
1397 }
1398
1399 /* See docs on what the args mean on bus_context_check_security_policy()
1400  * comment
1401  */
1402 BusResult
1403 bus_client_policy_check_can_receive (BusClientPolicy     *policy,
1404                                      BusRegistry         *registry,
1405                                      dbus_bool_t          requested_reply,
1406                                      DBusConnection      *sender,
1407                                      DBusConnection      *addressed_recipient,
1408                                      DBusConnection      *proposed_recipient,
1409                                      DBusMessage         *message,
1410                                      dbus_int32_t        *toggles,
1411                                      const char         **privilege_param,
1412                                      BusDeferredMessage **deferred_message,
1413                                      char               **out_rule)
1414 {
1415   DBusList *link;
1416   BusResult result;
1417   const char *privilege;
1418   BusPolicyRule *matched_rule = NULL;
1419   struct MatchReceiveRuleParams params;
1420
1421   params.eavesdropping =
1422     addressed_recipient != proposed_recipient &&
1423     dbus_message_get_destination (message) != NULL;
1424   params.registry = registry;
1425   params.requested_reply = requested_reply;
1426   params.sender = sender;
1427   params.message = message;
1428
1429   /* policy->rules is in the order the rules appeared
1430    * in the config file, i.e. last rule that applies wins
1431    */
1432
1433   _dbus_verbose ("  (policy) checking receive rules, eavesdropping = %d\n", eavesdropping);
1434   *toggles = 0;
1435
1436   result = BUS_RESULT_FALSE;
1437   link = _dbus_list_get_first_link (&policy->rules);
1438   while (link != NULL)
1439     {
1440       const BusPolicyRule *rule = link->data;
1441
1442       link = _dbus_list_get_next_link (&policy->rules, link);
1443
1444       if (check_receive_rule (rule, &params, &result, &privilege))
1445         {
1446           (*toggles)++;
1447           matched_rule = (BusPolicyRule *)rule;
1448
1449           _dbus_verbose ("  (policy) used rule, result now = %d\n",
1450                          result);
1451         }
1452     }
1453
1454
1455   if (result == BUS_RESULT_LATER)
1456     {
1457       BusContext *context = bus_connection_get_context(proposed_recipient);
1458       BusCheck *check = bus_context_get_check(context);
1459
1460       result = bus_check_privilege(check, message, sender, addressed_recipient, proposed_recipient,
1461                  privilege, BUS_DEFERRED_MESSAGE_CHECK_RECEIVE, deferred_message);
1462       if (result == BUS_RESULT_LATER && deferred_message != NULL)
1463         bus_deferred_message_set_policy_check_info(*deferred_message, requested_reply,
1464                     *toggles, privilege);
1465     }
1466   else
1467       privilege = NULL;
1468
1469   if (privilege_param != NULL)
1470      *privilege_param = privilege;
1471
1472   if (result == BUS_RESULT_FALSE)
1473     {
1474       if (matched_rule && out_rule)
1475         bus_policy_rule_to_string (matched_rule, out_rule);
1476     }
1477
1478   return result;
1479 }
1480
1481 static dbus_bool_t
1482 check_own_rule (const BusPolicyRule *rule,
1483                 const DBusString    *service_name,
1484                 BusResult           *result,
1485                 const char         **privilege)
1486 {
1487   /* Rule is skipped if it specifies a different service name from
1488    * the desired one.
1489    */
1490
1491   if (rule->type != BUS_POLICY_RULE_OWN)
1492     return FALSE;
1493
1494   if (!rule->d.own.prefix && rule->d.own.service_name != NULL)
1495     {
1496       if (!_dbus_string_equal_c_str (service_name,
1497                                      rule->d.own.service_name))
1498         return FALSE;
1499     }
1500   else if (rule->d.own.prefix)
1501     {
1502       if (!_dbus_string_starts_with_words_c_str (service_name,
1503                                                  rule->d.own.service_name,
1504                                                  '.'))
1505         return FALSE;
1506     }
1507
1508   /* Use this rule */
1509   switch (rule->access)
1510   {
1511   case BUS_POLICY_RULE_ACCESS_ALLOW:
1512     *result = BUS_RESULT_TRUE;
1513     break;
1514   case BUS_POLICY_RULE_ACCESS_DENY:
1515     *result = BUS_RESULT_FALSE;
1516     break;
1517   case BUS_POLICY_RULE_ACCESS_CHECK:
1518     *result = BUS_RESULT_LATER;
1519     *privilege = rule->privilege;
1520     break;
1521   }
1522
1523   return TRUE;
1524 }
1525
1526 static BusResult
1527 bus_rules_check_can_own (DBusList *rules,
1528                          const DBusString *service_name,
1529                          DBusConnection   *connection,
1530                          DBusMessage      *message)
1531 {
1532   DBusList *link;
1533   BusResult result;
1534   const char *privilege;
1535   
1536   /* rules is in the order the rules appeared
1537    * in the config file, i.e. last rule that applies wins
1538    */
1539
1540   result = BUS_RESULT_FALSE;
1541   link = _dbus_list_get_first_link (&rules);
1542   while (link != NULL)
1543     {
1544       const BusPolicyRule *rule = link->data;
1545
1546       link = _dbus_list_get_next_link (&rules, link);
1547       
1548       check_own_rule (rule, service_name, &result, &privilege);
1549     }
1550
1551   if (result == BUS_RESULT_LATER)
1552     {
1553       BusContext *context = bus_connection_get_context(connection);
1554       BusCheck *check = bus_context_get_check(context);
1555       BusDeferredMessage *deferred_message;
1556
1557       result = bus_check_privilege(check, message, connection, NULL, NULL,
1558           privilege, BUS_DEFERRED_MESSAGE_CHECK_OWN, &deferred_message);
1559       if (result == BUS_RESULT_LATER)
1560         {
1561           bus_deferred_message_disable_sender(deferred_message);
1562         }
1563     }
1564
1565   return result;
1566 }
1567
1568 BusResult
1569 bus_client_policy_check_can_own (BusClientPolicy  *policy,
1570                                  const DBusString *service_name,
1571                                  DBusConnection   *connection,
1572                                  DBusMessage      *message)
1573 {
1574   return bus_rules_check_can_own (policy->rules, service_name, connection, message);
1575 }
1576
1577 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1578 dbus_bool_t
1579 bus_policy_check_can_own (BusPolicy  *policy,
1580                           const DBusString *service_name)
1581 {
1582   return bus_rules_check_can_own (policy->default_rules, service_name, NULL, NULL);
1583 }
1584 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
1585