]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/opt.c
c2a5878ef68d1b07e15998fbc3a4171d75ce8d02
[thirdparty/openssl.git] / apps / opt.c
1 /*
2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /*
11 * This file is also used by the test suite. Do not #include "apps.h".
12 */
13 #include "opt.h"
14 #include "fmt.h"
15 #include "internal/nelem.h"
16 #include <string.h>
17 #if !defined(OPENSSL_SYS_MSDOS)
18 # include <unistd.h>
19 #endif
20
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <openssl/bio.h>
26 #include <openssl/x509v3.h>
27
28 #define MAX_OPT_HELP_WIDTH 30
29 const char OPT_HELP_STR[] = "--";
30 const char OPT_MORE_STR[] = "---";
31
32 /* Our state */
33 static char **argv;
34 static int argc;
35 static int opt_index;
36 static char *arg;
37 static char *flag;
38 static char *dunno;
39 static const OPTIONS *unknown;
40 static const OPTIONS *opts;
41 static char prog[40];
42
43 /*
44 * Return the simple name of the program; removing various platform gunk.
45 */
46 #if defined(OPENSSL_SYS_WIN32)
47 char *opt_progname(const char *argv0)
48 {
49 size_t i, n;
50 const char *p;
51 char *q;
52
53 /* find the last '/', '\' or ':' */
54 for (p = argv0 + strlen(argv0); --p > argv0;)
55 if (*p == '/' || *p == '\\' || *p == ':') {
56 p++;
57 break;
58 }
59
60 /* Strip off trailing nonsense. */
61 n = strlen(p);
62 if (n > 4 &&
63 (strcmp(&p[n - 4], ".exe") == 0 || strcmp(&p[n - 4], ".EXE") == 0))
64 n -= 4;
65
66 /* Copy over the name, in lowercase. */
67 if (n > sizeof(prog) - 1)
68 n = sizeof(prog) - 1;
69 for (q = prog, i = 0; i < n; i++, p++)
70 *q++ = tolower((unsigned char)*p);
71 *q = '\0';
72 return prog;
73 }
74
75 #elif defined(OPENSSL_SYS_VMS)
76
77 char *opt_progname(const char *argv0)
78 {
79 const char *p, *q;
80
81 /* Find last special character sys:[foo.bar]openssl */
82 for (p = argv0 + strlen(argv0); --p > argv0;)
83 if (*p == ':' || *p == ']' || *p == '>') {
84 p++;
85 break;
86 }
87
88 q = strrchr(p, '.');
89 strncpy(prog, p, sizeof(prog) - 1);
90 prog[sizeof(prog) - 1] = '\0';
91 if (q != NULL && q - p < sizeof(prog))
92 prog[q - p] = '\0';
93 return prog;
94 }
95
96 #else
97
98 char *opt_progname(const char *argv0)
99 {
100 const char *p;
101
102 /* Could use strchr, but this is like the ones above. */
103 for (p = argv0 + strlen(argv0); --p > argv0;)
104 if (*p == '/') {
105 p++;
106 break;
107 }
108 strncpy(prog, p, sizeof(prog) - 1);
109 prog[sizeof(prog) - 1] = '\0';
110 return prog;
111 }
112 #endif
113
114 char *opt_getprog(void)
115 {
116 return prog;
117 }
118
119 /* Set up the arg parsing. */
120 char *opt_init(int ac, char **av, const OPTIONS *o)
121 {
122 /* Store state. */
123 argc = ac;
124 argv = av;
125 opt_begin();
126 opts = o;
127 opt_progname(av[0]);
128 unknown = NULL;
129
130 for (; o->name; ++o) {
131 #ifndef NDEBUG
132 const OPTIONS *next;
133 int duplicated, i;
134 #endif
135
136 if (o->name == OPT_HELP_STR || o->name == OPT_MORE_STR)
137 continue;
138 #ifndef NDEBUG
139 i = o->valtype;
140
141 /* Make sure options are legit. */
142 OPENSSL_assert(o->name[0] != '-');
143 OPENSSL_assert(o->retval > 0);
144 switch (i) {
145 case 0: case '-': case '/': case '<': case '>': case 'E': case 'F':
146 case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's':
147 case 'u': case 'c':
148 break;
149 default:
150 OPENSSL_assert(0);
151 }
152
153 /* Make sure there are no duplicates. */
154 for (next = o + 1; next->name; ++next) {
155 /*
156 * Some compilers inline strcmp and the assert string is too long.
157 */
158 duplicated = strcmp(o->name, next->name) == 0;
159 OPENSSL_assert(!duplicated);
160 }
161 #endif
162 if (o->name[0] == '\0') {
163 OPENSSL_assert(unknown == NULL);
164 unknown = o;
165 OPENSSL_assert(unknown->valtype == 0 || unknown->valtype == '-');
166 }
167 }
168 return prog;
169 }
170
171 static OPT_PAIR formats[] = {
172 {"PEM/DER", OPT_FMT_PEMDER},
173 {"pkcs12", OPT_FMT_PKCS12},
174 {"smime", OPT_FMT_SMIME},
175 {"engine", OPT_FMT_ENGINE},
176 {"msblob", OPT_FMT_MSBLOB},
177 {"nss", OPT_FMT_NSS},
178 {"text", OPT_FMT_TEXT},
179 {"http", OPT_FMT_HTTP},
180 {"pvk", OPT_FMT_PVK},
181 {NULL}
182 };
183
184 /* Print an error message about a failed format parse. */
185 int opt_format_error(const char *s, unsigned long flags)
186 {
187 OPT_PAIR *ap;
188
189 if (flags == OPT_FMT_PEMDER) {
190 opt_printf_stderr("%s: Bad format \"%s\"; must be pem or der\n",
191 prog, s);
192 } else {
193 opt_printf_stderr("%s: Bad format \"%s\"; must be one of:\n",
194 prog, s);
195 for (ap = formats; ap->name; ap++)
196 if (flags & ap->retval)
197 opt_printf_stderr(" %s\n", ap->name);
198 }
199 return 0;
200 }
201
202 /* Parse a format string, put it into *result; return 0 on failure, else 1. */
203 int opt_format(const char *s, unsigned long flags, int *result)
204 {
205 switch (*s) {
206 default:
207 return 0;
208 case 'D':
209 case 'd':
210 if ((flags & OPT_FMT_PEMDER) == 0)
211 return opt_format_error(s, flags);
212 *result = FORMAT_ASN1;
213 break;
214 case 'T':
215 case 't':
216 if ((flags & OPT_FMT_TEXT) == 0)
217 return opt_format_error(s, flags);
218 *result = FORMAT_TEXT;
219 break;
220 case 'N':
221 case 'n':
222 if ((flags & OPT_FMT_NSS) == 0)
223 return opt_format_error(s, flags);
224 if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0)
225 return opt_format_error(s, flags);
226 *result = FORMAT_NSS;
227 break;
228 case 'S':
229 case 's':
230 if ((flags & OPT_FMT_SMIME) == 0)
231 return opt_format_error(s, flags);
232 *result = FORMAT_SMIME;
233 break;
234 case 'M':
235 case 'm':
236 if ((flags & OPT_FMT_MSBLOB) == 0)
237 return opt_format_error(s, flags);
238 *result = FORMAT_MSBLOB;
239 break;
240 case 'E':
241 case 'e':
242 if ((flags & OPT_FMT_ENGINE) == 0)
243 return opt_format_error(s, flags);
244 *result = FORMAT_ENGINE;
245 break;
246 case 'H':
247 case 'h':
248 if ((flags & OPT_FMT_HTTP) == 0)
249 return opt_format_error(s, flags);
250 *result = FORMAT_HTTP;
251 break;
252 case '1':
253 if ((flags & OPT_FMT_PKCS12) == 0)
254 return opt_format_error(s, flags);
255 *result = FORMAT_PKCS12;
256 break;
257 case 'P':
258 case 'p':
259 if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) {
260 if ((flags & OPT_FMT_PEMDER) == 0)
261 return opt_format_error(s, flags);
262 *result = FORMAT_PEM;
263 } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
264 if ((flags & OPT_FMT_PVK) == 0)
265 return opt_format_error(s, flags);
266 *result = FORMAT_PVK;
267 } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0
268 || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) {
269 if ((flags & OPT_FMT_PKCS12) == 0)
270 return opt_format_error(s, flags);
271 *result = FORMAT_PKCS12;
272 } else {
273 return 0;
274 }
275 break;
276 }
277 return 1;
278 }
279
280 /* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */
281 int opt_cipher(const char *name, const EVP_CIPHER **cipherp)
282 {
283 *cipherp = EVP_get_cipherbyname(name);
284 if (*cipherp != NULL)
285 return 1;
286 opt_printf_stderr("%s: Unrecognized flag %s\n", prog, name);
287 return 0;
288 }
289
290 /*
291 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
292 */
293 int opt_md(const char *name, const EVP_MD **mdp)
294 {
295 *mdp = EVP_get_digestbyname(name);
296 if (*mdp != NULL)
297 return 1;
298 opt_printf_stderr("%s: Unrecognized flag %s\n", prog, name);
299 return 0;
300 }
301
302 /* Look through a list of name/value pairs. */
303 int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
304 {
305 const OPT_PAIR *pp;
306
307 for (pp = pairs; pp->name; pp++)
308 if (strcmp(pp->name, name) == 0) {
309 *result = pp->retval;
310 return 1;
311 }
312 opt_printf_stderr("%s: Value must be one of:\n", prog);
313 for (pp = pairs; pp->name; pp++)
314 opt_printf_stderr("\t%s\n", pp->name);
315 return 0;
316 }
317
318 /* Parse an int, put it into *result; return 0 on failure, else 1. */
319 int opt_int(const char *value, int *result)
320 {
321 long l;
322
323 if (!opt_long(value, &l))
324 return 0;
325 *result = (int)l;
326 if (*result != l) {
327 opt_printf_stderr("%s: Value \"%s\" outside integer range\n",
328 prog, value);
329 return 0;
330 }
331 return 1;
332 }
333
334 static void opt_number_error(const char *v)
335 {
336 size_t i = 0;
337 struct strstr_pair_st {
338 char *prefix;
339 char *name;
340 } b[] = {
341 {"0x", "a hexadecimal"},
342 {"0X", "a hexadecimal"},
343 {"0", "an octal"}
344 };
345
346 for (i = 0; i < OSSL_NELEM(b); i++) {
347 if (strncmp(v, b[i].prefix, strlen(b[i].prefix)) == 0) {
348 opt_printf_stderr("%s: Can't parse \"%s\" as %s number\n",
349 prog, v, b[i].name);
350 return;
351 }
352 }
353 opt_printf_stderr("%s: Can't parse \"%s\" as a number\n", prog, v);
354 return;
355 }
356
357 /* Parse a long, put it into *result; return 0 on failure, else 1. */
358 int opt_long(const char *value, long *result)
359 {
360 int oerrno = errno;
361 long l;
362 char *endp;
363
364 errno = 0;
365 l = strtol(value, &endp, 0);
366 if (*endp
367 || endp == value
368 || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
369 || (l == 0 && errno != 0)) {
370 opt_number_error(value);
371 errno = oerrno;
372 return 0;
373 }
374 *result = l;
375 errno = oerrno;
376 return 1;
377 }
378
379 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
380 defined(INTMAX_MAX) && defined(UINTMAX_MAX) && \
381 !defined(OPENSSL_NO_INTTYPES_H)
382
383 /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
384 int opt_imax(const char *value, intmax_t *result)
385 {
386 int oerrno = errno;
387 intmax_t m;
388 char *endp;
389
390 errno = 0;
391 m = strtoimax(value, &endp, 0);
392 if (*endp
393 || endp == value
394 || ((m == INTMAX_MAX || m == INTMAX_MIN) && errno == ERANGE)
395 || (m == 0 && errno != 0)) {
396 opt_number_error(value);
397 errno = oerrno;
398 return 0;
399 }
400 *result = m;
401 errno = oerrno;
402 return 1;
403 }
404
405 /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
406 int opt_umax(const char *value, uintmax_t *result)
407 {
408 int oerrno = errno;
409 uintmax_t m;
410 char *endp;
411
412 errno = 0;
413 m = strtoumax(value, &endp, 0);
414 if (*endp
415 || endp == value
416 || (m == UINTMAX_MAX && errno == ERANGE)
417 || (m == 0 && errno != 0)) {
418 opt_number_error(value);
419 errno = oerrno;
420 return 0;
421 }
422 *result = m;
423 errno = oerrno;
424 return 1;
425 }
426 #endif
427
428 /*
429 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
430 */
431 int opt_ulong(const char *value, unsigned long *result)
432 {
433 int oerrno = errno;
434 char *endptr;
435 unsigned long l;
436
437 errno = 0;
438 l = strtoul(value, &endptr, 0);
439 if (*endptr
440 || endptr == value
441 || ((l == ULONG_MAX) && errno == ERANGE)
442 || (l == 0 && errno != 0)) {
443 opt_number_error(value);
444 errno = oerrno;
445 return 0;
446 }
447 *result = l;
448 errno = oerrno;
449 return 1;
450 }
451
452 /*
453 * We pass opt as an int but cast it to "enum range" so that all the
454 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
455 * in gcc do the right thing.
456 */
457 enum range { OPT_V_ENUM };
458
459 int opt_verify(int opt, X509_VERIFY_PARAM *vpm)
460 {
461 int i;
462 ossl_intmax_t t = 0;
463 ASN1_OBJECT *otmp;
464 X509_PURPOSE *xptmp;
465 const X509_VERIFY_PARAM *vtmp;
466
467 OPENSSL_assert(vpm != NULL);
468 OPENSSL_assert(opt > OPT_V__FIRST);
469 OPENSSL_assert(opt < OPT_V__LAST);
470
471 switch ((enum range)opt) {
472 case OPT_V__FIRST:
473 case OPT_V__LAST:
474 return 0;
475 case OPT_V_POLICY:
476 otmp = OBJ_txt2obj(opt_arg(), 0);
477 if (otmp == NULL) {
478 opt_printf_stderr("%s: Invalid Policy %s\n", prog, opt_arg());
479 return 0;
480 }
481 X509_VERIFY_PARAM_add0_policy(vpm, otmp);
482 break;
483 case OPT_V_PURPOSE:
484 /* purpose name -> purpose index */
485 i = X509_PURPOSE_get_by_sname(opt_arg());
486 if (i < 0) {
487 opt_printf_stderr("%s: Invalid purpose %s\n", prog, opt_arg());
488 return 0;
489 }
490
491 /* purpose index -> purpose object */
492 xptmp = X509_PURPOSE_get0(i);
493
494 /* purpose object -> purpose value */
495 i = X509_PURPOSE_get_id(xptmp);
496
497 if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {
498 opt_printf_stderr("%s: Internal error setting purpose %s\n",
499 prog, opt_arg());
500 return 0;
501 }
502 break;
503 case OPT_V_VERIFY_NAME:
504 vtmp = X509_VERIFY_PARAM_lookup(opt_arg());
505 if (vtmp == NULL) {
506 opt_printf_stderr("%s: Invalid verify name %s\n",
507 prog, opt_arg());
508 return 0;
509 }
510 X509_VERIFY_PARAM_set1(vpm, vtmp);
511 break;
512 case OPT_V_VERIFY_DEPTH:
513 i = atoi(opt_arg());
514 if (i >= 0)
515 X509_VERIFY_PARAM_set_depth(vpm, i);
516 break;
517 case OPT_V_VERIFY_AUTH_LEVEL:
518 i = atoi(opt_arg());
519 if (i >= 0)
520 X509_VERIFY_PARAM_set_auth_level(vpm, i);
521 break;
522 case OPT_V_ATTIME:
523 if (!opt_imax(opt_arg(), &t))
524 return 0;
525 if (t != (time_t)t) {
526 opt_printf_stderr("%s: epoch time out of range %s\n",
527 prog, opt_arg());
528 return 0;
529 }
530 X509_VERIFY_PARAM_set_time(vpm, (time_t)t);
531 break;
532 case OPT_V_VERIFY_HOSTNAME:
533 if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))
534 return 0;
535 break;
536 case OPT_V_VERIFY_EMAIL:
537 if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))
538 return 0;
539 break;
540 case OPT_V_VERIFY_IP:
541 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))
542 return 0;
543 break;
544 case OPT_V_IGNORE_CRITICAL:
545 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);
546 break;
547 case OPT_V_ISSUER_CHECKS:
548 /* NOP, deprecated */
549 break;
550 case OPT_V_CRL_CHECK:
551 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);
552 break;
553 case OPT_V_CRL_CHECK_ALL:
554 X509_VERIFY_PARAM_set_flags(vpm,
555 X509_V_FLAG_CRL_CHECK |
556 X509_V_FLAG_CRL_CHECK_ALL);
557 break;
558 case OPT_V_POLICY_CHECK:
559 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);
560 break;
561 case OPT_V_EXPLICIT_POLICY:
562 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);
563 break;
564 case OPT_V_INHIBIT_ANY:
565 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);
566 break;
567 case OPT_V_INHIBIT_MAP:
568 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);
569 break;
570 case OPT_V_X509_STRICT:
571 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);
572 break;
573 case OPT_V_EXTENDED_CRL:
574 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);
575 break;
576 case OPT_V_USE_DELTAS:
577 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);
578 break;
579 case OPT_V_POLICY_PRINT:
580 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);
581 break;
582 case OPT_V_CHECK_SS_SIG:
583 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);
584 break;
585 case OPT_V_TRUSTED_FIRST:
586 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);
587 break;
588 case OPT_V_SUITEB_128_ONLY:
589 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);
590 break;
591 case OPT_V_SUITEB_128:
592 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);
593 break;
594 case OPT_V_SUITEB_192:
595 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);
596 break;
597 case OPT_V_PARTIAL_CHAIN:
598 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
599 break;
600 case OPT_V_NO_ALT_CHAINS:
601 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);
602 break;
603 case OPT_V_NO_CHECK_TIME:
604 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);
605 break;
606 case OPT_V_ALLOW_PROXY_CERTS:
607 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS);
608 break;
609 }
610 return 1;
611
612 }
613
614 void opt_begin(void)
615 {
616 opt_index = 1;
617 arg = NULL;
618 flag = NULL;
619 }
620
621 /*
622 * Parse the next flag (and value if specified), return 0 if done, -1 on
623 * error, otherwise the flag's retval.
624 */
625 int opt_next(void)
626 {
627 char *p;
628 const OPTIONS *o;
629 int ival;
630 long lval;
631 unsigned long ulval;
632 ossl_intmax_t imval;
633 ossl_uintmax_t umval;
634
635 /* Look at current arg; at end of the list? */
636 arg = NULL;
637 p = argv[opt_index];
638 if (p == NULL)
639 return 0;
640
641 /* If word doesn't start with a -, we're done. */
642 if (*p != '-')
643 return 0;
644
645 /* Hit "--" ? We're done. */
646 opt_index++;
647 if (strcmp(p, "--") == 0)
648 return 0;
649
650 /* Allow -nnn and --nnn */
651 if (*++p == '-')
652 p++;
653 flag = p - 1;
654
655 /* If we have --flag=foo, snip it off */
656 if ((arg = strchr(p, '=')) != NULL)
657 *arg++ = '\0';
658 for (o = opts; o->name; ++o) {
659 /* If not this option, move on to the next one. */
660 if (strcmp(p, o->name) != 0)
661 continue;
662
663 /* If it doesn't take a value, make sure none was given. */
664 if (o->valtype == 0 || o->valtype == '-') {
665 if (arg) {
666 opt_printf_stderr("%s: Option -%s does not take a value\n",
667 prog, p);
668 return -1;
669 }
670 return o->retval;
671 }
672
673 /* Want a value; get the next param if =foo not used. */
674 if (arg == NULL) {
675 if (argv[opt_index] == NULL) {
676 opt_printf_stderr("%s: Option -%s needs a value\n",
677 prog, o->name);
678 return -1;
679 }
680 arg = argv[opt_index++];
681 }
682
683 /* Syntax-check value. */
684 switch (o->valtype) {
685 default:
686 case 's':
687 /* Just a string. */
688 break;
689 case '/':
690 if (opt_isdir(arg) > 0)
691 break;
692 opt_printf_stderr("%s: Not a directory: %s\n", prog, arg);
693 return -1;
694 case '<':
695 /* Input file. */
696 break;
697 case '>':
698 /* Output file. */
699 break;
700 case 'p':
701 case 'n':
702 if (!opt_int(arg, &ival)
703 || (o->valtype == 'p' && ival <= 0)) {
704 opt_printf_stderr("%s: Non-positive number \"%s\" for -%s\n",
705 prog, arg, o->name);
706 return -1;
707 }
708 break;
709 case 'M':
710 if (!opt_imax(arg, &imval)) {
711 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
712 prog, arg, o->name);
713 return -1;
714 }
715 break;
716 case 'U':
717 if (!opt_umax(arg, &umval)) {
718 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
719 prog, arg, o->name);
720 return -1;
721 }
722 break;
723 case 'l':
724 if (!opt_long(arg, &lval)) {
725 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
726 prog, arg, o->name);
727 return -1;
728 }
729 break;
730 case 'u':
731 if (!opt_ulong(arg, &ulval)) {
732 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
733 prog, arg, o->name);
734 return -1;
735 }
736 break;
737 case 'c':
738 case 'E':
739 case 'F':
740 case 'f':
741 if (opt_format(arg,
742 o->valtype == 'c' ? OPT_FMT_PDS :
743 o->valtype == 'E' ? OPT_FMT_PDE :
744 o->valtype == 'F' ? OPT_FMT_PEMDER
745 : OPT_FMT_ANY, &ival))
746 break;
747 opt_printf_stderr("%s: Invalid format \"%s\" for -%s\n",
748 prog, arg, o->name);
749 return -1;
750 }
751
752 /* Return the flag value. */
753 return o->retval;
754 }
755 if (unknown != NULL) {
756 dunno = p;
757 return unknown->retval;
758 }
759 opt_printf_stderr("%s: Option unknown option -%s\n", prog, p);
760 return -1;
761 }
762
763 /* Return the most recent flag parameter. */
764 char *opt_arg(void)
765 {
766 return arg;
767 }
768
769 /* Return the most recent flag. */
770 char *opt_flag(void)
771 {
772 return flag;
773 }
774
775 /* Return the unknown option. */
776 char *opt_unknown(void)
777 {
778 return dunno;
779 }
780
781 /* Return the rest of the arguments after parsing flags. */
782 char **opt_rest(void)
783 {
784 return &argv[opt_index];
785 }
786
787 /* How many items in remaining args? */
788 int opt_num_rest(void)
789 {
790 int i = 0;
791 char **pp;
792
793 for (pp = opt_rest(); *pp; pp++, i++)
794 continue;
795 return i;
796 }
797
798 /* Return a string describing the parameter type. */
799 static const char *valtype2param(const OPTIONS *o)
800 {
801 switch (o->valtype) {
802 case 0:
803 case '-':
804 return "";
805 case 's':
806 return "val";
807 case '/':
808 return "dir";
809 case '<':
810 return "infile";
811 case '>':
812 return "outfile";
813 case 'p':
814 return "+int";
815 case 'n':
816 return "int";
817 case 'l':
818 return "long";
819 case 'u':
820 return "ulong";
821 case 'E':
822 return "PEM|DER|ENGINE";
823 case 'F':
824 return "PEM|DER";
825 case 'f':
826 return "format";
827 case 'M':
828 return "intmax";
829 case 'U':
830 return "uintmax";
831 }
832 return "parm";
833 }
834
835 void opt_help(const OPTIONS *list)
836 {
837 const OPTIONS *o;
838 int i;
839 int standard_prolog;
840 int width = 5;
841 char start[80 + 1];
842 char *p;
843 const char *help;
844
845 /* Starts with its own help message? */
846 standard_prolog = list[0].name != OPT_HELP_STR;
847
848 /* Find the widest help. */
849 for (o = list; o->name; o++) {
850 if (o->name == OPT_MORE_STR)
851 continue;
852 i = 2 + (int)strlen(o->name);
853 if (o->valtype != '-')
854 i += 1 + strlen(valtype2param(o));
855 if (i < MAX_OPT_HELP_WIDTH && i > width)
856 width = i;
857 OPENSSL_assert(i < (int)sizeof(start));
858 }
859
860 if (standard_prolog)
861 opt_printf_stderr("Usage: %s [options]\nValid options are:\n", prog);
862
863 /* Now let's print. */
864 for (o = list; o->name; o++) {
865 help = o->helpstr ? o->helpstr : "(No additional info)";
866 if (o->name == OPT_HELP_STR) {
867 opt_printf_stderr(help, prog);
868 continue;
869 }
870
871 /* Pad out prefix */
872 memset(start, ' ', sizeof(start) - 1);
873 start[sizeof(start) - 1] = '\0';
874
875 if (o->name == OPT_MORE_STR) {
876 /* Continuation of previous line; pad and print. */
877 start[width] = '\0';
878 opt_printf_stderr("%s %s\n", start, help);
879 continue;
880 }
881
882 /* Build up the "-flag [param]" part. */
883 p = start;
884 *p++ = ' ';
885 *p++ = '-';
886 if (o->name[0])
887 p += strlen(strcpy(p, o->name));
888 else
889 *p++ = '*';
890 if (o->valtype != '-') {
891 *p++ = ' ';
892 p += strlen(strcpy(p, valtype2param(o)));
893 }
894 *p = ' ';
895 if ((int)(p - start) >= MAX_OPT_HELP_WIDTH) {
896 *p = '\0';
897 opt_printf_stderr("%s\n", start);
898 memset(start, ' ', sizeof(start));
899 }
900 start[width] = '\0';
901 opt_printf_stderr("%s %s\n", start, help);
902 }
903 }
904
905 /* opt_isdir section */
906 #ifdef _WIN32
907 # include <windows.h>
908 int opt_isdir(const char *name)
909 {
910 DWORD attr;
911 # if defined(UNICODE) || defined(_UNICODE)
912 size_t i, len_0 = strlen(name) + 1;
913 WCHAR tempname[MAX_PATH];
914
915 if (len_0 > MAX_PATH)
916 return -1;
917
918 # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
919 if (!MultiByteToWideChar(CP_ACP, 0, name, len_0, tempname, MAX_PATH))
920 # endif
921 for (i = 0; i < len_0; i++)
922 tempname[i] = (WCHAR)name[i];
923
924 attr = GetFileAttributes(tempname);
925 # else
926 attr = GetFileAttributes(name);
927 # endif
928 if (attr == INVALID_FILE_ATTRIBUTES)
929 return -1;
930 return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0);
931 }
932 #else
933 # include <sys/stat.h>
934 # ifndef S_ISDIR
935 # if defined(_S_IFMT) && defined(_S_IFDIR)
936 # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
937 # else
938 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
939 # endif
940 # endif
941
942 int opt_isdir(const char *name)
943 {
944 # if defined(S_ISDIR)
945 struct stat st;
946
947 if (stat(name, &st) == 0)
948 return S_ISDIR(st.st_mode);
949 else
950 return -1;
951 # else
952 return -1;
953 # endif
954 }
955 #endif