1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "optgroups.h"
31 optgroup_luks_available (void)
33 return prog_exists ("cryptsetup");
36 /* Callers must also call remove_temp (tempfile). */
38 write_key_to_temp (const char *key)
40 char *tempfile = strdup ("/tmp/luksXXXXXX");
42 reply_with_perror ("strdup");
46 int fd = mkstemp (tempfile);
48 reply_with_perror ("mkstemp");
52 size_t len = strlen (key);
53 if (xwrite (fd, key, len) == -1) {
54 reply_with_perror ("write");
59 if (close (fd) == -1) {
60 reply_with_perror ("close");
73 remove_temp (char *tempfile)
80 luks_open (const char *device, const char *key, const char *mapname,
83 /* Sanity check: /dev/mapper/mapname must not exist already. Note
84 * that the device-mapper control device (/dev/mapper/control) is
85 * always there, so you can't ever have mapname == "control".
87 size_t len = strlen (mapname);
88 char devmapper[len+32];
89 snprintf (devmapper, len+32, "/dev/mapper/%s", mapname);
90 if (access (devmapper, F_OK) == 0) {
91 reply_with_error ("%s: device already exists", devmapper);
95 char *tempfile = write_key_to_temp (key);
102 argv[i++] = "cryptsetup";
104 argv[i++] = tempfile;
105 if (readonly) argv[i++] = "--readonly";
106 argv[i++] = "luksOpen";
112 int r = commandv (NULL, &err, (const char * const *) argv);
113 remove_temp (tempfile);
116 reply_with_error ("%s", err);
129 do_luks_open (const char *device, const char *key, const char *mapname)
131 return luks_open (device, key, mapname, 0);
135 do_luks_open_ro (const char *device, const char *key, const char *mapname)
137 return luks_open (device, key, mapname, 1);
141 do_luks_close (const char *device)
143 /* Must be /dev/mapper/... */
144 if (! STRPREFIX (device, "/dev/mapper/")) {
145 reply_with_error ("luks_close: you must call this on the /dev/mapper device created by luks_open");
149 const char *mapname = &device[12];
152 int r = command (NULL, &err, "cryptsetup", "luksClose", mapname, NULL);
154 reply_with_error ("%s", err);
167 luks_format (const char *device, const char *key, int keyslot,
170 char *tempfile = write_key_to_temp (key);
174 const char *argv[16];
178 argv[i++] = "cryptsetup";
181 argv[i++] = "--cipher";
184 argv[i++] = "--key-slot";
185 snprintf (keyslot_s, sizeof keyslot_s, "%d", keyslot);
186 argv[i++] = keyslot_s;
187 argv[i++] = "luksFormat";
189 argv[i++] = tempfile;
193 int r = commandv (NULL, &err, (const char * const *) argv);
194 remove_temp (tempfile);
197 reply_with_error ("%s", err);
210 do_luks_format (const char *device, const char *key, int keyslot)
212 return luks_format (device, key, keyslot, NULL);
216 do_luks_format_cipher (const char *device, const char *key, int keyslot,
219 return luks_format (device, key, keyslot, cipher);
223 do_luks_add_key (const char *device, const char *key, const char *newkey,
226 char *keyfile = write_key_to_temp (key);
230 char *newkeyfile = write_key_to_temp (newkey);
232 remove_temp (keyfile);
236 const char *argv[16];
240 argv[i++] = "cryptsetup";
244 argv[i++] = "--key-slot";
245 snprintf (keyslot_s, sizeof keyslot_s, "%d", keyslot);
246 argv[i++] = keyslot_s;
247 argv[i++] = "luksAddKey";
249 argv[i++] = newkeyfile;
253 int r = commandv (NULL, &err, (const char * const *) argv);
254 remove_temp (keyfile);
255 remove_temp (newkeyfile);
258 reply_with_error ("%s", err);
269 do_luks_kill_slot (const char *device, const char *key, int keyslot)
271 char *tempfile = write_key_to_temp (key);
275 const char *argv[16];
279 argv[i++] = "cryptsetup";
282 argv[i++] = tempfile;
283 argv[i++] = "luksKillSlot";
285 snprintf (keyslot_s, sizeof keyslot_s, "%d", keyslot);
286 argv[i++] = keyslot_s;
290 int r = commandv (NULL, &err, (const char * const *) argv);
291 remove_temp (tempfile);
294 reply_with_error ("%s", err);