config-parser.c 96.5 KB
Newer Older
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 3
/* config-parser.c  XML-library-agnostic configuration file parser
 *
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
 * 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.
17
 *
18 19
 * 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 "config-parser-common.h"
26
#include "config-parser.h"
27
#include "test.h"
28
#include "utils.h"
29
#include "policy.h"
30
#include "selinux.h"
31 32
#include <dbus/dbus-list.h>
#include <dbus/dbus-internals.h>
33
#include <dbus/dbus-sysdeps.h>
34 35
#include <string.h>

36 37 38 39 40 41 42 43 44
typedef enum
{
  /* we ignore policies for unknown groups/users */
  POLICY_IGNORED,

  /* non-ignored */
  POLICY_DEFAULT,
  POLICY_MANDATORY,
  POLICY_USER,
45 46
  POLICY_GROUP,
  POLICY_CONSOLE
47 48
} PolicyType;

49 50 51 52
typedef struct
{
  ElementType type;

53 54
  unsigned int had_content : 1;

55 56 57 58
  union
  {
    struct
    {
59
      unsigned int ignore_missing : 1;
60
      unsigned int if_selinux_enabled : 1;
61
      unsigned int selinux_root_relative : 1;
62 63 64 65
    } include;

    struct
    {
66
      PolicyType type;
67
      unsigned long gid_uid_or_at_console;      
68 69
    } policy;

70 71 72 73 74 75
    struct
    {
      char *name;
      long value;
    } limit;
    
76
  } d;
77

78 79
} Element;

80 81 82
/**
 * Parser for bus configuration file. 
 */
83 84
struct BusConfigParser
{
85
  int refcount;        /**< Reference count */
86

87 88
  DBusString basedir;  /**< Directory we resolve paths relative to */
  
89
  DBusList *stack;     /**< stack of Element */
90

91 92
  char *user;          /**< user to run as */

93 94
  char *servicehelper; /**< location of the setuid helper */

95 96
  char *bus_type;          /**< Message bus type */
  
97
  DBusList *listen_on; /**< List of addresses to listen to */
98 99

  DBusList *mechanisms; /**< Auth mechanisms */
100

101
  DBusList *service_dirs; /**< Directories to look for session services in */
102

103 104
  DBusList *conf_dirs;   /**< Directories to look for policy configuration in */

105
  BusPolicy *policy;     /**< Security policy */
106 107

  BusLimits limits;      /**< Limits */
108 109 110

  char *pidfile;         /**< PID file */

111 112
  DBusList *included_files;  /**< Included files stack */

113
  DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
114

115
  unsigned int fork : 1; /**< TRUE to fork into daemon mode */
116

117
  unsigned int syslog : 1; /**< TRUE to enable syslog */
118
  unsigned int keep_umask : 1; /**< TRUE to keep original umask when forking */
119

120
  unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
121 122

  unsigned int allow_anonymous : 1; /**< TRUE to allow anonymous connections */
123 124
};

125 126 127 128 129 130
static Element*
push_element (BusConfigParser *parser,
              ElementType      type)
{
  Element *e;

131 132
  _dbus_assert (type != ELEMENT_NONE);
  
133 134 135
  e = dbus_new0 (Element, 1);
  if (e == NULL)
    return NULL;
136 137 138 139 140 141

  if (!_dbus_list_append (&parser->stack, e))
    {
      dbus_free (e);
      return NULL;
    }
142 143 144 145 146 147
  
  e->type = type;

  return e;
}

148 149 150
static void
element_free (Element *e)
{
151 152 153
  if (e->type == ELEMENT_LIMIT)
    dbus_free (e->d.limit.name);
  
154 155 156
  dbus_free (e);
}

157 158 159 160 161 162
static void
pop_element (BusConfigParser *parser)
{
  Element *e;

  e = _dbus_list_pop_last (&parser->stack);
163 164 165
  
  element_free (e);
}
166

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
static Element*
peek_element (BusConfigParser *parser)
{
  Element *e;

  e = _dbus_list_get_last (&parser->stack);

  return e;
}

static ElementType
top_element_type (BusConfigParser *parser)
{
  Element *e;

  e = _dbus_list_get_last (&parser->stack);

  if (e)
    return e->type;
  else
    return ELEMENT_NONE;
}

190 191 192 193 194
static dbus_bool_t
merge_service_context_hash (DBusHashTable *dest,
			    DBusHashTable *from)
{
  DBusHashIter iter;
195 196 197 198 199 200
  char *service_copy;
  char *context_copy;

  service_copy = NULL;
  context_copy = NULL;

201 202 203 204 205 206 207 208
  _dbus_hash_iter_init (from, &iter);
  while (_dbus_hash_iter_next (&iter))
    {
      const char *service = _dbus_hash_iter_get_string_key (&iter);
      const char *context = _dbus_hash_iter_get_value (&iter);

      service_copy = _dbus_strdup (service);
      if (service_copy == NULL)
209
        goto fail;
210 211
      context_copy = _dbus_strdup (context);
      if (context_copy == NULL)
212
        goto fail; 
213 214
      
      if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
215 216 217 218
        goto fail;

      service_copy = NULL;
      context_copy = NULL;    
219 220 221
    }

  return TRUE;
222 223 224 225 226 227 228 229 230

 fail:
  if (service_copy)
    dbus_free (service_copy);

  if (context_copy)
    dbus_free (context_copy);

  return FALSE;
231 232
}

233 234 235 236 237 238 239 240
static dbus_bool_t
service_dirs_find_dir (DBusList **service_dirs,
                       const char *dir)
{
  DBusList *link;

  _dbus_assert (dir != NULL);

241
  for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
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 269 270 271 272 273 274 275 276 277 278
    {
      const char *link_dir;
      
      link_dir = (const char *)link->data;
      if (strcmp (dir, link_dir) == 0)
        return TRUE;
    }

  return FALSE;
}

