policy.c 38.6 KB
Newer Older
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 9 10 11 12 13 14 15 16 17 18 19
 * 
 * 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
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 22 23
 *
 */

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

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;

48 49 50 51 52 53 54 55 56
  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:
57
      rule->d.send.message_type = DBUS_MESSAGE_TYPE_INVALID;
58 59 60 61 62

      /* allow rules default to TRUE (only requested replies allowed)
       * deny rules default to FALSE (only unrequested replies denied)
       */
      rule->d.send.requested_reply = rule->allow;
63
      break;
64
    case BUS_POLICY_RULE_RECEIVE:
65
      rule->d.receive.message_type = DBUS_MESSAGE_TYPE_INVALID;
66 67 68 69
      /* allow rules default to TRUE (only requested replies allowed)
       * deny rules default to FALSE (only unrequested replies denied)
       */
      rule->d.receive.requested_reply = rule->allow;
70
      break;
71 72
    case BUS_POLICY_RULE_OWN:
      break;
73 74
    default:
      _dbus_assert_not_reached ("invalid rule");
75 76
    }
  
77 78 79
  return rule;
}

80
BusPolicyRule *
81 82 83 84 85
bus_policy_rule_ref (BusPolicyRule *rule)
{
  _dbus_assert (rule->refcount > 0);

  rule->refcount += 1;
86 87

  return rule;
88 89 90 91 92 93 94 95
}

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

  rule->refcount -= 1;
96
  
97 98 99 100
  if (rule->refcount == 0)
    {
      switch (rule->type)
        {
101
        case BUS_POLICY_RULE_SEND:
102
          dbus_free (rule->d.send.path);
103 104 105
          dbus_free (rule->d.send.interface);
          dbus_free (rule->d.send.member);
          dbus_free (rule->d.send.error);
106 107
          dbus_free (rule->d.send.destination);
          break;
108
        case BUS_POLICY_RULE_RECEIVE:
109
          dbus_free (rule->d.receive.path);
110 111 112
          dbus_free (rule->d.receive.interface);
          dbus_free (rule->d.receive.member);
          dbus_free (rule->d.receive.error);
113 114
          dbus_free (rule->d.receive.origin);
          break;
115
        case BUS_POLICY_RULE_OWN:
116 117
          dbus_free (rule->d.own.service_name);
          break;
118
        case BUS_POLICY_RULE_USER:
119
          break;
120 121
        case BUS_POLICY_RULE_GROUP:
          break;
122 123
        default:
          _dbus_assert_not_reached ("invalid rule");
124 125 126 127 128 129 130 131 132 133
        }
      
      dbus_free (rule);
    }
}

struct BusPolicy
{
  int refcount;

134 135 136 137 138 139
  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 */
  DBusList *at_console_true_rules; /**< console user policy rules where at_console="true"*/
  DBusList *at_console_false_rules; /**< console user policy rules where at_console="false"*/
140 141
};

142 143 144 145 146 147 148 149 150 151 152 153 154 155
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;

156 157 158
  if (list == NULL) /* DBusHashTable is on crack */
    return;
  
159 160 161 162 163 164 165
  _dbus_list_foreach (list, free_rule_func, NULL);
  
  _dbus_list_clear (list);

  dbus_free (list);
}

166 167 168 169 170 171 172 173 174 175
BusPolicy*
bus_policy_new (void)
{
  BusPolicy *policy;

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

  policy->refcount = 1;
176
  
177
  policy->rules_by_uid = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
178 179 180 181 182
                                               NULL,
                                               free_rule_list_func);
  if (policy->rules_by_uid == NULL)
    goto failed;

183
  policy->rules_by_gid = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
184 185 186 187
                                               NULL,
                                               free_rule_list_func);
  if (policy->rules_by_gid == NULL)
    goto failed;
188

189
  return policy;
190 191 192 193
  
 failed:
  bus_policy_unref (policy);
  return NULL;
194 195
}

196
BusPolicy *
197 198 199 200 201
bus_policy_ref (BusPolicy *policy)
{
  _dbus_assert (policy->refcount > 0);

  policy->refcount += 1;
202 203

  return policy;
204 205
}

