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