static dbus_bool_t
service_dirs_append_unique_or_free (DBusList **service_dirs,
                                    char *dir)
{
  if (!service_dirs_find_dir (service_dirs, dir))
    return _dbus_list_append (service_dirs, dir);  

  dbus_free (dir);
  return TRUE;
}

static void 
service_dirs_append_link_unique_or_free (DBusList **service_dirs,
                                         DBusList *dir_link)
{
  if (!service_dirs_find_dir (service_dirs, dir_link->data))
    {
      _dbus_list_append_link (service_dirs, dir_link);
    }
  else
    {
      dbus_free (dir_link->data);
      _dbus_list_free_link (dir_link);
    }
}

279 280 281 282 283 284 285
static dbus_bool_t
merge_included (BusConfigParser *parser,
                BusConfigParser *included,
                DBusError       *error)
{
  DBusList *link;

286 287 288 289 290 291
  if (!bus_policy_merge (parser->policy,
                         included->policy))
    {
      BUS_SET_OOM (error);
      return FALSE;
    }
292

293 294
  if (!merge_service_context_hash (parser->service_context_table,
				   included->service_context_table))
295 296 297 298
    {
      BUS_SET_OOM (error);
      return FALSE;
    }
299
  
300 301 302 303 304 305 306
  if (included->user != NULL)
    {
      dbus_free (parser->user);
      parser->user = included->user;
      included->user = NULL;
    }

307 308 309 310 311 312 313
  if (included->bus_type != NULL)
    {
      dbus_free (parser->bus_type);
      parser->bus_type = included->bus_type;
      included->bus_type = NULL;
    }
  
314 315
  if (included->fork)
    parser->fork = TRUE;
316

317 318 319
  if (included->keep_umask)
    parser->keep_umask = TRUE;

320 321 322 323 324 325
  if (included->pidfile != NULL)
    {
      dbus_free (parser->pidfile);
      parser->pidfile = included->pidfile;
      included->pidfile = NULL;
    }
326
  
327 328 329
  while ((link = _dbus_list_pop_first_link (&included->listen_on)))
    _dbus_list_append_link (&parser->listen_on, link);

330 331
  while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
    _dbus_list_append_link (&parser->mechanisms, link);
332 333

  while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
334
    service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
335 336 337

  while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
    _dbus_list_append_link (&parser->conf_dirs, link);
338
  
339
  return TRUE;
340 341
}

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
static dbus_bool_t
seen_include (BusConfigParser  *parser,
	      const DBusString *file)
{
  DBusList *iter;

  iter = parser->included_files;
  while (iter != NULL)
    {
      if (! strcmp (_dbus_string_get_const_data (file), iter->data))
	return TRUE;

      iter = _dbus_list_get_next_link (&parser->included_files, iter);
    }

  return FALSE;
}

360
BusConfigParser*
361 362 363
bus_config_parser_new (const DBusString      *basedir,
                       dbus_bool_t            is_toplevel,
                       const BusConfigParser *parent)
364 365 366 367 368 369 370
{
  BusConfigParser *parser;

  parser = dbus_new0 (BusConfigParser, 1);
  if (parser == NULL)
    return NULL;

371 372
  parser->is_toplevel = !!is_toplevel;
  
373 374 375 376 377 378
  if (!_dbus_string_init (&parser->basedir))
    {
      dbus_free (parser);
      return NULL;
    }

379
  if (((parser->policy = bus_policy_new ()) == NULL) ||
380
      !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
381 382 383
      ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
							      dbus_free,
							      dbus_free)) == NULL))
384
    {
385 386 387
      if (parser->policy)
        bus_policy_unref (parser->policy);
      
388
      _dbus_string_free (&parser->basedir);
389

390 391 392
      dbus_free (parser);
      return NULL;
    }
393

394 395 396 397
  if (parent != NULL)
    {
      /* Initialize the parser's limits from the parent. */
      parser->limits = parent->limits;
398 399 400 401

      /* Use the parent's list of included_files to avoid
	 circular inclusions. */
      parser->included_files = parent->included_files;
402 403 404
    }
  else
    {
405

406
      /* Make up some numbers! woot! */
407 408
      parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 127;
      parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 127;
409
      parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
410 411 412 413 414 415 416 417 418

      /* We set relatively conservative values here since due to the
      way SCM_RIGHTS works we need to preallocate an array for the
      maximum number of file descriptors we can receive. Picking a
      high value here thus translates directly to more memory
      allocation. */
      parser->limits.max_incoming_unix_fds = 1024*4;
      parser->limits.max_outgoing_unix_fds = 1024*4;
      parser->limits.max_message_unix_fds = 1024;
419 420 421 422 423 424
      
      /* Making this long means the user has to wait longer for an error
       * message if something screws up, but making it too short means
       * they might see a false failure.
       */
      parser->limits.activation_timeout = 25000; /* 25 seconds */
425

426 427 428 429 430 431
      /* Making this long risks making a DOS attack easier, but too short
       * and legitimate auth will fail.  If interactive auth (ask user for
       * password) is allowed, then potentially it has to be quite long.
       */
      parser->limits.auth_timeout = 30000; /* 30 seconds */
      
432 433
      parser->limits.max_incomplete_connections = 64;
      parser->limits.max_connections_per_user = 256;
434 435 436 437 438
      
      /* Note that max_completed_connections / max_connections_per_user
       * is the number of users that would have to work together to
       * DOS all the other users.
       */
439
      parser->limits.max_completed_connections = 2048;
440
      
441 442
      parser->limits.max_pending_activations = 512;
      parser->limits.max_services_per_connection = 512;
443 444 445 446 447 448 449 450 451

      /* For this one, keep in mind that it isn't only the memory used
       * by the match rules, but slowdown from linearly walking a big
       * list of them. A client adding more than this is almost
       * certainly a bad idea for that reason, and should change to a
       * smaller number of wider-net match rules - getting every last
       * message to the bus is probably better than having a thousand
       * match rules.
       */
452
      parser->limits.max_match_rules_per_connection = 512;
453
      
454
      parser->limits.reply_timeout = -1; /* never */
455 456 457 458 459

      /* this is effectively a limit on message queue size for messages
       * that require a reply
       */
      parser->limits.max_replies_per_connection = 1024*8;
460 461
    }
      
