bolt-device.c 28.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright © 2017 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Christian J. Kellner <christian@kellner.me>
 */

#include "config.h"

#include "bolt-device.h"
24

25
#include "bolt-enums.h"
26
#include "bolt-error.h"
27
#include "bolt-io.h"
28
#include "bolt-log.h"
29
#include "bolt-manager.h"
30
#include "bolt-names.h"
31
#include "bolt-store.h"
32
#include "bolt-str.h"
33
#include "bolt-sysfs.h"
34
#include "bolt-time.h"
35

36
#include <dirent.h>
37 38
#include <libudev.h>

39 40 41 42 43 44
/* dbus property setter */
static gboolean handle_set_label (BoltExported *obj,
                                  const char   *name,
                                  const GValue *value,
                                  GError      **error);

45
/* dbus method calls */
46 47 48
static gboolean    handle_authorize (BoltExported          *object,
                                     GVariant              *params,
                                     GDBusMethodInvocation *invocation);
49

50

51 52
struct _BoltDevice
{
53
  BoltExported object;
54

55
  /* device props */
56
  char          *dbus_path;
57

58 59 60
  char          *uid;
  char          *name;
  char          *vendor;
61

62 63
  BoltDeviceType type;
  BoltStatus     status;
64

65
  /* when device is attached */
66 67
  char        *syspath;
  BoltSecurity security;
68
  char        *parent;
69

70
  guint64      conntime;
71
  guint64      authtime;
72

73
  /* when device is stored */
74
  BoltStore   *store;
75 76
  BoltPolicy   policy;
  BoltKeyState key;
77
  guint64      storetime;
78 79

  char        *label;
80 81 82 83 84 85
};


enum {
  PROP_0,

86
  PROP_STORE,
87
  PROP_SECURITY,
88

89
  /* exported properties start here, */
90
  PROP_UID,
91 92
  PROP_NAME,
  PROP_VENDOR,
93
  PROP_TYPE,
94
  PROP_STATUS,
95

96
  PROP_PARENT,
97
  PROP_SYSFS,
98
  PROP_CONNTIME,
99
  PROP_AUTHTIME,
100

101 102 103
  PROP_STORED,
  PROP_POLICY,
  PROP_HAVE_KEY,
104
  PROP_STORETIME,
105
  PROP_LABEL,
106 107 108

  PROP_LAST,
  PROP_EXPORTED = PROP_UID
109 110
};

111
static GParamSpec *props[PROP_LAST] = {NULL, };
112

113 114 115 116 117 118 119
enum {
  SIGNAL_STATUS_CHANGED,
  SIGNAL_LAST
};

static guint signals[SIGNAL_LAST] = {0};

120 121
G_DEFINE_TYPE (BoltDevice,
               bolt_device,
122
               BOLT_TYPE_EXPORTED)
123 124 125 126 127 128

static void
bolt_device_finalize (GObject *object)
{
  BoltDevice *dev = BOLT_DEVICE (object);

129 130
  g_clear_object (&dev->store);

131 132
  g_free (dev->dbus_path);

133
  g_free (dev->uid);
134 135
  g_free (dev->name);
  g_free (dev->vendor);
136 137

  g_free (dev->parent);
138
  g_free (dev->syspath);
139
  g_free (dev->label);
140 141 142 143 144

  G_OBJECT_CLASS (bolt_device_parent_class)->finalize (object);
}

static void
145
bolt_device_init (BoltDevice *dev)
146 147 148 149 150 151 152 153 154 155 156 157 158
{
}