206 207 208 209 210 211 212 213 214
void
bus_policy_unref (BusPolicy *policy)
{
  _dbus_assert (policy->refcount > 0);

  policy->refcount -= 1;

  if (policy->refcount == 0)
    {
215 216 217 218 219
      _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);
220 221 222 223 224 225 226

      _dbus_list_foreach (&policy->at_console_true_rules, free_rule_func, NULL);
      _dbus_list_clear (&policy->at_console_true_rules);

      _dbus_list_foreach (&policy->at_console_false_rules, free_rule_func, NULL);
      _dbus_list_clear (&policy->at_console_false_rules);

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
      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;
269 270 271

        default:
          _dbus_assert_not_reached ("invalid rule");
272 273 274 275 276 277 278 279
        }
    }
  
  return TRUE;
}

BusClientPolicy*
bus_policy_create_client_policy (BusPolicy      *policy,
280 281
                                 DBusConnection *connection,
                                 DBusError      *error)
282 283
{
  BusClientPolicy *client;
284 285
  dbus_uid_t uid;
  dbus_bool_t at_console;
286 287

  _dbus_assert (dbus_connection_get_is_authenticated (connection));
288
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
289 290 291
  
  client = bus_client_policy_new ();
  if (client == NULL)
292
    goto nomem;
293 294 295

  if (!add_list_to_client (&policy->default_rules,
                           client))
296
    goto nomem;
297 298 299 300 301 302

  /* 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)
    {
303
      unsigned long *groups;
304 305 306
      int n_groups;
      int i;
      
307
      if (!bus_connection_get_unix_groups (connection, &groups, &n_groups, error))
308 309 310 311 312
        goto failed;
      
      i = 0;
      while (i < n_groups)
        {
313 314
          DBusList **list;
          
315
          list = _dbus_hash_table_lookup_uintptr (policy->rules_by_gid,
316 317 318 319 320
                                                groups[i]);
          
          if (list != NULL)
            {
              if (!add_list_to_client (list, client))
321 322
                {
                  dbus_free (groups);
323
                  goto nomem;
324
                }
325 326 327 328
            }
          
          ++i;
        }
329 330

      dbus_free (groups);
331
    }
332 333
  
  if (dbus_connection_get_unix_user (connection, &uid))
334
    {
335 336 337 338
      if (_dbus_hash_table_get_n_entries (policy->rules_by_uid) > 0)
        {
          DBusList **list;
          
339
          list = _dbus_hash_table_lookup_uintptr (policy->rules_by_uid,
340 341 342 343 344 345 346 347
                                                uid);
          
          if (list != NULL)
            {
              if (!add_list_to_client (list, client))
                goto nomem;
            }
        }
348

349 350
      /* Add console rules */
      at_console = _dbus_unix_user_is_at_console (uid, error);
351
      
352
      if (at_console)
353
        {
354
          if (!add_list_to_client (&policy->at_console_true_rules, client))
355
            goto nomem;
356
        }
357 358 359 360 361 362 363 364
      else if (dbus_error_is_set (error) == TRUE)
        {
          goto failed;
        }
      else if (!add_list_to_client (&policy->at_console_false_rules, client))
        {
          goto nomem;
        }
365 366
    }

367 368
  if (!add_list_to_client (&policy->mandatory_rules,
                           client))
369
    goto nomem;
370 371 372 373

  bus_client_policy_optimize (client);
  
  return client;
374 375 376

 nomem:
  BUS_SET_OOM (error);
377
 failed:
378 379 380
  _DBUS_ASSERT_ERROR_IS_SET (error);
  if (client)
    bus_client_policy_unref (client);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
  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);
401

402 403
      if (rule->type == BUS_POLICY_RULE_USER)
        {
404 405 406 407 408 409
          _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)
410 411 412 413
            continue;
        }
      else if (rule->type == BUS_POLICY_RULE_GROUP)
        {
414 415
          _dbus_verbose ("List %p group rule gid="DBUS_GID_FORMAT"\n",
                         list, rule->d.group.gid);
416 417 418 419
          
          if (rule->d.group.gid == DBUS_GID_UNSET)
            ;  /* '*' wildcard */
          else
420
            {
421 422 423 424 425 426 427 428 429 430 431 432
              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;
433 434 435 436 437 438 439 440 441 442 443 444
            }
        }
      else
        continue;

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