462
  parser->refcount = 1;
463
      
464 465 466
  return parser;
}

467
BusConfigParser *
468 469 470 471 472
bus_config_parser_ref (BusConfigParser *parser)
{
  _dbus_assert (parser->refcount > 0);

  parser->refcount += 1;
473 474

  return parser;
475 476 477 478 479 480 481 482 483 484 485
}

void
bus_config_parser_unref (BusConfigParser *parser)
{
  _dbus_assert (parser->refcount > 0);

  parser->refcount -= 1;

  if (parser->refcount == 0)
    {
486 487
      while (parser->stack != NULL)
        pop_element (parser);
488

489
      dbus_free (parser->user);
490
      dbus_free (parser->servicehelper);
491
      dbus_free (parser->bus_type);
492
      dbus_free (parser->pidfile);
493
      
494 495 496 497 498 499
      _dbus_list_foreach (&parser->listen_on,
                          (DBusForeachFunction) dbus_free,
                          NULL);

      _dbus_list_clear (&parser->listen_on);

500 501 502 503 504
      _dbus_list_foreach (&parser->service_dirs,
                          (DBusForeachFunction) dbus_free,
                          NULL);

      _dbus_list_clear (&parser->service_dirs);
505

506 507 508 509 510 511
      _dbus_list_foreach (&parser->conf_dirs,
                          (DBusForeachFunction) dbus_free,
                          NULL);

      _dbus_list_clear (&parser->conf_dirs);

512 513 514 515 516 517
      _dbus_list_foreach (&parser->mechanisms,
                          (DBusForeachFunction) dbus_free,
                          NULL);

      _dbus_list_clear (&parser->mechanisms);
      
518
      _dbus_string_free (&parser->basedir);
519 520 521

      if (parser->policy)
        bus_policy_unref (parser->policy);
522

523 524
      if (parser->service_context_table)
        _dbus_hash_table_unref (parser->service_context_table);
525
      
526 527 528 529 530 531 532 533 534
      dbus_free (parser);
    }
}

dbus_bool_t
bus_config_parser_check_doctype (BusConfigParser   *parser,
                                 const char        *doctype,
                                 DBusError         *error)
{
535
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
536

537 538 539 540
  if (strcmp (doctype, "busconfig") != 0)
    {
      dbus_set_error (error,
                      DBUS_ERROR_FAILED,
541
                      "Configuration file has the wrong document type %s",
542 543 544 545 546 547 548
                      doctype);
      return FALSE;
    }
  else
    return TRUE;
}

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 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 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 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
typedef struct
{
  const char  *name;
  const char **retloc;
} LocateAttr;

static dbus_bool_t
locate_attributes (BusConfigParser  *parser,
                   const char       *element_name,
                   const char      **attribute_names,
                   const char      **attribute_values,
                   DBusError        *error,
                   const char       *first_attribute_name,
                   const char      **first_attribute_retloc,
                   ...)
{
  va_list args;
  const char *name;
  const char **retloc;
  int n_attrs;
#define MAX_ATTRS 24
  LocateAttr attrs[MAX_ATTRS];
  dbus_bool_t retval;
  int i;

  _dbus_assert (first_attribute_name != NULL);
  _dbus_assert (first_attribute_retloc != NULL);

  retval = TRUE;

  n_attrs = 1;
  attrs[0].name = first_attribute_name;
  attrs[0].retloc = first_attribute_retloc;
  *first_attribute_retloc = NULL;

  va_start (args, first_attribute_retloc);

  name = va_arg (args, const char*);
  retloc = va_arg (args, const char**);

  while (name != NULL)
    {
      _dbus_assert (retloc != NULL);
      _dbus_assert (n_attrs < MAX_ATTRS);

      attrs[n_attrs].name = name;
      attrs[n_attrs].retloc = retloc;
      n_attrs += 1;
      *retloc = NULL;

      name = va_arg (args, const char*);
      retloc = va_arg (args, const char**);
    }

  va_end (args);

  i = 0;
  while (attribute_names[i])
    {
      int j;
      dbus_bool_t found;
      
      found = FALSE;
      j = 0;
      while (j < n_attrs)
        {
          if (strcmp (attrs[j].name, attribute_names[i]) == 0)
            {
              retloc = attrs[j].retloc;

              if (*retloc != NULL)
                {
                  dbus_set_error (error, DBUS_ERROR_FAILED,
                                  "Attribute \"%s\" repeated twice on the same <%s> element",
                                  attrs[j].name, element_name);
                  retval = FALSE;
                  goto out;
                }

              *retloc = attribute_values[i];
              found = TRUE;
            }

          ++j;
        }

      if (!found)
        {
          dbus_set_error (error, DBUS_ERROR_FAILED,
                          "Attribute \"%s\" is invalid on <%s> element in this context",
                          attribute_names[i], element_name);
          retval = FALSE;
          goto out;
        }

      ++i;
    }

 out:
  return retval;
}

