~ubuntu-branches/ubuntu/jaunty/pcsc-lite/jaunty-security

« back to all changes in this revision

Viewing changes to pcsc-lite/src/winscard_clnt.c

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Rousseau
  • Date: 2005-11-27 18:04:59 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051127180459-qrex2gzpq9d8jexd
Tags: 1.2.9-beta9-1
* New upstream version
* debian/compat: change from 3 to 4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This handles smartcard reader communications and
3
 
 * forwarding requests over message queues.
4
 
 *
5
 
 * MUSCLE SmartCard Development ( http://www.linuxnet.com )
6
 
 *
7
 
 * Copyright (C) 1999-2004
8
 
 *  David Corcoran <corcoran@linuxnet.com>
9
 
 *  Damien Sauveron <damien.sauveron@labri.fr>
10
 
 *  Ludovic Rousseau <ludovic.rousseau@free.fr>
11
 
 *
12
 
 * $Id: winscard_clnt.c,v 1.54 2005/03/02 15:12:49 rousseau Exp $
13
 
 */
14
 
 
15
 
#include "config.h"
16
 
#include <stdlib.h>
17
 
#include <string.h>
18
 
#include <sys/types.h>
19
 
#include <fcntl.h>
20
 
#include <unistd.h>
21
 
#include <sys/un.h>
22
 
#include <errno.h>
23
 
 
24
 
#include "pcsclite.h"
25
 
#include "winscard.h"
26
 
#include "debuglog.h"
27
 
#include "thread_generic.h"
28
 
 
29
 
#include "readerfactory.h"
30
 
#include "eventhandler.h"
31
 
#include "sys_generic.h"
32
 
 
33
 
#include "winscard_msg.h"
34
 
 
35
 
#define SCARD_PROTOCOL_ANY_OLD  0x1000  /* used for backward compatibility */
36
 
 
37
 
#ifndef min
38
 
#define min(a,b) (((a) < (b)) ? (a) : (b))
39
 
#endif
40
 
 
41
 
struct _psChannelMap
42
 
{
43
 
        SCARDHANDLE hCard;
44
 
        LPTSTR readerName;
45
 
};
46
 
 
47
 
typedef struct _psChannelMap CHANNEL_MAP, *PCHANNEL_MAP;
48
 
 
49
 
static struct _psContextMap
50
 
{
51
 
        DWORD dwClientID;
52
 
        SCARDCONTEXT hContext;
53
 
        DWORD contextBlockStatus;
54
 
        PCSCLITE_THREAD_T TID;            /* Thread owner of this context */
55
 
        PCSCLITE_MUTEX_T mMutex;          /* Mutex for this context */
56
 
        CHANNEL_MAP psChannelMap[PCSCLITE_MAX_APPLICATION_CONTEXT_CHANNELS];
57
 
} psContextMap[PCSCLITE_MAX_APPLICATION_CONTEXTS];
58
 
 
59
 
static short isExecuted = 0;
60
 
static int mapAddr = 0;
61
 
 
62
 
static PCSCLITE_MUTEX clientMutex = PTHREAD_MUTEX_INITIALIZER;
63
 
 
64
 
static PREADER_STATE readerStates[PCSCLITE_MAX_READERS_CONTEXTS];
65
 
 
66
 
SCARD_IO_REQUEST g_rgSCardT0Pci = { SCARD_PROTOCOL_T0, 8 };
67
 
SCARD_IO_REQUEST g_rgSCardT1Pci = { SCARD_PROTOCOL_T1, 8 };
68
 
SCARD_IO_REQUEST g_rgSCardRawPci = { SCARD_PROTOCOL_RAW, 8 };
69
 
 
70
 
 
71
 
static LONG SCardAddContext(SCARDCONTEXT, DWORD);
72
 
static LONG SCardGetContextIndice(SCARDCONTEXT);
73
 
static LONG SCardGetContextIndiceTH(SCARDCONTEXT);
74
 
static LONG SCardRemoveContext(SCARDCONTEXT);
75
 
 
76
 
static LONG SCardAddHandle(SCARDHANDLE, DWORD, LPTSTR);
77
 
static LONG SCardGetIndicesFromHandle(SCARDHANDLE, PDWORD, PDWORD);
78
 
static LONG SCardGetIndicesFromHandleTH(SCARDHANDLE, PDWORD, PDWORD);
79
 
static LONG SCardRemoveHandle(SCARDHANDLE);
80
 
 
81
 
static LONG SCardGetSetAttrib(SCARDHANDLE hCard, int command, DWORD dwAttrId,
82
 
        LPBYTE pbAttr, LPDWORD pcbAttrLen);
83
 
 
84
 
static LONG SCardCheckDaemonAvailability(void);
85
 
 
86
 
/*
87
 
 * Thread safety functions
88
 
 */
89
 
inline static LONG SCardLockThread(void);
90
 
inline static LONG SCardUnlockThread(void);
91
 
 
92
 
static LONG SCardEstablishContextTH(DWORD, LPCVOID, LPCVOID, LPSCARDCONTEXT);
93
 
 
94
 
LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1,
95
 
        LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
96
 
{
97
 
        LONG rv;
98
 
 
99
 
        SCardLockThread();
100
 
        rv = SCardEstablishContextTH(dwScope, pvReserved1,
101
 
                pvReserved2, phContext);
102
 
        SCardUnlockThread();
103
 
 
104
 
        return rv;
105
 
}
106
 
 
107
 
static LONG SCardEstablishContextTH(DWORD dwScope, LPCVOID pvReserved1,
108
 
        LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
109
 
{
110
 
        LONG rv;
111
 
        int i;
112
 
        establish_struct scEstablishStruct;
113
 
        sharedSegmentMsg msgStruct;
114
 
        DWORD dwClientID = 0;
115
 
 
116
 
        if (phContext == NULL)
117
 
                return SCARD_E_INVALID_PARAMETER;
118
 
        else
119
 
                *phContext = 0;
120
 
 
121
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
122
 
                return SCARD_E_NO_SERVICE;
123
 
 
124
 
        /*
125
 
         * Do this only once
126
 
         */
127
 
        if (isExecuted == 0)
128
 
        {
129
 
                int pageSize;
130
 
 
131
 
                /*
132
 
                 * Initialize debug
133
 
                 */
134
 
                if (getenv("MUSCLECARD_DEBUG"))
135
 
                {
136
 
                        DebugLogSetLogType(DEBUGLOG_STDERR_DEBUG);
137
 
                        DebugLogSetLevel(PCSC_LOG_DEBUG);
138
 
                }
139
 
 
140
 
                /*
141
 
                 * Do any system initilization here
142
 
                 */
143
 
                SYS_Initialize();
144
 
 
145
 
                /*
146
 
                 * Set up the memory mapped reader stats structures
147
 
                 */
148
 
                mapAddr = SYS_OpenFile(PCSCLITE_PUBSHM_FILE, O_RDONLY, 0);
149
 
                if (mapAddr < 0)
150
 
                {
151
 
                        Log2(PCSC_LOG_CRITICAL, "Cannot open public shared file: %s",
152
 
                                PCSCLITE_PUBSHM_FILE);
153
 
                        return SCARD_E_NO_SERVICE;
154
 
                }
155
 
 
156
 
                pageSize = SYS_GetPageSize();
157
 
 
158
 
                /*
159
 
                 * Allocate each reader structure
160
 
                 */
161
 
                for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
162
 
                {
163
 
                        readerStates[i] = (PREADER_STATE)
164
 
                                SYS_PublicMemoryMap(sizeof(READER_STATE),
165
 
                                mapAddr, (i * pageSize));
166
 
                        if (readerStates[i] == NULL)
167
 
                        {
168
 
                                Log1(PCSC_LOG_CRITICAL, "Cannot public memory map");
169
 
                                SYS_CloseFile(mapAddr); /* Close the memory map file */
170
 
                                return SCARD_F_INTERNAL_ERROR;
171
 
                        }
172
 
                }
173
 
 
174
 
                for (i = 0; i < PCSCLITE_MAX_APPLICATION_CONTEXTS; i++)
175
 
                {
176
 
                        int j;
177
 
 
178
 
                        /*
179
 
                         * Initially set the context struct to zero
180
 
                         */
181
 
                        psContextMap[i].dwClientID = 0;
182
 
                        psContextMap[i].hContext = 0;
183
 
                        psContextMap[i].contextBlockStatus = BLOCK_STATUS_RESUME;
184
 
                        psContextMap[i].mMutex = 0;
185
 
 
186
 
                        for (j = 0; j < PCSCLITE_MAX_APPLICATION_CONTEXT_CHANNELS; j++)
187
 
                        {
188
 
                                /*
189
 
                                 * Initially set the hcard structs to zero
190
 
                                 */
191
 
                                psContextMap[i].psChannelMap[j].hCard = 0;
192
 
                                psContextMap[i].psChannelMap[j].readerName = NULL;
193
 
                        }
194
 
                }
195
 
 
196
 
        }
197
 
 
198
 
        /*
199
 
         * Is there a free slot for this connection ?
200
 
         */
201
 
 
202
 
        for (i = 0; i < PCSCLITE_MAX_APPLICATION_CONTEXTS; i++)
203
 
        {
204
 
                if (psContextMap[i].dwClientID == 0)
205
 
                        break;
206
 
        }
207
 
 
208
 
        if (i == PCSCLITE_MAX_APPLICATION_CONTEXTS)
209
 
        {
210
 
                return SCARD_E_NO_MEMORY;
211
 
        }
212
 
 
213
 
        if (SHMClientSetupSession(&dwClientID) != 0)
214
 
        {
215
 
                SYS_CloseFile(mapAddr);
216
 
                return SCARD_E_NO_SERVICE;
217
 
        }
218
 
 
219
 
        {       /* exchange client/server protocol versions */
220
 
                sharedSegmentMsg msgStruct;
221
 
                version_struct *veStr;
222
 
 
223
 
                memset(&msgStruct, 0, sizeof(msgStruct));
224
 
                msgStruct.mtype = CMD_VERSION;
225
 
                msgStruct.user_id = SYS_GetUID();
226
 
                msgStruct.group_id = SYS_GetGID();
227
 
                msgStruct.command = 0;
228
 
                msgStruct.date = time(NULL);
229
 
 
230
 
                veStr = (version_struct *) msgStruct.data;
231
 
                veStr->major = PROTOCOL_VERSION_MAJOR;
232
 
                veStr->minor = PROTOCOL_VERSION_MINOR;
233
 
 
234
 
                if (-1 == SHMMessageSend(&msgStruct, dwClientID,
235
 
                        PCSCLITE_MCLIENT_ATTEMPTS))
236
 
                        return SCARD_E_NO_SERVICE;
237
 
 
238
 
                /*
239
 
                 * Read a message from the server
240
 
                 */
241
 
                if (-1 == SHMMessageReceive(&msgStruct, dwClientID,
242
 
                        PCSCLITE_CLIENT_ATTEMPTS))
243
 
                {
244
 
                        Log1(PCSC_LOG_CRITICAL, "Your pcscd is too old and does not support CMD_VERSION");
245
 
                        return SCARD_F_COMM_ERROR;
246
 
                }
247
 
 
248
 
                Log3(PCSC_LOG_INFO, "Server is protocol version %d:%d",
249
 
                        veStr->major, veStr->minor);
250
 
 
251
 
                isExecuted = 1;
252
 
        }
253
 
 
254
 
 
255
 
        if (dwScope != SCARD_SCOPE_USER && dwScope != SCARD_SCOPE_TERMINAL &&
256
 
                dwScope != SCARD_SCOPE_SYSTEM && dwScope != SCARD_SCOPE_GLOBAL)
257
 
        {
258
 
                return SCARD_E_INVALID_VALUE;
259
 
        }
260
 
 
261
 
        scEstablishStruct.dwScope = dwScope;
262
 
        scEstablishStruct.phContext = 0;
263
 
 
264
 
        rv = WrapSHMWrite(SCARD_ESTABLISH_CONTEXT, dwClientID,
265
 
                sizeof(scEstablishStruct), PCSCLITE_MCLIENT_ATTEMPTS,
266
 
                (void *) &scEstablishStruct);
267
 
 
268
 
        if (rv == -1)
269
 
                return SCARD_E_NO_SERVICE;
270
 
 
271
 
        /*
272
 
         * Read a message from the server
273
 
         */
274
 
        rv = SHMClientRead(&msgStruct, dwClientID, PCSCLITE_CLIENT_ATTEMPTS);
275
 
 
276
 
        if (rv == -1)
277
 
                return SCARD_F_COMM_ERROR;
278
 
 
279
 
        memcpy(&scEstablishStruct, &msgStruct.data, sizeof(scEstablishStruct));
280
 
 
281
 
        if (scEstablishStruct.rv != SCARD_S_SUCCESS)
282
 
                return scEstablishStruct.rv;
283
 
 
284
 
        *phContext = scEstablishStruct.phContext;
285
 
 
286
 
        /*
287
 
         * Allocate the new hContext - if allocator full return an error
288
 
         */
289
 
 
290
 
        rv = SCardAddContext(*phContext, dwClientID);
291
 
 
292
 
        return rv;
293
 
}
294
 
 
295
 
LONG SCardReleaseContext(SCARDCONTEXT hContext)
296
 
{
297
 
        LONG rv;
298
 
        release_struct scReleaseStruct;
299
 
        sharedSegmentMsg msgStruct;
300
 
        DWORD dwContextIndex;
301
 
        PCSCLITE_THREAD_T currentTID;
302
 
 
303
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
304
 
                return SCARD_E_NO_SERVICE;
305
 
 
306
 
        /*
307
 
         * Make sure this context has been opened
308
 
         */
309
 
        dwContextIndex = SCardGetContextIndice(hContext);
310
 
        if (dwContextIndex == -1)
311
 
                return SCARD_E_INVALID_HANDLE;
312
 
 
313
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
314
 
 
315
 
        /*
316
 
         * Test if the thread that would release the context is the thread owning this context
317
 
         */
318
 
        currentTID = SYS_ThreadSelf();
319
 
        rv = SYS_ThreadEqual(&psContextMap[dwContextIndex].TID, &currentTID);
320
 
        
321
 
        if (rv == 0)
322
 
        {
323
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);
324
 
                /* Perhaps there is a better error code */
325
 
                return SCARD_F_INTERNAL_ERROR;
326
 
        }
327
 
 
328
 
        scReleaseStruct.hContext = hContext;
329
 
 
330
 
        rv = WrapSHMWrite(SCARD_RELEASE_CONTEXT, psContextMap[dwContextIndex].dwClientID,
331
 
                          sizeof(scReleaseStruct),
332
 
                          PCSCLITE_MCLIENT_ATTEMPTS, (void *) &scReleaseStruct);
333
 
 
334
 
        if (rv == -1)
335
 
        {
336
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
337
 
                return SCARD_E_NO_SERVICE;
338
 
        }
339
 
 
340
 
        /*
341
 
         * Read a message from the server
342
 
         */
343
 
        rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);
344
 
        memcpy(&scReleaseStruct, &msgStruct.data, sizeof(scReleaseStruct));
345
 
 
346
 
        if (rv == -1)
347
 
        {
348
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
349
 
                return SCARD_F_COMM_ERROR;
350
 
        }
351
 
        
352
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
353
 
 
354
 
        /*
355
 
         * Remove the local context from the stack
356
 
         */
357
 
        SCardLockThread();
358
 
        SCardRemoveContext(hContext);
359
 
        SCardUnlockThread();
360
 
 
361
 
        return scReleaseStruct.rv;
362
 
}
363
 
 
364
 
