DBusLoop: remove a layer of pointless abstraction around timeouts
[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
27 #include <stdio.h>
28
29 #include "activation.h"
30 #include "connection.h"
31 #include "services.h"
32 #include "utils.h"
33 #include "policy.h"
34 #include "config-parser.h"
35 #include "signals.h"
36 #include "selinux.h"
37 #include "dir-watch.h"
38 #include <dbus/dbus-list.h>
39 #include <dbus/dbus-hash.h>
40 #include <dbus/dbus-credentials.h>
41 #include <dbus/dbus-internals.h>
42
43 #ifdef DBUS_CYGWIN
44 #include <signal.h>
45 #endif
46
47 struct BusContext
48 {
49   int refcount;
50   DBusGUID uuid;
51   char *config_file;
52   char *type;
53   char *servicehelper;
54   char *address;
55   char *pidfile;
56   char *user;
57   char *log_prefix;
58   DBusLoop *loop;
59   DBusList *servers;
60   BusConnections *connections;
61   BusActivation *activation;
62   BusRegistry *registry;
63   BusPolicy *policy;
64   BusMatchmaker *matchmaker;
65   BusLimits limits;
66   unsigned int fork : 1;
67   unsigned int syslog : 1;
68   unsigned int keep_umask : 1;
69   unsigned int allow_anonymous : 1;
70   unsigned int systemd_activation : 1;
71 };
72
73 static dbus_int32_t server_data_slot = -1;
74
75 typedef struct
76 {
77   BusContext *context;
78 } BusServerData;
79
80 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
81
82 static BusContext*
83 server_get_context (DBusServer *server)
84 {
85   BusContext *context;
86   BusServerData *bd;
87
88   if (!dbus_server_allocate_data_slot (&server_data_slot))
89     return NULL;
90
91   bd = BUS_SERVER_DATA (server);
92   if (bd == NULL)
93     {
94       dbus_server_free_data_slot (&server_data_slot);
95       return NULL;
96     }
97
98   context = bd->context;
99
100   dbus_server_free_data_slot (&server_data_slot);
101
102   return context;
103 }
104
105 static dbus_bool_t
106 server_watch_callback (DBusWatch     *watch,
107                        unsigned int   condition,
108                        void          *data)
109 {
110   /* FIXME this can be done in dbus-mainloop.c
111    * if the code in activation.c for the babysitter
112    * watch handler is fixed.
113    */
114
115   return dbus_watch_handle (watch, condition);
116 }
117
118 static dbus_bool_t
119 add_server_watch (DBusWatch  *watch,
120                   void       *data)
121 {
122   DBusServer *server = data;
123   BusContext *context;
124
125   context = server_get_context (server);
126
127   return _dbus_loop_add_watch (context->loop,
128                                watch, server_watch_callback, server,
129                                NULL);
130 }
131
132 static void
133 remove_server_watch (DBusWatch  *watch,
134                      void       *data)
135 {
136   DBusServer *server = data;
137   BusContext *context;
138
139   context = server_get_context (server);
140
141   _dbus_loop_remove_watch (context->loop,
142                            watch, server_watch_callback, server);
143 }
144
145 static dbus_bool_t
146 add_server_timeout (DBusTimeout *timeout,
147                     void        *data)
148 {
149   DBusServer *server = data;
150   BusContext *context;
151
152   context = server_get_context (server);
153
154   return _dbus_loop_add_timeout (context->loop, timeout);
155 }
156
157 static void
158 remove_server_timeout (DBusTimeout *timeout,
159                        void        *data)
160 {
161   DBusServer *server = data;
162   BusContext *context;
163
164   context = server_get_context (server);
165
166   _dbus_loop_remove_timeout (context->loop, timeout);
167 }
168
169 static void
170 new_connection_callback (DBusServer     *server,
171                          DBusConnection *new_connection,
172                          void           *data)
173 {
174   BusContext *context = data;
175
176   if (!bus_connections_setup_connection (context->connections, new_connection))
177     {
178       _dbus_verbose ("No memory to setup new connection\n");
179
180       /* if we don't do this, it will get unref'd without
181        * being disconnected... kind of strange really
182        * that we have to do this, people won't get it right
183        * in general.
184        */
185       dbus_connection_close (new_connection);
186     }
187
188   dbus_connection_set_max_received_size (new_connection,
189                                          context->limits.max_incoming_bytes);
190
191   dbus_connection_set_max_message_size (new_connection,
192                                         context->limits.max_message_size);
193
194   dbus_connection_set_max_received_unix_fds (new_connection,
195                                          context->limits.max_incoming_unix_fds);
196
197   dbus_connection_set_max_message_unix_fds (new_connection,
198                                         context->limits.max_message_unix_fds);
199
200   dbus_connection_set_allow_anonymous (new_connection,
201                                        context->allow_anonymous);
202
203   /* on OOM, we won't have ref'd the connection so it will die. */
204 }
205
206 static void
207 free_server_data (void *data)
208 {
209   BusServerData *bd = data;
210
211   dbus_free (bd);
212 }
213
214 static dbus_bool_t
215 setup_server (BusContext *context,
216               DBusServer *server,
217               char      **auth_mechanisms,
218               DBusError  *error)
219 {
220   BusServerData *bd;
221
222   bd = dbus_new0 (BusServerData, 1);
223   if (bd == NULL || !dbus_server_set_data (server,
224                                            server_data_slot,
225                                            bd, free_server_data))
226     {
227       dbus_free (bd);
228       BUS_SET_OOM (error);
229       return FALSE;
230     }
231
232   bd->context = context;
233
234   if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
235     {
236       BUS_SET_OOM (error);
237       return FALSE;
238     }
239
240   dbus_server_set_new_connection_function (server,
241                                            new_connection_callback,
242                                            context, NULL);
243
244   if (!dbus_server_set_watch_functions (server,
245                                         add_server_watch,
246                                         remove_server_watch,
247                                         NULL,
248                                         server,
249                                         NULL))
250     {
251       BUS_SET_OOM (error);
252       return FALSE;
253     }
254
255   if (!dbus_server_set_timeout_functions (server,
256                                           add_server_timeout,
257                                           remove_server_timeout,
258                                           NULL,
259                                           server, NULL))
260     {
261       BUS_SET_OOM (error);
262       return FALSE;
263     }
264
265   return TRUE;
266 }
267
268 /* This code only gets executed the first time the
269  * config files are parsed.  It is not executed
270  * when config files are reloaded.
271  */
272 static dbus_bool_t
273 process_config_first_time_only (BusContext       *context,
274                                 BusConfigParser  *parser,
275                                 const DBusString *address,
276                                 dbus_bool_t      systemd_activation,
277                                 DBusError        *error)
278 {
279   DBusString log_prefix;
280   DBusList *link;
281   DBusList **addresses;
282   const char *user, *pidfile;
283   char **auth_mechanisms;
284   DBusList **auth_mechanisms_list;
285   int len;
286   dbus_bool_t retval;
287
288   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
289
290   retval = FALSE;
291   auth_mechanisms = NULL;
292
293   _dbus_init_system_log ();
294
295   context->systemd_activation = systemd_activation;
296
297   /* Check for an existing pid file. Of course this is a race;
298    * we'd have to use fcntl() locks on the pid file to
299    * avoid that. But we want to check for the pid file
300    * before overwriting any existing sockets, etc.
301    */
302   pidfile = bus_config_parser_get_pidfile (parser);
303   if (pidfile != NULL)
304     {
305       DBusString u;
306       DBusStat stbuf;
307
308       _dbus_string_init_const (&u, pidfile);
309
310       if (_dbus_stat (&u, &stbuf, NULL))
311         {
312 #ifdef DBUS_CYGWIN
313           DBusString p;
314           long /* int */ pid;
315
316           _dbus_string_init (&p);
317           _dbus_file_get_contents(&p, &u, NULL);
318           _dbus_string_parse_int(&p, 0, &pid, NULL);
319           _dbus_string_free(&p);
320
321           if ((kill((int)pid, 0))) {
322             dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
323                            "pid %ld not running, removing stale pid file\n",
324                            pid);
325             _dbus_delete_file(&u, NULL);
326           } else {
327 #endif
328           dbus_set_error (error, DBUS_ERROR_FAILED,
329                                   "The pid file \"%s\" exists, if the message bus is not running, remove this file",
330                           pidfile);
331               goto failed;
332 #ifdef DBUS_CYGWIN
333           }
334 #endif
335         }
336     }
337
338   /* keep around the pid filename so we can delete it later */
339   context->pidfile = _dbus_strdup (pidfile);
340
341   /* note that type may be NULL */
342   context->type = _dbus_strdup (bus_config_parser_get_type (parser));
343   if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
344     goto oom;
345
346   user = bus_config_parser_get_user (parser);
347   if (user != NULL)
348     {
349       context->user = _dbus_strdup (user);
350       if (context->user == NULL)
351         goto oom;
352     }
353
354   /* Set up the prefix for syslog messages */
355   if (!_dbus_string_init (&log_prefix))
356     goto oom;
357   if (context->type && !strcmp (context->type, "system"))
358     {
359       if (!_dbus_string_append (&log_prefix, "[system] "))
360         goto oom;
361     }
362   else if (context->type && !strcmp (context->type, "session"))
363     {
364       DBusCredentials *credentials;
365
366       credentials = _dbus_credentials_new_from_current_process ();
367       if (!credentials)
368         goto oom;
369       if (!_dbus_string_append (&log_prefix, "[session "))
370         {
371           _dbus_credentials_unref (credentials);
372           goto oom;
373         }
374       if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
375         {
376           _dbus_credentials_unref (credentials);
377           goto oom;
378         }
379       if (!_dbus_string_append (&log_prefix, "] "))
380         {
381           _dbus_credentials_unref (credentials);
382           goto oom;
383         }
384       _dbus_credentials_unref (credentials);
385     }
386   if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
387     goto oom;
388   _dbus_string_free (&log_prefix);
389
390   /* Build an array of auth mechanisms */
391
392   auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
393   len = _dbus_list_get_length (auth_mechanisms_list);
394
395   if (len > 0)
396     {
397       int i;
398
399       auth_mechanisms = dbus_new0 (char*, len + 1);
400       if (auth_mechanisms == NULL)
401         goto oom;
402
403       i = 0;
404       link = _dbus_list_get_first_link (auth_mechanisms_list);
405       while (link != NULL)
406         {
407           auth_mechanisms[i] = _dbus_strdup (link->data);
408           if (auth_mechanisms[i] == NULL)
409             goto oom;
410           link = _dbus_list_get_next_link (auth_mechanisms_list, link);
411         }
412     }
413   else
414     {
415       auth_mechanisms = NULL;
416     }
417
418   /* Listen on our addresses */
419
420   if (address)
421     {
422       DBusServer *server;
423
424       server = dbus_server_listen (_dbus_string_get_const_data(address), error);
425       if (server == NULL)
426         {
427           _DBUS_ASSERT_ERROR_IS_SET (error);
428           goto failed;
429         }
430       else if (!setup_server (context, server, auth_mechanisms, error))
431         {
432           _DBUS_ASSERT_ERROR_IS_SET (error);
433           goto failed;
434         }
435
436       if (!_dbus_list_append (&context->servers, server))
437         goto oom;
438     }
439   else
440     {
441       addresses = bus_config_parser_get_addresses (parser);
442
443       link = _dbus_list_get_first_link (addresses);
444       while (link != NULL)
445         {
446           DBusServer *server;
447
448           server = dbus_server_listen (link->data, error);
449           if (server == NULL)
450             {
451               _DBUS_ASSERT_ERROR_IS_SET (error);
452               goto failed;
453             }
454           else if (!setup_server (context, server, auth_mechanisms, error))
455             {
456               _DBUS_ASSERT_ERROR_IS_SET (error);
457               goto failed;
458             }
459
460           if (!_dbus_list_append (&context->servers, server))
461             goto oom;
462
463           link = _dbus_list_get_next_link (addresses, link);
464         }
465     }
466
467   context->fork = bus_config_parser_get_fork (parser);
468   context->syslog = bus_config_parser_get_syslog (parser);
469   context->keep_umask = bus_config_parser_get_keep_umask (parser);
470   context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
471
472   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
473   retval = TRUE;
474
475  failed:
476   dbus_free_string_array (auth_mechanisms);
477   return retval;
478
479  oom:
480   BUS_SET_OOM (error);
481   dbus_free_string_array (auth_mechanisms);
482   return FALSE;
483 }
484
485 /* This code gets executed every time the config files
486  * are parsed: both during BusContext construction
487  * and on reloads. This function is slightly screwy
488  * since it can do a "half reload" in out-of-memory
489  * situations. Realistically, unlikely to ever matter.
490  */
491 static dbus_bool_t
492 process_config_every_time (BusContext      *context,
493                            BusConfigParser *parser,
494                            dbus_bool_t      is_reload,
495                            DBusError       *error)
496 {
497   DBusString full_address;
498   DBusList *link;
499   DBusList **dirs;
500   BusActivation *new_activation;
501   char *addr;
502   const char *servicehelper;
503   char *s;
504
505   dbus_bool_t retval;
506
507   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
508
509   addr = NULL;
510   retval = FALSE;
511
512   if (!_dbus_string_init (&full_address))
513     {
514       BUS_SET_OOM (error);
515       return FALSE;
516     }
517
518   /* get our limits and timeout lengths */
519   bus_config_parser_get_limits (parser, &context->limits);
520
521   if (context->policy)
522     bus_policy_unref (context->policy);
523   context->policy = bus_config_parser_steal_policy (parser);
524   _dbus_assert (context->policy != NULL);
525
526   /* We have to build the address backward, so that
527    * <listen> later in the config file have priority
528    */
529   link = _dbus_list_get_last_link (&context->servers);
530   while (link != NULL)
531     {
532       addr = dbus_server_get_address (link->data);
533       if (addr == NULL)
534         {
535           BUS_SET_OOM (error);
536           goto failed;
537         }
538
539       if (_dbus_string_get_length (&full_address) > 0)
540         {
541           if (!_dbus_string_append (&full_address, ";"))
542             {
543               BUS_SET_OOM (error);
544               goto failed;
545             }
546         }
547
548       if (!_dbus_string_append (&full_address, addr))
549         {
550           BUS_SET_OOM (error);
551           goto failed;
552         }
553
554       dbus_free (addr);
555       addr = NULL;
556
557       link = _dbus_list_get_prev_link (&context->servers, link);
558     }
559
560   if (is_reload)
561     dbus_free (context->address);
562
563   if (!_dbus_string_copy_data (&full_address, &context->address))
564     {
565       BUS_SET_OOM (error);
566       goto failed;
567     }
568
569   /* get the service directories */
570   dirs = bus_config_parser_get_service_dirs (parser);
571
572   /* and the service helper */
573   servicehelper = bus_config_parser_get_servicehelper (parser);
574
575   s = _dbus_strdup(servicehelper);
576   if (s == NULL && servicehelper != NULL)
577     {
578       BUS_SET_OOM (error);
579       goto failed;
580     }
581   else
582     {
583       dbus_free(context->servicehelper);
584       context->servicehelper = s;
585     }
586
587   /* Create activation subsystem */
588   if (context->activation)
589     {
590       if (!bus_activation_reload (context->activation, &full_address, dirs, error))
591         goto failed;
592     }
593   else
594     {
595       context->activation = bus_activation_new (context, &full_address, dirs, error);
596     }
597
598   if (context->activation == NULL)
599     {
600       _DBUS_ASSERT_ERROR_IS_SET (error);
601       goto failed;
602     }
603
604   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
605   retval = TRUE;
606
607  failed:
608   _dbus_string_free (&full_address);
609
610   if (addr)
611     dbus_free (addr);
612
613   return retval;
614 }
615
616 static dbus_bool_t
617 list_concat_new (DBusList **a,
618                  DBusList **b,
619                  DBusList **result)
620 {
621   DBusList *link;
622
623   *result = NULL;
624
625   for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
626     {
627       if (!_dbus_list_append (result, link->data))
628         goto oom;
629     }
630   for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
631     {
632       if (!_dbus_list_append (result, link->data))
633         goto oom;
634     }
635
636   return TRUE;
637 oom:
638   _dbus_list_clear (result);
639   return FALSE;
640 }
641
642 static void
643 raise_file_descriptor_limit (BusContext      *context)
644 {
645
646   /* I just picked this out of thin air; we need some extra
647    * descriptors for things like any internal pipes we create,
648    * inotify, connections to SELinux, etc.
649    */
650   unsigned int arbitrary_extra_fds = 32;
651   unsigned int limit;
652
653   limit = context->limits.max_completed_connections +
654     context->limits.max_incomplete_connections
655     + arbitrary_extra_fds;
656
657   _dbus_request_file_descriptor_limit (limit);
658 }
659
660 static dbus_bool_t
661 process_config_postinit (BusContext      *context,
662                          BusConfigParser *parser,
663                          DBusError       *error)
664 {
665   DBusHashTable *service_context_table;
666   DBusList *watched_dirs = NULL;
667
668   raise_file_descriptor_limit (context);
669
670   service_context_table = bus_config_parser_steal_service_context_table (parser);
671   if (!bus_registry_set_service_context_table (context->registry,
672                                                service_context_table))
673     {
674       BUS_SET_OOM (error);
675       return FALSE;
676     }
677
678   _dbus_hash_table_unref (service_context_table);
679
680   /* We need to monitor both the configuration directories and directories
681    * containing .service files.
682    */
683   if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
684                         bus_config_parser_get_service_dirs (parser),
685                         &watched_dirs))
686     {
687       BUS_SET_OOM (error);
688       return FALSE;
689     }
690
691   bus_set_watched_dirs (context, &watched_dirs);
692
693   _dbus_list_clear (&watched_dirs);
694
695   return TRUE;
696 }
697
698 BusContext*
699 bus_context_new (const DBusString *config_file,
700                  ForceForkSetting  force_fork,
701                  DBusPipe         *print_addr_pipe,
702                  DBusPipe         *print_pid_pipe,
703                  const DBusString *address,
704                  dbus_bool_t      systemd_activation,
705                  DBusError        *error)
706 {
707   DBusString log_prefix;
708   BusContext *context;
709   BusConfigParser *parser;
710
711   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
712
713   context = NULL;
714   parser = NULL;
715
716   if (!dbus_server_allocate_data_slot (&server_data_slot))
717     {
718       BUS_SET_OOM (error);
719       return NULL;
720     }
721
722   context = dbus_new0 (BusContext, 1);
723   if (context == NULL)
724     {
725       BUS_SET_OOM (error);
726       goto failed;
727     }
728   context->refcount = 1;
729
730   _dbus_generate_uuid (&context->uuid);
731
732   if (!_dbus_string_copy_data (config_file, &context->config_file))
733     {
734       BUS_SET_OOM (error);
735       goto failed;
736     }
737
738   context->loop = _dbus_loop_new ();
739   if (context->loop == NULL)
740     {
741       BUS_SET_OOM (error);
742       goto failed;
743     }
744
745   context->registry = bus_registry_new (context);
746   if (context->registry == NULL)
747     {
748       BUS_SET_OOM (error);
749       goto failed;
750     }
751
752   parser = bus_config_load (config_file, TRUE, NULL, error);
753   if (parser == NULL)
754     {
755       _DBUS_ASSERT_ERROR_IS_SET (error);
756       goto failed;
757     }
758
759   if (!process_config_first_time_only (context, parser, address, systemd_activation, error))
760     {
761       _DBUS_ASSERT_ERROR_IS_SET (error);
762       goto failed;
763     }
764   if (!process_config_every_time (context, parser, FALSE, error))
765     {
766       _DBUS_ASSERT_ERROR_IS_SET (error);
767       goto failed;
768     }
769
770   /* we need another ref of the server data slot for the context
771    * to own
772    */
773   if (!dbus_server_allocate_data_slot (&server_data_slot))
774     _dbus_assert_not_reached ("second ref of server data slot failed");
775
776   /* Note that we don't know whether the print_addr_pipe is
777    * one of the sockets we're using to listen on, or some
778    * other random thing. But I think the answer is "don't do
779    * that then"
780    */
781   if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
782     {
783       DBusString addr;
784       const char *a = bus_context_get_address (context);
785       int bytes;
786
787       _dbus_assert (a != NULL);
788       if (!_dbus_string_init (&addr))
789         {
790           BUS_SET_OOM (error);
791           goto failed;
792         }
793
794       if (!_dbus_string_append (&addr, a) ||
795           !_dbus_string_append (&addr, "\n"))
796         {
797           _dbus_string_free (&addr);
798           BUS_SET_OOM (error);
799           goto failed;
800         }
801
802       bytes = _dbus_string_get_length (&addr);
803       if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
804         {
805           /* pipe write returns an error on failure but not short write */
806           if (error != NULL && !dbus_error_is_set (error))
807             {
808               dbus_set_error (error, DBUS_ERROR_FAILED,
809                               "Printing message bus address: did not write all bytes\n");
810             }
811           _dbus_string_free (&addr);
812           goto failed;
813         }
814
815       if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
816         _dbus_pipe_close (print_addr_pipe, NULL);
817
818       _dbus_string_free (&addr);
819     }
820
821   context->connections = bus_connections_new (context);
822   if (context->connections == NULL)
823     {
824       BUS_SET_OOM (error);
825       goto failed;
826     }
827
828   context->matchmaker = bus_matchmaker_new ();
829   if (context->matchmaker == NULL)
830     {
831       BUS_SET_OOM (error);
832       goto failed;
833     }
834
835   /* check user before we fork */
836   if (context->user != NULL)
837     {
838       if (!_dbus_verify_daemon_user (context->user))
839         {
840           dbus_set_error (error, DBUS_ERROR_FAILED,
841                           "Could not get UID and GID for username \"%s\"",
842                           context->user);
843           goto failed;
844         }
845     }
846
847   /* Now become a daemon if appropriate and write out pid file in any case */
848   {
849     DBusString u;
850
851     if (context->pidfile)
852       _dbus_string_init_const (&u, context->pidfile);
853
854     if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
855       {
856         _dbus_verbose ("Forking and becoming daemon\n");
857
858         if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
859                                   print_pid_pipe,
860                                   error,
861                                   context->keep_umask))
862           {
863             _DBUS_ASSERT_ERROR_IS_SET (error);
864             goto failed;
865           }
866       }
867     else
868       {
869         _dbus_verbose ("Fork not requested\n");
870
871         /* Need to write PID file and to PID pipe for ourselves,
872          * not for the child process. This is a no-op if the pidfile
873          * is NULL and print_pid_pipe is NULL.
874          */
875         if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
876                                                print_pid_pipe,
877                                                _dbus_getpid (),
878                                                error))
879           {
880             _DBUS_ASSERT_ERROR_IS_SET (error);
881             goto failed;
882           }
883       }
884   }
885
886   if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
887       !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
888     _dbus_pipe_close (print_pid_pipe, NULL);
889
890   if (!bus_selinux_full_init ())
891     {
892       bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
893     }
894
895   if (!process_config_postinit (context, parser, error))
896     {
897       _DBUS_ASSERT_ERROR_IS_SET (error);
898       goto failed;
899     }
900
901   if (parser != NULL)
902     {
903       bus_config_parser_unref (parser);
904       parser = NULL;
905     }
906
907   /* Here we change our credentials if required,
908    * as soon as we've set up our sockets and pidfile
909    */
910   if (context->user != NULL)
911     {
912       if (!_dbus_change_to_daemon_user (context->user, error))
913         {
914           _DBUS_ASSERT_ERROR_IS_SET (error);
915           goto failed;
916         }
917
918 #ifdef HAVE_SELINUX
919       /* FIXME - why not just put this in full_init() below? */
920       bus_selinux_audit_init ();
921 #endif
922     }
923
924   dbus_server_free_data_slot (&server_data_slot);
925
926   return context;
927
928  failed:
929   if (parser != NULL)
930     bus_config_parser_unref (parser);
931   if (context != NULL)
932     bus_context_unref (context);
933
934   if (server_data_slot >= 0)
935     dbus_server_free_data_slot (&server_data_slot);
936
937   return NULL;
938 }
939
940 dbus_bool_t
941 bus_context_get_id (BusContext       *context,
942                     DBusString       *uuid)
943 {
944   return _dbus_uuid_encode (&context->uuid, uuid);
945 }
946
947 dbus_bool_t
948 bus_context_reload_config (BusContext *context,
949                            DBusError  *error)
950 {
951   BusConfigParser *parser;
952   DBusString config_file;
953   dbus_bool_t ret;
954
955   /* Flush the user database cache */
956   _dbus_flush_caches ();
957
958   ret = FALSE;
959   _dbus_string_init_const (&config_file, context->config_file);
960   parser = bus_config_load (&config_file, TRUE, NULL, error);
961   if (parser == NULL)
962     {
963       _DBUS_ASSERT_ERROR_IS_SET (error);
964       goto failed;
965     }
966
967   if (!process_config_every_time (context, parser, TRUE, error))
968     {
969       _DBUS_ASSERT_ERROR_IS_SET (error);
970       goto failed;
971     }
972   if (!process_config_postinit (context, parser, error))
973     {
974       _DBUS_ASSERT_ERROR_IS_SET (error);
975       goto failed;
976     }
977   ret = TRUE;
978
979   bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
980  failed:
981   if (!ret)
982     bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
983   if (parser != NULL)
984     bus_config_parser_unref (parser);
985   return ret;
986 }
987
988 static void
989 shutdown_server (BusContext *context,
990                  DBusServer *server)
991 {
992   if (server == NULL ||
993       !dbus_server_get_is_connected (server))
994     return;
995
996   if (!dbus_server_set_watch_functions (server,
997                                         NULL, NULL, NULL,
998                                         context,
999                                         NULL))
1000     _dbus_assert_not_reached ("setting watch functions to NULL failed");
1001
1002   if (!dbus_server_set_timeout_functions (server,
1003                                           NULL, NULL, NULL,
1004                                           context,
1005                                           NULL))
1006     _dbus_assert_not_reached ("setting timeout functions to NULL failed");
1007
1008   dbus_server_disconnect (server);
1009 }
1010
1011 void
1012 bus_context_shutdown (BusContext  *context)
1013 {
1014   DBusList *link;
1015
1016   link = _dbus_list_get_first_link (&context->servers);
1017   while (link != NULL)
1018     {
1019       shutdown_server (context, link->data);
1020
1021       link = _dbus_list_get_next_link (&context->servers, link);
1022     }
1023 }
1024
1025 BusContext *
1026 bus_context_ref (BusContext *context)
1027 {
1028   _dbus_assert (context->refcount > 0);
1029   context->refcount += 1;
1030
1031   return context;
1032 }
1033
1034 void
1035 bus_context_unref (BusContext *context)
1036 {
1037   _dbus_assert (context->refcount > 0);
1038   context->refcount -= 1;
1039
1040   if (context->refcount == 0)
1041     {
1042       DBusList *link;
1043
1044       _dbus_verbose ("Finalizing bus context %p\n", context);
1045
1046       bus_context_shutdown (context);
1047
1048       if (context->connections)
1049         {
1050           bus_connections_unref (context->connections);
1051           context->connections = NULL;
1052         }
1053
1054       if (context->registry)
1055         {
1056           bus_registry_unref (context->registry);
1057           context->registry = NULL;
1058         }
1059
1060       if (context->activation)
1061         {
1062           bus_activation_unref (context->activation);
1063           context->activation = NULL;
1064         }
1065
1066       link = _dbus_list_get_first_link (&context->servers);
1067       while (link != NULL)
1068         {
1069           dbus_server_unref (link->data);
1070
1071           link = _dbus_list_get_next_link (&context->servers, link);
1072         }
1073       _dbus_list_clear (&context->servers);
1074
1075       if (context->policy)
1076         {
1077           bus_policy_unref (context->policy);
1078           context->policy = NULL;
1079         }
1080
1081       if (context->loop)
1082         {
1083           _dbus_loop_unref (context->loop);
1084           context->loop = NULL;
1085         }
1086
1087       if (context->matchmaker)
1088         {
1089           bus_matchmaker_unref (context->matchmaker);
1090           context->matchmaker = NULL;
1091         }
1092
1093       dbus_free (context->config_file);
1094       dbus_free (context->log_prefix);
1095       dbus_free (context->type);
1096       dbus_free (context->address);
1097       dbus_free (context->user);
1098       dbus_free (context->servicehelper);
1099
1100       if (context->pidfile)
1101         {
1102           DBusString u;
1103           _dbus_string_init_const (&u, context->pidfile);
1104
1105           /* Deliberately ignore errors here, since there's not much
1106            * we can do about it, and we're exiting anyways.
1107            */
1108           _dbus_delete_file (&u, NULL);
1109
1110           dbus_free (context->pidfile);
1111         }
1112       dbus_free (context);
1113
1114       dbus_server_free_data_slot (&server_data_slot);
1115     }
1116 }
1117
1118 /* type may be NULL */
1119 const char*
1120 bus_context_get_type (BusContext *context)
1121 {
1122   return context->type;
1123 }
1124
1125 const char*
1126 bus_context_get_address (BusContext *context)
1127 {
1128   return context->address;
1129 }
1130
1131 const char*
1132 bus_context_get_servicehelper (BusContext *context)
1133 {
1134   return context->servicehelper;
1135 }
1136
1137 dbus_bool_t
1138 bus_context_get_systemd_activation (BusContext *context)
1139 {
1140   return context->systemd_activation;
1141 }
1142
1143 BusRegistry*
1144 bus_context_get_registry (BusContext  *context)
1145 {
1146   return context->registry;
1147 }
1148
1149 BusConnections*
1150 bus_context_get_connections (BusContext  *context)
1151 {
1152   return context->connections;
1153 }
1154
1155 BusActivation*
1156 bus_context_get_activation (BusContext  *context)
1157 {
1158   return context->activation;
1159 }
1160
1161 BusMatchmaker*
1162 bus_context_get_matchmaker (BusContext  *context)
1163 {
1164   return context->matchmaker;
1165 }
1166
1167 DBusLoop*
1168 bus_context_get_loop (BusContext *context)
1169 {
1170   return context->loop;
1171 }
1172
1173 dbus_bool_t
1174 bus_context_allow_unix_user (BusContext   *context,
1175                              unsigned long uid)
1176 {
1177   return bus_policy_allow_unix_user (context->policy,
1178                                      uid);
1179 }
1180
1181 /* For now this is never actually called because the default
1182  * DBusConnection behavior of 'same user that owns the bus can connect'
1183  * is all it would do.
1184  */
1185 dbus_bool_t
1186 bus_context_allow_windows_user (BusContext       *context,
1187                                 const char       *windows_sid)
1188 {
1189   return bus_policy_allow_windows_user (context->policy,
1190                                         windows_sid);
1191 }
1192
1193 BusPolicy *
1194 bus_context_get_policy (BusContext *context)
1195 {
1196   return context->policy;
1197 }
1198
1199 BusClientPolicy*
1200 bus_context_create_client_policy (BusContext      *context,
1201                                   DBusConnection  *connection,
1202                                   DBusError       *error)
1203 {
1204   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1205   return bus_policy_create_client_policy (context->policy, connection,
1206                                           error);
1207 }
1208
1209 int
1210 bus_context_get_activation_timeout (BusContext *context)
1211 {
1212
1213   return context->limits.activation_timeout;
1214 }
1215
1216 int
1217 bus_context_get_auth_timeout (BusContext *context)
1218 {
1219   return context->limits.auth_timeout;
1220 }
1221
1222 int
1223 bus_context_get_max_completed_connections (BusContext *context)
1224 {
1225   return context->limits.max_completed_connections;
1226 }
1227
1228 int
1229 bus_context_get_max_incomplete_connections (BusContext *context)
1230 {
1231   return context->limits.max_incomplete_connections;
1232 }
1233
1234 int
1235 bus_context_get_max_connections_per_user (BusContext *context)
1236 {
1237   return context->limits.max_connections_per_user;
1238 }
1239
1240 int
1241 bus_context_get_max_pending_activations (BusContext *context)
1242 {
1243   return context->limits.max_pending_activations;
1244 }
1245
1246 int
1247 bus_context_get_max_services_per_connection (BusContext *context)
1248 {
1249   return context->limits.max_services_per_connection;
1250 }
1251
1252 int
1253 bus_context_get_max_match_rules_per_connection (BusContext *context)
1254 {
1255   return context->limits.max_match_rules_per_connection;
1256 }
1257
1258 int
1259 bus_context_get_max_replies_per_connection (BusContext *context)
1260 {
1261   return context->limits.max_replies_per_connection;
1262 }
1263
1264 int
1265 bus_context_get_reply_timeout (BusContext *context)
1266 {
1267   return context->limits.reply_timeout;
1268 }
1269
1270 void
1271 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1272
1273 void
1274 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1275 {
1276   va_list args;
1277
1278   if (!context->syslog)
1279     {
1280       /* we're not syslogging; just output to stderr */
1281       va_start (args, msg);
1282       vfprintf (stderr, msg, args);
1283       fprintf (stderr, "\n");
1284       va_end (args);
1285       return;
1286     }
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 && !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 }