bolt-device.c 27.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

34
#include <dirent.h>
35 36
#include <libudev.h>

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

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

48

49 50
struct _BoltDevice
{
51
  BoltExported object;
52

53
  /* device props */
54
  char          *dbus_path;
55

56 57 58
  char          *uid;
  char          *name;
  char          *vendor;
59

60 61
  BoltDeviceType type;
  BoltStatus     status;
62

63
  /* when device is attached */
64 65
  char        *syspath;
  BoltSecurity security;
66
  char        *parent;
67

68 69
  guint64      conntime;

70
  /* when device is stored */
71
  BoltStore   *store;
72 73
  BoltPolicy   policy;
  BoltKeyState key;
74 75

  char        *label;
76 77 78 79 80 81
};


enum {
  PROP_0,

82 83
  PROP_STORE,

84
  /* exported properties start here, */
85
  PROP_UID,
86 87
  PROP_NAME,
  PROP_VENDOR,
88
  PROP_TYPE,
89
  PROP_STATUS,
90

91
  PROP_PARENT,
92
  PROP_SYSFS,
93
  PROP_SECURITY,
94
  PROP_CONNTIME,
95

96 97 98
  PROP_STORED,
  PROP_POLICY,
  PROP_HAVE_KEY,
99
  PROP_LABEL,
100 101 102

  PROP_LAST,
  PROP_EXPORTED = PROP_UID
103 104
};

105
static GParamSpec *props[PROP_LAST] = {NULL, };
106

107 108 109 110 111 112 113
enum {
  SIGNAL_STATUS_CHANGED,
  SIGNAL_LAST
};

static guint signals[SIGNAL_LAST] = {0};

114 115
G_DEFINE_TYPE (BoltDevice,
               bolt_device,
116
               BOLT_TYPE_EXPORTED)
117 118 119 120 121 122

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

123 124
  g_clear_object (&dev->store);

125 126
  g_free (dev->dbus_path);

127
  g_free (dev->uid);
128 129
  g_free (dev->name);
  g_free (dev->vendor);
130 131

  g_free (dev->parent);
132
  g_free (dev->syspath);
133
  g_free (dev->label);
134 135 136 137 138

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

