New APIs: Support for opening LUKS-encrypted disks.
[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 static int
37 luks_open (const char *device, const char *key, const char *mapname,
38            int readonly)
39 {
40   /* Sanity check: /dev/mapper/mapname must not exist already.  Note
41    * that the device-mapper control device (/dev/mapper/control) is
42    * always there, so you can't ever have mapname == "control".
43    */
44   size_t len = strlen (mapname);
45   char devmapper[len+32];
46   snprintf (devmapper, len+32, "/dev/mapper/%s", mapname);
47   if (access (devmapper, F_OK) == 0) {
48     reply_with_error ("%s: device already exists", devmapper);
49     return -1;
50   }
51
52   char tempfile[] = "/tmp/luksXXXXXX";
53   int fd = mkstemp (tempfile);
54   if (fd == -1) {
55     reply_with_perror ("mkstemp");
56     return -1;
57   }
58
59   len = strlen (key);
60   if (xwrite (fd, key, len) == -1) {
61     reply_with_perror ("write");
62     close (fd);
63     unlink (tempfile);
64     return -1;
65   }
66
67   if (close (fd) == -1) {
68     reply_with_perror ("close");
69     unlink (tempfile);
70     return -1;
71   }
72
73   const char *argv[16];
74   size_t i = 0;
75
76   argv[i++] = "cryptsetup";
77   argv[i++] = "-d";
78   argv[i++] = tempfile;
79   if (readonly) argv[i++] = "--readonly";
80   argv[i++] = "luksOpen";
81   argv[i++] = device;
82   argv[i++] = mapname;
83   argv[i++] = NULL;
84
85   char *err;
86   int r = commandv (NULL, &err, (const char * const *) argv);
87   unlink (tempfile);
88
89   if (r == -1) {
90     reply_with_error ("%s", err);
91     free (err);
92     return -1;
93   }
94
95   free (err);
96
97   udev_settle ();
98
99   return 0;
100 }
101
102 int
103 do_luks_open (const char *device, const char *key, const char *mapname)
104 {
105   return luks_open (device, key, mapname, 0);
106 }
107
108 int
109 do_luks_open_ro (const char *device, const char *key, const char *mapname)
110 {
111   return luks_open (device, key, mapname, 1);
112 }
113
114 int
115 do_luks_close (const char *device)
116 {
117   /* Must be /dev/mapper/... */
118   if (! STRPREFIX (device, "/dev/mapper/")) {
119     reply_with_error ("luks_close: you must call this on the /dev/mapper device created by luks_open");
120     return -1;
121   }
122
123   const char *mapname = &device[12];
124
125   char *err;
126   int r = command (NULL, &err, "cryptsetup", "luksClose", mapname, NULL);
127   if (r == -1) {
128     reply_with_error ("%s", err);
129     free (err);
130     return -1;
131   }
132
133   free (err);
134
135   udev_settle ();
136
137   return 0;
138 }