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