driver.c 65.9 KB
Newer Older
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 10 11 12 13 14 15 16 17
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
18
 *
19 20
 * You should have received a copy of the GNU General Public License
 * 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 "apparmor.h"
28 29
#include "connection.h"
#include "driver.h"
30
#include "dispatch.h"
31
#include "services.h"
32
#include "selinux.h"
33
#include "signals.h"
34
#include "stats.h"
35
#include "utils.h"
36

37
#include <dbus/dbus-asv-util.h>
38
#include <dbus/dbus-connection-internal.h>
39
#include <dbus/dbus-string.h>
40
#include <dbus/dbus-internals.h>
41
#include <dbus/dbus-message.h>
42
#include <dbus/dbus-marshal-recursive.h>
43
#include <dbus/dbus-marshal-validate.h>
44 45
#include <string.h>

46 47 48 49 50 51 52
static inline const char *
nonnull (const char *maybe_null,
         const char *if_null)
{
  return (maybe_null ? maybe_null : if_null);
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static DBusConnection *
bus_driver_get_owner_of_name (DBusConnection *connection,
                              const char     *name)
{
  BusRegistry *registry;
  BusService *serv;
  DBusString str;

  registry = bus_connection_get_registry (connection);
  _dbus_string_init_const (&str, name);
  serv = bus_registry_lookup (registry, &str);

  if (serv == NULL)
    return NULL;

  return bus_service_get_primary_owners_connection (serv);
}

71 72 73 74 75 76 77 78
static DBusConnection *
bus_driver_get_conn_helper (DBusConnection  *connection,
                            DBusMessage     *message,
                            const char      *what_we_want,
                            const char     **name_p,
                            DBusError       *error)
{
  DBusConnection *conn;
79
  const char *name;
80 81 82 83 84 85 86 87 88

  if (!dbus_message_get_args (message, error,
                              DBUS_TYPE_STRING, &name,
                              DBUS_TYPE_INVALID))
    return NULL;

  _dbus_assert (name != NULL);
  _dbus_verbose ("asked for %s of connection %s\n", what_we_want, name);

89
  conn = bus_driver_get_owner_of_name (connection, name);
90

91
  if (conn == NULL)
92 93 94 95 96 97 98 99 100 101 102 103 104
    {
      dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
                      "Could not get %s of name '%s': no such name",
                      what_we_want, name);
      return NULL;
    }

  if (name_p != NULL)
    *name_p = name;

  return conn;
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/*
 * Log a security warning and set error unless the uid of the connection
 * is either the uid of this process, or on Unix, uid 0 (root).
 *
 * This is intended to be a second line of defence after <deny> rules,
 * to mitigate incorrect system bus security policy configuration files
 * like the ones in CVE-2014-8148 and CVE-2014-8156, and (if present)
 * LSM rules; so it doesn't need to be perfect, but as long as we have
 * potentially dangerous functionality in the system bus, it does need
 * to exist.
 */
static dbus_bool_t
bus_driver_check_caller_is_privileged (DBusConnection *connection,
                                       BusTransaction *transaction,
                                       DBusMessage    *message,
                                       DBusError      *error)
{
#ifdef DBUS_UNIX
  unsigned long uid;

  if (!dbus_connection_get_unix_user (connection, &uid))
    {
      const char *method = dbus_message_get_member (message);

129 130
      bus_context_log_and_set_error (bus_transaction_get_context (transaction),
          DBUS_SYSTEM_LOG_SECURITY, error, DBUS_ERROR_ACCESS_DENIED,
131 132 133 134
          "rejected attempt to call %s by connection %s (%s) with "
          "unknown uid", method,
          nonnull (bus_connection_get_name (connection), "(inactive)"),
          bus_connection_get_loginfo (connection));
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
      return FALSE;
    }

  /* I'm writing it in this slightly strange form so that it's more
   * obvious that this security-sensitive code is correct.
   */
  if (_dbus_unix_user_is_process_owner (uid))
    {
      /* OK */
    }
  else if (uid == 0)
    {
      /* OK */
    }
  else
    {
      const char *method = dbus_message_get_member (message);

153 154
      bus_context_log_and_set_error (bus_transaction_get_context (transaction),
          DBUS_SYSTEM_LOG_SECURITY, error, DBUS_ERROR_ACCESS_DENIED,
155 156 157 158
          "rejected attempt to call %s by connection %s (%s) with "
          "uid %lu", method,
          nonnull (bus_connection_get_name (connection), "(inactive)"),
          bus_connection_get_loginfo (connection), uid);
159 160 161 162
      return FALSE;
    }

  return TRUE;
163
#elif defined(DBUS_WIN)
164 165 166 167 168 169 170
  char *windows_sid = NULL;
  dbus_bool_t ret = FALSE;

  if (!dbus_connection_get_windows_user (connection, &windows_sid))
    {
      const char *method = dbus_message_get_member (message);

171 172
      bus_context_log_and_set_error (bus_transaction_get_context (transaction),
          DBUS_SYSTEM_LOG_SECURITY, error, DBUS_ERROR_ACCESS_DENIED,
173 174 175 176 177 178 179 180
          "rejected attempt to call %s by unknown uid", method);
      goto out;
    }

  if (!_dbus_windows_user_is_process_owner (windows_sid))
    {
      const char *method = dbus_message_get_member (message);

181 182
      bus_context_log_and_set_error (bus_transaction_get_context (transaction),
          DBUS_SYSTEM_LOG_SECURITY, error, DBUS_ERROR_ACCESS_DENIED,
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
          "rejected attempt to call %s by uid %s", method, windows_sid);
      goto out;
    }

  ret = TRUE;
out:
  dbus_free (windows_sid);
  return ret;
#else
  /* make sure we fail closed in the hypothetical case that we are neither
   * Unix nor Windows */
  dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
      "please teach bus/driver.c how uids work on this platform");
  return FALSE;
#endif
}

