source: trunk/src/sh_registry.c@ 297

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

Support for IPv6 (ticket #222).

File size: 23.1 KB
RevLine 
[294]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
[295]449#if !defined(KEY_WOW64_64KEY)
450#define KEY_WOW64_64KEY 0x0100;
451#endif
452#if !defined(KEY_WOW64_32KEY)
453#define KEY_WOW64_32KEY 0x0200;
454#endif
[294]455
456
457#define SH_KEY_NULL _("000000000000000000000000000000000000000000000000")
458
459int QueryKey(HKEY hKey, char * path, size_t pathlen, int isSingle)
460{
461 CHAR achKey[MAX_KEY_LENGTH]; /* buffer for subkey name */
462 DWORD cbName; /* size of name string */
463 /* CHAR achClass[MAX_PATH] = ""; *//* buffer for class name */
464 /* DWORD cchClassName = MAX_PATH/2;*//* size of class string */
465 DWORD cSubKeys=0; /* number of subkeys */
466 DWORD cbMaxSubKey; /* longest subkey size */
467 DWORD cchMaxClass; /* longest class string */
468 DWORD cValues; /* number of values for key */
469 DWORD cchMaxValue; /* longest value name */
470 DWORD cbMaxValueData; /* longest value data */
471 DWORD cbSecurityDescriptor; /* size of security descriptor */
472 FILETIME ftLastWriteTime; /* last write time */
473 DWORD lpType; /* type of data stored in value */
474 BYTE lpData[256]; /* buffer for data in value */
475 DWORD lpcbData; /* size of lpData buffer */
476 DWORD i, retCode;
477 DWORD cchValue = MAX_VALUE_NAME/2;
478
479 char hashbuf[KEYBUF_SIZE];
480 unsigned long totalSize = 0;
481 time_t fTime = 0;
482
483 char * tPath = NULL;
484 int doUpdate = S_FALSE;
485
486 retCode = RegQueryInfoKey(
487 hKey, /* key handle */
488 NULL /* achClass */, /* buffer for class name */
489 NULL /* &cchClassName */,/* size of class string */
490 NULL, /* reserved */
491 &cSubKeys, /* number of subkeys */
492 &cbMaxSubKey, /* longest subkey size */
493 &cchMaxClass, /* longest class string */
494 &cValues, /* number of values for this key */
495 &cchMaxValue, /* longest value name */
496 &cbMaxValueData, /* longest value data */
497 &cbSecurityDescriptor, /* security descriptor */
498 &ftLastWriteTime); /* last write time */
499
500 if (retCode != ERROR_SUCCESS)
501 {
502 return -1;
503 }
504
505 ++nKeys;
506
507 fTime = convertTime (&ftLastWriteTime);
508
509 /* Enumerate the subkeys, until RegEnumKeyEx fails. */
510
511 if (cSubKeys)
512 {
513 /*
514 * printf( "\nNumber of subkeys: %lu\n", (unsigned long) cSubKeys);
515 */
516
517 for (i=0; i<cSubKeys; i++)
518 {
519 cbName = MAX_KEY_LENGTH/2;
520 retCode = RegEnumKeyEx(hKey, i,
521 achKey,
522 &cbName,
523 NULL,
524 NULL,
525 NULL,
526 &ftLastWriteTime);
527
528 if (retCode == ERROR_SUCCESS && S_TRUE != isSingle)
529 {
530 /*
531 * _tprintf(TEXT("(%lu) %s\\%s\n"), (unsigned long) i+1,
532 * path, achKey);
533 */
534 CheckThisSubkey (hKey, achKey, path, isSingle);
535 }
536 }
537 }
538
539 /* Enumerate the key values. */
540
541 if (cValues)
542 {
543 char hashtmp[3][KEYBUF_SIZE];
544
545 memset(hashbuf, '0', sizeof(hashbuf));
546
547 /* Loop over values and build checksum */
548
549 for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++)
550 {
551 LPBYTE lpDataAlloc = NULL;
552
553 cchValue = MAX_VALUE_NAME/2;
554 achValue[0] = '\0';
555 lpcbData = sizeof(lpData);
556 retCode = RegEnumValue(hKey, i,
557 achValue,
558 &cchValue,
559 NULL,
560 &lpType,
561 lpData,
562 &lpcbData);
563
564 if (retCode == ERROR_MORE_DATA)
565 {
566 lpDataAlloc = SH_ALLOC(lpcbData);
567
568 retCode = RegEnumValue(hKey, i,
569 achValue,
570 &cchValue,
571 NULL,
572 &lpType,
573 lpDataAlloc,
574 &lpcbData);
575 }
576
577 if (retCode == ERROR_SUCCESS)
578 {
579 totalSize += lpcbData;
580
581 /* checksum(valuename) */
582 sh_tiger_hash (achValue, TIGER_DATA, cchValue,
583 hashtmp[0], KEYBUF_SIZE);
584
585 /* checksum(valuedata) */
586 if (NULL == lpDataAlloc)
587 {
588 sh_tiger_hash ((char*) lpData, TIGER_DATA, lpcbData,
589 hashtmp[1], KEYBUF_SIZE);
590 }
591 else
592 {
593 sh_tiger_hash ((char*) lpDataAlloc, TIGER_DATA, lpcbData,
594 hashtmp[1], KEYBUF_SIZE);
595 }
596
597 /* old_checksum */
598 memcpy(hashtmp[2], hashbuf, KEYBUF_SIZE);
599
600 /* hash(hash(valuename)+hash(valuedata)+old_hash) */
601 sh_tiger_hash ((char*) hashtmp, TIGER_DATA,
602 sizeof(hashtmp), hashbuf, sizeof(hashbuf));
603
604 ++nVals;
605 }
606
607 if (lpDataAlloc)
608 {
609 SH_FREE(lpDataAlloc);
610 }
611 }
612 }
613 else
614 {
615 /* no values */
616 sl_strlcpy(hashbuf, SH_KEY_NULL, sizeof(hashbuf));
617 }
618
619 /* Here we have:
620 * hashbuf [checksum over values],
621 * fTime [last write time],
622 * totalSize [size of all value data],
623 * cSubKeys [number of subkeys],
624 * cValues [number of values],
625 * path, pathlen [which may be up to 131072 (256*512) bytes]
626 */
627
628 if (pathlen > (PATH_MAX-1))
629 {
630 char hashbuf2[KEYBUF_SIZE];
631 char * p = strchr(path, '\\');
632
633 if (p)
634 {
635 char *q = p;
636
637 ++p;
638
639 tPath = SH_ALLOC(256 + KEYBUF_SIZE);
640 *q = '\0';
641 sl_strlcpy(tPath, path, 256); /* truncates */
642 *q = '\\';
643 sl_strlcat(tPath, "\\", 257);
644 (void) sh_tiger_hash(p, TIGER_DATA, sl_strlen(p),
645 hashbuf2, sizeof(hashbuf2));
646 sl_strlcat(tPath, hashbuf2, 256 + KEYBUF_SIZE);
647 }
648 }
649
650 if (sh.flag.checkSum == SH_CHECK_CHECK || sh.flag.update == S_TRUE)
651 {
652 struct store2db save;
653
654 memset(&save, '\0', sizeof(struct store2db));
655
656 if (tPath)
657 {
658 sh_hash_db2pop (tPath, &save);
659 }
660 else
661 {
662 sh_hash_db2pop (path, &save);
663 }
664
665 if (save.size == -1)
666 {
667 /* Not in database */
668
669 char * infobuf = SH_ALLOC(1024);
670 char * errbuf = SH_ALLOC(1024);
671 char * tmp = sh_util_safe_name ((tPath == NULL) ? path : tPath);
672 char timestr[32];
673
674 (void) sh_unix_gmttime (fTime, timestr, sizeof(timestr));
675
676 sl_snprintf(infobuf, 1024,
677 _("mtime=%s size=%lu subkeys=%lu values=%lu"),
678 timestr,
679 (unsigned long) totalSize,
680 (unsigned long) cSubKeys,
681 (unsigned long) cValues);
682
683 (void) format_changes (SH_REGFORM_NEW, errbuf, 1024,
684 0, 0, 0, 0, NULL,
685 fTime, totalSize, cSubKeys, cValues, hashbuf);
686
687 SH_MUTEX_LOCK(mutex_thread_nolog);
688 sh_error_handle(sh_reg_check_severity, FIL__, __LINE__,
689 0, MSG_REG_NEW,
690 infobuf, tmp, errbuf);
691 SH_MUTEX_UNLOCK(mutex_thread_nolog);
692
693 SH_FREE(tmp);
694 SH_FREE(errbuf);
695 SH_FREE(infobuf);
696
697 doUpdate = S_TRUE;
698 }
699 else if (save.val0 != totalSize ||
700 ((time_t) save.val1) != fTime ||
701 save.val2 != cSubKeys ||
702 save.val3 != cValues ||
703 0 != strcmp(save.checksum, hashbuf))
704 {
705 /* Change detected */
706 char * infobuf = SH_ALLOC(1024);
707 char * errbuf = SH_ALLOC(1024);
708 char * tmp = sh_util_safe_name ((tPath == NULL) ? path : tPath);
709 char timestr_new[32];
710
711 (void) sh_unix_gmttime (fTime, timestr_new, sizeof(timestr_new));
712
713 sl_snprintf(infobuf, 1024,
714 _("mtime=%s size %lu->%lu subkeys %lu->%lu values %lu->%lu checksum %s"),
715 timestr_new,
716 (unsigned long) save.val0, (unsigned long) totalSize,
717 (unsigned long) save.val2, (unsigned long) cSubKeys,
718 (unsigned long) save.val3, (unsigned long) cValues,
719 (0 == strcmp(save.checksum, hashbuf)) ? _("good") : _("bad"));
720
721 (void) format_changes (SH_REGFORM_OLD|SH_REGFORM_NEW, errbuf, 1024,
722 save.val1, save.val0,
723 save.val2, save.val3, save.checksum,
724 fTime, totalSize,
725 cSubKeys, cValues, hashbuf);
726
727 SH_MUTEX_LOCK(mutex_thread_nolog);
728 sh_error_handle(sh_reg_check_severity, FIL__, __LINE__,
729 0, MSG_REG_CHANGE,
730 infobuf, tmp, errbuf);
731 SH_MUTEX_UNLOCK(mutex_thread_nolog);
732
733 SH_FREE(tmp);
734 SH_FREE(errbuf);
735 SH_FREE(infobuf);
736
737 doUpdate = S_TRUE;
738 }
739 }
740
741 if ( sh.flag.checkSum == SH_CHECK_INIT || doUpdate == S_TRUE )
742 {
743 struct store2db save;
744
745 memset(&save, '\0', sizeof(struct store2db));
746
747 save.val0 = totalSize;
748 save.val1 = fTime;
749 save.val2 = cSubKeys;
750 save.val3 = cValues;
751 sl_strlcpy(save.checksum, hashbuf, KEY_LEN+1);
752
753 if (tPath)
754 {
755 sh_hash_push2db (tPath, &save);
756 }
757 else
758 {
759 sh_hash_push2db (path, &save);
760 }
761 }
762
763 if (tPath)
764 sh_hash_set_visited (tPath);
765 else
766 sh_hash_set_visited (path);
767
768 if (tPath)
769 {
770 SH_FREE(tPath);
771 }
772
773 return 0;
774}
775
776static int check_for_stop (char * name)
777{
778 struct regkeylist *this = keylist;
779
780 while (this)
781 {
782 if (STOP_FALSE != this->stop)
783 {
784#ifdef HAVE_REGEX_H
785 if (0 == regexec(&(this->preg), name, 0, NULL, 0))
786 return this->stop;
787#else
788 if (0 == strcmp(this->name, name))
789 return this->stop;
790#endif
791 }
792 this = this->next;
793 }
794 return STOP_FALSE;
795}
796
797
[295]798int CheckThisSubkey (HKEY key, char * subkey, char * path, int isSingle,
799 int view)
[294]800{
801 HKEY hTestKey;
802 char * newpath;
803 size_t len;
804 int retval = -1;
805
806 len = strlen(path) + 1 + strlen(subkey) + 1;
807 newpath = SH_ALLOC(len);
808 snprintf(newpath, len, "%s\\%s", path, subkey);
809
810 /* Check for stop condition, if not single key.
811 * Set flag to isSingle = S_TRUE if we should stop here.
812 */
813 if (S_TRUE != isSingle)
814 {
815 int isStop = check_for_stop(newpath);
816
817 if (STOP_CHECK == isStop)
818 {
819 isSingle = S_TRUE;
820 }
821 else if (STOP_IGN == isStop)
822 {
823 SH_FREE(newpath);
824 return 0;
825 }
826 }
827
828 len = strlen(path) + 1 + strlen(subkey) + 1;
829 newpath = SH_ALLOC(len);
830 snprintf(newpath, len, "%s\\%s", path, subkey);
831
832 if( RegOpenKeyEx( key,
833 subkey,
834 0,
[295]835 (KEY_READ | view),
[294]836 &hTestKey) == ERROR_SUCCESS
837 )
838 {
839 QueryKey(hTestKey, newpath, len-1, isSingle);
840 RegCloseKey(hTestKey);
841 retval = 0;
842 }
843 else
844 {
845 /* Error message */
846 char * tmp = sh_util_safe_name (newpath);
847 size_t tlen = sl_strlen(tmp);
848
849 if (SL_TRUE == sl_ok_adds(64, tlen))
850 {
851 char * errbuf = SH_ALLOC(64 + tlen);
852 sl_snprintf(errbuf, 64+tlen, _("Failed to open key %s"), tmp);
853
854 SH_MUTEX_LOCK(mutex_thread_nolog);
855 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
856 errbuf, _("CheckThisSubkey"));
857 SH_MUTEX_UNLOCK(mutex_thread_nolog);
858
859 SH_FREE(errbuf);
860 }
861 sh_reg_add_ign (tmp);
862 SH_FREE(tmp);
863 }
864
865 SH_FREE(newpath);
866 return retval;
867}
868
869
870int check_key (char * key, int isSingle)
871{
872 HKEY topKey;
873 char * subkey;
874 char path[20] = "";
875 int pos = 0;
[295]876 int retval = -1;
[294]877
878 if (0 == strncmp(key, _("HKEY_CLASSES_ROOT"), 17))
879 {
880 topKey = HKEY_CLASSES_ROOT;
881 pos = 17;
882 strncpy(path, _("HKEY_CLASSES_ROOT"), sizeof(path));
883 }
884 else if (0 == strncmp(key, _("HKEY_CURRENT_USER"), 17))
885 {
886 topKey = HKEY_CURRENT_USER;
887 pos = 17;
888 strncpy(path, _("HKEY_CURRENT_USER"), sizeof(path));
889 }
890 else if (0 == strncmp(key, _("HKEY_LOCAL_MACHINE"), 18))
891 {
892 topKey = HKEY_LOCAL_MACHINE;
893 pos = 18;
894 strncpy(path, _("HKEY_LOCAL_MACHINE"), sizeof(path));
895 }
896 else if (0 == strncmp(key, _("HKEY_USERS"), 10))
897 {
898 topKey = HKEY_USERS;
899 pos = 10;
900 strncpy(path, _("HKEY_USERS"), sizeof(path));
901 }
902
903
904 if (pos > 0)
905 {
906 if (key[pos] == '\\')
907 {
908 ++pos;
909 subkey = &key[pos];
910 }
911 }
912 else
913 {
914
915 char * tmp = sh_util_safe_name_keepspace(key);
916 size_t tlen = sl_strlen(tmp);
917
918 if (SL_TRUE == sl_ok_adds(64, tlen))
919 {
920 char * errbuf = SH_ALLOC(64 + tlen);
921
922 sl_snprintf(errbuf, 64+tlen, _("Invalid key %s"), tmp);
923
924 SH_MUTEX_LOCK(mutex_thread_nolog);
925 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
926 errbuf, _("check_key"));
927 SH_MUTEX_UNLOCK(mutex_thread_nolog);
928
929 SH_FREE(errbuf);
930 }
931 SH_FREE(tmp);
932 return -1;
933 }
[295]934
935 /************************
936 if (ShCheckBothViews)
937 {
938 CheckThisSubkey (topKey, subkey, path, isSingle, KEY_WOW64_32KEY);
939 return CheckThisSubkey (topKey, subkey, path, isSingle, KEY_WOW64_64KEY);
940 }
941 *************************/
942
943 return CheckThisSubkey (topKey, subkey, path, isSingle, 0);
[294]944}
945
946/* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
947#endif
948
949/* #ifdef USE_REGISTRY_CHECK */
950#endif
951
Note: See TracBrowser for help on using the repository browser.