forked from zensquare/evtsys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
371 lines (316 loc) · 9.36 KB
/
main.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
/*
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 "dns.h"
#include "eventlog.h"
#include "log.h"
#include "syslog.h"
#include "getopt.h"
#include "ver.h"
#include "check.h"
#include "winevent.h"
#include "wsock.h"
// Main program //
// Program variables //
static BOOL ProgramDebug = FALSE;
static BOOL ProgramInstall = FALSE;
static BOOL ProgramUninstall = FALSE;
static BOOL ProgramIncludeOnly = FALSE;
static char * ProgramName;
static char * ProgramSyslogFacility = NULL;
static char * ProgramSyslogLogHosts = NULL;
static char * ProgramSyslogPort = NULL;
static char * ProgramSyslogQueryDhcp = NULL;
static char * ProgramSyslogInterval = NULL;
static char * ProgramSyslogTag = NULL;
static char * ProgramLogLevel = NULL;
BOOL ProgramUseIPAddress = FALSE;
char ProgramHostName[256];
char ProgramExePath[MAX_PATH];
EventList IgnoredEvents[MAX_IGNORED_EVENTS];
XPathList * XPathQueries;
// Operate on program flags //
static int mainOperateFlags()
{
int status = 0;
// Install new service //
if (ProgramInstall) {
// Install registry //
if (RegistryInstall())
return 1;
// Install service //
if (ServiceInstall())
return 1;
// Success //
return 0;
}
else if (ProgramUninstall) { // Uninstall service //
// Remove service //
if (ServiceRemove())
status = 1;
// Remove registry settings //
if (RegistryUninstall())
status = 1;
// Return status //
return status;
}
else {
// Update Registry Values if necessary //
RegistryUpdate();
}
// discover our FQDN (or IP if no FQDN) //
if (ProgramUseIPAddress) {
if (get_hostname(ProgramHostName, 255) == 0) {
ProgramUseIPAddress = FALSE;
Log(LOG_ERROR, "Unable to determine IP address or FQDN for -a flag.");
return 1;
}
}
// Load the current registry keys //
if (RegistryRead())
return 1;
// Parse LogHosts from registry //
if (CheckSyslogLogHost(SyslogLogHosts))
return 1;
// Open Syslog Connections //
if(SyslogOpen())
return 1;
// If in debug mode, call main loop directly //
if (ProgramDebug)
status = MainLoop();
else
// Otherwise, start service dispatcher, that will eventually call MainLoop //
status = ServiceStart();
// Close syslog connections //
SyslogClose();
// Return status //
return status;
}
// Program usage information //
static void mainUsage()
{
if (LogInteractive) {
fprintf(stderr, "Version: %s (%s-bit)\n", VERSION,
#ifdef _WIN64
"64"
#else
"32"
#endif
);
fprintf(stderr, "Usage: %s -i|-u|-d [-h host[;host2;...]] [-f facility] [-p port]\n", ProgramName);
fputs(" [-t tag] [-s minutes] [-q bool] [-l level] [-n] [-a]\n", stderr);
fputs(" -i Install service\n", stderr);
fputs(" -u Uninstall service\n", stderr);
fputs(" -d Debug: run as console program\n", stderr);
fputs(" -a Use our IP address (or fqdn) in the syslog message\n", stderr);
fputs(" -h hosts Name of log host(s), separated by a ';'\n", stderr);
fputs(" -f facility Facility level of syslog message\n", stderr);
fputs(" -l level Minimum level to send to syslog\n", stderr);
fputs(" 0=All/Verbose, 1=Critical, 2=Error, 3=Warning, 4=Info\n", stderr);
fputs(" -n (**Win9x/Server 2003 Only**) Include only those events specified\n", stderr);
fputs(" in the config file\n", stderr);
fputs(" -p port Port number of syslogd\n", stderr);
fputs(" -q bool Query the Dhcp server to obtain the syslog/port to log to\n", stderr);
fputs(" (0/1 = disable/enable)\n", stderr);
fputs(" -t tag Include tag as program field in syslog message\n", stderr);
fputs(" -s minutes Optional interval between status messages. 0 = Disabled\n", stderr);
fputc('\n', stderr);
fprintf(stderr, "Default port: %u\n", SYSLOG_DEF_PORT);
fprintf(stderr, "Default facility: %s\n", SYSLOG_DEF_FAC_NAME);
fprintf(stderr, "Default status interval: %u\n", SYSLOG_DEF_INTERVAL);
fputs("Host (-h) required if installing.\n", stderr);
} else
Log(LOG_ERROR, "Invalid flag usage; Check startup parameters");
}
// Process flags //
static int mainProcessFlags(int argc, char ** argv)
{
int flag;
// Note all actions //
while ((flag = GetOpt(argc, argv, "f:iudh:p:q:s:l:nat:")) != EOF) {
switch (flag) {
case 'd':
ProgramDebug = TRUE;
break;
case 'f':
ProgramSyslogFacility = GetOptArg;
break;
case 'h':
ProgramSyslogLogHosts = GetOptArg;
break;
case 'i':
ProgramInstall = TRUE;
break;
case 'p':
ProgramSyslogPort = GetOptArg;
break;
case 'q':
ProgramSyslogQueryDhcp = GetOptArg;
break;
case 's':
ProgramSyslogInterval = GetOptArg;
break;
case 'u':
ProgramUninstall = TRUE;
break;
case 'l':
ProgramLogLevel = GetOptArg;
break;
case 'n':
ProgramIncludeOnly = TRUE;
break;
case 't':
ProgramSyslogTag = GetOptArg;
break;
case 'a':
ProgramUseIPAddress = TRUE;
break;
default:
mainUsage();
return 1;
}
}
argc -= GetOptInd;
argv += GetOptInd;
if (argc) {
mainUsage();
return 1;
}
// Must have only one of //
if (ProgramInstall + ProgramUninstall + ProgramDebug > 1) {
Log(LOG_ERROR, "Pass only one of -i, -u or -d");
return 1;
}
// If installing, must have a log host //
if (ProgramInstall && ProgramSyslogLogHosts == NULL) {
Log(LOG_ERROR, "Syslogd host name (-h) flag required");
return 1;
}
// Check arguments //
if (ProgramSyslogLogHosts) {
if (CheckSyslogLogHost(ProgramSyslogLogHosts))
return 1;
}
if (ProgramSyslogFacility) {
if (CheckSyslogFacility(ProgramSyslogFacility))
return 1;
}
if (ProgramSyslogPort) {
if (CheckSyslogPort(ProgramSyslogPort))
return 1;
}
if (ProgramSyslogInterval) {
if (CheckSyslogInterval(ProgramSyslogInterval))
return 1;
}
if (ProgramSyslogQueryDhcp) {
if (CheckSyslogQueryDhcp(ProgramSyslogQueryDhcp))
return 1;
}
if (ProgramLogLevel) {
if (CheckSyslogLogLevel(ProgramLogLevel))
return 1;
}
if (ProgramIncludeOnly)
CheckSyslogIncludeOnly();
if(ProgramSyslogTag)
if(CheckSyslogTag(ProgramSyslogTag))
return 1;
// Proceed to do operation //
return mainOperateFlags();
}
// Main program //
int main(int argc, char ** argv)
{
int status;
SetConsoleCtrlHandler(ShutdownConsole, TRUE);
// Save program name //
ProgramName = argv[0];
// Get and store executable path //
if (!GetModuleFileName(NULL, ProgramExePath, sizeof(ProgramExePath))) {
Log(LOG_ERROR, "Unable to get path to my executable");
return 1;
}
// Start eventlog //
if (LogStart()) {
return 1;
} else {
// Start the network //
if (WSockStart() == 0) {
// Process flags //
status = mainProcessFlags(argc, argv);
// Stop network if needed //
WSockStop();
}
}
// Show status //
if (LogInteractive) {
if (status)
puts("Command did not complete due to a failure");
else
puts("Command completed successfully");
}
// Stop event logging //
LogStop();
// Success //
return status;
}
// Shut down the console cleanly //
BOOL WINAPI ShutdownConsole(DWORD dwCtrlType)
{
Log(LOG_INFO, "Signal caught, shutting down and exiting...");
switch(dwCtrlType)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
if (CheckForWindowsEvents())
WinEventCancelSubscribes();
EventlogsClose();
SyslogClose();
break;
default:
return FALSE;
}
exit(0);
}