2003-05-13 James Willcox <jwillcox@gnome.org>
[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 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
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 <dbus/dbus-list.h>
32 #include <dbus/dbus-hash.h>
33 #include <dbus/dbus-internals.h>
34
35 struct BusContext
36 {
37   int refcount;
38   char *type;
39   char *address;
40   char *pidfile;
41   DBusLoop *loop;
42   DBusList *servers;
43   BusConnections *connections;
44   BusActivation *activation;
45   BusRegistry *registry;
46   BusPolicy *policy;
47   DBusUserDatabase *user_database;
48   BusLimits limits;
49 };
50
51 static int server_data_slot = -1;
52 static int server_data_slot_refcount = 0;
53
54 typedef struct
55 {
56   BusContext *context;
57 } BusServerData;
58
59 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
60
61 static dbus_bool_t
62 server_data_slot_ref (void)
63 {
64   if (server_data_slot < 0)
65     {
66       server_data_slot = dbus_server_allocate_data_slot ();
67       
68       if (server_data_slot < 0)
69         return FALSE;
70
71       _dbus_assert (server_data_slot_refcount == 0);
72     }  
73
74   server_data_slot_refcount += 1;
75
76   return TRUE;
77 }
78
79 static void
80 server_data_slot_unref (void)
81 {
82   _dbus_assert (server_data_slot_refcount > 0);
83
84   server_data_slot_refcount -= 1;
85   
86   if (server_data_slot_refcount == 0)
87     {
88       dbus_server_free_data_slot (server_data_slot);
89       server_data_slot = -1;
90     }
91 }
92
93 static BusContext*
94 server_get_context (DBusServer *server)
95 {
96   BusContext *context;
97   BusServerData *bd;
98   
99   if (!server_data_slot_ref ())
100     return NULL;
101
102   bd = BUS_SERVER_DATA (server);
103   if (bd == NULL)
104     {
105       server_data_slot_unref ();
106       return NULL;
107     }
108
109   context = bd->context;
110
111   server_data_slot_unref ();
112
113   return context;
114 }
115
116 static dbus_bool_t
117 server_watch_callback (DBusWatch     *watch,
118                        unsigned int   condition,
119                        void          *data)
120 {
121   /* FIXME this can be done in dbus-mainloop.c
122    * if the code in activation.c for the babysitter
123    * watch handler is fixed.
124    */
125   
126   return dbus_watch_handle (watch, condition);
127 }
128
129 static dbus_bool_t
130 add_server_watch (DBusWatch  *watch,
131                   void       *data)
132 {
133   DBusServer *server = data;
134   BusContext *context;
135   
136   context = server_get_context (server);
137   
138   return _dbus_loop_add_watch (context->loop,
139                                watch, server_watch_callback, server,
140                                NULL);
141 }
142
143 static void
144 remove_server_watch (DBusWatch  *watch,
145                      void       *data)
146 {
147   DBusServer *server = data;
148   BusContext *context;
149   
150   context = server_get_context (server);
151   
152   _dbus_loop_remove_watch (context->loop,
153                            watch, server_watch_callback, server);
154 }
155
156
157 static void
158 server_timeout_callback (DBusTimeout   *timeout,
159                          void          *data)
160 {
161   /* can return FALSE on OOM but we just let it fire again later */
162   dbus_timeout_handle (timeout);
163 }
164
165 static dbus_bool_t
166 add_server_timeout (DBusTimeout *timeout,
167                     void        *data)
168 {
169   DBusServer *server = data;
170   BusContext *context;
171   
172   context = server_get_context (server);
173
174   return _dbus_loop_add_timeout (context->loop,
175                                  timeout, server_timeout_callback, server, NULL);
176 }
177
178 static void
179 remove_server_timeout (DBusTimeout *timeout,
180                        void        *data)
181 {
182   DBusServer *server = data;
183   BusContext *context;
184   
185   context = server_get_context (server);
186   
187   _dbus_loop_remove_timeout (context->loop,
188                              timeout, server_timeout_callback, server);
189 }
190
191 static void
192 new_connection_callback (DBusServer     *server,
193                          DBusConnection *new_connection,
194                          void           *data)
195 {
196   BusContext *context = data;
197   
198   if (!bus_connections_setup_connection (context->connections, new_connection))
199     {
200       _dbus_verbose ("No memory to setup new connection\n");
201
202       /* if we don't do this, it will get unref'd without
203        * being disconnected... kind of strange really
204        * that we have to do this, people won't get it right
205        * in general.
206        */
207       dbus_connection_disconnect (new_connection);
208     }
209
210   dbus_connection_set_max_received_size (new_connection,
211                                          context->limits.max_incoming_bytes);
212
213   dbus_connection_set_max_message_size (new_connection,
214                                         context->limits.max_message_size);
215   
216   /* on OOM, we won't have ref'd the connection so it will die. */
217 }
218
219 static void
220 free_server_data (void *data)
221 {
222   BusServerData *bd = data;  
223   
224   dbus_free (bd);
225 }
226
227 static dbus_bool_t
228 setup_server (BusContext *context,
229               DBusServer *server,
230               char      **auth_mechanisms,
231               DBusError  *error)
232 {
233   BusServerData *bd;
234
235   bd = dbus_new0 (BusServerData, 1);
236   if (!dbus_server_set_data (server,
237                              server_data_slot,
238                              bd, free_server_data))
239     {
240       dbus_free (bd);
241       BUS_SET_OOM (error);
242       return FALSE;
243     }
244
245   bd->context = context;
246   
247   if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
248     {
249       BUS_SET_OOM (error);
250       return FALSE;
251     }
252   
253   dbus_server_set_new_connection_function (server,
254                                            new_connection_callback,
255                                            context, NULL);
256   
257   if (!dbus_server_set_watch_functions (server,
258                                         add_server_watch,
259                                         remove_server_watch,
260                                         NULL,
261                                         server,
262                                         NULL))
263     {
264       BUS_SET_OOM (error);
265       return FALSE;
266     }
267
268   if (!dbus_server_set_timeout_functions (server,
269                                           add_server_timeout,
270                                           remove_server_timeout,
271                                           NULL,
272                                           server, NULL))
273     {
274       BUS_SET_OOM (error);
275       return FALSE;
276     }
277   
278   return TRUE;
279 }
280
281 BusContext*
282 bus_context_new (const DBusString *config_file,
283                  dbus_bool_t       force_fork,
284                  int               print_addr_fd,
285                  int               print_pid_fd,
286                  DBusError        *error)
287 {
288   BusContext *context;
289   DBusList *link;
290   DBusList **addresses;
291   BusConfigParser *parser;
292   DBusString full_address;
293   const char *user, *pidfile;
294   char **auth_mechanisms;
295   DBusList **auth_mechanisms_list;
296   int len;
297   
298   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
299
300   if (!_dbus_string_init (&full_address))
301     {
302       BUS_SET_OOM (error);
303       return NULL;
304     }
305
306   if (!server_data_slot_ref ())
307     {
308       BUS_SET_OOM (error);
309       _dbus_string_free (&full_address);
310       return NULL;
311     }
312   
313   parser = NULL;
314   context = NULL;
315   auth_mechanisms = NULL;
316   
317   parser = bus_config_load (config_file, TRUE, error);
318   if (parser == NULL)
319     goto failed;
320
321   /* Check for an existing pid file. Of course this is a race;
322    * we'd have to use fcntl() locks on the pid file to
323    * avoid that. But we want to check for the pid file
324    * before overwriting any existing sockets, etc.
325    */
326   pidfile = bus_config_parser_get_pidfile (parser);
327   if (pidfile != NULL)
328     {
329       DBusString u;
330       DBusStat stbuf;
331       DBusError tmp_error;
332       
333       dbus_error_init (&tmp_error);
334       _dbus_string_init_const (&u, pidfile);
335       
336       if (_dbus_stat (&u, &stbuf, &tmp_error))
337         {
338           dbus_set_error (error, DBUS_ERROR_FAILED,
339                           "The pid file \"%s\" exists, if the message bus is not running, remove this file",
340                           pidfile);
341           dbus_error_free (&tmp_error);
342           goto failed;
343         }
344     }
345   
346   context = dbus_new0 (BusContext, 1);
347   if (context == NULL)
348     {
349       BUS_SET_OOM (error);
350       goto failed;
351     }
352   
353   context->refcount = 1;
354
355   /* get our limits and timeout lengths */
356   bus_config_parser_get_limits (parser, &context->limits);
357   
358   /* we need another ref of the server data slot for the context
359    * to own
360    */
361   if (!server_data_slot_ref ())
362     _dbus_assert_not_reached ("second ref of server data slot failed");
363   
364   context->user_database = _dbus_user_database_new ();
365   if (context->user_database == NULL)
366     {
367       BUS_SET_OOM (error);
368       goto failed;
369     }
370   
371   context->loop = _dbus_loop_new ();
372   if (context->loop == NULL)
373     {
374       BUS_SET_OOM (error);
375       goto failed;
376     }
377   
378   /* Build an array of auth mechanisms */
379   
380   auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
381   len = _dbus_list_get_length (auth_mechanisms_list);
382
383   if (len > 0)
384     {
385       int i;
386
387       auth_mechanisms = dbus_new0 (char*, len + 1);
388       if (auth_mechanisms == NULL)
389         goto failed;
390       
391       i = 0;
392       link = _dbus_list_get_first_link (auth_mechanisms_list);
393       while (link != NULL)
394         {
395           auth_mechanisms[i] = _dbus_strdup (link->data);
396           if (auth_mechanisms[i] == NULL)
397             goto failed;
398           link = _dbus_list_get_next_link (auth_mechanisms_list, link);
399         }
400     }
401   else
402     {
403       auth_mechanisms = NULL;
404     }
405
406   /* Listen on our addresses */
407   
408   addresses = bus_config_parser_get_addresses (parser);  
409   
410   link = _dbus_list_get_first_link (addresses);
411   while (link != NULL)
412     {
413       DBusServer *server;
414       
415       server = dbus_server_listen (link->data, error);
416       if (server == NULL)
417         goto failed;
418       else if (!setup_server (context, server, auth_mechanisms, error))
419         goto failed;
420
421       if (!_dbus_list_append (&context->servers, server))
422         {
423           BUS_SET_OOM (error);
424           goto failed;
425         }          
426       
427       link = _dbus_list_get_next_link (addresses, link);
428     }
429
430   /* note that type may be NULL */
431   context->type = _dbus_strdup (bus_config_parser_get_type (parser));
432   
433   /* We have to build the address backward, so that
434    * <listen> later in the config file have priority
435    */
436   link = _dbus_list_get_last_link (&context->servers);
437   while (link != NULL)
438     {
439       char *addr;
440       
441       addr = dbus_server_get_address (link->data);
442       if (addr == NULL)
443         {
444           BUS_SET_OOM (error);
445           goto failed;
446         }
447
448       if (_dbus_string_get_length (&full_address) > 0)
449         {
450           if (!_dbus_string_append (&full_address, ";"))
451             {
452               BUS_SET_OOM (error);
453               goto failed;
454             }
455         }
456
457       if (!_dbus_string_append (&full_address, addr))
458         {
459           BUS_SET_OOM (error);
460           goto failed;
461         }
462
463       dbus_free (addr);
464
465       link = _dbus_list_get_prev_link (&context->servers, link);
466     }
467
468   if (!_dbus_string_copy_data (&full_address, &context->address))
469     {
470       BUS_SET_OOM (error);
471       goto failed;
472     }
473
474   /* Note that we don't know whether the print_addr_fd is
475    * one of the sockets we're using to listen on, or some
476    * other random thing. But I think the answer is "don't do
477    * that then"
478    */
479   if (print_addr_fd >= 0)
480     {
481       DBusString addr;
482       const char *a = bus_context_get_address (context);
483       int bytes;
484       
485       _dbus_assert (a != NULL);
486       if (!_dbus_string_init (&addr))
487         {
488           BUS_SET_OOM (error);
489           goto failed;
490         }
491       
492       if (!_dbus_string_append (&addr, a) ||
493           !_dbus_string_append (&addr, "\n"))
494         {
495           _dbus_string_free (&addr);
496           BUS_SET_OOM (error);
497           goto failed;
498         }
499
500       bytes = _dbus_string_get_length (&addr);
501       if (_dbus_write (print_addr_fd, &addr, 0, bytes) != bytes)
502         {
503           dbus_set_error (error, DBUS_ERROR_FAILED,
504                           "Printing message bus address: %s\n",
505                           _dbus_strerror (errno));
506           _dbus_string_free (&addr);
507           goto failed;
508         }
509
510       if (print_addr_fd > 2)
511         _dbus_close (print_addr_fd, NULL);
512
513       _dbus_string_free (&addr);
514     }
515   
516   /* Create activation subsystem */
517   
518   context->activation = bus_activation_new (context, &full_address,
519                                             bus_config_parser_get_service_dirs (parser),
520                                             error);
521   if (context->activation == NULL)
522     {
523       _DBUS_ASSERT_ERROR_IS_SET (error);
524       goto failed;
525     }
526
527   context->connections = bus_connections_new (context);
528   if (context->connections == NULL)
529     {
530       BUS_SET_OOM (error);
531       goto failed;
532     }
533
534   context->registry = bus_registry_new (context);
535   if (context->registry == NULL)
536     {
537       BUS_SET_OOM (error);
538       goto failed;
539     }
540
541   context->policy = bus_config_parser_steal_policy (parser);
542   _dbus_assert (context->policy != NULL);
543   
544   /* Now become a daemon if appropriate */
545   if (force_fork || bus_config_parser_get_fork (parser))
546     {
547       DBusString u;
548
549       if (pidfile)
550         _dbus_string_init_const (&u, pidfile);
551       
552       if (!_dbus_become_daemon (pidfile ? &u : NULL, error))
553         goto failed;
554     }
555   else
556     {
557       /* Need to write PID file for ourselves, not for the child process */
558       if (pidfile != NULL)
559         {
560           DBusString u;
561
562           _dbus_string_init_const (&u, pidfile);
563           
564           if (!_dbus_write_pid_file (&u, _dbus_getpid (), error))
565             goto failed;
566         }
567     }
568
569   /* keep around the pid filename so we can delete it later */
570   context->pidfile = _dbus_strdup (pidfile);
571
572   /* Write PID if requested */
573   if (print_pid_fd >= 0)
574     {
575       DBusString pid;
576       int bytes;
577
578       if (!_dbus_string_init (&pid))
579         {
580           BUS_SET_OOM (error);
581           goto failed;
582         }
583       
584       if (!_dbus_string_append_int (&pid, _dbus_getpid ()) ||
585           !_dbus_string_append (&pid, "\n"))
586         {
587           _dbus_string_free (&pid);
588           BUS_SET_OOM (error);
589           goto failed;
590         }
591
592       bytes = _dbus_string_get_length (&pid);
593       if (_dbus_write (print_pid_fd, &pid, 0, bytes) != bytes)
594         {
595           dbus_set_error (error, DBUS_ERROR_FAILED,
596                           "Printing message bus PID: %s\n",
597                           _dbus_strerror (errno));
598           _dbus_string_free (&pid);
599           goto failed;
600         }
601
602       if (print_pid_fd > 2)
603         _dbus_close (print_pid_fd, NULL);
604       
605       _dbus_string_free (&pid);
606     }
607   
608   /* Here we change our credentials if required,
609    * as soon as we've set up our sockets and pidfile
610    */
611   user = bus_config_parser_get_user (parser);
612   if (user != NULL)
613     {
614       DBusCredentials creds;
615       DBusString u;
616
617       _dbus_string_init_const (&u, user);
618
619       if (!_dbus_credentials_from_username (&u, &creds) ||
620           creds.uid < 0 ||
621           creds.gid < 0)
622         {
623           dbus_set_error (error, DBUS_ERROR_FAILED,
624                           "Could not get UID and GID for username \"%s\"",
625                           user);
626           goto failed;
627         }
628       
629       if (!_dbus_change_identity (creds.uid, creds.gid, error))
630         goto failed;
631     }
632   
633   bus_config_parser_unref (parser);
634   _dbus_string_free (&full_address);
635   dbus_free_string_array (auth_mechanisms);
636   server_data_slot_unref ();
637   
638   return context;
639   
640  failed:  
641   if (parser != NULL)
642     bus_config_parser_unref (parser);
643
644   if (context != NULL)
645     bus_context_unref (context);
646
647   _dbus_string_free (&full_address);
648   dbus_free_string_array (auth_mechanisms);
649
650   server_data_slot_unref ();
651   
652   return NULL;
653 }
654
655 static void
656 shutdown_server (BusContext *context,
657                  DBusServer *server)
658 {
659   if (server == NULL ||
660       !dbus_server_get_is_connected (server))
661     return;
662   
663   if (!dbus_server_set_watch_functions (server,
664                                         NULL, NULL, NULL,
665                                         context,
666                                         NULL))
667     _dbus_assert_not_reached ("setting watch functions to NULL failed");
668   
669   if (!dbus_server_set_timeout_functions (server,
670                                           NULL, NULL, NULL,
671                                           context,
672                                           NULL))
673     _dbus_assert_not_reached ("setting timeout functions to NULL failed");
674   
675   dbus_server_disconnect (server);
676 }
677
678 void
679 bus_context_shutdown (BusContext  *context)
680 {
681   DBusList *link;
682
683   link = _dbus_list_get_first_link (&context->servers);
684   while (link != NULL)
685     {
686       shutdown_server (context, link->data);
687
688       link = _dbus_list_get_next_link (&context->servers, link);
689     }
690 }
691
692 void
693 bus_context_ref (BusContext *context)
694 {
695   _dbus_assert (context->refcount > 0);
696   context->refcount += 1;
697 }
698
699 void
700 bus_context_unref (BusContext *context)
701 {
702   _dbus_assert (context->refcount > 0);
703   context->refcount -= 1;
704
705   if (context->refcount == 0)
706     {
707       DBusList *link;
708       
709       _dbus_verbose ("Finalizing bus context %p\n", context);
710       
711       bus_context_shutdown (context);
712
713       if (context->connections)
714         {
715           bus_connections_unref (context->connections);
716           context->connections = NULL;
717         }
718       
719       if (context->registry)
720         {
721           bus_registry_unref (context->registry);
722           context->registry = NULL;
723         }
724       
725       if (context->activation)
726         {
727           bus_activation_unref (context->activation);
728           context->activation = NULL;
729         }
730
731       link = _dbus_list_get_first_link (&context->servers);
732       while (link != NULL)
733         {
734           dbus_server_unref (link->data);
735           
736           link = _dbus_list_get_next_link (&context->servers, link);
737         }
738       _dbus_list_clear (&context->servers);
739
740       if (context->policy)
741         {
742           bus_policy_unref (context->policy);
743           context->policy = NULL;
744         }
745       
746       if (context->loop)
747         {
748           _dbus_loop_unref (context->loop);
749           context->loop = NULL;
750         }
751       
752       dbus_free (context->type);
753       dbus_free (context->address);
754
755       if (context->pidfile)
756         {
757           DBusString u;
758           _dbus_string_init_const (&u, context->pidfile);
759
760           /* Deliberately ignore errors here, since there's not much
761            * we can do about it, and we're exiting anyways.
762            */
763           _dbus_delete_file (&u, NULL);
764
765           dbus_free (context->pidfile); 
766         }
767
768       _dbus_user_database_unref (context->user_database);
769       
770       dbus_free (context);
771
772       server_data_slot_unref ();
773     }
774 }
775
776 /* type may be NULL */
777 const char*
778 bus_context_get_type (BusContext *context)
779 {
780   return context->type;
781 }
782
783 const char*
784 bus_context_get_address (BusContext *context)
785 {
786   return context->address;
787 }
788
789 BusRegistry*
790 bus_context_get_registry (BusContext  *context)
791 {
792   return context->registry;
793 }
794
795 BusConnections*
796 bus_context_get_connections (BusContext  *context)
797 {
798   return context->connections;
799 }
800
801 BusActivation*
802 bus_context_get_activation (BusContext  *context)
803 {
804   return context->activation;
805 }
806
807 DBusLoop*
808 bus_context_get_loop (BusContext *context)
809 {
810   return context->loop;
811 }
812
813 DBusUserDatabase*
814 bus_context_get_user_database (BusContext *context)
815 {
816   return context->user_database;
817 }
818
819 dbus_bool_t
820 bus_context_allow_user (BusContext   *context,
821                         unsigned long uid)
822 {
823   return bus_policy_allow_user (context->policy,
824                                 context->user_database,
825                                 uid);
826 }
827
828 BusClientPolicy*
829 bus_context_create_client_policy (BusContext      *context,
830                                   DBusConnection  *connection,
831                                   DBusError       *error)
832 {
833   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
834   return bus_policy_create_client_policy (context->policy, connection,
835                                           error);
836 }
837
838 int
839 bus_context_get_activation_timeout (BusContext *context)
840 {
841   
842   return context->limits.activation_timeout;
843 }
844
845 int
846 bus_context_get_auth_timeout (BusContext *context)
847 {
848   return context->limits.auth_timeout;
849 }
850
851 int
852 bus_context_get_max_completed_connections (BusContext *context)
853 {
854   return context->limits.max_completed_connections;
855 }
856
857 int
858 bus_context_get_max_incomplete_connections (BusContext *context)
859 {
860   return context->limits.max_incomplete_connections;
861 }
862
863 int
864 bus_context_get_max_connections_per_user (BusContext *context)
865 {
866   return context->limits.max_connections_per_user;
867 }
868
869 int
870 bus_context_get_max_pending_activations (BusContext *context)
871 {
872   return context->limits.max_pending_activations;
873 }
874
875 int
876 bus_context_get_max_services_per_connection (BusContext *context)
877 {
878   return context->limits.max_services_per_connection;
879 }
880
881 dbus_bool_t
882 bus_context_check_security_policy (BusContext     *context,
883                                    DBusConnection *sender,
884                                    DBusConnection *recipient,
885                                    DBusMessage    *message,
886                                    DBusError      *error)
887 {
888   BusClientPolicy *sender_policy;
889   BusClientPolicy *recipient_policy;
890
891   /* NULL sender/receiver means the bus driver */
892
893   if (sender != NULL)
894     {
895       if (bus_connection_is_active (sender))
896         {
897           sender_policy = bus_connection_get_policy (sender);
898           _dbus_assert (sender_policy != NULL);
899         }
900       else
901         {
902           /* Policy for inactive connections is that they can only send
903            * the hello message to the bus driver
904            */
905           if (recipient == NULL &&
906               dbus_message_has_name (message, DBUS_MESSAGE_HELLO))
907             {
908               _dbus_verbose ("security check allowing %s message\n",
909                              DBUS_MESSAGE_HELLO);
910               return TRUE;
911             }
912           else
913             {
914               _dbus_verbose ("security check disallowing non-%s message\n",
915                              DBUS_MESSAGE_HELLO);
916
917               dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
918                               "Client tried to send a message other than %s without being registered",
919                               DBUS_MESSAGE_HELLO);
920               
921               return FALSE;
922             }
923         }
924     }
925   else
926     sender_policy = NULL;
927
928   _dbus_assert ((sender != NULL && sender_policy != NULL) ||
929                 (sender == NULL && sender_policy == NULL));
930   
931   if (recipient != NULL)
932     {
933       /* only the bus driver can send to an inactive recipient (as it
934        * owns no services, so other apps can't address it). Inactive
935        * recipients can receive any message.
936        */
937       if (bus_connection_is_active (recipient))
938         {
939           recipient_policy = bus_connection_get_policy (recipient);
940           _dbus_assert (recipient_policy != NULL);
941         }
942       else if (sender == NULL)
943         {
944           _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
945           recipient_policy = NULL;
946         }
947       else
948         {
949           _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
950           recipient_policy = NULL;
951         }
952     }
953   else
954     recipient_policy = NULL;
955   
956   _dbus_assert ((recipient != NULL && recipient_policy != NULL) ||
957                 (recipient != NULL && sender == NULL && recipient_policy == NULL) ||
958                 (recipient == NULL && recipient_policy == NULL));
959   
960   if (sender_policy &&
961       !bus_client_policy_check_can_send (sender_policy,
962                                          context->registry, recipient,
963                                          message))
964     {
965       const char *dest = dbus_message_get_destination (message);
966       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
967                       "A security policy in place prevents this sender "
968                       "from sending this message to this recipient, "
969                       "see message bus configuration file (rejected message "
970                       "had name \"%s\" destination \"%s\")",
971                       dbus_message_get_name (message),
972                       dest ? dest : DBUS_SERVICE_DBUS);
973       return FALSE;
974     }
975
976   if (recipient_policy &&
977       !bus_client_policy_check_can_receive (recipient_policy,
978                                             context->registry, sender,
979                                             message))
980     {
981       const char *dest = dbus_message_get_destination (message);
982       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
983                       "A security policy in place prevents this recipient "
984                       "from receiving this message from this sender, "
985                       "see message bus configuration file (rejected message "
986                       "had name \"%s\" destination \"%s\")",
987                       dbus_message_get_name (message),
988                       dest ? dest : DBUS_SERVICE_DBUS);
989       return FALSE;
990     }
991
992   /* See if limits on size have been exceeded */
993   if (recipient &&
994       dbus_connection_get_outgoing_size (recipient) >
995       context->limits.max_outgoing_bytes)
996     {
997       const char *dest = dbus_message_get_destination (message);
998       dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
999                       "The destination service \"%s\" has a full message queue",
1000                       dest ? dest : DBUS_SERVICE_DBUS);
1001       return FALSE;
1002     }
1003   
1004   return TRUE;
1005 }