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