1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2007-2010 David Zeuthen <zeuthen@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include <glib/gi18n-lib.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <stdlib.h>
#include <glib/gstdio.h>
#include "udiskslogging.h"
#include "udiskslinuxencrypted.h"
#include "udiskslinuxblockobject.h"
#include "udisksdaemon.h"
#include "udiskspersistentstore.h"
#include "udisksdaemonutil.h"
#include "udiskscleanup.h"
/**
* SECTION:udiskslinuxencrypted
* @title: UDisksLinuxEncrypted
* @short_description: Linux implementation of #UDisksEncrypted
*
* This type provides an implementation of the #UDisksEncrypted
* interface on Linux.
*/
typedef struct _UDisksLinuxEncryptedClass UDisksLinuxEncryptedClass;
/**
* UDisksLinuxEncrypted:
*
* The #UDisksLinuxEncrypted structure contains only private data and should
* only be accessed using the provided API.
*/
struct _UDisksLinuxEncrypted
{
UDisksEncryptedSkeleton parent_instance;
};
struct _UDisksLinuxEncryptedClass
{
UDisksEncryptedSkeletonClass parent_class;
};
static void encrypted_iface_init (UDisksEncryptedIface *iface);
G_DEFINE_TYPE_WITH_CODE (UDisksLinuxEncrypted, udisks_linux_encrypted, UDISKS_TYPE_ENCRYPTED_SKELETON,
G_IMPLEMENT_INTERFACE (UDISKS_TYPE_ENCRYPTED, encrypted_iface_init));
/* ---------------------------------------------------------------------------------------------------- */
static void
udisks_linux_encrypted_init (UDisksLinuxEncrypted *encrypted)
{
g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (encrypted),
G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
}
static void
udisks_linux_encrypted_class_init (UDisksLinuxEncryptedClass *klass)
{
}
/**
* udisks_linux_encrypted_new:
*
* Creates a new #UDisksLinuxEncrypted instance.
*
* Returns: A new #UDisksLinuxEncrypted. Free with g_object_unref().
*/
UDisksEncrypted *
udisks_linux_encrypted_new (void)
{
return UDISKS_ENCRYPTED (g_object_new (UDISKS_TYPE_LINUX_ENCRYPTED,
NULL));
}
/* ---------------------------------------------------------------------------------------------------- */
/**
* udisks_linux_encrypted_update:
* @encrypted: A #UDisksLinuxEncrypted.
* @object: The enclosing #UDisksLinuxBlockObject instance.
*
* Updates the interface.
*/
void
udisks_linux_encrypted_update (UDisksLinuxEncrypted *encrypted,
UDisksLinuxBlockObject *object)
{
/* do nothing */
}
/* ---------------------------------------------------------------------------------------------------- */
static gboolean
wait_for_cleartext_object (UDisksDaemon *daemon,
UDisksObject *object,
gpointer user_data)
{
const gchar *crypto_object_path = user_data;
UDisksBlock *block;
gboolean ret;
ret = FALSE;
block = udisks_object_peek_block (object);
if (block == NULL)
goto out;
if (g_strcmp0 (udisks_block_get_crypto_backing_device (block), crypto_object_path) == 0)
ret = TRUE;
out:
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
static gboolean
check_crypttab (UDisksBlock *block,
gboolean load_passphrase,
gboolean *out_found,
gchar **out_name,
gchar **out_passphrase,
gchar **out_options,
GError **error)
{
gboolean ret = FALSE;
GVariantIter iter;
const gchar *type;
GVariant *details;
g_variant_iter_init (&iter, udisks_block_get_configuration (block));
while (g_variant_iter_next (&iter, "(&s@a{sv})", &type, &details))
{
if (g_strcmp0 (type, "crypttab") == 0)
{
const gchar *passphrase_path;
if (out_found != NULL)
*out_found = TRUE;
g_variant_lookup (details, "name", "^ay", out_name);
g_variant_lookup (details, "options", "^ay", out_options);
if (g_variant_lookup (details, "passphrase-path", "^&ay", &passphrase_path) &&
strlen (passphrase_path) > 0 &&
!g_str_has_prefix (passphrase_path, "/dev"))
{
if (load_passphrase)
{
if (!g_file_get_contents (passphrase_path,
out_passphrase,
NULL,
error))
{
g_variant_unref (details);
goto out;
}
}
}
ret = TRUE;
g_variant_unref (details);
goto out;
}
g_variant_unref (details);
}
ret = TRUE;
out:
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
/* runs in thread dedicated to handling @invocation */
static gboolean
handle_unlock (UDisksEncrypted *encrypted,
GDBusMethodInvocation *invocation,
const gchar *passphrase,
GVariant *options)
{
UDisksObject *object;
UDisksBlock *block;
UDisksDaemon *daemon;
UDisksCleanup *cleanup;
gchar *error_message;
gchar *name;
gchar *escaped_name;
UDisksObject *cleartext_object;
UDisksBlock *cleartext_block;
GUdevDevice *udev_cleartext_device;
GError *error;
uid_t caller_uid;
const gchar *action_id;
gboolean is_in_crypttab = FALSE;
gchar *crypttab_name = NULL;
gchar *crypttab_passphrase = NULL;
gchar *crypttab_options = NULL;
object = NULL;
error_message = NULL;
name = NULL;
escaped_name = NULL;
udev_cleartext_device = NULL;
cleartext_object = NULL;
object = UDISKS_OBJECT (g_dbus_interface_get_object (G_DBUS_INTERFACE (encrypted)));
block = udisks_object_peek_block (object);
daemon = udisks_linux_block_object_get_daemon (UDISKS_LINUX_BLOCK_OBJECT (object));
cleanup = udisks_daemon_get_cleanup (daemon);
/* TODO: check if the device is mentioned in /etc/crypttab (see crypttab(5)) - if so use that
*
* Of course cryptsetup(8) don't support that, see https://bugzilla.redhat.com/show_bug.cgi?id=692258
*/
/* Fail if the device is not a LUKS device */
if (!(g_strcmp0 (udisks_block_get_id_usage (block), "crypto") == 0 &&
g_strcmp0 (udisks_block_get_id_type (block), "crypto_LUKS") == 0))
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Device %s does not appear to be a LUKS device",
udisks_block_get_device (block));
goto out;
}
/* Fail if device is already unlocked */
cleartext_object = udisks_daemon_wait_for_object_sync (daemon,
wait_for_cleartext_object,
g_strdup (g_dbus_object_get_object_path (G_DBUS_OBJECT (object))),
g_free,
0, /* timeout_seconds */
NULL); /* error */
if (cleartext_object != NULL)
{
UDisksBlock *unlocked_block;
unlocked_block = udisks_object_peek_block (cleartext_object);
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Device %s is already unlocked as %s",
udisks_block_get_device (block),
udisks_block_get_device (unlocked_block));
goto out;
}
/* we need the uid of the caller for the unlocked-luks file */
error = NULL;
if (!udisks_daemon_util_get_caller_uid_sync (daemon, invocation, NULL /* GCancellable */, &caller_uid, NULL, &error))
{
g_dbus_method_invocation_return_gerror (invocation, error);
g_error_free (error);
goto out;
}
/* check if in crypttab file */
error = NULL;
if (!check_crypttab (block,
TRUE,
&is_in_crypttab,
&crypttab_name,
&crypttab_passphrase,
&crypttab_options,
&error))
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
/* Now, check that the user is actually authorized to unlock the device.
*/
action_id = "org.freedesktop.udisks2.encrypted-unlock";
if (udisks_block_get_hint_system (block) &&
!(udisks_daemon_util_setup_by_user (daemon, object, caller_uid)))
action_id = "org.freedesktop.udisks2.encrypted-unlock-system";
if (is_in_crypttab)
action_id = "org.freedesktop.udisks2.encrypted-unlock-crypttab";
if (!udisks_daemon_util_check_authorization_sync (daemon,
object,
action_id,
options,
N_("Authentication is required to unlock the encrypted device $(udisks2.device)"),
invocation))
goto out;
/* calculate the name to use */
if (is_in_crypttab && crypttab_name != NULL)
name = g_strdup (crypttab_name);
else
name = g_strdup_printf ("LUKS-udisks2-%s", udisks_block_get_id_uuid (block));
escaped_name = g_strescape (name, NULL);
/* if available, use and prefer the /etc/crypttab passphrase */
if (is_in_crypttab && crypttab_passphrase != NULL && strlen (crypttab_passphrase) > 0)
{
passphrase = crypttab_passphrase;
}
/* TODO: support a 'readonly' option */
if (!udisks_daemon_launch_spawned_job_sync (daemon,
NULL, /* GCancellable */
0, /* uid_t run_as_uid */
0, /* uid_t run_as_euid */
NULL, /* gint *out_status */
&error_message,
passphrase, /* input_string */
"cryptsetup luksOpen \"%s\" \"%s\"",
udisks_block_get_device (block),
escaped_name))
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Error unlocking %s: %s",
udisks_block_get_device (block),
error_message);
goto out;
}
/* Determine the resulting cleartext object */
error = NULL;
cleartext_object = udisks_daemon_wait_for_object_sync (daemon,
wait_for_cleartext_object,
g_strdup (g_dbus_object_get_object_path (G_DBUS_OBJECT (object))),
g_free,
10, /* timeout_seconds */
&error);
if (cleartext_object == NULL)
{
g_prefix_error (&error,
"Error waiting for cleartext object after unlocking %s",
udisks_block_get_device (block));
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
cleartext_block = udisks_object_peek_block (cleartext_object);
udisks_notice ("Unlocked LUKS device %s as %s",
udisks_block_get_device (block),
udisks_block_get_device (cleartext_block));
udev_cleartext_device = udisks_linux_block_object_get_device (UDISKS_LINUX_BLOCK_OBJECT (cleartext_object));
/* update the unlocked-luks file */
if (!udisks_cleanup_add_unlocked_luks (cleanup,
udisks_block_get_device_number (cleartext_block),
udisks_block_get_device_number (block),
g_udev_device_get_sysfs_attr (udev_cleartext_device, "dm/uuid"),
caller_uid,
&error))
goto out;
udisks_encrypted_complete_unlock (encrypted,
invocation,
g_dbus_object_get_object_path (G_DBUS_OBJECT (cleartext_object)));
out:
g_free (crypttab_name);
g_free (crypttab_passphrase);
g_free (crypttab_options);
g_free (escaped_name);
g_free (name);
g_free (error_message);
if (udev_cleartext_device != NULL)
g_object_unref (udev_cleartext_device);
if (cleartext_object != NULL)
g_object_unref (cleartext_object);
return TRUE; /* returning TRUE means that we handled the method invocation */
}
/* ---------------------------------------------------------------------------------------------------- */
/* runs in thread dedicated to handling @invocation */
static gboolean
handle_lock (UDisksEncrypted *encrypted,
GDBusMethodInvocation *invocation,
GVariant *options)
{
UDisksObject *object;
UDisksBlock *block;
UDisksDaemon *daemon;
UDisksCleanup *cleanup;
gchar *error_message;
gchar *name;
gchar *escaped_name;
UDisksObject *cleartext_object;
UDisksBlock *cleartext_block;
GUdevDevice *device;
uid_t unlocked_by_uid;
dev_t cleartext_device_from_file;
GError *error;
uid_t caller_uid;
object = NULL;
daemon = NULL;
error_message = NULL;
name = NULL;
escaped_name = NULL;
cleartext_object = NULL;
device = NULL;
object = UDISKS_OBJECT (g_dbus_interface_get_object (G_DBUS_INTERFACE (encrypted)));
block = udisks_object_peek_block (object);
daemon = udisks_linux_block_object_get_daemon (UDISKS_LINUX_BLOCK_OBJECT (object));
cleanup = udisks_daemon_get_cleanup (daemon);
/* TODO: check if the device is mentioned in /etc/crypttab (see crypttab(5)) - if so use that
*
* Of course cryptsetup(8) don't support that, see https://bugzilla.redhat.com/show_bug.cgi?id=692258
*/
/* Fail if the device is not a LUKS device */
if (!(g_strcmp0 (udisks_block_get_id_usage (block), "crypto") == 0 &&
g_strcmp0 (udisks_block_get_id_type (block), "crypto_LUKS") == 0))
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Device %s does not appear to be a LUKS device",
udisks_block_get_device (block));
goto out;
}
/* Fail if device is not unlocked */
cleartext_object = udisks_daemon_wait_for_object_sync (daemon,
wait_for_cleartext_object,
g_strdup (g_dbus_object_get_object_path (G_DBUS_OBJECT (object))),
g_free,
0, /* timeout_seconds */
NULL); /* error */
if (cleartext_object == NULL)
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Device %s is not unlocked",
udisks_block_get_device (block));
goto out;
}
cleartext_block = udisks_object_peek_block (cleartext_object);
error = NULL;
cleartext_device_from_file = udisks_cleanup_find_unlocked_luks (cleanup,
udisks_block_get_device_number (block),
&unlocked_by_uid,
&error);
if (error != NULL)
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Error when looking for entry `%s' in unlocked-luks: %s (%s, %d)",
udisks_block_get_device (block),
error->message,
g_quark_to_string (error->domain),
error->code);
g_error_free (error);
goto out;
}
if (cleartext_device_from_file == 0)
{
/* allow locking stuff not mentioned in unlocked-luks, but treat it like root unlocked it */
unlocked_by_uid = 0;
}
/* we need the uid of the caller to check authorization */
error = NULL;
if (!udisks_daemon_util_get_caller_uid_sync (daemon, invocation, NULL /* GCancellable */, &caller_uid, NULL, &error))
{
g_dbus_method_invocation_return_gerror (invocation, error);
g_error_free (error);
goto out;
}
/* Check that the user is authorized to lock the device - if he
* already unlocked it, he is implicitly authorized...
*/
if (caller_uid != 0 && (caller_uid != unlocked_by_uid))
{
if (!udisks_daemon_util_check_authorization_sync (daemon,
object,
"org.freedesktop.udisks2.encrypted-lock-others",
options,
N_("Authentication is required to lock the encrypted device $(udisks2.device) unlocked by another user"),
invocation))
goto out;
}
device = udisks_linux_block_object_get_device (UDISKS_LINUX_BLOCK_OBJECT (cleartext_object));
escaped_name = g_strescape (g_udev_device_get_sysfs_attr (device, "dm/name"), NULL);
if (cleartext_device_from_file != 0)
{
if (!udisks_cleanup_ignore_unlocked_luks (cleanup, udisks_block_get_device_number (cleartext_block)))
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_ALREADY_UNMOUNTING,
"Cannot lock %s as it's already being locked",
udisks_block_get_device (block));
goto out;
}
}
if (!udisks_daemon_launch_spawned_job_sync (daemon,
NULL, /* GCancellable */
0, /* uid_t run_as_uid */
0, /* uid_t run_as_euid */
NULL, /* gint *out_status */
&error_message,
NULL, /* input_string */
"cryptsetup luksClose \"%s\"",
escaped_name))
{
if (cleartext_device_from_file != 0)
{
udisks_cleanup_unignore_unlocked_luks (cleanup, udisks_block_get_device_number (cleartext_block));
}
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Error locking %s (%s): %s",
udisks_block_get_device (cleartext_block),
udisks_block_get_device (block),
error_message);
goto out;
}
/* OK, device locked.. now to remove the entry from persistent file */
if (cleartext_device_from_file != 0)
{
error = NULL;
if (!udisks_cleanup_remove_unlocked_luks (cleanup, udisks_block_get_device_number (cleartext_block), &error))
{
if (error == NULL)
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Error removing entry for `%s' from unlocked-luks: Entry not found",
udisks_block_get_device (cleartext_block));
}
else
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Error removing entry for `%s' from unlocked-luks: %s (%s, %d)",
udisks_block_get_device (cleartext_block),
error->message,
g_quark_to_string (error->domain),
error->code);
g_error_free (error);
}
udisks_cleanup_unignore_unlocked_luks (cleanup, udisks_block_get_device_number (cleartext_block));
goto out;
}
udisks_cleanup_unignore_unlocked_luks (cleanup, udisks_block_get_device_number (cleartext_block));
}
udisks_notice ("Locked LUKS device %s (was unlocked as %s)",
udisks_block_get_device (block),
udisks_block_get_device (cleartext_block));
udisks_encrypted_complete_lock (encrypted, invocation);
out:
if (device != NULL)
g_object_unref (device);
g_free (escaped_name);
g_free (name);
g_free (error_message);
if (cleartext_object != NULL)
g_object_unref (cleartext_object);
return TRUE; /* returning TRUE means that we handled the method invocation */
}
/* ---------------------------------------------------------------------------------------------------- */
static void
encrypted_iface_init (UDisksEncryptedIface *iface)
{
iface->handle_unlock = handle_unlock;
iface->handle_lock = handle_lock;
}
|