Merge branch 'doxygenate' into dbus-1.4
[platform/upstream/dbus.git] / bus / bus.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* bus.c  message bus context object
3  *
4  * Copyright (C) 2003, 2004 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include <config.h>
25 #include "bus.h"
26 #include "activation.h"
27 #include "connection.h"
28 #include "services.h"
29 #include "utils.h"
30 #include "policy.h"
31 #include "config-parser.h"
32 #include "signals.h"
33 #include "selinux.h"
34 #include "dir-watch.h"
35 #include <dbus/dbus-list.h>
36 #include <dbus/dbus-hash.h>
37 #include <dbus/dbus-credentials.h>
38 #include <dbus/dbus-internals.h>
39 #ifdef DBUS_CYGWIN
40 #include <signal.h>
41 #endif
42
43 struct BusContext
44 {
45   int refcount;
46   DBusGUID uuid;
47   char *config_file;
48   char *type;
49   char *servicehelper;
50   char *address;
51   char *pidfile;
52   char *user;
53   char *log_prefix;
54   DBusLoop *loop;
55   DBusList *servers;
56   BusConnections *connections;
57   BusActivation *activation;
58   BusRegistry *registry;
59   BusPolicy *policy;
60   BusMatchmaker *matchmaker;
61   BusLimits limits;
62   unsigned int fork : 1;
63   unsigned int syslog : 1;
64   unsigned int keep_umask : 1;
65   unsigned int allow_anonymous : 1;
66   unsigned int systemd_activation : 1;
67 };
68
69 static dbus_int32_t server_data_slot = -1;
70
71 typedef struct
72 {
73   BusContext *context;
74 } BusServerData;
75
76 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
77
78 static BusContext*
79 server_get_context (DBusServer *server)
80 {
81   BusContext *context;
82   BusServerData *bd;
83
84   if (!dbus_server_allocate_data_slot (&server_data_slot))
85     return NULL;
86
87   bd = BUS_SERVER_DATA (server);
88   if (bd == NULL)
89     {
90       dbus_server_free_data_slot (&server_data_slot);
91       return NULL;
92     }
93
94   context = bd->context;
95
96   dbus_server_free_data_slot (&server_data_slot);
97
98   return context;
99 }
100
101 static dbus_bool_t
102 server_watch_callback (DBusWatch     *watch,
103                        unsigned int   condition,
104                        void          *data)
105 {
106   /* FIXME this can be done in dbus-mainloop.c
107    * if the code in activation.c for the babysitter
108    * watch handler is fixed.
109    */
110
111   return dbus_watch_handle (watch, condition);
112 }
113
114 static dbus_bool_t
115 add_server_watch (DBusWatch  *watch,
116                   void       *data)
117 {
118   DBusServer *server = data;
119   BusContext *context;
120
121   context = server_get_context (server);
122
123   return _dbus_loop_add_watch (context->loop,
124                                watch, server_watch_callback, server,
125                                NULL);
126 }
127
128 static void
129 remove_server_watch (DBusWatch  *watch,
130                      void       *data)
131 {
132   DBusServer *server = data;
133   BusContext *context;
134
135   context = server_get_context (server);
136
137   _dbus_loop_remove_watch (context->loop,
138                            watch, server_watch_callback, server);
139 }
140
141
142 static void
143 server_timeout_callback (DBusTimeout   *timeout,
144                          void          *data)
145 {
146   /* can return FALSE on OOM but we just let it fire again later */
147   dbus_timeout_handle (timeout);
148 }
149
150 static dbus_bool_t
151 add_server_timeout (DBusTimeout *timeout,
152                     void        *data)
153 {
154   DBusServer *server = data;
155   BusContext *context;
156
157   context = server_get_context (server);
158
159   return _dbus_loop_add_timeout (context->loop,
160                                  timeout, server_timeout_callback, server, NULL);
161 }
162
163 static void
164 remove_server_timeout (DBusTimeout *timeout,
165                        void        *data)
166 {
167   DBusServer *server = data;
168   BusContext *context;
169
170   context = server_get_context (server);
171
172   _dbus_loop_remove_timeout (context->loop,
173                              timeout, server_timeout_callback, server);
174 }
175
176 static void
177 new_connection_callback (DBusServer     *server,
178                          DBusConnection *new_connection,
179                          void           *data)
180 {
181   BusContext *context = data;
182
183   if (!bus_connections_setup_connection (context->connections, new_connection))
184     {
185       _dbus_verbose ("No memory to setup new connection\n");
186
187       /* if we don't do this, it will get unref'd without
188        * being disconnected... kind of strange really
189        * that we have to do this, people won't get it right
190        * in general.
191        */
192       dbus_connection_close (new_connection);
193     }
194
195   dbus_connection_set_max_received_size (new_connection,
196                                          context->limits.max_incoming_bytes);
197
198   dbus_connection_set_max_message_size (new_connection,
199                                         context->limits.max_message_size);
200
201   dbus_connection_set_max_received_unix_fds (new_connection,
202                                          context->limits.max_incoming_unix_fds);
203
204   dbus_connection_set_max_message_unix_fds (new_connection,
205                                         context->limits.max_message_unix_fds);
206
207   dbus_connection_set_allow_anonymous (new_connection,
208                                        context->allow_anonymous);
209
210   /* on OOM, we won't have ref'd the connection so it will die. */
211 }
212
213 static void
214 free_server_data (void *data)
215 {
216   BusServerData *bd = data;
217
218   dbus_free (bd);
219 }
220
221 static dbus_bool_t
222 setup_server (BusContext *context,
223               DBusServer *server,
224               char      **auth_mechanisms,
225               DBusError  *error)
226 {
227   BusServerData *bd;
228
229   bd = dbus_new0 (BusServerData, 1);
230   if (bd == NULL || !dbus_server_set_data (server,
231                                            server_data_slot,
232                                            bd, free_server_data))
233     {
234       dbus_free (bd);
235       BUS_SET_OOM (error);
236       return FALSE;
237     }
238
239   bd->context = context;
240
241   if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
242     {
243       BUS_SET_OOM (error);
244       return FALSE;
245     }
246
247   dbus_server_set_new_connection_function (server,
248                                            new_connection_callback,
249                                            context, NULL);
250
251   if (!dbus_server_set_watch_functions (server,
252                                         add_server_watch,
253                                         remove_server_watch,
254                                         NULL,
255                                         server,
256                                         NULL))
257     {
258       BUS_SET_OOM (error);
259       return FALSE;
260     }
261
262   if (!dbus_server_set_timeout_functions (server,
263                                           add_server_timeout,
264                                           remove_server_timeout,
265                                           NULL,
266                                           server, NULL))
267     {
268       BUS_SET_OOM (error);
269       return FALSE;
270     }
271
272   return TRUE;
273 }
274
275 /* This code only gets executed the first time the
276  * config files are parsed.  It is not executed
277  * when config files are reloaded.
278  */
279 static dbus_bool_t
280 process_config_first_time_only (BusContext       *context,
281                                 BusConfigParser  *parser,
282                                 const DBusString *address,
283                                 dbus_bool_t      systemd_activation,
284                                 DBusError        *error)
285 {
286   DBusString log_prefix;
287   DBusList *link;
288   DBusList **addresses;
289   const char *user, *pidfile;
290   char **auth_mechanisms;
291   DBusList **auth_mechanisms_list;
292   int len;
293   dbus_bool_t retval;
294
295   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
296
297   retval = FALSE;
298   auth_mechanisms = NULL;
299
300   context->systemd_activation = systemd_activation;
301
302   /* Check for an existing pid file. Of course this is a race;
303    * we'd have to use fcntl() locks on the pid file to
304    * avoid that. But we want to check for the pid file
305    * before overwriting any existing sockets, etc.
306    */
307   pidfile = bus_config_parser_get_pidfile (parser);
308   if (pidfile != NULL)
309     {
310       DBusString u;
311       DBusStat stbuf;
312
313       _dbus_string_init_const (&u, pidfile);
314
315       if (_dbus_stat (&u, &stbuf, NULL))
316         {
317 #ifdef DBUS_CYGWIN
318           DBusString p;
319           long /* int */ pid;
320
321           _dbus_string_init (&p);
322           _dbus_file_get_contents(&p, &u, NULL);
323           _dbus_string_parse_int(&p, 0, &pid, NULL);
324           _dbus_string_free(&p);
325
326           if ((kill((int)pid, 0))) {
327             dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
328                            "pid %ld not running, removing stale pid file\n",
329                            pid);
330             _dbus_delete_file(&u, NULL);
331           } else {
332 #endif
333           dbus_set_error (error, DBUS_ERROR_FAILED,
334                                   "The pid file \"%s\" exists, if the message bus is not running, remove this file",
335                           pidfile);
336               goto failed;
337 #ifdef DBUS_CYGWIN
338           }
339 #endif
340         }
341     }
342
343   /* keep around the pid filename so we can delete it later */
344   context->pidfile = _dbus_strdup (pidfile);
345
346   /* note that type may be NULL */
347   context->type = _dbus_strdup (bus_config_parser_get_type (parser));
348   if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
349     goto oom;
350
351   user = bus_config_parser_get_user (parser);
352   if (user != NULL)
353     {
354       context->user = _dbus_strdup (user);
355       if (context->user == NULL)
356         goto oom;
357     }
358
359   /* Set up the prefix for syslog messages */
360   if (!_dbus_string_init (&log_prefix))
361     goto oom;
362   if (context->type && !strcmp (context->type, "system"))
363     {
364       if (!_dbus_string_append (&log_prefix, "[system] "))
365         goto oom;
366     }
367   else if (context->type && !strcmp (context->type, "session"))
368     {
369       DBusCredentials *credentials;
370
371       credentials = _dbus_credentials_new_from_current_process ();
372       if (!credentials)
373         goto oom;
374       if (!_dbus_string_append (&log_prefix, "[session "))
375         {
376           _dbus_credentials_unref (credentials);
377           goto oom;
378         }
379       if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
380         {
381           _dbus_credentials_unref (credentials);
382           goto oom;
383         }
384       if (!_dbus_string_append (&log_prefix, "] "))
385         {
386           _dbus_credentials_unref (credentials);
387           goto oom;
388         }
389       _dbus_credentials_unref (credentials);
390     }
391   if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
392     goto oom;
393   _dbus_string_free (&log_prefix);
394
395   /* Build an array of auth mechanisms */
396
397   auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
398   len = _dbus_list_get_length (auth_mechanisms_list);
399
400   if (len > 0)
401     {
402       int i;
403
404       auth_mechanisms = dbus_new0 (char*, len + 1);
405       if (auth_mechanisms == NULL)
406         goto oom;
407
408       i = 0;
409       link = _dbus_list_get_first_link (auth_mechanisms_list);
410       while (link != NULL)
411         {
412           auth_mechanisms[i] = _dbus_strdup (link->data);
413           if (auth_mechanisms[i] == NULL)
414             goto oom;
415           link = _dbus_list_get_next_link (auth_mechanisms_list, link);
416         }
417     }
418   else
419     {
420       auth_mechanisms = NULL;
421     }
422
423   /* Listen on our addresses */
424
425   if (address)
426     {
427       DBusServer *server;
428
429       server = dbus_server_listen (_dbus_string_get_const_data(address), error);
430       if (server == NULL)
431         {
432           _DBUS_ASSERT_ERROR_IS_SET (error);
433           goto failed;
434         }
435       else if (!setup_server (context, server, auth_mechanisms, error))
436         {
437           _DBUS_ASSERT_ERROR_IS_SET (error);
438           goto failed;
439         }
440
441       if (!_dbus_list_append (&context->servers, server))
442         goto oom;
443     }
444   else
445     {
446       addresses = bus_config_parser_get_addresses (parser);
447
448       link = _dbus_list_get_first_link (addresses);
449       while (link != NULL)
450         {
451           DBusServer *server;
452
453           server = dbus_server_listen (link->data, error);
454           if (server == NULL)
455             {
456               _DBUS_ASSERT_ERROR_IS_SET (error);
457               goto failed;
458             }
459           else if (!setup_server (context, server, auth_mechanisms, error))
460             {
461               _DBUS_ASSERT_ERROR_IS_SET (error);
462               goto failed;
463             }
464
465           if (!_dbus_list_append (&context->servers, server))
466             goto oom;
467
468           link = _dbus_list_get_next_link (addresses, link);
469         }
470     }
471
472   context->fork = bus_config_parser_get_fork (parser);
473   context->syslog = bus_config_parser_get_syslog (parser);
474   context->keep_umask = bus_config_parser_get_keep_umask (parser);
475   context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
476
477   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
478   retval = TRUE;
479
480  failed:
481   dbus_free_string_array (auth_mechanisms);
482   return retval;
483
484  oom:
485   BUS_SET_OOM (error);
486   dbus_free_string_array (auth_mechanisms);
487   return FALSE;
488 }
489
490 /* This code gets executed every time the config files
491  * are parsed: both during BusContext construction
492  * and on reloads. This function is slightly screwy
493  * since it can do a "half reload" in out-of-memory
494  * situations. Realistically, unlikely to ever matter.
495  */
496 static dbus_bool_t
497 process_config_every_time (BusContext      *context,
498                            BusConfigParser *parser,
499                            dbus_bool_t      is_reload,
500                            DBusError       *error)
501 {
502   DBusString full_address;
503   DBusList *link;
504   DBusList **dirs;
505   BusActivation *new_activation;
506   char *addr;
507   const char *servicehelper;
508   char *s;
509
510   dbus_bool_t retval;
511
512   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
513
514   addr = NULL;
515   retval = FALSE;
516
517   if (!_dbus_string_init (&full_address))
518     {
519       BUS_SET_OOM (error);
520       return FALSE;
521     }
522
523   /* get our limits and timeout lengths */
524   bus_config_parser_get_limits (parser, &context->limits);
525
526   if (context->policy)
527     bus_policy_unref (context->policy);
528   context->policy = bus_config_parser_steal_policy (parser);
529   _dbus_assert (context->policy != NULL);
530
531   /* We have to build the address backward, so that
532    * <listen> later in the config file have priority
533    */
534   link = _dbus_list_get_last_link (&context->servers);
535   while (link != NULL)
536     {
537       addr = dbus_server_get_address (link->data);
538       if (addr == NULL)
539         {
540           BUS_SET_OOM (error);
541           goto failed;
542         }
543
544       if (_dbus_string_get_length (&full_address) > 0)
545         {
546           if (!_dbus_string_append (&full_address, ";"))
547             {
548               BUS_SET_OOM (error);
549               goto failed;
550             }
551         }
552
553       if (!_dbus_string_append (&full_address, addr))
554         {
555           BUS_SET_OOM (error);
556           goto failed;
557         }
558
559       dbus_free (addr);
560       addr = NULL;
561
562       link = _dbus_list_get_prev_link (&context->servers, link);
563     }
564
565   if (is_reload)
566     dbus_free (context->address);
567
568   if (!_dbus_string_copy_data (&full_address, &context->address))
569     {
570       BUS_SET_OOM (error);
571       goto failed;
572     }
573
574   /* get the service directories */
575   dirs = bus_config_parser_get_service_dirs (parser);
576
577   /* and the service helper */
578   servicehelper = bus_config_parser_get_servicehelper (parser);
579
580   s = _dbus_strdup(servicehelper);
581   if (s == NULL && servicehelper != NULL)
582     {
583       BUS_SET_OOM (error);
584       goto failed;
585     }
586   else
587     {
588       dbus_free(context->servicehelper);
589       context->servicehelper = s;
590     }
591
592   /* Create activation subsystem */
593   if (context->activation)
594     {
595       if (!bus_activation_reload (context->activation, &full_address, dirs, error))
596         goto failed;
597     }
598   else
599     {
600       context->activation = bus_activation_new (context, &full_address, dirs, error);
601     }
602
603   if (context->activation == NULL)
604     {
605       _DBUS_ASSERT_ERROR_IS_SET (error);
606       goto failed;
607     }
608
609   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
610   retval = TRUE;
611
612  failed:
613   _dbus_string_free (&full_address);
614
615   if (addr)
616     dbus_free (addr);
617
618   return retval;
619 }
620
621 static dbus_bool_t
622 list_concat_new (DBusList **a,
623                  DBusList **b,
624                  DBusList **result)
625 {
626   DBusList *link;
627
628   *result = NULL;
629
630   for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
631     {
632       if (!_dbus_list_append (result, link->data))
633         goto oom;
634     }
635   for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
636     {
637       if (!_dbus_list_append (result, link->data))
638         goto oom;
639     }
640
641   return TRUE;
642 oom:
643   _dbus_list_clear (result);
644   return FALSE;
645 }
646
647 static void
648 raise_file_descriptor_limit (BusContext      *context)
649 {
650
651   /* I just picked this out of thin air; we need some extra
652    * descriptors for things like any internal pipes we create,
653    * inotify, connections to SELinux, etc.
654    */
655   unsigned int arbitrary_extra_fds = 32;
656   unsigned int limit;
657
658   limit = context->limits.max_completed_connections +
659     context->limits.max_incomplete_connections
660     + arbitrary_extra_fds;
661
662   _dbus_request_file_descriptor_limit (limit);
663 }
664
665 static dbus_bool_t
666 process_config_postinit (BusContext      *context,
667                          BusConfigParser *parser,
668                          DBusError       *error)
669 {
670   DBusHashTable *service_context_table;
671   DBusList *watched_dirs = NULL;
672
673   raise_file_descriptor_limit (context);
674
675   service_context_table = bus_config_parser_steal_service_context_table (parser);
676   if (!bus_registry_set_service_context_table (context->registry,
677                                                service_context_table))
678     {
679       BUS_SET_OOM (error);
680       return FALSE;
681     }
682
683   _dbus_hash_table_unref (service_context_table);
684
685   /* We need to monitor both the configuration directories and directories
686    * containing .service files.
687    */
688   if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
689                         bus_config_parser_get_service_dirs (parser),
690                         &watched_dirs))
691     {
692       BUS_SET_OOM (error);
693       return FALSE;
694     }
695
696   bus_set_watched_dirs (context, &watched_dirs);
697
698   _dbus_list_clear (&watched_dirs);
699
700   return TRUE;
701 }
702
703 BusContext*
704 bus_context_new (const DBusString *config_file,
705                  ForceForkSetting  force_fork,
706                  DBusPipe         *print_addr_pipe,
707                  DBusPipe         *print_pid_pipe,
708                  const DBusString *address,
709                  dbus_bool_t      systemd_activation,
710                  DBusError        *error)
711 {
712   DBusString log_prefix;
713   BusContext *context;
714   BusConfigParser *parser;
715
716   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
717
718   context = NULL;
719   parser = NULL;
720
721   if (!dbus_server_allocate_data_slot (&server_data_slot))
722     {
723       BUS_SET_OOM (error);
724       return NULL;
725     }
726
727   context = dbus_new0 (BusContext, 1);
728   if (context == NULL)
729     {
730       BUS_SET_OOM (error);
731       goto failed;
732     }
733   context->refcount = 1;
734
735   _dbus_generate_uuid (&context->uuid);
736
737   if (!_dbus_string_copy_data (config_file, &context->config_file))
738     {
739       BUS_SET_OOM (error);
740       goto failed;
741     }
742
743   context->loop = _dbus_loop_new ();
744   if (context->loop == NULL)
745     {
746       BUS_SET_OOM (error);
747       goto failed;
748     }
749
750   context->registry = bus_registry_new (context);
751   if (context->registry == NULL)
752     {
753       BUS_SET_OOM (error);
754       goto failed;
755     }
756
757   parser = bus_config_load (config_file, TRUE, NULL, error);
758   if (parser == NULL)
759     {
760       _DBUS_ASSERT_ERROR_IS_SET (error);
761       goto failed;
762     }
763
764   if (!process_config_first_time_only (context, parser, address, systemd_activation, error))
765     {
766       _DBUS_ASSERT_ERROR_IS_SET (error);
767       goto failed;
768     }
769   if (!process_config_every_time (context, parser, FALSE, error))
770     {
771       _DBUS_ASSERT_ERROR_IS_SET (error);
772       goto failed;
773     }
774
775   /* we need another ref of the server data slot for the context
776    * to own
777    */
778   if (!dbus_server_allocate_data_slot (&server_data_slot))
779     _dbus_assert_not_reached ("second ref of server data slot failed");
780
781   /* Note that we don't know whether the print_addr_pipe is
782    * one of the sockets we're using to listen on, or some
783    * other random thing. But I think the answer is "don't do
784    * that then"
785    */
786   if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
787     {
788       DBusString addr;
789       const char *a = bus_context_get_address (context);
790       int bytes;
791
792       _dbus_assert (a != NULL);
793       if (!_dbus_string_init (&addr))
794         {
795           BUS_SET_OOM (error);
796           goto failed;
797         }
798
799       if (!_dbus_string_append (&addr, a) ||
800           !_dbus_string_append (&addr, "\n"))
801         {
802           _dbus_string_free (&addr);
803           BUS_SET_OOM (error);
804           goto failed;
805         }
806
807       bytes = _dbus_string_get_length (&addr);
808       if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
809         {
810           /* pipe write returns an error on failure but not short write */
811           if (error != NULL && !dbus_error_is_set (error))
812             {
813               dbus_set_error (error, DBUS_ERROR_FAILED,
814                               "Printing message bus address: did not write all bytes\n");
815             }
816           _dbus_string_free (&addr);
817           goto failed;
818         }
819
820       if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
821         _dbus_pipe_close (print_addr_pipe, NULL);
822
823       _dbus_string_free (&addr);
824     }
825
826   context->connections = bus_connections_new (context);
827   if (context->connections == NULL)
828     {
829       BUS_SET_OOM (error);
830       goto failed;
831     }
832
833   context->matchmaker = bus_matchmaker_new ();
834   if (context->matchmaker == NULL)
835     {
836       BUS_SET_OOM (error);
837       goto failed;
838     }
839
840   /* check user before we fork */
841   if (context->user != NULL)
842     {
843       if (!_dbus_verify_daemon_user (context->user))
844         {
845           dbus_set_error (error, DBUS_ERROR_FAILED,
846                           "Could not get UID and GID for username \"%s\"",
847                           context->user);
848           goto failed;
849         }
850     }
851
852   /* Now become a daemon if appropriate and write out pid file in any case */
853   {
854     DBusString u;
855
856     if (context->pidfile)
857       _dbus_string_init_const (&u, context->pidfile);
858
859     if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
860       {
861         _dbus_verbose ("Forking and becoming daemon\n");
862
863         if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
864                                   print_pid_pipe,
865                                   error,
866                                   context->keep_umask))
867           {
868             _DBUS_ASSERT_ERROR_IS_SET (error);
869             goto failed;
870           }
871       }
872     else
873       {
874         _dbus_verbose ("Fork not requested\n");
875
876         /* Need to write PID file and to PID pipe for ourselves,
877          * not for the child process. This is a no-op if the pidfile
878          * is NULL and print_pid_pipe is NULL.
879          */
880         if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
881                                                print_pid_pipe,
882                                                _dbus_getpid (),
883                                                error))
884           {
885             _DBUS_ASSERT_ERROR_IS_SET (error);
886             goto failed;
887           }
888       }
889   }
890
891   if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
892       !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
893     _dbus_pipe_close (print_pid_pipe, NULL);
894
895   if (!bus_selinux_full_init ())
896     {
897       bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
898     }
899
900   if (!process_config_postinit (context, parser, error))
901     {
902       _DBUS_ASSERT_ERROR_IS_SET (error);
903       goto failed;
904     }
905
906   if (parser != NULL)
907     {
908       bus_config_parser_unref (parser);
909       parser = NULL;
910     }
911
912   /* Here we change our credentials if required,
913    * as soon as we've set up our sockets and pidfile
914    */
915   if (context->user != NULL)
916     {
917       if (!_dbus_change_to_daemon_user (context->user, error))
918         {
919           _DBUS_ASSERT_ERROR_IS_SET (error);
920           goto failed;
921         }
922
923 #ifdef HAVE_SELINUX
924       /* FIXME - why not just put this in full_init() below? */
925       bus_selinux_audit_init ();
926 #endif
927     }
928
929   dbus_server_free_data_slot (&server_data_slot);
930
931   return context;
932
933  failed:
934   if (parser != NULL)
935     bus_config_parser_unref (parser);
936   if (context != NULL)
937     bus_context_unref (context);
938
939   if (server_data_slot >= 0)
940     dbus_server_free_data_slot (&server_data_slot);
941
942   return NULL;
943 }
944
945 dbus_bool_t
946 bus_context_get_id (BusContext       *context,
947                     DBusString       *uuid)
948 {
949   return _dbus_uuid_encode (&context->uuid, uuid);
950 }
951
952 dbus_bool_t
953 bus_context_reload_config (BusContext *context,
954                            DBusError  *error)
955 {
956   BusConfigParser *parser;
957   DBusString config_file;
958   dbus_bool_t ret;
959
960   /* Flush the user database cache */
961   _dbus_flush_caches ();
962
963   ret = FALSE;
964   _dbus_string_init_const (&config_file, context->config_file);
965   parser = bus_config_load (&config_file, TRUE, NULL, error);
966   if (parser == NULL)
967     {
968       _DBUS_ASSERT_ERROR_IS_SET (error);
969       goto failed;
970     }
971
972   if (!process_config_every_time (context, parser, TRUE, error))
973     {
974       _DBUS_ASSERT_ERROR_IS_SET (error);
975       goto failed;
976     }
977   if (!process_config_postinit (context, parser, error))
978     {
979       _DBUS_ASSERT_ERROR_IS_SET (error);
980       goto failed;
981     }
982   ret = TRUE;
983
984   bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
985  failed:
986   if (!ret)
987     bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
988   if (parser != NULL)
989     bus_config_parser_unref (parser);
990   return ret;
991 }
992
993 static void
994 shutdown_server (BusContext *context,
995                  DBusServer *server)
996 {
997   if (server == NULL ||
998       !dbus_server_get_is_connected (server))
999     return;
1000
1001   if (!dbus_server_set_watch_functions (server,
1002                                         NULL, NULL, NULL,
1003                                         context,
1004                                         NULL))
1005     _dbus_assert_not_reached ("setting watch functions to NULL failed");
1006
1007   if (!dbus_server_set_timeout_functions (server,
1008                                           NULL, NULL, NULL,
1009                                           context,
1010                                           NULL))
1011     _dbus_assert_not_reached ("setting timeout functions to NULL failed");
1012
1013   dbus_server_disconnect (server);
1014 }
1015
1016 void
1017 bus_context_shutdown (BusContext  *context)
1018 {
1019   DBusList *link;
1020
1021   link = _dbus_list_get_first_link (&context->servers);
1022   while (link != NULL)
1023     {
1024       shutdown_server (context, link->data);
1025
1026       link = _dbus_list_get_next_link (&context->servers, link);
1027     }
1028 }
1029
1030 BusContext *
1031 bus_context_ref (BusContext *context)
1032 {
1033   _dbus_assert (context->refcount > 0);
1034   context->refcount += 1;
1035
1036   return context;
1037 }
1038
1039 void
1040 bus_context_unref (BusContext *context)
1041 {
1042   _dbus_assert (context->refcount > 0);
1043   context->refcount -= 1;
1044
1045   if (context->refcount == 0)
1046     {
1047       DBusList *link;
1048
1049       _dbus_verbose ("Finalizing bus context %p\n", context);
1050
1051       bus_context_shutdown (context);
1052
1053       if (context->connections)
1054         {
1055           bus_connections_unref (context->connections);
1056           context->connections = NULL;
1057         }
1058
1059       if (context->registry)
1060         {
1061           bus_registry_unref (context->registry);
1062           context->registry = NULL;
1063         }
1064
1065       if (context->activation)
1066         {
1067           bus_activation_unref (context->activation);
1068           context->activation = NULL;
1069         }
1070
1071       link = _dbus_list_get_first_link (&context->servers);
1072       while (link != NULL)
1073         {
1074           dbus_server_unref (link->data);
1075
1076           link = _dbus_list_get_next_link (&context->servers, link);
1077         }
1078       _dbus_list_clear (&context->servers);
1079
1080       if (context->policy)
1081         {
1082           bus_policy_unref (context->policy);
1083           context->policy = NULL;
1084         }
1085
1086       if (context->loop)
1087         {
1088           _dbus_loop_unref (context->loop);
1089           context->loop = NULL;
1090         }
1091
1092       if (context->matchmaker)
1093         {
1094           bus_matchmaker_unref (context->matchmaker);
1095           context->matchmaker = NULL;
1096         }
1097
1098       dbus_free (context->config_file);
1099       dbus_free (context->log_prefix);
1100       dbus_free (context->type);
1101       dbus_free (context->address);
1102       dbus_free (context->user);
1103       dbus_free (context->servicehelper);
1104
1105       if (context->pidfile)
1106         {
1107           DBusString u;
1108           _dbus_string_init_const (&u, context->pidfile);
1109
1110           /* Deliberately ignore errors here, since there's not much
1111            * we can do about it, and we're exiting anyways.
1112            */
1113           _dbus_delete_file (&u, NULL);
1114
1115           dbus_free (context->pidfile);
1116         }
1117       dbus_free (context);
1118
1119       dbus_server_free_data_slot (&server_data_slot);
1120     }
1121 }
1122
1123 /* type may be NULL */
1124 const char*
1125 bus_context_get_type (BusContext *context)
1126 {
1127   return context->type;
1128 }
1129
1130 const char*
1131 bus_context_get_address (BusContext *context)
1132 {
1133   return context->address;
1134 }
1135
1136 const char*
1137 bus_context_get_servicehelper (BusContext *context)
1138 {
1139   return context->servicehelper;
1140 }
1141
1142 dbus_bool_t
1143 bus_context_get_systemd_activation (BusContext *context)
1144 {
1145   return context->systemd_activation;
1146 }
1147
1148 BusRegistry*
1149 bus_context_get_registry (BusContext  *context)
1150 {
1151   return context->registry;
1152 }
1153
1154 BusConnections*
1155 bus_context_get_connections (BusContext  *context)
1156 {
1157   return context->connections;
1158 }
1159
1160 BusActivation*
1161 bus_context_get_activation (BusContext  *context)
1162 {
1163   return context->activation;
1164 }
1165
1166 BusMatchmaker*
1167 bus_context_get_matchmaker (BusContext  *context)
1168 {
1169   return context->matchmaker;
1170 }
1171
1172 DBusLoop*
1173 bus_context_get_loop (BusContext *context)
1174 {
1175   return context->loop;
1176 }
1177
1178 dbus_bool_t
1179 bus_context_allow_unix_user (BusContext   *context,
1180                              unsigned long uid)
1181 {
1182   return bus_policy_allow_unix_user (context->policy,
1183                                      uid);
1184 }
1185
1186 /* For now this is never actually called because the default
1187  * DBusConnection behavior of 'same user that owns the bus can connect'
1188  * is all it would do.
1189  */
1190 dbus_bool_t
1191 bus_context_allow_windows_user (BusContext       *context,
1192                                 const char       *windows_sid)
1193 {
1194   return bus_policy_allow_windows_user (context->policy,
1195                                         windows_sid);
1196 }
1197
1198 BusPolicy *
1199 bus_context_get_policy (BusContext *context)
1200 {
1201   return context->policy;
1202 }
1203
1204 BusClientPolicy*
1205 bus_context_create_client_policy (BusContext      *context,
1206                                   DBusConnection  *connection,
1207                                   DBusError       *error)
1208 {
1209   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1210   return bus_policy_create_client_policy (context->policy, connection,
1211                                           error);
1212 }
1213
1214 int
1215 bus_context_get_activation_timeout (BusContext *context)
1216 {
1217
1218   return context->limits.activation_timeout;
1219 }
1220
1221 int
1222 bus_context_get_auth_timeout (BusContext *context)
1223 {
1224   return context->limits.auth_timeout;
1225 }
1226
1227 int
1228 bus_context_get_max_completed_connections (BusContext *context)
1229 {
1230   return context->limits.max_completed_connections;
1231 }
1232
1233 int
1234 bus_context_get_max_incomplete_connections (BusContext *context)
1235 {
1236   return context->limits.max_incomplete_connections;
1237 }
1238
1239 int
1240 bus_context_get_max_connections_per_user (BusContext *context)
1241 {
1242   return context->limits.max_connections_per_user;
1243 }
1244
1245 int
1246 bus_context_get_max_pending_activations (BusContext *context)
1247 {
1248   return context->limits.max_pending_activations;
1249 }
1250
1251 int
1252 bus_context_get_max_services_per_connection (BusContext *context)
1253 {
1254   return context->limits.max_services_per_connection;
1255 }
1256
1257 int
1258 bus_context_get_max_match_rules_per_connection (BusContext *context)
1259 {
1260   return context->limits.max_match_rules_per_connection;
1261 }
1262
1263 int
1264 bus_context_get_max_replies_per_connection (BusContext *context)
1265 {
1266   return context->limits.max_replies_per_connection;
1267 }
1268
1269 int
1270 bus_context_get_reply_timeout (BusContext *context)
1271 {
1272   return context->limits.reply_timeout;
1273 }
1274
1275 void
1276 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1277
1278 void
1279 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1280 {
1281   va_list args;
1282
1283   if (!context->syslog)
1284     return;
1285
1286   va_start (args, msg);
1287
1288   if (context->log_prefix)
1289     {
1290       DBusString full_msg;
1291
1292       if (!_dbus_string_init (&full_msg))
1293         goto out;
1294       if (!_dbus_string_append (&full_msg, context->log_prefix))
1295         goto oom_out;
1296       if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1297         goto oom_out;
1298
1299       _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1300     oom_out:
1301       _dbus_string_free (&full_msg);
1302     }
1303   else
1304     _dbus_system_logv (severity, msg, args);
1305
1306 out:
1307   va_end (args);
1308 }
1309
1310 /*
1311  * addressed_recipient is the recipient specified in the message.
1312  *
1313  * proposed_recipient is the recipient we're considering sending
1314  * to right this second, and may be an eavesdropper.
1315  *
1316  * sender is the sender of the message.
1317  *
1318  * NULL for proposed_recipient or sender definitely means the bus driver.
1319  *
1320  * NULL for addressed_recipient may mean the bus driver, or may mean
1321  * no destination was specified in the message (e.g. a signal).
1322  */
1323 dbus_bool_t
1324 bus_context_check_security_policy (BusContext     *context,
1325                                    BusTransaction *transaction,
1326                                    DBusConnection *sender,
1327                                    DBusConnection *addressed_recipient,
1328                                    DBusConnection *proposed_recipient,
1329                                    DBusMessage    *message,
1330                                    DBusError      *error)
1331 {
1332   const char *dest;
1333   BusClientPolicy *sender_policy;
1334   BusClientPolicy *recipient_policy;
1335   dbus_int32_t toggles;
1336   dbus_bool_t log;
1337   int type;
1338   dbus_bool_t requested_reply;
1339   const char *sender_name;
1340   const char *sender_loginfo;
1341   const char *proposed_recipient_loginfo;
1342
1343   type = dbus_message_get_type (message);
1344   dest = dbus_message_get_destination (message);
1345
1346   /* dispatch.c was supposed to ensure these invariants */
1347   _dbus_assert (dest != NULL ||
1348                 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1349                 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1350   _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1351                 addressed_recipient != NULL ||
1352                 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1353
1354   /* Used in logging below */
1355   if (sender != NULL)
1356     {
1357       sender_name = bus_connection_get_name (sender);
1358       sender_loginfo = bus_connection_get_loginfo (sender);
1359     }
1360   else
1361     {
1362       sender_name = NULL;
1363       sender_loginfo = "(bus)";
1364     }
1365
1366   if (proposed_recipient != NULL)
1367     proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1368   else
1369     proposed_recipient_loginfo = "bus";
1370
1371   switch (type)
1372     {
1373     case DBUS_MESSAGE_TYPE_METHOD_CALL:
1374     case DBUS_MESSAGE_TYPE_SIGNAL:
1375     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1376     case DBUS_MESSAGE_TYPE_ERROR:
1377       break;
1378
1379     default:
1380       _dbus_verbose ("security check disallowing message of unknown type %d\n",
1381                      type);
1382
1383       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1384                       "Message bus will not accept messages of unknown type\n");
1385
1386       return FALSE;
1387     }
1388
1389   requested_reply = FALSE;
1390
1391   if (sender != NULL)
1392     {
1393       /* First verify the SELinux access controls.  If allowed then
1394        * go on with the standard checks.
1395        */
1396       if (!bus_selinux_allows_send (sender, proposed_recipient,
1397                                     dbus_message_type_to_string (dbus_message_get_type (message)),
1398                                     dbus_message_get_interface (message),
1399                                     dbus_message_get_member (message),
1400                                     dbus_message_get_error_name (message),
1401                                     dest ? dest : DBUS_SERVICE_DBUS, error))
1402         {
1403           if (error != NULL && !dbus_error_is_set (error))
1404             {
1405               dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1406                               "An SELinux policy prevents this sender "
1407                               "from sending this message to this recipient "
1408                               "(rejected message had sender \"%s\" interface \"%s\" "
1409                               "member \"%s\" error name \"%s\" destination \"%s\")",
1410                               sender_name ? sender_name : "(unset)",
1411                               dbus_message_get_interface (message) ?
1412                               dbus_message_get_interface (message) : "(unset)",
1413                               dbus_message_get_member (message) ?
1414                               dbus_message_get_member (message) : "(unset)",
1415                               dbus_message_get_error_name (message) ?
1416                               dbus_message_get_error_name (message) : "(unset)",
1417                               dest ? dest : DBUS_SERVICE_DBUS);
1418               _dbus_verbose ("SELinux security check denying send to service\n");
1419             }
1420
1421           return FALSE;
1422         }
1423
1424       if (bus_connection_is_active (sender))
1425         {
1426           sender_policy = bus_connection_get_policy (sender);
1427           _dbus_assert (sender_policy != NULL);
1428
1429           /* Fill in requested_reply variable with TRUE if this is a
1430            * reply and the reply was pending.
1431            */
1432           if (dbus_message_get_reply_serial (message) != 0)
1433             {
1434               if (proposed_recipient != NULL /* not to the bus driver */ &&
1435                   addressed_recipient == proposed_recipient /* not eavesdropping */)
1436                 {
1437                   DBusError error2;
1438
1439                   dbus_error_init (&error2);
1440                   requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1441                                                                  transaction,
1442                                                                  sender, addressed_recipient, message,
1443                                                                  &error2);
1444                   if (dbus_error_is_set (&error2))
1445                     {
1446                       dbus_move_error (&error2, error);
1447                       return FALSE;
1448                     }
1449                 }
1450             }
1451         }
1452       else
1453         {
1454           /* Policy for inactive connections is that they can only send
1455            * the hello message to the bus driver
1456            */
1457           if (proposed_recipient == NULL &&
1458               dbus_message_is_method_call (message,
1459                                            DBUS_INTERFACE_DBUS,
1460                                            "Hello"))
1461             {
1462               _dbus_verbose ("security check allowing %s message\n",
1463                              "Hello");
1464               return TRUE;
1465             }
1466           else
1467             {
1468               _dbus_verbose ("security check disallowing non-%s message\n",
1469                              "Hello");
1470
1471               dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1472                               "Client tried to send a message other than %s without being registered",
1473                               "Hello");
1474
1475               return FALSE;
1476             }
1477         }
1478     }
1479   else
1480     {
1481       sender_policy = NULL;
1482
1483       /* If the sender is the bus driver, we assume any reply was a
1484        * requested reply as bus driver won't send bogus ones
1485        */
1486       if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1487           dbus_message_get_reply_serial (message) != 0)
1488         requested_reply = TRUE;
1489     }
1490
1491   _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1492                 (sender == NULL && sender_policy == NULL));
1493
1494   if (proposed_recipient != NULL)
1495     {
1496       /* only the bus driver can send to an inactive recipient (as it
1497        * owns no services, so other apps can't address it). Inactive
1498        * recipients can receive any message.
1499        */
1500       if (bus_connection_is_active (proposed_recipient))
1501         {
1502           recipient_policy = bus_connection_get_policy (proposed_recipient);
1503           _dbus_assert (recipient_policy != NULL);
1504         }
1505       else if (sender == NULL)
1506         {
1507           _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1508           recipient_policy = NULL;
1509         }
1510       else
1511         {
1512           _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1513           recipient_policy = NULL;
1514         }
1515     }
1516   else
1517     recipient_policy = NULL;
1518
1519   _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1520                 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1521                 (proposed_recipient == NULL && recipient_policy == NULL));
1522
1523   log = FALSE;
1524   if (sender_policy &&
1525       !bus_client_policy_check_can_send (sender_policy,
1526                                          context->registry,
1527                                          requested_reply,
1528                                          proposed_recipient,
1529                                          message, &toggles, &log))
1530     {
1531       const char *msg = "Rejected send message, %d matched rules; "
1532                         "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))";
1533
1534       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1535                       toggles,
1536                       dbus_message_type_to_string (dbus_message_get_type (message)),
1537                       sender_name ? sender_name : "(unset)",
1538                       sender_loginfo,
1539                       dbus_message_get_interface (message) ?
1540                       dbus_message_get_interface (message) : "(unset)",
1541                       dbus_message_get_member (message) ?
1542                       dbus_message_get_member (message) : "(unset)",
1543                       dbus_message_get_error_name (message) ?
1544                       dbus_message_get_error_name (message) : "(unset)",
1545                       requested_reply,
1546                       dest ? dest : DBUS_SERVICE_DBUS,
1547                       proposed_recipient_loginfo);
1548       /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1549       if (addressed_recipient == proposed_recipient)
1550         bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1551                                   toggles,
1552                                   dbus_message_type_to_string (dbus_message_get_type (message)),
1553                                   sender_name ? sender_name : "(unset)",
1554                                   sender_loginfo,
1555                                   dbus_message_get_interface (message) ?
1556                                   dbus_message_get_interface (message) : "(unset)",
1557                                   dbus_message_get_member (message) ?
1558                                   dbus_message_get_member (message) : "(unset)",
1559                                   dbus_message_get_error_name (message) ?
1560                                   dbus_message_get_error_name (message) : "(unset)",
1561                                   requested_reply,
1562                                   dest ? dest : DBUS_SERVICE_DBUS,
1563                                   proposed_recipient_loginfo);
1564       _dbus_verbose ("security policy disallowing message due to sender policy\n");
1565       return FALSE;
1566     }
1567
1568   if (log)
1569     bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY,
1570                               "Would reject message, %d matched rules; "
1571                               "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))",
1572                               toggles,
1573                               dbus_message_type_to_string (dbus_message_get_type (message)),
1574                               sender_name ? sender_name : "(unset)",
1575                               sender_loginfo,
1576                               dbus_message_get_interface (message) ?
1577                               dbus_message_get_interface (message) : "(unset)",
1578                               dbus_message_get_member (message) ?
1579                               dbus_message_get_member (message) : "(unset)",
1580                               dbus_message_get_error_name (message) ?
1581                               dbus_message_get_error_name (message) : "(unset)",
1582                               requested_reply,
1583                               dest ? dest : DBUS_SERVICE_DBUS,
1584                               proposed_recipient_loginfo);
1585
1586   if (recipient_policy &&
1587       !bus_client_policy_check_can_receive (recipient_policy,
1588                                             context->registry,
1589                                             requested_reply,
1590                                             sender,
1591                                             addressed_recipient, proposed_recipient,
1592                                             message, &toggles))
1593     {
1594       const char *msg = "Rejected receive message, %d matched rules; "
1595                         "type=\"%s\" sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" reply serial=%u requested_reply=%d destination=\"%s\" (%s))";
1596
1597       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1598                       toggles,
1599                       dbus_message_type_to_string (dbus_message_get_type (message)),
1600                       sender_name ? sender_name : "(unset)",
1601                       sender_loginfo,
1602                       dbus_message_get_interface (message) ?
1603                       dbus_message_get_interface (message) : "(unset)",
1604                       dbus_message_get_member (message) ?
1605                       dbus_message_get_member (message) : "(unset)",
1606                       dbus_message_get_error_name (message) ?
1607                       dbus_message_get_error_name (message) : "(unset)",
1608                       dbus_message_get_reply_serial (message),
1609                       requested_reply,
1610                       dest ? dest : DBUS_SERVICE_DBUS,
1611                       proposed_recipient_loginfo);
1612       /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1613       if (addressed_recipient == proposed_recipient)
1614         bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1615                                   toggles,
1616                                   dbus_message_type_to_string (dbus_message_get_type (message)),
1617                                   sender_name ? sender_name : "(unset)",
1618                                   sender_loginfo,
1619                                   dbus_message_get_interface (message) ?
1620                                   dbus_message_get_interface (message) : "(unset)",
1621                                   dbus_message_get_member (message) ?
1622                                   dbus_message_get_member (message) : "(unset)",
1623                                   dbus_message_get_error_name (message) ?
1624                                   dbus_message_get_error_name (message) : "(unset)",
1625                                   dbus_message_get_reply_serial (message),
1626                                   requested_reply,
1627                                   dest ? dest : DBUS_SERVICE_DBUS,
1628                                   proposed_recipient_loginfo);
1629       _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1630       return FALSE;
1631     }
1632
1633   /* See if limits on size have been exceeded */
1634   if (proposed_recipient &&
1635       ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1636        (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1637     {
1638       dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1639                       "The destination service \"%s\" has a full message queue",
1640                       dest ? dest : (proposed_recipient ?
1641                                      bus_connection_get_name (proposed_recipient) :
1642                                      DBUS_SERVICE_DBUS));
1643       _dbus_verbose ("security policy disallowing message due to full message queue\n");
1644       return FALSE;
1645     }
1646
1647   /* Record that we will allow a reply here in the future (don't
1648    * bother if the recipient is the bus or this is an eavesdropping
1649    * connection). Only the addressed recipient may reply.
1650    */
1651   if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1652       sender &&
1653       addressed_recipient &&
1654       addressed_recipient == proposed_recipient && /* not eavesdropping */
1655       !bus_connections_expect_reply (bus_connection_get_connections (sender),
1656                                      transaction,
1657                                      sender, addressed_recipient,
1658                                      message, error))
1659     {
1660       _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1661       return FALSE;
1662     }
1663
1664   _dbus_verbose ("security policy allowing message\n");
1665   return TRUE;
1666 }