static void
bolt_device_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  BoltDevice *dev = BOLT_DEVICE (object);

  switch (prop_id)
    {
159 160 161 162
    case PROP_STORE:
      g_value_set_object (value, dev->store);
      break;

163 164 165 166
    case PROP_SECURITY:
      g_value_set_enum (value, dev->security);
      break;

167 168 169 170
    case PROP_UID:
      g_value_set_string (value, dev->uid);
      break;

171 172 173 174
    case PROP_NAME:
      g_value_set_string (value, dev->name);
      break;

175
    case PROP_TYPE:
176
      g_value_set_enum (value, dev->type);
177 178
      break;

179 180 181 182
    case PROP_VENDOR:
      g_value_set_string (value, dev->vendor);
      break;

183
    case PROP_STATUS:
184
      g_value_set_enum (value, dev->status);
185 186
      break;

187 188 189 190
    case PROP_PARENT:
      g_value_set_string (value, dev->parent);
      break;

191 192 193 194
    case PROP_SYSFS:
      g_value_set_string (value, dev->syspath);
      break;

195 196 197 198
    case PROP_CONNTIME:
      g_value_set_uint64 (value, dev->conntime);
      break;

199 200 201 202
    case PROP_AUTHTIME:
      g_value_set_uint64 (value, dev->authtime);
      break;

203
    case PROP_STORED:
204
      g_value_set_boolean (value, dev->store != NULL);
205 206 207
      break;

    case PROP_POLICY:
208
      g_value_set_enum (value, dev->policy);
209 210 211
      break;

    case PROP_HAVE_KEY:
212
      g_value_set_enum (value, dev->key);
213 214
      break;

215 216 217 218
    case PROP_STORETIME:
      g_value_set_uint64 (value, dev->storetime);
      break;

219 220 221 222
    case PROP_LABEL:
      g_value_set_string (value, dev->label);
      break;

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
bolt_device_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  BoltDevice *dev = BOLT_DEVICE (object);

  switch (prop_id)
    {
238 239
    case PROP_STORE:
      dev->store = g_value_dup_object (value);
240
      g_object_notify_by_pspec (object, props[PROP_STORED]);
241 242
      break;

243 244 245 246
    case PROP_SECURITY:
      dev->security = g_value_get_enum (value);
      break;

247
    case PROP_UID:
248
      g_return_if_fail (dev->uid == NULL);
249 250 251
      dev->uid = g_value_dup_string (value);
      break;

252
    case PROP_NAME:
253
      g_clear_pointer (&dev->name, g_free);
254
      dev->name = g_value_dup_string (value);
255 256 257
      break;

    case PROP_VENDOR:
258
      g_clear_pointer (&dev->vendor, g_free);
259
      dev->vendor = g_value_dup_string (value);
260 261
      break;

262
    case PROP_TYPE:
263
      dev->type = g_value_get_enum (value);
264 265
      break;

266
    case PROP_STATUS:
267 268
      {
        BoltStatus old = dev->status;
269
        BoltStatus now = g_value_get_enum (value);
270 271 272 273 274 275 276 277 278 279
        if (old == now)
          break;

        dev->status = now;
        g_signal_emit (dev,
                       signals[SIGNAL_STATUS_CHANGED],
                       0,
                       old);
        break;
      }
280

281 282 283 284 285
    case PROP_PARENT:
      g_clear_pointer (&dev->parent, g_free);
      dev->parent = g_value_dup_string (value);
      break;

286
    case PROP_SYSFS:
287
      g_clear_pointer (&dev->syspath, g_free);
288 289
      dev->syspath = g_value_dup_string (value);
      break;
290

291 292 293 294
    case PROP_CONNTIME:
      dev->conntime = g_value_get_uint64 (value);
      break;

295 296 297 298
    case PROP_AUTHTIME:
      dev->authtime = g_value_get_uint64 (value);
      break;

299
    case PROP_POLICY:
300
      dev->policy = g_value_get_enum (value);
301 302 303
      break;

    case PROP_HAVE_KEY:
304
      dev->key = g_value_get_enum (value);
305 306
      break;

307 308 309 310
    case PROP_STORETIME:
      dev->storetime = g_value_get_uint64 (value);
      break;

311 312 313 314 315
    case PROP_LABEL:
      g_clear_pointer (&dev->label, g_free);
      dev->label = g_value_dup_string (value);
      break;

316 317 318 319 320 321 322 323 324 325
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}


static void
bolt_device_class_init (BoltDeviceClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
326
  BoltExportedClass *exported_class = BOLT_EXPORTED_CLASS (klass);
327 328 329 330 331 332

  gobject_class->finalize = bolt_device_finalize;

  gobject_class->get_property = bolt_device_get_property;
  gobject_class->set_property = bolt_device_set_property;

333 334 335 336 337
  props[PROP_STORE] =
    g_param_spec_object ("store",
                         NULL, NULL,
                         BOLT_TYPE_STORE,
                         G_PARAM_READWRITE |
338
                         G_PARAM_STATIC_STRINGS);
339

340 341 342 343 344 345 346 347
  props[PROP_SECURITY] =
    g_param_spec_enum ("security",
                       "Security", NULL,
                       BOLT_TYPE_SECURITY,
                       BOLT_SECURITY_NONE,
                       G_PARAM_READWRITE |
                       G_PARAM_STATIC_STRINGS);

348 349 350 351 352 353 354
  props[PROP_UID] =
    g_param_spec_string ("uid",
                         "Uid", NULL,
                         NULL,
                         G_PARAM_READWRITE |
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_STATIC_STRINGS);
355

356 357 358 359 360 361 362
  props[PROP_NAME] =
    g_param_spec_string ("name",
                         "Name", NULL,
                         NULL,
                         G_PARAM_READWRITE |
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_STATIC_STRINGS);
363

364 365 366 367 368 369 370 371 372
  props[PROP_VENDOR] =
    g_param_spec_string ("vendor",
                         "Vendor", NULL,
                         NULL,
                         G_PARAM_READWRITE |
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_STATIC_STRINGS);

  props[PROP_TYPE] =
373
    g_param_spec_enum ("type",
374
                       "Type", NULL,
375
                       BOLT_TYPE_DEVICE_TYPE,
376 377 378 379 380 381
                       BOLT_DEVICE_PERIPHERAL,
                       G_PARAM_READWRITE |
                       G_PARAM_CONSTRUCT_ONLY |
                       G_PARAM_STATIC_STRINGS);

  props[PROP_STATUS] =
382
    g_param_spec_enum ("status",
383
                       "Status", NULL,
384
                       BOLT_TYPE_STATUS,
385 386 387 388 389 390 391 392 393 394
                       BOLT_STATUS_DISCONNECTED,
                       G_PARAM_READWRITE |
                       G_PARAM_STATIC_STRINGS);

  props[PROP_PARENT] =
    g_param_spec_string ("parent",
                         "Parent", NULL,
                         NULL,
                         G_PARAM_READWRITE |
                         G_PARAM_STATIC_STRINGS);
395

396 397 398 399 400 401
  props[PROP_SYSFS] =
    g_param_spec_string ("sysfs-path",
                         "SysfsPath", NULL,
                         NULL,
                         G_PARAM_READWRITE |
                         G_PARAM_STATIC_STRINGS);
402

403 404 405 406 407 408 409
  props[PROP_CONNTIME] =
    g_param_spec_uint64 ("conntime",
                         "ConnectTime", NULL,
                         0, G_MAXUINT64, 0,
                         G_PARAM_READWRITE |
                         G_PARAM_STATIC_STRINGS);

410 411 412 413 414 415 416
  props[PROP_AUTHTIME] =
    g_param_spec_uint64 ("authtime",
                         "AuthorizeTime", NULL,
                         0, G_MAXUINT64, 0,
                         G_PARAM_READWRITE |
                         G_PARAM_STATIC_STRINGS);

417 418 419 420 421 422 423 424
  props[PROP_STORED] =
    g_param_spec_boolean ("stored",
                          "Stored", NULL,
                          FALSE,
                          G_PARAM_READWRITE |
                          G_PARAM_STATIC_STRINGS);

  props[PROP_POLICY] =
425
    g_param_spec_enum ("policy",
426
                       "Policy", NULL,
427
                       BOLT_TYPE_POLICY,
428 429 430 431 432
                       BOLT_POLICY_DEFAULT,
                       G_PARAM_READWRITE |
                       G_PARAM_STATIC_STRINGS);

  props[PROP_HAVE_KEY] =
433
    g_param_spec_enum ("key",
434
                       "Key", NULL,
435
                       BOLT_TYPE_KEY_STATE,
436 437 438
                       BOLT_KEY_MISSING,
                       G_PARAM_READWRITE |
                       G_PARAM_STATIC_STRINGS);
439

440 441 442 443 444 445 446
  props[PROP_STORETIME] =
    g_param_spec_uint64 ("storetime",
                         "StoreTime", NULL,
                         0, G_MAXUINT64, 0,
                         G_PARAM_READWRITE |
                         G_PARAM_STATIC_STRINGS);

447 448 449 450 451 452 453
  props[PROP_LABEL] =
    g_param_spec_string ("label",
                         "Label", NULL,
                         NULL,
                         G_PARAM_READWRITE |
                         G_PARAM_STATIC_STRINGS);

454 455 456
  g_object_class_install_properties (gobject_class,
                                     PROP_LAST,
                                     props);
457 458 459 460 461 462 463 464 465 466 467

  signals[SIGNAL_STATUS_CHANGED] =
    g_signal_new ("status-changed",
                  G_TYPE_FROM_CLASS (gobject_class),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL,
                  NULL,
                  G_TYPE_NONE,
                  1, BOLT_TYPE_STATUS);

468 469 470 471 472 473 474 475 476
  bolt_exported_class_set_interface_info (exported_class,
                                          BOLT_DBUS_DEVICE_INTERFACE,
                                          "/boltd/org.freedesktop.bolt.xml");

  bolt_exported_class_export_properties (exported_class,
                                         PROP_EXPORTED,
                                         PROP_LAST,
                                         props);

477 478 479 480
  bolt_exported_class_property_setter (exported_class,
                                       props[PROP_LABEL],
                                       handle_set_label);

481 482 483 484
  bolt_exported_class_export_method (exported_class,
                                     "Authorize",
                                     handle_authorize);

485
}
486 487 488

/* internal methods */

489 490
static const char *
read_sysattr_name (struct udev_device *udev, const char *attr, GError **error)
491 492
{
  g_autofree char *s = NULL;
493
  const char *v;
494 495

  s = g_strdup_printf ("%s_name", attr);
496 497 498 499 500 501
  v = udev_device_get_sysattr_value (udev, s);

  if (v != NULL)
    return v;

  v = udev_device_get_sysattr_value (udev, attr);
502

503
  if (v == NULL)
504 505 506
    g_set_error (error,
                 BOLT_ERROR, BOLT_ERROR_UDEV,
                 "failed to get sysfs attr: %s", attr);
507 508

  return v;
509 510
}

511 512 513 514 515 516 517 518 519 520 521 522
static gint
read_sysfs_attr_int (struct udev_device *device, const char *attr)
{
  const char *str;
  char *end;
  gint64 val;

  str = udev_device_get_sysattr_value (device, attr);

  if (str == NULL)
    return 0;

523
  val = g_ascii_strtoll (str, &end, 0);
524 525 526 527 528 529

  if (str == end)
    return 0;

  if (val > G_MAXINT || val < G_MININT)
    {
530
      bolt_warn ("value read from sysfs outside of gint's range.");
531 532 533 534 535 536 537 538 539 540 541 542
      val = 0;
    }

  return (gint) val;
}

static gboolean
string_nonzero (const char *str)
{
  return str != NULL && str[0] != '\0';
}

543 544 545 546 547 548 549 550 551 552 553 554 555
static struct udev_device *
bolt_sysfs_get_parent (struct udev_device *udev,
                       GError            **error)
{
  struct udev_device * parent = udev_device_get_parent (udev);

  if (parent == NULL)
    g_set_error (error, BOLT_ERROR, BOLT_ERROR_UDEV,
                 "could not get parent udev device");

  return parent;
}

556 557 558 559 560 561 562 563 564 565 566 567
static const char *
bolt_sysfs_get_parent_uid (struct udev_device *udev)
{
  struct udev_device *parent;
  const char *uid = NULL;

  parent = udev_device_get_parent (udev);
  if (parent)
    uid = udev_device_get_sysattr_value (parent, "unique_id");
  return uid;
}

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
typedef enum BoltStatTime {
  BOLT_ST_ATIME,
  BOLT_ST_CTIME,
  BOLT_ST_MTIME
} BoltStatTime;

static gint64
bolt_sysfs_device_get_time (struct udev_device *udev,
                            BoltStatTime        st)
{
  const char *path;
  struct stat sb;
  gint64 ms;
  int r;

  path = udev_device_get_syspath (udev);

  if (path == NULL)
    return 0;

  r = lstat (path, &sb);

  if (r == -1)
    return 0;

  switch (st)
    {
    case BOLT_ST_CTIME:
      ms = (gint64) sb.st_ctim.tv_sec;
      break;

    case BOLT_ST_ATIME:
      ms = (gint64) sb.st_atim.tv_sec;
      break;

    case BOLT_ST_MTIME:
      ms = (gint64) sb.st_mtim.tv_sec;
      break;

    default:
      bolt_warn_enum_unhandled (BoltStatTime, st);
      return 0;
    }

  if (ms < 0)
    ms = 0;

  return ms;
}

618
static BoltStatus
619 620
bolt_status_from_udev (struct udev_device *udev,
                       BoltSecurity        security)
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
{
  gint authorized;
  const char *key;
  gboolean have_key;

  authorized = read_sysfs_attr_int (udev, "authorized");

  if (authorized == 2)
    return BOLT_STATUS_AUTHORIZED_SECURE;

  key = udev_device_get_sysattr_value (udev, "key");
  have_key = string_nonzero (key);

  if (authorized == 1)
    {
636 637 638
      if (security == BOLT_SECURITY_DPONLY)
        return BOLT_STATUS_AUTHORIZED_DPONLY;
      else if (have_key)
639 640 641 642 643 644 645 646 647 648 649 650
        return BOLT_STATUS_AUTHORIZED_NEWKEY;
      else
        return BOLT_STATUS_AUTHORIZED;
    }
  else if (authorized == 0 && have_key)
    {
      return BOLT_STATUS_AUTH_ERROR;
    }

  return BOLT_STATUS_CONNECTED;
}

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
static const char *
cleanup_name (const char *name,
              const char *vendor,
              GError    **error)
{
  g_return_val_if_fail (vendor != NULL, NULL);
  g_return_val_if_fail (name != NULL, NULL);

  /* some devices have the vendor name as a prefix */
  if (!g_str_has_prefix (name, vendor))
    return name;

  name += strlen (vendor);

  while (g_ascii_isspace (*name))
    name++;

  if (*name == '\0')
    {
      g_set_error_literal (error,
                           BOLT_ERROR, BOLT_ERROR_UDEV,
                           "device has empty name after cleanup");
      return NULL;
    }

  return name;
}

679 680 681 682
/*  device authorization */

typedef struct
{
683
  BoltAuth *auth;
684 685

  /* the outer callback  */
686 687
  GAsyncReadyCallback callback;
  gpointer            user_data;
688 689 690 691 692 693 694 695

} AuthData;

static void
auth_data_free (gpointer data)
{
  AuthData *auth = data;

696
  g_clear_object (&auth->auth);
697 698 699
  g_slice_free (AuthData, auth);
}

700 701
static gboolean
authorize_device_internal (BoltDevice *dev,
702
                           BoltAuth   *auth,
703
                           GError    **error)
704 705
{
  g_autoptr(DIR) devdir = NULL;
706 707
  BoltKey *key;
  BoltSecurity level;
708 709
  gboolean ok;

710 711 712
  key = bolt_auth_get_key (auth);
  level = bolt_auth_get_level (auth);

713
  devdir = bolt_opendir (dev->syspath, error);
714
  if (devdir == NULL)
715
    return FALSE;
716

717
  ok = bolt_verify_uid (dirfd (devdir), dev->uid, error);
718
  if (!ok)
719 720
    return FALSE;

721
  if (key)
722
    {
723 724
      int keyfd;

725
      bolt_debug (LOG_DEV (dev), "writing key");
726 727 728 729
      keyfd = bolt_openat (dirfd (devdir), "key", O_WRONLY | O_CLOEXEC, error);
      if (keyfd < 0)
        return FALSE;

730
      ok = bolt_key_write_to (key, keyfd, &level, error);
731 732 733
      close (keyfd);
      if (!ok)
        return FALSE;
734 735
    }

736
  bolt_debug (LOG_DEV (dev), "writing authorization");
737 738
  ok = bolt_write_char_at (dirfd (devdir),
                           "authorized",
739
                           level,
740 741 742 743 744 745 746 747 748 749 750 751
                           error);
  return ok;
}

static void
authorize_in_thread (GTask        *task,
                     gpointer      source,
                     gpointer      context,
                     GCancellable *cancellable)
{
  g_autoptr(GError) error = NULL;
  BoltDevice *dev = source;
752 753
  AuthData *auth_data = context;
  BoltAuth *auth = auth_data->auth;
754 755 756
  gboolean ok;

  ok = authorize_device_internal (dev, auth, &error);
757 758

  if (!ok)
759 760 761
    g_task_return_new_error (task, BOLT_ERROR, BOLT_ERROR_FAILED,
                             "failed to authorize device: %s",
                             error->message);
762
  else
763
    g_task_return_boolean (task, TRUE);
764 765 766 767 768 769 770
}

static void
authorize_thread_done (GObject      *object,
                       GAsyncResult *res,
                       gpointer      user_data)
{
771
  g_autoptr(GError) error = NULL;
772 773
  BoltDevice *dev = BOLT_DEVICE (object);
  GTask *task = G_TASK (res);
774
  BoltStatus status;
775 776
  AuthData *auth_data;
  BoltAuth *auth;
777
  gboolean ok;
778
  guint64 now;
779

780 781 782
  auth_data = g_task_get_task_data (task);
  auth = auth_data->auth;

783 784
  ok = g_task_propagate_boolean (task, &error);

785
  if (!ok)
786 787
    bolt_auth_return_error (auth, &error);

788
  now = bolt_now_in_seconds ();
789
  status = bolt_auth_to_status (auth);
790 791 792 793
  g_object_set (dev,
                "status", status,
                "authtime", now,
                NULL);
794

795 796 797 798
  if (auth_data->callback)
    auth_data->callback (G_OBJECT (dev),
                         G_ASYNC_RESULT (auth),
                         auth_data->user_data);
799 800
}

801 802 803 804 805
void
bolt_device_authorize (BoltDevice         *dev,
                       BoltAuth           *auth,
                       GAsyncReadyCallback callback,
                       gpointer            user_data)
806 807 808 809
{
  AuthData *auth_data;
  GTask *task;

810 811
  g_object_set (auth, "device", dev, NULL);

812 813 814
  if (dev->status != BOLT_STATUS_CONNECTED &&
      dev->status != BOLT_STATUS_AUTH_ERROR)
    {
815
      bolt_auth_return_new_error (auth, BOLT_ERROR, BOLT_ERROR_FAILED,
816
                                  "wrong device state: %d", dev->status);
817

818 819
      if (callback)
        callback (G_OBJECT (dev), G_ASYNC_RESULT (auth), user_data);
820

821
      return;
822 823 824
    }

  task = g_task_new (dev, NULL, authorize_thread_done, NULL);
825 826 827
  auth_data = g_slice_new (AuthData);
  auth_data->callback = callback;
  auth_data->user_data = user_data;
828
  auth_data->auth = g_object_ref (auth);
829 830
  g_task_set_task_data (task, auth_data, auth_data_free);

831
  g_object_set (dev, "status", BOLT_STATUS_AUTHORIZING, NULL);
832 833 834 835 836

  g_task_run_in_thread (task, authorize_in_thread);
  g_object_unref (task);
}

837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
/* dbus property setter */

static gboolean
handle_set_label (BoltExported *obj,
                  const char   *name,
                  const GValue *value,
                  GError      **error)
{
  g_autofree char *nick = NULL;
  g_autofree char *old = NULL;
  BoltDevice *dev = BOLT_DEVICE (obj);
  const char *str = g_value_get_string (value);
  gboolean ok;

  nick = bolt_strdup_validate (str);

  if (nick == NULL)
    {
      g_set_error_literal (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
                           "string is invalid");
      return FALSE;
    }
  else if (strlen (nick) > 255)
    {
      g_set_error_literal (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
                           "string is too long");
      return FALSE;
    }

  if (dev->store == NULL)
    {
      g_set_error_literal (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
                           "device is not stored");
      return FALSE;
    }

  old = dev->label;
  dev->label = g_steal_pointer (&nick);

  ok = bolt_store_put_device (dev->store, dev, dev->policy, NULL, error);

  if (!ok)
    {
      bolt_warn_err (*error, LOG_DEV (dev), "failed to store device");

      nick = dev->label;
      dev->label = g_steal_pointer (&old);
    }

  return ok;
}
888 889 890 891

/* dbus methods */

static void
892 893 894
handle_authorize_done (GObject      *device,
                       GAsyncResult *res,
                       gpointer      user_data)
895
{
896 897 898 899 900 901 902
  GDBusMethodInvocation *inv;
  GError *error = NULL;
  BoltAuth *auth;
  gboolean ok;

  inv  = user_data;
  auth = BOLT_AUTH (res);
903

904
  ok = bolt_auth_check (auth, &error);
905
  if (ok)
906
    g_dbus_method_invocation_return_value (inv, g_variant_new ("()"));
907
  else
908
    g_dbus_method_invocation_take_error (inv, error);
909 910 911
}

static gboolean
912 913 914
handle_authorize (BoltExported          *object,
                  GVariant              *params,
                  GDBusMethodInvocation *inv)
915 916 917
{
  BoltDevice *dev = BOLT_DEVICE (object);
  GError *error = NULL;
918 919 920
  BoltAuth *auth;
  BoltSecurity level;
  BoltKey *key;
921

922 923
  level = dev->security;
  key = NULL;
924

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
  if (level == BOLT_SECURITY_SECURE)
    {
      if (dev->key)
        key = bolt_store_get_key (dev->store, dev->uid, &error);
      else
        level = BOLT_SECURITY_USER;
    }

  if (level == BOLT_SECURITY_SECURE && key == NULL)
    {
      g_dbus_method_invocation_take_error (inv, error);
      return TRUE;
    }

  auth = bolt_auth_new (dev, level, key);
  bolt_device_authorize (dev, auth, handle_authorize_done, inv);
941 942 943 944

  return TRUE;
}

945 946 947
/* public methods */

BoltDevice *
948
bolt_device_new_for_udev (struct udev_device *udev,
949 950
                          GError            **error)
{
951
  struct udev_device *parent_dev;
952 953 954 955
  const char *uid;
  const char *name;
  const char *vendor;
  const char *syspath;
956
  const char *parent;
957 958
  BoltSecurity security;
  BoltStatus status;
959
  BoltDeviceType type;
960
  BoltDevice *dev;
961
  guint64 ct, at;
962

963 964 965 966 967 968 969
  uid = udev_device_get_sysattr_value (udev, "unique_id");
  if (udev == NULL)
    {
      g_set_error (error, BOLT_ERROR, BOLT_ERROR_UDEV,
                   "could not get unique_id for udev");
      return NULL;
    }
970

971 972
  syspath = udev_device_get_syspath (udev);
  g_return_val_if_fail (syspath != NULL, NULL);
973

974
  name = read_sysattr_name (udev, "device", error);
975 976 977
  if (name == NULL)
    return NULL;

978
  vendor = read_sysattr_name (udev, "vendor", error);
979 980
  if (vendor == NULL)
    return NULL;
981

982 983 984 985
  name = cleanup_name (name, vendor, error);
  if (name == NULL)
    return NULL;

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
  parent_dev = bolt_sysfs_get_parent (udev, error);
  if (parent_dev == NULL)
    return NULL;

  if (bolt_sysfs_device_is_domain (parent_dev))
    {
      parent = NULL;
      type = BOLT_DEVICE_HOST;
    }
  else
    {
      parent = udev_device_get_sysattr_value (parent_dev, "unique_id");
      type = BOLT_DEVICE_PERIPHERAL;
    }

1001 1002
  ct = (guint64) bolt_sysfs_device_get_time (udev, BOLT_ST_CTIME);

1003
  parent = bolt_sysfs_get_parent_uid (udev);
1004
  security = bolt_sysfs_security_for_device (udev, NULL);
1005
  status = bolt_status_from_udev (udev, security);
1006 1007
  at = bolt_status_is_authorized (status) ? ct : 0;

1008
  dev = g_object_new (BOLT_TYPE_DEVICE,
1009 1010 1011
                      "uid", uid,
                      "name", name,
                      "vendor", vendor,
1012
                      "type", type,
1013
                      "status", status,
1014
                      "sysfs-path", syspath,
1015
                      "parent", parent,
1016
                      "conntime", ct,
1017 1018
                      "authtime", at,
                      "security", security,
1019 1020
                      NULL);

1021

1022 1023 1024
  return dev;
}

1025
const char *
1026 1027 1028 1029
bolt_device_export (BoltDevice      *device,
                    GDBusConnection *connection,
                    GError         **error)
{
1030
  const char *path;
1031
  gboolean ok;
1032 1033

  g_return_val_if_fail (BOLT_IS_DEVICE (device), FALSE);
1034

1035
  path = bolt_device_get_object_path (device);
1036

1037 1038 1039 1040 1041
  ok = bolt_exported_export (BOLT_EXPORTED (device),
                             connection,
                             path,
                             error);

1042
  return ok ? path : NULL;
1043 1044 1045 1046 1047
}

void
bolt_device_unexport (BoltDevice *device)
{
1048
  bolt_exported_unexport (BOLT_EXPORTED (device));
1049
}
1050

1051 1052 1053 1054 1055 1056

BoltStatus
bolt_device_connected (BoltDevice         *dev,
                       struct udev_device *udev)
{
  const char *syspath;
1057
  const char *parent;
1058 1059
  BoltSecurity security;
  BoltStatus status;