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:
parent
5514a0529f
commit
04f4f589a1
@ -163,19 +163,6 @@
|
|||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</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>
|
<varlistentry>
|
||||||
<term>
|
<term>
|
||||||
<option>--wait</option>,
|
<option>--wait</option>,
|
||||||
|
@ -52,6 +52,7 @@
|
|||||||
|
|
||||||
#include "libopensc/opensc.h"
|
#include "libopensc/opensc.h"
|
||||||
#include "libopensc/cardctl.h"
|
#include "libopensc/cardctl.h"
|
||||||
|
#include "libopensc/cards.h"
|
||||||
#include "libopensc/asn1.h"
|
#include "libopensc/asn1.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "libopensc/sc-ossl-compat.h"
|
#include "libopensc/sc-ossl-compat.h"
|
||||||
@ -80,7 +81,6 @@ static const struct option options[] = {
|
|||||||
{ "in", 1, NULL, 'i' },
|
{ "in", 1, NULL, 'i' },
|
||||||
{ "send-apdu", 1, NULL, 's' },
|
{ "send-apdu", 1, NULL, 's' },
|
||||||
{ "reader", 1, NULL, 'r' },
|
{ "reader", 1, NULL, 'r' },
|
||||||
{ "card-driver", 1, NULL, 'c' },
|
|
||||||
{ "wait", 0, NULL, 'w' },
|
{ "wait", 0, NULL, 'w' },
|
||||||
{ "verbose", 0, NULL, 'v' },
|
{ "verbose", 0, NULL, 'v' },
|
||||||
{ NULL, 0, NULL, 0 }
|
{ NULL, 0, NULL, 0 }
|
||||||
@ -98,7 +98,6 @@ static const char *option_help[] = {
|
|||||||
"Input file for cert",
|
"Input file for cert",
|
||||||
"Sends an APDU in format AA:BB:CC:DD:EE:FF...",
|
"Sends an APDU in format AA:BB:CC:DD:EE:FF...",
|
||||||
"Uses reader number <arg> [0]",
|
"Uses reader number <arg> [0]",
|
||||||
"Forces the use of driver <arg> [auto-detect; '?' for list]",
|
|
||||||
"Wait for a card to be inserted",
|
"Wait for a card to be inserted",
|
||||||
"Verbose operation. Use several times to enable debug output.",
|
"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 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_send_apdu = 0;
|
||||||
int do_admin_mode = 0;
|
int do_admin_mode = 0;
|
||||||
int do_gen_key = 0;
|
int do_gen_key = 0;
|
||||||
@ -478,9 +477,7 @@ int main(int argc, char *argv[])
|
|||||||
int compress_cert = 0;
|
int compress_cert = 0;
|
||||||
int do_print_serial = 0;
|
int do_print_serial = 0;
|
||||||
int do_print_name = 0;
|
int do_print_name = 0;
|
||||||
int do_list_card_drivers = 0;
|
|
||||||
int action_count = 0;
|
int action_count = 0;
|
||||||
const char *opt_driver = NULL;
|
|
||||||
const char *out_file = NULL;
|
const char *out_file = NULL;
|
||||||
const char *in_file = NULL;
|
const char *in_file = NULL;
|
||||||
const char *cert_id = NULL;
|
const char *cert_id = NULL;
|
||||||
@ -490,12 +487,7 @@ int main(int argc, char *argv[])
|
|||||||
sc_context_param_t ctx_param;
|
sc_context_param_t ctx_param;
|
||||||
char **old_apdus = NULL;
|
char **old_apdus = NULL;
|
||||||
|
|
||||||
while (1) {
|
while ((c = getopt_long(argc, argv, "nA:G:O:Z:C:i:o:r:fvs:c:w", options, (int *) 0)) != -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);
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case OPT_SERIAL:
|
case OPT_SERIAL:
|
||||||
do_print_serial = 1;
|
do_print_serial = 1;
|
||||||
@ -555,19 +547,11 @@ int main(int argc, char *argv[])
|
|||||||
case 'v':
|
case 'v':
|
||||||
verbose++;
|
verbose++;
|
||||||
break;
|
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':
|
case 'w':
|
||||||
opt_wait = 1;
|
opt_wait = 1;
|
||||||
break;
|
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)
|
if (action_count <= 0)
|
||||||
goto end;
|
goto end;
|
||||||
|
|
||||||
if (do_list_card_drivers) {
|
/* force PIV card driver */
|
||||||
err = util_list_card_drivers(ctx);
|
err = sc_set_card_driver(ctx, "PIV-II");
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opt_driver != NULL) {
|
|
||||||
err = sc_set_card_driver(ctx, opt_driver);
|
|
||||||
if (err) {
|
if (err) {
|
||||||
fprintf(stderr, "Driver '%s' not found!\n", opt_driver);
|
fprintf(stderr, "PIV card driver not found!\n");
|
||||||
err = 1;
|
err = 1;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
|
err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
|
||||||
if (err)
|
if (err)
|
||||||
goto end;
|
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 (do_admin_mode) {
|
||||||
if ((err = admin_mode(admin_info)))
|
if ((err = admin_mode(admin_info)))
|
||||||
goto end;
|
goto end;
|
||||||
|
Loading…
Reference in New Issue
Block a user