static void
139
bolt_device_init (BoltDevice *dev)
140 141 142 143 144 145 146 147 148 149 150 151 152
{
}

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

  switch (prop_id)
    {
153 154 155 156
    case PROP_STORE:
      g_value_set_object (value, dev->store);
      break;

157 158 159 160
    case PROP_UID:
      g_value_set_string (value, dev->uid);
      break;

161 162 163 164
    case PROP_NAME:
      g_value_set_string (value, dev->name);
      break;

165 166 167 168
    case PROP_TYPE:
      g_value_set_uint (value, dev->type);
      break;

169 170 171 172
    case PROP_VENDOR:
      g_value_set_string (value, dev->vendor);
      break;

173 174 175 176
    case PROP_STATUS:
      g_value_set_uint (value, dev->status);
      break;

177 178 179 180
    case PROP_PARENT:
      g_value_set_string (value, dev->parent);
      break;

181 182 183 184
    case PROP_SYSFS:
      g_value_set_string (value, dev->syspath);
      break;

185 186 187 188
    case PROP_SECURITY:
      g_value_set_uint (value, dev->security);
      break;

189 190 191 192
    case PROP_CONNTIME:
      g_value_set_uint64 (value, dev->conntime);
      break;

193
    case PROP_STORED:
194
      g_value_set_boolean (value, dev->store != NULL);
195 196 197 198 199 200 201 202 203 204
      break;

    case PROP_POLICY:
      g_value_set_uint (value, dev->policy);
      break;

    case PROP_HAVE_KEY:
      g_value_set_uint (value, dev->key);
      break;

205 206 207 208
    case PROP_LABEL:
      g_value_set_string (value, dev->label);
      break;

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    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)
    {
224 225
    case PROP_STORE:
      dev->store = g_value_dup_object (value);
226
      g_object_notify_by_pspec (object, props[PROP_STORED]);
227 228
      break;

229
    case PROP_UID:
230
      g_return_if_fail (dev->uid == NULL);
231 232 233
      dev->uid = g_value_dup_string (value);
      break;

234
    case PROP_NAME:
235
      g_clear_pointer (&dev->name, g_free);
236
      dev->name = g_value_dup_string (value);
237 238 239
      break;

    case PROP_VENDOR:
240
      g_clear_pointer (&dev->vendor, g_free);
241
      dev->vendor = g_value_dup_string (value);
242 243
      break;

244 245 246 247
    case PROP_TYPE:
      dev->type = g_value_get_uint (value);
      break;

248
    case PROP_STATUS:
249 250 251 252 253 254 255 256 257 258 259 260 261
      {
        BoltStatus old = dev->status;
        BoltStatus now = g_value_get_uint (value);
        if (old == now)
          break;

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

263 264 265 266 267
    case PROP_PARENT:
      g_clear_pointer (&dev->parent, g_free);
      dev->parent = g_value_dup_string (value);
      break;

268
    case PROP_SYSFS:
269
      g_clear_pointer (&dev->syspath, g_free);
270 271
      dev->syspath = g_value_dup_string (value);
      break;
272 273 274 275

    case PROP_SECURITY:
      dev->security = g_value_get_uint (value);
      break;
276

277 278 279 280
    case PROP_CONNTIME:
      dev->conntime = g_value_get_uint64 (value);
      break;

281 282 283 284 285 286 287 288
    case PROP_POLICY:
      dev->policy = g_value_get_uint (value);
      break;

    case PROP_HAVE_KEY:
      dev->key = g_value_get_uint (value);
      break;

289 290 291 292 293
    case PROP_LABEL:
      g_clear_pointer (&dev->label, g_free);
      dev->label = g_value_dup_string (value);
      break;

294 295 296 297 298 299 300 301 302 303
    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);
304
  BoltExportedClass *exported_class = BOLT_EXPORTED_CLASS (klass);
305 306 307 308 309 310

  gobject_class->finalize = bolt_device_finalize;

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

311 312 313 314 315
  props[PROP_STORE] =
    g_param_spec_object ("store",
                         NULL, NULL,
                         BOLT_TYPE_STORE,
                         G_PARAM_READWRITE |
316
                         G_PARAM_STATIC_STRINGS);
317

318 319 320 321 322 323 324
  props[PROP_UID] =
    g_param_spec_string ("uid",
                         "Uid", NULL,
                         NULL,
                         G_PARAM_READWRITE |
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_STATIC_STRINGS);
325

326 327 328 329 330 331 332
  props[PROP_NAME] =
    g_param_spec_string ("name",
                         "Name", NULL,
                         NULL,
                         G_PARAM_READWRITE |
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_STATIC_STRINGS);
333

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  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] =
    g_param_spec_uint ("type",
                       "Type", NULL,
                       BOLT_DEVICE_HOST,
                       BOLT_DEVICE_PERIPHERAL,
                       BOLT_DEVICE_PERIPHERAL,
                       G_PARAM_READWRITE |
                       G_PARAM_CONSTRUCT_ONLY |
                       G_PARAM_STATIC_STRINGS);

  props[PROP_STATUS] =
    g_param_spec_uint ("status",
                       "Status", NULL,
                       0,
                       BOLT_STATUS_LAST,
                       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);
367

368 369 370 371 372 373
  props[PROP_SYSFS] =
    g_param_spec_string ("sysfs-path",
                         "SysfsPath", NULL,
                         NULL,
                         G_PARAM_READWRITE |
                         G_PARAM_STATIC_STRINGS);
374