dbus_bool_t
445 446
bus_policy_allow_unix_user (BusPolicy        *policy,
                            unsigned long     uid)
447 448 449 450 451 452
{
  dbus_bool_t allowed;
  unsigned long *group_ids;
  int n_group_ids;

  /* On OOM or error we always reject the user */
453
  if (!_dbus_unix_groups_from_uid (uid, &group_ids, &n_group_ids))
454 455 456 457 458
    {
      _dbus_verbose ("Did not get any groups for UID %lu\n",
                     uid);
      return FALSE;
    }
John Palmieri's avatar
John Palmieri committed
459

460 461
  /* Default to "user owning bus" can connect */
  allowed = _dbus_unix_user_is_process_owner (uid);
462 463 464 465 466 467 468 469 470 471 472 473 474

  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);

475 476
  _dbus_verbose ("UID %lu allowed = %d\n", uid, allowed);
  
477 478 479
  return allowed;
}

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
/* For now this is never actually called because the default
 * DBusConnection behavior of 'same user that owns the bus can
 * connect' is all it would do. Set the windows user function in
 * connection.c if the config file ever supports doing something
 * interesting here.
 */
dbus_bool_t
bus_policy_allow_windows_user (BusPolicy        *policy,
                               const char       *windows_sid)
{
  /* Windows has no policies here since only the session bus
   * is really used for now, so just checking that the
   * connecting person is the same as the bus owner is fine.
   */
  return _dbus_windows_user_is_process_owner (windows_sid);
}

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
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;
}

521 522


523 524 525 526 527 528
static DBusList**
get_list (DBusHashTable *hash,
          unsigned long  key)
{
  DBusList **list;

529
  list = _dbus_hash_table_lookup_uintptr (hash, key);
530 531 532 533 534 535 536

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

537
      if (!_dbus_hash_table_insert_uintptr (hash, key, list))
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
        {
          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;
}

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
dbus_bool_t
bus_policy_append_console_rule (BusPolicy      *policy,
                                dbus_bool_t     at_console,
                                BusPolicyRule  *rule)
{
  if (at_console)
    {
      if (!_dbus_list_append (&policy->at_console_true_rules, rule))
        return FALSE;
    }
    else
    {
      if (!_dbus_list_append (&policy->at_console_false_rules, rule))
        return FALSE;
    }

  bus_policy_rule_ref (rule);

  return TRUE;

}

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
static dbus_bool_t
append_copy_of_policy_list (DBusList **list,
                            DBusList **to_append)
{
  DBusList *link;
  DBusList *tmp_list;

  tmp_list = NULL;

  /* Preallocate all our links */
  link = _dbus_list_get_first_link (to_append);
  while (link != NULL)
    {
      if (!_dbus_list_append (&tmp_list, link->data))
        {
          _dbus_list_clear (&tmp_list);
          return FALSE;
        }
      
      link = _dbus_list_get_next_link (to_append, link);
    }

  /* Now append them */
  while ((link = _dbus_list_pop_first_link (&tmp_list)))
    {
      bus_policy_rule_ref (link->data);
      _dbus_list_append_link (list, link);
    }

  return TRUE;
}

static dbus_bool_t
merge_id_hash (DBusHashTable *dest,
               DBusHashTable *to_absorb)
{
  DBusHashIter iter;
  
  _dbus_hash_iter_init (to_absorb, &iter);
  while (_dbus_hash_iter_next (&iter))
    {
650
      unsigned long id = _dbus_hash_iter_get_uintptr_key (&iter);
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
      DBusList **list = _dbus_hash_iter_get_value (&iter);
      DBusList **target = get_list (dest, id);

      if (target == NULL)
        return FALSE;

      if (!append_copy_of_policy_list (target, list))
        return FALSE;
    }

  return TRUE;
}

dbus_bool_t
bus_policy_merge (BusPolicy *policy,
                  BusPolicy *to_absorb)
{
668 669 670 671
  /* FIXME Not properly atomic, but as used for configuration files we
   * don't rely on it quite so much.
   */
  
672 673 674 675 676 677 678 679
  if (!append_copy_of_policy_list (&policy->default_rules,
                                   &to_absorb->default_rules))
    return FALSE;
  
  if (!append_copy_of_policy_list (&policy->mandatory_rules,
                                   &to_absorb->mandatory_rules))
    return FALSE;

680 681 682 683 684 685 686 687
  if (!append_copy_of_policy_list (&policy->at_console_true_rules,
                                   &to_absorb->at_console_true_rules))
    return FALSE;

  if (!append_copy_of_policy_list (&policy->at_console_false_rules,
                                   &to_absorb->at_console_false_rules))
    return FALSE;

688 689 690 691 692 693 694 695 696 697 698
  if (!merge_id_hash (policy->rules_by_uid,
                      to_absorb->rules_by_uid))
    return FALSE;
  
  if (!merge_id_hash (policy->rules_by_gid,
                      to_absorb->rules_by_gid))
    return FALSE;

  return TRUE;
}

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
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;
}

