8 #include <sys/sysmacros.h>
13 #include <libdevmapper.h>
17 #include "libcryptsetup.h"
20 #define DEVICE_DIR "/dev"
21 #define UUID_PREFIX "CRYPT-"
22 #define CRYPT_TARGET "crypt"
25 static void set_dm_error(int level, const char *file, int line,
38 static int _dm_simple(int task, const char *name);
40 static int dm_init(void)
42 dm_log_init(set_dm_error);
43 if (!_dm_simple(DM_DEVICE_LIST_VERSIONS, "test")) {
44 set_error("Cannot communicate with device-mapper. Is the dm_mod module loaded?");
48 return 1; /* unsafe memory */
51 static void dm_exit(void)
57 static char *__lookup_dev(char *path, dev_t dev)
66 path[PATH_MAX - 1] = '\0';
67 ptr = path + strlen(path);
70 space = PATH_MAX - (ptr - path);
76 while((entry = readdir(dir))) {
77 if (entry->d_name[0] == '.' &&
78 (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
79 entry->d_name[2] == '\0')))
82 strncpy(ptr, entry->d_name, space);
83 if (lstat(path, &st) < 0)
86 if (S_ISDIR(st.st_mode)) {
87 result = __lookup_dev(path, dev);
90 } else if (S_ISBLK(st.st_mode)) {
91 if (st.st_rdev == dev) {
92 result = strdup(path);
103 static char *lookup_dev(const char *dev)
105 uint32_t major, minor;
106 char buf[PATH_MAX + 1];
108 if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
111 strncpy(buf, DEVICE_DIR, PATH_MAX);
112 buf[PATH_MAX] = '\0';
114 return __lookup_dev(buf, makedev(major, minor));
117 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
120 long read_ahead_long;
122 if ((fd = open(dev, O_RDONLY)) < 0)
125 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
129 *read_ahead = (uint32_t) read_ahead_long;
134 static char *get_params(struct crypt_options *options, const char *key)
140 hexkey = safe_alloc(options->key_size * 2 + 1);
142 set_error("Memory allocation problem");
146 for(i = 0; i < options->key_size; i++)
147 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
149 params = safe_alloc(strlen(hexkey) + strlen(options->cipher) +
150 strlen(options->device) + 64);
152 set_error("Memory allocation problem");
156 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
157 options->cipher, hexkey, options->skip,
158 options->device, options->offset);
167 static int _dm_simple(int task, const char *name)
172 if (!(dmt = dm_task_create(task)))
175 if (!dm_task_set_name(dmt, name))
178 r = dm_task_run(dmt);
181 dm_task_destroy(dmt);
185 static int _error_device(struct crypt_options *options)
190 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
193 if (!dm_task_set_name(dmt, options->name))
196 if (!dm_task_add_target(dmt, UINT64_C(0), options->size, "error", ""))
199 if (!dm_task_set_ro(dmt))
202 if (!dm_task_no_open_count(dmt))
205 if (!dm_task_run(dmt))
208 if (!_dm_simple(DM_DEVICE_RESUME, options->name)) {
209 _dm_simple(DM_DEVICE_CLEAR, options->name);
216 dm_task_destroy(dmt);
220 static int _dm_remove(struct crypt_options *options, int force)
223 int retries = force ? RETRY_COUNT : 1;
225 /* If force flag is set, replace device with error, read-only target.
226 * it should stop processes from reading it and also removed underlying
227 * device from mapping, so it is usable again.
228 * Force flag should be used only for temporary devices, which are
229 * intended to work inside cryptsetup only!
230 * Anyway, if some process try to read temporary cryptsetup device,
231 * it is bug - no other process should try touch it (e.g. udev).
234 _error_device(options);
235 retries = RETRY_COUNT;
239 r = _dm_simple(DM_DEVICE_REMOVE, options->name) ? 0 : -EINVAL;
242 } while (r == -EINVAL && retries);
244 dm_task_update_nodes();
249 static int dm_create_device(int reload, struct crypt_options *options,
250 const char *key, const char *uuid)
252 struct dm_task *dmt = NULL;
253 struct dm_task *dmt_query = NULL;
259 uint32_t read_ahead = 0;
261 params = get_params(options, key);
266 strcpy(dev_uuid, UUID_PREFIX);
267 strcat(dev_uuid, uuid);
270 if (!(dmt = dm_task_create(reload ? DM_DEVICE_RELOAD
271 : DM_DEVICE_CREATE)))
273 if (!dm_task_set_name(dmt, options->name))
275 if (options->flags & CRYPT_FLAG_READONLY && !dm_task_set_ro(dmt))
277 if (!dm_task_add_target(dmt, 0, options->size, CRYPT_TARGET, params))
280 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
281 if (_dev_read_ahead(options->device, &read_ahead) &&
282 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
286 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
289 if (!dm_task_run(dmt))
293 dm_task_destroy(dmt);
294 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
296 if (!dm_task_set_name(dmt, options->name))
298 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
300 if (!dm_task_run(dmt))
304 if (!dm_task_get_info(dmt, &dmi))
307 options->flags |= CRYPT_FLAG_READONLY;
311 if (r < 0 && !reload) {
313 error = strdup(get_error());
315 _dm_remove(options, 0);
327 dm_task_destroy(dmt);
329 dm_task_destroy(dmt_query);
330 dm_task_update_nodes();
334 static int dm_query_device(int details, struct crypt_options *options,
339 uint64_t start, length;
340 char *target_type, *params;
344 if (!(dmt = dm_task_create(details ? DM_DEVICE_TABLE
345 : DM_DEVICE_STATUS)))
347 if (!dm_task_set_name(dmt, options->name))
350 if (!dm_task_run(dmt))
354 if (!dm_task_get_info(dmt, &dmi))
362 next = dm_get_next_target(dmt, next, &start, &length,
363 &target_type, ¶ms);
364 if (!target_type || strcmp(target_type, CRYPT_TARGET) != 0 ||
368 options->hash = NULL;
369 options->cipher = NULL;
372 options->size = length;
374 char *cipher, *key_, *device;
377 set_error("Invalid dm table");
379 cipher = strsep(¶ms, " ");
380 key_ = strsep(¶ms, " ");
384 val64 = strtoull(params, ¶ms, 10);
388 options->skip = val64;
390 device = strsep(¶ms, " ");
394 val64 = strtoull(params, ¶ms, 10);
397 options->offset = val64;
399 options->cipher = strdup(cipher);
400 options->key_size = strlen(key_) / 2;
406 *key = safe_alloc(options->key_size);
408 set_error("Out of memory");
414 for(i = 0; i < options->key_size; i++) {
415 memcpy(buffer, &key_[i * 2], 2);
416 (*key)[i] = strtoul(buffer, &endp, 16);
417 if (endp != &buffer[2]) {
424 memset(key_, 0, strlen(key_));
425 options->device = lookup_dev(device);
430 r = (dmi.open_count > 0);
434 dm_task_destroy(dmt);
437 options->flags |= CRYPT_FLAG_FREE_DEVICE;
439 options->flags |= CRYPT_FLAG_FREE_CIPHER;
440 options->flags &= ~CRYPT_FLAG_READONLY;
442 options->flags |= CRYPT_FLAG_READONLY;
444 if (options->device) {
445 free((char *)options->device);
446 options->device = NULL;
447 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
449 if (options->cipher) {
450 free((char *)options->cipher);
451 options->cipher = NULL;
452 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
458 static int dm_remove_device(int force, struct crypt_options *options)
460 if (!options || !options->name)
463 return _dm_remove(options, force);;
467 static const char *dm_get_dir(void)
472 struct setup_backend setup_libdevmapper_backend = {
476 .create = dm_create_device,
477 .status = dm_query_device,
478 .remove = dm_remove_device,