19 |
*/ |
*/ |
20 |
|
|
21 |
#include <stdio.h> |
#include <stdio.h> |
|
#include "rdesktop.h" |
|
22 |
#include <unistd.h> |
#include <unistd.h> |
23 |
#include <fcntl.h> |
#include <fcntl.h> |
24 |
#include <strings.h> |
#include <strings.h> |
25 |
#include <sys/types.h> |
#include <sys/types.h> |
26 |
#include <time.h> |
#include <time.h> |
27 |
|
/* PCSC does not use BOOL as parameters or return values in function calls, |
28 |
|
so let us just ignore their own definition of BOOL */ |
29 |
|
#define BOOL PCSC_BOOL |
30 |
#ifndef MAKE_PROTO |
#ifndef MAKE_PROTO |
31 |
#ifdef PCSC_OSX |
#ifdef PCSC_OSX |
32 |
|
#include <PCSC/wintypes.h> |
33 |
#include <PCSC/pcsclite.h> |
#include <PCSC/pcsclite.h> |
34 |
#include <PCSC/winscard.h> |
#include <PCSC/winscard.h> |
35 |
#else |
#else |
36 |
|
#include <wintypes.h> |
37 |
#include <pcsclite.h> |
#include <pcsclite.h> |
38 |
#include <winscard.h> |
#include <winscard.h> |
39 |
#endif /* PCSC_OSX */ |
#endif /* PCSC_OSX */ |
40 |
|
#undef BOOL |
41 |
|
#include "rdesktop.h" |
42 |
#include "scard.h" |
#include "scard.h" |
43 |
|
|
44 |
/* variable segment */ |
/* variable segment */ |
50 |
|
|
51 |
static struct stream out[STREAM_COUNT]; |
static struct stream out[STREAM_COUNT]; |
52 |
static int cur_stream_id = 0; |
static int cur_stream_id = 0; |
53 |
static pthread_mutex_t *tcp_sendcontrol_mutex = NULL; |
static pthread_mutex_t **scard_mutex = NULL; |
54 |
|
|
55 |
static uint32 curDevice = 0, curId = 0, curBytesOut = 0; |
static uint32 curDevice = 0, curId = 0, curBytesOut = 0; |
56 |
static PSCNameMapRec nameMapList = NULL; |
static PSCNameMapRec nameMapList = NULL; |
60 |
static pthread_mutex_t queueAccess; |
static pthread_mutex_t queueAccess; |
61 |
static pthread_mutex_t queueEmpty; |
static pthread_mutex_t queueEmpty; |
62 |
static pthread_mutex_t hcardAccess; |
static pthread_mutex_t hcardAccess; |
|
static pthread_mutex_t sendControl; |
|
63 |
|
|
64 |
static PMEM_HANDLE threadListHandle = NULL; |
static PMEM_HANDLE threadListHandle = NULL; |
65 |
static PThreadListElement threadList = NULL; |
static PThreadListElement threadList = NULL; |
85 |
|
|
86 |
#ifndef MAKE_PROTO |
#ifndef MAKE_PROTO |
87 |
|
|
88 |
static NTSTATUS |
static RD_NTSTATUS |
89 |
scard_create(uint32 device_id, uint32 accessmask, uint32 sharemode, uint32 create_disposition, |
scard_create(uint32 device_id, uint32 accessmask, uint32 sharemode, uint32 create_disposition, |
90 |
uint32 flags_and_attributes, char *filename, NTHANDLE * phandle) |
uint32 flags_and_attributes, char *filename, RD_NTHANDLE * phandle) |
91 |
{ |
{ |
92 |
return STATUS_SUCCESS; |
return RD_STATUS_SUCCESS; |
93 |
} |
} |
94 |
|
|
95 |
static NTSTATUS |
static RD_NTSTATUS |
96 |
scard_close(NTHANDLE handle) |
scard_close(RD_NTHANDLE handle) |
97 |
{ |
{ |
98 |
return STATUS_SUCCESS; |
return RD_STATUS_SUCCESS; |
99 |
} |
} |
100 |
|
|
101 |
static NTSTATUS |
static RD_NTSTATUS |
102 |
scard_read(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) |
scard_read(RD_NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) |
103 |
{ |
{ |
104 |
return STATUS_SUCCESS; |
return RD_STATUS_SUCCESS; |
105 |
} |
} |
106 |
|
|
107 |
static NTSTATUS |
static RD_NTSTATUS |
108 |
scard_write(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) |
scard_write(RD_NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) |
109 |
{ |
{ |
110 |
return STATUS_SUCCESS; |
return RD_STATUS_SUCCESS; |
111 |
} |
} |
112 |
#endif /* MAKE_PROTO */ |
#endif /* MAKE_PROTO */ |
113 |
|
|
157 |
return 0; |
return 0; |
158 |
} |
} |
159 |
|
|
|
if (0 != pthread_mutex_init(&sendControl, NULL)) |
|
|
{ |
|
|
error("[SMART CARD: Can't initialize send control mutex]\n"); |
|
|
return 0; |
|
|
} |
|
|
|
|
160 |
if (0 != |
if (0 != |
161 |
pthread_create(&queueHandler, NULL, (void *(*)(void *)) queue_handler_function, NULL)) |
pthread_create(&queueHandler, NULL, (void *(*)(void *)) queue_handler_function, NULL)) |
162 |
{ |
{ |
612 |
rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext); |
rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext); |
613 |
if (rv) |
if (rv) |
614 |
{ |
{ |
615 |
DEBUG_SCARD(("<--ERROR SCardEstablishContext Code=0x%.8x]-->\n", |
DEBUG_SCARD(("<--ERROR SCardEstablishContext Code=0x%.8x, %s]-->\n", |
616 |
(unsigned int) rv)); |
(unsigned int) rv, pcsc_stringify_error(rv))); |
617 |
} |
} |
618 |
else |
else |
619 |
{ |
{ |
642 |
|
|
643 |
if (rv) |
if (rv) |
644 |
{ |
{ |
645 |
DEBUG_SCARD(("<--ERROR SCardReleaseContext Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardReleaseContext Code=0x%.8x, %s-->\n", (unsigned int) rv, |
646 |
|
pcsc_stringify_error(rv))); |
647 |
} |
} |
648 |
else |
else |
649 |
{ |
{ |
676 |
|
|
677 |
if (rv) |
if (rv) |
678 |
{ |
{ |
679 |
DEBUG_SCARD(("<--ERROR SCardListReaders (no SCardIsValidContext) Code=0x%.8x-->\n", |
DEBUG_SCARD(("<--ERROR SCardListReaders (no SCardIsValidContext) Code=0x%.8x, %s-->\n", (unsigned int) rv, pcsc_stringify_error(rv))); |
|
(unsigned int) rv)); |
|
680 |
rv = SCARD_E_INVALID_HANDLE; |
rv = SCARD_E_INVALID_HANDLE; |
681 |
} |
} |
682 |
else |
else |
794 |
DEBUG_SCARD(("[MANGLED HCARD 0x%08x]\n", hCard)); |
DEBUG_SCARD(("[MANGLED HCARD 0x%08x]\n", hCard)); |
795 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
796 |
{ |
{ |
797 |
DEBUG_SCARD(("<--ERROR SCardConnect Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardConnect Code=0x%.8x, %s-->\n", (unsigned int) rv, |
798 |
|
pcsc_stringify_error(rv))); |
799 |
} |
} |
800 |
else |
else |
801 |
{ |
{ |
869 |
(MYPCSC_DWORD) dwInitialization, &dwActiveProtocol); |
(MYPCSC_DWORD) dwInitialization, &dwActiveProtocol); |
870 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
871 |
{ |
{ |
872 |
DEBUG_SCARD(("<--ERROR SCardReconnect Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardReconnect Code=0x%.8x, %s-->\n", (unsigned int) rv, |
873 |
|
pcsc_stringify_error(rv))); |
874 |
} |
} |
875 |
else |
else |
876 |
{ |
{ |
926 |
|
|
927 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
928 |
{ |
{ |
929 |
DEBUG_SCARD(("<--ERROR SCardDisconnect Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardDisconnect Code=0x%.8x, %s-->\n", (unsigned int) rv, |
930 |
|
pcsc_stringify_error(rv))); |
931 |
} |
} |
932 |
else |
else |
933 |
{ |
{ |
1139 |
|
|
1140 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
1141 |
{ |
{ |
1142 |
DEBUG_SCARD(("<--ERROR SCardGetStatusChange Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardGetStatusChange Code=0x%.8x, %s-->\n", |
1143 |
|
(unsigned int) rv, pcsc_stringify_error(rv))); |
1144 |
} |
} |
1145 |
else |
else |
1146 |
{ |
{ |
1210 |
rv = SCardCancel((MYPCSC_SCARDCONTEXT) hContext); |
rv = SCardCancel((MYPCSC_SCARDCONTEXT) hContext); |
1211 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
1212 |
{ |
{ |
1213 |
DEBUG_SCARD(("<--ERROR SCardCancel Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardCancel Code=0x%.8x, %s-->\n", (unsigned int) rv, |
1214 |
|
pcsc_stringify_error(rv))); |
1215 |
} |
} |
1216 |
else |
else |
1217 |
{ |
{ |
1280 |
copyReaderState_MyPCSCToServer(myRsArray, rsArray, readerCount); |
copyReaderState_MyPCSCToServer(myRsArray, rsArray, readerCount); |
1281 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
1282 |
{ |
{ |
1283 |
DEBUG_SCARD(("<--ERROR SCardGetStatusChange (no SCardLocateCardsByATR) Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardGetStatusChange (no SCardLocateCardsByATR) Code=0x%.8x, %s-->\n", (unsigned int) rv, pcsc_stringify_error(rv))); |
1284 |
} |
} |
1285 |
else |
else |
1286 |
{ |
{ |
1342 |
rv = SCardBeginTransaction(myHCard); |
rv = SCardBeginTransaction(myHCard); |
1343 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
1344 |
{ |
{ |
1345 |
DEBUG_SCARD(("<--ERROR SCardBeginTransaction Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardBeginTransaction Code=0x%.8x, %s-->\n", |
1346 |
|
(unsigned int) rv, pcsc_stringify_error(rv))); |
1347 |
} |
} |
1348 |
else |
else |
1349 |
{ |
{ |
1374 |
rv = SCardEndTransaction(myHCard, (MYPCSC_DWORD) dwDisposition); |
rv = SCardEndTransaction(myHCard, (MYPCSC_DWORD) dwDisposition); |
1375 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
1376 |
{ |
{ |
1377 |
DEBUG_SCARD(("<--ERROR SCardEndTransaction Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardEndTransaction Code=0x%.8x, %s-->\n", (unsigned int) rv, |
1378 |
|
pcsc_stringify_error(rv))); |
1379 |
} |
} |
1380 |
else |
else |
1381 |
{ |
{ |
1584 |
|
|
1585 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
1586 |
{ |
{ |
1587 |
DEBUG_SCARD(("<--ERROR SCardTransmit Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardTransmit Code=0x%.8x, %s-->\n", (unsigned int) rv, |
1588 |
|
pcsc_stringify_error(rv))); |
1589 |
} |
} |
1590 |
else |
else |
1591 |
{ |
{ |
1682 |
|
|
1683 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
1684 |
{ |
{ |
1685 |
DEBUG_SCARD(("<--ERROR SCardStatus Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardStatus Code=0x%.8x, %s-->\n", (unsigned int) rv, |
1686 |
|
pcsc_stringify_error(rv))); |
1687 |
return SC_returnCode(rv, &lcHandle, in, out); |
return SC_returnCode(rv, &lcHandle, in, out); |
1688 |
} |
} |
1689 |
else |
else |
1794 |
|
|
1795 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
1796 |
{ |
{ |
1797 |
DEBUG_SCARD(("<--ERROR SCardStatus (no ScardState) Code=0x%.8x-->\n", |
DEBUG_SCARD(("<--ERROR SCardStatus (no ScardState) Code=0x%.8x, %s-->\n", |
1798 |
(unsigned int) rv)); |
(unsigned int) rv, pcsc_stringify_error(rv))); |
1799 |
return SC_returnCode(rv, &lcHandle, in, out); |
return SC_returnCode(rv, &lcHandle, in, out); |
1800 |
} |
} |
1801 |
else |
else |
1873 |
|
|
1874 |
if (rv) |
if (rv) |
1875 |
{ |
{ |
1876 |
DEBUG_SCARD(("<--ERROR SCardListReaderGroups Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardListReaderGroups Code=0x%.8x, %s-->\n", |
1877 |
|
(unsigned int) rv, pcsc_stringify_error(rv))); |
1878 |
return SC_returnCode(rv, &lcHandle, in, out); |
return SC_returnCode(rv, &lcHandle, in, out); |
1879 |
} |
} |
1880 |
else |
else |
1965 |
|
|
1966 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
1967 |
{ |
{ |
1968 |
DEBUG_SCARD(("<--ERROR SCardGetAttrib Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardGetAttrib Code=0x%.8x, %s-->\n", (unsigned int) rv, |
1969 |
|
pcsc_stringify_error(rv))); |
1970 |
return SC_returnCode(rv, &lcHandle, in, out); |
return SC_returnCode(rv, &lcHandle, in, out); |
1971 |
} |
} |
1972 |
else |
else |
2034 |
|
|
2035 |
if (rv) |
if (rv) |
2036 |
{ |
{ |
2037 |
DEBUG_SCARD(("<--ERROR SCardSetAttrib Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardSetAttrib Code=0x%.8x, %s-->\n", (unsigned int) rv, |
2038 |
|
pcsc_stringify_error(rv))); |
2039 |
} |
} |
2040 |
else |
else |
2041 |
{ |
{ |
2138 |
#ifdef WITH_DEBUG_SCARD |
#ifdef WITH_DEBUG_SCARD |
2139 |
if (rv != SCARD_S_SUCCESS) |
if (rv != SCARD_S_SUCCESS) |
2140 |
{ |
{ |
2141 |
DEBUG_SCARD(("<--ERROR SCardControl Code=0x%.8x-->\n", (unsigned int) rv)); |
DEBUG_SCARD(("<--ERROR SCardControl Code=0x%.8x, %s-->\n", (unsigned int) rv, |
2142 |
|
pcsc_stringify_error(rv))); |
2143 |
} |
} |
2144 |
else |
else |
2145 |
{ |
{ |
2173 |
} |
} |
2174 |
|
|
2175 |
|
|
2176 |
static NTSTATUS |
static RD_NTSTATUS |
2177 |
scard_device_control(NTHANDLE handle, uint32 request, STREAM in, STREAM out) |
scard_device_control(RD_NTHANDLE handle, uint32 request, STREAM in, STREAM out) |
2178 |
{ |
{ |
2179 |
SERVER_DWORD Result = 0x00000000; |
SERVER_DWORD Result = 0x00000000; |
2180 |
unsigned char *psize, *pend, *pStatusCode; |
unsigned char *psize, *pend, *pStatusCode; |
2374 |
hexdump(pbeg, (size_t) (out->p) - (size_t) pbeg); |
hexdump(pbeg, (size_t) (out->p) - (size_t) pbeg); |
2375 |
DEBUG_SCARD(("--------------------------------\n")); |
DEBUG_SCARD(("--------------------------------\n")); |
2376 |
#endif |
#endif |
2377 |
return STATUS_SUCCESS; |
return RD_STATUS_SUCCESS; |
2378 |
} |
} |
2379 |
|
|
2380 |
/* Thread functions */ |
/* Thread functions */ |
2429 |
} |
} |
2430 |
|
|
2431 |
static PSCThreadData |
static PSCThreadData |
2432 |
SC_addToQueue(NTHANDLE handle, uint32 request, STREAM in, STREAM out) |
SC_addToQueue(RD_NTHANDLE handle, uint32 request, STREAM in, STREAM out) |
2433 |
{ |
{ |
2434 |
PMEM_HANDLE lcHandle = NULL; |
PMEM_HANDLE lcHandle = NULL; |
2435 |
PSCThreadData data = SC_xmalloc(&lcHandle, sizeof(TSCThreadData)); |
PSCThreadData data = SC_xmalloc(&lcHandle, sizeof(TSCThreadData)); |
2509 |
size_t buffer_len = 0; |
size_t buffer_len = 0; |
2510 |
scard_device_control(data->handle, data->request, data->in, data->out); |
scard_device_control(data->handle, data->request, data->in, data->out); |
2511 |
buffer_len = (size_t) data->out->p - (size_t) data->out->data; |
buffer_len = (size_t) data->out->p - (size_t) data->out->data; |
|
pthread_mutex_lock(&sendControl); |
|
2512 |
rdpdr_send_completion(data->device, data->id, 0, buffer_len, data->out->data, buffer_len); |
rdpdr_send_completion(data->device, data->id, 0, buffer_len, data->out->data, buffer_len); |
|
pthread_mutex_unlock(&sendControl); |
|
2513 |
SC_destroyThreadData(data); |
SC_destroyThreadData(data); |
2514 |
} |
} |
2515 |
|
|
2619 |
return NULL; |
return NULL; |
2620 |
} |
} |
2621 |
|
|
2622 |
static NTSTATUS |
static RD_NTSTATUS |
2623 |
thread_wrapper(NTHANDLE handle, uint32 request, STREAM in, STREAM out) |
thread_wrapper(RD_NTHANDLE handle, uint32 request, STREAM in, STREAM out) |
2624 |
{ |
{ |
2625 |
if (SC_addToQueue(handle, request, in, out)) |
if (SC_addToQueue(handle, request, in, out)) |
2626 |
return STATUS_PENDING | 0xC0000000; |
return RD_STATUS_PENDING | 0xC0000000; |
2627 |
else |
else |
2628 |
return STATUS_NO_SUCH_FILE; |
return RD_STATUS_NO_SUCH_FILE; |
2629 |
} |
} |
2630 |
|
|
2631 |
DEVICE_FNS scard_fns = { |
DEVICE_FNS scard_fns = { |
2638 |
#endif /* MAKE_PROTO */ |
#endif /* MAKE_PROTO */ |
2639 |
|
|
2640 |
void |
void |
2641 |
scard_tcp_lock(void) |
scard_lock(int lock) |
2642 |
{ |
{ |
2643 |
if (!tcp_sendcontrol_mutex) |
if (!scard_mutex) |
2644 |
|
{ |
2645 |
|
int i; |
2646 |
|
|
2647 |
|
scard_mutex = |
2648 |
|
(pthread_mutex_t **) xmalloc(sizeof(pthread_mutex_t *) * SCARD_LOCK_LAST); |
2649 |
|
|
2650 |
|
for (i = 0; i < SCARD_LOCK_LAST; i++) |
2651 |
|
{ |
2652 |
|
scard_mutex[i] = NULL; |
2653 |
|
} |
2654 |
|
} |
2655 |
|
|
2656 |
|
if (!scard_mutex[lock]) |
2657 |
{ |
{ |
2658 |
tcp_sendcontrol_mutex = (pthread_mutex_t *) xmalloc(sizeof(pthread_mutex_t)); |
scard_mutex[lock] = (pthread_mutex_t *) xmalloc(sizeof(pthread_mutex_t)); |
2659 |
pthread_mutex_init(tcp_sendcontrol_mutex, NULL); |
pthread_mutex_init(scard_mutex[lock], NULL); |
2660 |
} |
} |
2661 |
|
|
2662 |
pthread_mutex_lock(tcp_sendcontrol_mutex); |
pthread_mutex_lock(scard_mutex[lock]); |
2663 |
} |
} |
2664 |
|
|
2665 |
void |
void |
2666 |
scard_tcp_unlock(void) |
scard_unlock(int lock) |
2667 |
{ |
{ |
2668 |
pthread_mutex_unlock(tcp_sendcontrol_mutex); |
pthread_mutex_unlock(scard_mutex[lock]); |
2669 |
} |
} |
2670 |
|
|
2671 |
STREAM |
STREAM |