| 1 |
/* |
| 2 |
* Copyright 2015-2025 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 "app_libctx.h" |
| 16 |
#include "internal/nelem.h" |
| 17 |
#include "internal/numbers.h" |
| 18 |
#include <string.h> |
| 19 |
#if !defined(OPENSSL_SYS_MSDOS) |
| 20 |
#include <unistd.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <errno.h> |
| 25 |
#include <ctype.h> |
| 26 |
#include <limits.h> |
| 27 |
#include <openssl/err.h> |
| 28 |
#include <openssl/bio.h> |
| 29 |
#include <openssl/x509v3.h> |
| 30 |
|
| 31 |
#define MAX_OPT_HELP_WIDTH 30 |
| 32 |
const char OPT_HELP_STR[] = "-H"; |
| 33 |
const char OPT_MORE_STR[] = "-M"; |
| 34 |
const char OPT_SECTION_STR[] = "-S"; |
| 35 |
const char OPT_PARAM_STR[] = "-P"; |
| 36 |
|
| 37 |
/* Our state */ |
| 38 |
static char **argv; |
| 39 |
static int argc; |
| 40 |
static int opt_index; |
| 41 |
static char *arg; |
| 42 |
static char *flag; |
| 43 |
static char *dunno; |
| 44 |
static const char *unknown_name; |
| 45 |
static const OPTIONS *unknown; |
| 46 |
static const OPTIONS *opts; |
| 47 |
static char prog[40]; |
| 48 |
|
| 49 |
/* |
| 50 |
* Return the simple name of the program; removing various platform gunk. |
| 51 |
*/ |
| 52 |
#if defined(OPENSSL_SYS_WIN32) |
| 53 |
|
| 54 |
const char *opt_path_end(const char *filename) |
| 55 |
{ |
| 56 |
const char *p; |
| 57 |
|
| 58 |
/* find the last '/', '\' or ':' */ |
| 59 |
for (p = filename + strlen(filename); --p > filename;) |
| 60 |
if (*p == '/' || *p == '\\' || *p == ':') { |
| 61 |
p++; |
| 62 |
break; |
| 63 |
} |
| 64 |
return p; |
| 65 |
} |
| 66 |
|
| 67 |
char *opt_progname(const char *argv0) |
| 68 |
{ |
| 69 |
size_t i, n; |
| 70 |
const char *p; |
| 71 |
char *q; |
| 72 |
|
| 73 |
p = opt_path_end(argv0); |
| 74 |
|
| 75 |
/* Strip off trailing nonsense. */ |
| 76 |
n = strlen(p); |
| 77 |
if (n > 4 && (strcmp(&p[n - 4], ".exe") == 0 || strcmp(&p[n - 4], ".EXE") == 0)) |
| 78 |
n -= 4; |
| 79 |
|
| 80 |
/* Copy over the name, in lowercase. */ |
| 81 |
if (n > sizeof(prog) - 1) |
| 82 |
n = sizeof(prog) - 1; |
| 83 |
for (q = prog, i = 0; i < n; i++, p++) |
| 84 |
*q++ = tolower((unsigned char)*p); |
| 85 |
*q = '\0'; |
| 86 |
return prog; |
| 87 |
} |
| 88 |
|
| 89 |
#elif defined(OPENSSL_SYS_VMS) |
| 90 |
|
| 91 |
const char *opt_path_end(const char *filename) |
| 92 |
{ |
| 93 |
const char *p; |
| 94 |
|
| 95 |
/* Find last special character sys:[foo.bar]openssl */ |
| 96 |
for (p = filename + strlen(filename); --p > filename;) |
| 97 |
if (*p == ':' || *p == ']' || *p == '>') { |
| 98 |
p++; |
| 99 |
break; |
| 100 |
} |
| 101 |
return p; |
| 102 |
} |
| 103 |
|
| 104 |
char *opt_progname(const char *argv0) |
| 105 |
{ |
| 106 |
const char *p, *q; |
| 107 |
|
| 108 |
/* Find last special character sys:[foo.bar]openssl */ |
| 109 |
p = opt_path_end(argv0); |
| 110 |
q = strrchr(p, '.'); |
| 111 |
if (prog != p) |
| 112 |
strncpy(prog, p, sizeof(prog) - 1); |
| 113 |
prog[sizeof(prog) - 1] = '\0'; |
| 114 |
if (q != NULL && q - p < sizeof(prog)) |
| 115 |
prog[q - p] = '\0'; |
| 116 |
return prog; |
| 117 |
} |
| 118 |
|
| 119 |
#else |
| 120 |
|
| 121 |
const char *opt_path_end(const char *filename) |
| 122 |
{ |
| 123 |
const char *p; |
| 124 |
|
| 125 |
/* Could use strchr, but this is like the ones above. */ |
| 126 |
for (p = filename + strlen(filename); --p > filename;) |
| 127 |
if (*p == '/') { |
| 128 |
p++; |
| 129 |
break; |
| 130 |
} |
| 131 |
return p; |
| 132 |
} |
| 133 |
|
| 134 |
char *opt_progname(const char *argv0) |
| 135 |
{ |
| 136 |
const char *p; |
| 137 |
|
| 138 |
p = opt_path_end(argv0); |
| 139 |
if (prog != p) |
| 140 |
strncpy(prog, p, sizeof(prog) - 1); |
| 141 |
prog[sizeof(prog) - 1] = '\0'; |
| 142 |
return prog; |
| 143 |
} |
| 144 |
#endif |
| 145 |
|
| 146 |
char *opt_appname(const char *argv0) |
| 147 |
{ |
| 148 |
size_t len = strlen(prog); |
| 149 |
|
| 150 |
if (argv0 != NULL) |
| 151 |
BIO_snprintf(prog + len, sizeof(prog) - len - 1, " %s", argv0); |
| 152 |
return prog; |
| 153 |
} |
| 154 |
|
| 155 |
char *opt_getprog(void) |
| 156 |
{ |
| 157 |
return prog; |
| 158 |
} |
| 159 |
|
| 160 |
/* Set up the arg parsing. */ |
| 161 |
char *opt_init(int ac, char **av, const OPTIONS *o) |
| 162 |
{ |
| 163 |
/* Store state. */ |
| 164 |
argc = ac; |
| 165 |
argv = av; |
| 166 |
opt_begin(); |
| 167 |
opts = o; |
| 168 |
unknown = NULL; |
| 169 |
/* Make sure prog name is set for usage output */ |
| 170 |
(void)opt_progname(argv[0]); |
| 171 |
|
| 172 |
/* Check all options up until the PARAM marker (if present) */ |
| 173 |
for (; o->name != NULL && o->name != OPT_PARAM_STR; ++o) { |
| 174 |
#ifndef NDEBUG |
| 175 |
const OPTIONS *next; |
| 176 |
int duplicated, i; |
| 177 |
#endif |
| 178 |
|
| 179 |
if (o->name == OPT_HELP_STR |
| 180 |
|| o->name == OPT_MORE_STR |
| 181 |
|| o->name == OPT_SECTION_STR) |
| 182 |
continue; |
| 183 |
#ifndef NDEBUG |
| 184 |
i = o->valtype; |
| 185 |
|
| 186 |
/* Make sure options are legit. */ |
| 187 |
OPENSSL_assert(o->name[0] != '-'); |
| 188 |
if (o->valtype == '.') |
| 189 |
OPENSSL_assert(o->retval == OPT_PARAM); |
| 190 |
else |
| 191 |
OPENSSL_assert(o->retval == OPT_DUP || o->retval > OPT_PARAM); |
| 192 |
switch (i) { |
| 193 |
case 0: |
| 194 |
case '-': |
| 195 |
case '.': |
| 196 |
case '/': |
| 197 |
case '<': |
| 198 |
case '>': |
| 199 |
case 'E': |
| 200 |
case 'F': |
| 201 |
case 'M': |
| 202 |
case 'U': |
| 203 |
case 'f': |
| 204 |
case 'l': |
| 205 |
case 'n': |
| 206 |
case 'p': |
| 207 |
case 's': |
| 208 |
case 'u': |
| 209 |
case 'c': |
| 210 |
case ':': |
| 211 |
case 'N': |
| 212 |
case 'A': |
| 213 |
break; |
| 214 |
default: |
| 215 |
OPENSSL_assert(0); |
| 216 |
} |
| 217 |
|
| 218 |
/* Make sure there are no duplicates. */ |
| 219 |
for (next = o + 1; next->name; ++next) { |
| 220 |
/* |
| 221 |
* Some compilers inline strcmp and the assert string is too long. |
| 222 |
*/ |
| 223 |
duplicated = next->retval != OPT_DUP |
| 224 |
&& strcmp(o->name, next->name) == 0; |
| 225 |
if (duplicated) { |
| 226 |
opt_printf_stderr("%s: Internal error: duplicate option %s\n", |
| 227 |
prog, o->name); |
| 228 |
OPENSSL_assert(!duplicated); |
| 229 |
} |
| 230 |
} |
| 231 |
#endif |
| 232 |
if (o->name[0] == '\0') { |
| 233 |
OPENSSL_assert(unknown_name != NULL); |
| 234 |
OPENSSL_assert(unknown == NULL); |
| 235 |
unknown = o; |
| 236 |
OPENSSL_assert(unknown->valtype == 0 || unknown->valtype == '-'); |
| 237 |
} |
| 238 |
} |
| 239 |
return prog; |
| 240 |
} |
| 241 |
|
| 242 |
static OPT_PAIR formats[] = { |
| 243 |
{ "pem", OPT_FMT_PEM }, |
| 244 |
{ "der", OPT_FMT_DER }, |
| 245 |
{ "b64", OPT_FMT_B64 }, |
| 246 |
{ "pkcs12", OPT_FMT_PKCS12 }, |
| 247 |
{ "smime", OPT_FMT_SMIME }, |
| 248 |
{ "engine", OPT_FMT_ENGINE }, |
| 249 |
{ "msblob", OPT_FMT_MSBLOB }, |
| 250 |
{ "nss", OPT_FMT_NSS }, |
| 251 |
{ "text", OPT_FMT_TEXT }, |
| 252 |
{ "http", OPT_FMT_HTTP }, |
| 253 |
{ "pvk", OPT_FMT_PVK }, |
| 254 |
{ NULL } |
| 255 |
}; |
| 256 |
|
| 257 |
void opt_set_unknown_name(const char *name) |
| 258 |
{ |
| 259 |
unknown_name = name; |
| 260 |
} |
| 261 |
|
| 262 |
/* Print an error message about a failed format parse. */ |
| 263 |
static int opt_format_error(const char *s, unsigned long flags) |
| 264 |
{ |
| 265 |
OPT_PAIR *ap; |
| 266 |
|
| 267 |
opt_printf_stderr("%s: Bad format \"%s\"; must be one of: ", prog, s); |
| 268 |
for (ap = formats; ap->name; ap++) |
| 269 |
if (flags & ap->retval) |
| 270 |
opt_printf_stderr(" %s", ap->name); |
| 271 |
opt_printf_stderr("\n"); |
| 272 |
|
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
/* Parse a format string, put it into *result; return 0 on failure, else 1. */ |
| 277 |
int opt_format(const char *s, unsigned long flags, int *result) |
| 278 |
{ |
| 279 |
switch (*s) { |
| 280 |
default: |
| 281 |
opt_printf_stderr("%s: Bad format \"%s\"\n", prog, s); |
| 282 |
return 0; |
| 283 |
case 'B': |
| 284 |
case 'b': |
| 285 |
if (s[1] == '\0' |
| 286 |
|| strcmp(s, "B64") == 0 || strcmp(s, "b64") == 0 |
| 287 |
|| strcmp(s, "BASE64") == 0 || strcmp(s, "base64") == 0) { |
| 288 |
if ((flags & OPT_FMT_B64) == 0) |
| 289 |
return opt_format_error(s, flags); |
| 290 |
*result = FORMAT_BASE64; |
| 291 |
} else { |
| 292 |
return 0; |
| 293 |
} |
| 294 |
break; |
| 295 |
case 'D': |
| 296 |
case 'd': |
| 297 |
if ((flags & OPT_FMT_DER) == 0) |
| 298 |
return opt_format_error(s, flags); |
| 299 |
*result = FORMAT_ASN1; |
| 300 |
break; |
| 301 |
case 'T': |
| 302 |
case 't': |
| 303 |
if ((flags & OPT_FMT_TEXT) == 0) |
| 304 |
return opt_format_error(s, flags); |
| 305 |
*result = FORMAT_TEXT; |
| 306 |
break; |
| 307 |
case 'N': |
| 308 |
case 'n': |
| 309 |
if ((flags & OPT_FMT_NSS) == 0) |
| 310 |
return opt_format_error(s, flags); |
| 311 |
if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0) |
| 312 |
return opt_format_error(s, flags); |
| 313 |
*result = FORMAT_NSS; |
| 314 |
break; |
| 315 |
case 'S': |
| 316 |
case 's': |
| 317 |
if ((flags & OPT_FMT_SMIME) == 0) |
| 318 |
return opt_format_error(s, flags); |
| 319 |
*result = FORMAT_SMIME; |
| 320 |
break; |
| 321 |
case 'M': |
| 322 |
case 'm': |
| 323 |
if ((flags & OPT_FMT_MSBLOB) == 0) |
| 324 |
return opt_format_error(s, flags); |
| 325 |
*result = FORMAT_MSBLOB; |
| 326 |
break; |
| 327 |
case 'E': |
| 328 |
case 'e': |
| 329 |
if ((flags & OPT_FMT_ENGINE) == 0) |
| 330 |
return opt_format_error(s, flags); |
| 331 |
*result = FORMAT_ENGINE; |
| 332 |
break; |
| 333 |
case 'H': |
| 334 |
case 'h': |
| 335 |
if ((flags & OPT_FMT_HTTP) == 0) |
| 336 |
return opt_format_error(s, flags); |
| 337 |
*result = FORMAT_HTTP; |
| 338 |
break; |
| 339 |
case '1': |
| 340 |
if ((flags & OPT_FMT_PKCS12) == 0) |
| 341 |
return opt_format_error(s, flags); |
| 342 |
*result = FORMAT_PKCS12; |
| 343 |
break; |
| 344 |
case 'P': |
| 345 |
case 'p': |
| 346 |
if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) { |
| 347 |
if ((flags & OPT_FMT_PEM) == 0) |
| 348 |
return opt_format_error(s, flags); |
| 349 |
*result = FORMAT_PEM; |
| 350 |
} else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) { |
| 351 |
if ((flags & OPT_FMT_PVK) == 0) |
| 352 |
return opt_format_error(s, flags); |
| 353 |
*result = FORMAT_PVK; |
| 354 |
} else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0 |
| 355 |
|| strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) { |
| 356 |
if ((flags & OPT_FMT_PKCS12) == 0) |
| 357 |
return opt_format_error(s, flags); |
| 358 |
*result = FORMAT_PKCS12; |
| 359 |
} else { |
| 360 |
opt_printf_stderr("%s: Bad format \"%s\"\n", prog, s); |
| 361 |
return 0; |
| 362 |
} |
| 363 |
break; |
| 364 |
} |
| 365 |
return 1; |
| 366 |
} |
| 367 |
|
| 368 |
/* Return string representing the given format. */ |
| 369 |
static const char *format2str(int format) |
| 370 |
{ |
| 371 |
switch (format) { |
| 372 |
default: |
| 373 |
return "(undefined)"; |
| 374 |
case FORMAT_PEM: |
| 375 |
return "PEM"; |
| 376 |
case FORMAT_ASN1: |
| 377 |
return "DER"; |
| 378 |
case FORMAT_TEXT: |
| 379 |
return "TEXT"; |
| 380 |
case FORMAT_NSS: |
| 381 |
return "NSS"; |
| 382 |
case FORMAT_SMIME: |
| 383 |
return "SMIME"; |
| 384 |
case FORMAT_MSBLOB: |
| 385 |
return "MSBLOB"; |
| 386 |
case FORMAT_ENGINE: |
| 387 |
return "ENGINE"; |
| 388 |
case FORMAT_HTTP: |
| 389 |
return "HTTP"; |
| 390 |
case FORMAT_PKCS12: |
| 391 |
return "P12"; |
| 392 |
case FORMAT_PVK: |
| 393 |
return "PVK"; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/* Print an error message about unsuitable/unsupported format requested. */ |
| 398 |
void print_format_error(int format, unsigned long flags) |
| 399 |
{ |
| 400 |
(void)opt_format_error(format2str(format), flags); |
| 401 |
} |
| 402 |
|
| 403 |
/* |
| 404 |
* Parse a cipher name, put it in *cipherp after freeing what was there, if |
| 405 |
* cipherp is not NULL. Return 0 on failure, else 1. |
| 406 |
*/ |
| 407 |
int opt_cipher_silent(const char *name, EVP_CIPHER **cipherp) |
| 408 |
{ |
| 409 |
EVP_CIPHER *c; |
| 410 |
|
| 411 |
ERR_set_mark(); |
| 412 |
if ((c = EVP_CIPHER_fetch(app_get0_libctx(), name, |
| 413 |
app_get0_propq())) |
| 414 |
!= NULL |
| 415 |
|| (opt_legacy_okay() |
| 416 |
&& (c = (EVP_CIPHER *)EVP_get_cipherbyname(name)) != NULL)) { |
| 417 |
ERR_pop_to_mark(); |
| 418 |
if (cipherp != NULL) { |
| 419 |
EVP_CIPHER_free(*cipherp); |
| 420 |
*cipherp = c; |
| 421 |
} else { |
| 422 |
EVP_CIPHER_free(c); |
| 423 |
} |
| 424 |
return 1; |
| 425 |
} |
| 426 |
ERR_clear_last_mark(); |
| 427 |
return 0; |
| 428 |
} |
| 429 |
|
| 430 |
int opt_cipher_any(const char *name, EVP_CIPHER **cipherp) |
| 431 |
{ |
| 432 |
int ret; |
| 433 |
|
| 434 |
if (name == NULL) |
| 435 |
return 1; |
| 436 |
if ((ret = opt_cipher_silent(name, cipherp)) == 0) |
| 437 |
opt_printf_stderr("%s: Unknown option or cipher: %s\n", prog, name); |
| 438 |
return ret; |
| 439 |
} |
| 440 |
|
| 441 |
int opt_cipher(const char *name, EVP_CIPHER **cipherp) |
| 442 |
{ |
| 443 |
int mode, ret = 0; |
| 444 |
unsigned long int flags; |
| 445 |
EVP_CIPHER *c = NULL; |
| 446 |
|
| 447 |
if (name == NULL) |
| 448 |
return 1; |
| 449 |
if (opt_cipher_any(name, &c)) { |
| 450 |
mode = EVP_CIPHER_get_mode(c); |
| 451 |
flags = EVP_CIPHER_get_flags(c); |
| 452 |
if (mode == EVP_CIPH_XTS_MODE) { |
| 453 |
opt_printf_stderr("%s XTS ciphers not supported\n", prog); |
| 454 |
} else if ((flags & EVP_CIPH_FLAG_AEAD_CIPHER) != 0) { |
| 455 |
opt_printf_stderr("%s: AEAD ciphers not supported\n", prog); |
| 456 |
} else if ((flags & EVP_CIPH_FLAG_ENC_THEN_MAC) != 0) { |
| 457 |
opt_printf_stderr("%s: ENC-then-MAC cipher not supported\n", prog); |
| 458 |
} else { |
| 459 |
ret = 1; |
| 460 |
if (cipherp != NULL) |
| 461 |
*cipherp = c; |
| 462 |
} |
| 463 |
} |
| 464 |
return ret; |
| 465 |
} |
| 466 |
|
| 467 |
/* |
| 468 |
* Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1. |
| 469 |
*/ |
| 470 |
int opt_md_silent(const char *name, EVP_MD **mdp) |
| 471 |
{ |
| 472 |
EVP_MD *md; |
| 473 |
|
| 474 |
ERR_set_mark(); |
| 475 |
if ((md = EVP_MD_fetch(app_get0_libctx(), name, app_get0_propq())) != NULL |
| 476 |
|| (opt_legacy_okay() |
| 477 |
&& (md = (EVP_MD *)EVP_get_digestbyname(name)) != NULL)) { |
| 478 |
ERR_pop_to_mark(); |
| 479 |
if (mdp != NULL) { |
| 480 |
EVP_MD_free(*mdp); |
| 481 |
*mdp = md; |
| 482 |
} else { |
| 483 |
EVP_MD_free(md); |
| 484 |
} |
| 485 |
return 1; |
| 486 |
} |
| 487 |
ERR_clear_last_mark(); |
| 488 |
return 0; |
| 489 |
} |
| 490 |
|
| 491 |
int opt_md(const char *name, EVP_MD **mdp) |
| 492 |
{ |
| 493 |
int ret; |
| 494 |
|
| 495 |
if (name == NULL) |
| 496 |
return 1; |
| 497 |
if ((ret = opt_md_silent(name, mdp)) == 0) |
| 498 |
opt_printf_stderr("%s: Unknown option or message digest: %s\n", |
| 499 |
prog, name); |
| 500 |
return ret; |
| 501 |
} |
| 502 |
|
| 503 |
int opt_check_md(const char *name) |
| 504 |
{ |
| 505 |
if (opt_md(name, NULL)) |
| 506 |
return 1; |
| 507 |
ERR_clear_error(); |
| 508 |
return 0; |
| 509 |
} |
| 510 |
|
| 511 |
/* Look through a list of name/value pairs. */ |
| 512 |
int opt_pair(const char *name, const OPT_PAIR *pairs, int *result) |
| 513 |
{ |
| 514 |
const OPT_PAIR *pp; |
| 515 |
|
| 516 |
for (pp = pairs; pp->name; pp++) |
| 517 |
if (strcmp(pp->name, name) == 0) { |
| 518 |
*result = pp->retval; |
| 519 |
return 1; |
| 520 |
} |
| 521 |
opt_printf_stderr("%s: Value must be one of:\n", prog); |
| 522 |
for (pp = pairs; pp->name; pp++) |
| 523 |
opt_printf_stderr("\t%s\n", pp->name); |
| 524 |
return 0; |
| 525 |
} |
| 526 |
|
| 527 |
/* Look through a list of valid names */ |
| 528 |
int opt_string(const char *name, const char **options) |
| 529 |
{ |
| 530 |
const char **p; |
| 531 |
|
| 532 |
for (p = options; *p != NULL; p++) |
| 533 |
if (strcmp(*p, name) == 0) |
| 534 |
return 1; |
| 535 |
opt_printf_stderr("%s: Value must be one of:\n", prog); |
| 536 |
for (p = options; *p != NULL; p++) |
| 537 |
opt_printf_stderr("\t%s\n", *p); |
| 538 |
return 0; |
| 539 |
} |
| 540 |
|
| 541 |
/* Parse an int, put it into *result; return 0 on failure, else 1. */ |
| 542 |
int opt_int(const char *value, int *result) |
| 543 |
{ |
| 544 |
long l; |
| 545 |
|
| 546 |
if (!opt_long(value, &l)) |
| 547 |
return 0; |
| 548 |
*result = (int)l; |
| 549 |
if (*result != l) { |
| 550 |
opt_printf_stderr("%s: Value \"%s\" outside integer range\n", |
| 551 |
prog, value); |
| 552 |
return 0; |
| 553 |
} |
| 554 |
return 1; |
| 555 |
} |
| 556 |
|
| 557 |
/* Parse and return an integer, assuming range has been checked before. */ |
| 558 |
int opt_int_arg(void) |
| 559 |
{ |
| 560 |
int result = -1; |
| 561 |
|
| 562 |
(void)opt_int(arg, &result); |
| 563 |
return result; |
| 564 |
} |
| 565 |
|
| 566 |
static void opt_number_error(const char *v) |
| 567 |
{ |
| 568 |
size_t i = 0; |
| 569 |
struct strstr_pair_st { |
| 570 |
char *prefix; |
| 571 |
char *name; |
| 572 |
} b[] = { |
| 573 |
{ "0x", "a hexadecimal" }, |
| 574 |
{ "0X", "a hexadecimal" }, |
| 575 |
{ "0", "an octal" } |
| 576 |
}; |
| 577 |
|
| 578 |
for (i = 0; i < OSSL_NELEM(b); i++) { |
| 579 |
if (strncmp(v, b[i].prefix, strlen(b[i].prefix)) == 0) { |
| 580 |
opt_printf_stderr("%s: Can't parse \"%s\" as %s number\n", |
| 581 |
prog, v, b[i].name); |
| 582 |
return; |
| 583 |
} |
| 584 |
} |
| 585 |
opt_printf_stderr("%s: Can't parse \"%s\" as a number\n", prog, v); |
| 586 |
return; |
| 587 |
} |
| 588 |
|
| 589 |
/* Parse a long, put it into *result; return 0 on failure, else 1. */ |
| 590 |
int opt_long(const char *value, long *result) |
| 591 |
{ |
| 592 |
int oerrno = errno; |
| 593 |
long l; |
| 594 |
char *endp; |
| 595 |
|
| 596 |
errno = 0; |
| 597 |
l = strtol(value, &endp, 0); |
| 598 |
if (*endp |
| 599 |
|| endp == value |
| 600 |
|| ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) |
| 601 |
|| (l == 0 && errno != 0)) { |
| 602 |
opt_number_error(value); |
| 603 |
errno = oerrno; |
| 604 |
return 0; |
| 605 |
} |
| 606 |
*result = l; |
| 607 |
errno = oerrno; |
| 608 |
return 1; |
| 609 |
} |
| 610 |
|
| 611 |
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && defined(INTMAX_MAX) && defined(UINTMAX_MAX) && !defined(OPENSSL_NO_INTTYPES_H) |
| 612 |
|
| 613 |
/* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */ |
| 614 |
int opt_intmax(const char *value, ossl_intmax_t *result) |
| 615 |
{ |
| 616 |
int oerrno = errno; |
| 617 |
intmax_t m; |
| 618 |
char *endp; |
| 619 |
|
| 620 |
errno = 0; |
| 621 |
m = strtoimax(value, &endp, 0); |
| 622 |
if (*endp |
| 623 |
|| endp == value |
| 624 |
|| ((m == INTMAX_MAX || m == INTMAX_MIN) |
| 625 |
&& errno == ERANGE) |
| 626 |
|| (m == 0 && errno != 0)) { |
| 627 |
opt_number_error(value); |
| 628 |
errno = oerrno; |
| 629 |
return 0; |
| 630 |
} |
| 631 |
/* Ensure that the value in |m| is never too big for |*result| */ |
| 632 |
if (sizeof(m) > sizeof(*result) |
| 633 |
&& (m < OSSL_INTMAX_MIN || m > OSSL_INTMAX_MAX)) { |
| 634 |
opt_number_error(value); |
| 635 |
return 0; |
| 636 |
} |
| 637 |
*result = (ossl_intmax_t)m; |
| 638 |
errno = oerrno; |
| 639 |
return 1; |
| 640 |
} |
| 641 |
|
| 642 |
/* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */ |
| 643 |
int opt_uintmax(const char *value, ossl_uintmax_t *result) |
| 644 |
{ |
| 645 |
int oerrno = errno; |
| 646 |
uintmax_t m; |
| 647 |
char *endp; |
| 648 |
|
| 649 |
errno = 0; |
| 650 |
m = strtoumax(value, &endp, 0); |
| 651 |
if (*endp |
| 652 |
|| endp == value |
| 653 |
|| (m == UINTMAX_MAX && errno == ERANGE) |
| 654 |
|| (m == 0 && errno != 0)) { |
| 655 |
opt_number_error(value); |
| 656 |
errno = oerrno; |
| 657 |
return 0; |
| 658 |
} |
| 659 |
/* Ensure that the value in |m| is never too big for |*result| */ |
| 660 |
if (sizeof(m) > sizeof(*result) |
| 661 |
&& m > OSSL_UINTMAX_MAX) { |
| 662 |
opt_number_error(value); |
| 663 |
return 0; |
| 664 |
} |
| 665 |
*result = (ossl_uintmax_t)m; |
| 666 |
errno = oerrno; |
| 667 |
return 1; |
| 668 |
} |
| 669 |
#else |
| 670 |
/* Fallback implementations based on long */ |
| 671 |
int opt_intmax(const char *value, ossl_intmax_t *result) |
| 672 |
{ |
| 673 |
long m; |
| 674 |
int ret; |
| 675 |
|
| 676 |
if ((ret = opt_long(value, &m))) |
| 677 |
*result = m; |
| 678 |
return ret; |
| 679 |
} |
| 680 |
|
| 681 |
int opt_uintmax(const char *value, ossl_uintmax_t *result) |
| 682 |
{ |
| 683 |
unsigned long m; |
| 684 |
int ret; |
| 685 |
|
| 686 |
if ((ret = opt_ulong(value, &m))) |
| 687 |
*result = m; |
| 688 |
return ret; |
| 689 |
} |
| 690 |
#endif |
| 691 |
|
| 692 |
/* |
| 693 |
* Parse an unsigned long, put it into *result; return 0 on failure, else 1. |
| 694 |
*/ |
| 695 |
int opt_ulong(const char *value, unsigned long *result) |
| 696 |
{ |
| 697 |
int oerrno = errno; |
| 698 |
char *endptr; |
| 699 |
unsigned long l; |
| 700 |
|
| 701 |
errno = 0; |
| 702 |
l = strtoul(value, &endptr, 0); |
| 703 |
if (*endptr |
| 704 |
|| endptr == value |
| 705 |
|| ((l == ULONG_MAX) && errno == ERANGE) |
| 706 |
|| (l == 0 && errno != 0)) { |
| 707 |
opt_number_error(value); |
| 708 |
errno = oerrno; |
| 709 |
return 0; |
| 710 |
} |
| 711 |
*result = l; |
| 712 |
errno = oerrno; |
| 713 |
return 1; |
| 714 |
} |
| 715 |
|
| 716 |
/* |
| 717 |
* We pass opt as an int but cast it to "enum range" so that all the |
| 718 |
* items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch |
| 719 |
* in gcc do the right thing. |
| 720 |
*/ |
| 721 |
enum range { OPT_V_ENUM }; |
| 722 |
|
| 723 |
int opt_verify(int opt, X509_VERIFY_PARAM *vpm) |
| 724 |
{ |
| 725 |
int i; |
| 726 |
ossl_intmax_t t = 0; |
| 727 |
ASN1_OBJECT *otmp; |
| 728 |
X509_PURPOSE *xptmp; |
| 729 |
const X509_VERIFY_PARAM *vtmp; |
| 730 |
|
| 731 |
OPENSSL_assert(vpm != NULL); |
| 732 |
OPENSSL_assert(opt > OPT_V__FIRST); |
| 733 |
OPENSSL_assert(opt < OPT_V__LAST); |
| 734 |
|
| 735 |
switch ((enum range)opt) { |
| 736 |
case OPT_V__FIRST: |
| 737 |
case OPT_V__LAST: |
| 738 |
return 0; |
| 739 |
case OPT_V_POLICY: |
| 740 |
otmp = OBJ_txt2obj(opt_arg(), 0); |
| 741 |
if (otmp == NULL) { |
| 742 |
opt_printf_stderr("%s: Invalid Policy %s\n", prog, opt_arg()); |
| 743 |
return 0; |
| 744 |
} |
| 745 |
if (!X509_VERIFY_PARAM_add0_policy(vpm, otmp)) { |
| 746 |
ASN1_OBJECT_free(otmp); |
| 747 |
opt_printf_stderr("%s: Internal error adding Policy %s\n", |
| 748 |
prog, opt_arg()); |
| 749 |
return 0; |
| 750 |
} |
| 751 |
break; |
| 752 |
case OPT_V_PURPOSE: |
| 753 |
/* purpose name -> purpose index */ |
| 754 |
i = X509_PURPOSE_get_by_sname(opt_arg()); |
| 755 |
if (i < 0) { |
| 756 |
opt_printf_stderr("%s: Invalid purpose %s\n", prog, opt_arg()); |
| 757 |
return 0; |
| 758 |
} |
| 759 |
|
| 760 |
/* purpose index -> purpose object */ |
| 761 |
xptmp = X509_PURPOSE_get0(i); |
| 762 |
|
| 763 |
/* purpose object -> purpose value */ |
| 764 |
i = X509_PURPOSE_get_id(xptmp); |
| 765 |
|
| 766 |
if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) { |
| 767 |
opt_printf_stderr("%s: Internal error setting purpose %s\n", |
| 768 |
prog, opt_arg()); |
| 769 |
return 0; |
| 770 |
} |
| 771 |
break; |
| 772 |
case OPT_V_VERIFY_NAME: |
| 773 |
vtmp = X509_VERIFY_PARAM_lookup(opt_arg()); |
| 774 |
if (vtmp == NULL) { |
| 775 |
opt_printf_stderr("%s: Invalid verify name %s\n", |
| 776 |
prog, opt_arg()); |
| 777 |
return 0; |
| 778 |
} |
| 779 |
X509_VERIFY_PARAM_set1(vpm, vtmp); |
| 780 |
break; |
| 781 |
case OPT_V_VERIFY_DEPTH: |
| 782 |
i = atoi(opt_arg()); |
| 783 |
if (i >= 0) |
| 784 |
X509_VERIFY_PARAM_set_depth(vpm, i); |
| 785 |
break; |
| 786 |
case OPT_V_VERIFY_AUTH_LEVEL: |
| 787 |
i = atoi(opt_arg()); |
| 788 |
if (i >= 0) |
| 789 |
X509_VERIFY_PARAM_set_auth_level(vpm, i); |
| 790 |
break; |
| 791 |
case OPT_V_ATTIME: |
| 792 |
if (!opt_intmax(opt_arg(), &t)) |
| 793 |
return 0; |
| 794 |
if (t != (time_t)t) { |
| 795 |
opt_printf_stderr("%s: epoch time out of range %s\n", |
| 796 |
prog, opt_arg()); |
| 797 |
return 0; |
| 798 |
} |
| 799 |
X509_VERIFY_PARAM_set_time(vpm, (time_t)t); |
| 800 |
break; |
| 801 |
case OPT_V_VERIFY_HOSTNAME: |
| 802 |
if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0)) |
| 803 |
return 0; |
| 804 |
break; |
| 805 |
case OPT_V_VERIFY_EMAIL: |
| 806 |
if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0)) |
| 807 |
return 0; |
| 808 |
break; |
| 809 |
case OPT_V_VERIFY_IP: |
| 810 |
if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg())) |
| 811 |
return 0; |
| 812 |
break; |
| 813 |
case OPT_V_IGNORE_CRITICAL: |
| 814 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL); |
| 815 |
break; |
| 816 |
case OPT_V_ISSUER_CHECKS: |
| 817 |
/* NOP, deprecated */ |
| 818 |
break; |
| 819 |
case OPT_V_CRL_CHECK: |
| 820 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK); |
| 821 |
break; |
| 822 |
case OPT_V_CRL_CHECK_ALL: |
| 823 |
X509_VERIFY_PARAM_set_flags(vpm, |
| 824 |
X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL); |
| 825 |
break; |
| 826 |
case OPT_V_POLICY_CHECK: |
| 827 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK); |
| 828 |
break; |
| 829 |
case OPT_V_EXPLICIT_POLICY: |
| 830 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY); |
| 831 |
break; |
| 832 |
case OPT_V_INHIBIT_ANY: |
| 833 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY); |
| 834 |
break; |
| 835 |
case OPT_V_INHIBIT_MAP: |
| 836 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP); |
| 837 |
break; |
| 838 |
case OPT_V_X509_STRICT: |
| 839 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT); |
| 840 |
break; |
| 841 |
case OPT_V_EXTENDED_CRL: |
| 842 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT); |
| 843 |
break; |
| 844 |
case OPT_V_USE_DELTAS: |
| 845 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS); |
| 846 |
break; |
| 847 |
case OPT_V_POLICY_PRINT: |
| 848 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY); |
| 849 |
break; |
| 850 |
case OPT_V_CHECK_SS_SIG: |
| 851 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE); |
| 852 |
break; |
| 853 |
case OPT_V_TRUSTED_FIRST: |
| 854 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST); |
| 855 |
break; |
| 856 |
case OPT_V_SUITEB_128_ONLY: |
| 857 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY); |
| 858 |
break; |
| 859 |
case OPT_V_SUITEB_128: |
| 860 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS); |
| 861 |
break; |
| 862 |
case OPT_V_SUITEB_192: |
| 863 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS); |
| 864 |
break; |
| 865 |
case OPT_V_PARTIAL_CHAIN: |
| 866 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN); |
| 867 |
break; |
| 868 |
case OPT_V_NO_ALT_CHAINS: |
| 869 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS); |
| 870 |
break; |
| 871 |
case OPT_V_NO_CHECK_TIME: |
| 872 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME); |
| 873 |
break; |
| 874 |
case OPT_V_ALLOW_PROXY_CERTS: |
| 875 |
X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS); |
| 876 |
break; |
| 877 |
} |
| 878 |
return 1; |
| 879 |
} |
| 880 |
|
| 881 |
void opt_begin(void) |
| 882 |
{ |
| 883 |
opt_index = 1; |
| 884 |
arg = NULL; |
| 885 |
flag = NULL; |
| 886 |
} |
| 887 |
|
| 888 |
/* |
| 889 |
* Parse the next flag (and value if specified), return 0 if done, -1 on |
| 890 |
* error, otherwise the flag's retval. |
| 891 |
*/ |
| 892 |
int opt_next(void) |
| 893 |
{ |
| 894 |
char *p; |
| 895 |
const OPTIONS *o; |
| 896 |
int ival; |
| 897 |
long lval; |
| 898 |
unsigned long ulval; |
| 899 |
ossl_intmax_t imval; |
| 900 |
ossl_uintmax_t umval; |
| 901 |
|
| 902 |
/* Look at current arg; at end of the list? */ |
| 903 |
arg = NULL; |
| 904 |
p = argv[opt_index]; |
| 905 |
if (p == NULL) |
| 906 |
return 0; |
| 907 |
|
| 908 |
/* If word doesn't start with a -, we're done. */ |
| 909 |
if (*p != '-') |
| 910 |
return 0; |
| 911 |
|
| 912 |
/* Hit "--" ? We're done. */ |
| 913 |
opt_index++; |
| 914 |
if (strcmp(p, "--") == 0) |
| 915 |
return 0; |
| 916 |
|
| 917 |
/* Allow -nnn and --nnn */ |
| 918 |
if (*++p == '-') |
| 919 |
p++; |
| 920 |
flag = p - 1; |
| 921 |
|
| 922 |
/* If we have --flag=foo, snip it off */ |
| 923 |
if ((arg = strchr(p, '=')) != NULL) |
| 924 |
*arg++ = '\0'; |
| 925 |
for (o = opts; o->name; ++o) { |
| 926 |
/* If not this option, move on to the next one. */ |
| 927 |
if (!(strcmp(p, "h") == 0 && strcmp(o->name, "help") == 0) |
| 928 |
&& strcmp(p, o->name) != 0) |
| 929 |
continue; |
| 930 |
|
| 931 |
/* If it doesn't take a value, make sure none was given. */ |
| 932 |
if (o->valtype == 0 || o->valtype == '-') { |
| 933 |
if (arg) { |
| 934 |
opt_printf_stderr("%s: Option -%s does not take a value\n", |
| 935 |
prog, p); |
| 936 |
return -1; |
| 937 |
} |
| 938 |
return o->retval; |
| 939 |
} |
| 940 |
|
| 941 |
/* Want a value; get the next param if =foo not used. */ |
| 942 |
if (arg == NULL) { |
| 943 |
if (argv[opt_index] == NULL) { |
| 944 |
opt_printf_stderr("%s: Option -%s needs a value\n", |
| 945 |
prog, o->name); |
| 946 |
return -1; |
| 947 |
} |
| 948 |
arg = argv[opt_index++]; |
| 949 |
} |
| 950 |
|
| 951 |
/* Syntax-check value. */ |
| 952 |
switch (o->valtype) { |
| 953 |
default: |
| 954 |
case 's': |
| 955 |
case ':': |
| 956 |
/* Just a string. */ |
| 957 |
break; |
| 958 |
case '.': |
| 959 |
/* Parameters */ |
| 960 |
break; |
| 961 |
case '/': |
| 962 |
if (opt_isdir(arg) > 0) |
| 963 |
break; |
| 964 |
opt_printf_stderr("%s: Not a directory: %s\n", prog, arg); |
| 965 |
return -1; |
| 966 |
case '<': |
| 967 |
/* Input file. */ |
| 968 |
break; |
| 969 |
case '>': |
| 970 |
/* Output file. */ |
| 971 |
break; |
| 972 |
case 'p': |
| 973 |
case 'n': |
| 974 |
case 'N': |
| 975 |
if (!opt_int(arg, &ival)) |
| 976 |
return -1; |
| 977 |
if (o->valtype == 'p' && ival <= 0) { |
| 978 |
opt_printf_stderr("%s: Non-positive number \"%s\" for option -%s\n", |
| 979 |
prog, arg, o->name); |
| 980 |
return -1; |
| 981 |
} |
| 982 |
if (o->valtype == 'N' && ival < 0) { |
| 983 |
opt_printf_stderr("%s: Negative number \"%s\" for option -%s\n", |
| 984 |
prog, arg, o->name); |
| 985 |
return -1; |
| 986 |
} |
| 987 |
break; |
| 988 |
case 'M': |
| 989 |
if (!opt_intmax(arg, &imval)) |
| 990 |
return -1; |
| 991 |
break; |
| 992 |
case 'U': |
| 993 |
if (!opt_uintmax(arg, &umval)) |
| 994 |
return -1; |
| 995 |
break; |
| 996 |
case 'l': |
| 997 |
if (!opt_long(arg, &lval)) |
| 998 |
return -1; |
| 999 |
break; |
| 1000 |
case 'u': |
| 1001 |
if (!opt_ulong(arg, &ulval)) |
| 1002 |
return -1; |
| 1003 |
break; |
| 1004 |
case 'c': |
| 1005 |
case 'E': |
| 1006 |
case 'F': |
| 1007 |
case 'f': |
| 1008 |
case 'A': |
| 1009 |
case 'a': |
| 1010 |
if (opt_format(arg, |
| 1011 |
o->valtype == 'c' ? OPT_FMT_PDS : o->valtype == 'E' ? OPT_FMT_PDE |
| 1012 |
: o->valtype == 'F' ? OPT_FMT_PEMDER |
| 1013 |
: o->valtype == 'A' ? OPT_FMT_ASN1 |
| 1014 |
: OPT_FMT_ANY, |
| 1015 |
&ival)) |
| 1016 |
break; |
| 1017 |
opt_printf_stderr("%s: Invalid format \"%s\" for option -%s\n", |
| 1018 |
prog, arg, o->name); |
| 1019 |
return -1; |
| 1020 |
} |
| 1021 |
|
| 1022 |
/* Return the flag value. */ |
| 1023 |
return o->retval; |
| 1024 |
} |
| 1025 |
if (unknown != NULL) { |
| 1026 |
if (dunno != NULL) { |
| 1027 |
opt_printf_stderr("%s: Multiple %s or unknown options: -%s and -%s\n", |
| 1028 |
prog, unknown_name, dunno, p); |
| 1029 |
return -1; |
| 1030 |
} |
| 1031 |
dunno = p; |
| 1032 |
return unknown->retval; |
| 1033 |
} |
| 1034 |
opt_printf_stderr("%s: Unknown option: -%s\n", prog, p); |
| 1035 |
return -1; |
| 1036 |
} |
| 1037 |
|
| 1038 |
/* Return the most recent flag parameter. */ |
| 1039 |
char *opt_arg(void) |
| 1040 |
{ |
| 1041 |
return arg; |
| 1042 |
} |
| 1043 |
|
| 1044 |
/* Return the most recent flag (option name including the preceding '-'). */ |
| 1045 |
char *opt_flag(void) |
| 1046 |
{ |
| 1047 |
return flag; |
| 1048 |
} |
| 1049 |
|
| 1050 |
/* Return the unknown option. */ |
| 1051 |
char *opt_unknown(void) |
| 1052 |
{ |
| 1053 |
return dunno; |
| 1054 |
} |
| 1055 |
|
| 1056 |
/* Reset the unknown option; needed by ocsp to allow multiple digest options. */ |
| 1057 |
void reset_unknown(void) |
| 1058 |
{ |
| 1059 |
dunno = NULL; |
| 1060 |
} |
| 1061 |
|
| 1062 |
/* Return the rest of the arguments after parsing flags. */ |
| 1063 |
char **opt_rest(void) |
| 1064 |
{ |
| 1065 |
return &argv[opt_index]; |
| 1066 |
} |
| 1067 |
|
| 1068 |
/* How many items in remaining args? */ |
| 1069 |
int opt_num_rest(void) |
| 1070 |
{ |
| 1071 |
int i = 0; |
| 1072 |
char **pp; |
| 1073 |
|
| 1074 |
for (pp = opt_rest(); *pp; pp++, i++) |
| 1075 |
continue; |
| 1076 |
return i; |
| 1077 |
} |
| 1078 |
|
| 1079 |
int opt_check_rest_arg(const char *expected) |
| 1080 |
{ |
| 1081 |
char *opt = *opt_rest(); |
| 1082 |
|
| 1083 |
if (opt == NULL || *opt == '\0') { |
| 1084 |
if (expected == NULL) |
| 1085 |
return 1; |
| 1086 |
opt_printf_stderr("%s: Missing argument: %s\n", prog, expected); |
| 1087 |
return 0; |
| 1088 |
} |
| 1089 |
if (expected != NULL) { |
| 1090 |
opt = argv[opt_index + 1]; |
| 1091 |
if (opt == NULL || *opt == '\0') |
| 1092 |
return 1; |
| 1093 |
opt_printf_stderr("%s: Extra argument after %s: \"%s\"\n", prog, expected, opt); |
| 1094 |
return 0; |
| 1095 |
} |
| 1096 |
if (opt_unknown() == NULL) |
| 1097 |
opt_printf_stderr("%s: Extra option: \"%s\"\n", prog, opt); |
| 1098 |
else |
| 1099 |
opt_printf_stderr("%s: Extra (unknown) options: \"%s\" \"%s\"\n", |
| 1100 |
prog, opt_unknown(), opt); |
| 1101 |
return 0; |
| 1102 |
} |
| 1103 |
|
| 1104 |
/* Return a string describing the parameter type. */ |
| 1105 |
static const char *valtype2param(const OPTIONS *o) |
| 1106 |
{ |
| 1107 |
switch (o->valtype) { |
| 1108 |
case 0: |
| 1109 |
case '-': |
| 1110 |
return ""; |
| 1111 |
case ':': |
| 1112 |
return "uri"; |
| 1113 |
case 's': |
| 1114 |
return "val"; |
| 1115 |
case '/': |
| 1116 |
return "dir"; |
| 1117 |
case '<': |
| 1118 |
return "infile"; |
| 1119 |
case '>': |
| 1120 |
return "outfile"; |
| 1121 |
case 'p': |
| 1122 |
return "+int"; |
| 1123 |
case 'n': |
| 1124 |
return "int"; |
| 1125 |
case 'l': |
| 1126 |
return "long"; |
| 1127 |
case 'u': |
| 1128 |
return "ulong"; |
| 1129 |
case 'E': |
| 1130 |
return "PEM|DER|ENGINE"; |
| 1131 |
case 'F': |
| 1132 |
return "PEM|DER"; |
| 1133 |
case 'f': |
| 1134 |
return "format"; |
| 1135 |
case 'M': |
| 1136 |
return "intmax"; |
| 1137 |
case 'N': |
| 1138 |
return "nonneg"; |
| 1139 |
case 'U': |
| 1140 |
return "uintmax"; |
| 1141 |
} |
| 1142 |
return "parm"; |
| 1143 |
} |
| 1144 |
|
| 1145 |
static void opt_print(const OPTIONS *o, int doingparams, int width) |
| 1146 |
{ |
| 1147 |
const char *help; |
| 1148 |
char start[80 + 1]; |
| 1149 |
int linelen, printlen; |
| 1150 |
|
| 1151 |
/* Avoid OOB if width is beyond the buffer size of start */ |
| 1152 |
if (width >= (int)sizeof(start)) |
| 1153 |
width = (int)sizeof(start) - 1; |
| 1154 |
|
| 1155 |
help = o->helpstr ? o->helpstr : "(No additional info)"; |
| 1156 |
if (o->name == OPT_HELP_STR) { |
| 1157 |
opt_printf_stderr(help, prog); |
| 1158 |
return; |
| 1159 |
} else if (o->name == OPT_SECTION_STR) { |
| 1160 |
opt_printf_stderr("\n"); |
| 1161 |
opt_printf_stderr(help, prog); |
| 1162 |
return; |
| 1163 |
} else if (o->name == OPT_PARAM_STR) { |
| 1164 |
opt_printf_stderr("\nParameters:\n"); |
| 1165 |
return; |
| 1166 |
} |
| 1167 |
|
| 1168 |
/* Pad out prefix */ |
| 1169 |
memset(start, ' ', sizeof(start) - 1); |
| 1170 |
start[sizeof(start) - 1] = '\0'; |
| 1171 |
|
| 1172 |
if (o->name == OPT_MORE_STR) { |
| 1173 |
/* Continuation of previous line; pad and print. */ |
| 1174 |
start[width] = '\0'; |
| 1175 |
opt_printf_stderr("%s %s\n", start, help); |
| 1176 |
return; |
| 1177 |
} |
| 1178 |
|
| 1179 |
/* Build up the "-flag [param]" part. */ |
| 1180 |
linelen = 0; |
| 1181 |
|
| 1182 |
printlen = opt_printf_stderr(" %s", !doingparams ? "-" : ""); |
| 1183 |
linelen += (printlen > 0) ? printlen : MAX_OPT_HELP_WIDTH; |
| 1184 |
|
| 1185 |
printlen = opt_printf_stderr("%s", o->name[0] ? o->name : "*"); |
| 1186 |
linelen += (printlen > 0) ? printlen : MAX_OPT_HELP_WIDTH; |
| 1187 |
|
| 1188 |
if (o->valtype != '-') { |
| 1189 |
printlen = opt_printf_stderr(" %s", valtype2param(o)); |
| 1190 |
linelen += (printlen > 0) ? printlen : MAX_OPT_HELP_WIDTH; |
| 1191 |
} |
| 1192 |
|
| 1193 |
if (linelen >= MAX_OPT_HELP_WIDTH || linelen > width) { |
| 1194 |
opt_printf_stderr("%s", "\n"); |
| 1195 |
memset(start, ' ', sizeof(start)); |
| 1196 |
linelen = 0; |
| 1197 |
} |
| 1198 |
|
| 1199 |
width -= linelen; |
| 1200 |
|
| 1201 |
start[width] = '\0'; |
| 1202 |
opt_printf_stderr("%s %s\n", start, help); |
| 1203 |
} |
| 1204 |
|
| 1205 |
void opt_help(const OPTIONS *list) |
| 1206 |
{ |
| 1207 |
const OPTIONS *o; |
| 1208 |
int i, sawparams = 0, width = 5; |
| 1209 |
int standard_prolog; |
| 1210 |
|
| 1211 |
/* Starts with its own help message? */ |
| 1212 |
standard_prolog = list[0].name != OPT_HELP_STR; |
| 1213 |
|
| 1214 |
/* Find the widest help. */ |
| 1215 |
for (o = list; o->name; o++) { |
| 1216 |
if (o->name == OPT_MORE_STR) |
| 1217 |
continue; |
| 1218 |
|
| 1219 |
i = 2 + (int)strlen(o->name); |
| 1220 |
if (o->valtype != '-') |
| 1221 |
i += 1 + (int)strlen(valtype2param(o)); |
| 1222 |
|
| 1223 |
if (i > width) |
| 1224 |
width = i; |
| 1225 |
} |
| 1226 |
|
| 1227 |
if (width > MAX_OPT_HELP_WIDTH) |
| 1228 |
width = MAX_OPT_HELP_WIDTH; |
| 1229 |
|
| 1230 |
if (standard_prolog) { |
| 1231 |
opt_printf_stderr("Usage: %s [options]\n", prog); |
| 1232 |
if (list[0].name != OPT_SECTION_STR) |
| 1233 |
opt_printf_stderr("Valid options are:\n", prog); |
| 1234 |
} |
| 1235 |
|
| 1236 |
/* Now let's print. */ |
| 1237 |
for (o = list; o->name; o++) { |
| 1238 |
if (o->name == OPT_PARAM_STR) |
| 1239 |
sawparams = 1; |
| 1240 |
opt_print(o, sawparams, width); |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
/* opt_isdir section */ |
| 1245 |
#ifdef _WIN32 |
| 1246 |
#include <windows.h> |
| 1247 |
int opt_isdir(const char *name) |
| 1248 |
{ |
| 1249 |
DWORD attr; |
| 1250 |
#if defined(UNICODE) || defined(_UNICODE) |
| 1251 |
size_t i, len_0 = strlen(name) + 1; |
| 1252 |
WCHAR tempname[MAX_PATH]; |
| 1253 |
|
| 1254 |
if (len_0 > MAX_PATH) |
| 1255 |
return -1; |
| 1256 |
|
| 1257 |
#if !defined(_WIN32_WCE) || _WIN32_WCE >= 101 |
| 1258 |
if (!MultiByteToWideChar(CP_ACP, 0, name, (int)len_0, tempname, MAX_PATH)) |
| 1259 |
#endif |
| 1260 |
for (i = 0; i < len_0; i++) |
| 1261 |
tempname[i] = (WCHAR)name[i]; |
| 1262 |
|
| 1263 |
attr = GetFileAttributes(tempname); |
| 1264 |
#else |
| 1265 |
attr = GetFileAttributes(name); |
| 1266 |
#endif |
| 1267 |
if (attr == INVALID_FILE_ATTRIBUTES) |
| 1268 |
return -1; |
| 1269 |
return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0); |
| 1270 |
} |
| 1271 |
#else |
| 1272 |
#include <sys/stat.h> |
| 1273 |
#ifndef S_ISDIR |
| 1274 |
#if defined(_S_IFMT) && defined(_S_IFDIR) |
| 1275 |
#define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR) |
| 1276 |
#else |
| 1277 |
#define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR) |
| 1278 |
#endif |
| 1279 |
#endif |
| 1280 |
|
| 1281 |
int opt_isdir(const char *name) |
| 1282 |
{ |
| 1283 |
#if defined(S_ISDIR) |
| 1284 |
struct stat st; |
| 1285 |
|
| 1286 |
if (stat(name, &st) == 0) |
| 1287 |
return S_ISDIR(st.st_mode); |
| 1288 |
else |
| 1289 |
return -1; |
| 1290 |
#else |
| 1291 |
return -1; |
| 1292 |
#endif |
| 1293 |
} |
| 1294 |
#endif |