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