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