200 201 202 203 204 205
static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
                                                    DBusMessage    *hello_message,
                                                    BusTransaction *transaction,
                                                    DBusError      *error);

dbus_bool_t
206 207 208 209 210
bus_driver_send_service_owner_changed (const char     *service_name,
				       const char     *old_owner,
				       const char     *new_owner,
				       BusTransaction *transaction,
				       DBusError      *error)
211
{
212
  DBusMessage *message;
213
  dbus_bool_t retval;
214
  const char *null_service;
215 216

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
217 218

  null_service = "";
219
  _dbus_verbose ("sending name owner changed: %s [%s -> %s]\n",
220 221
                 service_name,
                 old_owner ? old_owner : null_service,
222
                 new_owner ? new_owner : null_service);
223

224 225
  message = dbus_message_new_signal (DBUS_PATH_DBUS,
                                     DBUS_INTERFACE_DBUS,
226
                                     "NameOwnerChanged");
227

228 229 230 231 232
  if (message == NULL)
    {
      BUS_SET_OOM (error);
      return FALSE;
    }
233

234
  if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
235 236 237
    goto oom;

  if (!dbus_message_append_args (message,
238 239 240
                                 DBUS_TYPE_STRING, &service_name,
                                 DBUS_TYPE_STRING, old_owner ? &old_owner : &null_service,
                                 DBUS_TYPE_STRING, new_owner ? &new_owner : &null_service,
241
                                 DBUS_TYPE_INVALID))
242
    goto oom;
243

244
  _dbus_assert (dbus_message_has_signature (message, "sss"));
245

246
  if (!bus_transaction_capture (transaction, NULL, NULL, message))
247 248
    goto oom;

249
  retval = bus_dispatch_matches (transaction, NULL, NULL, message, error);
250 251 252
  dbus_message_unref (message);

  return retval;
253

254
 oom:
255
  dbus_message_unref (message);
256 257
  BUS_SET_OOM (error);
  return FALSE;
258 259
}

260
dbus_bool_t
261
bus_driver_send_service_lost (DBusConnection *connection,
262 263 264
			      const char     *service_name,
                              BusTransaction *transaction,
                              DBusError      *error)
265 266 267
{
  DBusMessage *message;

268
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
269

270 271
  message = dbus_message_new_signal (DBUS_PATH_DBUS,
                                     DBUS_INTERFACE_DBUS,
272
                                     "NameLost");
273

274 275 276 277 278
  if (message == NULL)
    {
      BUS_SET_OOM (error);
      return FALSE;
    }
279

280 281
  if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
      !dbus_message_append_args (message,
282
                                 DBUS_TYPE_STRING, &service_name,
283
                                 DBUS_TYPE_INVALID))
284 285 286 287 288 289
    {
      dbus_message_unref (message);
      BUS_SET_OOM (error);
      return FALSE;
    }

290
  if (!bus_transaction_send_from_driver (transaction, connection, message))
291 292 293 294 295 296 297 298 299 300
    {
      dbus_message_unref (message);
      BUS_SET_OOM (error);
      return FALSE;
    }
  else
    {
      dbus_message_unref (message);
      return TRUE;
    }
301 302
}

303
dbus_bool_t
304
bus_driver_send_service_acquired (DBusConnection *connection,
305 306 307
                                  const char     *service_name,
                                  BusTransaction *transaction,
                                  DBusError      *error)
308 309 310
{
  DBusMessage *message;

311
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
312

313 314
  message = dbus_message_new_signal (DBUS_PATH_DBUS,
                                     DBUS_INTERFACE_DBUS,
315
                                     "NameAcquired");
316

317 318 319 320 321
  if (message == NULL)
    {
      BUS_SET_OOM (error);
      return FALSE;
    }
322

323 324
  if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
      !dbus_message_append_args (message,
325
                                 DBUS_TYPE_STRING, &service_name,
326
                                 DBUS_TYPE_INVALID))
327 328 329 330 331 332
    {
      dbus_message_unref (message);
      BUS_SET_OOM (error);
      return FALSE;
    }

333
  if (!bus_transaction_send_from_driver (transaction, connection, message))
334 335 336 337 338 339 340 341 342 343
    {
      dbus_message_unref (message);
      BUS_SET_OOM (error);
      return FALSE;
    }
  else
    {
      dbus_message_unref (message);
      return TRUE;
    }
344 345
}

346
static dbus_bool_t
347 348
create_unique_client_name (BusRegistry *registry,
                           DBusString  *str)