LONG SCardSetTimeout(SCARDCONTEXT hContext, DWORD dwTimeout)
365
 
{
366
 
        /*
367
 
         * Deprecated
368
 
         */
369
 
 
370
 
        return SCARD_S_SUCCESS;
371
 
}
372
 
 
373
 
LONG SCardConnect(SCARDCONTEXT hContext, LPCTSTR szReader,
374
 
        DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard,
375
 
        LPDWORD pdwActiveProtocol)
376
 
{
377
 
        LONG rv;
378
 
        connect_struct scConnectStruct;
379
 
        sharedSegmentMsg msgStruct;
380
 
        DWORD dwContextIndex;
381
 
 
382
 
        /*
383
 
         * Check for NULL parameters
384
 
         */
385
 
        if (phCard == 0 || pdwActiveProtocol == 0)
386
 
                return SCARD_E_INVALID_PARAMETER;
387
 
        else
388
 
                *phCard = 0;
389
 
 
390
 
        if (szReader == 0)
391
 
                return SCARD_E_UNKNOWN_READER;
392
 
 
393
 
        /*
394
 
         * Check for uninitialized strings
395
 
         */
396
 
        if (strlen(szReader) > MAX_READERNAME)
397
 
                return SCARD_E_INVALID_VALUE;
398
 
 
399
 
        if (!(dwPreferredProtocols & SCARD_PROTOCOL_T0) &&
400
 
                !(dwPreferredProtocols & SCARD_PROTOCOL_T1) &&
401
 
                !(dwPreferredProtocols & SCARD_PROTOCOL_RAW) &&
402
 
                !(dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD))
403
 
        {
404
 
                return SCARD_E_INVALID_VALUE;
405
 
        }
406
 
 
407
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
408
 
                return SCARD_E_NO_SERVICE;
409
 
 
410
 
        /*
411
 
         * Make sure this context has been opened
412
 
         */
413
 
        dwContextIndex = SCardGetContextIndice(hContext);
414
 
        if (dwContextIndex == -1)
415
 
                return SCARD_E_INVALID_HANDLE;
416
 
 
417
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
418
 
 
419
 
        strncpy(scConnectStruct.szReader, szReader, MAX_READERNAME);
420
 
 
421
 
        scConnectStruct.hContext = hContext;
422
 
        scConnectStruct.dwShareMode = dwShareMode;
423
 
        scConnectStruct.dwPreferredProtocols = dwPreferredProtocols;
424
 
        scConnectStruct.phCard = *phCard;
425
 
        scConnectStruct.pdwActiveProtocol = *pdwActiveProtocol;
426
 
 
427
 
        rv = WrapSHMWrite(SCARD_CONNECT, psContextMap[dwContextIndex].dwClientID,
428
 
                sizeof(scConnectStruct),
429
 
                PCSCLITE_CLIENT_ATTEMPTS, (void *) &scConnectStruct);
430
 
 
431
 
        if (rv == -1)
432
 
        {
433
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
434
 
                return SCARD_E_NO_SERVICE;
435
 
        }
436
 
 
437
 
        /*
438
 
         * Read a message from the server
439
 
         */
440
 
        rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);
441
 
 
442
 
        memcpy(&scConnectStruct, &msgStruct.data, sizeof(scConnectStruct));
443
 
 
444
 
        if (rv == -1)
445
 
        {
446
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
447
 
                return SCARD_F_COMM_ERROR;
448
 
        }
449
 
 
450
 
        *phCard = scConnectStruct.phCard;
451
 
        *pdwActiveProtocol = scConnectStruct.pdwActiveProtocol;
452
 
 
453
 
        if (scConnectStruct.rv == SCARD_S_SUCCESS)
454
 
        {
455
 
                /*
456
 
                 * Keep track of the handle locally
457
 
                 */
458
 
                rv = SCardAddHandle(*phCard, dwContextIndex, (LPTSTR) szReader);
459
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
460
 
                return rv;
461
 
        }
462
 
 
463
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
464
 
        return scConnectStruct.rv;
465
 
}
466
 
 
467
 
LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode,
468
 
        DWORD dwPreferredProtocols, DWORD dwInitialization,
469
 
        LPDWORD pdwActiveProtocol)
470
 
{
471
 
        LONG rv;
472
 
        reconnect_struct scReconnectStruct;
473
 
        sharedSegmentMsg msgStruct;
474
 
        int i;
475
 
        DWORD dwContextIndex, dwChannelIndex;
476
 
 
477
 
        if (dwInitialization != SCARD_LEAVE_CARD &&
478
 
                dwInitialization != SCARD_RESET_CARD &&
479
 
                dwInitialization != SCARD_UNPOWER_CARD &&
480
 
                dwInitialization != SCARD_EJECT_CARD)
481
 
        {
482
 
                return SCARD_E_INVALID_VALUE;
483
 
        }
484
 
 
485
 
        if (!(dwPreferredProtocols & SCARD_PROTOCOL_T0) &&
486
 
                !(dwPreferredProtocols & SCARD_PROTOCOL_T1) &&
487
 
                !(dwPreferredProtocols & SCARD_PROTOCOL_RAW) &&
488
 
                !(dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD))
489
 
        {
490
 
                return SCARD_E_INVALID_VALUE;
491
 
        }
492
 
 
493
 
        if (pdwActiveProtocol == 0)
494
 
                return SCARD_E_INVALID_PARAMETER;
495
 
 
496
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
497
 
                return SCARD_E_NO_SERVICE;
498
 
 
499
 
        /*
500
 
         * Make sure this handle has been opened
501
 
         */
502
 
        rv = SCardGetIndicesFromHandle(hCard, &dwContextIndex, &dwChannelIndex);
503
 
 
504
 
        if (rv == -1)
505
 
                return SCARD_E_INVALID_HANDLE;
506
 
 
507
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
508
 
 
509
 
 
510
 
        for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
511
 
        {
512
 
                char *r = psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName;
513
 
 
514
 
                /* by default r == NULL */
515
 
                if (r && strcmp(r, (readerStates[i])->readerName) == 0)
516
 
                        break;
517
 
        }
518
 
 
519
 
        if (i == PCSCLITE_MAX_READERS_CONTEXTS)
520
 
        {
521
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
522
 
                return SCARD_E_READER_UNAVAILABLE;
523
 
        }
524
 
 
525
 
        scReconnectStruct.hCard = hCard;
526
 
        scReconnectStruct.dwShareMode = dwShareMode;
527
 
        scReconnectStruct.dwPreferredProtocols = dwPreferredProtocols;
528
 
        scReconnectStruct.dwInitialization = dwInitialization;
529
 
        scReconnectStruct.pdwActiveProtocol = *pdwActiveProtocol;
530
 
 
531
 
        rv = WrapSHMWrite(SCARD_RECONNECT, psContextMap[dwContextIndex].dwClientID,
532
 
                sizeof(scReconnectStruct),
533
 
                PCSCLITE_CLIENT_ATTEMPTS, (void *) &scReconnectStruct);
534
 
 
535
 
        if (rv == -1)
536
 
        {
537
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
538
 
                return SCARD_E_NO_SERVICE;
539
 
        }
540
 
 
541
 
        /*
542
 
         * Read a message from the server
543
 
         */
544
 
        rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);
