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