349
{
350 351 352 353 354 355
  /* We never want to use the same unique client name twice, because
   * we want to guarantee that if you send a message to a given unique
   * name, you always get the same application. So we use two numbers
   * for INT_MAX * INT_MAX combinations, should be pretty safe against
   * wraparound.
   */
356
  /* FIXME these should be in BusRegistry rather than static vars */
357 358 359
  static int next_major_number = 0;
  static int next_minor_number = 0;
  int len;
360

361
  len = _dbus_string_get_length (str);
362

363
  while (TRUE)
364
    {
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
      /* start out with 1-0, go to 1-1, 1-2, 1-3,
       * up to 1-MAXINT, then 2-0, 2-1, etc.
       */
      if (next_minor_number <= 0)
        {
          next_major_number += 1;
          next_minor_number = 0;
          if (next_major_number <= 0)
            _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
        }

      _dbus_assert (next_major_number > 0);
      _dbus_assert (next_minor_number >= 0);

      /* appname:MAJOR-MINOR */
380

381 382
      if (!_dbus_string_append (str, ":"))
        return FALSE;
383

384 385 386
      if (!_dbus_string_append_int (str, next_major_number))
        return FALSE;

387
      if (!_dbus_string_append (str, "."))
388
        return FALSE;
389

390 391 392 393
      if (!_dbus_string_append_int (str, next_minor_number))
        return FALSE;

      next_minor_number += 1;
394

395
      /* Check if a client with the name exists */
396
      if (bus_registry_lookup (registry, str) == NULL)
397 398
	break;

399
      /* drop the number again, try the next one. */
400 401 402 403 404 405
      _dbus_string_set_length (str, len);
    }

  return TRUE;
}

406
static dbus_bool_t
407
bus_driver_handle_hello (DBusConnection *connection,
408 409 410
                         BusTransaction *transaction,
                         DBusMessage    *message,
                         DBusError      *error)
411 412 413
{
  DBusString unique_name;
  BusService *service;
414
  dbus_bool_t retval;
415
  BusRegistry *registry;
416
  BusConnections *connections;
417 418

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
419

420 421 422 423 424 425 426 427
  if (bus_connection_is_active (connection))
    {
      /* We already handled an Hello message for this connection. */
      dbus_set_error (error, DBUS_ERROR_FAILED,
                      "Already handled an Hello message");
      return FALSE;
    }

428 429 430 431 432 433 434 435 436 437 438 439 440
  /* Note that when these limits are exceeded we don't disconnect the
   * connection; we just sort of leave it hanging there until it times
   * out or disconnects itself or is dropped due to the max number of
   * incomplete connections. It's even OK if the connection wants to
   * retry the hello message, we support that.
   */
  connections = bus_connection_get_connections (connection);
  if (!bus_connections_check_limits (connections, connection,
                                     error))
    {
      _DBUS_ASSERT_ERROR_IS_SET (error);
      return FALSE;
    }
441

442
  if (!_dbus_string_init (&unique_name))
443 444 445 446
    {
      BUS_SET_OOM (error);
      return FALSE;
    }
447

448
  retval = FALSE;
449 450

  registry = bus_connection_get_registry (connection);
451

452
  if (!create_unique_client_name (registry, &unique_name))
453 454 455 456
    {
      BUS_SET_OOM (error);
      goto out_0;
    }
457

458
  if (!bus_connection_complete (connection, &unique_name, error))
459
    {
460
      _DBUS_ASSERT_ERROR_IS_SET (error);
461 462
      goto out_0;
    }
463

464 465 466 467 468 469
  if (!dbus_message_set_sender (message,
                                bus_connection_get_name (connection)))
    {
      BUS_SET_OOM (error);
      goto out_0;
    }
470

471 472
  if (!bus_driver_send_welcome_message (connection, message, transaction, error))
    goto out_0;
473

474
  /* Create the service */
475
  service = bus_registry_ensure (registry,
476
                                 &unique_name, connection, 0, transaction, error);
477 478
  if (service == NULL)
    goto out_0;
479

480
  _dbus_assert (bus_connection_is_active (connection));
481
  retval = TRUE;
482

483
 out_0:
484
  _dbus_string_free (&unique_name);
485
  return retval;
486 487
}

488
static dbus_bool_t
489
bus_driver_send_welcome_message (DBusConnection *connection,
490 491 492
                                 DBusMessage    *hello_message,
                                 BusTransaction *transaction,
                                 DBusError      *error)
493 494 495
{
  DBusMessage *welcome;
  const char *name;
496 497

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
498

499 500
  name = bus_connection_get_name (connection);
  _dbus_assert (name != NULL);
501

502
  welcome = dbus_message_new_method_return (hello_message);
503 504 505 506 507
  if (welcome == NULL)
    {
      BUS_SET_OOM (error);
      return FALSE;
    }
508

509
  if (!dbus_message_append_args (welcome,
510
                                 DBUS_TYPE_STRING, &name,
511
                                 DBUS_TYPE_INVALID))
512 513 514 515 516 517
    {
      dbus_message_unref (welcome);
      BUS_SET_OOM (error);
      return FALSE;
    }

518
  _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
519

520
  if (!bus_transaction_send_from_driver (transaction, connection, welcome))
521 522 523 524 525 526 527 528 529 530
    {
      dbus_message_unref (welcome);
      BUS_SET_OOM (error);
      return FALSE;
    }
  else
    {
      dbus_message_unref (welcome);
      return TRUE;
    }
531 532
}

533
static dbus_bool_t
534
bus_driver_handle_list_services (DBusConnection *connection,
535 536 537
                                 BusTransaction *transaction,
                                 DBusMessage    *message,
                                 DBusError      *error)
538 539
{
  DBusMessage *reply;
540
  int len;
541
  char **services;
542
  BusRegistry *registry;
543 544 545
  int i;
  DBusMessageIter iter;
  DBusMessageIter sub;
546 547

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
548

549
  registry = bus_connection_get_registry (connection);
550

551
  reply = dbus_message_new_method_return (message);
552 553 554 555 556
  if (reply == NULL)
    {
      BUS_SET_OOM (error);
      return FALSE;
    }
557

558
  if (!bus_registry_list_services (registry, &services, &len))
559 560 561 562 563
    {
      dbus_message_unref (reply);
      BUS_SET_OOM (error);
      return FALSE;
    }
564

565
  dbus_message_iter_init_append (reply, &iter);
566

567 568 569
  if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
                                         DBUS_TYPE_STRING_AS_STRING,
                                         &sub))
