source: trunk/src/sh_files.c@ 6

Last change on this file since 6 was 5, checked in by rainer, 19 years ago

Fixed a stupid bug in sh_files.c; released as 2.1.1a

File size: 50.6 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 1999 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#include "config_xor.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <limits.h>
26
27#include <errno.h>
28
29/* Must be before <utime.h> on FreeBSD
30 */
31#include <sys/types.h>
32#include <unistd.h>
33
34#include <utime.h>
35
36
37#ifdef HAVE_DIRENT_H
38#include <dirent.h>
39#define NAMLEN(dirent) sl_strlen((dirent)->d_name)
40#else
41#define dirent direct
42#define NAMLEN(dirent) (dirent)->d_namlen
43#ifdef HAVE_SYS_NDIR_H
44#include <sys/ndir.h>
45#endif
46#ifdef HAVE_SYS_DIR_H
47#include <sys/dir.h>
48#endif
49#ifdef HAVE_NDIR_H
50#include <ndir.h>
51#endif
52#endif
53
54#ifdef HAVE_GLOB_H
55#include <glob.h>
56#endif
57
58#include "samhain.h"
59
60#if (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE))
61
62#include "sh_error.h"
63#include "sh_utils.h"
64#include "sh_unix.h"
65#include "sh_files.h"
66#include "sh_tiger.h"
67#include "sh_hash.h"
68#include "sh_ignore.h"
69#include "zAVLTree.h"
70
71#undef FIL__
72#define FIL__ _("sh_files.c")
73
74extern int safe_logger (int signal, int method, pid_t thepid);
75
76extern int flag_err_debug;
77extern int flag_err_info;
78
79int sh_files_reportonce(char * c)
80{
81 int i;
82 SL_ENTER(_("sh_files_reportonce"));
83 i = sh_util_flagval(c, &(sh.flag.reportonce));
84
85 SL_RETURN(i, _("sh_files_reportonce"));
86}
87
88int sh_files_fulldetail(char * c)
89{
90 int i;
91 SL_ENTER(_("sh_files_fulldetail"));
92 i = sh_util_flagval(c, &(sh.flag.fulldetail));
93
94 SL_RETURN((i), _("sh_files_fulldetail"));
95}
96
97
98typedef struct dir_struct {
99 long NumRegular;
100 long NumDirs;
101 long NumSymlinks;
102 long NumFifos;
103 long NumSockets;
104 long NumCDev;
105 long NumBDev;
106 long NumAll;
107 long TotalBytes;
108 char DirPath[PATH_MAX];
109} dir_type;
110
111typedef struct dirstack_entry {
112 char * name;
113 int class;
114 unsigned long check_mask;
115 int rdepth;
116 short checked;
117 short childs_checked;
118 short reported;
119 /* struct dirstack_entry * next; */
120} dirstack_t;
121
122
123/* the destructor
124 */
125void free_dirstack (void * inptr)
126{
127 dirstack_t * here;
128
129 SL_ENTER(_("free_dirstack"));
130 if (inptr == NULL)
131 SL_RET0(_("free_dirstack"));
132 else
133 here = (dirstack_t *) inptr;
134
135 if (here->name != NULL)
136 SH_FREE(here->name);
137 SH_FREE(here);
138 SL_RET0(_("free_dirstack"));
139}
140
141/* Function to return the key for indexing
142 * the argument
143 */
144zAVLKey zdirstack_key (void const * arg)
145{
146 const dirstack_t * sa = (const dirstack_t *) arg;
147 return (zAVLKey) sa->name;
148}
149
150
151static zAVLTree * zdirListOne = NULL;
152static zAVLTree * zdirListTwo = NULL;
153static zAVLTree * zfileList = NULL;
154
155
156static int sh_files_fullpath (char * testdir, char * d_name,
157 char * statpath);
158static int sh_files_pushdir (int class, char * str_s);
159static int sh_files_pushfile (int class, char * str_s);
160static int sh_files_checkdir (int class, int rdepth, char * dirName,
161 char * relativeName);
162static ShFileType sh_files_filecheck (int class, char * dirName,
163 char * fileName, int * reported,
164 int rsrcflag);
165
166static long MaxRecursionLevel = 0;
167
168/* set default recursion level
169 */
170int sh_files_setrecursion (char * flag_s)
171{
172 long flag = 0;
173 static int reject = 0;
174
175 SL_ENTER( _("sh_files_setrecursion"));
176
177 if (reject == 1)
178 SL_RETURN((-1), _("sh_files_setrecursion"));
179
180 if (sh.flag.opts == 1)
181 reject = 1;
182
183 if (flag_s != NULL)
184 flag = (int)(atof(flag_s));
185
186 if (flag >= 0 && flag <= 99)
187 MaxRecursionLevel = flag;
188 else
189 SL_RETURN((-1), _("sh_files_setrecursion"));
190
191 SL_RETURN((0), _("sh_files_setrecursion"));
192}
193
194
195unsigned long sh_files_chk ()
196{
197 zAVLCursor cursor;
198 ShFileType status;
199 unsigned long fcount = 0;
200
201 char * tmp = NULL;
202
203 dirstack_t * ptr;
204 char * base;
205 char * file;
206
207 SL_ENTER(_("sh_files_chk"));
208
209 for (ptr = (dirstack_t *) zAVLFirst(&cursor, zfileList); ptr;
210 ptr = (dirstack_t *) zAVLNext(&cursor))
211 {
212
213 if (sig_urgent > 0) {
214 SL_RETURN(fcount, _("sh_files_chk"));
215 }
216
217 if (ptr->checked == S_FALSE)
218 {
219 base = sh_util_basename (ptr->name);
220 file = sh_util_filename (ptr->name);
221#if defined(WITH_TPT)
222 tmp = sh_util_safe_name (ptr->name);
223#endif
224
225
226 if (flag_err_info == SL_TRUE)
227 {
228#if !defined(WITH_TPT)
229 tmp = sh_util_safe_name (ptr->name);
230#endif
231 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_CHK,
232 tmp);
233 }
234
235 BREAKEXIT(sh_files_filecheck);
236 status = sh_files_filecheck (ptr->class, base, file,
237 (int *) &(ptr->reported), 0);
238
239 TPT(( 0, FIL__, __LINE__,
240 _("msg=<filecheck complete: %s> status=<%d> reported=<%d>\n"),
241 tmp, status, ptr->reported));
242
243 if (status == SH_FILE_UNKNOWN && ptr->reported == S_FALSE)
244 {
245 TPT(( 0, FIL__, __LINE__, _("msg=<file: %s> status=<%d>\n"),
246 tmp, status));
247
248 if ( sh.flag.checkSum == SH_CHECK_INIT ||
249 sh_hash_have_it (ptr->name) >= 0)
250 {
251 if (S_FALSE == sh_ignore_chk_del(ptr->name))
252 {
253 if (0 != hashreport_missing(ptr->name,
254 (ptr->class == SH_LEVEL_ALLIGNORE) ?
255 ShDFLevel[ptr->class] :
256 ShDFLevel[SH_ERR_T_FILE])) {
257 if (tmp == NULL)
258 tmp = sh_util_safe_name (ptr->name);
259 sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE) ?
260 ShDFLevel[ptr->class] :
261 ShDFLevel[SH_ERR_T_FILE],
262 FIL__, __LINE__, 0, MSG_FI_MISS,
263 tmp);
264 }
265 }
266 }
267 else /* not there at init, and still missing */
268 {
269 if (tmp == NULL)
270 tmp = sh_util_safe_name (ptr->name);
271 sh_error_handle (SH_ERR_NOTICE,
272 FIL__, __LINE__, 0,
273 MSG_FI_FAIL,
274 tmp);
275 }
276#ifndef REPLACE_OLD
277 /* this will tell that we have seen the file, and thus prevent
278 * deletion from the database, resulting in an incomplete
279 * message when the file reappears
280 */
281 if (sh.flag.checkSum != SH_CHECK_INIT)
282 sh_hash_set_visited_true(ptr->name);
283#else
284 if (sh.flag.checkSum != SH_CHECK_INIT)
285 sh_hash_set_missing(ptr->name);
286#endif
287 if (sh.flag.reportonce == S_TRUE)
288 ptr->reported = S_TRUE;
289 }
290 else
291 {
292 /* exists (status >= 0), but was missing (reported == TRUE)
293 */
294 if (status != SH_FILE_UNKNOWN && ptr->reported == S_TRUE)
295 {
296 ptr->reported = S_FALSE;
297 }
298 /* Catchall
299 */
300 else if (status == SH_FILE_UNKNOWN)
301 {
302 /* Thu Mar 7 15:09:40 CET 2002 Make sure missing file
303 * is reported if ptr->reported == S_TRUE because the
304 * file has been added.
305 */
306 if (sh_hash_have_it (ptr->name) >= 0)
307 {
308 if (S_FALSE == sh_ignore_chk_del(ptr->name))
309 {
310 if (0 != hashreport_missing(ptr->name,
311 (ptr->class == SH_LEVEL_ALLIGNORE) ?
312 ShDFLevel[ptr->class] :
313 ShDFLevel[SH_ERR_T_FILE])) {
314 if (tmp == NULL)
315 tmp = sh_util_safe_name (ptr->name);
316 sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE)?
317 ShDFLevel[ptr->class] :
318 ShDFLevel[SH_ERR_T_FILE],
319 FIL__, __LINE__, 0, MSG_FI_MISS,
320 tmp);
321 }
322 }
323#ifndef REPLACE_OLD
324 if (sh.flag.checkSum != SH_CHECK_INIT)
325 sh_hash_set_visited_true(ptr->name);
326#else
327 /* delete from database
328 */
329 if (sh.flag.checkSum != SH_CHECK_INIT)
330 sh_hash_set_missing(ptr->name);
331#endif
332 }
333 else
334 {
335 if (tmp == NULL)
336 tmp = sh_util_safe_name (ptr->name);
337 sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, 0,
338 MSG_FI_FAIL,
339 tmp);
340 if (sh.flag.checkSum != SH_CHECK_INIT)
341 sh_hash_set_visited_true(ptr->name);
342 }
343 }
344 ++fcount;
345 }
346
347 if (tmp != NULL)
348 {
349 SH_FREE(tmp);
350 tmp = NULL;
351 }
352 SH_FREE(file);
353 SH_FREE(base);
354
355 ptr->checked = S_TRUE;
356 }
357 }
358
359 SL_RETURN(fcount, _("sh_files_chk"));
360}
361
362int sh_files_delfilestack ()
363{
364 SL_ENTER(_("sh_files_delfilestack"));
365
366 zAVLFreeTree (zfileList, free_dirstack);
367 zfileList = NULL;
368
369 SL_RETURN(0, _("sh_files_delfilestack"));
370}
371
372int sh_files_setrec_int (zAVLTree * tree)
373{
374 dirstack_t * ptr;
375 zAVLCursor avlcursor;
376
377 SL_ENTER(_("sh_files_setrec"));
378 if (tree != NULL) {
379 for (ptr = (dirstack_t *) zAVLFirst(&avlcursor, tree); ptr;
380 ptr = (dirstack_t *) zAVLNext(&avlcursor))
381 {
382 if (ptr->rdepth < (-1) || ptr->rdepth > 99)
383 {
384 ptr->rdepth = MaxRecursionLevel;
385 }
386 if (ptr->rdepth == (-1) && sh.flag.checkSum != SH_CHECK_INIT)
387 hash_remove_tree (ptr->name);
388 }
389 }
390 SL_RETURN(0, _("sh_files_setrec"));
391}
392
393int sh_files_setrec ()
394{
395 sh_files_setrec_int(zdirListOne);
396 return sh_files_setrec_int(zdirListTwo);
397}
398
399zAVLTree * sh_files_deldirstack_int (zAVLTree * ptr)
400{
401 SL_ENTER(_("sh_files_deldirstack"));
402
403 zAVLFreeTree (ptr, free_dirstack);
404
405 SL_RETURN(NULL, _("sh_files_deldirstack"));
406}
407
408int sh_files_deldirstack ()
409{
410 zdirListOne = sh_files_deldirstack_int(zdirListOne);
411 zdirListTwo = sh_files_deldirstack_int(zdirListTwo);
412 return 0;
413}
414
415void sh_files_reset()
416{
417 dirstack_t * ptr;
418 zAVLCursor avlcursor;
419
420 SL_ENTER(_("sh_files_reset"));
421
422 for (ptr = (dirstack_t *) zAVLFirst(&avlcursor, zfileList); ptr;
423 ptr = (dirstack_t *) zAVLNext(&avlcursor))
424 ptr->checked = 0;
425
426 SL_RET0(_("sh_files_reset"));
427}
428
429void sh_dirs_reset()
430{
431 dirstack_t * ptr;
432 zAVLCursor avlcursor1;
433 zAVLCursor avlcursor2;
434
435 SL_ENTER(_("sh_dirs_reset"));
436
437 for (ptr = (dirstack_t *) zAVLFirst(&avlcursor1, zdirListOne); ptr;
438 ptr = (dirstack_t *) zAVLNext(&avlcursor1))
439 ptr->checked = 0;
440
441 for (ptr = (dirstack_t *) zAVLFirst(&avlcursor2, zdirListTwo); ptr;
442 ptr = (dirstack_t *) zAVLNext(&avlcursor2))
443 ptr->checked = 0;
444
445 SL_RET0(_("sh_dirs_reset"));
446}
447
448
449int sh_files_pushfile_prelink (char * str_s)
450{
451 return (sh_files_pushfile (SH_LEVEL_PRELINK, str_s));
452}
453
454int sh_files_pushfile_user0 (char * str_s)
455{
456 return (sh_files_pushfile (SH_LEVEL_USER0, str_s));
457}
458
459
460int sh_files_pushfile_user1 (char * str_s)
461{
462 return (sh_files_pushfile (SH_LEVEL_USER1, str_s));
463}
464
465
466int sh_files_pushfile_ro (char * str_s)
467{
468 return (sh_files_pushfile (SH_LEVEL_READONLY, str_s));
469}
470
471int sh_files_pushfile_attr (char * str_s)
472{
473 return (sh_files_pushfile (SH_LEVEL_ATTRIBUTES, str_s));
474}
475
476int sh_files_pushfile_log (char * str_s)
477{
478 return (sh_files_pushfile (SH_LEVEL_LOGFILES, str_s));
479}
480
481int sh_files_pushfile_glog (char * str_s)
482{
483 return (sh_files_pushfile (SH_LEVEL_LOGGROW, str_s));
484}
485
486int sh_files_pushfile_noig (char * str_s)
487{
488 return (sh_files_pushfile (SH_LEVEL_NOIGNORE, str_s));
489}
490
491int sh_files_pushfile_allig (char * str_s)
492{
493 return (sh_files_pushfile (SH_LEVEL_ALLIGNORE, str_s));
494}
495
496
497static void sh_files_set_mask (unsigned long * mask,
498 unsigned long val, int act)
499{
500 SL_ENTER(_("sh_files_set_mask"));
501
502 if (act == 0)
503 (*mask) = val;
504 else if (act > 0)
505 (*mask) |= val;
506 else
507 (*mask) &= ~val;
508
509 SL_RET0(_("sh_files_set_mask"));
510}
511
512/* set mask(class)
513 */
514static int sh_files_parse_mask (unsigned long * mask, char * str)
515{
516 int l, i = 0, act = 0, k = 0;
517 char myword[64];
518
519 SL_ENTER(_("sh_files_parse_mask"));
520
521 if (str == NULL)
522 {
523 SL_RETURN ( (-1), _("sh_files_parse_mask"));
524 }
525 else
526 l = sl_strlen(str);
527
528 while (i < l) {
529 if (str[i] == '\0')
530 break;
531 if (str[i] == ' ' || str[i] == '\t' || str[i] == ',')
532 {
533 ++i;
534 continue;
535 }
536
537 if (str[i] == '+')
538 {
539 act = +1; ++i;
540 continue;
541 }
542 else if (str[i] == '-')
543 {
544 act = -1; ++i;
545 continue;
546 }
547 else /* a word */
548 {
549 k = 0;
550 while (k < 63 && str[i] != ' ' && str[i] != '\t' && str[i] != ','
551 && str[i] != '+' && str[i] != '-' && str[i] != '\0') {
552 myword[k] = str[i];
553 ++i; ++k;
554 }
555 myword[k] = '\0';
556
557/* checksum */
558 if (0 == strncmp(myword, _("CHK"), 3))
559 sh_files_set_mask (mask, MODI_CHK, act);
560/* link */
561 if (0 == strncmp(myword, _("LNK"), 3))
562 sh_files_set_mask (mask, MODI_LNK, act);
563/* inode */
564 if (0 == strncmp(myword, _("RDEV"), 3))
565 sh_files_set_mask (mask, MODI_RDEV, act);
566/* inode */
567 if (0 == strncmp(myword, _("INO"), 3))
568 sh_files_set_mask (mask, MODI_INO, act);
569/* user */
570 if (0 == strncmp(myword, _("USR"), 3))
571 sh_files_set_mask (mask, MODI_USR, act);
572/* group */
573 if (0 == strncmp(myword, _("GRP"), 3))
574 sh_files_set_mask (mask, MODI_GRP, act);
575/* mtime */
576 if (0 == strncmp(myword, _("MTM"), 3))
577 sh_files_set_mask (mask, MODI_MTM, act);
578/* ctime */
579 if (0 == strncmp(myword, _("CTM"), 3))
580 sh_files_set_mask (mask, MODI_CTM, act);
581/* atime */
582 if (0 == strncmp(myword, _("ATM"), 3))
583 sh_files_set_mask (mask, MODI_ATM, act);
584/* size */
585 if (0 == strncmp(myword, _("SIZ"), 3))
586 sh_files_set_mask (mask, MODI_SIZ, act);
587/* file mode */
588 if (0 == strncmp(myword, _("MOD"), 3))
589 sh_files_set_mask (mask, MODI_MOD, act);
590/* hardlinks */
591 if (0 == strncmp(myword, _("HLN"), 3))
592 sh_files_set_mask (mask, MODI_HLN, act);
593
594 }
595 }
596 SL_RETURN ( (0), _("sh_files_parse_mask"));
597}
598
599int sh_files_redef_prelink(char * str)
600{
601 return (sh_files_parse_mask(&mask_PRELINK, str));
602}
603int sh_files_redef_user0(char * str)
604{
605 return (sh_files_parse_mask(&mask_USER0, str));
606}
607int sh_files_redef_user1(char * str)
608{
609 return (sh_files_parse_mask(&mask_USER1, str));
610}
611int sh_files_redef_readonly(char * str)
612{
613 return (sh_files_parse_mask(&mask_READONLY, str));
614}
615int sh_files_redef_loggrow(char * str)
616{
617 return (sh_files_parse_mask(&mask_LOGGROW, str));
618}
619int sh_files_redef_logfiles(char * str)
620{
621 return (sh_files_parse_mask(&mask_LOGFILES, str));
622}
623int sh_files_redef_attributes(char * str)
624{
625 return (sh_files_parse_mask(&mask_ATTRIBUTES, str));
626}
627int sh_files_redef_noignore(char * str)
628{
629 return (sh_files_parse_mask(&mask_NOIGNORE, str));
630}
631int sh_files_redef_allignore(char * str)
632{
633 return (sh_files_parse_mask(&mask_ALLIGNORE, str));
634}
635
636unsigned long sh_files_maskof (int class)
637{
638 switch (class)
639 {
640 case SH_LEVEL_READONLY:
641 return (unsigned long) mask_READONLY;
642 case SH_LEVEL_ATTRIBUTES:
643 return (unsigned long) mask_ATTRIBUTES;
644 case SH_LEVEL_LOGFILES:
645 return (unsigned long) mask_LOGFILES;
646 case SH_LEVEL_LOGGROW:
647 return (unsigned long) mask_LOGGROW;
648 case SH_LEVEL_ALLIGNORE:
649 return (unsigned long) mask_ALLIGNORE;
650 case SH_LEVEL_NOIGNORE:
651 return (unsigned long) mask_NOIGNORE;
652 case SH_LEVEL_USER0:
653 return (unsigned long) mask_USER0;
654 case SH_LEVEL_USER1:
655 return (unsigned long) mask_USER1;
656 case SH_LEVEL_PRELINK:
657 return (unsigned long) mask_PRELINK;
658 default:
659 return (unsigned long) 0;
660 }
661}
662
663#ifdef HAVE_GLOB_H
664int sh_files_has_metachar (const char * str)
665{
666 SL_ENTER(_("sh_files_has_metachar"));
667 if (NULL != strchr(str, '*'))
668 SL_RETURN(1, _("sh_files_has_metachar"));
669 else if (NULL != strchr(str, '?'))
670 SL_RETURN(1, _("sh_files_has_metachar"));
671 else if (NULL != (strchr(str, '[')))
672 SL_RETURN(1, _("sh_files_has_metachar"));
673 else
674 SL_RETURN(0, _("sh_files_has_metachar"));
675}
676
677
678int sh_files_globerr (const char * epath, int errnum)
679{
680 char * p;
681
682 SL_ENTER(_("sh_files_globerr"));
683
684 p = sh_util_safe_name (epath);
685 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, errnum, MSG_FI_GLOB,
686 sh_error_message (errnum), p);
687 SH_FREE(p);
688
689 SL_RETURN(0, _("sh_files_globerr"));
690}
691
692/* #ifdef HAVE_GLOB_H
693 */
694#endif
695
696int sh_files_push_file_int (int class, const char * str_s, int len)
697{
698 dirstack_t * new_item_ptr;
699 char * fileName;
700 int ret;
701
702 SL_ENTER(_("sh_files_push_file_int"));
703
704 fileName = SH_ALLOC(len+1);
705 sl_strlcpy(fileName, str_s, len+1);
706
707 new_item_ptr = (dirstack_t *) SH_ALLOC (sizeof(dirstack_t));
708
709 new_item_ptr->name = fileName;
710 new_item_ptr->class = class;
711 new_item_ptr->check_mask = sh_files_maskof(class);
712 new_item_ptr->rdepth = 0;
713 new_item_ptr->checked = S_FALSE;
714 new_item_ptr->reported = S_FALSE;
715 new_item_ptr->childs_checked = S_FALSE;
716
717 if (zfileList == NULL)
718 {
719 zfileList = zAVLAllocTree (zdirstack_key);
720 if (zfileList == NULL)
721 {
722 (void) safe_logger (0, 0, getpid());
723 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
724 }
725 }
726
727 ret = zAVLInsert (zfileList, new_item_ptr);
728
729 if (-1 == ret)
730 {
731 (void) safe_logger (0, 0, getpid());
732 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
733 }
734 if (3 == ret)
735 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
736 fileName);
737
738 SL_RETURN(0, _("sh_files_push_file_int"));
739}
740
741
742static int sh_files_pushfile (int class, char * str_s)
743{
744 char * tmp;
745 int len;
746#ifdef HAVE_GLOB_H
747 glob_t pglob;
748 int globstatus = -1;
749 unsigned int gloop;
750#endif
751
752 static int reject = 0;
753
754 SL_ENTER(_("sh_files_pushfile"));
755
756 if (reject == 1)
757 SL_RETURN((-1),_("sh_files_pushfile"));
758
759 /* if we push a filename from the command line, make sure it
760 * is the only one -- and will stay the only one
761 */
762 if (sh.flag.opts == 1)
763 {
764 sh_files_delfilestack ();
765 sh_files_deldirstack ();
766 reject = 1;
767 }
768
769 if (str_s == NULL)
770 SL_RETURN((-1),_("sh_files_pushfile"));
771
772 len = sl_strlen(str_s);
773
774 if (len >= PATH_MAX)
775 {
776 /* Name too long
777 */
778 tmp = sh_util_safe_name (str_s);
779 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_2LONG,
780 tmp);
781 SH_FREE(tmp);
782 SL_RETURN((-1),_("sh_files_pushfile"));
783 }
784 else if (len < 1)
785 {
786 /* Should not happen (str_s == NULL caught further above)
787 */
788 SL_RETURN((-1),_("sh_files_pushfile"));
789 }
790 else if (str_s[0] != '/')
791 {
792 /* Not an absolute path
793 */
794 tmp = sh_util_safe_name (str_s);
795 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_NOPATH,
796 tmp);
797 SH_FREE(tmp);
798 SL_RETURN((-1),_("sh_files_pushfile"));
799 }
800 else
801 {
802 /* remove a terminating '/', take care of the
803 * special case of the root directory.
804 */
805 if (str_s[len-1] == '/' && len > 1)
806 {
807 str_s[len-1] = '\0';
808 --len;
809 }
810
811 }
812
813#ifdef HAVE_GLOB_H
814 if (0 == sh_files_has_metachar(str_s))
815 {
816 sh_files_push_file_int (class, str_s, len);
817 }
818 else
819 {
820 pglob.gl_offs = 0;
821 globstatus = glob (str_s, 0, sh_files_globerr, &pglob);
822
823 if (globstatus == 0 && pglob.gl_pathc > 0)
824 {
825 for (gloop = 0; gloop < (unsigned int) pglob.gl_pathc; ++gloop)
826 sh_files_push_file_int (class, pglob.gl_pathv[gloop],
827 sl_strlen(pglob.gl_pathv[gloop]));
828 }
829 else
830 {
831 tmp = sh_util_safe_name (str_s);
832
833 if (pglob.gl_pathc == 0
834#ifdef GLOB_NOMATCH
835 || globstatus == GLOB_NOMATCH
836#endif
837 )
838 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
839 globstatus, MSG_FI_GLOB,
840 _("No matches found"), tmp);
841#ifdef GLOB_NOSPACE
842 else if (globstatus == GLOB_NOSPACE)
843 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
844 globstatus, MSG_FI_GLOB,
845 _("Out of memory"), tmp);
846#endif
847#ifdef GLOB_ABORTED
848 else if (globstatus == GLOB_ABORTED)
849 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
850 globstatus, MSG_FI_GLOB,
851 _("Read error"), tmp);
852#endif
853 else
854 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
855 globstatus, MSG_FI_GLOB,
856 _("Unknown error"), tmp);
857
858 SH_FREE(tmp);
859
860 }
861
862 globfree(&pglob);
863 }
864
865#else
866 sh_files_push_file_int (class, str_s, len);
867#endif
868
869 SL_RETURN((0),_("sh_files_pushfile"));
870}
871
872
873/* ------ directories ----- */
874
875int sh_files_is_allignore_int (char * str, zAVLTree * tree)
876{
877 dirstack_t * ptr;
878
879 SL_ENTER(_("sh_files_is_allignore"));
880
881 if (tree)
882 {
883 ptr = zAVLSearch(tree, str);
884 if (ptr)
885 {
886 if (ptr->class == SH_LEVEL_ALLIGNORE)
887 SL_RETURN( 1, _("sh_files_is_allignore"));
888 else
889 SL_RETURN( 0, _("sh_files_is_allignore"));
890 }
891 }
892 SL_RETURN( 0, _("sh_files_is_allignore"));
893}
894
895int sh_files_is_allignore (char * str)
896{
897 if (1 == sh_files_is_allignore_int(str, zdirListOne))
898 return 1;
899 if (NULL == zdirListTwo)
900 return 0;
901 return sh_files_is_allignore_int(str, zdirListTwo);
902}
903
904unsigned long sh_dirs_chk (int which)
905{
906 zAVLTree * tree;
907 zAVLCursor cursor;
908 dirstack_t * ptr;
909 dirstack_t * dst_ptr;
910 int status;
911 unsigned long dcount = 0;
912 char * tmp;
913
914 SL_ENTER(_("sh_dirs_chk"));
915
916 if (which == 1)
917 tree = zdirListOne;
918 else
919 tree = zdirListTwo;
920
921 for (ptr = (dirstack_t *) zAVLFirst(&cursor, tree); ptr;
922 ptr = (dirstack_t *) zAVLNext(&cursor))
923 {
924 if (sig_urgent > 0) {
925 SL_RETURN(dcount, _("sh_dirs_chk"));
926 }
927
928 if (ptr->checked == S_FALSE)
929 {
930 /* 28 Aug 2001 check the top level directory
931 */
932 status = S_FALSE;
933 dst_ptr = zAVLSearch(zfileList, ptr->name);
934 if (dst_ptr)
935 {
936 if (dst_ptr->checked == S_FALSE)
937 {
938 BREAKEXIT(sh_files_filecheck);
939 sh_files_filecheck (dst_ptr->class, ptr->name,
940 NULL, &status, 0);
941 dst_ptr->checked = S_TRUE;
942 status = S_TRUE;
943 }
944 else
945 {
946 status = S_TRUE;
947 }
948 }
949
950 if (status == S_FALSE)
951 sh_files_filecheck (ptr->class, ptr->name, NULL, &status, 0);
952
953 BREAKEXIT(sh_files_checkdir);
954 status = sh_files_checkdir (ptr->class, ptr->rdepth, ptr->name,
955 ptr->name);
956
957 if (status < 0 && ptr->reported == S_FALSE)
958 {
959 /* directory is missing
960 */
961 if (S_FALSE == sh_ignore_chk_del(ptr->name))
962 {
963 if (0 != hashreport_missing(ptr->name,
964 (ptr->class == SH_LEVEL_ALLIGNORE) ?
965 ShDFLevel[ptr->class] :
966 ShDFLevel[SH_ERR_T_DIR])) {
967 tmp = sh_util_safe_name (ptr->name);
968 sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE) ?
969 ShDFLevel[ptr->class] :
970 ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__,
971 0, MSG_FI_MISS, tmp);
972 SH_FREE(tmp);
973 }
974 }
975 if (sh.flag.reportonce == S_TRUE)
976 ptr->reported = S_TRUE;
977 }
978 else
979 {
980 /* exists (status >= 0), but was missing (reported == TRUE)
981 */
982 if (status >= 0 && ptr->reported == S_TRUE)
983 {
984 ptr->reported = S_FALSE;
985#if 0
986 /* obsoleted (really?) by the mandatory sh_files_filecheck()
987 * above, which will catch missing directories anyway
988 */
989 tmp = sh_util_safe_name (ptr->name);
990 sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE) ?
991 ShDFLevel[ptr->class] :
992 ShDFLevel[SH_ERR_T_DIR],
993 FIL__, __LINE__, 0, MSG_FI_ADD,
994 tmp);
995 SH_FREE(tmp);
996#endif
997 }
998 else if (status == SH_FILE_UNKNOWN)
999 {
1000 /* catchall
1001 */
1002 tmp = sh_util_safe_name (ptr->name);
1003 sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, 0,
1004 MSG_FI_FAIL,
1005 tmp);
1006 SH_FREE(tmp);
1007 if (sh.flag.checkSum != SH_CHECK_INIT)
1008 sh_hash_set_visited_true(ptr->name);
1009 }
1010
1011 ++dcount;
1012 }
1013 ptr->checked = S_TRUE;
1014 }
1015
1016 if (sig_urgent > 0) {
1017 SL_RETURN(dcount, _("sh_dirs_chk"));
1018 }
1019
1020 }
1021 SL_RETURN(dcount, _("sh_dirs_chk"));
1022}
1023
1024int sh_files_pushdir_prelink (char * str_s)
1025{
1026 return (sh_files_pushdir (SH_LEVEL_PRELINK, str_s));
1027}
1028
1029int sh_files_pushdir_user0 (char * str_s)
1030{
1031 return (sh_files_pushdir (SH_LEVEL_USER0, str_s));
1032}
1033
1034int sh_files_pushdir_user1 (char * str_s)
1035{
1036 return (sh_files_pushdir (SH_LEVEL_USER1, str_s));
1037}
1038
1039int sh_files_pushdir_attr (char * str_s)
1040{
1041 return (sh_files_pushdir (SH_LEVEL_ATTRIBUTES, str_s));
1042}
1043
1044int sh_files_pushdir_ro (char * str_s)
1045{
1046 return (sh_files_pushdir (SH_LEVEL_READONLY, str_s));
1047}
1048
1049int sh_files_pushdir_log (char * str_s)
1050{
1051 return (sh_files_pushdir (SH_LEVEL_LOGFILES, str_s));
1052}
1053
1054int sh_files_pushdir_glog (char * str_s)
1055{
1056 return (sh_files_pushdir (SH_LEVEL_LOGGROW, str_s));
1057}
1058
1059int sh_files_pushdir_noig (char * str_s)
1060{
1061 return (sh_files_pushdir (SH_LEVEL_NOIGNORE, str_s));
1062}
1063
1064int sh_files_pushdir_allig (char * str_s)
1065{
1066 return (sh_files_pushdir (SH_LEVEL_ALLIGNORE, str_s));
1067}
1068
1069static int which_dirList = 1;
1070
1071int set_dirList (int which)
1072{
1073 if (which == 2)
1074 which_dirList = 2;
1075 else
1076 which_dirList = 1;
1077 return 0;
1078}
1079
1080int sh_files_push_dir_int (int class, char * tail, int len, int rdepth)
1081{
1082 zAVLTree * tree;
1083 dirstack_t * new_item_ptr;
1084 char * dirName;
1085 int ret;
1086
1087 SL_ENTER(_("sh_files_push_dir_int"));
1088
1089 dirName = SH_ALLOC(len+1);
1090 sl_strlcpy(dirName, tail, len+1);
1091
1092 new_item_ptr = (dirstack_t * ) SH_ALLOC (sizeof(dirstack_t));
1093
1094 new_item_ptr->name = dirName;
1095 new_item_ptr->class = class;
1096 new_item_ptr->check_mask = sh_files_maskof(class);
1097 new_item_ptr->rdepth = rdepth;
1098 new_item_ptr->checked = S_FALSE;
1099 new_item_ptr->reported = S_FALSE;
1100 new_item_ptr->childs_checked = S_FALSE;
1101
1102 if (which_dirList == 1)
1103 {
1104 tree = zdirListOne;
1105 }
1106 else
1107 {
1108 tree = zdirListTwo;
1109 }
1110
1111 if (tree == NULL)
1112 {
1113 tree = zAVLAllocTree (zdirstack_key);
1114 if (tree == NULL)
1115 {
1116 (void) safe_logger (0, 0, getpid());
1117 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
1118 }
1119 if (which_dirList == 1)
1120 zdirListOne = tree;
1121 else
1122 zdirListTwo = tree;
1123 }
1124
1125 ret = zAVLInsert (tree, new_item_ptr);
1126
1127 if (-1 == ret)
1128 {
1129 (void) safe_logger (0, 0, getpid());
1130 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
1131 }
1132 if (3 == ret)
1133 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
1134 dirName);
1135
1136 SL_RETURN(0, _("sh_files_push_dir_int"));
1137}
1138
1139static int sh_files_pushdir (int class, char * str_s)
1140{
1141 char * tmp;
1142 int len;
1143 int rdepth = 0;
1144 char * tail = NULL;
1145
1146#ifdef HAVE_GLOB_H
1147 glob_t pglob;
1148 int globstatus = -1;
1149 unsigned int gloop;
1150#endif
1151
1152 SL_ENTER(_("sh_files_pushdir"));
1153
1154 if (sh.flag.opts == 1) {
1155 sh_files_delfilestack ();
1156 sh_files_deldirstack ();
1157 }
1158
1159 if (str_s == NULL)
1160 SL_RETURN((-1), _("sh_files_pushdir"));
1161
1162
1163 if (str_s[0] != '/')
1164 {
1165 rdepth = strtol(str_s, &tail, 10);
1166 if (tail == str_s)
1167 SL_RETURN((-1), _("sh_files_pushdir"));
1168 }
1169 else
1170 tail = str_s;
1171
1172
1173 if (rdepth < (-1) || tail == str_s || rdepth > 99)
1174 rdepth = (-2);
1175
1176 len = sl_strlen(tail);
1177
1178 if (len >= PATH_MAX)
1179 {
1180 tmp = sh_util_safe_name (tail);
1181 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_2LONG,
1182 tmp);
1183 SH_FREE(tmp);
1184 SL_RETURN((-1), _("sh_files_pushdir"));
1185 }
1186 else if (len < 1)
1187 {
1188 SL_RETURN((-1), _("sh_files_pushdir"));
1189 }
1190 else if (tail[0] != '/')
1191 {
1192 tmp = sh_util_safe_name (tail);
1193 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_NOPATH,
1194 tmp);
1195 SH_FREE(tmp);
1196 SL_RETURN((-1), _("sh_files_pushdir"));
1197 }
1198 else
1199 {
1200
1201 if (tail[len-1] == '/' && len > 1)
1202 {
1203 tail[len-1] = '\0';
1204 --len;
1205 }
1206
1207 }
1208
1209#ifdef HAVE_GLOB_H
1210 if (0 == sh_files_has_metachar(tail))
1211 {
1212 sh_files_push_dir_int (class, tail, len, rdepth);
1213 }
1214 else
1215 {
1216 pglob.gl_offs = 0;
1217 globstatus = glob (tail, 0, sh_files_globerr, &pglob);
1218
1219 if (globstatus == 0 && pglob.gl_pathc > 0)
1220 {
1221 for (gloop = 0; gloop < (unsigned int) pglob.gl_pathc; ++gloop)
1222 sh_files_push_dir_int (class,
1223 pglob.gl_pathv[gloop],
1224 sl_strlen(pglob.gl_pathv[gloop]),
1225 rdepth);
1226 }
1227 else
1228 {
1229 tmp = sh_util_safe_name (tail);
1230
1231 if (pglob.gl_pathc == 0
1232#ifdef GLOB_NOMATCH
1233 || globstatus == GLOB_NOMATCH
1234#endif
1235 )
1236 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
1237 globstatus, MSG_FI_GLOB,
1238 _("No matches found"), tmp);
1239#ifdef GLOB_NOSPACE
1240 else if (globstatus == GLOB_NOSPACE)
1241 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
1242 globstatus, MSG_FI_GLOB,
1243 _("Out of memory"), tmp);
1244#endif
1245#ifdef GLOB_ABORTED
1246 else if (globstatus == GLOB_ABORTED)
1247 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
1248 globstatus, MSG_FI_GLOB,
1249 _("Read error"), tmp);
1250#endif
1251 else
1252 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
1253 globstatus, MSG_FI_GLOB,
1254 _("Unknown error"), tmp);
1255 SH_FREE(tmp);
1256 }
1257
1258 globfree(&pglob);
1259 }
1260#else
1261 sh_files_push_dir_int (class, tail, len, rdepth);
1262#endif
1263
1264 SL_RETURN((0), _("sh_files_pushdir"));
1265}
1266
1267struct sh_dirent {
1268 /* char sh_d_name[NAME_MAX + 2]; */
1269 char * sh_d_name;
1270 struct sh_dirent * next;
1271};
1272
1273static void kill_sh_dirlist (struct sh_dirent * dirlist)
1274{
1275 struct sh_dirent * this;
1276
1277 while (dirlist)
1278 {
1279 this = dirlist->next;
1280 SH_FREE(dirlist->sh_d_name);
1281 SH_FREE(dirlist);
1282 dirlist = this;
1283 }
1284 return;
1285}
1286
1287/* -- add an entry to a directory listing
1288 */
1289static struct sh_dirent * addto_sh_dirlist (struct dirent * thisEntry,
1290 struct sh_dirent * dirlist)
1291{
1292 struct sh_dirent * this;
1293 int i;
1294
1295 if (thisEntry == NULL)
1296 return dirlist;
1297
1298 i = sl_strlen(thisEntry->d_name);
1299 if (i == 0)
1300 return dirlist;
1301 ++i;
1302
1303 this = SH_ALLOC(sizeof(struct sh_dirent));
1304 if (!this)
1305 return dirlist;
1306
1307 this->sh_d_name = SH_ALLOC(i);
1308 sl_strlcpy(this->sh_d_name, thisEntry->d_name, i);
1309
1310 this->next = dirlist;
1311 return this;
1312}
1313
1314static int sh_check_hardlinks = S_TRUE;
1315
1316/* Simply sets our boolean as to whether this check is active
1317 */
1318int sh_files_check_hardlinks (char * opt)
1319{
1320 int i;
1321 SL_ENTER(_("sh_files_check_hardlinks"));
1322 i = sh_util_flagval(opt, &sh_check_hardlinks);
1323 SL_RETURN(i, _("sh_files_check_hardlinks"));
1324}
1325
1326struct sh_hle_struct {
1327 long offset;
1328 char * path;
1329 struct sh_hle_struct * next;
1330};
1331
1332static struct sh_hle_struct * sh_hl_exc = NULL;
1333
1334int sh_files_hle_reg (char * str)
1335{
1336 long offset;
1337 size_t len;
1338 char * path;
1339
1340 struct sh_hle_struct * tmp = sh_hl_exc;
1341
1342 SL_ENTER(_("sh_files_hle_reg"));
1343
1344 /* Free the linked list if called with NULL argument
1345 */
1346 if (str == NULL)
1347 {
1348 while (tmp)
1349 {
1350 sh_hl_exc = tmp->next;
1351 SH_FREE(tmp->path);
1352 SH_FREE(tmp);
1353 tmp = sh_hl_exc;
1354 }
1355 sh_hl_exc = NULL;
1356 SL_RETURN(0, _("sh_files_hle_reg"));
1357 }
1358
1359 /* We expect 'offset:/path'
1360 */
1361 offset = strtol(str, &path, 0);
1362 if ((path == NULL) || (*path == '\0') || (*path != ':') || (path[1] != '/'))
1363 {
1364 SL_RETURN(-1, _("sh_files_hle_reg"));
1365 }
1366 ++path;
1367 len = 1 + sl_strlen(path);
1368
1369 tmp = SH_ALLOC(sizeof(struct sh_hle_struct));
1370 tmp->path = SH_ALLOC(len);
1371 sl_strlcpy (tmp->path, path, len);
1372 tmp->offset = offset;
1373 tmp->next = sh_hl_exc;
1374 sh_hl_exc = tmp;
1375
1376 SL_RETURN(0, _("sh_files_hle_reg"));
1377}
1378
1379#if !defined(HOST_IS_DARWIN)
1380static int sh_files_hle_test (int offset, char * path)
1381{
1382 struct sh_hle_struct * tmp = sh_hl_exc;
1383
1384 SL_ENTER(_("sh_files_hle_reg"));
1385
1386 while(tmp)
1387 {
1388 if ((offset == tmp->offset) && (0 == strcmp(path, tmp->path)))
1389 {
1390 SL_RETURN(0, _("sh_files_hle_test"));
1391 }
1392 tmp = tmp->next;
1393 }
1394 SL_RETURN(-1, _("sh_files_hle_test"));
1395}
1396#endif
1397
1398/* -- check a single directory and its content
1399 */
1400static int sh_files_checkdir (int iclass, int idepth, char * iname,
1401 char * relativeName)
1402{
1403 struct sh_dirent * dirlist = NULL;
1404 struct sh_dirent * dirlist_orig = NULL;
1405
1406 DIR * thisDir = NULL;
1407 struct dirent * thisEntry;
1408 int status;
1409 int dummy = S_FALSE;
1410 dir_type theDir;
1411 ShFileType checkit;
1412
1413
1414 file_type theFile;
1415 char * tmpname;
1416 char * tmpcat;
1417
1418 int rdepth = 0;
1419 int class = 0;
1420 int rdepth_next;
1421 int class_next;
1422 int file_class_next;
1423
1424 int checked_flag = S_FALSE;
1425 int cchecked_flag = S_FALSE;
1426
1427 dirstack_t * dst_ptr;
1428
1429 int hardlink_num = 0;
1430
1431
1432 SL_ENTER(_("sh_files_checkdir"));
1433
1434 if (sig_urgent > 0) {
1435 SL_RETURN((0), _("sh_files_checkdir"));
1436 }
1437
1438 if (iname == NULL || idepth < (-1))
1439 SL_RETURN((-1), _("sh_files_checkdir"));
1440
1441 if (idepth < 0)
1442 {
1443 /* hash_remove_tree (iname); */
1444 SL_RETURN((0), _("sh_files_checkdir"));
1445 }
1446
1447 rdepth = idepth;
1448 class = iclass;
1449
1450 tmpname = sh_util_safe_name (iname);
1451
1452 /* ---- check for obscure name ----
1453 */
1454 if (iclass != SH_LEVEL_ALLIGNORE)
1455 {
1456 sh_util_obscurename (ShDFLevel[SH_ERR_T_NAME], iname, S_TRUE);
1457 }
1458
1459 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_CHK,
1460 tmpname);
1461
1462 /* ---- check input ----
1463 */
1464 if ( sl_strlen(iname) >= PATH_MAX)
1465 {
1466 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
1467 MSG_FI_2LONG,
1468 tmpname);
1469 SH_FREE(tmpname);
1470 SL_RETURN((-1), _("sh_files_checkdir"));
1471 }
1472
1473 /* ---- check for absolute path ---- */
1474 if ( iname[0] != '/')
1475 {
1476 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
1477 MSG_FI_NOPATH,
1478 tmpname);
1479 SH_FREE(tmpname);
1480 SL_RETURN((-1), _("sh_files_checkdir"));
1481 }
1482
1483
1484 /* ---- stat the directory ----
1485 */
1486 sl_strlcpy (theFile.fullpath, iname, PATH_MAX);
1487
1488 (void) relativeName;
1489 status = sh_unix_getinfo (ShDFLevel[SH_ERR_T_DIR],
1490 iname,
1491 &theFile, NULL, iclass);
1492
1493 if ((sig_termfast == 1) || (sig_terminate == 1))
1494 {
1495 SL_RETURN((0), _("sh_files_checkdir"));
1496 }
1497
1498 if (status == -1)
1499 {
1500 SH_FREE(tmpname);
1501 SL_RETURN((-1), _("sh_files_checkdir"));
1502 }
1503
1504 if (theFile.c_mode[0] != 'd')
1505 {
1506 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
1507 MSG_FI_NODIR,
1508 tmpname);
1509 SH_FREE(tmpname);
1510 SL_RETURN((-1), _("sh_files_checkdir"));
1511 }
1512
1513 hardlink_num = theFile.hardlinks;
1514
1515
1516 /* ---- open directory for reading ----
1517 *
1518 * opendir() will fail with ENOTDIR if the path has been changed
1519 * to a non-directory in between lstat() and opendir().
1520 */
1521 thisDir = opendir (iname);
1522
1523 if (thisDir == NULL)
1524 {
1525 status = errno;
1526 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
1527 MSG_E_OPENDIR,
1528 sh_error_message (status), tmpname);
1529 SH_FREE(tmpname);
1530
1531 SL_RETURN((-1), _("sh_files_checkdir"));
1532 }
1533
1534 theDir.NumRegular = 0;
1535 theDir.NumDirs = 0;
1536 theDir.NumSymlinks = 0;
1537 theDir.NumFifos = 0;
1538 theDir.NumSockets = 0;
1539 theDir.NumCDev = 0;
1540 theDir.NumBDev = 0;
1541 theDir.NumAll = 0;
1542 theDir.TotalBytes = 0;
1543 sl_strlcpy (theDir.DirPath, iname, PATH_MAX);
1544
1545
1546 /* ---- read ----
1547 */
1548 do {
1549 thisEntry = readdir (thisDir);
1550 if (thisEntry != NULL)
1551 {
1552 ++theDir.NumAll;
1553 if (sl_strcmp (thisEntry->d_name, ".") == 0)
1554 {
1555 ++theDir.NumDirs;
1556 continue;
1557 }
1558 if (sl_strcmp (thisEntry->d_name, "..") == 0)
1559 {
1560 ++theDir.NumDirs;
1561 continue;
1562 }
1563 dirlist = addto_sh_dirlist (thisEntry, dirlist);
1564 }
1565 } while (thisEntry != NULL);
1566
1567 closedir (thisDir);
1568
1569 ++sh.statistics.dirs_checked;
1570
1571 dirlist_orig = dirlist;
1572
1573 do {
1574
1575 /* If the directory is empty, dirlist = NULL
1576 */
1577 if (!dirlist)
1578 break;
1579
1580 if (sig_termfast == 1)
1581 {
1582 SL_RETURN((0), _("sh_files_checkdir"));
1583 }
1584
1585 BREAKEXIT(sh_derr);
1586 if (0 == (rand() % 5))
1587 (void) sh_derr();
1588
1589 /* ---- Check the file. ----
1590 */
1591 tmpcat = SH_ALLOC(PATH_MAX);
1592 sl_strlcpy(tmpcat, iname, PATH_MAX);
1593 if (sl_strlen(tmpcat) > 1 || tmpcat[0] != '/')
1594 sl_strlcat(tmpcat, "/", PATH_MAX);
1595 sl_strlcat(tmpcat, dirlist->sh_d_name, PATH_MAX);
1596
1597 rdepth_next = rdepth - 1;
1598 class_next = class;
1599 file_class_next = class;
1600 checked_flag = -1;
1601 cchecked_flag = -1;
1602
1603 /* Wed Aug 24 2005 compare against dirListOne, dirListTwo
1604 * this fixes the problem that the directory special file
1605 * is checked with the policy of the parent directory
1606 */
1607 dst_ptr = (dirstack_t *) zAVLSearch(zdirListOne, tmpcat);
1608
1609 if (dst_ptr)
1610 {
1611 /* Tue Aug 6 22:13:27 CEST 2002 introduce file_class_next
1612 * this fixes the problem that a policy for the directory
1613 * inode erroneously becomes a policy for the directory itself.
1614 */
1615 file_class_next = dst_ptr->class;
1616 checked_flag = dst_ptr->checked;
1617 cchecked_flag = dst_ptr->childs_checked;
1618 }
1619
1620 if (checked_flag == -1)
1621 {
1622 dst_ptr = (dirstack_t *) zAVLSearch(zdirListTwo, tmpcat);
1623
1624 if (dst_ptr)
1625 {
1626 /* Tue Aug 6 22:13:27 CEST 2002 introduce file_class_next
1627 * this fixes the problem that a policy for the directory
1628 * inode erroneously becomes a policy for the directory itself.
1629 */
1630 file_class_next = dst_ptr->class;
1631 checked_flag = dst_ptr->checked;
1632 cchecked_flag = dst_ptr->childs_checked;
1633 }
1634 }
1635
1636 dst_ptr = (dirstack_t *) zAVLSearch(zfileList, tmpcat);
1637
1638 if (dst_ptr)
1639 {
1640 /* Tue Aug 6 22:13:27 CEST 2002 introduce file_class_next
1641 * this fixes the problem that a policy for the directory
1642 * inode erroneously becomes a policy for the directory itself.
1643 */
1644 file_class_next = dst_ptr->class;
1645 checked_flag = dst_ptr->checked;
1646 cchecked_flag = dst_ptr->childs_checked;
1647 }
1648
1649 /* ---- Has been checked already. ----
1650 */
1651 if (checked_flag == S_TRUE && cchecked_flag == S_TRUE)
1652 {
1653 /* Mar 11 2004 get ftype for complete directory count
1654 */
1655 checkit = sh_unix_get_ftype(tmpcat);
1656 if (checkit == SH_FILE_DIRECTORY)
1657 {
1658 ++theDir.NumDirs;
1659 }
1660 SH_FREE(tmpcat);
1661 dirlist = dirlist->next;
1662 continue;
1663 }
1664
1665 /* --- May be true, false, or not found. ---
1666 */
1667 if (checked_flag == S_TRUE)
1668 {
1669 /* -- need only the file type --
1670 */
1671 checkit = sh_unix_get_ftype(tmpcat);
1672 }
1673 else
1674 {
1675 /* -- need to check the file itself --
1676 */
1677 if (dst_ptr && sh.flag.reportonce == S_TRUE)
1678 dummy = dst_ptr->reported;
1679
1680 checkit = sh_files_filecheck (file_class_next,
1681 iname,
1682 dirlist->sh_d_name,
1683 &dummy, 0);
1684
1685 if (dst_ptr && checked_flag == S_FALSE)
1686 dst_ptr->checked = S_TRUE;
1687 /* Thu Mar 7 15:09:40 CET 2002 Propagate the 'reported' flag
1688 */
1689 if (dst_ptr && sh.flag.reportonce == S_TRUE)
1690 dst_ptr->reported = dummy;
1691 }
1692
1693 if (checkit == SH_FILE_REGULAR)
1694 ++theDir.NumRegular;
1695
1696 else if (checkit == SH_FILE_DIRECTORY)
1697 {
1698 ++theDir.NumDirs;
1699 if (rdepth_next >= 0 && cchecked_flag != S_TRUE)
1700 {
1701 rdepth_next = rdepth - 1;
1702
1703 /* check whether the new directory is in the
1704 * list with a recursion depth already defined
1705 */
1706 checked_flag = -1;
1707 cchecked_flag = -1;
1708
1709 dst_ptr = (dirstack_t *) zAVLSearch(zdirListOne, tmpcat);
1710
1711 if (dst_ptr)
1712 {
1713 TPT((0, FIL__, __LINE__,
1714 _("msg=<%s -> recursion depth %d\n>"),
1715 dst_ptr->name, dst_ptr->rdepth));
1716 rdepth_next = dst_ptr->rdepth;
1717 class_next = dst_ptr->class;
1718 /* 28. Aug 2001 reversed
1719 */
1720 cchecked_flag = dst_ptr->childs_checked;
1721 checked_flag = dst_ptr->checked;
1722 }
1723
1724 if (checked_flag == -1)
1725 {
1726 dst_ptr = (dirstack_t *) zAVLSearch(zdirListTwo, tmpcat);
1727
1728 if (dst_ptr)
1729 {
1730 TPT((0, FIL__, __LINE__,
1731 _("msg=<%s -> recursion depth %d\n>"),
1732 dst_ptr->name, dst_ptr->rdepth));
1733 rdepth_next = dst_ptr->rdepth;
1734 class_next = dst_ptr->class;
1735 /* 28. Aug 2001 reversed
1736 */
1737 cchecked_flag = dst_ptr->childs_checked;
1738 checked_flag = dst_ptr->checked;
1739 }
1740 }
1741
1742 if (cchecked_flag == S_FALSE)
1743 {
1744 sh_files_checkdir (class_next, rdepth_next, tmpcat,
1745 dirlist->sh_d_name);
1746 dst_ptr->childs_checked = S_TRUE;
1747 }
1748 else if (checked_flag == -1)
1749 sh_files_checkdir (class_next, rdepth_next, tmpcat,
1750 dirlist->sh_d_name);
1751
1752 }
1753 }
1754
1755 else if (checkit == SH_FILE_SYMLINK) ++theDir.NumSymlinks;
1756 else if (checkit == SH_FILE_FIFO) ++theDir.NumFifos;
1757 else if (checkit == SH_FILE_SOCKET) ++theDir.NumSockets;
1758 else if (checkit == SH_FILE_CDEV) ++theDir.NumCDev;
1759 else if (checkit == SH_FILE_BDEV) ++theDir.NumBDev;
1760
1761 SH_FREE(tmpcat);
1762
1763 if ((sig_termfast == 1) || (sig_terminate == 1))
1764 {
1765 SL_RETURN((0), _("sh_files_checkdir"));
1766 }
1767
1768 dirlist = dirlist->next;
1769
1770 } while (dirlist != NULL);
1771
1772 if (flag_err_info == SL_TRUE)
1773 {
1774 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DSUM,
1775 theDir.NumDirs,
1776 theDir.NumRegular,
1777 theDir.NumSymlinks,
1778 theDir.NumFifos,
1779 theDir.NumSockets,
1780 theDir.NumCDev,
1781 theDir.NumBDev);
1782 }
1783
1784 kill_sh_dirlist (dirlist_orig);
1785
1786#if !defined(HOST_IS_DARWIN)
1787 /*
1788 * Hardlink check; not done on MacOS X because of resource forks
1789 */
1790 if ((sh_check_hardlinks == S_TRUE) && (hardlink_num != theDir.NumDirs))
1791 {
1792 if (0 != sh_files_hle_test(hardlink_num-theDir.NumDirs, iname))
1793 {
1794 tmpcat = SH_ALLOC(strlen(tmpname) + 256);
1795 sl_snprintf(tmpcat, strlen(tmpname) + 256,
1796 _("%s: subdirectory count (%d) != hardlinks (%d)"),
1797 tmpname, theDir.NumDirs, hardlink_num);
1798 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
1799 MSG_E_SUBGEN, tmpcat, _("sh_files_checkdir"));
1800 SH_FREE(tmpcat);
1801 }
1802 }
1803#endif
1804
1805 SH_FREE(tmpname);
1806
1807 SL_RETURN((0), _("sh_files_checkdir"));
1808}
1809
1810int get_the_fd (SL_TICKET ticket);
1811
1812
1813static ShFileType sh_files_filecheck (int class, char * dirName,
1814 char * fileName,
1815 int * reported,
1816 int rsrcflag)
1817{
1818 /* 28 Aug 2001 allow NULL fileName
1819 */
1820 char fullpath[PATH_MAX];
1821 char fileHash[KEY_LEN + 1];
1822 int status;
1823 file_type theFile;
1824 char * tmpdir;
1825 char * tmpname;
1826 struct utimbuf utime_buf;
1827
1828 SL_ENTER(_("sh_files_filecheck"));
1829
1830 BREAKEXIT(sh_derr);
1831 if (0 == (rand() % 2))
1832 (void) sh_derr();
1833
1834 /* fileName may be NULL if this is a directory
1835 */
1836 if (dirName == NULL /* || fileName == NULL */)
1837 {
1838 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_NULL);
1839 SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
1840 }
1841
1842 if ((fileName != NULL) && (class != SH_LEVEL_ALLIGNORE) &&
1843 (0 != sh_util_obscurename (ShDFLevel[SH_ERR_T_NAME],
1844 fileName, S_FALSE)))
1845 {
1846 if ((dirName != NULL) && (dirName[0] == '/') && (dirName[1] == '\0'))
1847 {
1848 tmpname = sh_util_safe_name (fileName);
1849 sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, 0,
1850 MSG_FI_OBSC2,
1851 "", tmpname);
1852 SH_FREE(tmpname);
1853 }
1854 else
1855 {
1856 tmpdir = sh_util_safe_name (dirName);
1857 tmpname = sh_util_safe_name (fileName);
1858 sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, 0,
1859 MSG_FI_OBSC2,
1860 tmpdir, tmpname);
1861 SH_FREE(tmpname);
1862 SH_FREE(tmpdir);
1863 }
1864 }
1865
1866 /* sh_files_fullpath accepts NULL fileName
1867 */
1868 if (0 != sh_files_fullpath (dirName, fileName, fullpath))
1869 {
1870 tmpdir = sh_util_safe_name (dirName);
1871 tmpname = sh_util_safe_name (fileName);
1872 sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, 0,
1873 MSG_FI_2LONG2,
1874 tmpdir, tmpname);
1875 SH_FREE(tmpname);
1876 SH_FREE(tmpdir);
1877 SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
1878 }
1879
1880
1881 /* stat the file and determine checksum (if a regular file)
1882 */
1883 sl_strlcpy (theFile.fullpath, fullpath, PATH_MAX);
1884 theFile.check_mask = sh_files_maskof(class);
1885 theFile.reported = (*reported);
1886
1887 TPT(( 0, FIL__, __LINE__, _("msg=<checking file: %s>\n"), fullpath));
1888
1889 status = sh_unix_getinfo ( (class == SH_LEVEL_ALLIGNORE) ?
1890 ShDFLevel[class] : ShDFLevel[SH_ERR_T_FILE],
1891 fileName,
1892 &theFile, fileHash, class);
1893
1894 if (status != 0)
1895 {
1896 TPT(( 0, FIL__, __LINE__, _("msg=<file: %s> status=<%d>\n"),
1897 fullpath, status));
1898 if (class == SH_LEVEL_ALLIGNORE && sh.flag.checkSum != SH_CHECK_INIT)
1899 sh_hash_set_visited_true (fullpath);
1900 SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
1901 }
1902
1903 if (sig_termfast == 1) {
1904 goto ret_point;
1905 }
1906
1907 /* report
1908 */
1909 if ((flag_err_debug == SL_TRUE) && (theFile.c_mode[0] == '-'))
1910 {
1911 tmpname = sh_util_safe_name (fullpath); /* fixed in 1.5.4 */
1912 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_CSUM,
1913 fileHash, tmpname);
1914 SH_FREE(tmpname);
1915 }
1916 ++sh.statistics.files_checked;
1917
1918 if ( sh.flag.checkSum == SH_CHECK_INIT && sh.flag.update == S_FALSE )
1919 {
1920 sh_hash_pushdata (&theFile, fileHash);
1921 }
1922 else if (sh.flag.checkSum == SH_CHECK_INIT && sh.flag.update == S_TRUE )
1923 {
1924 if (0 == sh_hash_compdata (class, &theFile, fileHash, NULL, -1))
1925 {
1926 sh_hash_pushdata (&theFile, fileHash);
1927 }
1928 }
1929 else if (sh.flag.checkSum == SH_CHECK_CHECK
1930 /* && theFile.c_mode[0] == '-' */
1931 /* && class != SH_LEVEL_ALLIGNORE */
1932 )
1933 {
1934 sh_hash_compdata (class, &theFile, fileHash, NULL, -1);
1935 }
1936
1937 (*reported) = theFile.reported;
1938
1939 /* reset the access time
1940 */
1941 if (class == SH_LEVEL_NOIGNORE && (theFile.check_mask & MODI_ATM) != 0)
1942 {
1943 utime_buf.actime = (time_t) theFile.atime;
1944 utime_buf.modtime = (time_t) theFile.mtime;
1945#if !defined(O_NOATIME)
1946 retry_aud_utime (FIL__, __LINE__, fullpath, &utime_buf);
1947#endif
1948 }
1949
1950#ifdef HOST_IS_DARWIN
1951 /*
1952 * Check for resource fork
1953 */
1954 if ( (theFile.c_mode[0] != 'd') && (rsrcflag == 0) )
1955 {
1956 int dummy;
1957 static int rsrc_init = 0;
1958 static char rsrc[17];
1959 char testpath[PATH_MAX];
1960
1961 if (rsrc_init == 0) {
1962 sl_strlcpy(rsrc, _("..namedfork/rsrc"), 17);
1963 rsrc_init = 1;
1964 }
1965 sl_strlcpy (testpath, fullpath, PATH_MAX);
1966 sl_strlcat (testpath, "/", PATH_MAX);
1967 sl_strlcat (testpath, rsrc, PATH_MAX);
1968
1969 if (sl_strlen(testpath) == (17 + sl_strlen(fullpath)))
1970 {
1971 if (0 == sh_unix_file_stat (testpath))
1972 {
1973 sh_files_filecheck (class, fullpath, rsrc, &dummy, 1);
1974 }
1975 }
1976 }
1977#else
1978 (void) rsrcflag; /* avoid compiler warning */
1979#endif
1980
1981 ret_point:
1982
1983 switch (theFile.c_mode[0])
1984 {
1985 case '-': SL_RETURN(SH_FILE_REGULAR, _("sh_files_filecheck"));
1986 case 'l': SL_RETURN(SH_FILE_SYMLINK, _("sh_files_filecheck"));
1987 case 'd': SL_RETURN(SH_FILE_DIRECTORY, _("sh_files_filecheck"));
1988 case 'c': SL_RETURN(SH_FILE_CDEV, _("sh_files_filecheck"));
1989 case 'b': SL_RETURN(SH_FILE_BDEV, _("sh_files_filecheck"));
1990 case '|': SL_RETURN(SH_FILE_FIFO, _("sh_files_filecheck"));
1991 case 's': SL_RETURN(SH_FILE_SOCKET, _("sh_files_filecheck"));
1992 default: SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
1993 }
1994
1995 /* notreached */
1996}
1997
1998/* concatenate statpath = testdir"/"d_name
1999 */
2000static int sh_files_fullpath (char * testdir, char * d_name, char * statpath)
2001{
2002 int llen = 0;
2003
2004 SL_ENTER(_("sh_files_fullpath"));
2005
2006 if (testdir != NULL)
2007 {
2008 if ( (llen = sl_strlen(testdir)) > (PATH_MAX-2) )
2009 SL_RETURN((-1),_("sh_files_fullpath"));
2010 sl_strlcpy(statpath, testdir, PATH_MAX - 1);
2011 }
2012 if (d_name != NULL)
2013 {
2014 if (llen > 1 || statpath[0] != '/')
2015 sl_strlcat(statpath, "/", PATH_MAX);
2016 if ((sl_strlen(d_name) + sl_strlen(statpath)) >= PATH_MAX)
2017 SL_RETURN((-1),_("sh_files_fullpath"));
2018 sl_strlcat(statpath, d_name, PATH_MAX);
2019 }
2020 if (statpath == NULL)
2021 SL_RETURN((-1),_("sh_files_fullpath"));
2022 SL_RETURN((0),_("sh_files_fullpath"));
2023}
2024
2025
2026/* -----------------------------------
2027 *
2028 * The following two routines serve to
2029 * verify that the user has selected
2030 * a proper setup for file policies.
2031 *
2032 * -----------------------------------
2033 */
2034static int check_file(char * name)
2035{
2036 dirstack_t * pfilL;
2037 zAVLCursor cursor;
2038
2039 SL_ENTER(_("check_file"));
2040
2041 if (SH_FILE_DIRECTORY == sh_unix_get_ftype(name))
2042 SL_RETURN(0, _("check_file"));
2043
2044 for (pfilL = (dirstack_t *) zAVLFirst (&cursor, zfileList); pfilL;
2045 pfilL = (dirstack_t *) zAVLNext (&cursor))
2046 {
2047 if (0 == strcmp(name, pfilL->name) &&
2048 (pfilL->check_mask & MODI_ATM) == 0 &&
2049 (pfilL->check_mask & MODI_CTM) == 0 &&
2050 (pfilL->check_mask & MODI_MTM) == 0)
2051 SL_RETURN(0, _("check_file"));
2052 }
2053 SL_RETURN((-1), _("check_file"));
2054}
2055
2056int sh_files_test_setup_int (zAVLTree * tree)
2057{
2058 int dlen, flen;
2059 zAVLCursor cursor1;
2060 zAVLCursor cursor2;
2061
2062 dirstack_t * pdirL;
2063 dirstack_t * pfilL;
2064
2065 SL_ENTER(_("sh_files_test_setup"));
2066
2067 for (pdirL = (dirstack_t *) zAVLFirst (&cursor1, tree); pdirL;
2068 pdirL = (dirstack_t *) zAVLNext (&cursor1))
2069 {
2070 dlen = strlen(pdirL->name);
2071
2072 for (pfilL = (dirstack_t *) zAVLFirst (&cursor2, zfileList); pfilL;
2073 pfilL = (dirstack_t *) zAVLNext (&cursor2))
2074 {
2075 flen = strlen(pfilL->name);
2076
2077 /* check whether file is in tree of dir
2078 */
2079 if ((pfilL->class == SH_LEVEL_READONLY) ||
2080 (pfilL->class == SH_LEVEL_NOIGNORE))
2081 {
2082 ; /* do nothing */
2083 }
2084 else
2085 {
2086 if ((flen > (dlen+1)) &&
2087 (pfilL->name[dlen] == '/') &&
2088 (NULL == strchr(&(pfilL->name[dlen+1]), '/')) && /*30-5-01*/
2089 (0 == strncmp(pfilL->name, pdirL->name, dlen)))
2090 {
2091 if ((pdirL->check_mask & MODI_ATM) != 0 ||
2092 (pdirL->check_mask & MODI_MTM) != 0 ||
2093 (pdirL->check_mask & MODI_CTM) != 0)
2094 {
2095 if (check_file (pdirL->name) != 0)
2096 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_COLL,
2097 pdirL->name, pfilL->name);
2098 }
2099 }
2100 }
2101 }
2102 }
2103
2104 SL_RETURN((0), _("sh_files_test_setup"));
2105}
2106
2107int sh_files_test_double (zAVLTree * firstList, zAVLTree * secondList)
2108{
2109 int count;
2110 int retval = 0;
2111
2112 zAVLCursor cursor;
2113
2114 dirstack_t * first;
2115
2116 for (first = (dirstack_t *) zAVLFirst (&cursor, firstList); first;
2117 first = (dirstack_t *) zAVLNext (&cursor))
2118 {
2119
2120 if (NULL != zAVLSearch(secondList, first->name))
2121 {
2122 ++count;
2123 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
2124 first->name);
2125 retval = 1;
2126 }
2127 }
2128 return retval;
2129}
2130
2131extern void aud_exit (char * file, int line, int fd);
2132
2133int sh_files_test_setup ()
2134{
2135 int retval = 0;
2136
2137 /* Test for modifications allowed in ReadOnly directory
2138 */
2139 sh_files_test_setup_int (zdirListOne);
2140 sh_files_test_setup_int (zdirListTwo);
2141
2142 /* Test for files/dirz defined twice
2143 */
2144 retval = sh_files_test_double (zdirListOne, zdirListTwo);
2145 if (retval != 0)
2146 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2147
2148 retval = sh_files_test_double (zdirListTwo, zdirListOne);
2149 if (retval != 0)
2150 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2151
2152
2153 /*
2154 retval = sh_files_test_double (zfileList, NULL);
2155 if (retval != 0)
2156 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2157 */
2158 return 0;
2159}
2160
2161#endif
Note: See TracBrowser for help on using the repository browser.