tools: remove slots; implement change in sc_disconnect_card(); convert util_connect_card()/--wait to support the changes in r3931

git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@3933 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
martin 2010-01-24 15:29:47 +00:00
parent e191142d04
commit 5a0cc50123
15 changed files with 101 additions and 121 deletions

View File

@ -40,7 +40,7 @@ pkcs15_init_LDADD = $(OPTIONAL_OPENSSL_LIBS) \
$(top_builddir)/src/pkcs15init/libpkcs15init.la $(top_builddir)/src/pkcs15init/libpkcs15init.la
cardos_tool_SOURCES = cardos-tool.c util.c cardos_tool_SOURCES = cardos-tool.c util.c
cardos_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS) cardos_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS)
eidenv_SOURCES = eidenv.c eidenv_SOURCES = eidenv.c util.c
netkey_tool_SOURCES = netkey-tool.c netkey_tool_SOURCES = netkey-tool.c
netkey_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS) netkey_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS)
rutoken_tool_SOURCES = rutoken-tool.c util.c rutoken_tool_SOURCES = rutoken-tool.c util.c

View File

@ -44,8 +44,9 @@
static const char *app_name = "cardos-tool"; static const char *app_name = "cardos-tool";
static int opt_reader = -1, opt_debug = 0, opt_wait = 0; static int opt_debug = 0, opt_wait = 0;
static int verbose = 0; static int verbose = 0;
static char *opt_reader = NULL;
static const struct option options[] = { static const struct option options[] = {
{"info", 0, NULL, 'i'}, {"info", 0, NULL, 'i'},
@ -379,7 +380,7 @@ static int cardos_sm4h(const unsigned char *in, size_t inlen, unsigned char
unsigned char *mac_input, *enc_input; unsigned char *mac_input, *enc_input;
DES_key_schedule ks_a, ks_b; DES_key_schedule ks_a, ks_b;
DES_cblock des_in,des_out; DES_cblock des_in,des_out;
int i,j; unsigned int i,j;
if (keylen != 16) { if (keylen != 16) {
printf("key has wrong size, need 16 bytes, got %zd. aborting.\n", printf("key has wrong size, need 16 bytes, got %zd. aborting.\n",
@ -1098,7 +1099,7 @@ int main(int argc, char *const argv[])
action_count++; action_count++;
break; break;
case 'r': case 'r':
opt_reader = atoi(optarg); opt_reader = optarg;
break; break;
case 'v': case 'v':
verbose++; verbose++;
@ -1137,7 +1138,7 @@ int main(int argc, char *const argv[])
} }
} }
err = util_connect_card(ctx, &card, opt_reader, 0, opt_wait, verbose); err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
if (err) if (err)
goto end; goto end;
@ -1162,7 +1163,7 @@ int main(int argc, char *const argv[])
end: end:
if (card) { if (card) {
sc_unlock(card); sc_unlock(card);
sc_disconnect_card(card, 0); sc_disconnect_card(card);
} }
if (ctx) if (ctx)
sc_release_context(ctx); sc_release_context(ctx);

View File

@ -1180,14 +1180,14 @@ int main(int argc, char * const argv[])
err = 1; err = 1;
goto end; goto end;
} }
if (sc_detect_card_presence(screader, 0) <= 0) { if (sc_detect_card_presence(screader) <= 0) {
fprintf(stderr, "Card not present.\n"); fprintf(stderr, "Card not present.\n");
err = 3; err = 3;
goto end; goto end;
} }
if (verbose) if (verbose)
fprintf(stderr, "Connecting to card in reader %s...\n", screader->name); fprintf(stderr, "Connecting to card in reader %s...\n", screader->name);
r = sc_connect_card(screader, 0, &card); r = sc_connect_card(screader, &card);
if (r) { if (r) {
fprintf(stderr, "Failed to connect to card: %s\n", sc_strerror(r)); fprintf(stderr, "Failed to connect to card: %s\n", sc_strerror(r));
err = 1; err = 1;
@ -1237,7 +1237,7 @@ int main(int argc, char * const argv[])
end: end:
if (card) { if (card) {
sc_unlock(card); sc_unlock(card);
sc_disconnect_card(card, 0); sc_disconnect_card(card);
} }
if (ctx) if (ctx)
sc_release_context(ctx); sc_release_context(ctx);

View File

@ -35,17 +35,19 @@
#include "../libopensc/cards.h" #include "../libopensc/cards.h"
#include "../libopensc/esteid.h" #include "../libopensc/esteid.h"
static int reader_num = 0; static char *opt_reader = NULL;
static int stats = 0; static int stats = 0;
static int opt_wait = 0;
static char *exec_program = NULL; static char *exec_program = NULL;
static int exit_status = EXIT_FAILURE; static int exit_status = EXIT_FAILURE;
static const struct option options[] = { static const struct option options[] = {
{"reader", required_argument, NULL, 'r'}, {"reader", required_argument, NULL, 'r'},
{"print", no_argument, NULL, 'n'}, {"print", no_argument, NULL, 'p'},
{"exec", required_argument, NULL, 'x'}, {"exec", required_argument, NULL, 'x'},
{"stats", no_argument, NULL, 't'}, {"stats", no_argument, NULL, 't'},
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"wait", no_argument, NULL, 'w'},
{"version", no_argument, NULL, 'V'}, {"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
@ -93,7 +95,8 @@ static void show_help(void)
"-h --help - show this text and exit\n" "-h --help - show this text and exit\n"
"-v --version - show version and exit\n" "-v --version - show version and exit\n"
"-r --reader - the reader to use\n" "-r --reader - the reader to use\n"
"-n --print - print the datafile\n" "-w --wait - wait for a card to be inserted\n"
"-p --print - print the datafile\n"
"-t --stats - show usage counts of keys\n" "-t --stats - show usage counts of keys\n"
"-x --exec - execute a program with data in env vars.\n"); "-x --exec - execute a program with data in env vars.\n");
} }
@ -102,11 +105,11 @@ static void decode_options(int argc, char **argv)
{ {
int c; int c;
while ((c = getopt_long(argc, argv,"ptr:x:hV", options, (int *) 0)) != EOF) { while ((c = getopt_long(argc, argv,"pwtr:x:hV", options, (int *) 0)) != EOF) {
switch (c) { switch (c) {
case 'r': case 'r':
reader_num = atoi(optarg); opt_reader = optarg;
break; break;
case 't': case 't':
stats = !stats; stats = !stats;
@ -120,7 +123,10 @@ static void decode_options(int argc, char **argv)
show_help(); show_help();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case 'n': case 'p':
break;
case 'w':
opt_wait = 1;
break; break;
case 'V': case 'V':
show_version(); show_version();
@ -375,7 +381,6 @@ int main(int argc, char **argv)
{ {
sc_context_t *ctx = NULL; sc_context_t *ctx = NULL;
sc_context_param_t ctx_param; sc_context_param_t ctx_param;
sc_reader_t *reader = NULL;
sc_card_t *card = NULL; sc_card_t *card = NULL;
int r; int r;
@ -393,13 +398,7 @@ int main(int argc, char **argv)
sc_strerror(r)); sc_strerror(r));
return 1; return 1;
} }
if (reader_num > (int)sc_ctx_get_reader_count(ctx)) { r = util_connect_card(ctx, &card, opt_reader, opt_wait, 0);
fprintf(stderr, "Illegal reader number. Only %d reader(s) configured.\n", sc_ctx_get_reader_count(ctx));
return 1;
}
reader = sc_ctx_get_reader(ctx, (unsigned int)reader_num);
r = sc_connect_card(reader, 0, &card);
if (r) { if (r) {
fprintf(stderr, "Failed to connect to card: %s\n", sc_strerror(r)); fprintf(stderr, "Failed to connect to card: %s\n", sc_strerror(r));
return 1; return 1;
@ -424,7 +423,7 @@ int main(int argc, char **argv)
if (exec_program) { if (exec_program) {
char *const largv[] = {exec_program, NULL}; char *const largv[] = {exec_program, NULL};
sc_unlock(card); sc_unlock(card);
sc_disconnect_card(card, 0); sc_disconnect_card(card);
sc_release_context(ctx); sc_release_context(ctx);
execv(exec_program, largv); execv(exec_program, largv);
/* we should not get here */ /* we should not get here */
@ -434,7 +433,7 @@ int main(int argc, char **argv)
out: out:
sc_unlock(card); sc_unlock(card);
sc_disconnect_card(card, 0); sc_disconnect_card(card);
sc_release_context(ctx); sc_release_context(ctx);
exit(exit_status); exit(exit_status);
} }

View File

@ -559,19 +559,13 @@ int main(
exit(1); exit(1);
} }
printf("%d Reader detected\n", sc_ctx_get_reader_count(ctx)); printf("%d Readers detected\n", sc_ctx_get_reader_count(ctx));
for(i=0; i < sc_ctx_get_reader_count(ctx); ++i){
sc_reader_t *myreader = sc_ctx_get_reader(ctx, i);
printf("%lu: %s, Driver: %s, %d Slot(s)\n",
(unsigned long) i, myreader->name,
myreader->driver->name, myreader->slot_count);
}
if(reader < 0 || reader >= (int)sc_ctx_get_reader_count(ctx)){ if(reader < 0 || reader >= (int)sc_ctx_get_reader_count(ctx)){
fprintf(stderr,"Cannot open reader %d\n", reader); fprintf(stderr,"Cannot open reader %d\n", reader);
exit(1); exit(1);
} }
if((r = sc_connect_card(sc_ctx_get_reader(ctx, 0), 0, &card))<0){ if((r = sc_connect_card(sc_ctx_get_reader(ctx, 0), &card))<0){
fprintf(stderr,"Connect-Card failed: %s\n", sc_strerror(r)); fprintf(stderr,"Connect-Card failed: %s\n", sc_strerror(r));
exit(1); exit(1);
} }
@ -621,7 +615,7 @@ int main(
if(do_unblock+do_change+do_nullpin+do_readcert==0) show_certs(card); if(do_unblock+do_change+do_nullpin+do_readcert==0) show_certs(card);
sc_unlock(card); sc_unlock(card);
sc_disconnect_card(card,0); sc_disconnect_card(card);
sc_release_context(ctx); sc_release_context(ctx);
exit(0); exit(0);

View File

@ -38,8 +38,9 @@
static const char *app_name = "opensc-explorer"; static const char *app_name = "opensc-explorer";
static int opt_reader = -1, opt_wait = 0, verbose = 0; static int opt_wait = 0, verbose = 0;
static const char *opt_driver = NULL; static const char *opt_driver = NULL;
static const char *opt_reader = NULL;
static sc_file_t *current_file = NULL; static sc_file_t *current_file = NULL;
static sc_path_t current_path; static sc_path_t current_path;
@ -74,7 +75,7 @@ static void die(int ret)
sc_file_free(current_file); sc_file_free(current_file);
if (card) { if (card) {
sc_unlock(card); sc_unlock(card);
sc_disconnect_card(card, 0); sc_disconnect_card(card);
} }
if (ctx) if (ctx)
sc_release_context(ctx); sc_release_context(ctx);
@ -635,7 +636,7 @@ static int do_verify(int argc, char **argv)
} }
if (argc < 2) { if (argc < 2) {
if (!(card->reader->slot[0].capabilities & SC_SLOT_CAP_PIN_PAD)) { if (!(card->reader->capabilities & SC_READER_CAP_PIN_PAD)) {
printf("Card reader or driver doesn't support PIN PAD\n"); printf("Card reader or driver doesn't support PIN PAD\n");
return -1; return -1;
} }
@ -1567,7 +1568,7 @@ int main(int argc, char * const argv[])
util_print_usage_and_die(app_name, options, option_help); util_print_usage_and_die(app_name, options, option_help);
switch (c) { switch (c) {
case 'r': case 'r':
opt_reader = atoi(optarg); opt_reader = optarg;
break; break;
case 'c': case 'c':
opt_driver = optarg; opt_driver = optarg;
@ -1602,7 +1603,7 @@ int main(int argc, char * const argv[])
} }
} }
err = util_connect_card(ctx, &card, opt_reader, 0, opt_wait, 0); err = util_connect_card(ctx, &card, opt_reader, opt_wait, 0);
if (err) if (err)
goto end; goto end;

View File

@ -36,9 +36,9 @@
static const char *app_name = "opensc-tool"; static const char *app_name = "opensc-tool";
static int opt_reader = -1, static int opt_wait = 0;
opt_wait = 0;
static char ** opt_apdus; static char ** opt_apdus;
static char *opt_reader;
static int opt_apdu_count = 0; static int opt_apdu_count = 0;
static int verbose = 0; static int verbose = 0;
@ -262,7 +262,7 @@ static int list_readers(void)
for (i = 0; i < rcount; i++) { for (i = 0; i < rcount; i++) {
sc_reader_t *screader = sc_ctx_get_reader(ctx, i); sc_reader_t *screader = sc_ctx_get_reader(ctx, i);
printf("%-7d%-11s%-10s%s\n", i, screader->driver->short_name, printf("%-7d%-11s%-10s%s\n", i, screader->driver->short_name,
screader->slot[0].capabilities & SC_SLOT_CAP_PIN_PAD ? "PINpad":"", screader->capabilities & SC_READER_CAP_PIN_PAD ? "PINpad":"",
screader->name); screader->name);
} }
return 0; return 0;
@ -616,7 +616,7 @@ int main(int argc, char * const argv[])
action_count++; action_count++;
break; break;
case 'r': case 'r':
opt_reader = atoi(optarg); opt_reader = optarg;
break; break;
case 'v': case 'v':
verbose++; verbose++;
@ -689,7 +689,7 @@ int main(int argc, char * const argv[])
} }
} }
err = util_connect_card(ctx, &card, opt_reader, 0, opt_wait, verbose); err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
if (err) if (err)
goto end; goto end;
@ -730,7 +730,7 @@ int main(int argc, char * const argv[])
end: end:
if (card) { if (card) {
sc_unlock(card); sc_unlock(card);
sc_disconnect_card(card, 0); sc_disconnect_card(card);
} }
if (ctx) if (ctx)
sc_release_context(ctx); sc_release_context(ctx);

View File

@ -43,9 +43,9 @@
static const char *app_name = "piv-tool"; static const char *app_name = "piv-tool";
static int opt_reader = -1, static int opt_wait = 0;
opt_wait = 0;
static char ** opt_apdus; static char ** opt_apdus;
static char * opt_reader;
static int opt_apdu_count = 0; static int opt_apdu_count = 0;
static int verbose = 0; static int verbose = 0;
@ -426,7 +426,7 @@ int main(int argc, char * const argv[])
out_file = optarg; out_file = optarg;
break; break;
case 'r': case 'r':
opt_reader = atoi(optarg); opt_reader = optarg;
break; break;
case 'v': case 'v':
verbose++; verbose++;
@ -475,7 +475,7 @@ int main(int argc, char * const argv[])
} }
} }
err = util_connect_card(ctx, &card, opt_reader, 0, opt_wait, verbose); err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
if (err) if (err)
goto end; goto end;
@ -517,7 +517,7 @@ end:
BIO_free(bp); BIO_free(bp);
if (card) { if (card) {
sc_unlock(card); sc_unlock(card);
sc_disconnect_card(card, 0); sc_disconnect_card(card);
} }
if (ctx) if (ctx)
sc_release_context(ctx); sc_release_context(ctx);