static dbus_bool_t
check_no_attributes (BusConfigParser  *parser,
                     const char       *element_name,
                     const char      **attribute_names,
                     const char      **attribute_values,
                     DBusError        *error)
{
  if (attribute_names[0] != NULL)
    {
      dbus_set_error (error, DBUS_ERROR_FAILED,
                      "Attribute \"%s\" is invalid on <%s> element in this context",
                      attribute_names[0], element_name);
      return FALSE;
    }

  return TRUE;
}

static dbus_bool_t
start_busconfig_child (BusConfigParser   *parser,
                       const char        *element_name,
                       const char       **attribute_names,
                       const char       **attribute_values,
                       DBusError         *error)
{
676 677 678 679 680
  ElementType element_type;

  element_type = bus_config_parser_element_name_to_type (element_name);

  if (element_type == ELEMENT_USER)
681 682 683 684 685 686
    {
      if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
        return FALSE;

      if (push_element (parser, ELEMENT_USER) == NULL)
        {
687 688 689 690 691 692
          BUS_SET_OOM (error);
          return FALSE;
        }

      return TRUE;
    }
693
  else if (element_type == ELEMENT_CONFIGTYPE)
694 695 696 697
    {
      if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
        return FALSE;

698
      if (push_element (parser, ELEMENT_CONFIGTYPE) == NULL)
699 700
        {
          BUS_SET_OOM (error);
701 702 703
          return FALSE;
        }

704 705
      return TRUE;
    }
706
  else if (element_type == ELEMENT_FORK)
707 708 709 710 711 712
    {
      if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
        return FALSE;

      if (push_element (parser, ELEMENT_FORK) == NULL)
        {
713
          BUS_SET_OOM (error);
714 715 716 717 718
          return FALSE;
        }

      parser->fork = TRUE;
      
719 720
      return TRUE;
    }
721
  else if (element_type == ELEMENT_SYSLOG)
722
    {
723
      if (!check_no_attributes (parser, "syslog", attribute_names, attribute_values, error))
724 725
        return FALSE;

726
      if (push_element (parser, ELEMENT_SYSLOG) == NULL)
727 728 729 730
        {
          BUS_SET_OOM (error);
          return FALSE;
        }
731
      
732
      parser->syslog = TRUE;
733
      
734 735
      return TRUE;
    }
736
  else if (element_type == ELEMENT_KEEP_UMASK)
737
    {
738
      if (!check_no_attributes (parser, "keep_umask", attribute_names, attribute_values, error))
739 740
        return FALSE;

741
      if (push_element (parser, ELEMENT_KEEP_UMASK) == NULL)
742 743 744 745 746
        {
          BUS_SET_OOM (error);
          return FALSE;
        }

747
      parser->keep_umask = TRUE;
748
      
749 750
      return TRUE;
    }
751
  else if (element_type == ELEMENT_PIDFILE)
752 753 754 755 756 757 758 759 760 761
    {
      if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
        return FALSE;

      if (push_element (parser, ELEMENT_PIDFILE) == NULL)
        {
          BUS_SET_OOM (error);
          return FALSE;
        }

762 763
      return TRUE;
    }
764
  else if (element_type == ELEMENT_LISTEN)
765 766 767 768 769 770
    {
      if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
        return FALSE;

      if (push_element (parser, ELEMENT_LISTEN) == NULL)
        {
771
          BUS_SET_OOM (error);
772 773 774
          return FALSE;
        }

775 776
      return TRUE;
    }
777
  else if (element_type == ELEMENT_AUTH)
778 779 780 781 782 783
    {
      if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
        return FALSE;

      if (push_element (parser, ELEMENT_AUTH) == NULL)
        {
784
          BUS_SET_OOM (error);
785 786
          return FALSE;
        }
787
      
788 789
      return TRUE;
    }
790 791 792 793 794 795 796 797 798 799 800 801 802 803
  else if (element_type == ELEMENT_SERVICEHELPER)
    {
      if (!check_no_attributes (parser, "servicehelper", attribute_names, attribute_values, error))
        return FALSE;

      if (push_element (parser, ELEMENT_SERVICEHELPER) == NULL)
        {
          BUS_SET_OOM (error);
          return FALSE;
        }

      return TRUE;
    }
  else if (element_type == ELEMENT_INCLUDEDIR)
804 805 806 807 808 809
    {
      if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
        return FALSE;

      if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
        {
810
          BUS_SET_OOM (error);
811 812 813
          return FALSE;
        }

814 815
      return TRUE;
    }
816
  else if (element_type == ELEMENT_STANDARD_SESSION_SERVICEDIRS)
817
    {
818 819 820 821
      DBusList *link;
      DBusList *dirs;
      dirs = NULL;

822 823 824 825 826 827 828 829 830
      if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
        return FALSE;

      if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
        {
          BUS_SET_OOM (error);
          return FALSE;
        }

831 832 833 834 835 836 837 838 839
      if (!_dbus_get_standard_session_servicedirs (&dirs))
        {
          BUS_SET_OOM (error);
          return FALSE;
        }

        while ((link = _dbus_list_pop_first_link (&dirs)))
          service_dirs_append_link_unique_or_free (&parser->service_dirs, link);

840 841
      return TRUE;
    }
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
  else if (element_type == ELEMENT_STANDARD_SYSTEM_SERVICEDIRS)
    {
      DBusList *link;
      DBusList *dirs;
      dirs = NULL;

      if (!check_no_attributes (parser, "standard_system_servicedirs", attribute_names, attribute_values, error))
        return FALSE;

      if (push_element (parser, ELEMENT_STANDARD_SYSTEM_SERVICEDIRS) == NULL)
        {
          BUS_SET_OOM (error);
          return FALSE;
        }

      if (!_dbus_get_standard_system_servicedirs (&dirs))
        {
          BUS_SET_OOM (error);
          return FALSE;
        }

        while ((link = _dbus_list_pop_first_link (&dirs)))
          service_dirs_append_link_unique_or_free (&parser->service_dirs, link);

      return TRUE;
    }
868 869 870 871 872 873 874 875 876 877 878 879 880 881
  else if (element_type == ELEMENT_ALLOW_ANONYMOUS)
    {
      if (!check_no_attributes (parser, "allow_anonymous", attribute_names, attribute_values, error))
        return FALSE;

      if (push_element (parser, ELEMENT_ALLOW_ANONYMOUS) == NULL)
        {
          BUS_SET_OOM (error);
          return FALSE;
        }

      parser->allow_anonymous = TRUE;
      return TRUE;
    }
882
  else if (element_type == ELEMENT_SERVICEDIR)
883 884 885 886 887 888
    {
      if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
        return FALSE;

      if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
        {
889
          BUS_SET_OOM (error);
890 891 892
          return FALSE;
        }

893 894
      return TRUE;
    }
895
  else if (element_type == ELEMENT_INCLUDE)
896 897
    {
      Element *e;
898
      const char *if_selinux_enabled;
899
      const char *ignore_missing;
900
      const char *selinux_root_relative;
901 902 903

      if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
        {
904
          BUS_SET_OOM (error);
905 906 907 908
          return FALSE;
        }

      e->d.include.ignore_missing = FALSE;
909
      e->d.include.if_selinux_enabled = FALSE;
910
      e->d.include.selinux_root_relative = FALSE;
911 912 913 914 915 916

      if (!locate_attributes (parser, "include",
                              attribute_names,
                              attribute_values,
                              error,
                              "ignore_missing", &ignore_missing,
917
                              "if_selinux_enabled", &if_selinux_enabled,
918
                              "selinux_root_relative", &selinux_root_relative,
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
                              NULL))
        return FALSE;

      if (ignore_missing != NULL)
        {
          if (strcmp (ignore_missing, "yes") == 0)
            e->d.include.ignore_missing = TRUE;
          else if (strcmp (ignore_missing, "no") == 0)
            e->d.include.ignore_missing = FALSE;
          else
            {
              dbus_set_error (error, DBUS_ERROR_FAILED,
                              "ignore_missing attribute must have value \"yes\" or \"no\"");
              return FALSE;
            }
        }
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949

      if (if_selinux_enabled != NULL)
        {
          if (strcmp (if_selinux_enabled, "yes") == 0)
            e->d.include.if_selinux_enabled = TRUE;
          else if (strcmp (if_selinux_enabled, "no") == 0)
            e->d.include.if_selinux_enabled = FALSE;
          else
            {
              dbus_set_error (error, DBUS_ERROR_FAILED,
                              "if_selinux_enabled attribute must have value"
                              " \"yes\" or \"no\"");
              return FALSE;
	    }
        }
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
      
      if (selinux_root_relative != NULL)
        {
          if (strcmp (selinux_root_relative, "yes") == 0)
            e->d.include.selinux_root_relative = TRUE;
          else if (strcmp (selinux_root_relative, "no") == 0)
            e->d.include.selinux_root_relative = FALSE;
          else
            {
              dbus_set_error (error, DBUS_ERROR_FAILED,
                              "selinux_root_relative attribute must have value"
                              " \"yes\" or \"no\"");
              return FALSE;
	    }
        }
965 966 967

      return TRUE;
    }
968
  else if (element_type == ELEMENT_POLICY)
969 970 971 972 973
    {
      Element *e;
      const char *context;
      const char *user;
      const char *group;
974
      const char *at_console;
975 976 977

      if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
        {
978
          BUS_SET_OOM (error);
979 980 981
          return FALSE;
        }

982 983
      e->d.policy.type = POLICY_IGNORED;
      
984
      if (!locate_attributes (parser, "policy",
985 986 987 988 989 990
                              attribute_names,
                              attribute_values,
                              error,
                              "context", &context,
                              "user", &user,
                              "group", &group,
991
                              "at_console", &at_console,
992 993 994
                              NULL))
        return FALSE;

995
      if (((context && user) ||
996 997 998 999 1000 1001
           (context && group) ||
           (context && at_console)) ||
           ((user && group) ||
           (user && at_console)) ||
           (group && at_console) ||
          !(context || user || group || at_console))
1002 1003
        {
          dbus_set_error (error, DBUS_ERROR_FAILED,
1004
                          "<policy> element must have exactly one of (context|user|group|at_console) attributes");
1005 1006 1007 1008 1009 1010 1011
          return FALSE;
        }

      if (context != NULL)
        {
          if (strcmp (context, "default") == 0)
            {
1012
              e->d.policy.type = POLICY_DEFAULT;
1013 1014 1015
            }
          else if (strcmp (context, "mandatory") == 0)
            {
1016
              e->d.policy.type = POLICY_MANDATORY;
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
            }
          else
            {
              dbus_set_error (error, DBUS_ERROR_FAILED,
                              "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
                              context);
              return FALSE;
            }
        }
      else if (user != NULL)
        {
1028 1029
          DBusString username;
          _dbus_string_init_const (&username, user);
1030

1031 1032
          if (_dbus_parse_unix_user_from_config (&username,
                                                 &e->d.policy.gid_uid_or_at_console))
1033 1034 1035 1036
            e->d.policy.type = POLICY_USER;
          else
            _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
                        user);
1037 1038 1039
        }
      else if (group != NULL)
        {
1040 1041
          DBusString group_name;
          _dbus_string_init_const (&group_name, group);
1042

1043 1044
          if (_dbus_parse_unix_group_from_config (&group_name,
                                                  &e->d.policy.gid_uid_or_at_console))
1045 1046 1047 1048
            e->d.policy.type = POLICY_GROUP;
          else
            _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
                        group);          
1049
        }
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
      else if (at_console != NULL)
        {
           dbus_bool_t t;
           t = (strcmp (at_console, "true") == 0);
           if (t || strcmp (at_console, "false") == 0)
             {
               e->d.policy.gid_uid_or_at_console = t; 
               e->d.policy.type = POLICY_CONSOLE;
             }  
           else
             {
               dbus_set_error (error, DBUS_ERROR_FAILED,
                              "Unknown value \"%s\" for at_console in message bus configuration file",
                              at_console);

               return FALSE;
             }
        }
