1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
/*
Scan and print all the PC/SC devices available
Copyright (C) 2001-2009 Ludovic Rousseau <ludovic.rousseau@free.fr>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id: pcsc_scan.c 4192 2009-05-10 15:48:14Z rousseau $ */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#ifdef __APPLE__
#include <PCSC/wintypes.h>
#include <PCSC/winscard.h>
#else
#include <winscard.h>
#endif
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#define TIMEOUT 1000 /* 1 second timeout */
/* command used to parse (on screen) the ATR */
#define ATR_PARSER "ATR_analysis"
#ifndef SCARD_E_NO_READERS_AVAILABLE
#define SCARD_E_NO_READERS_AVAILABLE 0x8010002E
#endif
#define test_rv(fct, rv, hContext) \
do { \
if (rv != SCARD_S_SUCCESS) \
{ \
printf("%s%s: %s%s\n", red, fct, pcsc_stringify_error(rv), color_end); \
(void)SCardReleaseContext(hContext); \
exit(-1); \
} \
} while(0)
void usage(void)
{
printf("usage: pcsc_scan [-n] [-V] [-h]\n");
printf(" -n : no ATR analysis\n");
printf(" -V : print version number\n");
printf(" -h : this help\n");
} /* usage */
int main(int argc, char *argv[])
{
int current_reader;
LONG rv;
SCARDCONTEXT hContext;
SCARD_READERSTATE_A *rgReaderStates_t = NULL;
SCARD_READERSTATE_A rgReaderStates[1];
DWORD dwReaders = 0, dwReadersOld;
DWORD timeout;
LPSTR mszReaders = NULL;
char *ptr, **readers = NULL;
int nbReaders, i;
char atr[MAX_ATR_SIZE*3+1]; /* ATR in ASCII */
char atr_command[sizeof(atr)+sizeof(ATR_PARSER)+2+1];
int opt;
int analyse_atr = TRUE;
char *blue = "";
char *red = "";
char *magenta = "";
char *color_end = "";
int pnp = TRUE;
printf("PC/SC device scanner\n");
printf("V " VERSION " (c) 2001-2009, Ludovic Rousseau <ludovic.rousseau@free.fr>\n");
printf("Compiled with PC/SC lite version: " PCSCLITE_VERSION_NUMBER "\n");
while ((opt = getopt(argc, argv, "Vhn")) != EOF)
{
switch (opt)
{
case 'n':
analyse_atr = FALSE;
break;
case 'V':
/* the version number is printed by default */
return 1;
break;
case 'h':
default:
usage();
return 1;
break;
}
}
if (argc - optind != 0)
{
usage();
return 1;
}
/* check if terminal supports color */
{
const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
char *term;
term = getenv("TERM");
if (term)
{
int i;
/* for each known color terminal */
for (i = 0; i < sizeof(terms) / sizeof(terms[0]); i++)
{
/* we found a supported term? */
if (0 == strcmp(terms[i], term))
{
blue = "\33[34m";
red = "\33[31m";
magenta = "\33[35m";
color_end = "\33[0m";
break;
}
}
}
}
rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
test_rv("SCardEstablishContext", rv, hContext);
rgReaderStates[0].szReader = "\\\\?PnP?\\Notification";
rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;
rv = SCardGetStatusChange(hContext, 0, rgReaderStates, 1);
if (rgReaderStates[0].dwEventState && SCARD_STATE_UNKNOWN)
{
printf("PnP reader name not supported. Using polling.\n");
pnp = FALSE;
}
get_readers:
/* free memory possibly allocated in a previous loop */
if (NULL != readers)
{
free(readers);
readers = NULL;
}
if (NULL != rgReaderStates_t)
{
free(rgReaderStates_t);
rgReaderStates_t = NULL;
}
/* Retrieve the available readers list.
*
* 1. Call with a null buffer to get the number of bytes to allocate
* 2. malloc the necessary storage
* 3. call with the real allocated buffer
*/
printf("%sScanning present readers...%s\n", red, color_end);
rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
if (rv != SCARD_E_NO_READERS_AVAILABLE)
test_rv("SCardListReaders", rv, hContext);
dwReadersOld = dwReaders;
/* if non NULL we came back so free first */
if (mszReaders)
{
free(mszReaders);
mszReaders = NULL;
}
mszReaders = malloc(sizeof(char)*dwReaders);
if (mszReaders == NULL)
{
printf("malloc: not enough memory\n");
return 1;
}
*mszReaders = '\0';
rv = SCardListReaders(hContext, NULL, mszReaders, &dwReaders);
/* Extract readers from the null separated string and get the total
* number of readers */
nbReaders = 0;
ptr = mszReaders;
while (*ptr != '\0')
{
ptr += strlen(ptr)+1;
nbReaders++;
}
if (SCARD_E_NO_READERS_AVAILABLE == rv || 0 == nbReaders)
{
printf("%sWaiting for the first reader...%s", red, color_end);
fflush(stdout);
if (pnp)
{
rgReaderStates[0].szReader = "\\\\?PnP?\\Notification";
rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;
rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);
test_rv("SCardGetStatusChange", rv, hContext);
}
else
{
rv = SCARD_S_SUCCESS;
while ((SCARD_S_SUCCESS == rv) && (dwReaders == dwReadersOld))
{
rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
if (SCARD_E_NO_READERS_AVAILABLE == rv)
rv = SCARD_S_SUCCESS;
sleep(1);
}
}
printf("found one\n");
goto get_readers;
}
else
test_rv("SCardListReader", rv, hContext);
/* allocate the readers table */
readers = calloc(nbReaders+1, sizeof(char *));
if (NULL == readers)
{
printf("Not enough memory for readers table\n");
return -1;
}
/* fill the readers table */
nbReaders = 0;
ptr = mszReaders;
while (*ptr != '\0')
{
printf("%s%d: %s%s\n", blue, nbReaders, ptr, color_end);
readers[nbReaders] = ptr;
ptr += strlen(ptr)+1;
nbReaders++;
}
/* allocate the ReaderStates table */
rgReaderStates_t = calloc(nbReaders+1, sizeof(* rgReaderStates_t));
if (NULL == rgReaderStates_t)
{
printf("Not enough memory for readers states\n");
return -1;
}
/* Set the initial states to something we do not know
* The loop below will include this state to the dwCurrentState
*/
for (i=0; i<nbReaders; i++)
{
rgReaderStates_t[i].szReader = readers[i];
rgReaderStates_t[i].dwCurrentState = SCARD_STATE_UNAWARE;
}
rgReaderStates_t[nbReaders].szReader = "\\\\?PnP?\\Notification";
rgReaderStates_t[nbReaders].dwCurrentState = SCARD_STATE_UNAWARE;
/* Wait endlessly for all events in the list of readers
* We only stop in case of an error
*/
if (pnp)
{
timeout = INFINITE;
nbReaders++;
}
else
timeout = TIMEOUT;
rv = SCardGetStatusChange(hContext, timeout, rgReaderStates_t, nbReaders);
while ((rv == SCARD_S_SUCCESS) || (rv == SCARD_E_TIMEOUT))
{
if (pnp)
{
if (rgReaderStates_t[nbReaders-1].dwEventState &
SCARD_STATE_CHANGED)
goto get_readers;
}
else
{
/* A new reader appeared? */
if ((SCardListReaders(hContext, NULL, NULL, &dwReaders)
== SCARD_S_SUCCESS) && (dwReaders != dwReadersOld))
goto get_readers;
}
/* Now we have an event, check all the readers in the list to see what
* happened */
for (current_reader=0; current_reader < nbReaders; current_reader++)
{
time_t t;
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_CHANGED)
{
/* If something has changed the new state is now the current
* state */
rgReaderStates_t[current_reader].dwCurrentState =
rgReaderStates_t[current_reader].dwEventState;
}
else
/* If nothing changed then skip to the next reader */
continue;
/* From here we know that the state for the current reader has
* changed because we did not pass through the continue statement
* above.
*/
/* Timestamp the event as we get notified */
t = time(NULL);
/* Specify the current reader's number and name */
printf("\n%s Reader %d: %s%s%s\n", ctime(&t), current_reader,
magenta, rgReaderStates_t[current_reader].szReader,
color_end);
/* Dump the full current state */
printf(" Card state: %s", red);
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_IGNORE)
printf("Ignore this reader, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_UNKNOWN)
{
printf("Reader unknown\n");
goto get_readers;
}
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_UNAVAILABLE)
printf("Status unavailable, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_EMPTY)
printf("Card removed, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_PRESENT)
printf("Card inserted, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_ATRMATCH)
printf("ATR matches card, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_EXCLUSIVE)
printf("Exclusive Mode, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_INUSE)
printf("Shared Mode, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_MUTE)
printf("Unresponsive card, ");
printf("%s\n", color_end);
/* force display */
fflush(stdout);
/* Also dump the ATR if available */
if (rgReaderStates_t[current_reader].cbAtr > 0)
{
printf(" ATR: ");
if (rgReaderStates_t[current_reader].cbAtr)
{
for (i=0; i<rgReaderStates_t[current_reader].cbAtr; i++)
sprintf(&atr[i*3], "%02X ",
rgReaderStates_t[current_reader].rgbAtr[i]);
atr[i*3-1] = '\0';
}
else
atr[0] = '\0';
printf("%s%s%s\n\n", magenta, atr, color_end);
/* force display */
fflush(stdout);
if (analyse_atr)
{
sprintf(atr_command, ATR_PARSER " '%s'", atr);
if (system(atr_command))
perror(atr_command);
}
}
} /* for */
rv = SCardGetStatusChange(hContext, timeout, rgReaderStates_t,
nbReaders);
} /* while */
/* If we get out the loop, GetStatusChange() was unsuccessful */
test_rv("SCardGetStatusChange", rv, hContext);
/* We try to leave things as clean as possible */
rv = SCardReleaseContext(hContext);
test_rv("SCardReleaseContext", rv, hContext);
/* free memory possibly allocated */
if (NULL != readers)
free(readers);
if (NULL != rgReaderStates_t)
free(rgReaderStates_t);
return 0;
} /* main */
|