os/ossrv/ofdbus/dbus/bus/bus.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /* -*- mode: C; c-file-style: "gnu" -*- */
     2 /* bus.c  message bus context object
     3  *
     4  * Copyright (C) 2003, 2004 Red Hat, Inc.
     5  * Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     6  * Licensed under the Academic Free License version 2.1
     7  * 
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  * 
    18  * You should have received a copy of the GNU General Public License
    19  * along with this program; if not, write to the Free Software
    20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    21  *
    22  */
    23 
    24 #include "bus.h"
    25 #include "activation.h"
    26 #include "connection.h"
    27 #include "services.h"
    28 #include "utils.h"
    29 #include "policy.h"
    30 #include "config-parser.h"
    31 #include "signals.h"
    32 #include "selinux.h"
    33 #include "dir-watch.h"
    34 #ifndef __SYMBIAN32__
    35 #include <dbus/dbus-list.h>
    36 #include <dbus/dbus-hash.h>
    37 #include <dbus/dbus-internals.h>
    38 #else
    39 #include "dbus-list.h"
    40 #include "dbus-hash.h"
    41 #include "dbus-internals.h"
    42 #endif
    43 
    44 struct BusContext
    45 {
    46   int refcount;
    47   char *config_file;
    48   char *type;
    49   char *address;
    50   char *pidfile;
    51   char *user;
    52   DBusLoop *loop;
    53   DBusList *servers;
    54   BusConnections *connections;
    55   BusActivation *activation;
    56   BusRegistry *registry;
    57   BusPolicy *policy;
    58   BusMatchmaker *matchmaker;
    59   DBusUserDatabase *user_database;
    60   BusLimits limits;
    61   unsigned int fork : 1;
    62 };
    63 
    64 static dbus_int32_t server_data_slot = -1;
    65 
    66 typedef struct
    67 {
    68   BusContext *context;
    69 } BusServerData;
    70 
    71 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
    72 
    73 static BusContext*
    74 server_get_context (DBusServer *server)
    75 {
    76   BusContext *context;
    77   BusServerData *bd;
    78   
    79   if (!dbus_server_allocate_data_slot (&server_data_slot))
    80     return NULL;
    81 
    82   bd = BUS_SERVER_DATA (server);
    83   if (bd == NULL)
    84     {
    85       dbus_server_free_data_slot (&server_data_slot);
    86       return NULL;
    87     }
    88 
    89   context = bd->context;
    90 
    91   dbus_server_free_data_slot (&server_data_slot);
    92 
    93   return context;
    94 }
    95 
    96 static dbus_bool_t
    97 server_watch_callback (DBusWatch     *watch,
    98                        unsigned int   condition,
    99                        void          *data)
   100 {
   101   /* FIXME this can be done in dbus-mainloop.c
   102    * if the code in activation.c for the babysitter
   103    * watch handler is fixed.
   104    */
   105   
   106   return dbus_watch_handle (watch, condition);
   107 }
   108 
   109 static dbus_bool_t
   110 add_server_watch (DBusWatch  *watch,
   111                   void       *data)
   112 {
   113   DBusServer *server = data;
   114   BusContext *context;
   115   
   116   context = server_get_context (server);
   117   
   118   return _dbus_loop_add_watch (context->loop,
   119                                watch, server_watch_callback, server,
   120                                NULL);
   121 }
   122 
   123 static void
   124 remove_server_watch (DBusWatch  *watch,
   125                      void       *data)
   126 {
   127   DBusServer *server = data;
   128   BusContext *context;
   129   
   130   context = server_get_context (server);
   131   
   132   _dbus_loop_remove_watch (context->loop,
   133                            watch, server_watch_callback, server);
   134 }
   135 
   136 
   137 static void
   138 server_timeout_callback (DBusTimeout   *timeout,
   139                          void          *data)
   140 {
   141   /* can return FALSE on OOM but we just let it fire again later */
   142   dbus_timeout_handle (timeout);
   143 }
   144 
   145 static dbus_bool_t
   146 add_server_timeout (DBusTimeout *timeout,
   147                     void        *data)
   148 {
   149   DBusServer *server = data;
   150   BusContext *context;
   151   
   152   context = server_get_context (server);
   153 
   154   return _dbus_loop_add_timeout (context->loop,
   155                                  timeout, server_timeout_callback, server, NULL);
   156 }
   157 
   158 static void
   159 remove_server_timeout (DBusTimeout *timeout,
   160                        void        *data)
   161 {
   162   DBusServer *server = data;
   163   BusContext *context;
   164   
   165   context = server_get_context (server);
   166   
   167   _dbus_loop_remove_timeout (context->loop,
   168                              timeout, server_timeout_callback, server);
   169 }
   170 
   171 static void
   172 new_connection_callback (DBusServer     *server,
   173                          DBusConnection *new_connection,
   174                          void           *data)
   175 {
   176   BusContext *context = data;
   177   
   178   if (!bus_connections_setup_connection (context->connections, new_connection))
   179     {
   180       _dbus_verbose ("No memory to setup new connection\n");
   181 
   182       /* if we don't do this, it will get unref'd without
   183        * being disconnected... kind of strange really
   184        * that we have to do this, people won't get it right
   185        * in general.
   186        */
   187       dbus_connection_close (new_connection);
   188     }
   189 
   190   dbus_connection_set_max_received_size (new_connection,
   191                                          context->limits.max_incoming_bytes);
   192 
   193   dbus_connection_set_max_message_size (new_connection,
   194                                         context->limits.max_message_size);
   195   
   196   /* on OOM, we won't have ref'd the connection so it will die. */
   197 }
   198 
   199 static void
   200 free_server_data (void *data)
   201 {
   202   BusServerData *bd = data;  
   203   
   204   dbus_free (bd);
   205 }
   206 
   207 static dbus_bool_t
   208 setup_server (BusContext *context,
   209               DBusServer *server,
   210               char      **auth_mechanisms,
   211               DBusError  *error)
   212 {
   213   BusServerData *bd;
   214 
   215   bd = dbus_new0 (BusServerData, 1);
   216   if (!dbus_server_set_data (server,
   217                              server_data_slot,
   218                              bd, free_server_data))
   219     {
   220       dbus_free (bd);
   221       BUS_SET_OOM (error);
   222       return FALSE;
   223     }
   224 
   225   bd->context = context;
   226   
   227   if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
   228     {
   229       BUS_SET_OOM (error);
   230       return FALSE;
   231     }
   232   
   233   dbus_server_set_new_connection_function (server,
   234                                            new_connection_callback,
   235                                            context, NULL);
   236   
   237   if (!dbus_server_set_watch_functions (server,
   238                                         add_server_watch,
   239                                         remove_server_watch,
   240                                         NULL,
   241                                         server,
   242                                         NULL))
   243     {
   244       BUS_SET_OOM (error);
   245       return FALSE;
   246     }
   247 
   248   if (!dbus_server_set_timeout_functions (server,
   249                                           add_server_timeout,
   250                                           remove_server_timeout,
   251                                           NULL,
   252                                           server, NULL))
   253     {
   254       BUS_SET_OOM (error);
   255       return FALSE;
   256     }
   257   
   258   return TRUE;
   259 }
   260 
   261 /* This code only gets executed the first time the
   262    config files are parsed.  It is not executed
   263    when config files are reloaded.*/
   264 static dbus_bool_t
   265 process_config_first_time_only (BusContext      *context,
   266 				BusConfigParser *parser,
   267 				DBusError       *error)
   268 {
   269   DBusList *link;
   270   DBusList **addresses;
   271   const char *user, *pidfile;
   272   char **auth_mechanisms;
   273   DBusList **auth_mechanisms_list;
   274   int len;
   275   dbus_bool_t retval;
   276 
   277   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   278 
   279   retval = FALSE;
   280   auth_mechanisms = NULL;
   281 
   282   /* Check for an existing pid file. Of course this is a race;
   283    * we'd have to use fcntl() locks on the pid file to
   284    * avoid that. But we want to check for the pid file
   285    * before overwriting any existing sockets, etc.
   286    */
   287   pidfile = bus_config_parser_get_pidfile (parser);
   288   if (pidfile != NULL)
   289     {
   290       DBusString u;
   291       DBusStat stbuf;
   292       
   293       _dbus_string_init_const (&u, pidfile);
   294       
   295       if (_dbus_stat (&u, &stbuf, NULL))
   296 	{
   297 	  dbus_set_error (error, DBUS_ERROR_FAILED,
   298 			  "The pid file \"%s\" exists, if the message bus is not running, remove this file",
   299 			  pidfile);
   300 	  goto failed;
   301 	}
   302     }
   303   
   304   /* keep around the pid filename so we can delete it later */
   305   context->pidfile = _dbus_strdup (pidfile);
   306 
   307   /* Build an array of auth mechanisms */
   308   
   309   auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
   310   len = _dbus_list_get_length (auth_mechanisms_list);
   311 
   312   if (len > 0)
   313     {
   314       int i;
   315 
   316       auth_mechanisms = dbus_new0 (char*, len + 1);
   317       if (auth_mechanisms == NULL)
   318 	{
   319 	  BUS_SET_OOM (error);
   320 	  goto failed;
   321 	}
   322       
   323       i = 0;
   324       link = _dbus_list_get_first_link (auth_mechanisms_list);
   325       while (link != NULL)
   326         {
   327           auth_mechanisms[i] = _dbus_strdup (link->data);
   328           if (auth_mechanisms[i] == NULL)
   329 	    {
   330 	      BUS_SET_OOM (error);
   331 	      goto failed;
   332 	    }
   333           link = _dbus_list_get_next_link (auth_mechanisms_list, link);
   334         }
   335     }
   336   else
   337     {
   338       auth_mechanisms = NULL;
   339     }
   340 
   341   /* Listen on our addresses */
   342   
   343   addresses = bus_config_parser_get_addresses (parser);  
   344   
   345   link = _dbus_list_get_first_link (addresses);
   346   while (link != NULL)
   347     {
   348       DBusServer *server;
   349       
   350       server = dbus_server_listen (link->data, error);
   351       if (server == NULL)
   352 	{
   353 	  _DBUS_ASSERT_ERROR_IS_SET (error);
   354 	  goto failed;
   355 	}
   356       else if (!setup_server (context, server, auth_mechanisms, error))
   357 	{
   358 	  _DBUS_ASSERT_ERROR_IS_SET (error);
   359 	  goto failed;
   360 	}
   361 
   362       if (!_dbus_list_append (&context->servers, server))
   363         {
   364           BUS_SET_OOM (error);
   365           goto failed;
   366         }          
   367       
   368       link = _dbus_list_get_next_link (addresses, link);
   369     }
   370 
   371   /* note that type may be NULL */
   372   context->type = _dbus_strdup (bus_config_parser_get_type (parser));
   373   if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
   374     {
   375       BUS_SET_OOM (error);
   376       goto failed;
   377     }
   378 
   379   user = bus_config_parser_get_user (parser);
   380   if (user != NULL)
   381     {
   382       context->user = _dbus_strdup (user);
   383       if (context->user == NULL)
   384 	{
   385 	  BUS_SET_OOM (error);
   386 	  goto failed;
   387 	}
   388     }
   389 
   390   context->fork = bus_config_parser_get_fork (parser);
   391   
   392   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   393   retval = TRUE;
   394 
   395  failed:
   396   dbus_free_string_array (auth_mechanisms);
   397   return retval;
   398 }
   399 
   400 /* This code gets executed every time the config files
   401    are parsed: both during BusContext construction
   402    and on reloads. */
   403 static dbus_bool_t
   404 process_config_every_time (BusContext      *context,
   405 			   BusConfigParser *parser,
   406 			   dbus_bool_t      is_reload,
   407 			   DBusError       *error)
   408 {
   409   DBusString full_address;
   410   DBusList *link;
   411   char *addr;
   412 
   413   dbus_bool_t retval;
   414 
   415   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   416 
   417   addr = NULL;
   418   retval = FALSE;
   419 
   420   if (!_dbus_string_init (&full_address))
   421     {
   422       BUS_SET_OOM (error);
   423       return FALSE;
   424     }
   425 
   426   /* get our limits and timeout lengths */
   427   bus_config_parser_get_limits (parser, &context->limits);
   428 
   429   context->policy = bus_config_parser_steal_policy (parser);
   430   _dbus_assert (context->policy != NULL);
   431 
   432   /* We have to build the address backward, so that
   433    * <listen> later in the config file have priority
   434    */
   435   link = _dbus_list_get_last_link (&context->servers);
   436   while (link != NULL)
   437     {
   438       addr = dbus_server_get_address (link->data);
   439       if (addr == NULL)
   440         {
   441           BUS_SET_OOM (error);
   442           goto failed;
   443         }
   444 
   445       if (_dbus_string_get_length (&full_address) > 0)
   446         {
   447           if (!_dbus_string_append (&full_address, ";"))
   448             {
   449               BUS_SET_OOM (error);
   450               goto failed;
   451             }
   452         }
   453 
   454       if (!_dbus_string_append (&full_address, addr))
   455         {
   456           BUS_SET_OOM (error);
   457           goto failed;
   458         }
   459 
   460       dbus_free (addr);
   461       addr = NULL;
   462 
   463       link = _dbus_list_get_prev_link (&context->servers, link);
   464     }
   465 
   466   if (is_reload)
   467     dbus_free (context->address);
   468 
   469   if (!_dbus_string_copy_data (&full_address, &context->address))
   470     {
   471       BUS_SET_OOM (error);
   472       goto failed;
   473     }
   474 
   475   /* Create activation subsystem */
   476   
   477   if (is_reload)
   478     bus_activation_unref (context->activation);
   479   
   480   context->activation = bus_activation_new (context, &full_address,
   481                                             bus_config_parser_get_service_dirs (parser),
   482                                             error);
   483   if (context->activation == NULL)
   484     {
   485       _DBUS_ASSERT_ERROR_IS_SET (error);
   486       goto failed;
   487     }
   488 
   489   /* Drop existing conf-dir watches (if applicable) */
   490 
   491   if (is_reload)
   492     bus_drop_all_directory_watches ();
   493 
   494   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   495   retval = TRUE;
   496 
   497  failed:
   498   _dbus_string_free (&full_address);
   499   
   500   if (addr)
   501     dbus_free (addr);
   502 
   503   return retval;
   504 }
   505 
   506 static dbus_bool_t
   507 process_config_postinit (BusContext      *context,
   508 			 BusConfigParser *parser,
   509 			 DBusError       *error)
   510 {
   511   DBusHashTable *service_context_table;
   512 
   513   service_context_table = bus_config_parser_steal_service_context_table (parser);
   514   if (!bus_registry_set_service_context_table (context->registry,
   515 					       service_context_table))
   516     {
   517       BUS_SET_OOM (error);
   518       return FALSE;
   519     }
   520 
   521   _dbus_hash_table_unref (service_context_table);
   522 
   523   /* Watch all conf directories */
   524   _dbus_list_foreach (bus_config_parser_get_conf_dirs (parser),
   525 		      (DBusForeachFunction) bus_watch_directory,
   526 		      context);
   527 
   528   return TRUE;
   529 }
   530 
   531 BusContext*
   532 bus_context_new (const DBusString *config_file,
   533                  ForceForkSetting  force_fork,
   534                  int               print_addr_fd,
   535                  int               print_pid_fd,
   536                  DBusError        *error)
   537 {
   538   BusContext *context;
   539   BusConfigParser *parser;
   540   DBusCredentials creds;
   541 
   542   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   543 
   544   context = NULL;
   545   parser = NULL;
   546 
   547   if (!dbus_server_allocate_data_slot (&server_data_slot))
   548     {
   549       BUS_SET_OOM (error);
   550       return NULL;
   551     }
   552 
   553   context = dbus_new0 (BusContext, 1);
   554   if (context == NULL)
   555     {
   556       BUS_SET_OOM (error);
   557       goto failed;
   558     }
   559   context->refcount = 1;
   560 
   561   if (!_dbus_string_copy_data (config_file, &context->config_file))
   562     {
   563       BUS_SET_OOM (error);
   564       goto failed;
   565     }
   566 
   567   context->loop = _dbus_loop_new ();
   568   if (context->loop == NULL)
   569     {
   570       BUS_SET_OOM (error);
   571       goto failed;
   572     }
   573 
   574   context->registry = bus_registry_new (context);
   575   if (context->registry == NULL)
   576     {
   577       BUS_SET_OOM (error);
   578       goto failed;
   579     }
   580 
   581   parser = bus_config_load (config_file, TRUE, NULL, error);
   582   if (parser == NULL)
   583     {
   584       _DBUS_ASSERT_ERROR_IS_SET (error);
   585       goto failed;
   586     }
   587   
   588   if (!process_config_first_time_only (context, parser, error))
   589     {
   590       _DBUS_ASSERT_ERROR_IS_SET (error);
   591       goto failed;
   592     }
   593   if (!process_config_every_time (context, parser, FALSE, error))
   594     {
   595       _DBUS_ASSERT_ERROR_IS_SET (error);
   596       goto failed;
   597     }
   598   
   599   /* we need another ref of the server data slot for the context
   600    * to own
   601    */
   602   if (!dbus_server_allocate_data_slot (&server_data_slot))
   603     _dbus_assert_not_reached ("second ref of server data slot failed");
   604 
   605   context->user_database = _dbus_user_database_new ();
   606   if (context->user_database == NULL)
   607     {
   608       BUS_SET_OOM (error);
   609       goto failed;
   610     }
   611   
   612   /* Note that we don't know whether the print_addr_fd is
   613    * one of the sockets we're using to listen on, or some
   614    * other random thing. But I think the answer is "don't do
   615    * that then"
   616    */
   617   if (print_addr_fd >= 0)
   618     {
   619       DBusString addr;
   620       const char *a = bus_context_get_address (context);
   621       int bytes;
   622       
   623       _dbus_assert (a != NULL);
   624       if (!_dbus_string_init (&addr))
   625         {
   626           BUS_SET_OOM (error);
   627           goto failed;
   628         }
   629         
   630                 
   631         
   632         #ifndef __SYMBIAN32__
   633          if (!_dbus_string_append (&addr, a) ||
   634           !_dbus_string_append (&addr, "\n")) 
   635       #else
   636       if (!_dbus_string_append (&addr, a) )
   637     
   638        #endif 
   639         {
   640           _dbus_string_free (&addr);
   641           BUS_SET_OOM (error);
   642           goto failed;
   643         }
   644 		
   645 		
   646       bytes = _dbus_string_get_length (&addr);
   647       if (_dbus_write_socket (print_addr_fd, &addr, 0, bytes) != bytes)
   648         {
   649           dbus_set_error (error, DBUS_ERROR_FAILED,
   650                           "Printing message bus address: %s\n",
   651                           _dbus_strerror (errno));
   652           _dbus_string_free (&addr);
   653           goto failed;
   654         }
   655 
   656       if (print_addr_fd > 2)
   657         _dbus_close_socket (print_addr_fd, NULL);
   658 
   659       _dbus_string_free (&addr);
   660     }
   661   
   662   context->connections = bus_connections_new (context);
   663   if (context->connections == NULL)
   664     {
   665       BUS_SET_OOM (error);
   666       goto failed;
   667     }
   668 
   669   context->matchmaker = bus_matchmaker_new ();
   670   if (context->matchmaker == NULL)
   671     {
   672       BUS_SET_OOM (error);
   673       goto failed;
   674     }
   675 
   676   /* check user before we fork */
   677   if (context->user != NULL)
   678     {
   679       DBusString u;
   680 
   681       _dbus_string_init_const (&u, context->user);
   682 
   683       if (!_dbus_credentials_from_username (&u, &creds) ||
   684           creds.uid < 0 ||
   685           creds.gid < 0)
   686         {
   687           dbus_set_error (error, DBUS_ERROR_FAILED,
   688                           "Could not get UID and GID for username \"%s\"",
   689                           context->user);
   690           goto failed;
   691         }
   692     }
   693 
   694   /* Now become a daemon if appropriate */
   695   if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
   696     {
   697       DBusString u;
   698 
   699       if (context->pidfile)
   700         _dbus_string_init_const (&u, context->pidfile);
   701       
   702       if (!_dbus_become_daemon (context->pidfile ? &u : NULL, 
   703 				print_pid_fd,
   704 				error))
   705 	{
   706 	  _DBUS_ASSERT_ERROR_IS_SET (error);
   707 	  goto failed;
   708 	}
   709     }
   710   else
   711     {
   712       /* Need to write PID file for ourselves, not for the child process */
   713       if (context->pidfile != NULL)
   714         {
   715           DBusString u;
   716 
   717           _dbus_string_init_const (&u, context->pidfile);
   718           
   719           if (!_dbus_write_pid_file (&u, _dbus_getpid (), error))
   720 	    {
   721 	      _DBUS_ASSERT_ERROR_IS_SET (error);
   722 	      goto failed;
   723 	    }
   724         }
   725     }
   726 
   727   /* Write PID if requested */
   728   if (print_pid_fd >= 0)
   729     {
   730       DBusString pid;
   731       int bytes;
   732 
   733       if (!_dbus_string_init (&pid))
   734         {
   735           BUS_SET_OOM (error);
   736           goto failed;
   737         }
   738       
   739       if (!_dbus_string_append_int (&pid, _dbus_getpid ()) ||
   740           !_dbus_string_append (&pid, "\n"))
   741         {
   742           _dbus_string_free (&pid);
   743           BUS_SET_OOM (error);
   744           goto failed;
   745         }
   746 
   747       bytes = _dbus_string_get_length (&pid);
   748       if (_dbus_write_socket (print_pid_fd, &pid, 0, bytes) != bytes)
   749         {
   750           dbus_set_error (error, DBUS_ERROR_FAILED,
   751                           "Printing message bus PID: %s\n",
   752                           _dbus_strerror (errno));
   753           _dbus_string_free (&pid);
   754           goto failed;
   755         }
   756 
   757       if (print_pid_fd > 2)
   758         _dbus_close_socket (print_pid_fd, NULL);
   759       
   760       _dbus_string_free (&pid);
   761     }
   762 
   763   if (!bus_selinux_full_init ())
   764     {
   765       _dbus_warn ("SELinux initialization failed\n");
   766     }
   767 
   768   if (!process_config_postinit (context, parser, error))
   769     {
   770       _DBUS_ASSERT_ERROR_IS_SET (error);
   771       goto failed;
   772     }
   773 
   774   if (parser != NULL)
   775     {
   776       bus_config_parser_unref (parser);
   777       parser = NULL;
   778     }
   779   
   780   /* Here we change our credentials if required,
   781    * as soon as we've set up our sockets and pidfile
   782    */
   783   if (context->user != NULL)
   784     {
   785       if (!_dbus_change_identity (creds.uid, creds.gid, error))
   786 	{
   787 	  _DBUS_ASSERT_ERROR_IS_SET (error);
   788 	  goto failed;
   789 	}
   790     }
   791   
   792   dbus_server_free_data_slot (&server_data_slot);
   793   
   794   return context;
   795   
   796  failed:  
   797   if (parser != NULL)
   798     bus_config_parser_unref (parser);
   799   if (context != NULL)
   800     bus_context_unref (context);
   801 
   802   if (server_data_slot >= 0)
   803     dbus_server_free_data_slot (&server_data_slot);
   804   
   805   return NULL;
   806 }
   807 
   808 dbus_bool_t
   809 bus_context_reload_config (BusContext *context,
   810 			   DBusError  *error)
   811 {
   812   BusConfigParser *parser;
   813   DBusString config_file;
   814   dbus_bool_t ret;
   815 
   816   /* Flush the user database cache */
   817   _dbus_user_database_flush(context->user_database);
   818 
   819   ret = FALSE;
   820   _dbus_string_init_const (&config_file, context->config_file);
   821   parser = bus_config_load (&config_file, TRUE, NULL, error);
   822   if (parser == NULL)
   823     {
   824       _DBUS_ASSERT_ERROR_IS_SET (error);
   825       goto failed;
   826     }
   827   
   828   if (!process_config_every_time (context, parser, TRUE, error))
   829     {
   830       _DBUS_ASSERT_ERROR_IS_SET (error);
   831       goto failed;
   832     }
   833   if (!process_config_postinit (context, parser, error))
   834     {
   835       _DBUS_ASSERT_ERROR_IS_SET (error);
   836       goto failed;
   837     }
   838   ret = TRUE;
   839 
   840  failed:  
   841   if (parser != NULL)
   842     bus_config_parser_unref (parser);
   843   return ret;
   844 }
   845 
   846 static void
   847 shutdown_server (BusContext *context,
   848                  DBusServer *server)
   849 {
   850   if (server == NULL ||
   851       !dbus_server_get_is_connected (server))
   852     return;
   853   
   854   if (!dbus_server_set_watch_functions (server,
   855                                         NULL, NULL, NULL,
   856                                         context,
   857                                         NULL))
   858     _dbus_assert_not_reached ("setting watch functions to NULL failed");
   859   
   860   if (!dbus_server_set_timeout_functions (server,
   861                                           NULL, NULL, NULL,
   862                                           context,
   863                                           NULL))
   864     _dbus_assert_not_reached ("setting timeout functions to NULL failed");
   865   
   866   dbus_server_disconnect (server);
   867 }
   868 
   869 void
   870 bus_context_shutdown (BusContext  *context)
   871 {
   872   DBusList *link;
   873 
   874   link = _dbus_list_get_first_link (&context->servers);
   875   while (link != NULL)
   876     {
   877       shutdown_server (context, link->data);
   878 
   879       link = _dbus_list_get_next_link (&context->servers, link);
   880     }
   881 }
   882 
   883 BusContext *
   884 bus_context_ref (BusContext *context)
   885 {
   886   _dbus_assert (context->refcount > 0);
   887   context->refcount += 1;
   888 
   889   return context;
   890 }
   891 
   892 void
   893 bus_context_unref (BusContext *context)
   894 {
   895   _dbus_assert (context->refcount > 0);
   896   context->refcount -= 1;
   897 
   898   if (context->refcount == 0)
   899     {
   900       DBusList *link;
   901       
   902       _dbus_verbose ("Finalizing bus context %p\n", context);
   903       
   904       bus_context_shutdown (context);
   905 
   906       if (context->connections)
   907         {
   908           bus_connections_unref (context->connections);
   909           context->connections = NULL;
   910         }
   911       
   912       if (context->registry)
   913         {
   914           bus_registry_unref (context->registry);
   915           context->registry = NULL;
   916         }
   917       
   918       if (context->activation)
   919         {
   920           bus_activation_unref (context->activation);
   921           context->activation = NULL;
   922         }
   923 
   924       link = _dbus_list_get_first_link (&context->servers);
   925       while (link != NULL)
   926         {
   927           dbus_server_unref (link->data);
   928           
   929           link = _dbus_list_get_next_link (&context->servers, link);
   930         }
   931       _dbus_list_clear (&context->servers);
   932 
   933       if (context->policy)
   934         {
   935           bus_policy_unref (context->policy);
   936           context->policy = NULL;
   937         }
   938       
   939       if (context->loop)
   940         {
   941           _dbus_loop_unref (context->loop);
   942           context->loop = NULL;
   943         }
   944 
   945       if (context->matchmaker)
   946         {
   947           bus_matchmaker_unref (context->matchmaker);
   948           context->matchmaker = NULL;
   949         }
   950       
   951       dbus_free (context->config_file);
   952       dbus_free (context->type);
   953       dbus_free (context->address);
   954       dbus_free (context->user);
   955 
   956       if (context->pidfile)
   957 	{
   958           DBusString u;
   959           _dbus_string_init_const (&u, context->pidfile);
   960 
   961           /* Deliberately ignore errors here, since there's not much
   962 	   * we can do about it, and we're exiting anyways.
   963 	   */
   964 	  _dbus_delete_file (&u, NULL);
   965 
   966           dbus_free (context->pidfile); 
   967 	}
   968 
   969       if (context->user_database != NULL)
   970 	_dbus_user_database_unref (context->user_database);
   971       
   972       dbus_free (context);
   973 
   974       dbus_server_free_data_slot (&server_data_slot);
   975     }
   976 }
   977 
   978 /* type may be NULL */
   979 const char*
   980 bus_context_get_type (BusContext *context)
   981 {
   982   return context->type;
   983 }
   984 
   985 const char*
   986 bus_context_get_address (BusContext *context)
   987 {
   988   return context->address;
   989 }
   990 
   991 BusRegistry*
   992 bus_context_get_registry (BusContext  *context)
   993 {
   994   return context->registry;
   995 }
   996 
   997 BusConnections*
   998 bus_context_get_connections (BusContext  *context)
   999 {
  1000   return context->connections;
  1001 }
  1002 
  1003 BusActivation*
  1004 bus_context_get_activation (BusContext  *context)
  1005 {
  1006   return context->activation;
  1007 }
  1008 
  1009 BusMatchmaker*
  1010 bus_context_get_matchmaker (BusContext  *context)
  1011 {
  1012   return context->matchmaker;
  1013 }
  1014 
  1015 DBusLoop*
  1016 bus_context_get_loop (BusContext *context)
  1017 {
  1018   return context->loop;
  1019 }
  1020 
  1021 DBusUserDatabase*
  1022 bus_context_get_user_database (BusContext *context)
  1023 {
  1024   return context->user_database;
  1025 }
  1026 
  1027 dbus_bool_t
  1028 bus_context_allow_user (BusContext   *context,
  1029                         unsigned long uid)
  1030 {
  1031   return bus_policy_allow_user (context->policy,
  1032                                 context->user_database,
  1033                                 uid);
  1034 }
  1035 
  1036 BusPolicy *
  1037 bus_context_get_policy (BusContext *context)
  1038 {
  1039   return context->policy;
  1040 }
  1041 
  1042 BusClientPolicy*
  1043 bus_context_create_client_policy (BusContext      *context,
  1044                                   DBusConnection  *connection,
  1045                                   DBusError       *error)
  1046 {
  1047   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  1048   return bus_policy_create_client_policy (context->policy, connection,
  1049                                           error);
  1050 }
  1051 
  1052 int
  1053 bus_context_get_activation_timeout (BusContext *context)
  1054 {
  1055   
  1056   return context->limits.activation_timeout;
  1057 }
  1058 
  1059 int
  1060 bus_context_get_auth_timeout (BusContext *context)
  1061 {
  1062   return context->limits.auth_timeout;
  1063 }
  1064 
  1065 int
  1066 bus_context_get_max_completed_connections (BusContext *context)
  1067 {
  1068   return context->limits.max_completed_connections;
  1069 }
  1070 
  1071 int
  1072 bus_context_get_max_incomplete_connections (BusContext *context)
  1073 {
  1074   return context->limits.max_incomplete_connections;
  1075 }
  1076 
  1077 int
  1078 bus_context_get_max_connections_per_user (BusContext *context)
  1079 {
  1080   return context->limits.max_connections_per_user;
  1081 }
  1082 
  1083 int
  1084 bus_context_get_max_pending_activations (BusContext *context)
  1085 {
  1086   return context->limits.max_pending_activations;
  1087 }
  1088 
  1089 int
  1090 bus_context_get_max_services_per_connection (BusContext *context)
  1091 {
  1092   return context->limits.max_services_per_connection;
  1093 }
  1094 
  1095 int
  1096 bus_context_get_max_match_rules_per_connection (BusContext *context)
  1097 {
  1098   return context->limits.max_match_rules_per_connection;
  1099 }
  1100 
  1101 int
  1102 bus_context_get_max_replies_per_connection (BusContext *context)
  1103 {
  1104   return context->limits.max_replies_per_connection;
  1105 }
  1106 
  1107 int
  1108 bus_context_get_reply_timeout (BusContext *context)
  1109 {
  1110   return context->limits.reply_timeout;
  1111 }
  1112 
  1113 /*
  1114  * addressed_recipient is the recipient specified in the message.
  1115  *
  1116  * proposed_recipient is the recipient we're considering sending
  1117  * to right this second, and may be an eavesdropper.
  1118  *
  1119  * sender is the sender of the message.
  1120  *
  1121  * NULL for proposed_recipient or sender definitely means the bus driver.
  1122  *
  1123  * NULL for addressed_recipient may mean the bus driver, or may mean
  1124  * no destination was specified in the message (e.g. a signal).
  1125  */
  1126 dbus_bool_t
  1127 bus_context_check_security_policy (BusContext     *context,
  1128                                    BusTransaction *transaction,
  1129                                    DBusConnection *sender,
  1130                                    DBusConnection *addressed_recipient,
  1131                                    DBusConnection *proposed_recipient,
  1132                                    DBusMessage    *message,
  1133                                    DBusError      *error)
  1134 {
  1135   BusClientPolicy *sender_policy;
  1136   BusClientPolicy *recipient_policy;
  1137   int type;
  1138   dbus_bool_t requested_reply;
  1139   
  1140   type = dbus_message_get_type (message);
  1141   
  1142   /* dispatch.c was supposed to ensure these invariants */
  1143   _dbus_assert (dbus_message_get_destination (message) != NULL ||
  1144                 type == DBUS_MESSAGE_TYPE_SIGNAL ||
  1145                 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
  1146   _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
  1147                 addressed_recipient != NULL ||
  1148                 strcmp (dbus_message_get_destination (message), DBUS_SERVICE_DBUS) == 0);
  1149   
  1150   switch (type)
  1151     {
  1152     case DBUS_MESSAGE_TYPE_METHOD_CALL:
  1153     case DBUS_MESSAGE_TYPE_SIGNAL:
  1154     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
  1155     case DBUS_MESSAGE_TYPE_ERROR:
  1156       break;
  1157       
  1158     default:
  1159       _dbus_verbose ("security check disallowing message of unknown type %d\n",
  1160                      type);
  1161 
  1162       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
  1163                       "Message bus will not accept messages of unknown type\n");
  1164               
  1165       return FALSE;
  1166     }
  1167 
  1168   requested_reply = FALSE;
  1169   
  1170   if (sender != NULL)
  1171     {
  1172       const char *dest;
  1173 
  1174       dest = dbus_message_get_destination (message);
  1175 	
  1176       /* First verify the SELinux access controls.  If allowed then
  1177        * go on with the standard checks.
  1178        */
  1179       if (!bus_selinux_allows_send (sender, proposed_recipient,
  1180 				    dbus_message_type_to_string (dbus_message_get_type (message)),
  1181 				    dbus_message_get_interface (message),
  1182 				    dbus_message_get_member (message),
  1183 				    dbus_message_get_error_name (message),
  1184 				    dest ? dest : DBUS_SERVICE_DBUS, error))
  1185         {
  1186 
  1187 	  if (dbus_error_is_set (error) &&
  1188 	      dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
  1189 	    {
  1190 	      return FALSE;
  1191 	    }
  1192 	  
  1193 
  1194           dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
  1195                           "An SELinux policy prevents this sender "
  1196                           "from sending this message to this recipient "
  1197                           "(rejected message had interface \"%s\" "
  1198                           "member \"%s\" error name \"%s\" destination \"%s\")",
  1199                           dbus_message_get_interface (message) ?
  1200                           dbus_message_get_interface (message) : "(unset)",
  1201                           dbus_message_get_member (message) ?
  1202                           dbus_message_get_member (message) : "(unset)",
  1203                           dbus_message_get_error_name (message) ?
  1204                           dbus_message_get_error_name (message) : "(unset)",
  1205                           dest ? dest : DBUS_SERVICE_DBUS);
  1206           _dbus_verbose ("SELinux security check denying send to service\n");
  1207           return FALSE;
  1208         }
  1209        
  1210       if (bus_connection_is_active (sender))
  1211         {
  1212           sender_policy = bus_connection_get_policy (sender);
  1213           _dbus_assert (sender_policy != NULL);
  1214           
  1215           /* Fill in requested_reply variable with TRUE if this is a
  1216            * reply and the reply was pending.
  1217            */
  1218           if (dbus_message_get_reply_serial (message) != 0)
  1219             {
  1220               if (proposed_recipient != NULL /* not to the bus driver */ &&
  1221                   addressed_recipient == proposed_recipient /* not eavesdropping */)
  1222                 {
  1223                   DBusError error2;                  
  1224                   
  1225                   dbus_error_init (&error2);
  1226                   requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
  1227                                                                  transaction,
  1228                                                                  sender, addressed_recipient, message,
  1229                                                                  &error2);
  1230                   if (dbus_error_is_set (&error2))
  1231                     {
  1232                       dbus_move_error (&error2, error);
  1233                       return FALSE;
  1234                     }
  1235                 }
  1236             }
  1237         }
  1238       else
  1239         {
  1240           /* Policy for inactive connections is that they can only send
  1241            * the hello message to the bus driver
  1242            */
  1243           if (proposed_recipient == NULL &&
  1244               dbus_message_is_method_call (message,
  1245                                            DBUS_INTERFACE_DBUS,
  1246                                            "Hello"))
  1247             {
  1248               _dbus_verbose ("security check allowing %s message\n",
  1249                              "Hello");
  1250               return TRUE;
  1251             }
  1252           else
  1253             {
  1254               _dbus_verbose ("security check disallowing non-%s message\n",
  1255                              "Hello");
  1256 
  1257               dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
  1258                               "Client tried to send a message other than %s without being registered",
  1259                               "Hello");
  1260               
  1261               return FALSE;
  1262             }
  1263         }
  1264     }
  1265   else
  1266     {
  1267       sender_policy = NULL;
  1268 
  1269       /* If the sender is the bus driver, we assume any reply was a
  1270        * requested reply as bus driver won't send bogus ones
  1271        */
  1272       if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
  1273           dbus_message_get_reply_serial (message) != 0)
  1274         requested_reply = TRUE;
  1275     }
  1276 
  1277   _dbus_assert ((sender != NULL && sender_policy != NULL) ||
  1278                 (sender == NULL && sender_policy == NULL));
  1279   
  1280   if (proposed_recipient != NULL)
  1281     {
  1282       /* only the bus driver can send to an inactive recipient (as it
  1283        * owns no services, so other apps can't address it). Inactive
  1284        * recipients can receive any message.
  1285        */
  1286       if (bus_connection_is_active (proposed_recipient))
  1287         {
  1288           recipient_policy = bus_connection_get_policy (proposed_recipient);
  1289           _dbus_assert (recipient_policy != NULL);
  1290         }
  1291       else if (sender == NULL)
  1292         {
  1293           _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
  1294           recipient_policy = NULL;
  1295         }
  1296       else
  1297         {
  1298           _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
  1299           recipient_policy = NULL;
  1300         }
  1301     }
  1302   else
  1303     recipient_policy = NULL;
  1304   
  1305   _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
  1306                 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
  1307                 (proposed_recipient == NULL && recipient_policy == NULL));
  1308   
  1309   if (sender_policy &&
  1310       !bus_client_policy_check_can_send (sender_policy,
  1311                                          context->registry,
  1312                                          requested_reply,
  1313                                          proposed_recipient,
  1314                                          message))
  1315     {
  1316       const char *dest;
  1317 
  1318       dest = dbus_message_get_destination (message);
  1319       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
  1320                       "A security policy in place prevents this sender "
  1321                       "from sending this message to this recipient, "
  1322                       "see message bus configuration file (rejected message "
  1323                       "had interface \"%s\" member \"%s\" error name \"%s\" destination \"%s\")",
  1324                       dbus_message_get_interface (message) ?
  1325                       dbus_message_get_interface (message) : "(unset)",
  1326                       dbus_message_get_member (message) ?
  1327                       dbus_message_get_member (message) : "(unset)",
  1328                       dbus_message_get_error_name (message) ?
  1329                       dbus_message_get_error_name (message) : "(unset)",
  1330                       dest ? dest : DBUS_SERVICE_DBUS);
  1331       _dbus_verbose ("security policy disallowing message due to sender policy\n");
  1332       return FALSE;
  1333     }
  1334 
  1335   if (recipient_policy &&
  1336       !bus_client_policy_check_can_receive (recipient_policy,
  1337                                             context->registry,
  1338                                             requested_reply,
  1339                                             sender,
  1340                                             addressed_recipient, proposed_recipient,
  1341                                             message))
  1342     {
  1343       const char *dest;
  1344 
  1345       dest = dbus_message_get_destination (message);
  1346       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
  1347                       "A security policy in place prevents this recipient "
  1348                       "from receiving this message from this sender, "
  1349                       "see message bus configuration file (rejected message "
  1350                       "had interface \"%s\" member \"%s\" error name \"%s\" destination \"%s\" reply serial %u requested_reply=%d)",
  1351                       dbus_message_get_interface (message) ?
  1352                       dbus_message_get_interface (message) : "(unset)",
  1353                       dbus_message_get_member (message) ?
  1354                       dbus_message_get_member (message) : "(unset)",
  1355                       dbus_message_get_error_name (message) ?
  1356                       dbus_message_get_error_name (message) : "(unset)",
  1357                       dest ? dest : DBUS_SERVICE_DBUS,
  1358                       dbus_message_get_reply_serial (message),
  1359                       requested_reply);
  1360       _dbus_verbose ("security policy disallowing message due to recipient policy\n");
  1361       return FALSE;
  1362     }
  1363 
  1364   /* See if limits on size have been exceeded */
  1365   if (proposed_recipient &&
  1366       dbus_connection_get_outgoing_size (proposed_recipient) >
  1367       context->limits.max_outgoing_bytes)
  1368     {
  1369       const char *dest;
  1370 
  1371       dest = dbus_message_get_destination (message);
  1372       dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
  1373                       "The destination service \"%s\" has a full message queue",
  1374                       dest ? dest : (proposed_recipient ?
  1375                                      bus_connection_get_name (proposed_recipient) : 
  1376                                      DBUS_SERVICE_DBUS));
  1377       _dbus_verbose ("security policy disallowing message due to full message queue\n");
  1378       return FALSE;
  1379     }
  1380 
  1381   /* Record that we will allow a reply here in the future (don't
  1382    * bother if the recipient is the bus or this is an eavesdropping
  1383    * connection). Only the addressed recipient may reply.
  1384    */
  1385   if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
  1386       sender && 
  1387       addressed_recipient &&
  1388       addressed_recipient == proposed_recipient && /* not eavesdropping */
  1389       !bus_connections_expect_reply (bus_connection_get_connections (sender),
  1390                                      transaction,
  1391                                      sender, addressed_recipient,
  1392                                      message, error))
  1393     {
  1394       _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
  1395       return FALSE;
  1396     }
  1397   
  1398   _dbus_verbose ("security policy allowing message\n");
  1399   return TRUE;
  1400 }