View File

@ -40,7 +40,8 @@
static const char *app_name = "pkcs15-crypt"; static const char *app_name = "pkcs15-crypt";
static int opt_reader = -1, verbose = 0, opt_wait = 0, opt_raw = 0; static int verbose = 0, opt_wait = 0, opt_raw = 0;
static char * opt_reader;
static char * opt_pincode = NULL, * opt_key_id = NULL; static char * opt_pincode = NULL, * opt_key_id = NULL;
static char * opt_input = NULL, * opt_output = NULL; static char * opt_input = NULL, * opt_output = NULL;
static int opt_crypt_flags = 0; static int opt_crypt_flags = 0;
@ -466,7 +467,7 @@ static int get_key(unsigned int usage, sc_pkcs15_object_t **result)
pincode = get_pin(pin); pincode = get_pin(pin);
if (((pincode == NULL || *pincode == '\0')) && if (((pincode == NULL || *pincode == '\0')) &&
!(p15card->card->slot->capabilities & SC_SLOT_CAP_PIN_PAD)) !(p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD))
return 5; return 5;
r = sc_pkcs15_verify_pin(p15card, (struct sc_pkcs15_pin_info *) pin->data, r = sc_pkcs15_verify_pin(p15card, (struct sc_pkcs15_pin_info *) pin->data,
@ -513,7 +514,7 @@ int main(int argc, char * const argv[])
action_count++; action_count++;
break; break;
case 'r': case 'r':
opt_reader = atoi(optarg); opt_reader = optarg;
break; break;
case 'i': case 'i':
opt_input = optarg; opt_input = optarg;
@ -571,7 +572,7 @@ int main(int argc, char * const argv[])
if (verbose > 1) if (verbose > 1)
ctx->debug = verbose-1; ctx->debug = verbose-1;
err = util_connect_card(ctx, &card, opt_reader, 0, opt_wait, verbose); err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
if (err) if (err)
goto end; goto end;
@ -607,7 +608,7 @@ end:
#if 1 #if 1
sc_unlock(card); sc_unlock(card);
#endif #endif
sc_disconnect_card(card, 0); sc_disconnect_card(card);
} }
if (ctx) if (ctx)
sc_release_context(ctx); sc_release_context(ctx);

