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