1068 1069 1070 1071
      else
        {
          _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
        }
1072
      
1073 1074
      return TRUE;
    }
1075
  else if (element_type == ELEMENT_LIMIT)
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
    {
      Element *e;
      const char *name;

      if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
        {
          BUS_SET_OOM (error);
          return FALSE;
        }
      
      if (!locate_attributes (parser, "limit",
                              attribute_names,
                              attribute_values,
                              error,
                              "name", &name,
                              NULL))
        return FALSE;

      if (name == NULL)
        {
          dbus_set_error (error, DBUS_ERROR_FAILED,
                          "<limit> element must have a \"name\" attribute");
          return FALSE;
        }

      e->d.limit.name = _dbus_strdup (name);
      if (e->d.limit.name == NULL)
        {
          BUS_SET_OOM (error);
          return FALSE;
        }

1108 1109
      return TRUE;
    }
1110
  else if (element_type == ELEMENT_SELINUX)
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
    {
      if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
        return FALSE;

      if (push_element (parser, ELEMENT_SELINUX) == NULL)
        {
          BUS_SET_OOM (error);
          return FALSE;
        }

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
      return TRUE;
    }
  else
    {
      dbus_set_error (error, DBUS_ERROR_FAILED,
                      "Element <%s> not allowed inside <%s> in configuration file",
                      element_name, "busconfig");
      return FALSE;
    }
}

