forked from open-eid/smartcardpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCardLog.cpp
302 lines (268 loc) · 7.87 KB
/
SCardLog.cpp
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
/*
* SMARTCARDPP
*
* This software is released under either the GNU Library General Public
* License (see LICENSE.LGPL) or the BSD License (see LICENSE.BSD).
*
* Note that the only valid version of the LGPL license as far as this
* project is concerned is the original GNU Library General Public License
* Version 2.1, February 1999
*
*/
#include "SCardLog.h"
#ifdef WIN32
#include <Windows.h>
#endif
SCardLog::SCardLog(){}
SCardLog::~SCardLog(){}
FILE *SCardLog::openLog()
{
FILE *debugfp = NULL;
#if defined(__APPLE__)
struct stat buffer;
std::string fileName;
if(char *dir = getenv("TMPDIR"))
fileName = std::string(dir) + "/smartcardpp.log";
else
fileName = "/tmp/smartcardpp.log";
if(stat(fileName.c_str(), &buffer) >= 0)
{
debugfp=fopen(fileName.c_str(), "a+");
}
#elif defined(WIN32)
if(getenv("SMARTCARDPP_DEBUG")!= NULL)
{
WCHAR logfile[MAX_PATH * 4];
GetTempPathW(sizeof(logfile)/sizeof(WCHAR),logfile);
_snwprintf(logfile + wcslen(logfile),MAX_PATH,L"smartcardpp.log");
debugfp=_wfsopen(logfile, L"a+", _SH_DENYNO);
}
#else
struct stat buffer;
std::string fileName = "/tmp/smartcardpp.log";
if(stat(fileName.c_str(), &buffer) >= 0)
{
debugfp=fopen(fileName.c_str(), "a+");
}
#endif
return debugfp;
}
void SCardLog::writeLog(const char *fmt,...)
{
FILE *debugfp = openLog();
if(debugfp != NULL)
{
time_t dbgTime;
time(&dbgTime);
struct tm *timeinfo = localtime(&dbgTime);
struct timeval tim;
gettimeofday(&tim, NULL);
va_list vList ;
va_start(vList,fmt);
fprintf(debugfp, "%02i.%02i.%i %02i:%02i:%02i.%li [%s] ", timeinfo->tm_mday,
timeinfo->tm_mon+1,
(timeinfo->tm_year)-100,
timeinfo->tm_hour,
timeinfo->tm_min,
timeinfo->tm_sec,
tim.tv_usec,
SCardLog::getParentProcName().c_str());
vfprintf(debugfp, fmt, vList);
fprintf(debugfp, "\n");
va_end(vList);
fflush(debugfp);
fclose(debugfp);
debugfp = NULL;
}
}
void SCardLog::writeMessage(const char *fmt,...)
{
FILE *debugfp = openLog();
if(debugfp != NULL)
{
time_t dbgTime;
time(&dbgTime);
struct tm *timeinfo = localtime(&dbgTime);
struct timeval tim;
gettimeofday(&tim, NULL);
va_list vList ;
char msg[8192] ;
va_start(vList,fmt);
vsnprintf(msg, 8192, fmt, vList) ;
fprintf(debugfp, "%02i.%02i.%i %02i:%02i:%02i.%li [%s] %s\n", timeinfo->tm_mday, timeinfo->tm_mon+1, (timeinfo->tm_year)-100,
timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, tim.tv_usec, SCardLog::getParentProcName().c_str(), msg);
fflush(debugfp);
va_end(vList);
fclose(debugfp);
debugfp = NULL;
}
}
void SCardLog::writeAPDULog(const char *func, int line, ByteVec apdu, DWORD protocol, bool isCmd, unsigned int connectionID, unsigned int transactionID)
{
FILE *debugfp = openLog();
if(debugfp != NULL)
{
std::stringstream APDU;
time_t dbgTime;
time(&dbgTime);
struct tm *timeinfo = localtime(&dbgTime);
struct timeval tim;
gettimeofday(&tim, NULL);
APDU.str("");
for (ByteVec::iterator it = apdu.begin(); it < apdu.end(); it++)
{
APDU << "0x" << std::hex << std::setfill('0') << std::setw(2) << std::uppercase << (int) *it << ", ";
}
fprintf(debugfp, "%02i.%02i.%i %02i:%02i:%02i.%li [%s][%i:%i] [%s:%i] [%s] %s %s \n", timeinfo->tm_mday, timeinfo->tm_mon+1, (timeinfo->tm_year)-100,
timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, tim.tv_usec, SCardLog::getParentProcName().c_str(), connectionID, transactionID, func, line, protocol==SCARD_PROTOCOL_T0 ? "T0" : "T1", isCmd ? "->" : "<-", APDU.str().c_str());
fflush(debugfp);
fclose(debugfp);
debugfp = NULL;
}
}
void SCardLog::writeByteVecLog(ByteVec buff, const char *msg)
{
FILE *debugfp = openLog();
if(debugfp != NULL)
{
std::stringstream buffStream;
time_t dbgTime;
time(&dbgTime);
struct tm *timeinfo = localtime(&dbgTime);
struct timeval tim;
gettimeofday(&tim, NULL);
buffStream.str("");
for (ByteVec::iterator it = buff.begin(); it < buff.end(); it++)
buffStream << std::hex << std::setfill('0') << std::setw(2) << (int) *it << " ";
fprintf(debugfp, "%02i.%02i.%i %02i:%02i:%02i.%li [%s] [%s] %s with size: %i \n", timeinfo->tm_mday, timeinfo->tm_mon+1, (timeinfo->tm_year)-100,
timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, tim.tv_usec, SCardLog::getParentProcName().c_str(), msg, buffStream.str().c_str(), (int)buff.size());
fflush(debugfp);
fclose(debugfp);
debugfp = NULL;
}
}
std::string SCardLog::getParentProcName()
{
std::string procName = "";
#ifdef WIN32
WCHAR _cname[256 * 4 ] = L"\0";
HMODULE caller = GetModuleHandle(NULL);
PWCHAR cname = _cname + 1;
GetModuleFileNameW(caller,cname,256);
PWCHAR fl = (PWCHAR)cname + lstrlenW(cname) - 1;
while ('\\' != *fl)
fl--;
fl++;
char procNameChar[1024];
memset((void *)procNameChar, 0, sizeof(procNameChar));
char DefChar = ' ';
WideCharToMultiByte(CP_ACP, 0, fl, -1, procNameChar, sizeof(procNameChar), &DefChar, NULL);
procName.append(procNameChar);
#else
#ifdef __APPLE__
struct kinfo_proc info;
size_t length = sizeof(struct kinfo_proc);
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
{
procName.append("Unknown");
}
else
{
procName.append(info.kp_proc.p_comm);
}
#else
std::stringstream ss;
ss << getpid() << ":" << pthread_self();
procName = ss.str();
#endif
#endif
return procName;
}
std::string SCardLog::getParentProcFullName()
{
std::string procName = "";
#ifdef WIN32
WCHAR _cname[256 * 4] = L"\0";
HMODULE caller = GetModuleHandle(NULL);
PWCHAR cname = _cname + 1;
GetModuleFileNameW(caller, cname, 256);
char procNameChar[1024];
memset((void *)procNameChar, 0, sizeof(procNameChar));
char DefChar = ' ';
WideCharToMultiByte(CP_ACP, 0, cname, -1, procNameChar, sizeof(procNameChar), &DefChar, NULL);
procName.append(procNameChar);
#else
#ifdef __APPLE__
struct kinfo_proc info;
size_t length = sizeof(struct kinfo_proc);
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
{
procName.append("Unknown");
}
else
{
procName.append(info.kp_proc.p_comm);
}
#else
std::stringstream ss;
ss << getpid() << ":" << pthread_self();
procName = ss.str();
#endif
#endif
return procName;
}
#ifdef WIN32
int SCardLog::gettimeofday(struct timeval *tv, struct timezone *tz)
{
FILETIME ft;
unsigned __int64 tmpres = 0;
static int tzflag = 0;
if (NULL != tv)
{
GetSystemTimeAsFileTime(&ft);
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
tmpres /= 10; /*convert into microseconds*/
/*converting file time to unix epoch*/
tmpres -= DELTA_EPOCH_IN_MICROSECS;
tv->tv_sec = (long)(tmpres / 1000000UL);
tv->tv_usec = (long)(tmpres % 1000000UL);
}
if (NULL != tz)
{
if (!tzflag)
{
_tzset();
tzflag++;
}
tz->tz_minuteswest = _timezone / 60;
tz->tz_dsttime = _daylight;
}
return 0;
}
#endif
void SCardLog::writeByteBufferLog(const char *func, int line, unsigned int connectionID, unsigned int transactionID, unsigned char *buff, int buffLength, const char *msg)
{
FILE *debugfp = openLog();
if(debugfp != NULL)
{
std::stringstream buffStream;
time_t dbgTime;
time(&dbgTime);
struct tm *timeinfo = localtime(&dbgTime);
struct timeval tim;
gettimeofday(&tim, NULL);
buffStream.str("");
unsigned char *ptr = buff;
for(int i = 0; i < buffLength; i++)
buffStream << std::hex << std::setfill('0') << std::setw(2) << (int)ptr[i] << " ";
fprintf(debugfp, "%02i.%02i.%i %02i:%02i:%02i.%li [%s] [%s:%i] [%i:%i] [%s] %s with size: %i \n", timeinfo->tm_mday, timeinfo->tm_mon+1, (timeinfo->tm_year)-100,
timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, tim.tv_usec, SCardLog::getParentProcName().c_str(), func, line, connectionID, transactionID, msg, buffStream.str().c_str(), buffLength);
fflush(debugfp);
fclose(debugfp);
debugfp = NULL;
}
}