375 376 377 378 379 380 381 382 383
  props[PROP_SECURITY] =
    g_param_spec_uint ("security",
                       "Security", NULL,
                       0,
                       BOLT_SECURITY_LAST,
                       BOLT_SECURITY_NONE,
                       G_PARAM_READWRITE |
                       G_PARAM_STATIC_STRINGS);

384 385 386 387 388 389 390
  props[PROP_CONNTIME] =
    g_param_spec_uint64 ("conntime",
                         "ConnectTime", NULL,
                         0, G_MAXUINT64, 0,
                         G_PARAM_READWRITE |
                         G_PARAM_STATIC_STRINGS);

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
  props[PROP_STORED] =
    g_param_spec_boolean ("stored",
                          "Stored", NULL,
                          FALSE,
                          G_PARAM_READWRITE |
                          G_PARAM_STATIC_STRINGS);

  props[PROP_POLICY] =
    g_param_spec_uint ("policy",
                       "Policy", NULL,
                       0,
                       BOLT_POLICY_LAST,
                       BOLT_POLICY_DEFAULT,
                       G_PARAM_READWRITE |
                       G_PARAM_STATIC_STRINGS);

  props[PROP_HAVE_KEY] =
    g_param_spec_uint ("key",
                       "Key", NULL,
                       0,
                       BOLT_KEY_NEW,
                       BOLT_KEY_MISSING,
                       G_PARAM_READWRITE |
                       G_PARAM_STATIC_STRINGS);
415

416 417 418 419 420 421 422
  props[PROP_LABEL] =
    g_param_spec_string ("label",
                         "Label", NULL,
                         NULL,
                         G_PARAM_READWRITE |
                         G_PARAM_STATIC_STRINGS);

423 424 425
  g_object_class_install_properties (gobject_class,
                                     PROP_LAST,
                                     props);
426 427 428 429 430 431 432 433 434 435 436

  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);

437 438 439 440 441 442 443 444 445
  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);

446 447 448 449
  bolt_exported_class_property_setter (exported_class,
                                       props[PROP_LABEL],
                                       handle_set_label);

450 451 452 453
  bolt_exported_class_export_method (exported_class,
                                     "Authorize",
                                     handle_authorize);

454
}
455 456 457

/* internal methods */

458 459
static const char *
read_sysattr_name (struct udev_device *udev, const char *attr, GError **error)
460 461
{
  g_autofree char *s = NULL;
462
  const char *v;
463 464

  s = g_strdup_printf ("%s_name", attr);
465 466 467 468 469 470
  v = udev_device_get_sysattr_value (udev, s);

  if (v != NULL)
    return v;

  v = udev_device_get_sysattr_value (udev, attr);
471

472
  if (v == NULL)
473 474 475
    g_set_error (error,
                 BOLT_ERROR, BOLT_ERROR_UDEV,
                 "failed to get sysfs attr: %s", attr);
476 477

  return v;
478 479
}

480 481 482 483 484 485 486 487 488 489 490 491
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;

492
  val = g_ascii_strtoll (str, &end, 0);
493 494 495 496 497 498

  if (str == end)
    return 0;

  if (val > G_MAXINT || val < G_MININT)
    {
499
      bolt_warn ("value read from sysfs outside of gint's range.");
500 501 502 503 504 505 506 507 508 509 510 511
      val = 0;
    }

  return (gint) val;
}

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

512 513 514 515 516 517 518 519 520 521 522 523 524
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;
}

525 526 527 528 529 530 531 532 533 534 535 536
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;
}

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
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;
}

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
static BoltStatus
bolt_status_from_udev (struct udev_device *udev)
{
  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)
    {
      if (have_key)
        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;
}

617 618 619 620 621 622 623
static gboolean
bolt_sysfs_device_is_domain (struct udev_device *udev)
{
  const char *devtype = udev_device_get_devtype (udev);

  return bolt_streq (devtype, "thunderbolt_domain");
}
624 625

