75 static int contextMaxThreadCounter = PCSC_MAX_CONTEXT_THREADS;
76 static int contextMaxCardHandles = PCSC_MAX_CONTEXT_CARD_HANDLES;
96 static void MSGCleanupClient(
SCONTEXT *);
98 static void * ContextThread(LPVOID pdwIndex);
102 static int contextsListhContext_seeker(
const void *el,
const void *key)
106 if ((el == NULL) || (key == NULL))
108 Log3(PCSC_LOG_CRITICAL,
"called with NULL pointer: el=%p, key=%p",
113 if (currentContext->hContext == *(int32_t *)key)
118 LONG ContextsInitialize(
int customMaxThreadCounter,
119 int customMaxThreadCardHandles)
123 if (customMaxThreadCounter != 0)
124 contextMaxThreadCounter = customMaxThreadCounter;
126 if (customMaxThreadCardHandles != 0)
127 contextMaxCardHandles = customMaxThreadCardHandles;
132 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d", lrv);
135 lrv = list_attributes_seeker(&
contextsList, contextsListhContext_seeker);
138 Log2(PCSC_LOG_CRITICAL,
139 "list_attributes_seeker failed with return value: %d", lrv);
148 void ContextsDeinitialize(
void)
155 Log2(PCSC_LOG_DEBUG,
"remaining threads: %d", listSize);
182 if (listSize >= contextMaxThreadCounter)
184 Log2(PCSC_LOG_CRITICAL,
"Too many context running: %d", listSize);
189 newContext = malloc(
sizeof(*newContext));
190 if (NULL == newContext)
192 Log1(PCSC_LOG_CRITICAL,
"Could not allocate new context");
195 memset(newContext, 0,
sizeof(*newContext));
200 lrv = list_init(&newContext->cardsList);
203 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d", lrv);
208 list_attributes_copy(&newContext->cardsList, list_meter_int32_t, 1);
215 lrv = list_attributes_comparator(&newContext->cardsList,
216 list_comparator_int32_t);
219 Log2(PCSC_LOG_CRITICAL,
220 "list_attributes_comparator failed with return value: %d", lrv);
221 list_destroy(&newContext->cardsList);
230 Log2(PCSC_LOG_CRITICAL,
"list_append failed with return value: %d",
232 list_destroy(&newContext->cardsList);
236 rv = ThreadCreate(&newContext->
pthThread, THREAD_ATTR_DETACHED,
237 (PCSCLITE_THREAD_FUNCTION( )) ContextThread, (LPVOID) newContext);
242 Log2(PCSC_LOG_CRITICAL,
"ThreadCreate failed: %s", strerror(rv));
245 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %d", lrv2);
246 list_destroy(&newContext->cardsList);
263 (void)close(*pdwClientID);
298 "CANCEL_TRANSACTION",
302 "CMD_GET_READERS_STATE",
303 "CMD_WAIT_READER_STATE_CHANGE",
304 "CMD_STOP_WAITING_READER_STATE_CHANGE",
309 #define READ_BODY(v) \ 311 if (header.size != sizeof(v)) \ 313 ret = MessageReceive(&v, sizeof(v), filedes); \ 314 if (ret != SCARD_S_SUCCESS) { \ 315 Log2(PCSC_LOG_DEBUG, "Client die: %d", filedes); \ 320 #define WRITE_BODY(v) \ 321 WRITE_BODY_WITH_COMMAND(CommandsText[header.command], v) 322 #define WRITE_BODY_WITH_COMMAND(command, v) \ 324 Log4(PCSC_LOG_DEBUG, "%s rv=0x%X for client %d", command, v.rv, filedes); \ 325 ret = MessageSend(&v, sizeof(v), filedes); \ 328 static void * ContextThread(LPVOID newContext)
333 if (IsClientAuthorized(filedes,
"access_pcsc", NULL) == 0)
335 Log1(PCSC_LOG_CRITICAL,
"Rejected unauthorized PC/SC client");
340 Log1(PCSC_LOG_DEBUG,
"Authorized PC/SC client");
343 Log3(PCSC_LOG_DEBUG,
"Thread is started: dwClientID=%d, threadContext @%p",
354 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
359 if ((header.command > CMD_ENUM_FIRST)
360 && (header.command < CMD_ENUM_LAST))
361 Log3(PCSC_LOG_DEBUG,
"Received command: %s from client %d",
364 switch (header.command)
373 Log3(PCSC_LOG_DEBUG,
"Client is protocol version %d:%d",
374 veStr.major, veStr.minor);
382 Log1(PCSC_LOG_CRITICAL,
383 "Communication protocol mismatch!");
384 Log3(PCSC_LOG_ERROR,
"Client protocol is %d:%d",
385 veStr.major, veStr.minor);
386 Log3(PCSC_LOG_ERROR,
"Server protocol is %d:%d",
406 RFWaitForReaderInit();
420 RFWaitForReaderInit();
424 EHRegisterClientForEvent(filedes);
453 hContext = esStr.hContext;
456 esStr.hContext = hContext;
459 esStr.rv = MSGAddContext(esStr.hContext, threadContext);
474 reStr.rv = MSGRemoveContext(reStr.hContext, threadContext);
484 DWORD dwActiveProtocol;
488 coStr.szReader[
sizeof(coStr.szReader)-1] = 0;
490 dwActiveProtocol = coStr.dwActiveProtocol;
492 if (IsClientAuthorized(filedes,
"access_card", coStr.szReader) == 0)
494 Log2(PCSC_LOG_CRITICAL,
"Rejected unauthorized client for '%s'", coStr.szReader);
499 Log2(PCSC_LOG_DEBUG,
"Authorized client for '%s'", coStr.szReader);
503 coStr.dwShareMode, coStr.dwPreferredProtocols,
504 &hCard, &dwActiveProtocol);
507 coStr.dwActiveProtocol = dwActiveProtocol;
510 coStr.rv = MSGAddHandle(coStr.hContext, coStr.hCard,
520 DWORD dwActiveProtocol;
524 if (MSGCheckHandleAssociation(rcStr.hCard, threadContext))
528 rcStr.dwPreferredProtocols, rcStr.dwInitialization,
530 rcStr.dwActiveProtocol = dwActiveProtocol;
542 if (MSGCheckHandleAssociation(diStr.hCard, threadContext))
548 diStr.rv = MSGRemoveHandle(diStr.hCard, threadContext);
560 if (MSGCheckHandleAssociation(beStr.hCard, threadContext))
575 if (MSGCheckHandleAssociation(enStr.hCard, threadContext))
579 enStr.dwDisposition);
601 if (psTargetContext != NULL)
603 uint32_t fd = psTargetContext->dwClientID;
625 if (MSGCheckHandleAssociation(stStr.hCard, threadContext))
629 stStr.rv =
SCardStatus(stStr.hCard, NULL, NULL, NULL,
647 if (MSGCheckHandleAssociation(trStr.hCard, threadContext))
651 if ((trStr.pcbRecvLength >
sizeof(pbRecvBuffer))
652 || (trStr.cbSendLength >
sizeof(pbSendBuffer)))
653 goto buffer_overflow;
659 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
663 ioSendPci.
dwProtocol = trStr.ioSendPciProtocol;
665 ioRecvPci.
dwProtocol = trStr.ioRecvPciProtocol;
667 cbRecvLength =
sizeof pbRecvBuffer;
670 pbSendBuffer, trStr.cbSendLength, &ioRecvPci,
671 pbRecvBuffer, &cbRecvLength);
673 if (cbRecvLength > trStr.pcbRecvLength)
679 trStr.ioSendPciProtocol = ioSendPci.
dwProtocol;
681 trStr.ioRecvPciProtocol = ioRecvPci.
dwProtocol;
683 trStr.pcbRecvLength = cbRecvLength;
689 ret =
MessageSend(pbRecvBuffer, cbRecvLength, filedes);
698 DWORD dwBytesReturned;
702 if (MSGCheckHandleAssociation(ctStr.hCard, threadContext))
706 if ((ctStr.cbRecvLength >
sizeof(pbRecvBuffer))
707 || (ctStr.cbSendLength >
sizeof(pbSendBuffer)))
709 goto buffer_overflow;
716 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
720 dwBytesReturned = ctStr.dwBytesReturned;
722 ctStr.rv =
SCardControl(ctStr.hCard, ctStr.dwControlCode,
723 pbSendBuffer, ctStr.cbSendLength,
724 pbRecvBuffer,
sizeof pbRecvBuffer,
727 if (dwBytesReturned > ctStr.cbRecvLength)
733 ctStr.dwBytesReturned = dwBytesReturned;
739 ret =
MessageSend(pbRecvBuffer, dwBytesReturned, filedes);
750 if (MSGCheckHandleAssociation(gsStr.hCard, threadContext))
754 if (gsStr.cbAttrLen >
sizeof(gsStr.pbAttr))
755 goto buffer_overflow;
757 cbAttrLen = gsStr.cbAttrLen;
760 gsStr.pbAttr, &cbAttrLen);
762 gsStr.cbAttrLen = cbAttrLen;
774 if (MSGCheckHandleAssociation(gsStr.hCard, threadContext))
778 if (gsStr.cbAttrLen >
sizeof(gsStr.pbAttr))
779 goto buffer_overflow;
782 gsStr.pbAttr, gsStr.cbAttrLen);
789 Log2(PCSC_LOG_CRITICAL,
"Unknown command: %d", header.command);
797 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
803 Log2(PCSC_LOG_DEBUG,
"Buffer overflow detected: %d", filedes);
806 Log2(PCSC_LOG_DEBUG,
"Wrong length: %d", filedes);
808 (void)close(filedes);
809 MSGCleanupClient(threadContext);
810 (void)pthread_exit((LPVOID) NULL);
813 LONG MSGSignalClient(uint32_t filedes, LONG rv)
822 Log2(PCSC_LOG_DEBUG,
"Signal client: %d", filedes);
825 WRITE_BODY_WITH_COMMAND(
"SIGNAL", waStr);
830 LONG MSGSendReaderStates(uint32_t filedes)
834 Log2(PCSC_LOG_DEBUG,
"Send reader states: %d", filedes);
844 threadContext->hContext = hContext;
853 if (0 == threadContext->hContext)
855 Log1(PCSC_LOG_ERROR,
"Invalidated handle");
859 if (threadContext->hContext != hContext)
863 while (list_size(&threadContext->cardsList) != 0)
872 ptr = list_get_at(&threadContext->cardsList, 0);
875 Log1(PCSC_LOG_CRITICAL,
"list_get_at failed");
878 hCard = *(int32_t *)ptr;
883 rv = RFReaderInfoById(hCard, &rContext);
897 if (hCard != rContext->
hLockId)
916 rv =
SCardStatus(hCard, NULL, NULL, NULL, NULL, NULL, NULL);
926 lrv = list_delete_at(&threadContext->cardsList, 0);
928 Log2(PCSC_LOG_CRITICAL,
929 "list_delete_at failed with return value: %d", lrv);
931 UNREF_READER(rContext)
937 threadContext->hContext = 0;
947 if (0 == threadContext->hContext)
949 Log1(PCSC_LOG_ERROR,
"Invalidated handle");
953 if (threadContext->hContext == hContext)
962 listLength = list_size(&threadContext->cardsList);
963 if (listLength >= contextMaxCardHandles)
966 "Too many card handles for thread context @%p: %d (max is %d)" 967 "Restart pcscd with --max-card-handle-per-thread value",
968 threadContext, listLength, contextMaxCardHandles);
975 lrv = list_append(&threadContext->cardsList, &hCard);
978 Log2(PCSC_LOG_CRITICAL,
979 "list_append failed with return value: %d", lrv);
998 lrv = list_delete(&threadContext->cardsList, &hCard);
1002 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %d", lrv);
1010 static LONG MSGCheckHandleAssociation(
SCARDHANDLE hCard,
1015 if (0 == threadContext->hContext)
1019 Log1(PCSC_LOG_CRITICAL,
"Invalidated handle");
1024 list_index = list_locate(&threadContext->cardsList, &hCard);
1026 if (list_index >= 0)
1030 Log1(PCSC_LOG_ERROR,
"Client failed to authenticate");
1040 static void MSGCleanupClient(
SCONTEXT * threadContext)
1045 if (threadContext->hContext != 0)
1048 (void)MSGRemoveContext(threadContext->hContext, threadContext);
1052 list_destroy(&threadContext->cardsList);
1055 Log3(PCSC_LOG_DEBUG,
1056 "Thread is stopping: dwClientID=%d, threadContext @%p",
1062 memset((
void*) threadContext, 0,
sizeof(
SCONTEXT));
1063 Log2(PCSC_LOG_DEBUG,
"Freeing SCONTEXT @%p", threadContext);
1070 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %x", lrv);
1072 free(threadContext);
1077 Log2(PCSC_LOG_DEBUG,
"Starting suicide alarm in %d seconds",
1078 TIME_BEFORE_SUICIDE);
1079 alarm(TIME_BEFORE_SUICIDE);
LONG EHUnregisterClientForEvent(int32_t filedes)
Unregister a client and log an error if the client is not found.
#define SCARD_E_INVALID_VALUE
One or more of the supplied parameters values could not be properly interpreted.
used by SCardBeginTransaction()
contained in SCARD_CONNECT Messages.
wait for a reader state change
contained in SCARD_CANCEL Messages.
contained in SCARD_TRANSMIT Messages.
volatile SCARDHANDLE hLockId
Lock Id.
#define SCARD_S_SUCCESS
No error was encountered.
contained in SCARD_END_TRANSACTION Messages.
get the client/server protocol version
LONG CreateContextThread(uint32_t *pdwClientID)
Creates threads to handle messages received from Clients.
pthread_t pthThread
Event polling thread's ID.
used by SCardEstablishContext()
used by SCardEndTransaction()
PCSC_API LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
Creates an Application Context to the PC/SC Resource Manager.
unsigned long cbPciLength
Protocol Control Inf Length.
#define SCARD_LEAVE_CARD
Do nothing on close.
This handles abstract system level calls.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
uint32_t dwClientID
Connection ID used to reference the Client.
This demarshalls functions over the message queue and keeps track of clients and their handles.
#define PROTOCOL_VERSION_MAJOR
Major version of the current message protocol.
contained in SCARD_DISCONNECT Messages.
static list_t contextsList
Context tracking list.
PCSC_API LONG SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen)
Get an attribute from the IFD Handler (reader driver).
Information contained in SCARD_RELEASE_CONTEXT Messages.
PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition)
Terminates a connection made through SCardConnect().
contained in SCARD_BEGIN_TRANSACTION Messages.
Information contained in SCARD_ESTABLISH_CONTEXT Messages.
Information transmitted in CMD_VERSION Messages.
INTERNAL LONG MessageReceive(void *buffer_void, uint64_t buffer_size, int32_t filedes)
Called by the Client to get the reponse from the server or vice-versa.
used by SCardReleaseContext()
LONG SCARDCONTEXT
hContext returned by SCardEstablishContext()
#define SCARD_E_NO_MEMORY
Not enough memory available to complete this command.
contained in SCARD_STATUS Messages.
contained in SCARD_RECONNECT Messages.
unsigned long dwProtocol
Protocol identifier.
uint32_t timeOut
timeout in ms
#define PCSCLITE_MAX_READERS_CONTEXTS
Maximum readers context (a slot is count as a reader)
contained in SCARD_GET_ATTRIB and Messages.
This defines some structures and #defines to be used over the transport layer.
Information contained in CMD_WAIT_READER_STATE_CHANGE Messages.
#define SCARD_W_RESET_CARD
The smart card has been reset, so any shared state information is invalid.
PCSC_API LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol)
Reestablishes a connection to a reader that was previously connected to using SCardConnect().
#define MAX_BUFFER_SIZE_EXTENDED
enhanced (64K + APDU + Lc + Le + SW) Tx/Rx Buffer
static READER_STATE readerStates[PCSCLITE_MAX_READERS_CONTEXTS]
Area used to read status information about the readers.
LONG EHTryToUnregisterClientForEvent(int32_t filedes)
Try to unregisted a client If no client is found then do not log an error.
char AutoExit
Represents an Application Context on the Server side.
This handles card insertion/removal events, updates ATR, protocol, and status information.
PCSC_API LONG SCardConnect(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
Establishes a connection to the reader specified in * szReader.
stop waiting for a reader state change
PCSC_API LONG SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
Set an attribute of the IFD Handler.
#define SCARD_W_REMOVED_CARD
The smart card has been removed, so further communication is not possible.
#define SCARD_RESET_CARD
Reset on close.
LONG SCARDHANDLE
hCard returned by SCardConnect()
#define SCARD_E_INSUFFICIENT_BUFFER
The data buffer to receive returned data is too small for the returned data.
static const char * CommandsText[]
Handles messages received from Clients.
#define SCARD_E_CANCELLED
The action was cancelled by an SCardCancel request.
#define PROTOCOL_VERSION_MINOR
Minor version of the current message protocol.
PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard)
Establishes a temporary exclusive access mode for doing a serie of commands in a transaction.
This keeps a list of defines for pcsc-lite.
Protocol Control Information (PCI)
PCSC_API LONG SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer, DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned)
Sends a command directly to the IFD Handler (reader driver) to be processed by the reader.
Define an exported public reader state structure so each application gets instant notification of cha...
INTERNAL LONG MessageSend(void *buffer_void, uint64_t buffer_size, int32_t filedes)
Sends a menssage from client to server or vice-versa.
used by SCardDisconnect()
contained in SCARD_CONTROL Messages.
This keeps track of a list of currently available reader structures.
PCSC_API LONG SCardStatus(SCARDHANDLE hCard, LPSTR mszReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
Returns the current status of the reader connected to by hCard.
#define SCARD_F_INTERNAL_ERROR
An internal consistency check failed.
PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition)
Ends a previously begun transaction.
PCSC_API LONG SCardTransmit(SCARDHANDLE hCard, const SCARD_IO_REQUEST *pioSendPci, LPCBYTE pbSendBuffer, DWORD cbSendLength, SCARD_IO_REQUEST *pioRecvPci, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength)
Sends an APDU to the smart card contained in the reader connected to by SCardConnect().
pthread_mutex_t cardsList_lock
lock for the above list
#define SCARD_E_SERVICE_STOPPED
The Smart card resource manager has shut down.
pthread_mutex_t contextsList_lock
lock for the above list
#define SCARD_E_INVALID_HANDLE
The supplied handle was invalid.
This handles smart card reader communications.
PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext)
Destroys a communication context to the PC/SC Resource Manager.