chiark / gitweb /
7d5d3db49b9bb766f487c7ec6e87c3321325c655
[elogind.git] / src / cryptsetup.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
25 #include <libcryptsetup.h>
26
27 #include "log.h"
28 #include "util.h"
29 #include "ask-password-api.h"
30
31 static unsigned opt_tries = 0;
32 static char *opt_cipher = NULL;
33 static unsigned opt_size = 0;
34 static char *opt_hash = NULL;
35 static bool opt_readonly = false;
36 static bool opt_verify = false;
37 static usec_t arg_timeout = 0;
38
39 static int parse_one_option(const char *option) {
40         assert(option);
41
42         /* Handled outside of this tool */
43         if (streq(option, "swap") ||
44             streq(option, "tmp") ||
45             streq(option, "noauto"))
46                 return 0;
47
48         if (startswith(option, "cipher=")) {
49                 char *t;
50
51                 if (!(t = strdup(option+7)))
52                         return -ENOMEM;
53
54                 free(opt_cipher);
55                 opt_cipher = t;
56
57         } else if (startswith(option, "size=")) {
58
59                 if (safe_atou(option+5, &opt_size) < 0) {
60                         log_error("size= parse failure, ignoring.");
61                         return 0;
62                 }
63
64         } else if (startswith(option, "hash=")) {
65                 char *t;
66
67                 if (!(t = strdup(option+5)))
68                         return -ENOMEM;
69
70                 free(opt_hash);
71                 opt_hash = t;
72
73         } else if (startswith(option, "tries=")) {
74
75                 if (safe_atou(option+6, &opt_tries) < 0) {
76                         log_error("tries= parse failure, ignoring.");
77                         return 0;
78                 }
79
80         } else if (streq(option, "readonly"))
81                 opt_readonly = true;
82         else if (streq(option, "verify"))
83                 opt_verify = true;
84         else if (startswith(option, "timeout=")) {
85
86                 if (parse_usec(option+8, &arg_timeout) < 0) {
87                         log_error("timeout= parse failure, ignoring.");
88                         return 0;
89                 }
90
91         } else
92                 log_error("Encountered unknown /etc/crypttab option '%s', ignoring.", option);
93
94         return 0;
95 }
96
97 static int parse_options(const char *options) {
98         char *state;
99         char *w;
100         size_t l;
101
102         assert(options);
103
104         FOREACH_WORD_SEPARATOR(w, l, options, ",", state) {
105                 char *o;
106                 int r;
107
108                 if (!(o = strndup(w, l)))
109                         return -ENOMEM;
110
111                 r = parse_one_option(o);
112                 free(o);
113
114                 if (r < 0)
115                         return r;
116         }
117
118         return 0;
119 }
120
121 static void log_glue(int level, const char *msg, void *usrptr) {
122
123         log_full(level == CRYPT_LOG_ERROR   ? LOG_ERR :
124                  level == CRYPT_LOG_VERBOSE ? LOG_INFO :
125                  level == CRYPT_LOG_DEBUG   ? LOG_DEBUG :
126                                               LOG_NOTICE,
127                  "%s", msg);
128 }
129
130 static int password_glue(const char *msg, char *buf, size_t length, void *usrptr) {
131         usec_t until;
132         char *password = NULL;
133         int k;
134
135         until = now(CLOCK_MONOTONIC) + (arg_timeout > 0 ? arg_timeout : 60 * USEC_PER_SEC);
136
137         if ((k = ask_password_agent(msg, "drive-harddisk", until, &password)) < 0)
138                 return k;
139
140         strncpy(buf, password, length-1);
141         buf[length-1] = 0;
142
143         free(password);
144
145         return strlen(buf);
146 }
147
148 int main(int argc, char *argv[]) {
149         int r = EXIT_FAILURE;
150         struct crypt_device *cd = NULL;
151
152         if (argc < 3) {
153                 log_error("This program requires at least two arguments.");
154                 return EXIT_FAILURE;
155         }
156
157         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
158         log_parse_environment();
159         log_open();
160
161         if (streq(argv[1], "attach") ||
162             streq(argv[1], "format-and-attach")) {
163                 uint32_t flags = 0;
164                 int k;
165                 const char *key_file = NULL;
166
167                 if (argc < 4) {
168                         log_error("attach requires at least two arguments.");
169                         goto finish;
170                 }
171
172                 if (argc >= 5 && argv[4][0] && !streq(argv[4], "-")) {
173
174                         if (!path_is_absolute(argv[4]))
175                                 log_error("Password file path %s is not absolute. Ignoring.", argv[4]);
176                         else
177                                 key_file = argv[4];
178                 }
179
180                 if (argc >= 6 && argv[5][0] && !streq(argv[5], "-"))
181                         parse_options(argv[5]);
182
183                 if ((k = crypt_init(&cd, argv[3]))) {
184                         log_error("crypt_init() failed: %s", strerror(-k));
185                         goto finish;
186                 }
187
188                 crypt_set_log_callback(cd, log_glue, NULL);
189                 crypt_set_password_callback(cd, password_glue, NULL);
190
191                 if (streq(argv[1], "format-and-attach")) {
192
193                         /* Format with random key and attach */
194
195                         log_error("Formatting not yet supported.");
196                         goto finish;
197
198                 } else if ((k = crypt_load(cd, CRYPT_LUKS1, NULL))) {
199                         log_error("crypt_load() failed: %s", strerror(-k));
200                         goto finish;
201                 }
202
203                 if (opt_readonly)
204                         flags |= CRYPT_ACTIVATE_READONLY;
205
206                 if (key_file) {
207                         crypt_set_password_retry(cd, 1);
208                         k = crypt_activate_by_keyfile(cd, argv[2], CRYPT_ANY_SLOT, key_file, 0, flags);
209                 } else  {
210                         crypt_set_password_retry(cd, opt_tries > 0 ? opt_tries : 3);
211                         k = crypt_activate_by_passphrase(cd, argv[2], CRYPT_ANY_SLOT, NULL, 0, flags);
212                 }
213
214                 if (k < 0) {
215                         log_error("Failed to activate: %s", strerror(-k));
216                         goto finish;
217                 }
218
219         } else if (streq(argv[1], "detach")) {
220                 int k;
221
222                 if ((k = crypt_init_by_name(&cd, argv[2]))) {
223                         log_error("crypt_init() failed: %s", strerror(-k));
224                         goto finish;
225                 }
226
227                 crypt_set_log_callback(cd, log_glue, NULL);
228
229                 if ((k = crypt_deactivate(cd, argv[2])) < 0) {
230                         log_error("Failed to deactivate: %s", strerror(-k));
231                         goto finish;
232                 }
233
234         } else {
235                 log_error("Unknown verb %s.", argv[1]);
236                 goto finish;
237         }
238
239         r = EXIT_SUCCESS;
240
241 finish:
242
243         if (cd)
244                 crypt_free(cd);
245
246         return r;
247 }