1132 1133 1134 1135 1136 1137 1138 1139
static dbus_bool_t
append_rule_from_element (BusConfigParser   *parser,
                          const char        *element_name,
                          const char       **attribute_names,
                          const char       **attribute_values,
                          dbus_bool_t        allow,
                          DBusError         *error)
{
1140
  const char *log;
1141 1142 1143
  const char *send_interface;
  const char *send_member;
  const char *send_error;
1144
  const char *send_destination;
1145 1146
  const char *send_path;
  const char *send_type;
1147 1148 1149
  const char *receive_interface;
  const char *receive_member;
  const char *receive_error;
1150
  const char *receive_sender;
1151 1152
  const char *receive_path;
  const char *receive_type;
1153
  const char *eavesdrop;
1154 1155
  const char *send_requested_reply;
  const char *receive_requested_reply;
1156
  const char *own;
1157
  const char *own_prefix;
1158 1159
  const char *user;
  const char *group;
1160

1161 1162 1163 1164 1165 1166
  BusPolicyRule *rule;
  
  if (!locate_attributes (parser, element_name,
                          attribute_names,
                          attribute_values,
                          error,
1167 1168 1169
                          "send_interface", &send_interface,
                          "send_member", &send_member,
                          "send_error", &send_error,
1170
                          "send_destination", &send_destination,
1171 1172
                          "send_path", &send_path,
                          "send_type", &send_type,
1173 1174 1175
                          "receive_interface", &receive_interface,
                          "receive_member", &receive_member,
                          "receive_error", &receive_error,
1176
                          "receive_sender", &receive_sender,
1177 1178
                          "receive_path", &receive_path,
                          "receive_type", &receive_type,
1179
                          "eavesdrop", &eavesdrop,
1180 1181
                          "send_requested_reply", &send_requested_reply,
                          "receive_requested_reply", &receive_requested_reply,
1182
                          "own", &own,
1183
                          "own_prefix", &own_prefix,
1184 1185
                          "user", &user,
                          "group", &group,
1186
                          "log", &log,
1187 1188 1189
                          NULL))
    return FALSE;

1190
  if (!(send_interface || send_member || send_error || send_destination ||
1191
        send_type || send_path ||
1192
        receive_interface || receive_member || receive_error || receive_sender ||
1193 1194
        receive_type || receive_path || eavesdrop ||
        send_requested_reply || receive_requested_reply ||
1195
        own || own_prefix || user || group))
1196 1197 1198 1199 1200 1201 1202
    {
      dbus_set_error (error, DBUS_ERROR_FAILED,
                      "Element <%s> must have one or more attributes",
                      element_name);
      return FALSE;
    }

1203 1204
  if ((send_member && (send_interface == NULL && send_path == NULL)) ||
      (receive_member && (receive_interface == NULL && receive_path == NULL)))
1205 1206
    {
      dbus_set_error (error, DBUS_ERROR_FAILED,
1207
                      "On element <%s>, if you specify a member you must specify an interface or a path. Keep in mind that not all messages have an interface field.",
1208 1209 1210 1211 1212 1213 1214
                      element_name);
      return FALSE;
    }
  
  /* Allowed combinations of elements are:
   *
   *   base, must be all send or all receive:
1215
   *     nothing
1216 1217 1218 1219
   *     interface
   *     interface + member
   *     error
   * 
1220 1221
   *   base send_ can combine with send_destination, send_path, send_type, send_requested_reply
   *   base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1222
   *
1223
   *   user, group, own, own_prefix must occur alone
1224 1225
   *
   * Pretty sure the below stuff is broken, FIXME think about it more.
1226
   */
1227

1228 1229 1230 1231 1232 1233
  if ((send_interface && (send_error ||
                          receive_interface ||
                          receive_member ||
                          receive_error ||
                          receive_sender ||
                          receive_requested_reply ||
1234
                          own || own_prefix ||
1235 1236 1237 1238 1239 1240 1241 1242 1243
                          user ||
                          group)) ||

      (send_member && (send_error ||
                       receive_interface ||
                       receive_member ||
                       receive_error ||
                       receive_sender ||
                       receive_requested_reply ||
1244
                       own || own_prefix ||
1245 1246 1247 1248 1249 1250 1251 1252
                       user ||
                       group)) ||

      (send_error && (receive_interface ||
                      receive_member ||
                      receive_error ||
                      receive_sender ||
                      receive_requested_reply ||
1253
                      own || own_prefix ||
1254 1255 1256 1257 1258 1259 1260 1261
                      user ||
                      group)) ||

      (send_destination && (receive_interface ||
                            receive_member ||
                            receive_error ||
                            receive_sender ||
                            receive_requested_reply ||
1262
                            own || own_prefix ||
1263 1264 1265 1266 1267 1268 1269 1270
                            user ||
                            group)) ||

      (send_type && (receive_interface ||
                     receive_member ||
                     receive_error ||
                     receive_sender ||
                     receive_requested_reply ||
1271
                     own || own_prefix ||
1272 1273 1274 1275 1276 1277 1278 1279
                     user ||
                     group)) ||

      (send_path && (receive_interface ||
                     receive_member ||
                     receive_error ||
                     receive_sender ||
                     receive_requested_reply ||
1280
                     own || own_prefix ||
1281 1282 1283 1284 1285 1286 1287 1288
                     user ||
                     group)) ||

      (send_requested_reply && (receive_interface ||
                                receive_member ||
                                receive_error ||
                                receive_sender ||
                                receive_requested_reply ||
1289
                                own || own_prefix ||
1290 1291 1292 1293
                                user ||
                                group)) ||

      (receive_interface && (receive_error ||
1294
                             own || own_prefix ||
1295 1296 1297 1298
                             user ||
                             group)) ||

      (receive_member && (receive_error ||
1299
                          own || own_prefix ||
1300 1301 1302
                          user ||
                          group)) ||

1303
      (receive_error && (own || own_prefix ||
1304 1305 1306
                         user ||
                         group)) ||

1307
      (eavesdrop && (own || own_prefix ||
1308 1309 1310
                     user ||
                     group)) ||

1311
      (receive_requested_reply && (own || own_prefix ||
1312 1313 1314
                                   user ||
                                   group)) ||

1315 1316 1317
      (own && (own_prefix || user || group)) ||

      (own_prefix && (own || user || group)) ||
1318 1319

      (user && group))
1320 1321
    {
      dbus_set_error (error, DBUS_ERROR_FAILED,
1322
                      "Invalid combination of attributes on element <%s>",
1323 1324 1325
                      element_name);
      return FALSE;
    }
1326
  
1327 1328 1329 1330 1331 1332 1333
  rule = NULL;

  /* In BusPolicyRule, NULL represents wildcard.
   * In the config file, '*' represents it.
   */
#define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')

1334
  if (send_interface || send_member || send_error || send_destination ||
1335
      send_path || send_type || send_requested_reply)
1336
    {
1337
      int message_type;
1338
      
1339 1340 1341 1342 1343 1344
      if (IS_WILDCARD (send_interface))
        send_interface = NULL;
      if (IS_WILDCARD (send_member))
        send_member = NULL;
      if (IS_WILDCARD (send_error))
        send_error = NULL;
1345 1346
      if (IS_WILDCARD (send_destination))
        send_destination = NULL;
1347 1348 1349 1350 1351 1352 1353 1354
      if (IS_WILDCARD (send_path))
        send_path = NULL;
      if (IS_WILDCARD (send_type))
        send_type = NULL;

      message_type = DBUS_MESSAGE_TYPE_INVALID;
      if (send_type != NULL)
        {
1355
          message_type = dbus_message_type_from_string (send_type);
1356 1357 1358 1359 1360 1361 1362 1363
          if (message_type == DBUS_MESSAGE_TYPE_INVALID)
            {
              dbus_set_error (error, DBUS_ERROR_FAILED,
                              "Bad message type \"%s\"",
                              send_type);
              return FALSE;
            }
        }
1364

1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
      if (eavesdrop &&
          !(strcmp (eavesdrop, "true") == 0 ||
            strcmp (eavesdrop, "false") == 0))
        {
          dbus_set_error (error, DBUS_ERROR_FAILED,
                          "Bad value \"%s\" for %s attribute, must be true or false",
                          "eavesdrop", eavesdrop);
          return FALSE;
        }

1375 1376 1377 1378 1379 1380 1381 1382 1383
      if (send_requested_reply &&
          !(strcmp (send_requested_reply, "true") == 0 ||
            strcmp (send_requested_reply, "false") == 0))
        {
          dbus_set_error (error, DBUS_ERROR_FAILED,
                          "Bad value \"%s\" for %s attribute, must be true or false",
                          "send_requested_reply", send_requested_reply);
          return FALSE;
        }
1384
      
1385 1386 1387
      rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow); 
      if (rule == NULL)
        goto nomem;
1388
      
1389 1390 1391
      if (eavesdrop)
        rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);

