8 #include <sys/sysmacros.h>
13 #include <libdevmapper.h>
17 #include "libcryptsetup.h"
21 #define DEVICE_DIR "/dev"
22 #define DM_UUID_PREFIX "CRYPT-"
23 #define DM_UUID_PREFIX_LEN 6
24 #define DM_UUID_LEN UUID_STRING_L
25 #define DM_CRYPT_TARGET "crypt"
28 static void set_dm_error(int level, const char *file, int line,
41 static int _dm_simple(int task, const char *name);
43 static int dm_init(void)
45 dm_log_init(set_dm_error);
46 if (!_dm_simple(DM_DEVICE_LIST_VERSIONS, "test")) {
47 set_error("Cannot communicate with device-mapper. Is the dm_mod module loaded?");
51 return 1; /* unsafe memory */
54 static void dm_exit(void)
60 static char *__lookup_dev(char *path, dev_t dev)
69 path[PATH_MAX - 1] = '\0';
70 ptr = path + strlen(path);
73 space = PATH_MAX - (ptr - path);
79 while((entry = readdir(dir))) {
80 if (entry->d_name[0] == '.' &&
81 (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
82 entry->d_name[2] == '\0')))
85 strncpy(ptr, entry->d_name, space);
86 if (lstat(path, &st) < 0)
89 if (S_ISDIR(st.st_mode)) {
90 result = __lookup_dev(path, dev);
93 } else if (S_ISBLK(st.st_mode)) {
94 if (st.st_rdev == dev) {
95 result = strdup(path);
106 static char *lookup_dev(const char *dev)
108 uint32_t major, minor;
109 char buf[PATH_MAX + 1];
111 if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
114 strncpy(buf, DEVICE_DIR, PATH_MAX);
115 buf[PATH_MAX] = '\0';
117 return __lookup_dev(buf, makedev(major, minor));
120 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
123 long read_ahead_long;
125 if ((fd = open(dev, O_RDONLY)) < 0)
128 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
132 *read_ahead = (uint32_t) read_ahead_long;
137 static char *get_params(struct crypt_options *options, const char *key)
143 hexkey = safe_alloc(options->key_size * 2 + 1);
145 set_error("Memory allocation problem");
149 for(i = 0; i < options->key_size; i++)
150 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
152 params = safe_alloc(strlen(hexkey) + strlen(options->cipher) +
153 strlen(options->device) + 64);
155 set_error("Memory allocation problem");
159 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
160 options->cipher, hexkey, options->skip,
161 options->device, options->offset);
170 static int _dm_simple(int task, const char *name)
175 if (!(dmt = dm_task_create(task)))
178 if (!dm_task_set_name(dmt, name))
181 r = dm_task_run(dmt);
184 dm_task_destroy(dmt);
188 static int _error_device(struct crypt_options *options)
193 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
196 if (!dm_task_set_name(dmt, options->name))
199 if (!dm_task_add_target(dmt, UINT64_C(0), options->size, "error", ""))
202 if (!dm_task_set_ro(dmt))
205 if (!dm_task_no_open_count(dmt))
208 if (!dm_task_run(dmt))
211 if (!_dm_simple(DM_DEVICE_RESUME, options->name)) {
212 _dm_simple(DM_DEVICE_CLEAR, options->name);
219 dm_task_destroy(dmt);
223 static int _dm_remove(struct crypt_options *options, int force)
226 int retries = force ? RETRY_COUNT : 1;
228 /* If force flag is set, replace device with error, read-only target.
229 * it should stop processes from reading it and also removed underlying
230 * device from mapping, so it is usable again.
231 * Force flag should be used only for temporary devices, which are
232 * intended to work inside cryptsetup only!
233 * Anyway, if some process try to read temporary cryptsetup device,
234 * it is bug - no other process should try touch it (e.g. udev).
237 _error_device(options);
238 retries = RETRY_COUNT;
242 r = _dm_simple(DM_DEVICE_REMOVE, options->name) ? 0 : -EINVAL;
245 } while (r == -EINVAL && retries);
247 dm_task_update_nodes();
252 static int dm_create_device(int reload, struct crypt_options *options,
253 const char *key, const char *uuid)
255 struct dm_task *dmt = NULL;
256 struct dm_task *dmt_query = NULL;
260 char dev_uuid[DM_UUID_PREFIX_LEN + DM_UUID_LEN + 1] = {0};
262 uint32_t read_ahead = 0;
264 params = get_params(options, key);
269 strncpy(dev_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN);
270 strncpy(dev_uuid + DM_UUID_PREFIX_LEN, uuid, DM_UUID_LEN);
271 dev_uuid[DM_UUID_PREFIX_LEN + DM_UUID_LEN] = '\0';
274 if (!(dmt = dm_task_create(reload ? DM_DEVICE_RELOAD
275 : DM_DEVICE_CREATE)))
277 if (!dm_task_set_name(dmt, options->name))
279 if (options->flags & CRYPT_FLAG_READONLY && !dm_task_set_ro(dmt))
281 if (!dm_task_add_target(dmt, 0, options->size, DM_CRYPT_TARGET, params))
284 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
285 if (_dev_read_ahead(options->device, &read_ahead) &&
286 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
290 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
293 if (!dm_task_run(dmt))
297 dm_task_destroy(dmt);
298 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
300 if (!dm_task_set_name(dmt, options->name))
302 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
304 if (!dm_task_run(dmt))
308 if (!dm_task_get_info(dmt, &dmi))
311 options->flags |= CRYPT_FLAG_READONLY;
315 if (r < 0 && !reload) {
317 error = strdup(get_error());
319 _dm_remove(options, 0);
331 dm_task_destroy(dmt);
333 dm_task_destroy(dmt_query);
334 dm_task_update_nodes();
338 static int dm_query_device(int details, struct crypt_options *options,
343 uint64_t start, length;
344 char *target_type, *params;
348 if (!(dmt = dm_task_create(details ? DM_DEVICE_TABLE
349 : DM_DEVICE_STATUS)))
351 if (!dm_task_set_name(dmt, options->name))
354 if (!dm_task_run(dmt))
358 if (!dm_task_get_info(dmt, &dmi))
366 next = dm_get_next_target(dmt, next, &start, &length,
367 &target_type, ¶ms);
368 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
372 options->hash = NULL;
373 options->cipher = NULL;
376 options->size = length;
378 char *cipher, *key_, *device;
381 set_error("Invalid dm table");
383 cipher = strsep(¶ms, " ");
384 key_ = strsep(¶ms, " ");
388 val64 = strtoull(params, ¶ms, 10);
392 options->skip = val64;
394 device = strsep(¶ms, " ");
398 val64 = strtoull(params, ¶ms, 10);
401 options->offset = val64;
403 options->cipher = strdup(cipher);
404 options->key_size = strlen(key_) / 2;
410 *key = safe_alloc(options->key_size);
412 set_error("Out of memory");
418 for(i = 0; i < options->key_size; i++) {
419 memcpy(buffer, &key_[i * 2], 2);
420 (*key)[i] = strtoul(buffer, &endp, 16);
421 if (endp != &buffer[2]) {
428 memset(key_, 0, strlen(key_));
429 options->device = lookup_dev(device);
434 r = (dmi.open_count > 0);
438 dm_task_destroy(dmt);
441 options->flags |= CRYPT_FLAG_FREE_DEVICE;
443 options->flags |= CRYPT_FLAG_FREE_CIPHER;
444 options->flags &= ~CRYPT_FLAG_READONLY;
446 options->flags |= CRYPT_FLAG_READONLY;
448 if (options->device) {
449 free((char *)options->device);
450 options->device = NULL;
451 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
453 if (options->cipher) {
454 free((char *)options->cipher);
455 options->cipher = NULL;
456 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
462 static int dm_remove_device(int force, struct crypt_options *options)
464 if (!options || !options->name)
467 return _dm_remove(options, force);;
471 static const char *dm_get_dir(void)
476 struct setup_backend setup_libdevmapper_backend = {
480 .create = dm_create_device,
481 .status = dm_query_device,
482 .remove = dm_remove_device,