source: trunk/src/sh_registry.c@ 294

Last change on this file since 294 was 294, checked in by katerina, 14 years ago

Tikets #213 and #214 (Use auditd to determine who changed a file, Windows registry check).

File size: 22.7 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 2010 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20/***************************************************************************
21 *
22 * This file provides a module for samhain to check the MS Windows registry.
23 *
24 */
25
26#include "config_xor.h"
27
28#ifdef USE_REGISTRY_CHECK
29
30#include <windows.h>
31#include <stdio.h>
32#include <time.h>
33
34#define FIL__ _("sh_registry.c")
35
36/* We don't want to build this into yule
37 */
38#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
39
40#include <sys/types.h>
41#include <regex.h>
42
43#include "samhain.h"
44#include "sh_pthread.h"
45#include "sh_utils.h"
46#include "sh_unix.h"
47#include "sh_modules.h"
48#include "sh_hash.h"
49#include "sh_tiger.h"
50
51static int check_key (char * name, int isSingle);
52
53static int sh_reg_set_active (const char *s);
54static int sh_reg_set_interval (const char * c);
55static int sh_reg_set_severity (const char *s);
56static int sh_reg_add_key (const char *s);
57static int sh_reg_add_hierarchy (const char *s);
58static int sh_reg_add_stop (const char *s);
59static int sh_reg_add_ign (const char *s);
60
61#define STOP_FALSE 0
62#define STOP_CHECK 1
63#define STOP_IGN 2
64
65sh_rconf sh_reg_check_table[] = {
66 {
67 N_("severitychange"),
68 sh_reg_set_severity,
69 },
70 {
71 N_("registrycheckactive"),
72 sh_reg_set_active,
73 },
74 {
75 N_("registrycheckinterval"),
76 sh_reg_set_interval,
77 },
78 {
79 N_("singlekey"),
80 sh_reg_add_key,
81 },
82 {
83 N_("hierarchy"),
84 sh_reg_add_hierarchy,
85 },
86 {
87 N_("stopatkey"),
88 sh_reg_add_stop,
89 },
90 {
91 N_("ignorekey"),
92 sh_reg_add_ign,
93 },
94 {
95 NULL,
96 NULL
97 }
98};
99
100/* Runtime configuration */
101
102#define SH_REGISTRY_INTERVAL 300
103
104static int ShRegCheckActive = S_FALSE;
105static time_t sh_reg_check_interval = SH_REGISTRY_INTERVAL;
106static int sh_reg_check_severity = SH_ERR_SEVERE;
107
108struct regkeylist {
109 char * name;
110 int stop;
111 int single;
112#ifdef HAVE_REGEX_H
113 regex_t preg;
114#endif
115
116 struct regkeylist *next;
117};
118
119static struct regkeylist * keylist = NULL;
120
121static int sh_reg_set_active(const char *s)
122{
123 int value;
124
125 SL_ENTER(_("sh_reg_set_active"));
126
127 value = sh_util_flagval(s, &ShRegCheckActive);
128
129 SL_RETURN((value), _("sh_reg_set_active"));
130}
131
132static int sh_reg_set_interval (const char * c)
133{
134 int retval = 0;
135 long val;
136
137 SL_ENTER(_("sh_reg_set_interval"));
138 val = strtol (c, (char **)NULL, 10);
139 if (val <= 0)
140 {
141 SH_MUTEX_LOCK(mutex_thread_nolog);
142 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
143 _("registry check interval"), c);
144 SH_MUTEX_UNLOCK(mutex_thread_nolog);
145 retval = -1;
146 }
147
148 sh_reg_check_interval = (time_t) val;
149 SL_RETURN(0, _("sh_reg_set_interval"));
150}
151
152static int sh_reg_set_severity (const char *s)
153{
154 char tmp[32];
155 tmp[0] = '='; tmp[1] = '\0';
156 sl_strlcat (tmp, s, 32);
157 return sh_error_set_level (tmp, &sh_reg_check_severity);
158}
159
160static int sh_reg_add_key_int (const char *s, int isSingle, int isStop)
161{
162 struct regkeylist * newkey;
163 size_t len = sl_strlen(s);
164
165 if (len > 0)
166 {
167 newkey = SH_ALLOC(sizeof(struct regkeylist));
168 newkey->single = isSingle;
169 newkey->stop = isStop;
170 newkey->name = NULL;
171
172 if (STOP_FALSE == isStop)
173 {
174 newkey->name = SH_ALLOC(len + 1);
175 sl_strlcpy(newkey->name, s, len+1);
176 }
177 else
178 {
179#ifdef HAVE_REGEX_H
180 int status = regcomp(&(newkey->preg), s, REG_NOSUB|REG_EXTENDED);
181 if (status != 0)
182 {
183 char errbuf[256];
184 regerror(status, &(newkey->preg), errbuf, sizeof(errbuf));
185 SH_MUTEX_LOCK(mutex_thread_nolog);
186 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
187 errbuf, _("sh_reg_add_key_int"));
188 SH_MUTEX_UNLOCK(mutex_thread_nolog);
189 SH_FREE(newkey);
190 return -1;
191 }
192#else
193 newkey->name = SH_ALLOC(len + 1);
194 sl_strlcpy(newkey->name, s, len+1);
195#endif
196 }
197 newkey->next = keylist;
198 keylist = newkey;
199 return 0;
200 }
201 return -1;
202}
203
204static int sh_reg_add_key (const char *s)
205{
206 return sh_reg_add_key_int (s, S_TRUE, STOP_FALSE);
207}
208static int sh_reg_add_hierarchy (const char *s)
209{
210 return sh_reg_add_key_int (s, S_FALSE, STOP_FALSE);
211}
212static int sh_reg_add_stop (const char *s)
213{
214 return sh_reg_add_key_int (s, S_FALSE, STOP_CHECK);
215}
216static int sh_reg_add_ign (const char *s)
217{
218 return sh_reg_add_key_int (s, S_FALSE, STOP_IGN);
219}
220
221/* Module functions */
222
223int sh_reg_check_init(struct mod_type * arg)
224{
225#ifndef HAVE_PTHREAD
226 (void) arg;
227#endif
228
229 if (ShRegCheckActive == S_FALSE)
230 return SH_MOD_FAILED;
231#ifdef HAVE_PTHREAD
232 if (arg != NULL && arg->initval < 0 &&
233 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
234 {
235 if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
236 return SH_MOD_THREAD;
237 else
238 return SH_MOD_FAILED;
239 }
240#endif
241 return 0;
242}
243
244int sh_reg_check_timer(time_t tcurrent)
245{
246 static time_t lastcheck = 0;
247
248 SL_ENTER(_("sh_reg_check_timer"));
249 if ((time_t) (tcurrent - lastcheck) >= sh_reg_check_interval)
250 {
251 lastcheck = tcurrent;
252 SL_RETURN((-1), _("sh_reg_check_timer"));
253 }
254 SL_RETURN(0, _("sh_reg_check_timer"));
255}
256
257#define SH_REGFORM_NEW 1
258#define SH_REGFORM_OLD 2
259
260static char * format_changes(int flag, char * buf, size_t len,
261 time_t time_old, unsigned long size_old,
262 unsigned long keys_old, unsigned long values_old,
263 char * hash_old,
264 time_t time_new, unsigned long size_new,
265 unsigned long keys_new, unsigned long values_new,
266 char * hash_new)
267{
268 char timestr1[32];
269 char timestr2[32];
270 char timestr3[32];
271
272 char buf_old[512] = "";
273 char buf_new[512] = "";
274
275 if ((0 != (flag & SH_REGFORM_NEW)) && (NULL != hash_new))
276 {
277 (void) sh_unix_gmttime (time_new, timestr1, sizeof(timestr1));
278 (void) sh_unix_gmttime (keys_new, timestr2, sizeof(timestr2));
279 (void) sh_unix_gmttime (values_new, timestr3, sizeof(timestr3));
280
281#ifdef SH_USE_XML
282 sl_snprintf(buf_new, sizeof(buf_new),
283 "size_new=\"%lu\" mtime_new=\"%s\" ctime_new=\"%s\" atime_new=\"%s\" chksum_new=\"%s\"",
284 size_new, timestr1, timestr2, timestr3, hash_new);
285#else
286 sl_snprintf(buf_new, sizeof(buf_new),
287 "size_new=<%lu>, mtime_new=<%s>, ctime_new=<%s>, atime_new=<%s>, chksum_new=<%s>",
288 size_new, timestr1, timestr2, timestr3, hash_new);
289#endif
290 }
291
292 if ((0 != (flag & SH_REGFORM_OLD)) && (NULL != hash_old))
293 {
294 (void) sh_unix_gmttime (time_old, timestr1, sizeof(timestr1));
295 (void) sh_unix_gmttime (keys_old, timestr2, sizeof(timestr2));
296 (void) sh_unix_gmttime (values_old, timestr3, sizeof(timestr3));
297
298#ifdef SH_USE_XML
299 sl_snprintf(buf_old, sizeof(buf_old),
300 " size_old=\"%lu\" mtime_old=\"%s\" ctime_old=\"%s\" atime_old=\"%s\" chksum_old=\"%s\"",
301 size_old, timestr1, timestr2, timestr3, hash_old);
302#else
303 sl_snprintf(buf_old, sizeof(buf_old),
304 " size_old=<%lu>, mtime_old=<%s>, ctime_old=<%s>, atime_old=<%s>, chksum_old=<%s>",
305 size_old, timestr1, timestr2, timestr3, hash_old);
306#endif
307 }
308
309 sl_strlcpy(buf, buf_new, len);
310 sl_strlcat(buf, buf_old, len);
311
312 return buf;
313}
314
315static void report_missing_entry(const char * path)
316{
317 char * infobuf = SH_ALLOC(1024);
318 char * errbuf = SH_ALLOC(1024);
319 char * tmp = sh_util_safe_name (path);
320 char timestr[32];
321 struct store2db save;
322
323 memset(&save, '\0', sizeof(struct store2db));
324 sh_hash_db2pop (path, &save);
325
326 (void) sh_unix_gmttime (save.val1, timestr, sizeof(timestr));
327
328 sl_snprintf(infobuf, 1024, _("mtime=%s size=%lu subkeys=%lu values=%lu"),
329 timestr,
330 (unsigned long) save.val0,
331 (unsigned long) save.val2,
332 (unsigned long) save.val3);
333
334 (void) format_changes (SH_REGFORM_OLD, errbuf, 1024,
335 save.val1, save.val0, save.val2, save.val3, save.checksum,
336 0, 0, 0, 0, NULL);
337
338 SH_MUTEX_LOCK(mutex_thread_nolog);
339 sh_error_handle(sh_reg_check_severity, FIL__, __LINE__, 0, MSG_REG_MISS,
340 infobuf, tmp, errbuf);
341 SH_MUTEX_UNLOCK(mutex_thread_nolog);
342
343 SH_FREE(tmp);
344 SH_FREE(errbuf);
345 SH_FREE(infobuf);
346 return;
347}
348
349int sh_reg_check_run(void)
350{
351 struct regkeylist *this = keylist;
352
353 if (this)
354 {
355 SH_MUTEX_LOCK(mutex_thread_nolog);
356 sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
357 _("Checking the registry"),
358 _("sh_reg_check_run"));
359 SH_MUTEX_UNLOCK(mutex_thread_nolog);
360
361 while (this)
362 {
363 if (STOP_FALSE == this->stop)
364 {
365 /*
366 * -- Check key --
367 */
368 check_key (this->name, this->single);
369 }
370 this = this->next;
371 }
372 }
373 sh_hash_unvisited_custom ('H', report_missing_entry);
374
375 return 0;
376}
377
378int sh_reg_check_reconf(void)
379{
380 struct regkeylist *this;
381
382 while (keylist)
383 {
384 this = keylist;
385 keylist = keylist->next;
386
387 if (this->name)
388 SH_FREE(this->name);
389#ifdef HAVE_REGEX_H
390 if (STOP_FALSE != this->stop)
391 regfree(&(this->preg));
392#endif
393 SH_FREE(this);
394 }
395
396 sh_reg_check_interval = SH_REGISTRY_INTERVAL;
397
398 return 0;
399}
400
401int sh_reg_check_cleanup(void)
402{
403 sh_reg_check_reconf();
404 return 0;
405}
406
407/* >>>>>>>>>>>> Main check function <<<<<<<<<<<< */
408
409
410#include <windows.h>
411
412#define MAX_KEY_LENGTH (2*256)
413#define MAX_VALUE_NAME (2*16384)
414
415CHAR achValue[MAX_VALUE_NAME];
416
417unsigned long nKeys = 0;
418unsigned long nVals = 0;
419
420static int CheckThisSubkey (HKEY key, char * subkey, char * path, int isSingle);
421
422static time_t convertTime(FILETIME * ft)
423{
424 time_t result;
425
426 /* Shift high part up by 2^32
427 */
428 UINT64 date = ((UINT64)ft->dwHighDateTime) << 32;
429
430 /* Add low part
431 */
432 date |= (UINT64)ft->dwLowDateTime;
433
434 /* Subtract difference between Jan 1, 1601 and Jan 1, 1970
435 */
436 date -= ((UINT64)116444736) * ((UINT64)100) * ((UINT64)10000000);
437
438 /* Divide by number of 100-nanosecond intervals per second
439 */
440 date /= ((UINT64)10000000);
441
442 /* Convert to a time_t
443 */
444 result = (time_t) date;
445
446 return result;
447}
448
449
450
451#define SH_KEY_NULL _("000000000000000000000000000000000000000000000000")
452
453int QueryKey(HKEY hKey, char * path, size_t pathlen, int isSingle)
454{
455 CHAR achKey[MAX_KEY_LENGTH]; /* buffer for subkey name */
456 DWORD cbName; /* size of name string */
457 /* CHAR achClass[MAX_PATH] = ""; *//* buffer for class name */
458 /* DWORD cchClassName = MAX_PATH/2;*//* size of class string */
459 DWORD cSubKeys=0; /* number of subkeys */
460 DWORD cbMaxSubKey; /* longest subkey size */
461 DWORD cchMaxClass; /* longest class string */
462 DWORD cValues; /* number of values for key */
463 DWORD cchMaxValue; /* longest value name */
464 DWORD cbMaxValueData; /* longest value data */
465 DWORD cbSecurityDescriptor; /* size of security descriptor */
466 FILETIME ftLastWriteTime; /* last write time */
467 DWORD lpType; /* type of data stored in value */
468 BYTE lpData[256]; /* buffer for data in value */
469 DWORD lpcbData; /* size of lpData buffer */
470 DWORD i, retCode;
471 DWORD cchValue = MAX_VALUE_NAME/2;
472
473 char hashbuf[KEYBUF_SIZE];
474 unsigned long totalSize = 0;
475 time_t fTime = 0;
476
477 char * tPath = NULL;
478 int doUpdate = S_FALSE;
479
480 retCode = RegQueryInfoKey(
481 hKey, /* key handle */
482 NULL /* achClass */, /* buffer for class name */
483 NULL /* &cchClassName */,/* size of class string */
484 NULL, /* reserved */
485 &cSubKeys, /* number of subkeys */
486 &cbMaxSubKey, /* longest subkey size */
487 &cchMaxClass, /* longest class string */
488 &cValues, /* number of values for this key */
489 &cchMaxValue, /* longest value name */
490 &cbMaxValueData, /* longest value data */
491 &cbSecurityDescriptor, /* security descriptor */
492 &ftLastWriteTime); /* last write time */
493
494 if (retCode != ERROR_SUCCESS)
495 {
496 return -1;
497 }
498
499 ++nKeys;
500
501 fTime = convertTime (&ftLastWriteTime);
502
503 /* Enumerate the subkeys, until RegEnumKeyEx fails. */
504
505 if (cSubKeys)
506 {
507 /*
508 * printf( "\nNumber of subkeys: %lu\n", (unsigned long) cSubKeys);
509 */
510
511 for (i=0; i<cSubKeys; i++)
512 {
513 cbName = MAX_KEY_LENGTH/2;
514 retCode = RegEnumKeyEx(hKey, i,
515 achKey,
516 &cbName,
517 NULL,
518 NULL,
519 NULL,
520 &ftLastWriteTime);
521
522 if (retCode == ERROR_SUCCESS && S_TRUE != isSingle)
523 {
524 /*
525 * _tprintf(TEXT("(%lu) %s\\%s\n"), (unsigned long) i+1,
526 * path, achKey);
527 */
528 CheckThisSubkey (hKey, achKey, path, isSingle);
529 }
530 }
531 }
532
533 /* Enumerate the key values. */
534
535 if (cValues)
536 {
537 char hashtmp[3][KEYBUF_SIZE];
538
539 memset(hashbuf, '0', sizeof(hashbuf));
540
541 /* Loop over values and build checksum */
542
543 for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++)
544 {
545 LPBYTE lpDataAlloc = NULL;
546
547 cchValue = MAX_VALUE_NAME/2;
548 achValue[0] = '\0';
549 lpcbData = sizeof(lpData);
550 retCode = RegEnumValue(hKey, i,
551 achValue,
552 &cchValue,
553 NULL,
554 &lpType,
555 lpData,
556 &lpcbData);
557
558 if (retCode == ERROR_MORE_DATA)
559 {
560 lpDataAlloc = SH_ALLOC(lpcbData);
561
562 retCode = RegEnumValue(hKey, i,
563 achValue,
564 &cchValue,
565 NULL,
566 &lpType,
567 lpDataAlloc,
568 &lpcbData);
569 }
570
571 if (retCode == ERROR_SUCCESS)
572 {
573 totalSize += lpcbData;
574
575 /* checksum(valuename) */
576 sh_tiger_hash (achValue, TIGER_DATA, cchValue,
577 hashtmp[0], KEYBUF_SIZE);
578
579 /* checksum(valuedata) */
580 if (NULL == lpDataAlloc)
581 {
582 sh_tiger_hash ((char*) lpData, TIGER_DATA, lpcbData,
583 hashtmp[1], KEYBUF_SIZE);
584 }
585 else
586 {
587 sh_tiger_hash ((char*) lpDataAlloc, TIGER_DATA, lpcbData,
588 hashtmp[1], KEYBUF_SIZE);
589 }
590
591 /* old_checksum */
592 memcpy(hashtmp[2], hashbuf, KEYBUF_SIZE);
593
594 /* hash(hash(valuename)+hash(valuedata)+old_hash) */
595 sh_tiger_hash ((char*) hashtmp, TIGER_DATA,
596 sizeof(hashtmp), hashbuf, sizeof(hashbuf));
597
598 ++nVals;
599 }
600
601 if (lpDataAlloc)
602 {
603 SH_FREE(lpDataAlloc);
604 }
605 }
606 }
607 else
608 {
609 /* no values */
610 sl_strlcpy(hashbuf, SH_KEY_NULL, sizeof(hashbuf));
611 }
612
613 /* Here we have:
614 * hashbuf [checksum over values],
615 * fTime [last write time],
616 * totalSize [size of all value data],
617 * cSubKeys [number of subkeys],
618 * cValues [number of values],
619 * path, pathlen [which may be up to 131072 (256*512) bytes]
620 */
621
622 if (pathlen > (PATH_MAX-1))
623 {
624 char hashbuf2[KEYBUF_SIZE];
625 char * p = strchr(path, '\\');
626
627 if (p)
628 {
629 char *q = p;
630
631 ++p;
632
633 tPath = SH_ALLOC(256 + KEYBUF_SIZE);
634 *q = '\0';
635 sl_strlcpy(tPath, path, 256); /* truncates */
636 *q = '\\';
637 sl_strlcat(tPath, "\\", 257);
638 (void) sh_tiger_hash(p, TIGER_DATA, sl_strlen(p),
639 hashbuf2, sizeof(hashbuf2));
640 sl_strlcat(tPath, hashbuf2, 256 + KEYBUF_SIZE);
641 }
642 }
643
644 if (sh.flag.checkSum == SH_CHECK_CHECK || sh.flag.update == S_TRUE)
645 {
646 struct store2db save;
647
648 memset(&save, '\0', sizeof(struct store2db));
649
650 if (tPath)
651 {
652 sh_hash_db2pop (tPath, &save);
653 }
654 else
655 {
656 sh_hash_db2pop (path, &save);
657 }
658
659 if (save.size == -1)
660 {
661 /* Not in database */
662
663 char * infobuf = SH_ALLOC(1024);
664 char * errbuf = SH_ALLOC(1024);
665 char * tmp = sh_util_safe_name ((tPath == NULL) ? path : tPath);
666 char timestr[32];
667
668 (void) sh_unix_gmttime (fTime, timestr, sizeof(timestr));
669
670 sl_snprintf(infobuf, 1024,
671 _("mtime=%s size=%lu subkeys=%lu values=%lu"),
672 timestr,
673 (unsigned long) totalSize,
674 (unsigned long) cSubKeys,
675 (unsigned long) cValues);
676
677 (void) format_changes (SH_REGFORM_NEW, errbuf, 1024,
678 0, 0, 0, 0, NULL,
679 fTime, totalSize, cSubKeys, cValues, hashbuf);
680
681 SH_MUTEX_LOCK(mutex_thread_nolog);
682 sh_error_handle(sh_reg_check_severity, FIL__, __LINE__,
683 0, MSG_REG_NEW,
684 infobuf, tmp, errbuf);
685 SH_MUTEX_UNLOCK(mutex_thread_nolog);
686
687 SH_FREE(tmp);
688 SH_FREE(errbuf);
689 SH_FREE(infobuf);
690
691 doUpdate = S_TRUE;
692 }
693 else if (save.val0 != totalSize ||
694 ((time_t) save.val1) != fTime ||
695 save.val2 != cSubKeys ||
696 save.val3 != cValues ||
697 0 != strcmp(save.checksum, hashbuf))
698 {
699 /* Change detected */
700 char * infobuf = SH_ALLOC(1024);
701 char * errbuf = SH_ALLOC(1024);
702 char * tmp = sh_util_safe_name ((tPath == NULL) ? path : tPath);
703 char timestr_new[32];
704
705 (void) sh_unix_gmttime (fTime, timestr_new, sizeof(timestr_new));
706
707 sl_snprintf(infobuf, 1024,
708 _("mtime=%s size %lu->%lu subkeys %lu->%lu values %lu->%lu checksum %s"),
709 timestr_new,
710 (unsigned long) save.val0, (unsigned long) totalSize,
711 (unsigned long) save.val2, (unsigned long) cSubKeys,
712 (unsigned long) save.val3, (unsigned long) cValues,
713 (0 == strcmp(save.checksum, hashbuf)) ? _("good") : _("bad"));
714
715 (void) format_changes (SH_REGFORM_OLD|SH_REGFORM_NEW, errbuf, 1024,
716 save.val1, save.val0,
717 save.val2, save.val3, save.checksum,
718 fTime, totalSize,
719 cSubKeys, cValues, hashbuf);
720
721 SH_MUTEX_LOCK(mutex_thread_nolog);
722 sh_error_handle(sh_reg_check_severity, FIL__, __LINE__,
723 0, MSG_REG_CHANGE,
724 infobuf, tmp, errbuf);
725 SH_MUTEX_UNLOCK(mutex_thread_nolog);
726
727 SH_FREE(tmp);
728 SH_FREE(errbuf);
729 SH_FREE(infobuf);
730
731 doUpdate = S_TRUE;
732 }
733 }
734
735 if ( sh.flag.checkSum == SH_CHECK_INIT || doUpdate == S_TRUE )
736 {
737 struct store2db save;
738
739 memset(&save, '\0', sizeof(struct store2db));
740
741 save.val0 = totalSize;
742 save.val1 = fTime;
743 save.val2 = cSubKeys;
744 save.val3 = cValues;
745 sl_strlcpy(save.checksum, hashbuf, KEY_LEN+1);
746
747 if (tPath)
748 {
749 sh_hash_push2db (tPath, &save);
750 }
751 else
752 {
753 sh_hash_push2db (path, &save);
754 }
755 }
756
757 if (tPath)
758 sh_hash_set_visited (tPath);
759 else
760 sh_hash_set_visited (path);
761
762 if (tPath)
763 {
764 SH_FREE(tPath);
765 }
766
767 return 0;
768}
769
770static int check_for_stop (char * name)
771{
772 struct regkeylist *this = keylist;
773
774 while (this)
775 {
776 if (STOP_FALSE != this->stop)
777 {
778#ifdef HAVE_REGEX_H
779 if (0 == regexec(&(this->preg), name, 0, NULL, 0))
780 return this->stop;
781#else
782 if (0 == strcmp(this->name, name))
783 return this->stop;
784#endif
785 }
786 this = this->next;
787 }
788 return STOP_FALSE;
789}
790
791
792int CheckThisSubkey (HKEY key, char * subkey, char * path, int isSingle)
793{
794 HKEY hTestKey;
795 char * newpath;
796 size_t len;
797 int retval = -1;
798
799 len = strlen(path) + 1 + strlen(subkey) + 1;
800 newpath = SH_ALLOC(len);
801 snprintf(newpath, len, "%s\\%s", path, subkey);
802
803 /* Check for stop condition, if not single key.
804 * Set flag to isSingle = S_TRUE if we should stop here.
805 */
806 if (S_TRUE != isSingle)
807 {
808 int isStop = check_for_stop(newpath);
809
810 if (STOP_CHECK == isStop)
811 {
812 isSingle = S_TRUE;
813 }
814 else if (STOP_IGN == isStop)
815 {
816 SH_FREE(newpath);
817 return 0;
818 }
819 }
820
821 len = strlen(path) + 1 + strlen(subkey) + 1;
822 newpath = SH_ALLOC(len);
823 snprintf(newpath, len, "%s\\%s", path, subkey);
824
825 if( RegOpenKeyEx( key,
826 subkey,
827 0,
828 KEY_READ,
829 &hTestKey) == ERROR_SUCCESS
830 )
831 {
832 QueryKey(hTestKey, newpath, len-1, isSingle);
833 RegCloseKey(hTestKey);
834 retval = 0;
835 }
836 else
837 {
838 /* Error message */
839 char * tmp = sh_util_safe_name (newpath);
840 size_t tlen = sl_strlen(tmp);
841
842 if (SL_TRUE == sl_ok_adds(64, tlen))
843 {
844 char * errbuf = SH_ALLOC(64 + tlen);
845 sl_snprintf(errbuf, 64+tlen, _("Failed to open key %s"), tmp);
846
847 SH_MUTEX_LOCK(mutex_thread_nolog);
848 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
849 errbuf, _("CheckThisSubkey"));
850 SH_MUTEX_UNLOCK(mutex_thread_nolog);
851
852 SH_FREE(errbuf);
853 }
854 sh_reg_add_ign (tmp);
855 SH_FREE(tmp);
856 }
857
858 SH_FREE(newpath);
859 return retval;
860}
861
862
863int check_key (char * key, int isSingle)
864{
865 HKEY topKey;
866 char * subkey;
867 char path[20] = "";
868 int pos = 0;
869
870 if (0 == strncmp(key, _("HKEY_CLASSES_ROOT"), 17))
871 {
872 topKey = HKEY_CLASSES_ROOT;
873 pos = 17;
874 strncpy(path, _("HKEY_CLASSES_ROOT"), sizeof(path));
875 }
876 else if (0 == strncmp(key, _("HKEY_CURRENT_USER"), 17))
877 {
878 topKey = HKEY_CURRENT_USER;
879 pos = 17;
880 strncpy(path, _("HKEY_CURRENT_USER"), sizeof(path));
881 }
882 else if (0 == strncmp(key, _("HKEY_LOCAL_MACHINE"), 18))
883 {
884 topKey = HKEY_LOCAL_MACHINE;
885 pos = 18;
886 strncpy(path, _("HKEY_LOCAL_MACHINE"), sizeof(path));
887 }
888 else if (0 == strncmp(key, _("HKEY_USERS"), 10))
889 {
890 topKey = HKEY_USERS;
891 pos = 10;
892 strncpy(path, _("HKEY_USERS"), sizeof(path));
893 }
894
895
896 if (pos > 0)
897 {
898 if (key[pos] == '\\')
899 {
900 ++pos;
901 subkey = &key[pos];
902 }
903 }
904 else
905 {
906
907 char * tmp = sh_util_safe_name_keepspace(key);
908 size_t tlen = sl_strlen(tmp);
909
910 if (SL_TRUE == sl_ok_adds(64, tlen))
911 {
912 char * errbuf = SH_ALLOC(64 + tlen);
913
914 sl_snprintf(errbuf, 64+tlen, _("Invalid key %s"), tmp);
915
916 SH_MUTEX_LOCK(mutex_thread_nolog);
917 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
918 errbuf, _("check_key"));
919 SH_MUTEX_UNLOCK(mutex_thread_nolog);
920
921 SH_FREE(errbuf);
922 }
923 SH_FREE(tmp);
924 return -1;
925 }
926
927 return CheckThisSubkey (topKey, subkey, path, isSingle);
928}
929
930/* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
931#endif
932
933/* #ifdef USE_REGISTRY_CHECK */
934#endif
935
Note: See TracBrowser for help on using the repository browser.