address for daemon changed to "kdbus:", licencses and copyrights added
[platform/upstream/dbus.git] / bus / driver.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* driver.c  Bus client (driver)
3  *
4  * Copyright (C) 2003 CodeFactory AB
5  * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6  * Copyright (C) 2013  Samsung Electronics
7  *
8  * Licensed under the Academic Free License version 2.1
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23  *
24  */
25
26 #include <config.h>
27 #include "activation.h"
28 #include "connection.h"
29 #include "driver.h"
30 #include "dispatch.h"
31 #include "services.h"
32 #include "selinux.h"
33 #include "signals.h"
34 #include "stats.h"
35 #include "utils.h"
36
37 #include <dbus/dbus-asv-util.h>
38 #include <dbus/dbus-string.h>
39 #include <dbus/dbus-internals.h>
40 #include <dbus/dbus-message.h>
41 #include <dbus/dbus-marshal-recursive.h>
42 #include <string.h>
43
44 #include "kdbus-d.h"
45 #include <stdio.h>
46 #include <errno.h>
47 #include <limits.h>
48
49 static DBusConnection *
50 bus_driver_get_conn_helper (DBusConnection  *connection,
51                             DBusMessage     *message,
52                             const char      *what_we_want,
53                             const char     **name_p,
54                             DBusError       *error)
55 {
56   const char *name;
57   BusRegistry *registry;
58   BusService *serv;
59   DBusString str;
60   DBusConnection *conn;
61
62   if (!dbus_message_get_args (message, error,
63                               DBUS_TYPE_STRING, &name,
64                               DBUS_TYPE_INVALID))
65     return NULL;
66
67   _dbus_assert (name != NULL);
68   _dbus_verbose ("asked for %s of connection %s\n", what_we_want, name);
69
70   registry = bus_connection_get_registry (connection);
71   _dbus_string_init_const (&str, name);
72   serv = bus_registry_lookup (registry, &str);
73
74   if (serv == NULL)
75     {
76       dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
77                       "Could not get %s of name '%s': no such name",
78                       what_we_want, name);
79       return NULL;
80     }
81
82   conn = bus_service_get_primary_owners_connection (serv);
83   _dbus_assert (conn != NULL);
84
85   if (name_p != NULL)
86     *name_p = name;
87
88   return conn;
89 }
90
91 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
92                                                     DBusMessage    *hello_message,
93                                                     BusTransaction *transaction,
94                                                     DBusError      *error);
95
96 dbus_bool_t
97 bus_driver_send_service_owner_changed (const char     *service_name,
98                                        const char     *old_owner,
99                                        const char     *new_owner,
100                                        BusTransaction *transaction,
101                                        DBusError      *error)
102 {
103   DBusMessage *message;
104   dbus_bool_t retval;
105   const char *null_service;
106
107   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
108           return TRUE;
109
110   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
111
112   null_service = "";
113   _dbus_verbose ("sending name owner changed: %s [%s -> %s]\n",
114                  service_name,
115                  old_owner ? old_owner : null_service,
116                  new_owner ? new_owner : null_service);
117
118   message = dbus_message_new_signal (DBUS_PATH_DBUS,
119                                      DBUS_INTERFACE_DBUS,
120                                      "NameOwnerChanged");
121
122   if (message == NULL)
123     {
124       BUS_SET_OOM (error);
125       return FALSE;
126     }
127
128   if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
129     goto oom;
130
131   if (!dbus_message_append_args (message,
132                                  DBUS_TYPE_STRING, &service_name,
133                                  DBUS_TYPE_STRING, old_owner ? &old_owner : &null_service,
134                                  DBUS_TYPE_STRING, new_owner ? &new_owner : &null_service,
135                                  DBUS_TYPE_INVALID))
136     goto oom;
137
138   _dbus_assert (dbus_message_has_signature (message, "sss"));
139
140   retval = bus_dispatch_matches (transaction, NULL, NULL, message, error);
141   dbus_message_unref (message);
142
143   return retval;
144
145  oom:
146   dbus_message_unref (message);
147   BUS_SET_OOM (error);
148   return FALSE;
149 }
150
151 dbus_bool_t
152 bus_driver_send_service_lost (DBusConnection *connection,
153                               const char     *service_name,
154                               BusTransaction *transaction,
155                               DBusError      *error)
156 {
157   DBusMessage *message;
158
159   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
160           return TRUE;
161
162   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
163
164   message = dbus_message_new_signal (DBUS_PATH_DBUS,
165                                      DBUS_INTERFACE_DBUS,
166                                      "NameLost");
167
168   if (message == NULL)
169     {
170       BUS_SET_OOM (error);
171       return FALSE;
172     }
173
174   if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
175       !dbus_message_append_args (message,
176                                  DBUS_TYPE_STRING, &service_name,
177                                  DBUS_TYPE_INVALID))
178     {
179       dbus_message_unref (message);
180       BUS_SET_OOM (error);
181       return FALSE;
182     }
183
184   if (!bus_transaction_send_from_driver (transaction, connection, message))
185     {
186       dbus_message_unref (message);
187       BUS_SET_OOM (error);
188       return FALSE;
189     }
190   else
191     {
192       dbus_message_unref (message);
193       return TRUE;
194     }
195 }
196
197 dbus_bool_t
198 bus_driver_send_service_acquired (DBusConnection *connection,
199                                   const char     *service_name,
200                                   BusTransaction *transaction,
201                                   DBusError      *error)
202 {
203   DBusMessage *message;
204
205   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
206           return TRUE;
207
208   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
209
210   message = dbus_message_new_signal (DBUS_PATH_DBUS,
211                                      DBUS_INTERFACE_DBUS,
212                                      "NameAcquired");
213
214   if (message == NULL)
215     {
216       BUS_SET_OOM (error);
217       return FALSE;
218     }
219
220   if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
221       !dbus_message_append_args (message,
222                                  DBUS_TYPE_STRING, &service_name,
223                                  DBUS_TYPE_INVALID))
224     {
225       dbus_message_unref (message);
226       BUS_SET_OOM (error);
227       return FALSE;
228     }
229
230   if (!bus_transaction_send_from_driver (transaction, connection, message))
231     {
232       dbus_message_unref (message);
233       BUS_SET_OOM (error);
234       return FALSE;
235     }
236   else
237     {
238       dbus_message_unref (message);
239       return TRUE;
240     }
241 }
242
243 static dbus_bool_t
244 create_unique_client_name (BusRegistry *registry,
245                            DBusString  *str)
246 {
247   /* We never want to use the same unique client name twice, because
248    * we want to guarantee that if you send a message to a given unique
249    * name, you always get the same application. So we use two numbers
250    * for INT_MAX * INT_MAX combinations, should be pretty safe against
251    * wraparound.
252    */
253   /* FIXME these should be in BusRegistry rather than static vars */
254   static int next_major_number = 0;
255   static int next_minor_number = 0;
256   int len;
257
258   len = _dbus_string_get_length (str);
259
260   while (TRUE)
261     {
262       /* start out with 1-0, go to 1-1, 1-2, 1-3,
263        * up to 1-MAXINT, then 2-0, 2-1, etc.
264        */
265       if (next_minor_number <= 0)
266         {
267           next_major_number += 1;
268           next_minor_number = 0;
269           if (next_major_number <= 0)
270             _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
271         }
272
273       _dbus_assert (next_major_number > 0);
274       _dbus_assert (next_minor_number >= 0);
275
276       /* appname:MAJOR-MINOR */
277
278       if (!_dbus_string_append (str, ":"))
279         return FALSE;
280
281       if (!_dbus_string_append_int (str, next_major_number))
282         return FALSE;
283
284       if (!_dbus_string_append (str, "."))
285         return FALSE;
286
287       if (!_dbus_string_append_int (str, next_minor_number))
288         return FALSE;
289
290       next_minor_number += 1;
291
292       /* Check if a client with the name exists */
293       if (bus_registry_lookup (registry, str) == NULL)
294         break;
295
296       /* drop the number again, try the next one. */
297       _dbus_string_set_length (str, len);
298     }
299
300   return TRUE;
301 }
302
303 static dbus_bool_t
304 bus_driver_handle_hello (DBusConnection *connection,
305                          BusTransaction *transaction,
306                          DBusMessage    *message,
307                          DBusError      *error)
308 {
309   DBusString unique_name;
310   BusService *service;
311   dbus_bool_t retval;
312   BusRegistry *registry;
313   BusConnections *connections;
314
315   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
316
317   if (bus_connection_is_active (connection))
318     {
319       /* We already handled an Hello message for this connection. */
320       dbus_set_error (error, DBUS_ERROR_FAILED,
321                       "Already handled an Hello message");
322       return FALSE;
323     }
324
325   /* Note that when these limits are exceeded we don't disconnect the
326    * connection; we just sort of leave it hanging there until it times
327    * out or disconnects itself or is dropped due to the max number of
328    * incomplete connections. It's even OK if the connection wants to
329    * retry the hello message, we support that.
330    */
331   connections = bus_connection_get_connections (connection);
332   if (!bus_connections_check_limits (connections, connection,
333                                      error))
334     {
335       _DBUS_ASSERT_ERROR_IS_SET (error);
336       return FALSE;
337     }
338
339   if (!_dbus_string_init (&unique_name))
340     {
341       BUS_SET_OOM (error);
342       return FALSE;
343     }
344
345   retval = FALSE;
346
347   registry = bus_connection_get_registry (connection);
348
349   if (!create_unique_client_name (registry, &unique_name))
350     {
351       BUS_SET_OOM (error);
352       goto out_0;
353     }
354
355   if (!bus_connection_complete (connection, &unique_name, error))
356     {
357       _DBUS_ASSERT_ERROR_IS_SET (error);
358       goto out_0;
359     }
360
361   if (!dbus_message_set_sender (message,
362                                 bus_connection_get_name (connection)))
363     {
364       BUS_SET_OOM (error);
365       goto out_0;
366     }
367
368   if (!bus_driver_send_welcome_message (connection, message, transaction, error))
369     goto out_0;
370
371   /* Create the service */
372   service = bus_registry_ensure (registry,
373                                  &unique_name, connection, 0, transaction, error);
374   if (service == NULL)
375     goto out_0;
376
377   _dbus_assert (bus_connection_is_active (connection));
378   retval = TRUE;
379
380  out_0:
381   _dbus_string_free (&unique_name);
382   return retval;
383 }
384
385 static dbus_bool_t
386 bus_driver_send_welcome_message (DBusConnection *connection,
387                                  DBusMessage    *hello_message,
388                                  BusTransaction *transaction,
389                                  DBusError      *error)
390 {
391   DBusMessage *welcome;
392   const char *name;
393
394   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
395
396   name = bus_connection_get_name (connection);
397   _dbus_assert (name != NULL);
398
399   welcome = dbus_message_new_method_return (hello_message);
400   if (welcome == NULL)
401     {
402       BUS_SET_OOM (error);
403       return FALSE;
404     }
405
406   if (!dbus_message_append_args (welcome,
407                                  DBUS_TYPE_STRING, &name,
408                                  DBUS_TYPE_INVALID))
409     {
410       dbus_message_unref (welcome);
411       BUS_SET_OOM (error);
412       return FALSE;
413     }
414
415   _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
416
417   if (!bus_transaction_send_from_driver (transaction, connection, welcome))
418     {
419       dbus_message_unref (welcome);
420       BUS_SET_OOM (error);
421       return FALSE;
422     }
423   else
424     {
425       dbus_message_unref (welcome);
426       return TRUE;
427     }
428 }
429
430 static dbus_bool_t
431 bus_driver_handle_list_services (DBusConnection *connection,
432                                  BusTransaction *transaction,
433                                  DBusMessage    *message,
434                                  DBusError      *error)
435 {
436   DBusMessage *reply;
437   int len;
438   char **services;
439   BusRegistry *registry;
440   int i;
441   DBusMessageIter iter;
442   DBusMessageIter sub;
443
444   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
445
446   registry = bus_connection_get_registry (connection);
447
448   reply = dbus_message_new_method_return (message);
449   if (reply == NULL)
450     {
451       BUS_SET_OOM (error);
452       return FALSE;
453     }
454
455
456   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
457   {
458           if(!kdbus_list_services (connection, &services, &len))
459             {
460               dbus_message_unref (reply);
461               BUS_SET_OOM (error);
462               return FALSE;
463             }
464   }
465   else
466   if (!bus_registry_list_services (registry, &services, &len))
467     {
468       dbus_message_unref (reply);
469       BUS_SET_OOM (error);
470       return FALSE;
471     }
472
473   dbus_message_iter_init_append (reply, &iter);
474
475   if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
476                                          DBUS_TYPE_STRING_AS_STRING,
477                                          &sub))
478     {
479       dbus_free_string_array (services);
480       dbus_message_unref (reply);
481       BUS_SET_OOM (error);
482       return FALSE;
483     }
484
485   if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) //not needed for kdbus, we got it from kdbus_list_services
486   {
487     /* Include the bus driver in the list */
488     const char *v_STRING = DBUS_SERVICE_DBUS;
489     if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
490                                          &v_STRING))
491       {
492         dbus_free_string_array (services);
493         dbus_message_unref (reply);
494         BUS_SET_OOM (error);
495         return FALSE;
496       }
497   }
498
499   i = 0;
500   while (i < len)
501     {
502       if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
503                                            &services[i]))
504         {
505           dbus_free_string_array (services);
506           dbus_message_unref (reply);
507           BUS_SET_OOM (error);
508           return FALSE;
509         }
510       ++i;
511     }
512
513   dbus_free_string_array (services);
514
515   if (!dbus_message_iter_close_container (&iter, &sub))
516     {
517       dbus_message_unref (reply);
518       BUS_SET_OOM (error);
519       return FALSE;
520     }
521
522   if (!bus_transaction_send_from_driver (transaction, connection, reply))
523     {
524       dbus_message_unref (reply);
525       BUS_SET_OOM (error);
526       return FALSE;
527     }
528   else
529     {
530       dbus_message_unref (reply);
531       return TRUE;
532     }
533 }
534
535 static dbus_bool_t
536 bus_driver_handle_list_activatable_services (DBusConnection *connection,
537                                              BusTransaction *transaction,
538                                              DBusMessage    *message,
539                                              DBusError      *error)
540 {
541   DBusMessage *reply;
542   int len;
543   char **services;
544   BusActivation *activation;
545   int i;
546   DBusMessageIter iter;
547   DBusMessageIter sub;
548
549   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
550
551   activation = bus_connection_get_activation (connection);
552
553   reply = dbus_message_new_method_return (message);
554   if (reply == NULL)
555     {
556       BUS_SET_OOM (error);
557       return FALSE;
558     }
559
560   if (!bus_activation_list_services (activation, &services, &len))
561     {
562       dbus_message_unref (reply);
563       BUS_SET_OOM (error);
564       return FALSE;
565     }
566
567   dbus_message_iter_init_append (reply, &iter);
568
569   if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
570                                          DBUS_TYPE_STRING_AS_STRING,
571                                          &sub))
572     {
573       dbus_free_string_array (services);
574       dbus_message_unref (reply);
575       BUS_SET_OOM (error);
576       return FALSE;
577     }
578
579   {
580     /* Include the bus driver in the list */
581     const char *v_STRING = DBUS_SERVICE_DBUS;
582     if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
583                                          &v_STRING))
584       {
585         dbus_free_string_array (services);
586         dbus_message_unref (reply);
587         BUS_SET_OOM (error);
588         return FALSE;
589       }
590   }
591
592   i = 0;
593   while (i < len)
594     {
595       if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
596                                            &services[i]))
597         {
598           dbus_free_string_array (services);
599           dbus_message_unref (reply);
600           BUS_SET_OOM (error);
601           return FALSE;
602         }
603       ++i;
604     }
605
606   dbus_free_string_array (services);
607
608   if (!dbus_message_iter_close_container (&iter, &sub))
609     {
610       dbus_message_unref (reply);
611       BUS_SET_OOM (error);
612       return FALSE;
613     }
614
615   if (!bus_transaction_send_from_driver (transaction, connection, reply))
616     {
617       dbus_message_unref (reply);
618       BUS_SET_OOM (error);
619       return FALSE;
620     }
621   else
622     {
623       dbus_message_unref (reply);
624       return TRUE;
625     }
626 }
627
628 static dbus_bool_t
629 bus_driver_handle_acquire_service (DBusConnection *connection,
630                                    BusTransaction *transaction,
631                                    DBusMessage    *message,
632                                    DBusError      *error)
633 {
634   DBusMessage *reply;
635   DBusString service_name;
636   const char *name;
637   dbus_uint32_t service_reply;
638   dbus_uint32_t flags;
639   dbus_bool_t retval;
640   BusRegistry *registry;
641
642   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
643
644   registry = bus_connection_get_registry (connection);
645
646   if (!dbus_message_get_args (message, error,
647                               DBUS_TYPE_STRING, &name,
648                               DBUS_TYPE_UINT32, &flags,
649                               DBUS_TYPE_INVALID))
650     return FALSE;
651
652   _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
653
654   retval = FALSE;
655   reply = NULL;
656
657   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
658   {
659           if (!bus_registry_acquire_kdbus_service (registry, connection,
660                                                                                  message,
661                                              &service_reply, transaction,
662                                              error))
663             goto out;
664   }
665   else
666   {
667           _dbus_string_init_const (&service_name, name);
668
669           if (!bus_registry_acquire_service (registry, connection,
670                                              &service_name, flags,
671                                              &service_reply, transaction,
672                                              error))
673             goto out;
674
675   }
676
677   reply = dbus_message_new_method_return (message);
678   if (reply == NULL)
679     {
680       BUS_SET_OOM (error);
681       goto out;
682     }
683
684   if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
685     {
686       BUS_SET_OOM (error);
687       goto out;
688     }
689
690   if (!bus_transaction_send_from_driver (transaction, connection, reply))
691     {
692       BUS_SET_OOM (error);
693       goto out;
694     }
695
696   retval = TRUE;
697
698  out:
699   if (reply)
700     dbus_message_unref (reply);
701   return retval;
702 }
703
704 static dbus_bool_t
705 bus_driver_handle_release_service (DBusConnection *connection,
706                                    BusTransaction *transaction,
707                                    DBusMessage    *message,
708                                    DBusError      *error)
709 {
710   DBusMessage *reply;
711   DBusString service_name;
712   const char *name;
713   dbus_uint32_t service_reply;
714   dbus_bool_t retval;
715   BusRegistry *registry;
716
717   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
718
719   if (!dbus_message_get_args (message, error,
720                               DBUS_TYPE_STRING, &name,
721                               DBUS_TYPE_INVALID))
722     return FALSE;
723
724   _dbus_verbose ("Trying to release name %s\n", name);
725
726   retval = FALSE;
727   reply = NULL;
728
729   _dbus_string_init_const (&service_name, name);
730   registry = bus_connection_get_registry (connection);
731
732   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
733   {
734           if (!bus_registry_release_service_kdbus (dbus_message_get_sender(message), connection,
735                                              &service_name, &service_reply,
736                                              transaction, error))
737               goto out;
738   }
739   else if (!bus_registry_release_service (registry, connection,
740                                      &service_name, &service_reply,
741                                      transaction, error))
742     goto out;
743
744   reply = dbus_message_new_method_return (message);
745   if (reply == NULL)
746     {
747       BUS_SET_OOM (error);
748       goto out;
749     }
750
751   if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
752     {
753       BUS_SET_OOM (error);
754       goto out;
755     }
756
757   if (!bus_transaction_send_from_driver (transaction, connection, reply))
758     {
759       BUS_SET_OOM (error);
760       goto out;
761     }
762
763   retval = TRUE;
764
765  out:
766   if (reply)
767     dbus_message_unref (reply);
768   return retval;
769 }
770
771 static dbus_bool_t
772 bus_driver_handle_service_exists (DBusConnection *connection,
773                                   BusTransaction *transaction,
774                                   DBusMessage    *message,
775                                   DBusError      *error)
776 {
777   DBusMessage *reply;
778   DBusString service_name;
779   BusService *service;
780   dbus_bool_t service_exists;
781   const char *name;
782   dbus_bool_t retval;
783   BusRegistry *registry;
784
785   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
786
787   registry = bus_connection_get_registry (connection);
788
789   if (!dbus_message_get_args (message, error,
790                               DBUS_TYPE_STRING, &name,
791                               DBUS_TYPE_INVALID))
792     return FALSE;
793
794   retval = FALSE;
795
796   if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
797     {
798       service_exists = TRUE;
799     }
800   else
801     {
802           if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
803           {
804                   int inter_ret;
805                   struct nameInfo info;
806
807                   inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
808                         if((inter_ret == 0) || (inter_ret == -ENOENT))
809                                 service_exists = (inter_ret == 0) ? TRUE : FALSE;
810                         else
811                         {
812                                 _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
813                                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name);
814                                 service_exists = FALSE;
815                         }
816           }
817           else
818           {
819               _dbus_string_init_const (&service_name, name);
820               service = bus_registry_lookup (registry, &service_name);
821               service_exists = service != NULL;
822           }
823     }
824
825   reply = dbus_message_new_method_return (message);
826   if (reply == NULL)
827     {
828       BUS_SET_OOM (error);
829       goto out;
830     }
831
832   if (!dbus_message_append_args (reply,
833                                  DBUS_TYPE_BOOLEAN, &service_exists,
834                                  0))
835     {
836       BUS_SET_OOM (error);
837       goto out;
838     }
839
840   if (!bus_transaction_send_from_driver (transaction, connection, reply))
841     {
842       BUS_SET_OOM (error);
843       goto out;
844     }
845
846   retval = TRUE;
847
848  out:
849   if (reply)
850     dbus_message_unref (reply);
851
852   return retval;
853 }
854
855 static dbus_bool_t
856 bus_driver_handle_activate_service (DBusConnection *connection,
857                                     BusTransaction *transaction,
858                                     DBusMessage    *message,
859                                     DBusError      *error)
860 {
861   dbus_uint32_t flags;
862   const char *name;
863   dbus_bool_t retval;
864   BusActivation *activation;
865
866   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
867
868   activation = bus_connection_get_activation (connection);
869
870   if (!dbus_message_get_args (message, error,
871                               DBUS_TYPE_STRING, &name,
872                               DBUS_TYPE_UINT32, &flags,
873                               DBUS_TYPE_INVALID))
874     {
875       _DBUS_ASSERT_ERROR_IS_SET (error);
876       _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
877       return FALSE;
878     }
879
880   retval = FALSE;
881
882   if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
883                                         message, name, error))
884     {
885       _DBUS_ASSERT_ERROR_IS_SET (error);
886       _dbus_verbose ("bus_activation_activate_service() failed\n");
887       goto out;
888     }
889
890   retval = TRUE;
891
892  out:
893   return retval;
894 }
895
896 static dbus_bool_t
897 send_ack_reply (DBusConnection *connection,
898                 BusTransaction *transaction,
899                 DBusMessage    *message,
900                 DBusError      *error)
901 {
902   DBusMessage *reply;
903
904   if (dbus_message_get_no_reply (message))
905     return TRUE;
906
907   reply = dbus_message_new_method_return (message);
908   if (reply == NULL)
909     {
910       BUS_SET_OOM (error);
911       return FALSE;
912     }
913
914   if (!bus_transaction_send_from_driver (transaction, connection, reply))
915     {
916       BUS_SET_OOM (error);
917       dbus_message_unref (reply);
918       return FALSE;
919     }
920
921   dbus_message_unref (reply);
922
923   return TRUE;
924 }
925
926 static dbus_bool_t
927 bus_driver_handle_update_activation_environment (DBusConnection *connection,
928                                                  BusTransaction *transaction,
929                                                  DBusMessage    *message,
930                                                  DBusError      *error)
931 {
932   dbus_bool_t retval;
933   BusActivation *activation;
934   DBusMessageIter iter;
935   DBusMessageIter dict_iter;
936   DBusMessageIter dict_entry_iter;
937   int array_type;
938   int key_type;
939   DBusList *keys, *key_link;
940   DBusList *values, *value_link;
941
942   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
943
944   activation = bus_connection_get_activation (connection);
945
946   dbus_message_iter_init (message, &iter);
947
948   /* The message signature has already been checked for us,
949    * so let's just assert it's right.
950    */
951   _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
952
953   dbus_message_iter_recurse (&iter, &dict_iter);
954
955   retval = FALSE;
956
957   /* Then loop through the sent dictionary, add the location of
958    * the environment keys and values to lists. The result will
959    * be in reverse order, so we don't have to constantly search
960    * for the end of the list in a loop.
961    */
962   keys = NULL;
963   values = NULL;
964   while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
965     {
966       dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
967
968       while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
969         {
970           char *key;
971           char *value;
972           int value_type;
973
974           dbus_message_iter_get_basic (&dict_entry_iter, &key);
975           dbus_message_iter_next (&dict_entry_iter);
976
977           value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
978
979           if (value_type != DBUS_TYPE_STRING)
980             break;
981
982           dbus_message_iter_get_basic (&dict_entry_iter, &value);
983
984           if (!_dbus_list_append (&keys, key))
985             {
986               BUS_SET_OOM (error);
987               break;
988             }
989
990           if (!_dbus_list_append (&values, value))
991             {
992               BUS_SET_OOM (error);
993               break;
994             }
995
996           dbus_message_iter_next (&dict_entry_iter);
997         }
998
999       if (key_type != DBUS_TYPE_INVALID)
1000         break;
1001
1002       dbus_message_iter_next (&dict_iter);
1003     }
1004
1005   if (array_type != DBUS_TYPE_INVALID)
1006     goto out;
1007
1008   _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
1009
1010   key_link = keys;
1011   value_link = values;
1012   while (key_link != NULL)
1013   {
1014       const char *key;
1015       const char *value;
1016
1017       key = key_link->data;
1018       value = value_link->data;
1019
1020       if (!bus_activation_set_environment_variable (activation,
1021                                                     key, value, error))
1022       {
1023           _DBUS_ASSERT_ERROR_IS_SET (error);
1024           _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
1025           break;
1026       }
1027       key_link = _dbus_list_get_next_link (&keys, key_link);
1028       value_link = _dbus_list_get_next_link (&values, value_link);
1029   }
1030
1031   /* FIXME: We can fail early having set only some of the environment variables,
1032    * (because of OOM failure).  It's sort of hard to fix and it doesn't really
1033    * matter, so we're punting for now.
1034    */
1035   if (key_link != NULL)
1036     goto out;
1037
1038   if (!send_ack_reply (connection, transaction,
1039                        message, error))
1040     goto out;
1041
1042   retval = TRUE;
1043
1044  out:
1045   _dbus_list_clear (&keys);
1046   _dbus_list_clear (&values);
1047   return retval;
1048 }
1049
1050 static dbus_bool_t
1051 bus_driver_handle_add_match (DBusConnection *connection,
1052                              BusTransaction *transaction,
1053                              DBusMessage    *message,
1054                              DBusError      *error)
1055 {
1056   BusMatchRule *rule;
1057   const char *text;
1058   DBusString str;
1059   BusMatchmaker *matchmaker;
1060
1061   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1062
1063   text = NULL;
1064   rule = NULL;
1065
1066   if (bus_connection_get_n_match_rules (connection) >=
1067       bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
1068     {
1069       dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1070                       "Connection \"%s\" is not allowed to add more match rules "
1071                       "(increase limits in configuration file if required)",
1072                       bus_connection_is_active (connection) ?
1073                       bus_connection_get_name (connection) :
1074                       "(inactive)");
1075       goto failed;
1076     }
1077
1078   if (!dbus_message_get_args (message, error,
1079                               DBUS_TYPE_STRING, &text,
1080                               DBUS_TYPE_INVALID))
1081     {
1082       _dbus_verbose ("No memory to get arguments to AddMatch\n");
1083       goto failed;
1084     }
1085
1086   _dbus_string_init_const (&str, text);
1087
1088   rule = bus_match_rule_parse (connection, &str, error);
1089   if (rule == NULL)
1090     goto failed;
1091
1092   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1093   {
1094
1095           if (!kdbus_add_match_rule (connection, message, text, error))
1096               goto failed;
1097
1098           if (!send_ack_reply (connection, transaction,
1099                                message, error))
1100               goto failed;
1101   }
1102   else
1103   {
1104           matchmaker = bus_connection_get_matchmaker (connection);
1105
1106           if (!bus_matchmaker_add_rule (matchmaker, rule))
1107             {
1108               BUS_SET_OOM (error);
1109               goto failed;
1110             }
1111
1112           if (!send_ack_reply (connection, transaction,
1113                                message, error))
1114             {
1115               bus_matchmaker_remove_rule (matchmaker, rule);
1116               goto failed;
1117             }
1118   }
1119
1120   bus_match_rule_unref (rule);
1121
1122   return TRUE;
1123
1124  failed:
1125   _DBUS_ASSERT_ERROR_IS_SET (error);
1126   if (rule)
1127     bus_match_rule_unref (rule);
1128   return FALSE;
1129 }
1130
1131 static dbus_bool_t
1132 bus_driver_handle_remove_match (DBusConnection *connection,
1133                                 BusTransaction *transaction,
1134                                 DBusMessage    *message,
1135                                 DBusError      *error)
1136 {
1137   BusMatchRule *rule;
1138   const char *text;
1139   DBusString str;
1140   BusMatchmaker *matchmaker;
1141
1142   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1143
1144   text = NULL;
1145   rule = NULL;
1146
1147   if (!dbus_message_get_args (message, error,
1148                               DBUS_TYPE_STRING, &text,
1149                               DBUS_TYPE_INVALID))
1150     {
1151       _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1152       goto failed;
1153     }
1154
1155   _dbus_string_init_const (&str, text);
1156
1157   rule = bus_match_rule_parse (connection, &str, error);
1158   if (rule == NULL)
1159     goto failed;
1160
1161   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1162   {
1163           if(!kdbus_remove_match(connection, message, error))
1164                   goto failed;
1165   }
1166
1167   /* Send the ack before we remove the rule, since the ack is undone
1168    * on transaction cancel, but rule removal isn't.
1169    */
1170   if (!send_ack_reply (connection, transaction,
1171                        message, error))
1172     goto failed;
1173
1174   if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1175   {
1176           matchmaker = bus_connection_get_matchmaker (connection);
1177
1178           if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1179                 goto failed;
1180   }
1181
1182   bus_match_rule_unref (rule);
1183
1184   return TRUE;
1185
1186  failed:
1187   _DBUS_ASSERT_ERROR_IS_SET (error);
1188   if (rule)
1189     bus_match_rule_unref (rule);
1190   return FALSE;
1191 }
1192
1193 static dbus_bool_t
1194 bus_driver_handle_get_service_owner (DBusConnection *connection,
1195                                      BusTransaction *transaction,
1196                                      DBusMessage    *message,
1197                                      DBusError      *error)
1198 {
1199   const char *text;
1200   const char *base_name;
1201   DBusString str;
1202   BusRegistry *registry;
1203   BusService *service;
1204   DBusMessage *reply;
1205   char unique_name[(unsigned int)(snprintf((char*)base_name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
1206
1207   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1208
1209   registry = bus_connection_get_registry (connection);
1210
1211   text = NULL;
1212   reply = NULL;
1213
1214   if (! dbus_message_get_args (message, error,
1215                                DBUS_TYPE_STRING, &text,
1216                                DBUS_TYPE_INVALID))
1217       goto failed;
1218
1219   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1220   {
1221           int ret;
1222           struct nameInfo info;
1223
1224           ret = kdbus_NameQuery(text, dbus_connection_get_transport(connection), &info);
1225                 if(ret == 0) //unique id of the name
1226                 {
1227                         sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
1228                         _dbus_verbose("Unique name discovered:%s\n", unique_name);
1229                         base_name = unique_name;
1230                 }
1231                 else if(ret == -ENOENT)  //name has no owner
1232                 {
1233                           dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
1234                                                           "Could not get owner of name '%s': no such name", text);
1235                           goto failed;
1236                 }
1237                 else
1238                 {
1239                         _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
1240                         dbus_set_error (error, DBUS_ERROR_FAILED,
1241                                                           "Could not determine unique name for '%s'", text);
1242                         goto failed;
1243                 }
1244   }
1245   else
1246   {
1247           _dbus_string_init_const (&str, text);
1248           service = bus_registry_lookup (registry, &str);
1249           if (service == NULL &&
1250                   _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1251                 {
1252                   /* ORG_FREEDESKTOP_DBUS owns itself */
1253                   base_name = DBUS_SERVICE_DBUS;
1254                 }
1255           else if (service == NULL)
1256                 {
1257                   dbus_set_error (error,
1258                                                   DBUS_ERROR_NAME_HAS_NO_OWNER,
1259                                                   "Could not get owner of name '%s': no such name", text);
1260                   goto failed;
1261                 }
1262           else
1263                 {
1264                   base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1265                   if (base_name == NULL)
1266                         {
1267                           /* FIXME - how is this error possible? */
1268                           dbus_set_error (error,
1269                                                           DBUS_ERROR_FAILED,
1270                                                           "Could not determine unique name for '%s'", text);
1271                           goto failed;
1272                         }
1273                   _dbus_assert (*base_name == ':');
1274                 }
1275   }
1276   _dbus_assert (base_name != NULL);
1277
1278   reply = dbus_message_new_method_return (message);
1279   if (reply == NULL)
1280     goto oom;
1281
1282   if (! dbus_message_append_args (reply,
1283                                   DBUS_TYPE_STRING, &base_name,
1284                                   DBUS_TYPE_INVALID))
1285     goto oom;
1286
1287   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1288     goto oom;
1289
1290   dbus_message_unref (reply);
1291
1292   return TRUE;
1293
1294  oom:
1295   BUS_SET_OOM (error);
1296
1297  failed:
1298   _DBUS_ASSERT_ERROR_IS_SET (error);
1299   if (reply)
1300     dbus_message_unref (reply);
1301   return FALSE;
1302 }
1303
1304 static dbus_bool_t
1305 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1306                                       BusTransaction *transaction,
1307                                       DBusMessage    *message,
1308                                       DBusError      *error)
1309 {
1310   const char *text;
1311   DBusList *base_names;
1312   DBusList *link;
1313   DBusString str;
1314   BusRegistry *registry;
1315   BusService *service;
1316   DBusMessage *reply;
1317   DBusMessageIter iter, array_iter;
1318   char *dbus_service_name = DBUS_SERVICE_DBUS;
1319
1320   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1321
1322   registry = bus_connection_get_registry (connection);
1323
1324   base_names = NULL;
1325   text = NULL;
1326   reply = NULL;
1327
1328   if (! dbus_message_get_args (message, error,
1329                                DBUS_TYPE_STRING, &text,
1330                                DBUS_TYPE_INVALID))
1331       goto failed;
1332
1333   _dbus_string_init_const (&str, text);
1334   service = bus_registry_lookup (registry, &str);
1335   if (service == NULL &&
1336       _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1337     {
1338       /* ORG_FREEDESKTOP_DBUS owns itself */
1339       if (! _dbus_list_append (&base_names, dbus_service_name))
1340         goto oom;
1341     }
1342   else if (service == NULL)
1343     {
1344       dbus_set_error (error,
1345                       DBUS_ERROR_NAME_HAS_NO_OWNER,
1346                       "Could not get owners of name '%s': no such name", text);
1347       goto failed;
1348     }
1349   else
1350     {
1351       if (!bus_service_list_queued_owners (service,
1352                                            &base_names,
1353                                            error))
1354         goto failed;
1355     }
1356
1357   _dbus_assert (base_names != NULL);
1358
1359   reply = dbus_message_new_method_return (message);
1360   if (reply == NULL)
1361     goto oom;
1362
1363   dbus_message_iter_init_append (reply, &iter);
1364   if (!dbus_message_iter_open_container (&iter,
1365                                          DBUS_TYPE_ARRAY,
1366                                          DBUS_TYPE_STRING_AS_STRING,
1367                                          &array_iter))
1368     goto oom;
1369
1370   link = _dbus_list_get_first_link (&base_names);
1371   while (link != NULL)
1372     {
1373       char *uname;
1374
1375       _dbus_assert (link->data != NULL);
1376       uname = (char *)link->data;
1377
1378       if (!dbus_message_iter_append_basic (&array_iter,
1379                                            DBUS_TYPE_STRING,
1380                                            &uname))
1381         goto oom;
1382
1383       link = _dbus_list_get_next_link (&base_names, link);
1384     }
1385
1386   if (! dbus_message_iter_close_container (&iter, &array_iter))
1387     goto oom;
1388
1389
1390   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1391     goto oom;
1392
1393   dbus_message_unref (reply);
1394
1395   return TRUE;
1396
1397  oom:
1398   BUS_SET_OOM (error);
1399
1400  failed:
1401   _DBUS_ASSERT_ERROR_IS_SET (error);
1402   if (reply)
1403     dbus_message_unref (reply);
1404
1405   if (base_names)
1406     _dbus_list_clear (&base_names);
1407
1408   return FALSE;
1409 }
1410
1411 static dbus_bool_t
1412 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1413                                             BusTransaction *transaction,
1414                                             DBusMessage    *message,
1415                                             DBusError      *error)
1416 {
1417   DBusConnection *conn;
1418   DBusMessage *reply;
1419   unsigned long uid;
1420   dbus_uint32_t uid32;
1421   const char *service;
1422
1423   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1424
1425   reply = dbus_message_new_method_return (message);
1426   if (reply == NULL)
1427     goto oom;
1428
1429   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1430   {
1431           if(!kdbus_get_connection_unix_user(connection, message, &uid, error))
1432                   goto failed;
1433   }
1434   else
1435   {
1436           conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
1437                                      error);
1438
1439           if (conn == NULL)
1440                   goto failed;
1441
1442           if (!dbus_connection_get_unix_user (conn, &uid))
1443                 {
1444                   dbus_set_error (error,
1445                                                   DBUS_ERROR_FAILED,
1446                                                   "Could not determine UID for '%s'", service);
1447                   goto failed;
1448                 }
1449   }
1450
1451   uid32 = uid;
1452   if (! dbus_message_append_args (reply,
1453                                   DBUS_TYPE_UINT32, &uid32,
1454                                   DBUS_TYPE_INVALID))
1455     goto oom;
1456
1457   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1458     goto oom;
1459
1460   dbus_message_unref (reply);
1461
1462   return TRUE;
1463
1464  oom:
1465   BUS_SET_OOM (error);
1466
1467  failed:
1468   _DBUS_ASSERT_ERROR_IS_SET (error);
1469   if (reply)
1470     dbus_message_unref (reply);
1471   return FALSE;
1472 }
1473
1474 static dbus_bool_t
1475 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1476                                                   BusTransaction *transaction,
1477                                                   DBusMessage    *message,
1478                                                   DBusError      *error)
1479 {
1480   DBusConnection *conn;
1481   DBusMessage *reply;
1482   unsigned long pid;
1483   dbus_uint32_t pid32;
1484   const char *service;
1485
1486   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1487
1488   reply = dbus_message_new_method_return (message);
1489   if (reply == NULL)
1490     goto oom;
1491
1492   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1493   {
1494           if(!kdbus_get_connection_unix_process_id(connection, message, &pid, error))
1495                   goto failed;
1496   }
1497   else
1498   {
1499           conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
1500                                                                                  error);
1501
1502           if (conn == NULL)
1503                 goto failed;
1504
1505
1506
1507           if (!dbus_connection_get_unix_process_id (conn, &pid))
1508                 {
1509                   dbus_set_error (error,
1510                                                   DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1511                                                   "Could not determine PID for '%s'", service);
1512                   goto failed;
1513                 }
1514   }
1515
1516   pid32 = pid;
1517   if (! dbus_message_append_args (reply,
1518                                   DBUS_TYPE_UINT32, &pid32,
1519                                   DBUS_TYPE_INVALID))
1520     goto oom;
1521
1522   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1523     goto oom;
1524
1525   dbus_message_unref (reply);
1526
1527   return TRUE;
1528
1529  oom:
1530   BUS_SET_OOM (error);
1531
1532  failed:
1533   _DBUS_ASSERT_ERROR_IS_SET (error);
1534   if (reply)
1535     dbus_message_unref (reply);
1536   return FALSE;
1537 }
1538
1539 static dbus_bool_t
1540 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1541                                               BusTransaction *transaction,
1542                                               DBusMessage    *message,
1543                                               DBusError      *error)
1544 {
1545   DBusConnection *conn;
1546   DBusMessage *reply;
1547   void *data = NULL;
1548   dbus_uint32_t data_size;
1549   const char *service;
1550
1551   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1552
1553   reply = NULL;
1554
1555   conn = bus_driver_get_conn_helper (connection, message,
1556                                      "audit session data", &service, error);
1557
1558   if (conn == NULL)
1559     goto failed;
1560
1561   reply = dbus_message_new_method_return (message);
1562   if (reply == NULL)
1563     goto oom;
1564
1565   if (!dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) || data == NULL)
1566     {
1567       dbus_set_error (error,
1568                       DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1569                       "Could not determine audit session data for '%s'", service);
1570       goto failed;
1571     }
1572
1573   if (! dbus_message_append_args (reply,
1574                                   DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1575                                   DBUS_TYPE_INVALID))
1576     goto oom;
1577
1578   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1579     goto oom;
1580
1581   dbus_message_unref (reply);
1582
1583   return TRUE;
1584
1585  oom:
1586   BUS_SET_OOM (error);
1587
1588  failed:
1589   _DBUS_ASSERT_ERROR_IS_SET (error);
1590   if (reply)
1591     dbus_message_unref (reply);
1592   return FALSE;
1593 }
1594
1595 static dbus_bool_t
1596 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1597                                                            BusTransaction *transaction,
1598                                                            DBusMessage    *message,
1599                                                            DBusError      *error)
1600 {
1601   DBusConnection *conn;
1602   DBusMessage *reply;
1603   BusSELinuxID *context;
1604   const char *service;
1605
1606   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1607
1608   reply = dbus_message_new_method_return (message);
1609   if (reply == NULL)
1610     goto oom;
1611
1612   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1613   {
1614           if(!kdbus_get_connection_unix_selinux_security_context(connection, message, reply, error))
1615                   goto failed;
1616   }
1617   else
1618   {
1619           conn = bus_driver_get_conn_helper (connection, message, "security context",
1620                                                                                  &service, error);
1621
1622           if (conn == NULL)
1623                 goto failed;
1624
1625           context = bus_connection_get_selinux_id (conn);
1626           if (!context)
1627                 {
1628                   dbus_set_error (error,
1629                                                   DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1630                                                   "Could not determine security context for '%s'", service);
1631                   goto failed;
1632                 }
1633
1634           if (! bus_selinux_append_context (reply, context, error))
1635                 goto failed;
1636   }
1637
1638   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1639     goto oom;
1640
1641   dbus_message_unref (reply);
1642
1643   return TRUE;
1644
1645  oom:
1646   BUS_SET_OOM (error);
1647
1648  failed:
1649   _DBUS_ASSERT_ERROR_IS_SET (error);
1650   if (reply)
1651     dbus_message_unref (reply);
1652   return FALSE;
1653 }
1654
1655 static dbus_bool_t
1656 bus_driver_handle_get_connection_credentials (DBusConnection *connection,
1657                                               BusTransaction *transaction,
1658                                               DBusMessage    *message,
1659                                               DBusError      *error)
1660 {
1661   DBusConnection *conn;
1662   DBusMessage *reply;
1663   DBusMessageIter reply_iter;
1664   DBusMessageIter array_iter;
1665   unsigned long ulong_val;
1666   const char *service;
1667
1668   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1669
1670   reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
1671   if (reply == NULL)
1672     goto oom;
1673
1674   if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1675   {
1676           conn = bus_driver_get_conn_helper (connection, message, "credentials",
1677                                                                                  &service, error);
1678
1679           if (conn == NULL)
1680                 goto failed;
1681
1682           /* we can't represent > 32-bit pids; if your system needs them, please
1683            * add ProcessID64 to the spec or something */
1684           if (dbus_connection_get_unix_process_id (conn, &ulong_val) &&
1685                   ulong_val <= _DBUS_UINT32_MAX)
1686                 {
1687                   if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1688                         goto oom;
1689                 }
1690
1691           /* we can't represent > 32-bit uids; if your system needs them, please
1692            * add UnixUserID64 to the spec or something */
1693           if (dbus_connection_get_unix_user (conn, &ulong_val) &&
1694                   ulong_val <= _DBUS_UINT32_MAX)
1695                 {
1696                   if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1697                         goto oom;
1698                 }
1699   }
1700   else
1701   {
1702           if(kdbus_get_connection_unix_process_id(connection, message, &ulong_val, error))
1703           {
1704                   if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1705                         goto oom;
1706           }
1707           else
1708                   goto failed;
1709
1710           if(kdbus_get_connection_unix_user(connection, message, &ulong_val, error))
1711           {
1712                   if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1713                         goto oom;
1714           }
1715           else
1716                   goto failed;
1717   }
1718
1719   if (!_dbus_asv_close (&reply_iter, &array_iter))
1720     goto oom;
1721
1722   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1723     {
1724       /* this time we don't want to close the iterator again, so just
1725        * get rid of the message */
1726       dbus_message_unref (reply);
1727       reply = NULL;
1728       goto oom;
1729     }
1730
1731   return TRUE;
1732
1733  oom:
1734   BUS_SET_OOM (error);
1735
1736  failed:
1737   _DBUS_ASSERT_ERROR_IS_SET (error);
1738
1739   if (reply)
1740     {
1741       _dbus_asv_abandon (&reply_iter, &array_iter);
1742       dbus_message_unref (reply);
1743     }
1744
1745   return FALSE;
1746 }
1747
1748 static dbus_bool_t
1749 bus_driver_handle_reload_config (DBusConnection *connection,
1750                                  BusTransaction *transaction,
1751                                  DBusMessage    *message,
1752                                  DBusError      *error)
1753 {
1754   BusContext *context;
1755   DBusMessage *reply;
1756
1757   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1758
1759   reply = NULL;
1760
1761   context = bus_connection_get_context (connection);
1762   if (!bus_context_reload_config (context, error))
1763     goto failed;
1764
1765   reply = dbus_message_new_method_return (message);
1766   if (reply == NULL)
1767     goto oom;
1768
1769   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1770     goto oom;
1771
1772   dbus_message_unref (reply);
1773   return TRUE;
1774
1775  oom:
1776   BUS_SET_OOM (error);
1777
1778  failed:
1779   _DBUS_ASSERT_ERROR_IS_SET (error);
1780   if (reply)
1781     dbus_message_unref (reply);
1782   return FALSE;
1783 }
1784
1785 static dbus_bool_t
1786 bus_driver_handle_get_id (DBusConnection *connection,
1787                           BusTransaction *transaction,
1788                           DBusMessage    *message,
1789                           DBusError      *error)
1790 {
1791   BusContext *context;
1792   DBusMessage *reply;
1793   DBusString uuid;
1794   const char *v_STRING;
1795
1796   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1797
1798   if (!_dbus_string_init (&uuid))
1799     {
1800       BUS_SET_OOM (error);
1801       return FALSE;
1802     }
1803
1804   reply = NULL;
1805
1806   context = bus_connection_get_context (connection);
1807   if (!bus_context_get_id (context, &uuid))
1808     goto oom;
1809
1810   reply = dbus_message_new_method_return (message);
1811   if (reply == NULL)
1812     goto oom;
1813
1814   v_STRING = _dbus_string_get_const_data (&uuid);
1815   if (!dbus_message_append_args (reply,
1816                                  DBUS_TYPE_STRING, &v_STRING,
1817                                  DBUS_TYPE_INVALID))
1818     goto oom;
1819
1820   _dbus_assert (dbus_message_has_signature (reply, "s"));
1821
1822   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1823     goto oom;
1824
1825   _dbus_string_free (&uuid);
1826   dbus_message_unref (reply);
1827   return TRUE;
1828
1829  oom:
1830   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1831
1832   BUS_SET_OOM (error);
1833
1834   if (reply)
1835     dbus_message_unref (reply);
1836   _dbus_string_free (&uuid);
1837   return FALSE;
1838 }
1839
1840 typedef struct
1841 {
1842   const char *name;
1843   const char *in_args;
1844   const char *out_args;
1845   dbus_bool_t (* handler) (DBusConnection *connection,
1846                            BusTransaction *transaction,
1847                            DBusMessage    *message,
1848                            DBusError      *error);
1849 } MessageHandler;
1850
1851 /* For speed it might be useful to sort this in order of
1852  * frequency of use (but doesn't matter with only a few items
1853  * anyhow)
1854  */
1855 static const MessageHandler dbus_message_handlers[] = {
1856   { "Hello",
1857     "",
1858     DBUS_TYPE_STRING_AS_STRING,
1859     bus_driver_handle_hello },
1860   { "RequestName",
1861     DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1862     DBUS_TYPE_UINT32_AS_STRING,
1863     bus_driver_handle_acquire_service },
1864   { "ReleaseName",
1865     DBUS_TYPE_STRING_AS_STRING,
1866     DBUS_TYPE_UINT32_AS_STRING,
1867     bus_driver_handle_release_service },
1868   { "StartServiceByName",
1869     DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1870     DBUS_TYPE_UINT32_AS_STRING,
1871     bus_driver_handle_activate_service },
1872   { "UpdateActivationEnvironment",
1873     DBUS_TYPE_ARRAY_AS_STRING DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1874     "",
1875     bus_driver_handle_update_activation_environment },
1876   { "NameHasOwner",
1877     DBUS_TYPE_STRING_AS_STRING,
1878     DBUS_TYPE_BOOLEAN_AS_STRING,
1879     bus_driver_handle_service_exists },
1880   { "ListNames",
1881     "",
1882     DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1883     bus_driver_handle_list_services },
1884   { "ListActivatableNames",
1885     "",
1886     DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1887     bus_driver_handle_list_activatable_services },
1888   { "AddMatch",
1889     DBUS_TYPE_STRING_AS_STRING,
1890     "",
1891     bus_driver_handle_add_match },
1892   { "RemoveMatch",
1893     DBUS_TYPE_STRING_AS_STRING,
1894     "",
1895     bus_driver_handle_remove_match },
1896   { "GetNameOwner",
1897     DBUS_TYPE_STRING_AS_STRING,
1898     DBUS_TYPE_STRING_AS_STRING,
1899     bus_driver_handle_get_service_owner },
1900   { "ListQueuedOwners",
1901     DBUS_TYPE_STRING_AS_STRING,
1902     DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1903     bus_driver_handle_list_queued_owners },
1904   { "GetConnectionUnixUser",
1905     DBUS_TYPE_STRING_AS_STRING,
1906     DBUS_TYPE_UINT32_AS_STRING,
1907     bus_driver_handle_get_connection_unix_user },
1908   { "GetConnectionUnixProcessID",
1909     DBUS_TYPE_STRING_AS_STRING,
1910     DBUS_TYPE_UINT32_AS_STRING,
1911     bus_driver_handle_get_connection_unix_process_id },
1912   { "GetAdtAuditSessionData",
1913     DBUS_TYPE_STRING_AS_STRING,
1914     DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1915     bus_driver_handle_get_adt_audit_session_data },
1916   { "GetConnectionSELinuxSecurityContext",
1917     DBUS_TYPE_STRING_AS_STRING,
1918     DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1919     bus_driver_handle_get_connection_selinux_security_context },
1920   { "ReloadConfig",
1921     "",
1922     "",
1923     bus_driver_handle_reload_config },
1924   { "GetId",
1925     "",
1926     DBUS_TYPE_STRING_AS_STRING,
1927     bus_driver_handle_get_id },
1928   { "GetConnectionCredentials", "s", "a{sv}",
1929     bus_driver_handle_get_connection_credentials },
1930   { NULL, NULL, NULL, NULL }
1931 };
1932
1933 static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
1934     BusTransaction *, DBusMessage *, DBusError *);
1935
1936 static const MessageHandler introspectable_message_handlers[] = {
1937   { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
1938   { NULL, NULL, NULL, NULL }
1939 };
1940
1941 #ifdef DBUS_ENABLE_STATS
1942 static const MessageHandler stats_message_handlers[] = {
1943   { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
1944   { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
1945   { NULL, NULL, NULL, NULL }
1946 };
1947 #endif
1948
1949 typedef struct {
1950   const char *name;
1951   const MessageHandler *message_handlers;
1952   const char *extra_introspection;
1953 } InterfaceHandler;
1954
1955 /* These should ideally be sorted by frequency of use, although it
1956  * probably doesn't matter with this few items */
1957 static InterfaceHandler interface_handlers[] = {
1958   { DBUS_INTERFACE_DBUS, dbus_message_handlers,
1959     "    <signal name=\"NameOwnerChanged\">\n"
1960     "      <arg type=\"s\"/>\n"
1961     "      <arg type=\"s\"/>\n"
1962     "      <arg type=\"s\"/>\n"
1963     "    </signal>\n"
1964     "    <signal name=\"NameLost\">\n"
1965     "      <arg type=\"s\"/>\n"
1966     "    </signal>\n"
1967     "    <signal name=\"NameAcquired\">\n"
1968     "      <arg type=\"s\"/>\n"
1969     "    </signal>\n" },
1970   { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
1971 #ifdef DBUS_ENABLE_STATS
1972   { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
1973 #endif
1974   { NULL, NULL, NULL }
1975 };
1976
1977 static dbus_bool_t
1978 write_args_for_direction (DBusString *xml,
1979                           const char *signature,
1980                           dbus_bool_t in)
1981 {
1982   DBusTypeReader typereader;
1983   DBusString sigstr;
1984   int current_type;
1985
1986   _dbus_string_init_const (&sigstr, signature);
1987   _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1988
1989   while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1990     {
1991       const DBusString *subsig;
1992       int start, len;
1993
1994       _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1995       if (!_dbus_string_append_printf (xml, "      <arg direction=\"%s\" type=\"",
1996                                        in ? "in" : "out"))
1997         goto oom;
1998       if (!_dbus_string_append_len (xml,
1999                                     _dbus_string_get_const_data (subsig) + start,
2000                                     len))
2001         goto oom;
2002       if (!_dbus_string_append (xml, "\"/>\n"))
2003         goto oom;
2004
2005       _dbus_type_reader_next (&typereader);
2006     }
2007   return TRUE;
2008  oom:
2009   return FALSE;
2010 }
2011
2012 dbus_bool_t
2013 bus_driver_generate_introspect_string (DBusString *xml)
2014 {
2015   const InterfaceHandler *ih;
2016   const MessageHandler *mh;
2017
2018   if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
2019     return FALSE;
2020   if (!_dbus_string_append (xml, "<node>\n"))
2021     return FALSE;
2022
2023   for (ih = interface_handlers; ih->name != NULL; ih++)
2024     {
2025       if (!_dbus_string_append_printf (xml, "  <interface name=\"%s\">\n",
2026                                        ih->name))
2027         return FALSE;
2028
2029       for (mh = ih->message_handlers; mh->name != NULL; mh++)
2030         {
2031           if (!_dbus_string_append_printf (xml, "    <method name=\"%s\">\n",
2032                                            mh->name))
2033             return FALSE;
2034
2035           if (!write_args_for_direction (xml, mh->in_args, TRUE))
2036             return FALSE;
2037
2038           if (!write_args_for_direction (xml, mh->out_args, FALSE))
2039             return FALSE;
2040
2041           if (!_dbus_string_append (xml, "    </method>\n"))
2042             return FALSE;
2043         }
2044
2045       if (ih->extra_introspection != NULL &&
2046           !_dbus_string_append (xml, ih->extra_introspection))
2047         return FALSE;
2048
2049       if (!_dbus_string_append (xml, "  </interface>\n"))
2050         return FALSE;
2051     }
2052
2053   if (!_dbus_string_append (xml, "</node>\n"))
2054     return FALSE;
2055
2056   return TRUE;
2057 }
2058
2059 static dbus_bool_t
2060 bus_driver_handle_introspect (DBusConnection *connection,
2061                               BusTransaction *transaction,
2062                               DBusMessage    *message,
2063                               DBusError      *error)
2064 {
2065   DBusString xml;
2066   DBusMessage *reply;
2067   const char *v_STRING;
2068
2069   _dbus_verbose ("Introspect() on bus driver\n");
2070
2071   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2072
2073   reply = NULL;
2074
2075   if (! dbus_message_get_args (message, error,
2076                                DBUS_TYPE_INVALID))
2077     {
2078       _DBUS_ASSERT_ERROR_IS_SET (error);
2079       return FALSE;
2080     }
2081
2082   if (!_dbus_string_init (&xml))
2083     {
2084       BUS_SET_OOM (error);
2085       return FALSE;
2086     }
2087
2088   if (!bus_driver_generate_introspect_string (&xml))
2089     goto oom;
2090
2091   v_STRING = _dbus_string_get_const_data (&xml);
2092
2093   reply = dbus_message_new_method_return (message);
2094   if (reply == NULL)
2095     goto oom;
2096
2097   if (! dbus_message_append_args (reply,
2098                                   DBUS_TYPE_STRING, &v_STRING,
2099                                   DBUS_TYPE_INVALID))
2100     goto oom;
2101
2102   if (! bus_transaction_send_from_driver (transaction, connection, reply))
2103     goto oom;
2104
2105   dbus_message_unref (reply);
2106   _dbus_string_free (&xml);
2107
2108   return TRUE;
2109
2110  oom:
2111   BUS_SET_OOM (error);
2112
2113   if (reply)
2114     dbus_message_unref (reply);
2115
2116   _dbus_string_free (&xml);
2117
2118   return FALSE;
2119 }
2120
2121 dbus_bool_t
2122 bus_driver_handle_message (DBusConnection *connection,
2123                            BusTransaction *transaction,
2124                            DBusMessage    *message,
2125                            DBusError      *error)
2126 {
2127   const char *name, *interface;
2128   const InterfaceHandler *ih;
2129   const MessageHandler *mh;
2130   dbus_bool_t found_interface = FALSE;
2131
2132   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2133
2134   if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
2135     {
2136       BusContext *context;
2137
2138       context = bus_connection_get_context (connection);
2139       return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
2140     }
2141
2142   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
2143     {
2144       _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
2145       return TRUE; /* we just ignore this */
2146     }
2147
2148   /* may be NULL, which means "any interface will do" */
2149   interface = dbus_message_get_interface (message);
2150
2151   _dbus_assert (dbus_message_get_member (message) != NULL);
2152
2153   name = dbus_message_get_member (message);
2154
2155   _dbus_verbose ("Driver got a method call: %s\n", name);
2156
2157   /* security checks should have kept this from getting here */
2158   _dbus_assert (dbus_message_get_sender (message) != NULL ||
2159                 strcmp (name, "Hello") == 0);
2160
2161   for (ih = interface_handlers; ih->name != NULL; ih++)
2162     {
2163       if (interface != NULL && strcmp (interface, ih->name) != 0)
2164         continue;
2165
2166       found_interface = TRUE;
2167
2168       for (mh = ih->message_handlers; mh->name != NULL; mh++)
2169         {
2170           if (strcmp (mh->name, name) != 0)
2171             continue;
2172
2173           _dbus_verbose ("Found driver handler for %s\n", name);
2174
2175           if (!dbus_message_has_signature (message, mh->in_args))
2176             {
2177               _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2178               _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
2179                              name, dbus_message_get_signature (message),
2180                              mh->in_args);
2181
2182               dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2183                               "Call to %s has wrong args (%s, expected %s)\n",
2184                               name, dbus_message_get_signature (message),
2185                               mh->in_args);
2186               _DBUS_ASSERT_ERROR_IS_SET (error);
2187               return FALSE;
2188             }
2189
2190           if ((* mh->handler) (connection, transaction, message, error))
2191             {
2192               _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2193               _dbus_verbose ("Driver handler succeeded\n");
2194               return TRUE;
2195             }
2196           else
2197             {
2198               _DBUS_ASSERT_ERROR_IS_SET (error);
2199               _dbus_verbose ("Driver handler returned failure\n");
2200               return FALSE;
2201             }
2202         }
2203     }
2204
2205   _dbus_verbose ("No driver handler for message \"%s\"\n",
2206                  name);
2207
2208   dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2209                   "%s does not understand message %s",
2210                   DBUS_SERVICE_DBUS, name);
2211
2212   return FALSE;
2213 }
2214
2215 void
2216 bus_driver_remove_connection (DBusConnection *connection)
2217 {
2218   /* FIXME 1.0 Does nothing for now, should unregister the connection
2219    * with the bus driver.
2220    */
2221 }