545
 
 
546
 
        memcpy(&scReconnectStruct, &msgStruct.data, sizeof(scReconnectStruct));
547
 
 
548
 
        if (rv == -1)
549
 
        {
550
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
551
 
                return SCARD_F_COMM_ERROR;
552
 
        }
553
 
 
554
 
        *pdwActiveProtocol = scReconnectStruct.pdwActiveProtocol;
555
 
 
556
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
557
 
        
558
 
        return scReconnectStruct.rv;
559
 
}
560
 
 
561
 
LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition)
562
 
{
563
 
        LONG rv;
564
 
        disconnect_struct scDisconnectStruct;
565
 
        sharedSegmentMsg msgStruct;
566
 
        DWORD dwContextIndex, dwChannelIndex;
567
 
 
568
 
        if (dwDisposition != SCARD_LEAVE_CARD &&
569
 
                dwDisposition != SCARD_RESET_CARD &&
570
 
                dwDisposition != SCARD_UNPOWER_CARD &&
571
 
                dwDisposition != SCARD_EJECT_CARD)
572
 
        {
573
 
                return SCARD_E_INVALID_VALUE;
574
 
        }
575
 
 
576
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
577
 
                return SCARD_E_NO_SERVICE;
578
 
 
579
 
        /*
580
 
         * Make sure this handle has been opened
581
 
         */
582
 
        rv = SCardGetIndicesFromHandle(hCard, &dwContextIndex, &dwChannelIndex);
583
 
 
584
 
        if (rv == -1)
585
 
                return SCARD_E_INVALID_HANDLE;
586
 
 
587
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
588
 
 
589
 
        scDisconnectStruct.hCard = hCard;
590
 
        scDisconnectStruct.dwDisposition = dwDisposition;
591
 
 
592
 
        rv = WrapSHMWrite(SCARD_DISCONNECT, psContextMap[dwContextIndex].dwClientID,
593
 
                sizeof(scDisconnectStruct),
594
 
                PCSCLITE_CLIENT_ATTEMPTS, (void *) &scDisconnectStruct);
595
 
 
596
 
        if (rv == -1)
597
 
        {
598
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
599
 
                return SCARD_E_NO_SERVICE;
600
 
        }
601
 
 
602
 
        /*
603
 
         * Read a message from the server
604
 
         */
605
 
        rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);
606
 
 
607
 
        memcpy(&scDisconnectStruct, &msgStruct.data,
608
 
                sizeof(scDisconnectStruct));
609
 
 
610
 
        if (rv == -1)
611
 
        {
612
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
613
 
                return SCARD_F_COMM_ERROR;
614
 
        }
615
 
 
616
 
        SCardRemoveHandle(hCard);
617
 
 
618
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
619
 
 
620
 
        return scDisconnectStruct.rv;
621
 
}
622
 
 
623
 
LONG SCardBeginTransaction(SCARDHANDLE hCard)
624
 
{
625
 
 
626
 
        LONG rv;
627
 
        begin_struct scBeginStruct;
628
 
        int i;
629
 
        sharedSegmentMsg msgStruct;
630
 
        DWORD dwContextIndex, dwChannelIndex;
631
 
 
632
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
633
 
                return SCARD_E_NO_SERVICE;
634
 
 
635
 
        /*
636
 
         * Make sure this handle has been opened
637
 
         */
638
 
        rv = SCardGetIndicesFromHandle(hCard, &dwContextIndex, &dwChannelIndex);
639
 
 
640
 
        if (rv == -1)
641
 
                return SCARD_E_INVALID_HANDLE;
642
 
 
643
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
644
 
 
645
 
        for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
646
 
        {
647
 
                char *r = psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName;
648
 
 
649
 
                /* by default r == NULL */
650
 
                if (r && strcmp(r, (readerStates[i])->readerName) == 0)
651
 
                        break;
652
 
        }
653
 
 
654
 
        if (i == PCSCLITE_MAX_READERS_CONTEXTS)
655
 
        {
656
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
657
 
                return SCARD_E_READER_UNAVAILABLE;
658
 
        }
659
 
 
660
 
        scBeginStruct.hCard = hCard;
661
 
 
662
 
        /*
663
 
         * Query the server every so often until the sharing violation ends
664
 
         * and then hold the lock for yourself.
665
 
         */
666
 
 
667
 
        do
668
 
        {
669
 
                /*
670
 
                 * Look to see if it is locked before polling the server for
671
 
                 * admission to the readers resources
672
 
                 */
673
 
                if ((readerStates[i])->lockState != 0)
674
 
                {
675
 
                        int randnum = 0;
676
 
                        int j;
677
 
 
678
 
                        for (j = 0; j < 100; j++)
679
 
                        {
680
 
                                /*
681
 
                                 * This helps prevent starvation
682
 
                                 */
683
 
                                randnum = SYS_RandomInt(1000, 10000);
684
 
                                SYS_USleep(randnum);
685
 
 
686
 
                                if ((readerStates[i])->lockState == 0)
687
 
                                {
688
 
                                        break;
689
 
                                }
690
 
                        }
691
 
                }
692
 
 
693
 
                rv = WrapSHMWrite(SCARD_BEGIN_TRANSACTION, psContextMap[dwContextIndex].dwClientID,
694
 
                        sizeof(scBeginStruct),
695
 
                        PCSCLITE_CLIENT_ATTEMPTS, (void *) &scBeginStruct);
696
 
 
697
 
                if (rv == -1)
698
 
                {
699
 
                        
700
 
                        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
701
 
                        return SCARD_E_NO_SERVICE;
702
 
                }
703
 
 
704
 
                /*
705
 
                 * Read a message from the server
706
 
                 */
707
 
                rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);
708
 
 
709
 
 
710
 
                memcpy(&scBeginStruct, &msgStruct.data, sizeof(scBeginStruct));
711
 
 
712
 
                if (rv == -1)
713
 
                {
714
 
                        
715
 
                        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
716
 
                        return SCARD_F_COMM_ERROR;
717
 
                }
718
 
 
719
 
        }
720
 
        while (scBeginStruct.rv == SCARD_E_SHARING_VIOLATION);
721
 
 
722
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
723
 
        return scBeginStruct.rv;
724
 
}
725
 
 
726
 
LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition)
727
 
{
728
 
        LONG rv;
729
 
        end_struct scEndStruct;
730
 
        sharedSegmentMsg msgStruct;
731
 
        int randnum, i;
732
 
        DWORD dwContextIndex, dwChannelIndex;
733
 
 
734
 
        /*
735
 
         * Zero out everything
736
 
         */
737
 
        randnum = 0;
738
 
 
739
 
        if (dwDisposition != SCARD_LEAVE_CARD &&
740
 
                dwDisposition != SCARD_RESET_CARD &&
741
 
                dwDisposition != SCARD_UNPOWER_CARD &&
742
 
                dwDisposition != SCARD_EJECT_CARD)
743
 
        {
744
 
                return SCARD_E_INVALID_VALUE;
745
 
        }
746
 
 
747
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
748
 
                return SCARD_E_NO_SERVICE;
749
 
 
750
 
        /*
751
 
         * Make sure this handle has been opened
752
 
         */
753
 
        rv = SCardGetIndicesFromHandle(hCard, &dwContextIndex, &dwChannelIndex);
754
 
 
755
 
        if (rv == -1)
756
 
                return SCARD_E_INVALID_HANDLE;
757
 
 
758
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
759
 
 
760
 
        for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
761
 
        {
762
 
                char *r = psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName;
763
 
 
764
 
                /* by default r == NULL */
765
 
                if (r && strcmp(r, (readerStates[i])->readerName) == 0)
766
 
                        break;
767
 
        }
768
 
 
769
 
        if (i == PCSCLITE_MAX_READERS_CONTEXTS)
770
 
        {
771
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
772
 
                return SCARD_E_READER_UNAVAILABLE;
773
 
        }
774
 
 
775
 
        scEndStruct.hCard = hCard;
776
 
        scEndStruct.dwDisposition = dwDisposition;
777
 
 
778
 
        rv = WrapSHMWrite(SCARD_END_TRANSACTION, psContextMap[dwContextIndex].dwClientID,
779
 
                sizeof(scEndStruct),
780
 
                PCSCLITE_CLIENT_ATTEMPTS, (void *) &scEndStruct);
781
 
 
782
 
        if (rv == -1)
783
 
        {
784
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
785
 
                return SCARD_E_NO_SERVICE;
786
 
        }
787
 
 
788
 
        /*
789
 
         * Read a message from the server
790
 
         */
791
 
        rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);
792
 
 
793
 
        memcpy(&scEndStruct, &msgStruct.data, sizeof(scEndStruct));
794
 
 
795
 
        if (rv == -1)
796
 
        {
797
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
798
 
                return SCARD_F_COMM_ERROR;
799
 
        }
800
 
 
801
 
        /*
802
 
         * This helps prevent starvation
803
 
         */
804
 
        randnum = SYS_RandomInt(1000, 10000);
805
 
        SYS_USleep(randnum);
806
 
 
807
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
808
 
 
809
 
        return scEndStruct.rv;
810
 
}
811
 
 
812
 
LONG SCardCancelTransaction(SCARDHANDLE hCard)
813
 
