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