1 /* 2 File autogenerated by gengetopt version 2.22.6 3 generated with the following command: 4 gengetopt --no-handle-help --input cmdline.ggo Makefile.am 5 6 The developers of gengetopt consider the fixed text that goes in all 7 gengetopt output files to be in the public domain: 8 we make no copyright claims on it. 9 */ 10 11 /* If we use autoconf. */ 12 #ifdef HAVE_CONFIG_H 13 #include "config.h" 14 #endif 15 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <string.h> 19 20 #ifndef FIX_UNUSED 21 #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */ 22 #endif 23 24 #include <getopt.h> 25 26 #include "cmdline.h" 27 28 const char *gengetopt_args_info_purpose = "Perform a U2F registration operation and print a configuration line that can be\nused with the pam_u2f module."; 29 30 const char *gengetopt_args_info_usage = "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]..."; 31 32 const char *gengetopt_args_info_versiontext = ""; 33 34 const char *gengetopt_args_info_description = ""; 35 36 const char *gengetopt_args_info_help[] = { 37 " -h, --help Print help and exit", 38 " --version Print version and exit", 39 " -o, --origin=STRING Origin URL to use during registration. Defaults to\n pam://hostname", 40 " -i, --appid=STRING Application ID to use during registration. Defaults\n to pam://hostname", 41 " -t, --type=STRING COSE type to use during registration (ES256 or\n RS256). Defaults to ES256.", 42 " -r, --resident Generate a resident credential (default=off)", 43 " -P, --no-user-presence Allow the credential to be used without ensuring the\n user's presence (default=off)", 44 " -N, --pin-verification Require PIN verification during authentication\n (default=off)", 45 " -V, --user-verification Require user verification during authentication\n (default=off)", 46 " -d, --debug Print debug information (highly verbose)\n (default=off)", 47 " -v, --verbose Print information about chosen origin and appid\n (default=off)", 48 "\n Group: user", 49 " -u, --username=STRING The name of the user registering the device.\n Defaults to the current user name", 50 " -n, --nouser Print only registration information (keyHandle and\n public key). Useful for appending", 51 0 52 }; 53 54 typedef enum {ARG_NO 55 , ARG_FLAG 56 , ARG_STRING 57 } cmdline_parser_arg_type; 58 59 static 60 void clear_given (struct gengetopt_args_info *args_info); 61 static 62 void clear_args (struct gengetopt_args_info *args_info); 63 64 static int 65 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info, 66 struct cmdline_parser_params *params, const char *additional_error); 67 68 69 static char * 70 gengetopt_strdup (const char *s); 71 72 static 73 void clear_given (struct gengetopt_args_info *args_info) 74 { 75 args_info->help_given = 0 ; 76 args_info->version_given = 0 ; 77 args_info->origin_given = 0 ; 78 args_info->appid_given = 0 ; 79 args_info->type_given = 0 ; 80 args_info->resident_given = 0 ; 81 args_info->no_user_presence_given = 0 ; 82 args_info->pin_verification_given = 0 ; 83 args_info->user_verification_given = 0 ; 84 args_info->debug_given = 0 ; 85 args_info->verbose_given = 0 ; 86 args_info->username_given = 0 ; 87 args_info->nouser_given = 0 ; 88 args_info->user_group_counter = 0 ; 89 } 90 91 static 92 void clear_args (struct gengetopt_args_info *args_info) 93 { 94 FIX_UNUSED (args_info); 95 args_info->origin_arg = NULL; 96 args_info->origin_orig = NULL; 97 args_info->appid_arg = NULL; 98 args_info->appid_orig = NULL; 99 args_info->type_arg = NULL; 100 args_info->type_orig = NULL; 101 args_info->resident_flag = 0; 102 args_info->no_user_presence_flag = 0; 103 args_info->pin_verification_flag = 0; 104 args_info->user_verification_flag = 0; 105 args_info->debug_flag = 0; 106 args_info->verbose_flag = 0; 107 args_info->username_arg = NULL; 108 args_info->username_orig = NULL; 109 110 } 111 112 static 113 void init_args_info(struct gengetopt_args_info *args_info) 114 { 115 116 117 args_info->help_help = gengetopt_args_info_help[0] ; 118 args_info->version_help = gengetopt_args_info_help[1] ; 119 args_info->origin_help = gengetopt_args_info_help[2] ; 120 args_info->appid_help = gengetopt_args_info_help[3] ; 121 args_info->type_help = gengetopt_args_info_help[4] ; 122 args_info->resident_help = gengetopt_args_info_help[5] ; 123 args_info->no_user_presence_help = gengetopt_args_info_help[6] ; 124 args_info->pin_verification_help = gengetopt_args_info_help[7] ; 125 args_info->user_verification_help = gengetopt_args_info_help[8] ; 126 args_info->debug_help = gengetopt_args_info_help[9] ; 127 args_info->verbose_help = gengetopt_args_info_help[10] ; 128 args_info->username_help = gengetopt_args_info_help[12] ; 129 args_info->nouser_help = gengetopt_args_info_help[13] ; 130 131 } 132 133 void 134 cmdline_parser_print_version (void) 135 { 136 printf ("%s %s\n", 137 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE), 138 CMDLINE_PARSER_VERSION); 139 140 if (strlen(gengetopt_args_info_versiontext) > 0) 141 printf("\n%s\n", gengetopt_args_info_versiontext); 142 } 143 144 static void print_help_common(void) { 145 cmdline_parser_print_version (); 146 147 if (strlen(gengetopt_args_info_purpose) > 0) 148 printf("\n%s\n", gengetopt_args_info_purpose); 149 150 if (strlen(gengetopt_args_info_usage) > 0) 151 printf("\n%s\n", gengetopt_args_info_usage); 152 153 printf("\n"); 154 155 if (strlen(gengetopt_args_info_description) > 0) 156 printf("%s\n\n", gengetopt_args_info_description); 157 } 158 159 void 160 cmdline_parser_print_help (void) 161 { 162 int i = 0; 163 print_help_common(); 164 while (gengetopt_args_info_help[i]) 165 printf("%s\n", gengetopt_args_info_help[i++]); 166 } 167 168 void 169 cmdline_parser_init (struct gengetopt_args_info *args_info) 170 { 171 clear_given (args_info); 172 clear_args (args_info); 173 init_args_info (args_info); 174 } 175 176 void 177 cmdline_parser_params_init(struct cmdline_parser_params *params) 178 { 179 if (params) 180 { 181 params->override = 0; 182 params->initialize = 1; 183 params->check_required = 1; 184 params->check_ambiguity = 0; 185 params->print_errors = 1; 186 } 187 } 188 189 struct cmdline_parser_params * 190 cmdline_parser_params_create(void) 191 { 192 struct cmdline_parser_params *params = 193 (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params)); 194 cmdline_parser_params_init(params); 195 return params; 196 } 197 198 static void 199 free_string_field (char **s) 200 { 201 if (*s) 202 { 203 free (*s); 204 *s = 0; 205 } 206 } 207 208 209 static void 210 cmdline_parser_release (struct gengetopt_args_info *args_info) 211 { 212 213 free_string_field (&(args_info->origin_arg)); 214 free_string_field (&(args_info->origin_orig)); 215 free_string_field (&(args_info->appid_arg)); 216 free_string_field (&(args_info->appid_orig)); 217 free_string_field (&(args_info->type_arg)); 218 free_string_field (&(args_info->type_orig)); 219 free_string_field (&(args_info->username_arg)); 220 free_string_field (&(args_info->username_orig)); 221 222 223 224 clear_given (args_info); 225 } 226 227 228 static void 229 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[]) 230 { 231 FIX_UNUSED (values); 232 if (arg) { 233 fprintf(outfile, "%s=\"%s\"\n", opt, arg); 234 } else { 235 fprintf(outfile, "%s\n", opt); 236 } 237 } 238 239 240 int 241 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info) 242 { 243 int i = 0; 244 245 if (!outfile) 246 { 247 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE); 248 return EXIT_FAILURE; 249 } 250 251 if (args_info->help_given) 252 write_into_file(outfile, "help", 0, 0 ); 253 if (args_info->version_given) 254 write_into_file(outfile, "version", 0, 0 ); 255 if (args_info->origin_given) 256 write_into_file(outfile, "origin", args_info->origin_orig, 0); 257 if (args_info->appid_given) 258 write_into_file(outfile, "appid", args_info->appid_orig, 0); 259 if (args_info->type_given) 260 write_into_file(outfile, "type", args_info->type_orig, 0); 261 if (args_info->resident_given) 262 write_into_file(outfile, "resident", 0, 0 ); 263 if (args_info->no_user_presence_given) 264 write_into_file(outfile, "no-user-presence", 0, 0 ); 265 if (args_info->pin_verification_given) 266 write_into_file(outfile, "pin-verification", 0, 0 ); 267 if (args_info->user_verification_given) 268 write_into_file(outfile, "user-verification", 0, 0 ); 269 if (args_info->debug_given) 270 write_into_file(outfile, "debug", 0, 0 ); 271 if (args_info->verbose_given) 272 write_into_file(outfile, "verbose", 0, 0 ); 273 if (args_info->username_given) 274 write_into_file(outfile, "username", args_info->username_orig, 0); 275 if (args_info->nouser_given) 276 write_into_file(outfile, "nouser", 0, 0 ); 277 278 279 i = EXIT_SUCCESS; 280 return i; 281 } 282 283 int 284 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info) 285 { 286 FILE *outfile; 287 int i = 0; 288 289 outfile = fopen(filename, "w"); 290 291 if (!outfile) 292 { 293 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename); 294 return EXIT_FAILURE; 295 } 296 297 i = cmdline_parser_dump(outfile, args_info); 298 fclose (outfile); 299 300 return i; 301 } 302 303 void 304 cmdline_parser_free (struct gengetopt_args_info *args_info) 305 { 306 cmdline_parser_release (args_info); 307 } 308 309 /** @brief replacement of strdup, which is not standard */ 310 char * 311 gengetopt_strdup (const char *s) 312 { 313 char *result = 0; 314 if (!s) 315 return result; 316 317 result = (char*)malloc(strlen(s) + 1); 318 if (result == (char*)0) 319 return (char*)0; 320 strcpy(result, s); 321 return result; 322 } 323 324 static void 325 reset_group_user(struct gengetopt_args_info *args_info) 326 { 327 if (! args_info->user_group_counter) 328 return; 329 330 args_info->username_given = 0 ; 331 free_string_field (&(args_info->username_arg)); 332 free_string_field (&(args_info->username_orig)); 333 args_info->nouser_given = 0 ; 334 335 args_info->user_group_counter = 0; 336 } 337 338 int 339 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info) 340 { 341 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1); 342 } 343 344 int 345 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info, 346 struct cmdline_parser_params *params) 347 { 348 int result; 349 result = cmdline_parser_internal (argc, argv, args_info, params, 0); 350 351 if (result == EXIT_FAILURE) 352 { 353 cmdline_parser_free (args_info); 354 exit (EXIT_FAILURE); 355 } 356 357 return result; 358 } 359 360 int 361 cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required) 362 { 363 int result; 364 struct cmdline_parser_params params; 365 366 params.override = override; 367 params.initialize = initialize; 368 params.check_required = check_required; 369 params.check_ambiguity = 0; 370 params.print_errors = 1; 371 372 result = cmdline_parser_internal (argc, argv, args_info, ¶ms, 0); 373 374 if (result == EXIT_FAILURE) 375 { 376 cmdline_parser_free (args_info); 377 exit (EXIT_FAILURE); 378 } 379 380 return result; 381 } 382 383 int 384 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name) 385 { 386 FIX_UNUSED (args_info); 387 FIX_UNUSED (prog_name); 388 return EXIT_SUCCESS; 389 } 390 391 392 static char *package_name = 0; 393 394 /** 395 * @brief updates an option 396 * @param field the generic pointer to the field to update 397 * @param orig_field the pointer to the orig field 398 * @param field_given the pointer to the number of occurrence of this option 399 * @param prev_given the pointer to the number of occurrence already seen 400 * @param value the argument for this option (if null no arg was specified) 401 * @param possible_values the possible values for this option (if specified) 402 * @param default_value the default value (in case the option only accepts fixed values) 403 * @param arg_type the type of this option 404 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity 405 * @param override @see cmdline_parser_params.override 406 * @param no_free whether to free a possible previous value 407 * @param multiple_option whether this is a multiple option 408 * @param long_opt the corresponding long option 409 * @param short_opt the corresponding short option (or '-' if none) 410 * @param additional_error possible further error specification 411 */ 412 static 413 int update_arg(void *field, char **orig_field, 414 unsigned int *field_given, unsigned int *prev_given, 415 char *value, const char *possible_values[], 416 const char *default_value, 417 cmdline_parser_arg_type arg_type, 418 int check_ambiguity, int override, 419 int no_free, int multiple_option, 420 const char *long_opt, char short_opt, 421 const char *additional_error) 422 { 423 char *stop_char = 0; 424 const char *val = value; 425 int found; 426 char **string_field; 427 FIX_UNUSED (field); 428 429 stop_char = 0; 430 found = 0; 431 432 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given))) 433 { 434 if (short_opt != '-') 435 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 436 package_name, long_opt, short_opt, 437 (additional_error ? additional_error : "")); 438 else 439 fprintf (stderr, "%s: `--%s' option given more than once%s\n", 440 package_name, long_opt, 441 (additional_error ? additional_error : "")); 442 return 1; /* failure */ 443 } 444 445 FIX_UNUSED (default_value); 446 447 if (field_given && *field_given && ! override) 448 return 0; 449 if (prev_given) 450 (*prev_given)++; 451 if (field_given) 452 (*field_given)++; 453 if (possible_values) 454 val = possible_values[found]; 455 456 switch(arg_type) { 457 case ARG_FLAG: 458 *((int *)field) = !*((int *)field); 459 break; 460 case ARG_STRING: 461 if (val) { 462 string_field = (char **)field; 463 if (!no_free && *string_field) 464 free (*string_field); /* free previous string */ 465 *string_field = gengetopt_strdup (val); 466 } 467 break; 468 default: 469 break; 470 }; 471 472 473 /* store the original value */ 474 switch(arg_type) { 475 case ARG_NO: 476 case ARG_FLAG: 477 break; 478 default: 479 if (value && orig_field) { 480 if (no_free) { 481 *orig_field = value; 482 } else { 483 if (*orig_field) 484 free (*orig_field); /* free previous string */ 485 *orig_field = gengetopt_strdup (value); 486 } 487 } 488 }; 489 490 return 0; /* OK */ 491 } 492 493 494 int 495 cmdline_parser_internal ( 496 int argc, char **argv, struct gengetopt_args_info *args_info, 497 struct cmdline_parser_params *params, const char *additional_error) 498 { 499 int c; /* Character of the parsed option. */ 500 501 int error_occurred = 0; 502 struct gengetopt_args_info local_args_info; 503 504 int override; 505 int initialize; 506 int check_required; 507 int check_ambiguity; 508 509 package_name = argv[0]; 510 511 override = params->override; 512 initialize = params->initialize; 513 check_required = params->check_required; 514 check_ambiguity = params->check_ambiguity; 515 516 if (initialize) 517 cmdline_parser_init (args_info); 518 519 cmdline_parser_init (&local_args_info); 520 521 optarg = 0; 522 optind = 0; 523 opterr = params->print_errors; 524 optopt = '?'; 525 526 while (1) 527 { 528 int option_index = 0; 529 530 static struct option long_options[] = { 531 { "help", 0, NULL, 'h' }, 532 { "version", 0, NULL, 0 }, 533 { "origin", 1, NULL, 'o' }, 534 { "appid", 1, NULL, 'i' }, 535 { "type", 1, NULL, 't' }, 536 { "resident", 0, NULL, 'r' }, 537 { "no-user-presence", 0, NULL, 'P' }, 538 { "pin-verification", 0, NULL, 'N' }, 539 { "user-verification", 0, NULL, 'V' }, 540 { "debug", 0, NULL, 'd' }, 541 { "verbose", 0, NULL, 'v' }, 542 { "username", 1, NULL, 'u' }, 543 { "nouser", 0, NULL, 'n' }, 544 { 0, 0, 0, 0 } 545 }; 546 547 c = getopt_long (argc, argv, "ho:i:t:rPNVdvu:n", long_options, &option_index); 548 549 if (c == -1) break; /* Exit from `while (1)' loop. */ 550 551 switch (c) 552 { 553 case 'h': /* Print help and exit. */ 554 555 556 if (update_arg( 0 , 557 0 , &(args_info->help_given), 558 &(local_args_info.help_given), optarg, 0, 0, ARG_NO, 559 check_ambiguity, override, 0, 0, 560 "help", 'h', 561 additional_error)) 562 goto failure; 563 cmdline_parser_free (&local_args_info); 564 return 0; 565 566 break; 567 case 'o': /* Origin URL to use during registration. Defaults to pam://hostname. */ 568 569 570 if (update_arg( (void *)&(args_info->origin_arg), 571 &(args_info->origin_orig), &(args_info->origin_given), 572 &(local_args_info.origin_given), optarg, 0, 0, ARG_STRING, 573 check_ambiguity, override, 0, 0, 574 "origin", 'o', 575 additional_error)) 576 goto failure; 577 578 break; 579 case 'i': /* Application ID to use during registration. Defaults to pam://hostname. */ 580 581 582 if (update_arg( (void *)&(args_info->appid_arg), 583 &(args_info->appid_orig), &(args_info->appid_given), 584 &(local_args_info.appid_given), optarg, 0, 0, ARG_STRING, 585 check_ambiguity, override, 0, 0, 586 "appid", 'i', 587 additional_error)) 588 goto failure; 589 590 break; 591 case 't': /* COSE type to use during registration (ES256 or RS256). Defaults to ES256.. */ 592 593 594 if (update_arg( (void *)&(args_info->type_arg), 595 &(args_info->type_orig), &(args_info->type_given), 596 &(local_args_info.type_given), optarg, 0, 0, ARG_STRING, 597 check_ambiguity, override, 0, 0, 598 "type", 't', 599 additional_error)) 600 goto failure; 601 602 break; 603 case 'r': /* Generate a resident credential. */ 604 605 606 if (update_arg((void *)&(args_info->resident_flag), 0, &(args_info->resident_given), 607 &(local_args_info.resident_given), optarg, 0, 0, ARG_FLAG, 608 check_ambiguity, override, 1, 0, "resident", 'r', 609 additional_error)) 610 goto failure; 611 612 break; 613 case 'P': /* Allow the credential to be used without ensuring the user's presence. */ 614 615 616 if (update_arg((void *)&(args_info->no_user_presence_flag), 0, &(args_info->no_user_presence_given), 617 &(local_args_info.no_user_presence_given), optarg, 0, 0, ARG_FLAG, 618 check_ambiguity, override, 1, 0, "no-user-presence", 'P', 619 additional_error)) 620 goto failure; 621 622 break; 623 case 'N': /* Require PIN verification during authentication. */ 624 625 626 if (update_arg((void *)&(args_info->pin_verification_flag), 0, &(args_info->pin_verification_given), 627 &(local_args_info.pin_verification_given), optarg, 0, 0, ARG_FLAG, 628 check_ambiguity, override, 1, 0, "pin-verification", 'N', 629 additional_error)) 630 goto failure; 631 632 break; 633 case 'V': /* Require user verification during authentication. */ 634 635 636 if (update_arg((void *)&(args_info->user_verification_flag), 0, &(args_info->user_verification_given), 637 &(local_args_info.user_verification_given), optarg, 0, 0, ARG_FLAG, 638 check_ambiguity, override, 1, 0, "user-verification", 'V', 639 additional_error)) 640 goto failure; 641 642 break; 643 case 'd': /* Print debug information (highly verbose). */ 644 645 646 if (update_arg((void *)&(args_info->debug_flag), 0, &(args_info->debug_given), 647 &(local_args_info.debug_given), optarg, 0, 0, ARG_FLAG, 648 check_ambiguity, override, 1, 0, "debug", 'd', 649 additional_error)) 650 goto failure; 651 652 break; 653 case 'v': /* Print information about chosen origin and appid. */ 654 655 656 if (update_arg((void *)&(args_info->verbose_flag), 0, &(args_info->verbose_given), 657 &(local_args_info.verbose_given), optarg, 0, 0, ARG_FLAG, 658 check_ambiguity, override, 1, 0, "verbose", 'v', 659 additional_error)) 660 goto failure; 661 662 break; 663 case 'u': /* The name of the user registering the device. Defaults to the current user name. */ 664 665 if (args_info->user_group_counter && override) 666 reset_group_user (args_info); 667 args_info->user_group_counter += 1; 668 669 if (update_arg( (void *)&(args_info->username_arg), 670 &(args_info->username_orig), &(args_info->username_given), 671 &(local_args_info.username_given), optarg, 0, 0, ARG_STRING, 672 check_ambiguity, override, 0, 0, 673 "username", 'u', 674 additional_error)) 675 goto failure; 676 677 break; 678 case 'n': /* Print only registration information (keyHandle and public key). Useful for appending. */ 679 680 if (args_info->user_group_counter && override) 681 reset_group_user (args_info); 682 args_info->user_group_counter += 1; 683 684 if (update_arg( 0 , 685 0 , &(args_info->nouser_given), 686 &(local_args_info.nouser_given), optarg, 0, 0, ARG_NO, 687 check_ambiguity, override, 0, 0, 688 "nouser", 'n', 689 additional_error)) 690 goto failure; 691 692 break; 693 694 case 0: /* Long option with no short option */ 695 if (strcmp (long_options[option_index].name, "version") == 0) { 696 cmdline_parser_print_version (); 697 cmdline_parser_free (&local_args_info); 698 exit (EXIT_SUCCESS); 699 } 700 701 case '?': /* Invalid option. */ 702 /* `getopt_long' already printed an error message. */ 703 goto failure; 704 705 default: /* bug: option not considered. */ 706 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : "")); 707 abort (); 708 } /* switch */ 709 } /* while */ 710 711 if (args_info->user_group_counter > 1) 712 { 713 fprintf (stderr, "%s: %d options of group user were given. At most one is required%s.\n", argv[0], args_info->user_group_counter, (additional_error ? additional_error : "")); 714 error_occurred = 1; 715 } 716 717 718 719 720 cmdline_parser_release (&local_args_info); 721 722 if ( error_occurred ) 723 return (EXIT_FAILURE); 724 725 return 0; 726 727 failure: 728 729 cmdline_parser_release (&local_args_info); 730 return (EXIT_FAILURE); 731 } 732