{
814
 
        LONG rv;
815
 
        cancel_struct scCancelStruct;
816
 
        sharedSegmentMsg msgStruct;
817
 
        int i;
818
 
        DWORD dwContextIndex, dwChannelIndex;
819
 
 
820
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
821
 
                return SCARD_E_NO_SERVICE;
822
 
 
823
 
        /*
824
 
         * Make sure this handle has been opened
825
 
         */
826
 
        rv = SCardGetIndicesFromHandle(hCard, &dwContextIndex, &dwChannelIndex);
827
 
 
828
 
        if (rv == -1)
829
 
                return SCARD_E_INVALID_HANDLE;
830
 
 
831
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
832
 
 
833
 
        for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
834
 
        {
835
 
                char *r = psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName;
836
 
 
837
 
                /* by default r == NULL */
838
 
                if (r && strcmp(r, (readerStates[i])->readerName) == 0)
839
 
                        break;
840
 
        }
841
 
 
842
 
        if (i == PCSCLITE_MAX_READERS_CONTEXTS)
843
 
        {
844
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
845
 
                return SCARD_E_READER_UNAVAILABLE;
846
 
        }
847
 
 
848
 
        scCancelStruct.hCard = hCard;
849
 
 
850
 
        rv = WrapSHMWrite(SCARD_CANCEL_TRANSACTION, psContextMap[dwContextIndex].dwClientID,
851
 
                sizeof(scCancelStruct),
852
 
                PCSCLITE_CLIENT_ATTEMPTS, (void *) &scCancelStruct);
853
 
 
854
 
        if (rv == -1)
855
 
        {
856
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
857
 
                return SCARD_E_NO_SERVICE;
858
 
        }
859
 
 
860
 
        /*
861
 
         * Read a message from the server
862
 
         */
863
 
        rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);
864
 
 
865
 
        memcpy(&scCancelStruct, &msgStruct.data, sizeof(scCancelStruct));
866
 
 
867
 
        if (rv == -1)
868
 
        {
869
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
870
 
                return SCARD_F_COMM_ERROR;
871
 
        }
872
 
 
873
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
874
 
 
875
 
        return scCancelStruct.rv;
876
 
}
877
 
 
878
 
LONG SCardStatus(SCARDHANDLE hCard, LPTSTR mszReaderNames,
879
 
        LPDWORD pcchReaderLen, LPDWORD pdwState,
880
 
        LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
881
 
{
882
 
        DWORD dwReaderLen, dwAtrLen;
883
 
        LONG rv;
884
 
        int i;
885
 
        status_struct scStatusStruct;
886
 
        sharedSegmentMsg msgStruct;
887
 
        DWORD dwContextIndex, dwChannelIndex;
888
 
 
889
 
        /*
890
 
         * Check for NULL parameters
891
 
         */
892
 
 
893
 
        if (pcchReaderLen == NULL || pcbAtrLen == NULL)
894
 
                return SCARD_E_INVALID_PARAMETER;
895
 
 
896
 
        /* length passed from caller */
897
 
        dwReaderLen = *pcchReaderLen;
898
 
        dwAtrLen = *pcbAtrLen;
899
 
 
900
 
        /* default output values */
901
 
        if (pdwState)
902
 
                *pdwState = 0;
903
 
 
904
 
        if (pdwProtocol)
905
 
                *pdwProtocol = 0;
906
 
 
907
 
        *pcchReaderLen = 0;
908
 
        *pcbAtrLen = 0;
909
 
 
910
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
911
 
                return SCARD_E_NO_SERVICE;
912
 
 
913
 
        /*
914
 
         * Make sure this handle has been opened
915
 
         */
916
 
        rv = SCardGetIndicesFromHandle(hCard, &dwContextIndex, &dwChannelIndex);
917
 
 
918
 
        if (rv == -1)
919
 
                return SCARD_E_INVALID_HANDLE;
920
 
 
921
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
922
 
 
923
 
        for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
924
 
        {
925
 
                char *r = psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName;
926
 
 
927
 
                /* by default r == NULL */
928
 
                if (r && strcmp(r, (readerStates[i])->readerName) == 0)
929
 
                        break;
930
 
        }
931
 
 
932
 
        if (i == PCSCLITE_MAX_READERS_CONTEXTS)
933
 
        {
934
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
935
 
                return SCARD_E_READER_UNAVAILABLE;
936
 
        }
937
 
 
938
 
        /* initialise the structure */
939
 
        memset(&scStatusStruct, 0, sizeof(scStatusStruct));
940
 
        scStatusStruct.hCard = hCard;
941
 
 
942
 
        /* those sizes need to be initialised */
943
 
        scStatusStruct.pcchReaderLen = sizeof(scStatusStruct.mszReaderNames);
944
 
        scStatusStruct.pcbAtrLen = sizeof(scStatusStruct.pbAtr);
945
 
 
946
 
        rv = WrapSHMWrite(SCARD_STATUS, psContextMap[dwContextIndex].dwClientID,
947
 
                sizeof(scStatusStruct),
948
 
                PCSCLITE_CLIENT_ATTEMPTS, (void *) &scStatusStruct);
949
 
 
950
 
        if (rv == -1)
951
 
        {
952
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
953
 
                return SCARD_E_NO_SERVICE;
954
 
        }
955
 
 
956
 
        /*
957
 
         * Read a message from the server
958
 
         */
959
 
        rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);
960
 
 
961
 
        memcpy(&scStatusStruct, &msgStruct.data, sizeof(scStatusStruct));
962
 
 
963
 
        if (rv == -1)
964
 
        {
965
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
966
 
                return SCARD_F_COMM_ERROR;
967
 
        }
968
 
 
969
 
        rv = scStatusStruct.rv;
970
 
        if (rv != SCARD_S_SUCCESS && rv != SCARD_E_INSUFFICIENT_BUFFER)
971
 
        {
972
 
                /*
973
 
                 * An event must have occurred
974
 
                 */
975
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
976
 
                return rv;
977
 
        }
978
 
 
979
 
        /*
980
 
         * Now continue with the client side SCardStatus
981
 
         */
982
 
 
983
 
        *pcchReaderLen = strlen(psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName) + 1;
984
 
        *pcbAtrLen = (readerStates[i])->cardAtrLength;
985
 
 
986
 
        if (pdwState)
987
 
                *pdwState = (readerStates[i])->readerState;
988
 
 
989
 
        if (pdwProtocol)
990
 
                *pdwProtocol = (readerStates[i])->cardProtocol;
991
 
 
992
 
        /* return SCARD_E_INSUFFICIENT_BUFFER only if buffer pointer is non NULL */
993
 
        if (mszReaderNames)
994
 
        {
995
 
                if (*pcchReaderLen > dwReaderLen)
996
 
                        rv = SCARD_E_INSUFFICIENT_BUFFER;
997
 
 
998
 
                strncpy(mszReaderNames, 
999
 
                        psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName, 
1000
 
                        dwReaderLen);
1001
 
        }
1002
 
 
1003
 
        if (pbAtr)
1004
 
        {
1005
 
                if (*pcbAtrLen > dwAtrLen)
1006
 
                        rv = SCARD_E_INSUFFICIENT_BUFFER;
1007
 
 
1008
 
                memcpy(pbAtr, (readerStates[i])->cardAtr,
1009
 
                        min(*pcbAtrLen, dwAtrLen));
1010
 
        }
1011
 
        
1012
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1013
 
 
1014
 
        return rv;
1015
 
}
1016
 
 
1017
 
LONG SCardGetStatusChange(SCARDCONTEXT hContext, DWORD dwTimeout,
1018
 
        LPSCARD_READERSTATE_A rgReaderStates, DWORD cReaders)
1019
 
