policy.c 22.9 KB
Newer Older
1
/* -*- mode: C; c-file-style: "gnu" -*- */
2
/* policy.c  Bus security policy
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Copyright (C) 2003  Red Hat, Inc.
 *
 * Licensed under the Academic Free License version 1.2
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "policy.h"
25
#include "services.h"
26
#include "test.h"
27
#include "utils.h"
28
#include <dbus/dbus-list.h>
29
#include <dbus/dbus-hash.h>
30
#include <dbus/dbus-internals.h>
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

BusPolicyRule*
bus_policy_rule_new (BusPolicyRuleType type,
                     dbus_bool_t       allow)
{
  BusPolicyRule *rule;

  rule = dbus_new0 (BusPolicyRule, 1);
  if (rule == NULL)
    return NULL;

  rule->type = type;
  rule->refcount = 1;
  rule->allow = allow;

46 47 48 49 50 51 52 53 54 55 56 57 58 59
  switch (rule->type)
    {
    case BUS_POLICY_RULE_USER:
      rule->d.user.uid = DBUS_UID_UNSET;
      break;
    case BUS_POLICY_RULE_GROUP:
      rule->d.group.gid = DBUS_GID_UNSET;
      break;
    case BUS_POLICY_RULE_SEND:
    case BUS_POLICY_RULE_RECEIVE:
    case BUS_POLICY_RULE_OWN:
      break;
    }
  
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  return rule;
}

void
bus_policy_rule_ref (BusPolicyRule *rule)
{
  _dbus_assert (rule->refcount > 0);

  rule->refcount += 1;
}

void
bus_policy_rule_unref (BusPolicyRule *rule)
{
  _dbus_assert (rule->refcount > 0);

  rule->refcount -= 1;
77
  
78 79 80 81
  if (rule->refcount == 0)
    {
      switch (rule->type)
        {
82
        case BUS_POLICY_RULE_SEND:
83 84 85
          dbus_free (rule->d.send.message_name);
          dbus_free (rule->d.send.destination);
          break;
86
        case BUS_POLICY_RULE_RECEIVE:
87 88 89
          dbus_free (rule->d.receive.message_name);
          dbus_free (rule->d.receive.origin);
          break;
90
        case BUS_POLICY_RULE_OWN:
91 92
          dbus_free (rule->d.own.service_name);
          break;
93
        case BUS_POLICY_RULE_USER:
94
          break;
95 96
        case BUS_POLICY_RULE_GROUP:
          break;
97 98 99 100 101 102 103 104 105 106
        }
      
      dbus_free (rule);
    }
}

struct BusPolicy
{
  int refcount;

107 108 109 110
  DBusList *default_rules;       /**< Default policy rules */
  DBusList *mandatory_rules;     /**< Mandatory policy rules */
  DBusHashTable *rules_by_uid;   /**< per-UID policy rules */
  DBusHashTable *rules_by_gid;   /**< per-GID policy rules */
111 112
};

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
static void
free_rule_func (void *data,
                void *user_data)
{
  BusPolicyRule *rule = data;

  bus_policy_rule_unref (rule);
}

static void
free_rule_list_func (void *data)
{
  DBusList **list = data;

  _dbus_list_foreach (list, free_rule_func, NULL);
  
  _dbus_list_clear (list);

  dbus_free (list);
}

134 135 136 137 138 139 140 141 142 143
BusPolicy*
bus_policy_new (void)
{
  BusPolicy *policy;

  policy = dbus_new0 (BusPolicy, 1);
  if (policy == NULL)
    return NULL;

  policy->refcount = 1;
144 145 146 147 148 149 150 151 152 153 154 155 156
  
  policy->rules_by_uid = _dbus_hash_table_new (DBUS_HASH_ULONG,
                                               NULL,
                                               free_rule_list_func);
  if (policy->rules_by_uid == NULL)
    goto failed;

  policy->rules_by_gid = _dbus_hash_table_new (DBUS_HASH_ULONG,
                                               NULL,
                                               free_rule_list_func);
  if (policy->rules_by_gid == NULL)
    goto failed;
  
157
  return policy;
158 159 160 161
  
 failed:
  bus_policy_unref (policy);
  return NULL;
162 163 164 165 166 167 168 169 170 171
}

