1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* kdbus-d.c kdbus related daemon functions
4 * Copyright (C) 2013 Samsung Electronics
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version and under the terms of the GNU
12 * Lesser General Public License as published by the
13 * Free Software Foundation; either version 2.1 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <dbus/dbus-connection-internal.h>
30 #include <dbus/kdbus.h>
31 #include <dbus/dbus-bus.h>
33 #include <dbus/kdbus-common.h>
34 #include <dbus/dbus-transport.h>
35 #include <dbus/dbus-transport-kdbus.h>
36 #include "connection.h"
37 #include "activation.h"
39 #include <dbus/dbus-connection.h>
49 * Converts string with unique name into __u64 id number. If the name is not unique, sets error.
51 __u64 sender_name_to_id(const char* name, DBusError* error)
55 if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
56 sender_id = strtoull(&name[3], NULL, 10);
58 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");
64 * Seeks key in rule string, and duplicates value of the key into pValue.
65 * Because of the duplication, pValue must be freed after use.
67 * @param rule rule to look through
68 * @param key key to look for
69 * @param pValue pointer to value of the key found
70 * @return length of the value string, 0 means not found
72 static int parse_match_key(const char *rule, const char* key, char** pValue)
75 const char* pValueEnd;
78 pBegin = strstr(rule, key);
81 pBegin += strlen(key);
82 pValueEnd = strchr(pBegin, '\'');
85 value_length = pValueEnd - pBegin;
86 *pValue = strndup(pBegin, value_length);
88 _dbus_verbose ("found for key: %s value:'%s'\n", key, *pValue);
95 * Adds a match rule to match broadcast messages going through the message bus.
96 * Do no affect messages addressed directly.
98 * The "rule" argument is the string form of a match rule.
100 * Only part of the dbus's matching capabilities is implemented in kdbus now, because of different mechanism.
102 * interface match key mapped to bloom
103 * sender match key mapped to src_name
105 * @param transport transport
106 * @param id id of connection for which the rule is to be added
107 * @param rule textual form of match rule
109 dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rule)
111 struct kdbus_cmd_match* pCmd_match;
112 struct kdbus_item *pItem;
113 __u64 src_id = KDBUS_MATCH_SRC_ID_ANY;
117 char* pInterface = NULL;
118 dbus_bool_t ret_value = FALSE;
122 if(!_dbus_transport_get_socket_fd(transport, &fd))
125 bloom_size = dbus_transport_get_bloom_size(transport);
127 /*parsing rule and calculating size of command*/
128 size = sizeof(struct kdbus_cmd_match);
129 if(parse_match_key(rule, "interface='", &pInterface)) /*actual size is not important for interface because bloom size is defined by bus*/
130 size += KDBUS_ITEM_HEADER_SIZE + bloom_size;
131 name_size = parse_match_key(rule, "sender='", &pName);
134 if(!strncmp(pName, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
136 src_id = strtoull(&pName[3], NULL, 10);
141 size += KDBUS_ITEM_SIZE(name_size + 1); //well known name
144 pCmd_match = alloca(size);
145 if(pCmd_match == NULL)
149 pCmd_match->cookie = id;
150 pCmd_match->size = size;
151 pCmd_match->src_id = src_id;
153 pItem = pCmd_match->items;
156 pItem->type = KDBUS_MATCH_SRC_NAME;
157 pItem->size = KDBUS_ITEM_HEADER_SIZE + name_size + 1;
158 memcpy(pItem->str, pName, strlen(pName) + 1);
159 pItem = KDBUS_PART_NEXT(pItem);
163 pItem->type = KDBUS_MATCH_BLOOM;
164 pItem->size = KDBUS_ITEM_HEADER_SIZE + bloom_size;
165 strncpy(pItem->data, pInterface, bloom_size);
168 if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
169 _dbus_verbose("Failed adding match bus rule %s,\nerror: %d, %m\n", rule, errno);
172 _dbus_verbose("Added match bus rule %s for id:%llu\n", rule, (unsigned long long)id);
185 * Opposing to dbus, in kdbus removes all match rules with given
186 * cookie, which in this implementation is equal to uniqe id.
188 * @param transport transport
189 * @param id connection id for which rules are to be removed
191 dbus_bool_t remove_match_kdbus (DBusTransport* transport, __u64 id)
193 struct kdbus_cmd_match __attribute__ ((__aligned__(8))) cmd;
196 if(!_dbus_transport_get_socket_fd(transport, &fd))
201 cmd.size = sizeof(struct kdbus_cmd_match);
203 if(ioctl(fd, KDBUS_CMD_MATCH_REMOVE, &cmd))
205 _dbus_verbose("Failed removing match rule for id: %llu; error: %d, %m\n", (unsigned long long)id, errno);
210 _dbus_verbose("Match rule removed correctly.\n");
216 * Performs kdbus query of id of the given name
218 * @param name name to query for
219 * @param transport transport
220 * @param pInfo nameInfo structure address to store info about the name
221 * @return 0 on success, -errno if failed
223 int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo* pInfo)
225 struct kdbus_cmd_conn_info *cmd;
231 memset(pInfo, 0, sizeof(struct nameInfo));
233 if(!_dbus_transport_get_socket_fd(transport, &fd))
236 size = sizeof(struct kdbus_cmd_conn_info);
237 if((name[0] == ':') && (name[1] == '1') && (name[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
238 id = strtoull(&name[3], NULL, 10);
240 size += strlen(name) + 1;
245 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
249 memset(cmd, 0, sizeof(struct kdbus_cmd_conn_info));
253 memcpy(cmd->name, name, strlen(name) + 1);
256 ret = ioctl(fd, KDBUS_CMD_CONN_INFO, cmd);
266 struct kdbus_conn_info *info;
267 struct kdbus_item *item;
269 info = (struct kdbus_conn_info *)((char*)dbus_transport_get_pool_pointer(transport) + cmd->offset);
270 pInfo->uniqueId = info->id;
273 while((uint8_t *)(item) < (uint8_t *)(info) + info->size)
275 if(item->type == KDBUS_ITEM_CREDS)
277 pInfo->userId = item->creds.uid;
278 pInfo->processId = item->creds.pid;
281 if(item->type == KDBUS_ITEM_SECLABEL)
283 pInfo->sec_label_len = item->size - KDBUS_ITEM_HEADER_SIZE - 1;
284 if(pInfo->sec_label_len != 0)
286 pInfo->sec_label = malloc(pInfo->sec_label_len);
287 if(pInfo->sec_label == NULL)
290 memcpy(pInfo->sec_label, item->data, pInfo->sec_label_len);
294 item = KDBUS_PART_NEXT(item);
298 if (ioctl(fd, KDBUS_CMD_FREE, &cmd->offset) < 0)
302 _dbus_verbose("kdbus error freeing pool: %d (%m)\n", errno);
311 * Creates kdbus bus of given type.
313 char* make_kdbus_bus(DBusBusType type, const char* address, DBusError *error)
315 // TODO Function alloca() used. In upstream there was a patch proposing to
316 // replace alloca() with malloc() to assure memory alignment. If there will be
317 // suggestion to use malloc instead of alloca this function has to be modified
318 struct kdbus_cmd_make *bus_make;
319 struct kdbus_item *item;
320 __u64 name_size, bus_make_size;
322 char *addr_value = NULL;
326 if(type == DBUS_BUS_SYSTEM)
327 name_size = snprintf(name, 0, "%u-kdbus-%s", getuid(), "system") + 1;
328 else if(type == DBUS_BUS_SESSION)
329 name_size = snprintf(name, 0, "%u-kdbus", getuid()) + 1;
331 name_size = snprintf(name, 0, "%u-kdbus-%u", getuid(), getpid()) + 1;
333 name = alloca(name_size);
339 bus_make_size = sizeof(struct kdbus_cmd_make) + KDBUS_ITEM_SIZE(name_size) + KDBUS_ITEM_SIZE(sizeof(__u64));
340 bus_make = alloca(bus_make_size);
346 bus_make->size = bus_make_size;
347 #ifdef POLICY_TO_KDBUS
348 bus_make->flags = KDBUS_MAKE_ACCESS_WORLD;
350 bus_make->flags = KDBUS_MAKE_ACCESS_WORLD | KDBUS_MAKE_POLICY_OPEN;
352 item = bus_make->items;
354 item->type = KDBUS_ITEM_MAKE_NAME;
355 item->size = KDBUS_ITEM_HEADER_SIZE + name_size;
356 if(type == DBUS_BUS_SYSTEM)
357 sprintf(name, "%u-kdbus-%s", getuid(), "system");
358 else if(type == DBUS_BUS_SESSION)
359 sprintf(name, "%u-kdbus", getuid());
361 sprintf(name, "%u-kdbus-%u", getuid(), getpid());
362 memcpy((bus_make->items)->str, name, name_size);
364 item = KDBUS_PART_NEXT(item);
365 item->type = KDBUS_ITEM_BLOOM_SIZE;
366 item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(__u64);
367 item->data64[0] = 64;
369 addr_value = strchr(address, ':') + 1;
372 if(!strcmp(addr_value, "sbb"))
373 bus_make->flags |= KDBUS_MAKE_SBB_OFFSET;
376 dbus_set_error_const(error, DBUS_ERROR_BAD_ADDRESS, "Invalid address parameter.");
381 _dbus_verbose("Opening /dev/kdbus/control\n");
382 fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
385 _dbus_verbose("--- error %d (%m)\n", fdc);
386 dbus_set_error(error, DBUS_ERROR_FAILED, "Opening /dev/kdbus/control failed: %d (%m)", fdc);
390 _dbus_verbose("Creating bus '%s'\n", (bus_make->items[0]).str);
391 ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, bus_make);
394 _dbus_verbose("--- error %d (%m)\n", errno);
395 dbus_set_error(error, DBUS_ERROR_FAILED, "Creating bus '%s' failed: %d (%m)",
396 (bus_make->items[0]).str, errno);
400 if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", (bus_make->items[0]).str) < 0)
406 _dbus_verbose("Return value '%s'\n", bus);
411 * Minimal server init needed by context to go further.
413 DBusServer* empty_server_init(char* address)
415 return dbus_server_init_mini(address);
418 static dbus_bool_t add_matches_for_kdbus_broadcasts(DBusConnection* connection)
420 struct kdbus_cmd_match* pCmd_match;
421 struct kdbus_item *pItem;
424 DBusTransport *transport;
426 transport = dbus_connection_get_transport(connection);
428 if(!_dbus_transport_get_socket_fd(transport, &fd))
434 size = sizeof(struct kdbus_cmd_match);
435 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2; /*3 name related items plus 2 id related items*/
437 pCmd_match = alloca(size);
438 if(pCmd_match == NULL)
445 pCmd_match->cookie = 1;
446 pCmd_match->size = size;
447 pCmd_match->src_id = 0;
449 pItem = pCmd_match->items;
450 pItem->type = KDBUS_MATCH_NAME_CHANGE;
451 pItem->size = KDBUS_ITEM_HEADER_SIZE + 1;
452 pItem = KDBUS_PART_NEXT(pItem);
453 pItem->type = KDBUS_MATCH_NAME_ADD;
454 pItem->size = KDBUS_ITEM_HEADER_SIZE + 1;
455 pItem = KDBUS_PART_NEXT(pItem);
456 pItem->type = KDBUS_MATCH_NAME_REMOVE;
457 pItem->size = KDBUS_ITEM_HEADER_SIZE + 1;
458 pItem = KDBUS_PART_NEXT(pItem);
459 pItem->type = KDBUS_MATCH_ID_ADD;
460 pItem->size = KDBUS_ITEM_HEADER_SIZE + sizeof(__u64);
461 pItem->id = KDBUS_MATCH_SRC_ID_ANY;
462 pItem = KDBUS_PART_NEXT(pItem);
463 pItem->type = KDBUS_MATCH_ID_REMOVE;
464 pItem->size = KDBUS_ITEM_HEADER_SIZE + sizeof(__u64);
465 pItem->id = KDBUS_MATCH_SRC_ID_ANY;
467 if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
469 _dbus_verbose("Failed adding match rule for daemon, error: %d, %m\n", errno);
473 _dbus_verbose("Added match rule for daemon correctly.\n");
478 * Connects daemon to bus created by him and adds matches for "system" broadcasts.
479 * Do not requests org.freedesktop.DBus name, because it's to early
480 * (some structures of BusContext are not ready yet).
482 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
484 DBusConnection* connection;
486 dbus_bus_set_bus_connection_address(type, address);
488 connection = dbus_bus_get_private(type, error); /*todo possibly could be optimised by using lower functions*/
489 if(connection == NULL)
492 if(!add_matches_for_kdbus_broadcasts(connection))
494 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for daemon, %s", _dbus_strerror_from_errno ());
498 if(dbus_error_is_set(error))
501 _dbus_connection_close_possibly_shared (connection);
502 dbus_connection_unref (connection);
506 _dbus_verbose ("Daemon connected as kdbus client.\n");
512 * Asks bus for org.freedesktop.DBus well-known name.
514 dbus_bool_t register_daemon_name(DBusConnection* connection)
516 DBusString daemon_name;
517 dbus_bool_t retval = FALSE;
518 BusTransaction *transaction;
520 _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
521 #ifdef POLICY_TO_KDBUS
522 if(!register_kdbus_policy(DBUS_SERVICE_DBUS, dbus_connection_get_transport(connection), geteuid()))
526 if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
529 transaction = bus_transaction_new (bus_connection_get_context(connection));
530 if (transaction == NULL)
532 kdbus_release_name(connection, &daemon_name, 0);
536 if(!bus_registry_ensure (bus_connection_get_registry (connection), &daemon_name, connection, 0, transaction, NULL))
538 kdbus_release_name(connection, &daemon_name, 0);
546 bus_transaction_execute_and_free(transaction);
548 bus_transaction_cancel_and_free(transaction);
553 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
557 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
559 return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
562 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
566 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
568 return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
572 * Asks kdbus for well-known names registered on the bus
574 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
577 struct kdbus_cmd_name_list __attribute__ ((__aligned__(8))) cmd;
578 struct kdbus_name_list *name_list;
579 struct kdbus_cmd_name *name;
580 DBusTransport *transport = dbus_connection_get_transport(connection);
581 dbus_bool_t ret_val = FALSE;
587 if(!_dbus_transport_get_socket_fd(transport, &fd))
590 cmd.flags = KDBUS_NAME_LIST_NAMES | KDBUS_NAME_LIST_UNIQUE;
593 if(ioctl(fd, KDBUS_CMD_NAME_LIST, &cmd))
599 _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
604 name_list = (struct kdbus_name_list *)((char*)dbus_transport_get_pool_pointer(transport) + cmd.offset);
606 for (name = name_list->names; (uint8_t *)(name) < (uint8_t *)(name_list) + name_list->size; name = KDBUS_PART_NEXT(name))
609 _dbus_verbose ("List len: %d\n", list_len);
611 list = malloc(sizeof(char*) * (list_len + 1));
615 for (name = name_list->names; (uint8_t *)(name) < (uint8_t *)(name_list) + name_list->size; name = KDBUS_PART_NEXT(name))
617 if(name->size > sizeof(struct kdbus_cmd_name) )
619 list[i] = strdup(name->name);
625 list[i] = malloc(snprintf(list[i], 0, ":1.%llu0", (unsigned long long)name->id));
628 sprintf(list[i], ":1.%llu", (unsigned long long int)name->id);
630 _dbus_verbose ("Name %d: %s\n", i, list[i]);
635 *array_len = list_len;
640 if (ioctl(fd, KDBUS_CMD_FREE, &cmd.offset) < 0)
644 _dbus_verbose("kdbus error freeing pool: %d (%m)\n", errno);
660 * Asks kdbus for list of connections being in the queue to own
661 * given well-known name. The list includes the owner of the name on the
664 dbus_bool_t kdbus_list_queued (DBusConnection *connection, DBusList **return_list,
665 const char *name, DBusError *error)
668 dbus_bool_t ret_val = FALSE;
670 struct kdbus_cmd_conn_info *pCmd;
672 DBusTransport *transport = dbus_connection_get_transport(connection);
673 struct kdbus_name_list *name_list;
674 struct kdbus_cmd_name *owner;
676 _dbus_assert (*return_list == NULL);
678 name_length = strlen(name) + 1;
679 cmd_size = sizeof(struct kdbus_cmd_conn_info) + name_length;
680 pCmd = alloca(cmd_size);
683 pCmd->size = cmd_size;
685 memcpy(pCmd->name, name, name_length);
687 _dbus_verbose ("Asking for queued owners of %s\n", pCmd->name);
689 _dbus_transport_get_socket_fd(transport, &fd);
692 if(ioctl(fd, KDBUS_CMD_NAME_LIST_QUEUED, pCmd))
696 else if(errno == ESRCH)
698 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
699 "Could not get owners of name '%s': no such name", name);
704 _dbus_verbose("kdbus error asking for queued owners list: err %d (%m)\n",errno);
709 name_list = (struct kdbus_name_list *)((char*)dbus_transport_get_pool_pointer(transport) + pCmd->offset);
711 for (owner = name_list->names; (uint8_t *)(owner) < (uint8_t *)(name_list) + name_list->size; owner = KDBUS_PART_NEXT(owner))
715 _dbus_verbose ("Got queued owner id: %llu\n", (unsigned long long)owner->id);
716 uname = malloc(snprintf(uname, 0, ":1.%llu0", (unsigned long long)owner->id));
719 sprintf(uname, ":1.%llu", (unsigned long long int)owner->id);
720 if (!_dbus_list_append (return_list, uname))
727 if (ioctl(fd, KDBUS_CMD_FREE, &pCmd->offset) < 0)
731 _dbus_verbose("kdbus error freeing pool: %d (%m)\n", errno);
738 dbus_set_error (error, _dbus_error_from_errno (errno),
739 "Failed to list queued owners of \"%s\": %s",
740 name, _dbus_strerror (errno));
742 link = _dbus_list_get_first_link (return_list);
745 DBusList *next = _dbus_list_get_next_link (return_list, link);
747 if(link->data != NULL)
750 _dbus_list_free_link (link);
759 * Register match rule in kdbus on behalf of sender of the message
761 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
765 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
766 if(dbus_error_is_set(error))
769 if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
771 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
772 sender_id, _dbus_strerror_from_errno ());
780 * Removes match rule in kdbus on behalf of sender of the message
782 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
786 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
787 if(dbus_error_is_set(error))
790 if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
792 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
799 int kdbus_get_name_owner(DBusConnection* connection, const char* name, char* owner)
802 struct nameInfo info;
804 ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
805 if(ret == 0) //unique id of the name
807 sprintf(owner, ":1.%llu", (unsigned long long int)info.uniqueId);
808 _dbus_verbose("Unique name discovered:%s\n", owner);
810 else if((ret != -ENOENT) && (ret != -ENXIO))
811 _dbus_verbose("kdbus error sending name query: err %d (%m)\n", ret);
817 * Asks kdbus for uid of the owner of the name given in the message
819 dbus_bool_t kdbus_get_unix_user(DBusConnection* connection, const char* name, unsigned long* uid, DBusError* error)
821 struct nameInfo info;
823 dbus_bool_t ret = FALSE;
825 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
826 if(inter_ret == 0) //name found
828 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
832 else if((inter_ret == -ENOENT) || (inter_ret == -ENXIO)) //name has no owner
834 _dbus_verbose ("Name %s has no owner.\n", name);
835 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
840 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
841 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
848 * Asks kdbus for pid of the owner of the name given in the message
850 dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, const char* name, unsigned long* pid, DBusError* error)
852 struct nameInfo info;
854 dbus_bool_t ret = FALSE;
856 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
857 if(inter_ret == 0) //name found
859 _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
860 *pid = info.processId;
863 else if((inter_ret == -ENOENT) || (inter_ret == -ENXIO)) //name has no owner
864 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
867 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
868 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
875 * Asks kdbus for selinux_security_context of the owner of the name given in the message
877 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
880 struct nameInfo info;
882 dbus_bool_t ret = FALSE;
884 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
885 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
886 if((inter_ret == -ENOENT) || (inter_ret == -ENXIO)) //name has no owner
887 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
888 else if(inter_ret < 0)
890 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
891 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
895 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
897 _DBUS_SET_OOM (error);
907 * Gets the UNIX user ID of the connection from kdbus, if known. Returns #TRUE if
908 * the uid is filled in. Always returns #FALSE on non-UNIX platforms
909 * for now., though in theory someone could hook Windows to NIS or
910 * something. Always returns #FALSE prior to authenticating the
913 * The UID of is only read by bus daemon from kdbus. You can not
914 * call this function from client side of the connection.
916 * You can ask the bus to tell you the UID of another connection though
917 * if you like; this is done with dbus_bus_get_unix_user().
919 * @param connection the connection
920 * @param uid return location for the user ID
921 * @returns #TRUE if uid is filled in with a valid user ID
924 dbus_connection_get_unix_user (DBusConnection *connection,
927 _dbus_return_val_if_fail (connection != NULL, FALSE);
928 _dbus_return_val_if_fail (uid != NULL, FALSE);
930 if(bus_context_is_kdbus(bus_connection_get_context (connection)))
931 return kdbus_get_unix_user(connection, bus_connection_get_name(connection), uid, NULL);
933 return dbus_connection_get_unix_user_dbus(connection, uid);
937 * Gets the process ID of the connection if any.
938 * Returns #TRUE if the pid is filled in.
940 * @param connection the connection
941 * @param pid return location for the process ID
942 * @returns #TRUE if uid is filled in with a valid process ID
945 dbus_connection_get_unix_process_id (DBusConnection *connection,
948 _dbus_return_val_if_fail (connection != NULL, FALSE);
949 _dbus_return_val_if_fail (pid != NULL, FALSE);
951 if(bus_context_is_kdbus(bus_connection_get_context (connection)))
952 return kdbus_get_connection_unix_process_id(connection, bus_connection_get_name(connection), pid, NULL);
954 return dbus_connection_get_unix_process_id_dbus(connection, pid);
958 * Create connection structure for given name. It is needed to control starters - activatable services
959 * and for ListQueued method (as long as kdbus is not supporting it). This connections don't have it's own
960 * fd so it is set up on the basis of daemon's transport. Functionality of such connection is limited.
962 DBusConnection* create_phantom_connection(DBusConnection* connection, const char* name, DBusError* error)
964 DBusConnection *phantom_connection;
967 _dbus_string_init_const(&Sname, name);
969 phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
970 if(phantom_connection == NULL)
972 if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
974 dbus_connection_unref_phantom(phantom_connection);
975 phantom_connection = NULL;
976 dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name);
979 if(!bus_connection_complete(phantom_connection, &Sname, error))
981 bus_connection_disconnected(phantom_connection);
982 phantom_connection = NULL;
986 _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
989 return phantom_connection;
993 * Registers activatable services as kdbus starters.
995 dbus_bool_t register_kdbus_starters(DBusConnection* connection)
999 dbus_bool_t retval = FALSE;
1001 BusTransaction *transaction;
1003 DBusTransport* transport;
1005 transaction = bus_transaction_new (bus_connection_get_context(connection));
1006 if (transaction == NULL)
1009 if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
1012 transport = dbus_connection_get_transport(connection);
1014 if(!_dbus_transport_get_socket_fd (transport, &fd))
1017 _dbus_string_init(&name);
1019 for(i=0; i<len; i++)
1021 #ifdef POLICY_TO_KDBUS
1022 if(!register_kdbus_policy(services[i], transport, geteuid()))
1026 if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER_NAME) , 0) < 0)
1029 if(!_dbus_string_append(&name, services[i]))
1031 if(!bus_registry_ensure (bus_connection_get_registry (connection), &name, connection,
1032 (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER_NAME), transaction, NULL))
1034 if(!_dbus_string_set_length(&name, 0))
1043 release_kdbus_name(fd, services[j], 0);
1045 dbus_free_string_array (services);
1046 _dbus_string_free(&name);
1048 bus_transaction_execute_and_free(transaction);
1050 bus_transaction_cancel_and_free(transaction);
1056 * Updates kdbus starters (activatable services) after configuration was reloaded.
1057 * It releases all previous starters and registers all new.
1059 dbus_bool_t update_kdbus_starters(DBusConnection* connection)
1061 dbus_bool_t retval = FALSE;
1062 DBusList **services_old;
1064 BusService *service = NULL;
1065 BusTransaction *transaction;
1068 transaction = bus_transaction_new (bus_connection_get_context(connection));
1069 if (transaction == NULL)
1072 if(!_dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd))
1075 services_old = bus_connection_get_services_owned(connection);
1076 link = _dbus_list_get_first_link(services_old);
1077 link = _dbus_list_get_next_link (services_old, link); //skip org.freedesktop.DBus which is not starter
1079 while (link != NULL)
1083 service = (BusService*) link->data;
1087 ret = release_kdbus_name(fd, bus_service_get_name(service), 0);
1089 if (ret == DBUS_RELEASE_NAME_REPLY_RELEASED)
1091 if(!bus_service_remove_owner(service, connection, transaction, NULL))
1092 _dbus_verbose ("Unable to remove\n");
1097 link = _dbus_list_get_next_link (services_old, link);
1100 if(!register_kdbus_starters(connection))
1102 _dbus_verbose ("Registering kdbus starters for dbus activatable names failed!\n");
1109 bus_transaction_execute_and_free(transaction);
1111 bus_transaction_cancel_and_free(transaction);
1117 * Analyzes system broadcasts about id and name changes.
1118 * Basing on this it sends NameAcquired and NameLost signals and clear phantom connections.
1120 void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusConnection *connection)
1122 const char *name, *old, *new;
1124 if(!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID))
1126 _dbus_verbose ("Couldn't get args of NameOwnerChanged signal.\n");//, error.message);
1130 _dbus_verbose ("Got NameOwnerChanged signal:\nName: %s\nOld: %s\nNew: %s\n", name, old, new);
1132 if(!strncmp(name, ":1.", 3))/*if it starts from :1. it is unique name - this might be IdRemoved info*/
1134 if(!strcmp(name, old)) //yes it is - someone has disconnected
1136 DBusConnection* conn;
1138 conn = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name);
1140 bus_connection_disconnected(conn);
1143 else //it is well-known name
1145 if((*old != 0) && (strcmp(old, bus_connection_get_name(connection))))
1147 DBusMessage *message;
1149 if(bus_connections_find_conn_by_name(bus_connection_get_connections(connection), old) == NULL)
1152 _dbus_verbose ("Owner '%s' lost name '%s'. Sending NameLost.\n", old, name);
1154 message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameLost");
1155 if (message == NULL)
1158 if (!dbus_message_set_destination (message, old) || !dbus_message_append_args (message,
1159 DBUS_TYPE_STRING, &name,
1162 dbus_message_unref (message);
1166 bus_transaction_send_from_driver (transaction, connection, message);
1167 dbus_message_unref (message);
1170 if((*new != 0) && (strcmp(new, bus_connection_get_name(connection))))
1172 DBusMessage *message;
1174 _dbus_verbose ("Owner '%s' acquired name '%s'. Sending NameAcquired.\n", new, name);
1176 message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameAcquired");
1177 if (message == NULL)
1180 if (!dbus_message_set_destination (message, new) || !dbus_message_append_args (message,
1181 DBUS_TYPE_STRING, &name,
1184 dbus_message_unref (message);
1188 bus_transaction_send_from_driver (transaction, connection, message);
1189 dbus_message_unref (message);
1193 if(bus_transaction_send(transaction, connection, msg))
1194 _dbus_verbose ("NameOwnerChanged sent\n");
1196 _dbus_verbose ("Sending NameOwnerChanged failed\n");