cardos-tool: cleanup

- remove command line option '--card-driver';
- instead force driver 'cardos' and fail if card is not a CardOS card
- overhaul option parsing
      - remove unused variable 'long_optind'
      - 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 18:03:19 +01:00
parent 04f4f589a1
commit 58ecb4aba2
2 changed files with 17 additions and 45 deletions

View File

@ -33,19 +33,6 @@ smart cards and similar security tokens based on Siemens Card/OS M4.
<title>Options</title> <title>Options</title>
<para> <para>
<variablelist> <variablelist>
<varlistentry>
<term>
<option>--card-driver</option> <replaceable>name</replaceable>,
<option>-c</option> <replaceable>name</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>cardos-tool</command>.
</para></listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term> <term>
<option>--format</option>, <option>--format</option>,

View File

@ -39,6 +39,7 @@
#endif #endif
#include "libopensc/opensc.h" #include "libopensc/opensc.h"
#include "libopensc/cards.h"
#include "util.h" #include "util.h"
static const char *app_name = "cardos-tool"; static const char *app_name = "cardos-tool";
@ -54,7 +55,6 @@ static const struct option options[] = {
{"startkey", 1, NULL, 's'}, {"startkey", 1, NULL, 's'},
{"change-startkey", 1, NULL, 'S'}, {"change-startkey", 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}
@ -67,7 +67,6 @@ static const char *option_help[] = {
"Specify startkey for format", "Specify startkey for format",
"Change Startkey with given APDU command", "Change Startkey with given APDU command",
"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.",
}; };
@ -1034,22 +1033,16 @@ static int cardos_change_startkey(const char *change_startkey_apdu) {
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_info = 0; int do_info = 0;
int do_format = 0; int do_format = 0;
int do_change_startkey = 0; int do_change_startkey = 0;
int do_list_card_drivers = 0;
int action_count = 0; int action_count = 0;
const char *opt_driver = NULL;
const char *opt_startkey = NULL; const char *opt_startkey = NULL;
const char *opt_change_startkey = NULL; const char *opt_change_startkey = NULL;
sc_context_param_t ctx_param; sc_context_param_t ctx_param;
while (1) { while ((c = getopt_long(argc, argv, "hifs:r:vdwS:", options, (int *) 0)) != -1) {
c = getopt_long(argc, argv, "hifs:r:vdc:wS:", options,
&long_optind);
if (c == -1)
break;
switch (c) { switch (c) {
case 'h': case 'h':
printf("NB! This tool is only for Siemens CardOS based cards!\n\n"); printf("NB! This tool is only for Siemens CardOS based cards!\n\n");
@ -1076,19 +1069,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);
} }
} }
@ -1103,25 +1088,25 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if (do_list_card_drivers) { /* force CardOS card driver */
err = util_list_card_drivers(ctx); err = sc_set_card_driver(ctx, "cardos");
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", fprintf(stderr, "CardOS card driver not found!\n");
opt_driver);
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 CardOS card */
if (card->type < SC_CARD_TYPE_CARDOS_BASE || card->type >= SC_CARD_TYPE_CARDOS_BASE+1000) {
fprintf(stderr, "Card type %X: not a CardOS card\n", card->type);
err = 1;
goto end;
}
if (do_info) { if (do_info) {
if ((err = cardos_info())) { if ((err = cardos_info())) {
goto end; goto end;