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