source: trunk/src/sh_files.c@ 436

Last change on this file since 436 was 433, checked in by katerina, 11 years ago

Fix for ticket #338 (steady growth of memory usage).

File size: 74.7 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#if defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
23#define _XOPEN_SOURCE 500
24#endif
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <ctype.h>
30#include <limits.h>
31
32#include <errno.h>
33
34/* Must be before <utime.h> on FreeBSD
35 */
36#include <sys/types.h>
37#include <unistd.h>
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <fcntl.h>
41
42#if !defined(O_NOATIME)
43#if defined(__linux__) && (defined(__i386__) || defined(__x86_64__) || defined(__PPC__))
44#define O_NOATIME 01000000
45#endif
46#endif
47
48#include <utime.h>
49
50#ifdef HAVE_DIRENT_H
51#include <dirent.h>
52#define NAMLEN(dirent) sl_strlen((dirent)->d_name)
53#else
54#define dirent direct
55#define NAMLEN(dirent) (dirent)->d_namlen
56#ifdef HAVE_SYS_NDIR_H
57#include <sys/ndir.h>
58#endif
59#ifdef HAVE_SYS_DIR_H
60#include <sys/dir.h>
61#endif
62#ifdef HAVE_NDIR_H
63#include <ndir.h>
64#endif
65#endif
66#define NEED_ADD_DIRENT
67
68#ifdef HAVE_GLOB_H
69#include <glob.h>
70#endif
71#ifdef HAVE_FNMATCH_H
72#include <fnmatch.h>
73#endif
74
75
76#include "samhain.h"
77
78#if (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE))
79
80#include "sh_pthread.h"
81#include "sh_error.h"
82#include "sh_utils.h"
83#include "sh_unix.h"
84#include "sh_files.h"
85#include "sh_tiger.h"
86#include "sh_hash.h"
87#include "sh_ignore.h"
88#include "sh_inotify.h"
89#include "zAVLTree.h"
90
91#undef FIL__
92#define FIL__ _("sh_files.c")
93
94extern sh_watches sh_file_watches;
95
96static char * sh_files_C_dequote (char * s, size_t * length)
97{
98 size_t i, len = *length;
99 int flag = 0;
100 char *p, *q, *po, *pend;
101
102 /* search for backslash
103 */
104 for (i = 0; i < len; ++i)
105 {
106 if (s[i] == '\\')
107 {
108 flag = 1;
109 break;
110 }
111 }
112
113 if (flag == 0 || *s == '\0')
114 return s;
115
116 po = SH_ALLOC(len+1); *po = '\0'; p = po; pend = &po[len];
117
118 q = s;
119
120 do
121 {
122 if (*q == '\\')
123 {
124 ++q;
125
126 if (*q == '\0')
127 { *p = *q; flag = 0; break; }
128 else if (*q == 'a')
129 { *p = '\a'; ++p; ++q; }
130 else if (*q == 'b')
131 { *p = '\b'; ++p; ++q; }
132 else if (*q == 'f')
133 { *p = '\f'; ++p; ++q; }
134 else if (*q == 'n')
135 { *p = '\n'; ++p; ++q; }
136 else if (*q == 'r')
137 { *p = '\r'; ++p; ++q; }
138 else if (*q == 't')
139 { *p = '\t'; ++p; ++q; }
140 else if (*q == 'v')
141 { *p = '\v'; ++p; ++q; }
142 else if (*q == '\\')
143 { *p = '\\'; ++p; ++q; }
144 else if (*q == '\'')
145 { *p = '\''; ++p; ++q; }
146 else if (*q == '"')
147 { *p = '"'; ++p; ++q; }
148 else if (*q == 'x')
149 {
150 if (isxdigit((int) q[1]) && isxdigit((int) q[2]))
151 {
152 /* hexadecimal value following */
153 unsigned char cc = (16 * sh_util_hexchar(q[1]))
154 + sh_util_hexchar(q[2]);
155 *p = (char) cc;
156 ++p; q += 3;
157 }
158 else
159 {
160 *p = '\0'; flag = 0; break;
161 }
162 }
163 else if (isdigit((int)*q))
164 {
165 if (isdigit((int) q[1]) && q[1] < '8' &&
166 isdigit((int) q[2]) && q[2] < '8')
167 {
168 /* octal value following */
169 char tmp[4]; unsigned char cc;
170 tmp[0] = *q; ++q; tmp[1] = *q; ++q; tmp[2] = *q; ++q;
171 tmp[3] = '\0';
172 cc = strtol(tmp, NULL, 8);
173 *p = (char) cc; ++p;
174 }
175 else
176 {
177 *p = '\0'; flag = 0; break;
178 }
179 }
180 else
181 {
182 /* invalid escape sequence */
183 *p = '\0'; flag = 0; break;
184 }
185 }
186 else
187 {
188 *p = *q;
189 ++p; ++q;
190 }
191 } while (*q && p <= pend);
192
193 SL_REQUIRE (p <= pend, _("p <= pend"));
194
195 if (flag)
196 {
197 *p = '\0';
198 *length = strlen(po);
199 }
200 else
201 {
202 SH_FREE(po);
203 po = NULL;
204 *length = 0;
205 }
206
207 SL_REQUIRE (*length <= len, _("*length <= len"));
208
209 SH_FREE(s);
210 return po;
211}
212
213extern int flag_err_debug;
214extern int flag_err_info;
215
216int sh_files_reportonce(const char * c)
217{
218 int i;
219 SL_ENTER(_("sh_files_reportonce"));
220 i = sh_util_flagval(c, &(sh.flag.reportonce));
221
222 SL_RETURN(i, _("sh_files_reportonce"));
223}
224
225int sh_files_fulldetail(const char * c)
226{
227 int i;
228 SL_ENTER(_("sh_files_fulldetail"));
229 i = sh_util_flagval(c, &(sh.flag.fulldetail));
230
231 SL_RETURN((i), _("sh_files_fulldetail"));
232}
233
234
235typedef struct dir_struct {
236 long NumRegular;
237 long NumDirs;
238 long NumSymlinks;
239 long NumFifos;
240 long NumSockets;
241 long NumCDev;
242 long NumBDev;
243 long NumDoor;
244 long NumPort;
245 long NumAll;
246 long TotalBytes;
247 char DirPath[PATH_MAX];
248} dir_type;
249
250typedef struct dirstack_entry {
251 char * name;
252 int class;
253 unsigned long check_mask;
254 int rdepth;
255 short checked;
256 short childs_checked;
257 short is_reported;
258 /* struct dirstack_entry * next; */
259} dirstack_t;
260
261
262/* the destructor
263 */
264void free_dirstack (void * inptr)
265{
266 dirstack_t * here;
267
268 SL_ENTER(_("free_dirstack"));
269 if (inptr == NULL)
270 SL_RET0(_("free_dirstack"));
271 else
272 here = (dirstack_t *) inptr;
273
274 if (here->name != NULL)
275 SH_FREE(here->name);
276 SH_FREE(here);
277 SL_RET0(_("free_dirstack"));
278}
279
280/* Function to return the key for indexing
281 * the argument
282 */
283zAVLKey zdirstack_key (void const * arg)
284{
285 const dirstack_t * sa = (const dirstack_t *) arg;
286 return (zAVLKey) sa->name;
287}
288
289#define SH_LIST_FILE 0
290#define SH_LIST_DIR1 1
291#define SH_LIST_DIR2 2
292
293
294static int which_dirList = SH_LIST_DIR1;
295
296static zAVLTree * zdirListOne = NULL;
297static zAVLTree * zdirListTwo = NULL;
298static zAVLTree * zfileList = NULL;
299
300SH_MUTEX_STATIC(mutex_zfiles, PTHREAD_MUTEX_INITIALIZER);
301SH_MUTEX_STATIC(mutex_zglob, PTHREAD_MUTEX_INITIALIZER);
302SH_MUTEX_RECURSIVE(mutex_zdirs);
303
304static int sh_files_fullpath (const char * testdir,
305 const char * d_name,
306 char * statpath);
307static int sh_files_pushdir (int class, const char * str_s);
308static int sh_files_pushfile (int class, const char * str_s);
309
310static long MaxRecursionLevel = 0;
311
312/* set default recursion level
313 */
314int sh_files_setrecursion (const char * flag_s)
315{
316 long flag = 0;
317 static int reject = 0;
318
319 SL_ENTER( _("sh_files_setrecursion"));
320
321 if (reject == 1)
322 SL_RETURN((-1), _("sh_files_setrecursion"));
323
324 if (sh.flag.opts == 1)
325 reject = 1;
326
327 if (flag_s != NULL)
328 flag = (int)(atof(flag_s));
329
330 if (flag >= 0 && flag <= 99)
331 MaxRecursionLevel = flag;
332 else
333 SL_RETURN((-1), _("sh_files_setrecursion"));
334
335 SL_RETURN((0), _("sh_files_setrecursion"));
336}
337
338unsigned long sh_files_chk ()
339{
340 zAVLCursor cursor;
341 ShFileType status;
342 unsigned long fcount = 0;
343
344 char * tmp = NULL;
345
346 dirstack_t * ptr;
347 char * dir;
348 char * file;
349 int tmp_reported;
350
351 SL_ENTER(_("sh_files_chk"));
352
353 for (ptr = (dirstack_t *) zAVLFirst(&cursor, zfileList); ptr;
354 ptr = (dirstack_t *) zAVLNext(&cursor))
355 {
356
357 if (sig_urgent > 0) {
358 SL_RETURN(fcount, _("sh_files_chk"));
359 }
360
361 if (ptr->checked == S_FALSE)
362 {
363 dir = sh_util_dirname (ptr->name);
364 file = sh_util_basename (ptr->name);
365#if defined(WITH_TPT)
366 tmp = sh_util_safe_name (ptr->name);
367#endif
368
369
370 if (flag_err_info == SL_TRUE)
371 {
372 char pstr[32];
373#if !defined(WITH_TPT)
374 tmp = sh_util_safe_name (ptr->name);
375#endif
376 sl_strlcpy(pstr, sh_hash_getpolicy(ptr->class), sizeof(pstr));
377 sh_error_handle ((-1), FIL__, __LINE__, 0,
378 MSG_FI_CHK, pstr, tmp);
379 }
380
381 if ((sh.flag.inotify & SH_INOTIFY_INSCAN) != 0)
382 {
383 sh_inotify_add_watch_later(ptr->name, &sh_file_watches, NULL,
384 ptr->class, ptr->check_mask,
385 SH_INOTIFY_FILE, 0);
386 }
387
388 BREAKEXIT(sh_files_filecheck);
389 tmp_reported = ptr->is_reported; /* fix aliasing warning */
390 status = sh_files_filecheck (ptr->class, ptr->check_mask, dir, file,
391 &tmp_reported, 0);
392 ptr->is_reported = tmp_reported;
393
394 TPT(( 0, FIL__, __LINE__,
395 _("msg=<filecheck complete: %s> status=<%d> reported=<%d>\n"),
396 tmp, status, ptr->is_reported));
397
398 if (status == SH_FILE_UNKNOWN && (!SH_FFLAG_REPORTED_SET(ptr->is_reported)))
399 {
400 TPT(( 0, FIL__, __LINE__, _("msg=<file: %s> status=<%d>\n"),
401 tmp, status));
402
403 if ( sh.flag.checkSum == SH_CHECK_INIT ||
404 sh_hash_have_it (ptr->name) >= 0)
405 {
406 if (S_FALSE == sh_ignore_chk_del(ptr->name))
407 {
408 if (0 != hashreport_missing(ptr->name,
409 (ptr->class == SH_LEVEL_ALLIGNORE) ?
410 ShDFLevel[ptr->class] :
411 ShDFLevel[SH_ERR_T_FILE])) {
412 if (tmp == NULL)
413 tmp = sh_util_safe_name (ptr->name);
414 sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE) ?
415 ShDFLevel[ptr->class] :
416 ShDFLevel[SH_ERR_T_FILE],
417 FIL__, __LINE__, 0, MSG_FI_MISS,
418 tmp);
419 ++sh.statistics.files_report;
420 }
421 }
422 }
423 else /* not there at init, and still missing */
424 {
425 if (tmp == NULL)
426 tmp = sh_util_safe_name (ptr->name);
427 sh_error_handle (SH_ERR_NOTICE,
428 FIL__, __LINE__, 0,
429 MSG_FI_FAIL,
430 tmp);
431 }
432#ifndef REPLACE_OLD
433 /* this will tell that we have seen the file, and thus prevent
434 * deletion from the database, resulting in an incomplete
435 * message when the file reappears
436 */
437 if (sh.flag.checkSum != SH_CHECK_INIT)
438 sh_hash_set_visited_true(ptr->name);
439#else
440 if (sh.flag.checkSum != SH_CHECK_INIT)
441 sh_hash_set_missing(ptr->name);
442#endif
443 if (sh.flag.reportonce == S_TRUE)
444 SET_SH_FFLAG_REPORTED(ptr->is_reported);
445 }
446 else
447 {
448 /* exists (status >= 0), but was missing (reported == TRUE)
449 */
450 if (status != SH_FILE_UNKNOWN && SH_FFLAG_REPORTED_SET(ptr->is_reported))
451 {
452 CLEAR_SH_FFLAG_REPORTED(ptr->is_reported);
453 }
454
455 /* Catchall
456 */
457 else if (status == SH_FILE_UNKNOWN)
458 {
459 /* Thu Mar 7 15:09:40 CET 2002 Make sure missing file
460 * is reported if ptr->reported == S_TRUE because the
461 * file has been added.
462 */
463 if (sh_hash_have_it (ptr->name) >= 0 &&
464 !SH_FFLAG_REPORTED_SET(ptr->is_reported))
465 {
466 if (S_FALSE == sh_ignore_chk_del(ptr->name))
467 {
468 if (0 != hashreport_missing(ptr->name,
469 (ptr->class == SH_LEVEL_ALLIGNORE) ?
470 ShDFLevel[ptr->class] :
471 ShDFLevel[SH_ERR_T_FILE])) {
472 if (tmp == NULL)
473 tmp = sh_util_safe_name (ptr->name);
474 sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE)?
475 ShDFLevel[ptr->class] :
476 ShDFLevel[SH_ERR_T_FILE],
477 FIL__, __LINE__, 0, MSG_FI_MISS,
478 tmp);
479 ++sh.statistics.files_report;
480 }
481 }
482#ifndef REPLACE_OLD
483 if (sh.flag.checkSum != SH_CHECK_INIT)
484 sh_hash_set_visited_true(ptr->name);
485#else
486 /* delete from database
487 */
488 if (sh.flag.checkSum != SH_CHECK_INIT)
489 sh_hash_set_missing(ptr->name);
490#endif
491 }
492 else
493 {
494 if (tmp == NULL)
495 tmp = sh_util_safe_name (ptr->name);
496 sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, 0,
497 MSG_FI_FAIL,
498 tmp);
499 if (sh.flag.checkSum != SH_CHECK_INIT)
500 sh_hash_set_visited_true(ptr->name);
501 }
502 }
503 ++fcount;
504 }
505
506 if (tmp != NULL)
507 {
508 SH_FREE(tmp);
509 tmp = NULL;
510 }
511 if (file)
512 SH_FREE(file);
513 if (dir)
514 SH_FREE(dir);
515
516 ptr->checked = S_TRUE;
517 }
518 }
519
520 SL_RETURN(fcount, _("sh_files_chk"));
521}
522
523int sh_files_delfilestack ()
524{
525 SL_ENTER(_("sh_files_delfilestack"));
526
527 SH_MUTEX_LOCK(mutex_zfiles);
528 zAVLFreeTree (zfileList, free_dirstack);
529 zfileList = NULL;
530 SH_MUTEX_UNLOCK(mutex_zfiles);
531
532 SL_RETURN(0, _("sh_files_delfilestack"));
533}
534
535int sh_files_setrec_int (zAVLTree * tree)
536{
537 dirstack_t * ptr;
538 zAVLCursor avlcursor;
539
540 SL_ENTER(_("sh_files_setrec"));
541 if (tree != NULL) {
542 for (ptr = (dirstack_t *) zAVLFirst(&avlcursor, tree); ptr;
543 ptr = (dirstack_t *) zAVLNext(&avlcursor))
544 {
545 if (ptr->rdepth < (-1) || ptr->rdepth > 99)
546 {
547 ptr->rdepth = MaxRecursionLevel;
548 }
549 if (ptr->rdepth == (-1) && sh.flag.checkSum != SH_CHECK_INIT)
550 hash_remove_tree (ptr->name);
551 }
552 }
553 SL_RETURN(0, _("sh_files_setrec"));
554}
555
556int sh_files_setrec ()
557{
558 volatile int ret;
559 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
560 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
561 sh_files_setrec_int(zdirListOne);
562 ret = sh_files_setrec_int(zdirListTwo);
563 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
564
565 return ret;
566}
567
568zAVLTree * sh_files_deldirstack_int (zAVLTree * ptr)
569{
570 SL_ENTER(_("sh_files_deldirstack"));
571
572 zAVLFreeTree (ptr, free_dirstack);
573
574 SL_RETURN(NULL, _("sh_files_deldirstack"));
575}
576
577int sh_files_deldirstack ()
578{
579 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
580 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
581 zdirListOne = sh_files_deldirstack_int(zdirListOne);
582 zdirListTwo = sh_files_deldirstack_int(zdirListTwo);
583 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
584 return 0;
585}
586
587void sh_files_reset()
588{
589 dirstack_t * ptr;
590 zAVLCursor avlcursor;
591
592 SL_ENTER(_("sh_files_reset"));
593
594 SH_MUTEX_LOCK(mutex_zfiles);
595 for (ptr = (dirstack_t *) zAVLFirst(&avlcursor, zfileList); ptr;
596 ptr = (dirstack_t *) zAVLNext(&avlcursor))
597 ptr->checked = 0;
598 SH_MUTEX_UNLOCK(mutex_zfiles);
599 SL_RET0(_("sh_files_reset"));
600}
601
602void sh_dirs_reset()
603{
604 dirstack_t * ptr;
605 zAVLCursor avlcursor1;
606 zAVLCursor avlcursor2;
607
608 SL_ENTER(_("sh_dirs_reset"));
609
610 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
611 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
612 for (ptr = (dirstack_t *) zAVLFirst(&avlcursor1, zdirListOne); ptr;
613 ptr = (dirstack_t *) zAVLNext(&avlcursor1))
614 ptr->checked = 0;
615
616 for (ptr = (dirstack_t *) zAVLFirst(&avlcursor2, zdirListTwo); ptr;
617 ptr = (dirstack_t *) zAVLNext(&avlcursor2))
618 ptr->checked = 0;
619 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
620
621 SL_RET0(_("sh_dirs_reset"));
622}
623
624
625int sh_files_pushfile_prelink (const char * str_s)
626{
627 return (sh_files_pushfile (SH_LEVEL_PRELINK, str_s));
628}
629
630int sh_files_pushfile_user0 (const char * str_s)
631{
632 return (sh_files_pushfile (SH_LEVEL_USER0, str_s));
633}
634
635int sh_files_pushfile_user1 (const char * str_s)
636{
637 return (sh_files_pushfile (SH_LEVEL_USER1, str_s));
638}
639
640int sh_files_pushfile_user2 (const char * str_s)
641{
642 return (sh_files_pushfile (SH_LEVEL_USER2, str_s));
643}
644
645int sh_files_pushfile_user3 (const char * str_s)
646{
647 return (sh_files_pushfile (SH_LEVEL_USER3, str_s));
648}
649
650int sh_files_pushfile_user4 (const char * str_s)
651{
652 return (sh_files_pushfile (SH_LEVEL_USER4, str_s));
653}
654
655
656int sh_files_pushfile_ro (const char * str_s)
657{
658 return (sh_files_pushfile (SH_LEVEL_READONLY, str_s));
659}
660
661int sh_files_pushfile_attr (const char * str_s)
662{
663 return (sh_files_pushfile (SH_LEVEL_ATTRIBUTES, str_s));
664}
665
666int sh_files_pushfile_log (const char * str_s)
667{
668 return (sh_files_pushfile (SH_LEVEL_LOGFILES, str_s));
669}
670
671int sh_files_pushfile_glog (const char * str_s)
672{
673 return (sh_files_pushfile (SH_LEVEL_LOGGROW, str_s));
674}
675
676int sh_files_pushfile_noig (const char * str_s)
677{
678 return (sh_files_pushfile (SH_LEVEL_NOIGNORE, str_s));
679}
680
681int sh_files_pushfile_allig (const char * str_s)
682{
683 return (sh_files_pushfile (SH_LEVEL_ALLIGNORE, str_s));
684}
685
686
687static void sh_files_set_mask (unsigned long * mask,
688 unsigned long val, int act)
689{
690 SL_ENTER(_("sh_files_set_mask"));
691
692 if (act == 0)
693 (*mask) = val;
694 else if (act > 0)
695 (*mask) |= val;
696 else
697 (*mask) &= ~val;
698
699 SL_RET0(_("sh_files_set_mask"));
700}
701
702/* set mask(class)
703 */
704static int sh_files_parse_mask (unsigned long * mask, const char * str)
705{
706 int l, i = 0, act = 0, k = 0;
707 char myword[64];
708
709 SL_ENTER(_("sh_files_parse_mask"));
710
711 myword[0] = '\0';
712
713 if (str == NULL)
714 {
715 SL_RETURN ( (-1), _("sh_files_parse_mask"));
716 }
717 else
718 l = sl_strlen(str);
719
720 while (i < l) {
721
722 if (str[i] == '\0')
723 break;
724
725 if (str[i] == ' ' || str[i] == '\t' || str[i] == ',')
726 {
727 ++i;
728 continue;
729 }
730
731 if (str[i] == '+')
732 {
733 act = +1; ++i;
734 myword[0] = '\0';
735 goto getword;
736 }
737 else if (str[i] == '-')
738 {
739 act = -1; ++i;
740 myword[0] = '\0';
741 goto getword;
742 }
743 else /* a word */
744 {
745 getword:
746 k = 0;
747 while (k < 63 && str[i] != ' ' && str[i] != '\t' && str[i] != ','
748 && str[i] != '+' && str[i] != '-' && str[i] != '\0') {
749 myword[k] = str[i];
750 ++i; ++k;
751 }
752 myword[k] = '\0';
753
754 if (sl_strlen(myword) == 0)
755 {
756 SL_RETURN ( (-1), _("sh_files_parse_mask"));
757 }
758
759/* checksum */
760 if (0 == strcmp(myword, _("CHK")))
761 sh_files_set_mask (mask, MODI_CHK, act);
762/* link */
763 else if (0 == strcmp(myword, _("LNK")))
764 sh_files_set_mask (mask, MODI_LNK, act);
765/* inode */
766 else if (0 == strcmp(myword, _("RDEV")))
767 sh_files_set_mask (mask, MODI_RDEV, act);
768/* inode */
769 else if (0 == strcmp(myword, _("INO")))
770 sh_files_set_mask (mask, MODI_INO, act);
771/* user */
772 else if (0 == strcmp(myword, _("USR")))
773 sh_files_set_mask (mask, MODI_USR, act);
774/* group */
775 else if (0 == strcmp(myword, _("GRP")))
776 sh_files_set_mask (mask, MODI_GRP, act);
777/* mtime */
778 else if (0 == strcmp(myword, _("MTM")))
779 sh_files_set_mask (mask, MODI_MTM, act);
780/* ctime */
781 else if (0 == strcmp(myword, _("CTM")))
782 sh_files_set_mask (mask, MODI_CTM, act);
783/* atime */
784 else if (0 == strcmp(myword, _("ATM")))
785 sh_files_set_mask (mask, MODI_ATM, act);
786/* size */
787 else if (0 == strcmp(myword, _("SIZ")))
788 sh_files_set_mask (mask, MODI_SIZ, act);
789/* file mode */
790 else if (0 == strcmp(myword, _("MOD")))
791 sh_files_set_mask (mask, MODI_MOD, act);
792/* hardlinks */
793 else if (0 == strcmp(myword, _("HLN")))
794 sh_files_set_mask (mask, MODI_HLN, act);
795/* size may grow */
796 else if (0 == strcmp(myword, _("SGROW")))
797 sh_files_set_mask (mask, MODI_SGROW, act);
798/* use prelink */
799 else if (0 == strcmp(myword, _("PRE")))
800 sh_files_set_mask (mask, MODI_PREL, act);
801/* get content */
802 else if (0 == strcmp(myword, _("TXT")))
803 sh_files_set_mask (mask, MODI_TXT, act);
804/* get content */
805 else if (0 == strcmp(myword, _("AUDIT")))
806 sh_files_set_mask (mask, MODI_AUDIT, act);
807 else
808 {
809 SL_RETURN ( (-1), _("sh_files_parse_mask"));
810 }
811 act = 0;
812 myword[0] = '\0';
813 }
814 }
815 SL_RETURN ( (0), _("sh_files_parse_mask"));
816}
817
818int sh_files_redef_prelink(const char * str)
819{
820 return (sh_files_parse_mask(&mask_PRELINK, str));
821}
822int sh_files_redef_user0(const char * str)
823{
824 return (sh_files_parse_mask(&mask_USER0, str));
825}
826int sh_files_redef_user1(const char * str)
827{
828 return (sh_files_parse_mask(&mask_USER1, str));
829}
830int sh_files_redef_user2(const char * str)
831{
832 return (sh_files_parse_mask(&mask_USER2, str));
833}
834int sh_files_redef_user3(const char * str)
835{
836 return (sh_files_parse_mask(&mask_USER3, str));
837}
838int sh_files_redef_user4(const char * str)
839{
840 return (sh_files_parse_mask(&mask_USER4, str));
841}
842int sh_files_redef_readonly(const char * str)
843{
844 return (sh_files_parse_mask(&mask_READONLY, str));
845}
846int sh_files_redef_loggrow(const char * str)
847{
848 return (sh_files_parse_mask(&mask_LOGGROW, str));
849}
850int sh_files_redef_logfiles(const char * str)
851{
852 return (sh_files_parse_mask(&mask_LOGFILES, str));
853}
854int sh_files_redef_attributes(const char * str)
855{
856 return (sh_files_parse_mask(&mask_ATTRIBUTES, str));
857}
858int sh_files_redef_noignore(const char * str)
859{
860 return (sh_files_parse_mask(&mask_NOIGNORE, str));
861}
862int sh_files_redef_allignore(const char * str)
863{
864 return (sh_files_parse_mask(&mask_ALLIGNORE, str));
865}
866
867unsigned long sh_files_maskof (int class)
868{
869 switch (class)
870 {
871 case SH_LEVEL_READONLY:
872 return (unsigned long) (mask_READONLY | MODI_INIT);
873 case SH_LEVEL_ATTRIBUTES:
874 return (unsigned long) (mask_ATTRIBUTES | MODI_INIT);
875 case SH_LEVEL_LOGFILES:
876 return (unsigned long) (mask_LOGFILES | MODI_INIT);
877 case SH_LEVEL_LOGGROW:
878 return (unsigned long) (mask_LOGGROW | MODI_INIT);
879 case SH_LEVEL_ALLIGNORE:
880 return (unsigned long) (mask_ALLIGNORE | MODI_INIT);
881 case SH_LEVEL_NOIGNORE:
882 return (unsigned long) (mask_NOIGNORE | MODI_INIT);
883 case SH_LEVEL_USER0:
884 return (unsigned long) (mask_USER0 | MODI_INIT);
885 case SH_LEVEL_USER1:
886 return (unsigned long) (mask_USER1 | MODI_INIT);
887 case SH_LEVEL_USER2:
888 return (unsigned long) (mask_USER2 | MODI_INIT);
889 case SH_LEVEL_USER3:
890 return (unsigned long) (mask_USER3 | MODI_INIT);
891 case SH_LEVEL_USER4:
892 return (unsigned long) (mask_USER4 | MODI_INIT);
893 case SH_LEVEL_PRELINK:
894 return (unsigned long) (mask_PRELINK | MODI_INIT);
895 default:
896 return (unsigned long) 0;
897 }
898}
899
900#ifdef HAVE_GLOB_H
901int sh_files_has_metachar (const char * str)
902{
903 SL_ENTER(_("sh_files_has_metachar"));
904 if (NULL != strchr(str, '*'))
905 SL_RETURN(1, _("sh_files_has_metachar"));
906 else if (NULL != strchr(str, '?'))
907 SL_RETURN(1, _("sh_files_has_metachar"));
908 else if (NULL != (strchr(str, '[')))
909 SL_RETURN(1, _("sh_files_has_metachar"));
910 else
911 SL_RETURN(0, _("sh_files_has_metachar"));
912}
913
914
915int sh_files_globerr (const char * epath, int errnum)
916{
917 char * p;
918 char errbuf[SH_ERRBUF_SIZE];
919
920 SL_ENTER(_("sh_files_globerr"));
921
922 if (errnum == ENOTDIR || errnum == ENOENT)
923 {
924 SL_RETURN(0, _("sh_files_globerr"));
925 }
926
927 p = sh_util_safe_name (epath);
928 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, errnum, MSG_FI_GLOB,
929 sh_error_message (errnum, errbuf, sizeof(errbuf)), p);
930 SH_FREE(p);
931
932 SL_RETURN(0, _("sh_files_globerr"));
933}
934
935/* #ifdef HAVE_GLOB_H
936 */
937#endif
938
939int sh_files_push_file_int (int class, const char * str_s, size_t len,
940 unsigned long check_mask)
941{
942 dirstack_t * new_item_ptr;
943 char * fileName;
944 int ret;
945 volatile int count = 0;
946
947 SL_ENTER(_("sh_files_push_file_int"));
948
949 fileName = SH_ALLOC(len+1);
950 sl_strlcpy(fileName, str_s, len+1);
951
952 new_item_ptr = (dirstack_t *) SH_ALLOC (sizeof(dirstack_t));
953
954 new_item_ptr->name = fileName;
955 new_item_ptr->class = class;
956 new_item_ptr->check_mask = check_mask;
957 new_item_ptr->rdepth = 0;
958 new_item_ptr->checked = S_FALSE;
959 new_item_ptr->is_reported = 0;
960 new_item_ptr->childs_checked = S_FALSE;
961
962 SH_MUTEX_LOCK(mutex_zfiles);
963 if (zfileList == NULL)
964 {
965 zfileList = zAVLAllocTree (zdirstack_key, zAVL_KEY_STRING);
966 if (zfileList == NULL)
967 {
968 (void) safe_logger (0, 0, NULL);
969 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
970 }
971 }
972
973 ret = zAVLInsert (zfileList, new_item_ptr);
974 SH_MUTEX_UNLOCK(mutex_zfiles);
975
976 if (-1 == ret)
977 {
978 (void) safe_logger (0, 0, NULL);
979 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
980 }
981 else if (3 == ret)
982 {
983 if (sh.flag.started != S_TRUE)
984 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
985 fileName);
986 SH_FREE(fileName);
987 SH_FREE(new_item_ptr);
988 new_item_ptr = NULL;
989 }
990 else
991 {
992 int reported;
993 unsigned long check_mask = sh_files_maskof(class);
994
995 if ((sh.flag.inotify & SH_INOTIFY_INSCAN) != 0)
996 {
997 sh_files_filecheck (class, check_mask, str_s, NULL,
998 &reported, 0);
999 if (SH_FFLAG_REPORTED_SET(reported))
1000 sh_files_set_file_reported(str_s);
1001 sh_inotify_add_watch_later(str_s, &sh_file_watches, NULL,
1002 class, check_mask,
1003 SH_INOTIFY_FILE, 0);
1004 }
1005
1006 if (MODI_AUDIT_ENABLED(check_mask))
1007 {
1008 sh_audit_mark(str_s);
1009 }
1010 ++count;
1011 }
1012 SL_RETURN(count, _("sh_files_push_file_int"));
1013}
1014
1015int sh_files_push_dir_int (int class, char * tail, size_t len, int rdepth, unsigned long check_mask);
1016
1017#ifdef HAVE_GLOB_H
1018
1019typedef struct globstack_entry {
1020 char * name;
1021 int class;
1022 unsigned long check_mask;
1023 int rdepth;
1024 short type;
1025 /* struct dirstack_entry * next; */
1026} sh_globstack_t;
1027
1028static zAVLTree * zglobList = NULL;
1029
1030static int sh_files_pushglob (int class, int type, const char * p, int rdepth,
1031 unsigned long check_mask_in, int flag)
1032{
1033 int globstatus = -1;
1034 unsigned int gloop;
1035 glob_t pglob;
1036
1037 volatile int count = 0;
1038 volatile unsigned long check_mask = (flag == 0) ? sh_files_maskof(class) : check_mask_in;
1039
1040 SL_ENTER(_("sh_files_pushglob"));
1041
1042 pglob.gl_offs = 0;
1043 globstatus = glob (p, 0, sh_files_globerr, &pglob);
1044
1045 if (globstatus == 0 && pglob.gl_pathc > 0)
1046 {
1047
1048 if (sh.flag.checkSum != SH_CHECK_INIT)
1049 {
1050 sh_globstack_t * new_item_ptr;
1051 char * fileName;
1052 int ret;
1053
1054 SH_MUTEX_TRYLOCK(mutex_zfiles);
1055 fileName = sh_util_strdup (p);
1056
1057 new_item_ptr = (sh_globstack_t *) SH_ALLOC (sizeof(sh_globstack_t));
1058
1059 new_item_ptr->name = fileName;
1060 new_item_ptr->class = class;
1061 new_item_ptr->check_mask = check_mask;
1062 new_item_ptr->rdepth = rdepth;
1063 new_item_ptr->type = type;
1064
1065 if (zglobList == NULL)
1066 {
1067 zglobList = zAVLAllocTree (zdirstack_key, zAVL_KEY_STRING);
1068 if (zglobList == NULL)
1069 {
1070 (void) safe_logger (0, 0, NULL);
1071 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
1072 }
1073 }
1074
1075 ret = zAVLInsert (zglobList, new_item_ptr);
1076
1077 if (ret != 0) /* already in list */
1078 {
1079 SH_FREE(fileName);
1080 SH_FREE(new_item_ptr);
1081 }
1082 SH_MUTEX_TRYLOCK_UNLOCK(mutex_zfiles);
1083 }
1084
1085 for (gloop = 0; gloop < (unsigned int) pglob.gl_pathc; ++gloop)
1086 {
1087 if (type == SH_LIST_FILE)
1088 {
1089 count += sh_files_push_file_int (class, pglob.gl_pathv[gloop],
1090 sl_strlen(pglob.gl_pathv[gloop]), check_mask);
1091 }
1092 else
1093 {
1094 which_dirList = type;
1095
1096 count += sh_files_push_dir_int (class, pglob.gl_pathv[gloop],
1097 sl_strlen(pglob.gl_pathv[gloop]), rdepth, check_mask);
1098 }
1099 }
1100 }
1101 else
1102 {
1103 char * tmp = sh_util_safe_name (p);
1104
1105 if (pglob.gl_pathc == 0
1106#ifdef GLOB_NOMATCH
1107 || globstatus == GLOB_NOMATCH
1108#endif
1109 )
1110 sh_error_handle ((sh.flag.started != S_TRUE) ? SH_ERR_ERR : SH_ERR_NOTICE,
1111 FIL__, __LINE__,
1112 globstatus, MSG_FI_GLOB,
1113 _("No matches found"), tmp);
1114#ifdef GLOB_NOSPACE
1115 else if (globstatus == GLOB_NOSPACE)
1116 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
1117 globstatus, MSG_FI_GLOB,
1118 _("Out of memory"), tmp);
1119#endif
1120#ifdef GLOB_ABORTED
1121 else if (globstatus == GLOB_ABORTED)
1122 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
1123 globstatus, MSG_FI_GLOB,
1124 _("Read error"), tmp);
1125#endif
1126 else
1127 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
1128 globstatus, MSG_FI_GLOB,
1129 _("Unknown error"), tmp);
1130
1131 SH_FREE(tmp);
1132
1133 }
1134
1135 globfree(&pglob);
1136 SL_RETURN(count, _("sh_files_pushglob"));
1137 return count;
1138}
1139
1140void sh_files_check_globFilePatterns()
1141{
1142 sh_globstack_t * testPattern;
1143 zAVLCursor cursor;
1144
1145 SL_ENTER(_("sh_files_check_globPatterns"));
1146
1147 SH_MUTEX_LOCK(mutex_zglob);
1148 for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList);
1149 testPattern;
1150 testPattern = (sh_globstack_t *) zAVLNext (&cursor))
1151 {
1152 if (testPattern->type == SH_LIST_FILE)
1153 {
1154 sh_files_pushglob(testPattern->class, testPattern->type,
1155 testPattern->name, testPattern->rdepth,
1156 testPattern->check_mask, 1);
1157 }
1158 }
1159 SH_MUTEX_UNLOCK(mutex_zglob);
1160 SL_RET0(_("sh_files_check_globPatterns"));
1161}
1162
1163void sh_files_check_globPatterns()
1164{
1165 sh_globstack_t * testPattern;
1166 zAVLCursor cursor;
1167
1168 SL_ENTER(_("sh_files_check_globPatterns"));
1169
1170 SH_MUTEX_LOCK(mutex_zglob);
1171 for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList);
1172 testPattern;
1173 testPattern = (sh_globstack_t *) zAVLNext (&cursor))
1174 {
1175 sh_files_pushglob(testPattern->class, testPattern->type,
1176 testPattern->name, testPattern->rdepth,
1177 testPattern->check_mask, 1);
1178 }
1179 SH_MUTEX_UNLOCK(mutex_zglob);
1180 SL_RET0(_("sh_files_check_globPatterns"));
1181}
1182
1183/* the destructor
1184 */
1185void free_globstack (void * inptr)
1186{
1187 sh_globstack_t * here;
1188
1189 SL_ENTER(_("free_globstack"));
1190 if (inptr == NULL)
1191 SL_RET0(_("free_globstack"));
1192 else
1193 here = (sh_globstack_t *) inptr;
1194
1195 if (here->name != NULL)
1196 SH_FREE(here->name);
1197 SH_FREE(here);
1198 SL_RET0(_("free_globstack"));
1199}
1200
1201int sh_files_delglobstack ()
1202{
1203 SL_ENTER(_("sh_files_delglobstack"));
1204
1205 SH_MUTEX_LOCK(mutex_zglob);
1206 zAVLFreeTree (zglobList, free_globstack);
1207 zglobList = NULL;
1208 SH_MUTEX_UNLOCK(mutex_zglob);
1209
1210 SL_RETURN(0, _("sh_files_delglobstack"));
1211}
1212
1213
1214#else
1215void sh_files_check_globPatterns()
1216{
1217 return;
1218}
1219int sh_files_delglobstack ()
1220{
1221 return 0;
1222}
1223#endif
1224
1225static int sh_files_pushfile (int class, const char * str_s)
1226{
1227 size_t len;
1228 char * tmp;
1229 char * p;
1230
1231 static int reject = 0;
1232
1233 SL_ENTER(_("sh_files_pushfile"));
1234
1235 if (reject == 1)
1236 SL_RETURN((-1),_("sh_files_pushfile"));
1237
1238 /* if we push a filename from the command line, make sure it
1239 * is the only one -- and will stay the only one
1240 */
1241 if (sh.flag.opts == 1)
1242 {
1243 sh_files_delfilestack ();
1244 sh_files_deldirstack ();
1245 sh_files_delglobstack ();
1246 reject = 1;
1247 }
1248
1249 if (str_s == NULL || str_s[0] == '\0')
1250 SL_RETURN((-1),_("sh_files_pushfile"));
1251
1252 len = sl_strlen(str_s);
1253
1254 if ( (str_s[0] == '"' && str_s[len-1] == '"' ) ||
1255 (str_s[0] == '\'' && str_s[len-1] == '\'') )
1256 {
1257 if (len < 3)
1258 SL_RETURN((-1),_("sh_files_pushfile"));
1259 --len;
1260 p = sh_util_strdup_l(&str_s[1], len);
1261 p[len-1] = '\0';
1262 --len;
1263 }
1264 else
1265 {
1266 p = sh_util_strdup_l(str_s, len);
1267 }
1268
1269 p = sh_files_C_dequote(p, &len);
1270 if (!p || len == 0)
1271 SL_RETURN((-1), _("sh_files_pushfile"));
1272
1273 if (len >= PATH_MAX)
1274 {
1275 /* Name too long
1276 */
1277 tmp = sh_util_safe_name (p);
1278 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_2LONG,
1279 tmp);
1280 SH_FREE(tmp);
1281 SL_RETURN((-1),_("sh_files_pushfile"));
1282 }
1283 else if (p[0] != '/')
1284 {
1285 /* Not an absolute path
1286 */
1287 tmp = sh_util_safe_name (p);
1288 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_NOPATH,
1289 tmp);
1290 SH_FREE(tmp);
1291 SL_RETURN((-1),_("sh_files_pushfile"));
1292 }
1293 else
1294 {
1295 /* remove a terminating '/', take care of the
1296 * special case of the root directory.
1297 */
1298 if (p[len-1] == '/' && len > 1)
1299 {
1300 p[len-1] = '\0';
1301 --len;
1302 }
1303 }
1304
1305#ifdef HAVE_GLOB_H
1306 if (0 == sh_files_has_metachar(p))
1307 {
1308 sh_files_push_file_int (class, p, len, sh_files_maskof(class));
1309 }
1310 else
1311 {
1312 sh_files_pushglob (class, SH_LIST_FILE, p, 0, 0, 0);
1313 }
1314
1315#else
1316 sh_files_push_file_int (class, p, len, sh_files_maskof(class));
1317#endif
1318
1319 SH_FREE(p);
1320 SL_RETURN((0),_("sh_files_pushfile"));
1321}
1322
1323
1324/* ------ directories ----- */
1325
1326int sh_files_is_allignore_int (char * str, zAVLTree * tree)
1327{
1328 dirstack_t * ptr;
1329
1330 SL_ENTER(_("sh_files_is_allignore"));
1331
1332 if (tree)
1333 {
1334 ptr = zAVLSearch(tree, str);
1335 if (ptr)
1336 {
1337 if (ptr->class == SH_LEVEL_ALLIGNORE)
1338 SL_RETURN( 1, _("sh_files_is_allignore"));
1339 else
1340 SL_RETURN( 0, _("sh_files_is_allignore"));
1341 }
1342 }
1343 SL_RETURN( 0, _("sh_files_is_allignore"));
1344}
1345
1346int sh_files_is_allignore (char * str)
1347{
1348 int retval = 0;
1349
1350 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
1351 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
1352 retval = sh_files_is_allignore_int(str, zdirListOne);
1353
1354 if (NULL != zdirListTwo && retval == 0)
1355 {
1356 retval = sh_files_is_allignore_int(str, zdirListTwo);
1357 }
1358 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
1359 return retval;
1360}
1361
1362static void * sh_dummy_ptr;
1363
1364unsigned long sh_dirs_chk (int which)
1365{
1366 zAVLTree * tree;
1367 zAVLCursor cursor;
1368 dirstack_t * ptr;
1369 dirstack_t * dst_ptr;
1370 int status;
1371 volatile unsigned long dcount = 0;
1372 char * tmp;
1373
1374 SL_ENTER(_("sh_dirs_chk"));
1375
1376 sh_dummy_ptr = (void *) &ptr;
1377
1378 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
1379 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
1380 if (which == 1)
1381 tree = zdirListOne;
1382 else
1383 tree = zdirListTwo;
1384
1385 for (ptr = (dirstack_t *) zAVLFirst(&cursor, tree); ptr;
1386 ptr = (dirstack_t *) zAVLNext(&cursor))
1387 {
1388 if (sig_urgent > 0) {
1389 goto out;
1390 }
1391
1392 if (ptr->checked == S_FALSE)
1393 {
1394 SH_MUTEX_LOCK(mutex_zfiles);
1395 /* 28 Aug 2001 check the top level directory
1396 */
1397 status = S_FALSE;
1398 dst_ptr = zAVLSearch(zfileList, ptr->name);
1399 if (dst_ptr)
1400 {
1401 if (dst_ptr->checked == S_FALSE)
1402 {
1403 BREAKEXIT(sh_files_filecheck);
1404 sh_files_filecheck (dst_ptr->class, dst_ptr->check_mask,
1405 ptr->name,
1406 NULL, &status, 0);
1407 dst_ptr->checked = S_TRUE;
1408 status = S_TRUE;
1409 }
1410 else
1411 {
1412 status = S_TRUE;
1413 }
1414 }
1415 SH_MUTEX_UNLOCK(mutex_zfiles);
1416
1417 if (status == S_FALSE)
1418 sh_files_filecheck (ptr->class, ptr->check_mask,
1419 ptr->name, NULL, &status, 0);
1420
1421 BREAKEXIT(sh_files_checkdir);
1422 status = sh_files_checkdir (ptr->class, ptr->check_mask,
1423 ptr->rdepth, ptr->name,
1424 ptr->name);
1425
1426 if (status < 0 && (!SH_FFLAG_REPORTED_SET(ptr->is_reported)))
1427 {
1428 /* directory is missing
1429 */
1430 if (S_FALSE == sh_ignore_chk_del(ptr->name))
1431 {
1432 if (0 != hashreport_missing(ptr->name,
1433 (ptr->class == SH_LEVEL_ALLIGNORE) ?
1434 ShDFLevel[ptr->class] :
1435 ShDFLevel[SH_ERR_T_DIR])) {
1436 tmp = sh_util_safe_name (ptr->name);
1437 sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE) ?
1438 ShDFLevel[ptr->class] :
1439 ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__,
1440 0, MSG_FI_MISS, tmp);
1441 ++sh.statistics.files_report;
1442 SH_FREE(tmp);
1443 }
1444 }
1445 if (sh.flag.reportonce == S_TRUE)
1446 SET_SH_FFLAG_REPORTED(ptr->is_reported);
1447 }
1448 else
1449 {
1450 /* exists (status >= 0), but was missing (reported == TRUE)
1451 */
1452 if (status >= 0 && SH_FFLAG_REPORTED_SET(ptr->is_reported))
1453 {
1454 CLEAR_SH_FFLAG_REPORTED(ptr->is_reported);
1455#if 0
1456 /* obsoleted (really?) by the mandatory sh_files_filecheck()
1457 * above, which will catch missing directories anyway
1458 */
1459 tmp = sh_util_safe_name (ptr->name);
1460 sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE) ?
1461 ShDFLevel[ptr->class] :
1462 ShDFLevel[SH_ERR_T_DIR],
1463 FIL__, __LINE__, 0, MSG_FI_ADD,
1464 tmp);
1465 ++sh.statistics.files_report;
1466 SH_FREE(tmp);
1467#endif
1468 }
1469 else if (status == SH_FILE_UNKNOWN)
1470 {
1471 /* catchall
1472 */
1473 tmp = sh_util_safe_name (ptr->name);
1474 sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, 0,
1475 MSG_FI_FAIL,
1476 tmp);
1477 SH_FREE(tmp);
1478 if (sh.flag.checkSum != SH_CHECK_INIT)
1479 sh_hash_set_visited_true(ptr->name);
1480 }
1481
1482 ++dcount;
1483 }
1484 ptr->checked = S_TRUE;
1485 ptr->childs_checked = S_TRUE;
1486 }
1487
1488 if (sig_urgent > 0) {
1489 goto out;
1490 }
1491
1492 }
1493 out:
1494 ; /* 'label at end of compound statement' */
1495 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
1496
1497 SL_RETURN(dcount, _("sh_dirs_chk"));
1498}
1499
1500int sh_files_pushdir_prelink (const char * str_s)
1501{
1502 return (sh_files_pushdir (SH_LEVEL_PRELINK, str_s));
1503}
1504
1505int sh_files_pushdir_user0 (const char * str_s)
1506{
1507 return (sh_files_pushdir (SH_LEVEL_USER0, str_s));
1508}
1509
1510int sh_files_pushdir_user1 (const char * str_s)
1511{
1512 return (sh_files_pushdir (SH_LEVEL_USER1, str_s));
1513}
1514
1515int sh_files_pushdir_user2 (const char * str_s)
1516{
1517 return (sh_files_pushdir (SH_LEVEL_USER2, str_s));
1518}
1519
1520int sh_files_pushdir_user3 (const char * str_s)
1521{
1522 return (sh_files_pushdir (SH_LEVEL_USER3, str_s));
1523}
1524
1525int sh_files_pushdir_user4 (const char * str_s)
1526{
1527 return (sh_files_pushdir (SH_LEVEL_USER4, str_s));
1528}
1529
1530int sh_files_pushdir_attr (const char * str_s)
1531{
1532 return (sh_files_pushdir (SH_LEVEL_ATTRIBUTES, str_s));
1533}
1534
1535int sh_files_pushdir_ro (const char * str_s)
1536{
1537 return (sh_files_pushdir (SH_LEVEL_READONLY, str_s));
1538}
1539
1540int sh_files_pushdir_log (const char * str_s)
1541{
1542 return (sh_files_pushdir (SH_LEVEL_LOGFILES, str_s));
1543}
1544
1545int sh_files_pushdir_glog (const char * str_s)
1546{
1547 return (sh_files_pushdir (SH_LEVEL_LOGGROW, str_s));
1548}
1549
1550int sh_files_pushdir_noig (const char * str_s)
1551{
1552 return (sh_files_pushdir (SH_LEVEL_NOIGNORE, str_s));
1553}
1554
1555int sh_files_pushdir_allig (const char * str_s)
1556{
1557 return (sh_files_pushdir (SH_LEVEL_ALLIGNORE, str_s));
1558}
1559
1560int set_dirList (int which)
1561{
1562 if (which == 2)
1563 which_dirList = SH_LIST_DIR2;
1564 else
1565 which_dirList = SH_LIST_DIR1;
1566 return 0;
1567}
1568
1569int sh_files_push_dir_int (int class, char * tail, size_t len, int rdepth, unsigned long check_mask)
1570{
1571 zAVLTree * tree;
1572 dirstack_t * new_item_ptr;
1573 char * dirName;
1574 int ret;
1575
1576 SL_ENTER(_("sh_files_push_dir_int"));
1577
1578 dirName = SH_ALLOC(len+1);
1579 sl_strlcpy(dirName, tail, len+1);
1580
1581 new_item_ptr = (dirstack_t * ) SH_ALLOC (sizeof(dirstack_t));
1582
1583 new_item_ptr->name = dirName;
1584 new_item_ptr->class = class;
1585 new_item_ptr->check_mask = check_mask;
1586 new_item_ptr->rdepth = rdepth;
1587 new_item_ptr->checked = S_FALSE;
1588 new_item_ptr->is_reported = 0;
1589 new_item_ptr->childs_checked = S_FALSE;
1590
1591 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
1592 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
1593 if (which_dirList == SH_LIST_DIR1)
1594 {
1595 tree = zdirListOne;
1596 }
1597 else
1598 {
1599 tree = zdirListTwo;
1600 }
1601
1602 if (tree == NULL)
1603 {
1604 tree = zAVLAllocTree (zdirstack_key, zAVL_KEY_STRING);
1605 if (tree == NULL)
1606 {
1607 (void) safe_logger (0, 0, NULL);
1608 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
1609 }
1610 if (which_dirList == SH_LIST_DIR1)
1611 zdirListOne = tree;
1612 else
1613 zdirListTwo = tree;
1614 }
1615
1616 ret = zAVLInsert (tree, new_item_ptr);
1617 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
1618
1619 if (-1 == ret)
1620 {
1621 (void) safe_logger (0, 0, NULL);
1622 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
1623 }
1624 if (3 == ret)
1625 {
1626 if (sh.flag.started != S_TRUE)
1627 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
1628 dirName);
1629 SH_FREE(dirName);
1630 SH_FREE(new_item_ptr);
1631 new_item_ptr = NULL;
1632 }
1633 else
1634 {
1635 if (MODI_AUDIT_ENABLED(check_mask))
1636 {
1637 sh_audit_mark(tail);
1638 }
1639 }
1640 SL_RETURN(0, _("sh_files_push_dir_int"));
1641}
1642
1643static int sh_files_pushdir (int class, const char * str_s)
1644{
1645 char * tmp;
1646 size_t len;
1647 int rdepth = 0;
1648 char * tail = NULL;
1649 char * p;
1650
1651 SL_ENTER(_("sh_files_pushdir"));
1652
1653 if (sh.flag.opts == 1) {
1654 sh_files_delfilestack ();
1655 sh_files_deldirstack ();
1656 sh_files_delglobstack ();
1657 }
1658
1659 if (str_s == NULL || str_s[0] == '\0')
1660 SL_RETURN((-1),_("sh_files_pushdir"));
1661
1662 len = sl_strlen(str_s);
1663
1664 if ( (str_s[0] == '"' && str_s[len-1] == '"' ) ||
1665 (str_s[0] == '\'' && str_s[len-1] == '\'') )
1666 {
1667 if (len < 3)
1668 SL_RETURN((-1),_("sh_files_pushdir"));
1669 --len;
1670 p = sh_util_strdup_l(&str_s[1], len);
1671 p[len-1] = '\0';
1672 --len;
1673 }
1674 else
1675 {
1676 p = sh_util_strdup_l(str_s, len);
1677 }
1678
1679 p = sh_files_C_dequote(p, &len);
1680 if (!p || len == 0)
1681 SL_RETURN((-1), _("sh_files_pushdir"));
1682
1683 if (p[0] != '/')
1684 {
1685 rdepth = strtol(p, &tail, 10);
1686 if (tail == p)
1687 {
1688 SH_FREE(p);
1689 SL_RETURN((-1), _("sh_files_pushdir"));
1690 }
1691 }
1692 else
1693 tail = p;
1694
1695
1696 if (tail == p)
1697 {
1698 /* Setting to an invalid number will force MaxRecursionLevel,
1699 * see sh_files_setrec_int()
1700 */
1701 rdepth = (-2);
1702 }
1703 else if ( (rdepth < (-1) || rdepth > 99) ||
1704 ((rdepth == (-1)) && (class != SH_LEVEL_ALLIGNORE)) )
1705 {
1706 SH_FREE(p);
1707 SL_RETURN((-1), _("sh_files_pushdir"));
1708 }
1709
1710 len = sl_strlen(tail);
1711
1712 if (len >= PATH_MAX)
1713 {
1714 tmp = sh_util_safe_name (tail);
1715 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_2LONG,
1716 tmp);
1717 SH_FREE(tmp);
1718 SH_FREE(p);
1719 SL_RETURN((-1), _("sh_files_pushdir"));
1720 }
1721 else if (len < 1)
1722 {
1723 SH_FREE(p);
1724 SL_RETURN((-1), _("sh_files_pushdir"));
1725 }
1726 else if (tail[0] != '/')
1727 {
1728 tmp = sh_util_safe_name (tail);
1729 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_NOPATH,
1730 tmp);
1731 SH_FREE(tmp);
1732 SH_FREE(p);
1733 SL_RETURN((-1), _("sh_files_pushdir"));
1734 }
1735 else
1736 {
1737 if (tail[len-1] == '/' && len > 1)
1738 {
1739 tail[len-1] = '\0';
1740 --len;
1741 }
1742 }
1743
1744#ifdef HAVE_GLOB_H
1745 if (0 == sh_files_has_metachar(tail))
1746 {
1747 sh_files_push_dir_int (class, tail, len, rdepth, sh_files_maskof(class));
1748 }
1749 else
1750 {
1751 sh_files_pushglob (class, which_dirList, tail, rdepth, 0, 0);
1752 }
1753#else
1754 sh_files_push_dir_int (class, tail, len, rdepth, sh_files_maskof(class));
1755#endif
1756
1757 SH_FREE(p);
1758 SL_RETURN((0), _("sh_files_pushdir"));
1759}
1760
1761/**
1762struct sh_dirent {
1763 char * sh_d_name;
1764 struct sh_dirent * next;
1765};
1766**/
1767
1768void kill_sh_dirlist (struct sh_dirent * dirlist)
1769{
1770 struct sh_dirent * this;
1771
1772 while (dirlist)
1773 {
1774 this = dirlist->next;
1775 SH_FREE(dirlist->sh_d_name);
1776 SH_FREE(dirlist);
1777 dirlist = this;
1778 }
1779 return;
1780}
1781
1782/* -- add an entry to a directory listing
1783 */
1784struct sh_dirent * addto_sh_dirlist (struct dirent * thisEntry,
1785 struct sh_dirent * dirlist)
1786{
1787 struct sh_dirent * this;
1788 size_t len;
1789
1790 if (thisEntry == NULL)
1791 return dirlist;
1792
1793 len = sl_strlen(thisEntry->d_name);
1794 if (len == 0)
1795 return dirlist;
1796 ++len;
1797
1798 this = SH_ALLOC(sizeof(struct sh_dirent));
1799 if (!this)
1800 return dirlist;
1801
1802 this->sh_d_name = SH_ALLOC(len);
1803 sl_strlcpy(this->sh_d_name, thisEntry->d_name, len);
1804
1805 this->next = dirlist;
1806 return this;
1807}
1808
1809static int sh_check_hardlinks = S_TRUE;
1810
1811/* Simply sets our boolean as to whether this check is active
1812 */
1813int sh_files_check_hardlinks (const char * opt)
1814{
1815 int i;
1816 SL_ENTER(_("sh_files_check_hardlinks"));
1817 i = sh_util_flagval(opt, &sh_check_hardlinks);
1818 SL_RETURN(i, _("sh_files_check_hardlinks"));
1819}
1820
1821struct sh_hle_struct {
1822 long offset;
1823 char * path;
1824 struct sh_hle_struct * next;
1825};
1826
1827static struct sh_hle_struct * sh_hl_exc = NULL;
1828
1829int sh_files_hle_reg (const char * str)
1830{
1831 long offset;
1832 size_t len;
1833 char * path;
1834
1835 struct sh_hle_struct * tmp = sh_hl_exc;
1836
1837 SL_ENTER(_("sh_files_hle_reg"));
1838
1839 /* Free the linked list if called with NULL argument
1840 */
1841 if (str == NULL)
1842 {
1843 while (tmp)
1844 {
1845 sh_hl_exc = tmp->next;
1846 SH_FREE(tmp->path);
1847 SH_FREE(tmp);
1848 tmp = sh_hl_exc;
1849 }
1850 sh_hl_exc = NULL;
1851 SL_RETURN(0, _("sh_files_hle_reg"));
1852 }
1853
1854 /* We expect 'offset:/path'
1855 */
1856 offset = strtol(str, &path, 0);
1857 if ((path == NULL) || (*path == '\0') || (*path != ':') || (path[1] != '/'))
1858 {
1859 SL_RETURN(-1, _("sh_files_hle_reg"));
1860 }
1861 ++path;
1862 len = 1 + sl_strlen(path);
1863
1864 tmp = SH_ALLOC(sizeof(struct sh_hle_struct));
1865 tmp->path = SH_ALLOC(len);
1866 sl_strlcpy (tmp->path, path, len);
1867 tmp->offset = offset;
1868 tmp->next = sh_hl_exc;
1869 sh_hl_exc = tmp;
1870
1871 SL_RETURN(0, _("sh_files_hle_reg"));
1872}
1873
1874#if !defined(HOST_IS_DARWIN)
1875static int sh_files_hle_test (int offset, char * path)
1876{
1877 struct sh_hle_struct * tmp = sh_hl_exc;
1878
1879 SL_ENTER(_("sh_files_hle_reg"));
1880
1881 while(tmp)
1882 {
1883 if ((offset == tmp->offset) && (0 == strcmp(path, tmp->path)))
1884 {
1885 SL_RETURN(0, _("sh_files_hle_test"));
1886 }
1887 tmp = tmp->next;
1888 }
1889 SL_RETURN(-1, _("sh_files_hle_test"));
1890}
1891#endif
1892
1893static void * sh_dummy_dirlist;
1894static void * sh_dummy_tmpcat;
1895
1896/* -- Check a single directory and its content. Does not
1897 * check the directory inode itself.
1898 */
1899int sh_files_checkdir (int iclass, unsigned long check_mask,
1900 int idepth, char * iname,
1901 char * relativeName)
1902{
1903 struct sh_dirent * dirlist;
1904 struct sh_dirent * dirlist_orig;
1905
1906 DIR * thisDir = NULL;
1907 struct dirent * thisEntry;
1908 int status;
1909 int dummy = S_FALSE;
1910 dir_type * theDir;
1911 ShFileType checkit;
1912 static unsigned int state = 1;
1913
1914 file_type * theFile;
1915 char * tmpname;
1916 char * tmpcat;
1917 char errbuf[SH_ERRBUF_SIZE];
1918
1919 int rdepth = 0;
1920 int class = 0;
1921 volatile int rdepth_next;
1922 volatile int class_next;
1923 volatile int file_class_next;
1924 volatile unsigned long check_mask_next;
1925 volatile unsigned long file_check_mask_next;
1926
1927 volatile int checked_flag = S_FALSE;
1928 volatile int cchecked_flag = S_FALSE;
1929
1930 dirstack_t * dst_ptr;
1931 dirstack_t * tmp_ptr;
1932
1933 int hardlink_num = 0;
1934#if !defined(HOST_IS_DARWIN)
1935 size_t len;
1936#endif
1937
1938 SL_ENTER(_("sh_files_checkdir"));
1939
1940 if (sig_urgent > 0) {
1941 SL_RETURN((0), _("sh_files_checkdir"));
1942 }
1943
1944 if (iname == NULL || idepth < (-1))
1945 SL_RETURN((-1), _("sh_files_checkdir"));
1946
1947 if (idepth < 0)
1948 {
1949 /* hash_remove_tree (iname); */
1950 SL_RETURN((0), _("sh_files_checkdir"));
1951 }
1952
1953 rdepth = idepth;
1954 class = iclass;
1955
1956 tmpname = sh_util_safe_name (iname);
1957
1958 /* ---- check for obscure name ----
1959 */
1960 if (iclass != SH_LEVEL_ALLIGNORE)
1961 {
1962 sh_util_obscurename (ShDFLevel[SH_ERR_T_NAME], iname, S_TRUE);
1963 }
1964
1965 if (flag_err_info == SL_TRUE)
1966 {
1967 char pstr[32];
1968
1969 sl_strlcpy(pstr, sh_hash_getpolicy(iclass), sizeof(pstr));
1970 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_CHK, pstr, tmpname);
1971 }
1972
1973 /* ---- check input ----
1974 */
1975 if ( sl_strlen(iname) >= PATH_MAX)
1976 {
1977 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
1978 MSG_FI_2LONG,
1979 tmpname);
1980 SH_FREE(tmpname);
1981 SL_RETURN((-1), _("sh_files_checkdir"));
1982 }
1983
1984 /* ---- check for absolute path ---- */
1985 if ( iname[0] != '/')
1986 {
1987 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
1988 MSG_FI_NOPATH,
1989 tmpname);
1990 SH_FREE(tmpname);
1991 SL_RETURN((-1), _("sh_files_checkdir"));
1992 }
1993
1994 /* ---- stat the directory ----
1995 */
1996 theFile = SH_ALLOC(sizeof(file_type));
1997 sl_strlcpy (theFile->fullpath, iname, PATH_MAX);
1998 theFile->attr_string = NULL;
1999 theFile->link_path = NULL;
2000 theFile->check_mask = check_mask;
2001
2002 (void) relativeName;
2003 status = sh_unix_getinfo (ShDFLevel[SH_ERR_T_DIR],
2004 iname,
2005 theFile, NULL, iclass);
2006
2007 if ((sig_termfast == 1) || (sig_terminate == 1))
2008 {
2009 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2010 if (theFile->link_path) SH_FREE(theFile->link_path);
2011 SH_FREE(theFile);
2012 SH_FREE(tmpname);
2013 SL_RETURN((0), _("sh_files_checkdir"));
2014 }
2015
2016 if (status == -1)
2017 {
2018 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2019 if (theFile->link_path) SH_FREE(theFile->link_path);
2020 SH_FREE(theFile);
2021 SH_FREE(tmpname);
2022 SL_RETURN((-1), _("sh_files_checkdir"));
2023 }
2024
2025 if (theFile->c_mode[0] != 'd')
2026 {
2027 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
2028 MSG_FI_NODIR,
2029 tmpname);
2030 ++sh.statistics.files_nodir;
2031 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2032 if (theFile->link_path) SH_FREE(theFile->link_path);
2033 SH_FREE(theFile);
2034 SH_FREE(tmpname);
2035 SL_RETURN((-1), _("sh_files_checkdir"));
2036 }
2037
2038 if ((sh.flag.inotify & SH_INOTIFY_INSCAN) != 0)
2039 {
2040 sh_inotify_add_watch_later(iname, &sh_file_watches, &status,
2041 iclass, check_mask, SH_INOTIFY_DIR, idepth);
2042 }
2043
2044 hardlink_num = theFile->hardlinks;
2045
2046 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2047 if (theFile->link_path) SH_FREE(theFile->link_path);
2048 SH_FREE(theFile);
2049
2050 /* ---- open directory for reading ----
2051 *
2052 * opendir() will fail with ENOTDIR if the path has been changed
2053 * to a non-directory in between lstat() and opendir().
2054 */
2055 thisDir = opendir (iname);
2056
2057 if (thisDir == NULL)
2058 {
2059 status = errno;
2060 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
2061 MSG_E_OPENDIR,
2062 sh_error_message (status, errbuf, sizeof(errbuf)), tmpname);
2063 SH_FREE(tmpname);
2064 SL_RETURN((-1), _("sh_files_checkdir"));
2065 }
2066
2067 theDir = SH_ALLOC(sizeof(dir_type));
2068
2069 theDir->NumRegular = 0;
2070 theDir->NumDirs = 0;
2071 theDir->NumSymlinks = 0;
2072 theDir->NumFifos = 0;
2073 theDir->NumSockets = 0;
2074 theDir->NumCDev = 0;
2075 theDir->NumBDev = 0;
2076 theDir->NumDoor = 0;
2077 theDir->NumPort = 0;
2078 theDir->NumAll = 0;
2079 theDir->TotalBytes = 0;
2080 sl_strlcpy (theDir->DirPath, iname, PATH_MAX);
2081
2082
2083 sh_dummy_dirlist = (void *) &dirlist;
2084 sh_dummy_tmpcat = (void *) &tmpcat;
2085
2086 /* ---- read ----
2087 */
2088 SH_MUTEX_LOCK(mutex_readdir);
2089
2090 dirlist = NULL;
2091 dirlist_orig = NULL;
2092
2093 do {
2094 thisEntry = readdir (thisDir);
2095 if (thisEntry != NULL)
2096 {
2097 ++theDir->NumAll;
2098 if (sl_strcmp (thisEntry->d_name, ".") == 0)
2099 {
2100 ++theDir->NumDirs;
2101 continue;
2102 }
2103 if (sl_strcmp (thisEntry->d_name, "..") == 0)
2104 {
2105 ++theDir->NumDirs;
2106 continue;
2107 }
2108 dirlist = addto_sh_dirlist (thisEntry, dirlist);
2109 }
2110 } while (thisEntry != NULL);
2111
2112 SH_MUTEX_UNLOCK(mutex_readdir);
2113
2114 closedir (thisDir);
2115
2116 ++sh.statistics.dirs_checked;
2117
2118 dirlist_orig = dirlist;
2119
2120 do {
2121
2122 /* If the directory is empty, dirlist = NULL
2123 */
2124 if (!dirlist)
2125 break;
2126
2127 if (sig_termfast == 1)
2128 {
2129 SH_FREE(theDir);
2130 SH_FREE(tmpname);
2131 SL_RETURN((0), _("sh_files_checkdir"));
2132 }
2133
2134 BREAKEXIT(sh_derr);
2135
2136#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_RAND_R)
2137 if (0 == (rand_r(&state) % 5)) (void) sh_derr();
2138#else
2139 if (0 == state * (rand() % 5)) (void) sh_derr();
2140#endif
2141
2142 /* ---- Check the file. ----
2143 */
2144 tmpcat = SH_ALLOC(PATH_MAX);
2145 sl_strlcpy(tmpcat, iname, PATH_MAX);
2146 if (sl_strlen(tmpcat) > 1 || tmpcat[0] != '/')
2147 sl_strlcat(tmpcat, "/", PATH_MAX);
2148 sl_strlcat(tmpcat, dirlist->sh_d_name, PATH_MAX);
2149
2150 rdepth_next = rdepth - 1;
2151 class_next = class;
2152 check_mask_next = check_mask;
2153 file_class_next = class;
2154 file_check_mask_next = check_mask;
2155 checked_flag = -1;
2156 cchecked_flag = -1;
2157
2158 /* Wed Aug 24 2005 compare against dirListOne, dirListTwo
2159 * this fixes the problem that the directory special file
2160 * is checked with the policy of the parent directory
2161 */
2162 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
2163 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
2164 dst_ptr = (dirstack_t *) zAVLSearch(zdirListOne, tmpcat);
2165
2166 if (dst_ptr)
2167 {
2168 /* Tue Aug 6 22:13:27 CEST 2002 introduce file_class_next
2169 * this fixes the problem that a policy for the directory
2170 * inode erroneously becomes a policy for the directory itself.
2171 */
2172 file_class_next = dst_ptr->class;
2173 file_check_mask_next = dst_ptr->check_mask;
2174 checked_flag = dst_ptr->checked;
2175 cchecked_flag = dst_ptr->childs_checked;
2176 }
2177
2178 if (checked_flag == -1)
2179 {
2180 dst_ptr = (dirstack_t *) zAVLSearch(zdirListTwo, tmpcat);
2181
2182 if (dst_ptr)
2183 {
2184 /* Tue Aug 6 22:13:27 CEST 2002 introduce file_class_next
2185 * this fixes the problem that a policy for the directory
2186 * inode erroneously becomes a policy for the directory itself.
2187 */
2188 file_class_next = dst_ptr->class;
2189 file_check_mask_next = dst_ptr->check_mask;
2190 checked_flag = dst_ptr->checked;
2191 cchecked_flag = dst_ptr->childs_checked;
2192 }
2193 }
2194 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
2195
2196 SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
2197 dst_ptr = (dirstack_t *) zAVLSearch(zfileList, tmpcat);
2198
2199 if (dst_ptr)
2200 {
2201 /* Tue Aug 6 22:13:27 CEST 2002 introduce file_class_next
2202 * this fixes the problem that a policy for the directory
2203 * inode erroneously becomes a policy for the directory itself.
2204 */
2205 file_class_next = dst_ptr->class;
2206 file_check_mask_next = dst_ptr->check_mask;
2207 checked_flag = dst_ptr->checked;
2208 /* not set, hence always FALSE */
2209 /* cchecked_flag = dst_ptr->childs_checked; */
2210
2211 if (checked_flag != S_TRUE)
2212 {
2213 /* -- need to check the file itself --
2214 */
2215 if (sh.flag.reportonce == S_TRUE)
2216 dummy = dst_ptr->is_reported;
2217 }
2218 }
2219 SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
2220
2221 /* ---- Has been checked already. ----
2222 */
2223 if (checked_flag == S_TRUE && cchecked_flag == S_TRUE)
2224 {
2225 /* Mar 11 2004 get ftype for complete directory count
2226 */
2227 checkit = sh_unix_get_ftype(tmpcat);
2228 if (checkit == SH_FILE_DIRECTORY)
2229 {
2230 ++theDir->NumDirs;
2231 }
2232 SH_FREE(tmpcat);
2233 dirlist = dirlist->next;
2234 continue;
2235 }
2236
2237 /* --- May be true, false, or not found. ---
2238 */
2239 if (checked_flag == S_TRUE)
2240 {
2241 /* -- need only the file type --
2242 */
2243 checkit = sh_unix_get_ftype(tmpcat);
2244 }
2245 else
2246 {
2247 /* -- need to check the file itself --
2248 */
2249 /* -- moved up --
2250 * if (dst_ptr && sh.flag.reportonce == S_TRUE)
2251 * dummy = dst_ptr->is_reported;
2252 */
2253
2254 checkit = sh_files_filecheck (file_class_next, file_check_mask_next,
2255 iname,
2256 dirlist->sh_d_name,
2257 &dummy, 0);
2258
2259
2260 SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
2261 dst_ptr = (dirstack_t *) zAVLSearch(zfileList, tmpcat);
2262
2263 if (dst_ptr && checked_flag == S_FALSE)
2264 dst_ptr->checked = S_TRUE;
2265
2266 /* Thu Mar 7 15:09:40 CET 2002 Propagate the 'reported' flag
2267 */
2268 if (dst_ptr && sh.flag.reportonce == S_TRUE)
2269 dst_ptr->is_reported = dummy;
2270
2271 if (dst_ptr)
2272 dst_ptr->childs_checked = S_TRUE;
2273 SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
2274 }
2275
2276 if (checkit == SH_FILE_REGULAR)
2277 ++theDir->NumRegular;
2278
2279 else if (checkit == SH_FILE_DIRECTORY)
2280 {
2281 ++theDir->NumDirs;
2282
2283 if (rdepth_next >= 0 && cchecked_flag != S_TRUE)
2284 {
2285 rdepth_next = rdepth - 1;
2286
2287 /* check whether the new directory is in the
2288 * list with a recursion depth already defined
2289 */
2290 checked_flag = -1;
2291 cchecked_flag = -1;
2292
2293 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
2294 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
2295 tmp_ptr = (dirstack_t *) zAVLSearch(zdirListOne, tmpcat);
2296
2297 if (tmp_ptr)
2298 {
2299 TPT((0, FIL__, __LINE__,
2300 _("msg=<%s -> recursion depth %d\n>"),
2301 tmp_ptr->name, tmp_ptr->rdepth));
2302 rdepth_next = tmp_ptr->rdepth;
2303 class_next = tmp_ptr->class;
2304 check_mask_next = tmp_ptr->check_mask;
2305 /* 28. Aug 2001 reversed
2306 */
2307 cchecked_flag = tmp_ptr->childs_checked;
2308 checked_flag = tmp_ptr->checked;
2309 }
2310
2311 if (checked_flag == -1)
2312 {
2313 tmp_ptr = (dirstack_t *) zAVLSearch(zdirListTwo, tmpcat);
2314
2315 if (tmp_ptr)
2316 {
2317 TPT((0, FIL__, __LINE__,
2318 _("msg=<%s -> recursion depth %d\n>"),
2319 tmp_ptr->name, tmp_ptr->rdepth));
2320 rdepth_next = tmp_ptr->rdepth;
2321 class_next = tmp_ptr->class;
2322 check_mask_next = tmp_ptr->check_mask;
2323 /* 28. Aug 2001 reversed
2324 */
2325 cchecked_flag = tmp_ptr->childs_checked;
2326 checked_flag = tmp_ptr->checked;
2327 }
2328 }
2329
2330 if (tmp_ptr && cchecked_flag == S_FALSE)
2331 {
2332 tmp_ptr->childs_checked = S_TRUE;
2333 /*
2334 * 04. Feb 2006 avoid double checking
2335 */
2336 tmp_ptr->checked = S_TRUE;
2337 }
2338 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
2339
2340 if (cchecked_flag == S_FALSE)
2341 {
2342 sh_files_checkdir (class_next, check_mask_next, rdepth_next,
2343 tmpcat, dirlist->sh_d_name);
2344 /*
2345 tmp_ptr->childs_checked = S_TRUE;
2346 tmp_ptr->checked = S_TRUE;
2347 */
2348 }
2349 else if (checked_flag == -1)
2350 sh_files_checkdir (class_next, check_mask_next, rdepth_next,
2351 tmpcat, dirlist->sh_d_name);
2352
2353 }
2354 }
2355
2356 else if (checkit == SH_FILE_SYMLINK) ++theDir->NumSymlinks;
2357 else if (checkit == SH_FILE_FIFO) ++theDir->NumFifos;
2358 else if (checkit == SH_FILE_SOCKET) ++theDir->NumSockets;
2359 else if (checkit == SH_FILE_CDEV) ++theDir->NumCDev;
2360 else if (checkit == SH_FILE_BDEV) ++theDir->NumBDev;
2361 else if (checkit == SH_FILE_DOOR) ++theDir->NumDoor;
2362 else if (checkit == SH_FILE_PORT) ++theDir->NumPort;
2363
2364 SH_FREE(tmpcat);
2365
2366 if ((sig_termfast == 1) || (sig_terminate == 1))
2367 {
2368 SH_FREE(theDir);
2369 sh_dummy_dirlist = NULL;
2370 SH_FREE(tmpname);
2371 SL_RETURN((0), _("sh_files_checkdir"));
2372 }
2373
2374 dirlist = dirlist->next;
2375
2376 /* -- moved up, only affects zfileList anyway
2377 * if (dst_ptr)
2378 * dst_ptr->childs_checked = S_TRUE;
2379 */
2380
2381 } while (dirlist != NULL);
2382
2383 if (flag_err_info == SL_TRUE)
2384 {
2385 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DSUM,
2386 theDir->NumDirs,
2387 theDir->NumRegular,
2388 theDir->NumSymlinks,
2389 theDir->NumFifos,
2390 theDir->NumSockets,
2391 theDir->NumCDev,
2392 theDir->NumBDev);
2393 }
2394
2395 kill_sh_dirlist (dirlist_orig);
2396
2397#if !defined(HOST_IS_DARWIN)
2398 /*
2399 * Hardlink check; not done on MacOS X because of resource forks
2400 */
2401 if ((sh_check_hardlinks == S_TRUE) && (hardlink_num != theDir->NumDirs))
2402 {
2403 if (0 != sh_files_hle_test(hardlink_num-theDir->NumDirs, iname))
2404 {
2405 len = strlen(tmpname);
2406 if (sl_ok_adds(len, 256))
2407 len += 256;
2408 tmpcat = SH_ALLOC(len);
2409 sl_snprintf(tmpcat, len,
2410 _("%s: subdirectory count (%d) != hardlinks (%d)"),
2411 tmpname, theDir->NumDirs, hardlink_num);
2412 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
2413 MSG_E_SUBGEN, tmpcat, _("sh_files_checkdir"));
2414 SH_FREE(tmpcat);
2415 }
2416 }
2417#endif
2418
2419 SH_FREE(tmpname);
2420 SH_FREE(theDir);
2421
2422 sh_dummy_dirlist = NULL;
2423
2424 SL_RETURN((0), _("sh_files_checkdir"));
2425}
2426
2427int get_the_fd (SL_TICKET ticket);
2428
2429static int sh_use_rsrc = S_FALSE;
2430
2431int sh_files_use_rsrc(const char * str)
2432{
2433 return sh_util_flagval(str, &sh_use_rsrc);
2434}
2435
2436static void * sh_dummy_fileName;
2437static void * sh_dummy_tmpname;
2438static void * sh_dummy_tmpdir;
2439
2440ShFileType sh_files_filecheck (int class, unsigned long check_mask,
2441 const char * dirName,
2442 const char * infileName,
2443 int * reported,
2444 int rsrcflag)
2445{
2446 /* 28 Aug 2001 allow NULL fileName
2447 */
2448 char * fullpath;
2449 char fileHash[2*(KEY_LEN + 1)];
2450 int status;
2451 file_type * theFile;
2452 char * tmpdir;
2453 char * tmpname;
2454 const char * fileName;
2455#if !defined(O_NOATIME)
2456 struct utimbuf utime_buf;
2457#endif
2458 static unsigned int state = 1;
2459 char sc;
2460
2461 SL_ENTER(_("sh_files_filecheck"));
2462
2463 fullpath = SH_ALLOC(PATH_MAX);
2464 theFile = SH_ALLOC(sizeof(file_type));
2465
2466 /* Take the address to keep gcc from putting it into a register.
2467 * Avoids the 'clobbered by longjmp' warning.
2468 */
2469 sh_dummy_fileName = (void *) &fileName;
2470 sh_dummy_tmpname = (void *) &tmpname;
2471 sh_dummy_tmpdir = (void *) &tmpdir;
2472
2473 BREAKEXIT(sh_derr);
2474
2475#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_RAND_R)
2476 if (0 == (rand_r(&state) % 2)) (void) sh_derr();
2477#else
2478 if (0 == state * (rand() % 2)) (void) sh_derr();
2479#endif
2480
2481 if (dirName && infileName && (dirName[0] == '/') && (dirName[1] == '\0')
2482 && (infileName[0] == '/') && (infileName[1] == '\0'))
2483 {
2484 fileName = NULL;
2485 }
2486 else
2487 {
2488 fileName = infileName;
2489 }
2490
2491 /* fileName may be NULL if this is a directory
2492 */
2493 if (dirName == NULL /* || fileName == NULL */)
2494 {
2495 SH_MUTEX_LOCK(mutex_thread_nolog);
2496 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_NULL);
2497 SH_MUTEX_UNLOCK(mutex_thread_nolog);
2498 SH_FREE(fullpath);
2499 SH_FREE(theFile);
2500 SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
2501 }
2502
2503 if ((fileName != NULL) && (class != SH_LEVEL_ALLIGNORE) &&
2504 (0 != sh_util_obscurename (ShDFLevel[SH_ERR_T_NAME],
2505 fileName, S_FALSE)))
2506 {
2507 if ((dirName != NULL) && (dirName[0] == '/') && (dirName[1] == '\0'))
2508 {
2509 tmpname = sh_util_safe_name (fileName);
2510 SH_MUTEX_LOCK(mutex_thread_nolog);
2511 sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, 0,
2512 MSG_FI_OBSC2,
2513 "", tmpname);
2514 SH_MUTEX_UNLOCK(mutex_thread_nolog);
2515 SH_FREE(tmpname);
2516 }
2517 else
2518 {
2519 tmpdir = sh_util_safe_name (dirName);
2520 tmpname = sh_util_safe_name (fileName);
2521 SH_MUTEX_LOCK(mutex_thread_nolog);
2522 sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, 0,
2523 MSG_FI_OBSC2,
2524 tmpdir, tmpname);
2525 SH_MUTEX_UNLOCK(mutex_thread_nolog);
2526 SH_FREE(tmpname);
2527 SH_FREE(tmpdir);
2528 }
2529 }
2530
2531 /* sh_files_fullpath accepts NULL fileName
2532 */
2533 if (0 != sh_files_fullpath (dirName, fileName, fullpath))
2534 {
2535 tmpdir = sh_util_safe_name (dirName);
2536 tmpname = sh_util_safe_name (fileName);
2537 SH_MUTEX_LOCK(mutex_thread_nolog);
2538 sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, 0,
2539 MSG_FI_2LONG2,
2540 tmpdir, tmpname);
2541 SH_MUTEX_UNLOCK(mutex_thread_nolog);
2542 SH_FREE(tmpname);
2543 SH_FREE(tmpdir);
2544 SH_FREE(fullpath);
2545 SH_FREE(theFile);
2546 SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
2547 }
2548
2549
2550 /* stat the file and determine checksum (if a regular file)
2551 */
2552 sl_strlcpy (theFile->fullpath, fullpath, PATH_MAX);
2553 theFile->check_mask = check_mask /* sh_files_maskof(class) */;
2554 theFile->file_reported = (*reported);
2555 theFile->attr_string = NULL;
2556 theFile->link_path = NULL;
2557
2558 TPT(( 0, FIL__, __LINE__, _("msg=<checking file: %s>\n"), fullpath));
2559
2560 status = sh_unix_getinfo ( (class == SH_LEVEL_ALLIGNORE) ?
2561 ShDFLevel[class] : ShDFLevel[SH_ERR_T_FILE],
2562 fileName,
2563 theFile, fileHash, class);
2564
2565 if (status != 0)
2566 {
2567 TPT(( 0, FIL__, __LINE__, _("msg=<file: %s> status=<%d>\n"),
2568 fullpath, status));
2569 if (class == SH_LEVEL_ALLIGNORE && sh.flag.checkSum != SH_CHECK_INIT)
2570 sh_hash_set_visited_true (fullpath);
2571 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2572 if (theFile->link_path) SH_FREE(theFile->link_path);
2573 SH_FREE(fullpath);
2574 SH_FREE(theFile);
2575 SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
2576 }
2577
2578 if (sig_termfast == 1) {
2579 goto ret_point;
2580 }
2581
2582 /* report
2583 */
2584 if ((flag_err_debug == SL_TRUE) && (theFile->c_mode[0] == '-'))
2585 {
2586 tmpname = sh_util_safe_name (fullpath); /* fixed in 1.5.4 */
2587 SH_MUTEX_LOCK(mutex_thread_nolog);
2588 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_CSUM,
2589 fileHash, tmpname);
2590 SH_MUTEX_UNLOCK(mutex_thread_nolog);
2591 SH_FREE(tmpname);
2592 }
2593 ++sh.statistics.files_checked;
2594
2595 if ( sh.flag.checkSum == SH_CHECK_INIT /* && sh.flag.update == S_FALSE */)
2596 {
2597 sh_hash_pushdata (theFile, fileHash);
2598 }
2599 else if (sh.flag.checkSum == SH_CHECK_CHECK
2600 /* && theFile.c_mode[0] == '-' */
2601 /* && class != SH_LEVEL_ALLIGNORE */
2602 )
2603 {
2604 sh_hash_compdata (class, theFile, fileHash, NULL, -1);
2605 }
2606
2607 (*reported) = theFile->file_reported;
2608
2609 /* reset the access time
2610 */
2611#if !defined(O_NOATIME)
2612 if (class == SH_LEVEL_NOIGNORE && (theFile->check_mask & MODI_ATM) != 0)
2613 {
2614 utime_buf.actime = (time_t) theFile->atime;
2615 utime_buf.modtime = (time_t) theFile->mtime;
2616
2617 retry_aud_utime (FIL__, __LINE__, fullpath, &utime_buf);
2618 }
2619#endif
2620
2621#if defined(HOST_IS_DARWIN)
2622 /*
2623 * Check for resource fork
2624 */
2625 if ( (sh_use_rsrc == S_TRUE) && (theFile->c_mode[0] != 'd') && (rsrcflag == 0) )
2626 {
2627 int dummy;
2628 static int rsrc_init = 0;
2629 static char rsrc[17];
2630 char * testpath = SH_ALLOC(PATH_MAX);
2631
2632 if (rsrc_init == 0) {
2633 sl_strlcpy(rsrc, _("..namedfork/rsrc"), 17);
2634 rsrc_init = 1;
2635 }
2636 sl_strlcpy (testpath, fullpath, PATH_MAX);
2637 sl_strlcat (testpath, "/", PATH_MAX);
2638 sl_strlcat (testpath, rsrc, PATH_MAX);
2639
2640 if (sl_strlen(testpath) == (17 + sl_strlen(fullpath)))
2641 {
2642 if (S_TRUE == sh_unix_file_exists (testpath))
2643 {
2644 sh_files_filecheck (class, check_mask, fullpath, rsrc, &dummy, 1);
2645 }
2646 }
2647 SH_FREE(testpath);
2648 }
2649#else
2650 (void) rsrcflag; /* avoid compiler warning */
2651#endif
2652
2653 ret_point:
2654
2655 sc = theFile->c_mode[0];
2656
2657 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2658 if (theFile->link_path) SH_FREE(theFile->link_path);
2659 SH_FREE(fullpath);
2660 SH_FREE(theFile);
2661
2662 switch (sc)
2663 {
2664 case '-': SL_RETURN(SH_FILE_REGULAR, _("sh_files_filecheck"));
2665 case 'l': SL_RETURN(SH_FILE_SYMLINK, _("sh_files_filecheck"));
2666 case 'd': SL_RETURN(SH_FILE_DIRECTORY, _("sh_files_filecheck"));
2667 case 'c': SL_RETURN(SH_FILE_CDEV, _("sh_files_filecheck"));
2668 case 'b': SL_RETURN(SH_FILE_BDEV, _("sh_files_filecheck"));
2669 case '|': SL_RETURN(SH_FILE_FIFO, _("sh_files_filecheck"));
2670 case 'D': SL_RETURN(SH_FILE_DOOR, _("sh_files_filecheck"));
2671 case 'P': SL_RETURN(SH_FILE_PORT, _("sh_files_filecheck"));
2672 case 's': SL_RETURN(SH_FILE_SOCKET, _("sh_files_filecheck"));
2673 default: SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
2674 }
2675
2676 /* notreached */
2677}
2678
2679/* concatenate statpath = testdir"/"d_name
2680 */
2681static int sh_files_fullpath (const char * testdir, const char * d_name,
2682 char * statpath)
2683{
2684 int llen = 0;
2685
2686 SL_ENTER(_("sh_files_fullpath"));
2687
2688 if (testdir != NULL)
2689 {
2690 if ( (llen = sl_strlen(testdir)) > (PATH_MAX-2) )
2691 SL_RETURN((-1),_("sh_files_fullpath"));
2692 sl_strlcpy(statpath, testdir, PATH_MAX - 1);
2693 }
2694 if (d_name != NULL)
2695 {
2696 if (llen > 1 || statpath[0] != '/')
2697 sl_strlcat(statpath, "/", PATH_MAX);
2698 if ((sl_strlen(d_name) + sl_strlen(statpath)) >= PATH_MAX)
2699 SL_RETURN((-1),_("sh_files_fullpath"));
2700 sl_strlcat(statpath, d_name, PATH_MAX);
2701 }
2702 if (statpath == NULL)
2703 SL_RETURN((-1),_("sh_files_fullpath"));
2704 SL_RETURN((0),_("sh_files_fullpath"));
2705}
2706
2707/* -----------------------------------
2708 * Routines required for inotify
2709 * -----------------------------------
2710 */
2711int sh_files_search_dir(char * name, int * class,
2712 unsigned long *check_mask, int *reported,
2713 int * rdepth)
2714{
2715 volatile int retval = 0;
2716#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
2717 sh_globstack_t * testPattern;
2718 zAVLCursor cursor;
2719#endif
2720 dirstack_t * item;
2721
2722 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
2723 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
2724
2725 item = zAVLSearch(zdirListOne, name);
2726
2727 if (item)
2728 {
2729 *check_mask = item->check_mask;
2730 *class = item->class;
2731 *reported = item->is_reported;
2732 *rdepth = item->rdepth;
2733 item->checked = S_FALSE;
2734 item->childs_checked = S_FALSE;
2735 item->is_reported = S_FALSE;
2736 retval = 1;
2737 goto out;
2738 }
2739
2740 item = zAVLSearch(zdirListTwo, name);
2741
2742 if (item)
2743 {
2744 *check_mask = item->check_mask;
2745 *class = item->class;
2746 *reported = item->is_reported;
2747 *rdepth = item->rdepth;
2748 item->checked = S_FALSE;
2749 item->childs_checked = S_FALSE;
2750 item->is_reported = S_FALSE;
2751 retval = 1;
2752 goto out;
2753 }
2754
2755#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
2756 SH_MUTEX_LOCK(mutex_zglob);
2757 for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList);
2758 testPattern;
2759 testPattern = (sh_globstack_t *) zAVLNext (&cursor))
2760 {
2761 if (testPattern->type == SH_LIST_DIR1 ||
2762 testPattern->type == SH_LIST_DIR2)
2763 {
2764 if (0 == fnmatch(testPattern->name, name, FNM_PATHNAME|FNM_PERIOD))
2765 {
2766 *check_mask = testPattern->check_mask;
2767 *class = testPattern->class;
2768 *rdepth = testPattern->rdepth;
2769 retval = 1;
2770 break;
2771 }
2772
2773 }
2774 }
2775 SH_MUTEX_UNLOCK(mutex_zglob);
2776#endif
2777 out:
2778 ; /* 'label at end of compound statement' */
2779 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
2780 return retval;
2781}
2782
2783int sh_files_search_file(char * name, int * class,
2784 unsigned long *check_mask, int *reported)
2785{
2786 volatile int retval = 0;
2787#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
2788 sh_globstack_t * testPattern;
2789 zAVLCursor cursor;
2790#endif
2791 dirstack_t * item;
2792
2793 SH_MUTEX_LOCK(mutex_zfiles);
2794 item = zAVLSearch(zfileList, name);
2795
2796 if (item)
2797 {
2798 *check_mask = item->check_mask;
2799 *class = item->class;
2800 *reported = item->is_reported;
2801 retval = 1;
2802 }
2803 SH_MUTEX_UNLOCK(mutex_zfiles);
2804
2805#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
2806 if (retval == 0)
2807 {
2808 SH_MUTEX_LOCK(mutex_zglob);
2809 for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList);
2810 testPattern;
2811 testPattern = (sh_globstack_t *) zAVLNext (&cursor))
2812 {
2813 if (testPattern->type == SH_LIST_FILE)
2814 {
2815 if (0 == fnmatch(testPattern->name, name,
2816 FNM_PATHNAME|FNM_PERIOD))
2817 {
2818 *check_mask = testPattern->check_mask;
2819 *class = testPattern->class;
2820 retval = 1;
2821 break;
2822 }
2823
2824 }
2825 }
2826 SH_MUTEX_UNLOCK(mutex_zglob);
2827 }
2828#endif
2829
2830 return retval;
2831}
2832
2833void sh_files_set_file_reported(const char * name)
2834{
2835 dirstack_t * item;
2836
2837 SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
2838 item = zAVLSearch(zfileList, name);
2839
2840 if (item)
2841 {
2842 if (sh.flag.reportonce == S_TRUE)
2843 SET_SH_FFLAG_REPORTED(item->is_reported);
2844 }
2845 SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
2846 return;
2847}
2848
2849void sh_files_clear_file_reported(const char * name)
2850{
2851 dirstack_t * item;
2852
2853 SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
2854 item = zAVLSearch(zfileList, name);
2855
2856 if (item)
2857 {
2858 CLEAR_SH_FFLAG_REPORTED(item->is_reported);
2859 }
2860 SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
2861 return;
2862}
2863
2864/* -----------------------------------
2865 *
2866 * The following two routines serve to
2867 * verify that the user has selected
2868 * a proper setup for file policies.
2869 *
2870 * -----------------------------------
2871 */
2872static int check_file(char * name)
2873{
2874 dirstack_t * pfilL;
2875 zAVLCursor cursor;
2876 volatile int retval = -1;
2877
2878 SL_ENTER(_("check_file"));
2879
2880 if (SH_FILE_DIRECTORY == sh_unix_get_ftype(name))
2881 SL_RETURN(0, _("check_file"));
2882
2883 for (pfilL = (dirstack_t *) zAVLFirst (&cursor, zfileList); pfilL;
2884 pfilL = (dirstack_t *) zAVLNext (&cursor))
2885 {
2886 if (0 == strcmp(name, pfilL->name) &&
2887 (pfilL->check_mask & MODI_ATM) == 0 &&
2888 (pfilL->check_mask & MODI_CTM) == 0 &&
2889 (pfilL->check_mask & MODI_MTM) == 0)
2890 {
2891 retval = 0;
2892 break;
2893 }
2894 }
2895
2896 SL_RETURN(retval, _("check_file"));
2897}
2898
2899static void * sh_dummy_pdirL;
2900
2901int sh_files_test_setup_int (zAVLTree * tree)
2902{
2903 int dlen, flen;
2904 zAVLCursor cursor1;
2905 zAVLCursor cursor2;
2906
2907 dirstack_t * pdirL;
2908 dirstack_t * pfilL;
2909
2910 SL_ENTER(_("sh_files_test_setup"));
2911
2912 sh_dummy_pdirL = (void *) &pdirL;
2913
2914 for (pdirL = (dirstack_t *) zAVLFirst (&cursor1, tree); pdirL;
2915 pdirL = (dirstack_t *) zAVLNext (&cursor1))
2916 {
2917 dlen = strlen(pdirL->name);
2918
2919 SH_MUTEX_LOCK(mutex_zfiles);
2920 for (pfilL = (dirstack_t *) zAVLFirst (&cursor2, zfileList); pfilL;
2921 pfilL = (dirstack_t *) zAVLNext (&cursor2))
2922 {
2923 flen = strlen(pfilL->name);
2924
2925 /* check whether file is in tree of dir
2926 */
2927 if ((pfilL->class == SH_LEVEL_READONLY) ||
2928 (pfilL->class == SH_LEVEL_NOIGNORE))
2929 {
2930 ; /* do nothing */
2931 }
2932 else
2933 {
2934 if ((flen > (dlen+1)) &&
2935 (pfilL->name[dlen] == '/') &&
2936 (NULL == strchr(&(pfilL->name[dlen+1]), '/')) && /*30-5-01*/
2937 (0 == strncmp(pfilL->name, pdirL->name, dlen)))
2938 {
2939 if ((pdirL->check_mask & MODI_ATM) != 0 ||
2940 (pdirL->check_mask & MODI_MTM) != 0 ||
2941 (pdirL->check_mask & MODI_CTM) != 0)
2942 {
2943 if (check_file (pdirL->name) != 0)
2944 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_COLL,
2945 pdirL->name, pfilL->name);
2946 }
2947 }
2948 }
2949 }
2950 SH_MUTEX_UNLOCK(mutex_zfiles);
2951 }
2952
2953 SL_RETURN((0), _("sh_files_test_setup"));
2954}
2955
2956int sh_files_test_double (zAVLTree * firstList, zAVLTree * secondList)
2957{
2958 int retval = 0;
2959 zAVLCursor cursor;
2960 dirstack_t * first;
2961
2962 for (first = (dirstack_t *) zAVLFirst (&cursor, firstList); first;
2963 first = (dirstack_t *) zAVLNext (&cursor))
2964 {
2965
2966 if (NULL != zAVLSearch(secondList, first->name))
2967 {
2968 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
2969 first->name);
2970 retval = 1;
2971 }
2972 }
2973 return retval;
2974}
2975
2976extern void aud_exit (const char * file, int line, int fd);
2977
2978int sh_files_test_setup ()
2979{
2980 int retval;
2981
2982 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
2983 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
2984 /* Test for modifications allowed in ReadOnly directory
2985 */
2986 sh_files_test_setup_int (zdirListOne);
2987 sh_files_test_setup_int (zdirListTwo);
2988
2989 /* Test for files/dirz defined twice
2990 */
2991 retval = sh_files_test_double (zdirListOne, zdirListTwo);
2992 if (retval != 0)
2993 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2994
2995 retval = sh_files_test_double (zdirListTwo, zdirListOne);
2996 if (retval != 0)
2997 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2998 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
2999
3000 return 0;
3001}
3002
3003#endif
3004
3005#ifdef SH_CUTEST
3006#include "CuTest.h"
3007
3008void Test_file_dequote (CuTest *tc)
3009{
3010#if (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE))
3011
3012 char str1[] = "1234567890";
3013 char str1a[] = "123456\\\"789\\r";
3014 char str1b[] = "12345678\\r9";
3015 char str1c[] = "12345678\\x0a_9";
3016 char str1d[] = "12345678\\007_9";
3017 char str1e[] = "123456789\\\\";
3018
3019 char str2[] = "1234567890\\xw";
3020 char str3[] = "1234567890\\xw99";
3021 char str4[] = "1234567890\\0ww";
3022 char str5[] = "12345\\g67890";
3023 char str6[] = "1234567890\\009a";
3024
3025 char *s, *p, *q;
3026 size_t lo, lr;
3027
3028 s = SH_ALLOC(64); sl_strlcpy(s, str1, 64); p = s; lo = strlen(s); lr = lo;
3029 q = sh_files_C_dequote(s, &lr);
3030 CuAssertPtrNotNull(tc, q);
3031 CuAssertTrue(tc, p == q);
3032 CuAssertTrue(tc, lr == lo);
3033
3034 s = SH_ALLOC(64); sl_strlcpy(s, str1a, 64); p = s; lo = strlen(s); lr = lo;
3035 q = sh_files_C_dequote(s, &lr);
3036 CuAssertPtrNotNull(tc, q);
3037 CuAssertTrue(tc, p != q);
3038 CuAssertTrue(tc, 0 == strcmp(q, "123456\"789\r"));
3039 CuAssertTrue(tc, lr == (lo-2));
3040
3041 s = SH_ALLOC(64); sl_strlcpy(s, str1b, 64); p = s; lo = strlen(s); lr = lo;
3042 q = sh_files_C_dequote(s, &lr);
3043 CuAssertPtrNotNull(tc, q);
3044 CuAssertTrue(tc, p != q);
3045 CuAssertTrue(tc, 0 == strcmp(q, "12345678\r9"));
3046 CuAssertTrue(tc, lr == (lo-1));
3047
3048 s = SH_ALLOC(64); sl_strlcpy(s, str1c, 64); p = s; lo = strlen(s); lr = lo;
3049 q = sh_files_C_dequote(s, &lr);
3050 CuAssertPtrNotNull(tc, q);
3051 CuAssertTrue(tc, p != q);
3052 CuAssertTrue(tc, 0 == strcmp(q, "12345678\x0a_9"));
3053 CuAssertTrue(tc, lr == (lo-3));
3054
3055 s = SH_ALLOC(64); sl_strlcpy(s, str1d, 64); p = s; lo = strlen(s); lr = lo;
3056 q = sh_files_C_dequote(s, &lr);
3057 CuAssertPtrNotNull(tc, q);
3058 CuAssertTrue(tc, p != q);
3059 CuAssertTrue(tc, 0 == strcmp(q, "12345678\007_9"));
3060 CuAssertTrue(tc, lr == (lo-3));
3061
3062 s = SH_ALLOC(64); sl_strlcpy(s, str1e, 64); p = s; lo = strlen(s); lr = lo;
3063 q = sh_files_C_dequote(s, &lr);
3064 CuAssertPtrNotNull(tc, q);
3065 CuAssertTrue(tc, p != q);
3066 CuAssertTrue(tc, 0 == strcmp(q, "123456789\\"));
3067 CuAssertTrue(tc, lr == (lo-1));
3068
3069 s = SH_ALLOC(64); sl_strlcpy(s, str2, 64); p = s; lo = strlen(s); lr = lo;
3070 q = sh_files_C_dequote(s, &lr);
3071 CuAssertTrue(tc, q == NULL);
3072 CuAssertTrue(tc, lr == 0);
3073
3074 s = SH_ALLOC(64); sl_strlcpy(s, str3, 64); p = s; lo = strlen(s); lr = lo;
3075 q = sh_files_C_dequote(s, &lr);
3076 CuAssertTrue(tc, q == NULL);
3077 CuAssertTrue(tc, lr == 0);
3078
3079 s = SH_ALLOC(64); sl_strlcpy(s, str4, 64); p = s; lo = strlen(s); lr = lo;
3080 q = sh_files_C_dequote(s, &lr);
3081 CuAssertTrue(tc, q == NULL);
3082 CuAssertTrue(tc, lr == 0);
3083
3084 s = SH_ALLOC(64); sl_strlcpy(s, str5, 64); p = s; lo = strlen(s); lr = lo;
3085 q = sh_files_C_dequote(s, &lr);
3086 CuAssertTrue(tc, q == NULL);
3087 CuAssertTrue(tc, lr == 0);
3088
3089 s = SH_ALLOC(64); sl_strlcpy(s, str6, 64); p = s; lo = strlen(s); lr = lo;
3090 q = sh_files_C_dequote(s, &lr);
3091 CuAssertTrue(tc, q == NULL);
3092 CuAssertTrue(tc, lr == 0);
3093
3094 return;
3095#else
3096 (void) tc; /* fix compiler warning */
3097 return;
3098#endif
3099}
3100#endif
3101
Note: See TracBrowser for help on using the repository browser.