chiark / gitweb /
f7c3f23f5e7cda669e6cb48da2b46340fa3eb6a3
[elogind.git] / src / cryptsetup-generator.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <string.h>
23 #include <errno.h>
24 #include <unistd.h>
25
26 #include "log.h"
27 #include "util.h"
28 #include "unit-name.h"
29
30 const char *arg_dest = "/tmp";
31
32 static bool has_option(const char *haystack, const char *needle) {
33         const char *f = haystack;
34         size_t l;
35
36         assert(needle);
37
38         if (!haystack)
39                 return false;
40
41         l = strlen(needle);
42
43         while ((f = strstr(f, needle))) {
44
45                 if (f > haystack && f[-1] != ',') {
46                         f++;
47                         continue;
48                 }
49
50                 if (f[l] != 0 && f[l] == ',') {
51                         f++;
52                         continue;
53                 }
54
55                 return true;
56         }
57
58         return false;
59 }
60
61 static int create_disk(
62                 const char *name,
63                 const char *device,
64                 const char *password,
65                 const char *options) {
66
67         char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *from = NULL, *to = NULL, *e = NULL;
68         int r;
69         FILE *f = NULL;
70
71         assert(name);
72         assert(device);
73
74         if (!(n = unit_name_build_escape("cryptsetup", name, ".service"))) {
75                 r = -ENOMEM;
76                 log_error("Failed to allocate unit name.");
77                 goto fail;
78         }
79
80         if (asprintf(&p, "%s/%s", arg_dest, n) < 0) {
81                 r = -ENOMEM;
82                 log_error("Failed to allocate unit file name.");
83                 goto fail;
84         }
85
86         if (!(u = fstab_node_to_udev_node(device))) {
87                 r = -ENOMEM;
88                 log_error("Failed to allocate device node.");
89                 goto fail;
90         }
91
92         if (!(d = unit_name_from_path(u, ".device"))) {
93                 r = -ENOMEM;
94                 log_error("Failed to allocate device name.");
95                 goto fail;
96         }
97
98         if (!(f = fopen(p, "wxe"))) {
99                 r = -errno;
100                 log_error("Failed to create unit file: %m");
101                 goto fail;
102         }
103
104         fprintf(f,
105                 "[Unit]\n"
106                 "Description=Cryptography Setup for %%I\n"
107                 "DefaultDependencies=no\n"
108                 "BindTo=%s dev-mapper-%%i.device\n"
109                 "After=systemd-readahead-collect.service systemd-readahead-replay.service %s\n"
110                 "Before=dev-mapper-%%i.device shutdown.target cryptsetup.target\n",
111                 d, d);
112
113         if (password && (streq(password, "/dev/urandom") ||
114                          streq(password, "/dev/random") ||
115                          streq(password, "/dev/hw_random")))
116                 fprintf(f,
117                         "After=systemd-random-seed-load.service\n");
118
119         fprintf(f,
120                 "\n[Service]\n"
121                 "Type=oneshot\n"
122                 "RemainAfterExit=yes\n"
123                 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
124                 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n"
125                 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
126                 name, u, strempty(password), strempty(options),
127                 name);
128
129         if (has_option(options, "tmp"))
130                 fprintf(f,
131                         "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
132                         name);
133
134         if (has_option(options, "swap"))
135                 fprintf(f,
136                         "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
137                         name);
138
139         fflush(f);
140
141         if (ferror(f)) {
142                 r = -errno;
143                 log_error("Failed to write file: %m");
144                 goto fail;
145         }
146
147         if (asprintf(&from, "../%s", n) < 0) {
148                 r = -ENOMEM;
149                 goto fail;
150         }
151
152         if (!has_option(options, "noauto")) {
153
154                 if (asprintf(&to, "%s/%s.wants/%s", arg_dest, d, n) < 0) {
155                         r = -ENOMEM;
156                         goto fail;
157                 }
158
159                 mkdir_parents(to, 0755);
160
161                 if (symlink(from, to) < 0) {
162                         log_error("Failed to create symlink '%s' to '%s': %m", from, to);
163                         r = -errno;
164                         goto fail;
165                 }
166
167                 free(to);
168                 to = NULL;
169
170                 if (!has_option(options, "nofail")) {
171
172                         if (asprintf(&to, "%s/cryptsetup.target.wants/%s", arg_dest, n) < 0) {
173                                 r = -ENOMEM;
174                                 goto fail;
175                         }
176
177                         mkdir_parents(to, 0755);
178
179                         if (symlink(from, to) < 0) {
180                                 log_error("Failed to create symlink '%s' to '%s': %m", from, to);
181                                 r = -errno;
182                                 goto fail;
183                         }
184                 }
185         }
186
187         free(to);
188         to = NULL;
189
190         e = unit_name_escape(name);
191         if (asprintf(&to, "%s/dev-mapper-%s.device.requires/%s", arg_dest, e, n) < 0) {
192                 r = -ENOMEM;
193                 goto fail;
194         }
195
196         mkdir_parents(to, 0755);
197
198         if (symlink(from, to) < 0) {
199                 log_error("Failed to create symlink '%s' to '%s': %m", from, to);
200                 r = -errno;
201                 goto fail;
202         }
203
204         r = 0;
205
206 fail:
207         free(p);
208         free(n);
209         free(d);
210         free(e);
211
212         free(from);
213         free(to);
214
215         if (f)
216                 fclose(f);
217
218         return r;
219 }
220
221 int main(int argc, char *argv[]) {
222         FILE *f;
223         int r = EXIT_SUCCESS;
224         unsigned n = 0;
225
226         if (argc > 2) {
227                 log_error("This program takes one or no arguments.");
228                 return EXIT_FAILURE;
229         }
230
231         if (argc > 1)
232                 arg_dest = argv[1];
233
234         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
235         log_parse_environment();
236         log_open();
237
238         if (!(f = fopen("/etc/crypttab", "re"))) {
239
240                 if (errno == ENOENT)
241                         r = EXIT_SUCCESS;
242                 else {
243                         r = EXIT_FAILURE;
244                         log_error("Failed to open /etc/crypttab: %m");
245                 }
246
247                 goto finish;
248         }
249
250         for (;;) {
251                 char line[LINE_MAX], *l;
252                 char *name = NULL, *device = NULL, *password = NULL, *options = NULL;
253                 int k;
254
255                 if (!(fgets(line, sizeof(line), f)))
256                         break;
257
258                 n++;
259
260                 l = strstrip(line);
261                 if (*l == '#' || *l == 0)
262                         continue;
263
264                 if ((k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &password, &options)) < 2 || k > 4) {
265                         log_error("Failed to parse /etc/crypttab:%u, ignoring.", n);
266                         r = EXIT_FAILURE;
267                         goto next;
268                 }
269
270                 if (create_disk(name, device, password, options) < 0)
271                         r = EXIT_FAILURE;
272
273         next:
274                 free(name);
275                 free(device);
276                 free(password);
277                 free(options);
278         }
279
280 finish:
281         return r;
282 }