77 static int contextMaxThreadCounter = PCSC_MAX_CONTEXT_THREADS;
78 static int contextMaxCardHandles = PCSC_MAX_CONTEXT_CARD_HANDLES;
98 static LONG MSGCleanupClient(
SCONTEXT *);
100 static void ContextThread(LPVOID pdwIndex);
104 static int contextsListhContext_seeker(
const void *el,
const void *key)
108 if ((el == NULL) || (key == NULL))
110 Log3(PCSC_LOG_CRITICAL,
"called with NULL pointer: el=%p, key=%p",
115 if (currentContext->hContext == *(int32_t *)key)
120 LONG ContextsInitialize(
int customMaxThreadCounter,
121 int customMaxThreadCardHandles)
125 if (customMaxThreadCounter != 0)
126 contextMaxThreadCounter = customMaxThreadCounter;
128 if (customMaxThreadCardHandles != 0)
129 contextMaxCardHandles = customMaxThreadCardHandles;
131 lrv = list_init(&contextsList);
134 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d", lrv);
137 lrv = list_attributes_seeker(& contextsList, contextsListhContext_seeker);
140 Log2(PCSC_LOG_CRITICAL,
141 "list_attributes_seeker failed with return value: %d", lrv);
150 void ContextsDeinitialize(
void)
153 listSize = list_size(&contextsList);
154 Log2(PCSC_LOG_DEBUG,
"remaining threads: %d", listSize);
157 list_destroy(&contextsList);
180 listSize = list_size(&contextsList);
181 if (listSize >= contextMaxThreadCounter)
183 Log2(PCSC_LOG_CRITICAL,
"Too many context running: %d", listSize);
188 newContext = malloc(
sizeof(*newContext));
189 if (NULL == newContext)
191 Log1(PCSC_LOG_CRITICAL,
"Could not allocate new context");
194 memset(newContext, 0,
sizeof(*newContext));
199 lrv = list_init(&newContext->cardsList);
202 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d", lrv);
207 list_attributes_copy(&newContext->cardsList, list_meter_int32_t, 1);
214 lrv = list_attributes_comparator(&newContext->cardsList,
215 list_comparator_int32_t);
218 Log2(PCSC_LOG_CRITICAL,
219 "list_attributes_comparator failed with return value: %d", lrv);
220 list_destroy(&newContext->cardsList);
226 lrv = list_append(&contextsList, newContext);
229 Log2(PCSC_LOG_CRITICAL,
"list_append failed with return value: %d",
231 list_destroy(&newContext->cardsList);
235 rv = ThreadCreate(&newContext->
pthThread, THREAD_ATTR_DETACHED,
236 (PCSCLITE_THREAD_FUNCTION( )) ContextThread, (LPVOID) newContext);
241 Log2(PCSC_LOG_CRITICAL,
"ThreadCreate failed: %s", strerror(rv));
242 lrv2 = list_delete(&contextsList, newContext);
244 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %d", lrv2);
245 list_destroy(&newContext->cardsList);
262 (void)close(*pdwClientID);
297 "CANCEL_TRANSACTION",
301 "CMD_GET_READERS_STATE",
302 "CMD_WAIT_READER_STATE_CHANGE",
303 "CMD_STOP_WAITING_READER_STATE_CHANGE",
308 #define READ_BODY(v) \
309 if (header.size != sizeof(v)) { goto wrong_length; } \
310 ret = MessageReceive(&v, sizeof(v), filedes); \
311 if (ret != SCARD_S_SUCCESS) { Log2(PCSC_LOG_DEBUG, "Client die: %d", filedes); goto exit; }
313 #define WRITE_BODY(v) \
314 WRITE_BODY_WITH_COMMAND(CommandsText[header.command], v)
315 #define WRITE_BODY_WITH_COMMAND(command, v) \
316 Log4(PCSC_LOG_DEBUG, "%s rv=0x%X for client %d", command, v.rv, filedes); \
317 ret = MessageSend(&v, sizeof(v), filedes);
319 static void ContextThread(LPVOID newContext)
324 if (IsClientAuthorized(filedes,
"access_pcsc", NULL) == 0)
326 Log1(PCSC_LOG_CRITICAL,
"Rejected unauthorized PC/SC client");
331 Log1(PCSC_LOG_DEBUG,
"Authorized PC/SC client");
334 Log3(PCSC_LOG_DEBUG,
"Thread is started: dwClientID=%d, threadContext @%p",
345 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
350 if ((header.command > CMD_ENUM_FIRST)
351 && (header.command < CMD_ENUM_LAST))
352 Log3(PCSC_LOG_DEBUG,
"Received command: %s from client %d",
353 CommandsText[header.command], filedes);
355 switch (header.command)
364 Log3(PCSC_LOG_DEBUG, "Client is protocol version %d:%d",
373 Log3(PCSC_LOG_CRITICAL,
"Client protocol is %d:%d",
374 veStr.major, veStr.minor);
375 Log3(PCSC_LOG_CRITICAL,
"Server protocol is %d:%d",
395 RFWaitForReaderInit();
399 ret =
MessageSend(readerStates,
sizeof(readerStates), filedes);
410 EHRegisterClientForEvent(filedes);
438 hContext = esStr.hContext;
441 esStr.hContext = hContext;
444 esStr.rv = MSGAddContext(esStr.hContext, threadContext);
459 reStr.rv = MSGRemoveContext(reStr.hContext, threadContext);
469 DWORD dwActiveProtocol;
473 coStr.szReader[sizeof(coStr.szReader)-1] = 0;
475 dwActiveProtocol = coStr.dwActiveProtocol;
477 if (IsClientAuthorized(filedes, "access_card", coStr.szReader) == 0)
479 Log2(PCSC_LOG_CRITICAL,
"Rejected unauthorized client for '%s'", coStr.szReader);
484 Log2(PCSC_LOG_DEBUG,
"Authorized client for '%s'", coStr.szReader);
488 coStr.dwShareMode, coStr.dwPreferredProtocols,
489 &hCard, &dwActiveProtocol);
492 coStr.dwActiveProtocol = dwActiveProtocol;
495 coStr.rv = MSGAddHandle(coStr.hContext, coStr.hCard,
505 DWORD dwActiveProtocol;
509 if (MSGCheckHandleAssociation(rcStr.hCard, threadContext))
513 rcStr.dwPreferredProtocols, rcStr.dwInitialization,
515 rcStr.dwActiveProtocol = dwActiveProtocol;
527 if (MSGCheckHandleAssociation(diStr.hCard, threadContext))
533 diStr.rv = MSGRemoveHandle(diStr.hCard, threadContext);
545 if (MSGCheckHandleAssociation(beStr.hCard, threadContext))
560 if (MSGCheckHandleAssociation(enStr.hCard, threadContext))
564 enStr.dwDisposition);
578 psTargetContext = (
SCONTEXT *) list_seek(&contextsList,
581 if (psTargetContext != NULL)
583 uint32_t fd = psTargetContext->dwClientID;
599 if (MSGCheckHandleAssociation(stStr.hCard, threadContext))
603 stStr.rv =
SCardStatus(stStr.hCard, NULL, NULL, NULL,
621 if (MSGCheckHandleAssociation(trStr.hCard, threadContext))
625 if ((trStr.pcbRecvLength > sizeof(pbRecvBuffer))
626 || (trStr.cbSendLength > sizeof(pbSendBuffer)))
627 goto buffer_overflow;
633 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
637 ioSendPci.
dwProtocol = trStr.ioSendPciProtocol;
639 ioRecvPci.
dwProtocol = trStr.ioRecvPciProtocol;
641 cbRecvLength =
sizeof pbRecvBuffer;
644 pbSendBuffer, trStr.cbSendLength, &ioRecvPci,
645 pbRecvBuffer, &cbRecvLength);
647 if (cbRecvLength > trStr.pcbRecvLength)
653 trStr.ioSendPciProtocol = ioSendPci.
dwProtocol;
655 trStr.ioRecvPciProtocol = ioRecvPci.
dwProtocol;
657 trStr.pcbRecvLength = cbRecvLength;
663 ret =
MessageSend(pbRecvBuffer, cbRecvLength, filedes);
672 DWORD dwBytesReturned;
676 if (MSGCheckHandleAssociation(ctStr.hCard, threadContext))
680 if ((ctStr.cbRecvLength > sizeof(pbRecvBuffer))
681 || (ctStr.cbSendLength > sizeof(pbSendBuffer)))
683 goto buffer_overflow;
690 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
694 dwBytesReturned = ctStr.dwBytesReturned;
696 ctStr.rv =
SCardControl(ctStr.hCard, ctStr.dwControlCode,
697 pbSendBuffer, ctStr.cbSendLength,
698 pbRecvBuffer, ctStr.cbRecvLength,
701 ctStr.dwBytesReturned = dwBytesReturned;
707 ret =
MessageSend(pbRecvBuffer, dwBytesReturned, filedes);
718 if (MSGCheckHandleAssociation(gsStr.hCard, threadContext))
722 if (gsStr.cbAttrLen > sizeof(gsStr.pbAttr))
723 goto buffer_overflow;
725 cbAttrLen = gsStr.cbAttrLen;
728 gsStr.pbAttr, &cbAttrLen);
730 gsStr.cbAttrLen = cbAttrLen;
742 if (MSGCheckHandleAssociation(gsStr.hCard, threadContext))
746 if (gsStr.cbAttrLen > sizeof(gsStr.pbAttr))
747 goto buffer_overflow;
750 gsStr.pbAttr, gsStr.cbAttrLen);
757 Log2(PCSC_LOG_CRITICAL, "Unknown command: %d", header.command);
765 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
771 Log2(PCSC_LOG_DEBUG,
"Buffer overflow detected: %d", filedes);
774 Log2(PCSC_LOG_DEBUG,
"Wrong length: %d", filedes);
776 (void)close(filedes);
777 (void)MSGCleanupClient(threadContext);
778 (void)pthread_exit((LPVOID) NULL);
781 LONG MSGSignalClient(uint32_t filedes, LONG rv)
786 Log2(PCSC_LOG_DEBUG,
"Signal client: %d", filedes);
789 WRITE_BODY_WITH_COMMAND(
"SIGNAL", waStr)
796 threadContext->hContext = hContext;
805 if (threadContext->hContext != hContext)
809 while (list_size(&threadContext->cardsList) != 0)
818 ptr = list_get_at(&threadContext->cardsList, 0);
821 Log1(PCSC_LOG_CRITICAL,
"list_get_at failed");
824 hCard = *(int32_t *)ptr;
829 rv = RFReaderInfoById(hCard, &rContext);
839 if (hCard != hLockId)
854 rv =
SCardStatus(hCard, NULL, NULL, NULL, NULL, NULL, NULL);
863 lrv = list_delete_at(&threadContext->cardsList, 0);
865 Log2(PCSC_LOG_CRITICAL,
866 "list_delete_at failed with return value: %d", lrv);
868 UNREF_READER(rContext)
871 list_destroy(&threadContext->cardsList);
875 threadContext->hContext = 0;
885 if (threadContext->hContext == hContext)
894 listLength = list_size(&threadContext->cardsList);
895 if (listLength >= contextMaxCardHandles)
898 "Too many card handles for thread context @%p: %d (max is %d)"
899 "Restart pcscd with --max-card-handle-per-thread value",
900 threadContext, listLength, contextMaxCardHandles);
907 lrv = list_append(&threadContext->cardsList, &hCard);
910 Log2(PCSC_LOG_CRITICAL,
911 "list_append failed with return value: %d", lrv);
929 lrv = list_delete(&threadContext->cardsList, &hCard);
933 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %d", lrv);
941 static LONG MSGCheckHandleAssociation(
SCARDHANDLE hCard,
946 if (0 == threadContext->hContext)
950 Log1(PCSC_LOG_CRITICAL,
"Invalidated handle");
955 list_index = list_locate(&threadContext->cardsList, &hCard);
961 Log1(PCSC_LOG_ERROR,
"Client failed to authenticate");
971 static LONG MSGCleanupClient(
SCONTEXT * threadContext)
976 if (threadContext->hContext != 0)
979 (void)MSGRemoveContext(threadContext->hContext, threadContext);
983 "Thread is stopping: dwClientID=%d, threadContext @%p",
989 memset((
void*) threadContext, 0,
sizeof(
SCONTEXT));
990 Log2(PCSC_LOG_DEBUG,
"Freeing SCONTEXT @%p", threadContext);
993 lrv = list_delete(&contextsList, threadContext);
994 listSize = list_size(&contextsList);
997 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %x", lrv);
1004 Log2(PCSC_LOG_DEBUG,
"Starting suicide alarm in %d seconds",
1005 TIME_BEFORE_SUICIDE);
1006 alarm(TIME_BEFORE_SUICIDE);
LONG EHUnregisterClientForEvent(int32_t filedes)
Unregister a client and log an error if the client is not found.
used by SCardBeginTransaction()
contained in SCARD_CONNECT Messages.
#define SCARD_E_CANCELLED
The action was cancelled by an SCardCancel request.
wait for a reader state change
contained in SCARD_CANCEL Messages.
contained in SCARD_TRANSMIT Messages.
volatile SCARDHANDLE hLockId
Lock Id.
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()
int32_t minor
IPC minor PROTOCOL_VERSION_MINOR.
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.
#define SCARD_E_NO_SERVICE
The Smart card resource manager is not running.
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.
#define SCARD_E_INVALID_HANDLE
The supplied handle was invalid.
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_INVALID_VALUE
One or more of the supplied parameters values could not be properly interpreted.
contained in SCARD_STATUS Messages.
contained in SCARD_RECONNECT Messages.
unsigned long dwProtocol
Protocol identifier.
#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.
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.
#define SCARD_W_REMOVED_CARD
The smart card has been removed, so further communication is not possible.
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 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.
PCSC_API LONG SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
Set an attribute of the IFD Handler.
#define SCARD_RESET_CARD
Reset on close.
LONG SCARDHANDLE
hCard returned by SCardConnect()
static const char * CommandsText[]
Handles messages received from Clients.
#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.
#define SCARD_W_RESET_CARD
The smart card has been reset, so any shared state information is invalid.
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()
#define SCARD_E_NO_MEMORY
Not enough memory available to complete this command.
contained in SCARD_CONTROL Messages.
This keeps track of a list of currently available reader structures.
#define SCARD_E_INSUFFICIENT_BUFFER
The data buffer to receive returned data is too small for the returned data.
int32_t major
IPC major PROTOCOL_VERSION_MAJOR.
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
pthread_mutex_t contextsList_lock
lock for the above list
#define SCARD_S_SUCCESS
error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx
This handles smart card reader communications.
PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext)
Destroys a communication context to the PC/SC Resource Manager.