720
BusClientPolicy *
721 722 723 724 725
bus_client_policy_ref (BusClientPolicy *policy)
{
  _dbus_assert (policy->refcount > 0);

  policy->refcount += 1;
726 727

  return policy;
728 729
}

730 731 732 733 734 735 736 737 738 739
static void
rule_unref_foreach (void *data,
                    void *user_data)
{
  BusPolicyRule *rule = data;

  bus_policy_rule_unref (rule);
}

void
740
bus_client_policy_unref (BusClientPolicy *policy)
741 742 743 744 745 746 747 748 749 750 751 752
{
  _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);
753

754 755 756 757
      dbus_free (policy);
    }
}

758
static void
759
remove_rules_by_type_up_to (BusClientPolicy   *policy,
760 761 762 763 764
                            BusPolicyRuleType  type,
                            DBusList          *up_to)
{
  DBusList *link;

765
  link = _dbus_list_get_first_link (&policy->rules);
766 767 768 769 770
  while (link != up_to)
    {
      BusPolicyRule *rule = link->data;
      DBusList *next = _dbus_list_get_next_link (&policy->rules, link);

771 772 773 774 775 776
      if (rule->type == type)
        {
          _dbus_list_remove_link (&policy->rules, link);
          bus_policy_rule_unref (rule);
        }
      
777 778 779 780
      link = next;
    }
}