static struct udev_device *
626
bolt_sysfs_domain_for_device (struct udev_device *udev)
627 628 629 630 631 632 633 634 635 636 637 638
{
  struct udev_device *parent;
  gboolean found;

  found = FALSE;
  parent = udev;
  do
    {
      parent = udev_device_get_parent (parent);
      if (!parent)
        break;

639
      found = bolt_sysfs_device_is_domain (parent);
640 641 642 643 644 645 646
    }
  while (!found);

  return found ? parent : NULL;
}

static BoltSecurity
647
bolt_sysfs_security_for_device (struct udev_device *udev)
648 649 650 651 652
{
  struct udev_device *parent = NULL;
  const char *v;
  BoltSecurity s;

653
  parent = bolt_sysfs_domain_for_device (udev);
654 655
  if (parent == NULL)
    {
656
      bolt_warn ("failed to determine domain device");
657 658 659 660 661 662
      return BOLT_SECURITY_NONE;
    }

  v = udev_device_get_sysattr_value (parent, "security");
  s = bolt_security_from_string (v);

663 664
  if (!bolt_security_validate (s))
    {
665
      bolt_warn ("invalid security: %s", v);
666 667 668
      s = BOLT_SECURITY_NONE;
    }

669 670 671
  return s;
}

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 697 698 699
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;
}

700 701 702 703
/*  device authorization */

typedef struct
{
704
  BoltAuth *auth;
705 706

  /* the outer callback  */
707 708
  GAsyncReadyCallback callback;
  gpointer            user_data;
709 710 711 712 713 714 715 716

} AuthData;

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

717
  g_clear_object (&auth->auth);
718 719 720
  g_slice_free (AuthData, auth);
}

721 722
static gboolean
authorize_device_internal (BoltDevice *dev,
723
                           BoltAuth   *auth,
724
                           GError    **error)
725 726
{
  g_autoptr(DIR) devdir = NULL;
727 728
  BoltKey *key;
  BoltSecurity level;
729 730
  gboolean ok;

731 732 733
  key = bolt_auth_get_key (auth);
  level = bolt_auth_get_level (auth);

734
  devdir = bolt_opendir (dev->syspath, error);
735
  if (devdir == NULL)
736
    return FALSE;
737

738
  ok = bolt_verify_uid (dirfd (devdir), dev->uid, error);
739
  if (!ok)
740 741
    return FALSE;

742
  if (key)
743
    {
744 745
      int keyfd;

746
      bolt_debug (LOG_DEV (dev), "writing key");
747 748 749 750
      keyfd = bolt_openat (dirfd (devdir), "key", O_WRONLY | O_CLOEXEC, error);
      if (keyfd < 0)
        return FALSE;

751
      ok = bolt_key_write_to (key, keyfd, &level, error);
752 753 754
      close (keyfd);
      if (!ok)
        return FALSE;
755 756
    }

757
  bolt_debug (LOG_DEV (dev), "writing authorization");
758 759
  ok = bolt_write_char_at (dirfd (devdir),
                           "authorized",
760
                           level,
761 762 763 764 765 766 767 768 769 770 771 772
                           error);
  return ok;
}

static void
authorize_in_thread (GTask        *task,
                     gpointer      source,
                     gpointer      context,
                     GCancellable *cancellable)
{
  g_autoptr(GError) error = NULL;
  BoltDevice *dev = source;
773 774
  AuthData *auth_data = context;
  BoltAuth *auth = auth_data->auth;
775 776 777
  gboolean ok;

  ok = authorize_device_internal (dev, auth, &error);
778 779

  if (!ok)
780 781 782
    g_task_return_new_error (task, BOLT_ERROR, BOLT_ERROR_FAILED,
                             "failed to authorize device: %s",
                             error->message);
783
  else
784
    g_task_return_boolean (task, TRUE);
785 786 787 788 789 790 791
}

