Add progress messages to download command.
[libguestfs.git] / daemon / luks.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "daemon.h"
26 #include "c-ctype.h"
27 #include "actions.h"
28 #include "optgroups.h"
29
30 int
31 optgroup_luks_available (void)
32 {
33   return prog_exists ("cryptsetup");
34 }
35
36 /* Callers must also call remove_temp (tempfile). */
37 static char *
38 write_key_to_temp (const char *key)
39 {
40   char *tempfile = strdup ("/tmp/luksXXXXXX");
41   if (!tempfile) {
42     reply_with_perror ("strdup");
43     return NULL;
44   }
45
46   int fd = mkstemp (tempfile);
47   if (fd == -1) {
48     reply_with_perror ("mkstemp");
49     goto error;
50   }
51
52   size_t len = strlen (key);
53   if (xwrite (fd, key, len) == -1) {
54     reply_with_perror ("write");
55     close (fd);
56     goto error;
57   }
58
59   if (close (fd) == -1) {
60     reply_with_perror ("close");
61     goto error;
62   }
63
64   return tempfile;
65
66  error:
67   unlink (tempfile);
68   free (tempfile);
69   return NULL;
70 }
71
72 static void
73 remove_temp (char *tempfile)
74 {
75   unlink (tempfile);
76   free (tempfile);
77 }
78
79 static int
80 luks_open (const char *device, const char *key, const char *mapname,
81            int readonly)
82 {
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".
86    */
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);
92     return -1;
93   }
94
95   char *tempfile = write_key_to_temp (key);
96   if (!tempfile)
97     return -1;
98
99   const char *argv[16];
100   size_t i = 0;
101
102   argv[i++] = "cryptsetup";
103   argv[i++] = "-d";
104   argv[i++] = tempfile;
105   if (readonly) argv[i++] = "--readonly";
106   argv[i++] = "luksOpen";
107   argv[i++] = device;
108   argv[i++] = mapname;
109   argv[i++] = NULL;
110
111   char *err;
112   int r = commandv (NULL, &err, (const char * const *) argv);
113   remove_temp (tempfile);
114
115   if (r == -1) {
116     reply_with_error ("%s", err);
117     free (err);
118     return -1;
119   }
120
121   free (err);
122
123   udev_settle ();
124
125   return 0;
126 }
127
128 int
129 do_luks_open (const char *device, const char *key, const char *mapname)
130 {
131   return luks_open (device, key, mapname, 0);
132 }
133
134 int
135 do_luks_open_ro (const char *device, const char *key, const char *mapname)
136 {
137   return luks_open (device, key, mapname, 1);
138 }
139
140 int
141 do_luks_close (const char *device)
142 {
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");
146     return -1;
147   }
148
149   const char *mapname = &device[12];
150
151   char *err;
152   int r = command (NULL, &err, "cryptsetup", "luksClose", mapname, NULL);
153   if (r == -1) {
154     reply_with_error ("%s", err);
155     free (err);
156     return -1;
157   }
158
159   free (err);
160
161   udev_settle ();
162
163   return 0;
164 }
165
166 static int
167 luks_format (const char *device, const char *key, int keyslot,
168              const char *cipher)
169 {
170   char *tempfile = write_key_to_temp (key);
171   if (!tempfile)
172     return -1;
173
174   const char *argv[16];
175   char keyslot_s[16];
176   size_t i = 0;
177
178   argv[i++] = "cryptsetup";
179   argv[i++] = "-q";
180   if (cipher) {
181     argv[i++] = "--cipher";
182     argv[i++] = cipher;
183   }
184   argv[i++] = "--key-slot";
185   snprintf (keyslot_s, sizeof keyslot_s, "%d", keyslot);
186   argv[i++] = keyslot_s;
187   argv[i++] = "luksFormat";
188   argv[i++] = device;
189   argv[i++] = tempfile;
190   argv[i++] = NULL;
191
192   char *err;
193   int r = commandv (NULL, &err, (const char * const *) argv);
194   remove_temp (tempfile);
195
196   if (r == -1) {
197     reply_with_error ("%s", err);
198     free (err);
199     return -1;
200   }
201
202   free (err);
203
204   udev_settle ();
205
206   return 0;
207 }
208
209 int
210 do_luks_format (const char *device, const char *key, int keyslot)
211 {
212   return luks_format (device, key, keyslot, NULL);
213 }
214
215 int
216 do_luks_format_cipher (const char *device, const char *key, int keyslot,
217                        const char *cipher)
218 {
219   return luks_format (device, key, keyslot, cipher);
220 }
221
222 int
223 do_luks_add_key (const char *device, const char *key, const char *newkey,
224                  int keyslot)
225 {
226   char *keyfile = write_key_to_temp (key);
227   if (!keyfile)
228     return -1;
229
230   char *newkeyfile = write_key_to_temp (newkey);
231   if (!newkeyfile) {
232     remove_temp (keyfile);
233     return -1;
234   }
235
236   const char *argv[16];
237   char keyslot_s[16];
238   size_t i = 0;
239
240   argv[i++] = "cryptsetup";
241   argv[i++] = "-q";
242   argv[i++] = "-d";
243   argv[i++] = keyfile;
244   argv[i++] = "--key-slot";
245   snprintf (keyslot_s, sizeof keyslot_s, "%d", keyslot);
246   argv[i++] = keyslot_s;
247   argv[i++] = "luksAddKey";
248   argv[i++] = device;
249   argv[i++] = newkeyfile;
250   argv[i++] = NULL;
251
252   char *err;
253   int r = commandv (NULL, &err, (const char * const *) argv);
254   remove_temp (keyfile);
255   remove_temp (newkeyfile);
256
257   if (r == -1) {
258     reply_with_error ("%s", err);
259     free (err);
260     return -1;
261   }
262
263   free (err);
264
265   return 0;
266 }
267
268 int
269 do_luks_kill_slot (const char *device, const char *key, int keyslot)
270 {
271   char *tempfile = write_key_to_temp (key);
272   if (!tempfile)
273     return -1;
274
275   const char *argv[16];
276   char keyslot_s[16];
277   size_t i = 0;
278
279   argv[i++] = "cryptsetup";
280   argv[i++] = "-q";
281   argv[i++] = "-d";
282   argv[i++] = tempfile;
283   argv[i++] = "luksKillSlot";
284   argv[i++] = device;
285   snprintf (keyslot_s, sizeof keyslot_s, "%d", keyslot);
286   argv[i++] = keyslot_s;
287   argv[i++] = NULL;
288
289   char *err;
290   int r = commandv (NULL, &err, (const char * const *) argv);
291   remove_temp (tempfile);
292
293   if (r == -1) {
294     reply_with_error ("%s", err);
295     free (err);
296     return -1;
297   }
298
299   free (err);
300
301   return 0;
302 }