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