[daemon-dev][daemon-fix] starting services by direct message (autostart) and some...
[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)))
735   {
736           registry = (BusRegistry*) dbus_message_get_sender(message);
737           /* todo looks like hack? Yes.
738            * But how to pass sender of message to bus_registry_release_service in other way?
739            */
740   }
741
742   if (!bus_registry_release_service (registry, connection,
743                                      &service_name, &service_reply,
744                                      transaction, error))
745     goto out;
746
747   reply = dbus_message_new_method_return (message);
748   if (reply == NULL)
749     {
750       BUS_SET_OOM (error);
751       goto out;
752     }
753
754   if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
755     {
756       BUS_SET_OOM (error);
757       goto out;
758     }
759
760   if (!bus_transaction_send_from_driver (transaction, connection, reply))
761     {
762       BUS_SET_OOM (error);
763       goto out;
764     }
765
766   retval = TRUE;
767
768  out:
769   if (reply)
770     dbus_message_unref (reply);
771   return retval;
772 }
773
774 static dbus_bool_t
775 bus_driver_handle_service_exists (DBusConnection *connection,
776                                   BusTransaction *transaction,
777                                   DBusMessage    *message,
778                                   DBusError      *error)
779 {
780   DBusMessage *reply;
781   DBusString service_name;
782   BusService *service;
783   dbus_bool_t service_exists;
784   const char *name;
785   dbus_bool_t retval;
786   BusRegistry *registry;
787
788   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
789
790   registry = bus_connection_get_registry (connection);
791
792   if (!dbus_message_get_args (message, error,
793                               DBUS_TYPE_STRING, &name,
794                               DBUS_TYPE_INVALID))
795     return FALSE;
796
797   retval = FALSE;
798
799   if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
800     {
801       service_exists = TRUE;
802     }
803   else
804     {
805           if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
806           {
807                   int inter_ret;
808                   struct nameInfo info;
809
810                   inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
811                         if((inter_ret == 0) || (inter_ret == -ENOENT))
812                                 service_exists = (inter_ret == 0) ? TRUE : FALSE;
813                         else
814                         {
815                                 _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
816                                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name);
817                                 service_exists = FALSE;
818                         }
819           }
820           else
821           {
822               _dbus_string_init_const (&service_name, name);
823               service = bus_registry_lookup (registry, &service_name);
824               service_exists = service != NULL;
825           }
826     }
827
828   reply = dbus_message_new_method_return (message);
829   if (reply == NULL)
830     {
831       BUS_SET_OOM (error);
832       goto out;
833     }
834
835   if (!dbus_message_append_args (reply,
836                                  DBUS_TYPE_BOOLEAN, &service_exists,
837                                  0))
838     {
839       BUS_SET_OOM (error);
840       goto out;
841     }
842
843   if (!bus_transaction_send_from_driver (transaction, connection, reply))
844     {
845       BUS_SET_OOM (error);
846       goto out;
847     }
848
849   retval = TRUE;
850
851  out:
852   if (reply)
853     dbus_message_unref (reply);
854
855   return retval;
856 }
857
858 static dbus_bool_t
859 bus_driver_handle_activate_service (DBusConnection *connection,
860                                     BusTransaction *transaction,
861                                     DBusMessage    *message,
862                                     DBusError      *error)
863 {
864   dbus_uint32_t flags;
865   const char *name;
866   dbus_bool_t retval;
867   BusActivation *activation;
868
869   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
870
871   activation = bus_connection_get_activation (connection);
872
873   if (!dbus_message_get_args (message, error,
874                               DBUS_TYPE_STRING, &name,
875                               DBUS_TYPE_UINT32, &flags,
876                               DBUS_TYPE_INVALID))
877     {
878       _DBUS_ASSERT_ERROR_IS_SET (error);
879       _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
880       return FALSE;
881     }
882
883   retval = FALSE;
884
885   if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
886                                         message, name, error))
887     {
888       _DBUS_ASSERT_ERROR_IS_SET (error);
889       _dbus_verbose ("bus_activation_activate_service() failed\n");
890       goto out;
891     }
892
893   retval = TRUE;
894
895  out:
896   return retval;
897 }
898
899 static dbus_bool_t
900 send_ack_reply (DBusConnection *connection,
901                 BusTransaction *transaction,
902                 DBusMessage    *message,
903                 DBusError      *error)
904 {
905   DBusMessage *reply;
906
907   if (dbus_message_get_no_reply (message))
908     return TRUE;
909
910   reply = dbus_message_new_method_return (message);
911   if (reply == NULL)
912     {
913       BUS_SET_OOM (error);
914       return FALSE;
915     }
916
917   if (!bus_transaction_send_from_driver (transaction, connection, reply))
918     {
919       BUS_SET_OOM (error);
920       dbus_message_unref (reply);
921       return FALSE;
922     }
923
924   dbus_message_unref (reply);
925
926   return TRUE;
927 }
928
929 static dbus_bool_t
930 bus_driver_handle_update_activation_environment (DBusConnection *connection,
931                                                  BusTransaction *transaction,
932                                                  DBusMessage    *message,
933                                                  DBusError      *error)
934 {
935   dbus_bool_t retval;
936   BusActivation *activation;
937   DBusMessageIter iter;
938   DBusMessageIter dict_iter;
939   DBusMessageIter dict_entry_iter;
940   int array_type;
941   int key_type;
942   DBusList *keys, *key_link;
943   DBusList *values, *value_link;
944
945   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
946
947   activation = bus_connection_get_activation (connection);
948
949   dbus_message_iter_init (message, &iter);
950
951   /* The message signature has already been checked for us,
952    * so let's just assert it's right.
953    */
954   _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
955
956   dbus_message_iter_recurse (&iter, &dict_iter);
957
958   retval = FALSE;
959
960   /* Then loop through the sent dictionary, add the location of
961    * the environment keys and values to lists. The result will
962    * be in reverse order, so we don't have to constantly search
963    * for the end of the list in a loop.
964    */
965   keys = NULL;
966   values = NULL;
967   while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
968     {
969       dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
970
971       while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
972         {
973           char *key;
974           char *value;
975           int value_type;
976
977           dbus_message_iter_get_basic (&dict_entry_iter, &key);
978           dbus_message_iter_next (&dict_entry_iter);
979
980           value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
981
982           if (value_type != DBUS_TYPE_STRING)
983             break;
984
985           dbus_message_iter_get_basic (&dict_entry_iter, &value);
986
987           if (!_dbus_list_append (&keys, key))
988             {
989               BUS_SET_OOM (error);
990               break;
991             }
992
993           if (!_dbus_list_append (&values, value))
994             {
995               BUS_SET_OOM (error);
996               break;
997             }
998
999           dbus_message_iter_next (&dict_entry_iter);
1000         }
1001
1002       if (key_type != DBUS_TYPE_INVALID)
1003         break;
1004
1005       dbus_message_iter_next (&dict_iter);
1006     }
1007
1008   if (array_type != DBUS_TYPE_INVALID)
1009     goto out;
1010
1011   _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
1012
1013   key_link = keys;
1014   value_link = values;
1015   while (key_link != NULL)
1016   {
1017       const char *key;
1018       const char *value;
1019
1020       key = key_link->data;
1021       value = value_link->data;
1022
1023       if (!bus_activation_set_environment_variable (activation,
1024                                                     key, value, error))
1025       {
1026           _DBUS_ASSERT_ERROR_IS_SET (error);
1027           _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
1028           break;
1029       }
1030       key_link = _dbus_list_get_next_link (&keys, key_link);
1031       value_link = _dbus_list_get_next_link (&values, value_link);
1032   }
1033
1034   /* FIXME: We can fail early having set only some of the environment variables,
1035    * (because of OOM failure).  It's sort of hard to fix and it doesn't really
1036    * matter, so we're punting for now.
1037    */
1038   if (key_link != NULL)
1039     goto out;
1040
1041   if (!send_ack_reply (connection, transaction,
1042                        message, error))
1043     goto out;
1044
1045   retval = TRUE;
1046
1047  out:
1048   _dbus_list_clear (&keys);
1049   _dbus_list_clear (&values);
1050   return retval;
1051 }
1052
1053 static dbus_bool_t
1054 bus_driver_handle_add_match (DBusConnection *connection,
1055                              BusTransaction *transaction,
1056                              DBusMessage    *message,
1057                              DBusError      *error)
1058 {
1059   BusMatchRule *rule;
1060   const char *text;
1061   DBusString str;
1062   BusMatchmaker *matchmaker;
1063
1064   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1065
1066   text = NULL;
1067   rule = NULL;
1068
1069   if (bus_connection_get_n_match_rules (connection) >=
1070       bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
1071     {
1072       dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1073                       "Connection \"%s\" is not allowed to add more match rules "
1074                       "(increase limits in configuration file if required)",
1075                       bus_connection_is_active (connection) ?
1076                       bus_connection_get_name (connection) :
1077                       "(inactive)");
1078       goto failed;
1079     }
1080
1081   if (!dbus_message_get_args (message, error,
1082                               DBUS_TYPE_STRING, &text,
1083                               DBUS_TYPE_INVALID))
1084     {
1085       _dbus_verbose ("No memory to get arguments to AddMatch\n");
1086       goto failed;
1087     }
1088
1089   _dbus_string_init_const (&str, text);
1090
1091   rule = bus_match_rule_parse (connection, &str, error);
1092   if (rule == NULL)
1093     goto failed;
1094
1095   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
1096   {
1097
1098           if (!kdbus_add_match_rule (connection, message, text, error))
1099               goto failed;
1100
1101           if (!send_ack_reply (connection, transaction,
1102                                message, error))
1103               goto failed;
1104   }
1105   else
1106   {
1107           matchmaker = bus_connection_get_matchmaker (connection);
1108
1109           if (!bus_matchmaker_add_rule (matchmaker, rule))
1110             {
1111               BUS_SET_OOM (error);
1112               goto failed;
1113             }
1114
1115           if (!send_ack_reply (connection, transaction,
1116                                message, error))
1117             {
1118               bus_matchmaker_remove_rule (matchmaker, rule);
1119               goto failed;
1120             }
1121   }
1122
1123   bus_match_rule_unref (rule);
1124
1125   return TRUE;
1126
1127  failed:
1128   _DBUS_ASSERT_ERROR_IS_SET (error);
1129   if (rule)
1130     bus_match_rule_unref (rule);
1131   return FALSE;
1132 }
1133
1134 static dbus_bool_t
1135 bus_driver_handle_remove_match (DBusConnection *connection,
1136                                 BusTransaction *transaction,
1137                                 DBusMessage    *message,
1138                                 DBusError      *error)
1139 {
1140   BusMatchRule *rule;
1141   const char *text;
1142   DBusString str;
1143   BusMatchmaker *matchmaker;
1144
1145   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1146
1147   text = NULL;
1148   rule = NULL;
1149
1150   if (!dbus_message_get_args (message, error,
1151                               DBUS_TYPE_STRING, &text,
1152                               DBUS_TYPE_INVALID))
1153     {
1154       _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1155       goto failed;
1156     }
1157
1158   _dbus_string_init_const (&str, text);
1159
1160   rule = bus_match_rule_parse (connection, &str, error);
1161   if (rule == NULL)
1162     goto failed;
1163
1164   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
1165   {
1166           if(!kdbus_remove_match(connection, message, error))
1167                   goto failed;
1168   }
1169
1170   /* Send the ack before we remove the rule, since the ack is undone
1171    * on transaction cancel, but rule removal isn't.
1172    */
1173   if (!send_ack_reply (connection, transaction,
1174                        message, error))
1175     goto failed;
1176
1177   if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
1178   {
1179           matchmaker = bus_connection_get_matchmaker (connection);
1180
1181           if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1182                 goto failed;
1183   }
1184
1185   bus_match_rule_unref (rule);
1186
1187   return TRUE;
1188
1189  failed:
1190   _DBUS_ASSERT_ERROR_IS_SET (error);
1191   if (rule)
1192     bus_match_rule_unref (rule);
1193   return FALSE;
1194 }
1195
1196 static dbus_bool_t
1197 bus_driver_handle_get_service_owner (DBusConnection *connection,
1198                                      BusTransaction *transaction,
1199                                      DBusMessage    *message,
1200                                      DBusError      *error)
1201 {
1202   const char *text;
1203   const char *base_name;
1204   DBusString str;
1205   BusRegistry *registry;
1206   BusService *service;
1207   DBusMessage *reply;
1208   char unique_name[(unsigned int)(snprintf((char*)base_name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
1209
1210   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1211
1212   registry = bus_connection_get_registry (connection);
1213
1214   text = NULL;
1215   reply = NULL;
1216
1217   if (! dbus_message_get_args (message, error,
1218                                DBUS_TYPE_STRING, &text,
1219                                DBUS_TYPE_INVALID))
1220       goto failed;
1221
1222   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
1223   {
1224           int ret;
1225           struct nameInfo info;
1226
1227           ret = kdbus_NameQuery(text, dbus_connection_get_transport(connection), &info);
1228                 if(ret == 0) //unique id of the name
1229                 {
1230                         sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
1231                         _dbus_verbose("Unique name discovered:%s\n", unique_name);
1232                         base_name = unique_name;
1233                 }
1234                 else if(ret == -ENOENT)  //name has no owner
1235                 {
1236                           dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
1237                                                           "Could not get owner of name '%s': no such name", text);
1238                           goto failed;
1239                 }
1240                 else
1241                 {
1242                         _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
1243                         dbus_set_error (error, DBUS_ERROR_FAILED,
1244                                                           "Could not determine unique name for '%s'", text);
1245                         goto failed;
1246                 }
1247   }
1248   else
1249   {
1250           _dbus_string_init_const (&str, text);
1251           service = bus_registry_lookup (registry, &str);
1252           if (service == NULL &&
1253                   _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1254                 {
1255                   /* ORG_FREEDESKTOP_DBUS owns itself */
1256                   base_name = DBUS_SERVICE_DBUS;
1257                 }
1258           else if (service == NULL)
1259                 {
1260                   dbus_set_error (error,
1261                                                   DBUS_ERROR_NAME_HAS_NO_OWNER,
1262                                                   "Could not get owner of name '%s': no such name", text);
1263                   goto failed;
1264                 }
1265           else
1266                 {
1267                   base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1268                   if (base_name == NULL)
1269                         {
1270                           /* FIXME - how is this error possible? */
1271                           dbus_set_error (error,
1272                                                           DBUS_ERROR_FAILED,
1273                                                           "Could not determine unique name for '%s'", text);
1274                           goto failed;
1275                         }
1276                   _dbus_assert (*base_name == ':');
1277                 }
1278   }
1279   _dbus_assert (base_name != NULL);
1280
1281   reply = dbus_message_new_method_return (message);
1282   if (reply == NULL)
1283     goto oom;
1284
1285   if (! dbus_message_append_args (reply,
1286                                   DBUS_TYPE_STRING, &base_name,
1287                                   DBUS_TYPE_INVALID))
1288     goto oom;
1289
1290   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1291     goto oom;
1292
1293   dbus_message_unref (reply);
1294
1295   return TRUE;
1296
1297  oom:
1298   BUS_SET_OOM (error);
1299
1300  failed:
1301   _DBUS_ASSERT_ERROR_IS_SET (error);
1302   if (reply)
1303     dbus_message_unref (reply);
1304   return FALSE;
1305 }
1306
1307 static dbus_bool_t
1308 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1309                                       BusTransaction *transaction,
1310                                       DBusMessage    *message,
1311                                       DBusError      *error)
1312 {
1313   const char *text;
1314   DBusList *base_names;
1315   DBusList *link;
1316   DBusString str;
1317   BusRegistry *registry;
1318   BusService *service;
1319   DBusMessage *reply;
1320   DBusMessageIter iter, array_iter;
1321   char *dbus_service_name = DBUS_SERVICE_DBUS;
1322
1323   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1324
1325   registry = bus_connection_get_registry (connection);
1326
1327   base_names = NULL;
1328   text = NULL;
1329   reply = NULL;
1330
1331   if (! dbus_message_get_args (message, error,
1332                                DBUS_TYPE_STRING, &text,
1333                                DBUS_TYPE_INVALID))
1334       goto failed;
1335
1336   _dbus_string_init_const (&str, text);
1337   service = bus_registry_lookup (registry, &str);
1338   if (service == NULL &&
1339       _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1340     {
1341       /* ORG_FREEDESKTOP_DBUS owns itself */
1342       if (! _dbus_list_append (&base_names, dbus_service_name))
1343         goto oom;
1344     }
1345   else if (service == NULL)
1346     {
1347       dbus_set_error (error,
1348                       DBUS_ERROR_NAME_HAS_NO_OWNER,
1349                       "Could not get owners of name '%s': no such name", text);
1350       goto failed;
1351     }
1352   else
1353     {
1354       if (!bus_service_list_queued_owners (service,
1355                                            &base_names,
1356                                            error))
1357         goto failed;
1358     }
1359
1360   _dbus_assert (base_names != NULL);
1361
1362   reply = dbus_message_new_method_return (message);
1363   if (reply == NULL)
1364     goto oom;
1365
1366   dbus_message_iter_init_append (reply, &iter);
1367   if (!dbus_message_iter_open_container (&iter,
1368                                          DBUS_TYPE_ARRAY,
1369                                          DBUS_TYPE_STRING_AS_STRING,
1370                                          &array_iter))
1371     goto oom;
1372
1373   link = _dbus_list_get_first_link (&base_names);
1374   while (link != NULL)
1375     {
1376       char *uname;
1377
1378       _dbus_assert (link->data != NULL);
1379       uname = (char *)link->data;
1380
1381       if (!dbus_message_iter_append_basic (&array_iter,
1382                                            DBUS_TYPE_STRING,
1383                                            &uname))
1384         goto oom;
1385
1386       link = _dbus_list_get_next_link (&base_names, link);
1387     }
1388
1389   if (! dbus_message_iter_close_container (&iter, &array_iter))
1390     goto oom;
1391
1392
1393   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1394     goto oom;
1395
1396   dbus_message_unref (reply);
1397
1398   return TRUE;
1399
1400  oom:
1401   BUS_SET_OOM (error);
1402
1403  failed:
1404   _DBUS_ASSERT_ERROR_IS_SET (error);
1405   if (reply)
1406     dbus_message_unref (reply);
1407
1408   if (base_names)
1409     _dbus_list_clear (&base_names);
1410
1411   return FALSE;
1412 }
1413
1414 static dbus_bool_t
1415 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1416                                             BusTransaction *transaction,
1417                                             DBusMessage    *message,
1418                                             DBusError      *error)
1419 {
1420   DBusConnection *conn;
1421   DBusMessage *reply;
1422   unsigned long uid;
1423   dbus_uint32_t uid32;
1424   const char *service;
1425
1426   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1427
1428   reply = dbus_message_new_method_return (message);
1429   if (reply == NULL)
1430     goto oom;
1431
1432   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
1433   {
1434           if(!kdbus_get_connection_unix_user(connection, message, &uid, error))
1435                   goto failed;
1436   }
1437   else
1438   {
1439           conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
1440                                      error);
1441
1442           if (conn == NULL)
1443                   goto failed;
1444
1445           if (!dbus_connection_get_unix_user (conn, &uid))
1446                 {
1447                   dbus_set_error (error,
1448                                                   DBUS_ERROR_FAILED,
1449                                                   "Could not determine UID for '%s'", service);
1450                   goto failed;
1451                 }
1452   }
1453
1454   uid32 = uid;
1455   if (! dbus_message_append_args (reply,
1456                                   DBUS_TYPE_UINT32, &uid32,
1457                                   DBUS_TYPE_INVALID))
1458     goto oom;
1459
1460   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1461     goto oom;
1462
1463   dbus_message_unref (reply);
1464
1465   return TRUE;
1466
1467  oom:
1468   BUS_SET_OOM (error);
1469
1470  failed:
1471   _DBUS_ASSERT_ERROR_IS_SET (error);
1472   if (reply)
1473     dbus_message_unref (reply);
1474   return FALSE;
1475 }
1476
1477 static dbus_bool_t
1478 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1479                                                   BusTransaction *transaction,
1480                                                   DBusMessage    *message,
1481                                                   DBusError      *error)
1482 {
1483   DBusConnection *conn;
1484   DBusMessage *reply;
1485   unsigned long pid;
1486   dbus_uint32_t pid32;
1487   const char *service;
1488
1489   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1490
1491   reply = dbus_message_new_method_return (message);
1492   if (reply == NULL)
1493     goto oom;
1494
1495   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
1496   {
1497           if(!kdbus_get_connection_unix_process_id(connection, message, &pid, error))
1498                   goto failed;
1499   }
1500   else
1501   {
1502           conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
1503                                                                                  error);
1504
1505           if (conn == NULL)
1506                 goto failed;
1507
1508
1509
1510           if (!dbus_connection_get_unix_process_id (conn, &pid))
1511                 {
1512                   dbus_set_error (error,
1513                                                   DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1514                                                   "Could not determine PID for '%s'", service);
1515                   goto failed;
1516                 }
1517   }
1518
1519   pid32 = pid;
1520   if (! dbus_message_append_args (reply,
1521                                   DBUS_TYPE_UINT32, &pid32,
1522                                   DBUS_TYPE_INVALID))
1523     goto oom;
1524
1525   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1526     goto oom;
1527
1528   dbus_message_unref (reply);
1529
1530   return TRUE;
1531
1532  oom:
1533   BUS_SET_OOM (error);
1534
1535  failed:
1536   _DBUS_ASSERT_ERROR_IS_SET (error);
1537   if (reply)
1538     dbus_message_unref (reply);
1539   return FALSE;
1540 }
1541
1542 static dbus_bool_t
1543 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1544                                               BusTransaction *transaction,
1545                                               DBusMessage    *message,
1546                                               DBusError      *error)
1547 {
1548   DBusConnection *conn;
1549   DBusMessage *reply;
1550   void *data = NULL;
1551   dbus_uint32_t data_size;
1552   const char *service;
1553
1554   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1555
1556   reply = NULL;
1557
1558   conn = bus_driver_get_conn_helper (connection, message,
1559                                      "audit session data", &service, error);
1560
1561   if (conn == NULL)
1562     goto failed;
1563
1564   reply = dbus_message_new_method_return (message);
1565   if (reply == NULL)
1566     goto oom;
1567
1568   if (!dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) || data == NULL)
1569     {
1570       dbus_set_error (error,
1571                       DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1572                       "Could not determine audit session data for '%s'", service);
1573       goto failed;
1574     }
1575
1576   if (! dbus_message_append_args (reply,
1577                                   DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1578                                   DBUS_TYPE_INVALID))
1579     goto oom;
1580
1581   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1582     goto oom;
1583
1584   dbus_message_unref (reply);
1585
1586   return TRUE;
1587
1588  oom:
1589   BUS_SET_OOM (error);
1590
1591  failed:
1592   _DBUS_ASSERT_ERROR_IS_SET (error);
1593   if (reply)
1594     dbus_message_unref (reply);
1595   return FALSE;
1596 }
1597
1598 static dbus_bool_t
1599 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1600                                                            BusTransaction *transaction,
1601                                                            DBusMessage    *message,
1602                                                            DBusError      *error)
1603 {
1604   DBusConnection *conn;
1605   DBusMessage *reply;
1606   BusSELinuxID *context;
1607   const char *service;
1608
1609   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1610
1611   reply = dbus_message_new_method_return (message);
1612   if (reply == NULL)
1613     goto oom;
1614
1615   if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
1616   {
1617           if(!kdbus_get_connection_unix_selinux_security_context(connection, message, reply, error))
1618                   goto failed;
1619   }
1620   else
1621   {
1622           conn = bus_driver_get_conn_helper (connection, message, "security context",
1623                                                                                  &service, error);
1624
1625           if (conn == NULL)
1626                 goto failed;
1627
1628           context = bus_connection_get_selinux_id (conn);
1629           if (!context)
1630                 {
1631                   dbus_set_error (error,
1632                                                   DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1633                                                   "Could not determine security context for '%s'", service);
1634                   goto failed;
1635                 }
1636
1637           if (! bus_selinux_append_context (reply, context, error))
1638                 goto failed;
1639   }
1640
1641   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1642     goto oom;
1643
1644   dbus_message_unref (reply);
1645
1646   return TRUE;
1647
1648  oom:
1649   BUS_SET_OOM (error);
1650
1651  failed:
1652   _DBUS_ASSERT_ERROR_IS_SET (error);
1653   if (reply)
1654     dbus_message_unref (reply);
1655   return FALSE;
1656 }
1657
1658 static dbus_bool_t
1659 bus_driver_handle_get_connection_credentials (DBusConnection *connection,
1660                                               BusTransaction *transaction,
1661                                               DBusMessage    *message,
1662                                               DBusError      *error)
1663 {
1664   DBusConnection *conn;
1665   DBusMessage *reply;
1666   DBusMessageIter reply_iter;
1667   DBusMessageIter array_iter;
1668   unsigned long ulong_val;
1669   const char *service;
1670
1671   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1672
1673   reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
1674   if (reply == NULL)
1675     goto oom;
1676
1677   if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
1678   {
1679           conn = bus_driver_get_conn_helper (connection, message, "credentials",
1680                                                                                  &service, error);
1681
1682           if (conn == NULL)
1683                 goto failed;
1684
1685           /* we can't represent > 32-bit pids; if your system needs them, please
1686            * add ProcessID64 to the spec or something */
1687           if (dbus_connection_get_unix_process_id (conn, &ulong_val) &&
1688                   ulong_val <= _DBUS_UINT32_MAX)
1689                 {
1690                   if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1691                         goto oom;
1692                 }
1693
1694           /* we can't represent > 32-bit uids; if your system needs them, please
1695            * add UnixUserID64 to the spec or something */
1696           if (dbus_connection_get_unix_user (conn, &ulong_val) &&
1697                   ulong_val <= _DBUS_UINT32_MAX)
1698                 {
1699                   if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1700                         goto oom;
1701                 }
1702   }
1703   else
1704   {
1705           if(kdbus_get_connection_unix_process_id(connection, message, &ulong_val, error))
1706           {
1707                   if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1708                         goto oom;
1709           }
1710           else
1711                   goto failed;
1712
1713           if(kdbus_get_connection_unix_user(connection, message, &ulong_val, error))
1714           {
1715                   if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1716                         goto oom;
1717           }
1718           else
1719                   goto failed;
1720   }
1721
1722   if (!_dbus_asv_close (&reply_iter, &array_iter))
1723     goto oom;
1724
1725   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1726     {
1727       /* this time we don't want to close the iterator again, so just
1728        * get rid of the message */
1729       dbus_message_unref (reply);
1730       reply = NULL;
1731       goto oom;
1732     }
1733
1734   return TRUE;
1735
1736  oom:
1737   BUS_SET_OOM (error);
1738
1739  failed:
1740   _DBUS_ASSERT_ERROR_IS_SET (error);
1741
1742   if (reply)
1743     {
1744       _dbus_asv_abandon (&reply_iter, &array_iter);
1745       dbus_message_unref (reply);
1746     }
1747
1748   return FALSE;
1749 }
1750
1751 static dbus_bool_t
1752 bus_driver_handle_reload_config (DBusConnection *connection,
1753                                  BusTransaction *transaction,
1754                                  DBusMessage    *message,
1755                                  DBusError      *error)
1756 {
1757   BusContext *context;
1758   DBusMessage *reply;
1759
1760   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1761
1762   reply = NULL;
1763
1764   context = bus_connection_get_context (connection);
1765   if (!bus_context_reload_config (context, error))
1766     goto failed;
1767
1768   reply = dbus_message_new_method_return (message);
1769   if (reply == NULL)
1770     goto oom;
1771
1772   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1773     goto oom;
1774
1775   dbus_message_unref (reply);
1776   return TRUE;
1777
1778  oom:
1779   BUS_SET_OOM (error);
1780
1781  failed:
1782   _DBUS_ASSERT_ERROR_IS_SET (error);
1783   if (reply)
1784     dbus_message_unref (reply);
1785   return FALSE;
1786 }
1787
1788 static dbus_bool_t
1789 bus_driver_handle_get_id (DBusConnection *connection,
1790                           BusTransaction *transaction,
1791                           DBusMessage    *message,
1792                           DBusError      *error)
1793 {
1794   BusContext *context;
1795   DBusMessage *reply;
1796   DBusString uuid;
1797   const char *v_STRING;
1798
1799   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1800
1801   if (!_dbus_string_init (&uuid))
1802     {
1803       BUS_SET_OOM (error);
1804       return FALSE;
1805     }
1806
1807   reply = NULL;
1808
1809   context = bus_connection_get_context (connection);
1810   if (!bus_context_get_id (context, &uuid))
1811     goto oom;
1812
1813   reply = dbus_message_new_method_return (message);
1814   if (reply == NULL)
1815     goto oom;
1816
1817   v_STRING = _dbus_string_get_const_data (&uuid);
1818   if (!dbus_message_append_args (reply,
1819                                  DBUS_TYPE_STRING, &v_STRING,
1820                                  DBUS_TYPE_INVALID))
1821     goto oom;
1822
1823   _dbus_assert (dbus_message_has_signature (reply, "s"));
1824
1825   if (! bus_transaction_send_from_driver (transaction, connection, reply))
1826     goto oom;
1827
1828   _dbus_string_free (&uuid);
1829   dbus_message_unref (reply);
1830   return TRUE;
1831
1832  oom:
1833   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1834
1835   BUS_SET_OOM (error);
1836
1837   if (reply)
1838     dbus_message_unref (reply);
1839   _dbus_string_free (&uuid);
1840   return FALSE;
1841 }
1842
1843 typedef struct
1844 {
1845   const char *name;
1846   const char *in_args;
1847   const char *out_args;
1848   dbus_bool_t (* handler) (DBusConnection *connection,
1849                            BusTransaction *transaction,
1850                            DBusMessage    *message,
1851                            DBusError      *error);
1852 } MessageHandler;
1853
1854 /* For speed it might be useful to sort this in order of
1855  * frequency of use (but doesn't matter with only a few items
1856  * anyhow)
1857  */
1858 static const MessageHandler dbus_message_handlers[] = {
1859   { "Hello",
1860     "",
1861     DBUS_TYPE_STRING_AS_STRING,
1862     bus_driver_handle_hello },
1863   { "RequestName",
1864     DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1865     DBUS_TYPE_UINT32_AS_STRING,
1866     bus_driver_handle_acquire_service },
1867   { "ReleaseName",
1868     DBUS_TYPE_STRING_AS_STRING,
1869     DBUS_TYPE_UINT32_AS_STRING,
1870     bus_driver_handle_release_service },
1871   { "StartServiceByName",
1872     DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1873     DBUS_TYPE_UINT32_AS_STRING,
1874     bus_driver_handle_activate_service },
1875   { "UpdateActivationEnvironment",
1876     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,
1877     "",
1878     bus_driver_handle_update_activation_environment },
1879   { "NameHasOwner",
1880     DBUS_TYPE_STRING_AS_STRING,
1881     DBUS_TYPE_BOOLEAN_AS_STRING,
1882     bus_driver_handle_service_exists },
1883   { "ListNames",
1884     "",
1885     DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1886     bus_driver_handle_list_services },
1887   { "ListActivatableNames",
1888     "",
1889     DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1890     bus_driver_handle_list_activatable_services },
1891   { "AddMatch",
1892     DBUS_TYPE_STRING_AS_STRING,
1893     "",
1894     bus_driver_handle_add_match },
1895   { "RemoveMatch",
1896     DBUS_TYPE_STRING_AS_STRING,
1897     "",
1898     bus_driver_handle_remove_match },
1899   { "GetNameOwner",
1900     DBUS_TYPE_STRING_AS_STRING,
1901     DBUS_TYPE_STRING_AS_STRING,
1902     bus_driver_handle_get_service_owner },
1903   { "ListQueuedOwners",
1904     DBUS_TYPE_STRING_AS_STRING,
1905     DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1906     bus_driver_handle_list_queued_owners },
1907   { "GetConnectionUnixUser",
1908     DBUS_TYPE_STRING_AS_STRING,
1909     DBUS_TYPE_UINT32_AS_STRING,
1910     bus_driver_handle_get_connection_unix_user },
1911   { "GetConnectionUnixProcessID",
1912     DBUS_TYPE_STRING_AS_STRING,
1913     DBUS_TYPE_UINT32_AS_STRING,
1914     bus_driver_handle_get_connection_unix_process_id },
1915   { "GetAdtAuditSessionData",
1916     DBUS_TYPE_STRING_AS_STRING,
1917     DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1918     bus_driver_handle_get_adt_audit_session_data },
1919   { "GetConnectionSELinuxSecurityContext",
1920     DBUS_TYPE_STRING_AS_STRING,
1921     DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1922     bus_driver_handle_get_connection_selinux_security_context },
1923   { "ReloadConfig",
1924     "",
1925     "",
1926     bus_driver_handle_reload_config },
1927   { "GetId",
1928     "",
1929     DBUS_TYPE_STRING_AS_STRING,
1930     bus_driver_handle_get_id },
1931   { "GetConnectionCredentials", "s", "a{sv}",
1932     bus_driver_handle_get_connection_credentials },
1933   { NULL, NULL, NULL, NULL }
1934 };
1935
1936 static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
1937     BusTransaction *, DBusMessage *, DBusError *);
1938
1939 static const MessageHandler introspectable_message_handlers[] = {
1940   { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
1941   { NULL, NULL, NULL, NULL }
1942 };
1943
1944 #ifdef DBUS_ENABLE_STATS
1945 static const MessageHandler stats_message_handlers[] = {
1946   { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
1947   { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
1948   { NULL, NULL, NULL, NULL }
1949 };
1950 #endif
1951
1952 typedef struct {
1953   const char *name;
1954   const MessageHandler *message_handlers;
1955   const char *extra_introspection;
1956 } InterfaceHandler;
1957
1958 /* These should ideally be sorted by frequency of use, although it
1959  * probably doesn't matter with this few items */
1960 static InterfaceHandler interface_handlers[] = {
1961   { DBUS_INTERFACE_DBUS, dbus_message_handlers,
1962     "    <signal name=\"NameOwnerChanged\">\n"
1963     "      <arg type=\"s\"/>\n"
1964     "      <arg type=\"s\"/>\n"
1965     "      <arg type=\"s\"/>\n"
1966     "    </signal>\n"
1967     "    <signal name=\"NameLost\">\n"
1968     "      <arg type=\"s\"/>\n"
1969     "    </signal>\n"
1970     "    <signal name=\"NameAcquired\">\n"
1971     "      <arg type=\"s\"/>\n"
1972     "    </signal>\n" },
1973   { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
1974 #ifdef DBUS_ENABLE_STATS
1975   { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
1976 #endif
1977   { NULL, NULL, NULL }
1978 };
1979
1980 static dbus_bool_t
1981 write_args_for_direction (DBusString *xml,
1982                           const char *signature,
1983                           dbus_bool_t in)
1984 {
1985   DBusTypeReader typereader;
1986   DBusString sigstr;
1987   int current_type;
1988
1989   _dbus_string_init_const (&sigstr, signature);
1990   _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1991
1992   while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1993     {
1994       const DBusString *subsig;
1995       int start, len;
1996
1997       _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1998       if (!_dbus_string_append_printf (xml, "      <arg direction=\"%s\" type=\"",
1999                                        in ? "in" : "out"))
2000         goto oom;
2001       if (!_dbus_string_append_len (xml,
2002                                     _dbus_string_get_const_data (subsig) + start,
2003                                     len))
2004         goto oom;
2005       if (!_dbus_string_append (xml, "\"/>\n"))
2006         goto oom;
2007
2008       _dbus_type_reader_next (&typereader);
2009     }
2010   return TRUE;
2011  oom:
2012   return FALSE;
2013 }
2014
2015 dbus_bool_t
2016 bus_driver_generate_introspect_string (DBusString *xml)
2017 {
2018   const InterfaceHandler *ih;
2019   const MessageHandler *mh;
2020
2021   if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
2022     return FALSE;
2023   if (!_dbus_string_append (xml, "<node>\n"))
2024     return FALSE;
2025
2026   for (ih = interface_handlers; ih->name != NULL; ih++)
2027     {
2028       if (!_dbus_string_append_printf (xml, "  <interface name=\"%s\">\n",
2029                                        ih->name))
2030         return FALSE;
2031
2032       for (mh = ih->message_handlers; mh->name != NULL; mh++)
2033         {
2034           if (!_dbus_string_append_printf (xml, "    <method name=\"%s\">\n",
2035                                            mh->name))
2036             return FALSE;
2037
2038           if (!write_args_for_direction (xml, mh->in_args, TRUE))
2039             return FALSE;
2040
2041           if (!write_args_for_direction (xml, mh->out_args, FALSE))
2042             return FALSE;
2043
2044           if (!_dbus_string_append (xml, "    </method>\n"))
2045             return FALSE;
2046         }
2047
2048       if (ih->extra_introspection != NULL &&
2049           !_dbus_string_append (xml, ih->extra_introspection))
2050         return FALSE;
2051
2052       if (!_dbus_string_append (xml, "  </interface>\n"))
2053         return FALSE;
2054     }
2055
2056   if (!_dbus_string_append (xml, "</node>\n"))
2057     return FALSE;
2058
2059   return TRUE;
2060 }
2061
2062 static dbus_bool_t
2063 bus_driver_handle_introspect (DBusConnection *connection,
2064                               BusTransaction *transaction,
2065                               DBusMessage    *message,
2066                               DBusError      *error)
2067 {
2068   DBusString xml;
2069   DBusMessage *reply;
2070   const char *v_STRING;
2071
2072   _dbus_verbose ("Introspect() on bus driver\n");
2073
2074   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2075
2076   reply = NULL;
2077
2078   if (! dbus_message_get_args (message, error,
2079                                DBUS_TYPE_INVALID))
2080     {
2081       _DBUS_ASSERT_ERROR_IS_SET (error);
2082       return FALSE;
2083     }
2084
2085   if (!_dbus_string_init (&xml))
2086     {
2087       BUS_SET_OOM (error);
2088       return FALSE;
2089     }
2090
2091   if (!bus_driver_generate_introspect_string (&xml))
2092     goto oom;
2093
2094   v_STRING = _dbus_string_get_const_data (&xml);
2095
2096   reply = dbus_message_new_method_return (message);
2097   if (reply == NULL)
2098     goto oom;
2099
2100   if (! dbus_message_append_args (reply,
2101                                   DBUS_TYPE_STRING, &v_STRING,
2102                                   DBUS_TYPE_INVALID))
2103     goto oom;
2104
2105   if (! bus_transaction_send_from_driver (transaction, connection, reply))
2106     goto oom;
2107
2108   dbus_message_unref (reply);
2109   _dbus_string_free (&xml);
2110
2111   return TRUE;
2112
2113  oom:
2114   BUS_SET_OOM (error);
2115
2116   if (reply)
2117     dbus_message_unref (reply);
2118
2119   _dbus_string_free (&xml);
2120
2121   return FALSE;
2122 }
2123
2124 dbus_bool_t
2125 bus_driver_handle_message (DBusConnection *connection,
2126                            BusTransaction *transaction,
2127                            DBusMessage    *message,
2128                            DBusError      *error)
2129 {
2130   const char *name, *interface;
2131   const InterfaceHandler *ih;
2132   const MessageHandler *mh;
2133   dbus_bool_t found_interface = FALSE;
2134
2135   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2136
2137   if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
2138     {
2139       BusContext *context;
2140
2141       context = bus_connection_get_context (connection);
2142       return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
2143     }
2144
2145   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
2146     {
2147       _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
2148       return TRUE; /* we just ignore this */
2149     }
2150
2151   /* may be NULL, which means "any interface will do" */
2152   interface = dbus_message_get_interface (message);
2153
2154   _dbus_assert (dbus_message_get_member (message) != NULL);
2155
2156   name = dbus_message_get_member (message);
2157
2158   _dbus_verbose ("Driver got a method call: %s\n", name);
2159
2160   /* security checks should have kept this from getting here */
2161   _dbus_assert (dbus_message_get_sender (message) != NULL ||
2162                 strcmp (name, "Hello") == 0);
2163
2164   for (ih = interface_handlers; ih->name != NULL; ih++)
2165     {
2166       if (interface != NULL && strcmp (interface, ih->name) != 0)
2167         continue;
2168
2169       found_interface = TRUE;
2170
2171       for (mh = ih->message_handlers; mh->name != NULL; mh++)
2172         {
2173           if (strcmp (mh->name, name) != 0)
2174             continue;
2175
2176           _dbus_verbose ("Found driver handler for %s\n", name);
2177
2178           if (!dbus_message_has_signature (message, mh->in_args))
2179             {
2180               _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2181               _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
2182                              name, dbus_message_get_signature (message),
2183                              mh->in_args);
2184
2185               dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2186                               "Call to %s has wrong args (%s, expected %s)\n",
2187                               name, dbus_message_get_signature (message),
2188                               mh->in_args);
2189               _DBUS_ASSERT_ERROR_IS_SET (error);
2190               return FALSE;
2191             }
2192
2193           if ((* mh->handler) (connection, transaction, message, error))
2194             {
2195               _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2196               _dbus_verbose ("Driver handler succeeded\n");
2197               return TRUE;
2198             }
2199           else
2200             {
2201               _DBUS_ASSERT_ERROR_IS_SET (error);
2202               _dbus_verbose ("Driver handler returned failure\n");
2203               return FALSE;
2204             }
2205         }
2206     }
2207
2208   _dbus_verbose ("No driver handler for message \"%s\"\n",
2209                  name);
2210
2211   dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2212                   "%s does not understand message %s",
2213                   DBUS_SERVICE_DBUS, name);
2214
2215   return FALSE;
2216 }
2217
2218 void
2219 bus_driver_remove_connection (DBusConnection *connection)
2220 {
2221   /* FIXME 1.0 Does nothing for now, should unregister the connection
2222    * with the bus driver.
2223    */
2224 }