View File

@ -79,7 +79,7 @@ typedef int (*pkcs15_encoder)(sc_context_t *,
struct sc_pkcs15_card *, u8 **, size_t *); struct sc_pkcs15_card *, u8 **, size_t *);
/* Local functions */ /* Local functions */
static int open_reader_and_card(int); static int open_reader_and_card(char *);
static int do_assert_pristine(sc_card_t *); static int do_assert_pristine(sc_card_t *);
static int do_erase(sc_card_t *, struct sc_profile *); static int do_erase(sc_card_t *, struct sc_profile *);
static int do_delete_objects(struct sc_profile *, unsigned int myopt_delete_flags); static int do_delete_objects(struct sc_profile *, unsigned int myopt_delete_flags);
@ -310,9 +310,9 @@ struct secret {
static sc_context_t * ctx = NULL; static sc_context_t * ctx = NULL;
static sc_card_t * card = NULL; static sc_card_t * card = NULL;
static struct sc_pkcs15_card * p15card = NULL; static struct sc_pkcs15_card * p15card = NULL;
static char * opt_reader = NULL;
static unsigned int opt_actions; static unsigned int opt_actions;
static int opt_reader = -1, static int opt_extractable = 0,
opt_extractable = 0,
opt_unprotected = 0, opt_unprotected = 0,
opt_authority = 0, opt_authority = 0,
opt_softkeygen = 0, opt_softkeygen = 0,
@ -489,14 +489,14 @@ out:
} }
if (card) { if (card) {
sc_unlock(card); sc_unlock(card);
sc_disconnect_card(card, 0); sc_disconnect_card(card);
} }
sc_release_context(ctx); sc_release_context(ctx);
return r < 0? 1 : 0; return r < 0? 1 : 0;
} }
static int static int
open_reader_and_card(int reader) open_reader_and_card(char *reader)
{ {
int r; int r;
sc_context_param_t ctx_param; sc_context_param_t ctx_param;
@ -515,7 +515,7 @@ open_reader_and_card(int reader)
ctx->debug_file = stderr; ctx->debug_file = stderr;
} }
if (util_connect_card(ctx, &card, reader, 0, opt_wait, verbose)) if (util_connect_card(ctx, &card, reader, opt_wait, verbose))
return 0; return 0;
return 1; return 1;
@ -2524,7 +2524,7 @@ handle_option(const struct option *opt)
opt_card_profile = optarg; opt_card_profile = optarg;
break; break;
case 'r': case 'r':
opt_reader = atoi(optarg); opt_reader = optarg;
break; break;
case 'u': case 'u':
parse_x509_usage(optarg, &opt_x509_usage); parse_x509_usage(optarg, &opt_x509_usage);