570 571 572 573 574 575
    {
      dbus_free_string_array (services);
      dbus_message_unref (reply);
      BUS_SET_OOM (error);
      return FALSE;
    }
576

577 578
  {
    /* Include the bus driver in the list */
579
    const char *v_STRING = DBUS_SERVICE_DBUS;
580 581 582 583 584 585 586 587 588
    if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
                                         &v_STRING))
      {
        dbus_free_string_array (services);
        dbus_message_unref (reply);
        BUS_SET_OOM (error);
        return FALSE;
      }
  }
589

590 591 592 593 594 595 596 597 598 599 600 601 602 603
  i = 0;
  while (i < len)
    {
      if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
                                           &services[i]))
        {
          dbus_free_string_array (services);
          dbus_message_unref (reply);
          BUS_SET_OOM (error);
          return FALSE;
        }
      ++i;
    }

604 605
  dbus_free_string_array (services);

606 607 608 609 610 611
  if (!dbus_message_iter_close_container (&iter, &sub))
    {
      dbus_message_unref (reply);
      BUS_SET_OOM (error);
      return FALSE;
    }
612

613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
  if (!bus_transaction_send_from_driver (transaction, connection, reply))
    {
      dbus_message_unref (reply);
      BUS_SET_OOM (error);
      return FALSE;
    }
  else
    {
      dbus_message_unref (reply);
      return TRUE;
    }
}

static dbus_bool_t
bus_driver_handle_list_activatable_services (DBusConnection *connection,
					     BusTransaction *transaction,
					     DBusMessage    *message,
					     DBusError      *error)
{
  DBusMessage *reply;
  int len;
  char **services;
  BusActivation *activation;
  int i;
  DBusMessageIter iter;
  DBusMessageIter sub;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  activation = bus_connection_get_activation (connection);

  reply = dbus_message_new_method_return (message);
  if (reply == NULL)
    {
      BUS_SET_OOM (error);
      return FALSE;
    }

  if (!bus_activation_list_services (activation, &services, &len))
    {
      dbus_message_unref (reply);
      BUS_SET_OOM (error);
      return FALSE;
    }

  dbus_message_iter_init_append (reply, &iter);

  if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
					 DBUS_TYPE_STRING_AS_STRING,
					 &sub))
    {
      dbus_free_string_array (services);
      dbus_message_unref (reply);
      BUS_SET_OOM (error);
      return FALSE;
    }

  {
    /* Include the bus driver in the list */
    const char *v_STRING = DBUS_SERVICE_DBUS;
    if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
					 &v_STRING))
      {
	dbus_free_string_array (services);
	dbus_message_unref (reply);
	BUS_SET_OOM (error);
	return FALSE;
      }
  }

  i = 0;
  while (i < len)
    {
      if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
					   &services[i]))
	{
	  dbus_free_string_array (services);
	  dbus_message_unref (reply);
	  BUS_SET_OOM (error);
	  return FALSE;
	}
      ++i;
    }

697
  dbus_free_string_array (services);
698 699 700 701 702 703 704 705

  if (!dbus_message_iter_close_container (&iter, &sub))
    {
      dbus_message_unref (reply);
      BUS_SET_OOM (error);
      return FALSE;
    }

706
  if (!bus_transaction_send_from_driver (transaction, connection, reply))
707
    {
708 709 710 711 712 713 714 715
      dbus_message_unref (reply);
      BUS_SET_OOM (error);
      return FALSE;
    }
  else
    {
      dbus_message_unref (reply);
      return TRUE;
716
    }
717 718
}

719
static dbus_bool_t
720
bus_driver_handle_acquire_service (DBusConnection *connection,
721 722 723
                                   BusTransaction *transaction,
                                   DBusMessage    *message,
                                   DBusError      *error)
724
{
725
  DBusMessage *reply;
726
  DBusString service_name;
727
  const char *name;
728
  dbus_uint32_t service_reply;
729
  dbus_uint32_t flags;
730
  dbus_bool_t retval;
731
  BusRegistry *registry;
732 733

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
734

735
  registry = bus_connection_get_registry (connection);
736

737
  if (!dbus_message_get_args (message, error,
738 739
                              DBUS_TYPE_STRING, &name,
                              DBUS_TYPE_UINT32, &flags,
740
                              DBUS_TYPE_INVALID))
741
    return FALSE;
742

743
  _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
744

745 746
  retval = FALSE;
  reply = NULL;
747 748

  _dbus_string_init_const (&service_name, name);
749

750 751 752 753 754
  if (!bus_registry_acquire_service (registry, connection,
                                     &service_name, flags,
                                     &service_reply, transaction,
                                     error))
    goto out;
755

756
  reply = dbus_message_new_method_return (message);
757 758 759 760
  if (reply == NULL)
    {
      BUS_SET_OOM (error);
      goto out;
761
    }
762

763
  if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
764 765 766 767
    {
      BUS_SET_OOM (error);
      goto out;
    }
768

769
  if (!bus_transaction_send_from_driver (transaction, connection, reply))
770 771 772 773 774 775
    {
      BUS_SET_OOM (error);
      goto out;
    }

  retval = TRUE;
776

777 778 779 780
 out:
  if (reply)
    dbus_message_unref (reply);
  return retval;
781
}
782

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
static dbus_bool_t
bus_driver_handle_release_service (DBusConnection *connection,
                                   BusTransaction *transaction,
                                   DBusMessage    *message,
                                   DBusError      *error)
{
  DBusMessage *reply;
  DBusString service_name;
  const char *name;
  dbus_uint32_t service_reply;
  dbus_bool_t retval;
  BusRegistry *registry;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  registry = bus_connection_get_registry (connection);

  if (!dbus_message_get_args (message, error,
                              DBUS_TYPE_STRING, &name,
                              DBUS_TYPE_INVALID))
    return FALSE;

  _dbus_verbose ("Trying to release name %s\n", name);

  retval = FALSE;
  reply = NULL;

  _dbus_string_init_const (&service_name, name);

  if (!bus_registry_release_service (registry, connection,
                                     &service_name, &service_reply,
                                     transaction, error))
    goto out;

  reply = dbus_message_new_method_return (message);
  if (reply == NULL)
    {
      BUS_SET_OOM (error);
      goto out;
    }

  if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
    {
      BUS_SET_OOM (error);
      goto out;
    }

  if (!bus_transaction_send_from_driver (transaction, connection, reply))
    {
      BUS_SET_OOM (error);
      goto out;
    }

  retval = TRUE;

 out:
  if (reply)
    dbus_message_unref (reply);
  return retval;
}

