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.
27 #include "optgroups.h"
30 optgroup_luks_available (void)
32 return prog_exists ("cryptsetup");
35 /* Callers must also call remove_temp (tempfile). */
37 write_key_to_temp (const char *key)
39 char *tempfile = strdup ("/tmp/luksXXXXXX");
41 reply_with_perror ("strdup");
45 int fd = mkstemp (tempfile);
47 reply_with_perror ("mkstemp");
51 size_t len = strlen (key);
52 if (xwrite (fd, key, len) == -1) {
53 reply_with_perror ("write");
58 if (close (fd) == -1) {
59 reply_with_perror ("close");
72 remove_temp (char *tempfile)
79 luks_open (const char *device, const char *key, const char *mapname,
82 /* Sanity check: /dev/mapper/mapname must not exist already. Note
83 * that the device-mapper control device (/dev/mapper/control) is
84 * always there, so you can't ever have mapname == "control".
86 size_t len = strlen (mapname);
87 char devmapper[len+32];
88 snprintf (devmapper, len+32, "/dev/mapper/%s", mapname);
89 if (access (devmapper, F_OK) == 0) {
90 reply_with_error ("%s: device already exists", devmapper);
94 char *tempfile = write_key_to_temp (key);
101 argv[i++] = "cryptsetup";
103 argv[i++] = tempfile;
104 if (readonly) argv[i++] = "--readonly";
105 argv[i++] = "luksOpen";
111 int r = commandv (NULL, &err, (const char * const *) argv);
112 remove_temp (tempfile);
115 reply_with_error ("%s", err);
128 do_luks_open (const char *device, const char *key, const char *mapname)
130 return luks_open (device, key, mapname, 0);
134 do_luks_open_ro (const char *device, const char *key, const char *mapname)
136 return luks_open (device, key, mapname, 1);
140 do_luks_close (const char *device)
142 /* Must be /dev/mapper/... */
143 if (! STRPREFIX (device, "/dev/mapper/")) {
144 reply_with_error ("luks_close: you must call this on the /dev/mapper device created by luks_open");
148 const char *mapname = &device[12];
151 int r = command (NULL, &err, "cryptsetup", "luksClose", mapname, NULL);
153 reply_with_error ("%s", err);
166 luks_format (const char *device, const char *key, int keyslot,
169 char *tempfile = write_key_to_temp (key);
173 const char *argv[16];
177 argv[i++] = "cryptsetup";
180 argv[i++] = "--cipher";
183 argv[i++] = "--key-slot";
184 snprintf (keyslot_s, sizeof keyslot_s, "%d", keyslot);
185 argv[i++] = keyslot_s;
186 argv[i++] = "luksFormat";
188 argv[i++] = tempfile;
192 int r = commandv (NULL, &err, (const char * const *) argv);
193 remove_temp (tempfile);
196 reply_with_error ("%s", err);
209 do_luks_format (const char *device, const char *key, int keyslot)
211 return luks_format (device, key, keyslot, NULL);
215 do_luks_format_cipher (const char *device, const char *key, int keyslot,
218 return luks_format (device, key, keyslot, cipher);
222 do_luks_add_key (const char *device, const char *key, const char *newkey,
225 char *keyfile = write_key_to_temp (key);
229 char *newkeyfile = write_key_to_temp (newkey);
231 remove_temp (keyfile);
235 const char *argv[16];
239 argv[i++] = "cryptsetup";
243 argv[i++] = "--key-slot";
244 snprintf (keyslot_s, sizeof keyslot_s, "%d", keyslot);
245 argv[i++] = keyslot_s;
246 argv[i++] = "luksAddKey";
248 argv[i++] = newkeyfile;
252 int r = commandv (NULL, &err, (const char * const *) argv);
253 remove_temp (keyfile);
254 remove_temp (newkeyfile);
257 reply_with_error ("%s", err);
268 do_luks_kill_slot (const char *device, const char *key, int keyslot)
270 char *tempfile = write_key_to_temp (key);
274 const char *argv[16];
278 argv[i++] = "cryptsetup";
281 argv[i++] = tempfile;
282 argv[i++] = "luksKillSlot";
284 snprintf (keyslot_s, sizeof keyslot_s, "%d", keyslot);
285 argv[i++] = keyslot_s;
289 int r = commandv (NULL, &err, (const char * const *) argv);
290 remove_temp (tempfile);
293 reply_with_error ("%s", err);