1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ofdbus/dbus/bus/driver.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,1755 @@
1.4 +/* -*- mode: C; c-file-style: "gnu" -*- */
1.5 +/* driver.c Bus client (driver)
1.6 + *
1.7 + * Copyright (C) 2003 CodeFactory AB
1.8 + * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
1.9 + * Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.10 + * Licensed under the Academic Free License version 2.1
1.11 + *
1.12 + * This program is free software; you can redistribute it and/or modify
1.13 + * it under the terms of the GNU General Public License as published by
1.14 + * the Free Software Foundation; either version 2 of the License, or
1.15 + * (at your option) any later version.
1.16 + *
1.17 + * This program is distributed in the hope that it will be useful,
1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.20 + * GNU General Public License for more details.
1.21 + *
1.22 + * You should have received a copy of the GNU General Public License
1.23 + * along with this program; if not, write to the Free Software
1.24 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.25 + *
1.26 + */
1.27 +
1.28 +#include "activation.h"
1.29 +#include "connection.h"
1.30 +#include "driver.h"
1.31 +#include "dispatch.h"
1.32 +#include "services.h"
1.33 +#include "selinux.h"
1.34 +#include "signals.h"
1.35 +#include "utils.h"
1.36 +#ifndef __SYMBIAN32__
1.37 +#include <dbus/dbus-string.h>
1.38 +#include <dbus/dbus-internals.h>
1.39 +#include <dbus/dbus-marshal-recursive.h>
1.40 +#else
1.41 +#include "dbus-string.h"
1.42 +#include "dbus-internals.h"
1.43 +#include "dbus-marshal-recursive.h"
1.44 +#endif //__SYMBIAN32__
1.45 +#include <string.h>
1.46 +
1.47 +static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
1.48 + DBusMessage *hello_message,
1.49 + BusTransaction *transaction,
1.50 + DBusError *error);
1.51 +
1.52 +dbus_bool_t
1.53 +bus_driver_send_service_owner_changed (const char *service_name,
1.54 + const char *old_owner,
1.55 + const char *new_owner,
1.56 + BusTransaction *transaction,
1.57 + DBusError *error)
1.58 +{
1.59 + DBusMessage *message;
1.60 + dbus_bool_t retval;
1.61 + const char *null_service;
1.62 +
1.63 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.64 +
1.65 + null_service = "";
1.66 + _dbus_verbose ("sending name owner changed: %s [%s -> %s]\n",
1.67 + service_name,
1.68 + old_owner ? old_owner : null_service,
1.69 + new_owner ? new_owner : null_service);
1.70 +
1.71 + message = dbus_message_new_signal (DBUS_PATH_DBUS,
1.72 + DBUS_INTERFACE_DBUS,
1.73 + "NameOwnerChanged");
1.74 +
1.75 + if (message == NULL)
1.76 + {
1.77 + BUS_SET_OOM (error);
1.78 + return FALSE;
1.79 + }
1.80 +
1.81 + if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
1.82 + goto oom;
1.83 +
1.84 + if (!dbus_message_append_args (message,
1.85 + DBUS_TYPE_STRING, &service_name,
1.86 + DBUS_TYPE_STRING, old_owner ? &old_owner : &null_service,
1.87 + DBUS_TYPE_STRING, new_owner ? &new_owner : &null_service,
1.88 + DBUS_TYPE_INVALID))
1.89 + goto oom;
1.90 +
1.91 + _dbus_assert (dbus_message_has_signature (message, "sss"));
1.92 +
1.93 + retval = bus_dispatch_matches (transaction, NULL, NULL, message, error);
1.94 + dbus_message_unref (message);
1.95 +
1.96 + return retval;
1.97 +
1.98 + oom:
1.99 + dbus_message_unref (message);
1.100 + BUS_SET_OOM (error);
1.101 + return FALSE;
1.102 +}
1.103 +
1.104 +dbus_bool_t
1.105 +bus_driver_send_service_lost (DBusConnection *connection,
1.106 + const char *service_name,
1.107 + BusTransaction *transaction,
1.108 + DBusError *error)
1.109 +{
1.110 + DBusMessage *message;
1.111 +
1.112 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.113 +
1.114 + message = dbus_message_new_signal (DBUS_PATH_DBUS,
1.115 + DBUS_INTERFACE_DBUS,
1.116 + "NameLost");
1.117 +
1.118 + if (message == NULL)
1.119 + {
1.120 + BUS_SET_OOM (error);
1.121 + return FALSE;
1.122 + }
1.123 +
1.124 + if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
1.125 + !dbus_message_append_args (message,
1.126 + DBUS_TYPE_STRING, &service_name,
1.127 + DBUS_TYPE_INVALID))
1.128 + {
1.129 + dbus_message_unref (message);
1.130 + BUS_SET_OOM (error);
1.131 + return FALSE;
1.132 + }
1.133 +
1.134 + if (!bus_transaction_send_from_driver (transaction, connection, message))
1.135 + {
1.136 + dbus_message_unref (message);
1.137 + BUS_SET_OOM (error);
1.138 + return FALSE;
1.139 + }
1.140 + else
1.141 + {
1.142 + dbus_message_unref (message);
1.143 + return TRUE;
1.144 + }
1.145 +}
1.146 +
1.147 +dbus_bool_t
1.148 +bus_driver_send_service_acquired (DBusConnection *connection,
1.149 + const char *service_name,
1.150 + BusTransaction *transaction,
1.151 + DBusError *error)
1.152 +{
1.153 + DBusMessage *message;
1.154 +
1.155 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.156 +
1.157 + message = dbus_message_new_signal (DBUS_PATH_DBUS,
1.158 + DBUS_INTERFACE_DBUS,
1.159 + "NameAcquired");
1.160 +
1.161 + if (message == NULL)
1.162 + {
1.163 + BUS_SET_OOM (error);
1.164 + return FALSE;
1.165 + }
1.166 +
1.167 + if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
1.168 + !dbus_message_append_args (message,
1.169 + DBUS_TYPE_STRING, &service_name,
1.170 + DBUS_TYPE_INVALID))
1.171 + {
1.172 + dbus_message_unref (message);
1.173 + BUS_SET_OOM (error);
1.174 + return FALSE;
1.175 + }
1.176 +
1.177 + if (!bus_transaction_send_from_driver (transaction, connection, message))
1.178 + {
1.179 + dbus_message_unref (message);
1.180 + BUS_SET_OOM (error);
1.181 + return FALSE;
1.182 + }
1.183 + else
1.184 + {
1.185 + dbus_message_unref (message);
1.186 + return TRUE;
1.187 + }
1.188 +}
1.189 +
1.190 +static dbus_bool_t
1.191 +create_unique_client_name (BusRegistry *registry,
1.192 + DBusString *str)
1.193 +{
1.194 + /* We never want to use the same unique client name twice, because
1.195 + * we want to guarantee that if you send a message to a given unique
1.196 + * name, you always get the same application. So we use two numbers
1.197 + * for INT_MAX * INT_MAX combinations, should be pretty safe against
1.198 + * wraparound.
1.199 + */
1.200 + /* FIXME these should be in BusRegistry rather than static vars */
1.201 + static int next_major_number = 0;
1.202 + static int next_minor_number = 0;
1.203 + int len;
1.204 +
1.205 + len = _dbus_string_get_length (str);
1.206 +
1.207 + while (TRUE)
1.208 + {
1.209 + /* start out with 1-0, go to 1-1, 1-2, 1-3,
1.210 + * up to 1-MAXINT, then 2-0, 2-1, etc.
1.211 + */
1.212 + if (next_minor_number <= 0)
1.213 + {
1.214 + next_major_number += 1;
1.215 + next_minor_number = 0;
1.216 + if (next_major_number <= 0)
1.217 + _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
1.218 + }
1.219 +
1.220 + _dbus_assert (next_major_number > 0);
1.221 + _dbus_assert (next_minor_number >= 0);
1.222 +
1.223 + /* appname:MAJOR-MINOR */
1.224 +
1.225 + if (!_dbus_string_append (str, ":"))
1.226 + return FALSE;
1.227 +
1.228 + if (!_dbus_string_append_int (str, next_major_number))
1.229 + return FALSE;
1.230 +
1.231 + if (!_dbus_string_append (str, "."))
1.232 + return FALSE;
1.233 +
1.234 + if (!_dbus_string_append_int (str, next_minor_number))
1.235 + return FALSE;
1.236 +
1.237 + next_minor_number += 1;
1.238 +
1.239 + /* Check if a client with the name exists */
1.240 + if (bus_registry_lookup (registry, str) == NULL)
1.241 + break;
1.242 +
1.243 + /* drop the number again, try the next one. */
1.244 + _dbus_string_set_length (str, len);
1.245 + }
1.246 +
1.247 + return TRUE;
1.248 +}
1.249 +
1.250 +static dbus_bool_t
1.251 +bus_driver_handle_hello (DBusConnection *connection,
1.252 + BusTransaction *transaction,
1.253 + DBusMessage *message,
1.254 + DBusError *error)
1.255 +{
1.256 + DBusString unique_name;
1.257 + BusService *service;
1.258 + dbus_bool_t retval;
1.259 + BusRegistry *registry;
1.260 + BusConnections *connections;
1.261 +
1.262 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.263 +
1.264 + if (bus_connection_is_active (connection))
1.265 + {
1.266 + /* We already handled an Hello message for this connection. */
1.267 + dbus_set_error (error, DBUS_ERROR_FAILED,
1.268 + "Already handled an Hello message");
1.269 + return FALSE;
1.270 + }
1.271 +
1.272 + /* Note that when these limits are exceeded we don't disconnect the
1.273 + * connection; we just sort of leave it hanging there until it times
1.274 + * out or disconnects itself or is dropped due to the max number of
1.275 + * incomplete connections. It's even OK if the connection wants to
1.276 + * retry the hello message, we support that.
1.277 + */
1.278 + connections = bus_connection_get_connections (connection);
1.279 + if (!bus_connections_check_limits (connections, connection,
1.280 + error))
1.281 + {
1.282 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.283 + return FALSE;
1.284 + }
1.285 +
1.286 + if (!_dbus_string_init (&unique_name))
1.287 + {
1.288 + BUS_SET_OOM (error);
1.289 + return FALSE;
1.290 + }
1.291 +
1.292 + retval = FALSE;
1.293 +
1.294 + registry = bus_connection_get_registry (connection);
1.295 +
1.296 + if (!create_unique_client_name (registry, &unique_name))
1.297 + {
1.298 + BUS_SET_OOM (error);
1.299 + goto out_0;
1.300 + }
1.301 +
1.302 + if (!bus_connection_complete (connection, &unique_name, error))
1.303 + {
1.304 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.305 + goto out_0;
1.306 + }
1.307 +
1.308 + if (!dbus_message_set_sender (message,
1.309 + bus_connection_get_name (connection)))
1.310 + {
1.311 + BUS_SET_OOM (error);
1.312 + goto out_0;
1.313 + }
1.314 +
1.315 + if (!bus_driver_send_welcome_message (connection, message, transaction, error))
1.316 + goto out_0;
1.317 +
1.318 + /* Create the service */
1.319 + service = bus_registry_ensure (registry,
1.320 + &unique_name, connection, 0, transaction, error);
1.321 + if (service == NULL)
1.322 + goto out_0;
1.323 +
1.324 + _dbus_assert (bus_connection_is_active (connection));
1.325 + retval = TRUE;
1.326 +
1.327 + out_0:
1.328 + _dbus_string_free (&unique_name);
1.329 + return retval;
1.330 +}
1.331 +
1.332 +static dbus_bool_t
1.333 +bus_driver_send_welcome_message (DBusConnection *connection,
1.334 + DBusMessage *hello_message,
1.335 + BusTransaction *transaction,
1.336 + DBusError *error)
1.337 +{
1.338 + DBusMessage *welcome;
1.339 + const char *name;
1.340 +
1.341 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.342 +
1.343 + name = bus_connection_get_name (connection);
1.344 + _dbus_assert (name != NULL);
1.345 +
1.346 + welcome = dbus_message_new_method_return (hello_message);
1.347 + if (welcome == NULL)
1.348 + {
1.349 + BUS_SET_OOM (error);
1.350 + return FALSE;
1.351 + }
1.352 +
1.353 + if (!dbus_message_append_args (welcome,
1.354 + DBUS_TYPE_STRING, &name,
1.355 + DBUS_TYPE_INVALID))
1.356 + {
1.357 + dbus_message_unref (welcome);
1.358 + BUS_SET_OOM (error);
1.359 + return FALSE;
1.360 + }
1.361 +
1.362 + _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
1.363 +
1.364 + if (!bus_transaction_send_from_driver (transaction, connection, welcome))
1.365 + {
1.366 + dbus_message_unref (welcome);
1.367 + BUS_SET_OOM (error);
1.368 + return FALSE;
1.369 + }
1.370 + else
1.371 + {
1.372 + dbus_message_unref (welcome);
1.373 + return TRUE;
1.374 + }
1.375 +}
1.376 +
1.377 +static dbus_bool_t
1.378 +bus_driver_handle_list_services (DBusConnection *connection,
1.379 + BusTransaction *transaction,
1.380 + DBusMessage *message,
1.381 + DBusError *error)
1.382 +{
1.383 + DBusMessage *reply;
1.384 + int len;
1.385 + char **services;
1.386 + BusRegistry *registry;
1.387 + int i;
1.388 + DBusMessageIter iter;
1.389 + DBusMessageIter sub;
1.390 +
1.391 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.392 +
1.393 + registry = bus_connection_get_registry (connection);
1.394 +
1.395 + reply = dbus_message_new_method_return (message);
1.396 + if (reply == NULL)
1.397 + {
1.398 + BUS_SET_OOM (error);
1.399 + return FALSE;
1.400 + }
1.401 +
1.402 + if (!bus_registry_list_services (registry, &services, &len))
1.403 + {
1.404 + dbus_message_unref (reply);
1.405 + BUS_SET_OOM (error);
1.406 + return FALSE;
1.407 + }
1.408 +
1.409 + dbus_message_iter_init_append (reply, &iter);
1.410 +
1.411 + if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
1.412 + DBUS_TYPE_STRING_AS_STRING,
1.413 + &sub))
1.414 + {
1.415 + dbus_free_string_array (services);
1.416 + dbus_message_unref (reply);
1.417 + BUS_SET_OOM (error);
1.418 + return FALSE;
1.419 + }
1.420 +
1.421 + {
1.422 + /* Include the bus driver in the list */
1.423 + const char *v_STRING = DBUS_SERVICE_DBUS;
1.424 + if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
1.425 + &v_STRING))
1.426 + {
1.427 + dbus_free_string_array (services);
1.428 + dbus_message_unref (reply);
1.429 + BUS_SET_OOM (error);
1.430 + return FALSE;
1.431 + }
1.432 + }
1.433 +
1.434 + i = 0;
1.435 + while (i < len)
1.436 + {
1.437 + if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
1.438 + &services[i]))
1.439 + {
1.440 + dbus_free_string_array (services);
1.441 + dbus_message_unref (reply);
1.442 + BUS_SET_OOM (error);
1.443 + return FALSE;
1.444 + }
1.445 + ++i;
1.446 + }
1.447 +
1.448 + dbus_free_string_array (services);
1.449 +
1.450 + if (!dbus_message_iter_close_container (&iter, &sub))
1.451 + {
1.452 + dbus_message_unref (reply);
1.453 + BUS_SET_OOM (error);
1.454 + return FALSE;
1.455 + }
1.456 +
1.457 + if (!bus_transaction_send_from_driver (transaction, connection, reply))
1.458 + {
1.459 + dbus_message_unref (reply);
1.460 + BUS_SET_OOM (error);
1.461 + return FALSE;
1.462 + }
1.463 + else
1.464 + {
1.465 + dbus_message_unref (reply);
1.466 + return TRUE;
1.467 + }
1.468 +}
1.469 +
1.470 +static dbus_bool_t
1.471 +bus_driver_handle_list_activatable_services (DBusConnection *connection,
1.472 + BusTransaction *transaction,
1.473 + DBusMessage *message,
1.474 + DBusError *error)
1.475 +{
1.476 + DBusMessage *reply;
1.477 + int len;
1.478 + char **services;
1.479 + BusActivation *activation;
1.480 + int i;
1.481 + DBusMessageIter iter;
1.482 + DBusMessageIter sub;
1.483 +
1.484 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.485 +
1.486 + activation = bus_connection_get_activation (connection);
1.487 +
1.488 + reply = dbus_message_new_method_return (message);
1.489 + if (reply == NULL)
1.490 + {
1.491 + BUS_SET_OOM (error);
1.492 + return FALSE;
1.493 + }
1.494 +
1.495 + if (!bus_activation_list_services (activation, &services, &len))
1.496 + {
1.497 + dbus_message_unref (reply);
1.498 + BUS_SET_OOM (error);
1.499 + return FALSE;
1.500 + }
1.501 +
1.502 + dbus_message_iter_init_append (reply, &iter);
1.503 +
1.504 + if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
1.505 + DBUS_TYPE_STRING_AS_STRING,
1.506 + &sub))
1.507 + {
1.508 + dbus_free_string_array (services);
1.509 + dbus_message_unref (reply);
1.510 + BUS_SET_OOM (error);
1.511 + return FALSE;
1.512 + }
1.513 +
1.514 + {
1.515 + /* Include the bus driver in the list */
1.516 + const char *v_STRING = DBUS_SERVICE_DBUS;
1.517 + if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
1.518 + &v_STRING))
1.519 + {
1.520 + dbus_free_string_array (services);
1.521 + dbus_message_unref (reply);
1.522 + BUS_SET_OOM (error);
1.523 + return FALSE;
1.524 + }
1.525 + }
1.526 +
1.527 + i = 0;
1.528 + while (i < len)
1.529 + {
1.530 + if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
1.531 + &services[i]))
1.532 + {
1.533 + dbus_free_string_array (services);
1.534 + dbus_message_unref (reply);
1.535 + BUS_SET_OOM (error);
1.536 + return FALSE;
1.537 + }
1.538 + ++i;
1.539 + }
1.540 +
1.541 + dbus_free_string_array (services);
1.542 +
1.543 + if (!dbus_message_iter_close_container (&iter, &sub))
1.544 + {
1.545 + dbus_message_unref (reply);
1.546 + BUS_SET_OOM (error);
1.547 + return FALSE;
1.548 + }
1.549 +
1.550 + if (!bus_transaction_send_from_driver (transaction, connection, reply))
1.551 + {
1.552 + dbus_message_unref (reply);
1.553 + BUS_SET_OOM (error);
1.554 + return FALSE;
1.555 + }
1.556 + else
1.557 + {
1.558 + dbus_message_unref (reply);
1.559 + return TRUE;
1.560 + }
1.561 +}
1.562 +
1.563 +static dbus_bool_t
1.564 +bus_driver_handle_acquire_service (DBusConnection *connection,
1.565 + BusTransaction *transaction,
1.566 + DBusMessage *message,
1.567 + DBusError *error)
1.568 +{
1.569 + DBusMessage *reply;
1.570 + DBusString service_name;
1.571 + const char *name;
1.572 + dbus_uint32_t service_reply;
1.573 + dbus_uint32_t flags;
1.574 + dbus_bool_t retval;
1.575 + BusRegistry *registry;
1.576 +
1.577 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.578 +
1.579 + registry = bus_connection_get_registry (connection);
1.580 +
1.581 + if (!dbus_message_get_args (message, error,
1.582 + DBUS_TYPE_STRING, &name,
1.583 + DBUS_TYPE_UINT32, &flags,
1.584 + DBUS_TYPE_INVALID))
1.585 + return FALSE;
1.586 +
1.587 + _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
1.588 +
1.589 + retval = FALSE;
1.590 + reply = NULL;
1.591 +
1.592 + _dbus_string_init_const (&service_name, name);
1.593 +
1.594 + if (!bus_registry_acquire_service (registry, connection,
1.595 + &service_name, flags,
1.596 + &service_reply, transaction,
1.597 + error))
1.598 + goto out;
1.599 +
1.600 + reply = dbus_message_new_method_return (message);
1.601 + if (reply == NULL)
1.602 + {
1.603 + BUS_SET_OOM (error);
1.604 + goto out;
1.605 + }
1.606 +
1.607 + if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
1.608 + {
1.609 + BUS_SET_OOM (error);
1.610 + goto out;
1.611 + }
1.612 +
1.613 + if (!bus_transaction_send_from_driver (transaction, connection, reply))
1.614 + {
1.615 + BUS_SET_OOM (error);
1.616 + goto out;
1.617 + }
1.618 +
1.619 + retval = TRUE;
1.620 +
1.621 + out:
1.622 + if (reply)
1.623 + dbus_message_unref (reply);
1.624 + return retval;
1.625 +}
1.626 +
1.627 +static dbus_bool_t
1.628 +bus_driver_handle_release_service (DBusConnection *connection,
1.629 + BusTransaction *transaction,
1.630 + DBusMessage *message,
1.631 + DBusError *error)
1.632 +{
1.633 + DBusMessage *reply;
1.634 + DBusString service_name;
1.635 + const char *name;
1.636 + dbus_uint32_t service_reply;
1.637 + dbus_bool_t retval;
1.638 + BusRegistry *registry;
1.639 +
1.640 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.641 +
1.642 + registry = bus_connection_get_registry (connection);
1.643 +
1.644 + if (!dbus_message_get_args (message, error,
1.645 + DBUS_TYPE_STRING, &name,
1.646 + DBUS_TYPE_INVALID))
1.647 + return FALSE;
1.648 +
1.649 + _dbus_verbose ("Trying to release name %s\n", name);
1.650 +
1.651 + retval = FALSE;
1.652 + reply = NULL;
1.653 +
1.654 + _dbus_string_init_const (&service_name, name);
1.655 +
1.656 + if (!bus_registry_release_service (registry, connection,
1.657 + &service_name, &service_reply,
1.658 + transaction, error))
1.659 + goto out;
1.660 +
1.661 + reply = dbus_message_new_method_return (message);
1.662 + if (reply == NULL)
1.663 + {
1.664 + BUS_SET_OOM (error);
1.665 + goto out;
1.666 + }
1.667 +
1.668 + if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
1.669 + {
1.670 + BUS_SET_OOM (error);
1.671 + goto out;
1.672 + }
1.673 +
1.674 + if (!bus_transaction_send_from_driver (transaction, connection, reply))
1.675 + {
1.676 + BUS_SET_OOM (error);
1.677 + goto out;
1.678 + }
1.679 +
1.680 + retval = TRUE;
1.681 +
1.682 + out:
1.683 + if (reply)
1.684 + dbus_message_unref (reply);
1.685 + return retval;
1.686 +}
1.687 +
1.688 +static dbus_bool_t
1.689 +bus_driver_handle_service_exists (DBusConnection *connection,
1.690 + BusTransaction *transaction,
1.691 + DBusMessage *message,
1.692 + DBusError *error)
1.693 +{
1.694 + DBusMessage *reply;
1.695 + DBusString service_name;
1.696 + BusService *service;
1.697 + dbus_bool_t service_exists;
1.698 + const char *name;
1.699 + dbus_bool_t retval;
1.700 + BusRegistry *registry;
1.701 +
1.702 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.703 +
1.704 + registry = bus_connection_get_registry (connection);
1.705 +
1.706 + if (!dbus_message_get_args (message, error,
1.707 + DBUS_TYPE_STRING, &name,
1.708 + DBUS_TYPE_INVALID))
1.709 + return FALSE;
1.710 +
1.711 + retval = FALSE;
1.712 +
1.713 + if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
1.714 + {
1.715 + service_exists = TRUE;
1.716 + }
1.717 + else
1.718 + {
1.719 + _dbus_string_init_const (&service_name, name);
1.720 + service = bus_registry_lookup (registry, &service_name);
1.721 + service_exists = service != NULL;
1.722 + }
1.723 +
1.724 + reply = dbus_message_new_method_return (message);
1.725 + if (reply == NULL)
1.726 + {
1.727 + BUS_SET_OOM (error);
1.728 + goto out;
1.729 + }
1.730 +
1.731 + if (!dbus_message_append_args (reply,
1.732 + DBUS_TYPE_BOOLEAN, &service_exists,
1.733 + 0))
1.734 + {
1.735 + BUS_SET_OOM (error);
1.736 + goto out;
1.737 + }
1.738 +
1.739 + if (!bus_transaction_send_from_driver (transaction, connection, reply))
1.740 + {
1.741 + BUS_SET_OOM (error);
1.742 + goto out;
1.743 + }
1.744 +
1.745 + retval = TRUE;
1.746 +
1.747 + out:
1.748 + if (reply)
1.749 + dbus_message_unref (reply);
1.750 +
1.751 + return retval;
1.752 +}
1.753 +
1.754 +static dbus_bool_t
1.755 +bus_driver_handle_activate_service (DBusConnection *connection,
1.756 + BusTransaction *transaction,
1.757 + DBusMessage *message,
1.758 + DBusError *error)
1.759 +{
1.760 + dbus_uint32_t flags;
1.761 + const char *name;
1.762 + dbus_bool_t retval;
1.763 + BusActivation *activation;
1.764 +
1.765 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.766 +
1.767 + activation = bus_connection_get_activation (connection);
1.768 +
1.769 + if (!dbus_message_get_args (message, error,
1.770 + DBUS_TYPE_STRING, &name,
1.771 + DBUS_TYPE_UINT32, &flags,
1.772 + DBUS_TYPE_INVALID))
1.773 + {
1.774 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.775 + _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
1.776 + return FALSE;
1.777 + }
1.778 +
1.779 + retval = FALSE;
1.780 +
1.781 + if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
1.782 + message, name, error))
1.783 + {
1.784 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.785 + _dbus_verbose ("bus_activation_activate_service() failed\n");
1.786 + goto out;
1.787 + }
1.788 +
1.789 + retval = TRUE;
1.790 +
1.791 + out:
1.792 + return retval;
1.793 +}
1.794 +
1.795 +static dbus_bool_t
1.796 +send_ack_reply (DBusConnection *connection,
1.797 + BusTransaction *transaction,
1.798 + DBusMessage *message,
1.799 + DBusError *error)
1.800 +{
1.801 + DBusMessage *reply;
1.802 +
1.803 + reply = dbus_message_new_method_return (message);
1.804 + if (reply == NULL)
1.805 + {
1.806 + BUS_SET_OOM (error);
1.807 + return FALSE;
1.808 + }
1.809 +
1.810 + if (!bus_transaction_send_from_driver (transaction, connection, reply))
1.811 + {
1.812 + BUS_SET_OOM (error);
1.813 + dbus_message_unref (reply);
1.814 + return FALSE;
1.815 + }
1.816 +
1.817 + dbus_message_unref (reply);
1.818 +
1.819 + return TRUE;
1.820 +}
1.821 +
1.822 +static dbus_bool_t
1.823 +bus_driver_handle_add_match (DBusConnection *connection,
1.824 + BusTransaction *transaction,
1.825 + DBusMessage *message,
1.826 + DBusError *error)
1.827 +{
1.828 + BusMatchRule *rule;
1.829 + const char *text;
1.830 + DBusString str;
1.831 + BusMatchmaker *matchmaker;
1.832 +
1.833 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.834 +
1.835 + text = NULL;
1.836 + rule = NULL;
1.837 +
1.838 + if (bus_connection_get_n_match_rules (connection) >=
1.839 + bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
1.840 + {
1.841 + dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1.842 + "Connection \"%s\" is not allowed to add more match rules "
1.843 + "(increase limits in configuration file if required)",
1.844 + bus_connection_is_active (connection) ?
1.845 + bus_connection_get_name (connection) :
1.846 + "(inactive)");
1.847 + goto failed;
1.848 + }
1.849 +
1.850 + if (!dbus_message_get_args (message, error,
1.851 + DBUS_TYPE_STRING, &text,
1.852 + DBUS_TYPE_INVALID))
1.853 + {
1.854 + _dbus_verbose ("No memory to get arguments to AddMatch\n");
1.855 + goto failed;
1.856 + }
1.857 +
1.858 + _dbus_string_init_const (&str, text);
1.859 +
1.860 + rule = bus_match_rule_parse (connection, &str, error);
1.861 + if (rule == NULL)
1.862 + goto failed;
1.863 +
1.864 + matchmaker = bus_connection_get_matchmaker (connection);
1.865 +
1.866 + if (!bus_matchmaker_add_rule (matchmaker, rule))
1.867 + {
1.868 + BUS_SET_OOM (error);
1.869 + goto failed;
1.870 + }
1.871 +
1.872 + if (!send_ack_reply (connection, transaction,
1.873 + message, error))
1.874 + {
1.875 + bus_matchmaker_remove_rule (matchmaker, rule);
1.876 + goto failed;
1.877 + }
1.878 +
1.879 + bus_match_rule_unref (rule);
1.880 +
1.881 + return TRUE;
1.882 +
1.883 + failed:
1.884 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.885 + if (rule)
1.886 + bus_match_rule_unref (rule);
1.887 + return FALSE;
1.888 +}
1.889 +
1.890 +static dbus_bool_t
1.891 +bus_driver_handle_remove_match (DBusConnection *connection,
1.892 + BusTransaction *transaction,
1.893 + DBusMessage *message,
1.894 + DBusError *error)
1.895 +{
1.896 + BusMatchRule *rule;
1.897 + const char *text;
1.898 + DBusString str;
1.899 + BusMatchmaker *matchmaker;
1.900 +
1.901 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.902 +
1.903 + text = NULL;
1.904 + rule = NULL;
1.905 +
1.906 + if (!dbus_message_get_args (message, error,
1.907 + DBUS_TYPE_STRING, &text,
1.908 + DBUS_TYPE_INVALID))
1.909 + {
1.910 + _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1.911 + goto failed;
1.912 + }
1.913 +
1.914 + _dbus_string_init_const (&str, text);
1.915 +
1.916 + rule = bus_match_rule_parse (connection, &str, error);
1.917 + if (rule == NULL)
1.918 + goto failed;
1.919 +
1.920 + /* Send the ack before we remove the rule, since the ack is undone
1.921 + * on transaction cancel, but rule removal isn't.
1.922 + */
1.923 + if (!send_ack_reply (connection, transaction,
1.924 + message, error))
1.925 + goto failed;
1.926 +
1.927 + matchmaker = bus_connection_get_matchmaker (connection);
1.928 +
1.929 + if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1.930 + goto failed;
1.931 +
1.932 + bus_match_rule_unref (rule);
1.933 +
1.934 + return TRUE;
1.935 +
1.936 + failed:
1.937 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.938 + if (rule)
1.939 + bus_match_rule_unref (rule);
1.940 + return FALSE;
1.941 +}
1.942 +
1.943 +static dbus_bool_t
1.944 +bus_driver_handle_get_service_owner (DBusConnection *connection,
1.945 + BusTransaction *transaction,
1.946 + DBusMessage *message,
1.947 + DBusError *error)
1.948 +{
1.949 + const char *text;
1.950 + const char *base_name;
1.951 + DBusString str;
1.952 + BusRegistry *registry;
1.953 + BusService *service;
1.954 + DBusMessage *reply;
1.955 +
1.956 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.957 +
1.958 + registry = bus_connection_get_registry (connection);
1.959 +
1.960 + text = NULL;
1.961 + reply = NULL;
1.962 +
1.963 + if (! dbus_message_get_args (message, error,
1.964 + DBUS_TYPE_STRING, &text,
1.965 + DBUS_TYPE_INVALID))
1.966 + goto failed;
1.967 +
1.968 + _dbus_string_init_const (&str, text);
1.969 + service = bus_registry_lookup (registry, &str);
1.970 + if (service == NULL &&
1.971 + _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1.972 + {
1.973 + /* ORG_FREEDESKTOP_DBUS owns itself */
1.974 + base_name = DBUS_SERVICE_DBUS;
1.975 + }
1.976 + else if (service == NULL)
1.977 + {
1.978 + dbus_set_error (error,
1.979 + DBUS_ERROR_NAME_HAS_NO_OWNER,
1.980 + "Could not get owner of name '%s': no such name", text);
1.981 + goto failed;
1.982 + }
1.983 + else
1.984 + {
1.985 + base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1.986 + if (base_name == NULL)
1.987 + {
1.988 + /* FIXME - how is this error possible? */
1.989 + dbus_set_error (error,
1.990 + DBUS_ERROR_FAILED,
1.991 + "Could not determine unique name for '%s'", text);
1.992 + goto failed;
1.993 + }
1.994 + _dbus_assert (*base_name == ':');
1.995 + }
1.996 +
1.997 + _dbus_assert (base_name != NULL);
1.998 +
1.999 + reply = dbus_message_new_method_return (message);
1.1000 + if (reply == NULL)
1.1001 + goto oom;
1.1002 +
1.1003 + if (! dbus_message_append_args (reply,
1.1004 + DBUS_TYPE_STRING, &base_name,
1.1005 + DBUS_TYPE_INVALID))
1.1006 + goto oom;
1.1007 +
1.1008 + if (! bus_transaction_send_from_driver (transaction, connection, reply))
1.1009 + goto oom;
1.1010 +
1.1011 + dbus_message_unref (reply);
1.1012 +
1.1013 + return TRUE;
1.1014 +
1.1015 + oom:
1.1016 + BUS_SET_OOM (error);
1.1017 +
1.1018 + failed:
1.1019 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.1020 + if (reply)
1.1021 + dbus_message_unref (reply);
1.1022 + return FALSE;
1.1023 +}
1.1024 +
1.1025 +static dbus_bool_t
1.1026 +bus_driver_handle_list_queued_owners (DBusConnection *connection,
1.1027 + BusTransaction *transaction,
1.1028 + DBusMessage *message,
1.1029 + DBusError *error)
1.1030 +{
1.1031 + const char *text;
1.1032 + DBusList *base_names;
1.1033 + DBusList *link;
1.1034 + DBusString str;
1.1035 + BusRegistry *registry;
1.1036 + BusService *service;
1.1037 + DBusMessage *reply;
1.1038 + DBusMessageIter iter, array_iter;
1.1039 + char *dbus_service_name = DBUS_SERVICE_DBUS;
1.1040 +
1.1041 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.1042 +
1.1043 + registry = bus_connection_get_registry (connection);
1.1044 +
1.1045 + base_names = NULL;
1.1046 + text = NULL;
1.1047 + reply = NULL;
1.1048 +
1.1049 + if (! dbus_message_get_args (message, error,
1.1050 + DBUS_TYPE_STRING, &text,
1.1051 + DBUS_TYPE_INVALID))
1.1052 + goto failed;
1.1053 +
1.1054 + _dbus_string_init_const (&str, text);
1.1055 + service = bus_registry_lookup (registry, &str);
1.1056 + if (service == NULL &&
1.1057 + _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1.1058 + {
1.1059 + /* ORG_FREEDESKTOP_DBUS owns itself */
1.1060 + if (! _dbus_list_append (&base_names, dbus_service_name))
1.1061 + goto oom;
1.1062 + }
1.1063 + else if (service == NULL)
1.1064 + {
1.1065 + dbus_set_error (error,
1.1066 + DBUS_ERROR_NAME_HAS_NO_OWNER,
1.1067 + "Could not get owners of name '%s': no such name", text);
1.1068 + goto failed;
1.1069 + }
1.1070 + else
1.1071 + {
1.1072 + if (!bus_service_list_queued_owners (service,
1.1073 + &base_names,
1.1074 + error))
1.1075 + goto failed;
1.1076 + }
1.1077 +
1.1078 + _dbus_assert (base_names != NULL);
1.1079 +
1.1080 + reply = dbus_message_new_method_return (message);
1.1081 + if (reply == NULL)
1.1082 + goto oom;
1.1083 +
1.1084 + dbus_message_iter_init_append (reply, &iter);
1.1085 + if (!dbus_message_iter_open_container (&iter,
1.1086 + DBUS_TYPE_ARRAY,
1.1087 + DBUS_TYPE_STRING_AS_STRING,
1.1088 + &array_iter))
1.1089 + goto oom;
1.1090 +
1.1091 + link = _dbus_list_get_first_link (&base_names);
1.1092 + while (link != NULL)
1.1093 + {
1.1094 + char *uname;
1.1095 +
1.1096 + _dbus_assert (link->data != NULL);
1.1097 + uname = (char *)link->data;
1.1098 +
1.1099 + if (!dbus_message_iter_append_basic (&array_iter,
1.1100 + DBUS_TYPE_STRING,
1.1101 + &uname))
1.1102 + goto oom;
1.1103 +
1.1104 + link = _dbus_list_get_next_link (&base_names, link);
1.1105 + }
1.1106 +
1.1107 + if (! dbus_message_iter_close_container (&iter, &array_iter))
1.1108 + goto oom;
1.1109 +
1.1110 +
1.1111 + if (! bus_transaction_send_from_driver (transaction, connection, reply))
1.1112 + goto oom;
1.1113 +
1.1114 + dbus_message_unref (reply);
1.1115 +
1.1116 + return TRUE;
1.1117 +
1.1118 + oom:
1.1119 + BUS_SET_OOM (error);
1.1120 +
1.1121 + failed:
1.1122 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.1123 + if (reply)
1.1124 + dbus_message_unref (reply);
1.1125 +
1.1126 + if (base_names)
1.1127 + _dbus_list_clear (&base_names);
1.1128 +
1.1129 + return FALSE;
1.1130 +}
1.1131 +
1.1132 +static dbus_bool_t
1.1133 +bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1.1134 + BusTransaction *transaction,
1.1135 + DBusMessage *message,
1.1136 + DBusError *error)
1.1137 +{
1.1138 + const char *service;
1.1139 + DBusString str;
1.1140 + BusRegistry *registry;
1.1141 + BusService *serv;
1.1142 + DBusConnection *conn;
1.1143 + DBusMessage *reply;
1.1144 + unsigned long uid;
1.1145 + dbus_uint32_t uid32;
1.1146 +
1.1147 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.1148 +
1.1149 + registry = bus_connection_get_registry (connection);
1.1150 +
1.1151 + service = NULL;
1.1152 + reply = NULL;
1.1153 +
1.1154 + if (! dbus_message_get_args (message, error,
1.1155 + DBUS_TYPE_STRING, &service,
1.1156 + DBUS_TYPE_INVALID))
1.1157 + goto failed;
1.1158 +
1.1159 + _dbus_verbose ("asked for UID of connection %s\n", service);
1.1160 +
1.1161 + _dbus_string_init_const (&str, service);
1.1162 + serv = bus_registry_lookup (registry, &str);
1.1163 + if (serv == NULL)
1.1164 + {
1.1165 + dbus_set_error (error,
1.1166 + DBUS_ERROR_NAME_HAS_NO_OWNER,
1.1167 + "Could not get UID of name '%s': no such name", service);
1.1168 + goto failed;
1.1169 + }
1.1170 +
1.1171 + conn = bus_service_get_primary_owners_connection (serv);
1.1172 +
1.1173 + reply = dbus_message_new_method_return (message);
1.1174 + if (reply == NULL)
1.1175 + goto oom;
1.1176 +
1.1177 + if (!dbus_connection_get_unix_user (conn, &uid))
1.1178 + {
1.1179 + dbus_set_error (error,
1.1180 + DBUS_ERROR_FAILED,
1.1181 + "Could not determine UID for '%s'", service);
1.1182 + goto failed;
1.1183 + }
1.1184 +
1.1185 + uid32 = uid;
1.1186 + if (! dbus_message_append_args (reply,
1.1187 + DBUS_TYPE_UINT32, &uid32,
1.1188 + DBUS_TYPE_INVALID))
1.1189 + goto oom;
1.1190 +
1.1191 + if (! bus_transaction_send_from_driver (transaction, connection, reply))
1.1192 + goto oom;
1.1193 +
1.1194 + dbus_message_unref (reply);
1.1195 +
1.1196 + return TRUE;
1.1197 +
1.1198 + oom:
1.1199 + BUS_SET_OOM (error);
1.1200 +
1.1201 + failed:
1.1202 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.1203 + if (reply)
1.1204 + dbus_message_unref (reply);
1.1205 + return FALSE;
1.1206 +}
1.1207 +
1.1208 +static dbus_bool_t
1.1209 +bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1.1210 + BusTransaction *transaction,
1.1211 + DBusMessage *message,
1.1212 + DBusError *error)
1.1213 +{
1.1214 + const char *service;
1.1215 + DBusString str;
1.1216 + BusRegistry *registry;
1.1217 + BusService *serv;
1.1218 + DBusConnection *conn;
1.1219 + DBusMessage *reply;
1.1220 + unsigned long pid;
1.1221 + dbus_uint32_t pid32;
1.1222 +
1.1223 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.1224 +
1.1225 + registry = bus_connection_get_registry (connection);
1.1226 +
1.1227 + service = NULL;
1.1228 + reply = NULL;
1.1229 +
1.1230 + if (! dbus_message_get_args (message, error,
1.1231 + DBUS_TYPE_STRING, &service,
1.1232 + DBUS_TYPE_INVALID))
1.1233 + goto failed;
1.1234 +
1.1235 + _dbus_verbose ("asked for PID of connection %s\n", service);
1.1236 +
1.1237 + _dbus_string_init_const (&str, service);
1.1238 + serv = bus_registry_lookup (registry, &str);
1.1239 + if (serv == NULL)
1.1240 + {
1.1241 + dbus_set_error (error,
1.1242 + DBUS_ERROR_NAME_HAS_NO_OWNER,
1.1243 + "Could not get PID of name '%s': no such name", service);
1.1244 + goto failed;
1.1245 + }
1.1246 +
1.1247 + conn = bus_service_get_primary_owners_connection (serv);
1.1248 +
1.1249 + reply = dbus_message_new_method_return (message);
1.1250 + if (reply == NULL)
1.1251 + goto oom;
1.1252 +
1.1253 + if (!dbus_connection_get_unix_process_id (conn, &pid))
1.1254 + {
1.1255 + dbus_set_error (error,
1.1256 + DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1.1257 + "Could not determine PID for '%s'", service);
1.1258 + goto failed;
1.1259 + }
1.1260 +
1.1261 + pid32 = pid;
1.1262 + if (! dbus_message_append_args (reply,
1.1263 + DBUS_TYPE_UINT32, &pid32,
1.1264 + DBUS_TYPE_INVALID))
1.1265 + goto oom;
1.1266 +
1.1267 + if (! bus_transaction_send_from_driver (transaction, connection, reply))
1.1268 + goto oom;
1.1269 +
1.1270 + dbus_message_unref (reply);
1.1271 +
1.1272 + return TRUE;
1.1273 +
1.1274 + oom:
1.1275 + BUS_SET_OOM (error);
1.1276 +
1.1277 + failed:
1.1278 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.1279 + if (reply)
1.1280 + dbus_message_unref (reply);
1.1281 + return FALSE;
1.1282 +}
1.1283 +
1.1284 +static dbus_bool_t
1.1285 +bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1.1286 + BusTransaction *transaction,
1.1287 + DBusMessage *message,
1.1288 + DBusError *error)
1.1289 +{
1.1290 + const char *service;
1.1291 + DBusString str;
1.1292 + BusRegistry *registry;
1.1293 + BusService *serv;
1.1294 + DBusConnection *conn;
1.1295 + DBusMessage *reply;
1.1296 + BusSELinuxID *context;
1.1297 +
1.1298 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.1299 +
1.1300 + registry = bus_connection_get_registry (connection);
1.1301 +
1.1302 + service = NULL;
1.1303 + reply = NULL;
1.1304 +
1.1305 + if (! dbus_message_get_args (message, error,
1.1306 + DBUS_TYPE_STRING, &service,
1.1307 + DBUS_TYPE_INVALID))
1.1308 + goto failed;
1.1309 +
1.1310 + _dbus_verbose ("asked for security context of connection %s\n", service);
1.1311 +
1.1312 + _dbus_string_init_const (&str, service);
1.1313 + serv = bus_registry_lookup (registry, &str);
1.1314 + if (serv == NULL)
1.1315 + {
1.1316 + dbus_set_error (error,
1.1317 + DBUS_ERROR_NAME_HAS_NO_OWNER,
1.1318 + "Could not get security context of name '%s': no such name", service);
1.1319 + goto failed;
1.1320 + }
1.1321 +
1.1322 + conn = bus_service_get_primary_owners_connection (serv);
1.1323 +
1.1324 + reply = dbus_message_new_method_return (message);
1.1325 + if (reply == NULL)
1.1326 + goto oom;
1.1327 +
1.1328 + context = bus_connection_get_selinux_id (conn);
1.1329 + if (!context)
1.1330 + {
1.1331 + dbus_set_error (error,
1.1332 + DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1.1333 + "Could not determine security context for '%s'", service);
1.1334 + goto failed;
1.1335 + }
1.1336 +
1.1337 + if (! bus_selinux_append_context (reply, context, error))
1.1338 + goto failed;
1.1339 +
1.1340 + if (! bus_transaction_send_from_driver (transaction, connection, reply))
1.1341 + goto oom;
1.1342 +
1.1343 + dbus_message_unref (reply);
1.1344 +
1.1345 + return TRUE;
1.1346 +
1.1347 + oom:
1.1348 + BUS_SET_OOM (error);
1.1349 +
1.1350 + failed:
1.1351 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.1352 + if (reply)
1.1353 + dbus_message_unref (reply);
1.1354 + return FALSE;
1.1355 +}
1.1356 +
1.1357 +static dbus_bool_t
1.1358 +bus_driver_handle_reload_config (DBusConnection *connection,
1.1359 + BusTransaction *transaction,
1.1360 + DBusMessage *message,
1.1361 + DBusError *error)
1.1362 +{
1.1363 + BusContext *context;
1.1364 + DBusMessage *reply;
1.1365 +
1.1366 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.1367 +
1.1368 + reply = NULL;
1.1369 +
1.1370 + context = bus_connection_get_context (connection);
1.1371 + if (!bus_context_reload_config (context, error))
1.1372 + goto failed;
1.1373 +
1.1374 + reply = dbus_message_new_method_return (message);
1.1375 + if (reply == NULL)
1.1376 + goto oom;
1.1377 +
1.1378 + if (! bus_transaction_send_from_driver (transaction, connection, reply))
1.1379 + goto oom;
1.1380 +
1.1381 + dbus_message_unref (reply);
1.1382 + return TRUE;
1.1383 +
1.1384 + oom:
1.1385 + BUS_SET_OOM (error);
1.1386 +
1.1387 + failed:
1.1388 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.1389 + if (reply)
1.1390 + dbus_message_unref (reply);
1.1391 + return FALSE;
1.1392 +}
1.1393 +
1.1394 +/* For speed it might be useful to sort this in order of
1.1395 + * frequency of use (but doesn't matter with only a few items
1.1396 + * anyhow)
1.1397 + */
1.1398 +struct
1.1399 +{
1.1400 + const char *name;
1.1401 + const char *in_args;
1.1402 + const char *out_args;
1.1403 + dbus_bool_t (* handler) (DBusConnection *connection,
1.1404 + BusTransaction *transaction,
1.1405 + DBusMessage *message,
1.1406 + DBusError *error);
1.1407 +} message_handlers[] = {
1.1408 + { "RequestName",
1.1409 + DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1.1410 + DBUS_TYPE_UINT32_AS_STRING,
1.1411 + bus_driver_handle_acquire_service },
1.1412 + { "ReleaseName",
1.1413 + DBUS_TYPE_STRING_AS_STRING,
1.1414 + DBUS_TYPE_UINT32_AS_STRING,
1.1415 + bus_driver_handle_release_service },
1.1416 + { "StartServiceByName",
1.1417 + DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1.1418 + DBUS_TYPE_UINT32_AS_STRING,
1.1419 + bus_driver_handle_activate_service },
1.1420 + { "Hello",
1.1421 + "",
1.1422 + DBUS_TYPE_STRING_AS_STRING,
1.1423 + bus_driver_handle_hello },
1.1424 + { "NameHasOwner",
1.1425 + DBUS_TYPE_STRING_AS_STRING,
1.1426 + DBUS_TYPE_BOOLEAN_AS_STRING,
1.1427 + bus_driver_handle_service_exists },
1.1428 + { "ListNames",
1.1429 + "",
1.1430 + DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1.1431 + bus_driver_handle_list_services },
1.1432 + { "ListActivatableNames",
1.1433 + "",
1.1434 + DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1.1435 + bus_driver_handle_list_activatable_services },
1.1436 + { "AddMatch",
1.1437 + DBUS_TYPE_STRING_AS_STRING,
1.1438 + "",
1.1439 + bus_driver_handle_add_match },
1.1440 + { "RemoveMatch",
1.1441 + DBUS_TYPE_STRING_AS_STRING,
1.1442 + "",
1.1443 + bus_driver_handle_remove_match },
1.1444 + { "GetNameOwner",
1.1445 + DBUS_TYPE_STRING_AS_STRING,
1.1446 + DBUS_TYPE_STRING_AS_STRING,
1.1447 + bus_driver_handle_get_service_owner },
1.1448 + { "ListQueuedOwners",
1.1449 + DBUS_TYPE_STRING_AS_STRING,
1.1450 + DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1.1451 + bus_driver_handle_list_queued_owners },
1.1452 + { "GetConnectionUnixUser",
1.1453 + DBUS_TYPE_STRING_AS_STRING,
1.1454 + DBUS_TYPE_UINT32_AS_STRING,
1.1455 + bus_driver_handle_get_connection_unix_user },
1.1456 + { "GetConnectionUnixProcessID",
1.1457 + DBUS_TYPE_STRING_AS_STRING,
1.1458 + DBUS_TYPE_UINT32_AS_STRING,
1.1459 + bus_driver_handle_get_connection_unix_process_id },
1.1460 + { "GetConnectionSELinuxSecurityContext",
1.1461 + DBUS_TYPE_STRING_AS_STRING,
1.1462 + DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1.1463 + bus_driver_handle_get_connection_selinux_security_context },
1.1464 + { "ReloadConfig",
1.1465 + "",
1.1466 + "",
1.1467 + bus_driver_handle_reload_config }
1.1468 +};
1.1469 +
1.1470 +static dbus_bool_t
1.1471 +write_args_for_direction (DBusString *xml,
1.1472 + const char *signature,
1.1473 + dbus_bool_t in)
1.1474 +{
1.1475 + DBusTypeReader typereader;
1.1476 + DBusString sigstr;
1.1477 + int current_type;
1.1478 +
1.1479 + _dbus_string_init_const (&sigstr, signature);
1.1480 + _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1.1481 +
1.1482 + while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1.1483 + {
1.1484 + const DBusString *subsig;
1.1485 + int start, len;
1.1486 +
1.1487 + _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1.1488 + if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
1.1489 + in ? "in" : "out"))
1.1490 + goto oom;
1.1491 + if (!_dbus_string_append_len (xml,
1.1492 + _dbus_string_get_const_data (subsig) + start,
1.1493 + len))
1.1494 + goto oom;
1.1495 + if (!_dbus_string_append (xml, "\"/>\n"))
1.1496 + goto oom;
1.1497 +
1.1498 + _dbus_type_reader_next (&typereader);
1.1499 + }
1.1500 + return TRUE;
1.1501 + oom:
1.1502 + return FALSE;
1.1503 +}
1.1504 +
1.1505 +dbus_bool_t
1.1506 +bus_driver_generate_introspect_string (DBusString *xml)
1.1507 +{
1.1508 + int i;
1.1509 +
1.1510 + if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
1.1511 + return FALSE;
1.1512 + if (!_dbus_string_append (xml, "<node>\n"))
1.1513 + return FALSE;
1.1514 + if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n", DBUS_INTERFACE_INTROSPECTABLE))
1.1515 + return FALSE;
1.1516 + if (!_dbus_string_append (xml, " <method name=\"Introspect\">\n"))
1.1517 + return FALSE;
1.1518 + if (!_dbus_string_append_printf (xml, " <arg name=\"data\" direction=\"out\" type=\"%s\"/>\n", DBUS_TYPE_STRING_AS_STRING))
1.1519 + return FALSE;
1.1520 + if (!_dbus_string_append (xml, " </method>\n"))
1.1521 + return FALSE;
1.1522 + if (!_dbus_string_append (xml, " </interface>\n"))
1.1523 + return FALSE;
1.1524 +
1.1525 + if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
1.1526 + DBUS_INTERFACE_DBUS))
1.1527 + return FALSE;
1.1528 +
1.1529 + i = 0;
1.1530 + while (i < _DBUS_N_ELEMENTS (message_handlers))
1.1531 + {
1.1532 +
1.1533 + if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
1.1534 + message_handlers[i].name))
1.1535 + return FALSE;
1.1536 +
1.1537 + if (!write_args_for_direction (xml, message_handlers[i].in_args, TRUE))
1.1538 + return FALSE;
1.1539 +
1.1540 + if (!write_args_for_direction (xml, message_handlers[i].out_args, FALSE))
1.1541 + return FALSE;
1.1542 +
1.1543 + if (!_dbus_string_append (xml, " </method>\n"))
1.1544 + return FALSE;
1.1545 +
1.1546 + ++i;
1.1547 + }
1.1548 +
1.1549 + if (!_dbus_string_append_printf (xml, " <signal name=\"NameOwnerChanged\">\n"))
1.1550 + return FALSE;
1.1551 +
1.1552 + if (!_dbus_string_append_printf (xml, " <arg type=\"s\"/>\n"))
1.1553 + return FALSE;
1.1554 +
1.1555 + if (!_dbus_string_append_printf (xml, " <arg type=\"s\"/>\n"))
1.1556 + return FALSE;
1.1557 +
1.1558 + if (!_dbus_string_append_printf (xml, " <arg type=\"s\"/>\n"))
1.1559 + return FALSE;
1.1560 +
1.1561 + if (!_dbus_string_append_printf (xml, " </signal>\n"))
1.1562 + return FALSE;
1.1563 +
1.1564 +
1.1565 +
1.1566 + if (!_dbus_string_append_printf (xml, " <signal name=\"NameLost\">\n"))
1.1567 + return FALSE;
1.1568 +
1.1569 + if (!_dbus_string_append_printf (xml, " <arg type=\"s\"/>\n"))
1.1570 + return FALSE;
1.1571 +
1.1572 + if (!_dbus_string_append_printf (xml, " </signal>\n"))
1.1573 + return FALSE;
1.1574 +
1.1575 +
1.1576 +
1.1577 + if (!_dbus_string_append_printf (xml, " <signal name=\"NameAcquired\">\n"))
1.1578 + return FALSE;
1.1579 +
1.1580 + if (!_dbus_string_append_printf (xml, " <arg type=\"s\"/>\n"))
1.1581 + return FALSE;
1.1582 +
1.1583 + if (!_dbus_string_append_printf (xml, " </signal>\n"))
1.1584 + return FALSE;
1.1585 +
1.1586 + if (!_dbus_string_append (xml, " </interface>\n"))
1.1587 + return FALSE;
1.1588 +
1.1589 + if (!_dbus_string_append (xml, "</node>\n"))
1.1590 + return FALSE;
1.1591 +
1.1592 + return TRUE;
1.1593 +}
1.1594 +
1.1595 +static dbus_bool_t
1.1596 +bus_driver_handle_introspect (DBusConnection *connection,
1.1597 + BusTransaction *transaction,
1.1598 + DBusMessage *message,
1.1599 + DBusError *error)
1.1600 +{
1.1601 + DBusString xml;
1.1602 + DBusMessage *reply;
1.1603 + const char *v_STRING;
1.1604 +
1.1605 + _dbus_verbose ("Introspect() on bus driver\n");
1.1606 +
1.1607 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.1608 +
1.1609 + reply = NULL;
1.1610 +
1.1611 + if (! dbus_message_get_args (message, error,
1.1612 + DBUS_TYPE_INVALID))
1.1613 + {
1.1614 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.1615 + return FALSE;
1.1616 + }
1.1617 +
1.1618 + if (!_dbus_string_init (&xml))
1.1619 + {
1.1620 + BUS_SET_OOM (error);
1.1621 + return FALSE;
1.1622 + }
1.1623 +
1.1624 + if (!bus_driver_generate_introspect_string (&xml))
1.1625 + goto oom;
1.1626 +
1.1627 + v_STRING = _dbus_string_get_const_data (&xml);
1.1628 +
1.1629 + reply = dbus_message_new_method_return (message);
1.1630 + if (reply == NULL)
1.1631 + goto oom;
1.1632 +
1.1633 + if (! dbus_message_append_args (reply,
1.1634 + DBUS_TYPE_STRING, &v_STRING,
1.1635 + DBUS_TYPE_INVALID))
1.1636 + goto oom;
1.1637 +
1.1638 + if (! bus_transaction_send_from_driver (transaction, connection, reply))
1.1639 + goto oom;
1.1640 +
1.1641 + dbus_message_unref (reply);
1.1642 + _dbus_string_free (&xml);
1.1643 +
1.1644 + return TRUE;
1.1645 +
1.1646 + oom:
1.1647 + BUS_SET_OOM (error);
1.1648 +
1.1649 + if (reply)
1.1650 + dbus_message_unref (reply);
1.1651 +
1.1652 + _dbus_string_free (&xml);
1.1653 +
1.1654 + return FALSE;
1.1655 +}
1.1656 +
1.1657 +dbus_bool_t
1.1658 +bus_driver_handle_message (DBusConnection *connection,
1.1659 + BusTransaction *transaction,
1.1660 + DBusMessage *message,
1.1661 + DBusError *error)
1.1662 +{
1.1663 + const char *name, *sender, *interface;
1.1664 + int i;
1.1665 +
1.1666 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.1667 +
1.1668 + if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
1.1669 + {
1.1670 + _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
1.1671 + return TRUE; /* we just ignore this */
1.1672 + }
1.1673 +
1.1674 + if (dbus_message_is_method_call (message,
1.1675 + DBUS_INTERFACE_INTROSPECTABLE,
1.1676 + "Introspect"))
1.1677 + return bus_driver_handle_introspect (connection, transaction, message, error);
1.1678 +
1.1679 + interface = dbus_message_get_interface (message);
1.1680 + if (interface == NULL)
1.1681 + interface = DBUS_INTERFACE_DBUS;
1.1682 +
1.1683 + _dbus_assert (dbus_message_get_member (message) != NULL);
1.1684 +
1.1685 + name = dbus_message_get_member (message);
1.1686 + sender = dbus_message_get_sender (message);
1.1687 +
1.1688 + if (strcmp (interface,
1.1689 + DBUS_INTERFACE_DBUS) != 0)
1.1690 + {
1.1691 + _dbus_verbose ("Driver got message to unknown interface \"%s\"\n",
1.1692 + interface);
1.1693 + goto unknown;
1.1694 + }
1.1695 +
1.1696 + _dbus_verbose ("Driver got a method call: %s\n",
1.1697 + dbus_message_get_member (message));
1.1698 +
1.1699 + /* security checks should have kept this from getting here */
1.1700 + _dbus_assert (sender != NULL || strcmp (name, "Hello") == 0);
1.1701 +
1.1702 + i = 0;
1.1703 + while (i < _DBUS_N_ELEMENTS (message_handlers))
1.1704 + {
1.1705 + if (strcmp (message_handlers[i].name, name) == 0)
1.1706 + {
1.1707 + _dbus_verbose ("Found driver handler for %s\n", name);
1.1708 +
1.1709 + if (!dbus_message_has_signature (message, message_handlers[i].in_args))
1.1710 + {
1.1711 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.1712 + _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
1.1713 + name, dbus_message_get_signature (message),
1.1714 + message_handlers[i].in_args);
1.1715 +
1.1716 + dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1.1717 + "Call to %s has wrong args (%s, expected %s)\n",
1.1718 + name, dbus_message_get_signature (message),
1.1719 + message_handlers[i].in_args);
1.1720 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.1721 + return FALSE;
1.1722 + }
1.1723 +
1.1724 + if ((* message_handlers[i].handler) (connection, transaction, message, error))
1.1725 + {
1.1726 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1.1727 + _dbus_verbose ("Driver handler succeeded\n");
1.1728 + return TRUE;
1.1729 + }
1.1730 + else
1.1731 + {
1.1732 + _DBUS_ASSERT_ERROR_IS_SET (error);
1.1733 + _dbus_verbose ("Driver handler returned failure\n");
1.1734 + return FALSE;
1.1735 + }
1.1736 + }
1.1737 +
1.1738 + ++i;
1.1739 + }
1.1740 +
1.1741 + unknown:
1.1742 + _dbus_verbose ("No driver handler for message \"%s\"\n",
1.1743 + name);
1.1744 +
1.1745 + dbus_set_error (error, DBUS_ERROR_UNKNOWN_METHOD,
1.1746 + "%s does not understand message %s",
1.1747 + DBUS_SERVICE_DBUS, name);
1.1748 +
1.1749 + return FALSE;
1.1750 +}
1.1751 +
1.1752 +void
1.1753 +bus_driver_remove_connection (DBusConnection *connection)
1.1754 +{
1.1755 + /* FIXME 1.0 Does nothing for now, should unregister the connection
1.1756 + * with the bus driver.
1.1757 + */
1.1758 +}