* Add --master-key-file option for luksFormat and luksAddKey.
[platform/upstream/cryptsetup.git] / src / cryptsetup.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <inttypes.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <assert.h>
10
11 #include <libcryptsetup.h>
12 #include <popt.h>
13
14 #include "../config.h"
15
16 #include "cryptsetup.h"
17
18 static int opt_verbose = 0;
19 static int opt_debug = 0;
20 static char *opt_cipher = NULL;
21 static char *opt_hash = NULL;
22 static int opt_verify_passphrase = 0;
23 static char *opt_key_file = NULL;
24 static char *opt_master_key_file = NULL;
25 static unsigned int opt_key_size = 0;
26 static int opt_key_slot = CRYPT_ANY_SLOT;
27 static uint64_t opt_size = 0;
28 static uint64_t opt_offset = 0;
29 static uint64_t opt_skip = 0;
30 static int opt_readonly = 0;
31 static int opt_iteration_time = 1000;
32 static int opt_batch_mode = 0;
33 static int opt_version_mode = 0;
34 static int opt_timeout = 0;
35 static int opt_tries = 3;
36 static int opt_align_payload = 0;
37 static int opt_non_exclusive = 0;
38
39 static const char **action_argv;
40 static int action_argc;
41
42 static int action_create(int arg);
43 static int action_remove(int arg);
44 static int action_resize(int arg);
45 static int action_status(int arg);
46 static int action_luksFormat(int arg);
47 static int action_luksOpen(int arg);
48 static int action_luksAddKey(int arg);
49 static int action_luksDelKey(int arg);
50 static int action_luksKillSlot(int arg);
51 static int action_luksRemoveKey(int arg);
52 static int action_isLuks(int arg);
53 static int action_luksUUID(int arg);
54 static int action_luksDump(int arg);
55
56 static struct action_type {
57         const char *type;
58         int (*handler)(int);
59         int arg;
60         int required_action_argc;
61         int required_dm_backend;
62         int required_memlock;
63         int show_status;
64         const char *arg_desc;
65         const char *desc;
66 } action_types[] = {
67         { "create",     action_create,          0, 2, 1, 1, 1, N_("<name> <device>"),N_("create device") },
68         { "remove",     action_remove,          0, 1, 1, 0, 1, N_("<name>"), N_("remove device") },
69         { "resize",     action_resize,          0, 1, 1, 1, 1, N_("<name>"), N_("resize active device") },
70         { "status",     action_status,          0, 1, 1, 0, 1, N_("<name>"), N_("show device status") },
71         { "luksFormat", action_luksFormat,      0, 1, 1, 1, 1, N_("<device> [<new key file>]"), N_("formats a LUKS device") },
72         { "luksOpen",   action_luksOpen,        0, 2, 1, 1, 1, N_("<device> <name> "), N_("open LUKS device as mapping <name>") },
73         { "luksAddKey", action_luksAddKey,      0, 1, 1, 1, 1, N_("<device> [<new key file>]"), N_("add key to LUKS device") },
74         { "luksRemoveKey",action_luksRemoveKey, 0, 1, 1, 1, 1, N_("<device> [<key file>]"), N_("removes supplied key or key file from LUKS device") },
75         { "luksKillSlot",  action_luksKillSlot, 0, 2, 1, 1, 1, N_("<device> <key slot>"), N_("wipes key with number <key slot> from LUKS device") },
76         { "luksUUID",   action_luksUUID,        0, 1, 0, 0, 1, N_("<device>"), N_("print UUID of LUKS device") },
77         { "isLuks",     action_isLuks,          0, 1, 0, 0, 0, N_("<device>"), N_("tests <device> for LUKS partition header") },
78         { "luksClose",  action_remove,          0, 1, 1, 0, 1, N_("<name>"), N_("remove LUKS mapping") },
79         { "luksDump",   action_luksDump,        0, 1, 0, 0, 1, N_("<device>"), N_("dump LUKS partition information") },
80         { "luksDelKey", action_luksDelKey,      0, 2, 1, 1, 1, N_("<device> <key slot>"), N_("identical to luksKillSlot - DEPRECATED - see man page") },
81         { "reload",     action_create,          1, 2, 1, 1, 1, N_("<name> <device>"), N_("modify active device - DEPRECATED - see man page") },
82         { NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL }
83 };
84
85 /* Interface Callbacks */
86 static int yesDialog(char *msg)
87 {
88         char *answer = NULL;
89         size_t size = 0;
90         int r = 1;
91
92         if(isatty(0) && !opt_batch_mode) {
93                 log_std("\nWARNING!\n========\n");
94                 log_std("%s\n\nAre you sure? (Type uppercase yes): ", msg);
95                 if(getline(&answer, &size, stdin) == -1) {
96                         perror("getline");
97                         free(answer);
98                         return 0;
99                 }
100                 if(strcmp(answer, "YES\n"))
101                         r = 0;
102                 free(answer);
103         }
104
105         return r;
106 }
107
108 static void cmdLineLog(int class, char *msg) {
109     switch(class) {
110
111     case CRYPT_LOG_NORMAL:
112             fputs(msg, stdout);
113             break;
114     case CRYPT_LOG_ERROR:
115             fputs(msg, stderr);
116             break;
117     default:
118             fprintf(stderr, "Internal error on logging class for msg: %s", msg);
119             break;
120     }
121 }
122
123 static struct interface_callbacks cmd_icb = {
124         .yesDialog = yesDialog,
125         .log = cmdLineLog,
126 };
127
128 /* End ICBs */
129
130 static void show_status(int errcode)
131 {
132         char error[256];
133
134         if(!errcode && opt_verbose) {
135                 log_std(_("Command successful.\n"));
136                 return;
137         }
138
139         crypt_get_error(error, sizeof(error));
140
141         if (!opt_verbose) {
142                 char *error_ = strerror_r(errcode, error, sizeof(error));
143                 if (error_ != error) {
144                         strncpy(error, error_, sizeof(error));
145                         error[sizeof error - 1] = '\0';
146                 }
147         }
148
149         log_err(_("Command failed"));
150         if (*error)
151                 log_err(": %s\n", error);
152         else
153                 log_err(".\n");
154         return;
155 }
156
157 static int action_create(int reload)
158 {
159         struct crypt_options options = {
160                 .name = action_argv[0],
161                 .device = action_argv[1],
162                 .cipher = opt_cipher?opt_cipher:DEFAULT_CIPHER,
163                 .hash = opt_hash ?: DEFAULT_HASH,
164                 .key_file = opt_key_file,
165                 .key_size = ((opt_key_size)?opt_key_size:DEFAULT_KEY_SIZE)/8,
166                 .key_slot = opt_key_slot,
167                 .flags = 0,
168                 .size = opt_size,
169                 .offset = opt_offset,
170                 .skip = opt_skip,
171                 .timeout = opt_timeout,
172                 .tries = opt_tries,
173                 .icb = &cmd_icb,
174         };
175         int r;
176
177         if(reload) 
178                 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"));
179
180         if (options.hash && strcmp(options.hash, "plain") == 0)
181                 options.hash = NULL;
182         if (opt_verify_passphrase)
183                 options.flags |= CRYPT_FLAG_VERIFY;
184         if (opt_readonly)
185                 options.flags |= CRYPT_FLAG_READONLY;
186
187         if (reload)
188                 r = crypt_update_device(&options);
189         else
190                 r = crypt_create_device(&options);
191
192         return r;
193 }
194
195 static int action_remove(int arg)
196 {
197         struct crypt_options options = {
198                 .name = action_argv[0],
199                 .icb = &cmd_icb,
200         };
201
202         return crypt_remove_device(&options);
203 }
204
205 static int action_resize(int arg)
206 {
207         struct crypt_options options = {
208                 .name = action_argv[0],
209                 .size = opt_size,
210                 .icb = &cmd_icb,
211         };
212
213         return crypt_resize_device(&options);
214 }
215
216 static int action_status(int arg)
217 {
218         struct crypt_options options = {
219                 .name = action_argv[0],
220                 .icb = &cmd_icb,
221         };
222         int r;
223
224         r = crypt_query_device(&options);
225         if (r < 0)
226                 return r;
227
228         if (r == 0) {
229                 /* inactive */
230                 log_std("%s/%s is inactive.\n", crypt_get_dir(), options.name);
231                 r = 1;
232         } else {
233                 /* active */
234                 log_std("%s/%s is active:\n", crypt_get_dir(), options.name);
235                 log_std("  cipher:  %s\n", options.cipher);
236                 log_std("  keysize: %d bits\n", options.key_size * 8);
237                 log_std("  device:  %s\n", options.device);
238                 log_std("  offset:  %" PRIu64 " sectors\n", options.offset);
239                 log_std("  size:    %" PRIu64 " sectors\n", options.size);
240                 if (options.skip)
241                         log_std("  skipped: %" PRIu64 " sectors\n", options.skip);
242                 log_std("  mode:    %s\n", (options.flags & CRYPT_FLAG_READONLY)
243                                            ? "readonly" : "read/write");
244                 crypt_put_options(&options);
245                 r = 0;
246         }
247         return r;
248 }
249
250 static int _action_luksFormat_generateMK()
251 {
252         struct crypt_options options = {
253                 .key_size = (opt_key_size ?: DEFAULT_LUKS_KEY_SIZE) / 8,
254                 .key_slot = opt_key_slot,
255                 .device = action_argv[0],
256                 .cipher = opt_cipher ?: DEFAULT_LUKS_CIPHER,
257                 .hash = opt_hash ?: DEFAULT_LUKS_HASH,
258                 .new_key_file = action_argc > 1 ? action_argv[1] : NULL,
259                 .flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE :  0),
260                 .iteration_time = opt_iteration_time,
261                 .timeout = opt_timeout,
262                 .align_payload = opt_align_payload,
263                 .icb = &cmd_icb,
264         };
265
266         return crypt_luksFormat(&options);
267 }
268
269 static int _read_mk(const char *file, char **key, int keysize)
270 {
271         int fd;
272
273         *key = malloc(keysize);
274         if (!*key)
275                 return -ENOMEM;
276
277         fd = open(file, O_RDONLY);
278         if (fd == -1) {
279                 log_err("Cannot read keyfile %s.\n", file);
280                 return -EINVAL;
281         }
282         if ((read(fd, *key, keysize) != keysize)) {
283                 log_err("Cannot read %d bytes from keyfile %s.\n", keysize, file);
284                 close(fd);
285                 memset(*key, 0, keysize);
286                 free(*key);
287                 return -EINVAL;
288         }
289         close(fd);
290         return 0;
291 }
292
293 static int _action_luksFormat_useMK()
294 {
295         int r = -EINVAL, keysize;
296         char *key = NULL, cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
297         struct crypt_device *cd = NULL;
298         struct crypt_params_luks1 params = {
299                 .hash = opt_hash ?: DEFAULT_LUKS_HASH,
300                 .data_alignment = opt_align_payload,
301         };
302
303         if (parse_into_name_and_mode(opt_cipher ?: DEFAULT_LUKS_CIPHER, cipher, cipher_mode)) {
304                 log_err("No known cipher specification pattern detected.\n");
305                 return -EINVAL;
306         }
307
308         keysize = (opt_key_size ?: DEFAULT_LUKS_KEY_SIZE) / 8;
309         if (_read_mk(opt_master_key_file, &key, keysize) < 0)
310                 return -EINVAL;
311
312         if ((r = crypt_init(&cd, action_argv[0])))
313                 goto out;
314
315         crypt_set_password_verify(cd, 1);
316         crypt_set_timeout(cd, opt_timeout);
317         if (opt_iteration_time)
318                 crypt_set_iterarion_time(cd, opt_iteration_time);
319
320         if ((r = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, keysize, &params)))
321                 goto out;
322
323         r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot, key, keysize, NULL, 0);
324 out:
325
326         crypt_free(cd);
327         if (key) {
328                 memset(key, 0, keysize);
329                 free(key);
330         }
331         return r;
332 }
333
334 static int action_luksFormat(int arg)
335 {
336         int r = 0; char *msg = NULL;
337
338         /* Avoid overwriting possibly wrong part of device than user requested by rejecting these options */
339         if (opt_offset || opt_skip) {
340                 log_err("Options --offset and --skip are not supported for luksFormat.\n"); 
341                 return -EINVAL;
342         }
343
344         if(asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]) == -1) {
345                 log_err(_("memory allocation error in action_luksFormat"));
346                 return -ENOMEM;
347         }
348         r = yesDialog(msg);
349         free(msg);
350
351         if (!r)
352                 return -EINVAL;
353
354         if (opt_master_key_file)
355                 return _action_luksFormat_useMK();
356         else
357                 return _action_luksFormat_generateMK();
358 }
359
360 static int action_luksOpen(int arg)
361 {
362         struct crypt_options options = {
363                 .name = action_argv[1],
364                 .device = action_argv[0],
365                 .key_file = opt_key_file,
366                 .key_size = opt_key_file ? (opt_key_size / 8) : 0, /* limit bytes read from keyfile */
367                 .timeout = opt_timeout,
368                 .tries = opt_key_file ? 1 : opt_tries, /* verify is usefull only for tty */
369                 .icb = &cmd_icb,
370         };
371
372         if (opt_readonly)
373                 options.flags |= CRYPT_FLAG_READONLY;
374         if (opt_non_exclusive)
375                 options.flags |= CRYPT_FLAG_NON_EXCLUSIVE_ACCESS;
376         return crypt_luksOpen(&options);
377 }
378
379 static int action_luksDelKey(int arg)
380 {
381         log_err("luksDelKey is a deprecated action name.\nPlease use luksKillSlot.\n"); 
382         return action_luksKillSlot(arg);
383 }
384
385 static int action_luksKillSlot(int arg)
386 {
387         struct crypt_options options = {
388                 .device = action_argv[0],
389                 .key_slot = atoi(action_argv[1]),
390                 .key_file = opt_key_file,
391                 .timeout = opt_timeout,
392                 .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
393                 .icb = &cmd_icb,
394         };
395
396         return crypt_luksKillSlot(&options);
397 }
398
399 static int action_luksRemoveKey(int arg)
400 {
401         struct crypt_options options = {
402                 .device = action_argv[0],
403                 .new_key_file = action_argc>1?action_argv[1]:NULL,
404                 .key_file = opt_key_file,
405                 .timeout = opt_timeout,
406                 .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
407                 .icb = &cmd_icb,
408         };
409
410         return crypt_luksRemoveKey(&options);
411 }
412
413 static int _action_luksAddKey_useMK()
414 {
415         int r = -EINVAL, keysize;
416         char *key = NULL;
417         struct crypt_device *cd = NULL;
418
419         if ((r = crypt_init(&cd, action_argv[0])))
420                 goto out;
421
422         if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
423                 goto out;
424
425         keysize = crypt_get_volume_key_size(cd);
426         crypt_set_password_verify(cd, 1);
427         crypt_set_timeout(cd, opt_timeout);
428         if (opt_iteration_time)
429                 crypt_set_iterarion_time(cd, opt_iteration_time);
430
431         if (_read_mk(opt_master_key_file, &key, keysize) < 0)
432                 goto out;
433
434         r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot, key, keysize, NULL, 0);
435 out:
436         crypt_free(cd);
437         if (key) {
438                 memset(key, 0, keysize);
439                 free(key);
440         }
441         return r;
442 }
443
444 static int action_luksAddKey(int arg)
445 {
446         struct crypt_options options = {
447                 .device = action_argv[0],
448                 .new_key_file = action_argc>1?action_argv[1]:NULL,
449                 .key_file = opt_key_file,
450                 .key_slot = opt_key_slot,
451                 .flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE : 0),
452                 .iteration_time = opt_iteration_time,
453                 .timeout = opt_timeout,
454                 .icb = &cmd_icb,
455         };
456
457         if (opt_master_key_file)
458                 return _action_luksAddKey_useMK();
459         else
460                 return crypt_luksAddKey(&options);
461 }
462
463 static int action_isLuks(int arg)
464 {
465         struct crypt_options options = {
466                 .device = action_argv[0],
467                 .icb = &cmd_icb,
468         };
469
470         return crypt_isLuks(&options);
471 }
472
473 static int action_luksUUID(int arg)
474 {
475         struct crypt_options options = {
476                 .device = action_argv[0],
477                 .icb = &cmd_icb,
478         };
479
480         return crypt_luksUUID(&options);
481 }
482
483 static int action_luksDump(int arg)
484 {
485         struct crypt_options options = {
486                 .device = action_argv[0],
487                 .icb = &cmd_icb,
488         };
489
490         return crypt_luksDump(&options);
491 }
492
493 static void usage(poptContext popt_context, int exitcode,
494                   const char *error, const char *more)
495 {
496         poptPrintUsage(popt_context, stderr, 0);
497         if (error)
498                 log_err("%s: %s\n", more, error);
499         exit(exitcode);
500 }
501
502 static void help(poptContext popt_context, enum poptCallbackReason reason,
503                  struct poptOption *key, const char * arg, void *data)
504 {
505         if (key->shortName == '?') {
506                 struct action_type *action;
507
508                 log_std("%s\n",PACKAGE_STRING);
509
510                 poptPrintHelp(popt_context, stdout, 0);
511
512                 log_std(_("\n"
513                          "<action> is one of:\n"));
514
515                 for(action = action_types; action->type; action++)
516                         log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
517                 
518                 log_std(_("\n"
519                          "<name> is the device to create under %s\n"
520                          "<device> is the encrypted device\n"
521                          "<key slot> is the LUKS key slot number to modify\n"
522                          "<key file> optional key file for the new key for luksAddKey action\n"),
523                         crypt_get_dir());
524                 exit(0);
525         } else
526                 usage(popt_context, 0, NULL, NULL);
527 }
528
529 void set_debug_level(int level);
530
531 static void _dbg_version_and_cmd(int argc, char **argv)
532 {
533         int i;
534
535         log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
536         for (i = 0; i < argc; i++) {
537                 if (i)
538                         log_std(" ");
539                 log_std(argv[i]);
540         }
541         log_std("\"\n");
542 }
543
544 static int run_action(struct action_type *action)
545 {
546         int r;
547
548         if (dm_init(NULL, action->required_dm_backend) < 1) {
549                 log_err("Cannot communicate with device-mapper. Is dm_mod kernel module loaded?\n");
550                 return -ENOSYS;
551         }
552
553         if (action->required_memlock)
554                 crypt_memory_lock(NULL, 1);
555
556         r = action->handler(action->arg);
557
558         if (action->required_memlock)
559                 crypt_memory_lock(NULL, 0);
560
561         if (action->required_dm_backend)
562                 dm_exit();
563
564         if (r < 0 && (opt_verbose || action->show_status))
565                 show_status(-r);
566
567         return r;
568 }
569
570 int main(int argc, char **argv)
571 {
572         static char *popt_tmp;
573         static struct poptOption popt_help_options[] = {
574                 { NULL,    '\0', POPT_ARG_CALLBACK, help, 0, NULL,                         NULL },
575                 { "help",  '?',  POPT_ARG_NONE,     NULL, 0, N_("Show this help message"), NULL },
576                 { "usage", '\0', POPT_ARG_NONE,     NULL, 0, N_("Display brief usage"),    NULL },
577                 POPT_TABLEEND
578         };
579         static struct poptOption popt_options[] = {
580                 { NULL,                '\0', POPT_ARG_INCLUDE_TABLE,                      popt_help_options,      0, N_("Help options:"),                                                   NULL },
581                 { "verbose",           'v',  POPT_ARG_NONE,                               &opt_verbose,           0, N_("Shows more detailed error messages"),                              NULL },
582                 { "debug",             '\0', POPT_ARG_NONE,                               &opt_debug,             0, N_("Show debug messsgages"),                                           NULL },
583                 { "cipher",            'c',  POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &opt_cipher,            0, N_("The cipher used to encrypt the disk (see /proc/crypto)"),          NULL },
584                 { "hash",              'h',  POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &opt_hash,              0, N_("The hash used to create the encryption key from the passphrase"),  NULL },
585                 { "verify-passphrase", 'y',  POPT_ARG_NONE,                               &opt_verify_passphrase, 0, N_("Verifies the passphrase by asking for it twice"),                  NULL },
586                 { "key-file",          'd',  POPT_ARG_STRING,                             &opt_key_file,          0, N_("Read the key from a file (can be /dev/random)"),                   NULL },
587                 { "master-key-file",  '\0',  POPT_ARG_STRING,                             &opt_master_key_file,   0, N_("Read the volume (master) key from file."),                         NULL },
588                 { "key-size",          's',  POPT_ARG_INT    | POPT_ARGFLAG_SHOW_DEFAULT, &opt_key_size,          0, N_("The size of the encryption key"),                                  N_("BITS") },
589                 { "key-slot",          'S',  POPT_ARG_INT,                                &opt_key_slot,          0, N_("Slot number for new key (default is first free)"),      NULL },
590                 { "size",              'b',  POPT_ARG_STRING,                             &popt_tmp,              1, N_("The size of the device"),                                          N_("SECTORS") },
591                 { "offset",            'o',  POPT_ARG_STRING,                             &popt_tmp,              2, N_("The start offset in the backend device"),                          N_("SECTORS") },
592                 { "skip",              'p',  POPT_ARG_STRING,                             &popt_tmp,              3, N_("How many sectors of the encrypted data to skip at the beginning"), N_("SECTORS") },
593                 { "readonly",          'r',  POPT_ARG_NONE,                               &opt_readonly,          0, N_("Create a readonly mapping"),                                       NULL },
594                 { "iter-time",         'i',  POPT_ARG_INT,                                &opt_iteration_time,    0, N_("PBKDF2 iteration time for LUKS (in ms)"),
595                   N_("msecs") },
596                 { "batch-mode",        'q',  POPT_ARG_NONE,                               &opt_batch_mode,        0, N_("Do not ask for confirmation"),                                     NULL },
597                 { "version",        '\0',  POPT_ARG_NONE,                                 &opt_version_mode,        0, N_("Print package version"),                                     NULL },
598                 { "timeout",           't',  POPT_ARG_INT,                                &opt_timeout,           0, N_("Timeout for interactive passphrase prompt (in seconds)"),          N_("secs") },
599                 { "tries",             'T',  POPT_ARG_INT,                                &opt_tries,             0, N_("How often the input of the passphrase canbe retried"),            NULL },
600                 { "align-payload",     '\0',  POPT_ARG_INT,                               &opt_align_payload,     0, N_("Align payload at <n> sector boundaries - for luksFormat"),         N_("SECTORS") },
601                 { "non-exclusive",     '\0',  POPT_ARG_NONE,                              &opt_non_exclusive,     0, N_("Allows non-exclusive access for luksOpen, WARNING see manpage."),        NULL },
602                 POPT_TABLEEND
603         };
604         poptContext popt_context;
605         struct action_type *action;
606         char *aname;
607         int r;
608         const char *null_action_argv[] = {NULL};
609
610         set_default_log(cmdLineLog);
611
612         setlocale(LC_ALL, "");
613         bindtextdomain(PACKAGE, LOCALEDIR);
614         textdomain(PACKAGE);
615
616         popt_context = poptGetContext(PACKAGE, argc, (const char **)argv,
617                                       popt_options, 0);
618         poptSetOtherOptionHelp(popt_context,
619                                N_("[OPTION...] <action> <action-specific>]"));
620
621         while((r = poptGetNextOpt(popt_context)) > 0) {
622                 unsigned long long ull_value;
623                 char *endp;
624
625                 ull_value = strtoull(popt_tmp, &endp, 0);
626                 if (*endp || !*popt_tmp)
627                         r = POPT_ERROR_BADNUMBER;
628
629                 switch(r) {
630                         case 1:
631                                 opt_size = ull_value;
632                                 break;
633                         case 2:
634                                 opt_offset = ull_value;
635                                 break;
636                         case 3:
637                                 opt_skip = ull_value;
638                                 break;
639                 }
640
641                 if (r < 0)
642                         break;
643         }
644
645         if (r < -1)
646                 usage(popt_context, 1, poptStrerror(r),
647                       poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
648         if (opt_version_mode) {
649                 log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
650                 exit(0);
651         }
652
653         if (opt_key_size % 8)
654                 usage(popt_context, 1,
655                       _("Key size must be a multiple of 8 bits"),
656                       poptGetInvocationName(popt_context));
657
658         if (!(aname = (char *)poptGetArg(popt_context)))
659                 usage(popt_context, 1, _("Argument <action> missing."),
660                       poptGetInvocationName(popt_context));
661         for(action = action_types; action->type; action++)
662                 if (strcmp(action->type, aname) == 0)
663                         break;
664         if (!action->type)
665                 usage(popt_context, 1, _("Unknown action."),
666                       poptGetInvocationName(popt_context));
667
668         action_argc = 0;
669         action_argv = poptGetArgs(popt_context);
670         /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
671         if(!action_argv) 
672                 action_argv = null_action_argv;
673
674         /* Count args, somewhat unnice, change? */
675         while(action_argv[action_argc] != NULL)
676                 action_argc++;
677
678         if(action_argc < action->required_action_argc) {
679                 char buf[128];
680                 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
681                 usage(popt_context, 1, buf,
682                       poptGetInvocationName(popt_context));
683         }
684
685         if (opt_debug) {
686                 opt_verbose = 1;
687                 crypt_set_debug_level(-1);
688                 _dbg_version_and_cmd(argc, argv);
689         }
690
691         return run_action(action);
692 }