void
bus_policy_ref (BusPolicy *policy)
{
  _dbus_assert (policy->refcount > 0);

  policy->refcount += 1;
}

172 173 174 175 176 177 178 179 180
void
bus_policy_unref (BusPolicy *policy)
{
  _dbus_assert (policy->refcount > 0);

  policy->refcount -= 1;

  if (policy->refcount == 0)
    {
181 182 183 184 185 186
      _dbus_list_foreach (&policy->default_rules, free_rule_func, NULL);
      _dbus_list_clear (&policy->default_rules);

      _dbus_list_foreach (&policy->mandatory_rules, free_rule_func, NULL);
      _dbus_list_clear (&policy->mandatory_rules);
      
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
      if (policy->rules_by_uid)
        {
          _dbus_hash_table_unref (policy->rules_by_uid);
          policy->rules_by_uid = NULL;
        }

      if (policy->rules_by_gid)
        {
          _dbus_hash_table_unref (policy->rules_by_gid);
          policy->rules_by_gid = NULL;
        }
      
      dbus_free (policy);
    }
}

static dbus_bool_t
add_list_to_client (DBusList        **list,
                    BusClientPolicy  *client)
{
  DBusList *link;

  link = _dbus_list_get_first_link (list);
  while (link != NULL)
    {
      BusPolicyRule *rule = link->data;
      link = _dbus_list_get_next_link (list, link);

      switch (rule->type)
        {
        case BUS_POLICY_RULE_USER:
        case BUS_POLICY_RULE_GROUP:
          /* These aren't per-connection policies */
          break;

        case BUS_POLICY_RULE_OWN:
        case BUS_POLICY_RULE_SEND:
        case BUS_POLICY_RULE_RECEIVE:
          /* These are per-connection */
          if (!bus_client_policy_append_rule (client, rule))
            return FALSE;
          break;
        }
    }
  
  return TRUE;
}

BusClientPolicy*
bus_policy_create_client_policy (BusPolicy      *policy,
237 238
                                 DBusConnection *connection,
                                 DBusError      *error)
239 240 241 242 243
{
  BusClientPolicy *client;
  unsigned long uid;

  _dbus_assert (dbus_connection_get_is_authenticated (connection));
244
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
245 246 247
  
  client = bus_client_policy_new ();
  if (client == NULL)
248
    goto nomem;
249 250 251

  if (!add_list_to_client (&policy->default_rules,
                           client))
252
    goto nomem;
253 254 255 256 257 258

  /* we avoid the overhead of looking up user's groups
   * if we don't have any group rules anyway
   */
  if (_dbus_hash_table_get_n_entries (policy->rules_by_gid) > 0)
    {
259
      unsigned long *groups;
260 261 262
      int n_groups;
      int i;
      
263
      if (!bus_connection_get_groups (connection, &groups, &n_groups, error))
264 265 266 267 268
        goto failed;
      
      i = 0;
      while (i < n_groups)
        {
269 270
          DBusList **list;
          
271 272 273 274 275 276
          list = _dbus_hash_table_lookup_ulong (policy->rules_by_gid,
                                                groups[i]);
          
          if (list != NULL)
            {
              if (!add_list_to_client (list, client))
277 278
                {
                  dbus_free (groups);
279
                  goto nomem;
280
                }
281 282 283 284
            }
          
          ++i;
        }
285 286

      dbus_free (groups);
287 288 289
    }

  if (!dbus_connection_get_unix_user (connection, &uid))
290 291 292 293 294
    {
      dbus_set_error (error, DBUS_ERROR_FAILED,
                      "No user ID known for connection, cannot determine security policy\n");
      goto failed;
    }
295

296 297 298 299 300 301 302 303 304 305
  if (_dbus_hash_table_get_n_entries (policy->rules_by_uid) > 0)
    {
      DBusList **list;
      
      list = _dbus_hash_table_lookup_ulong (policy->rules_by_uid,
                                            uid);

      if (list != NULL)
        {
          if (!add_list_to_client (list, client))
306
            goto nomem;
307 308
        }
    }
309 310 311

  if (!add_list_to_client (&policy->mandatory_rules,
                           client))
312
    goto nomem;
313 314 315 316

  bus_client_policy_optimize (client);
  
  return client;
317 318 319

 nomem:
  BUS_SET_OOM (error);
320
 failed:
321 322 323
  _DBUS_ASSERT_ERROR_IS_SET (error);
  if (client)
    bus_client_policy_unref (client);
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
  return NULL;
}

