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