781
void
782
bus_client_policy_optimize (BusClientPolicy *policy)
783 784 785 786 787
{
  DBusList *link;

  /* The idea here is that if we have:
   * 
788 789
   * <allow send_interface="foo.bar"/>
   * <deny send_interface="*"/>
790 791 792 793 794 795 796 797 798 799
   *
   * (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.
   */

800 801 802
  _dbus_verbose ("Optimizing policy with %d rules\n",
                 _dbus_list_get_length (&policy->rules));
  
803
  link = _dbus_list_get_first_link (&policy->rules);
804 805
  while (link != NULL)
    {
806 807
      BusPolicyRule *rule;
      DBusList *next;
808 809
      dbus_bool_t remove_preceding;

810 811 812
      next = _dbus_list_get_next_link (&policy->rules, link);
      rule = link->data;
      
813
      remove_preceding = FALSE;
814 815

      _dbus_assert (rule != NULL);
816 817 818 819 820
      
      switch (rule->type)
        {
        case BUS_POLICY_RULE_SEND:
          remove_preceding =
821 822
            rule->d.send.message_type == DBUS_MESSAGE_TYPE_INVALID &&
            rule->d.send.path == NULL &&
823 824 825
            rule->d.send.interface == NULL &&
            rule->d.send.member == NULL &&
            rule->d.send.error == NULL &&
826 827 828 829
            rule->d.send.destination == NULL;
          break;
        case BUS_POLICY_RULE_RECEIVE:
          remove_preceding =
830 831
            rule->d.receive.message_type == DBUS_MESSAGE_TYPE_INVALID &&
            rule->d.receive.path == NULL &&
832 833 834
            rule->d.receive.interface == NULL &&
            rule->d.receive.member == NULL &&
            rule->d.receive.error == NULL &&
835 836 837 838 839 840
            rule->d.receive.origin == NULL;
          break;
        case BUS_POLICY_RULE_OWN:
          remove_preceding =
            rule->d.own.service_name == NULL;
          break;
841 842

        /* The other rule types don't appear in this list */
843 844
        case BUS_POLICY_RULE_USER:
        case BUS_POLICY_RULE_GROUP:
845
        default:
846 847
          _dbus_assert_not_reached ("invalid rule");
          break;
848
        }
849

850 851 852 853 854 855
      if (remove_preceding)
        remove_rules_by_type_up_to (policy, rule->type,
                                    link);
      
      link = next;
    }
856 857 858 859 860 861

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

dbus_bool_t
862 863
bus_client_policy_append_rule (BusClientPolicy *policy,
                               BusPolicyRule   *rule)
864
{
865 866 867
  _dbus_verbose ("Appending rule %p with type %d to policy %p\n",
                 rule, rule->type, policy);
  
868
  if (!_dbus_list_append (&policy->rules, rule))
869 870 871 872 873
    return FALSE;

  bus_policy_rule_ref (rule);

  return TRUE;
874 875
}

876
dbus_bool_t
877 878
bus_client_policy_check_can_send (BusClientPolicy *policy,
                                  BusRegistry     *registry,
879
                                  dbus_bool_t      requested_reply,
880
                                  DBusConnection  *receiver,
881
                                  DBusMessage     *message,
882 883
                                  dbus_int32_t    *toggles,
                                  dbus_bool_t     *log)
884
{
885 886
  DBusList *link;
  dbus_bool_t allowed;
887
  
888 889 890 891
  /* policy->rules is in the order the rules appeared
   * in the config file, i.e. last rule that applies wins
   */

892
  _dbus_verbose ("  (policy) checking send rules\n");
893
  *toggles = 0;
894
  
895
  allowed = FALSE;
896
  link = _dbus_list_get_first_link (&policy->rules);
897 898 899 900 901 902 903 904 905 906 907 908
  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)
909 910 911 912
        {
          _dbus_verbose ("  (policy) skipping non-send rule\n");
          continue;
        }
913

914 915 916 917 918 919 920 921
      if (rule->d.send.message_type != DBUS_MESSAGE_TYPE_INVALID)
        {
          if (dbus_message_get_type (message) != rule->d.send.message_type)
            {
              _dbus_verbose ("  (policy) skipping rule for different message type\n");
              continue;
            }
        }
922 923 924 925 926 927 928 929

      /* If it's a reply, the requested_reply flag kicks in */
      if (dbus_message_get_reply_serial (message) != 0)
        {
          /* for allow, requested_reply=true means the rule applies
           * only when reply was requested. requested_reply=false means
           * always allow.
           */
930
          if (!requested_reply && rule->allow && rule->d.send.requested_reply && !rule->d.send.eavesdrop)
931
            {
932
              _dbus_verbose ("  (policy) skipping allow rule since it only applies to requested replies and does not allow eavesdropping\n");
933 934 935 936 937 938 939 940 941 942 943 944 945
              continue;
            }

          /* for deny, requested_reply=false means the rule applies only
           * when the reply was not requested. requested_reply=true means the
           * rule always applies.
           */
          if (requested_reply && !rule->allow && !rule->d.send.requested_reply)
            {
              _dbus_verbose ("  (policy) skipping deny rule since it only applies to unrequested replies\n");
              continue;
            }
        }
946 947 948 949 950 951 952 953 954 955 956 957
      
      if (rule->d.send.path != NULL)
        {
          if (dbus_message_get_path (message) != NULL &&
              strcmp (dbus_message_get_path (message),
                      rule->d.send.path) != 0)
            {
              _dbus_verbose ("  (policy) skipping rule for different path\n");
              continue;
            }
        }
      
958
      if (rule->d.send.interface != NULL)
959
        {
960 961 962 963 964 965 966 967 968 969 970 971 972
          /* The interface is optional in messages. For allow rules, if the message
           * has no interface we want to skip the rule (and thus not allow);
           * for deny rules, if the message has no interface we want to use the
           * rule (and thus deny).
           */
          dbus_bool_t no_interface;

          no_interface = dbus_message_get_interface (message) == NULL;
          
          if ((no_interface && rule->allow) ||
              (!no_interface && 
               strcmp (dbus_message_get_interface (message),
                       rule->d.send.interface) != 0))
973
            {
974
              _dbus_verbose ("  (policy) skipping rule for different interface\n");
975 976
              continue;
            }
977
        }
978 979

      if (rule->d.send.member != NULL)
980
        {
981
          if (dbus_message_get_member (message) != NULL &&
982 983
              strcmp (dbus_message_get_member (message),
                      rule->d.send.member) != 0)
984 985 986 987 988
            {
              _dbus_verbose ("  (policy) skipping rule for different member\n");
              continue;
            }
        }
989 990

      if (rule->d.send.error != NULL)
991
        {
992
          if (dbus_message_get_error_name (message) != NULL &&
993 994
              strcmp (dbus_message_get_error_name (message),
                      rule->d.send.error) != 0)
995 996 997 998 999
            {
              _dbus_verbose ("  (policy) skipping rule for different error name\n");
              continue;
            }
        }
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

      if (rule->d.send.broadcast != BUS_POLICY_TRISTATE_ANY)
        {
          if (dbus_message_get_destination (message) == NULL &&
              dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
            {
              /* it's a broadcast */
              if (rule->d.send.broadcast == BUS_POLICY_TRISTATE_FALSE)
                {
                  _dbus_verbose ("  (policy) skipping rule because message is a broadcast\n");
                  continue;
                }
            }
          /* else it isn't a broadcast: there is some destination */
          else if (rule->d.send.broadcast == BUS_POLICY_TRISTATE_TRUE)
            {
              _dbus_verbose ("  (policy) skipping rule because message is not a broadcast\n");
              continue;
            }
        }

1021 1022 1023 1024 1025 1026
      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.
1027 1028 1029 1030 1031
           *
           * Similarly, receiver can be NULL when we're deciding whether
           * activation should be allowed; we make the authorization decision
           * on the assumption that the activated service will have the
           * requested name and no others.
1032 1033 1034
           */
          if (receiver == NULL)
            {
1035 1036 1037 1038 1039 1040 1041
              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;
                }
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
            }
          else
            {
              DBusString str;
              BusService *service;
              
              _dbus_string_init_const (&str, rule->d.send.destination);
              
              service = bus_registry_lookup (registry, &str);
              if (service == NULL)
1052 1053 1054 1055 1056
                {
                  _dbus_verbose ("  (policy) skipping rule because dest %s doesn't exist\n",
                                 rule->d.send.destination);
                  continue;
                }
1057 1058

              if (!bus_service_has_owner (service, receiver))
1059 1060 1061 1062 1063
                {
                  _dbus_verbose ("  (policy) skipping rule because dest %s isn't owned by receiver\n",
                                 rule->d.send.destination);
                  continue;
                }
1064 1065 1066
            }
        }

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
      if (rule->d.send.min_fds > 0 ||
          rule->d.send.max_fds < DBUS_MAXIMUM_MESSAGE_UNIX_FDS)
        {
          unsigned int n_fds = _dbus_message_get_n_unix_fds (message);

          if (n_fds < rule->d.send.min_fds || n_fds > rule->d.send.max_fds)
            {
              _dbus_verbose ("  (policy) skipping rule because message has %u fds "
                             "and that is outside range [%u,%u]",
                             n_fds, rule->d.send.min_fds, rule->d.send.max_fds);
              continue;
            }
        }

