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