844
static dbus_bool_t
845
bus_driver_handle_service_exists (DBusConnection *connection,
846 847 848
                                  BusTransaction *transaction,
                                  DBusMessage    *message,
                                  DBusError      *error)
849 850 851 852
{
  DBusMessage *reply;
  DBusString service_name;
  BusService *service;
853
  dbus_bool_t service_exists;
854
  const char *name;
855
  dbus_bool_t retval;
856
  BusRegistry *registry;
857 858

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
859

860
  registry = bus_connection_get_registry (connection);
861

862 863
  if (!dbus_message_get_args (message, error,
                              DBUS_TYPE_STRING, &name,
864
                              DBUS_TYPE_INVALID))
865
    return FALSE;
866

867
  retval = FALSE;
868

869
  if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
870 871 872 873 874 875 876 877 878
    {
      service_exists = TRUE;
    }
  else
    {
      _dbus_string_init_const (&service_name, name);
      service = bus_registry_lookup (registry, &service_name);
      service_exists = service != NULL;
    }
879

880
  reply = dbus_message_new_method_return (message);
881 882 883 884 885 886 887
  if (reply == NULL)
    {
      BUS_SET_OOM (error);
      goto out;
    }

  if (!dbus_message_append_args (reply,
888
                                 DBUS_TYPE_BOOLEAN, &service_exists,
889 890 891 892 893 894
                                 0))
    {
      BUS_SET_OOM (error);
      goto out;
    }

895
  if (!bus_transaction_send_from_driver (transaction, connection, reply))
896 897 898 899 900 901
    {
      BUS_SET_OOM (error);
      goto out;
    }

  retval = TRUE;
902

903 904 905 906 907
 out:
  if (reply)
    dbus_message_unref (reply);

  return retval;
908
}
909

910
static dbus_bool_t
911
bus_driver_handle_activate_service (DBusConnection *connection,
912 913 914
                                    BusTransaction *transaction,
                                    DBusMessage    *message,
                                    DBusError      *error)
915 916
{
  dbus_uint32_t flags;
917
  const char *name;
918
  dbus_bool_t retval;
919 920
  BusActivation *activation;

921
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
922

923
  activation = bus_connection_get_activation (connection);
924

925 926 927
  if (!dbus_message_get_args (message, error,
                              DBUS_TYPE_STRING, &name,
                              DBUS_TYPE_UINT32, &flags,
928
                              DBUS_TYPE_INVALID))
929 930
    {
      _DBUS_ASSERT_ERROR_IS_SET (error);
931
      _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
932 933
      return FALSE;
    }
934

935
  retval = FALSE;
936

937
  if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
938
                                        message, name, error))
939 940 941 942 943
    {
      _DBUS_ASSERT_ERROR_IS_SET (error);
      _dbus_verbose ("bus_activation_activate_service() failed\n");
      goto out;
    }
944 945

  retval = TRUE;
946

947 948
 out:
  return retval;
949 950
}

951 952 953 954 955 956 957 958
static dbus_bool_t
send_ack_reply (DBusConnection *connection,
                BusTransaction *transaction,
                DBusMessage    *message,
                DBusError      *error)
{
  DBusMessage *reply;

959 960 961
  if (dbus_message_get_no_reply (message))
    return TRUE;

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
  reply = dbus_message_new_method_return (message);
  if (reply == NULL)
    {
      BUS_SET_OOM (error);
      return FALSE;
    }

  if (!bus_transaction_send_from_driver (transaction, connection, reply))
    {
      BUS_SET_OOM (error);
      dbus_message_unref (reply);
      return FALSE;
    }

  dbus_message_unref (reply);
977

978 979 980
  return TRUE;
}

