Fix verbose mode compiler warnings.
[platform/upstream/cryptsetup.git] / lib / libdevmapper.c
1 /*
2  * libdevmapper - device-mapper backend for cryptsetup
3  *
4  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <sys/ioctl.h>
23 #include <sys/stat.h>
24 #include <stdio.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <libdevmapper.h>
28 #include <fcntl.h>
29 #include <linux/fs.h>
30 #include <uuid/uuid.h>
31
32 #include "internal.h"
33 #include "luks.h"
34
35 #define DEVICE_DIR              "/dev"
36 #define DM_UUID_LEN             129
37 #define DM_UUID_PREFIX          "CRYPT-"
38 #define DM_UUID_PREFIX_LEN      6
39 #define DM_CRYPT_TARGET         "crypt"
40 #define RETRY_COUNT             5
41
42 /* Set if dm-crypt version was probed */
43 static int _dm_crypt_checked = 0;
44 static uint32_t _dm_crypt_flags = 0;
45
46 static int _dm_use_count = 0;
47 static struct crypt_device *_context = NULL;
48
49 /* Check if we have DM flag to instruct kernel to force wipe buffers */
50 #if !HAVE_DECL_DM_TASK_SECURE_DATA
51 static int dm_task_secure_data(struct dm_task *dmt) { return 1; }
52 #endif
53
54 /* Compatibility for old device-mapper without udev support */
55 #if HAVE_DECL_DM_UDEV_DISABLE_DISK_RULES_FLAG
56 #define CRYPT_TEMP_UDEV_FLAGS   DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
57                                 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
58                                 DM_UDEV_DISABLE_OTHER_RULES_FLAG
59 #define _dm_task_set_cookie     dm_task_set_cookie
60 #define _dm_udev_wait           dm_udev_wait
61 #else
62 #define CRYPT_TEMP_UDEV_FLAGS   0
63 static int _dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
64 static int _dm_udev_wait(uint32_t cookie) { return 0; };
65 #endif
66
67 static int _dm_use_udev(void)
68 {
69 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
70         return dm_udev_get_sync_support();
71 #else
72         return 0;
73 #endif
74 }
75
76 __attribute__((format(printf, 4, 5)))
77 static void set_dm_error(int level,
78                          const char *file __attribute__((unused)),
79                          int line __attribute__((unused)),
80                          const char *f, ...)
81 {
82         char *msg = NULL;
83         va_list va;
84
85         va_start(va, f);
86         if (vasprintf(&msg, f, va) > 0) {
87                 if (level < 4) {
88                         log_err(_context, msg);
89                         log_err(_context, "\n");
90                 } else
91                         log_dbg(msg);
92         }
93         free(msg);
94         va_end(va);
95 }
96
97 static int _dm_simple(int task, const char *name, int udev_wait);
98
99 static void _dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
100                                  unsigned crypt_min, unsigned crypt_patch)
101 {
102         unsigned dm_maj, dm_min, dm_patch;
103
104         if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3)
105                 dm_maj = dm_min = dm_patch = 0;
106
107         log_dbg("Detected dm-crypt version %i.%i.%i, dm-ioctl version %u.%u.%u.",
108                 crypt_maj, crypt_min, crypt_patch, dm_maj, dm_min, dm_patch);
109
110         if (crypt_maj >= 1 && crypt_min >= 2)
111                 _dm_crypt_flags |= DM_KEY_WIPE_SUPPORTED;
112         else
113                 log_dbg("Suspend and resume disabled, no wipe key support.");
114
115         if (crypt_maj >= 1 && crypt_min >= 10)
116                 _dm_crypt_flags |= DM_LMK_SUPPORTED;
117
118         if (dm_maj >= 4 && dm_min >= 20)
119                 _dm_crypt_flags |= DM_SECURE_SUPPORTED;
120
121         /* not perfect, 2.6.33 supports with 1.7.0 */
122         if (crypt_maj >= 1 && crypt_min >= 8)
123                 _dm_crypt_flags |= DM_PLAIN64_SUPPORTED;
124
125         /* Repeat test if dm-crypt is not present */
126         if (crypt_maj > 0)
127                 _dm_crypt_checked = 1;
128 }
129
130 static int _dm_check_versions(void)
131 {
132         struct dm_task *dmt;
133         struct dm_versions *target, *last_target;
134         char dm_version[16];
135
136         if (_dm_crypt_checked)
137                 return 1;
138
139         /* FIXME: add support to DM so it forces crypt target module load here */
140         if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
141                 return 0;
142
143         if (!dm_task_run(dmt)) {
144                 dm_task_destroy(dmt);
145                 return 0;
146         }
147
148         if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version))) {
149                 dm_task_destroy(dmt);
150                 return 0;
151         }
152
153         target = dm_task_get_versions(dmt);
154         do {
155                 last_target = target;
156                 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
157                         _dm_set_crypt_compat(dm_version,
158                                              (unsigned)target->version[0],
159                                              (unsigned)target->version[1],
160                                              (unsigned)target->version[2]);
161                 }
162                 target = (void *) target + target->next;
163         } while (last_target != target);
164
165         dm_task_destroy(dmt);
166         return 1;
167 }
168
169 uint32_t dm_flags(void)
170 {
171         if (!_dm_crypt_checked)
172                 _dm_check_versions();
173
174         return _dm_crypt_flags;
175 }
176
177 int dm_init(struct crypt_device *context, int check_kernel)
178 {
179         if (!_dm_use_count++) {
180                 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
181                         check_kernel ? "" : " (NO kernel check requested)",
182                         _dm_use_udev() ? "en" : "dis");
183                 if (check_kernel && !_dm_check_versions()) {
184                         log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
185                         return -1;
186                 }
187                 if (getuid() || geteuid())
188                         log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
189                 dm_log_init(set_dm_error);
190                 dm_log_init_verbose(10);
191         }
192
193         // FIXME: global context is not safe
194         if (context)
195                 _context = context;
196
197         return 1;       /* unsafe memory */
198 }
199
200 void dm_exit(void)
201 {
202         if (_dm_use_count && (!--_dm_use_count)) {
203                 log_dbg("Releasing device-mapper backend.");
204                 dm_log_init_verbose(0);
205                 dm_log_init(NULL);
206                 dm_lib_release();
207                 _context = NULL;
208         }
209 }
210
211 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
212 {
213         struct dirent *entry;
214         struct stat st;
215         char *ptr;
216         char *result = NULL;
217         DIR *dir;
218         int space;
219
220         /* Ignore strange nested directories */
221         if (dir_level > max_level)
222                 return NULL;
223
224         path[PATH_MAX - 1] = '\0';
225         ptr = path + strlen(path);
226         *ptr++ = '/';
227         *ptr = '\0';
228         space = PATH_MAX - (ptr - path);
229
230         dir = opendir(path);
231         if (!dir)
232                 return NULL;
233
234         while((entry = readdir(dir))) {
235                 if (entry->d_name[0] == '.' ||
236                     !strncmp(entry->d_name, "..", 2))
237                         continue;
238
239                 strncpy(ptr, entry->d_name, space);
240                 if (stat(path, &st) < 0)
241                         continue;
242
243                 if (S_ISDIR(st.st_mode)) {
244                         result = __lookup_dev(path, dev, dir_level + 1, max_level);
245                         if (result)
246                                 break;
247                 } else if (S_ISBLK(st.st_mode)) {
248                         /* workaround: ignore dm-X devices, these are internal kernel names */
249                         if (dir_level == 0 && !strncmp(entry->d_name, "dm-", 3))
250                                 continue;
251                         if (st.st_rdev == dev) {
252                                 result = strdup(path);
253                                 break;
254                         }
255                 }
256         }
257
258         closedir(dir);
259         return result;
260 }
261
262 static char *lookup_dev(const char *dev_id)
263 {
264         uint32_t major, minor;
265         dev_t dev;
266         char *result = NULL, buf[PATH_MAX + 1];
267
268         if (sscanf(dev_id, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
269                 return NULL;
270
271         dev = makedev(major, minor);
272         strncpy(buf, DEVICE_DIR, PATH_MAX);
273         buf[PATH_MAX] = '\0';
274
275         /* First try low level device */
276         if ((result = __lookup_dev(buf, dev, 0, 0)))
277                 return result;
278
279         /* If it is dm, try DM dir  */
280         if (dm_is_dm_major(major)) {
281                 strncpy(buf, dm_dir(), PATH_MAX);
282                 if ((result = __lookup_dev(buf, dev, 0, 0)))
283                         return result;
284         }
285
286         strncpy(buf, DEVICE_DIR, PATH_MAX);
287         result = __lookup_dev(buf, dev, 0, 4);
288
289         /* If not found, return NULL */
290         return result;
291 }
292
293 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
294 {
295         int fd, r = 0;
296         long read_ahead_long;
297
298         if ((fd = open(dev, O_RDONLY)) < 0)
299                 return 0;
300
301         r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
302         close(fd);
303
304         if (r)
305                 *read_ahead = (uint32_t) read_ahead_long;
306
307         return r;
308 }
309
310 static void hex_key(char *hexkey, size_t key_size, const char *key)
311 {
312         unsigned i;
313
314         for(i = 0; i < key_size; i++)
315                 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
316 }
317
318 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
319                         const char *cipher, size_t key_size, const char *key)
320 {
321         char *params;
322         char *hexkey;
323
324         hexkey = crypt_safe_alloc(key_size * 2 + 1);
325         if (!hexkey)
326                 return NULL;
327
328         hex_key(hexkey, key_size, key);
329
330         params = crypt_safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
331         if (!params)
332                 goto out;
333
334         sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
335                 cipher, hexkey, skip, device, offset);
336
337 out:
338         crypt_safe_free(hexkey);
339         return params;
340 }
341
342 /* DM helpers */
343 static int _dm_simple(int task, const char *name, int udev_wait)
344 {
345         int r = 0;
346         struct dm_task *dmt;
347         uint32_t cookie = 0;
348
349         if (!_dm_use_udev())
350                 udev_wait = 0;
351
352         if (!(dmt = dm_task_create(task)))
353                 return 0;
354
355         if (name && !dm_task_set_name(dmt, name))
356                 goto out;
357
358         if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
359                 goto out;
360
361         r = dm_task_run(dmt);
362
363         if (udev_wait)
364                 (void)_dm_udev_wait(cookie);
365
366       out:
367         dm_task_destroy(dmt);
368         return r;
369 }
370
371 static int _error_device(const char *name, size_t size)
372 {
373         struct dm_task *dmt;
374         int r = 0;
375
376         if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
377                 return 0;
378
379         if (!dm_task_set_name(dmt, name))
380                 goto error;
381
382         if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
383                 goto error;
384
385         if (!dm_task_set_ro(dmt))
386                 goto error;
387
388         if (!dm_task_no_open_count(dmt))
389                 goto error;
390
391         if (!dm_task_run(dmt))
392                 goto error;
393
394         if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
395                 _dm_simple(DM_DEVICE_CLEAR, name, 0);
396                 goto error;
397         }
398
399         r = 1;
400
401 error:
402         dm_task_destroy(dmt);
403         return r;
404 }
405
406 int dm_remove_device(const char *name, int force, uint64_t size)
407 {
408         int r = -EINVAL;
409         int retries = force ? RETRY_COUNT : 1;
410         int error_target = 0;
411
412         if (!name || (force && !size))
413                 return -EINVAL;
414
415         do {
416                 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
417                 if (--retries && r) {
418                         log_dbg("WARNING: other process locked internal device %s, %s.",
419                                 name, retries ? "retrying remove" : "giving up");
420                         if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
421                                 debug_processes_using_device(name);
422                         sleep(1);
423                         if (force && !error_target) {
424                                 /* If force flag is set, replace device with error, read-only target.
425                                  * it should stop processes from reading it and also removed underlying
426                                  * device from mapping, so it is usable again.
427                                  * Force flag should be used only for temporary devices, which are
428                                  * intended to work inside cryptsetup only!
429                                  * Anyway, if some process try to read temporary cryptsetup device,
430                                  * it is bug - no other process should try touch it (e.g. udev).
431                                  */
432                                 _error_device(name, size);
433                                 error_target = 1;
434                         }
435                 }
436         } while (r == -EINVAL && retries);
437
438         dm_task_update_nodes();
439
440         return r;
441 }
442
443 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
444 /*
445  * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
446  * CRYPT-PLAIN-name
447  * CRYPT-LUKS1-00000000000000000000000000000000-name
448  * CRYPT-TEMP-name
449  */
450 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
451 {
452         char *ptr, uuid2[UUID_LEN] = {0};
453         uuid_t uu;
454         unsigned i = 0;
455
456         /* Remove '-' chars */
457         if (uuid && !uuid_parse(uuid, uu)) {
458                 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
459                         if (uuid[i] != '-') {
460                                 *ptr = uuid[i];
461                                 ptr++;
462                         }
463         }
464
465         i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
466                 type ?: "", type ? "-" : "",
467                 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
468                 name);
469
470         log_dbg("DM-UUID is %s", buf);
471         if (i >= buflen)
472                 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
473 }
474
475 int dm_create_device(const char *name,
476                      const char *device,
477                      const char *cipher,
478                      const char *type,
479                      const char *uuid,
480                      uint64_t size,
481                      uint64_t skip,
482                      uint64_t offset,
483                      size_t key_size,
484                      const char *key,
485                      int read_only,
486                      int reload)
487 {
488         struct dm_task *dmt = NULL;
489         struct dm_info dmi;
490         char *params = NULL;
491         char *error = NULL;
492         char dev_uuid[DM_UUID_LEN] = {0};
493         int r = -EINVAL;
494         uint32_t read_ahead = 0;
495         uint32_t cookie = 0;
496         uint16_t udev_flags = 0;
497
498         params = get_params(device, skip, offset, cipher, key_size, key);
499         if (!params)
500                 goto out_no_removal;
501
502         if (type && !strncmp(type, "TEMP", 4))
503                 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
504
505         /* All devices must have DM_UUID, only resize on old device is exception */
506         if (reload) {
507                 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
508                         goto out_no_removal;
509
510                 if (!dm_task_set_name(dmt, name))
511                         goto out_no_removal;
512         } else {
513                 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
514
515                 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
516                         goto out_no_removal;
517
518                 if (!dm_task_set_name(dmt, name))
519                         goto out_no_removal;
520
521                 if (!dm_task_set_uuid(dmt, dev_uuid))
522                         goto out_no_removal;
523
524                 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
525                         goto out_no_removal;
526         }
527
528         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
529                 goto out_no_removal;
530         if (read_only && !dm_task_set_ro(dmt))
531                 goto out_no_removal;
532         if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
533                 goto out_no_removal;
534
535 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
536         if (_dev_read_ahead(device, &read_ahead) &&
537             !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
538                 goto out_no_removal;
539 #endif
540
541         if (!dm_task_run(dmt))
542                 goto out_no_removal;
543
544         if (reload) {
545                 dm_task_destroy(dmt);
546                 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
547                         goto out;
548                 if (!dm_task_set_name(dmt, name))
549                         goto out;
550                 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
551                         goto out;
552                 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
553                         goto out;
554                 if (!dm_task_run(dmt))
555                         goto out;
556         }
557
558         if (!dm_task_get_info(dmt, &dmi))
559                 goto out;
560
561         r = 0;
562 out:
563         if (_dm_use_udev()) {
564                 (void)_dm_udev_wait(cookie);
565                 cookie = 0;
566         }
567
568         if (r < 0 && !reload) {
569                 if (get_error())
570                         error = strdup(get_error());
571
572                 dm_remove_device(name, 0, 0);
573
574                 if (error) {
575                         set_error(error);
576                         free(error);
577                 }
578         }
579
580 out_no_removal:
581         if (cookie && _dm_use_udev())
582                 (void)_dm_udev_wait(cookie);
583
584         if (params)
585                 crypt_safe_free(params);
586         if (dmt)
587                 dm_task_destroy(dmt);
588
589         dm_task_update_nodes();
590         return r;
591 }
592
593 int dm_status_device(const char *name)
594 {
595         struct dm_task *dmt;
596         struct dm_info dmi;
597         uint64_t start, length;
598         char *target_type, *params;
599         void *next = NULL;
600         int r = -EINVAL;
601
602         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
603                 goto out;
604
605         if (!dm_task_set_name(dmt, name))
606                 goto out;
607
608         if (!dm_task_run(dmt))
609                 goto out;
610
611         if (!dm_task_get_info(dmt, &dmi))
612                 goto out;
613
614         if (!dmi.exists) {
615                 r = -ENODEV;
616                 goto out;
617         }
618
619         next = dm_get_next_target(dmt, next, &start, &length,
620                                   &target_type, &params);
621         if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
622             start != 0 || next)
623                 r = -EINVAL;
624         else
625                 r = (dmi.open_count > 0);
626 out:
627         if (dmt)
628                 dm_task_destroy(dmt);
629
630         return r;
631 }
632
633 int dm_query_device(const char *name,
634                     char **device,
635                     uint64_t *size,
636                     uint64_t *skip,
637                     uint64_t *offset,
638                     char **cipher,
639                     int *key_size,
640                     char **key,
641                     int *read_only,
642                     int *suspended,
643                     char **uuid)
644 {
645         struct dm_task *dmt;
646         struct dm_info dmi;
647         uint64_t start, length, val64;
648         char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
649         void *next = NULL;
650         int i, r = -EINVAL;
651
652         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
653                 goto out;
654         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
655                 goto out;
656         if (!dm_task_set_name(dmt, name))
657                 goto out;
658         r = -ENODEV;
659         if (!dm_task_run(dmt))
660                 goto out;
661
662         r = -EINVAL;
663         if (!dm_task_get_info(dmt, &dmi))
664                 goto out;
665
666         if (!dmi.exists) {
667                 r = -ENODEV;
668                 goto out;
669         }
670
671         next = dm_get_next_target(dmt, next, &start, &length,
672                                   &target_type, &params);
673         if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
674             start != 0 || next)
675                 goto out;
676
677         if (size)
678                 *size = length;
679
680         rcipher = strsep(&params, " ");
681         /* cipher */
682         if (cipher)
683                 *cipher = strdup(rcipher);
684
685         /* skip */
686         key_ = strsep(&params, " ");
687         if (!params)
688                 goto out;
689         val64 = strtoull(params, &params, 10);
690         if (*params != ' ')
691                 goto out;
692         params++;
693         if (skip)
694                 *skip = val64;
695
696         /* device */
697         rdevice = strsep(&params, " ");
698         if (device)
699                 *device = lookup_dev(rdevice);
700
701         /*offset */
702         if (!params)
703                 goto out;
704         val64 = strtoull(params, &params, 10);
705         if (*params)
706                 goto out;
707         if (offset)
708                 *offset = val64;
709
710         /* key_size */
711         if (key_size)
712                 *key_size = strlen(key_) / 2;
713
714         /* key */
715         if (key_size && key) {
716                 *key = crypt_safe_alloc(*key_size);
717                 if (!*key) {
718                         r = -ENOMEM;
719                         goto out;
720                 }
721
722                 buffer[2] = '\0';
723                 for(i = 0; i < *key_size; i++) {
724                         memcpy(buffer, &key_[i * 2], 2);
725                         (*key)[i] = strtoul(buffer, &endp, 16);
726                         if (endp != &buffer[2]) {
727                                 crypt_safe_free(key);
728                                 *key = NULL;
729                                 goto out;
730                         }
731                 }
732         }
733         memset(key_, 0, strlen(key_));
734
735         if (read_only)
736                 *read_only = dmi.read_only;
737
738         if (suspended)
739                 *suspended = dmi.suspended;
740
741         if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
742             !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
743                 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
744
745         r = (dmi.open_count > 0);
746 out:
747         if (dmt)
748                 dm_task_destroy(dmt);
749
750         return r;
751 }
752
753 static int _dm_message(const char *name, const char *msg)
754 {
755         int r = 0;
756         struct dm_task *dmt;
757
758         if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
759                 return 0;
760
761         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
762                 goto out;
763
764         if (name && !dm_task_set_name(dmt, name))
765                 goto out;
766
767         if (!dm_task_set_sector(dmt, (uint64_t) 0))
768                 goto out;
769
770         if (!dm_task_set_message(dmt, msg))
771                 goto out;
772
773         r = dm_task_run(dmt);
774
775       out:
776         dm_task_destroy(dmt);
777         return r;
778 }
779
780 int dm_suspend_and_wipe_key(const char *name)
781 {
782         if (!_dm_check_versions())
783                 return -ENOTSUP;
784
785         if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
786                 return -ENOTSUP;
787
788         if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
789                 return -EINVAL;
790
791         if (!_dm_message(name, "key wipe")) {
792                 _dm_simple(DM_DEVICE_RESUME, name, 1);
793                 return -EINVAL;
794         }
795
796         return 0;
797 }
798
799 int dm_resume_and_reinstate_key(const char *name,
800                                 size_t key_size,
801                                 const char *key)
802 {
803         int msg_size = key_size * 2 + 10; // key set <key>
804         char *msg;
805         int r = 0;
806
807         if (!_dm_check_versions())
808                 return -ENOTSUP;
809
810         if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
811                 return -ENOTSUP;
812
813         msg = crypt_safe_alloc(msg_size);
814         if (!msg)
815                 return -ENOMEM;
816
817         memset(msg, 0, msg_size);
818         strcpy(msg, "key set ");
819         hex_key(&msg[8], key_size, key);
820
821         if (!_dm_message(name, msg) ||
822             !_dm_simple(DM_DEVICE_RESUME, name, 1))
823                 r = -EINVAL;
824
825         crypt_safe_free(msg);
826         return r;
827 }
828
829 const char *dm_get_dir(void)
830 {
831         return dm_dir();
832 }