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