8 #include <sys/sysmacros.h>
12 #include <libdevmapper.h>
14 #include "libcryptsetup.h"
17 #define DEVICE_DIR "/dev"
19 #define CRYPT_TARGET "crypt"
22 static void set_dm_error(int level, const char *file, int line,
35 static int _dm_simple(int task, const char *name);
37 static int dm_init(void)
39 dm_log_init(set_dm_error);
40 if (!_dm_simple(DM_DEVICE_LIST_VERSIONS, "test")) {
41 set_error("Cannot communicate with device-mapper. Is the dm_mod module loaded?");
45 return 1; /* unsafe memory */
48 static void dm_exit(void)
54 static char *__lookup_dev(char *path, dev_t dev)
63 path[PATH_MAX - 1] = '\0';
64 ptr = path + strlen(path);
67 space = PATH_MAX - (ptr - path);
73 while((entry = readdir(dir))) {
74 if (entry->d_name[0] == '.' &&
75 (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
76 entry->d_name[2] == '\0')))
79 strncpy(ptr, entry->d_name, space);
80 if (lstat(path, &st) < 0)
83 if (S_ISDIR(st.st_mode)) {
84 result = __lookup_dev(path, dev);
87 } else if (S_ISBLK(st.st_mode)) {
88 if (st.st_rdev == dev) {
89 result = strdup(path);
100 static char *lookup_dev(const char *dev)
102 uint32_t major, minor;
103 char buf[PATH_MAX + 1];
105 if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
108 strncpy(buf, DEVICE_DIR, PATH_MAX);
109 buf[PATH_MAX] = '\0';
111 return __lookup_dev(buf, makedev(major, minor));
114 static char *get_params(struct crypt_options *options, const char *key)
120 hexkey = safe_alloc(options->key_size * 2 + 1);
122 set_error("Memory allocation problem");
126 for(i = 0; i < options->key_size; i++)
127 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
129 params = safe_alloc(strlen(hexkey) + strlen(options->cipher) +
130 strlen(options->device) + 64);
132 set_error("Memory allocation problem");
136 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
137 options->cipher, hexkey, options->skip,
138 options->device, options->offset);
147 static int _dm_simple(int task, const char *name)
152 if (!(dmt = dm_task_create(task)))
155 if (!dm_task_set_name(dmt, name))
158 r = dm_task_run(dmt);
161 dm_task_destroy(dmt);
165 static int _error_device(struct crypt_options *options)
170 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
173 if (!dm_task_set_name(dmt, options->name))
176 if (!dm_task_add_target(dmt, UINT64_C(0), options->size, "error", ""))
179 if (!dm_task_set_ro(dmt))
182 if (!dm_task_no_open_count(dmt))
185 if (!dm_task_run(dmt))
188 if (!_dm_simple(DM_DEVICE_RESUME, options->name)) {
189 _dm_simple(DM_DEVICE_CLEAR, options->name);
196 dm_task_destroy(dmt);
200 static int _dm_remove(struct crypt_options *options, int force)
203 int retries = force ? RETRY_COUNT : 1;
205 /* If force flag is set, replace device with error, read-only target.
206 * it should stop processes from reading it and also removed underlying
207 * device from mapping, so it is usable again.
208 * Force flag should be used only for temporary devices, which are
209 * intended to work inside cryptsetup only!
210 * Anyway, if some process try to read temporary cryptsetup device,
211 * it is bug - no other process should try touch it (e.g. udev).
214 _error_device(options);
215 retries = RETRY_COUNT;
219 r = _dm_simple(DM_DEVICE_REMOVE, options->name) ? 0 : -EINVAL;
222 } while (r == -EINVAL && retries);
224 dm_task_update_nodes();
229 static int dm_create_device(int reload, struct crypt_options *options,
232 struct dm_task *dmt = NULL;
233 struct dm_task *dmt_query = NULL;
239 params = get_params(options, key);
242 if (!(dmt = dm_task_create(reload ? DM_DEVICE_RELOAD
243 : DM_DEVICE_CREATE)))
245 if (!dm_task_set_name(dmt, options->name))
247 if (options->flags & CRYPT_FLAG_READONLY && !dm_task_set_ro(dmt))
249 if (!dm_task_add_target(dmt, 0, options->size, CRYPT_TARGET, params))
251 if (!dm_task_run(dmt))
255 dm_task_destroy(dmt);
256 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
258 if (!dm_task_set_name(dmt, options->name))
260 if (!dm_task_run(dmt))
264 if (!dm_task_get_info(dmt, &dmi))
267 options->flags |= CRYPT_FLAG_READONLY;
271 if (r < 0 && !reload) {
273 error = strdup(get_error());
275 _dm_remove(options, 0);
287 dm_task_destroy(dmt);
289 dm_task_destroy(dmt_query);
290 dm_task_update_nodes();
294 static int dm_query_device(int details, struct crypt_options *options,
299 uint64_t start, length;
300 char *target_type, *params;
304 if (!(dmt = dm_task_create(details ? DM_DEVICE_TABLE
305 : DM_DEVICE_STATUS)))
307 if (!dm_task_set_name(dmt, options->name))
310 if (!dm_task_run(dmt))
314 if (!dm_task_get_info(dmt, &dmi))
322 next = dm_get_next_target(dmt, next, &start, &length,
323 &target_type, ¶ms);
324 if (!target_type || strcmp(target_type, CRYPT_TARGET) != 0 ||
328 options->hash = NULL;
329 options->cipher = NULL;
332 options->size = length;
334 char *cipher, *key_, *device;
337 set_error("Invalid dm table");
339 cipher = strsep(¶ms, " ");
340 key_ = strsep(¶ms, " ");
344 val64 = strtoull(params, ¶ms, 10);
348 options->skip = val64;
350 device = strsep(¶ms, " ");
354 val64 = strtoull(params, ¶ms, 10);
357 options->offset = val64;
359 options->cipher = strdup(cipher);
360 options->key_size = strlen(key_) / 2;
366 *key = safe_alloc(options->key_size);
368 set_error("Out of memory");
374 for(i = 0; i < options->key_size; i++) {
375 memcpy(buffer, &key_[i * 2], 2);
376 (*key)[i] = strtoul(buffer, &endp, 16);
377 if (endp != &buffer[2]) {
384 memset(key_, 0, strlen(key_));
385 options->device = lookup_dev(device);
390 r = (dmi.open_count > 0);
394 dm_task_destroy(dmt);
397 options->flags |= CRYPT_FLAG_FREE_DEVICE;
399 options->flags |= CRYPT_FLAG_FREE_CIPHER;
400 options->flags &= ~CRYPT_FLAG_READONLY;
402 options->flags |= CRYPT_FLAG_READONLY;
404 if (options->device) {
405 free((char *)options->device);
406 options->device = NULL;
407 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
409 if (options->cipher) {
410 free((char *)options->cipher);
411 options->cipher = NULL;
412 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
418 static int dm_remove_device(int force, struct crypt_options *options)
420 if (!options || !options->name)
423 return _dm_remove(options, force);;
427 static const char *dm_get_dir(void)
432 struct setup_backend setup_libdevmapper_backend = {
436 .create = dm_create_device,
437 .status = dm_query_device,
438 .remove = dm_remove_device,