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