1bf114f1dSAlex Hornung #include <string.h>
2bf114f1dSAlex Hornung #include <stdio.h>
3bf114f1dSAlex Hornung #include <stdlib.h>
4bf114f1dSAlex Hornung #include <stdint.h>
5bf114f1dSAlex Hornung #include <stdarg.h>
6bf114f1dSAlex Hornung #include <inttypes.h>
7bf114f1dSAlex Hornung #include <errno.h>
8bf114f1dSAlex Hornung #include <unistd.h>
9bf114f1dSAlex Hornung #include <fcntl.h>
10bf114f1dSAlex Hornung #include <assert.h>
1113af8cbeSAlex Hornung #include <getopt.h>
12bf114f1dSAlex Hornung 
13bf114f1dSAlex Hornung #include <libcryptsetup.h>
14bf114f1dSAlex Hornung 
1513af8cbeSAlex Hornung #include "config.h"
16bf114f1dSAlex Hornung 
17bf114f1dSAlex Hornung #include "cryptsetup.h"
18bf114f1dSAlex Hornung 
19bf114f1dSAlex Hornung static int opt_verbose = 0;
20bf114f1dSAlex Hornung static int opt_debug = 0;
21bf114f1dSAlex Hornung static char *opt_cipher = NULL;
22bf114f1dSAlex Hornung static char *opt_hash = NULL;
23bf114f1dSAlex Hornung static int opt_verify_passphrase = 0;
24bf114f1dSAlex Hornung static char *opt_key_file = NULL;
25bf114f1dSAlex Hornung static char *opt_master_key_file = NULL;
26bf114f1dSAlex Hornung static char *opt_header_backup_file = NULL;
27bf114f1dSAlex Hornung static unsigned int opt_key_size = 0;
28bf114f1dSAlex Hornung static int opt_key_slot = CRYPT_ANY_SLOT;
29bf114f1dSAlex Hornung static uint64_t opt_size = 0;
30bf114f1dSAlex Hornung static uint64_t opt_offset = 0;
31bf114f1dSAlex Hornung static uint64_t opt_skip = 0;
32bf114f1dSAlex Hornung static int opt_readonly = 0;
33bf114f1dSAlex Hornung static int opt_iteration_time = 1000;
34bf114f1dSAlex Hornung static int opt_batch_mode = 0;
35bf114f1dSAlex Hornung static int opt_version_mode = 0;
36bf114f1dSAlex Hornung static int opt_timeout = 0;
37bf114f1dSAlex Hornung static int opt_tries = 3;
38bf114f1dSAlex Hornung static int opt_align_payload = 0;
39bf114f1dSAlex Hornung static int opt_non_exclusive = 0;
40bf114f1dSAlex Hornung 
41bf114f1dSAlex Hornung static const char **action_argv;
42bf114f1dSAlex Hornung static int action_argc;
43bf114f1dSAlex Hornung 
44bf114f1dSAlex Hornung static int action_create(int arg);
45bf114f1dSAlex Hornung static int action_remove(int arg);
46bf114f1dSAlex Hornung static int action_resize(int arg);
47bf114f1dSAlex Hornung static int action_status(int arg);
48bf114f1dSAlex Hornung static int action_luksFormat(int arg);
49bf114f1dSAlex Hornung static int action_luksOpen(int arg);
50bf114f1dSAlex Hornung static int action_luksAddKey(int arg);
51bf114f1dSAlex Hornung static int action_luksDelKey(int arg);
52bf114f1dSAlex Hornung static int action_luksKillSlot(int arg);
53bf114f1dSAlex Hornung static int action_luksRemoveKey(int arg);
54bf114f1dSAlex Hornung static int action_isLuks(int arg);
55bf114f1dSAlex Hornung static int action_luksUUID(int arg);
56bf114f1dSAlex Hornung static int action_luksDump(int arg);
57bf114f1dSAlex Hornung static int action_luksSuspend(int arg);
58bf114f1dSAlex Hornung static int action_luksResume(int arg);
59bf114f1dSAlex Hornung static int action_luksBackup(int arg);
60bf114f1dSAlex Hornung static int action_luksRestore(int arg);
61bf114f1dSAlex Hornung 
62bf114f1dSAlex Hornung static struct action_type {
63bf114f1dSAlex Hornung 	const char *type;
64bf114f1dSAlex Hornung 	int (*handler)(int);
65bf114f1dSAlex Hornung 	int arg;
66bf114f1dSAlex Hornung 	int required_action_argc;
67bf114f1dSAlex Hornung 	int required_memlock;
68bf114f1dSAlex Hornung 	const char *arg_desc;
69bf114f1dSAlex Hornung 	const char *desc;
70bf114f1dSAlex Hornung } action_types[] = {
71bf114f1dSAlex Hornung 	{ "create",	action_create,		0, 2, 1, N_("<name> <device>"),N_("create device") },
72bf114f1dSAlex Hornung 	{ "remove",	action_remove,		0, 1, 1, N_("<name>"), N_("remove device") },
73bf114f1dSAlex Hornung 	{ "resize",	action_resize,		0, 1, 1, N_("<name>"), N_("resize active device") },
74bf114f1dSAlex Hornung 	{ "status",	action_status,		0, 1, 0, N_("<name>"), N_("show device status") },
75bf114f1dSAlex Hornung 	{ "luksFormat", action_luksFormat,	0, 1, 1, N_("<device> [<new key file>]"), N_("formats a LUKS device") },
76bf114f1dSAlex Hornung 	{ "luksOpen",	action_luksOpen,	0, 2, 1, N_("<device> <name> "), N_("open LUKS device as mapping <name>") },
77bf114f1dSAlex Hornung 	{ "luksAddKey",	action_luksAddKey,	0, 1, 1, N_("<device> [<new key file>]"), N_("add key to LUKS device") },
78bf114f1dSAlex Hornung 	{ "luksRemoveKey",action_luksRemoveKey,	0, 1, 1, N_("<device> [<key file>]"), N_("removes supplied key or key file from LUKS device") },
79bf114f1dSAlex Hornung 	{ "luksKillSlot",  action_luksKillSlot, 0, 2, 1, N_("<device> <key slot>"), N_("wipes key with number <key slot> from LUKS device") },
80bf114f1dSAlex Hornung 	{ "luksUUID",	action_luksUUID,	0, 1, 0, N_("<device>"), N_("print UUID of LUKS device") },
81bf114f1dSAlex Hornung 	{ "isLuks",	action_isLuks,		0, 1, 0, N_("<device>"), N_("tests <device> for LUKS partition header") },
82bf114f1dSAlex Hornung 	{ "luksClose",	action_remove,		0, 1, 1, N_("<name>"), N_("remove LUKS mapping") },
83bf114f1dSAlex Hornung 	{ "luksDump",	action_luksDump,	0, 1, 0, N_("<device>"), N_("dump LUKS partition information") },
84bf114f1dSAlex Hornung 	{ "luksSuspend",action_luksSuspend,	0, 1, 1, N_("<device>"), N_("Suspend LUKS device and wipe key (all IOs are frozen).") },
85bf114f1dSAlex Hornung 	{ "luksResume",	action_luksResume,	0, 1, 1, N_("<device>"), N_("Resume suspended LUKS device.") },
86bf114f1dSAlex Hornung 	{ "luksHeaderBackup",action_luksBackup,	0, 1, 1, N_("<device>"), N_("Backup LUKS device header and keyslots") },
87bf114f1dSAlex Hornung 	{ "luksHeaderRestore",action_luksRestore,0,1, 1, N_("<device>"), N_("Restore LUKS device header and keyslots") },
88bf114f1dSAlex Hornung 	{ "luksDelKey", action_luksDelKey,	0, 2, 1, N_("<device> <key slot>"), N_("identical to luksKillSlot - DEPRECATED - see man page") },
89bf114f1dSAlex Hornung 	{ "reload",	action_create,		1, 2, 1, N_("<name> <device>"), N_("modify active device - DEPRECATED - see man page") },
90bf114f1dSAlex Hornung 	{ NULL, NULL, 0, 0, 0, NULL, NULL }
91bf114f1dSAlex Hornung };
92bf114f1dSAlex Hornung 
clogger(struct crypt_device * cd,int level,const char * file,int line,const char * format,...)93bf114f1dSAlex Hornung static void clogger(struct crypt_device *cd, int level, const char *file,
94bf114f1dSAlex Hornung 		   int line, const char *format, ...)
95bf114f1dSAlex Hornung {
96bf114f1dSAlex Hornung 	va_list argp;
97bf114f1dSAlex Hornung 	char *target = NULL;
98bf114f1dSAlex Hornung 
99bf114f1dSAlex Hornung 	va_start(argp, format);
100bf114f1dSAlex Hornung 
101bf114f1dSAlex Hornung 	if (vasprintf(&target, format, argp) > 0) {
102bf114f1dSAlex Hornung 		if (level >= 0) {
103bf114f1dSAlex Hornung 			crypt_log(cd, level, target);
104bf114f1dSAlex Hornung #ifdef CRYPT_DEBUG
105bf114f1dSAlex Hornung 		} else if (opt_debug)
106bf114f1dSAlex Hornung 			printf("# %s:%d %s\n", file ?: "?", line, target);
107bf114f1dSAlex Hornung #else
108bf114f1dSAlex Hornung 		} else if (opt_debug)
109bf114f1dSAlex Hornung 			printf("# %s\n", target);
110bf114f1dSAlex Hornung #endif
111bf114f1dSAlex Hornung 	}
112bf114f1dSAlex Hornung 
113bf114f1dSAlex Hornung 	va_end(argp);
114bf114f1dSAlex Hornung 	free(target);
115bf114f1dSAlex Hornung }
116bf114f1dSAlex Hornung 
117bf114f1dSAlex Hornung /* Interface Callbacks */
yesDialog(char * msg)118bf114f1dSAlex Hornung static int yesDialog(char *msg)
119bf114f1dSAlex Hornung {
120bf114f1dSAlex Hornung 	char *answer = NULL;
121bf114f1dSAlex Hornung 	size_t size = 0;
122bf114f1dSAlex Hornung 	int r = 1;
123bf114f1dSAlex Hornung 
124bf114f1dSAlex Hornung 	if(isatty(0) && !opt_batch_mode) {
125bf114f1dSAlex Hornung 		log_std("\nWARNING!\n========\n");
126bf114f1dSAlex Hornung 		log_std("%s\n\nAre you sure? (Type uppercase yes): ", msg);
127bf114f1dSAlex Hornung 		if(getline(&answer, &size, stdin) == -1) {
128bf114f1dSAlex Hornung 			perror("getline");
129bf114f1dSAlex Hornung 			free(answer);
130bf114f1dSAlex Hornung 			return 0;
131bf114f1dSAlex Hornung 		}
132bf114f1dSAlex Hornung 		if(strcmp(answer, "YES\n"))
133bf114f1dSAlex Hornung 			r = 0;
134bf114f1dSAlex Hornung 		free(answer);
135bf114f1dSAlex Hornung 	}
136bf114f1dSAlex Hornung 
137bf114f1dSAlex Hornung 	return r;
138bf114f1dSAlex Hornung }
139bf114f1dSAlex Hornung 
cmdLineLog(int level,char * msg)140bf114f1dSAlex Hornung static void cmdLineLog(int level, char *msg) {
141bf114f1dSAlex Hornung 	switch(level) {
142bf114f1dSAlex Hornung 
143bf114f1dSAlex Hornung 	case CRYPT_LOG_NORMAL:
144bf114f1dSAlex Hornung 		fputs(msg, stdout);
145bf114f1dSAlex Hornung 		break;
146bf114f1dSAlex Hornung 	case CRYPT_LOG_VERBOSE:
147bf114f1dSAlex Hornung 		if (opt_verbose)
148bf114f1dSAlex Hornung 			fputs(msg, stdout);
149bf114f1dSAlex Hornung 		break;
150bf114f1dSAlex Hornung 	case CRYPT_LOG_ERROR:
151bf114f1dSAlex Hornung 		fputs(msg, stderr);
152bf114f1dSAlex Hornung 		break;
153bf114f1dSAlex Hornung 	default:
154bf114f1dSAlex Hornung 		fprintf(stderr, "Internal error on logging class for msg: %s", msg);
155bf114f1dSAlex Hornung 		break;
156bf114f1dSAlex Hornung 	}
157bf114f1dSAlex Hornung }
158bf114f1dSAlex Hornung 
159bf114f1dSAlex Hornung static struct interface_callbacks cmd_icb = {
160bf114f1dSAlex Hornung 	.yesDialog = yesDialog,
161bf114f1dSAlex Hornung 	.log = cmdLineLog,
162bf114f1dSAlex Hornung };
163bf114f1dSAlex Hornung 
_log(int level,const char * msg,void * usrptr)164bf114f1dSAlex Hornung static void _log(int level, const char *msg, void *usrptr)
165bf114f1dSAlex Hornung {
166bf114f1dSAlex Hornung 	cmdLineLog(level, (char *)msg);
167bf114f1dSAlex Hornung }
168bf114f1dSAlex Hornung 
_yesDialog(const char * msg,void * usrptr)169bf114f1dSAlex Hornung static int _yesDialog(const char *msg, void *usrptr)
170bf114f1dSAlex Hornung {
171bf114f1dSAlex Hornung 	return yesDialog((char*)msg);
172bf114f1dSAlex Hornung }
173bf114f1dSAlex Hornung 
174bf114f1dSAlex Hornung /* End ICBs */
175bf114f1dSAlex Hornung 
show_status(int errcode)176bf114f1dSAlex Hornung static void show_status(int errcode)
177bf114f1dSAlex Hornung {
17813af8cbeSAlex Hornung 	char error[256];
17913af8cbeSAlex Hornung 	int ret;
180bf114f1dSAlex Hornung 
181bf114f1dSAlex Hornung 	if(!opt_verbose)
182bf114f1dSAlex Hornung 		return;
183bf114f1dSAlex Hornung 
184bf114f1dSAlex Hornung 	if(!errcode) {
185bf114f1dSAlex Hornung 		log_std(_("Command successful.\n"));
186bf114f1dSAlex Hornung 		return;
187bf114f1dSAlex Hornung 	}
188bf114f1dSAlex Hornung 
189bf114f1dSAlex Hornung 	crypt_get_error(error, sizeof(error));
190bf114f1dSAlex Hornung 
191bf114f1dSAlex Hornung 	if (!error[0]) {
19213af8cbeSAlex Hornung 		ret = strerror_r(-errcode, error, sizeof(error));
193bf114f1dSAlex Hornung 	}
194bf114f1dSAlex Hornung 
195bf114f1dSAlex Hornung 	log_err(_("Command failed with code %i"), -errcode);
196bf114f1dSAlex Hornung 	if (*error)
197bf114f1dSAlex Hornung 		log_err(": %s\n", error);
198bf114f1dSAlex Hornung 	else
199bf114f1dSAlex Hornung 		log_err(".\n");
200bf114f1dSAlex Hornung }
201bf114f1dSAlex Hornung 
action_create(int reload)202bf114f1dSAlex Hornung static int action_create(int reload)
203bf114f1dSAlex Hornung {
204bf114f1dSAlex Hornung 	struct crypt_options options = {
205bf114f1dSAlex Hornung 		.name = action_argv[0],
206bf114f1dSAlex Hornung 		.device = action_argv[1],
207bf114f1dSAlex Hornung 		.cipher = opt_cipher ? opt_cipher : DEFAULT_CIPHER(PLAIN),
208bf114f1dSAlex Hornung 		.hash = opt_hash ?: DEFAULT_PLAIN_HASH,
209bf114f1dSAlex Hornung 		.key_file = opt_key_file,
210bf114f1dSAlex Hornung 		.key_size = (opt_key_size ?: DEFAULT_PLAIN_KEYBITS) / 8,
211bf114f1dSAlex Hornung 		.key_slot = opt_key_slot,
212bf114f1dSAlex Hornung 		.flags = 0,
213bf114f1dSAlex Hornung 		.size = opt_size,
214bf114f1dSAlex Hornung 		.offset = opt_offset,
215bf114f1dSAlex Hornung 		.skip = opt_skip,
216bf114f1dSAlex Hornung 		.timeout = opt_timeout,
217bf114f1dSAlex Hornung 		.tries = opt_tries,
218bf114f1dSAlex Hornung 		.icb = &cmd_icb,
219bf114f1dSAlex Hornung 	};
220bf114f1dSAlex Hornung 	int r;
221bf114f1dSAlex Hornung 
222bf114f1dSAlex Hornung         if(reload)
223bf114f1dSAlex Hornung                 log_err(_("The reload action is deprecated. Please use \"dmsetup reload\" in case you really need this functionality.\nWARNING: do not use reload to touch LUKS devices. If that is the case, hit Ctrl-C now.\n"));
224bf114f1dSAlex Hornung 
225bf114f1dSAlex Hornung 	if (options.hash && strcmp(options.hash, "plain") == 0)
226bf114f1dSAlex Hornung 		options.hash = NULL;
227bf114f1dSAlex Hornung 	if (opt_verify_passphrase)
228bf114f1dSAlex Hornung 		options.flags |= CRYPT_FLAG_VERIFY;
229bf114f1dSAlex Hornung 	if (opt_readonly)
230bf114f1dSAlex Hornung 		options.flags |= CRYPT_FLAG_READONLY;
231bf114f1dSAlex Hornung 
232bf114f1dSAlex Hornung 	if (reload)
233bf114f1dSAlex Hornung 		r = crypt_update_device(&options);
234bf114f1dSAlex Hornung 	else
235bf114f1dSAlex Hornung 		r = crypt_create_device(&options);
236bf114f1dSAlex Hornung 
237bf114f1dSAlex Hornung 	return r;
238bf114f1dSAlex Hornung }
239bf114f1dSAlex Hornung 
action_remove(int arg)240bf114f1dSAlex Hornung static int action_remove(int arg)
241bf114f1dSAlex Hornung {
242bf114f1dSAlex Hornung 	struct crypt_options options = {
243bf114f1dSAlex Hornung 		.name = action_argv[0],
244bf114f1dSAlex Hornung 		.icb = &cmd_icb,
245bf114f1dSAlex Hornung 	};
246bf114f1dSAlex Hornung 
247bf114f1dSAlex Hornung 	return crypt_remove_device(&options);
248bf114f1dSAlex Hornung }
249bf114f1dSAlex Hornung 
action_resize(int arg)250bf114f1dSAlex Hornung static int action_resize(int arg)
251bf114f1dSAlex Hornung {
252bf114f1dSAlex Hornung 	struct crypt_options options = {
253bf114f1dSAlex Hornung 		.name = action_argv[0],
254bf114f1dSAlex Hornung 		.size = opt_size,
255bf114f1dSAlex Hornung 		.icb = &cmd_icb,
256bf114f1dSAlex Hornung 	};
257bf114f1dSAlex Hornung 
258bf114f1dSAlex Hornung 	return crypt_resize_device(&options);
259bf114f1dSAlex Hornung }
260bf114f1dSAlex Hornung 
action_status(int arg)261bf114f1dSAlex Hornung static int action_status(int arg)
262bf114f1dSAlex Hornung {
263bf114f1dSAlex Hornung 	struct crypt_options options = {
264bf114f1dSAlex Hornung 		.name = action_argv[0],
265bf114f1dSAlex Hornung 		.icb = &cmd_icb,
266bf114f1dSAlex Hornung 	};
267bf114f1dSAlex Hornung 	int r;
268bf114f1dSAlex Hornung 
269bf114f1dSAlex Hornung 	r = crypt_query_device(&options);
270bf114f1dSAlex Hornung 	if (r < 0)
271bf114f1dSAlex Hornung 		return r;
272bf114f1dSAlex Hornung 
273bf114f1dSAlex Hornung 	if (r == 0) {
274bf114f1dSAlex Hornung 		/* inactive */
275bf114f1dSAlex Hornung 		log_std("%s/%s is inactive.\n", crypt_get_dir(), options.name);
276bf114f1dSAlex Hornung 		r = 1;
277bf114f1dSAlex Hornung 	} else {
278bf114f1dSAlex Hornung 		/* active */
279bf114f1dSAlex Hornung 		log_std("%s/%s is active:\n", crypt_get_dir(), options.name);
280bf114f1dSAlex Hornung 		log_std("  cipher:  %s\n", options.cipher);
281bf114f1dSAlex Hornung 		log_std("  keysize: %d bits\n", options.key_size * 8);
282bf114f1dSAlex Hornung 		log_std("  device:  %s\n", options.device ?: "");
283bf114f1dSAlex Hornung 		log_std("  offset:  %" PRIu64 " sectors\n", options.offset);
284bf114f1dSAlex Hornung 		log_std("  size:    %" PRIu64 " sectors\n", options.size);
285bf114f1dSAlex Hornung 		if (options.skip)
286bf114f1dSAlex Hornung 			log_std("  skipped: %" PRIu64 " sectors\n", options.skip);
287bf114f1dSAlex Hornung 		log_std("  mode:    %s\n", (options.flags & CRYPT_FLAG_READONLY)
288bf114f1dSAlex Hornung 		                           ? "readonly" : "read/write");
289bf114f1dSAlex Hornung 		crypt_put_options(&options);
290bf114f1dSAlex Hornung 		r = 0;
291bf114f1dSAlex Hornung 	}
292bf114f1dSAlex Hornung 	return r;
293bf114f1dSAlex Hornung }
294bf114f1dSAlex Hornung 
_action_luksFormat_generateMK()295bf114f1dSAlex Hornung static int _action_luksFormat_generateMK()
296bf114f1dSAlex Hornung {
297bf114f1dSAlex Hornung 	struct crypt_options options = {
298bf114f1dSAlex Hornung 		.key_size = (opt_key_size ?: DEFAULT_LUKS1_KEYBITS) / 8,
299bf114f1dSAlex Hornung 		.key_slot = opt_key_slot,
300bf114f1dSAlex Hornung 		.device = action_argv[0],
301bf114f1dSAlex Hornung 		.cipher = opt_cipher ?: DEFAULT_CIPHER(LUKS1),
302bf114f1dSAlex Hornung 		.hash = opt_hash ?: DEFAULT_LUKS1_HASH,
303bf114f1dSAlex Hornung 		.new_key_file = opt_key_file ?: (action_argc > 1 ? action_argv[1] : NULL),
304bf114f1dSAlex Hornung 		.flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE :  0),
305bf114f1dSAlex Hornung 		.iteration_time = opt_iteration_time,
306bf114f1dSAlex Hornung 		.timeout = opt_timeout,
307bf114f1dSAlex Hornung 		.align_payload = opt_align_payload,
308bf114f1dSAlex Hornung 		.icb = &cmd_icb,
309bf114f1dSAlex Hornung 	};
310bf114f1dSAlex Hornung 
311bf114f1dSAlex Hornung 	return crypt_luksFormat(&options);
312bf114f1dSAlex Hornung }
313bf114f1dSAlex Hornung 
_read_mk(const char * file,char ** key,int keysize)314bf114f1dSAlex Hornung static int _read_mk(const char *file, char **key, int keysize)
315bf114f1dSAlex Hornung {
316bf114f1dSAlex Hornung 	int fd;
317bf114f1dSAlex Hornung 
318bf114f1dSAlex Hornung 	*key = malloc(keysize);
319bf114f1dSAlex Hornung 	if (!*key)
320bf114f1dSAlex Hornung 		return -ENOMEM;
321bf114f1dSAlex Hornung 
322bf114f1dSAlex Hornung 	fd = open(file, O_RDONLY);
323bf114f1dSAlex Hornung 	if (fd == -1) {
324bf114f1dSAlex Hornung 		log_err("Cannot read keyfile %s.\n", file);
325bf114f1dSAlex Hornung 		return -EINVAL;
326bf114f1dSAlex Hornung 	}
327bf114f1dSAlex Hornung 	if ((read(fd, *key, keysize) != keysize)) {
328bf114f1dSAlex Hornung 		log_err("Cannot read %d bytes from keyfile %s.\n", keysize, file);
329bf114f1dSAlex Hornung 		close(fd);
330bf114f1dSAlex Hornung 		memset(*key, 0, keysize);
331bf114f1dSAlex Hornung 		free(*key);
332bf114f1dSAlex Hornung 		return -EINVAL;
333bf114f1dSAlex Hornung 	}
334bf114f1dSAlex Hornung 	close(fd);
335bf114f1dSAlex Hornung 	return 0;
336bf114f1dSAlex Hornung }
337bf114f1dSAlex Hornung 
_action_luksFormat_useMK()338bf114f1dSAlex Hornung static int _action_luksFormat_useMK()
339bf114f1dSAlex Hornung {
340bf114f1dSAlex Hornung 	int r = -EINVAL, keysize;
341bf114f1dSAlex Hornung 	char *key = NULL, cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
342bf114f1dSAlex Hornung 	struct crypt_device *cd = NULL;
343bf114f1dSAlex Hornung 	struct crypt_params_luks1 params = {
344bf114f1dSAlex Hornung 		.hash = opt_hash ?: DEFAULT_LUKS1_HASH,
345bf114f1dSAlex Hornung 		.data_alignment = opt_align_payload,
346bf114f1dSAlex Hornung 	};
347bf114f1dSAlex Hornung 
348bf114f1dSAlex Hornung 	if (sscanf(opt_cipher ?: DEFAULT_CIPHER(LUKS1),
349bf114f1dSAlex Hornung 		   "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s",
350bf114f1dSAlex Hornung 		   cipher, cipher_mode) != 2) {
351bf114f1dSAlex Hornung 		log_err("No known cipher specification pattern detected.\n");
352bf114f1dSAlex Hornung 		return -EINVAL;
353bf114f1dSAlex Hornung 	}
354bf114f1dSAlex Hornung 
355bf114f1dSAlex Hornung 	keysize = (opt_key_size ?: DEFAULT_LUKS1_KEYBITS) / 8;
356bf114f1dSAlex Hornung 	if (_read_mk(opt_master_key_file, &key, keysize) < 0)
357bf114f1dSAlex Hornung 		return -EINVAL;
358bf114f1dSAlex Hornung 
359bf114f1dSAlex Hornung 	if ((r = crypt_init(&cd, action_argv[0])))
360bf114f1dSAlex Hornung 		goto out;
361bf114f1dSAlex Hornung 
362bf114f1dSAlex Hornung 	crypt_set_password_verify(cd, 1);
363bf114f1dSAlex Hornung 	crypt_set_timeout(cd, opt_timeout);
364bf114f1dSAlex Hornung 	if (opt_iteration_time)
365bf114f1dSAlex Hornung 		crypt_set_iterarion_time(cd, opt_iteration_time);
366bf114f1dSAlex Hornung 
367bf114f1dSAlex Hornung 	if ((r = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, keysize, &params)))
368bf114f1dSAlex Hornung 		goto out;
369bf114f1dSAlex Hornung 
370bf114f1dSAlex Hornung 	r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot, key, keysize, NULL, 0);
371bf114f1dSAlex Hornung out:
372bf114f1dSAlex Hornung 
373bf114f1dSAlex Hornung 	crypt_free(cd);
374bf114f1dSAlex Hornung 	if (key) {
375bf114f1dSAlex Hornung 		memset(key, 0, keysize);
376bf114f1dSAlex Hornung 		free(key);
377bf114f1dSAlex Hornung 	}
378bf114f1dSAlex Hornung 	return r;
379bf114f1dSAlex Hornung }
380bf114f1dSAlex Hornung 
action_luksFormat(int arg)381bf114f1dSAlex Hornung static int action_luksFormat(int arg)
382bf114f1dSAlex Hornung {
383bf114f1dSAlex Hornung 	int r = 0; char *msg = NULL;
384bf114f1dSAlex Hornung 
385bf114f1dSAlex Hornung 	/* Avoid overwriting possibly wrong part of device than user requested by rejecting these options */
386bf114f1dSAlex Hornung 	if (opt_offset || opt_skip) {
387bf114f1dSAlex Hornung 		log_err("Options --offset and --skip are not supported for luksFormat.\n");
388bf114f1dSAlex Hornung 		return -EINVAL;
389bf114f1dSAlex Hornung 	}
390bf114f1dSAlex Hornung 
391bf114f1dSAlex Hornung 	if (action_argc > 1 && opt_key_file)
392bf114f1dSAlex Hornung 		log_err(_("Option --key-file takes precedence over specified key file argument.\n"));
393bf114f1dSAlex Hornung 
394bf114f1dSAlex Hornung 	if(asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]) == -1) {
395bf114f1dSAlex Hornung 		log_err(_("memory allocation error in action_luksFormat"));
396bf114f1dSAlex Hornung 		return -ENOMEM;
397bf114f1dSAlex Hornung 	}
398bf114f1dSAlex Hornung 	r = yesDialog(msg);
399bf114f1dSAlex Hornung 	free(msg);
400bf114f1dSAlex Hornung 
401bf114f1dSAlex Hornung 	if (!r)
402bf114f1dSAlex Hornung 		return -EINVAL;
403bf114f1dSAlex Hornung 
404bf114f1dSAlex Hornung 	if (opt_master_key_file)
405bf114f1dSAlex Hornung 		return _action_luksFormat_useMK();
406bf114f1dSAlex Hornung 	else
407bf114f1dSAlex Hornung 		return _action_luksFormat_generateMK();
408bf114f1dSAlex Hornung }
409bf114f1dSAlex Hornung 
action_luksOpen(int arg)410bf114f1dSAlex Hornung static int action_luksOpen(int arg)
411bf114f1dSAlex Hornung {
412bf114f1dSAlex Hornung 	struct crypt_options options = {
413bf114f1dSAlex Hornung 		.name = action_argv[1],
414bf114f1dSAlex Hornung 		.device = action_argv[0],
415bf114f1dSAlex Hornung 		.key_file = opt_key_file,
416bf114f1dSAlex Hornung 		.key_size = opt_key_file ? (opt_key_size / 8) : 0, /* limit bytes read from keyfile */
417bf114f1dSAlex Hornung 		.timeout = opt_timeout,
418bf114f1dSAlex Hornung 		.tries = opt_key_file ? 1 : opt_tries, /* verify is usefull only for tty */
419bf114f1dSAlex Hornung 		.icb = &cmd_icb,
420bf114f1dSAlex Hornung 	};
421bf114f1dSAlex Hornung 
422bf114f1dSAlex Hornung 	if (opt_readonly)
423bf114f1dSAlex Hornung 		options.flags |= CRYPT_FLAG_READONLY;
424bf114f1dSAlex Hornung 	if (opt_non_exclusive)
425bf114f1dSAlex Hornung 		log_err(_("Obsolete option --non-exclusive is ignored.\n"));
426bf114f1dSAlex Hornung 
427bf114f1dSAlex Hornung 	return crypt_luksOpen(&options);
428bf114f1dSAlex Hornung }
429bf114f1dSAlex Hornung 
action_luksDelKey(int arg)430bf114f1dSAlex Hornung static int action_luksDelKey(int arg)
431bf114f1dSAlex Hornung {
432bf114f1dSAlex Hornung 	log_err("luksDelKey is a deprecated action name.\nPlease use luksKillSlot.\n");
433bf114f1dSAlex Hornung 	return action_luksKillSlot(arg);
434bf114f1dSAlex Hornung }
435bf114f1dSAlex Hornung 
action_luksKillSlot(int arg)436bf114f1dSAlex Hornung static int action_luksKillSlot(int arg)
437bf114f1dSAlex Hornung {
438bf114f1dSAlex Hornung 	struct crypt_options options = {
439bf114f1dSAlex Hornung 		.device = action_argv[0],
440bf114f1dSAlex Hornung 		.key_slot = atoi(action_argv[1]),
441bf114f1dSAlex Hornung 		.key_file = opt_key_file,
442bf114f1dSAlex Hornung 		.timeout = opt_timeout,
443bf114f1dSAlex Hornung 		.flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
444bf114f1dSAlex Hornung 		.icb = &cmd_icb,
445bf114f1dSAlex Hornung 	};
446bf114f1dSAlex Hornung 
447bf114f1dSAlex Hornung 	return crypt_luksKillSlot(&options);
448bf114f1dSAlex Hornung }
449bf114f1dSAlex Hornung 
action_luksRemoveKey(int arg)450bf114f1dSAlex Hornung static int action_luksRemoveKey(int arg)
451bf114f1dSAlex Hornung {
452bf114f1dSAlex Hornung 	struct crypt_options options = {
453bf114f1dSAlex Hornung 		.device = action_argv[0],
454bf114f1dSAlex Hornung 		.new_key_file = action_argc>1?action_argv[1]:NULL,
455bf114f1dSAlex Hornung 		.key_file = opt_key_file,
456bf114f1dSAlex Hornung 		.timeout = opt_timeout,
457bf114f1dSAlex Hornung 		.flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
458bf114f1dSAlex Hornung 		.icb = &cmd_icb,
459bf114f1dSAlex Hornung 	};
460bf114f1dSAlex Hornung 
461bf114f1dSAlex Hornung 	return crypt_luksRemoveKey(&options);
462bf114f1dSAlex Hornung }
463bf114f1dSAlex Hornung 
_action_luksAddKey_useMK()464bf114f1dSAlex Hornung static int _action_luksAddKey_useMK()
465bf114f1dSAlex Hornung {
466bf114f1dSAlex Hornung 	int r = -EINVAL, keysize = 0;
467bf114f1dSAlex Hornung 	char *key = NULL;
468bf114f1dSAlex Hornung 	struct crypt_device *cd = NULL;
469bf114f1dSAlex Hornung 
470bf114f1dSAlex Hornung 	if ((r = crypt_init(&cd, action_argv[0])))
471bf114f1dSAlex Hornung 		goto out;
472bf114f1dSAlex Hornung 
473bf114f1dSAlex Hornung 	if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
474bf114f1dSAlex Hornung 		goto out;
475bf114f1dSAlex Hornung 
476bf114f1dSAlex Hornung 	keysize = crypt_get_volume_key_size(cd);
477bf114f1dSAlex Hornung 	crypt_set_password_verify(cd, 1);
478bf114f1dSAlex Hornung 	crypt_set_timeout(cd, opt_timeout);
479bf114f1dSAlex Hornung 	if (opt_iteration_time)
480bf114f1dSAlex Hornung 		crypt_set_iterarion_time(cd, opt_iteration_time);
481bf114f1dSAlex Hornung 
482bf114f1dSAlex Hornung 	if (_read_mk(opt_master_key_file, &key, keysize) < 0)
483bf114f1dSAlex Hornung 		goto out;
484bf114f1dSAlex Hornung 
485bf114f1dSAlex Hornung 	r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot, key, keysize, NULL, 0);
486bf114f1dSAlex Hornung out:
487bf114f1dSAlex Hornung 	crypt_free(cd);
488bf114f1dSAlex Hornung 	if (key) {
489bf114f1dSAlex Hornung 		memset(key, 0, keysize);
490bf114f1dSAlex Hornung 		free(key);
491bf114f1dSAlex Hornung 	}
492bf114f1dSAlex Hornung 	return r;
493bf114f1dSAlex Hornung }
494bf114f1dSAlex Hornung 
action_luksAddKey(int arg)495bf114f1dSAlex Hornung static int action_luksAddKey(int arg)
496bf114f1dSAlex Hornung {
497bf114f1dSAlex Hornung 	struct crypt_options options = {
498bf114f1dSAlex Hornung 		.device = action_argv[0],
499bf114f1dSAlex Hornung 		.new_key_file = action_argc>1?action_argv[1]:NULL,
500bf114f1dSAlex Hornung 		.key_file = opt_key_file,
501bf114f1dSAlex Hornung 		.key_slot = opt_key_slot,
502bf114f1dSAlex Hornung 		.flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE : 0),
503bf114f1dSAlex Hornung 		.iteration_time = opt_iteration_time,
504bf114f1dSAlex Hornung 		.timeout = opt_timeout,
505bf114f1dSAlex Hornung 		.icb = &cmd_icb,
506bf114f1dSAlex Hornung 	};
507bf114f1dSAlex Hornung 
508bf114f1dSAlex Hornung 	if (opt_master_key_file)
509bf114f1dSAlex Hornung 		return _action_luksAddKey_useMK();
510bf114f1dSAlex Hornung 	else
511bf114f1dSAlex Hornung 		return crypt_luksAddKey(&options);
512bf114f1dSAlex Hornung }
513bf114f1dSAlex Hornung 
action_isLuks(int arg)514bf114f1dSAlex Hornung static int action_isLuks(int arg)
515bf114f1dSAlex Hornung {
516bf114f1dSAlex Hornung 	struct crypt_options options = {
517bf114f1dSAlex Hornung 		.device = action_argv[0],
518bf114f1dSAlex Hornung 		.icb = &cmd_icb,
519bf114f1dSAlex Hornung 	};
520bf114f1dSAlex Hornung 
521bf114f1dSAlex Hornung 	return crypt_isLuks(&options);
522bf114f1dSAlex Hornung }
523bf114f1dSAlex Hornung 
action_luksUUID(int arg)524bf114f1dSAlex Hornung static int action_luksUUID(int arg)
525bf114f1dSAlex Hornung {
526bf114f1dSAlex Hornung 	struct crypt_options options = {
527bf114f1dSAlex Hornung 		.device = action_argv[0],
528bf114f1dSAlex Hornung 		.icb = &cmd_icb,
529bf114f1dSAlex Hornung 	};
530bf114f1dSAlex Hornung 
531bf114f1dSAlex Hornung 	return crypt_luksUUID(&options);
532bf114f1dSAlex Hornung }
533bf114f1dSAlex Hornung 
action_luksDump(int arg)534bf114f1dSAlex Hornung static int action_luksDump(int arg)
535bf114f1dSAlex Hornung {
536bf114f1dSAlex Hornung 	struct crypt_options options = {
537bf114f1dSAlex Hornung 		.device = action_argv[0],
538bf114f1dSAlex Hornung 		.icb = &cmd_icb,
539bf114f1dSAlex Hornung 	};
540bf114f1dSAlex Hornung 
541bf114f1dSAlex Hornung 	return crypt_luksDump(&options);
542bf114f1dSAlex Hornung }
543bf114f1dSAlex Hornung 
action_luksSuspend(int arg)544bf114f1dSAlex Hornung static int action_luksSuspend(int arg)
545bf114f1dSAlex Hornung {
546bf114f1dSAlex Hornung 	struct crypt_device *cd = NULL;
547bf114f1dSAlex Hornung 	int r;
548bf114f1dSAlex Hornung 
549bf114f1dSAlex Hornung 	r = crypt_init_by_name(&cd, action_argv[0]);
550bf114f1dSAlex Hornung 	if (!r)
551bf114f1dSAlex Hornung 		r = crypt_suspend(cd, action_argv[0]);
552bf114f1dSAlex Hornung 
553bf114f1dSAlex Hornung 	crypt_free(cd);
554bf114f1dSAlex Hornung 	return r;
555bf114f1dSAlex Hornung }
556bf114f1dSAlex Hornung 
action_luksResume(int arg)557bf114f1dSAlex Hornung static int action_luksResume(int arg)
558bf114f1dSAlex Hornung {
559bf114f1dSAlex Hornung 	struct crypt_device *cd = NULL;
560bf114f1dSAlex Hornung 	int r;
561bf114f1dSAlex Hornung 
562bf114f1dSAlex Hornung 	if ((r = crypt_init_by_name(&cd, action_argv[0])))
563bf114f1dSAlex Hornung 		goto out;
564bf114f1dSAlex Hornung 
565bf114f1dSAlex Hornung 	if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
566bf114f1dSAlex Hornung 		goto out;
567bf114f1dSAlex Hornung 
568bf114f1dSAlex Hornung 	if (opt_key_file)
569bf114f1dSAlex Hornung 		r = crypt_resume_by_keyfile(cd, action_argv[0], CRYPT_ANY_SLOT,
570bf114f1dSAlex Hornung 					    opt_key_file, opt_key_size / 8);
571bf114f1dSAlex Hornung 	else
572bf114f1dSAlex Hornung 		r = crypt_resume_by_passphrase(cd, action_argv[0], CRYPT_ANY_SLOT,
573bf114f1dSAlex Hornung 					       NULL, 0);
574bf114f1dSAlex Hornung out:
575bf114f1dSAlex Hornung 	crypt_free(cd);
576bf114f1dSAlex Hornung 	return r;
577bf114f1dSAlex Hornung }
578bf114f1dSAlex Hornung 
action_luksBackup(int arg)579bf114f1dSAlex Hornung static int action_luksBackup(int arg)
580bf114f1dSAlex Hornung {
581bf114f1dSAlex Hornung 	struct crypt_device *cd = NULL;
582bf114f1dSAlex Hornung 	int r;
583bf114f1dSAlex Hornung 
584bf114f1dSAlex Hornung 	if (!opt_header_backup_file) {
585bf114f1dSAlex Hornung 		log_err(_("Option --header-backup-file is required.\n"));
586bf114f1dSAlex Hornung 		return -EINVAL;
587bf114f1dSAlex Hornung 	}
588bf114f1dSAlex Hornung 
589bf114f1dSAlex Hornung 	if ((r = crypt_init(&cd, action_argv[0])))
590bf114f1dSAlex Hornung 		goto out;
591bf114f1dSAlex Hornung 
592bf114f1dSAlex Hornung 	crypt_set_log_callback(cd, _log, NULL);
593bf114f1dSAlex Hornung 	crypt_set_confirm_callback(cd, _yesDialog, NULL);
594bf114f1dSAlex Hornung 
595bf114f1dSAlex Hornung 	r = crypt_header_backup(cd, CRYPT_LUKS1, opt_header_backup_file);
596bf114f1dSAlex Hornung out:
597bf114f1dSAlex Hornung 	crypt_free(cd);
598bf114f1dSAlex Hornung 	return r;
599bf114f1dSAlex Hornung }
600bf114f1dSAlex Hornung 
action_luksRestore(int arg)601bf114f1dSAlex Hornung static int action_luksRestore(int arg)
602bf114f1dSAlex Hornung {
603bf114f1dSAlex Hornung 	struct crypt_device *cd = NULL;
604bf114f1dSAlex Hornung 	int r = 0;
605bf114f1dSAlex Hornung 
606bf114f1dSAlex Hornung 	if (!opt_header_backup_file) {
607bf114f1dSAlex Hornung 		log_err(_("Option --header-backup-file is required.\n"));
608bf114f1dSAlex Hornung 		return -EINVAL;
609bf114f1dSAlex Hornung 	}
610bf114f1dSAlex Hornung 
611bf114f1dSAlex Hornung 	if ((r = crypt_init(&cd, action_argv[0])))
612bf114f1dSAlex Hornung 		goto out;
613bf114f1dSAlex Hornung 
614bf114f1dSAlex Hornung 	crypt_set_log_callback(cd, _log, NULL);
615bf114f1dSAlex Hornung 	crypt_set_confirm_callback(cd, _yesDialog, NULL);
616bf114f1dSAlex Hornung 	r = crypt_header_restore(cd, CRYPT_LUKS1, opt_header_backup_file);
617bf114f1dSAlex Hornung out:
618bf114f1dSAlex Hornung 	crypt_free(cd);
619bf114f1dSAlex Hornung 	return r;
620bf114f1dSAlex Hornung }
621bf114f1dSAlex Hornung 
usage(const char * msg)62213af8cbeSAlex Hornung static void usage(const char *msg)
623bf114f1dSAlex Hornung {
62413af8cbeSAlex Hornung 	log_err("Usage: cryptsetup [-?vyrq] [-?|--help] [--usage] [-v|--verbose]\n"
62513af8cbeSAlex Hornung 	    "        [--debug] [-c|--cipher=STRING] [-h|--hash=STRING]\n"
62613af8cbeSAlex Hornung 	    "        [-y|--verify-passphrase] [-d|--key-file=STRING]\n"
62713af8cbeSAlex Hornung 	    "        [--master-key-file=STRING] [-s|--key-size=BITS] [-S|--key-slot=INT]\n"
62813af8cbeSAlex Hornung             "        [-b|--size=SECTORS] [-o|--offset=SECTORS] [-p|--skip=SECTORS]\n"
62913af8cbeSAlex Hornung             "        [-r|--readonly] [-i|--iter-time=msecs] [-q|--batch-mode] [--version]\n"
63013af8cbeSAlex Hornung             "        [-t|--timeout=secs] [-T|--tries=INT] [--align-payload=SECTORS]\n"
63113af8cbeSAlex Hornung             "        [--non-exclusive] [--header-backup-file=STRING] [OPTION...]\n"
63213af8cbeSAlex Hornung             "        <action> <action-specific>]\n");
63313af8cbeSAlex Hornung 
63413af8cbeSAlex Hornung 	if (msg)
63513af8cbeSAlex Hornung 		log_err("%s\n", msg);
63613af8cbeSAlex Hornung 
63713af8cbeSAlex Hornung 	exit(1);
638bf114f1dSAlex Hornung }
639bf114f1dSAlex Hornung 
help()64013af8cbeSAlex Hornung static void help()
641bf114f1dSAlex Hornung {
642bf114f1dSAlex Hornung 	struct action_type *action;
643bf114f1dSAlex Hornung 
644bf114f1dSAlex Hornung 	log_std("%s\n",PACKAGE_STRING);
64513af8cbeSAlex Hornung 	log_std("Usage: cryptsetup [OPTION...] <action> <action-specific>]\n"
64613af8cbeSAlex Hornung 	    "  -v, --verbose                       Shows more detailed error messages\n"
64713af8cbeSAlex Hornung 	    "      --debug                         Show debug messages\n"
64813af8cbeSAlex Hornung 	    "  -c, --cipher=STRING                 The cipher used to encrypt the disk (see /proc/crypto)\n"
64913af8cbeSAlex Hornung 	    "  -h, --hash=STRING                   The hash used to create the encryption key from the passphrase\n"
65013af8cbeSAlex Hornung 	    "  -y, --verify-passphrase             Verifies the passphrase by asking for it twice\n"
65113af8cbeSAlex Hornung 	    "  -d, --key-file=STRING               Read the key from a file (can be /dev/random)\n"
65213af8cbeSAlex Hornung 	    "      --master-key-file=STRING        Read the volume (master) key from file.\n"
65313af8cbeSAlex Hornung 	    "  -s, --key-size=BITS                 The size of the encryption key\n"
65413af8cbeSAlex Hornung 	    "  -S, --key-slot=INT                  Slot number for new key (default is first free)\n"
65513af8cbeSAlex Hornung 	    "  -b, --size=SECTORS                  The size of the device\n"
65613af8cbeSAlex Hornung 	    "  -o, --offset=SECTORS                The start offset in the backend device\n"
65713af8cbeSAlex Hornung 	    "  -p, --skip=SECTORS                  How many sectors of the encrypted data to skip at the beginning\n"
65813af8cbeSAlex Hornung 	    "  -r, --readonly                      Create a readonly mapping\n"
65913af8cbeSAlex Hornung 	    "  -i, --iter-time=msecs               PBKDF2 iteration time for LUKS (in ms)\n"
66013af8cbeSAlex Hornung 	    "  -q, --batch-mode                    Do not ask for confirmation\n"
66113af8cbeSAlex Hornung 	    "      --version                       Print package version\n"
66213af8cbeSAlex Hornung 	    "  -t, --timeout=secs                  Timeout for interactive passphrase prompt (in seconds)\n"
66313af8cbeSAlex Hornung 	    "  -T, --tries=INT                     How often the input of the passphrase can be retried\n"
66413af8cbeSAlex Hornung 	    "      --align-payload=SECTORS         Align payload at <n> sector boundaries - for luksFormat\n"
66513af8cbeSAlex Hornung 	    "      --non-exclusive                 (Obsoleted, see man page.)\n"
66613af8cbeSAlex Hornung 	    "      --header-backup-file=STRING     File with LUKS header and keyslots backup.\n"
66713af8cbeSAlex Hornung 	    "\n"
66813af8cbeSAlex Hornung 	    "Help options:\n"
66913af8cbeSAlex Hornung 	    "  -?, --help                          Show this help message\n"
67013af8cbeSAlex Hornung 	    "      --usage                         Display brief usage\n" );
671bf114f1dSAlex Hornung 
672bf114f1dSAlex Hornung 	log_std(_("\n"
673bf114f1dSAlex Hornung 		 "<action> is one of:\n"));
674bf114f1dSAlex Hornung 
675bf114f1dSAlex Hornung 	for(action = action_types; action->type; action++)
676bf114f1dSAlex Hornung 		log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
677bf114f1dSAlex Hornung 
678bf114f1dSAlex Hornung 	log_std(_("\n"
679bf114f1dSAlex Hornung 		 "<name> is the device to create under %s\n"
680bf114f1dSAlex Hornung 		 "<device> is the encrypted device\n"
681bf114f1dSAlex Hornung 		 "<key slot> is the LUKS key slot number to modify\n"
682bf114f1dSAlex Hornung 		 "<key file> optional key file for the new key for luksAddKey action\n"),
683bf114f1dSAlex Hornung 		crypt_get_dir());
684bf114f1dSAlex Hornung 
685bf114f1dSAlex Hornung 	log_std(_("\nDefault compiled-in device cipher parameters:\n"
686bf114f1dSAlex Hornung 		 "\tplain: %s, Key: %d bits, Password hashing: %s\n"
687bf114f1dSAlex Hornung 		 "\tLUKS1: %s, Key: %d bits, LUKS header hashing: %s\n"),
688bf114f1dSAlex Hornung 		 DEFAULT_CIPHER(PLAIN), DEFAULT_PLAIN_KEYBITS, DEFAULT_PLAIN_HASH,
689bf114f1dSAlex Hornung 		 DEFAULT_CIPHER(LUKS1), DEFAULT_LUKS1_KEYBITS, DEFAULT_LUKS1_HASH);
690bf114f1dSAlex Hornung 	exit(0);
69113af8cbeSAlex Hornung 
692bf114f1dSAlex Hornung }
693bf114f1dSAlex Hornung 
694bf114f1dSAlex Hornung void set_debug_level(int level);
695bf114f1dSAlex Hornung 
_dbg_version_and_cmd(int argc,char ** argv)696bf114f1dSAlex Hornung static void _dbg_version_and_cmd(int argc, char **argv)
697bf114f1dSAlex Hornung {
698bf114f1dSAlex Hornung 	int i;
699bf114f1dSAlex Hornung 
700bf114f1dSAlex Hornung 	log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
701bf114f1dSAlex Hornung 	for (i = 0; i < argc; i++) {
702bf114f1dSAlex Hornung 		if (i)
703bf114f1dSAlex Hornung 			log_std(" ");
704bf114f1dSAlex Hornung 		log_std(argv[i]);
705bf114f1dSAlex Hornung 	}
706bf114f1dSAlex Hornung 	log_std("\"\n");
707bf114f1dSAlex Hornung }
708bf114f1dSAlex Hornung 
run_action(struct action_type * action)709bf114f1dSAlex Hornung static int run_action(struct action_type *action)
710bf114f1dSAlex Hornung {
711bf114f1dSAlex Hornung 	int r;
712bf114f1dSAlex Hornung 
713bf114f1dSAlex Hornung 	if (action->required_memlock)
714bf114f1dSAlex Hornung 		crypt_memory_lock(NULL, 1);
715bf114f1dSAlex Hornung 
716bf114f1dSAlex Hornung 	r = action->handler(action->arg);
717bf114f1dSAlex Hornung 
718bf114f1dSAlex Hornung 	if (action->required_memlock)
719bf114f1dSAlex Hornung 		crypt_memory_lock(NULL, 0);
720bf114f1dSAlex Hornung 
721bf114f1dSAlex Hornung 	show_status(r);
722bf114f1dSAlex Hornung 
723bf114f1dSAlex Hornung 	return r;
724bf114f1dSAlex Hornung }
725bf114f1dSAlex Hornung 
main(int argc,char ** argv)726bf114f1dSAlex Hornung int main(int argc, char **argv)
727bf114f1dSAlex Hornung {
72813af8cbeSAlex Hornung 	static struct option options[] = {
72913af8cbeSAlex Hornung 		{ "help",  	       no_argument, 		NULL, '?' },
73013af8cbeSAlex Hornung 		{ "usage", 	       no_argument, 		NULL, 'u' },
73113af8cbeSAlex Hornung 		{ "verbose",           no_argument, 		NULL, 'v' },
73213af8cbeSAlex Hornung 		{ "debug",             no_argument, 		&opt_debug, 1 },
73313af8cbeSAlex Hornung 		{ "cipher",            required_argument, 	NULL, 'c' },
73413af8cbeSAlex Hornung 		{ "hash",              required_argument, 	NULL, 'h' },
73513af8cbeSAlex Hornung 		{ "verify-passphrase", no_argument, 		NULL, 'y' },
73613af8cbeSAlex Hornung 		{ "key-file",          required_argument, 	NULL, 'd' },
73713af8cbeSAlex Hornung 		{ "master-key-file",   required_argument, 	NULL, 'm' },
73813af8cbeSAlex Hornung 		{ "key-size",          required_argument, 	NULL, 's' },
73913af8cbeSAlex Hornung 		{ "key-slot",          required_argument, 	NULL, 'S' },
74013af8cbeSAlex Hornung 		{ "size",              required_argument, 	NULL, 'b' },
74113af8cbeSAlex Hornung 		{ "offset",            required_argument, 	NULL, 'o' },
74213af8cbeSAlex Hornung 		{ "skip",              required_argument,	NULL, 'p' },
74313af8cbeSAlex Hornung 		{ "readonly",          no_argument, 		NULL, 'r' },
74413af8cbeSAlex Hornung 		{ "iter-time",         required_argument,	NULL, 'i' },
74513af8cbeSAlex Hornung 		{ "batch-mode",        no_argument,		NULL, 'q' },
74613af8cbeSAlex Hornung 		{ "version",           no_argument, 		&opt_version_mode, 1 },
74713af8cbeSAlex Hornung 		{ "timeout",           required_argument, 	NULL, 't' },
74813af8cbeSAlex Hornung 		{ "tries",             required_argument, 	NULL, 'T' },
74913af8cbeSAlex Hornung 		{ "align-payload",     required_argument, 	NULL, 'a' },
75013af8cbeSAlex Hornung 		{ "header-backup-file",required_argument, 	NULL, 'x' },
75113af8cbeSAlex Hornung 		{ NULL,			0,			NULL, 0 }
752bf114f1dSAlex Hornung 	};
753bf114f1dSAlex Hornung 	struct action_type *action;
754bf114f1dSAlex Hornung 	char *aname;
755bf114f1dSAlex Hornung 	int r;
756bf114f1dSAlex Hornung 	const char *null_action_argv[] = {NULL};
757bf114f1dSAlex Hornung 
758bf114f1dSAlex Hornung 	crypt_set_log_callback(NULL, _log, NULL);
759bf114f1dSAlex Hornung 
760bf114f1dSAlex Hornung 	setlocale(LC_ALL, "");
761bf114f1dSAlex Hornung 	bindtextdomain(PACKAGE, LOCALEDIR);
762bf114f1dSAlex Hornung 	textdomain(PACKAGE);
763bf114f1dSAlex Hornung 
76413af8cbeSAlex Hornung 	while((r = getopt_long(argc, argv, "?vc:h:yd:m:s:S:b:o:p:ri:qt:T:", options, NULL)) != -1)
76513af8cbeSAlex Hornung 	{
766bf114f1dSAlex Hornung 		switch (r) {
76713af8cbeSAlex Hornung 		case 'u':
76813af8cbeSAlex Hornung 			usage(NULL);
769bf114f1dSAlex Hornung 			break;
77013af8cbeSAlex Hornung 		case 'v':
77113af8cbeSAlex Hornung 			opt_verbose = 1;
772bf114f1dSAlex Hornung 			break;
77313af8cbeSAlex Hornung 		case 'c':
77413af8cbeSAlex Hornung 			opt_cipher = optarg;
775bf114f1dSAlex Hornung 			break;
77613af8cbeSAlex Hornung 		case 'h':
77713af8cbeSAlex Hornung 			opt_hash = optarg;
778bf114f1dSAlex Hornung 			break;
77913af8cbeSAlex Hornung 		case 'y':
78013af8cbeSAlex Hornung 			opt_verify_passphrase = 1;
78113af8cbeSAlex Hornung 			break;
78213af8cbeSAlex Hornung 		case 'd':
78313af8cbeSAlex Hornung 			opt_key_file = optarg;
78413af8cbeSAlex Hornung 			break;
78513af8cbeSAlex Hornung 		case 'm':
78613af8cbeSAlex Hornung 			opt_master_key_file = optarg;
78713af8cbeSAlex Hornung 			break;
78813af8cbeSAlex Hornung 		case 's':
78913af8cbeSAlex Hornung 			opt_key_size = atoi(optarg);
79013af8cbeSAlex Hornung 			break;
79113af8cbeSAlex Hornung 		case 'S':
79213af8cbeSAlex Hornung 			opt_key_slot = atoi(optarg);
79313af8cbeSAlex Hornung 			break;
79413af8cbeSAlex Hornung 		case 'b':
79513af8cbeSAlex Hornung 			opt_size = strtoull(optarg, NULL, 0);
79613af8cbeSAlex Hornung 			break;
79713af8cbeSAlex Hornung 		case 'o':
79813af8cbeSAlex Hornung 			opt_offset = strtoull(optarg, NULL, 0);
79913af8cbeSAlex Hornung 			break;
80013af8cbeSAlex Hornung 		case 'p':
80113af8cbeSAlex Hornung 			opt_skip = strtoull(optarg, NULL, 0);
80213af8cbeSAlex Hornung 			break;
80313af8cbeSAlex Hornung 		case 'r':
80413af8cbeSAlex Hornung 			opt_readonly = 1;
80513af8cbeSAlex Hornung 			break;
80613af8cbeSAlex Hornung 		case 'i':
80713af8cbeSAlex Hornung 			opt_iteration_time = atoi(optarg);
80813af8cbeSAlex Hornung 			break;
80913af8cbeSAlex Hornung 		case 'q':
81013af8cbeSAlex Hornung 			opt_batch_mode = 1;
81113af8cbeSAlex Hornung 			break;
81213af8cbeSAlex Hornung 		case 't':
81313af8cbeSAlex Hornung 			opt_timeout = atoi(optarg);
81413af8cbeSAlex Hornung 			break;
81513af8cbeSAlex Hornung 		case 'T':
81613af8cbeSAlex Hornung 			opt_tries = atoi(optarg);
81713af8cbeSAlex Hornung 			break;
81813af8cbeSAlex Hornung 		case 'a':
81913af8cbeSAlex Hornung 			opt_align_payload = atoi(optarg);
82013af8cbeSAlex Hornung 			break;
82113af8cbeSAlex Hornung 		case 'x':
82213af8cbeSAlex Hornung 			opt_header_backup_file = optarg;
82313af8cbeSAlex Hornung 			break;
82413af8cbeSAlex Hornung 		case '?':
82513af8cbeSAlex Hornung 			help();
82613af8cbeSAlex Hornung 			break;
82713af8cbeSAlex Hornung 		case 0:
828bf114f1dSAlex Hornung 			if (opt_version_mode) {
829bf114f1dSAlex Hornung 				log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
830bf114f1dSAlex Hornung 				exit(0);
831bf114f1dSAlex Hornung 			}
83213af8cbeSAlex Hornung 			break;
83313af8cbeSAlex Hornung 		}
83413af8cbeSAlex Hornung 	}
835bf114f1dSAlex Hornung 
836bf114f1dSAlex Hornung 	if (opt_key_size % 8)
83713af8cbeSAlex Hornung 		usage(_("Key size must be a multiple of 8 bits"));
838bf114f1dSAlex Hornung 
83913af8cbeSAlex Hornung 	argc -= optind;
84013af8cbeSAlex Hornung 	argv += optind;
84113af8cbeSAlex Hornung 
84213af8cbeSAlex Hornung 	if (argc == 0) {
84313af8cbeSAlex Hornung 		usage(_("Argument <action> missing."));
84413af8cbeSAlex Hornung 		/* NOTREACHED */
84513af8cbeSAlex Hornung 	}
84613af8cbeSAlex Hornung 
84713af8cbeSAlex Hornung 	aname = argv[0];
848bf114f1dSAlex Hornung 	for(action = action_types; action->type; action++)
849bf114f1dSAlex Hornung 		if (strcmp(action->type, aname) == 0)
850bf114f1dSAlex Hornung 			break;
85113af8cbeSAlex Hornung 	if (!action->type) {
85213af8cbeSAlex Hornung 		usage( _("Unknown action."));
85313af8cbeSAlex Hornung 		/* NOTREACHED */
85413af8cbeSAlex Hornung 	}
855bf114f1dSAlex Hornung 
85613af8cbeSAlex Hornung 	action_argc = argc-1;
857*79244848SFrançois Tigeot 	action_argv = (void *)&argv[1];
858bf114f1dSAlex Hornung 
859bf114f1dSAlex Hornung 	if(action_argc < action->required_action_argc) {
860bf114f1dSAlex Hornung 		char buf[128];
861bf114f1dSAlex Hornung 		snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
86213af8cbeSAlex Hornung 		usage(buf);
86313af8cbeSAlex Hornung 		/* NOTREACHED */
864bf114f1dSAlex Hornung 	}
865bf114f1dSAlex Hornung 
866bf114f1dSAlex Hornung 	if (opt_debug) {
867bf114f1dSAlex Hornung 		opt_verbose = 1;
868bf114f1dSAlex Hornung 		crypt_set_debug_level(-1);
869bf114f1dSAlex Hornung 		_dbg_version_and_cmd(argc, argv);
870bf114f1dSAlex Hornung 	}
871bf114f1dSAlex Hornung 
872bf114f1dSAlex Hornung 	return run_action(action);
873bf114f1dSAlex Hornung }
874