source: trunk/src/sh_files.c@ 427

Last change on this file since 427 was 425, checked in by katerina, 12 years ago

Fix for tickets #329, #330, #331, #332

File size: 74.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#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 SL_RETURN((0), _("sh_files_checkdir"));
2013 }
2014
2015 if (status == -1)
2016 {
2017 SH_FREE(tmpname);
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 SL_RETURN((-1), _("sh_files_checkdir"));
2022 }
2023
2024 if (theFile->c_mode[0] != 'd')
2025 {
2026 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
2027 MSG_FI_NODIR,
2028 tmpname);
2029 ++sh.statistics.files_nodir;
2030 SH_FREE(tmpname);
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 SL_RETURN((-1), _("sh_files_checkdir"));
2035 }
2036
2037 if ((sh.flag.inotify & SH_INOTIFY_INSCAN) != 0)
2038 {
2039 sh_inotify_add_watch_later(iname, &sh_file_watches, &status,
2040 iclass, check_mask, SH_INOTIFY_DIR, idepth);
2041 }
2042
2043 hardlink_num = theFile->hardlinks;
2044
2045 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2046 if (theFile->link_path) SH_FREE(theFile->link_path);
2047 SH_FREE(theFile);
2048
2049 /* ---- open directory for reading ----
2050 *
2051 * opendir() will fail with ENOTDIR if the path has been changed
2052 * to a non-directory in between lstat() and opendir().
2053 */
2054 thisDir = opendir (iname);
2055
2056 if (thisDir == NULL)
2057 {
2058 status = errno;
2059 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
2060 MSG_E_OPENDIR,
2061 sh_error_message (status, errbuf, sizeof(errbuf)), tmpname);
2062 SH_FREE(tmpname);
2063 SL_RETURN((-1), _("sh_files_checkdir"));
2064 }
2065
2066 theDir = SH_ALLOC(sizeof(dir_type));
2067
2068 theDir->NumRegular = 0;
2069 theDir->NumDirs = 0;
2070 theDir->NumSymlinks = 0;
2071 theDir->NumFifos = 0;
2072 theDir->NumSockets = 0;
2073 theDir->NumCDev = 0;
2074 theDir->NumBDev = 0;
2075 theDir->NumDoor = 0;
2076 theDir->NumPort = 0;
2077 theDir->NumAll = 0;
2078 theDir->TotalBytes = 0;
2079 sl_strlcpy (theDir->DirPath, iname, PATH_MAX);
2080
2081
2082 sh_dummy_dirlist = (void *) &dirlist;
2083 sh_dummy_tmpcat = (void *) &tmpcat;
2084
2085 /* ---- read ----
2086 */
2087 SH_MUTEX_LOCK(mutex_readdir);
2088
2089 dirlist = NULL;
2090 dirlist_orig = NULL;
2091
2092 do {
2093 thisEntry = readdir (thisDir);
2094 if (thisEntry != NULL)
2095 {
2096 ++theDir->NumAll;
2097 if (sl_strcmp (thisEntry->d_name, ".") == 0)
2098 {
2099 ++theDir->NumDirs;
2100 continue;
2101 }
2102 if (sl_strcmp (thisEntry->d_name, "..") == 0)
2103 {
2104 ++theDir->NumDirs;
2105 continue;
2106 }
2107 dirlist = addto_sh_dirlist (thisEntry, dirlist);
2108 }
2109 } while (thisEntry != NULL);
2110
2111 SH_MUTEX_UNLOCK(mutex_readdir);
2112
2113 closedir (thisDir);
2114
2115 ++sh.statistics.dirs_checked;
2116
2117 dirlist_orig = dirlist;
2118
2119 do {
2120
2121 /* If the directory is empty, dirlist = NULL
2122 */
2123 if (!dirlist)
2124 break;
2125
2126 if (sig_termfast == 1)
2127 {
2128 SH_FREE(theDir);
2129 SL_RETURN((0), _("sh_files_checkdir"));
2130 }
2131
2132 BREAKEXIT(sh_derr);
2133
2134#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_RAND_R)
2135 if (0 == (rand_r(&state) % 5)) (void) sh_derr();
2136#else
2137 if (0 == state * (rand() % 5)) (void) sh_derr();
2138#endif
2139
2140 /* ---- Check the file. ----
2141 */
2142 tmpcat = SH_ALLOC(PATH_MAX);
2143 sl_strlcpy(tmpcat, iname, PATH_MAX);
2144 if (sl_strlen(tmpcat) > 1 || tmpcat[0] != '/')
2145 sl_strlcat(tmpcat, "/", PATH_MAX);
2146 sl_strlcat(tmpcat, dirlist->sh_d_name, PATH_MAX);
2147
2148 rdepth_next = rdepth - 1;
2149 class_next = class;
2150 check_mask_next = check_mask;
2151 file_class_next = class;
2152 file_check_mask_next = check_mask;
2153 checked_flag = -1;
2154 cchecked_flag = -1;
2155
2156 /* Wed Aug 24 2005 compare against dirListOne, dirListTwo
2157 * this fixes the problem that the directory special file
2158 * is checked with the policy of the parent directory
2159 */
2160 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
2161 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
2162 dst_ptr = (dirstack_t *) zAVLSearch(zdirListOne, tmpcat);
2163
2164 if (dst_ptr)
2165 {
2166 /* Tue Aug 6 22:13:27 CEST 2002 introduce file_class_next
2167 * this fixes the problem that a policy for the directory
2168 * inode erroneously becomes a policy for the directory itself.
2169 */
2170 file_class_next = dst_ptr->class;
2171 file_check_mask_next = dst_ptr->check_mask;
2172 checked_flag = dst_ptr->checked;
2173 cchecked_flag = dst_ptr->childs_checked;
2174 }
2175
2176 if (checked_flag == -1)
2177 {
2178 dst_ptr = (dirstack_t *) zAVLSearch(zdirListTwo, tmpcat);
2179
2180 if (dst_ptr)
2181 {
2182 /* Tue Aug 6 22:13:27 CEST 2002 introduce file_class_next
2183 * this fixes the problem that a policy for the directory
2184 * inode erroneously becomes a policy for the directory itself.
2185 */
2186 file_class_next = dst_ptr->class;
2187 file_check_mask_next = dst_ptr->check_mask;
2188 checked_flag = dst_ptr->checked;
2189 cchecked_flag = dst_ptr->childs_checked;
2190 }
2191 }
2192 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
2193
2194 SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
2195 dst_ptr = (dirstack_t *) zAVLSearch(zfileList, tmpcat);
2196
2197 if (dst_ptr)
2198 {
2199 /* Tue Aug 6 22:13:27 CEST 2002 introduce file_class_next
2200 * this fixes the problem that a policy for the directory
2201 * inode erroneously becomes a policy for the directory itself.
2202 */
2203 file_class_next = dst_ptr->class;
2204 file_check_mask_next = dst_ptr->check_mask;
2205 checked_flag = dst_ptr->checked;
2206 /* not set, hence always FALSE */
2207 /* cchecked_flag = dst_ptr->childs_checked; */
2208
2209 if (checked_flag != S_TRUE)
2210 {
2211 /* -- need to check the file itself --
2212 */
2213 if (sh.flag.reportonce == S_TRUE)
2214 dummy = dst_ptr->is_reported;
2215 }
2216 }
2217 SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
2218
2219 /* ---- Has been checked already. ----
2220 */
2221 if (checked_flag == S_TRUE && cchecked_flag == S_TRUE)
2222 {
2223 /* Mar 11 2004 get ftype for complete directory count
2224 */
2225 checkit = sh_unix_get_ftype(tmpcat);
2226 if (checkit == SH_FILE_DIRECTORY)
2227 {
2228 ++theDir->NumDirs;
2229 }
2230 SH_FREE(tmpcat);
2231 dirlist = dirlist->next;
2232 continue;
2233 }
2234
2235 /* --- May be true, false, or not found. ---
2236 */
2237 if (checked_flag == S_TRUE)
2238 {
2239 /* -- need only the file type --
2240 */
2241 checkit = sh_unix_get_ftype(tmpcat);
2242 }
2243 else
2244 {
2245 /* -- need to check the file itself --
2246 */
2247 /* -- moved up --
2248 * if (dst_ptr && sh.flag.reportonce == S_TRUE)
2249 * dummy = dst_ptr->is_reported;
2250 */
2251
2252 checkit = sh_files_filecheck (file_class_next, file_check_mask_next,
2253 iname,
2254 dirlist->sh_d_name,
2255 &dummy, 0);
2256
2257
2258 SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
2259 dst_ptr = (dirstack_t *) zAVLSearch(zfileList, tmpcat);
2260
2261 if (dst_ptr && checked_flag == S_FALSE)
2262 dst_ptr->checked = S_TRUE;
2263
2264 /* Thu Mar 7 15:09:40 CET 2002 Propagate the 'reported' flag
2265 */
2266 if (dst_ptr && sh.flag.reportonce == S_TRUE)
2267 dst_ptr->is_reported = dummy;
2268
2269 if (dst_ptr)
2270 dst_ptr->childs_checked = S_TRUE;
2271 SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
2272 }
2273
2274 if (checkit == SH_FILE_REGULAR)
2275 ++theDir->NumRegular;
2276
2277 else if (checkit == SH_FILE_DIRECTORY)
2278 {
2279 ++theDir->NumDirs;
2280
2281 if (rdepth_next >= 0 && cchecked_flag != S_TRUE)
2282 {
2283 rdepth_next = rdepth - 1;
2284
2285 /* check whether the new directory is in the
2286 * list with a recursion depth already defined
2287 */
2288 checked_flag = -1;
2289 cchecked_flag = -1;
2290
2291 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
2292 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
2293 tmp_ptr = (dirstack_t *) zAVLSearch(zdirListOne, tmpcat);
2294
2295 if (tmp_ptr)
2296 {
2297 TPT((0, FIL__, __LINE__,
2298 _("msg=<%s -> recursion depth %d\n>"),
2299 tmp_ptr->name, tmp_ptr->rdepth));
2300 rdepth_next = tmp_ptr->rdepth;
2301 class_next = tmp_ptr->class;
2302 check_mask_next = tmp_ptr->check_mask;
2303 /* 28. Aug 2001 reversed
2304 */
2305 cchecked_flag = tmp_ptr->childs_checked;
2306 checked_flag = tmp_ptr->checked;
2307 }
2308
2309 if (checked_flag == -1)
2310 {
2311 tmp_ptr = (dirstack_t *) zAVLSearch(zdirListTwo, tmpcat);
2312
2313 if (tmp_ptr)
2314 {
2315 TPT((0, FIL__, __LINE__,
2316 _("msg=<%s -> recursion depth %d\n>"),
2317 tmp_ptr->name, tmp_ptr->rdepth));
2318 rdepth_next = tmp_ptr->rdepth;
2319 class_next = tmp_ptr->class;
2320 check_mask_next = tmp_ptr->check_mask;
2321 /* 28. Aug 2001 reversed
2322 */
2323 cchecked_flag = tmp_ptr->childs_checked;
2324 checked_flag = tmp_ptr->checked;
2325 }
2326 }
2327
2328 if (tmp_ptr && cchecked_flag == S_FALSE)
2329 {
2330 tmp_ptr->childs_checked = S_TRUE;
2331 /*
2332 * 04. Feb 2006 avoid double checking
2333 */
2334 tmp_ptr->checked = S_TRUE;
2335 }
2336 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
2337
2338 if (cchecked_flag == S_FALSE)
2339 {
2340 sh_files_checkdir (class_next, check_mask_next, rdepth_next,
2341 tmpcat, dirlist->sh_d_name);
2342 /*
2343 tmp_ptr->childs_checked = S_TRUE;
2344 tmp_ptr->checked = S_TRUE;
2345 */
2346 }
2347 else if (checked_flag == -1)
2348 sh_files_checkdir (class_next, check_mask_next, rdepth_next,
2349 tmpcat, dirlist->sh_d_name);
2350
2351 }
2352 }
2353
2354 else if (checkit == SH_FILE_SYMLINK) ++theDir->NumSymlinks;
2355 else if (checkit == SH_FILE_FIFO) ++theDir->NumFifos;
2356 else if (checkit == SH_FILE_SOCKET) ++theDir->NumSockets;
2357 else if (checkit == SH_FILE_CDEV) ++theDir->NumCDev;
2358 else if (checkit == SH_FILE_BDEV) ++theDir->NumBDev;
2359 else if (checkit == SH_FILE_DOOR) ++theDir->NumDoor;
2360 else if (checkit == SH_FILE_PORT) ++theDir->NumPort;
2361
2362 SH_FREE(tmpcat);
2363
2364 if ((sig_termfast == 1) || (sig_terminate == 1))
2365 {
2366 SH_FREE(theDir);
2367 sh_dummy_dirlist = NULL;
2368 SL_RETURN((0), _("sh_files_checkdir"));
2369 }
2370
2371 dirlist = dirlist->next;
2372
2373 /* -- moved up, only affects zfileList anyway
2374 * if (dst_ptr)
2375 * dst_ptr->childs_checked = S_TRUE;
2376 */
2377
2378 } while (dirlist != NULL);
2379
2380 if (flag_err_info == SL_TRUE)
2381 {
2382 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DSUM,
2383 theDir->NumDirs,
2384 theDir->NumRegular,
2385 theDir->NumSymlinks,
2386 theDir->NumFifos,
2387 theDir->NumSockets,
2388 theDir->NumCDev,
2389 theDir->NumBDev);
2390 }
2391
2392 kill_sh_dirlist (dirlist_orig);
2393
2394#if !defined(HOST_IS_DARWIN)
2395 /*
2396 * Hardlink check; not done on MacOS X because of resource forks
2397 */
2398 if ((sh_check_hardlinks == S_TRUE) && (hardlink_num != theDir->NumDirs))
2399 {
2400 if (0 != sh_files_hle_test(hardlink_num-theDir->NumDirs, iname))
2401 {
2402 len = strlen(tmpname);
2403 if (sl_ok_adds(len, 256))
2404 len += 256;
2405 tmpcat = SH_ALLOC(len);
2406 sl_snprintf(tmpcat, len,
2407 _("%s: subdirectory count (%d) != hardlinks (%d)"),
2408 tmpname, theDir->NumDirs, hardlink_num);
2409 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
2410 MSG_E_SUBGEN, tmpcat, _("sh_files_checkdir"));
2411 SH_FREE(tmpcat);
2412 }
2413 }
2414#endif
2415
2416 SH_FREE(tmpname);
2417 SH_FREE(theDir);
2418
2419 sh_dummy_dirlist = NULL;
2420
2421 SL_RETURN((0), _("sh_files_checkdir"));
2422}
2423
2424int get_the_fd (SL_TICKET ticket);
2425
2426static int sh_use_rsrc = S_FALSE;
2427
2428int sh_files_use_rsrc(const char * str)
2429{
2430 return sh_util_flagval(str, &sh_use_rsrc);
2431}
2432
2433static void * sh_dummy_fileName;
2434static void * sh_dummy_tmpname;
2435static void * sh_dummy_tmpdir;
2436
2437ShFileType sh_files_filecheck (int class, unsigned long check_mask,
2438 const char * dirName,
2439 const char * infileName,
2440 int * reported,
2441 int rsrcflag)
2442{
2443 /* 28 Aug 2001 allow NULL fileName
2444 */
2445 char * fullpath;
2446 char fileHash[2*(KEY_LEN + 1)];
2447 int status;
2448 file_type * theFile;
2449 char * tmpdir;
2450 char * tmpname;
2451 const char * fileName;
2452#if !defined(O_NOATIME)
2453 struct utimbuf utime_buf;
2454#endif
2455 static unsigned int state = 1;
2456 char sc;
2457
2458 SL_ENTER(_("sh_files_filecheck"));
2459
2460 fullpath = SH_ALLOC(PATH_MAX);
2461 theFile = SH_ALLOC(sizeof(file_type));
2462
2463 /* Take the address to keep gcc from putting it into a register.
2464 * Avoids the 'clobbered by longjmp' warning.
2465 */
2466 sh_dummy_fileName = (void *) &fileName;
2467 sh_dummy_tmpname = (void *) &tmpname;
2468 sh_dummy_tmpdir = (void *) &tmpdir;
2469
2470 BREAKEXIT(sh_derr);
2471
2472#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_RAND_R)
2473 if (0 == (rand_r(&state) % 2)) (void) sh_derr();
2474#else
2475 if (0 == state * (rand() % 2)) (void) sh_derr();
2476#endif
2477
2478 if (dirName && infileName && (dirName[0] == '/') && (dirName[1] == '\0')
2479 && (infileName[0] == '/') && (infileName[1] == '\0'))
2480 {
2481 fileName = NULL;
2482 }
2483 else
2484 {
2485 fileName = infileName;
2486 }
2487
2488 /* fileName may be NULL if this is a directory
2489 */
2490 if (dirName == NULL /* || fileName == NULL */)
2491 {
2492 SH_MUTEX_LOCK(mutex_thread_nolog);
2493 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_NULL);
2494 SH_MUTEX_UNLOCK(mutex_thread_nolog);
2495 SH_FREE(fullpath);
2496 SH_FREE(theFile);
2497 SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
2498 }
2499
2500 if ((fileName != NULL) && (class != SH_LEVEL_ALLIGNORE) &&
2501 (0 != sh_util_obscurename (ShDFLevel[SH_ERR_T_NAME],
2502 fileName, S_FALSE)))
2503 {
2504 if ((dirName != NULL) && (dirName[0] == '/') && (dirName[1] == '\0'))
2505 {
2506 tmpname = sh_util_safe_name (fileName);
2507 SH_MUTEX_LOCK(mutex_thread_nolog);
2508 sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, 0,
2509 MSG_FI_OBSC2,
2510 "", tmpname);
2511 SH_MUTEX_UNLOCK(mutex_thread_nolog);
2512 SH_FREE(tmpname);
2513 }
2514 else
2515 {
2516 tmpdir = sh_util_safe_name (dirName);
2517 tmpname = sh_util_safe_name (fileName);
2518 SH_MUTEX_LOCK(mutex_thread_nolog);
2519 sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, 0,
2520 MSG_FI_OBSC2,
2521 tmpdir, tmpname);
2522 SH_MUTEX_UNLOCK(mutex_thread_nolog);
2523 SH_FREE(tmpname);
2524 SH_FREE(tmpdir);
2525 }
2526 }
2527
2528 /* sh_files_fullpath accepts NULL fileName
2529 */
2530 if (0 != sh_files_fullpath (dirName, fileName, fullpath))
2531 {
2532 tmpdir = sh_util_safe_name (dirName);
2533 tmpname = sh_util_safe_name (fileName);
2534 SH_MUTEX_LOCK(mutex_thread_nolog);
2535 sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, 0,
2536 MSG_FI_2LONG2,
2537 tmpdir, tmpname);
2538 SH_MUTEX_UNLOCK(mutex_thread_nolog);
2539 SH_FREE(tmpname);
2540 SH_FREE(tmpdir);
2541 SH_FREE(fullpath);
2542 SH_FREE(theFile);
2543 SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
2544 }
2545
2546
2547 /* stat the file and determine checksum (if a regular file)
2548 */
2549 sl_strlcpy (theFile->fullpath, fullpath, PATH_MAX);
2550 theFile->check_mask = check_mask /* sh_files_maskof(class) */;
2551 theFile->file_reported = (*reported);
2552 theFile->attr_string = NULL;
2553 theFile->link_path = NULL;
2554
2555 TPT(( 0, FIL__, __LINE__, _("msg=<checking file: %s>\n"), fullpath));
2556
2557 status = sh_unix_getinfo ( (class == SH_LEVEL_ALLIGNORE) ?
2558 ShDFLevel[class] : ShDFLevel[SH_ERR_T_FILE],
2559 fileName,
2560 theFile, fileHash, class);
2561
2562 if (status != 0)
2563 {
2564 TPT(( 0, FIL__, __LINE__, _("msg=<file: %s> status=<%d>\n"),
2565 fullpath, status));
2566 if (class == SH_LEVEL_ALLIGNORE && sh.flag.checkSum != SH_CHECK_INIT)
2567 sh_hash_set_visited_true (fullpath);
2568 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2569 if (theFile->link_path) SH_FREE(theFile->link_path);
2570 SH_FREE(fullpath);
2571 SH_FREE(theFile);
2572 SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
2573 }
2574
2575 if (sig_termfast == 1) {
2576 goto ret_point;
2577 }
2578
2579 /* report
2580 */
2581 if ((flag_err_debug == SL_TRUE) && (theFile->c_mode[0] == '-'))
2582 {
2583 tmpname = sh_util_safe_name (fullpath); /* fixed in 1.5.4 */
2584 SH_MUTEX_LOCK(mutex_thread_nolog);
2585 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_CSUM,
2586 fileHash, tmpname);
2587 SH_MUTEX_UNLOCK(mutex_thread_nolog);
2588 SH_FREE(tmpname);
2589 }
2590 ++sh.statistics.files_checked;
2591
2592 if ( sh.flag.checkSum == SH_CHECK_INIT /* && sh.flag.update == S_FALSE */)
2593 {
2594 sh_hash_pushdata (theFile, fileHash);
2595 }
2596 else if (sh.flag.checkSum == SH_CHECK_CHECK
2597 /* && theFile.c_mode[0] == '-' */
2598 /* && class != SH_LEVEL_ALLIGNORE */
2599 )
2600 {
2601 sh_hash_compdata (class, theFile, fileHash, NULL, -1);
2602 }
2603
2604 (*reported) = theFile->file_reported;
2605
2606 /* reset the access time
2607 */
2608#if !defined(O_NOATIME)
2609 if (class == SH_LEVEL_NOIGNORE && (theFile->check_mask & MODI_ATM) != 0)
2610 {
2611 utime_buf.actime = (time_t) theFile->atime;
2612 utime_buf.modtime = (time_t) theFile->mtime;
2613
2614 retry_aud_utime (FIL__, __LINE__, fullpath, &utime_buf);
2615 }
2616#endif
2617
2618#if defined(HOST_IS_DARWIN)
2619 /*
2620 * Check for resource fork
2621 */
2622 if ( (sh_use_rsrc == S_TRUE) && (theFile->c_mode[0] != 'd') && (rsrcflag == 0) )
2623 {
2624 int dummy;
2625 static int rsrc_init = 0;
2626 static char rsrc[17];
2627 char * testpath = SH_ALLOC(PATH_MAX);
2628
2629 if (rsrc_init == 0) {
2630 sl_strlcpy(rsrc, _("..namedfork/rsrc"), 17);
2631 rsrc_init = 1;
2632 }
2633 sl_strlcpy (testpath, fullpath, PATH_MAX);
2634 sl_strlcat (testpath, "/", PATH_MAX);
2635 sl_strlcat (testpath, rsrc, PATH_MAX);
2636
2637 if (sl_strlen(testpath) == (17 + sl_strlen(fullpath)))
2638 {
2639 if (S_TRUE == sh_unix_file_exists (testpath))
2640 {
2641 sh_files_filecheck (class, check_mask, fullpath, rsrc, &dummy, 1);
2642 }
2643 }
2644 SH_FREE(testpath);
2645 }
2646#else
2647 (void) rsrcflag; /* avoid compiler warning */
2648#endif
2649
2650 ret_point:
2651
2652 sc = theFile->c_mode[0];
2653
2654 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2655 if (theFile->link_path) SH_FREE(theFile->link_path);
2656 SH_FREE(fullpath);
2657 SH_FREE(theFile);
2658
2659 switch (sc)
2660 {
2661 case '-': SL_RETURN(SH_FILE_REGULAR, _("sh_files_filecheck"));
2662 case 'l': SL_RETURN(SH_FILE_SYMLINK, _("sh_files_filecheck"));
2663 case 'd': SL_RETURN(SH_FILE_DIRECTORY, _("sh_files_filecheck"));
2664 case 'c': SL_RETURN(SH_FILE_CDEV, _("sh_files_filecheck"));
2665 case 'b': SL_RETURN(SH_FILE_BDEV, _("sh_files_filecheck"));
2666 case '|': SL_RETURN(SH_FILE_FIFO, _("sh_files_filecheck"));
2667 case 'D': SL_RETURN(SH_FILE_DOOR, _("sh_files_filecheck"));
2668 case 'P': SL_RETURN(SH_FILE_PORT, _("sh_files_filecheck"));
2669 case 's': SL_RETURN(SH_FILE_SOCKET, _("sh_files_filecheck"));
2670 default: SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
2671 }
2672
2673 /* notreached */
2674}
2675
2676/* concatenate statpath = testdir"/"d_name
2677 */
2678static int sh_files_fullpath (const char * testdir, const char * d_name,
2679 char * statpath)
2680{
2681 int llen = 0;
2682
2683 SL_ENTER(_("sh_files_fullpath"));
2684
2685 if (testdir != NULL)
2686 {
2687 if ( (llen = sl_strlen(testdir)) > (PATH_MAX-2) )
2688 SL_RETURN((-1),_("sh_files_fullpath"));
2689 sl_strlcpy(statpath, testdir, PATH_MAX - 1);
2690 }
2691 if (d_name != NULL)
2692 {
2693 if (llen > 1 || statpath[0] != '/')
2694 sl_strlcat(statpath, "/", PATH_MAX);
2695 if ((sl_strlen(d_name) + sl_strlen(statpath)) >= PATH_MAX)
2696 SL_RETURN((-1),_("sh_files_fullpath"));
2697 sl_strlcat(statpath, d_name, PATH_MAX);
2698 }
2699 if (statpath == NULL)
2700 SL_RETURN((-1),_("sh_files_fullpath"));
2701 SL_RETURN((0),_("sh_files_fullpath"));
2702}
2703
2704/* -----------------------------------
2705 * Routines required for inotify
2706 * -----------------------------------
2707 */
2708int sh_files_search_dir(char * name, int * class,
2709 unsigned long *check_mask, int *reported,
2710 int * rdepth)
2711{
2712 volatile int retval = 0;
2713#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
2714 sh_globstack_t * testPattern;
2715 zAVLCursor cursor;
2716#endif
2717 dirstack_t * item;
2718
2719 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
2720 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
2721
2722 item = zAVLSearch(zdirListOne, name);
2723
2724 if (item)
2725 {
2726 *check_mask = item->check_mask;
2727 *class = item->class;
2728 *reported = item->is_reported;
2729 *rdepth = item->rdepth;
2730 item->checked = S_FALSE;
2731 item->childs_checked = S_FALSE;
2732 item->is_reported = S_FALSE;
2733 retval = 1;
2734 goto out;
2735 }
2736
2737 item = zAVLSearch(zdirListTwo, name);
2738
2739 if (item)
2740 {
2741 *check_mask = item->check_mask;
2742 *class = item->class;
2743 *reported = item->is_reported;
2744 *rdepth = item->rdepth;
2745 item->checked = S_FALSE;
2746 item->childs_checked = S_FALSE;
2747 item->is_reported = S_FALSE;
2748 retval = 1;
2749 goto out;
2750 }
2751
2752#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
2753 SH_MUTEX_LOCK(mutex_zglob);
2754 for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList);
2755 testPattern;
2756 testPattern = (sh_globstack_t *) zAVLNext (&cursor))
2757 {
2758 if (testPattern->type == SH_LIST_DIR1 ||
2759 testPattern->type == SH_LIST_DIR2)
2760 {
2761 if (0 == fnmatch(testPattern->name, name, FNM_PATHNAME|FNM_PERIOD))
2762 {
2763 *check_mask = testPattern->check_mask;
2764 *class = testPattern->class;
2765 *rdepth = testPattern->rdepth;
2766 retval = 1;
2767 break;
2768 }
2769
2770 }
2771 }
2772 SH_MUTEX_UNLOCK(mutex_zglob);
2773#endif
2774 out:
2775 ; /* 'label at end of compound statement' */
2776 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
2777 return retval;
2778}
2779
2780int sh_files_search_file(char * name, int * class,
2781 unsigned long *check_mask, int *reported)
2782{
2783 volatile int retval = 0;
2784#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
2785 sh_globstack_t * testPattern;
2786 zAVLCursor cursor;
2787#endif
2788 dirstack_t * item;
2789
2790 SH_MUTEX_LOCK(mutex_zfiles);
2791 item = zAVLSearch(zfileList, name);
2792
2793 if (item)
2794 {
2795 *check_mask = item->check_mask;
2796 *class = item->class;
2797 *reported = item->is_reported;
2798 retval = 1;
2799 }
2800 SH_MUTEX_UNLOCK(mutex_zfiles);
2801
2802#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
2803 if (retval == 0)
2804 {
2805 SH_MUTEX_LOCK(mutex_zglob);
2806 for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList);
2807 testPattern;
2808 testPattern = (sh_globstack_t *) zAVLNext (&cursor))
2809 {
2810 if (testPattern->type == SH_LIST_FILE)
2811 {
2812 if (0 == fnmatch(testPattern->name, name,
2813 FNM_PATHNAME|FNM_PERIOD))
2814 {
2815 *check_mask = testPattern->check_mask;
2816 *class = testPattern->class;
2817 retval = 1;
2818 break;
2819 }
2820
2821 }
2822 }
2823 SH_MUTEX_UNLOCK(mutex_zglob);
2824 }
2825#endif
2826
2827 return retval;
2828}
2829
2830void sh_files_set_file_reported(const char * name)
2831{
2832 dirstack_t * item;
2833
2834 SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
2835 item = zAVLSearch(zfileList, name);
2836
2837 if (item)
2838 {
2839 if (sh.flag.reportonce == S_TRUE)
2840 SET_SH_FFLAG_REPORTED(item->is_reported);
2841 }
2842 SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
2843 return;
2844}
2845
2846void sh_files_clear_file_reported(const char * name)
2847{
2848 dirstack_t * item;
2849
2850 SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
2851 item = zAVLSearch(zfileList, name);
2852
2853 if (item)
2854 {
2855 CLEAR_SH_FFLAG_REPORTED(item->is_reported);
2856 }
2857 SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
2858 return;
2859}
2860
2861/* -----------------------------------
2862 *
2863 * The following two routines serve to
2864 * verify that the user has selected
2865 * a proper setup for file policies.
2866 *
2867 * -----------------------------------
2868 */
2869static int check_file(char * name)
2870{
2871 dirstack_t * pfilL;
2872 zAVLCursor cursor;
2873 volatile int retval = -1;
2874
2875 SL_ENTER(_("check_file"));
2876
2877 if (SH_FILE_DIRECTORY == sh_unix_get_ftype(name))
2878 SL_RETURN(0, _("check_file"));
2879
2880 for (pfilL = (dirstack_t *) zAVLFirst (&cursor, zfileList); pfilL;
2881 pfilL = (dirstack_t *) zAVLNext (&cursor))
2882 {
2883 if (0 == strcmp(name, pfilL->name) &&
2884 (pfilL->check_mask & MODI_ATM) == 0 &&
2885 (pfilL->check_mask & MODI_CTM) == 0 &&
2886 (pfilL->check_mask & MODI_MTM) == 0)
2887 {
2888 retval = 0;
2889 break;
2890 }
2891 }
2892
2893 SL_RETURN(retval, _("check_file"));
2894}
2895
2896static void * sh_dummy_pdirL;
2897
2898int sh_files_test_setup_int (zAVLTree * tree)
2899{
2900 int dlen, flen;
2901 zAVLCursor cursor1;
2902 zAVLCursor cursor2;
2903
2904 dirstack_t * pdirL;
2905 dirstack_t * pfilL;
2906
2907 SL_ENTER(_("sh_files_test_setup"));
2908
2909 sh_dummy_pdirL = (void *) &pdirL;
2910
2911 for (pdirL = (dirstack_t *) zAVLFirst (&cursor1, tree); pdirL;
2912 pdirL = (dirstack_t *) zAVLNext (&cursor1))
2913 {
2914 dlen = strlen(pdirL->name);
2915
2916 SH_MUTEX_LOCK(mutex_zfiles);
2917 for (pfilL = (dirstack_t *) zAVLFirst (&cursor2, zfileList); pfilL;
2918 pfilL = (dirstack_t *) zAVLNext (&cursor2))
2919 {
2920 flen = strlen(pfilL->name);
2921
2922 /* check whether file is in tree of dir
2923 */
2924 if ((pfilL->class == SH_LEVEL_READONLY) ||
2925 (pfilL->class == SH_LEVEL_NOIGNORE))
2926 {
2927 ; /* do nothing */
2928 }
2929 else
2930 {
2931 if ((flen > (dlen+1)) &&
2932 (pfilL->name[dlen] == '/') &&
2933 (NULL == strchr(&(pfilL->name[dlen+1]), '/')) && /*30-5-01*/
2934 (0 == strncmp(pfilL->name, pdirL->name, dlen)))
2935 {
2936 if ((pdirL->check_mask & MODI_ATM) != 0 ||
2937 (pdirL->check_mask & MODI_MTM) != 0 ||
2938 (pdirL->check_mask & MODI_CTM) != 0)
2939 {
2940 if (check_file (pdirL->name) != 0)
2941 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_COLL,
2942 pdirL->name, pfilL->name);
2943 }
2944 }
2945 }
2946 }
2947 SH_MUTEX_UNLOCK(mutex_zfiles);
2948 }
2949
2950 SL_RETURN((0), _("sh_files_test_setup"));
2951}
2952
2953int sh_files_test_double (zAVLTree * firstList, zAVLTree * secondList)
2954{
2955 int retval = 0;
2956 zAVLCursor cursor;
2957 dirstack_t * first;
2958
2959 for (first = (dirstack_t *) zAVLFirst (&cursor, firstList); first;
2960 first = (dirstack_t *) zAVLNext (&cursor))
2961 {
2962
2963 if (NULL != zAVLSearch(secondList, first->name))
2964 {
2965 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
2966 first->name);
2967 retval = 1;
2968 }
2969 }
2970 return retval;
2971}
2972
2973extern void aud_exit (const char * file, int line, int fd);
2974
2975int sh_files_test_setup ()
2976{
2977 int retval;
2978
2979 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
2980 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
2981 /* Test for modifications allowed in ReadOnly directory
2982 */
2983 sh_files_test_setup_int (zdirListOne);
2984 sh_files_test_setup_int (zdirListTwo);
2985
2986 /* Test for files/dirz defined twice
2987 */
2988 retval = sh_files_test_double (zdirListOne, zdirListTwo);
2989 if (retval != 0)
2990 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2991
2992 retval = sh_files_test_double (zdirListTwo, zdirListOne);
2993 if (retval != 0)
2994 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2995 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
2996
2997 return 0;
2998}
2999
3000#endif
3001
3002#ifdef SH_CUTEST
3003#include "CuTest.h"
3004
3005void Test_file_dequote (CuTest *tc)
3006{
3007#if (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE))
3008
3009 char str1[] = "1234567890";
3010 char str1a[] = "123456\\\"789\\r";
3011 char str1b[] = "12345678\\r9";
3012 char str1c[] = "12345678\\x0a_9";
3013 char str1d[] = "12345678\\007_9";
3014 char str1e[] = "123456789\\\\";
3015
3016 char str2[] = "1234567890\\xw";
3017 char str3[] = "1234567890\\xw99";
3018 char str4[] = "1234567890\\0ww";
3019 char str5[] = "12345\\g67890";
3020 char str6[] = "1234567890\\009a";
3021
3022 char *s, *p, *q;
3023 size_t lo, lr;
3024
3025 s = SH_ALLOC(64); sl_strlcpy(s, str1, 64); p = s; lo = strlen(s); lr = lo;
3026 q = sh_files_C_dequote(s, &lr);
3027 CuAssertPtrNotNull(tc, q);
3028 CuAssertTrue(tc, p == q);
3029 CuAssertTrue(tc, lr == lo);
3030
3031 s = SH_ALLOC(64); sl_strlcpy(s, str1a, 64); p = s; lo = strlen(s); lr = lo;
3032 q = sh_files_C_dequote(s, &lr);
3033 CuAssertPtrNotNull(tc, q);
3034 CuAssertTrue(tc, p != q);
3035 CuAssertTrue(tc, 0 == strcmp(q, "123456\"789\r"));
3036 CuAssertTrue(tc, lr == (lo-2));
3037
3038 s = SH_ALLOC(64); sl_strlcpy(s, str1b, 64); p = s; lo = strlen(s); lr = lo;
3039 q = sh_files_C_dequote(s, &lr);
3040 CuAssertPtrNotNull(tc, q);
3041 CuAssertTrue(tc, p != q);
3042 CuAssertTrue(tc, 0 == strcmp(q, "12345678\r9"));
3043 CuAssertTrue(tc, lr == (lo-1));
3044
3045 s = SH_ALLOC(64); sl_strlcpy(s, str1c, 64); p = s; lo = strlen(s); lr = lo;
3046 q = sh_files_C_dequote(s, &lr);
3047 CuAssertPtrNotNull(tc, q);
3048 CuAssertTrue(tc, p != q);
3049 CuAssertTrue(tc, 0 == strcmp(q, "12345678\x0a_9"));
3050 CuAssertTrue(tc, lr == (lo-3));
3051
3052 s = SH_ALLOC(64); sl_strlcpy(s, str1d, 64); p = s; lo = strlen(s); lr = lo;
3053 q = sh_files_C_dequote(s, &lr);
3054 CuAssertPtrNotNull(tc, q);
3055 CuAssertTrue(tc, p != q);
3056 CuAssertTrue(tc, 0 == strcmp(q, "12345678\007_9"));
3057 CuAssertTrue(tc, lr == (lo-3));
3058
3059 s = SH_ALLOC(64); sl_strlcpy(s, str1e, 64); p = s; lo = strlen(s); lr = lo;
3060 q = sh_files_C_dequote(s, &lr);
3061 CuAssertPtrNotNull(tc, q);
3062 CuAssertTrue(tc, p != q);
3063 CuAssertTrue(tc, 0 == strcmp(q, "123456789\\"));
3064 CuAssertTrue(tc, lr == (lo-1));
3065
3066 s = SH_ALLOC(64); sl_strlcpy(s, str2, 64); p = s; lo = strlen(s); lr = lo;
3067 q = sh_files_C_dequote(s, &lr);
3068 CuAssertTrue(tc, q == NULL);
3069 CuAssertTrue(tc, lr == 0);
3070
3071 s = SH_ALLOC(64); sl_strlcpy(s, str3, 64); p = s; lo = strlen(s); lr = lo;
3072 q = sh_files_C_dequote(s, &lr);
3073 CuAssertTrue(tc, q == NULL);
3074 CuAssertTrue(tc, lr == 0);
3075
3076 s = SH_ALLOC(64); sl_strlcpy(s, str4, 64); p = s; lo = strlen(s); lr = lo;
3077 q = sh_files_C_dequote(s, &lr);
3078 CuAssertTrue(tc, q == NULL);
3079 CuAssertTrue(tc, lr == 0);
3080
3081 s = SH_ALLOC(64); sl_strlcpy(s, str5, 64); p = s; lo = strlen(s); lr = lo;
3082 q = sh_files_C_dequote(s, &lr);
3083 CuAssertTrue(tc, q == NULL);
3084 CuAssertTrue(tc, lr == 0);
3085
3086 s = SH_ALLOC(64); sl_strlcpy(s, str6, 64); p = s; lo = strlen(s); lr = lo;
3087 q = sh_files_C_dequote(s, &lr);
3088 CuAssertTrue(tc, q == NULL);
3089 CuAssertTrue(tc, lr == 0);
3090
3091 return;
3092#else
3093 (void) tc; /* fix compiler warning */
3094 return;
3095#endif
3096}
3097#endif
3098
Note: See TracBrowser for help on using the repository browser.