static dbus_bool_t
list_allows_user (dbus_bool_t           def,
                  DBusList            **list,
                  unsigned long         uid,
                  const unsigned long  *group_ids,
                  int                   n_group_ids)
{
  DBusList *link;
  dbus_bool_t allowed;
  
  allowed = def;

  link = _dbus_list_get_first_link (list);
  while (link != NULL)
    {
      BusPolicyRule *rule = link->data;
      link = _dbus_list_get_next_link (list, link);
      
      if (rule->type == BUS_POLICY_RULE_USER)
        {
347 348 349 350 351 352
          _dbus_verbose ("List %p user rule uid="DBUS_UID_FORMAT"\n",
                         list, rule->d.user.uid);
          
          if (rule->d.user.uid == DBUS_UID_UNSET)
            ; /* '*' wildcard */
          else if (rule->d.user.uid != uid)
353 354 355 356
            continue;
        }
      else if (rule->type == BUS_POLICY_RULE_GROUP)
        {
357 358 359 360 361 362
          _dbus_verbose ("List %p group rule uid="DBUS_UID_FORMAT"\n",
                         list, rule->d.user.uid);
          
          if (rule->d.group.gid == DBUS_GID_UNSET)
            ;  /* '*' wildcard */
          else
363
            {
364 365 366 367 368 369 370 371 372 373 374 375
              int i;
              
              i = 0;
              while (i < n_group_ids)
                {
                  if (rule->d.group.gid == group_ids[i])
                    break;
                  ++i;
                }
              
              if (i == n_group_ids)
                continue;
376 377 378 379 380 381 382 383 384 385 386 387
            }
        }
      else
        continue;

      allowed = rule->allow;
    }
  
  return allowed;
}

dbus_bool_t
388 389 390
bus_policy_allow_user (BusPolicy        *policy,
                       DBusUserDatabase *user_database,
                       unsigned long     uid)
391 392 393 394 395 396
{
  dbus_bool_t allowed;
  unsigned long *group_ids;
  int n_group_ids;

  /* On OOM or error we always reject the user */
397 398
  if (!_dbus_user_database_get_groups (user_database,
                                       uid, &group_ids, &n_group_ids, NULL))
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    {
      _dbus_verbose ("Did not get any groups for UID %lu\n",
                     uid);
      return FALSE;
    }
  
  allowed = FALSE;

  allowed = list_allows_user (allowed,
                              &policy->default_rules,
                              uid,
                              group_ids, n_group_ids);

  allowed = list_allows_user (allowed,
                              &policy->mandatory_rules,
                              uid,
                              group_ids, n_group_ids);

  dbus_free (group_ids);

419 420
  _dbus_verbose ("UID %lu allowed = %d\n", uid, allowed);
  
421 422 423
  return allowed;
}

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
dbus_bool_t
bus_policy_append_default_rule (BusPolicy      *policy,
                                BusPolicyRule  *rule)
{
  if (!_dbus_list_append (&policy->default_rules, rule))
    return FALSE;

  bus_policy_rule_ref (rule);

  return TRUE;
}

dbus_bool_t
bus_policy_append_mandatory_rule (BusPolicy      *policy,
                                  BusPolicyRule  *rule)
{
  if (!_dbus_list_append (&policy->mandatory_rules, rule))
    return FALSE;

  bus_policy_rule_ref (rule);

  return TRUE;
}

static DBusList**
get_list (DBusHashTable *hash,
          unsigned long  key)
{
  DBusList **list;

  list = _dbus_hash_table_lookup_ulong (hash, key);

  if (list == NULL)
    {
      list = dbus_new0 (DBusList*, 1);
      if (list == NULL)
        return NULL;

      if (!_dbus_hash_table_insert_ulong (hash, key, list))
        {
          dbus_free (list);
          return NULL;
        }
    }

  return list;
}