981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
static dbus_bool_t
bus_driver_handle_update_activation_environment (DBusConnection *connection,
                                                 BusTransaction *transaction,
                                                 DBusMessage    *message,
                                                 DBusError      *error)
{
  dbus_bool_t retval;
  BusActivation *activation;
  DBusMessageIter iter;
  DBusMessageIter dict_iter;
  DBusMessageIter dict_entry_iter;
  int array_type;
  int key_type;
  DBusList *keys, *key_link;
  DBusList *values, *value_link;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

999 1000 1001
  if (!bus_driver_check_message_is_for_us (message, error))
    return FALSE;

1002 1003 1004
#ifdef DBUS_UNIX
    {
      /* UpdateActivationEnvironment is basically a recipe for privilege
1005 1006 1007 1008 1009 1010
       * escalation so let's be extra-careful: do not allow the sysadmin
       * to shoot themselves in the foot.
       */
      if (!bus_driver_check_caller_is_privileged (connection, transaction,
                                                  message, error))
        return FALSE;
1011 1012 1013
    }
#endif

1014 1015 1016 1017 1018 1019 1020
  activation = bus_connection_get_activation (connection);

  dbus_message_iter_init (message, &iter);

  /* The message signature has already been checked for us,
   * so let's just assert it's right.
   */
1021
  _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

  dbus_message_iter_recurse (&iter, &dict_iter);

  retval = FALSE;

  /* Then loop through the sent dictionary, add the location of
   * the environment keys and values to lists. The result will
   * be in reverse order, so we don't have to constantly search
   * for the end of the list in a loop.
   */
  keys = NULL;
  values = NULL;
  while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
    {
      dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);

      while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
        {
          char *key;
          char *value;
          int value_type;

          dbus_message_iter_get_basic (&dict_entry_iter, &key);
          dbus_message_iter_next (&dict_entry_iter);

          value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);

          if (value_type != DBUS_TYPE_STRING)
            break;

          dbus_message_iter_get_basic (&dict_entry_iter, &value);

          if (!_dbus_list_append (&keys, key))
            {
              BUS_SET_OOM (error);
              break;
            }

          if (!_dbus_list_append (&values, value))
            {
              BUS_SET_OOM (error);
              break;
            }

          dbus_message_iter_next (&dict_entry_iter);
        }

      if (key_type != DBUS_TYPE_INVALID)
        break;

      dbus_message_iter_next (&dict_iter);
    }

  if (array_type != DBUS_TYPE_INVALID)
    goto out;

  _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));

  key_link = keys;
  value_link = values;
  while (key_link != NULL)
  {
      const char *key;
      const char *value;

      key = key_link->data;
      value = value_link->data;

      if (!bus_activation_set_environment_variable (activation,
                                                    key, value, error))
      {
          _DBUS_ASSERT_ERROR_IS_SET (error);
          _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
          break;
      }
      key_link = _dbus_list_get_next_link (&keys, key_link);
      value_link = _dbus_list_get_next_link (&values, value_link);
  }

  /* FIXME: We can fail early having set only some of the environment variables,
   * (because of OOM failure).  It's sort of hard to fix and it doesn't really
   * matter, so we're punting for now.
   */
  if (key_link != NULL)
    goto out;

  if (!send_ack_reply (connection, transaction,
                       message, error))
    goto out;

  retval = TRUE;

 out:
  _dbus_list_clear (&keys);
  _dbus_list_clear (&values);
  return retval;
}

1120 1121 1122 1123 1124 1125 1126
static dbus_bool_t
bus_driver_handle_add_match (DBusConnection *connection,
                             BusTransaction *transaction,
                             DBusMessage    *message,
                             DBusError      *error)
{
  BusMatchRule *rule;
1127
  const char *text, *bustype;
1128 1129
  DBusString str;
  BusMatchmaker *matchmaker;
1130
  BusContext *context;
1131

1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  text = NULL;
  rule = NULL;

  if (bus_connection_get_n_match_rules (connection) >=
      bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
    {
      dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
                      "Connection \"%s\" is not allowed to add more match rules "
                      "(increase limits in configuration file if required)",
                      bus_connection_is_active (connection) ?
                      bus_connection_get_name (connection) :
                      "(inactive)");
      goto failed;
    }
1148

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
  if (!dbus_message_get_args (message, error,
                              DBUS_TYPE_STRING, &text,
                              DBUS_TYPE_INVALID))
    {
      _dbus_verbose ("No memory to get arguments to AddMatch\n");
      goto failed;
    }

  _dbus_string_init_const (&str, text);

  rule = bus_match_rule_parse (connection, &str, error);
  if (rule == NULL)
    goto failed;

1163 1164 1165 1166 1167 1168
  context = bus_transaction_get_context (transaction);
  bustype = context ? bus_context_get_type (context) : NULL;
  if (bus_match_rule_get_client_is_eavesdropping (rule) &&
      !bus_apparmor_allows_eavesdropping (connection, bustype, error))
    goto failed;

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
  matchmaker = bus_connection_get_matchmaker (connection);

  if (!bus_matchmaker_add_rule (matchmaker, rule))
    {
      BUS_SET_OOM (error);
      goto failed;
    }

  if (!send_ack_reply (connection, transaction,
                       message, error))
    {
      bus_matchmaker_remove_rule (matchmaker, rule);
      goto failed;
    }
1183

1184
  bus_match_rule_unref (rule);
1185

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
  return TRUE;

 failed:
  _DBUS_ASSERT_ERROR_IS_SET (error);
  if (rule)
    bus_match_rule_unref (rule);
  return FALSE;
}