1081 1082
      /* Use this rule */
      allowed = rule->allow;
1083
      *log = rule->d.send.log;
1084
      (*toggles)++;
1085 1086 1087

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

1090
  return allowed;
1091 1092
}

1093 1094 1095
/* See docs on what the args mean on bus_context_check_security_policy()
 * comment
 */
1096
dbus_bool_t
1097 1098
bus_client_policy_check_can_receive (BusClientPolicy *policy,
                                     BusRegistry     *registry,
1099
                                     dbus_bool_t      requested_reply,
1100
                                     DBusConnection  *sender,
1101 1102
                                     DBusConnection  *addressed_recipient,
                                     DBusConnection  *proposed_recipient,
1103 1104
                                     DBusMessage     *message,
                                     dbus_int32_t    *toggles)
1105
{
1106 1107
  DBusList *link;
  dbus_bool_t allowed;
1108 1109 1110
  dbus_bool_t eavesdropping;

  eavesdropping =
1111 1112
    addressed_recipient != proposed_recipient &&
    dbus_message_get_destination (message) != NULL;
1113 1114 1115 1116 1117
  
  /* policy->rules is in the order the rules appeared
   * in the config file, i.e. last rule that applies wins
   */

1118
  _dbus_verbose ("  (policy) checking receive rules, eavesdropping = %d\n", eavesdropping);
1119
  *toggles = 0;
1120
  
1121
  allowed = FALSE;
1122
  link = _dbus_list_get_first_link (&policy->rules);
1123 1124 1125 1126
  while (link != NULL)
    {
      BusPolicyRule *rule = link->data;

1127
      link = _dbus_list_get_next_link (&policy->rules, link);      
1128 1129
      
      if (rule->type != BUS_POLICY_RULE_RECEIVE)
1130 1131 1132 1133
        {
          _dbus_verbose ("  (policy) skipping non-receive rule\n");
          continue;
        }
1134 1135 1136 1137 1138 1139 1140 1141 1142

      if (rule->d.receive.message_type != DBUS_MESSAGE_TYPE_INVALID)
        {
          if (dbus_message_get_type (message) != rule->d.receive.message_type)
            {
              _dbus_verbose ("  (policy) skipping rule for different message type\n");
              continue;
            }
        }
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160

      /* for allow, eavesdrop=false means the rule doesn't apply when
       * eavesdropping. eavesdrop=true means always allow.
       */
      if (eavesdropping && rule->allow && !rule->d.receive.eavesdrop)
        {
          _dbus_verbose ("  (policy) skipping allow rule since it doesn't apply to eavesdropping\n");
          continue;
        }

      /* for deny, eavesdrop=true means the rule applies only when
       * eavesdropping; eavesdrop=false means always deny.
       */
      if (!eavesdropping && !rule->allow && rule->d.receive.eavesdrop)
        {
          _dbus_verbose ("  (policy) skipping deny rule since it only applies to eavesdropping\n");
          continue;
        }
1161 1162 1163 1164 1165 1166 1167 1168

      /* If it's a reply, the requested_reply flag kicks in */
      if (dbus_message_get_reply_serial (message) != 0)
        {
          /* for allow, requested_reply=true means the rule applies
           * only when reply was requested. requested_reply=false means
           * always allow.
           */
1169
          if (!requested_reply && rule->allow && rule->d.receive.requested_reply && !rule->d.receive.eavesdrop)
1170
            {
1171
              _dbus_verbose ("  (policy) skipping allow rule since it only applies to requested replies and does not allow eavesdropping\n");
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
              continue;
            }

          /* for deny, requested_reply=false means the rule applies only
           * when the reply was not requested. requested_reply=true means the
           * rule always applies.
           */
          if (requested_reply && !rule->allow && !rule->d.receive.requested_reply)
            {
              _dbus_verbose ("  (policy) skipping deny rule since it only applies to unrequested replies\n");
              continue;
            }
        }
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
      
      if (rule->d.receive.path != NULL)
        {
          if (dbus_message_get_path (message) != NULL &&
              strcmp (dbus_message_get_path (message),
                      rule->d.receive.path) != 0)
            {
              _dbus_verbose ("  (policy) skipping rule for different path\n");
              continue;
            }
        }
1196
      
1197
      if (rule->d.receive.interface != NULL)
1198
        {
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
          /* The interface is optional in messages. For allow rules, if the message
           * has no interface we want to skip the rule (and thus not allow);
           * for deny rules, if the message has no interface we want to use the
           * rule (and thus deny).
           */
          dbus_bool_t no_interface;

          no_interface = dbus_message_get_interface (message) == NULL;
          
          if ((no_interface && rule->allow) ||
              (!no_interface &&
               strcmp (dbus_message_get_interface (message),
                       rule->d.receive.interface) != 0))
1212
            {
1213
              _dbus_verbose ("  (policy) skipping rule for different interface\n");
1214 1215
              continue;
            }
1216 1217 1218
        }      

      if (rule->d.receive.member != NULL)
1219
        {
1220
          if (dbus_message_get_member (message) != NULL &&
1221 1222
              strcmp (dbus_message_get_member (message),
                      rule->d.receive.member) != 0)
1223 1224 1225 1226 1227
            {
              _dbus_verbose ("  (policy) skipping rule for different member\n");
              continue;
            }
        }
1228 1229

      if (rule->d.receive.error != NULL)
1230
        {
1231
          if (dbus_message_get_error_name (message) != NULL &&
1232 1233
              strcmp (dbus_message_get_error_name (message),
                      rule->d.receive.error) != 0)
1234 1235 1236 1237 1238 1239
            {
              _dbus_verbose ("  (policy) skipping rule for different error name\n");
              continue;
            }
        }
      
1240 1241 1242 1243 1244 1245 1246 1247 1248
      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)
            {
1249 1250
              if (!dbus_message_has_sender (message,
                                            rule->d.receive.origin))
1251 1252 1253 1254 1255
                {
                  _dbus_verbose ("  (policy) skipping rule because message sender is not %s\n",
                                 rule->d.receive.origin);
                  continue;
                }
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
            }
          else
            {
              BusService *service;
              DBusString str;

              _dbus_string_init_const (&str, rule->d.receive.origin);
              
              service = bus_registry_lookup (registry, &str);
              
              if (service == NULL)
1267 1268 1269 1270 1271
                {
                  _dbus_verbose ("  (policy) skipping rule because origin %s doesn't exist\n",
                                 rule->d.receive.origin);
                  continue;
                }
1272 1273

              if (!bus_service_has_owner (service, sender))
1274 1275 1276 1277 1278
                {
                  _dbus_verbose ("  (policy) skipping rule because origin %s isn't owned by sender\n",
                                 rule->d.receive.origin);
                  continue;
                }
1279 1280
            }
        }
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296

      if (rule->d.receive.min_fds > 0 ||
          rule->d.receive.max_fds < DBUS_MAXIMUM_MESSAGE_UNIX_FDS)
        {
          unsigned int n_fds = _dbus_message_get_n_unix_fds (message);

          if (n_fds < rule->d.receive.min_fds || n_fds > rule->d.receive.max_fds)
            {
              _dbus_verbose ("  (policy) skipping rule because message has %u fds "
                             "and that is outside range [%u,%u]",
                             n_fds, rule->d.receive.min_fds,
                             rule->d.receive.max_fds);
              continue;
            }
        }

