| 1 |
/* |
| 2 |
* /net/reactivated/Fprint/Device/foo object implementation |
| 3 |
* Copyright (C) 2008 Daniel Drake <[email protected]> |
| 4 |
* Copyright (C) 2020 Marco Trevisan <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License along |
| 17 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 18 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <glib/gi18n.h> |
| 24 |
#include <gio/gio.h> |
| 25 |
#include <polkit/polkit.h> |
| 26 |
#include <fprint.h> |
| 27 |
|
| 28 |
#include <sys/types.h> |
| 29 |
#include <pwd.h> |
| 30 |
#include <errno.h> |
| 31 |
|
| 32 |
#include "fprintd-dbus.h" |
| 33 |
#include "fprintd.h" |
| 34 |
#include "storage.h" |
| 35 |
|
| 36 |
#define VERIFY_STOP_DEVICE_WAIT 1 /* Seconds to wait for the device to complete */ |
| 37 |
|
| 38 |
static const char *FINGERS_NAMES[] = { |
| 39 |
[FP_FINGER_UNKNOWN] = "unknown", |
| 40 |
[FP_FINGER_LEFT_THUMB] = "left-thumb", |
| 41 |
[FP_FINGER_LEFT_INDEX] = "left-index-finger", |
| 42 |
[FP_FINGER_LEFT_MIDDLE] = "left-middle-finger", |
| 43 |
[FP_FINGER_LEFT_RING] = "left-ring-finger", |
| 44 |
[FP_FINGER_LEFT_LITTLE] = "left-little-finger", |
| 45 |
[FP_FINGER_RIGHT_THUMB] = "right-thumb", |
| 46 |
[FP_FINGER_RIGHT_INDEX] = "right-index-finger", |
| 47 |
[FP_FINGER_RIGHT_MIDDLE] = "right-middle-finger", |
| 48 |
[FP_FINGER_RIGHT_RING] = "right-ring-finger", |
| 49 |
[FP_FINGER_RIGHT_LITTLE] = "right-little-finger" |
| 50 |
}; |
| 51 |
|
| 52 |
static void fprint_device_dbus_skeleton_iface_init (FprintDBusDeviceIface *); |
| 53 |
static gboolean action_authorization_handler (GDBusInterfaceSkeleton *, |
| 54 |
GDBusMethodInvocation *, |
| 55 |
gpointer user_data); |
| 56 |
|
| 57 |
static gboolean delete_enrolled_fingers (FprintDevice *rdev, |
| 58 |
const char *user, |
| 59 |
FpFinger finger, |
| 60 |
GError **error); |
| 61 |
|
| 62 |
static GQuark quark_auth_user = 0; |
| 63 |
|
| 64 |
typedef enum { |
| 65 |
ACTION_NONE = 0, |
| 66 |
ACTION_IDENTIFY, |
| 67 |
ACTION_VERIFY, |
| 68 |
ACTION_ENROLL, |
| 69 |
ACTION_OPEN, |
| 70 |
ACTION_CLOSE, |
| 71 |
ACTION_DELETE, |
| 72 |
} FprintDeviceAction; |
| 73 |
|
| 74 |
typedef enum { |
| 75 |
STATE_CLAIMED, |
| 76 |
STATE_UNCLAIMED, |
| 77 |
STATE_AUTO_CLAIM, |
| 78 |
STATE_ANYTIME, |
| 79 |
} FprintDeviceClaimState; |
| 80 |
|
| 81 |
typedef struct |
| 82 |
{ |
| 83 |
volatile gint _refcount; |
| 84 |
|
| 85 |
/* current method invocation */ |
| 86 |
GDBusMethodInvocation *invocation; |
| 87 |
|
| 88 |
/* The current user of the device, if claimed */ |
| 89 |
const char * const sender; |
| 90 |
|
| 91 |
/* The current user of the device, or if allowed, |
| 92 |
* what was passed as a username argument */ |
| 93 |
const char * const username; |
| 94 |
|
| 95 |
gboolean verify_status_reported; |
| 96 |
} SessionData; |
| 97 |
|
| 98 |
typedef struct |
| 99 |
{ |
| 100 |
guint32 id; |
| 101 |
FpDevice *dev; |
| 102 |
SessionData *_session; |
| 103 |
|
| 104 |
gboolean local_storage_checked; |
| 105 |
|
| 106 |
guint verify_stop_wait_timeout_id; |
| 107 |
|
| 108 |
PolkitAuthority *auth; |
| 109 |
|
| 110 |
/* Hashtable of connected clients */ |
| 111 |
GHashTable *clients; |
| 112 |
|
| 113 |
/* Required to restart the operation on a retry failure. */ |
| 114 |
FpPrint *verify_data; |
| 115 |
GPtrArray *identify_data; |
| 116 |
int enroll_data; |
| 117 |
|
| 118 |
/* whether we're running an identify, or a verify */ |
| 119 |
FprintDeviceAction current_action; |
| 120 |
GCancellable *current_cancellable; |
| 121 |
GDBusMethodInvocation *current_cancel_invocation; |
| 122 |
} FprintDevicePrivate; |
| 123 |
|
| 124 |
G_DEFINE_TYPE_WITH_CODE (FprintDevice, fprint_device, |
| 125 |
FPRINT_DBUS_TYPE_DEVICE_SKELETON, |
| 126 |
G_ADD_PRIVATE (FprintDevice) |
| 127 |
G_IMPLEMENT_INTERFACE (FPRINT_DBUS_TYPE_DEVICE, |
| 128 |
fprint_device_dbus_skeleton_iface_init)); |
| 129 |
|
| 130 |
enum fprint_device_properties { |
| 131 |
FPRINT_DEVICE_CONSTRUCT_DEV = 1, |
| 132 |
FPRINT_DEVICE_BUSY, |
| 133 |
}; |
| 134 |
|
| 135 |
enum fprint_device_signals { |
| 136 |
SIGNAL_VERIFY_STATUS, |
| 137 |
SIGNAL_VERIFY_FINGER_SELECTED, |
| 138 |
SIGNAL_ENROLL_STATUS, |
| 139 |
NUM_SIGNALS, |
| 140 |
}; |
| 141 |
|
| 142 |
static guint32 last_id = ~0; |
| 143 |
static guint signals[NUM_SIGNALS] = { 0, }; |
| 144 |
|
| 145 |
#ifndef POLKIT_HAS_AUTOPOINTERS |
| 146 |
/* FIXME: Remove this once we're fine to depend on polkit 0.114 */ |
| 147 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC (PolkitAuthorizationResult, g_object_unref) |
| 148 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC (PolkitSubject, g_object_unref) |
| 149 |
#endif |
| 150 |
|
| 151 |
static void |
| 152 |
session_data_unref (SessionData *session) |
| 153 |
{ |
| 154 |
if (g_atomic_int_dec_and_test (&session->_refcount)) |
| 155 |
{ |
| 156 |
g_clear_pointer ((char **) &session->sender, g_free); |
| 157 |
g_clear_pointer ((char **) &session->username, g_free); |
| 158 |
g_clear_object (&session->invocation); |
| 159 |
g_free (session); |
| 160 |
} |
| 161 |
} |
| 162 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SessionData, session_data_unref); |
| 163 |
|
| 164 |
static SessionData * |
| 165 |
session_data_get (FprintDevicePrivate *priv) |
| 166 |
{ |
| 167 |
SessionData *invalid = (SessionData *) &priv->_session; |
| 168 |
SessionData *cur; |
| 169 |
|
| 170 |
/* Get the current pointer and mark the pointer as "busy". */ |
| 171 |
do |
| 172 |
{ |
| 173 |
cur = g_atomic_pointer_get (&priv->_session); |
| 174 |
/* Swap if cur is valid, otherwise busy loop. */ |
| 175 |
} |
| 176 |
while (cur == invalid || !g_atomic_pointer_compare_and_exchange (&priv->_session, cur, invalid)); |
| 177 |
|
| 178 |
/* We can safely increase the reference count now. */ |
| 179 |
if (cur) |
| 180 |
g_atomic_int_inc (&cur->_refcount); |
| 181 |
|
| 182 |
/* Swap back, this must succeed. */ |
| 183 |
if (!g_atomic_pointer_compare_and_exchange (&priv->_session, invalid, cur)) |
| 184 |
g_assert_not_reached (); |
| 185 |
|
| 186 |
return cur; |
| 187 |
} |
| 188 |
|
| 189 |
/* Pass NULL sender and username to unset session data. */ |
| 190 |
static SessionData * |
| 191 |
session_data_set_new (FprintDevicePrivate *priv, gchar *sender, gchar *username) |
| 192 |
{ |
| 193 |
SessionData *invalid = (SessionData *) &priv->_session; |
| 194 |
SessionData *new = NULL; |
| 195 |
SessionData *old; |
| 196 |
|
| 197 |
g_assert ((!sender && !username) || (sender && username)); |
| 198 |
if (sender) |
| 199 |
{ |
| 200 |
new = g_new0 (SessionData, 1); |
| 201 |
/* Internal reference of the pointer and returned reference. */ |
| 202 |
new->_refcount = 2; |
| 203 |
*(char **) &new->sender = sender; |
| 204 |
*(char **) &new->username = username; |
| 205 |
} |
| 206 |
|
| 207 |
/* Get the current (but not if it is busy) and put the new one in place. */ |
| 208 |
do |
| 209 |
{ |
| 210 |
old = g_atomic_pointer_get (&priv->_session); |
| 211 |
/* Swap if old is valid, otherwise busy loop as someone is ref'ing it currently. */ |
| 212 |
} |
| 213 |
while (old == invalid || !g_atomic_pointer_compare_and_exchange (&priv->_session, old, new)); |
| 214 |
|
| 215 |
/* We can safely drop the our internal reference now. */ |
| 216 |
if (old) |
| 217 |
session_data_unref (old); |
| 218 |
|
| 219 |
return new; |
| 220 |
} |
| 221 |
|
| 222 |
typedef FprintDevice FprintDeviceActionUnset; |
| 223 |
static void |
| 224 |
auto_device_action_unset (FprintDeviceActionUnset *self) |
| 225 |
{ |
| 226 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (self); |
| 227 |
|
| 228 |
priv->current_action = ACTION_NONE; |
| 229 |
} |
| 230 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC (FprintDeviceActionUnset, auto_device_action_unset); |
| 231 |
|
| 232 |
static void |
| 233 |
fprint_device_dispose (GObject *object) |
| 234 |
{ |
| 235 |
FprintDevice *self = (FprintDevice *) object; |
| 236 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (self); |
| 237 |
|
| 238 |
g_hash_table_remove_all (priv->clients); |
| 239 |
|
| 240 |
G_OBJECT_CLASS (fprint_device_parent_class)->dispose (object); |
| 241 |
} |
| 242 |
|
| 243 |
static void |
| 244 |
fprint_device_finalize (GObject *object) |
| 245 |
{ |
| 246 |
FprintDevice *self = (FprintDevice *) object; |
| 247 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (self); |
| 248 |
|
| 249 |
g_clear_handle_id (&priv->verify_stop_wait_timeout_id, g_source_remove); |
| 250 |
g_hash_table_destroy (priv->clients); |
| 251 |
session_data_set_new (priv, NULL, NULL); |
| 252 |
g_clear_object (&priv->auth); |
| 253 |
g_clear_object (&priv->dev); |
| 254 |
|
| 255 |
if (priv->current_action != ACTION_NONE || |
| 256 |
priv->_session || |
| 257 |
priv->verify_data || |
| 258 |
priv->identify_data || |
| 259 |
priv->current_cancellable || |
| 260 |
priv->current_cancel_invocation) |
| 261 |
g_critical ("Device was not cleaned up properly before being finalized."); |
| 262 |
|
| 263 |
G_OBJECT_CLASS (fprint_device_parent_class)->finalize (object); |
| 264 |
} |
| 265 |
|
| 266 |
static void |
| 267 |
fprint_device_set_property (GObject *object, guint property_id, |
| 268 |
const GValue *value, GParamSpec *pspec) |
| 269 |
{ |
| 270 |
FprintDevice *self = (FprintDevice *) object; |
| 271 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (self); |
| 272 |
|
| 273 |
switch (property_id) |
| 274 |
{ |
| 275 |
case FPRINT_DEVICE_CONSTRUCT_DEV: |
| 276 |
priv->dev = g_value_dup_object (value); |
| 277 |
break; |
| 278 |
|
| 279 |
default: |
| 280 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 281 |
break; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
static void |
| 286 |
fprint_device_get_property (GObject *object, guint property_id, |
| 287 |
GValue *value, GParamSpec *pspec) |
| 288 |
{ |
| 289 |
FprintDevice *self = (FprintDevice *) object; |
| 290 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (self); |
| 291 |
|
| 292 |
switch (property_id) |
| 293 |
{ |
| 294 |
case FPRINT_DEVICE_CONSTRUCT_DEV: |
| 295 |
g_value_set_object (value, priv->dev); |
| 296 |
break; |
| 297 |
|
| 298 |
case FPRINT_DEVICE_BUSY: |
| 299 |
g_value_set_boolean (value, |
| 300 |
g_hash_table_size (priv->clients) != 0 || |
| 301 |
fp_device_get_temperature (priv->dev) > FP_TEMPERATURE_COLD); |
| 302 |
break; |
| 303 |
|
| 304 |
default: |
| 305 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 306 |
break; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
static void |
| 311 |
on_nr_enroll_stages_changed (FprintDevice *rdev, |
| 312 |
GParamSpec *spec, |
| 313 |
FpDevice *device) |
| 314 |
{ |
| 315 |
FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (rdev); |
| 316 |
gint nr_enroll_stages; |
| 317 |
|
| 318 |
nr_enroll_stages = fp_device_get_nr_enroll_stages (device); |
| 319 |
|
| 320 |
/* One extra step for our internal identification. */ |
| 321 |
if (fp_device_has_feature (device, FP_DEVICE_FEATURE_IDENTIFY)) |
| 322 |
nr_enroll_stages += 1; |
| 323 |
|
| 324 |
g_debug ("Device %s enroll stages changed to %d", |
| 325 |
fp_device_get_name (device), |
| 326 |
nr_enroll_stages); |
| 327 |
|
| 328 |
fprint_dbus_device_set_num_enroll_stages (dbus_dev, nr_enroll_stages); |
| 329 |
} |
| 330 |
|
| 331 |
static void |
| 332 |
on_scan_type_changed (FprintDevice *rdev, |
| 333 |
GParamSpec *spec, |
| 334 |
FpDevice *device) |
| 335 |
{ |
| 336 |
FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (rdev); |
| 337 |
|
| 338 |
if (fp_device_get_scan_type (device) == FP_SCAN_TYPE_PRESS) |
| 339 |
fprint_dbus_device_set_scan_type (dbus_dev, "press"); |
| 340 |
else if (fp_device_get_scan_type (device) == FP_SCAN_TYPE_SWIPE) |
| 341 |
fprint_dbus_device_set_scan_type (dbus_dev, "swipe"); |
| 342 |
|
| 343 |
g_debug ("Device %s scan type changed to '%s'", |
| 344 |
fp_device_get_name (device), |
| 345 |
fprint_dbus_device_get_scan_type (dbus_dev)); |
| 346 |
} |
| 347 |
|
| 348 |
static void |
| 349 |
on_finger_status_changed (FprintDevice *rdev, |
| 350 |
GParamSpec *spec, |
| 351 |
FpDevice *device) |
| 352 |
{ |
| 353 |
FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (rdev); |
| 354 |
FpFingerStatusFlags finger_status = fp_device_get_finger_status (device); |
| 355 |
gboolean present, needed; |
| 356 |
|
| 357 |
present = !!(finger_status & FP_FINGER_STATUS_PRESENT); |
| 358 |
fprint_dbus_device_set_finger_present (dbus_dev, present); |
| 359 |
g_debug ("Finger present %d", present); |
| 360 |
|
| 361 |
needed = !!(finger_status & FP_FINGER_STATUS_NEEDED); |
| 362 |
fprint_dbus_device_set_finger_needed (dbus_dev, needed); |
| 363 |
g_debug ("Finger needed %d", needed); |
| 364 |
} |
| 365 |
|
| 366 |
static void |
| 367 |
on_temperature_changed (FprintDevice *rdev, |
| 368 |
GParamSpec *spec, |
| 369 |
FpDevice *device) |
| 370 |
{ |
| 371 |
g_object_notify (G_OBJECT (rdev), "busy"); |
| 372 |
} |
| 373 |
|
| 374 |
static void |
| 375 |
fprint_device_constructed (GObject *object) |
| 376 |
{ |
| 377 |
FprintDevice *rdev = FPRINT_DEVICE (object); |
| 378 |
FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (rdev); |
| 379 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 380 |
|
| 381 |
fprint_dbus_device_set_name (dbus_dev, fp_device_get_name (priv->dev)); |
| 382 |
|
| 383 |
g_signal_connect_object (priv->dev, "notify::scan-type", |
| 384 |
G_CALLBACK (on_scan_type_changed), |
| 385 |
rdev, G_CONNECT_SWAPPED); |
| 386 |
on_scan_type_changed (rdev, NULL, priv->dev); |
| 387 |
|
| 388 |
g_signal_connect_object (priv->dev, "notify::nr-enroll-stages", |
| 389 |
G_CALLBACK (on_nr_enroll_stages_changed), |
| 390 |
rdev, G_CONNECT_SWAPPED); |
| 391 |
on_nr_enroll_stages_changed (rdev, NULL, priv->dev); |
| 392 |
|
| 393 |
g_signal_connect_object (priv->dev, "notify::finger-status", |
| 394 |
G_CALLBACK (on_finger_status_changed), |
| 395 |
rdev, G_CONNECT_SWAPPED); |
| 396 |
on_finger_status_changed (rdev, NULL, priv->dev); |
| 397 |
|
| 398 |
g_signal_connect_object (priv->dev, "notify::temperature", |
| 399 |
G_CALLBACK (on_temperature_changed), |
| 400 |
rdev, G_CONNECT_SWAPPED); |
| 401 |
on_temperature_changed (rdev, NULL, priv->dev); |
| 402 |
|
| 403 |
G_OBJECT_CLASS (fprint_device_parent_class)->constructed (object); |
| 404 |
} |
| 405 |
|
| 406 |
static void |
| 407 |
fprint_device_class_init (FprintDeviceClass *klass) |
| 408 |
{ |
| 409 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 410 |
GParamSpec *pspec; |
| 411 |
|
| 412 |
gobject_class->constructed = fprint_device_constructed; |
| 413 |
gobject_class->dispose = fprint_device_dispose; |
| 414 |
gobject_class->finalize = fprint_device_finalize; |
| 415 |
gobject_class->set_property = fprint_device_set_property; |
| 416 |
gobject_class->get_property = fprint_device_get_property; |
| 417 |
|
| 418 |
pspec = g_param_spec_object ("dev", "Device", |
| 419 |
"Set device construction property", |
| 420 |
FP_TYPE_DEVICE, |
| 421 |
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE); |
| 422 |
g_object_class_install_property (gobject_class, |
| 423 |
FPRINT_DEVICE_CONSTRUCT_DEV, pspec); |
| 424 |
|
| 425 |
pspec = g_param_spec_boolean ("busy", "Busy", |
| 426 |
"Whether the device is in use or too warm", FALSE, |
| 427 |
G_PARAM_READABLE); |
| 428 |
g_object_class_install_property (gobject_class, |
| 429 |
FPRINT_DEVICE_BUSY, pspec); |
| 430 |
|
| 431 |
signals[SIGNAL_VERIFY_STATUS] = |
| 432 |
g_signal_lookup ("verify-status", FPRINT_TYPE_DEVICE); |
| 433 |
signals[SIGNAL_ENROLL_STATUS] = |
| 434 |
g_signal_lookup ("enroll-status", FPRINT_TYPE_DEVICE); |
| 435 |
signals[SIGNAL_VERIFY_FINGER_SELECTED] = |
| 436 |
g_signal_lookup ("verify-finger-selected", FPRINT_TYPE_DEVICE); |
| 437 |
|
| 438 |
quark_auth_user = g_quark_from_static_string ("authorized-user"); |
| 439 |
} |
| 440 |
|
| 441 |
static void |
| 442 |
_unwatch_name (gpointer id) |
| 443 |
{ |
| 444 |
g_bus_unwatch_name (GPOINTER_TO_INT (id)); |
| 445 |
} |
| 446 |
|
| 447 |
static void |
| 448 |
fprint_device_init (FprintDevice *device) |
| 449 |
{ |
| 450 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (device); |
| 451 |
|
| 452 |
priv->id = ++last_id; |
| 453 |
|
| 454 |
/* Setup PolicyKit */ |
| 455 |
priv->auth = polkit_authority_get_sync (NULL, NULL); |
| 456 |
priv->clients = g_hash_table_new_full (g_str_hash, |
| 457 |
g_str_equal, |
| 458 |
g_free, |
| 459 |
_unwatch_name); |
| 460 |
|
| 461 |
g_signal_connect (device, "g-authorize-method", |
| 462 |
G_CALLBACK (action_authorization_handler), |
| 463 |
NULL); |
| 464 |
} |
| 465 |
|
| 466 |
FprintDevice * |
| 467 |
fprint_device_new (FpDevice *dev) |
| 468 |
{ |
| 469 |
return g_object_new (FPRINT_TYPE_DEVICE, "dev", dev, NULL); |
| 470 |
} |
| 471 |
|
| 472 |
guint32 |
| 473 |
_fprint_device_get_id (FprintDevice *rdev) |
| 474 |
{ |
| 475 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 476 |
|
| 477 |
return priv->id; |
| 478 |
} |
| 479 |
|
| 480 |
static void |
| 481 |
suspend_cb (GObject *source_obj, |
| 482 |
GAsyncResult *res, |
| 483 |
gpointer user_data) |
| 484 |
{ |
| 485 |
g_autoptr(GTask) task = user_data; |
| 486 |
GError *error = NULL; |
| 487 |
|
| 488 |
fp_device_suspend_finish (FP_DEVICE (source_obj), res, &error); |
| 489 |
if (error) |
| 490 |
g_task_return_error (task, error); |
| 491 |
else |
| 492 |
g_task_return_boolean (task, TRUE); |
| 493 |
} |
| 494 |
|
| 495 |
static void |
| 496 |
resume_cb (GObject *source_obj, |
| 497 |
GAsyncResult *res, |
| 498 |
gpointer user_data) |
| 499 |
{ |
| 500 |
g_autoptr(GTask) task = user_data; |
| 501 |
GError *error = NULL; |
| 502 |
|
| 503 |
fp_device_resume_finish (FP_DEVICE (source_obj), res, &error); |
| 504 |
if (error) |
| 505 |
g_task_return_error (task, error); |
| 506 |
else |
| 507 |
g_task_return_boolean (task, TRUE); |
| 508 |
} |
| 509 |
|
| 510 |
void |
| 511 |
fprint_device_suspend (FprintDevice *rdev, |
| 512 |
GAsyncReadyCallback callback, |
| 513 |
void *user_data) |
| 514 |
{ |
| 515 |
GTask *task = NULL; |
| 516 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 517 |
|
| 518 |
/* Just forward to libfprint. */ |
| 519 |
|
| 520 |
task = g_task_new (rdev, NULL, callback, user_data); |
| 521 |
fp_device_suspend (priv->dev, NULL, suspend_cb, task); |
| 522 |
} |
| 523 |
|
| 524 |
void |
| 525 |
fprint_device_resume (FprintDevice *rdev, |
| 526 |
GAsyncReadyCallback callback, |
| 527 |
void *user_data) |
| 528 |
{ |
| 529 |
GTask *task = NULL; |
| 530 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 531 |
|
| 532 |
/* Just forward to libfprint. */ |
| 533 |
|
| 534 |
task = g_task_new (rdev, NULL, callback, user_data); |
| 535 |
fp_device_resume (priv->dev, NULL, resume_cb, task); |
| 536 |
} |
| 537 |
|
| 538 |
void |
| 539 |
fprint_device_suspend_finish (FprintDevice *rdev, |
| 540 |
GAsyncResult *res, |
| 541 |
GError **error) |
| 542 |
{ |
| 543 |
g_task_propagate_boolean (G_TASK (res), error); |
| 544 |
} |
| 545 |
|
| 546 |
void |
| 547 |
fprint_device_resume_finish (FprintDevice *rdev, |
| 548 |
GAsyncResult *res, |
| 549 |
GError **error) |
| 550 |
{ |
| 551 |
g_task_propagate_boolean (G_TASK (res), error); |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
static const char * |
| 556 |
fp_finger_to_name (FpFinger finger) |
| 557 |
{ |
| 558 |
if (finger == FP_FINGER_UNKNOWN) |
| 559 |
return "any"; |
| 560 |
if (!FP_FINGER_IS_VALID (finger)) |
| 561 |
return NULL; |
| 562 |
return FINGERS_NAMES[finger]; |
| 563 |
} |
| 564 |
|
| 565 |
static FpFinger |
| 566 |
finger_name_to_fp_finger (const char *finger_name) |
| 567 |
{ |
| 568 |
FpFinger i; |
| 569 |
|
| 570 |
if (finger_name == NULL || *finger_name == '\0' || g_str_equal (finger_name, "any")) |
| 571 |
return FP_FINGER_UNKNOWN; |
| 572 |
|
| 573 |
for (i = FP_FINGER_FIRST; i <= FP_FINGER_LAST; i++) |
| 574 |
if (g_str_equal (finger_name, FINGERS_NAMES[i])) |
| 575 |
return i; |
| 576 |
|
| 577 |
/* Invalid, let's try that */ |
| 578 |
return FP_FINGER_UNKNOWN; |
| 579 |
} |
| 580 |
|
| 581 |
static const char * |
| 582 |
verify_result_to_name (gboolean match, GError *error) |
| 583 |
{ |
| 584 |
if (!error) |
| 585 |
{ |
| 586 |
if (match) |
| 587 |
return "verify-match"; |
| 588 |
else |
| 589 |
return "verify-no-match"; |
| 590 |
} |
| 591 |
|
| 592 |
if (error->domain == FP_DEVICE_RETRY) |
| 593 |
{ |
| 594 |
switch (error->code) |
| 595 |
{ |
| 596 |
case FP_DEVICE_RETRY_TOO_SHORT: |
| 597 |
return "verify-swipe-too-short"; |
| 598 |
|
| 599 |
case FP_DEVICE_RETRY_CENTER_FINGER: |
| 600 |
return "verify-finger-not-centered"; |
| 601 |
|
| 602 |
case FP_DEVICE_RETRY_REMOVE_FINGER: |
| 603 |
return "verify-remove-and-retry"; |
| 604 |
|
| 605 |
case FP_DEVICE_RETRY_TOO_FAST: |
| 606 |
return "verify-too-fast"; |
| 607 |
|
| 608 |
default: |
| 609 |
return "verify-retry-scan"; |
| 610 |
} |
| 611 |
} |
| 612 |
else |
| 613 |
{ |
| 614 |
/* Which errors should be mapped to disconnection? |
| 615 |
* Are drivers/libfprint/fprintd really in agreement here? |
| 616 |
*/ |
| 617 |
if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_PROTO)) |
| 618 |
return "verify-disconnected"; |
| 619 |
else if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_REMOVED)) |
| 620 |
return "verify-disconnected"; |
| 621 |
else if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_TOO_HOT)) |
| 622 |
return "verify-disconnected"; |
| 623 |
else if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) || |
| 624 |
g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_DATA_NOT_FOUND)) |
| 625 |
return "verify-no-match"; |
| 626 |
|
| 627 |
return "verify-unknown-error"; |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
static const char * |
| 632 |
enroll_result_to_name (gboolean completed, gboolean enrolled, GError *error) |
| 633 |
{ |
| 634 |
if (!error) |
| 635 |
{ |
| 636 |
if (!completed) |
| 637 |
return "enroll-stage-passed"; |
| 638 |
else if (enrolled) |
| 639 |
return "enroll-completed"; |
| 640 |
else |
| 641 |
return "enroll-failed"; |
| 642 |
} |
| 643 |
|
| 644 |
if (error->domain == FP_DEVICE_RETRY) |
| 645 |
{ |
| 646 |
switch (error->code) |
| 647 |
{ |
| 648 |
case FP_DEVICE_RETRY_TOO_SHORT: |
| 649 |
return "enroll-swipe-too-short"; |
| 650 |
|
| 651 |
case FP_DEVICE_RETRY_CENTER_FINGER: |
| 652 |
return "enroll-finger-not-centered"; |
| 653 |
|
| 654 |
case FP_DEVICE_RETRY_REMOVE_FINGER: |
| 655 |
return "enroll-remove-and-retry"; |
| 656 |
|
| 657 |
case FP_DEVICE_RETRY_TOO_FAST: |
| 658 |
return "enroll-too-fast"; |
| 659 |
|
| 660 |
default: |
| 661 |
return "enroll-retry-scan"; |
| 662 |
} |
| 663 |
} |
| 664 |
else |
| 665 |
{ |
| 666 |
/* Which errors should be mapped to disconnection? |
| 667 |
* Are drivers/libfprint/fprintd really in agreement here? |
| 668 |
*/ |
| 669 |
if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_PROTO)) |
| 670 |
return "enroll-disconnected"; |
| 671 |
else if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_REMOVED)) |
| 672 |
return "enroll-disconnected"; |
| 673 |
else if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_TOO_HOT)) |
| 674 |
return "enroll-disconnected"; |
| 675 |
else if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_DATA_FULL)) |
| 676 |
return "enroll-data-full"; |
| 677 |
else if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_DATA_DUPLICATE)) |
| 678 |
return "enroll-duplicate"; |
| 679 |
else if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
| 680 |
return "enroll-failed"; |
| 681 |
|
| 682 |
return "enroll-unknown-error"; |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
static FprintDevicePermission |
| 687 |
get_permissions_for_invocation (GDBusMethodInvocation *invocation) |
| 688 |
{ |
| 689 |
FprintDevicePermission required_perms; |
| 690 |
const char *method_name; |
| 691 |
|
| 692 |
required_perms = FPRINT_DEVICE_PERMISSION_NONE; |
| 693 |
method_name = g_dbus_method_invocation_get_method_name (invocation); |
| 694 |
|
| 695 |
if (g_str_equal (method_name, "Claim")) |
| 696 |
{ |
| 697 |
required_perms |= FPRINT_DEVICE_PERMISSION_VERIFY; |
| 698 |
required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; |
| 699 |
} |
| 700 |
else if (g_str_equal (method_name, "DeleteEnrolledFinger")) |
| 701 |
{ |
| 702 |
required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; |
| 703 |
} |
| 704 |
else if (g_str_equal (method_name, "DeleteEnrolledFingers")) |
| 705 |
{ |
| 706 |
required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; |
| 707 |
} |
| 708 |
else if (g_str_equal (method_name, "DeleteEnrolledFingers2")) |
| 709 |
{ |
| 710 |
required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; |
| 711 |
} |
| 712 |
else if (g_str_equal (method_name, "EnrollStart")) |
| 713 |
{ |
| 714 |
required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; |
| 715 |
} |
| 716 |
else if (g_str_equal (method_name, "ListEnrolledFingers")) |
| 717 |
{ |
| 718 |
required_perms |= FPRINT_DEVICE_PERMISSION_VERIFY; |
| 719 |
} |
| 720 |
else if (g_str_equal (method_name, "VerifyStart")) |
| 721 |
{ |
| 722 |
required_perms |= FPRINT_DEVICE_PERMISSION_VERIFY; |
| 723 |
} |
| 724 |
else if (g_str_equal (method_name, "Release")) |
| 725 |
{ |
| 726 |
} |
| 727 |
else if (g_str_equal (method_name, "EnrollStop")) |
| 728 |
{ |
| 729 |
} |
| 730 |
else if (g_str_equal (method_name, "VerifyStop")) |
| 731 |
{ |
| 732 |
/* Don't require permissiong for for release/stop operations. |
| 733 |
* We are authenticated already if we could start, and we don't |
| 734 |
* want to end up authorizing interactively again. |
| 735 |
*/ |
| 736 |
} |
| 737 |
else |
| 738 |
{ |
| 739 |
g_assert_not_reached (); |
| 740 |
} |
| 741 |
|
| 742 |
return required_perms; |
| 743 |
} |
| 744 |
|
| 745 |
static FprintDeviceClaimState |
| 746 |
get_claim_state_for_invocation (GDBusMethodInvocation *invocation) |
| 747 |
{ |
| 748 |
const char *method_name; |
| 749 |
|
| 750 |
method_name = g_dbus_method_invocation_get_method_name (invocation); |
| 751 |
|
| 752 |
if (g_str_equal (method_name, "Claim")) |
| 753 |
return STATE_UNCLAIMED; |
| 754 |
else if (g_str_equal (method_name, "DeleteEnrolledFingers")) |
| 755 |
return STATE_AUTO_CLAIM; |
| 756 |
else if (g_str_equal (method_name, "ListEnrolledFingers")) |
| 757 |
return STATE_ANYTIME; |
| 758 |
|
| 759 |
return STATE_CLAIMED; |
| 760 |
} |
| 761 |
|
| 762 |
static gboolean |
| 763 |
_fprint_device_check_claimed (FprintDevice *rdev, |
| 764 |
GDBusMethodInvocation *invocation, |
| 765 |
GError **error) |
| 766 |
{ |
| 767 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 768 |
|
| 769 |
g_autoptr(SessionData) session = NULL; |
| 770 |
FprintDeviceClaimState requested_state; |
| 771 |
const char *sender; |
| 772 |
|
| 773 |
requested_state = get_claim_state_for_invocation (invocation); |
| 774 |
|
| 775 |
if (requested_state == STATE_ANYTIME) |
| 776 |
return TRUE; |
| 777 |
|
| 778 |
session = session_data_get (priv); |
| 779 |
if (requested_state == STATE_AUTO_CLAIM) |
| 780 |
requested_state = session ? STATE_CLAIMED : STATE_UNCLAIMED; |
| 781 |
|
| 782 |
if (requested_state == STATE_UNCLAIMED) |
| 783 |
{ |
| 784 |
/* Is it already claimed? */ |
| 785 |
if (!session) |
| 786 |
return TRUE; |
| 787 |
|
| 788 |
g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, |
| 789 |
"Device was already claimed"); |
| 790 |
return FALSE; |
| 791 |
} |
| 792 |
|
| 793 |
g_assert (requested_state == STATE_CLAIMED); |
| 794 |
|
| 795 |
/* The device wasn't claimed, exit */ |
| 796 |
if (session == NULL) |
| 797 |
{ |
| 798 |
g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_CLAIM_DEVICE, |
| 799 |
_("Device was not claimed before use")); |
| 800 |
return FALSE; |
| 801 |
} |
| 802 |
|
| 803 |
sender = g_dbus_method_invocation_get_sender (invocation); |
| 804 |
|
| 805 |
if (!g_str_equal (sender, session->sender) || session->invocation != NULL) |
| 806 |
{ |
| 807 |
g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, |
| 808 |
_("Device already in use by another user")); |
| 809 |
return FALSE; |
| 810 |
} |
| 811 |
|
| 812 |
return TRUE; |
| 813 |
} |
| 814 |
|
| 815 |
static gboolean |
| 816 |
_fprint_device_check_polkit_for_action (FprintDevice *rdev, |
| 817 |
GDBusMethodInvocation *invocation, |
| 818 |
const char *action, |
| 819 |
GError **error) |
| 820 |
{ |
| 821 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 822 |
const char *sender; |
| 823 |
|
| 824 |
g_autoptr(GError) local_error = NULL; |
| 825 |
g_autoptr(PolkitAuthorizationResult) result = NULL; |
| 826 |
g_autoptr(PolkitSubject) subject = NULL; |
| 827 |
|
| 828 |
/* Check that caller is privileged */ |
| 829 |
sender = g_dbus_method_invocation_get_sender (invocation); |
| 830 |
subject = polkit_system_bus_name_new (sender); |
| 831 |
|
| 832 |
result = polkit_authority_check_authorization_sync (priv->auth, |
| 833 |
subject, |
| 834 |
action, |
| 835 |
NULL, |
| 836 |
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 837 |
NULL, &local_error); |
| 838 |
if (result == NULL) |
| 839 |
{ |
| 840 |
g_set_error (error, FPRINT_ERROR, |
| 841 |
FPRINT_ERROR_PERMISSION_DENIED, |
| 842 |
"Not Authorized: %s", local_error->message); |
| 843 |
return FALSE; |
| 844 |
} |
| 845 |
|
| 846 |
if (!polkit_authorization_result_get_is_authorized (result)) |
| 847 |
{ |
| 848 |
g_set_error (error, FPRINT_ERROR, |
| 849 |
FPRINT_ERROR_PERMISSION_DENIED, |
| 850 |
"Not Authorized: %s", action); |
| 851 |
return FALSE; |
| 852 |
} |
| 853 |
|
| 854 |
return TRUE; |
| 855 |
} |
| 856 |
|
| 857 |
static gboolean |
| 858 |
fprint_device_check_polkit_for_permissions (FprintDevice *rdev, |
| 859 |
GDBusMethodInvocation *invocation, |
| 860 |
FprintDevicePermission permissions, |
| 861 |
GError **error) |
| 862 |
{ |
| 863 |
g_autoptr(GFlagsClass) permission_flags = NULL; |
| 864 |
unsigned i; |
| 865 |
|
| 866 |
if (permissions == FPRINT_DEVICE_PERMISSION_NONE) |
| 867 |
return TRUE; |
| 868 |
|
| 869 |
permission_flags = g_type_class_ref (FPRINT_TYPE_DEVICE_PERMISSION); |
| 870 |
|
| 871 |
for (i = 0; i < permission_flags->n_values; ++i) |
| 872 |
{ |
| 873 |
GFlagsValue *value = &permission_flags->values[i]; |
| 874 |
const char *action; |
| 875 |
|
| 876 |
if (!(value->value & permissions)) |
| 877 |
continue; |
| 878 |
|
| 879 |
action = value->value_nick; |
| 880 |
g_debug ("Getting authorization to perform Polkit action %s", |
| 881 |
action); |
| 882 |
|
| 883 |
g_clear_error (error); |
| 884 |
if (_fprint_device_check_polkit_for_action (rdev, invocation, |
| 885 |
action, error)) |
| 886 |
return TRUE; |
| 887 |
} |
| 888 |
|
| 889 |
g_assert (!error || *error); |
| 890 |
return FALSE; |
| 891 |
} |
| 892 |
|
| 893 |
static char * |
| 894 |
_fprint_device_check_for_username (FprintDevice *rdev, |
| 895 |
GDBusMethodInvocation *invocation, |
| 896 |
const char *username, |
| 897 |
GError **error) |
| 898 |
{ |
| 899 |
g_autoptr(GVariant) ret = NULL; |
| 900 |
g_autoptr(GError) local_error = NULL; |
| 901 |
GDBusConnection *connection; |
| 902 |
const char *sender; |
| 903 |
struct passwd *user; |
| 904 |
guint32 uid; |
| 905 |
|
| 906 |
/* Get details about the current sender, and username/uid */ |
| 907 |
connection = g_dbus_method_invocation_get_connection (invocation); |
| 908 |
sender = g_dbus_method_invocation_get_sender (invocation); |
| 909 |
|
| 910 |
ret = g_dbus_connection_call_sync (connection, |
| 911 |
"org.freedesktop.DBus", |
| 912 |
"/org/freedesktop/DBus", |
| 913 |
"org.freedesktop.DBus", |
| 914 |
"GetConnectionUnixUser", |
| 915 |
g_variant_new ("(s)", sender), |
| 916 |
NULL, G_DBUS_CALL_FLAGS_NONE, -1, |
| 917 |
NULL, &local_error); |
| 918 |
|
| 919 |
if (!ret) |
| 920 |
{ |
| 921 |
g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_INTERNAL, |
| 922 |
"Could not get connection unix user ID: %s", |
| 923 |
local_error->message); |
| 924 |
return NULL; |
| 925 |
} |
| 926 |
|
| 927 |
g_variant_get (ret, "(u)", &uid); |
| 928 |
user = getpwuid (uid); |
| 929 |
if (user == NULL) |
| 930 |
{ |
| 931 |
g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_INTERNAL, |
| 932 |
"Failed to get information about user UID %u", uid); |
| 933 |
return NULL; |
| 934 |
} |
| 935 |
|
| 936 |
/* The current user is usually allowed to access their |
| 937 |
* own data, this should be followed by PolicyKit checks |
| 938 |
* anyway */ |
| 939 |
if (username == NULL || *username == '\0' || g_str_equal (username, user->pw_name)) |
| 940 |
return g_strdup (user->pw_name); |
| 941 |
|
| 942 |
/* If we're not allowed to set a different username, |
| 943 |
* then fail */ |
| 944 |
if (!fprint_device_check_polkit_for_permissions (rdev, invocation, |
| 945 |
FPRINT_DEVICE_PERMISSION_SETUSERNAME, |
| 946 |
error)) |
| 947 |
return NULL; |
| 948 |
|
| 949 |
return g_strdup (username); |
| 950 |
} |
| 951 |
|
| 952 |
static void |
| 953 |
_fprint_device_client_vanished (GDBusConnection *connection, |
| 954 |
const char *name, |
| 955 |
FprintDevice *rdev) |
| 956 |
{ |
| 957 |
g_autoptr(GError) error = NULL; |
| 958 |
g_autoptr(SessionData) session = NULL; |
| 959 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 960 |
|
| 961 |
session = session_data_get (priv); |
| 962 |
|
| 963 |
/* Was that the client that claimed the device? */ |
| 964 |
if (session != NULL && |
| 965 |
g_strcmp0 (session->sender, name) == 0) |
| 966 |
{ |
| 967 |
g_cancellable_cancel (priv->current_cancellable); |
| 968 |
|
| 969 |
if (!priv->current_cancellable) |
| 970 |
{ |
| 971 |
/* This isn't optimal, but for verify/identify/enroll we expect the stop |
| 972 |
* command. And we use current_cancellable as a flag to know that the |
| 973 |
* underlying operation has finished already. |
| 974 |
* If it has finished, unset the current_action. */ |
| 975 |
switch (priv->current_action) |
| 976 |
{ |
| 977 |
case ACTION_VERIFY: |
| 978 |
case ACTION_IDENTIFY: |
| 979 |
case ACTION_ENROLL: |
| 980 |
priv->current_action = ACTION_NONE; |
| 981 |
break; |
| 982 |
|
| 983 |
default: |
| 984 |
break; |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
while (priv->current_action != ACTION_NONE) |
| 989 |
g_main_context_iteration (NULL, TRUE); |
| 990 |
|
| 991 |
/* The session may have disappeared at this point if the device |
| 992 |
* was already closing. */ |
| 993 |
g_clear_pointer (&session, session_data_unref); |
| 994 |
session = session_data_get (priv); |
| 995 |
if (session && !fp_device_close_sync (priv->dev, NULL, &error)) |
| 996 |
g_critical ("Error closing device after disconnect: %s", error->message); |
| 997 |
|
| 998 |
session_data_set_new (priv, NULL, NULL); |
| 999 |
} |
| 1000 |
g_hash_table_remove (priv->clients, name); |
| 1001 |
|
| 1002 |
if (g_hash_table_size (priv->clients) == 0) |
| 1003 |
g_object_notify (G_OBJECT (rdev), "busy"); |
| 1004 |
} |
| 1005 |
|
| 1006 |
static void |
| 1007 |
_fprint_device_add_client (FprintDevice *rdev, const char *sender) |
| 1008 |
{ |
| 1009 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1010 |
guint id; |
| 1011 |
|
| 1012 |
id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->clients, sender)); |
| 1013 |
if (id == 0) |
| 1014 |
{ |
| 1015 |
id = g_bus_watch_name (G_BUS_TYPE_SYSTEM, |
| 1016 |
sender, |
| 1017 |
G_BUS_NAME_WATCHER_FLAGS_NONE, |
| 1018 |
NULL, |
| 1019 |
(GBusNameVanishedCallback) _fprint_device_client_vanished, |
| 1020 |
rdev, |
| 1021 |
NULL); |
| 1022 |
g_hash_table_insert (priv->clients, g_strdup (sender), GUINT_TO_POINTER (id)); |
| 1023 |
g_object_notify (G_OBJECT (rdev), "busy"); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
static void |
| 1028 |
dev_open_cb (FpDevice *dev, GAsyncResult *res, void *user_data) |
| 1029 |
{ |
| 1030 |
g_autoptr(GError) error = NULL; |
| 1031 |
FprintDevice *rdev = user_data; |
| 1032 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1033 |
|
| 1034 |
g_autoptr(SessionData) session = NULL; |
| 1035 |
g_autoptr(GDBusMethodInvocation) invocation = NULL; |
| 1036 |
g_autoptr(FprintDeviceActionUnset) action_unset = NULL; |
| 1037 |
|
| 1038 |
action_unset = rdev; |
| 1039 |
session = session_data_get (priv); |
| 1040 |
invocation = g_steal_pointer (&session->invocation); |
| 1041 |
|
| 1042 |
if (!fp_device_open_finish (dev, res, &error)) |
| 1043 |
{ |
| 1044 |
g_autoptr(GError) dbus_error = NULL; |
| 1045 |
|
| 1046 |
dbus_error = g_error_new (FPRINT_ERROR, |
| 1047 |
FPRINT_ERROR_INTERNAL, |
| 1048 |
"Open failed with error: %s", error->message); |
| 1049 |
g_dbus_method_invocation_return_gerror (invocation, dbus_error); |
| 1050 |
session_data_set_new (priv, NULL, NULL); |
| 1051 |
return; |
| 1052 |
} |
| 1053 |
|
| 1054 |
g_debug ("claimed device %d", priv->id); |
| 1055 |
|
| 1056 |
fprint_dbus_device_complete_claim (FPRINT_DBUS_DEVICE (rdev), |
| 1057 |
invocation); |
| 1058 |
} |
| 1059 |
|
| 1060 |
static gboolean |
| 1061 |
fprintd_device_authorize_user (FprintDevice *rdev, |
| 1062 |
GDBusMethodInvocation *invocation, |
| 1063 |
GError **error) |
| 1064 |
{ |
| 1065 |
GVariant *params = NULL; |
| 1066 |
const char *username = NULL; |
| 1067 |
g_autofree char *user = NULL; |
| 1068 |
|
| 1069 |
params = g_dbus_method_invocation_get_parameters (invocation); |
| 1070 |
g_assert (g_variant_n_children (params) == 1); |
| 1071 |
g_variant_get (params, "(&s)", &username); |
| 1072 |
g_assert (username); |
| 1073 |
|
| 1074 |
user = _fprint_device_check_for_username (rdev, |
| 1075 |
invocation, |
| 1076 |
username, |
| 1077 |
error); |
| 1078 |
if (user == NULL) |
| 1079 |
return FALSE; |
| 1080 |
|
| 1081 |
/* We keep the user attached to the invocation as it may not be the same |
| 1082 |
* of the requested one, in case an empty one was passed. |
| 1083 |
* Given that now we may have multiple cuncurrent requests, it wouldn't |
| 1084 |
* be safe to add another member to the priv, as it would need even more |
| 1085 |
* multi-thread checks around, and over-complicate things. |
| 1086 |
*/ |
| 1087 |
g_object_set_qdata_full (G_OBJECT (invocation), quark_auth_user, |
| 1088 |
g_steal_pointer (&user), g_free); |
| 1089 |
|
| 1090 |
return TRUE; |
| 1091 |
} |
| 1092 |
|
| 1093 |
static gboolean |
| 1094 |
fprint_device_claim (FprintDBusDevice *dbus_dev, |
| 1095 |
GDBusMethodInvocation *invocation, |
| 1096 |
const char *username) |
| 1097 |
{ |
| 1098 |
FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); |
| 1099 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1100 |
|
| 1101 |
g_autoptr(SessionData) session = NULL; |
| 1102 |
g_autoptr(GError) error = NULL; |
| 1103 |
char *sender, *user; |
| 1104 |
|
| 1105 |
if (!_fprint_device_check_claimed (rdev, invocation, &error)) |
| 1106 |
{ |
| 1107 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 1108 |
return TRUE; |
| 1109 |
} |
| 1110 |
|
| 1111 |
user = g_object_steal_qdata (G_OBJECT (invocation), quark_auth_user); |
| 1112 |
g_assert (user); |
| 1113 |
g_assert (g_str_equal (username, "") || g_str_equal (user, username)); |
| 1114 |
|
| 1115 |
sender = g_strdup (g_dbus_method_invocation_get_sender (invocation)); |
| 1116 |
_fprint_device_add_client (rdev, sender); |
| 1117 |
|
| 1118 |
session = session_data_set_new (priv, g_steal_pointer (&sender), g_steal_pointer (&user)); |
| 1119 |
session->invocation = g_object_ref (invocation); |
| 1120 |
|
| 1121 |
g_debug ("user '%s' claiming the device: %d", session->username, priv->id); |
| 1122 |
|
| 1123 |
priv->current_action = ACTION_OPEN; |
| 1124 |
fp_device_open (priv->dev, NULL, (GAsyncReadyCallback) dev_open_cb, rdev); |
| 1125 |
|
| 1126 |
return TRUE; |
| 1127 |
} |
| 1128 |
|
| 1129 |
static void |
| 1130 |
dev_close_cb (FpDevice *dev, GAsyncResult *res, void *user_data) |
| 1131 |
{ |
| 1132 |
g_autoptr(GError) error = NULL; |
| 1133 |
FprintDevice *rdev = user_data; |
| 1134 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1135 |
|
| 1136 |
g_autoptr(SessionData) session = NULL; |
| 1137 |
g_autoptr(GDBusMethodInvocation) invocation = NULL; |
| 1138 |
g_autoptr(FprintDeviceActionUnset) action_unset = NULL; |
| 1139 |
|
| 1140 |
session = session_data_get (priv); |
| 1141 |
session_data_set_new (priv, NULL, NULL); |
| 1142 |
invocation = g_steal_pointer (&session->invocation); |
| 1143 |
action_unset = rdev; |
| 1144 |
|
| 1145 |
if (!fp_device_close_finish (dev, res, &error)) |
| 1146 |
{ |
| 1147 |
g_autoptr(GError) dbus_error = NULL; |
| 1148 |
|
| 1149 |
dbus_error = g_error_new (FPRINT_ERROR, |
| 1150 |
FPRINT_ERROR_INTERNAL, |
| 1151 |
"Release failed with error: %s", error->message); |
| 1152 |
g_dbus_method_invocation_return_gerror (invocation, dbus_error); |
| 1153 |
return; |
| 1154 |
} |
| 1155 |
|
| 1156 |
g_debug ("released device %d", priv->id); |
| 1157 |
|
| 1158 |
fprint_dbus_device_complete_release (FPRINT_DBUS_DEVICE (rdev), |
| 1159 |
invocation); |
| 1160 |
} |
| 1161 |
|
| 1162 |
static gboolean |
| 1163 |
fprint_device_release (FprintDBusDevice *dbus_dev, |
| 1164 |
GDBusMethodInvocation *invocation) |
| 1165 |
{ |
| 1166 |
g_autoptr(GError) error = NULL; |
| 1167 |
g_autoptr(SessionData) session = NULL; |
| 1168 |
FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); |
| 1169 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1170 |
|
| 1171 |
if (!_fprint_device_check_claimed (rdev, invocation, &error)) |
| 1172 |
{ |
| 1173 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 1174 |
return TRUE; |
| 1175 |
} |
| 1176 |
|
| 1177 |
if (priv->current_cancellable) |
| 1178 |
{ |
| 1179 |
if (priv->current_action == ACTION_ENROLL) |
| 1180 |
g_warning ("Enrollment was in progress, stopping it"); |
| 1181 |
else if (priv->current_action == ACTION_IDENTIFY || |
| 1182 |
priv->current_action == ACTION_VERIFY) |
| 1183 |
g_warning ("Verification was in progress, stopping it"); |
| 1184 |
else if (priv->current_action == ACTION_DELETE) |
| 1185 |
g_warning ("Deletion was in progress, stopping it"); |
| 1186 |
|
| 1187 |
g_cancellable_cancel (priv->current_cancellable); |
| 1188 |
while (priv->current_action != ACTION_NONE) |
| 1189 |
g_main_context_iteration (NULL, TRUE); |
| 1190 |
} |
| 1191 |
|
| 1192 |
session = session_data_get (priv); |
| 1193 |
|
| 1194 |
/* We iterated the mainloop, the session may have disappeared already. */ |
| 1195 |
if (!session) |
| 1196 |
{ |
| 1197 |
fprint_dbus_device_complete_release (FPRINT_DBUS_DEVICE (rdev), invocation); |
| 1198 |
return TRUE; |
| 1199 |
} |
| 1200 |
|
| 1201 |
session->invocation = g_object_ref (invocation); |
| 1202 |
|
| 1203 |
priv->current_action = ACTION_CLOSE; |
| 1204 |
fp_device_close (priv->dev, NULL, (GAsyncReadyCallback) dev_close_cb, rdev); |
| 1205 |
|
| 1206 |
return TRUE; |
| 1207 |
} |
| 1208 |
|
| 1209 |
/* NOTE: This should probably be moved to the storage layer. */ |
| 1210 |
static GPtrArray * |
| 1211 |
load_user_prints (FprintDevice *rdev, |
| 1212 |
const char *username) |
| 1213 |
{ |
| 1214 |
g_autoptr(GPtrArray) res = g_ptr_array_new_with_free_func (g_object_unref); |
| 1215 |
g_autoptr(GSList) fingers = NULL; |
| 1216 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1217 |
GSList *l; |
| 1218 |
|
| 1219 |
fingers = store.discover_prints (priv->dev, username); |
| 1220 |
|
| 1221 |
for (l = fingers; l; l = l->next) |
| 1222 |
{ |
| 1223 |
g_autoptr(FpPrint) print = NULL; |
| 1224 |
|
| 1225 |
store.print_data_load (priv->dev, |
| 1226 |
GPOINTER_TO_UINT (l->data), |
| 1227 |
username, |
| 1228 |
&print); |
| 1229 |
|
| 1230 |
if (!print) |
| 1231 |
continue; |
| 1232 |
|
| 1233 |
g_ptr_array_add (res, g_steal_pointer (&print)); |
| 1234 |
} |
| 1235 |
|
| 1236 |
return g_steal_pointer (&res); |
| 1237 |
} |
| 1238 |
|
| 1239 |
static GPtrArray * |
| 1240 |
load_all_prints (FprintDevice *rdev) |
| 1241 |
{ |
| 1242 |
g_autoptr(GPtrArray) res = g_ptr_array_new_with_free_func (g_object_unref); |
| 1243 |
GSList *user, *users = NULL; |
| 1244 |
guint i; |
| 1245 |
|
| 1246 |
users = store.discover_users (); |
| 1247 |
|
| 1248 |
for (user = users; user; user = user->next) |
| 1249 |
{ |
| 1250 |
const char *username = user->data; |
| 1251 |
g_autoptr(GPtrArray) prints = load_user_prints (rdev, username); |
| 1252 |
|
| 1253 |
/* TODO: Use g_ptr_array_extend_and_steal with GLib >= 2.62 */ |
| 1254 |
for (i = 0; i < prints->len; i++) |
| 1255 |
g_ptr_array_add (res, g_object_ref (g_ptr_array_index (prints, i))); |
| 1256 |
} |
| 1257 |
|
| 1258 |
g_slist_free_full (users, g_free); |
| 1259 |
|
| 1260 |
return g_steal_pointer (&res); |
| 1261 |
} |
| 1262 |
|
| 1263 |
static void |
| 1264 |
report_verify_status (FprintDevice *rdev, |
| 1265 |
gboolean match, |
| 1266 |
GError *error) |
| 1267 |
{ |
| 1268 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1269 |
const char *result = verify_result_to_name (match, error); |
| 1270 |
|
| 1271 |
g_autoptr(SessionData) session = NULL; |
| 1272 |
gboolean done; |
| 1273 |
|
| 1274 |
done = (error == NULL || error->domain != FP_DEVICE_RETRY); |
| 1275 |
|
| 1276 |
session = session_data_get (priv); |
| 1277 |
|
| 1278 |
if (done && session->verify_status_reported) |
| 1279 |
{ |
| 1280 |
/* It is completely fine for cancellation to occur after a |
| 1281 |
* result has been reported. */ |
| 1282 |
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
| 1283 |
g_warning ("Verify status already reported. Ignoring %s", result); |
| 1284 |
return; |
| 1285 |
} |
| 1286 |
|
| 1287 |
g_debug ("report_verify_status: result %s", result); |
| 1288 |
g_signal_emit (rdev, signals[SIGNAL_VERIFY_STATUS], 0, result, done); |
| 1289 |
|
| 1290 |
if (done) |
| 1291 |
session->verify_status_reported = TRUE; |
| 1292 |
} |
| 1293 |
|
| 1294 |
static void |
| 1295 |
check_local_storage (FprintDevice *rdev, |
| 1296 |
gboolean found_match, |
| 1297 |
GError *error) |
| 1298 |
{ |
| 1299 |
g_autoptr(GError) err = NULL; |
| 1300 |
g_autoptr(GPtrArray) device_prints = NULL; |
| 1301 |
g_autoptr(GPtrArray) host_prints = NULL; |
| 1302 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1303 |
unsigned i; |
| 1304 |
|
| 1305 |
g_return_if_fail (priv->current_action == ACTION_VERIFY || |
| 1306 |
priv->current_action == ACTION_IDENTIFY); |
| 1307 |
|
| 1308 |
/* This only ever sense if the device can list prints. */ |
| 1309 |
if (!fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_STORAGE_LIST)) |
| 1310 |
return; |
| 1311 |
|
| 1312 |
/* We do not have any proper driver that correctly reports DATA_NOT_FOUND |
| 1313 |
* errors. Only synaptics, but there the feature is being disabled on the |
| 1314 |
* firmware side. |
| 1315 |
* As such, just always run a test the first time we get a match failure. |
| 1316 |
*/ |
| 1317 |
if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_DATA_NOT_FOUND)) |
| 1318 |
{ |
| 1319 |
if (priv->local_storage_checked) |
| 1320 |
g_warning ("Device %s reported that a passed print did not exist during action %d, but we verified the local storage!", |
| 1321 |
fp_device_get_name (priv->dev), priv->current_action); |
| 1322 |
else |
| 1323 |
g_debug ("Device %s reported that a passed print did not exist during action %d", |
| 1324 |
fp_device_get_name (priv->dev), priv->current_action); |
| 1325 |
} |
| 1326 |
else if (error || priv->local_storage_checked) |
| 1327 |
{ |
| 1328 |
return; |
| 1329 |
} |
| 1330 |
else if (!found_match) |
| 1331 |
{ |
| 1332 |
g_debug ("Device %s failed to match during action %d, verifying local storage", |
| 1333 |
fp_device_get_name (priv->dev), priv->current_action); |
| 1334 |
} |
| 1335 |
else |
| 1336 |
{ |
| 1337 |
return; |
| 1338 |
} |
| 1339 |
|
| 1340 |
priv->local_storage_checked = TRUE; |
| 1341 |
|
| 1342 |
device_prints = fp_device_list_prints_sync (priv->dev, NULL, &err); |
| 1343 |
if (!device_prints) |
| 1344 |
{ |
| 1345 |
g_warning ("Failed to query prints: %s", err->message); |
| 1346 |
return; |
| 1347 |
} |
| 1348 |
|
| 1349 |
host_prints = load_all_prints (rdev); |
| 1350 |
|
| 1351 |
for (i = 0; i < host_prints->len; i++) |
| 1352 |
{ |
| 1353 |
FpPrint *print = g_ptr_array_index (host_prints, i); |
| 1354 |
int r; |
| 1355 |
|
| 1356 |
if (g_ptr_array_find_with_equal_func (device_prints, |
| 1357 |
print, |
| 1358 |
(GEqualFunc) fp_print_equal, |
| 1359 |
NULL)) |
| 1360 |
continue; |
| 1361 |
|
| 1362 |
/* Print not known by device, remove locally */ |
| 1363 |
if ((r = store.print_data_delete (priv->dev, |
| 1364 |
fp_print_get_finger (print), |
| 1365 |
fp_print_get_username (print))) == 0) |
| 1366 |
{ |
| 1367 |
g_message ("Deleted stored finger %d for user %s as it is unknown to device.", |
| 1368 |
fp_print_get_finger (print), |
| 1369 |
fp_print_get_username (print)); |
| 1370 |
} |
| 1371 |
else |
| 1372 |
{ |
| 1373 |
g_warning ("Error deleting finger %d for user %s that is unknown to device: %d!", |
| 1374 |
fp_print_get_finger (print), |
| 1375 |
fp_print_get_username (print), |
| 1376 |
r); |
| 1377 |
} |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
static gboolean |
| 1382 |
can_start_action (FprintDevice *rdev, GError **error) |
| 1383 |
{ |
| 1384 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1385 |
|
| 1386 |
switch (priv->current_action) |
| 1387 |
{ |
| 1388 |
case ACTION_NONE: |
| 1389 |
return TRUE; |
| 1390 |
|
| 1391 |
case ACTION_ENROLL: |
| 1392 |
g_set_error (error, |
| 1393 |
FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, |
| 1394 |
"Enrollment already in progress"); |
| 1395 |
break; |
| 1396 |
|
| 1397 |
case ACTION_IDENTIFY: |
| 1398 |
case ACTION_VERIFY: |
| 1399 |
g_set_error (error, |
| 1400 |
FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, |
| 1401 |
"Verification already in progress"); |
| 1402 |
break; |
| 1403 |
|
| 1404 |
case ACTION_OPEN: |
| 1405 |
g_set_error (error, |
| 1406 |
FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, |
| 1407 |
"Claim already in progress"); |
| 1408 |
break; |
| 1409 |
|
| 1410 |
case ACTION_CLOSE: |
| 1411 |
g_set_error (error, |
| 1412 |
FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, |
| 1413 |
"Release already in progress"); |
| 1414 |
break; |
| 1415 |
|
| 1416 |
case ACTION_DELETE: |
| 1417 |
g_set_error (error, |
| 1418 |
FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, |
| 1419 |
"Delete already in progress"); |
| 1420 |
break; |
| 1421 |
|
| 1422 |
default: /* Fallback only. */ |
| 1423 |
g_assert_not_reached (); |
| 1424 |
g_set_error (error, |
| 1425 |
FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, |
| 1426 |
"Another operation is already in progress"); |
| 1427 |
} |
| 1428 |
|
| 1429 |
return FALSE; |
| 1430 |
} |
| 1431 |
|
| 1432 |
static gboolean |
| 1433 |
can_stop_action (FprintDevice *rdev, |
| 1434 |
FprintDeviceAction action, |
| 1435 |
GError **error) |
| 1436 |
{ |
| 1437 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1438 |
gboolean action_matches; |
| 1439 |
|
| 1440 |
switch (priv->current_action) |
| 1441 |
{ |
| 1442 |
case ACTION_IDENTIFY: |
| 1443 |
case ACTION_VERIFY: |
| 1444 |
action_matches = (action == ACTION_VERIFY || action == ACTION_IDENTIFY); |
| 1445 |
break; |
| 1446 |
|
| 1447 |
default: |
| 1448 |
action_matches = priv->current_action == action; |
| 1449 |
} |
| 1450 |
|
| 1451 |
if (action_matches && !priv->current_cancel_invocation) |
| 1452 |
return TRUE; |
| 1453 |
|
| 1454 |
if (priv->current_action != ACTION_NONE || action_matches) |
| 1455 |
{ |
| 1456 |
g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, |
| 1457 |
"Another operation is already in progress"); |
| 1458 |
return FALSE; |
| 1459 |
} |
| 1460 |
|
| 1461 |
switch (action) |
| 1462 |
{ |
| 1463 |
case ACTION_ENROLL: |
| 1464 |
g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_NO_ACTION_IN_PROGRESS, |
| 1465 |
"No enrollment in progress"); |
| 1466 |
return FALSE; |
| 1467 |
|
| 1468 |
case ACTION_VERIFY: |
| 1469 |
g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_NO_ACTION_IN_PROGRESS, |
| 1470 |
"No verification in progress"); |
| 1471 |
return FALSE; |
| 1472 |
|
| 1473 |
default: |
| 1474 |
g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_NO_ACTION_IN_PROGRESS, |
| 1475 |
"No action in progress"); |
| 1476 |
return FALSE; |
| 1477 |
} |
| 1478 |
} |
| 1479 |
|
| 1480 |
static void |
| 1481 |
stoppable_action_completed (FprintDevice *rdev) |
| 1482 |
{ |
| 1483 |
g_autoptr(SessionData) session = NULL; |
| 1484 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1485 |
FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (rdev); |
| 1486 |
|
| 1487 |
session = session_data_get (priv); |
| 1488 |
|
| 1489 |
/* Return the cancellation or reset action right away if vanished. */ |
| 1490 |
if (priv->current_cancel_invocation) |
| 1491 |
{ |
| 1492 |
switch (priv->current_action) |
| 1493 |
{ |
| 1494 |
case ACTION_VERIFY: |
| 1495 |
case ACTION_IDENTIFY: |
| 1496 |
fprint_dbus_device_complete_verify_stop (dbus_dev, |
| 1497 |
g_steal_pointer (&priv->current_cancel_invocation)); |
| 1498 |
break; |
| 1499 |
|
| 1500 |
case ACTION_ENROLL: |
| 1501 |
fprint_dbus_device_complete_enroll_stop (dbus_dev, |
| 1502 |
g_steal_pointer (&priv->current_cancel_invocation)); |
| 1503 |
break; |
| 1504 |
|
| 1505 |
default: |
| 1506 |
g_assert_not_reached (); |
| 1507 |
} |
| 1508 |
|
| 1509 |
priv->current_action = ACTION_NONE; |
| 1510 |
session->verify_status_reported = FALSE; |
| 1511 |
} |
| 1512 |
else if (g_cancellable_is_cancelled (priv->current_cancellable)) |
| 1513 |
{ |
| 1514 |
priv->current_action = ACTION_NONE; |
| 1515 |
session->verify_status_reported = FALSE; |
| 1516 |
} |
| 1517 |
|
| 1518 |
g_clear_object (&priv->current_cancellable); |
| 1519 |
} |
| 1520 |
|
| 1521 |
static void |
| 1522 |
stoppable_action_stop (FprintDevice *rdev, |
| 1523 |
GDBusMethodInvocation *invocation) |
| 1524 |
{ |
| 1525 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1526 |
|
| 1527 |
g_assert (priv->current_cancel_invocation == NULL); |
| 1528 |
|
| 1529 |
priv->current_cancel_invocation = invocation; |
| 1530 |
|
| 1531 |
/* We return only when the action was cancelled */ |
| 1532 |
if (priv->current_cancellable) |
| 1533 |
g_cancellable_cancel (priv->current_cancellable); |
| 1534 |
else |
| 1535 |
stoppable_action_completed (rdev); |
| 1536 |
} |
| 1537 |
|
| 1538 |
static void |
| 1539 |
match_cb (FpDevice *device, |
| 1540 |
FpPrint *match, |
| 1541 |
FpPrint *print, |
| 1542 |
gpointer user_data, |
| 1543 |
GError *error) |
| 1544 |
{ |
| 1545 |
FprintDevice *rdev = user_data; |
| 1546 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1547 |
gboolean matched; |
| 1548 |
gboolean cancelled; |
| 1549 |
|
| 1550 |
g_assert_true (error == NULL || error->domain == FP_DEVICE_RETRY); |
| 1551 |
|
| 1552 |
cancelled = g_cancellable_is_cancelled (priv->current_cancellable); |
| 1553 |
matched = match != NULL && cancelled == FALSE; |
| 1554 |
|
| 1555 |
report_verify_status (rdev, matched, error); |
| 1556 |
} |
| 1557 |
|
| 1558 |
static void |
| 1559 |
verify_cb (FpDevice *dev, GAsyncResult *res, void *user_data) |
| 1560 |
{ |
| 1561 |
g_autoptr(GError) error = NULL; |
| 1562 |
FprintDevice *rdev = user_data; |
| 1563 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1564 |
gboolean success; |
| 1565 |
const char *name; |
| 1566 |
gboolean match; |
| 1567 |
|
| 1568 |
success = fp_device_verify_finish (dev, res, &match, NULL, &error); |
| 1569 |
g_assert (!!success == !error); |
| 1570 |
name = verify_result_to_name (match, error); |
| 1571 |
|
| 1572 |
g_debug ("verify_cb: result %s", name); |
| 1573 |
|
| 1574 |
/* Automatically restart the operation for retry failures */ |
| 1575 |
if (error && error->domain == FP_DEVICE_RETRY) |
| 1576 |
{ |
| 1577 |
fp_device_verify (priv->dev, |
| 1578 |
priv->verify_data, |
| 1579 |
priv->current_cancellable, |
| 1580 |
match_cb, rdev, NULL, |
| 1581 |
(GAsyncReadyCallback) verify_cb, |
| 1582 |
rdev); |
| 1583 |
} |
| 1584 |
else |
| 1585 |
{ |
| 1586 |
g_clear_object (&priv->verify_data); |
| 1587 |
|
| 1588 |
if (error) |
| 1589 |
{ |
| 1590 |
report_verify_status (rdev, FALSE, error); |
| 1591 |
|
| 1592 |
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
| 1593 |
g_warning ("Device reported an error during verify: %s", |
| 1594 |
error->message); |
| 1595 |
} |
| 1596 |
|
| 1597 |
check_local_storage (rdev, match, error); |
| 1598 |
|
| 1599 |
stoppable_action_completed (rdev); |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
static void |
| 1604 |
identify_cb (FpDevice *dev, GAsyncResult *res, void *user_data) |
| 1605 |
{ |
| 1606 |
g_autoptr(GError) error = NULL; |
| 1607 |
g_autoptr(FpPrint) match = NULL; |
| 1608 |
FprintDevice *rdev = user_data; |
| 1609 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1610 |
const char *name; |
| 1611 |
gboolean success; |
| 1612 |
|
| 1613 |
success = fp_device_identify_finish (dev, res, &match, NULL, &error); |
| 1614 |
g_assert (!!success == !error); |
| 1615 |
name = verify_result_to_name (match != NULL, error); |
| 1616 |
|
| 1617 |
g_debug ("identify_cb: result %s", name); |
| 1618 |
|
| 1619 |
/* Automatically restart the operation for retry failures */ |
| 1620 |
if (error && error->domain == FP_DEVICE_RETRY) |
| 1621 |
{ |
| 1622 |
fp_device_identify (priv->dev, |
| 1623 |
priv->identify_data, |
| 1624 |
priv->current_cancellable, |
| 1625 |
match_cb, rdev, NULL, |
| 1626 |
(GAsyncReadyCallback) identify_cb, |
| 1627 |
rdev); |
| 1628 |
} |
| 1629 |
else |
| 1630 |
{ |
| 1631 |
g_clear_pointer (&priv->identify_data, g_ptr_array_unref); |
| 1632 |
|
| 1633 |
if (error) |
| 1634 |
{ |
| 1635 |
report_verify_status (rdev, FALSE, error); |
| 1636 |
|
| 1637 |
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
| 1638 |
g_warning ("Device reported an error during identify: %s", |
| 1639 |
error->message); |
| 1640 |
} |
| 1641 |
|
| 1642 |
check_local_storage (rdev, match != NULL, error); |
| 1643 |
|
| 1644 |
stoppable_action_completed (rdev); |
| 1645 |
} |
| 1646 |
} |
| 1647 |
|
| 1648 |
static gboolean |
| 1649 |
fprint_device_verify_start (FprintDBusDevice *dbus_dev, |
| 1650 |
GDBusMethodInvocation *invocation, |
| 1651 |
const char *finger_name) |
| 1652 |
{ |
| 1653 |
FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); |
| 1654 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1655 |
|
| 1656 |
g_autoptr(GPtrArray) gallery = NULL; |
| 1657 |
g_autoptr(SessionData) session = NULL; |
| 1658 |
g_autoptr(GError) error = NULL; |
| 1659 |
FpFinger finger = finger_name_to_fp_finger (finger_name); |
| 1660 |
|
| 1661 |
if (!_fprint_device_check_claimed (rdev, invocation, &error)) |
| 1662 |
{ |
| 1663 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 1664 |
return TRUE; |
| 1665 |
} |
| 1666 |
|
| 1667 |
session = session_data_get (priv); |
| 1668 |
|
| 1669 |
if (!can_start_action (rdev, &error)) |
| 1670 |
{ |
| 1671 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 1672 |
return TRUE; |
| 1673 |
} |
| 1674 |
|
| 1675 |
if (finger == FP_FINGER_UNKNOWN) |
| 1676 |
{ |
| 1677 |
gallery = load_user_prints (rdev, session->username); |
| 1678 |
|
| 1679 |
if (!gallery->len) |
| 1680 |
{ |
| 1681 |
g_set_error (&error, FPRINT_ERROR, FPRINT_ERROR_NO_ENROLLED_PRINTS, |
| 1682 |
"No fingerprints enrolled"); |
| 1683 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 1684 |
return TRUE; |
| 1685 |
} |
| 1686 |
else if (gallery->len == 1) |
| 1687 |
{ |
| 1688 |
FpPrint *fprint = g_ptr_array_index (gallery, 0); |
| 1689 |
|
| 1690 |
/* If we only have a single finger, then do verify on that. |
| 1691 |
* This also means we report it back correctly to the user. */ |
| 1692 |
finger = fp_print_get_finger (fprint); |
| 1693 |
} |
| 1694 |
else if (fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_IDENTIFY)) |
| 1695 |
{ |
| 1696 |
guint i; |
| 1697 |
|
| 1698 |
for (i = 0; i < gallery->len; i++) |
| 1699 |
{ |
| 1700 |
FpPrint *fprint = g_ptr_array_index (gallery, i); |
| 1701 |
|
| 1702 |
g_debug ("adding finger %s to the gallery", |
| 1703 |
fp_finger_to_name (fp_print_get_finger (fprint))); |
| 1704 |
} |
| 1705 |
} |
| 1706 |
} |
| 1707 |
|
| 1708 |
if (fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_IDENTIFY) && |
| 1709 |
finger == FP_FINGER_UNKNOWN) |
| 1710 |
{ |
| 1711 |
priv->current_action = ACTION_IDENTIFY; |
| 1712 |
|
| 1713 |
g_debug ("start identification device %d", priv->id); |
| 1714 |
priv->current_cancellable = g_cancellable_new (); |
| 1715 |
priv->identify_data = g_ptr_array_ref (gallery); |
| 1716 |
fp_device_identify (priv->dev, gallery, priv->current_cancellable, |
| 1717 |
match_cb, rdev, NULL, |
| 1718 |
(GAsyncReadyCallback) identify_cb, rdev); |
| 1719 |
} |
| 1720 |
else |
| 1721 |
{ |
| 1722 |
g_autoptr(FpPrint) print = NULL; |
| 1723 |
|
| 1724 |
if (gallery) |
| 1725 |
{ |
| 1726 |
/* TODO: Use g_ptr_array_remove_index_fast with GLib >= 2.58 */ |
| 1727 |
print = g_object_ref (g_ptr_array_index (gallery, 0)); |
| 1728 |
g_ptr_array_remove_index_fast (gallery, 0); |
| 1729 |
finger = fp_print_get_finger (print); |
| 1730 |
} |
| 1731 |
else |
| 1732 |
{ |
| 1733 |
store.print_data_load (priv->dev, finger, session->username, &print); |
| 1734 |
} |
| 1735 |
|
| 1736 |
if (!print) |
| 1737 |
{ |
| 1738 |
g_set_error (&error, FPRINT_ERROR, FPRINT_ERROR_NO_ENROLLED_PRINTS, |
| 1739 |
"No such print %d", finger); |
| 1740 |
g_dbus_method_invocation_return_gerror (invocation, |
| 1741 |
error); |
| 1742 |
return TRUE; |
| 1743 |
} |
| 1744 |
|
| 1745 |
g_debug ("start verification device %d finger %s", priv->id, |
| 1746 |
fp_finger_to_name (finger)); |
| 1747 |
|
| 1748 |
priv->current_action = ACTION_VERIFY; |
| 1749 |
priv->current_cancellable = g_cancellable_new (); |
| 1750 |
priv->verify_data = g_object_ref (print); |
| 1751 |
fp_device_verify (priv->dev, print, priv->current_cancellable, |
| 1752 |
match_cb, rdev, NULL, |
| 1753 |
(GAsyncReadyCallback) verify_cb, rdev); |
| 1754 |
} |
| 1755 |
|
| 1756 |
fprint_dbus_device_complete_verify_start (dbus_dev, invocation); |
| 1757 |
|
| 1758 |
/* Emit VerifyFingerSelected telling the front-end which finger |
| 1759 |
* we selected for auth */ |
| 1760 |
g_signal_emit (rdev, signals[SIGNAL_VERIFY_FINGER_SELECTED], |
| 1761 |
0, fp_finger_to_name (finger)); |
| 1762 |
|
| 1763 |
return TRUE; |
| 1764 |
} |
| 1765 |
|
| 1766 |
static gboolean |
| 1767 |
verify_stop_wait_timeout (gpointer data) |
| 1768 |
{ |
| 1769 |
guint *timeout_id = data; |
| 1770 |
|
| 1771 |
*timeout_id = 0; |
| 1772 |
return FALSE; |
| 1773 |
} |
| 1774 |
|
| 1775 |
static gboolean |
| 1776 |
verify_has_completed (FprintDevice *rdev) |
| 1777 |
{ |
| 1778 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1779 |
|
| 1780 |
if (!priv->current_cancellable || |
| 1781 |
g_cancellable_is_cancelled (priv->current_cancellable)) |
| 1782 |
return TRUE; |
| 1783 |
|
| 1784 |
switch (priv->current_action) |
| 1785 |
{ |
| 1786 |
case ACTION_VERIFY: |
| 1787 |
return !priv->verify_data; |
| 1788 |
|
| 1789 |
case ACTION_IDENTIFY: |
| 1790 |
return !priv->identify_data; |
| 1791 |
|
| 1792 |
default: |
| 1793 |
g_assert_not_reached (); |
| 1794 |
} |
| 1795 |
} |
| 1796 |
|
| 1797 |
static gboolean |
| 1798 |
fprint_device_verify_stop (FprintDBusDevice *dbus_dev, |
| 1799 |
GDBusMethodInvocation *invocation) |
| 1800 |
{ |
| 1801 |
FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); |
| 1802 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1803 |
|
| 1804 |
g_autoptr(GError) error = NULL; |
| 1805 |
|
| 1806 |
if (!_fprint_device_check_claimed (rdev, invocation, &error)) |
| 1807 |
{ |
| 1808 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 1809 |
return TRUE; |
| 1810 |
} |
| 1811 |
|
| 1812 |
if (!can_stop_action (rdev, ACTION_VERIFY, &error)) |
| 1813 |
{ |
| 1814 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 1815 |
return TRUE; |
| 1816 |
} |
| 1817 |
|
| 1818 |
if (!verify_has_completed (rdev)) |
| 1819 |
{ |
| 1820 |
g_autoptr(SessionData) session = session_data_get (priv); |
| 1821 |
|
| 1822 |
if (session->verify_status_reported) |
| 1823 |
{ |
| 1824 |
/* If we got a status report we need to delay the cancellation |
| 1825 |
* of the action, leaving the device some more time to complete |
| 1826 |
* the operation (and in case return the real error) before proceed |
| 1827 |
* in cancelling it. |
| 1828 |
* In case Release or client vanished while waiting the invocation |
| 1829 |
* will be handled by stoppable_action_completed() during cancellation |
| 1830 |
*/ |
| 1831 |
g_assert (priv->verify_stop_wait_timeout_id == 0); |
| 1832 |
|
| 1833 |
priv->verify_stop_wait_timeout_id = |
| 1834 |
g_timeout_add_seconds (VERIFY_STOP_DEVICE_WAIT, verify_stop_wait_timeout, |
| 1835 |
&priv->verify_stop_wait_timeout_id); |
| 1836 |
|
| 1837 |
g_assert (priv->current_cancel_invocation == NULL); |
| 1838 |
priv->current_cancel_invocation = invocation; |
| 1839 |
|
| 1840 |
while (priv->verify_stop_wait_timeout_id && !verify_has_completed (rdev)) |
| 1841 |
g_main_context_iteration (NULL, TRUE); |
| 1842 |
|
| 1843 |
g_clear_handle_id (&priv->verify_stop_wait_timeout_id, g_source_remove); |
| 1844 |
|
| 1845 |
if (!priv->current_cancel_invocation) |
| 1846 |
return TRUE; |
| 1847 |
|
| 1848 |
priv->current_cancel_invocation = NULL; |
| 1849 |
} |
| 1850 |
} |
| 1851 |
|
| 1852 |
stoppable_action_stop (rdev, invocation); |
| 1853 |
|
| 1854 |
return TRUE; |
| 1855 |
} |
| 1856 |
|
| 1857 |
static void |
| 1858 |
enroll_progress_cb (FpDevice *dev, |
| 1859 |
gint completed_stages, |
| 1860 |
FpPrint *print, |
| 1861 |
gpointer user_data, |
| 1862 |
GError *error) |
| 1863 |
{ |
| 1864 |
FprintDevice *rdev = user_data; |
| 1865 |
const char *name = enroll_result_to_name (FALSE, FALSE, error); |
| 1866 |
|
| 1867 |
g_debug ("enroll_stage_cb: result %s", name); |
| 1868 |
|
| 1869 |
/* NOTE: We add one more step internally, but we can ignore that here. */ |
| 1870 |
if (completed_stages < fp_device_get_nr_enroll_stages (dev)) |
| 1871 |
g_signal_emit (rdev, signals[SIGNAL_ENROLL_STATUS], 0, name, FALSE); |
| 1872 |
} |
| 1873 |
|
| 1874 |
static gint |
| 1875 |
garbage_collect_sort (gconstpointer a, gconstpointer b) |
| 1876 |
{ |
| 1877 |
/* Prefer */ |
| 1878 |
FpPrint *print_a = *((FpPrint **) a); |
| 1879 |
FpPrint *print_b = *((FpPrint **) b); |
| 1880 |
const GDate *date_a, *date_b; |
| 1881 |
guint32 julian_a = 0; |
| 1882 |
guint32 julian_b = 0; |
| 1883 |
gint32 rand_a, rand_b; |
| 1884 |
|
| 1885 |
date_a = fp_print_get_enroll_date (print_a); |
| 1886 |
date_b = fp_print_get_enroll_date (print_b); |
| 1887 |
|
| 1888 |
if (date_a && g_date_valid (date_a)) |
| 1889 |
julian_a = g_date_get_julian (date_a); |
| 1890 |
if (date_b && g_date_valid (date_b)) |
| 1891 |
julian_b = g_date_get_julian (date_b); |
| 1892 |
|
| 1893 |
/* Sort older prints first. */ |
| 1894 |
if (julian_a != julian_b) |
| 1895 |
return julian_b - julian_a; |
| 1896 |
|
| 1897 |
/* Randomize the order, |
| 1898 |
* do so by sorting on a random number we assign to each print. |
| 1899 |
* Not nice, but gets the job done. |
| 1900 |
*/ |
| 1901 |
rand_a = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (print_a), "sort-rand")); |
| 1902 |
rand_b = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (print_b), "sort-rand")); |
| 1903 |
if (rand_a == 0) |
| 1904 |
{ |
| 1905 |
rand_a = g_random_int_range (1, G_MAXINT32); |
| 1906 |
g_object_set_data (G_OBJECT (print_a), "sort-rand", GINT_TO_POINTER (rand_a)); |
| 1907 |
} |
| 1908 |
|
| 1909 |
if (rand_b == 0) |
| 1910 |
{ |
| 1911 |
rand_b = g_random_int_range (1, G_MAXINT32); |
| 1912 |
g_object_set_data (G_OBJECT (print_b), "sort-rand", GINT_TO_POINTER (rand_b)); |
| 1913 |
} |
| 1914 |
|
| 1915 |
return rand_b - rand_a; |
| 1916 |
} |
| 1917 |
|
| 1918 |
static gboolean |
| 1919 |
try_delete_print (FprintDevice *rdev) |
| 1920 |
{ |
| 1921 |
g_autoptr(GError) error = NULL; |
| 1922 |
g_autoptr(GPtrArray) device_prints = NULL; |
| 1923 |
g_autoptr(GPtrArray) host_prints = NULL; |
| 1924 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1925 |
guint i; |
| 1926 |
|
| 1927 |
device_prints = fp_device_list_prints_sync (priv->dev, NULL, &error); |
| 1928 |
if (!device_prints) |
| 1929 |
{ |
| 1930 |
g_warning ("Failed to query prints: %s", error->message); |
| 1931 |
return FALSE; |
| 1932 |
} |
| 1933 |
|
| 1934 |
g_debug ("Device has %d prints stored", device_prints->len); |
| 1935 |
|
| 1936 |
/* Sort in order of preferred garbage collection. |
| 1937 |
* With randomization if we can't sort them. */ |
| 1938 |
g_ptr_array_sort (device_prints, garbage_collect_sort); |
| 1939 |
|
| 1940 |
host_prints = load_all_prints (rdev); |
| 1941 |
|
| 1942 |
for (i = 0; i < host_prints->len; i++) |
| 1943 |
{ |
| 1944 |
guint index; |
| 1945 |
|
| 1946 |
if (!g_ptr_array_find_with_equal_func (device_prints, |
| 1947 |
g_ptr_array_index (host_prints, i), |
| 1948 |
(GEqualFunc) fp_print_equal, |
| 1949 |
&index)) |
| 1950 |
continue; |
| 1951 |
|
| 1952 |
/* Found an equal print, remove it */ |
| 1953 |
g_ptr_array_remove_index (device_prints, index); |
| 1954 |
} |
| 1955 |
|
| 1956 |
g_debug ("Device has %d prints stored that we do not need", device_prints->len); |
| 1957 |
if (device_prints->len == 0) |
| 1958 |
return FALSE; |
| 1959 |
|
| 1960 |
/* Just delete the first print in the list at this point. |
| 1961 |
* We could be smarter and fetch some more metadata. */ |
| 1962 |
fp_device_delete_print_sync (priv->dev, |
| 1963 |
g_ptr_array_index (device_prints, 0), |
| 1964 |
NULL, |
| 1965 |
&error); |
| 1966 |
|
| 1967 |
if (error) |
| 1968 |
{ |
| 1969 |
g_warning ("Failed to garbage collect a print: %s", error->message); |
| 1970 |
return FALSE; |
| 1971 |
} |
| 1972 |
|
| 1973 |
return TRUE; |
| 1974 |
} |
| 1975 |
|
| 1976 |
#if !GLIB_CHECK_VERSION (2, 63, 3) |
| 1977 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GDate, g_date_free); |
| 1978 |
#endif |
| 1979 |
|
| 1980 |
static FpPrint * |
| 1981 |
fprint_device_create_enroll_template (FprintDevice *rdev, FpFinger finger) |
| 1982 |
{ |
| 1983 |
g_autoptr(SessionData) session = NULL; |
| 1984 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 1985 |
|
| 1986 |
g_autoptr(GDateTime) datetime = NULL; |
| 1987 |
g_autoptr(GDate) date = NULL; |
| 1988 |
FpPrint *template = NULL; |
| 1989 |
gint year, month, day; |
| 1990 |
|
| 1991 |
session = session_data_get (priv); |
| 1992 |
|
| 1993 |
template = fp_print_new (priv->dev); |
| 1994 |
fp_print_set_finger (template, finger); |
| 1995 |
fp_print_set_username (template, session->username); |
| 1996 |
datetime = g_date_time_new_now_local (); |
| 1997 |
g_date_time_get_ymd (datetime, &year, &month, &day); |
| 1998 |
date = g_date_new_dmy (day, month, year); |
| 1999 |
fp_print_set_enroll_date (template, date); |
| 2000 |
|
| 2001 |
return template; |
| 2002 |
} |
| 2003 |
|
| 2004 |
static void |
| 2005 |
enroll_cb (FpDevice *dev, GAsyncResult *res, void *user_data) |
| 2006 |
{ |
| 2007 |
g_autoptr(GError) error = NULL; |
| 2008 |
FprintDevice *rdev = user_data; |
| 2009 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 2010 |
|
| 2011 |
g_autoptr(FpPrint) print = NULL; |
| 2012 |
const char *name; |
| 2013 |
|
| 2014 |
print = fp_device_enroll_finish (dev, res, &error); |
| 2015 |
|
| 2016 |
/* We need to special case the issue where the on device storage |
| 2017 |
* is completely full. In that case, we check whether we can delete |
| 2018 |
* a print that is not coming from us; assuming it is from an old |
| 2019 |
* installation. |
| 2020 |
* We do this synchronously, which is not great but should be good |
| 2021 |
* enough. */ |
| 2022 |
if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_DATA_FULL)) |
| 2023 |
{ |
| 2024 |
g_debug ("Device storage is full"); |
| 2025 |
if (fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_STORAGE_LIST) && |
| 2026 |
try_delete_print (rdev)) |
| 2027 |
{ |
| 2028 |
/* Success? Then restart the operation */ |
| 2029 |
fp_device_enroll (priv->dev, |
| 2030 |
fprint_device_create_enroll_template (rdev, priv->enroll_data), |
| 2031 |
priv->current_cancellable, |
| 2032 |
enroll_progress_cb, |
| 2033 |
rdev, |
| 2034 |
NULL, |
| 2035 |
(GAsyncReadyCallback) enroll_cb, |
| 2036 |
rdev); |
| 2037 |
return; |
| 2038 |
} |
| 2039 |
} |
| 2040 |
|
| 2041 |
name = enroll_result_to_name (TRUE, print != NULL, error); |
| 2042 |
|
| 2043 |
g_debug ("enroll_cb: result %s", name); |
| 2044 |
|
| 2045 |
if (print) |
| 2046 |
{ |
| 2047 |
int r; |
| 2048 |
r = store.print_data_save (print); |
| 2049 |
if (r != 0) |
| 2050 |
name = "enroll-failed"; |
| 2051 |
} |
| 2052 |
|
| 2053 |
g_signal_emit (rdev, signals[SIGNAL_ENROLL_STATUS], 0, name, TRUE); |
| 2054 |
|
| 2055 |
if (error && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
| 2056 |
g_warning ("Device reported an error during enroll: %s", error->message); |
| 2057 |
|
| 2058 |
stoppable_action_completed (rdev); |
| 2059 |
} |
| 2060 |
|
| 2061 |
static void |
| 2062 |
enroll_start (FprintDevice *rdev) |
| 2063 |
{ |
| 2064 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 2065 |
|
| 2066 |
fp_device_enroll (priv->dev, |
| 2067 |
fprint_device_create_enroll_template (rdev, priv->enroll_data), |
| 2068 |
priv->current_cancellable, |
| 2069 |
enroll_progress_cb, |
| 2070 |
rdev, |
| 2071 |
NULL, |
| 2072 |
(GAsyncReadyCallback) enroll_cb, |
| 2073 |
rdev); |
| 2074 |
} |
| 2075 |
|
| 2076 |
static void |
| 2077 |
enroll_identify_cb (FpDevice *dev, GAsyncResult *res, void *user_data) |
| 2078 |
{ |
| 2079 |
g_autoptr(GError) error = NULL; |
| 2080 |
g_autoptr(FpPrint) matched_print = NULL; |
| 2081 |
g_autoptr(FpPrint) found_print = NULL; |
| 2082 |
FprintDevice *rdev = user_data; |
| 2083 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 2084 |
const char *name; |
| 2085 |
|
| 2086 |
fp_device_identify_finish (dev, res, &matched_print, &found_print, &error); |
| 2087 |
|
| 2088 |
if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_DATA_NOT_FOUND)) |
| 2089 |
{ |
| 2090 |
g_clear_object (&found_print); |
| 2091 |
g_clear_error (&error); |
| 2092 |
} |
| 2093 |
|
| 2094 |
/* We may need to retry or error out. */ |
| 2095 |
if (error) |
| 2096 |
{ |
| 2097 |
gboolean retry = error->domain == FP_DEVICE_RETRY; |
| 2098 |
|
| 2099 |
name = enroll_result_to_name (!retry, FALSE, error); |
| 2100 |
g_signal_emit (rdev, signals[SIGNAL_ENROLL_STATUS], 0, name, !retry); |
| 2101 |
|
| 2102 |
/* Retry or clean up. */ |
| 2103 |
if (retry) |
| 2104 |
{ |
| 2105 |
g_autoptr(GPtrArray) all_prints = NULL; |
| 2106 |
|
| 2107 |
all_prints = load_all_prints (rdev); |
| 2108 |
fp_device_identify (priv->dev, |
| 2109 |
all_prints, |
| 2110 |
priv->current_cancellable, |
| 2111 |
NULL, |
| 2112 |
NULL, |
| 2113 |
NULL, |
| 2114 |
(GAsyncReadyCallback) enroll_identify_cb, |
| 2115 |
rdev); |
| 2116 |
} |
| 2117 |
else |
| 2118 |
{ |
| 2119 |
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
| 2120 |
g_warning ("Device reported an error during identify for enroll: %s", error->message); |
| 2121 |
|
| 2122 |
stoppable_action_completed (rdev); |
| 2123 |
} |
| 2124 |
|
| 2125 |
return; |
| 2126 |
} |
| 2127 |
|
| 2128 |
/* Identify has finished (successfully), there are three possible cases: |
| 2129 |
* 1. Match found in the gallery, in this case, we error out. |
| 2130 |
* 2. No match found, but on-device print returned, we should delete it |
| 2131 |
* 3. None of the above, we can just continue. |
| 2132 |
*/ |
| 2133 |
|
| 2134 |
if (matched_print) |
| 2135 |
{ |
| 2136 |
g_signal_emit (rdev, signals[SIGNAL_ENROLL_STATUS], 0, "enroll-duplicate", TRUE); |
| 2137 |
|
| 2138 |
stoppable_action_completed (rdev); |
| 2139 |
return; |
| 2140 |
} |
| 2141 |
|
| 2142 |
if (found_print && fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_STORAGE)) |
| 2143 |
{ |
| 2144 |
if (!fp_print_get_device_stored (found_print)) |
| 2145 |
g_critical ("libfprint driver bug: Returned device print not marked as stored on device."); |
| 2146 |
|
| 2147 |
/* Try to delete the print (synchronously), and continue if it succeeds. */ |
| 2148 |
if (!fp_device_delete_print_sync (priv->dev, |
| 2149 |
found_print, |
| 2150 |
priv->current_cancellable, |
| 2151 |
&error)) |
| 2152 |
{ |
| 2153 |
g_warning ("Failed to garbage collect duplicate print, cannot continue with enroll: %s", |
| 2154 |
error->message); |
| 2155 |
g_signal_emit (rdev, signals[SIGNAL_ENROLL_STATUS], 0, "enroll-duplicate", TRUE); |
| 2156 |
|
| 2157 |
stoppable_action_completed (rdev); |
| 2158 |
return; |
| 2159 |
} |
| 2160 |
} |
| 2161 |
|
| 2162 |
g_signal_emit (rdev, signals[SIGNAL_ENROLL_STATUS], 0, "enroll-stage-passed", FALSE); |
| 2163 |
|
| 2164 |
/* We are good and can start to enroll. */ |
| 2165 |
enroll_start (rdev); |
| 2166 |
} |
| 2167 |
|
| 2168 |
static gboolean |
| 2169 |
is_first_enrollment (FprintDevice * rdev) |
| 2170 |
{ |
| 2171 |
g_autoptr(GPtrArray) host_prints = NULL; |
| 2172 |
host_prints = load_all_prints (rdev); |
| 2173 |
|
| 2174 |
return host_prints->len == 0; |
| 2175 |
} |
| 2176 |
|
| 2177 |
static gboolean |
| 2178 |
fprint_device_enroll_start (FprintDBusDevice *dbus_dev, |
| 2179 |
GDBusMethodInvocation *invocation, |
| 2180 |
const char *finger_name) |
| 2181 |
{ |
| 2182 |
g_autoptr(GError) error = NULL; |
| 2183 |
g_autoptr(FpPrint) existing_print = NULL; |
| 2184 |
g_autoptr(SessionData) session = NULL; |
| 2185 |
FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); |
| 2186 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 2187 |
FpFinger finger = finger_name_to_fp_finger (finger_name); |
| 2188 |
|
| 2189 |
if (!_fprint_device_check_claimed (rdev, invocation, &error)) |
| 2190 |
{ |
| 2191 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2192 |
return TRUE; |
| 2193 |
} |
| 2194 |
|
| 2195 |
if (finger == FP_FINGER_UNKNOWN) |
| 2196 |
{ |
| 2197 |
g_set_error (&error, FPRINT_ERROR, FPRINT_ERROR_INVALID_FINGERNAME, |
| 2198 |
"Invalid finger name"); |
| 2199 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2200 |
return TRUE; |
| 2201 |
} |
| 2202 |
|
| 2203 |
session = session_data_get (priv); |
| 2204 |
store.print_data_load (priv->dev, finger, |
| 2205 |
session->username, &existing_print); |
| 2206 |
|
| 2207 |
if (!can_start_action (rdev, &error)) |
| 2208 |
{ |
| 2209 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2210 |
return TRUE; |
| 2211 |
} |
| 2212 |
|
| 2213 |
if (existing_print) |
| 2214 |
{ |
| 2215 |
if (!delete_enrolled_fingers (rdev, session->username, finger, &error)) |
| 2216 |
{ |
| 2217 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2218 |
return TRUE; |
| 2219 |
} |
| 2220 |
} |
| 2221 |
|
| 2222 |
g_debug ("start enrollment device %d finger %d", priv->id, finger); |
| 2223 |
|
| 2224 |
priv->current_cancellable = g_cancellable_new (); |
| 2225 |
priv->enroll_data = finger; |
| 2226 |
priv->current_action = ACTION_ENROLL; |
| 2227 |
|
| 2228 |
if (fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_STORAGE) && |
| 2229 |
!fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_STORAGE_LIST) && |
| 2230 |
is_first_enrollment (rdev)) |
| 2231 |
{ |
| 2232 |
g_autoptr(GError) clear_err = NULL; |
| 2233 |
|
| 2234 |
if (!fp_device_clear_storage_sync (priv->dev, NULL, &clear_err)) |
| 2235 |
g_warning ("Failed to clear storage before first enrollment: %s", |
| 2236 |
clear_err->message); |
| 2237 |
} |
| 2238 |
|
| 2239 |
if (fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_IDENTIFY)) |
| 2240 |
{ |
| 2241 |
g_autoptr(GPtrArray) all_prints = load_all_prints (rdev); |
| 2242 |
|
| 2243 |
/* We (now) have the policy that there must be no duplicate prints. |
| 2244 |
* We need to do this for MoC devices, as their "identify" function |
| 2245 |
* will generally just identify across all device stored prints. |
| 2246 |
* For MoH, we also do it. For consistency and because it allows us |
| 2247 |
* to implement new features in the future (i.e. logging in/unlocking |
| 2248 |
* the correct user without selecting it first). |
| 2249 |
*/ |
| 2250 |
fp_device_identify (priv->dev, |
| 2251 |
all_prints, |
| 2252 |
priv->current_cancellable, |
| 2253 |
NULL, |
| 2254 |
NULL, |
| 2255 |
NULL, |
| 2256 |
(GAsyncReadyCallback) enroll_identify_cb, |
| 2257 |
rdev); |
| 2258 |
} |
| 2259 |
else |
| 2260 |
{ |
| 2261 |
/* We may still want to try to use verification to check for duplicates |
| 2262 |
* if only one fingerprint was previously enrolled, or add more verify |
| 2263 |
* stages up to a predefined limit */ |
| 2264 |
g_warning ("Device %s does not support duplicate identification and so " |
| 2265 |
"fprintd duplicate detection won't work", |
| 2266 |
fp_device_get_name (priv->dev)); |
| 2267 |
enroll_start (rdev); |
| 2268 |
} |
| 2269 |
|
| 2270 |
fprint_dbus_device_complete_enroll_start (dbus_dev, invocation); |
| 2271 |
|
| 2272 |
return TRUE; |
| 2273 |
} |
| 2274 |
|
| 2275 |
static gboolean |
| 2276 |
fprint_device_enroll_stop (FprintDBusDevice *dbus_dev, |
| 2277 |
GDBusMethodInvocation *invocation) |
| 2278 |
{ |
| 2279 |
FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); |
| 2280 |
|
| 2281 |
g_autoptr(GError) error = NULL; |
| 2282 |
|
| 2283 |
if (!_fprint_device_check_claimed (rdev, invocation, &error)) |
| 2284 |
{ |
| 2285 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2286 |
return TRUE; |
| 2287 |
} |
| 2288 |
|
| 2289 |
if (!can_stop_action (rdev, ACTION_ENROLL, &error)) |
| 2290 |
{ |
| 2291 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2292 |
return TRUE; |
| 2293 |
} |
| 2294 |
|
| 2295 |
stoppable_action_stop (rdev, invocation); |
| 2296 |
|
| 2297 |
return TRUE; |
| 2298 |
} |
| 2299 |
|
| 2300 |
static gboolean |
| 2301 |
fprint_device_list_enrolled_fingers (FprintDBusDevice *dbus_dev, |
| 2302 |
GDBusMethodInvocation *invocation, |
| 2303 |
const char *username) |
| 2304 |
{ |
| 2305 |
FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); |
| 2306 |
|
| 2307 |
g_autoptr(GPtrArray) ret = NULL; |
| 2308 |
g_autoptr(GPtrArray) prints = NULL; |
| 2309 |
const char *sender; |
| 2310 |
const char *user; |
| 2311 |
guint i; |
| 2312 |
|
| 2313 |
sender = g_dbus_method_invocation_get_sender (invocation); |
| 2314 |
_fprint_device_add_client (rdev, sender); |
| 2315 |
|
| 2316 |
user = g_object_get_qdata (G_OBJECT (invocation), quark_auth_user); |
| 2317 |
g_assert (user); |
| 2318 |
prints = load_user_prints (rdev, user); |
| 2319 |
|
| 2320 |
ret = g_ptr_array_new (); |
| 2321 |
for (i = 0; i < prints->len; i++) |
| 2322 |
{ |
| 2323 |
FpFinger finger = fp_print_get_finger (g_ptr_array_index (prints, i)); |
| 2324 |
|
| 2325 |
if (finger != FP_FINGER_UNKNOWN) |
| 2326 |
g_ptr_array_add (ret, (char *) fp_finger_to_name (finger)); |
| 2327 |
} |
| 2328 |
|
| 2329 |
if (!ret->len) |
| 2330 |
{ |
| 2331 |
g_dbus_method_invocation_return_error_literal (invocation, |
| 2332 |
FPRINT_ERROR, |
| 2333 |
FPRINT_ERROR_NO_ENROLLED_PRINTS, |
| 2334 |
"Failed to discover prints"); |
| 2335 |
return TRUE; |
| 2336 |
} |
| 2337 |
|
| 2338 |
/* Add null-termination */ |
| 2339 |
g_ptr_array_add (ret, NULL); |
| 2340 |
|
| 2341 |
fprint_dbus_device_complete_list_enrolled_fingers (dbus_dev, |
| 2342 |
invocation, (const gchar *const *) ret->pdata); |
| 2343 |
|
| 2344 |
return TRUE; |
| 2345 |
} |
| 2346 |
|
| 2347 |
static gboolean |
| 2348 |
user_has_print_enrolled (FprintDevice *rdev, |
| 2349 |
const char *user, |
| 2350 |
FpFinger finger) |
| 2351 |
{ |
| 2352 |
g_autoptr(GSList) prints = NULL; |
| 2353 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 2354 |
|
| 2355 |
prints = store.discover_prints (priv->dev, user); |
| 2356 |
|
| 2357 |
if (finger == FP_FINGER_UNKNOWN) |
| 2358 |
return prints != NULL; |
| 2359 |
|
| 2360 |
return g_slist_find (prints, GUINT_TO_POINTER (finger)) != NULL; |
| 2361 |
} |
| 2362 |
|
| 2363 |
static gboolean |
| 2364 |
delete_enrolled_fingers (FprintDevice *rdev, |
| 2365 |
const char *user, |
| 2366 |
FpFinger finger, |
| 2367 |
GError **error) |
| 2368 |
{ |
| 2369 |
g_autoptr(GError) device_error = NULL; |
| 2370 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 2371 |
guint i; |
| 2372 |
int r; |
| 2373 |
|
| 2374 |
if (finger != FP_FINGER_UNKNOWN) |
| 2375 |
g_debug ("Deleting enrolled finger %s for user %s", |
| 2376 |
fp_finger_to_name (finger), user); |
| 2377 |
else |
| 2378 |
g_debug ("Deleting enrolled fingers for user %s", user); |
| 2379 |
|
| 2380 |
if (!user_has_print_enrolled (rdev, user, finger)) |
| 2381 |
{ |
| 2382 |
if (finger != FP_FINGER_UNKNOWN) |
| 2383 |
g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_NO_ENROLLED_PRINTS, |
| 2384 |
"Fingerprint for finger %s is not enrolled", |
| 2385 |
fp_finger_to_name (finger)); |
| 2386 |
else |
| 2387 |
g_set_error_literal (error, FPRINT_ERROR, FPRINT_ERROR_NO_ENROLLED_PRINTS, |
| 2388 |
"No fingerprint enrolled"); |
| 2389 |
|
| 2390 |
return FALSE; |
| 2391 |
} |
| 2392 |
|
| 2393 |
/* First try deleting the print from the device, we don't consider it |
| 2394 |
* fatal if this does not work. */ |
| 2395 |
if (fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_STORAGE)) |
| 2396 |
{ |
| 2397 |
g_autoptr(GSList) prints = NULL; |
| 2398 |
GSList *l; |
| 2399 |
|
| 2400 |
prints = store.discover_prints (priv->dev, user); |
| 2401 |
|
| 2402 |
for (l = prints; l != NULL; l = l->next) |
| 2403 |
{ |
| 2404 |
g_autoptr(FpPrint) print = NULL; |
| 2405 |
|
| 2406 |
store.print_data_load (priv->dev, |
| 2407 |
GPOINTER_TO_UINT (l->data), |
| 2408 |
user, |
| 2409 |
&print); |
| 2410 |
|
| 2411 |
if (print) |
| 2412 |
{ |
| 2413 |
g_autoptr(GError) local_error = NULL; |
| 2414 |
|
| 2415 |
if (finger != FP_FINGER_UNKNOWN && fp_print_get_finger (print) != finger) |
| 2416 |
continue; |
| 2417 |
|
| 2418 |
if (!fp_device_delete_print_sync (priv->dev, print, NULL, &local_error)) |
| 2419 |
{ |
| 2420 |
g_warning ("Error deleting print from device: %s", local_error->message); |
| 2421 |
g_warning ("This might indicate an issue in the libfprint driver or in the fingerprint device."); |
| 2422 |
|
| 2423 |
if (!device_error) |
| 2424 |
{ |
| 2425 |
g_set_error (&device_error, FPRINT_ERROR, |
| 2426 |
FPRINT_ERROR_PRINTS_NOT_DELETED_FROM_DEVICE, |
| 2427 |
"Failed to delete print from device storage: %s", |
| 2428 |
local_error->message); |
| 2429 |
} |
| 2430 |
} |
| 2431 |
} |
| 2432 |
} |
| 2433 |
} |
| 2434 |
|
| 2435 |
if (finger != FP_FINGER_UNKNOWN) |
| 2436 |
{ |
| 2437 |
if ((r = store.print_data_delete (priv->dev, finger, user)) != 0) |
| 2438 |
{ |
| 2439 |
if (user_has_print_enrolled (rdev, user, finger)) |
| 2440 |
{ |
| 2441 |
g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_PRINTS_NOT_DELETED, |
| 2442 |
"Impossible to delete fingerprint reference " |
| 2443 |
"got error: %d", r); |
| 2444 |
return FALSE; |
| 2445 |
} |
| 2446 |
} |
| 2447 |
} |
| 2448 |
else |
| 2449 |
{ |
| 2450 |
g_autoptr(GError) local_error = NULL; |
| 2451 |
|
| 2452 |
for (i = FP_FINGER_FIRST; i <= FP_FINGER_LAST; i++) |
| 2453 |
{ |
| 2454 |
if ((r = store.print_data_delete (priv->dev, i, user)) != 0) |
| 2455 |
{ |
| 2456 |
if (local_error) |
| 2457 |
continue; |
| 2458 |
|
| 2459 |
if (user_has_print_enrolled (rdev, user, i)) |
| 2460 |
{ |
| 2461 |
g_set_error (&local_error, FPRINT_ERROR, FPRINT_ERROR_PRINTS_NOT_DELETED, |
| 2462 |
"Impossible to delete fingerprint reference " |
| 2463 |
"got error: %d", r); |
| 2464 |
/* Do not return yet, at least try to remove the remaining prints */ |
| 2465 |
} |
| 2466 |
} |
| 2467 |
} |
| 2468 |
|
| 2469 |
if (local_error) |
| 2470 |
{ |
| 2471 |
g_propagate_error (error, g_steal_pointer (&local_error)); |
| 2472 |
return FALSE; |
| 2473 |
} |
| 2474 |
} |
| 2475 |
|
| 2476 |
if (device_error) |
| 2477 |
{ |
| 2478 |
/* This is a low priority error, higher priority errors would have returned failure already */ |
| 2479 |
g_propagate_error (error, g_steal_pointer (&device_error)); |
| 2480 |
return FALSE; |
| 2481 |
} |
| 2482 |
|
| 2483 |
return TRUE; |
| 2484 |
} |
| 2485 |
|
| 2486 |
#ifdef __linux__ |
| 2487 |
static void |
| 2488 |
log_offending_client_cb (GObject *object, |
| 2489 |
GAsyncResult *res, |
| 2490 |
gpointer user_data) |
| 2491 |
{ |
| 2492 |
GDBusConnection *connection = G_DBUS_CONNECTION (object); |
| 2493 |
|
| 2494 |
g_autoptr(GVariant) ret = NULL; |
| 2495 |
g_autofree char *path = NULL; |
| 2496 |
g_autofree char *content = NULL; |
| 2497 |
guint pid = 0; |
| 2498 |
|
| 2499 |
ret = g_dbus_connection_call_finish (connection, res, NULL); |
| 2500 |
|
| 2501 |
if (!ret) |
| 2502 |
return; |
| 2503 |
|
| 2504 |
g_variant_get (ret, "(u)", &pid); |
| 2505 |
path = g_strdup_printf ("/proc/%u/comm", pid); |
| 2506 |
if (g_file_get_contents (path, &content, NULL, NULL)) |
| 2507 |
{ |
| 2508 |
g_strchomp (content); |
| 2509 |
g_warning ("Offending API user is %s", content); |
| 2510 |
} |
| 2511 |
} |
| 2512 |
|
| 2513 |
static void |
| 2514 |
log_offending_client (GDBusMethodInvocation *invocation) |
| 2515 |
{ |
| 2516 |
const char *sender; |
| 2517 |
GDBusConnection *connection; |
| 2518 |
|
| 2519 |
connection = g_dbus_method_invocation_get_connection (invocation); |
| 2520 |
sender = g_dbus_method_invocation_get_sender (invocation); |
| 2521 |
|
| 2522 |
g_dbus_connection_call (connection, |
| 2523 |
"org.freedesktop.DBus", |
| 2524 |
"/org/freedesktop/DBus", |
| 2525 |
"org.freedesktop.DBus", |
| 2526 |
"GetConnectionUnixProcessID", |
| 2527 |
g_variant_new ("(s)", sender), |
| 2528 |
NULL, G_DBUS_CALL_FLAGS_NONE, |
| 2529 |
-1, NULL, log_offending_client_cb, NULL); |
| 2530 |
} |
| 2531 |
#endif |
| 2532 |
|
| 2533 |
static gboolean |
| 2534 |
fprint_device_delete_enrolled_fingers (FprintDBusDevice *dbus_dev, |
| 2535 |
GDBusMethodInvocation *invocation, |
| 2536 |
const char *username) |
| 2537 |
{ |
| 2538 |
FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); |
| 2539 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 2540 |
|
| 2541 |
g_autoptr(FprintDeviceActionUnset) action_unset = NULL; |
| 2542 |
g_autoptr(GError) error = NULL; |
| 2543 |
g_autofree char *user = NULL; |
| 2544 |
const char *sender; |
| 2545 |
gboolean opened; |
| 2546 |
|
| 2547 |
g_warning ("The API user should be updated to use DeleteEnrolledFingers2 method!"); |
| 2548 |
#ifdef __linux__ |
| 2549 |
log_offending_client (invocation); |
| 2550 |
#endif |
| 2551 |
|
| 2552 |
if (!can_start_action (rdev, &error)) |
| 2553 |
{ |
| 2554 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2555 |
return TRUE; |
| 2556 |
} |
| 2557 |
|
| 2558 |
priv->current_action = ACTION_DELETE; |
| 2559 |
action_unset = rdev; |
| 2560 |
|
| 2561 |
if (!_fprint_device_check_claimed (rdev, invocation, &error)) |
| 2562 |
{ |
| 2563 |
/* Return error for anything but FPRINT_ERROR_CLAIM_DEVICE */ |
| 2564 |
if (!g_error_matches (error, FPRINT_ERROR, FPRINT_ERROR_CLAIM_DEVICE)) |
| 2565 |
{ |
| 2566 |
g_dbus_method_invocation_return_gerror (invocation, |
| 2567 |
error); |
| 2568 |
return TRUE; |
| 2569 |
} |
| 2570 |
|
| 2571 |
opened = FALSE; |
| 2572 |
} |
| 2573 |
else |
| 2574 |
{ |
| 2575 |
opened = fp_device_is_open (priv->dev); |
| 2576 |
} |
| 2577 |
|
| 2578 |
sender = g_dbus_method_invocation_get_sender (invocation); |
| 2579 |
_fprint_device_add_client (rdev, sender); |
| 2580 |
|
| 2581 |
if (!opened && fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_STORAGE)) |
| 2582 |
fp_device_open_sync (priv->dev, NULL, NULL); |
| 2583 |
|
| 2584 |
user = g_object_steal_qdata (G_OBJECT (invocation), quark_auth_user); |
| 2585 |
g_assert (user); |
| 2586 |
g_assert (g_str_equal (username, "") || g_str_equal (user, username)); |
| 2587 |
|
| 2588 |
g_clear_error (&error); |
| 2589 |
delete_enrolled_fingers (rdev, user, FP_FINGER_UNKNOWN, &error); |
| 2590 |
|
| 2591 |
if (!opened && fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_STORAGE)) |
| 2592 |
fp_device_close_sync (priv->dev, NULL, NULL); |
| 2593 |
|
| 2594 |
if (error) |
| 2595 |
{ |
| 2596 |
g_dbus_method_invocation_return_gerror (invocation, |
| 2597 |
error); |
| 2598 |
return TRUE; |
| 2599 |
} |
| 2600 |
|
| 2601 |
fprint_dbus_device_complete_delete_enrolled_fingers (dbus_dev, |
| 2602 |
invocation); |
| 2603 |
return TRUE; |
| 2604 |
} |
| 2605 |
|
| 2606 |
static gboolean |
| 2607 |
fprint_device_delete_enrolled_fingers2 (FprintDBusDevice *dbus_dev, |
| 2608 |
GDBusMethodInvocation *invocation) |
| 2609 |
{ |
| 2610 |
FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); |
| 2611 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 2612 |
|
| 2613 |
g_autoptr(SessionData) session = NULL; |
| 2614 |
g_autoptr(GError) error = NULL; |
| 2615 |
g_autoptr(FprintDeviceActionUnset) action_unset = NULL; |
| 2616 |
|
| 2617 |
if (!_fprint_device_check_claimed (rdev, invocation, &error)) |
| 2618 |
{ |
| 2619 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2620 |
return TRUE; |
| 2621 |
} |
| 2622 |
|
| 2623 |
if (!can_start_action (rdev, &error)) |
| 2624 |
{ |
| 2625 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2626 |
return TRUE; |
| 2627 |
} |
| 2628 |
|
| 2629 |
priv->current_action = ACTION_DELETE; |
| 2630 |
action_unset = rdev; |
| 2631 |
|
| 2632 |
session = session_data_get (priv); |
| 2633 |
|
| 2634 |
if (!delete_enrolled_fingers (rdev, session->username, FP_FINGER_UNKNOWN, &error)) |
| 2635 |
{ |
| 2636 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2637 |
return TRUE; |
| 2638 |
} |
| 2639 |
|
| 2640 |
fprint_dbus_device_complete_delete_enrolled_fingers2 (dbus_dev, |
| 2641 |
invocation); |
| 2642 |
return TRUE; |
| 2643 |
} |
| 2644 |
|
| 2645 |
static gboolean |
| 2646 |
fprint_device_delete_enrolled_finger (FprintDBusDevice *dbus_dev, |
| 2647 |
GDBusMethodInvocation *invocation, |
| 2648 |
const gchar *finger_name) |
| 2649 |
{ |
| 2650 |
FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); |
| 2651 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 2652 |
FpFinger finger = finger_name_to_fp_finger (finger_name); |
| 2653 |
|
| 2654 |
g_autoptr(FprintDeviceActionUnset) action_unset = NULL; |
| 2655 |
g_autoptr(SessionData) session = NULL; |
| 2656 |
g_autoptr(GError) error = NULL; |
| 2657 |
|
| 2658 |
if (!_fprint_device_check_claimed (rdev, invocation, &error)) |
| 2659 |
{ |
| 2660 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2661 |
return TRUE; |
| 2662 |
} |
| 2663 |
|
| 2664 |
if (finger == FP_FINGER_UNKNOWN) |
| 2665 |
{ |
| 2666 |
g_dbus_method_invocation_return_error_literal (invocation, |
| 2667 |
FPRINT_ERROR, |
| 2668 |
FPRINT_ERROR_INVALID_FINGERNAME, |
| 2669 |
"Invalid finger name"); |
| 2670 |
return TRUE; |
| 2671 |
} |
| 2672 |
|
| 2673 |
if (!can_start_action (rdev, &error)) |
| 2674 |
{ |
| 2675 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2676 |
return TRUE; |
| 2677 |
} |
| 2678 |
|
| 2679 |
priv->current_action = ACTION_DELETE; |
| 2680 |
|
| 2681 |
session = session_data_get (priv); |
| 2682 |
action_unset = rdev; |
| 2683 |
|
| 2684 |
if (!delete_enrolled_fingers (rdev, session->username, finger, &error)) |
| 2685 |
{ |
| 2686 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2687 |
return TRUE; |
| 2688 |
} |
| 2689 |
|
| 2690 |
fprint_dbus_device_complete_delete_enrolled_finger (dbus_dev, invocation); |
| 2691 |
return TRUE; |
| 2692 |
} |
| 2693 |
|
| 2694 |
static gboolean |
| 2695 |
handle_unauthorized_access (FprintDevice *rdev, |
| 2696 |
GDBusMethodInvocation *invocation, |
| 2697 |
GError *error) |
| 2698 |
{ |
| 2699 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 2700 |
|
| 2701 |
g_assert (error); |
| 2702 |
|
| 2703 |
g_warning ("Authorization denied to %s to call method '%s' for device '%s': %s", |
| 2704 |
g_dbus_method_invocation_get_sender (invocation), |
| 2705 |
g_dbus_method_invocation_get_method_name (invocation), |
| 2706 |
fp_device_get_name (priv->dev), |
| 2707 |
error->message); |
| 2708 |
g_dbus_method_invocation_return_gerror (invocation, error); |
| 2709 |
|
| 2710 |
return FALSE; |
| 2711 |
} |
| 2712 |
|
| 2713 |
static gboolean |
| 2714 |
action_authorization_handler (GDBusInterfaceSkeleton *interface, |
| 2715 |
GDBusMethodInvocation *invocation, |
| 2716 |
gpointer user_data) |
| 2717 |
{ |
| 2718 |
FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (interface); |
| 2719 |
FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); |
| 2720 |
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); |
| 2721 |
FprintDevicePermission required_perms; |
| 2722 |
gboolean needs_user_auth = FALSE; |
| 2723 |
|
| 2724 |
g_autoptr(GError) error = NULL; |
| 2725 |
const gchar *method_name; |
| 2726 |
|
| 2727 |
method_name = g_dbus_method_invocation_get_method_name (invocation); |
| 2728 |
|
| 2729 |
g_debug ("Requesting authorization from %s to call method '%s' for device '%s'", |
| 2730 |
g_dbus_method_invocation_get_sender (invocation), method_name, |
| 2731 |
fp_device_get_name (priv->dev)); |
| 2732 |
|
| 2733 |
if (g_str_equal (method_name, "Claim")) |
| 2734 |
needs_user_auth = TRUE; |
| 2735 |
else if (g_str_equal (method_name, "DeleteEnrolledFingers")) |
| 2736 |
needs_user_auth = TRUE; |
| 2737 |
else if (g_str_equal (method_name, "ListEnrolledFingers")) |
| 2738 |
needs_user_auth = TRUE; |
| 2739 |
|
| 2740 |
/* This is just a quick check in order to avoid authentication if |
| 2741 |
* the user cannot make the call at this time anyway. |
| 2742 |
* The method handler itself is required to check again! */ |
| 2743 |
if (!_fprint_device_check_claimed (rdev, invocation, &error)) |
| 2744 |
return handle_unauthorized_access (rdev, invocation, error); |
| 2745 |
|
| 2746 |
if (needs_user_auth && |
| 2747 |
!fprintd_device_authorize_user (rdev, invocation, &error)) |
| 2748 |
return handle_unauthorized_access (rdev, invocation, error); |
| 2749 |
|
| 2750 |
required_perms = get_permissions_for_invocation (invocation); |
| 2751 |
|
| 2752 |
/* This may possibly block the invocation till the user has not |
| 2753 |
* provided an authentication method, so other calls could arrive */ |
| 2754 |
if (!fprint_device_check_polkit_for_permissions (rdev, invocation, |
| 2755 |
required_perms, |
| 2756 |
&error)) |
| 2757 |
return handle_unauthorized_access (rdev, invocation, error); |
| 2758 |
|
| 2759 |
g_debug ("Authorization granted to %s to call method '%s' for device '%s'!", |
| 2760 |
g_dbus_method_invocation_get_sender (invocation), |
| 2761 |
g_dbus_method_invocation_get_method_name (invocation), |
| 2762 |
fp_device_get_name (priv->dev)); |
| 2763 |
|
| 2764 |
return TRUE; |
| 2765 |
} |
| 2766 |
|
| 2767 |
static void |
| 2768 |
fprint_device_dbus_skeleton_iface_init (FprintDBusDeviceIface *iface) |
| 2769 |
{ |
| 2770 |
iface->handle_claim = fprint_device_claim; |
| 2771 |
iface->handle_delete_enrolled_finger = fprint_device_delete_enrolled_finger; |
| 2772 |
iface->handle_delete_enrolled_fingers = fprint_device_delete_enrolled_fingers; |
| 2773 |
iface->handle_delete_enrolled_fingers2 = fprint_device_delete_enrolled_fingers2; |
| 2774 |
iface->handle_enroll_start = fprint_device_enroll_start; |
| 2775 |
iface->handle_enroll_stop = fprint_device_enroll_stop; |
| 2776 |
iface->handle_list_enrolled_fingers = fprint_device_list_enrolled_fingers; |
| 2777 |
iface->handle_release = fprint_device_release; |
| 2778 |
iface->handle_verify_start = fprint_device_verify_start; |
| 2779 |
iface->handle_verify_stop = fprint_device_verify_stop; |
| 2780 |
} |