-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinscard_msg.c
520 lines (454 loc) · 12.6 KB
/
winscard_msg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html )
*
* Copyright (C) 2001-2004
* David Corcoran <corcoran@musclecard.com>
* Copyright (C) 2003-2004
* Damien Sauveron <damien.sauveron@labri.fr>
* Copyright (C) 2002-2010
* Ludovic Rousseau <ludovic.rousseau@free.fr>
*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: winscard_msg.c 7004 2014-10-02 09:26:36Z rousseau $
*/
/**
* @file
* @brief This is responsible for client/server communication.
*
* A file based socket (\c commonSocket) is used to send/receive only messages
* among clients and server.\n
* The messages' data are passed throw a memory mapped file: \c sharedSegmentMsg.
*/
#include <PCSC/config.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#include <PCSC/misc.h>
#include <PCSC/pcscd.h>
#include <PCSC/winscard.h>
#include <PCSC/debuglog.h>
#include <PCSC/winscard_msg.h>
#include <PCSC/sys_generic.h>
#include <PCSC/utils.h>
#include <PCSC/strlcpycat.h>
#ifdef PCSCD
/* functions used by pcscd only */
#else
/* functions used by libpcsclite only */
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC 0
#endif
char *getSocketName(void)
{
static char socketName[sizeof(struct sockaddr_un)];
if ('\0' == socketName[0])
{
/* socket name not yet initialized */
char *socketNameEnv;
socketNameEnv = getenv("PCSCLITE_CSOCK_NAME");
if (socketNameEnv)
strlcpy(socketName, socketNameEnv, sizeof(socketName));
else
strlcpy(socketName, PCSCLITE_CSOCK_NAME, sizeof(socketName));
}
return socketName;
}
/**
* @brief Prepares a communication channel for the client to talk to the server.
*
* This is called by the application to create a socket for local IPC with the
* server. The socket is associated to the file \c PCSCLITE_CSOCK_NAME.
*
* @param[out] pdwClientID Client Connection ID.
*
* @retval 0 Success.
* @retval -1 Can not create the socket.
* @retval -1 The socket can not open a connection.
* @retval -1 Can not set the socket to non-blocking.
*/
INTERNAL int ClientSetupSession(uint32_t *pdwClientID)
{
struct sockaddr_un svc_addr;
int ret;
char *socketName;
ret = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (ret < 0)
{
Log2(PCSC_LOG_CRITICAL, "Error: create on client socket: %s",
strerror(errno));
return -1;
}
*pdwClientID = ret;
socketName = getSocketName();
svc_addr.sun_family = AF_UNIX;
strncpy(svc_addr.sun_path, socketName, sizeof(svc_addr.sun_path));
if (connect(*pdwClientID, (struct sockaddr *) &svc_addr,
sizeof(svc_addr.sun_family) + strlen(svc_addr.sun_path) + 1) < 0)
{
Log3(PCSC_LOG_CRITICAL, "Error: connect to client socket %s: %s",
socketName, strerror(errno));
(void)close(*pdwClientID);
return -1;
}
ret = fcntl(*pdwClientID, F_GETFL, 0);
if (ret < 0)
{
Log3(PCSC_LOG_CRITICAL, "Error: cannot retrieve socket %s flags: %s",
socketName, strerror(errno));
(void)close(*pdwClientID);
return -1;
}
if (fcntl(*pdwClientID, F_SETFL, ret | O_NONBLOCK) < 0)
{
Log3(PCSC_LOG_CRITICAL, "Error: cannot set socket %s nonblocking: %s",
socketName, strerror(errno));
(void)close(*pdwClientID);
return -1;
}
return 0;
}
/**
* @brief Closes the socket used by the client to communicate with the server.
*
* @param[in] dwClientID Client socket handle to be closed.
*
* @retval 0 Success.
*/
INTERNAL int ClientCloseSession(uint32_t dwClientID)
{
return close(dwClientID);
}
/**
* @brief Called by the Client to get the reponse from the server or vice-versa.
*
* Reads the message from the file \c filedes.
*
* @param[in] command one of the \ref pcsc_msg_commands commands
* @param[out] buffer_void Message read.
* @param[in] buffer_size Size to read
* @param[in] filedes Socket handle.
* @param[in] timeOut Timeout in milliseconds.
*
* @retval SCARD_S_SUCCESS Success.
* @retval SCARD_E_TIMEOUT Timeout.
* @retval SCARD_F_COMM_ERROR Socket is closed.
* @retval SCARD_F_COMM_ERROR A signal was received.
*/
INTERNAL LONG MessageReceiveTimeout(uint32_t command, void *buffer_void,
uint64_t buffer_size, int32_t filedes, long timeOut)
{
char *buffer = buffer_void;
/* default is success */
LONG retval = SCARD_S_SUCCESS;
/* record the time when we started */
struct timeval start;
/* how many bytes we must read */
size_t remaining = buffer_size;
gettimeofday(&start, NULL);
/* repeat until we get the whole message */
while (remaining > 0)
{
fd_set read_fd;
struct timeval timeout, now;
int selret;
long delta;
gettimeofday(&now, NULL);
delta = time_sub(&now, &start);
if (delta > timeOut*1000)
{
/* we already timed out */
retval = SCARD_E_TIMEOUT;
break;
}
/* remaining time to wait */
delta = timeOut*1000 - delta;
FD_ZERO(&read_fd);
FD_SET(filedes, &read_fd);
timeout.tv_sec = delta/1000000;
timeout.tv_usec = delta - timeout.tv_sec*1000000;
selret = select(filedes + 1, &read_fd, NULL, NULL, &timeout);
/* try to read only when socket is readable */
if (selret > 0)
{
int readed;
if (!FD_ISSET(filedes, &read_fd))
{
/* very strange situation. it should be an assert really */
retval = SCARD_F_COMM_ERROR;
break;
}
readed = read(filedes, buffer, remaining);
if (readed > 0)
{
/* we got something */
buffer += readed;
remaining -= readed;
} else if (readed == 0)
{
/* peer closed the socket */
retval = SCARD_F_COMM_ERROR;
break;
} else
{
/* we ignore the signals and empty socket situations, all
* other errors are fatal */
if (errno != EINTR && errno != EAGAIN)
{
retval = SCARD_F_COMM_ERROR;
break;
}
}
} else if (selret == 0)
{
/* is the daemon still there? */
retval = SCardCheckDaemonAvailability();
if (retval != SCARD_S_SUCCESS)
{
/* timeout */
break;
}
/* you need to set the env variable PCSCLITE_DEBUG=0 since
* this is logged on the client side and not on the pcscd
* side*/
Log2(PCSC_LOG_INFO, "Command 0x%X not yet finished", command);
} else
{
/* we ignore signals, all other errors are fatal */
if (errno != EINTR)
{
Log2(PCSC_LOG_ERROR, "select returns with failure: %s",
strerror(errno));
retval = SCARD_F_COMM_ERROR;
break;
}
}
}
return retval;
}
/**
* @brief Wrapper for the MessageSend() function.
*
* Called by clients to send messages to the server.
* The parameters \p command and \p data are set in the \c sharedSegmentMsg
* struct in order to be sent.
*
* @param[in] command Command to be sent.
* @param[in] dwClientID Client socket handle.
* @param[in] size Size of the message (\p data).
* @param[in] data_void Data to be sent.
*
* @return Same error codes as MessageSend().
*/
INTERNAL LONG MessageSendWithHeader(uint32_t command, uint32_t dwClientID,
uint64_t size, void *data_void)
{
struct rxHeader header;
LONG ret;
/* header */
header.command = command;
header.size = size;
ret = MessageSend(&header, sizeof(header), dwClientID);
/* command */
if (size > 0)
ret = MessageSend(data_void, size, dwClientID);
return ret;
}
#endif
/* functions used by pcscd and libpcsclite */
/**
* @brief Sends a menssage from client to server or vice-versa.
*
* Writes the message in the shared file \c filedes.
*
* @param[in] buffer_void Message to be sent.
* @param[in] buffer_size Size of the message to send
* @param[in] filedes Socket handle.
*
* @retval SCARD_S_SUCCESS Success
* @retval SCARD_E_TIMEOUT Timeout.
* @retval SCARD_F_COMM_ERROR Socket is closed.
* @retval SCARD_F_COMM_ERROR A signal was received.
*/
INTERNAL LONG MessageSend(void *buffer_void, uint64_t buffer_size,
int32_t filedes)
{
char *buffer = buffer_void;
/* default is success */
LONG retval = SCARD_S_SUCCESS;
/* how many bytes remains to be written */
size_t remaining = buffer_size;
/* repeat until all data is written */
while (remaining > 0)
{
fd_set write_fd;
int selret;
FD_ZERO(&write_fd);
FD_SET(filedes, &write_fd);
selret = select(filedes + 1, NULL, &write_fd, NULL, NULL);
/* try to write only when the file descriptor is writable */
if (selret > 0)
{
int written;
if (!FD_ISSET(filedes, &write_fd))
{
/* very strange situation. it should be an assert really */
retval = SCARD_F_COMM_ERROR;
break;
}
/* since we are a user library we can't play with signals
* The signals may already be used by the application */
#ifdef MSG_NOSIGNAL
/* Get EPIPE return code instead of SIGPIPE signal
* Works on Linux */
written = send(filedes, buffer, remaining, MSG_NOSIGNAL);
#else
/* we may get a SIGPIPE signal if the other side has closed */
written = write(filedes, buffer, remaining);
#endif
if (written > 0)
{
/* we wrote something */
buffer += written;
remaining -= written;
} else if (written == 0)
{
/* peer closed the socket */
retval = SCARD_F_COMM_ERROR;
break;
} else
{
/* we ignore the signals and socket full situations, all
* other errors are fatal */
if (errno != EINTR && errno != EAGAIN)
{
retval = SCARD_E_NO_SERVICE;
break;
}
}
} else if (selret == 0)
{
/* timeout */
retval = SCARD_E_TIMEOUT;
break;
} else
{
/* ignore signals */
if (errno != EINTR)
{
Log2(PCSC_LOG_ERROR, "select returns with failure: %s",
strerror(errno));
retval = SCARD_F_COMM_ERROR;
break;
}
}
}
return retval;
}
/**
* @brief Called by the Client to get the reponse from the server or vice-versa.
*
* Reads the message from the file \c filedes.
*
* @param[out] buffer_void Message read.
* @param[in] buffer_size Size to read
* @param[in] filedes Socket handle.
*
* @retval SCARD_S_SUCCESS Success.
* @retval SCARD_F_COMM_ERROR Socket is closed.
* @retval SCARD_F_COMM_ERROR A signal was received.
*/
INTERNAL LONG MessageReceive(void *buffer_void, uint64_t buffer_size,
int32_t filedes)
{
char *buffer = buffer_void;
/* default is success */
LONG retval = SCARD_S_SUCCESS;
/* how many bytes we must read */
size_t remaining = buffer_size;
/* repeat until we get the whole message */
while (remaining > 0)
{
fd_set read_fd;
int selret;
FD_ZERO(&read_fd);
FD_SET(filedes, &read_fd);
selret = select(filedes + 1, &read_fd, NULL, NULL, NULL);
/* try to read only when socket is readable */
if (selret > 0)
{
int readed;
if (!FD_ISSET(filedes, &read_fd))
{
/* very strange situation. it should be an assert really */
retval = SCARD_F_COMM_ERROR;
break;
}
readed = read(filedes, buffer, remaining);
if (readed > 0)
{
/* we got something */
buffer += readed;
remaining -= readed;
} else if (readed == 0)
{
/* peer closed the socket */
retval = SCARD_F_COMM_ERROR;
break;
} else
{
/* we ignore the signals and empty socket situations, all
* other errors are fatal */
if (errno != EINTR && errno != EAGAIN)
{
retval = SCARD_F_COMM_ERROR;
break;
}
}
}
else
{
/* we ignore signals, all other errors are fatal */
if (errno != EINTR)
{
Log2(PCSC_LOG_ERROR, "select returns with failure: %s",
strerror(errno));
retval = SCARD_F_COMM_ERROR;
break;
}
}
}
return retval;
}