piv-tool: cleanup

- remove command line option '--card-driver';
- instead force driver 'PIV-II' and fail if card is not a PIV card
- overhaul option parsing
  - remove unused variable 'long_optind'
  - make work option '--reader' ( "r:" was missing in the optstring!!!)
  - bail out with usage message on all unknown/unhandled args
  - correctly terminate option parsing (no infinite loop)
This commit is contained in:
Peter Marschall 2020-01-06 17:17:09 +01:00
parent 5514a0529f
commit 04f4f589a1
2 changed files with 17 additions and 45 deletions

View File

@ -163,19 +163,6 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>--card-driver</option> <replaceable>driver</replaceable>,
<option>-c</option> <replaceable>driver</replaceable>
</term>
<listitem><para>
Use the given card driver.
The default is to auto-detect the correct card driver.
The literal value <literal>?</literal> lists
all available card drivers and terminates
<command>piv-tool</command>.
</para></listitem>
</varlistentry>
<varlistentry>
<term>
<option>--wait</option>,

View File

@ -52,6 +52,7 @@
#include "libopensc/opensc.h"
#include "libopensc/cardctl.h"
#include "libopensc/cards.h"
#include "libopensc/asn1.h"
#include "util.h"
#include "libopensc/sc-ossl-compat.h"
@ -80,7 +81,6 @@ static const struct option options[] = {
{ "in", 1, NULL, 'i' },
{ "send-apdu", 1, NULL, 's' },
{ "reader", 1, NULL, 'r' },
{ "card-driver", 1, NULL, 'c' },
{ "wait", 0, NULL, 'w' },
{ "verbose", 0, NULL, 'v' },
{ NULL, 0, NULL, 0 }
@ -98,7 +98,6 @@ static const char *option_help[] = {
"Input file for cert",
"Sends an APDU in format AA:BB:CC:DD:EE:FF...",
"Uses reader number <arg> [0]",
"Forces the use of driver <arg> [auto-detect; '?' for list]",
"Wait for a card to be inserted",
"Verbose operation. Use several times to enable debug output.",
};
@ -469,7 +468,7 @@ static void print_serial(sc_card_t *in_card)
int main(int argc, char *argv[])
{
int err = 0, r, c, long_optind = 0;
int err = 0, r, c;
int do_send_apdu = 0;
int do_admin_mode = 0;
int do_gen_key = 0;
@ -478,9 +477,7 @@ int main(int argc, char *argv[])
int compress_cert = 0;
int do_print_serial = 0;
int do_print_name = 0;
int do_list_card_drivers = 0;
int action_count = 0;
const char *opt_driver = NULL;
const char *out_file = NULL;
const char *in_file = NULL;
const char *cert_id = NULL;
@ -490,12 +487,7 @@ int main(int argc, char *argv[])
sc_context_param_t ctx_param;
char **old_apdus = NULL;
while (1) {
c = getopt_long(argc, argv, "nA:G:O:Z:C:i:o:fvs:c:w", options, &long_optind);
if (c == -1)
break;
if (c == '?')
util_print_usage_and_die(app_name, options, option_help, NULL);
while ((c = getopt_long(argc, argv, "nA:G:O:Z:C:i:o:r:fvs:c:w", options, (int *) 0)) != -1) {
switch (c) {
case OPT_SERIAL:
do_print_serial = 1;
@ -555,19 +547,11 @@ int main(int argc, char *argv[])
case 'v':
verbose++;
break;
case 'c':
opt_driver = optarg;
/* special card driver value "?" means: list available drivers */
if (opt_driver != NULL && strncmp("?", opt_driver, sizeof("?")) == 0) {
opt_driver = NULL;
do_list_card_drivers = 1;
action_count++;
}
break;
case 'w':
opt_wait = 1;
break;
default:
util_print_usage_and_die(app_name, options, option_help, NULL);
}
}
@ -612,24 +596,25 @@ int main(int argc, char *argv[])
if (action_count <= 0)
goto end;
if (do_list_card_drivers) {
err = util_list_card_drivers(ctx);
/* force PIV card driver */
err = sc_set_card_driver(ctx, "PIV-II");
if (err) {
fprintf(stderr, "PIV card driver not found!\n");
err = 1;
goto end;
}
if (opt_driver != NULL) {
err = sc_set_card_driver(ctx, opt_driver);
if (err) {
fprintf(stderr, "Driver '%s' not found!\n", opt_driver);
err = 1;
goto end;
}
}
err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
if (err)
goto end;
/* fail if card is not a PIV card */
if (card->type < SC_CARD_TYPE_PIV_II_BASE || card->type >= SC_CARD_TYPE_PIV_II_BASE+1000) {
fprintf(stderr, "Card type %X: not a PIV card\n", card->type);
err = 1;
goto end;
}
if (do_admin_mode) {
if ((err = admin_mode(admin_info)))
goto end;