1392 1393 1394
      if (log)
        rule->d.send.log = (strcmp (log, "true") == 0);

1395 1396
      if (send_requested_reply)
        rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1397

1398 1399
      rule->d.send.message_type = message_type;
      rule->d.send.path = _dbus_strdup (send_path);
1400 1401 1402
      rule->d.send.interface = _dbus_strdup (send_interface);
      rule->d.send.member = _dbus_strdup (send_member);
      rule->d.send.error = _dbus_strdup (send_error);
1403
      rule->d.send.destination = _dbus_strdup (send_destination);
1404 1405
      if (send_path && rule->d.send.path == NULL)
        goto nomem;
1406 1407 1408
      if (send_interface && rule->d.send.interface == NULL)
        goto nomem;
      if (send_member && rule->d.send.member == NULL)
1409
        goto nomem;
1410 1411
      if (send_error && rule->d.send.error == NULL)
        goto nomem;
1412
      if (send_destination && rule->d.send.destination == NULL)
1413 1414
        goto nomem;
    }
1415
  else if (receive_interface || receive_member || receive_error || receive_sender ||
1416
           receive_path || receive_type || eavesdrop || receive_requested_reply)
1417
    {
1418
      int message_type;
1419
      
1420 1421 1422 1423 1424 1425
      if (IS_WILDCARD (receive_interface))
        receive_interface = NULL;
      if (IS_WILDCARD (receive_member))
        receive_member = NULL;
      if (IS_WILDCARD (receive_error))
        receive_error = NULL;
1426 1427
      if (IS_WILDCARD (receive_sender))
        receive_sender = NULL;
1428 1429 1430 1431 1432 1433 1434 1435
      if (IS_WILDCARD (receive_path))
        receive_path = NULL;
      if (IS_WILDCARD (receive_type))
        receive_type = NULL;

      message_type = DBUS_MESSAGE_TYPE_INVALID;
      if (receive_type != NULL)
        {
1436
          message_type = dbus_message_type_from_string (receive_type);
1437 1438 1439 1440 1441 1442 1443 1444
          if (message_type == DBUS_MESSAGE_TYPE_INVALID)
            {
              dbus_set_error (error, DBUS_ERROR_FAILED,
                              "Bad message type \"%s\"",
                              receive_type);
              return FALSE;
            }
        }
1445 1446 1447 1448 1449 1450 1451


      if (eavesdrop &&
          !(strcmp (eavesdrop, "true") == 0 ||
            strcmp (eavesdrop, "false") == 0))
        {
          dbus_set_error (error, DBUS_ERROR_FAILED,
1452 1453 1454 1455 1456
                          "Bad value \"%s\" for %s attribute, must be true or false",
                          "eavesdrop", eavesdrop);
          return FALSE;
        }

1457 1458 1459
      if (receive_requested_reply &&
          !(strcmp (receive_requested_reply, "true") == 0 ||
            strcmp (receive_requested_reply, "false") == 0))
1460 1461 1462
        {
          dbus_set_error (error, DBUS_ERROR_FAILED,
                          "Bad value \"%s\" for %s attribute, must be true or false",
1463
                          "receive_requested_reply", receive_requested_reply);
1464 1465
          return FALSE;
        }
1466 1467 1468 1469
      
      rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow); 
      if (rule == NULL)
        goto nomem;
