76static int contextMaxThreadCounter = PCSC_MAX_CONTEXT_THREADS;
77static int contextMaxCardHandles = PCSC_MAX_CONTEXT_CARD_HANDLES;
97static void MSGCleanupClient(
SCONTEXT *);
99static void * ContextThread(LPVOID pdwIndex);
102extern int16_t ReaderEvents;
104static 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)
120LONG ContextsInitialize(
int customMaxThreadCounter,
121 int customMaxThreadCardHandles)
125 if (customMaxThreadCounter != 0)
126 contextMaxThreadCounter = customMaxThreadCounter;
128 if (customMaxThreadCardHandles != 0)
129 contextMaxCardHandles = customMaxThreadCardHandles;
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);
150void ContextsDeinitialize(
void)
158 Log2(PCSC_LOG_DEBUG,
"remaining threads: %d", listSize);
163 Log1(PCSC_LOG_ERROR,
"list_iterator_start failed");
169 Log3(PCSC_LOG_DEBUG,
"Cancel dwClientID=%d hContext: %p",
173 Log2(PCSC_LOG_DEBUG,
"Waiting client: %d", elt->
dwClientID);
175 Log2(PCSC_LOG_INFO,
"Client %d terminated", elt->
dwClientID);
203 if (listSize >= contextMaxThreadCounter)
205 Log2(PCSC_LOG_CRITICAL,
"Too many context running: %d", listSize);
210 newContext = malloc(
sizeof(*newContext));
211 if (NULL == newContext)
213 Log1(PCSC_LOG_CRITICAL,
"Could not allocate new context");
216 memset(newContext, 0,
sizeof(*newContext));
221 lrv = list_init(&newContext->cardsList);
224 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d", lrv);
229 list_attributes_copy(&newContext->cardsList, list_meter_int32_t, 1);
236 lrv = list_attributes_comparator(&newContext->cardsList,
237 list_comparator_int32_t);
240 Log2(PCSC_LOG_CRITICAL,
241 "list_attributes_comparator failed with return value: %d", lrv);
242 list_destroy(&newContext->cardsList);
251 Log2(PCSC_LOG_CRITICAL,
"list_append failed with return value: %d",
253 list_destroy(&newContext->cardsList);
257 rv = ThreadCreate(&newContext->
pthThread, THREAD_ATTR_DETACHED,
258 (PCSCLITE_THREAD_FUNCTION( )) ContextThread, (LPVOID) newContext);
263 Log2(PCSC_LOG_CRITICAL,
"ThreadCreate failed: %s", strerror(rv));
266 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %d", lrv2);
267 list_destroy(&newContext->cardsList);
284 (void)close(*pdwClientID);
319 "CANCEL_TRANSACTION",
323 "CMD_GET_READERS_STATE",
324 "CMD_WAIT_READER_STATE_CHANGE",
325 "CMD_STOP_WAITING_READER_STATE_CHANGE",
326 "CMD_GET_READER_EVENTS",
331#define READ_BODY(v) \
333 if (header.size != sizeof(v)) \
335 ret = MessageReceive(&v, sizeof(v), filedes); \
336 if (ret != SCARD_S_SUCCESS) { \
337 Log2(PCSC_LOG_DEBUG, "Client die: %d", filedes); \
342#define WRITE_BODY(v) \
343 WRITE_BODY_WITH_COMMAND(CommandsText[header.command], v)
344#define WRITE_BODY_WITH_COMMAND(command, v) \
346 LogRv4(PCSC_LOG_DEBUG, v.rv, "%s for client %d", command, filedes); \
347 ret = MessageSend(&v, sizeof(v), filedes); \
350static void * ContextThread(LPVOID newContext)
355 if (IsClientAuthorized(filedes,
"access_pcsc", NULL) == 0)
357 Log1(PCSC_LOG_CRITICAL,
"Rejected unauthorized PC/SC client");
362 Log1(PCSC_LOG_DEBUG,
"Authorized PC/SC client");
365 Log3(PCSC_LOG_DEBUG,
"Thread is started: dwClientID=%d, threadContext @%p",
376 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
381 if ((header.command > CMD_ENUM_FIRST)
382 && (header.command < CMD_ENUM_LAST))
383 Log3(PCSC_LOG_DEBUG,
"Received command: %s from client %d",
386 switch (header.command)
395 Log3(PCSC_LOG_DEBUG,
"Client is protocol version %d:%d",
396 veStr.major, veStr.minor);
404 Log1(PCSC_LOG_CRITICAL,
405 "Communication protocol mismatch!");
406 Log3(PCSC_LOG_ERROR,
"Client protocol is %d:%d",
407 veStr.major, veStr.minor);
408 Log3(PCSC_LOG_ERROR,
"Server protocol is %d:%d",
428 RFWaitForReaderInit();
432 ret =
MessageSend(readerStates,
sizeof(readerStates), filedes);
442 RFWaitForReaderInit();
446 EHRegisterClientForEvent(filedes);
478 .readerEvents = ReaderEvents,
482 WRITE_BODY(readerEvents);
493 hContext = esStr.hContext;
494 esStr.rv = SCardEstablishContext(esStr.dwScope, 0, 0,
496 esStr.hContext = hContext;
499 esStr.rv = MSGAddContext(esStr.hContext, threadContext);
511 reStr.rv = SCardReleaseContext(reStr.hContext);
514 reStr.rv = MSGRemoveContext(reStr.hContext, threadContext);
524 DWORD dwActiveProtocol;
528 coStr.szReader[
sizeof(coStr.szReader)-1] = 0;
530 dwActiveProtocol = coStr.dwActiveProtocol;
532 if (IsClientAuthorized(filedes,
"access_card", coStr.szReader) == 0)
534 Log2(PCSC_LOG_CRITICAL,
"Rejected unauthorized client for '%s'", coStr.szReader);
538 dwActiveProtocol = -1;
542 Log2(PCSC_LOG_DEBUG,
"Authorized client for '%s'", coStr.szReader);
544 coStr.rv = SCardConnect(coStr.hContext, coStr.szReader,
545 coStr.dwShareMode, coStr.dwPreferredProtocols,
546 &hCard, &dwActiveProtocol);
550 coStr.dwActiveProtocol = dwActiveProtocol;
554 coStr.rv = MSGAddHandle(coStr.hContext, coStr.hCard,
573 if (MSGCheckHandleAssociation(rcStr.hCard, threadContext))
576 rcStr.rv = SCardReconnect(rcStr.hCard, rcStr.dwShareMode,
577 rcStr.dwPreferredProtocols, rcStr.dwInitialization,
579 rcStr.dwActiveProtocol = dwActiveProtocol;
591 if (MSGCheckHandleAssociation(diStr.hCard, threadContext))
594 diStr.rv = SCardDisconnect(diStr.hCard, diStr.dwDisposition);
597 diStr.rv = MSGRemoveHandle(diStr.hCard, threadContext);
609 if (MSGCheckHandleAssociation(beStr.hCard, threadContext))
612 beStr.rv = SCardBeginTransaction(beStr.hCard);
624 if (MSGCheckHandleAssociation(enStr.hCard, threadContext))
627 enStr.rv = SCardEndTransaction(enStr.hCard,
628 enStr.dwDisposition);
650 if (psTargetContext != NULL)
652 uint32_t fd = psTargetContext->dwClientID;
676 if (MSGCheckHandleAssociation(stStr.hCard, threadContext))
680 stStr.rv = SCardStatus(stStr.hCard, NULL, NULL, NULL,
698 if (MSGCheckHandleAssociation(trStr.hCard, threadContext))
702 if (trStr.cbSendLength >
sizeof(pbSendBuffer))
703 goto buffer_overflow;
709 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
713 ioSendPci.
dwProtocol = trStr.ioSendPciProtocol;
715 ioRecvPci.
dwProtocol = trStr.ioRecvPciProtocol;
717 cbRecvLength =
sizeof pbRecvBuffer;
719 trStr.rv = SCardTransmit(trStr.hCard, &ioSendPci,
720 pbSendBuffer, trStr.cbSendLength, &ioRecvPci,
721 pbRecvBuffer, &cbRecvLength);
723 if (cbRecvLength > trStr.pcbRecvLength)
729 trStr.ioSendPciProtocol = ioSendPci.
dwProtocol;
731 trStr.ioRecvPciProtocol = ioRecvPci.
dwProtocol;
733 trStr.pcbRecvLength = cbRecvLength;
739 ret =
MessageSend(pbRecvBuffer, cbRecvLength, filedes);
748 DWORD dwBytesReturned;
752 if (MSGCheckHandleAssociation(ctStr.hCard, threadContext))
756 if (ctStr.cbSendLength >
sizeof(pbSendBuffer))
758 goto buffer_overflow;
765 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
769 dwBytesReturned = ctStr.dwBytesReturned;
771 ctStr.rv = SCardControl(ctStr.hCard, ctStr.dwControlCode,
772 pbSendBuffer, ctStr.cbSendLength,
773 pbRecvBuffer,
sizeof pbRecvBuffer,
776 if (dwBytesReturned > ctStr.cbRecvLength)
782 ctStr.dwBytesReturned = dwBytesReturned;
788 ret =
MessageSend(pbRecvBuffer, dwBytesReturned, filedes);
799 if (MSGCheckHandleAssociation(gsStr.hCard, threadContext))
803 if (gsStr.cbAttrLen >
sizeof(gsStr.pbAttr))
804 goto buffer_overflow;
806 cbAttrLen = gsStr.cbAttrLen;
808 gsStr.rv = SCardGetAttrib(gsStr.hCard, gsStr.dwAttrId,
809 gsStr.pbAttr, &cbAttrLen);
811 gsStr.cbAttrLen = cbAttrLen;
823 if (MSGCheckHandleAssociation(gsStr.hCard, threadContext))
827 if (gsStr.cbAttrLen >
sizeof(gsStr.pbAttr))
828 goto buffer_overflow;
830 gsStr.rv = SCardSetAttrib(gsStr.hCard, gsStr.dwAttrId,
831 gsStr.pbAttr, gsStr.cbAttrLen);
838 Log2(PCSC_LOG_CRITICAL,
"Unknown command: %d", header.command);
846 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
852 Log2(PCSC_LOG_DEBUG,
"Buffer overflow detected: %d", filedes);
855 Log2(PCSC_LOG_DEBUG,
"Wrong length: %d", filedes);
857 (void)close(filedes);
858 MSGCleanupClient(threadContext);
859 (void)pthread_exit((LPVOID) NULL);
862LONG MSGSignalClient(uint32_t filedes, LONG rv)
871 Log2(PCSC_LOG_DEBUG,
"Signal client: %d", filedes);
874 WRITE_BODY_WITH_COMMAND(
"SIGNAL", waStr);
879LONG MSGSendReaderStates(uint32_t filedes)
883 Log2(PCSC_LOG_DEBUG,
"Send reader states: %d", filedes);
886 ret =
MessageSend(readerStates,
sizeof(readerStates), filedes);
893 threadContext->hContext = hContext;
902 if (0 == threadContext->hContext)
904 Log1(PCSC_LOG_ERROR,
"Invalidated handle");
908 if (threadContext->hContext != hContext)
912 while (list_size(&threadContext->cardsList) != 0)
921 ptr = list_get_at(&threadContext->cardsList, 0);
924 Log1(PCSC_LOG_CRITICAL,
"list_get_at failed");
927 hCard = *(int32_t *)ptr;
934 rv = RFReaderInfoById(hCard, &rContext);
951 if (hCard != rContext->
hLockId)
970 rv = SCardStatus(hCard, NULL, NULL, NULL, NULL, NULL, NULL);
981 lrv = list_delete_at(&threadContext->cardsList, 0);
983 Log2(PCSC_LOG_CRITICAL,
984 "list_delete_at failed with return value: %d", lrv);
987 UNREF_READER(rContext)
994 threadContext->hContext = 0;
1004 if (0 == threadContext->hContext)
1006 Log1(PCSC_LOG_ERROR,
"Invalidated handle");
1010 if (threadContext->hContext == hContext)
1019 listLength = list_size(&threadContext->cardsList);
1020 if (listLength >= contextMaxCardHandles)
1022 Log4(PCSC_LOG_DEBUG,
1023 "Too many card handles for thread context @%p: %d (max is %d). "
1024 "Restart pcscd with --max-card-handle-per-thread value",
1025 threadContext, listLength, contextMaxCardHandles);
1032 lrv = list_append(&threadContext->cardsList, &hCard);
1035 Log2(PCSC_LOG_CRITICAL,
1036 "list_append failed with return value: %d", lrv);
1055 lrv = list_delete(&threadContext->cardsList, &hCard);
1059 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %d", lrv);
1067static LONG MSGCheckHandleAssociation(
SCARDHANDLE hCard,
1072 if (0 == threadContext->hContext)
1076 Log1(PCSC_LOG_CRITICAL,
"Invalidated handle");
1081 list_index = list_locate(&threadContext->cardsList, &hCard);
1083 if (list_index >= 0)
1087 Log1(PCSC_LOG_ERROR,
"Client failed to authenticate");
1097static void MSGCleanupClient(
SCONTEXT * threadContext)
1102 if (threadContext->hContext != 0)
1104 (void)SCardReleaseContext(threadContext->hContext);
1105 (void)MSGRemoveContext(threadContext->hContext, threadContext);
1109 list_destroy(&threadContext->cardsList);
1112 Log3(PCSC_LOG_DEBUG,
1113 "Thread is stopping: dwClientID=%d, threadContext @%p",
1119 memset((
void*) threadContext, 0,
sizeof(
SCONTEXT));
1120 Log2(PCSC_LOG_DEBUG,
"Freeing SCONTEXT @%p", threadContext);
1127 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %x", lrv);
1129 free(threadContext);
1134 Log2(PCSC_LOG_DEBUG,
"Starting suicide alarm in %d seconds",
1135 TIME_BEFORE_SUICIDE);
1136 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_SECURITY_VIOLATION
Access was denied because of a security violation.
#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.
#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()
@ CMD_GET_READER_EVENTS
get the number of reader events
@ 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.