static void
authorize_thread_done (GObject      *object,
                       GAsyncResult *res,
                       gpointer      user_data)
{
792
  g_autoptr(GError) error = NULL;
793 794
  BoltDevice *dev = BOLT_DEVICE (object);
  GTask *task = G_TASK (res);
795
  BoltStatus status;
796 797
  AuthData *auth_data;
  BoltAuth *auth;
798 799
  gboolean ok;

800 801 802
  auth_data = g_task_get_task_data (task);
  auth = auth_data->auth;

803 804
  ok = g_task_propagate_boolean (task, &error);

805
  if (!ok)
806 807 808 809
    bolt_auth_return_error (auth, &error);

  status = bolt_auth_to_status (auth);
  g_object_set (dev, "status", status, NULL);
810

811 812 813 814
  if (auth_data->callback)
    auth_data->callback (G_OBJECT (dev),
                         G_ASYNC_RESULT (auth),
                         auth_data->user_data);
815 816
}

817 818 819 820 821
void
bolt_device_authorize (BoltDevice         *dev,
                       BoltAuth           *auth,
                       GAsyncReadyCallback callback,
                       gpointer            user_data)
822 823 824 825
{
  AuthData *auth_data;
  GTask *task;

826 827
  g_object_set (auth, "device", dev, NULL);

828 829 830
  if (dev->status != BOLT_STATUS_CONNECTED &&
      dev->status != BOLT_STATUS_AUTH_ERROR)
    {
831 832
      bolt_auth_return_new_error (auth, BOLT_ERROR, BOLT_ERROR_FAILED,
                                  "wrong device state: %u", dev->status);
833

834 835
      if (callback)
        callback (G_OBJECT (dev), G_ASYNC_RESULT (auth), user_data);
836

837
      return;
838 839 840
    }

  task = g_task_new (dev, NULL, authorize_thread_done, NULL);
841 842 843
  auth_data = g_slice_new (AuthData);
  auth_data->callback = callback;
  auth_data->user_data = user_data;
844
  auth_data->auth = g_object_ref (auth);
845 846
  g_task_set_task_data (task, auth_data, auth_data_free);

847
  g_object_set (dev, "status", BOLT_STATUS_AUTHORIZING, NULL);
848 849 850 851 852

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

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 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
/* 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;
}
904 905 906 907

/* dbus methods */

static void
908 909 910
handle_authorize_done (GObject      *device,
                       GAsyncResult *res,
                       gpointer      user_data)
911
{
912 913 914 915 916 917 918
  GDBusMethodInvocation *inv;
  GError *error = NULL;
  BoltAuth *auth;
  gboolean ok;

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

920
  ok = bolt_auth_check (auth, &error);
921
  if (ok)
922
    g_dbus_method_invocation_return_value (inv, g_variant_new ("()"));
923
  else
924
    g_dbus_method_invocation_take_error (inv, error);
925 926 927
}

static gboolean
928 929 930
handle_authorize (BoltExported          *object,
                  GVariant              *params,
                  GDBusMethodInvocation *inv)
931 932 933
{
  BoltDevice *dev = BOLT_DEVICE (object);
  GError *error = NULL;
934 935 936
  BoltAuth *auth;
  BoltSecurity level;
  BoltKey *key;
937

938 939
  level = dev->security;
  key = NULL;
940

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
  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);
957 958 959 960

  return TRUE;
}

961 962 963
/* public methods */

BoltDevice *
964
bolt_device_new_for_udev (struct udev_device *udev,
965 966
                          GError            **error)
{
967
  struct udev_device *parent_dev;
968 969 970 971
  const char *uid;
  const char *name;
  const char *vendor;
  const char *syspath;
972
  const char *parent;
973
  BoltDeviceType type;
974
  BoltDevice *dev;
975
  guint64 ct;
976

977 978 979 980 981 982 983
  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;
    }
984

985 986
  syspath = udev_device_get_syspath (udev);
  g_return_val_if_fail (syspath != NULL, NULL);
987

988
  name = read_sysattr_name (udev, "device", error);