1297 1298
      /* Use this rule */
      allowed = rule->allow;
1299
      (*toggles)++;
1300 1301 1302

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

1305
  return allowed;
1306 1307
}

1308 1309 1310 1311 1312


static dbus_bool_t
bus_rules_check_can_own (DBusList *rules,
                         const DBusString *service_name)
1313
{
1314 1315 1316
  DBusList *link;
  dbus_bool_t allowed;
  
1317
  /* rules is in the order the rules appeared
1318 1319
   * in the config file, i.e. last rule that applies wins
   */
1320

1321
  allowed = FALSE;
1322
  link = _dbus_list_get_first_link (&rules);
1323 1324 1325
  while (link != NULL)
    {
      BusPolicyRule *rule = link->data;
1326

1327
      link = _dbus_list_get_next_link (&rules, link);
1328 1329 1330 1331 1332 1333 1334 1335
      
      /* Rule is skipped if it specifies a different service name from
       * the desired one.
       */
      
      if (rule->type != BUS_POLICY_RULE_OWN)
        continue;

1336
      if (!rule->d.own.prefix && rule->d.own.service_name != NULL)
1337 1338 1339 1340 1341
        {
          if (!_dbus_string_equal_c_str (service_name,
                                         rule->d.own.service_name))
            continue;
        }
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
      else if (rule->d.own.prefix)
        {
          const char *data;
          char next_char;
          if (!_dbus_string_starts_with_c_str (service_name,
                                               rule->d.own.service_name))
            continue;

          data = _dbus_string_get_const_data (service_name);
          next_char = data[strlen (rule->d.own.service_name)];
          if (next_char != '\0' && next_char != '.')
            continue;
        }
1355

1356 1357 1358 1359 1360 1361
      /* Use this rule */
      allowed = rule->allow;
    }

  return allowed;
}
1362 1363 1364 1365 1366 1367 1368 1369

dbus_bool_t
bus_client_policy_check_can_own (BusClientPolicy  *policy,
                                 const DBusString *service_name)
{
  return bus_rules_check_can_own (policy->rules, service_name);
}

1370
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
1371 1372 1373 1374 1375 1376
dbus_bool_t
bus_policy_check_can_own (BusPolicy  *policy,
                          const DBusString *service_name)
{
  return bus_rules_check_can_own (policy->default_rules, service_name);
}
1377
#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
1378