chiark / gitweb /
getty: simplify things a bit
[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         bool noauto, nofail;
71
72         assert(name);
73         assert(device);
74
75         noauto = has_option(options, "noauto");
76         nofail = has_option(options, "nofail");
77
78         if (!(n = unit_name_build_escape("cryptsetup", name, ".service"))) {
79                 r = -ENOMEM;
80                 log_error("Failed to allocate unit name.");
81                 goto fail;
82         }
83
84         if (asprintf(&p, "%s/%s", arg_dest, n) < 0) {
85                 r = -ENOMEM;
86                 log_error("Failed to allocate unit file name.");
87                 goto fail;
88         }
89
90         if (!(u = fstab_node_to_udev_node(device))) {
91                 r = -ENOMEM;
92                 log_error("Failed to allocate device node.");
93                 goto fail;
94         }
95
96         if (!(d = unit_name_from_path(u, ".device"))) {
97                 r = -ENOMEM;
98                 log_error("Failed to allocate device name.");
99                 goto fail;
100         }
101
102         if (!(f = fopen(p, "wxe"))) {
103                 r = -errno;
104                 log_error("Failed to create unit file: %m");
105                 goto fail;
106         }
107
108         fprintf(f,
109                 "[Unit]\n"
110                 "Description=Cryptography Setup for %%I\n"
111                 "Conflicts=umount.target\n"
112                 "DefaultDependencies=no\n"
113                 "BindTo=%s dev-mapper-%%i.device\n"
114                 "After=systemd-readahead-collect.service systemd-readahead-replay.service %s\n"
115                 "Before=umount.target\n"
116                 "Before=local-fs.target\n",
117                 d, d);
118
119         if (!nofail)
120                 fprintf(f,
121                         "Before=cryptsetup.target\n");
122
123         if (password && (streq(password, "/dev/urandom") ||
124                          streq(password, "/dev/random") ||
125                          streq(password, "/dev/hw_random")))
126                 fprintf(f,
127                         "After=systemd-random-seed-load.service\n");
128
129         fprintf(f,
130                 "\n[Service]\n"
131                 "Type=oneshot\n"
132                 "RemainAfterExit=yes\n"
133                 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
134                 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n"
135                 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
136                 name, u, strempty(password), strempty(options),
137                 name);
138
139         if (has_option(options, "tmp"))
140                 fprintf(f,
141                         "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
142                         name);
143
144         if (has_option(options, "swap"))
145                 fprintf(f,
146                         "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
147                         name);
148
149         fflush(f);
150
151         if (ferror(f)) {
152                 r = -errno;
153                 log_error("Failed to write file: %m");
154                 goto fail;
155         }
156
157         if (asprintf(&from, "../%s", n) < 0) {
158                 r = -ENOMEM;
159                 goto fail;
160         }
161
162         if (!noauto) {
163
164                 if (asprintf(&to, "%s/%s.wants/%s", arg_dest, d, n) < 0) {
165                         r = -ENOMEM;
166                         goto fail;
167                 }
168
169                 mkdir_parents(to, 0755);
170
171                 if (symlink(from, to) < 0) {
172                         log_error("Failed to create symlink '%s' to '%s': %m", from, to);
173                         r = -errno;
174                         goto fail;
175                 }
176
177                 free(to);
178                 to = NULL;
179
180                 if (!nofail)
181                         asprintf(&to, "%s/cryptsetup.target.requires/%s", arg_dest, n);
182                 else
183                         asprintf(&to, "%s/cryptsetup.target.wants/%s", arg_dest, n);
184
185                 if (!to) {
186                         r = -ENOMEM;
187                         goto fail;
188                 }
189
190                 mkdir_parents(to, 0755);
191
192                 if (symlink(from, to) < 0) {
193                         log_error("Failed to create symlink '%s' to '%s': %m", from, to);
194                         r = -errno;
195                         goto fail;
196                 }
197         }
198
199         free(to);
200         to = NULL;
201
202         e = unit_name_escape(name);
203         if (asprintf(&to, "%s/dev-mapper-%s.device.requires/%s", arg_dest, e, n) < 0) {
204                 r = -ENOMEM;
205                 goto fail;
206         }
207
208         mkdir_parents(to, 0755);
209
210         if (symlink(from, to) < 0) {
211                 log_error("Failed to create symlink '%s' to '%s': %m", from, to);
212                 r = -errno;
213                 goto fail;
214         }
215
216         r = 0;
217
218 fail:
219         free(p);
220         free(n);
221         free(d);
222         free(e);
223
224         free(from);
225         free(to);
226
227         if (f)
228                 fclose(f);
229
230         return r;
231 }
232
233 int main(int argc, char *argv[]) {
234         FILE *f;
235         int r = EXIT_SUCCESS;
236         unsigned n = 0;
237
238         if (argc > 2) {
239                 log_error("This program takes one or no arguments.");
240                 return EXIT_FAILURE;
241         }
242
243         if (argc > 1)
244                 arg_dest = argv[1];
245
246         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
247         log_parse_environment();
248         log_open();
249
250         umask(0022);
251
252         if (!(f = fopen("/etc/crypttab", "re"))) {
253
254                 if (errno == ENOENT)
255                         r = EXIT_SUCCESS;
256                 else {
257                         r = EXIT_FAILURE;
258                         log_error("Failed to open /etc/crypttab: %m");
259                 }
260
261                 goto finish;
262         }
263
264         for (;;) {
265                 char line[LINE_MAX], *l;
266                 char *name = NULL, *device = NULL, *password = NULL, *options = NULL;
267                 int k;
268
269                 if (!(fgets(line, sizeof(line), f)))
270                         break;
271
272                 n++;
273
274                 l = strstrip(line);
275                 if (*l == '#' || *l == 0)
276                         continue;
277
278                 if ((k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &password, &options)) < 2 || k > 4) {
279                         log_error("Failed to parse /etc/crypttab:%u, ignoring.", n);
280                         r = EXIT_FAILURE;
281                         goto next;
282                 }
283
284                 if (create_disk(name, device, password, options) < 0)
285                         r = EXIT_FAILURE;
286
287         next:
288                 free(name);
289                 free(device);
290                 free(password);
291                 free(options);
292         }
293
294 finish:
295         return r;
296 }