-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
configfile.l
405 lines (347 loc) · 9.43 KB
/
configfile.l
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
/*
* Reads lexical config files and updates database.
*
* MUSCLE SmartCard Development ( https://pcsclite.apdu.fr/ )
*
* Copyright (C) 1999-2002
* David Corcoran <[email protected]>
* Copyright (C) 2004
* Damien Sauveron <[email protected]>
* Copyright (C) 2004-2010
* Ludovic Rousseau <[email protected]>
*
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.
*/
%{
#include <dirent.h>
#include <sys/stat.h>
#include "wintypes.h"
#include "pcscd.h"
#include "readerfactory.h"
#include "configfile.h"
int evaluatetoken(char *pcToken);
static int iLinenumber;
static int iOldLinenumber;
static char *pcPrevious;
static char *pcCurrent;
static char *pcFriendlyname;
static char *pcDevicename;
static char *pcLibpath;
static char *pcChannelid;
static int badError;
static SerialReader *reader_list;
static int reader_list_size;
const char *ConfFile;
void tok_error(const char *pcToken_error);
%}
%option nounput
%option noinput
%option noyywrap
%%
#.* {}
"\n" { iLinenumber++; }
(\"[^"\n]*["\n])|(\'[^'\n]*['\n]) { (void)evaluatetoken(yytext); }
[ \t] {}
([A-Z]|[a-z]|[0-9]|[=\\\/\-\.\_\@:])+ { (void)evaluatetoken(yytext); }
. { iOldLinenumber = iLinenumber; tok_error(yytext); }
%%
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "config.h"
#include "misc.h"
#include "pcsclite.h"
#include "pcscd.h"
#include "debuglog.h"
#include "sys_generic.h"
#include "readerfactory.h"
int evaluatetoken(char *pcToken)
{
if (pcPrevious == NULL)
{ /* This is the key */
pcPrevious = strdup(pcToken);
iOldLinenumber = iLinenumber;
}
else
{
/* first and second tokens are not on the same line */
if (iOldLinenumber != iLinenumber)
{
tok_error(pcPrevious);
pcPrevious = strdup(pcToken);
iOldLinenumber = iLinenumber;
return 1;
}
pcCurrent = pcToken;
if (strcmp(pcPrevious, "FRIENDLYNAME") == 0)
{
if (pcFriendlyname == NULL)
{
size_t n, p;
pcFriendlyname = malloc(strlen(pcCurrent) + 1);
for (n = 0, p = 0; n < strlen(pcCurrent); n++)
{
if (pcCurrent[n] != '"')
{ /* Strip off the quotes */
pcFriendlyname[p++] = pcCurrent[n];
}
}
pcFriendlyname[p++] = '\0';
}
else
{
tok_error(pcPrevious);
return 1;
}
}
else if (strcmp(pcPrevious, "DEVICENAME") == 0)
{
if (pcDevicename == NULL)
{
struct stat fStatBuf;
pcDevicename = strdup(pcCurrent);
if ((NULL == strchr(pcDevicename, ':'))
&& (stat(pcDevicename, &fStatBuf) != 0))
{
Log3(PCSC_LOG_CRITICAL, "Error with device %s: %s",
pcDevicename, strerror(errno));
Log1(PCSC_LOG_CRITICAL, "You should remove the DEVICENAME line if your driver does not use this field");
badError = 1;
}
}
else
{
tok_error(pcPrevious);
return 1;
}
}
else if (strcmp(pcPrevious, "LIBPATH") == 0)
{
if (pcLibpath == NULL)
{
struct stat fStatBuf;
pcLibpath = strdup(pcCurrent);
if (stat(pcLibpath, &fStatBuf) != 0)
{
Log3(PCSC_LOG_CRITICAL, "Error with library %s: %s",
pcLibpath, strerror(errno));
badError = 1;
}
if (strstr(pcLibpath, ".bundle") != NULL)
{
Log1(PCSC_LOG_ERROR, "WARNING *************************************");
Log2(PCSC_LOG_ERROR, "WARNING: USB drivers SHOULD NOT be declared in reader.conf: %s", pcLibpath);
Log1(PCSC_LOG_ERROR, "WARNING *************************************");
}
}
else
{
tok_error(pcPrevious);
return 1;
}
}
else if (strcmp(pcPrevious, "CHANNELID") == 0)
{
if (pcChannelid == NULL)
pcChannelid = strdup(pcCurrent);
else
{
tok_error(pcPrevious);
return 1;
}
}
else
{
tok_error(pcPrevious);
free(pcPrevious);
pcPrevious = NULL;
return 1;
}
free(pcPrevious);
pcPrevious = NULL;
}
/* CHANNELID and DEVICENAME are both optional but not at the same time */
if (pcFriendlyname && pcLibpath && badError != 1
&& (pcChannelid || pcDevicename))
{
int channelId;
static char* defaultDeviceName = (char *)"";
Log2(PCSC_LOG_DEBUG, "Add reader: %s", pcFriendlyname);
if (0 == reader_list_size)
{
/* one real reader and one end marker */
reader_list_size = 2;
reader_list = malloc(reader_list_size * sizeof(SerialReader));
}
else
{
SerialReader *new_reader_list = NULL;
reader_list_size++;
new_reader_list = realloc(reader_list, reader_list_size *
sizeof(SerialReader));
if (new_reader_list == NULL)
free(reader_list);
reader_list = new_reader_list;
}
if (reader_list == NULL)
{
tok_error("No Memory");
return 1;
}
/* end marker */
reader_list[reader_list_size-1].pcFriendlyname = NULL;
/* the DEVICENAME parameter is optional */
if (NULL == pcDevicename)
pcDevicename = defaultDeviceName;
if (pcChannelid)
channelId = strtoul(pcChannelid, NULL, 0);
else
channelId = 0;
reader_list[reader_list_size-2].pcFriendlyname = strdup(pcFriendlyname);
reader_list[reader_list_size-2].pcDevicename = strdup(pcDevicename);
reader_list[reader_list_size-2].pcLibpath = strdup(pcLibpath),
reader_list[reader_list_size-2].channelId = channelId;
free(pcFriendlyname);
pcFriendlyname = NULL;
if (pcDevicename != defaultDeviceName)
free(pcDevicename);
pcDevicename = NULL;
free(pcLibpath);
pcLibpath = NULL;
if (pcChannelid)
free(pcChannelid);
pcChannelid = NULL;
}
return 0;
}
void tok_error(const char *token_error)
{
#ifdef NO_LOG
(void)token_error;
#endif
Log4(PCSC_LOG_ERROR, "tok_error: invalid value line %d in %s: %s",
iOldLinenumber, ConfFile, token_error);
badError = 1;
}
int DBGetReaderListDir(const char *readerconf_dir,
SerialReader **caller_reader_list)
{
DIR *dir;
int ret = 0;
/* (re)start with an empty list */
reader_list = NULL;
reader_list_size = 0;
dir = opendir(readerconf_dir);
if (dir)
{
/* the configuration file is a directory */
struct dirent *direntry;
Log2(PCSC_LOG_DEBUG, "Parsing conf directory: %s", readerconf_dir);
/* for each configuration file */
while ((direntry = readdir(dir)) != NULL)
{
char filename[FILENAME_MAX];
int r;
snprintf(filename, sizeof(filename), "%s/%s",
readerconf_dir, direntry->d_name);
/* skip non regular files */
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
if (direntry->d_type == DT_UNKNOWN)
#endif
{
struct stat st;
if (lstat(filename, &st) != 0)
{
Log2(PCSC_LOG_DEBUG, "Skipping non statable file: %s",
direntry->d_name);
continue;
}
if (!S_ISREG(st.st_mode))
{
Log2(PCSC_LOG_DEBUG, "Skipping non regular file: %s",
direntry->d_name);
continue;
}
}
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
else
if (direntry->d_type != DT_REG)
{
Log2(PCSC_LOG_DEBUG, "Skipping non regular file: %s",
direntry->d_name);
continue;
}
#endif
/* skip files starting with . like ., .., .svn, etc */
if ('.' == direntry->d_name[0])
{
Log2(PCSC_LOG_DEBUG, "Skipping hidden file: %s",
direntry->d_name);
continue;
}
/* each call to DBGetReaderList() will append to the list */
r = DBGetReaderList(filename, caller_reader_list);
/* set the global return value to the latest error */
if (r)
ret = r;
}
closedir(dir);
}
else
/* the configuration file is really a file */
ret = DBGetReaderList(readerconf_dir, caller_reader_list);
return ret;
}
int DBGetReaderList(const char *readerconf, SerialReader **caller_reader_list)
{
FILE *configFile = NULL;
*caller_reader_list = NULL; /* no list by default */
/* used by tok_error() */
ConfFile = readerconf;
Log2(PCSC_LOG_DEBUG, "Parsing conf file: %s", ConfFile);
configFile = fopen(readerconf, "r");
if (configFile == NULL)
return 1;
yyin = configFile;
/* (re)start with a clean state */
iLinenumber = 1;
iOldLinenumber = -1;
pcFriendlyname = NULL;
pcDevicename = NULL;
pcLibpath = NULL;
pcChannelid = NULL;
pcPrevious = NULL;
pcCurrent = NULL;
badError = 0;
do
{
(void)yylex();
}
while (!feof(configFile));
yylex_destroy();
(void)fclose(configFile);
*caller_reader_list = reader_list;
if (badError == 1)
return -1;
else
return 0;
} /* End of configfile.c */