forked from zensquare/evtsys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.c
322 lines (263 loc) · 9.29 KB
/
registry.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
/*
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 <mbstring.h>
#include "main.h"
#include "log.h"
#include "syslog.h"
#include "eventlog.h"
#include "winevent.h"
// Registry //
// List of registry items to store //
struct RegistryData {
char * name; // Name of key //
DWORD key_type; // Type of key //
void * value; // Value of key //
DWORD size; // Size of key //
int required; // Key is required //
};
// Location of application data in registry tree //
static char RegistryApplicationDataPath[] = "Software\\ECN\\EvtSys\\3.0";
// List of application data //
static struct RegistryData RegistryApplicationDataList[] = {
{ "Facility", REG_DWORD, &SyslogFacility, sizeof(SyslogFacility), TRUE },
{ "LogHost", REG_SZ, &SyslogLogHosts, sizeof(SyslogLogHosts), TRUE },
{ "Port", REG_DWORD, &SyslogPort, sizeof(SyslogPort), TRUE },
{ "StatusInterval", REG_DWORD, &SyslogStatusInterval, sizeof(SyslogStatusInterval), FALSE },
{ "QueryDhcp", REG_DWORD, &SyslogQueryDhcp, sizeof(SyslogQueryDhcp), FALSE },
{ "LogLevel", REG_DWORD, &SyslogLogLevel, sizeof(SyslogLogLevel), FALSE },
{ "IncludeOnly", REG_DWORD, &SyslogIncludeOnly, sizeof(SyslogIncludeOnly), FALSE },
{ "Tag", REG_SZ, &SyslogTag, sizeof(SyslogTag), FALSE },
{ "MaxMessageSize", REG_DWORD, &SyslogMessageSize, sizeof(SyslogMessageSize), FALSE },
{ "UseIPAddress", REG_DWORD, &ProgramUseIPAddress, sizeof(ProgramUseIPAddress), FALSE },
{ "EnableTcp", REG_DWORD, &SyslogEnableTcp, sizeof(SyslogEnableTcp), FALSE }
};
// Location of eventlog data in registry tree //
static char RegistryEventlogDataPath[] = "System\\CurrentControlSet\\Services\\EventLog\\Application\\EvtSys";
// List of eventlog data //
static DWORD RegistryEventlogTypes = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
static struct RegistryData RegistrEventlogDataList[] = {
{ "EventMessageFile", REG_EXPAND_SZ, ProgramExePath, sizeof(ProgramExePath)-1 },
{ "TypesSupported", REG_DWORD, &RegistryEventlogTypes, sizeof(RegistryEventlogTypes) }
};
// Location of eventlog keys //
static char RegistryEventlogKeyPath[] = "SYSTEM\\CurrentControlSet\\Services\\Eventlog";
// Create default entries into registry //
static int RegistryCreate(char * path, struct RegistryData * list, int count)
{
DWORD disposition;
HKEY registry_handle;
int i;
// Open location for putting key information //
if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
path,
0,
"",
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
®istry_handle,
&disposition)) {
Log(LOG_ERROR|LOG_SYS, "Cannot initialize access to registry: \"%s\"", path);
return 1;
}
// Check for existing //
if (disposition == REG_OPENED_EXISTING_KEY)
Log(LOG_WARNING, "Replacing existing keys: \"%s\"", path);
// Process all values //
for (i = 0; i < count; i++) {
// Set value //
if (RegSetValueEx(registry_handle,
list[i].name,
0,
list[i].key_type,
list[i].value,
list[i].size)) {
Log(LOG_ERROR|LOG_SYS, "Cannot install registry key: Key=\"%s\"", list[i].name);
break;
}
}
// Close registry //
RegCloseKey(registry_handle);
// Return status //
return i != count;
}
// Delete registry entries //
static int RegistryDelete(char * path)
{
// Open location for putting key information //
if (RegDeleteKey(HKEY_LOCAL_MACHINE, path)) {
Log(LOG_ERROR|LOG_SYS, "Cannot delete registry keys: \"%s\"", path);
return 1;
}
return 0;
}
// Install registry settings //
int RegistryInstall()
{
// Register application and eventlog data //
if (RegistryCreate(RegistryApplicationDataPath, RegistryApplicationDataList, COUNT_OF(RegistryApplicationDataList)))
return 1;
if (RegistryCreate(RegistryEventlogDataPath, RegistrEventlogDataList, COUNT_OF(RegistrEventlogDataList)))
return 1;
// Success //
return 0;
}
// Remove registry settings //
int RegistryUninstall()
{
int status = 0;
// Remove application and eventlog data //
if (RegistryDelete(RegistryApplicationDataPath))
status = 1;
if (RegistryDelete(RegistryEventlogDataPath))
status = 1;
// Return overall status //
return status;
}
// Read registry settings //
int RegistryRead()
{
DWORD key_type;
HKEY registry_handle;
int e;
int i;
// Open location for putting key information //
if (RegOpenKey(HKEY_LOCAL_MACHINE, RegistryApplicationDataPath, ®istry_handle)) {
Log(LOG_ERROR|LOG_SYS, "Cannot initialize access to registry: \"%s\"", RegistryApplicationDataPath);
return 1;
}
// Process all values //
for (i = 0; i < COUNT_OF(RegistryApplicationDataList); i++) {
// Get value //
if (e = RegQueryValueEx(registry_handle, RegistryApplicationDataList[i].name, 0, &key_type, RegistryApplicationDataList[i].value, &RegistryApplicationDataList[i].size))
if (e != ERROR_FILE_NOT_FOUND || RegistryApplicationDataList[i].required) {
Log(LOG_ERROR|LOG_SYS, "Cannot query registry key: \"%s\"", RegistryApplicationDataList[i].name);
break;
}
}
// Close registry //
RegCloseKey(registry_handle);
// Return status //
return i != COUNT_OF(RegistryApplicationDataList);
}
// Gather list of keys //
int RegistryGather()
{
DWORD size;
HKEY registry_handle;
char name[EVENTLOG_NAME_SZ];
int errnum;
int index;
// Open location for enumerating key information //
if (RegOpenKey(HKEY_LOCAL_MACHINE, RegistryEventlogKeyPath, ®istry_handle)) {
Log(LOG_ERROR|LOG_SYS, "Cannot initialize access to registry: \"%s\"", RegistryEventlogKeyPath);
return 1;
}
// Process keys until end of list //
index = 0;
while (1) {
// Get next key //
size = sizeof(name);
errnum = RegEnumKey(registry_handle, index, name, size);
// Stop if last item //
if (errnum == ERROR_NO_MORE_ITEMS)
break;
// Check for error //
if (errnum) {
Log(LOG_ERROR|LOG_SYS, "Cannot enumerate registry key: \"%s\"", RegistryEventlogKeyPath);
break;
}
// Create new eventlog //
if (EventlogCreate(name))
break;
// Advance index number //
index++;
}
// Close registry //
RegCloseKey(registry_handle);
// Return status //
return errnum != ERROR_NO_MORE_ITEMS;
}
// Update Registry Keys //
int RegistryUpdate()
{
DWORD key_type;
DWORD key_size = sizeof(ProgramExePath);
HKEY registry_handle;
int e;
int status = 0;
LPBYTE msgfile = malloc(key_size);
// Check if DLL was specified //
// Open location for reading key information //
if (RegOpenKey(HKEY_LOCAL_MACHINE, RegistryEventlogDataPath, ®istry_handle)) {
Log(LOG_ERROR|LOG_SYS, "Cannot initialize access to registry: \"%s\"", RegistryEventlogDataPath);
return 1;
}
// Get value //
if (e = RegQueryValueEx(registry_handle, "EventMessageFile", 0, &key_type, msgfile, &key_size))
{
if (e != ERROR_FILE_NOT_FOUND)
{
Log(LOG_ERROR|LOG_SYS, "Cannot query registry key: \"%s\"", "EventMessageFile");
status = 1;
}
}
else
{
// Ensure string is null terminated //
if (msgfile[key_size-1] != '\0')
if (key_size < sizeof(ProgramExePath))
msgfile[key_size] = '\0';
else
msgfile[key_size-1] = '\0';
// If DLL found, replace with exe //
if (_mbsstr(msgfile, (const LPBYTE)".dll") != NULL || _mbsstr(msgfile, (const LPBYTE)".DLL") != NULL)
status = RegistryCreate(RegistryEventlogDataPath, RegistrEventlogDataList, COUNT_OF(RegistrEventlogDataList));
}
if (msgfile != NULL)
free(msgfile);
// Close the key when done //
RegCloseKey(registry_handle);
// Success //
return status;
}