static dbus_bool_t
bus_driver_handle_remove_match (DBusConnection *connection,
                                BusTransaction *transaction,
                                DBusMessage    *message,
                                DBusError      *error)
{
  BusMatchRule *rule;
1202
  const char *text;
1203 1204
  DBusString str;
  BusMatchmaker *matchmaker;
1205

1206 1207 1208 1209
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  text = NULL;
  rule = NULL;
1210

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
  if (!dbus_message_get_args (message, error,
                              DBUS_TYPE_STRING, &text,
                              DBUS_TYPE_INVALID))
    {
      _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
      goto failed;
    }

  _dbus_string_init_const (&str, text);

  rule = bus_match_rule_parse (connection, &str, error);
  if (rule == NULL)
    goto failed;

  /* Send the ack before we remove the rule, since the ack is undone
   * on transaction cancel, but rule removal isn't.
   */
  if (!send_ack_reply (connection, transaction,
                       message, error))
    goto failed;
1231

1232 1233 1234 1235 1236 1237
  matchmaker = bus_connection_get_matchmaker (connection);

  if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
    goto failed;

  bus_match_rule_unref (rule);
1238

1239 1240 1241 1242 1243 1244 1245 1246 1247
  return TRUE;

 failed:
  _DBUS_ASSERT_ERROR_IS_SET (error);
  if (rule)
    bus_match_rule_unref (rule);
  return FALSE;
}

1248 1249 1250 1251 1252 1253
static dbus_bool_t
bus_driver_handle_get_service_owner (DBusConnection *connection,
				     BusTransaction *transaction,
				     DBusMessage    *message,
				     DBusError      *error)
{
1254
  const char *text;
1255 1256 1257 1258 1259
  const char *base_name;
  DBusString str;
  BusRegistry *registry;
  BusService *service;
  DBusMessage *reply;
1260

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  registry = bus_connection_get_registry (connection);

  text = NULL;
  reply = NULL;

  if (! dbus_message_get_args (message, error,
			       DBUS_TYPE_STRING, &text,
			       DBUS_TYPE_INVALID))
      goto failed;

  _dbus_string_init_const (&str, text);
  service = bus_registry_lookup (registry, &str);
1275
  if (service == NULL &&
1276
      _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1277 1278
    {
      /* ORG_FREEDESKTOP_DBUS owns itself */
1279
      base_name = DBUS_SERVICE_DBUS;
1280 1281
    }
  else if (service == NULL)
1282
    {
1283
      dbus_set_error (error,
1284 1285
                      DBUS_ERROR_NAME_HAS_NO_OWNER,
                      "Could not get owner of name '%s': no such name", text);
1286 1287
      goto failed;
    }
1288
  else
1289
    {
1290
      base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1291 1292 1293 1294 1295 1296 1297 1298
      if (base_name == NULL)
        {
          /* FIXME - how is this error possible? */
          dbus_set_error (error,
                          DBUS_ERROR_FAILED,
                          "Could not determine unique name for '%s'", text);
          goto failed;
        }
1299
      _dbus_assert (*base_name == ':');
1300
    }
1301 1302

  _dbus_assert (base_name != NULL);
1303 1304 1305 1306 1307

  reply = dbus_message_new_method_return (message);
  if (reply == NULL)
    goto oom;

1308
  if (! dbus_message_append_args (reply,
1309
				  DBUS_TYPE_STRING, &base_name,
1310 1311
				  DBUS_TYPE_INVALID))
    goto oom;
1312

1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
  if (! bus_transaction_send_from_driver (transaction, connection, reply))
    goto oom;

  dbus_message_unref (reply);

  return TRUE;

 oom:
  BUS_SET_OOM (error);

 failed:
  _DBUS_ASSERT_ERROR_IS_SET (error);
  if (reply)
    dbus_message_unref (reply);
  return FALSE;
}

1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
static dbus_bool_t
bus_driver_handle_list_queued_owners (DBusConnection *connection,
				      BusTransaction *transaction,
				      DBusMessage    *message,
				      DBusError      *error)
{
  const char *text;
  DBusList *base_names;
  DBusList *link;
  DBusString str;
  BusRegistry *registry;
  BusService *service;
  DBusMessage *reply;
  DBusMessageIter iter, array_iter;
  char *dbus_service_name = DBUS_SERVICE_DBUS;
1345

1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  registry = bus_connection_get_registry (connection);

  base_names = NULL;
  text = NULL;
  reply = NULL;

  if (! dbus_message_get_args (message, error,
			       DBUS_TYPE_STRING, &text,
			       DBUS_TYPE_INVALID))
      goto failed;

  _dbus_string_init_const (&str, text);
  service = bus_registry_lookup (registry, &str);
  if (service == NULL &&
      _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
    {
      /* ORG_FREEDESKTOP_DBUS owns itself */
      if (! _dbus_list_append (&base_names, dbus_service_name))
        goto oom;
    }
  else if (service == NULL)
    {
1370
      dbus_set_error (error,
1371 1372 1373 1374 1375 1376
                      DBUS_ERROR_NAME_HAS_NO_OWNER,
                      "Could not get owners of name '%s': no such name", text);
      goto failed;
    }
  else
    {
1377
      if (!bus_service_list_queued_owners (service,
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
                                           &base_names,
                                           error))
        goto failed;
    }

  _dbus_assert (base_names != NULL);

  reply = dbus_message_new_method_return (message);
  if (reply == NULL)
    goto oom;

  dbus_message_iter_init_append (reply, &iter);
  if (!dbus_message_iter_open_container (&iter,
                                         DBUS_TYPE_ARRAY,
                                         DBUS_TYPE_STRING_AS_STRING,
                                         &array_iter))
    goto oom;
1395

1396 1397 1398 1399 1400 1401 1402
  link = _dbus_list_get_first_link (&base_names);
  while (link != NULL)
    {
      char *uname;

      _dbus_assert (link->data != NULL);
      uname = (char *)link->data;
1403 1404

      if (!dbus_message_iter_append_basic (&array_iter,
1405 1406 1407 1408 1409 1410 1411 1412 1413
                                           DBUS_TYPE_STRING,
                                           &uname))
        goto oom;

      link = _dbus_list_get_next_link (&base_names, link);
    }

  if (! dbus_message_iter_close_container (&iter, &array_iter))
    goto oom;
1414 1415


1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
  if (! bus_transaction_send_from_driver (transaction, connection, reply))
    goto oom;

  dbus_message_unref (reply);

  return TRUE;

 oom:
  BUS_SET_OOM (error);

 failed:
  _DBUS_ASSERT_ERROR_IS_SET (error);
  if (reply)
    dbus_message_unref (reply);

  if (base_names)
    _dbus_list_clear (&base_names);

  return FALSE;
}

1437 1438 1439 1440 1441 1442 1443 1444 1445
static dbus_bool_t
bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
                                            BusTransaction *transaction,
                                            DBusMessage    *message,
                                            DBusError      *error)
{
  DBusConnection *conn;
  DBusMessage *reply;
  unsigned long uid;
1446
  dbus_uint32_t uid32;
1447
  const char *service;
1448 1449 1450 1451 1452

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  reply = NULL;

1453 1454
  conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
                                     error);
1455

1456 1457
  if (conn == NULL)
    goto failed;
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470

  reply = dbus_message_new_method_return (message);
  if (reply == NULL)
    goto oom;

  if (!dbus_connection_get_unix_user (conn, &uid))
    {
      dbus_set_error (error,
                      DBUS_ERROR_FAILED,
                      "Could not determine UID for '%s'", service);
      goto failed;
    }

1471
  uid32 = uid;
1472
  if (! dbus_message_append_args (reply,
1473
                                  DBUS_TYPE_UINT32, &uid32,
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
                                  DBUS_TYPE_INVALID))
    goto oom;

  if (! bus_transaction_send_from_driver (transaction, connection, reply))
    goto oom;

  dbus_message_unref (reply);

  return TRUE;

 oom:
  BUS_SET_OOM (error);

 failed:
  _DBUS_ASSERT_ERROR_IS_SET (error);
  if (reply)
    dbus_message_unref (reply);
  return FALSE;
}

