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