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