1470

1471 1472
      if (eavesdrop)
        rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1473

1474 1475
      if (receive_requested_reply)
        rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1476
      
1477 1478
      rule->d.receive.message_type = message_type;
      rule->d.receive.path = _dbus_strdup (receive_path);
1479 1480 1481
      rule->d.receive.interface = _dbus_strdup (receive_interface);
      rule->d.receive.member = _dbus_strdup (receive_member);
      rule->d.receive.error = _dbus_strdup (receive_error);
1482
      rule->d.receive.origin = _dbus_strdup (receive_sender);
1483

1484 1485
      if (receive_path && rule->d.receive.path == NULL)
        goto nomem;
1486 1487 1488 1489 1490
      if (receive_interface && rule->d.receive.interface == NULL)
        goto nomem;
      if (receive_member && rule->d.receive.member == NULL)
        goto nomem;
      if (receive_error && rule->d.receive.error == NULL)
1491
        goto nomem;
1492
      if (receive_sender && rule->d.receive.origin == NULL)
1493 1494
        goto nomem;
    }
1495
  else if (own || own_prefix)
1496 1497 1498 1499 1500
    {
      rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow); 
      if (rule == NULL)
        goto nomem;

1501 1502 1503 1504
      if (own)
        {
          if (IS_WILDCARD (own))
            own = NULL;
1505
      
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
          rule->d.own.prefix = 0;
          rule->d.own.service_name = _dbus_strdup (own);
          if (own && rule->d.own.service_name == NULL)
            goto nomem;
        }
      else
        {
          rule->d.own.prefix = 1;
          rule->d.own.service_name = _dbus_strdup (own_prefix);
          if (rule->d.own.service_name == NULL)
            goto nomem;
        }
1518 1519 1520 1521 1522 1523 1524 1525 1526
    }
  else if (user)
    {      
      if (IS_WILDCARD (user))
        {
          rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow); 
          if (rule == NULL)
            goto nomem;

1527
          rule->d.user.uid = DBUS_UID_UNSET;
1528 1529 1530 1531 1532 1533 1534 1535
        }
      else
        {
          DBusString username;
          dbus_uid_t uid;
          
          _dbus_string_init_const (&username, user);
      
1536
          if (_dbus_parse_unix_user_from_config (&username, &uid))
1537 1538 1539 1540
            {
              rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow); 
              if (rule == NULL)
                goto nomem;
1541

1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
              rule->d.user.uid = uid;
            }
          else
            {
              _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
                          user, element_name);
            }
        }
    }
  else if (group)
    {
      if (IS_WILDCARD (group))
        {
          rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow); 
          if (rule == NULL)
            goto nomem;

1559
          rule->d.group.gid = DBUS_GID_UNSET;
1560 1561 1562 1563 1564 1565 1566 1567
        }
      else
        {
          DBusString groupname;
          dbus_gid_t gid;
          
          _dbus_string_init_const (&groupname, group);
          
1568
          if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1569 1570 1571 1572
            {
              rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow); 
              if (rule == NULL)
                goto nomem;
1573

1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597