-
Notifications
You must be signed in to change notification settings - Fork 12
/
eventlog.c
460 lines (379 loc) · 12 KB
/
eventlog.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
/*
This code is a modification of the original Eventlog to Syslog Script written by
Curtis Smith of Purdue University. The original copyright notice can be found below.
The original program was modified by Sherwin Faria for Rochester Institute of Technology
in July 2009 to provide bug fixes and add several new features. Additions include
the ability to ignore specific events, add the event timestamp to outgoing messages,
a service status file, and compatibility with the new Vista/2k8 Windows Events service.
Sherwin Faria
Rochester Institute of Technology
Information & Technology Services Bldg. 10
1 Lomb Memorial Drive
Rochester, NY 14623 U.S.A.
Send all comments, suggestions, or bug reports to:
*/
/*
Copyright (c) 1998-2007, Purdue University
All rights reserved.
Redistribution and use in source and binary forms are permitted provided
that:
(1) source distributions retain this entire copyright notice and comment,
and
(2) distributions including binaries display the following acknowledgement:
"This product includes software developed by Purdue University."
in the documentation or other materials provided with the distribution
and in all advertising materials mentioning features or use of this
software.
The name of the University may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
This software was developed by:
Curtis Smith
Purdue University
Engineering Computer Network
465 Northwestern Avenue
West Lafayette, Indiana 47907-2035 U.S.A.
Send all comments, suggestions, or bug reports to:
*/
/* Include files */
#include "main.h"
#include "eventlog.h"
#include "log.h"
#include "service.h"
#include "syslog.h"
#include "check.h"
#include "dns.h"
/* Number of eventlogs */
#define EVENTLOG_SZ 32
/* Size of buffer */
#define EVENTLOG_BUF_SZ (10*1024)
/* Number of strings in formatted message */
#define EVENTLOG_ARRAY_SZ 96
/* Eventlog descriptor */
struct Eventlog {
char name[EVENTLOG_NAME_SZ]; /* Name of eventlog */
HANDLE handle; /* Handle to eventlog */
char buffer[EVENTLOG_BUF_SZ]; /* Message buffer */
int count; /* Number of messages left */
int pos; /* Current position in buffer */
int recnum; /* Next record number */
};
/* List of eventlogs */
static struct Eventlog EventlogList[EVENTLOG_SZ];
int EventlogCount = 0;
/* Create new eventlog descriptor */
int EventlogCreate(char * name)
{
/* Check count */
if (EventlogCount == EVENTLOG_SZ) {
Log(LOG_ERROR, "Too many eventlogs: %d", EVENTLOG_SZ);
return 1;
}
/* Store new name */
strncpy_s(EventlogList[EventlogCount].name, sizeof(EventlogList[EventlogCount].name), name, _TRUNCATE);
/* Increment count */
EventlogCount++;
/* Success */
return 0;
}
/* Close eventlog */
static void EventlogClose(int log)
{
/* Close log */
CloseEventLog(EventlogList[log].handle);
EventlogList[log].handle = NULL;
}
/* Close eventlogs */
void EventlogsClose()
{
int i;
/* Loop until list depleted */
for (i = 0; i < EventlogCount; i++)
if (EventlogList[i].handle)
EventlogClose(i);
/* Reset count */
EventlogCount = 0;
}
/* Open event log */
static int EventlogOpen(int log)
{
DWORD count;
DWORD oldest;
/* Reset all indicators */
EventlogList[log].count = 0;
EventlogList[log].pos = 0;
EventlogList[log].recnum = 1;
/* Open log */
EventlogList[log].handle = OpenEventLog(NULL, EventlogList[log].name);
if (EventlogList[log].handle == NULL) {
Log(LOG_ERROR|LOG_SYS, "Cannot open event log: \"%s\"", EventlogList[log].name);
return 1;
}
/* Get number of records to skip */
if (GetNumberOfEventLogRecords(EventlogList[log].handle, &count) == 0) {
Log(LOG_ERROR|LOG_SYS, "Cannot get record count for event log: \"%s\"", EventlogList[log].name);
return 1;
}
/* Get oldest record number */
if (GetOldestEventLogRecord(EventlogList[log].handle, &oldest) == 0 && count != 0) {
Log(LOG_ERROR|LOG_SYS, "Cannot get oldest record number for event log: \"%s\"", EventlogList[log].name);
return 1;
}
/* Store record of next event */
EventlogList[log].recnum = oldest + count;
if (EventlogList[log].recnum == 0)
EventlogList[log].recnum = 1; /* ?? */
/* Success */
return 0;
}
/* Open event logs */
int EventlogsOpen()
{
int i;
/* Open the log files */
for (i = 0; i < EventlogCount; i++)
if (EventlogOpen(i))
break;
/* Check for errors */
if (i != EventlogCount) {
EventlogsClose();
return 1;
}
/* Success */
return 0;
}
/* Get the next eventlog message */
char * EventlogNext(EventList ignore_list[MAX_IGNORED_EVENTS], int log, int * level)
{
BOOL reopen = FALSE;
DWORD errnum;
DWORD needed;
DWORD loglevel;
EVENTLOGRECORD * event;
char * cp;
char * current;
char * formatted_string;
char * message_file;
char * source;
char * string_array[EVENTLOG_ARRAY_SZ];
char * username;
char hostname[HOSTNAME_SZ];
char defmsg[ERRMSG_SZ];
int event_id;
int i;
char *index;
static char message[SYSLOG_DEF_SZ-17];
static char tstamped_message[SYSLOG_DEF_SZ];
/* Initialize array to prevent memory exceptions with bad message definitions */
for(i = 0; i < EVENTLOG_ARRAY_SZ; i++)
string_array[i]="*";
/* Are there any records left in buffer */
while (EventlogList[log].pos == EventlogList[log].count) {
/* Reset input position */
EventlogList[log].count = 0;
EventlogList[log].pos = 0;
/* Read a record */
needed = 0;
if (ReadEventLog(EventlogList[log].handle, EVENTLOG_FORWARDS_READ | EVENTLOG_SEEK_READ, EventlogList[log].recnum, EventlogList[log].buffer, sizeof(EventlogList[log].buffer), &EventlogList[log].count, &needed) == 0) {
/* Check error */
errnum = GetLastError();
switch (errnum) {
/* Message too large... skip over */
case ERROR_INSUFFICIENT_BUFFER:
Log(LOG_WARNING, "Eventlog message size too large: \"%s\": %u bytes", EventlogList[log].name, needed);
EventlogList[log].recnum++;
break;
/* Eventlog corrupted (?)... Reopen */
case ERROR_EVENTLOG_FILE_CORRUPT:
Log(LOG_INFO, "Eventlog was corrupted: \"%s\"", EventlogList[log].name);
reopen = TRUE;
break;
/* Eventlog files are clearing... Reopen */
case ERROR_EVENTLOG_FILE_CHANGED:
Log(LOG_INFO, "Eventlog was cleared: \"%s\"", EventlogList[log].name);
reopen = TRUE;
break;
/* Record not available (yet) */
case ERROR_INVALID_PARAMETER:
return NULL;
/* Normal end of eventlog messages */
case ERROR_HANDLE_EOF:
return NULL;
/* Eventlog probably closing down */
case RPC_S_UNKNOWN_IF:
return NULL;
/* Unknown condition */
default:
Log(LOG_ERROR|LOG_SYS, "Eventlog \"%s\" returned error: ", EventlogList[log].name);
ServiceIsRunning = FALSE;
return NULL;
}
/* Process reopen */
if (reopen) {
EventlogClose(log);
if (EventlogOpen(log)) {
ServiceIsRunning = FALSE;
return NULL;
}
reopen = FALSE;
}
}
}
/* Increase record number */
EventlogList[log].recnum++;
/* Get position into buffer */
current = EventlogList[log].buffer + EventlogList[log].pos;
/* Get pointer to current event record */
event = (EVENTLOGRECORD *) current;
/* Advance position */
EventlogList[log].pos += event->Length;
/* Get source and event id */
source = current + sizeof(*event);
event_id = (int) HRESULT_CODE(event->EventID);
/* Check Event Info Against Ignore List */
if (IgnoreSyslogEvent(ignore_list, source, event_id)) {
if (LogInteractive)
printf("IGNORING_EVENT: SOURCE=%s & ID=%i\n", source, event_id);
return NULL;
}
/* Check number of strings */
if (event->NumStrings > COUNT_OF(string_array)) {
/* Too many strings */
Log(LOG_WARNING, "Eventlog message has too many strings to print message: \"%s\": %u strings", EventlogList[log].name, event->NumStrings);
formatted_string = NULL;
} else {
/* Convert strings to arrays */
cp = current + event->StringOffset;
for (i = 0; i < event->NumStrings; i++) {
string_array[i] = cp;
while (*cp++ != '\0');
}
message_file = LookupMessageFile(EventlogList[log].name, source, event->EventID);
if (message_file == NULL) {
/* Cannot load resources */
formatted_string = NULL;
} else {
/* Format eventlog message */
formatted_string = FormatLibraryMessage(message_file, event->EventID, string_array);
}
}
/* Create a default message if resources or formatting didn't work */
if (formatted_string == NULL) {
_snprintf_s(defmsg, sizeof(defmsg), _TRUNCATE,
"(Facility: %u, Status: %s)",
HRESULT_FACILITY(event->EventID),
FAILED(event->EventID) ? "Failure" : "Success"
);
formatted_string = defmsg;
}
/* replace every space in source by underscores */
index = source;
while( *index ) {
if( *index == ' ' ) {
*index = '_';
}
index++;
}
/* Format source and event ID number */
if(SyslogIncludeTag) {
_snprintf_s(message, sizeof(message), _TRUNCATE,
"%s: %s: %u: ",
SyslogTag,
source,
HRESULT_CODE(event->EventID)
);
} else {
_snprintf_s(message, sizeof(message), _TRUNCATE,
"%s: %u: ",
source,
HRESULT_CODE(event->EventID)
);
}
/* Convert user */
if (event->UserSidLength > 0) {
username = GetUsername((SID *) (current + event->UserSidOffset));
if (username) {
strncat_s(message, sizeof(message), username, _TRUNCATE);
strncat_s(message, sizeof(message), ": ", _TRUNCATE);
}
}
/* Add formatted string to base message */
strncat_s(message, sizeof(message), formatted_string, _TRUNCATE);
/* Select syslog level */
switch (event->EventType) {
case EVENTLOG_ERROR_TYPE:
loglevel = SYSLOG_ERR;
*level = SYSLOG_BUILD(SyslogFacility, loglevel);
break;
case EVENTLOG_WARNING_TYPE:
loglevel = SYSLOG_WARNING;
*level = SYSLOG_BUILD(SyslogFacility, loglevel);
break;
case EVENTLOG_INFORMATION_TYPE:
loglevel = SYSLOG_NOTICE;
*level = SYSLOG_BUILD(SyslogFacility, loglevel);
break;
case EVENTLOG_AUDIT_SUCCESS:
strncat_s(message, sizeof(message), "AUDIT_SUCCESS ", _TRUNCATE);
loglevel = SYSLOG_NOTICE;
*level = SYSLOG_BUILD(SyslogFacility, loglevel);
break;
case EVENTLOG_AUDIT_FAILURE:
strncat_s(message, sizeof(message), "AUDIT_FAILURE ", _TRUNCATE);
loglevel = SYSLOG_ERR;
*level = SYSLOG_BUILD(SyslogFacility, loglevel);
break;
/* Everything else */
case EVENTLOG_SUCCESS:
default:
loglevel = SYSLOG_NOTICE;
*level = SYSLOG_BUILD(SyslogFacility, loglevel);
break;
}
/* If event is not being ignored, make sure it is severe enough to be logged */
if (SyslogLogLevel != 0)
if (SyslogLogLevel < loglevel-1)
return NULL;
/* Add hostname for RFC compliance (RFC 3164) */
/* if -a then use the fqdn bound to our IP address. If none, use the IP address */
if (ProgramUseIPAddress == TRUE) {
strcpy_s(hostname, HOSTNAME_SZ, ProgramHostName);
} else {
if (ExpandEnvironmentStrings("%COMPUTERNAME%", hostname, COUNT_OF(hostname)) == 0) {
strcpy_s(hostname, COUNT_OF(hostname), "HOSTNAME_ERR");
Log(LOG_ERROR|LOG_SYS, "Cannot expand %COMPUTERNAME%");
}
}
/* Query and Add timestamp from EventLog, add hostname, */
/* and finally the message to the string */
_snprintf_s(tstamped_message, sizeof(tstamped_message), _TRUNCATE,
"%s %s %s",
TimeToString(event->TimeGenerated),
hostname,
message
);
/* Return formatted message */
return tstamped_message;
}
/* Format Timestamp from EventLog */
char * TimeToString(DWORD dw)
{
time_t tt;
struct tm stm;
char result[16];
static char formatted_result[] = "Mmm dd hh:mm:ss";
tt = (time_t) dw;
/* Format timestamp string */
if (localtime_s(&stm, &tt) == 0) {
strftime(result, sizeof(result), "%b %d %H:%M:%S", &stm);
if (result[4] == '0') /* Replace leading zero with a space for */
result[4] = ' '; /* single digit days so we comply with the RFC */
} else
result[0] = '\0';
strncpy_s(formatted_result, sizeof(result), result, _TRUNCATE);
return formatted_result;
}