]>
Commit | Line | Data |
---|---|---|
e23a0ce8 LP |
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 | ||
f653f683 LP |
36 | assert(needle); |
37 | ||
38 | if (!haystack) | |
39 | return false; | |
40 | ||
e23a0ce8 LP |
41 | l = strlen(needle); |
42 | ||
43 | while ((f = strstr(f, needle))) { | |
44 | ||
45 | if (f > haystack && f[-1] != ',') { | |
46 | f++; | |
47 | continue; | |
48 | } | |
49 | ||
aae5220d | 50 | if (f[l] != 0 && f[l] != ',') { |
e23a0ce8 LP |
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 | ||
74715b82 | 67 | char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *from = NULL, *to = NULL, *e = NULL; |
e23a0ce8 LP |
68 | int r; |
69 | FILE *f = NULL; | |
155da457 | 70 | bool noauto, nofail; |
e23a0ce8 LP |
71 | |
72 | assert(name); | |
73 | assert(device); | |
74 | ||
155da457 LP |
75 | noauto = has_option(options, "noauto"); |
76 | nofail = has_option(options, "nofail"); | |
77 | ||
e23a0ce8 LP |
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" | |
a4477e68 | 110 | "Description=Cryptography Setup for %%I\n" |
155da457 | 111 | "Conflicts=umount.target\n" |
e23a0ce8 | 112 | "DefaultDependencies=no\n" |
49d50c55 | 113 | "BindTo=%s dev-mapper-%%i.device\n" |
e23a0ce8 | 114 | "After=systemd-readahead-collect.service systemd-readahead-replay.service %s\n" |
87e75fdd | 115 | "Before=umount.target\n", |
e23a0ce8 LP |
116 | d, d); |
117 | ||
155da457 LP |
118 | if (!nofail) |
119 | fprintf(f, | |
120 | "Before=cryptsetup.target\n"); | |
121 | ||
e23a0ce8 LP |
122 | if (password && (streq(password, "/dev/urandom") || |
123 | streq(password, "/dev/random") || | |
124 | streq(password, "/dev/hw_random"))) | |
125 | fprintf(f, | |
126 | "After=systemd-random-seed-load.service\n"); | |
87e75fdd TG |
127 | else |
128 | fprintf(f, | |
129 | "Before=local-fs.target\n"); | |
e23a0ce8 LP |
130 | |
131 | fprintf(f, | |
132 | "\n[Service]\n" | |
133 | "Type=oneshot\n" | |
134 | "RemainAfterExit=yes\n" | |
90724929 | 135 | "TimeoutSec=0\n" /* the binary handles timeouts anyway */ |
260ab287 | 136 | "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n" |
7f4e0805 | 137 | "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n", |
e23a0ce8 LP |
138 | name, u, strempty(password), strempty(options), |
139 | name); | |
140 | ||
f653f683 | 141 | if (has_option(options, "tmp")) |
e23a0ce8 | 142 | fprintf(f, |
50109038 | 143 | "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n", |
1d3399e6 | 144 | name); |
e23a0ce8 | 145 | |
f653f683 | 146 | if (has_option(options, "swap")) |
e23a0ce8 | 147 | fprintf(f, |
50109038 | 148 | "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n", |
1d3399e6 | 149 | name); |
e23a0ce8 LP |
150 | |
151 | fflush(f); | |
152 | ||
153 | if (ferror(f)) { | |
154 | r = -errno; | |
155 | log_error("Failed to write file: %m"); | |
156 | goto fail; | |
157 | } | |
158 | ||
74715b82 LP |
159 | if (asprintf(&from, "../%s", n) < 0) { |
160 | r = -ENOMEM; | |
161 | goto fail; | |
162 | } | |
163 | ||
155da457 | 164 | if (!noauto) { |
e23a0ce8 LP |
165 | |
166 | if (asprintf(&to, "%s/%s.wants/%s", arg_dest, d, n) < 0) { | |
167 | r = -ENOMEM; | |
168 | goto fail; | |
169 | } | |
170 | ||
e23a0ce8 LP |
171 | mkdir_parents(to, 0755); |
172 | ||
173 | if (symlink(from, to) < 0) { | |
174 | log_error("Failed to create symlink '%s' to '%s': %m", from, to); | |
175 | r = -errno; | |
176 | goto fail; | |
177 | } | |
2f8cd170 LP |
178 | |
179 | free(to); | |
180 | to = NULL; | |
181 | ||
155da457 LP |
182 | if (!nofail) |
183 | asprintf(&to, "%s/cryptsetup.target.requires/%s", arg_dest, n); | |
184 | else | |
185 | asprintf(&to, "%s/cryptsetup.target.wants/%s", arg_dest, n); | |
2f8cd170 | 186 | |
155da457 LP |
187 | if (!to) { |
188 | r = -ENOMEM; | |
189 | goto fail; | |
190 | } | |
2f8cd170 | 191 | |
155da457 | 192 | mkdir_parents(to, 0755); |
2f8cd170 | 193 | |
155da457 LP |
194 | if (symlink(from, to) < 0) { |
195 | log_error("Failed to create symlink '%s' to '%s': %m", from, to); | |
196 | r = -errno; | |
197 | goto fail; | |
2f8cd170 | 198 | } |
e23a0ce8 LP |
199 | } |
200 | ||
74715b82 LP |
201 | free(to); |
202 | to = NULL; | |
203 | ||
204 | e = unit_name_escape(name); | |
8c4dd542 | 205 | if (asprintf(&to, "%s/dev-mapper-%s.device.requires/%s", arg_dest, e, n) < 0) { |
74715b82 LP |
206 | r = -ENOMEM; |
207 | goto fail; | |
208 | } | |
209 | ||
210 | mkdir_parents(to, 0755); | |
211 | ||
212 | if (symlink(from, to) < 0) { | |
213 | log_error("Failed to create symlink '%s' to '%s': %m", from, to); | |
214 | r = -errno; | |
215 | goto fail; | |
216 | } | |
217 | ||
e23a0ce8 LP |
218 | r = 0; |
219 | ||
220 | fail: | |
221 | free(p); | |
222 | free(n); | |
223 | free(d); | |
74715b82 | 224 | free(e); |
e23a0ce8 LP |
225 | |
226 | free(from); | |
227 | free(to); | |
228 | ||
229 | if (f) | |
230 | fclose(f); | |
231 | ||
232 | return r; | |
233 | } | |
234 | ||
235 | int main(int argc, char *argv[]) { | |
236 | FILE *f; | |
237 | int r = EXIT_SUCCESS; | |
238 | unsigned n = 0; | |
239 | ||
240 | if (argc > 2) { | |
241 | log_error("This program takes one or no arguments."); | |
242 | return EXIT_FAILURE; | |
243 | } | |
244 | ||
2a796654 LP |
245 | if (argc > 1) |
246 | arg_dest = argv[1]; | |
e23a0ce8 | 247 | |
4cfa2c99 | 248 | log_set_target(LOG_TARGET_AUTO); |
e23a0ce8 LP |
249 | log_parse_environment(); |
250 | log_open(); | |
251 | ||
4c12626c LP |
252 | umask(0022); |
253 | ||
e23a0ce8 LP |
254 | if (!(f = fopen("/etc/crypttab", "re"))) { |
255 | ||
256 | if (errno == ENOENT) | |
257 | r = EXIT_SUCCESS; | |
258 | else { | |
259 | r = EXIT_FAILURE; | |
260 | log_error("Failed to open /etc/crypttab: %m"); | |
261 | } | |
262 | ||
263 | goto finish; | |
264 | } | |
265 | ||
266 | for (;;) { | |
267 | char line[LINE_MAX], *l; | |
268 | char *name = NULL, *device = NULL, *password = NULL, *options = NULL; | |
269 | int k; | |
270 | ||
271 | if (!(fgets(line, sizeof(line), f))) | |
272 | break; | |
273 | ||
274 | n++; | |
275 | ||
276 | l = strstrip(line); | |
277 | if (*l == '#' || *l == 0) | |
278 | continue; | |
279 | ||
280 | if ((k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &password, &options)) < 2 || k > 4) { | |
281 | log_error("Failed to parse /etc/crypttab:%u, ignoring.", n); | |
282 | r = EXIT_FAILURE; | |
283 | goto next; | |
284 | } | |
285 | ||
286 | if (create_disk(name, device, password, options) < 0) | |
287 | r = EXIT_FAILURE; | |
288 | ||
289 | next: | |
290 | free(name); | |
291 | free(device); | |
292 | free(password); | |
293 | free(options); | |
294 | } | |
295 | ||
296 | finish: | |
297 | return r; | |
298 | } |