f3ef761b30a3543dd0407b5120ba21d3a87e7963
[platform/upstream/dbus.git] / bus / bus.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
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.0
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "bus.h"
25 #include "activation.h"
26 #include "connection.h"
27 #include "services.h"
28 #include "utils.h"
29 #include "policy.h"
30 #include "config-parser.h"
31 #include "signals.h"
32 #include <dbus/dbus-list.h>
33 #include <dbus/dbus-hash.h>
34 #include <dbus/dbus-internals.h>
35
36 struct BusContext
37 {
38   int refcount;
39   char *config_file;
40   char *type;
41   char *address;
42   char *pidfile;
43   char *user;
44   DBusLoop *loop;
45   DBusList *servers;
46   BusConnections *connections;
47   BusActivation *activation;
48   BusRegistry *registry;
49   BusPolicy *policy;
50   BusMatchmaker *matchmaker;
51   DBusUserDatabase *user_database;
52   BusLimits limits;
53   unsigned int fork : 1;
54 };
55
56 static dbus_int32_t server_data_slot = -1;
57
58 typedef struct
59 {
60   BusContext *context;
61 } BusServerData;
62
63 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
64
65 static BusContext*
66 server_get_context (DBusServer *server)
67 {
68   BusContext *context;
69   BusServerData *bd;
70   
71   if (!dbus_server_allocate_data_slot (&server_data_slot))
72     return NULL;
73
74   bd = BUS_SERVER_DATA (server);
75   if (bd == NULL)
76     {
77       dbus_server_free_data_slot (&server_data_slot);
78       return NULL;
79     }
80
81   context = bd->context;
82
83   dbus_server_free_data_slot (&server_data_slot);
84
85   return context;
86 }
87
88 static dbus_bool_t
89 server_watch_callback (DBusWatch     *watch,
90                        unsigned int   condition,
91                        void          *data)
92 {
93   /* FIXME this can be done in dbus-mainloop.c
94    * if the code in activation.c for the babysitter
95    * watch handler is fixed.
96    */
97   
98   return dbus_watch_handle (watch, condition);
99 }
100
101 static dbus_bool_t
102 add_server_watch (DBusWatch  *watch,
103                   void       *data)
104 {
105   DBusServer *server = data;
106   BusContext *context;
107   
108   context = server_get_context (server);
109   
110   return _dbus_loop_add_watch (context->loop,
111                                watch, server_watch_callback, server,
112                                NULL);
113 }
114
115 static void
116 remove_server_watch (DBusWatch  *watch,
117                      void       *data)
118 {
119   DBusServer *server = data;
120   BusContext *context;
121   
122   context = server_get_context (server);
123   
124   _dbus_loop_remove_watch (context->loop,
125                            watch, server_watch_callback, server);
126 }
127
128
129 static void
130 server_timeout_callback (DBusTimeout   *timeout,
131                          void          *data)
132 {
133   /* can return FALSE on OOM but we just let it fire again later */
134   dbus_timeout_handle (timeout);
135 }
136
137 static dbus_bool_t
138 add_server_timeout (DBusTimeout *timeout,
139                     void        *data)
140 {
141   DBusServer *server = data;
142   BusContext *context;
143   
144   context = server_get_context (server);
145
146   return _dbus_loop_add_timeout (context->loop,
147                                  timeout, server_timeout_callback, server, NULL);
148 }
149
150 static void
151 remove_server_timeout (DBusTimeout *timeout,
152                        void        *data)
153 {
154   DBusServer *server = data;
155   BusContext *context;
156   
157   context = server_get_context (server);
158   
159   _dbus_loop_remove_timeout (context->loop,
160                              timeout, server_timeout_callback, server);
161 }
162
163 static void
164 new_connection_callback (DBusServer     *server,
165                          DBusConnection *new_connection,
166                          void           *data)
167 {
168   BusContext *context = data;
169   
170   if (!bus_connections_setup_connection (context->connections, new_connection))
171     {
172       _dbus_verbose ("No memory to setup new connection\n");
173
174       /* if we don't do this, it will get unref'd without
175        * being disconnected... kind of strange really
176        * that we have to do this, people won't get it right
177        * in general.
178        */
179       dbus_connection_disconnect (new_connection);
180     }
181
182   dbus_connection_set_max_received_size (new_connection,
183                                          context->limits.max_incoming_bytes);
184
185   dbus_connection_set_max_message_size (new_connection,
186                                         context->limits.max_message_size);
187   
188   /* on OOM, we won't have ref'd the connection so it will die. */
189 }
190
191 static void
192 free_server_data (void *data)
193 {
194   BusServerData *bd = data;  
195   
196   dbus_free (bd);
197 }
198
199 static dbus_bool_t
200 setup_server (BusContext *context,
201               DBusServer *server,
202               char      **auth_mechanisms,
203               DBusError  *error)
204 {
205   BusServerData *bd;
206
207   bd = dbus_new0 (BusServerData, 1);
208   if (!dbus_server_set_data (server,
209                              server_data_slot,
210                              bd, free_server_data))
211     {
212       dbus_free (bd);
213       BUS_SET_OOM (error);
214       return FALSE;
215     }
216
217   bd->context = context;
218   
219   if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
220     {
221       BUS_SET_OOM (error);
222       return FALSE;
223     }
224   
225   dbus_server_set_new_connection_function (server,
226                                            new_connection_callback,
227                                            context, NULL);
228   
229   if (!dbus_server_set_watch_functions (server,
230                                         add_server_watch,
231                                         remove_server_watch,
232                                         NULL,
233                                         server,
234                                         NULL))
235     {
236       BUS_SET_OOM (error);
237       return FALSE;
238     }
239
240   if (!dbus_server_set_timeout_functions (server,
241                                           add_server_timeout,
242                                           remove_server_timeout,
243                                           NULL,
244                                           server, NULL))
245     {
246       BUS_SET_OOM (error);
247       return FALSE;
248     }
249   
250   return TRUE;
251 }
252
253 /* This code only gets executed the first time the
254    config files are parsed.  It is not executed
255    when config files are reloaded.*/
256 static dbus_bool_t
257 process_config_first_time_only (BusContext      *context,
258                                 BusConfigParser *parser,
259                                 DBusError       *error)
260 {
261   DBusList *link;
262   DBusList **addresses;
263   const char *user, *pidfile;
264   char **auth_mechanisms;
265   DBusList **auth_mechanisms_list;
266   int len;
267   dbus_bool_t retval;
268
269   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
270
271   retval = FALSE;
272   auth_mechanisms = NULL;
273
274   /* Check for an existing pid file. Of course this is a race;
275    * we'd have to use fcntl() locks on the pid file to
276    * avoid that. But we want to check for the pid file
277    * before overwriting any existing sockets, etc.
278    */
279   pidfile = bus_config_parser_get_pidfile (parser);
280   if (pidfile != NULL)
281     {
282       DBusString u;
283       DBusStat stbuf;
284       DBusError tmp_error;
285       
286       dbus_error_init (&tmp_error);
287       _dbus_string_init_const (&u, pidfile);
288       
289       if (_dbus_stat (&u, &stbuf, &tmp_error))
290         {
291           dbus_set_error (error, DBUS_ERROR_FAILED,
292                           "The pid file \"%s\" exists, if the message bus is not running, remove this file",
293                           pidfile);
294           dbus_error_free (&tmp_error);
295           goto failed;
296         }
297     }
298   
299   /* keep around the pid filename so we can delete it later */
300   context->pidfile = _dbus_strdup (pidfile);
301
302   /* Build an array of auth mechanisms */
303   
304   auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
305   len = _dbus_list_get_length (auth_mechanisms_list);
306
307   if (len > 0)
308     {
309       int i;
310
311       auth_mechanisms = dbus_new0 (char*, len + 1);
312       if (auth_mechanisms == NULL)
313         {
314           BUS_SET_OOM (error);
315           goto failed;
316         }
317       
318       i = 0;
319       link = _dbus_list_get_first_link (auth_mechanisms_list);
320       while (link != NULL)
321         {
322           auth_mechanisms[i] = _dbus_strdup (link->data);
323           if (auth_mechanisms[i] == NULL)
324             {
325               BUS_SET_OOM (error);
326               goto failed;
327             }
328           link = _dbus_list_get_next_link (auth_mechanisms_list, link);
329         }
330     }
331   else
332     {
333       auth_mechanisms = NULL;
334     }
335
336   /* Listen on our addresses */
337   
338   addresses = bus_config_parser_get_addresses (parser);  
339   
340   link = _dbus_list_get_first_link (addresses);
341   while (link != NULL)
342     {
343       DBusServer *server;
344       
345       server = dbus_server_listen (link->data, error);
346       if (server == NULL)
347         {
348           _DBUS_ASSERT_ERROR_IS_SET (error);
349           goto failed;
350         }
351       else if (!setup_server (context, server, auth_mechanisms, error))
352         {
353           _DBUS_ASSERT_ERROR_IS_SET (error);
354           goto failed;
355         }
356
357       if (!_dbus_list_append (&context->servers, server))
358         {
359           BUS_SET_OOM (error);
360           goto failed;
361         }          
362       
363       link = _dbus_list_get_next_link (addresses, link);
364     }
365
366   /* note that type may be NULL */
367   context->type = _dbus_strdup (bus_config_parser_get_type (parser));
368   if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
369     {
370       BUS_SET_OOM (error);
371       goto failed;
372     }
373
374   user = bus_config_parser_get_user (parser);
375   if (user != NULL)
376     {
377       context->user = _dbus_strdup (user);
378       if (context->user == NULL)
379         {
380           BUS_SET_OOM (error);
381           goto failed;
382         }
383     }
384
385   context->fork = bus_config_parser_get_fork (parser);
386   
387   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
388   retval = TRUE;
389
390  failed:
391   dbus_free_string_array (auth_mechanisms);
392   return retval;
393 }
394
395 /* This code gets executed every time the config files
396    are parsed: both during BusContext construction
397    and on reloads. */
398 static dbus_bool_t
399 process_config_every_time (BusContext      *context,
400                            BusConfigParser *parser,
401                            dbus_bool_t      is_reload,
402                            DBusError       *error)
403 {
404   DBusString full_address;
405   DBusList *link;
406   
407   dbus_bool_t retval;
408
409   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
410
411   retval = FALSE;
412
413   if (!_dbus_string_init (&full_address))
414     {
415       BUS_SET_OOM (error);
416       return FALSE;
417     }
418
419   /* get our limits and timeout lengths */
420   bus_config_parser_get_limits (parser, &context->limits);
421
422   context->policy = bus_config_parser_steal_policy (parser);
423   _dbus_assert (context->policy != NULL);
424
425   /* We have to build the address backward, so that
426    * <listen> later in the config file have priority
427    */
428   link = _dbus_list_get_last_link (&context->servers);
429   while (link != NULL)
430     {
431       char *addr;
432       
433       addr = dbus_server_get_address (link->data);
434       if (addr == NULL)
435         {
436           BUS_SET_OOM (error);
437           goto failed;
438         }
439
440       if (_dbus_string_get_length (&full_address) > 0)
441         {
442           if (!_dbus_string_append (&full_address, ";"))
443             {
444               BUS_SET_OOM (error);
445               goto failed;
446             }
447         }
448
449       if (!_dbus_string_append (&full_address, addr))
450         {
451           BUS_SET_OOM (error);
452           goto failed;
453         }
454
455       dbus_free (addr);
456
457       link = _dbus_list_get_prev_link (&context->servers, link);
458     }
459
460   if (is_reload)
461     dbus_free (context->address);
462
463   if (!_dbus_string_copy_data (&full_address, &context->address))
464     {
465       BUS_SET_OOM (error);
466       goto failed;
467     }
468
469   /* Create activation subsystem */
470   
471   if (is_reload)
472     bus_activation_unref (context->activation);
473   
474   context->activation = bus_activation_new (context, &full_address,
475                                             bus_config_parser_get_service_dirs (parser),
476                                             error);
477   if (context->activation == NULL)
478     {
479       _DBUS_ASSERT_ERROR_IS_SET (error);
480       goto failed;
481     }
482
483   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
484   retval = TRUE;
485
486  failed:
487   _dbus_string_free (&full_address);
488   return retval;
489 }
490
491 static dbus_bool_t
492 load_config (BusContext *context,
493              dbus_bool_t is_reload,
494              DBusError  *error)
495 {
496   BusConfigParser *parser;
497   DBusString config_file;
498   dbus_bool_t retval;
499
500   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
501
502   retval = FALSE;
503   parser = NULL;
504
505   _dbus_string_init_const (&config_file, context->config_file);
506   parser = bus_config_load (&config_file, TRUE, NULL, error);
507   if (parser == NULL)
508     {
509       _DBUS_ASSERT_ERROR_IS_SET (error);
510       goto failed;
511     }
512   
513   if (!is_reload && !process_config_first_time_only (context, parser, error))
514     {
515       _DBUS_ASSERT_ERROR_IS_SET (error);
516       goto failed;
517     }
518
519   if (!process_config_every_time (context, parser, is_reload, error))
520     {
521       _DBUS_ASSERT_ERROR_IS_SET (error);
522       goto failed;
523     }
524
525   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
526   retval = TRUE;
527
528  failed:
529   if (parser)
530     bus_config_parser_unref (parser);
531   return retval;
532 }
533
534 BusContext*
535 bus_context_new (const DBusString *config_file,
536                  dbus_bool_t       force_fork,
537                  int               print_addr_fd,
538                  int               print_pid_fd,
539                  DBusError        *error)
540 {
541   BusContext *context;
542   
543   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
544
545   if (!dbus_server_allocate_data_slot (&server_data_slot))
546     {
547       BUS_SET_OOM (error);
548       return NULL;
549     }
550
551   context = dbus_new0 (BusContext, 1);
552   if (context == NULL)
553     {
554       BUS_SET_OOM (error);
555       goto failed;
556     }
557   context->refcount = 1;
558
559   if (!_dbus_string_copy_data (config_file, &context->config_file))
560     {
561       BUS_SET_OOM (error);
562       goto failed;
563     }
564
565   context->loop = _dbus_loop_new ();
566   if (context->loop == NULL)
567     {
568       BUS_SET_OOM (error);
569       goto failed;
570     }
571
572   if (!load_config (context, FALSE, error))
573     {
574       _DBUS_ASSERT_ERROR_IS_SET (error);
575       goto failed;
576     }
577   
578   /* we need another ref of the server data slot for the context
579    * to own
580    */
581   if (!dbus_server_allocate_data_slot (&server_data_slot))
582     _dbus_assert_not_reached ("second ref of server data slot failed");
583
584   context->user_database = _dbus_user_database_new ();
585   if (context->user_database == NULL)
586     {
587       BUS_SET_OOM (error);
588       goto failed;
589     }
590   
591   /* Note that we don't know whether the print_addr_fd is
592    * one of the sockets we're using to listen on, or some
593    * other random thing. But I think the answer is "don't do
594    * that then"
595    */
596   if (print_addr_fd >= 0)
597     {
598       DBusString addr;
599       const char *a = bus_context_get_address (context);
600       int bytes;
601       
602       _dbus_assert (a != NULL);
603       if (!_dbus_string_init (&addr))
604         {
605           BUS_SET_OOM (error);
606           goto failed;
607         }
608       
609       if (!_dbus_string_append (&addr, a) ||
610           !_dbus_string_append (&addr, "\n"))
611         {
612           _dbus_string_free (&addr);
613           BUS_SET_OOM (error);
614           goto failed;
615         }
616
617       bytes = _dbus_string_get_length (&addr);
618       if (_dbus_write (print_addr_fd, &addr, 0, bytes) != bytes)
619         {
620           dbus_set_error (error, DBUS_ERROR_FAILED,
621                           "Printing message bus address: %s\n",
622                           _dbus_strerror (errno));
623           _dbus_string_free (&addr);
624           goto failed;
625         }
626
627       if (print_addr_fd > 2)
628         _dbus_close (print_addr_fd, NULL);
629
630       _dbus_string_free (&addr);
631     }
632   
633   context->connections = bus_connections_new (context);
634   if (context->connections == NULL)
635     {
636       BUS_SET_OOM (error);
637       goto failed;
638     }
639
640   context->registry = bus_registry_new (context);
641   if (context->registry == NULL)
642     {
643       BUS_SET_OOM (error);
644       goto failed;
645     }
646
647   context->matchmaker = bus_matchmaker_new ();
648   if (context->matchmaker == NULL)
649     {
650       BUS_SET_OOM (error);
651       goto failed;
652     }
653   
654   /* Now become a daemon if appropriate */
655   if (force_fork || context->fork)
656     {
657       DBusString u;
658
659       if (context->pidfile)
660         _dbus_string_init_const (&u, context->pidfile);
661       
662       if (!_dbus_become_daemon (context->pidfile ? &u : NULL, error))
663         {
664           _DBUS_ASSERT_ERROR_IS_SET (error);
665           goto failed;
666         }
667     }
668   else
669     {
670       /* Need to write PID file for ourselves, not for the child process */
671       if (context->pidfile != NULL)
672         {
673           DBusString u;
674
675           _dbus_string_init_const (&u, context->pidfile);
676           
677           if (!_dbus_write_pid_file (&u, _dbus_getpid (), error))
678             {
679               _DBUS_ASSERT_ERROR_IS_SET (error);
680               goto failed;
681             }
682         }
683     }
684
685   /* Write PID if requested */
686   if (print_pid_fd >= 0)
687     {
688       DBusString pid;
689       int bytes;
690
691       if (!_dbus_string_init (&pid))
692         {
693           BUS_SET_OOM (error);
694           goto failed;
695         }
696       
697       if (!_dbus_string_append_int (&pid, _dbus_getpid ()) ||
698           !_dbus_string_append (&pid, "\n"))
699         {
700           _dbus_string_free (&pid);
701           BUS_SET_OOM (error);
702           goto failed;
703         }
704
705       bytes = _dbus_string_get_length (&pid);
706       if (_dbus_write (print_pid_fd, &pid, 0, bytes) != bytes)
707         {
708           dbus_set_error (error, DBUS_ERROR_FAILED,
709                           "Printing message bus PID: %s\n",
710                           _dbus_strerror (errno));
711           _dbus_string_free (&pid);
712           goto failed;
713         }
714
715       if (print_pid_fd > 2)
716         _dbus_close (print_pid_fd, NULL);
717       
718       _dbus_string_free (&pid);
719     }
720   
721   /* Here we change our credentials if required,
722    * as soon as we've set up our sockets and pidfile
723    */
724   if (context->user != NULL)
725     {
726       DBusCredentials creds;
727       DBusString u;
728
729       _dbus_string_init_const (&u, context->user);
730
731       if (!_dbus_credentials_from_username (&u, &creds) ||
732           creds.uid < 0 ||
733           creds.gid < 0)
734         {
735           dbus_set_error (error, DBUS_ERROR_FAILED,
736                           "Could not get UID and GID for username \"%s\"",
737                           context->user);
738           goto failed;
739         }
740       
741       if (!_dbus_change_identity (creds.uid, creds.gid, error))
742         {
743           _DBUS_ASSERT_ERROR_IS_SET (error);
744           goto failed;
745         }
746     }
747   
748   dbus_server_free_data_slot (&server_data_slot);
749   
750   return context;
751   
752  failed:  
753   if (context != NULL)
754     bus_context_unref (context);
755
756   if (server_data_slot >= 0)
757     dbus_server_free_data_slot (&server_data_slot);
758   
759   return NULL;
760 }
761
762 dbus_bool_t
763 bus_context_reload_config (BusContext *context,
764                            DBusError  *error)
765 {
766   return load_config (context,
767                       TRUE, /* yes, we are re-loading */
768                       error);
769 }
770
771 static void
772 shutdown_server (BusContext *context,
773                  DBusServer *server)
774 {
775   if (server == NULL ||
776       !dbus_server_get_is_connected (server))
777     return;
778   
779   if (!dbus_server_set_watch_functions (server,
780                                         NULL, NULL, NULL,
781                                         context,
782                                         NULL))
783     _dbus_assert_not_reached ("setting watch functions to NULL failed");
784   
785   if (!dbus_server_set_timeout_functions (server,
786                                           NULL, NULL, NULL,
787                                           context,
788                                           NULL))
789     _dbus_assert_not_reached ("setting timeout functions to NULL failed");
790   
791   dbus_server_disconnect (server);
792 }
793
794 void
795 bus_context_shutdown (BusContext  *context)
796 {
797   DBusList *link;
798
799   link = _dbus_list_get_first_link (&context->servers);
800   while (link != NULL)
801     {
802       shutdown_server (context, link->data);
803
804       link = _dbus_list_get_next_link (&context->servers, link);
805     }
806 }
807
808 BusContext *
809 bus_context_ref (BusContext *context)
810 {
811   _dbus_assert (context->refcount > 0);
812   context->refcount += 1;
813
814   return context;
815 }
816
817 void
818 bus_context_unref (BusContext *context)
819 {
820   _dbus_assert (context->refcount > 0);
821   context->refcount -= 1;
822
823   if (context->refcount == 0)
824     {
825       DBusList *link;
826       
827       _dbus_verbose ("Finalizing bus context %p\n", context);
828       
829       bus_context_shutdown (context);
830
831       if (context->connections)
832         {
833           bus_connections_unref (context->connections);
834           context->connections = NULL;
835         }
836       
837       if (context->registry)
838         {
839           bus_registry_unref (context->registry);
840           context->registry = NULL;
841         }
842       
843       if (context->activation)
844         {
845           bus_activation_unref (context->activation);
846           context->activation = NULL;
847         }
848
849       link = _dbus_list_get_first_link (&context->servers);
850       while (link != NULL)
851         {
852           dbus_server_unref (link->data);
853           
854           link = _dbus_list_get_next_link (&context->servers, link);
855         }
856       _dbus_list_clear (&context->servers);
857
858       if (context->policy)
859         {
860           bus_policy_unref (context->policy);
861           context->policy = NULL;
862         }
863       
864       if (context->loop)
865         {
866           _dbus_loop_unref (context->loop);
867           context->loop = NULL;
868         }
869
870       if (context->matchmaker)
871         {
872           bus_matchmaker_unref (context->matchmaker);
873           context->matchmaker = NULL;
874         }
875       
876       dbus_free (context->config_file);
877       dbus_free (context->type);
878       dbus_free (context->address);
879       dbus_free (context->user);
880
881       if (context->pidfile)
882         {
883           DBusString u;
884           _dbus_string_init_const (&u, context->pidfile);
885
886           /* Deliberately ignore errors here, since there's not much
887            * we can do about it, and we're exiting anyways.
888            */
889           _dbus_delete_file (&u, NULL);
890
891           dbus_free (context->pidfile); 
892         }
893
894       if (context->user_database != NULL)
895         _dbus_user_database_unref (context->user_database);
896       
897       dbus_free (context);
898
899       dbus_server_free_data_slot (&server_data_slot);
900     }
901 }
902
903 /* type may be NULL */
904 const char*
905 bus_context_get_type (BusContext *context)
906 {
907   return context->type;
908 }
909
910 const char*
911 bus_context_get_address (BusContext *context)
912 {
913   return context->address;
914 }
915
916 BusRegistry*
917 bus_context_get_registry (BusContext  *context)
918 {
919   return context->registry;
920 }
921
922 BusConnections*
923 bus_context_get_connections (BusContext  *context)
924 {
925   return context->connections;
926 }
927
928 BusActivation*
929 bus_context_get_activation (BusContext  *context)
930 {
931   return context->activation;
932 }
933
934 BusMatchmaker*
935 bus_context_get_matchmaker (BusContext  *context)
936 {
937   return context->matchmaker;
938 }
939
940 DBusLoop*
941 bus_context_get_loop (BusContext *context)
942 {
943   return context->loop;
944 }
945
946 DBusUserDatabase*
947 bus_context_get_user_database (BusContext *context)
948 {
949   return context->user_database;
950 }
951
952 dbus_bool_t
953 bus_context_allow_user (BusContext   *context,
954                         unsigned long uid)
955 {
956   return bus_policy_allow_user (context->policy,
957                                 context->user_database,
958                                 uid);
959 }
960
961 BusClientPolicy*
962 bus_context_create_client_policy (BusContext      *context,
963                                   DBusConnection  *connection,
964                                   DBusError       *error)
965 {
966   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
967   return bus_policy_create_client_policy (context->policy, connection,
968                                           error);
969 }
970
971 int
972 bus_context_get_activation_timeout (BusContext *context)
973 {
974   
975   return context->limits.activation_timeout;
976 }
977
978 int
979 bus_context_get_auth_timeout (BusContext *context)
980 {
981   return context->limits.auth_timeout;
982 }
983
984 int
985 bus_context_get_max_completed_connections (BusContext *context)
986 {
987   return context->limits.max_completed_connections;
988 }
989
990 int
991 bus_context_get_max_incomplete_connections (BusContext *context)
992 {
993   return context->limits.max_incomplete_connections;
994 }
995
996 int
997 bus_context_get_max_connections_per_user (BusContext *context)
998 {
999   return context->limits.max_connections_per_user;
1000 }
1001
1002 int
1003 bus_context_get_max_pending_activations (BusContext *context)
1004 {
1005   return context->limits.max_pending_activations;
1006 }
1007
1008 int
1009 bus_context_get_max_services_per_connection (BusContext *context)
1010 {
1011   return context->limits.max_services_per_connection;
1012 }
1013
1014 int
1015 bus_context_get_max_match_rules_per_connection (BusContext *context)
1016 {
1017   return context->limits.max_match_rules_per_connection;
1018 }
1019
1020 int
1021 bus_context_get_max_replies_per_connection (BusContext *context)
1022 {
1023   return context->limits.max_replies_per_connection;
1024 }
1025
1026 int
1027 bus_context_get_reply_timeout (BusContext *context)
1028 {
1029   return context->limits.reply_timeout;
1030 }
1031
1032 /*
1033  * addressed_recipient is the recipient specified in the message.
1034  *
1035  * proposed_recipient is the recipient we're considering sending
1036  * to right this second, and may be an eavesdropper.
1037  *
1038  * sender is the sender of the message.
1039  *
1040  * NULL for proposed_recipient or sender definitely means the bus driver.
1041  *
1042  * NULL for addressed_recipient may mean the bus driver, or may mean
1043  * no destination was specified in the message (e.g. a signal).
1044  */
1045 dbus_bool_t
1046 bus_context_check_security_policy (BusContext     *context,
1047                                    BusTransaction *transaction,
1048                                    DBusConnection *sender,
1049                                    DBusConnection *addressed_recipient,
1050                                    DBusConnection *proposed_recipient,
1051                                    DBusMessage    *message,
1052                                    DBusError      *error)
1053 {
1054   BusClientPolicy *sender_policy;
1055   BusClientPolicy *recipient_policy;
1056   int type;
1057   dbus_bool_t requested_reply;
1058   
1059   type = dbus_message_get_type (message);
1060   
1061   /* dispatch.c was supposed to ensure these invariants */
1062   _dbus_assert (dbus_message_get_destination (message) != NULL ||
1063                 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1064                 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1065   _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1066                 addressed_recipient != NULL ||
1067                 strcmp (dbus_message_get_destination (message), DBUS_SERVICE_ORG_FREEDESKTOP_DBUS) == 0);
1068   
1069   switch (type)
1070     {
1071     case DBUS_MESSAGE_TYPE_METHOD_CALL:
1072     case DBUS_MESSAGE_TYPE_SIGNAL:
1073     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1074     case DBUS_MESSAGE_TYPE_ERROR:
1075       break;
1076       
1077     default:
1078       _dbus_verbose ("security check disallowing message of unknown type %d\n",
1079                      type);
1080
1081       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1082                       "Message bus will not accept messages of unknown type\n");
1083               
1084       return FALSE;
1085     }
1086
1087   requested_reply = FALSE;
1088   
1089   if (sender != NULL)
1090     {
1091       if (bus_connection_is_active (sender))
1092         {
1093           sender_policy = bus_connection_get_policy (sender);
1094           _dbus_assert (sender_policy != NULL);
1095           
1096           /* Fill in requested_reply variable with TRUE if this is a
1097            * reply and the reply was pending.
1098            */
1099           if (dbus_message_get_reply_serial (message) != 0)
1100             {
1101               if (proposed_recipient != NULL /* not to the bus driver */ &&
1102                   addressed_recipient == proposed_recipient /* not eavesdropping */)
1103                 {
1104                   DBusError error2;                  
1105                   
1106                   dbus_error_init (&error2);
1107                   requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1108                                                                  transaction,
1109                                                                  sender, addressed_recipient, message,
1110                                                                  &error2);
1111                   if (dbus_error_is_set (&error2))
1112                     {
1113                       dbus_move_error (&error2, error);
1114                       return FALSE;
1115                     }
1116                 }
1117             }
1118         }
1119       else
1120         {
1121           /* Policy for inactive connections is that they can only send
1122            * the hello message to the bus driver
1123            */
1124           if (proposed_recipient == NULL &&
1125               dbus_message_is_method_call (message,
1126                                            DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1127                                            "Hello"))
1128             {
1129               _dbus_verbose ("security check allowing %s message\n",
1130                              "Hello");
1131               return TRUE;
1132             }
1133           else
1134             {
1135               _dbus_verbose ("security check disallowing non-%s message\n",
1136                              "Hello");
1137
1138               dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1139                               "Client tried to send a message other than %s without being registered",
1140                               "Hello");
1141               
1142               return FALSE;
1143             }
1144         }
1145     }
1146   else
1147     {
1148       sender_policy = NULL;
1149
1150       /* If the sender is the bus driver, we assume any reply was a
1151        * requested reply as bus driver won't send bogus ones
1152        */
1153       if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1154           dbus_message_get_reply_serial (message) != 0)
1155         requested_reply = TRUE;
1156     }
1157
1158   _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1159                 (sender == NULL && sender_policy == NULL));
1160   
1161   if (proposed_recipient != NULL)
1162     {
1163       /* only the bus driver can send to an inactive recipient (as it
1164        * owns no services, so other apps can't address it). Inactive
1165        * recipients can receive any message.
1166        */
1167       if (bus_connection_is_active (proposed_recipient))
1168         {
1169           recipient_policy = bus_connection_get_policy (proposed_recipient);
1170           _dbus_assert (recipient_policy != NULL);
1171         }
1172       else if (sender == NULL)
1173         {
1174           _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1175           recipient_policy = NULL;
1176         }
1177       else
1178         {
1179           _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1180           recipient_policy = NULL;
1181         }
1182     }
1183   else
1184     recipient_policy = NULL;
1185   
1186   _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1187                 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1188                 (proposed_recipient == NULL && recipient_policy == NULL));
1189   
1190   if (sender_policy &&
1191       !bus_client_policy_check_can_send (sender_policy,
1192                                          context->registry,
1193                                          requested_reply,
1194                                          proposed_recipient,
1195                                          message))
1196     {
1197       const char *dest = dbus_message_get_destination (message);
1198       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1199                       "A security policy in place prevents this sender "
1200                       "from sending this message to this recipient, "
1201                       "see message bus configuration file (rejected message "
1202                       "had interface \"%s\" member \"%s\" error name \"%s\" destination \"%s\")",
1203                       dbus_message_get_interface (message) ?
1204                       dbus_message_get_interface (message) : "(unset)",
1205                       dbus_message_get_member (message) ?
1206                       dbus_message_get_member (message) : "(unset)",
1207                       dbus_message_get_error_name (message) ?
1208                       dbus_message_get_error_name (message) : "(unset)",
1209                       dest ? dest : DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
1210       _dbus_verbose ("security policy disallowing message due to sender policy\n");
1211       return FALSE;
1212     }
1213
1214   if (recipient_policy &&
1215       !bus_client_policy_check_can_receive (recipient_policy,
1216                                             context->registry,
1217                                             requested_reply,
1218                                             sender,
1219                                             addressed_recipient, proposed_recipient,
1220                                             message))
1221     {
1222       const char *dest = dbus_message_get_destination (message);
1223       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1224                       "A security policy in place prevents this recipient "
1225                       "from receiving this message from this sender, "
1226                       "see message bus configuration file (rejected message "
1227                       "had interface \"%s\" member \"%s\" error name \"%s\" destination \"%s\" reply serial %u requested_reply=%d)",
1228                       dbus_message_get_interface (message) ?
1229                       dbus_message_get_interface (message) : "(unset)",
1230                       dbus_message_get_member (message) ?
1231                       dbus_message_get_member (message) : "(unset)",
1232                       dbus_message_get_error_name (message) ?
1233                       dbus_message_get_error_name (message) : "(unset)",
1234                       dest ? dest : DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1235                       dbus_message_get_reply_serial (message),
1236                       requested_reply);
1237       _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1238       return FALSE;
1239     }
1240
1241   /* See if limits on size have been exceeded */
1242   if (proposed_recipient &&
1243       dbus_connection_get_outgoing_size (proposed_recipient) >
1244       context->limits.max_outgoing_bytes)
1245     {
1246       const char *dest = dbus_message_get_destination (message);
1247       dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1248                       "The destination service \"%s\" has a full message queue",
1249                       dest ? dest : (proposed_recipient ?
1250                                      bus_connection_get_name (proposed_recipient) : 
1251                                      DBUS_SERVICE_ORG_FREEDESKTOP_DBUS));
1252       _dbus_verbose ("security policy disallowing message due to full message queue\n");
1253       return FALSE;
1254     }
1255
1256   if (type == DBUS_MESSAGE_TYPE_METHOD_CALL)
1257     {
1258       /* Record that we will allow a reply here in the future (don't
1259        * bother if the recipient is the bus). Only the addressed recipient
1260        * may reply.
1261        */
1262       if (sender && addressed_recipient &&
1263           !bus_connections_expect_reply (bus_connection_get_connections (sender),
1264                                          transaction,
1265                                          sender, addressed_recipient,
1266                                          message, error))
1267         {
1268           _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1269           return FALSE;
1270         }
1271     }
1272   
1273   _dbus_verbose ("security policy allowing message\n");
1274   return TRUE;
1275 }