989 990 991
  if (name == NULL)
    return NULL;

992
  vendor = read_sysattr_name (udev, "vendor", error);
993 994
  if (vendor == NULL)
    return NULL;
995

996 997 998 999
  name = cleanup_name (name, vendor, error);
  if (name == NULL)
    return NULL;

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
  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;
    }

1015 1016
  ct = (guint64) bolt_sysfs_device_get_time (udev, BOLT_ST_CTIME);

1017
  parent = bolt_sysfs_get_parent_uid (udev);
1018
  dev = g_object_new (BOLT_TYPE_DEVICE,
1019 1020 1021
                      "uid", uid,
                      "name", name,
                      "vendor", vendor,
1022
                      "type", type,
1023
                      "sysfs-path", syspath,
1024
                      "parent", parent,
1025
                      "conntime", ct,
1026 1027
                      NULL);

1028
  dev->status = bolt_status_from_udev (udev);
1029
  dev->security = bolt_sysfs_security_for_device (udev);
1030

1031 1032 1033
  return dev;
}

1034
const char *
1035 1036 1037 1038
bolt_device_export (BoltDevice      *device,
                    GDBusConnection *connection,
                    GError         **error)
{
1039
  const char *path;
1040
  gboolean ok;
1041 1042

  g_return_val_if_fail (BOLT_IS_DEVICE (device), FALSE);
1043

1044
  path = bolt_device_get_object_path (device);
1045

1046 1047 1048 1049 1050
  ok = bolt_exported_export (BOLT_EXPORTED (device),
                             connection,
                             path,
                             error);

1051
  return ok ? path : NULL;
1052 1053 1054 1055 1056
}

void
bolt_device_unexport (BoltDevice *device)
{
1057
  bolt_exported_unexport (BOLT_EXPORTED (device));
1058
}
1059

1060 1061 1062 1063 1064 1065

BoltStatus
bolt_device_connected (BoltDevice         *dev,
                       struct udev_device *udev)
{
  const char *syspath;
1066
  const char *parent;
1067 1068
  BoltSecurity security;
  BoltStatus status;
1069
  guint64 ct;
1070 1071 1072

  syspath = udev_device_get_syspath (udev);
  status = bolt_status_from_udev (udev);
1073
  security = bolt_sysfs_security_for_device (udev);
1074
  parent = bolt_sysfs_get_parent_uid (udev);
1075

1076 1077
  ct = (guint64) bolt_sysfs_device_get_time (udev, BOLT_ST_CTIME);

1078
  g_object_set (G_OBJECT (dev),
1079
                "parent", parent,
1080 1081 1082
                "sysfs-path", syspath,
                "security", security,
                "status", status,
1083
                "conntime", ct,
1084 1085
                NULL);

1086
  bolt_info (LOG_DEV (dev), "parent is %s", dev->parent);
1087

1088 1089 1090 1091 1092 1093 1094
  return status;
}

BoltStatus
bolt_device_disconnected (BoltDevice *dev)
{
  g_object_set (G_OBJECT (dev),
1095
                "parent", NULL,
1096 1097 1098
                "sysfs-path", NULL,
                "security", BOLT_SECURITY_NONE,
                "status", BOLT_STATUS_DISCONNECTED,
1099
                "conntime", 0,
1100 1101
                NULL);

1102 1103 1104 1105 1106
  /* check if we have a new key for the device, and
   * if so, change its state to KEY_HAVE, because
   * now it is not new anymore.
   */
  if (dev->key == BOLT_KEY_NEW)
1107
    g_object_set (G_OBJECT (dev), "key", BOLT_KEY_HAVE, NULL);
1108

1109 1110 1111 1112
  return dev->status;
}

gboolean
1113
bolt_device_is_connected (const BoltDevice *device)
1114
{
1115
  return bolt_status_is_connected (device->status);
1116 1117
}

1118 1119 1120 1121