{
1020
 
        PSCARD_READERSTATE_A currReader;
1021
 
        PREADER_STATE rContext;
1022
 
        DWORD dwTime = 0;
1023
 
        DWORD dwState;
1024
 
        DWORD dwBreakFlag = 0;
1025
 
        int j;
1026
 
        DWORD dwContextIndex;
1027
 
 
1028
 
        if (rgReaderStates == 0 && cReaders > 0)
1029
 
                return SCARD_E_INVALID_PARAMETER;
1030
 
 
1031
 
        if (cReaders < 0)
1032
 
                return SCARD_E_INVALID_VALUE;
1033
 
 
1034
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
1035
 
                return SCARD_E_NO_SERVICE;
1036
 
 
1037
 
        /*
1038
 
         * Make sure this context has been opened
1039
 
         */
1040
 
 
1041
 
        dwContextIndex = SCardGetContextIndice(hContext);
1042
 
        if (dwContextIndex == -1)
1043
 
                return SCARD_E_INVALID_HANDLE;
1044
 
 
1045
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
1046
 
 
1047
 
        /*
1048
 
         * Application is waiting for a reader - return the first available
1049
 
         * reader
1050
 
         */
1051
 
 
1052
 
        if (cReaders == 0)
1053
 
        {
1054
 
                while (1)
1055
 
                {
1056
 
                        int i;
1057
 
 
1058
 
                        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
1059
 
                        {
1060
 
                                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1061
 
                                return SCARD_E_NO_SERVICE;
1062
 
                        }
1063
 
 
1064
 
                        for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
1065
 
                        {
1066
 
                                if ((readerStates[i])->readerID != 0)
1067
 
                                {
1068
 
                                        /*
1069
 
                                         * Reader was found
1070
 
                                         */
1071
 
                                        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1072
 
                                        return SCARD_S_SUCCESS;
1073
 
                                }
1074
 
                        }
1075
 
 
1076
 
                        if (dwTimeout == 0)
1077
 
                        {
1078
 
                                /*
1079
 
                                 * return immediately - no reader available
1080
 
                                 */
1081
 
                                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1082
 
                                return SCARD_E_READER_UNAVAILABLE;
1083
 
                        }
1084
 
 
1085
 
                        SYS_USleep(PCSCLITE_STATUS_WAIT);
1086
 
 
1087
 
                        if (dwTimeout != INFINITE)
1088
 
                        {
1089
 
                                dwTime += PCSCLITE_STATUS_WAIT;
1090
 
 
1091
 
                                if (dwTime >= (dwTimeout * 1000))
1092
 
                                {
1093
 
                                        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1094
 
                                        return SCARD_E_TIMEOUT;
1095
 
                                }
1096
 
                        }
1097
 
                }
1098
 
        }
1099
 
        else
1100
 
                if (cReaders >= PCSCLITE_MAX_READERS_CONTEXTS)
1101
 
                {
1102
 
                        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1103
 
                        return SCARD_E_INVALID_VALUE;
1104
 
                }
1105
 
 
1106
 
        /*
1107
 
         * Check the integrity of the reader states structures
1108
 
         */
1109
 
 
1110
 
        for (j = 0; j < cReaders; j++)
1111
 
        {
1112
 
                currReader = &rgReaderStates[j];
1113
 
 
1114
 
                if (currReader->szReader == 0)
1115
 
                {
1116
 
                        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1117
 
                        return SCARD_E_INVALID_VALUE;
1118
 
                }
1119
 
        }
1120
 
 
1121
 
        /*
1122
 
         * End of search for readers
1123
 
         */
1124
 
 
1125
 
        /*
1126
 
         * Clear the event state for all readers
1127
 
         */
1128
 
        for (j = 0; j < cReaders; j++)
1129
 
        {
1130
 
                currReader = &rgReaderStates[j];
1131
 
                currReader->dwEventState = 0;
1132
 
        }
1133
 
 
1134
 
        /*
1135
 
         * Now is where we start our event checking loop
1136
 
         */
1137
 
 
1138
 
        Log1(PCSC_LOG_DEBUG, "Event Loop Start");
1139
 
 
1140
 
        psContextMap[dwContextIndex].contextBlockStatus = BLOCK_STATUS_BLOCKING;
1141
 
 
1142
 
        j = 0;
1143
 
 
1144
 
        do
1145
 
        {
1146
 
                if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
1147
 
                {
1148
 
                        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1149
 
                        return SCARD_E_NO_SERVICE;
1150
 
                }
1151
 
 
1152
 
                currReader = &rgReaderStates[j];
1153
 
 
1154
 
        /************ Look for IGNORED readers ****************************/
1155
 
 
1156
 
                if (currReader->dwCurrentState & SCARD_STATE_IGNORE)
1157
 
                {
1158
 
                        currReader->dwEventState = SCARD_STATE_IGNORE;
1159
 
                } else
1160
 
                {
1161
 
                        LPTSTR lpcReaderName;
1162
 
                        int i;
1163
 
 
1164
 
          /************ Looks for correct readernames *********************/
1165
 
 
1166
 
                        lpcReaderName = (char *) currReader->szReader;
1167
 
 
1168
 
                        for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
1169
 
                        {
1170
 
                                if (strcmp(lpcReaderName,
1171
 
                                                (readerStates[i])->readerName) == 0)
1172
 
                                {
1173
 
                                        break;
1174
 
                                }
1175
 
                        }
1176
 
 
1177
 
                        /*
1178
 
                         * The requested reader name is not recognized
1179
 
                         */
1180
 
                        if (i == PCSCLITE_MAX_READERS_CONTEXTS)
1181
 
                        {
1182
 
                                if (currReader->dwCurrentState & SCARD_STATE_UNKNOWN)
1183
 
                                {
1184
 
                                        currReader->dwEventState = SCARD_STATE_UNKNOWN;
1185
 
                                } else
1186
 
                                {
1187
 
                                        currReader->dwEventState =
1188
 
                                                SCARD_STATE_UNKNOWN | SCARD_STATE_CHANGED;
1189
 
                                        /*
1190
 
                                         * Spec says use SCARD_STATE_IGNORE but a removed USB
1191
 
                                         * reader with eventState fed into currentState will
1192
 
                                         * be ignored forever
1193
 
                                         */
1194
 
                                        dwBreakFlag = 1;
1195
 
                                }
1196
 
                        } else
1197
 
                        {
1198
 
 
1199
 
                                /*
1200
 
                                 * The reader has come back after being away
1201
 
                                 */
1202
 
                                if (currReader->dwCurrentState & SCARD_STATE_UNKNOWN)
1203
 
                                {
1204
 
                                        currReader->dwEventState |= SCARD_STATE_CHANGED;
1205
 
                                        currReader->dwEventState &= ~SCARD_STATE_UNKNOWN;
1206
 
                                        dwBreakFlag = 1;
1207
 
                                }
1208
 
 
1209
 
        /*****************************************************************/
1210
 
 
1211
 
                                /*
1212
 
                                 * Set the reader status structure
1213
 
                                 */
1214
 
                                rContext = readerStates[i];
1215
 
 
1216
 
                                /*
1217
 
                                 * Now we check all the Reader States
1218
 
                                 */
1219
 
                                dwState = rContext->readerState;
1220
 
 
1221
 
        /*********** Check if the reader is in the correct state ********/
1222
 
                                if (dwState & SCARD_UNKNOWN)
1223
 
                                {
1224
 
                                        /*
1225
 
                                         * App thinks reader is in bad state and it is
1226
 
                                         */
1227
 
                                        if (currReader->
1228
 
                                                dwCurrentState & SCARD_STATE_UNAVAILABLE)
1229
 
                                        {
1230
 
                                                currReader->dwEventState = SCARD_STATE_UNAVAILABLE;
1231
 
                                        } else
1232
 
                                        {
1233
 
                                                /*
1234
 
                                                 * App thinks reader is in good state and it is
1235
 
                                                 * not
1236
 
                                                 */
1237
 
                                                currReader->dwEventState = SCARD_STATE_CHANGED |
1238
 
                                                        SCARD_STATE_UNAVAILABLE;
1239
 
                                                dwBreakFlag = 1;
1240
 
                                        }
1241
 
                                } else
1242
 
                                {
1243
 
                                        /*
1244
 
                                         * App thinks reader in bad state but it is not
1245
 
                                         */
1246
 
                                        if (currReader->
1247
 
                                                dwCurrentState & SCARD_STATE_UNAVAILABLE)
1248
 
                                        {
1249
 
                                                currReader->dwEventState &=
1250
 
                                                        ~SCARD_STATE_UNAVAILABLE;
1251
 
                                                currReader->dwEventState |= SCARD_STATE_CHANGED;
1252
 
                                                dwBreakFlag = 1;
1253
 
                                        }
1254
 
                                }
1255
 
 
1256
 
        /********** Check for card presence in the reader **************/
1257
 
 
1258
 
                                if (dwState & SCARD_PRESENT)
1259
 
                                {
1260
 
                                        /* card present but not yet powered up */
1261
 
                                        if (0 == rContext->cardAtrLength)
1262
 
                                                /* Allow the status thread to convey information */
1263
 
                                                SYS_USleep(PCSCLITE_STATUS_POLL_RATE + 10);
1264
 
 
1265
 
                                        currReader->cbAtr = rContext->cardAtrLength;
1266
 
                                        memcpy(currReader->rgbAtr, rContext->cardAtr,
1267
 
                                                currReader->cbAtr);
1268
 
                                } else
1269
 
                                {
1270
 
                                        currReader->cbAtr = 0;
1271
 
                                }
1272
 
 
1273
 
                                /*
1274
 
                                 * Card is now absent
1275
 
                                 */
1276
 
                                if (dwState & SCARD_ABSENT)
1277
 
                                {
1278
 
                                        currReader->dwEventState |= SCARD_STATE_EMPTY;
1279
 
                                        currReader->dwEventState &= ~SCARD_STATE_PRESENT;
1280
 
                                        currReader->dwEventState &= ~SCARD_STATE_UNAWARE;
1281
 
                                        currReader->dwEventState &= ~SCARD_STATE_IGNORE;
1282
 
                                        currReader->dwEventState &= ~SCARD_STATE_UNKNOWN;
1283
 
                                        currReader->dwEventState &= ~SCARD_STATE_UNAVAILABLE;
1284
 
                                        currReader->dwEventState &= ~SCARD_STATE_ATRMATCH;
1285
 
                                        currReader->dwEventState &= ~SCARD_STATE_MUTE;
1286
 
                                        currReader->dwEventState &= ~SCARD_STATE_INUSE;
1287
 
 
1288
 
                                        /*
1289
 
                                         * After present the rest are assumed
1290
 
                                         */
1291
 
                                        if (currReader->dwCurrentState & SCARD_STATE_PRESENT ||
1292
 
                                                currReader->dwCurrentState & SCARD_STATE_ATRMATCH
1293
 
                                                || currReader->
1294
 
                                                dwCurrentState & SCARD_STATE_EXCLUSIVE
1295
 
                                                || currReader->dwCurrentState & SCARD_STATE_INUSE)
1296
 
                                        {
1297
 
                                                currReader->dwEventState |= SCARD_STATE_CHANGED;
1298
 
                                                dwBreakFlag = 1;
1299
 
                                        }
1300
 
 
1301
 
                                        /*
1302
 
                                         * Card is now present
1303
 
                                         */
1304
 
                                } else if (dwState & SCARD_PRESENT)
1305
 
                                {
1306
 
                                        currReader->dwEventState |= SCARD_STATE_PRESENT;
1307
 
                                        currReader->dwEventState &= ~SCARD_STATE_EMPTY;
1308
 
                                        currReader->dwEventState &= ~SCARD_STATE_UNAWARE;
1309
 
                                        currReader->dwEventState &= ~SCARD_STATE_IGNORE;
1310
 
                                        currReader->dwEventState &= ~SCARD_STATE_UNKNOWN;
1311
 
                                        currReader->dwEventState &= ~SCARD_STATE_UNAVAILABLE;
1312
 
                                        currReader->dwEventState &= ~SCARD_STATE_MUTE;
1313
 
 
1314
 
                                        if (currReader->dwCurrentState & SCARD_STATE_EMPTY)
1315
 
                                        {
1316
 
                                                currReader->dwEventState |= SCARD_STATE_CHANGED;
1317
 
                                                dwBreakFlag = 1;
1318
 
                                        }
1319
 
 
1320
 
                                        if (dwState & SCARD_SWALLOWED)
1321
 
                                        {
1322
 
                                                if (currReader->dwCurrentState & SCARD_STATE_MUTE)
1323
 
                                                {
1324
 
                                                        currReader->dwEventState |= SCARD_STATE_MUTE;
1325
 
                                                } else
1326
 
                                                {
1327
 
                                                        currReader->dwEventState |= SCARD_STATE_MUTE;
1328
 
                                                        if (currReader->dwCurrentState !=
1329
 
                                                                SCARD_STATE_UNAWARE)
1330
 
                                                        {
1331
 
                                                                currReader->dwEventState |=
1332
 
                                                                        SCARD_STATE_CHANGED;
1333
 
                                                        }
1334
 
                                                        dwBreakFlag = 1;
1335
 
                                                }
1336
 
                                        } else
1337
 
                                        {
1338
 
                                                /*
1339
 
                                                 * App thinks card is mute but it is not
1340
 
                                                 */
1341
 
                                                if (currReader->dwCurrentState & SCARD_STATE_MUTE)
1342
 
                                                {
1343
 
                                                        currReader->dwEventState |=
1344
 
                                                                SCARD_STATE_CHANGED;
1345
 
                                                        dwBreakFlag = 1;
1346
 
                                                }
1347
 
                                        }
1348
 
                                }
1349
 
 
1350
 
                                /*
1351
 
                                 * Now figure out sharing modes
1352
 
                                 */
1353
 
                                if (rContext->readerSharing == -1)
1354
 
                                {
1355
 
                                        currReader->dwEventState |= SCARD_STATE_EXCLUSIVE;
1356
 
                                        currReader->dwEventState &= ~SCARD_STATE_INUSE;
1357
 
                                        if (currReader->dwCurrentState & SCARD_STATE_INUSE)
1358
 
                                        {
1359
 
                                                currReader->dwEventState |= SCARD_STATE_CHANGED;
1360
 
                                                dwBreakFlag = 1;
1361
 
                                        }
1362
 
                                } else if (rContext->readerSharing >= 1)
1363
 
                                {
1364
 
                                        /*
1365
 
                                         * A card must be inserted for it to be INUSE
1366
 
                                         */
1367
 
                                        if (dwState & SCARD_PRESENT)
1368
 
                                        {
1369
 
                                                currReader->dwEventState |= SCARD_STATE_INUSE;
1370
 
                                                currReader->dwEventState &= ~SCARD_STATE_EXCLUSIVE;
1371
 
                                                if (currReader->
1372
 
                                                        dwCurrentState & SCARD_STATE_EXCLUSIVE)
1373
 
                                                {
1374
 
                                                        currReader->dwEventState |=
1375
 
                                                                SCARD_STATE_CHANGED;
1376
 
                                                        dwBreakFlag = 1;
1377
 
                                                }
1378
 
                                        }
1379
 
                                } else if (rContext->readerSharing == 0)
1380
 
                                {
1381
 
                                        currReader->dwEventState &= ~SCARD_STATE_INUSE;
1382
 
                                        currReader->dwEventState &= ~SCARD_STATE_EXCLUSIVE;
1383
 
 
1384
 
                                        if (currReader->dwCurrentState & SCARD_STATE_INUSE)
1385
 
                                        {
1386
 
                                                currReader->dwEventState |= SCARD_STATE_CHANGED;
1387
 
                                                dwBreakFlag = 1;
1388
 
                                        } else if (currReader->
1389
 
                                                dwCurrentState & SCARD_STATE_EXCLUSIVE)
1390
 
                                        {
1391
 
                                                currReader->dwEventState |= SCARD_STATE_CHANGED;
1392
 
                                                dwBreakFlag = 1;
1393
 
                                        }
1394
 
                                }
1395
 
 
1396
 
                                if (currReader->dwCurrentState == SCARD_STATE_UNAWARE)
1397
 
                                {
1398
 
                                        /*
1399
 
                                         * Break out of the while .. loop and return status
1400
 
                                         * once all the status's for all readers is met
1401
 
                                         */
1402
 
                                        currReader->dwEventState |= SCARD_STATE_CHANGED;
1403
 
                                        dwBreakFlag = 1;
1404
 
                                }
1405
 
 
1406
 
                        }       /* End of SCARD_STATE_UNKNOWN */
1407
 
 
1408
 
                }       /* End of SCARD_STATE_IGNORE */
1409
 
 
1410
 
                /*
1411
 
                 * Counter and resetter
1412
 
                 */
1413
 
                j = j + 1;
1414
 
                if (j == cReaders)
1415
 
                        j = 0;
1416
 
 
1417
 
                /*
1418
 
                 * Declare all the break conditions
1419
 
                 */
1420
 
 
1421
 
                if (psContextMap[dwContextIndex].contextBlockStatus ==
1422
 
                        BLOCK_STATUS_RESUME)
1423
 
                        break;
1424
 
 
1425
 
                /*
1426
 
                 * Break if UNAWARE is set and all readers have been checked
1427
 
                 */
1428
 
                if ((dwBreakFlag == 1) && (j == 0))
1429
 
                        break;
1430
 
 
1431
 
                /*
1432
 
                 * Timeout has occurred and all readers checked
1433
 
                 */
1434
 
                if ((dwTimeout == 0) && (j == 0))
1435
 
                        break;
1436
 
 
1437
 
                if (dwTimeout != INFINITE && dwTimeout != 0)
1438
 
                {
1439
 
                        /*
1440
 
                         * If time is greater than timeout and all readers have been
1441
 
                         * checked
1442
 
                         */
1443
 
                        if ((dwTime >= (dwTimeout * 1000)) && (j == 0))
1444
 
                        {
1445
 
                                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1446
 
                                return SCARD_E_TIMEOUT;
1447
 
                        }
1448
 
                }
1449
 
 
1450
 
                /*
1451
 
                 * Only sleep once for each cycle of reader checks.
1452
 
                 */
1453
 
                if (j == 0)
1454
 
                {
1455
 
                        SYS_USleep(PCSCLITE_STATUS_WAIT);
1456
 
                        dwTime += PCSCLITE_STATUS_WAIT;
1457
 
                }
1458
 
        }
1459
 
        while (1);
1460
 
 
1461
 
        Log1(PCSC_LOG_DEBUG, "Event Loop End");
1462
 
 
1463
 
        if (psContextMap[dwContextIndex].contextBlockStatus ==
1464
 
                        BLOCK_STATUS_RESUME)
1465
 
        {
1466
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1467
 
                return SCARD_E_CANCELLED;
1468
 
        }
1469
 
 
1470
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1471
 
 
1472
 
        return SCARD_S_SUCCESS;
1473
 
}
1474
 
 
1475
 