View File

@ -42,9 +42,10 @@ typedef unsigned __int32 uint32_t;
static const char *app_name = "pkcs15-tool"; static const char *app_name = "pkcs15-tool";
static int opt_reader = -1, opt_wait = 0; static int opt_wait = 0;
static int opt_no_cache = 0; static int opt_no_cache = 0;
static char * opt_auth_id; static char * opt_auth_id;
static char * opt_reader = NULL;
static char * opt_cert = NULL; static char * opt_cert = NULL;
static char * opt_data = NULL; static char * opt_data = NULL;
static char * opt_pubkey = NULL; static char * opt_pubkey = NULL;
@ -987,7 +988,7 @@ static int unblock_pin(void)
u8 *pin, *puk; u8 *pin, *puk;
int r, pinpad_present = 0; int r, pinpad_present = 0;
pinpad_present = p15card->card->reader->slot[0].capabilities & SC_SLOT_CAP_PIN_PAD; pinpad_present = p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD;
if (!(pin_obj = get_pin_info())) if (!(pin_obj = get_pin_info()))
return 2; return 2;
@ -1050,7 +1051,7 @@ static int change_pin(void)
u8 *pincode, *newpin; u8 *pincode, *newpin;
int r, pinpad_present = 0; int r, pinpad_present = 0;
pinpad_present = p15card->card->reader->slot[0].capabilities & SC_SLOT_CAP_PIN_PAD; pinpad_present = p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD;
if (!(pin_obj = get_pin_info())) if (!(pin_obj = get_pin_info()))
return 2; return 2;
@ -1516,7 +1517,7 @@ int main(int argc, char * const argv[])
action_count++; action_count++;
break; break;
case OPT_READER: case OPT_READER:
opt_reader = atoi(optarg); opt_reader = optarg;
break; break;
case OPT_PIN: case OPT_PIN:
opt_pin = (u8 *) optarg; opt_pin = (u8 *) optarg;
@ -1559,7 +1560,7 @@ int main(int argc, char * const argv[])
if (verbose > 1 ) if (verbose > 1 )
ctx->debug = verbose-1; ctx->debug = verbose-1;
err = util_connect_card(ctx, &card, opt_reader, 0, opt_wait, verbose); err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
if (err) if (err)
goto end; goto end;
@ -1659,7 +1660,7 @@ end:
sc_pkcs15_unbind(p15card); sc_pkcs15_unbind(p15card);
if (card) { if (card) {
sc_unlock(card); sc_unlock(card);
sc_disconnect_card(card, 0); sc_disconnect_card(card);
} }
if (ctx) if (ctx)
sc_release_context(ctx); sc_release_context(ctx);