1494 1495 1496 1497 1498 1499 1500 1501 1502
static dbus_bool_t
bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
						  BusTransaction *transaction,
						  DBusMessage    *message,
						  DBusError      *error)
{
  DBusConnection *conn;
  DBusMessage *reply;
  unsigned long pid;
1503
  dbus_uint32_t pid32;
1504
  const char *service;
1505 1506 1507 1508 1509

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  reply = NULL;

1510 1511
  conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
                                     error);
1512

1513 1514
  if (conn == NULL)
    goto failed;
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527

  reply = dbus_message_new_method_return (message);
  if (reply == NULL)
    goto oom;

  if (!dbus_connection_get_unix_process_id (conn, &pid))
    {
      dbus_set_error (error,
                      DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
                      "Could not determine PID for '%s'", service);
      goto failed;
    }

1528
  pid32 = pid;
1529
  if (! dbus_message_append_args (reply,
1530
                                  DBUS_TYPE_UINT32, &pid32,
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
                                  DBUS_TYPE_INVALID))
    goto oom;

  if (! bus_transaction_send_from_driver (transaction, connection, reply))
    goto oom;

  dbus_message_unref (reply);

  return TRUE;

 oom:
  BUS_SET_OOM (error);

 failed:
  _DBUS_ASSERT_ERROR_IS_SET (error);
  if (reply)
    dbus_message_unref (reply);
  return FALSE;
}

1551 1552 1553 1554 1555 1556 1557 1558
static dbus_bool_t
bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
					      BusTransaction *transaction,
					      DBusMessage    *message,
					      DBusError      *error)
{
  DBusConnection *conn;
  DBusMessage *reply;
1559
  void *data = NULL;
1560
  dbus_uint32_t data_size;
1561
  const char *service;
1562 1563 1564 1565 1566

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  reply = NULL;

1567 1568
  conn = bus_driver_get_conn_helper (connection, message,
                                     "audit session data", &service, error);
1569

1570 1571
  if (conn == NULL)
    goto failed;
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606

  reply = dbus_message_new_method_return (message);
  if (reply == NULL)
    goto oom;

  if (!dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) || data == NULL)
    {
      dbus_set_error (error,
                      DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
                      "Could not determine audit session data for '%s'", service);
      goto failed;
    }

  if (! dbus_message_append_args (reply,
                                  DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
                                  DBUS_TYPE_INVALID))
    goto oom;

  if (! bus_transaction_send_from_driver (transaction, connection, reply))
    goto oom;

  dbus_message_unref (reply);

  return TRUE;

 oom:
  BUS_SET_OOM (error);

 failed:
  _DBUS_ASSERT_ERROR_IS_SET (error);
  if (reply)
    dbus_message_unref (reply);
  return FALSE;
}

1607
static dbus_bool_t
1608 1609 1610 1611
bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
							   BusTransaction *transaction,
							   DBusMessage    *message,
							   DBusError      *error)
1612 1613 1614 1615
{
  DBusConnection *conn;
  DBusMessage *reply;
  BusSELinuxID *context;
1616
  const char *service;
1617 1618 1619 1620 1621

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  reply = NULL;

1622 1623
  conn = bus_driver_get_conn_helper (connection, message, "security context",
                                     &service, error);