dbus_bool_t
bus_policy_append_user_rule (BusPolicy      *policy,
                             dbus_uid_t      uid,
                             BusPolicyRule  *rule)
{
  DBusList **list;

  list = get_list (policy->rules_by_uid, uid);

  if (list == NULL)
    return FALSE;

  if (!_dbus_list_append (list, rule))
    return FALSE;

  bus_policy_rule_ref (rule);

  return TRUE;
}

dbus_bool_t
bus_policy_append_group_rule (BusPolicy      *policy,
                              dbus_gid_t      gid,
                              BusPolicyRule  *rule)
{
  DBusList **list;

  list = get_list (policy->rules_by_gid, gid);

  if (list == NULL)
    return FALSE;

  if (!_dbus_list_append (list, rule))
    return FALSE;

  bus_policy_rule_ref (rule);

  return TRUE;
}

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
struct BusClientPolicy
{
  int refcount;

  DBusList *rules;
};

BusClientPolicy*
bus_client_policy_new (void)
{
  BusClientPolicy *policy;

  policy = dbus_new0 (BusClientPolicy, 1);
  if (policy == NULL)
    return NULL;

  policy->refcount = 1;

  return policy;
}

void
bus_client_policy_ref (BusClientPolicy *policy)
{
  _dbus_assert (policy->refcount > 0);

  policy->refcount += 1;
}

541 542 543 544 545 546 547 548 549 550
static void
rule_unref_foreach (void *data,
                    void *user_data)
{
  BusPolicyRule *rule = data;

  bus_policy_rule_unref (rule);
}

void
551
bus_client_policy_unref (BusClientPolicy *policy)
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
{
  _dbus_assert (policy->refcount > 0);

  policy->refcount -= 1;

  if (policy->refcount == 0)
    {
      _dbus_list_foreach (&policy->rules,
                          rule_unref_foreach,
                          NULL);

      _dbus_list_clear (&policy->rules);
      
      dbus_free (policy);
    }
}

569
static void
570
remove_rules_by_type_up_to (BusClientPolicy   *policy,
571 572 573 574 575
                            BusPolicyRuleType  type,
                            DBusList          *up_to)
{
  DBusList *link;

576
  link = _dbus_list_get_first_link (&policy->rules);
577 578 579 580 581
  while (link != up_to)
    {
      BusPolicyRule *rule = link->data;
      DBusList *next = _dbus_list_get_next_link (&policy->rules, link);

582 583 584 585 586 587
      if (rule->type == type)
        {
          _dbus_list_remove_link (&policy->rules, link);
          bus_policy_rule_unref (rule);
        }
      
588 589 590 591
      link = next;
    }
}

592
void
593
bus_client_policy_optimize (BusClientPolicy *policy)
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
{
  DBusList *link;

  /* The idea here is that if we have:
   * 
   * <allow send="foo"/>
   * <deny send="*"/>
   *
   * (for example) the deny will always override the allow.  So we
   * delete the allow. Ditto for deny followed by allow, etc. This is
   * a dumb thing to put in a config file, but the <include> feature
   * of files allows for an "inheritance and override" pattern where
   * it could make sense. If an included file wants to "start over"
   * with a blanket deny, no point keeping the rules from the parent
   * file.
   */

611 612 613
  _dbus_verbose ("Optimizing policy with %d rules\n",
                 _dbus_list_get_length (&policy->rules));
  
614
  link = _dbus_list_get_first_link (&policy->rules);
615 616
  while (link != NULL)
    {
617 618
      BusPolicyRule *rule;
      DBusList *next;
619 620
      dbus_bool_t remove_preceding;

621 622 623
      next = _dbus_list_get_next_link (&policy->rules, link);
      rule = link->data;
      
624
      remove_preceding = FALSE;
625 626

      _dbus_assert (rule != NULL);
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
      
      switch (rule->type)
        {
        case BUS_POLICY_RULE_SEND:
          remove_preceding =
            rule->d.send.message_name == NULL &&
            rule->d.send.destination == NULL;
          break;
        case BUS_POLICY_RULE_RECEIVE:
          remove_preceding =
            rule->d.receive.message_name == NULL &&
            rule->d.receive.origin == NULL;
          break;
        case BUS_POLICY_RULE_OWN:
          remove_preceding =
            rule->d.own.service_name == NULL;
          break;
644 645 646 647
        case BUS_POLICY_RULE_USER:
        case BUS_POLICY_RULE_GROUP:
          _dbus_assert_not_reached ("invalid rule");
          break;
648
        }
649

650 651 652 653 654 655
      if (remove_preceding)
        remove_rules_by_type_up_to (policy, rule->type,
                                    link);
      
      link = next;
    }
656 657 658 659 660 661

  _dbus_verbose ("After optimization, policy has %d rules\n",
                 _dbus_list_get_length (&policy->rules));
}