LONG SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer,
1476
 
        DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength,
1477
 
        LPDWORD lpBytesReturned)
1478
 
{
1479
 
        LONG rv;
1480
 
        control_struct scControlStruct;
1481
 
        sharedSegmentMsg msgStruct;
1482
 
        int i;
1483
 
        DWORD dwContextIndex, dwChannelIndex;
1484
 
 
1485
 
        /* 0 bytes received by default */
1486
 
        if (NULL != lpBytesReturned)
1487
 
                *lpBytesReturned = 0;
1488
 
 
1489
 
        if (pbSendBuffer == 0)
1490
 
                return SCARD_E_INVALID_PARAMETER;
1491
 
 
1492
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
1493
 
                return SCARD_E_NO_SERVICE;
1494
 
 
1495
 
        /*
1496
 
         * Make sure this handle has been opened
1497
 
         */
1498
 
        rv = SCardGetIndicesFromHandle(hCard, &dwContextIndex, &dwChannelIndex);
1499
 
 
1500
 
        if (rv == -1)
1501
 
                return SCARD_E_INVALID_HANDLE;
1502
 
 
1503
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
1504
 
 
1505
 
        for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
1506
 
        {
1507
 
                char *r = psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName;
1508
 
 
1509
 
                /* by default r == NULL */
1510
 
                if (r && strcmp(r, (readerStates[i])->readerName) == 0)
1511
 
                        break;
1512
 
        }
1513
 
 
1514
 
        if (i == PCSCLITE_MAX_READERS_CONTEXTS)
1515
 
        {
1516
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1517
 
                return SCARD_E_READER_UNAVAILABLE;
1518
 
        }
1519
 
 
1520
 
        if (cbSendLength > MAX_BUFFER_SIZE)
1521
 
        {
1522
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1523
 
                return SCARD_E_INSUFFICIENT_BUFFER;
1524
 
        }
1525
 
 
1526
 
        scControlStruct.hCard = hCard;
1527
 
        scControlStruct.dwControlCode = dwControlCode;
1528
 
        scControlStruct.cbSendLength = cbSendLength;
1529
 
        scControlStruct.cbRecvLength = cbRecvLength;
1530
 
        memcpy(scControlStruct.pbSendBuffer, pbSendBuffer, cbSendLength);
1531
 
 
1532
 
        rv = WrapSHMWrite(SCARD_CONTROL, psContextMap[dwContextIndex].dwClientID,
1533
 
                sizeof(scControlStruct), PCSCLITE_CLIENT_ATTEMPTS, &scControlStruct);
1534
 
 
1535
 
        if (rv == -1)
1536
 
        {
1537
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1538
 
                return SCARD_E_NO_SERVICE;
1539
 
        }
1540
 
 
1541
 
        /*
1542
 
         * Read a message from the server
1543
 
         */
1544
 
        rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);
1545
 
 
1546
 
        if (rv == -1)
1547
 
        {
1548
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1549
 
                return SCARD_F_COMM_ERROR;
1550
 
        }
1551
 
 
1552
 
        memcpy(&scControlStruct, &msgStruct.data, sizeof(scControlStruct));
1553
 
 
1554
 
        if (NULL != lpBytesReturned)
1555
 
                *lpBytesReturned = scControlStruct.dwBytesReturned;
1556
 
 
1557
 
        if (scControlStruct.rv == SCARD_S_SUCCESS)
1558
 
        {
1559
 
                /*
1560
 
                 * Copy and zero it so any secret information is not leaked
1561
 
                 */
1562
 
                memcpy(pbRecvBuffer, scControlStruct.pbRecvBuffer,
1563
 
                        scControlStruct.cbRecvLength);
1564
 
                memset(scControlStruct.pbRecvBuffer, 0x00,
1565
 
                        sizeof(scControlStruct.pbRecvBuffer));
1566
 
        }
1567
 
 
1568
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1569
 
                
1570
 
        return scControlStruct.rv;
1571
 
}
1572
 
 
1573
 
LONG SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr,
1574
 
        LPDWORD pcbAttrLen)
1575
 
{
1576
 
        if (NULL == pcbAttrLen)
1577
 
                return SCARD_E_INVALID_PARAMETER;
1578
 
 
1579
 
        /* if only get the length */
1580
 
        if (NULL == pbAttr)
1581
 
                /* this variable may not be set by the caller. use a reasonable size */
1582
 
                *pcbAttrLen = MAX_BUFFER_SIZE;
1583
 
 
1584
 
        return SCardGetSetAttrib(hCard, SCARD_GET_ATTRIB, dwAttrId, pbAttr,
1585
 
                pcbAttrLen);
1586
 
}
1587
 
 
1588
 
LONG SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr,
1589
 
        DWORD cbAttrLen)
1590
 
{
1591
 
        if (NULL == pbAttr || 0 == cbAttrLen)
1592
 
                return SCARD_E_INVALID_PARAMETER;
1593
 
 
1594
 
        return SCardGetSetAttrib(hCard, SCARD_SET_ATTRIB, dwAttrId, (LPBYTE)pbAttr,
1595
 
                &cbAttrLen);
1596
 
}
1597
 
 
1598
 
static LONG SCardGetSetAttrib(SCARDHANDLE hCard, int command, DWORD dwAttrId,
1599
 
        LPBYTE pbAttr, LPDWORD pcbAttrLen)
1600
 
{
1601
 
        LONG rv;
1602
 
        getset_struct scGetSetStruct;
1603
 
        sharedSegmentMsg msgStruct;
1604
 
        int i;
1605
 
        DWORD dwContextIndex, dwChannelIndex;
1606
 
 
1607
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
1608
 
                return SCARD_E_NO_SERVICE;
1609
 
 
1610
 
        /*
1611
 
         * Make sure this handle has been opened
1612
 
         */
1613
 
        rv = SCardGetIndicesFromHandle(hCard, &dwContextIndex, &dwChannelIndex);
1614
 
 
1615
 
        if (rv == -1)
1616
 
                return SCARD_E_INVALID_HANDLE;
1617
 
 
1618
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
1619
 
 
1620
 
        for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
1621
 
        {
1622
 
                char *r = psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName;
1623
 
 
1624
 
                /* by default r == NULL */
1625
 
                if (r && strcmp(r, (readerStates[i])->readerName) == 0)
1626
 
                        break;
1627
 
        }
1628
 
 
1629
 
        if (i == PCSCLITE_MAX_READERS_CONTEXTS)
1630
 
        {
1631
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1632
 
                return SCARD_E_READER_UNAVAILABLE;
1633
 
        }
1634
 
 
1635
 
        if (*pcbAttrLen > MAX_BUFFER_SIZE)
1636
 
        {
1637
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1638
 
                return SCARD_E_INSUFFICIENT_BUFFER;
1639
 
        }
1640
 
 
1641
 
        scGetSetStruct.hCard = hCard;
1642
 
        scGetSetStruct.dwAttrId = dwAttrId;
1643
 
        scGetSetStruct.cbAttrLen = *pcbAttrLen;
1644
 
        scGetSetStruct.rv = SCARD_E_NO_SERVICE;
1645
 
        if (SCARD_SET_ATTRIB == command)
1646
 
                memcpy(scGetSetStruct.pbAttr, pbAttr, *pcbAttrLen);
1647
 
 
1648
 
        rv = WrapSHMWrite(command,
1649
 
                psContextMap[dwContextIndex].dwClientID, sizeof(scGetSetStruct),
1650
 
                PCSCLITE_CLIENT_ATTEMPTS, &scGetSetStruct);
1651
 
 
1652
 
        if (rv == -1)
1653
 
        {
1654
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1655
 
                return SCARD_E_NO_SERVICE;
1656
 
        }
1657
 
 
1658
 
        /*
1659
 
         * Read a message from the server
1660
 
         */
1661
 
        rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);
