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