dbus_bool_t
662 663
bus_client_policy_append_rule (BusClientPolicy *policy,
                               BusPolicyRule   *rule)
664
{
665 666 667
  _dbus_verbose ("Appending rule %p with type %d to policy %p\n",
                 rule, rule->type, policy);
  
668
  if (!_dbus_list_append (&policy->rules, rule))
669 670 671 672 673
    return FALSE;

  bus_policy_rule_ref (rule);

  return TRUE;
674 675
}

676
dbus_bool_t
677 678 679 680
bus_client_policy_check_can_send (BusClientPolicy *policy,
                                  BusRegistry     *registry,
                                  DBusConnection  *receiver,
                                  DBusMessage     *message)
681
{
682 683
  DBusList *link;
  dbus_bool_t allowed;
684
  
685 686 687 688
  /* policy->rules is in the order the rules appeared
   * in the config file, i.e. last rule that applies wins
   */

689 690
  _dbus_verbose ("  (policy) checking send rules\n");
  
691
  allowed = FALSE;
692
  link = _dbus_list_get_first_link (&policy->rules);
693 694 695 696 697 698 699 700 701 702 703 704
  while (link != NULL)
    {
      BusPolicyRule *rule = link->data;

      link = _dbus_list_get_next_link (&policy->rules, link);
      
      /* Rule is skipped if it specifies a different
       * message name from the message, or a different
       * destination from the message
       */
      
      if (rule->type != BUS_POLICY_RULE_SEND)
705 706 707 708
        {
          _dbus_verbose ("  (policy) skipping non-send rule\n");
          continue;
        }
709 710 711

      if (rule->d.send.message_name != NULL)
        {
712 713
          if (!dbus_message_has_name (message,
                                      rule->d.send.message_name))
714 715 716 717
            {
              _dbus_verbose ("  (policy) skipping rule for different message name\n");
              continue;
            }
718 719 720 721 722 723 724 725 726 727 728
        }

      if (rule->d.send.destination != NULL)
        {
          /* receiver can be NULL for messages that are sent to the
           * message bus itself, we check the strings in that case as
           * built-in services don't have a DBusConnection but messages
           * to them have a destination service name.
           */
          if (receiver == NULL)
            {
729 730 731 732 733 734 735
              if (!dbus_message_has_destination (message,
                                                 rule->d.send.destination))
                {
                  _dbus_verbose ("  (policy) skipping rule because message dest is not %s\n",
                                 rule->d.send.destination);
                  continue;
                }
736 737 738 739 740 741 742 743 744 745
            }
          else
            {
              DBusString str;
              BusService *service;
              
              _dbus_string_init_const (&str, rule->d.send.destination);
              
              service = bus_registry_lookup (registry, &str);
              if (service == NULL)
746 747 748 749 750
                {
                  _dbus_verbose ("  (policy) skipping rule because dest %s doesn't exist\n",
                                 rule->d.send.destination);
                  continue;
                }
751 752

              if (!bus_service_has_owner (service, receiver))
753 754 755 756 757
                {
                  _dbus_verbose ("  (policy) skipping rule because dest %s isn't owned by receiver\n",
                                 rule->d.send.destination);
                  continue;
                }
758 759 760 761 762
            }
        }

      /* Use this rule */
      allowed = rule->allow;
763 764 765

      _dbus_verbose ("  (policy) used rule, allow now = %d\n",
                     allowed);
766
    }
767

768
  return allowed;
769 770 771
}

dbus_bool_t
772 773 774 775
bus_client_policy_check_can_receive (BusClientPolicy *policy,
                                     BusRegistry     *registry,
                                     DBusConnection  *sender,
                                     DBusMessage     *message)
