-
Notifications
You must be signed in to change notification settings - Fork 0
/
winscard_msg_srv.c
269 lines (237 loc) · 6.65 KB
/
winscard_msg_srv.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
/*
* 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_srv.c 7004 2014-10-02 09:26:36Z rousseau $
*/
/**
* @file
* @brief client/server communication (on the server side only)
*
* 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>
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#include <PCSC/misc.h>
#include <PCSC/pcscd.h>
#include <PCSC/sd-daemon.h>
#include <PCSC/winscard.h>
#include <PCSC/debuglog.h>
#include <PCSC/winscard_msg.h>
/**
* Socket to a file, used for clients-server comminication.
*/
static int commonSocket = 0;
extern char AraKiri;
/**
* @brief Accepts a Client connection.
*
* Called by \c ProcessEventsServer().
*
* @param[out] pdwClientID Connection ID used to reference the Client.
*
* @return Error code.
* @retval 0 Success.
* @retval -1 Can not establish the connection.
*/
static int ProcessCommonChannelRequest(/*@out@*/ uint32_t *pdwClientID)
{
socklen_t clnt_len;
int new_sock;
struct sockaddr_un clnt_addr;
clnt_len = sizeof(clnt_addr);
if ((new_sock = accept(commonSocket, (struct sockaddr *) &clnt_addr,
&clnt_len)) < 0)
{
Log2(PCSC_LOG_CRITICAL, "Accept on common socket: %s",
strerror(errno));
return -1;
}
*pdwClientID = new_sock;
return 0;
}
/**
* @brief Prepares the communication channel used by the server to talk to the
* clients.
*
* This is called by the server to create a socket for local IPC with the
* clients. The socket is associated to the file \c PCSCLITE_CSOCK_NAME.
* Each client will open a connection to this socket.
*
* @return Error code.
* @retval 0 Success
* @retval -1 Can not create the socket.
* @retval -1 Can not bind the socket to the file \c PCSCLITE_CSOCK_NAME.
* @retval -1 Can not put the socket in listen mode.
*/
INTERNAL int32_t InitializeSocket(void)
{
union
{
struct sockaddr sa;
struct sockaddr_un un;
} sa;
/*
* Create the common shared connection socket
*/
if ((commonSocket = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
{
Log2(PCSC_LOG_CRITICAL, "Unable to create common socket: %s",
strerror(errno));
return -1;
}
memset(&sa, 0, sizeof sa);
sa.un.sun_family = AF_UNIX;
strncpy(sa.un.sun_path, PCSCLITE_CSOCK_NAME, sizeof sa.un.sun_path);
(void)remove(PCSCLITE_CSOCK_NAME);
if (bind(commonSocket, &sa.sa, sizeof sa) < 0)
{
Log2(PCSC_LOG_CRITICAL, "Unable to bind common socket: %s",
strerror(errno));
return -1;
}
if (listen(commonSocket, 1) < 0)
{
Log2(PCSC_LOG_CRITICAL, "Unable to listen common socket: %s",
strerror(errno));
return -1;
}
/*
* Chmod the public entry channel
*/
(void)chmod(PCSCLITE_CSOCK_NAME, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
return 0;
}
/**
* @brief Acquires a socket passed in from systemd.
*
* This is called by the server to start listening on an existing socket for
* local IPC with the clients.
*
* @param fd The file descriptor to start listening on.
*
* @return Error code.
* @retval 0 Success
* @retval -1 Passed FD is not an UNIX socket.
*/
INTERNAL int32_t ListenExistingSocket(int fd)
{
if (!sd_is_socket(fd, AF_UNIX, SOCK_STREAM, -1))
{
Log1(PCSC_LOG_CRITICAL, "Passed FD is not an UNIX socket");
return -1;
}
commonSocket = fd;
return 0;
}
/**
* @brief Looks for messages sent by clients.
*
* This is called by the Server's function \c SVCServiceRunLoop().
*
* @param[out] pdwClientID Connection ID used to reference the Client.
*
* @return Error code.
* @retval 0 Success.
* @retval -1 Error accessing the communication channel.
* @retval -2 EINTR
* @retval 2 Timeout.
*/
#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#define DO_TIMEOUT
#endif
INTERNAL int32_t ProcessEventsServer(uint32_t *pdwClientID)
{
fd_set read_fd;
int selret;
#ifdef DO_TIMEOUT
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
#endif
FD_ZERO(&read_fd);
/*
* Set up the bit masks for select
*/
FD_SET(commonSocket, &read_fd);
selret = select(commonSocket + 1, &read_fd, (fd_set *) NULL,
(fd_set *) NULL,
#ifdef DO_TIMEOUT
&tv
#else
NULL
#endif
);
if (selret < 0)
{
if (EINTR == errno)
return -2;
Log2(PCSC_LOG_CRITICAL, "Select returns with failure: %s",
strerror(errno));
return -1;
}
if (selret == 0)
/* timeout. On *BSD only */
return 2;
/*
* A common pipe packet has arrived - it could be a new application
*/
if (FD_ISSET(commonSocket, &read_fd))
{
Log1(PCSC_LOG_DEBUG, "Common channel packet arrival");
if (ProcessCommonChannelRequest(pdwClientID) == -1)
{
Log2(PCSC_LOG_ERROR,
"error in ProcessCommonChannelRequest: %d", *pdwClientID);
return -1;
}
}
else
return -1;
Log2(PCSC_LOG_DEBUG,
"ProcessCommonChannelRequest detects: %d", *pdwClientID);
return 0;
}