1662
 
 
1663
 
        if (rv == -1)
1664
 
        {
1665
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1666
 
                return SCARD_F_COMM_ERROR;
1667
 
        }
1668
 
 
1669
 
        memcpy(&scGetSetStruct, &msgStruct.data, sizeof(scGetSetStruct));
1670
 
 
1671
 
        if ((SCARD_S_SUCCESS == scGetSetStruct.rv) && (SCARD_GET_ATTRIB == command))
1672
 
        {
1673
 
                /*
1674
 
                 * Copy and zero it so any secret information is not leaked
1675
 
                 */
1676
 
                if (*pcbAttrLen < scGetSetStruct.cbAttrLen)
1677
 
                {
1678
 
                        scGetSetStruct.cbAttrLen = *pcbAttrLen;
1679
 
                        scGetSetStruct.rv = SCARD_E_INSUFFICIENT_BUFFER;
1680
 
                }
1681
 
                else
1682
 
                        *pcbAttrLen = scGetSetStruct.cbAttrLen;
1683
 
 
1684
 
                if (pbAttr)
1685
 
                        memcpy(pbAttr, scGetSetStruct.pbAttr, scGetSetStruct.cbAttrLen);
1686
 
 
1687
 
                memset(scGetSetStruct.pbAttr, 0x00, sizeof(scGetSetStruct.pbAttr));
1688
 
        }
1689
 
 
1690
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1691
 
 
1692
 
        return scGetSetStruct.rv;
1693
 
}
1694
 
 
1695
 
LONG SCardTransmit(SCARDHANDLE hCard, LPCSCARD_IO_REQUEST pioSendPci,
1696
 
        LPCBYTE pbSendBuffer, DWORD cbSendLength,
1697
 
        LPSCARD_IO_REQUEST pioRecvPci, LPBYTE pbRecvBuffer,
1698
 
        LPDWORD pcbRecvLength)
1699
 
{
1700
 
        LONG rv;
1701
 
        transmit_struct scTransmitStruct;
1702
 
        sharedSegmentMsg msgStruct;
1703
 
        int i;
1704
 
        DWORD dwContextIndex, dwChannelIndex;
1705
 
 
1706
 
        if (pbSendBuffer == 0 || pbRecvBuffer == 0 ||
1707
 
                        pcbRecvLength == 0 || pioSendPci == 0)
1708
 
                return SCARD_E_INVALID_PARAMETER;
1709
 
 
1710
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
1711
 
                return SCARD_E_NO_SERVICE;
1712
 
 
1713
 
        /*
1714
 
         * Make sure this handle has been opened
1715
 
         */
1716
 
        rv = SCardGetIndicesFromHandle(hCard, &dwContextIndex, &dwChannelIndex);
1717
 
 
1718
 
        if (rv == -1)
1719
 
        {
1720
 
                *pcbRecvLength = 0;
1721
 
                return SCARD_E_INVALID_HANDLE;
1722
 
        }
1723
 
 
1724
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
1725
 
 
1726
 
        for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
1727
 
        {
1728
 
                char *r = psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName;
1729
 
 
1730
 
                /* by default r == NULL */
1731
 
                if (r && strcmp(r, (readerStates[i])->readerName) == 0)
1732
 
                        break;
1733
 
        }
1734
 
 
1735
 
        if (i == PCSCLITE_MAX_READERS_CONTEXTS)
1736
 
        {
1737
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1738
 
                return SCARD_E_READER_UNAVAILABLE;
1739
 
        }
1740
 
 
1741
 
        if (cbSendLength > MAX_BUFFER_SIZE)
1742
 
        {
1743
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1744
 
                return SCARD_E_INSUFFICIENT_BUFFER;
1745
 
        }
1746
 
 
1747
 
        scTransmitStruct.hCard = hCard;
1748
 
        scTransmitStruct.cbSendLength = cbSendLength;
1749
 
        scTransmitStruct.pcbRecvLength = *pcbRecvLength;
1750
 
        memcpy(&scTransmitStruct.pioSendPci, pioSendPci,
1751
 
                sizeof(SCARD_IO_REQUEST));
1752
 
        memcpy(scTransmitStruct.pbSendBuffer, pbSendBuffer, cbSendLength);
1753
 
 
1754
 
        if (pioRecvPci)
1755
 
        {
1756
 
                memcpy(&scTransmitStruct.pioRecvPci, pioRecvPci,
1757
 
                        sizeof(SCARD_IO_REQUEST));
1758
 
        }
1759
 
        else
1760
 
                scTransmitStruct.pioRecvPci.dwProtocol = SCARD_PROTOCOL_ANY;
1761
 
 
1762
 
        rv = WrapSHMWrite(SCARD_TRANSMIT, psContextMap[dwContextIndex].dwClientID,
1763
 
                sizeof(scTransmitStruct),
1764
 
                PCSCLITE_CLIENT_ATTEMPTS, (void *) &scTransmitStruct);
1765
 
 
1766
 
        if (rv == -1)
1767
 
        {
1768
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1769
 
                return SCARD_E_NO_SERVICE;
1770
 
        }
1771
 
 
1772
 
        /*
1773
 
         * Read a message from the server
1774
 
         */
1775
 
        rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);
1776
 
 
1777
 
        memcpy(&scTransmitStruct, &msgStruct.data, sizeof(scTransmitStruct));
1778
 
 
1779
 
        if (rv == -1)
1780
 
        {
1781
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1782
 
                return SCARD_F_COMM_ERROR;
1783
 
        }
1784
 
 
1785
 
        /*
1786
 
         * Zero it and free it so any secret information cannot be leaked
1787
 
         */
1788
 
        memset(scTransmitStruct.pbSendBuffer, 0x00, cbSendLength);
1789
 
 
1790
 
        if (scTransmitStruct.rv == SCARD_S_SUCCESS)
1791
 
        {
1792
 
                *pcbRecvLength = scTransmitStruct.pcbRecvLength;
1793
 
 
1794
 
                /*
1795
 
                 * Copy and zero it so any secret information is not leaked
1796
 
                 */
1797
 
                memcpy(pbRecvBuffer, scTransmitStruct.pbRecvBuffer,
1798
 
                        scTransmitStruct.pcbRecvLength);
1799
 
                memset(scTransmitStruct.pbRecvBuffer, 0x00,
1800
 
                        scTransmitStruct.pcbRecvLength);
1801
 
 
1802
 
                if (pioRecvPci)
1803
 
                {
1804
 
                        memcpy(pioRecvPci, &scTransmitStruct.pioRecvPci,
1805
 
                                sizeof(SCARD_IO_REQUEST));
1806
 
                }
1807
 
 
1808
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1809
 
                
1810
 
                return scTransmitStruct.rv;
1811
 
        } else
1812
 
        {
1813
 
                *pcbRecvLength = scTransmitStruct.pcbRecvLength;
1814
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1815
 
                return scTransmitStruct.rv;
1816
 
        }
1817
 
}
1818
 
 
1819
 
LONG SCardListReaders(SCARDCONTEXT hContext, LPCTSTR mszGroups,
1820
 
        LPTSTR mszReaders, LPDWORD pcchReaders)
1821
 
{
1822
 
        DWORD dwReadersLen;
1823
 
        int i, lastChrPtr;
1824
 
        DWORD dwContextIndex;
1825
 
 
1826
 
        /*
1827
 
         * Check for NULL parameters
1828
 
         */
1829
 
        if (pcchReaders == NULL)
1830
 
                return SCARD_E_INVALID_PARAMETER;
1831
 
 
1832
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
1833
 
                return SCARD_E_NO_SERVICE;
1834
 
 
1835
 
        /*
1836
 
         * Make sure this context has been opened
1837
 
         */
1838
 
        dwContextIndex = SCardGetContextIndice(hContext);
1839
 
        if (dwContextIndex == -1)
1840
 
                return SCARD_E_INVALID_HANDLE;
1841
 
 
1842
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
1843
 
 
1844
 
        dwReadersLen = 0;
1845
 
        for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
1846
 
                if ((readerStates[i])->readerID != 0)
1847
 
                        dwReadersLen += strlen((readerStates[i])->readerName) + 1;
1848
 
 
1849
 
        /* for the last NULL byte */
1850
 
        dwReadersLen += 1;
1851
 
 
1852
 
        if ((mszReaders == NULL)        /* text array not allocated */
1853
 
                || (*pcchReaders == 0)) /* size == 0 */
1854
 
        {
1855
 
                *pcchReaders = dwReadersLen;
1856
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1857
 
                return SCARD_S_SUCCESS;
1858
 
        }
1859
 
        
1860
 
        if (*pcchReaders < dwReadersLen)
1861
 
        {
1862
 
                *pcchReaders = dwReadersLen;
1863
 
                SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1864
 
                return SCARD_E_INSUFFICIENT_BUFFER;
1865
 
        }
1866
 
 
1867
 
        lastChrPtr = 0;
1868
 
        for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
1869
 
        {
1870
 
                if ((readerStates[i])->readerID != 0)
1871
 
                {
1872
 
                        /*
1873
 
                         * Build the multi-string
1874
 
                         */
1875
 
                        strcpy(&mszReaders[lastChrPtr], (readerStates[i])->readerName);
1876
 
                        lastChrPtr += strlen((readerStates[i])->readerName)+1;
1877
 
                }
1878
 
        }
1879
 
        mszReaders[lastChrPtr] = '\0';  /* Add the last null */
1880
 
 
1881
 
        *pcchReaders = dwReadersLen;
1882
 
 
1883
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1884
 
        return SCARD_S_SUCCESS;
1885
 
}
1886
 
 
1887
 
LONG SCardListReaderGroups(SCARDCONTEXT hContext, LPTSTR mszGroups,
1888
 
        LPDWORD pcchGroups)
1889
 
