53 static int contextMaxThreadCounter = PCSC_MAX_CONTEXT_THREADS;
54 static int contextMaxCardHandles = PCSC_MAX_CONTEXT_CARD_HANDLES;
74 static LONG MSGCleanupClient(
SCONTEXT *);
76 static void ContextThread(LPVOID pdwIndex);
80 static int contextsListhContext_seeker(
const void *el,
const void *key)
84 if ((el == NULL) || (key == NULL))
86 Log3(PCSC_LOG_CRITICAL,
"called with NULL pointer: el=%p, key=%p",
91 if (currentContext->hContext == *(int32_t *)key)
96 LONG ContextsInitialize(
int customMaxThreadCounter,
97 int customMaxThreadCardHandles)
101 if (customMaxThreadCounter != 0)
102 contextMaxThreadCounter = customMaxThreadCounter;
104 if (customMaxThreadCardHandles != 0)
105 contextMaxCardHandles = customMaxThreadCardHandles;
107 lrv = list_init(&contextsList);
110 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d", lrv);
113 lrv = list_attributes_seeker(& contextsList, contextsListhContext_seeker);
116 Log2(PCSC_LOG_CRITICAL,
117 "list_attributes_seeker failed with return value: %d", lrv);
126 void ContextsDeinitialize(
void)
129 listSize = list_size(&contextsList);
130 Log2(PCSC_LOG_DEBUG,
"remaining threads: %d", listSize);
154 listSize = list_size(&contextsList);
155 if (listSize >= contextMaxThreadCounter)
157 Log2(PCSC_LOG_CRITICAL,
"Too many context running: %d", listSize);
162 newContext = malloc(
sizeof(*newContext));
163 if (NULL == newContext)
165 Log1(PCSC_LOG_CRITICAL,
"Could not allocate new context");
168 memset(newContext, 0,
sizeof(*newContext));
173 lrv = list_init(&newContext->cardsList);
176 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d", lrv);
181 list_attributes_copy(&newContext->cardsList, list_meter_int32_t, 1);
188 lrv = list_attributes_comparator(&newContext->cardsList,
189 list_comparator_int32_t);
192 Log2(PCSC_LOG_CRITICAL,
193 "list_attributes_comparator failed with return value: %d", lrv);
194 list_destroy(&newContext->cardsList);
200 lrv = list_append(&contextsList, newContext);
203 Log2(PCSC_LOG_CRITICAL,
"list_append failed with return value: %d",
205 list_destroy(&newContext->cardsList);
209 rv = ThreadCreate(&newContext->
pthThread, THREAD_ATTR_DETACHED,
210 (PCSCLITE_THREAD_FUNCTION( )) ContextThread, (LPVOID) newContext);
215 Log2(PCSC_LOG_CRITICAL,
"ThreadCreate failed: %s", strerror(rv));
216 lrv2 = list_delete(&contextsList, newContext);
218 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %d", lrv2);
219 list_destroy(&newContext->cardsList);
236 (void)close(*pdwClientID);
271 "CANCEL_TRANSACTION",
275 "CMD_GET_READERS_STATE",
276 "CMD_WAIT_READER_STATE_CHANGE",
277 "CMD_STOP_WAITING_READER_STATE_CHANGE",
282 #define READ_BODY(v) \
283 if (header.size != sizeof(v)) { goto wrong_length; } \
284 ret = MessageReceive(&v, sizeof(v), filedes); \
285 if (ret != SCARD_S_SUCCESS) { Log2(PCSC_LOG_DEBUG, "Client die: %d", filedes); goto exit; }
287 #define WRITE_BODY(v) \
288 WRITE_BODY_WITH_COMMAND(CommandsText[header.command], v)
289 #define WRITE_BODY_WITH_COMMAND(command, v) \
290 Log4(PCSC_LOG_DEBUG, "%s rv=0x%X for client %d", command, v.rv, filedes); \
291 ret = MessageSend(&v, sizeof(v), filedes);
293 static void ContextThread(LPVOID newContext)
298 Log3(PCSC_LOG_DEBUG,
"Thread is started: dwClientID=%d, threadContext @%p",
309 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
314 if ((header.command > CMD_ENUM_FIRST)
315 && (header.command < CMD_ENUM_LAST))
316 Log3(PCSC_LOG_DEBUG,
"Received command: %s from client %d",
317 CommandsText[header.command], filedes);
319 switch (header.command)
328 Log3(PCSC_LOG_DEBUG, "Client is protocol version %d:%d",
337 Log3(PCSC_LOG_CRITICAL,
"Client protocol is %d:%d",
338 veStr.major, veStr.minor);
339 Log3(PCSC_LOG_CRITICAL,
"Server protocol is %d:%d",
359 RFWaitForReaderInit();
363 ret =
MessageSend(readerStates,
sizeof(readerStates), filedes);
374 EHRegisterClientForEvent(filedes);
402 hContext = esStr.hContext;
405 esStr.hContext = hContext;
408 esStr.rv = MSGAddContext(esStr.hContext, threadContext);
423 reStr.rv = MSGRemoveContext(reStr.hContext, threadContext);
433 DWORD dwActiveProtocol;
438 dwActiveProtocol = coStr.dwActiveProtocol;
441 coStr.dwShareMode, coStr.dwPreferredProtocols,
442 &hCard, &dwActiveProtocol);
445 coStr.dwActiveProtocol = dwActiveProtocol;
448 coStr.rv = MSGAddHandle(coStr.hContext, coStr.hCard,
458 DWORD dwActiveProtocol;
462 if (MSGCheckHandleAssociation(rcStr.hCard, threadContext))
466 rcStr.dwPreferredProtocols, rcStr.dwInitialization,
468 rcStr.dwActiveProtocol = dwActiveProtocol;
480 if (MSGCheckHandleAssociation(diStr.hCard, threadContext))
486 diStr.rv = MSGRemoveHandle(diStr.hCard, threadContext);
498 if (MSGCheckHandleAssociation(beStr.hCard, threadContext))
513 if (MSGCheckHandleAssociation(enStr.hCard, threadContext))
517 enStr.dwDisposition);
531 psTargetContext = (
SCONTEXT *) list_seek(&contextsList,
534 if (psTargetContext != NULL)
536 uint32_t fd = psTargetContext->dwClientID;
552 if (MSGCheckHandleAssociation(stStr.hCard, threadContext))
556 stStr.rv =
SCardStatus(stStr.hCard, NULL, NULL, NULL,
574 if (MSGCheckHandleAssociation(trStr.hCard, threadContext))
578 if ((trStr.pcbRecvLength > sizeof(pbRecvBuffer))
579 || (trStr.cbSendLength > sizeof(pbSendBuffer)))
580 goto buffer_overflow;
586 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
590 ioSendPci.
dwProtocol = trStr.ioSendPciProtocol;
592 ioRecvPci.
dwProtocol = trStr.ioRecvPciProtocol;
594 cbRecvLength = trStr.pcbRecvLength;
597 pbSendBuffer, trStr.cbSendLength, &ioRecvPci,
598 pbRecvBuffer, &cbRecvLength);
600 trStr.ioSendPciProtocol = ioSendPci.
dwProtocol;
602 trStr.ioRecvPciProtocol = ioRecvPci.
dwProtocol;
604 trStr.pcbRecvLength = cbRecvLength;
610 ret =
MessageSend(pbRecvBuffer, cbRecvLength, filedes);
619 DWORD dwBytesReturned;
623 if (MSGCheckHandleAssociation(ctStr.hCard, threadContext))
627 if ((ctStr.cbRecvLength > sizeof(pbRecvBuffer))
628 || (ctStr.cbSendLength > sizeof(pbSendBuffer)))
630 goto buffer_overflow;
637 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
641 dwBytesReturned = ctStr.dwBytesReturned;
643 ctStr.rv =
SCardControl(ctStr.hCard, ctStr.dwControlCode,
644 pbSendBuffer, ctStr.cbSendLength,
645 pbRecvBuffer, ctStr.cbRecvLength,
648 ctStr.dwBytesReturned = dwBytesReturned;
654 ret =
MessageSend(pbRecvBuffer, dwBytesReturned, filedes);
665 if (MSGCheckHandleAssociation(gsStr.hCard, threadContext))
669 if (gsStr.cbAttrLen > sizeof(gsStr.pbAttr))
670 goto buffer_overflow;
672 cbAttrLen = gsStr.cbAttrLen;
675 gsStr.pbAttr, &cbAttrLen);
677 gsStr.cbAttrLen = cbAttrLen;
689 if (MSGCheckHandleAssociation(gsStr.hCard, threadContext))
693 if (gsStr.cbAttrLen > sizeof(gsStr.pbAttr))
694 goto buffer_overflow;
697 gsStr.pbAttr, gsStr.cbAttrLen);
704 Log2(PCSC_LOG_CRITICAL, "Unknown command: %d", header.command);
712 Log2(PCSC_LOG_DEBUG,
"Client die: %d", filedes);
718 Log2(PCSC_LOG_DEBUG,
"Buffer overflow detected: %d", filedes);
721 Log2(PCSC_LOG_DEBUG,
"Wrong length: %d", filedes);
723 (void)close(filedes);
724 (void)MSGCleanupClient(threadContext);
725 (void)pthread_exit((LPVOID) NULL);
728 LONG MSGSignalClient(uint32_t filedes, LONG rv)
733 Log2(PCSC_LOG_DEBUG,
"Signal client: %d", filedes);
736 WRITE_BODY_WITH_COMMAND(
"SIGNAL", waStr)
743 threadContext->hContext = hContext;
752 if (threadContext->hContext != hContext)
756 while (list_size(&threadContext->cardsList) != 0)
765 ptr = list_get_at(&threadContext->cardsList, 0);
768 Log1(PCSC_LOG_CRITICAL,
"list_get_at failed");
771 hCard = *(int32_t *)ptr;
776 rv = RFReaderInfoById(hCard, &rContext);
786 if (hCard != hLockId)
801 rv =
SCardStatus(hCard, NULL, NULL, NULL, NULL, NULL, NULL);
810 lrv = list_delete_at(&threadContext->cardsList, 0);
812 Log2(PCSC_LOG_CRITICAL,
813 "list_delete_at failed with return value: %d", lrv);
816 list_destroy(&threadContext->cardsList);
820 threadContext->hContext = 0;
830 if (threadContext->hContext == hContext)
839 listLength = list_size(&threadContext->cardsList);
840 if (listLength >= contextMaxCardHandles)
843 "Too many card handles for thread context @%p: %d (max is %d)"
844 "Restart pcscd with --max-card-handle-per-thread value",
845 threadContext, listLength, contextMaxCardHandles);
850 lrv = list_append(&threadContext->cardsList, &hCard);
853 Log2(PCSC_LOG_CRITICAL,
854 "list_append failed with return value: %d", lrv);
871 lrv = list_delete(&threadContext->cardsList, &hCard);
875 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %d", lrv);
883 static LONG MSGCheckHandleAssociation(
SCARDHANDLE hCard,
888 if (0 == threadContext->hContext)
892 Log1(PCSC_LOG_CRITICAL,
"Invalidated handle");
897 list_index = list_locate(&threadContext->cardsList, &hCard);
903 Log1(PCSC_LOG_ERROR,
"Client failed to authenticate");
913 static LONG MSGCleanupClient(
SCONTEXT * threadContext)
918 if (threadContext->hContext != 0)
921 (void)MSGRemoveContext(threadContext->hContext, threadContext);
925 "Thread is stopping: dwClientID=%d, threadContext @%p",
931 memset((
void*) threadContext, 0,
sizeof(
SCONTEXT));
932 Log2(PCSC_LOG_DEBUG,
"Freeing SCONTEXT @%p", threadContext);
935 lrv = list_delete(&contextsList, threadContext);
936 listSize = list_size(&contextsList);
939 Log2(PCSC_LOG_CRITICAL,
"list_delete failed with error %x", lrv);
946 Log2(PCSC_LOG_DEBUG,
"Starting suicide alarm in %d seconds",
947 TIME_BEFORE_SUICIDE);
948 alarm(TIME_BEFORE_SUICIDE);