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