76static int contextMaxThreadCounter = PCSC_MAX_CONTEXT_THREADS;
77static int contextMaxCardHandles = PCSC_MAX_CONTEXT_CARD_HANDLES;
97static void MSGCleanupClient(
SCONTEXT *);
99static void * ContextThread(LPVOID pdwIndex);
103static int contextsListhContext_seeker(
const void *el,
const void *key)
107 if ((el == NULL) || (key == NULL))
109 Log3(PCSC_LOG_CRITICAL,
"called with NULL pointer: el=%p, key=%p",
114 if (currentContext->hContext == *(int32_t *)key)
119LONG ContextsInitialize(
int customMaxThreadCounter,
120 int customMaxThreadCardHandles)
124 if (customMaxThreadCounter != 0)
125 contextMaxThreadCounter = customMaxThreadCounter;
127 if (customMaxThreadCardHandles != 0)
128 contextMaxCardHandles = customMaxThreadCardHandles;
133 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d", lrv);
136 lrv = list_attributes_seeker(&
contextsList, contextsListhContext_seeker);
139 Log2(PCSC_LOG_CRITICAL,
140 "list_attributes_seeker failed with return value: %d", lrv);
149void ContextsDeinitialize(
void)
156 Log2(PCSC_LOG_DEBUG,
"remaining threads: %d", listSize);
183 if (listSize >= contextMaxThreadCounter)
185 Log2(PCSC_LOG_CRITICAL,
"Too many context running: %d", listSize);
190 newContext = malloc(
sizeof(*newContext));
191 if (NULL == newContext)
193 Log1(PCSC_LOG_CRITICAL,
"Could not allocate new context");
196 memset(newContext, 0,
sizeof(*newContext));
201 lrv = list_init(&newContext->cardsList);
204 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d", lrv);
209 list_attributes_copy(&newContext->cardsList, list_meter_int32_t, 1);
216 lrv = list_attributes_comparator(&newContext->cardsList,
217 list_comparator_int32_t);
220 Log2(PCSC_LOG_CRITICAL,
221 "list_attributes_comparator failed with return value: %d", lrv);
222 list_destroy(&newContext->cardsList);
231 Log2(PCSC_LOG_CRITICAL,
"list_append failed with return value: %d",
233 list_destroy(&newContext->cardsList);
237 rv = ThreadCreate(&newContext->
pthThread, THREAD_ATTR_DETACHED,
238 (PCSCLITE_THREAD_FUNCTION( )) ContextThread, (LPVOID) newContext);
243 Log2(PCSC_LOG_CRITICAL,
"ThreadCreate failed: %s", strerror(rv));
246 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %d", lrv2);
247 list_destroy(&newContext->cardsList);
264 (void)close(*pdwClientID);
299 "CANCEL_TRANSACTION",
303 "CMD_GET_READERS_STATE",
304 "CMD_WAIT_READER_STATE_CHANGE",
305 "CMD_STOP_WAITING_READER_STATE_CHANGE",
310#define READ_BODY(v) \
312 if (header.size != sizeof(v)) \
314 ret = MessageReceive(&v, sizeof(v), filedes); \
315 if (ret != SCARD_S_SUCCESS) { \
316 Log2(PCSC_LOG_DEBUG, "Client die: %d", filedes); \
321#define WRITE_BODY(v) \
322 WRITE_BODY_WITH_COMMAND(CommandsText[header.command], v)
323#define WRITE_BODY_WITH_COMMAND(command, v) \
325 LogRv4(PCSC_LOG_DEBUG, v.rv, "%s for client %d", command, filedes); \
326 ret = MessageSend(&v, sizeof(v), filedes); \
329static void * ContextThread(LPVOID newContext)
334 if (IsClientAuthorized(filedes,
"access_pcsc", NULL) == 0)
336 Log1(PCSC_LOG_CRITICAL,
"Rejected unauthorized PC/SC client");
341 Log1(PCSC_LOG_DEBUG,
"Authorized PC/SC client");
344 Log3(PCSC_LOG_DEBUG,
"Thread is started: dwClientID=%d, threadContext @%p",
355 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
360 if ((header.command > CMD_ENUM_FIRST)
361 && (header.command < CMD_ENUM_LAST))
362 Log3(PCSC_LOG_DEBUG,
"Received command: %s from client %d",
365 switch (header.command)
374 Log3(PCSC_LOG_DEBUG,
"Client is protocol version %d:%d",
375 veStr.major, veStr.minor);
383 Log1(PCSC_LOG_CRITICAL,
384 "Communication protocol mismatch!");
385 Log3(PCSC_LOG_ERROR,
"Client protocol is %d:%d",
386 veStr.major, veStr.minor);
387 Log3(PCSC_LOG_ERROR,
"Server protocol is %d:%d",
407 RFWaitForReaderInit();
411 ret =
MessageSend(readerStates,
sizeof(readerStates), filedes);
421 RFWaitForReaderInit();
425 EHRegisterClientForEvent(filedes);
458 hContext = esStr.hContext;
459 esStr.rv = SCardEstablishContext(esStr.dwScope, 0, 0,
461 esStr.hContext = hContext;
464 esStr.rv = MSGAddContext(esStr.hContext, threadContext);
476 reStr.rv = SCardReleaseContext(reStr.hContext);
479 reStr.rv = MSGRemoveContext(reStr.hContext, threadContext);
489 DWORD dwActiveProtocol;
493 coStr.szReader[
sizeof(coStr.szReader)-1] = 0;
495 dwActiveProtocol = coStr.dwActiveProtocol;
497 if (IsClientAuthorized(filedes,
"access_card", coStr.szReader) == 0)
499 Log2(PCSC_LOG_CRITICAL,
"Rejected unauthorized client for '%s'", coStr.szReader);
504 Log2(PCSC_LOG_DEBUG,
"Authorized client for '%s'", coStr.szReader);
507 coStr.rv = SCardConnect(coStr.hContext, coStr.szReader,
508 coStr.dwShareMode, coStr.dwPreferredProtocols,
509 &hCard, &dwActiveProtocol);
512 coStr.dwActiveProtocol = dwActiveProtocol;
516 coStr.rv = MSGAddHandle(coStr.hContext, coStr.hCard,
535 if (MSGCheckHandleAssociation(rcStr.hCard, threadContext))
538 rcStr.rv = SCardReconnect(rcStr.hCard, rcStr.dwShareMode,
539 rcStr.dwPreferredProtocols, rcStr.dwInitialization,
541 rcStr.dwActiveProtocol = dwActiveProtocol;
553 if (MSGCheckHandleAssociation(diStr.hCard, threadContext))
556 diStr.rv = SCardDisconnect(diStr.hCard, diStr.dwDisposition);
559 diStr.rv = MSGRemoveHandle(diStr.hCard, threadContext);
571 if (MSGCheckHandleAssociation(beStr.hCard, threadContext))
574 beStr.rv = SCardBeginTransaction(beStr.hCard);
586 if (MSGCheckHandleAssociation(enStr.hCard, threadContext))
589 enStr.rv = SCardEndTransaction(enStr.hCard,
590 enStr.dwDisposition);
612 if (psTargetContext != NULL)
614 uint32_t fd = psTargetContext->dwClientID;
636 if (MSGCheckHandleAssociation(stStr.hCard, threadContext))
640 stStr.rv = SCardStatus(stStr.hCard, NULL, NULL, NULL,
658 if (MSGCheckHandleAssociation(trStr.hCard, threadContext))
662 if (trStr.cbSendLength >
sizeof(pbSendBuffer))
663 goto buffer_overflow;
669 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
673 ioSendPci.
dwProtocol = trStr.ioSendPciProtocol;
675 ioRecvPci.
dwProtocol = trStr.ioRecvPciProtocol;
677 cbRecvLength =
sizeof pbRecvBuffer;
679 trStr.rv = SCardTransmit(trStr.hCard, &ioSendPci,
680 pbSendBuffer, trStr.cbSendLength, &ioRecvPci,
681 pbRecvBuffer, &cbRecvLength);
683 if (cbRecvLength > trStr.pcbRecvLength)
689 trStr.ioSendPciProtocol = ioSendPci.
dwProtocol;
691 trStr.ioRecvPciProtocol = ioRecvPci.
dwProtocol;
693 trStr.pcbRecvLength = cbRecvLength;
699 ret =
MessageSend(pbRecvBuffer, cbRecvLength, filedes);
708 DWORD dwBytesReturned;
712 if (MSGCheckHandleAssociation(ctStr.hCard, threadContext))
716 if (ctStr.cbSendLength >
sizeof(pbSendBuffer))
718 goto buffer_overflow;
725 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
729 dwBytesReturned = ctStr.dwBytesReturned;
731 ctStr.rv = SCardControl(ctStr.hCard, ctStr.dwControlCode,
732 pbSendBuffer, ctStr.cbSendLength,
733 pbRecvBuffer,
sizeof pbRecvBuffer,
736 if (dwBytesReturned > ctStr.cbRecvLength)
742 ctStr.dwBytesReturned = dwBytesReturned;
748 ret =
MessageSend(pbRecvBuffer, dwBytesReturned, filedes);
759 if (MSGCheckHandleAssociation(gsStr.hCard, threadContext))
763 if (gsStr.cbAttrLen >
sizeof(gsStr.pbAttr))
764 goto buffer_overflow;
766 cbAttrLen = gsStr.cbAttrLen;
768 gsStr.rv = SCardGetAttrib(gsStr.hCard, gsStr.dwAttrId,
769 gsStr.pbAttr, &cbAttrLen);
771 gsStr.cbAttrLen = cbAttrLen;
783 if (MSGCheckHandleAssociation(gsStr.hCard, threadContext))
787 if (gsStr.cbAttrLen >
sizeof(gsStr.pbAttr))
788 goto buffer_overflow;
790 gsStr.rv = SCardSetAttrib(gsStr.hCard, gsStr.dwAttrId,
791 gsStr.pbAttr, gsStr.cbAttrLen);
798 Log2(PCSC_LOG_CRITICAL,
"Unknown command: %d", header.command);
806 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
812 Log2(PCSC_LOG_DEBUG,
"Buffer overflow detected: %d", filedes);
815 Log2(PCSC_LOG_DEBUG,
"Wrong length: %d", filedes);
817 (void)close(filedes);
818 MSGCleanupClient(threadContext);
819 (void)pthread_exit((LPVOID) NULL);
822LONG MSGSignalClient(uint32_t filedes, LONG rv)
831 Log2(PCSC_LOG_DEBUG,
"Signal client: %d", filedes);
834 WRITE_BODY_WITH_COMMAND(
"SIGNAL", waStr);
839LONG MSGSendReaderStates(uint32_t filedes)
843 Log2(PCSC_LOG_DEBUG,
"Send reader states: %d", filedes);
846 ret =
MessageSend(readerStates,
sizeof(readerStates), filedes);
853 threadContext->hContext = hContext;
862 if (0 == threadContext->hContext)
864 Log1(PCSC_LOG_ERROR,
"Invalidated handle");
868 if (threadContext->hContext != hContext)
872 while (list_size(&threadContext->cardsList) != 0)
881 ptr = list_get_at(&threadContext->cardsList, 0);
884 Log1(PCSC_LOG_CRITICAL,
"list_get_at failed");
887 hCard = *(int32_t *)ptr;
894 rv = RFReaderInfoById(hCard, &rContext);
911 if (hCard != rContext->
hLockId)
930 rv = SCardStatus(hCard, NULL, NULL, NULL, NULL, NULL, NULL);
941 lrv = list_delete_at(&threadContext->cardsList, 0);
943 Log2(PCSC_LOG_CRITICAL,
944 "list_delete_at failed with return value: %d", lrv);
947 UNREF_READER(rContext)
954 threadContext->hContext = 0;
964 if (0 == threadContext->hContext)
966 Log1(PCSC_LOG_ERROR,
"Invalidated handle");
970 if (threadContext->hContext == hContext)
979 listLength = list_size(&threadContext->cardsList);
980 if (listLength >= contextMaxCardHandles)
983 "Too many card handles for thread context @%p: %d (max is %d). "
984 "Restart pcscd with --max-card-handle-per-thread value",
985 threadContext, listLength, contextMaxCardHandles);
992 lrv = list_append(&threadContext->cardsList, &hCard);
995 Log2(PCSC_LOG_CRITICAL,
996 "list_append failed with return value: %d", lrv);
1015 lrv = list_delete(&threadContext->cardsList, &hCard);
1019 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %d", lrv);
1027static LONG MSGCheckHandleAssociation(
SCARDHANDLE hCard,
1032 if (0 == threadContext->hContext)
1036 Log1(PCSC_LOG_CRITICAL,
"Invalidated handle");
1041 list_index = list_locate(&threadContext->cardsList, &hCard);
1043 if (list_index >= 0)
1047 Log1(PCSC_LOG_ERROR,
"Client failed to authenticate");
1057static void MSGCleanupClient(
SCONTEXT * threadContext)
1062 if (threadContext->hContext != 0)
1064 (void)SCardReleaseContext(threadContext->hContext);
1065 (void)MSGRemoveContext(threadContext->hContext, threadContext);
1069 list_destroy(&threadContext->cardsList);
1072 Log3(PCSC_LOG_DEBUG,
1073 "Thread is stopping: dwClientID=%d, threadContext @%p",
1079 memset((
void*) threadContext, 0,
sizeof(
SCONTEXT));
1080 Log2(PCSC_LOG_DEBUG,
"Freeing SCONTEXT @%p", threadContext);
1087 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %x", lrv);
1089 free(threadContext);
1094 Log2(PCSC_LOG_DEBUG,
"Starting suicide alarm in %d seconds",
1095 TIME_BEFORE_SUICIDE);
1096 alarm(TIME_BEFORE_SUICIDE);
LONG EHTryToUnregisterClientForEvent(int32_t filedes)
Try to unregister a client If no client is found then do not log an error.
LONG EHUnregisterClientForEvent(int32_t filedes)
Unregister a client and log an error if the client is not found.
This handles card insertion/removal events, updates ATR, protocol, and status information.
#define SCARD_E_INVALID_HANDLE
The supplied handle was invalid.
#define SCARD_F_INTERNAL_ERROR
An internal consistency check failed.
#define SCARD_W_RESET_CARD
The smart card has been reset, so any shared state information is invalid.
#define SCARD_E_SERVICE_STOPPED
The Smart card resource manager has shut down.
#define SCARD_E_CANCELLED
The action was cancelled by an SCardCancel request.
#define SCARD_S_SUCCESS
No error was encountered.
#define SCARD_E_NO_MEMORY
Not enough memory available to complete this command.
#define SCARD_E_INVALID_VALUE
One or more of the supplied parameters values could not be properly interpreted.
#define SCARD_W_REMOVED_CARD
The smart card has been removed, so further communication is not possible.
#define SCARD_E_INSUFFICIENT_BUFFER
The data buffer to receive returned data is too small for the returned data.
#define SCARD_E_READER_UNAVAILABLE
The specified reader is not currently available for use.
This keeps a list of defines for pcsc-lite.
#define SCARD_RESET_CARD
Reset on close.
LONG SCARDCONTEXT
hContext returned by SCardEstablishContext()
#define SCARD_PROTOCOL_UNDEFINED
protocol not set
#define SCARD_LEAVE_CARD
Do nothing on close.
#define MAX_BUFFER_SIZE_EXTENDED
enhanced (64K + APDU + Lc + Le + SW) Tx/Rx Buffer
LONG SCARDHANDLE
hCard returned by SCardConnect()
#define PCSCLITE_MAX_READERS_CONTEXTS
Maximum readers context (a slot is count as a reader)
This keeps track of a list of currently available reader structures.
_Atomic SCARDHANDLE hLockId
Lock Id.
Protocol Control Information (PCI)
unsigned long dwProtocol
Protocol identifier.
unsigned long cbPciLength
Protocol Control Inf Length.
pthread_mutex_t cardsList_lock
lock for the above list
pthread_t pthThread
Event polling thread's ID.
uint32_t dwClientID
Connection ID used to reference the Client.
contained in SCARD_BEGIN_TRANSACTION Messages.
contained in SCARD_CANCEL Messages.
contained in SCARD_CONNECT Messages.
contained in SCARD_CONTROL Messages.
contained in SCARD_DISCONNECT Messages.
contained in SCARD_END_TRANSACTION Messages.
Information contained in SCARD_ESTABLISH_CONTEXT Messages.
contained in SCARD_GET_ATTRIB and Messages.
Define an exported public reader state structure so each application gets instant notification of cha...
contained in SCARD_RECONNECT Messages.
Information contained in SCARD_RELEASE_CONTEXT Messages.
contained in SCARD_STATUS Messages.
contained in SCARD_TRANSMIT Messages.
Information transmitted in CMD_VERSION Messages.
Information contained in CMD_WAIT_READER_STATE_CHANGE Messages.
uint32_t timeOut
timeout in ms
This handles abstract system level calls.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
This handles smart card reader communications.
INTERNAL LONG MessageSend(void *buffer_void, uint64_t buffer_size, int32_t filedes)
Sends a menssage from client to server or vice-versa.
INTERNAL LONG MessageReceive(void *buffer_void, uint64_t buffer_size, int32_t filedes)
Called by the Client to get the response from the server or vice-versa.
This defines some structures and #defines to be used over the transport layer.
#define PROTOCOL_VERSION_MAJOR
Major version of the current message protocol.
#define PROTOCOL_VERSION_MINOR
Minor version of the current message protocol.
@ SCARD_DISCONNECT
used by SCardDisconnect()
@ SCARD_SET_ATTRIB
used by SCardSetAttrib()
@ SCARD_RELEASE_CONTEXT
used by SCardReleaseContext()
@ CMD_STOP_WAITING_READER_STATE_CHANGE
stop waiting for a reader state change
@ CMD_GET_READERS_STATE
get the readers state
@ SCARD_CONTROL
used by SCardControl()
@ CMD_VERSION
get the client/server protocol version
@ CMD_WAIT_READER_STATE_CHANGE
wait for a reader state change
@ SCARD_RECONNECT
used by SCardReconnect()
@ SCARD_STATUS
used by SCardStatus()
@ SCARD_GET_ATTRIB
used by SCardGetAttrib()
@ SCARD_BEGIN_TRANSACTION
used by SCardBeginTransaction()
@ SCARD_TRANSMIT
used by SCardTransmit()
@ SCARD_END_TRANSACTION
used by SCardEndTransaction()
@ SCARD_CANCEL
used by SCardCancel()
@ SCARD_CONNECT
used by SCardConnect()
@ SCARD_ESTABLISH_CONTEXT
used by SCardEstablishContext()
bool AutoExit
Represents an Application Context on the Server side.
LONG CreateContextThread(uint32_t *pdwClientID)
Creates threads to handle messages received from Clients.
static const char * CommandsText[]
Handles messages received from Clients.
static list_t contextsList
Context tracking list.
pthread_mutex_t contextsList_lock
lock for the above list
This demarshalls functions over the message queue and keeps track of clients and their handles.