View File

@ -389,9 +389,9 @@ static int generate_gostkey(sc_card_t *card, u8 keyid, u8 keyoptions)
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
int opt_reader = -1;
int opt_wait = 0; int opt_wait = 0;
const char *opt_pin = NULL; const char *opt_pin = NULL;
const char *opt_reader = NULL;
int opt_key = 0; int opt_key = 0;
int opt_is_iv = 0; int opt_is_iv = 0;
u8 opt_keytype = SC_RUTOKEN_OPTIONS_GOST_CRYPT_PZ; u8 opt_keytype = SC_RUTOKEN_OPTIONS_GOST_CRYPT_PZ;
@ -416,7 +416,7 @@ int main(int argc, char* argv[])
case '?': case '?':
util_print_usage_and_die(app_name, options, option_help); util_print_usage_and_die(app_name, options, option_help);
case 'r': case 'r':
opt_reader = atoi(optarg); opt_reader = optarg;
break; break;
case 'w': case 'w':
opt_wait = 1; opt_wait = 1;
@ -488,7 +488,7 @@ int main(int argc, char* argv[])
} }
ctx->debug = opt_debug; ctx->debug = opt_debug;
if (util_connect_card(ctx, &card, opt_reader, 0, opt_wait, opt_debug) != 0) if (util_connect_card(ctx, &card, opt_reader, opt_wait, opt_debug) != 0)
err = -1; err = -1;
if (err == 0 && opt_pin) { if (err == 0 && opt_pin) {
@ -543,7 +543,7 @@ int main(int argc, char* argv[])
if (card) { if (card) {
/* sc_lock and sc_connect_card in util_connect_card */ /* sc_lock and sc_connect_card in util_connect_card */
sc_unlock(card); sc_unlock(card);
sc_disconnect_card(card, 0); sc_disconnect_card(card);
} }
if (ctx) if (ctx)
sc_release_context(ctx); sc_release_context(ctx);

View File

@ -8,73 +8,57 @@
#include "util.h" #include "util.h"
int util_connect_card(sc_context_t *ctx, sc_card_t **cardp, int util_connect_card(sc_context_t *ctx, sc_card_t **cardp,
int reader_id, int slot_id, int wait, int verbose) char *reader_id, int wait, int verbose)
{ {
sc_reader_t *reader; sc_reader_t *reader, *found;
sc_card_t *card; sc_card_t *card;
int r; int r, tmp_reader_num;
if (wait) { if (wait) {
sc_reader_t *readers[16];
int slots[16];
unsigned int i;
int j, k, found;
unsigned int event; unsigned int event;
for (i = k = 0; i < sc_ctx_get_reader_count(ctx); i++) {
if (reader_id >= 0 && (unsigned int)reader_id != i)
continue;
reader = sc_ctx_get_reader(ctx, i);
for (j = 0; j < reader->slot_count; j++, k++) {
readers[k] = reader;
slots[k] = j;
}
}
printf("Waiting for card to be inserted...\n"); printf("Waiting for card to be inserted...\n");
r = sc_wait_for_event(readers, slots, k, r = sc_wait_for_event(ctx, SC_EVENT_CARD_INSERTED, &found, &event, -1);
SC_EVENT_CARD_INSERTED,
&found, &event, -1);
if (r < 0) { if (r < 0) {
fprintf(stderr, fprintf(stderr, "Error while waiting for card: %s\n", sc_strerror(r));
"Error while waiting for card: %s\n",
sc_strerror(r));
return 3; return 3;
} }
reader = readers[found]; reader = found;
slot_id = slots[found];
} else { } else {
if (sc_ctx_get_reader_count(ctx) == 0) { if (sc_ctx_get_reader_count(ctx) == 0) {
fprintf(stderr, fprintf(stderr,
"No smart card readers found.\n"); "No smart card readers found.\n");
return 1; return 1;
} }
if (reader_id < 0) { if (!reader_id) {
unsigned int i; unsigned int i;
/* Automatically try to skip to a reader with a card if reader not specified */ /* Automatically try to skip to a reader with a card if reader not specified */
for (i = 0; i < sc_ctx_get_reader_count(ctx); i++) { for (i = 0; i < sc_ctx_get_reader_count(ctx); i++) {
reader = sc_ctx_get_reader(ctx, i); reader = sc_ctx_get_reader(ctx, i);
if (sc_detect_card_presence(reader, 0) & SC_SLOT_CARD_PRESENT) { if (sc_detect_card_presence(reader) & SC_READER_CARD_PRESENT) {
reader_id = i;
fprintf(stderr, "Using reader with a card: %s\n", reader->name); fprintf(stderr, "Using reader with a card: %s\n", reader->name);
goto autofound; goto autofound;
} }
} }
reader_id = 0; /* If no reader had a card, default to the first reader */
reader = sc_ctx_get_reader(ctx, 0);
} else {
/* Get the reader by name if possible */
if (!sscanf(reader_id, "%d", &tmp_reader_num)) {
reader = sc_ctx_get_reader_by_name(ctx, reader_id);
} else {
reader = sc_ctx_get_reader(ctx, tmp_reader_num);
}
} }
autofound: autofound:
if ((unsigned int)reader_id >= sc_ctx_get_reader_count(ctx)) { if (!reader) {
fprintf(stderr, fprintf(stderr,
"Illegal reader number. " "Reader \"%s\" not found (%d reader(s) detected)\n", reader_id, sc_ctx_get_reader_count(ctx));
"Only %d reader(s) configured.\n",
sc_ctx_get_reader_count(ctx));
return 1; return 1;
} }
reader = sc_ctx_get_reader(ctx, reader_id); if (sc_detect_card_presence(reader) <= 0) {
slot_id = 0;
if (sc_detect_card_presence(reader, 0) <= 0) {
fprintf(stderr, "Card not present.\n"); fprintf(stderr, "Card not present.\n");
return 3; return 3;
} }
@ -82,7 +66,7 @@ autofound:
if (verbose) if (verbose)
printf("Connecting to card in reader %s...\n", reader->name); printf("Connecting to card in reader %s...\n", reader->name);
if ((r = sc_connect_card(reader, slot_id, &card)) < 0) { if ((r = sc_connect_card(reader, &card)) < 0) {
fprintf(stderr, fprintf(stderr,
"Failed to connect to card: %s\n", "Failed to connect to card: %s\n",
sc_strerror(r)); sc_strerror(r));
@ -96,7 +80,7 @@ autofound:
fprintf(stderr, fprintf(stderr,
"Failed to lock card: %s\n", "Failed to lock card: %s\n",
sc_strerror(r)); sc_strerror(r));
sc_disconnect_card(card, 0); sc_disconnect_card(card);
return 1; return 1;
} }

View File

@ -33,8 +33,7 @@ void util_warn(const char *fmt, ...);
void util_error(const char *fmt, ...); void util_error(const char *fmt, ...);
void util_fatal(const char *fmt, ...); void util_fatal(const char *fmt, ...);
/* All singing all dancing card connect routine */ /* All singing all dancing card connect routine */
int util_connect_card(struct sc_context *, struct sc_card **, int util_connect_card(struct sc_context *, struct sc_card **, char *reader_id, int wait, int verbose);
int reader_id, int slot_id, int wait, int verbose);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -102,7 +102,7 @@ static int verify_pin(sc_card_t *card, int pin_reference, char *pin_value)
data.flags = SC_PIN_CMD_NEED_PADDING; data.flags = SC_PIN_CMD_NEED_PADDING;
if (card->slot->capabilities & SC_SLOT_CAP_PIN_PAD) if (card->reader->capabilities & SC_READER_CAP_PIN_PAD)
{ {
printf("Please enter PIN on the reader's pin pad.\n"); printf("Please enter PIN on the reader's pin pad.\n");
data.pin1.prompt = "Please enter PIN"; data.pin1.prompt = "Please enter PIN";
@ -155,7 +155,7 @@ static int change_pin(sc_card_t *card,
data.flags = SC_PIN_CMD_NEED_PADDING; data.flags = SC_PIN_CMD_NEED_PADDING;
if (card->slot->capabilities & SC_SLOT_CAP_PIN_PAD) if (card->reader->capabilities & SC_READER_CAP_PIN_PAD)
{ {
printf("Please enter PIN on the reader's pin pad.\n"); printf("Please enter PIN on the reader's pin pad.\n");
data.pin1.prompt = "Please enter PIN"; data.pin1.prompt = "Please enter PIN";
@ -213,7 +213,7 @@ static int debloque_pin(sc_card_t *card,
data.flags = SC_PIN_CMD_NEED_PADDING; data.flags = SC_PIN_CMD_NEED_PADDING;
if (card->slot->capabilities & SC_SLOT_CAP_PIN_PAD) if (card->reader->capabilities & SC_READER_CAP_PIN_PAD)
{ {
printf("Please enter PIN on the reader's pin pad.\n"); printf("Please enter PIN on the reader's pin pad.\n");
data.pin1.prompt = "Please enter PIN"; data.pin1.prompt = "Please enter PIN";
@ -497,9 +497,9 @@ int main(int argc, char *argv[])
for(i = 0; i<sc_ctx_get_reader_count(ctx); i++) for(i = 0; i<sc_ctx_get_reader_count(ctx); i++)
{ {
lecteur = sc_ctx_get_reader(ctx, i); lecteur = sc_ctx_get_reader(ctx, i);
if(sc_detect_card_presence(lecteur, 0)) if(sc_detect_card_presence(lecteur))
{ {
r = sc_connect_card(lecteur, 0, &card); r = sc_connect_card(lecteur, &card);
if(r>=0) if(r>=0)
{ {
printf("card->name = %s\n", card->name); printf("card->name = %s\n", card->name);
@ -508,7 +508,7 @@ int main(int argc, char *argv[])
card_presente = 1; card_presente = 1;
break; break;
} }
sc_disconnect_card(card,0); sc_disconnect_card(card);
card = NULL; card = NULL;
} }
} }
@ -519,14 +519,14 @@ int main(int argc, char *argv[])
if(no_lecteur < sc_ctx_get_reader_count(ctx)) if(no_lecteur < sc_ctx_get_reader_count(ctx))
{ {
lecteur = sc_ctx_get_reader(ctx, no_lecteur); lecteur = sc_ctx_get_reader(ctx, no_lecteur);
r = sc_connect_card(lecteur, 0, &card); r = sc_connect_card(lecteur, &card);
if(r>=0) if(r>=0)
{ {
card_presente = 1; card_presente = 1;
} }
else else
{ {
sc_disconnect_card(card,0); sc_disconnect_card(card);
} }
} }
} }
@ -943,7 +943,7 @@ out:
if (card) if (card)
{ {
sc_unlock(card); sc_unlock(card);
sc_disconnect_card(card, 0); sc_disconnect_card(card);
} }
if (ctx) if (ctx)