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