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