{
1890
 
        LONG rv = SCARD_S_SUCCESS;
1891
 
        DWORD dwContextIndex;
1892
 
 
1893
 
        const char ReaderGroup[] = "SCard$DefaultReaders";
1894
 
        const int dwGroups = strlen(ReaderGroup) + 2;
1895
 
 
1896
 
        if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
1897
 
                return SCARD_E_NO_SERVICE;
1898
 
 
1899
 
        /*
1900
 
         * Make sure this context has been opened
1901
 
         */
1902
 
        dwContextIndex = SCardGetContextIndice(hContext);
1903
 
        if (dwContextIndex == -1)
1904
 
                return SCARD_E_INVALID_HANDLE;
1905
 
 
1906
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
1907
 
 
1908
 
        if (mszGroups)
1909
 
        {
1910
 
 
1911
 
                if (*pcchGroups < dwGroups)
1912
 
                        rv = SCARD_E_INSUFFICIENT_BUFFER;
1913
 
                else
1914
 
                {
1915
 
                        memset(mszGroups, 0, dwGroups);
1916
 
                        memcpy(mszGroups, ReaderGroup, strlen(ReaderGroup));
1917
 
                }
1918
 
        }
1919
 
 
1920
 
        *pcchGroups = dwGroups;
1921
 
 
1922
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);   
1923
 
        return rv;
1924
 
}
1925
 
 
1926
 
LONG SCardCancel(SCARDCONTEXT hContext)
1927
 
{
1928
 
        DWORD dwContextIndex;
1929
 
 
1930
 
        dwContextIndex = SCardGetContextIndice(hContext);
1931
 
 
1932
 
        if (dwContextIndex == -1)
1933
 
                return SCARD_E_INVALID_HANDLE;
1934
 
 
1935
 
        SYS_MutexLock(psContextMap[dwContextIndex].mMutex);     
1936
 
 
1937
 
        /*
1938
 
         * Set the block status for this Context so blocking calls will
1939
 
         * complete
1940
 
         */
1941
 
        psContextMap[dwContextIndex].contextBlockStatus = BLOCK_STATUS_RESUME;
1942
 
 
1943
 
        SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);
1944
 
 
1945
 
        return SCARD_S_SUCCESS;
1946
 
}
1947
 
 
1948
 
/*
1949
 
 * Functions for managing instances of SCardEstablishContext These
1950
 
 * functions keep track of Context handles and associate the blocking
1951
 
 * variable contextBlockStatus to an hContext
1952
 
 */
1953
 
 
1954
 
static LONG SCardAddContext(SCARDCONTEXT hContext, DWORD dwClientID)
1955
 
{
1956
 
        int i;
1957
 
 
1958
 
        for (i = 0; i < PCSCLITE_MAX_APPLICATION_CONTEXTS; i++)
1959
 
        {
1960
 
                if (psContextMap[i].hContext == 0)
1961
 
                {
1962
 
                        psContextMap[i].hContext = hContext;
1963
 
                        psContextMap[i].TID = SYS_ThreadSelf();
1964
 
                        psContextMap[i].dwClientID = dwClientID;
1965
 
                        psContextMap[i].contextBlockStatus = BLOCK_STATUS_RESUME;
1966
 
                        psContextMap[i].mMutex = (PCSCLITE_MUTEX_T) malloc(sizeof(PCSCLITE_MUTEX));
1967
 
                        SYS_MutexInit(psContextMap[i].mMutex);
1968
 
                        return SCARD_S_SUCCESS;
1969
 
                }
1970
 
        }
1971
 
 
1972
 
        return SCARD_E_NO_MEMORY;
1973
 
}
1974
 
 
1975
 
static LONG SCardGetContextIndice(SCARDCONTEXT hContext)
1976
 
{
1977
 
        LONG rv;
1978
 
 
1979
 
        SCardLockThread();
1980
 
        rv = SCardGetContextIndiceTH(hContext);
1981
 
        SCardUnlockThread();
1982
 
 
1983
 
        return rv;
1984
 
}
1985
 
 
1986
 
 
1987
 
static LONG SCardGetContextIndiceTH(SCARDCONTEXT hContext)
1988
 
{
1989
 
        int i;
1990
 
 
1991
 
        /*
1992
 
         * Find this context and return it's spot in the array
1993
 
         */
1994
 
        for (i = 0; i < PCSCLITE_MAX_APPLICATION_CONTEXTS; i++)
1995
 
        {
1996
 
                if ((hContext == psContextMap[i].hContext) && (hContext != 0))
1997
 
                        return i;
1998
 
        }
1999
 
 
2000
 
        return -1;
2001
 
}
2002
 
 
2003
 
static LONG SCardRemoveContext(SCARDCONTEXT hContext)
2004
 
{
2005
 
        LONG  retIndice;
2006
 
 
2007
 
        retIndice = SCardGetContextIndiceTH(hContext);
2008
 
 
2009
 
        if (retIndice == -1)
2010
 
                return SCARD_E_INVALID_HANDLE;
2011
 
        else
2012
 
        {
2013
 
                int i;
2014
 
 
2015
 
                psContextMap[retIndice].hContext = 0;
2016
 
                SHMClientCloseSession(psContextMap[retIndice].dwClientID);
2017
 
                psContextMap[retIndice].dwClientID = 0;
2018
 
                free(psContextMap[retIndice].mMutex);
2019
 
                psContextMap[retIndice].mMutex = 0;
2020
 
                psContextMap[retIndice].contextBlockStatus = BLOCK_STATUS_RESUME;
2021
 
 
2022
 
                for (i = 0; i < PCSCLITE_MAX_APPLICATION_CONTEXT_CHANNELS; i++)
2023
 
                {
2024
 
                        /*
2025
 
                         * Initially set the hcard structs to zero
2026
 
                         */
2027
 
                        psContextMap[retIndice].psChannelMap[i].hCard = 0;
2028
 
                        free(psContextMap[retIndice].psChannelMap[i].readerName);
2029
 
                        psContextMap[retIndice].psChannelMap[i].readerName = NULL;
2030
 
                }
2031
 
 
2032
 
                return SCARD_S_SUCCESS;
2033
 
        }
2034
 
}
2035
 
 
2036
 
/*
2037
 
 * Functions for managing hCard values returned from SCardConnect.
2038
 
 */
2039
 
 
2040
 
static LONG SCardAddHandle(SCARDHANDLE hCard, DWORD dwContextIndex,
2041
 
        LPTSTR readerName)
2042
 
{
2043
 
        int i;
2044
 
 
2045
 
        for (i = 0; i < PCSCLITE_MAX_APPLICATION_CONTEXT_CHANNELS; i++)
2046
 
        {
2047
 
                if (psContextMap[dwContextIndex].psChannelMap[i].hCard == 0)
2048
 
                {
2049
 
                        psContextMap[dwContextIndex].psChannelMap[i].hCard = hCard;
2050
 
                        psContextMap[dwContextIndex].psChannelMap[i].readerName = strdup(readerName);
2051
 
                        return SCARD_S_SUCCESS;
2052
 
                }
2053
 
        }
2054
 
 
2055
 
        return SCARD_E_NO_MEMORY;
2056
 
}
2057
 
 
2058
 
static LONG SCardRemoveHandle(SCARDHANDLE hCard)
2059
 
{
2060
 
        DWORD dwContextIndice, dwChannelIndice;
2061
 
        LONG rv;
2062
 
 
2063
 
        rv = SCardGetIndicesFromHandle(hCard, &dwContextIndice, &dwChannelIndice);
2064
 
 
2065
 
        if (rv == -1)
2066
 
                return SCARD_E_INVALID_HANDLE;
2067
 
        else
2068
 
        {
2069
 
                psContextMap[dwContextIndice].psChannelMap[dwChannelIndice].hCard = 0;
2070
 
                free(psContextMap[dwContextIndice].psChannelMap[dwChannelIndice].readerName);
2071
 
                psContextMap[dwContextIndice].psChannelMap[dwChannelIndice].readerName = NULL;
2072
 
                return SCARD_S_SUCCESS;
2073
 
        }
2074
 
}
2075
 
 
2076
 
static LONG SCardGetIndicesFromHandle(SCARDHANDLE hCard, PDWORD pdwContextIndice, PDWORD pdwChannelIndice)
2077
 
{
2078
 
        LONG rv;
2079
 
 
2080
 
        SCardLockThread();
2081
 
        rv = SCardGetIndicesFromHandleTH(hCard, pdwContextIndice, pdwChannelIndice);
2082
 
        SCardUnlockThread();
2083
 
 
2084
 
        return rv;
2085
 
}
2086
 
 
2087
 
static LONG SCardGetIndicesFromHandleTH(SCARDHANDLE hCard, PDWORD pdwContextIndice, PDWORD pdwChannelIndice)
2088
 
{
2089
 
        int i;
2090
 
 
2091
 
        for (i = 0; i < PCSCLITE_MAX_APPLICATION_CONTEXTS; i++)
2092
 
        {
2093
 
                if (psContextMap[i].hContext != 0)
2094
 
                {
2095
 
                        int j;
2096
 
 
2097
 
                        for (j = 0; j < PCSCLITE_MAX_APPLICATION_CONTEXT_CHANNELS; j++)
2098
 
                        {
2099
 
                                if (psContextMap[i].psChannelMap[j].hCard == hCard)
2100
 
                                {
2101
 
                                        *pdwContextIndice = i;
2102
 
                                        *pdwChannelIndice = j;
2103
 
                                        return SCARD_S_SUCCESS;
2104
 
                                }
2105
 
                        }
2106
 
                        
2107
 
                }
2108
 
        }
2109
 
 
2110
 
        return -1;
2111
 
}
2112
 
 
2113
 
/*
2114
 
 * This function locks a mutex so another thread must wait to use this
2115
 
 * function
2116
 
 */
2117
 
 
2118
 
inline static LONG SCardLockThread(void)
2119
 
{
2120
 
        return SYS_MutexLock(&clientMutex);
2121
 
}
2122
 
 
2123
 
/*
2124
 
 * This function unlocks a mutex so another thread may use the client
2125
 
 * library
2126
 
 */
2127
 
 
2128
 
inline static LONG SCardUnlockThread(void)
2129
 
{
2130
 
        return SYS_MutexUnLock(&clientMutex);
2131
 
}
2132
 
 
2133
 
static LONG SCardCheckDaemonAvailability(void)
2134
 
{
2135
 
        LONG rv;
2136
 
        struct stat statBuffer;
2137
 
 
2138
 
        rv = SYS_Stat(PCSCLITE_IPC_DIR, &statBuffer);
2139
 
 
2140
 
        if (rv != 0)
2141
 
        {
2142
 
                Log1(PCSC_LOG_ERROR, "PCSC Not Running");
2143
 
                return SCARD_E_NO_SERVICE;
2144
 
        }
2145
 
 
2146
 
        return SCARD_S_SUCCESS;
2147
 
}
2148
 
 
2149
 
/*
2150
 
 * free resources allocated by the library
2151
 
 * You _shall_ call this function if you use dlopen/dlclose to load/unload the
2152
 
 * library. Otherwise you will exhaust the ressources available.
2153
 
 */
2154
 
void SCardUnload(void)
2155
 
{
2156
 
        if (!isExecuted)
2157
 
                return;
2158
 
 
2159
 
        SYS_CloseFile(mapAddr);
2160
 
        isExecuted = 0;
2161
 
}
2162