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