776
{
777 778 779 780 781 782 783
  DBusList *link;
  dbus_bool_t allowed;
  
  /* policy->rules is in the order the rules appeared
   * in the config file, i.e. last rule that applies wins
   */

784 785
  _dbus_verbose ("  (policy) checking receive rules\n");
  
786
  allowed = FALSE;
787
  link = _dbus_list_get_first_link (&policy->rules);
788 789 790 791 792 793 794 795 796 797 798 799
  while (link != NULL)
    {
      BusPolicyRule *rule = link->data;

      link = _dbus_list_get_next_link (&policy->rules, link);
      
      /* Rule is skipped if it specifies a different
       * message name from the message, or a different
       * origin from the message
       */
      
      if (rule->type != BUS_POLICY_RULE_RECEIVE)
800 801 802 803
        {
          _dbus_verbose ("  (policy) skipping non-receive rule\n");
          continue;
        }
804 805 806

      if (rule->d.receive.message_name != NULL)
        {
807 808
          if (!dbus_message_has_name (message,
                                      rule->d.receive.message_name))
809 810 811 812
            {
              _dbus_verbose ("  (policy) skipping rule for different message name\n");
              continue;
            }
813 814 815 816 817 818 819 820 821 822 823
        }

      if (rule->d.receive.origin != NULL)
        {          
          /* sender can be NULL for messages that originate from the
           * message bus itself, we check the strings in that case as
           * built-in services don't have a DBusConnection but will
           * still set the sender on their messages.
           */
          if (sender == NULL)
            {
824 825
              if (!dbus_message_has_sender (message,
                                            rule->d.receive.origin))
826 827 828 829 830
                {
                  _dbus_verbose ("  (policy) skipping rule because message sender is not %s\n",
                                 rule->d.receive.origin);
                  continue;
                }
831 832 833 834 835 836 837 838 839 840 841
            }
          else
            {
              BusService *service;
              DBusString str;

              _dbus_string_init_const (&str, rule->d.receive.origin);
              
              service = bus_registry_lookup (registry, &str);
              
              if (service == NULL)
842 843 844 845 846
                {
                  _dbus_verbose ("  (policy) skipping rule because origin %s doesn't exist\n",
                                 rule->d.receive.origin);
                  continue;
                }
847 848

              if (!bus_service_has_owner (service, sender))
849 850 851 852 853
                {
                  _dbus_verbose ("  (policy) skipping rule because origin %s isn't owned by sender\n",
                                 rule->d.receive.origin);
                  continue;
                }
854 855
            }
        }
856

857 858
      /* Use this rule */
      allowed = rule->allow;
859 860 861

      _dbus_verbose ("  (policy) used rule, allow now = %d\n",
                     allowed);
862
    }
863

864
  return allowed;
865 866 867
}

dbus_bool_t
868 869 870
bus_client_policy_check_can_own (BusClientPolicy  *policy,
                                 DBusConnection   *connection,
                                 const DBusString *service_name)
871
{
872 873 874 875 876 877
  DBusList *link;
  dbus_bool_t allowed;
  
  /* policy->rules is in the order the rules appeared
   * in the config file, i.e. last rule that applies wins
   */
878

879
  allowed = FALSE;
880
  link = _dbus_list_get_first_link (&policy->rules);
881 882 883
  while (link != NULL)
    {
      BusPolicyRule *rule = link->data;
884

885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
      link = _dbus_list_get_next_link (&policy->rules, link);
      
      /* Rule is skipped if it specifies a different service name from
       * the desired one.
       */
      
      if (rule->type != BUS_POLICY_RULE_OWN)
        continue;

      if (rule->d.own.service_name != NULL)
        {
          if (!_dbus_string_equal_c_str (service_name,
                                         rule->d.own.service_name))
            continue;
        }
900

901 902 903 904 905 906
      /* Use this rule */
      allowed = rule->allow;
    }

  return allowed;
}
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921

#ifdef DBUS_BUILD_TESTS

dbus_bool_t
bus_policy_test (const DBusString *test_data_dir)
{
  /* This doesn't do anything for now because I decided to do it in
   * dispatch.c instead by having some of the clients in dispatch.c
   * have particular policies applied to them.
   */
  
  return TRUE;
}

#endif /* DBUS_BUILD_TESTS */