source: trunk/src/sh_files.c@ 413

Last change on this file since 413 was 405, checked in by katerina, 12 years ago

Fix for tickets #303, #304, #305. #306, and #307. Update version number.

File size: 74.6 KB
RevLine 
[1]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
[377]22#if defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
23#define _XOPEN_SOURCE 500
24#endif
25
[1]26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
[286]29#include <ctype.h>
[1]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>
[386]38#include <sys/types.h>
39#include <sys/stat.h>
40#include <fcntl.h>
[1]41
[386]42#if !defined(O_NOATIME)
[395]43#if defined(__linux__) && (defined(__i386__) || defined(__x86_64__) || defined(__PPC__))
[386]44#define O_NOATIME 01000000
45#endif
46#endif
47
[1]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
[137]66#define NEED_ADD_DIRENT
[1]67
68#ifdef HAVE_GLOB_H
69#include <glob.h>
70#endif
[371]71#ifdef HAVE_FNMATCH_H
72#include <fnmatch.h>
73#endif
[1]74
[371]75
[1]76#include "samhain.h"
77
78#if (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE))
79
[131]80#include "sh_pthread.h"
[1]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"
[367]88#include "sh_inotify.h"
[1]89#include "zAVLTree.h"
90
91#undef FIL__
92#define FIL__ _("sh_files.c")
93
[367]94extern sh_watches sh_file_watches;
95
[286]96static char * sh_files_C_dequote (char * s, size_t * length)
97{
[362]98 size_t i, len = *length;
[286]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
[383]118 q = s;
[286]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
[1]213extern int flag_err_debug;
214extern int flag_err_info;
215
[22]216int sh_files_reportonce(const char * c)
[1]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
[22]225int sh_files_fulldetail(const char * c)
[1]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;
[40]243 long NumDoor;
244 long NumPort;
[1]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;
[114]257 short is_reported;
[1]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
[256]289#define SH_LIST_FILE 0
290#define SH_LIST_DIR1 1
291#define SH_LIST_DIR2 2
[1]292
[256]293
294static int which_dirList = SH_LIST_DIR1;
295
[1]296static zAVLTree * zdirListOne = NULL;
297static zAVLTree * zdirListTwo = NULL;
298static zAVLTree * zfileList = NULL;
299
[371]300SH_MUTEX_STATIC(mutex_zfiles, PTHREAD_MUTEX_INITIALIZER);
301SH_MUTEX_STATIC(mutex_zglob, PTHREAD_MUTEX_INITIALIZER);
[373]302SH_MUTEX_RECURSIVE(mutex_zdirs);
[1]303
[373]304static int sh_files_fullpath (const char * testdir,
305 const char * d_name,
[1]306 char * statpath);
[22]307static int sh_files_pushdir (int class, const char * str_s);
308static int sh_files_pushfile (int class, const char * str_s);
[1]309
310static long MaxRecursionLevel = 0;
311
312/* set default recursion level
313 */
[20]314int sh_files_setrecursion (const char * flag_s)
[1]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;
[34]347 char * dir;
[1]348 char * file;
[46]349 int tmp_reported;
[1]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 {
[34]363 dir = sh_util_dirname (ptr->name);
364 file = sh_util_basename (ptr->name);
[1]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 {
[356]372 char pstr[32];
[1]373#if !defined(WITH_TPT)
374 tmp = sh_util_safe_name (ptr->name);
375#endif
[356]376 sl_strlcpy(pstr, sh_hash_getpolicy(ptr->class), sizeof(pstr));
[365]377 sh_error_handle ((-1), FIL__, __LINE__, 0,
378 MSG_FI_CHK, pstr, tmp);
[1]379 }
380
[373]381 if ((sh.flag.inotify & SH_INOTIFY_INSCAN) != 0)
[367]382 {
383 sh_inotify_add_watch_later(ptr->name, &sh_file_watches, NULL,
[373]384 ptr->class, ptr->check_mask,
385 SH_INOTIFY_FILE, 0);
[367]386 }
387
[1]388 BREAKEXIT(sh_files_filecheck);
[114]389 tmp_reported = ptr->is_reported; /* fix aliasing warning */
[365]390 status = sh_files_filecheck (ptr->class, ptr->check_mask, dir, file,
[46]391 &tmp_reported, 0);
[114]392 ptr->is_reported = tmp_reported;
[1]393
394 TPT(( 0, FIL__, __LINE__,
395 _("msg=<filecheck complete: %s> status=<%d> reported=<%d>\n"),
[114]396 tmp, status, ptr->is_reported));
[1]397
[114]398 if (status == SH_FILE_UNKNOWN && (!SH_FFLAG_REPORTED_SET(ptr->is_reported)))
[1]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);
[405]419 ++sh.statistics.files_report;
[1]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)
[114]444 SET_SH_FFLAG_REPORTED(ptr->is_reported);
[1]445 }
446 else
447 {
448 /* exists (status >= 0), but was missing (reported == TRUE)
449 */
[114]450 if (status != SH_FILE_UNKNOWN && SH_FFLAG_REPORTED_SET(ptr->is_reported))
[1]451 {
[114]452 CLEAR_SH_FFLAG_REPORTED(ptr->is_reported);
[1]453 }
[402]454
[1]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 */
[305]463 if (sh_hash_have_it (ptr->name) >= 0 &&
464 !SH_FFLAG_REPORTED_SET(ptr->is_reported))
[1]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);
[405]479 ++sh.statistics.files_report;
[1]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 }
[94]511 if (file)
512 SH_FREE(file);
513 if (dir)
514 SH_FREE(dir);
[1]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
[371]527 SH_MUTEX_LOCK(mutex_zfiles);
[1]528 zAVLFreeTree (zfileList, free_dirstack);
529 zfileList = NULL;
[371]530 SH_MUTEX_UNLOCK(mutex_zfiles);
[1]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{
[373]558 volatile int ret;
559 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
560 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
[1]561 sh_files_setrec_int(zdirListOne);
[373]562 ret = sh_files_setrec_int(zdirListTwo);
563 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
564
565 return ret;
[1]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{
[373]579 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
580 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
[1]581 zdirListOne = sh_files_deldirstack_int(zdirListOne);
582 zdirListTwo = sh_files_deldirstack_int(zdirListTwo);
[373]583 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
[1]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
[371]594 SH_MUTEX_LOCK(mutex_zfiles);
[1]595 for (ptr = (dirstack_t *) zAVLFirst(&avlcursor, zfileList); ptr;
596 ptr = (dirstack_t *) zAVLNext(&avlcursor))
597 ptr->checked = 0;
[371]598 SH_MUTEX_UNLOCK(mutex_zfiles);
[1]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
[373]610 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
611 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
[1]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;
[373]619 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
[1]620
621 SL_RET0(_("sh_dirs_reset"));
622}
623
624
[22]625int sh_files_pushfile_prelink (const char * str_s)
[1]626{
627 return (sh_files_pushfile (SH_LEVEL_PRELINK, str_s));
628}
629
[22]630int sh_files_pushfile_user0 (const char * str_s)
[1]631{
632 return (sh_files_pushfile (SH_LEVEL_USER0, str_s));
633}
634
[22]635int sh_files_pushfile_user1 (const char * str_s)
[1]636{
637 return (sh_files_pushfile (SH_LEVEL_USER1, str_s));
638}
639
[27]640int sh_files_pushfile_user2 (const char * str_s)
641{
642 return (sh_files_pushfile (SH_LEVEL_USER2, str_s));
643}
[1]644
[27]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
[22]656int sh_files_pushfile_ro (const char * str_s)
[1]657{
658 return (sh_files_pushfile (SH_LEVEL_READONLY, str_s));
659}
660
[22]661int sh_files_pushfile_attr (const char * str_s)
[1]662{
663 return (sh_files_pushfile (SH_LEVEL_ATTRIBUTES, str_s));
664}
665
[22]666int sh_files_pushfile_log (const char * str_s)
[1]667{
668 return (sh_files_pushfile (SH_LEVEL_LOGFILES, str_s));
669}
670
[22]671int sh_files_pushfile_glog (const char * str_s)
[1]672{
673 return (sh_files_pushfile (SH_LEVEL_LOGGROW, str_s));
674}
675
[22]676int sh_files_pushfile_noig (const char * str_s)
[1]677{
678 return (sh_files_pushfile (SH_LEVEL_NOIGNORE, str_s));
679}
680
[22]681int sh_files_pushfile_allig (const char * str_s)
[1]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 */
[22]704static int sh_files_parse_mask (unsigned long * mask, const char * str)
[1]705{
706 int l, i = 0, act = 0, k = 0;
707 char myword[64];
708
709 SL_ENTER(_("sh_files_parse_mask"));
710
[381]711 myword[0] = '\0';
712
[1]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) {
[381]721
[1]722 if (str[i] == '\0')
723 break;
[381]724
[1]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;
[381]734 myword[0] = '\0';
735 goto getword;
[1]736 }
737 else if (str[i] == '-')
738 {
739 act = -1; ++i;
[381]740 myword[0] = '\0';
741 goto getword;
[1]742 }
743 else /* a word */
744 {
[381]745 getword:
[1]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
[381]754 if (sl_strlen(myword) == 0)
755 {
756 SL_RETURN ( (-1), _("sh_files_parse_mask"));
757 }
758
[1]759/* checksum */
[381]760 if (0 == strcmp(myword, _("CHK")))
[1]761 sh_files_set_mask (mask, MODI_CHK, act);
762/* link */
[381]763 else if (0 == strcmp(myword, _("LNK")))
[1]764 sh_files_set_mask (mask, MODI_LNK, act);
765/* inode */
[381]766 else if (0 == strcmp(myword, _("RDEV")))
[1]767 sh_files_set_mask (mask, MODI_RDEV, act);
768/* inode */
[381]769 else if (0 == strcmp(myword, _("INO")))
[1]770 sh_files_set_mask (mask, MODI_INO, act);
771/* user */
[381]772 else if (0 == strcmp(myword, _("USR")))
[1]773 sh_files_set_mask (mask, MODI_USR, act);
774/* group */
[381]775 else if (0 == strcmp(myword, _("GRP")))
[1]776 sh_files_set_mask (mask, MODI_GRP, act);
777/* mtime */
[381]778 else if (0 == strcmp(myword, _("MTM")))
[1]779 sh_files_set_mask (mask, MODI_MTM, act);
780/* ctime */
[381]781 else if (0 == strcmp(myword, _("CTM")))
[1]782 sh_files_set_mask (mask, MODI_CTM, act);
783/* atime */
[381]784 else if (0 == strcmp(myword, _("ATM")))
[1]785 sh_files_set_mask (mask, MODI_ATM, act);
786/* size */
[381]787 else if (0 == strcmp(myword, _("SIZ")))
[1]788 sh_files_set_mask (mask, MODI_SIZ, act);
789/* file mode */
[381]790 else if (0 == strcmp(myword, _("MOD")))
[1]791 sh_files_set_mask (mask, MODI_MOD, act);
792/* hardlinks */
[381]793 else if (0 == strcmp(myword, _("HLN")))
[1]794 sh_files_set_mask (mask, MODI_HLN, act);
[19]795/* size may grow */
[381]796 else if (0 == strcmp(myword, _("SGROW")))
[19]797 sh_files_set_mask (mask, MODI_SGROW, act);
798/* use prelink */
[381]799 else if (0 == strcmp(myword, _("PRE")))
[19]800 sh_files_set_mask (mask, MODI_PREL, act);
[167]801/* get content */
[381]802 else if (0 == strcmp(myword, _("TXT")))
[167]803 sh_files_set_mask (mask, MODI_TXT, act);
[294]804/* get content */
[381]805 else if (0 == strcmp(myword, _("AUDIT")))
[294]806 sh_files_set_mask (mask, MODI_AUDIT, act);
[381]807 else
808 {
809 SL_RETURN ( (-1), _("sh_files_parse_mask"));
810 }
811 act = 0;
812 myword[0] = '\0';
[1]813 }
814 }
815 SL_RETURN ( (0), _("sh_files_parse_mask"));
816}
817
[22]818int sh_files_redef_prelink(const char * str)
[1]819{
820 return (sh_files_parse_mask(&mask_PRELINK, str));
821}
[22]822int sh_files_redef_user0(const char * str)
[1]823{
824 return (sh_files_parse_mask(&mask_USER0, str));
825}
[22]826int sh_files_redef_user1(const char * str)
[1]827{
828 return (sh_files_parse_mask(&mask_USER1, str));
829}
[27]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}
[22]842int sh_files_redef_readonly(const char * str)
[1]843{
844 return (sh_files_parse_mask(&mask_READONLY, str));
845}
[22]846int sh_files_redef_loggrow(const char * str)
[1]847{
848 return (sh_files_parse_mask(&mask_LOGGROW, str));
849}
[22]850int sh_files_redef_logfiles(const char * str)
[1]851{
852 return (sh_files_parse_mask(&mask_LOGFILES, str));
853}
[22]854int sh_files_redef_attributes(const char * str)
[1]855{
856 return (sh_files_parse_mask(&mask_ATTRIBUTES, str));
857}
[22]858int sh_files_redef_noignore(const char * str)
[1]859{
860 return (sh_files_parse_mask(&mask_NOIGNORE, str));
861}
[22]862int sh_files_redef_allignore(const char * str)
[1]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:
[381]872 return (unsigned long) (mask_READONLY | MODI_INIT);
[1]873 case SH_LEVEL_ATTRIBUTES:
[381]874 return (unsigned long) (mask_ATTRIBUTES | MODI_INIT);
[1]875 case SH_LEVEL_LOGFILES:
[381]876 return (unsigned long) (mask_LOGFILES | MODI_INIT);
[1]877 case SH_LEVEL_LOGGROW:
[381]878 return (unsigned long) (mask_LOGGROW | MODI_INIT);
[1]879 case SH_LEVEL_ALLIGNORE:
[381]880 return (unsigned long) (mask_ALLIGNORE | MODI_INIT);
[1]881 case SH_LEVEL_NOIGNORE:
[381]882 return (unsigned long) (mask_NOIGNORE | MODI_INIT);
[1]883 case SH_LEVEL_USER0:
[381]884 return (unsigned long) (mask_USER0 | MODI_INIT);
[1]885 case SH_LEVEL_USER1:
[381]886 return (unsigned long) (mask_USER1 | MODI_INIT);
[27]887 case SH_LEVEL_USER2:
[381]888 return (unsigned long) (mask_USER2 | MODI_INIT);
[27]889 case SH_LEVEL_USER3:
[381]890 return (unsigned long) (mask_USER3 | MODI_INIT);
[27]891 case SH_LEVEL_USER4:
[381]892 return (unsigned long) (mask_USER4 | MODI_INIT);
[1]893 case SH_LEVEL_PRELINK:
[381]894 return (unsigned long) (mask_PRELINK | MODI_INIT);
[1]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;
[132]918 char errbuf[SH_ERRBUF_SIZE];
[1]919
920 SL_ENTER(_("sh_files_globerr"));
921
[61]922 if (errnum == ENOTDIR || errnum == ENOENT)
923 {
924 SL_RETURN(0, _("sh_files_globerr"));
925 }
926
[1]927 p = sh_util_safe_name (epath);
928 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, errnum, MSG_FI_GLOB,
[132]929 sh_error_message (errnum, errbuf, sizeof(errbuf)), p);
[1]930 SH_FREE(p);
931
932 SL_RETURN(0, _("sh_files_globerr"));
933}
934
935/* #ifdef HAVE_GLOB_H
936 */
937#endif
938
[383]939int sh_files_push_file_int (int class, const char * str_s, size_t len,
940 unsigned long check_mask)
[1]941{
942 dirstack_t * new_item_ptr;
943 char * fileName;
944 int ret;
[383]945 volatile int count = 0;
[1]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;
[381]956 new_item_ptr->check_mask = check_mask;
[1]957 new_item_ptr->rdepth = 0;
958 new_item_ptr->checked = S_FALSE;
[114]959 new_item_ptr->is_reported = 0;
[1]960 new_item_ptr->childs_checked = S_FALSE;
961
[371]962 SH_MUTEX_LOCK(mutex_zfiles);
[1]963 if (zfileList == NULL)
964 {
[363]965 zfileList = zAVLAllocTree (zdirstack_key, zAVL_KEY_STRING);
[1]966 if (zfileList == NULL)
967 {
[22]968 (void) safe_logger (0, 0, NULL);
[1]969 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
970 }
971 }
972
973 ret = zAVLInsert (zfileList, new_item_ptr);
[371]974 SH_MUTEX_UNLOCK(mutex_zfiles);
[1]975
976 if (-1 == ret)
977 {
[22]978 (void) safe_logger (0, 0, NULL);
[1]979 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
980 }
[371]981 else if (3 == ret)
[256]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);
[294]988 new_item_ptr = NULL;
[256]989 }
[371]990 else
[294]991 {
[373]992 int reported;
993 unsigned long check_mask = sh_files_maskof(class);
994
995 if ((sh.flag.inotify & SH_INOTIFY_INSCAN) != 0)
[371]996 {
[373]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);
[371]1004 }
[373]1005
1006 if (MODI_AUDIT_ENABLED(check_mask))
1007 {
1008 sh_audit_mark(str_s);
1009 }
1010 ++count;
[294]1011 }
[373]1012 SL_RETURN(count, _("sh_files_push_file_int"));
[1]1013}
1014
[381]1015int sh_files_push_dir_int (int class, char * tail, size_t len, int rdepth, unsigned long check_mask);
[1]1016
[256]1017#ifdef HAVE_GLOB_H
1018
1019typedef struct globstack_entry {
1020 char * name;
1021 int class;
[365]1022 unsigned long check_mask;
[256]1023 int rdepth;
1024 short type;
1025 /* struct dirstack_entry * next; */
1026} sh_globstack_t;
1027
1028static zAVLTree * zglobList = NULL;
1029
[373]1030static int sh_files_pushglob (int class, int type, const char * p, int rdepth,
[371]1031 unsigned long check_mask_in, int flag)
[1]1032{
1033 int globstatus = -1;
1034 unsigned int gloop;
[22]1035 glob_t pglob;
[371]1036
[383]1037 volatile int count = 0;
[381]1038 volatile unsigned long check_mask = (flag == 0) ? sh_files_maskof(class) : check_mask_in;
[256]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
[371]1054 SH_MUTEX_TRYLOCK(mutex_zfiles);
[256]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;
[381]1061 new_item_ptr->check_mask = check_mask;
[256]1062 new_item_ptr->rdepth = rdepth;
1063 new_item_ptr->type = type;
1064
1065 if (zglobList == NULL)
1066 {
[363]1067 zglobList = zAVLAllocTree (zdirstack_key, zAVL_KEY_STRING);
[256]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 }
[371]1082 SH_MUTEX_TRYLOCK_UNLOCK(mutex_zfiles);
[256]1083 }
1084
1085 for (gloop = 0; gloop < (unsigned int) pglob.gl_pathc; ++gloop)
1086 {
1087 if (type == SH_LIST_FILE)
1088 {
[373]1089 count += sh_files_push_file_int (class, pglob.gl_pathv[gloop],
[381]1090 sl_strlen(pglob.gl_pathv[gloop]), check_mask);
[256]1091 }
1092 else
1093 {
1094 which_dirList = type;
1095
[373]1096 count += sh_files_push_dir_int (class, pglob.gl_pathv[gloop],
[381]1097 sl_strlen(pglob.gl_pathv[gloop]), rdepth, check_mask);
[256]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
[1]1108#endif
[256]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);
[373]1136 SL_RETURN(count, _("sh_files_pushglob"));
1137 return count;
[256]1138}
[1]1139
[371]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
[256]1163void sh_files_check_globPatterns()
1164{
1165 sh_globstack_t * testPattern;
1166 zAVLCursor cursor;
1167
1168 SL_ENTER(_("sh_files_check_globPatterns"));
1169
[371]1170 SH_MUTEX_LOCK(mutex_zglob);
1171 for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList);
1172 testPattern;
[256]1173 testPattern = (sh_globstack_t *) zAVLNext (&cursor))
1174 {
1175 sh_files_pushglob(testPattern->class, testPattern->type,
[365]1176 testPattern->name, testPattern->rdepth,
1177 testPattern->check_mask, 1);
[256]1178 }
[371]1179 SH_MUTEX_UNLOCK(mutex_zglob);
[256]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{
[365]1203 SL_ENTER(_("sh_files_delglobstack"));
[256]1204
[371]1205 SH_MUTEX_LOCK(mutex_zglob);
[256]1206 zAVLFreeTree (zglobList, free_globstack);
1207 zglobList = NULL;
[371]1208 SH_MUTEX_UNLOCK(mutex_zglob);
[256]1209
[365]1210 SL_RETURN(0, _("sh_files_delglobstack"));
[256]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
[1]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 ();
[256]1245 sh_files_delglobstack ();
[1]1246 reject = 1;
1247 }
1248
[286]1249 if (str_s == NULL || str_s[0] == '\0')
[1]1250 SL_RETURN((-1),_("sh_files_pushfile"));
1251
1252 len = sl_strlen(str_s);
1253
[286]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)
[310]1271 SL_RETURN((-1), _("sh_files_pushfile"));
[286]1272
[1]1273 if (len >= PATH_MAX)
1274 {
1275 /* Name too long
1276 */
[286]1277 tmp = sh_util_safe_name (p);
[1]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 }
[286]1283 else if (p[0] != '/')
[1]1284 {
1285 /* Not an absolute path
1286 */
[286]1287 tmp = sh_util_safe_name (p);
[1]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 */
[22]1298 if (p[len-1] == '/' && len > 1)
[1]1299 {
[22]1300 p[len-1] = '\0';
[1]1301 --len;
1302 }
1303 }
1304
1305#ifdef HAVE_GLOB_H
[22]1306 if (0 == sh_files_has_metachar(p))
[1]1307 {
[381]1308 sh_files_push_file_int (class, p, len, sh_files_maskof(class));
[1]1309 }
1310 else
1311 {
[365]1312 sh_files_pushglob (class, SH_LIST_FILE, p, 0, 0, 0);
[1]1313 }
1314
1315#else
[381]1316 sh_files_push_file_int (class, p, len, sh_files_maskof(class));
[1]1317#endif
1318
[22]1319 SH_FREE(p);
[1]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{
[373]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;
[1]1360}
1361
[371]1362static void * sh_dummy_ptr;
1363
[1]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;
[371]1371 volatile unsigned long dcount = 0;
[1]1372 char * tmp;
1373
1374 SL_ENTER(_("sh_dirs_chk"));
1375
[371]1376 sh_dummy_ptr = (void *) &ptr;
1377
[373]1378 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
1379 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
[1]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) {
[373]1389 goto out;
[1]1390 }
1391
1392 if (ptr->checked == S_FALSE)
1393 {
[371]1394 SH_MUTEX_LOCK(mutex_zfiles);
[1]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);
[373]1404 sh_files_filecheck (dst_ptr->class, dst_ptr->check_mask,
1405 ptr->name,
[1]1406 NULL, &status, 0);
1407 dst_ptr->checked = S_TRUE;
1408 status = S_TRUE;
1409 }
1410 else
1411 {
1412 status = S_TRUE;
1413 }
1414 }
[371]1415 SH_MUTEX_UNLOCK(mutex_zfiles);
[1]1416
1417 if (status == S_FALSE)
[373]1418 sh_files_filecheck (ptr->class, ptr->check_mask,
1419 ptr->name, NULL, &status, 0);
[1]1420
1421 BREAKEXIT(sh_files_checkdir);
[373]1422 status = sh_files_checkdir (ptr->class, ptr->check_mask,
1423 ptr->rdepth, ptr->name,
[1]1424 ptr->name);
1425
[114]1426 if (status < 0 && (!SH_FFLAG_REPORTED_SET(ptr->is_reported)))
[1]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);
[405]1441 ++sh.statistics.files_report;
[1]1442 SH_FREE(tmp);
1443 }
1444 }
1445 if (sh.flag.reportonce == S_TRUE)
[114]1446 SET_SH_FFLAG_REPORTED(ptr->is_reported);
[1]1447 }
1448 else
1449 {
1450 /* exists (status >= 0), but was missing (reported == TRUE)
1451 */
[114]1452 if (status >= 0 && SH_FFLAG_REPORTED_SET(ptr->is_reported))
[1]1453 {
[114]1454 CLEAR_SH_FFLAG_REPORTED(ptr->is_reported);
[1]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);
[405]1465 ++sh.statistics.files_report;
[1]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 }
[19]1484 ptr->checked = S_TRUE;
1485 ptr->childs_checked = S_TRUE;
[1]1486 }
1487
1488 if (sig_urgent > 0) {
[373]1489 goto out;
[1]1490 }
1491
1492 }
[373]1493 out:
[377]1494 ; /* 'label at end of compound statement' */
[373]1495 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
1496
[1]1497 SL_RETURN(dcount, _("sh_dirs_chk"));
1498}
1499
[22]1500int sh_files_pushdir_prelink (const char * str_s)
[1]1501{
1502 return (sh_files_pushdir (SH_LEVEL_PRELINK, str_s));
1503}
1504
[22]1505int sh_files_pushdir_user0 (const char * str_s)
[1]1506{
1507 return (sh_files_pushdir (SH_LEVEL_USER0, str_s));
1508}
1509
[22]1510int sh_files_pushdir_user1 (const char * str_s)
[1]1511{
1512 return (sh_files_pushdir (SH_LEVEL_USER1, str_s));
1513}
1514
[27]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
[22]1530int sh_files_pushdir_attr (const char * str_s)
[1]1531{
1532 return (sh_files_pushdir (SH_LEVEL_ATTRIBUTES, str_s));
1533}
1534
[22]1535int sh_files_pushdir_ro (const char * str_s)
[1]1536{
1537 return (sh_files_pushdir (SH_LEVEL_READONLY, str_s));
1538}
1539
[22]1540int sh_files_pushdir_log (const char * str_s)
[1]1541{
1542 return (sh_files_pushdir (SH_LEVEL_LOGFILES, str_s));
1543}
1544
[22]1545int sh_files_pushdir_glog (const char * str_s)
[1]1546{
1547 return (sh_files_pushdir (SH_LEVEL_LOGGROW, str_s));
1548}
1549
[22]1550int sh_files_pushdir_noig (const char * str_s)
[1]1551{
1552 return (sh_files_pushdir (SH_LEVEL_NOIGNORE, str_s));
1553}
1554
[22]1555int sh_files_pushdir_allig (const char * str_s)
[1]1556{
1557 return (sh_files_pushdir (SH_LEVEL_ALLIGNORE, str_s));
1558}
1559
1560int set_dirList (int which)
1561{
1562 if (which == 2)
[256]1563 which_dirList = SH_LIST_DIR2;
[1]1564 else
[256]1565 which_dirList = SH_LIST_DIR1;
[1]1566 return 0;
1567}
1568
[381]1569int sh_files_push_dir_int (int class, char * tail, size_t len, int rdepth, unsigned long check_mask)
[1]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;
[381]1585 new_item_ptr->check_mask = check_mask;
[1]1586 new_item_ptr->rdepth = rdepth;
1587 new_item_ptr->checked = S_FALSE;
[114]1588 new_item_ptr->is_reported = 0;
[1]1589 new_item_ptr->childs_checked = S_FALSE;
1590
[373]1591 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
1592 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
[256]1593 if (which_dirList == SH_LIST_DIR1)
[1]1594 {
1595 tree = zdirListOne;
1596 }
1597 else
1598 {
1599 tree = zdirListTwo;
1600 }
1601
1602 if (tree == NULL)
1603 {
[363]1604 tree = zAVLAllocTree (zdirstack_key, zAVL_KEY_STRING);
[1]1605 if (tree == NULL)
1606 {
[22]1607 (void) safe_logger (0, 0, NULL);
[1]1608 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
1609 }
[256]1610 if (which_dirList == SH_LIST_DIR1)
[1]1611 zdirListOne = tree;
1612 else
1613 zdirListTwo = tree;
1614 }
1615
1616 ret = zAVLInsert (tree, new_item_ptr);
[373]1617 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
[1]1618
1619 if (-1 == ret)
1620 {
[22]1621 (void) safe_logger (0, 0, NULL);
[1]1622 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
1623 }
1624 if (3 == ret)
[256]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);
[294]1631 new_item_ptr = NULL;
[256]1632 }
[373]1633 else
[294]1634 {
[381]1635 if (MODI_AUDIT_ENABLED(check_mask))
[373]1636 {
1637 sh_audit_mark(tail);
1638 }
[294]1639 }
[1]1640 SL_RETURN(0, _("sh_files_push_dir_int"));
1641}
1642
[22]1643static int sh_files_pushdir (int class, const char * str_s)
[1]1644{
1645 char * tmp;
[34]1646 size_t len;
[1]1647 int rdepth = 0;
1648 char * tail = NULL;
[22]1649 char * p;
[1]1650
1651 SL_ENTER(_("sh_files_pushdir"));
1652
1653 if (sh.flag.opts == 1) {
1654 sh_files_delfilestack ();
1655 sh_files_deldirstack ();
[256]1656 sh_files_delglobstack ();
[1]1657 }
1658
[286]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)
[1]1681 SL_RETURN((-1), _("sh_files_pushdir"));
1682
[22]1683 if (p[0] != '/')
[1]1684 {
[22]1685 rdepth = strtol(p, &tail, 10);
1686 if (tail == p)
1687 {
1688 SH_FREE(p);
1689 SL_RETURN((-1), _("sh_files_pushdir"));
1690 }
[1]1691 }
1692 else
[22]1693 tail = p;
[1]1694
1695
[310]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 }
[1]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);
[22]1718 SH_FREE(p);
[1]1719 SL_RETURN((-1), _("sh_files_pushdir"));
1720 }
1721 else if (len < 1)
1722 {
[22]1723 SH_FREE(p);
[1]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);
[22]1732 SH_FREE(p);
[1]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 {
[381]1747 sh_files_push_dir_int (class, tail, len, rdepth, sh_files_maskof(class));
[1]1748 }
1749 else
1750 {
[365]1751 sh_files_pushglob (class, which_dirList, tail, rdepth, 0, 0);
[1]1752 }
1753#else
[381]1754 sh_files_push_dir_int (class, tail, len, rdepth, sh_files_maskof(class));
[1]1755#endif
1756
[22]1757 SH_FREE(p);
[1]1758 SL_RETURN((0), _("sh_files_pushdir"));
[34]1759}
[1]1760
[131]1761/**
[1]1762struct sh_dirent {
1763 char * sh_d_name;
1764 struct sh_dirent * next;
1765};
[131]1766**/
[1]1767
[131]1768void kill_sh_dirlist (struct sh_dirent * dirlist)
[1]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 */
[131]1784struct sh_dirent * addto_sh_dirlist (struct dirent * thisEntry,
1785 struct sh_dirent * dirlist)
[1]1786{
1787 struct sh_dirent * this;
[34]1788 size_t len;
[1]1789
1790 if (thisEntry == NULL)
1791 return dirlist;
1792
[34]1793 len = sl_strlen(thisEntry->d_name);
1794 if (len == 0)
[1]1795 return dirlist;
[34]1796 ++len;
[1]1797
1798 this = SH_ALLOC(sizeof(struct sh_dirent));
1799 if (!this)
1800 return dirlist;
1801
[34]1802 this->sh_d_name = SH_ALLOC(len);
1803 sl_strlcpy(this->sh_d_name, thisEntry->d_name, len);
[1]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 */
[22]1813int sh_files_check_hardlinks (const char * opt)
[1]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
[22]1829int sh_files_hle_reg (const char * str)
[1]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
[371]1893static void * sh_dummy_dirlist;
[383]1894static void * sh_dummy_tmpcat;
[371]1895
[373]1896/* -- Check a single directory and its content. Does not
1897 * check the directory inode itself.
[1]1898 */
[373]1899int sh_files_checkdir (int iclass, unsigned long check_mask,
1900 int idepth, char * iname,
1901 char * relativeName)
[1]1902{
[170]1903 struct sh_dirent * dirlist;
1904 struct sh_dirent * dirlist_orig;
[1]1905
1906 DIR * thisDir = NULL;
1907 struct dirent * thisEntry;
1908 int status;
1909 int dummy = S_FALSE;
[227]1910 dir_type * theDir;
[1]1911 ShFileType checkit;
[131]1912 static unsigned int state = 1;
[1]1913
[227]1914 file_type * theFile;
[1]1915 char * tmpname;
1916 char * tmpcat;
[132]1917 char errbuf[SH_ERRBUF_SIZE];
[1]1918
1919 int rdepth = 0;
1920 int class = 0;
[377]1921 volatile int rdepth_next;
[383]1922 volatile int class_next;
[371]1923 volatile int file_class_next;
[383]1924 volatile unsigned long check_mask_next;
[371]1925 volatile unsigned long file_check_mask_next;
[1]1926
[371]1927 volatile int checked_flag = S_FALSE;
1928 volatile int cchecked_flag = S_FALSE;
[1]1929
1930 dirstack_t * dst_ptr;
[19]1931 dirstack_t * tmp_ptr;
[1]1932
1933 int hardlink_num = 0;
[34]1934#if !defined(HOST_IS_DARWIN)
1935 size_t len;
1936#endif
[1]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
[8]1965 if (flag_err_info == SL_TRUE)
1966 {
[356]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);
[8]1971 }
1972
[1]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 }
[365]1993
[1]1994 /* ---- stat the directory ----
1995 */
[227]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;
[365]2000 theFile->check_mask = check_mask;
[1]2001
2002 (void) relativeName;
2003 status = sh_unix_getinfo (ShDFLevel[SH_ERR_T_DIR],
2004 iname,
[227]2005 theFile, NULL, iclass);
[1]2006
2007 if ((sig_termfast == 1) || (sig_terminate == 1))
2008 {
[227]2009 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2010 if (theFile->link_path) SH_FREE(theFile->link_path);
2011 SH_FREE(theFile);
[1]2012 SL_RETURN((0), _("sh_files_checkdir"));
2013 }
2014
2015 if (status == -1)
2016 {
2017 SH_FREE(tmpname);
[227]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"));
[1]2022 }
2023
[227]2024 if (theFile->c_mode[0] != 'd')
[1]2025 {
2026 sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
2027 MSG_FI_NODIR,
2028 tmpname);
[405]2029 ++sh.statistics.files_nodir;
[1]2030 SH_FREE(tmpname);
[227]2031 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2032 if (theFile->link_path) SH_FREE(theFile->link_path);
2033 SH_FREE(theFile);
[1]2034 SL_RETURN((-1), _("sh_files_checkdir"));
2035 }
2036
[373]2037 if ((sh.flag.inotify & SH_INOTIFY_INSCAN) != 0)
[372]2038 {
2039 sh_inotify_add_watch_later(iname, &sh_file_watches, &status,
[373]2040 iclass, check_mask, SH_INOTIFY_DIR, idepth);
[372]2041 }
2042
[227]2043 hardlink_num = theFile->hardlinks;
[1]2044
[227]2045 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2046 if (theFile->link_path) SH_FREE(theFile->link_path);
2047 SH_FREE(theFile);
[1]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,
[132]2061 sh_error_message (status, errbuf, sizeof(errbuf)), tmpname);
[1]2062 SH_FREE(tmpname);
2063 SL_RETURN((-1), _("sh_files_checkdir"));
2064 }
2065
[227]2066 theDir = SH_ALLOC(sizeof(dir_type));
[1]2067
[227]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);
[1]2080
[227]2081
[383]2082 sh_dummy_dirlist = (void *) &dirlist;
2083 sh_dummy_tmpcat = (void *) &tmpcat;
2084
[1]2085 /* ---- read ----
2086 */
[137]2087 SH_MUTEX_LOCK(mutex_readdir);
[131]2088
[170]2089 dirlist = NULL;
2090 dirlist_orig = NULL;
2091
[1]2092 do {
2093 thisEntry = readdir (thisDir);
2094 if (thisEntry != NULL)
2095 {
[227]2096 ++theDir->NumAll;
[1]2097 if (sl_strcmp (thisEntry->d_name, ".") == 0)
2098 {
[227]2099 ++theDir->NumDirs;
[1]2100 continue;
2101 }
2102 if (sl_strcmp (thisEntry->d_name, "..") == 0)
2103 {
[227]2104 ++theDir->NumDirs;
[1]2105 continue;
2106 }
2107 dirlist = addto_sh_dirlist (thisEntry, dirlist);
2108 }
2109 } while (thisEntry != NULL);
2110
[137]2111 SH_MUTEX_UNLOCK(mutex_readdir);
[131]2112
[1]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 {
[227]2128 SH_FREE(theDir);
[1]2129 SL_RETURN((0), _("sh_files_checkdir"));
2130 }
2131
2132 BREAKEXIT(sh_derr);
[131]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
[1]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;
[365]2150 check_mask_next = check_mask;
[1]2151 file_class_next = class;
[365]2152 file_check_mask_next = check_mask;
[1]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 */
[373]2160 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
2161 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
[1]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;
[365]2171 file_check_mask_next = dst_ptr->check_mask;
[1]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;
[365]2187 file_check_mask_next = dst_ptr->check_mask;
[1]2188 checked_flag = dst_ptr->checked;
2189 cchecked_flag = dst_ptr->childs_checked;
2190 }
2191 }
[373]2192 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
[1]2193
[373]2194 SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
[1]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;
[365]2204 file_check_mask_next = dst_ptr->check_mask;
[1]2205 checked_flag = dst_ptr->checked;
[19]2206 /* not set, hence always FALSE */
2207 /* cchecked_flag = dst_ptr->childs_checked; */
[373]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 }
[1]2216 }
[373]2217 SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
[1]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 {
[227]2228 ++theDir->NumDirs;
[1]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 */
[373]2247 /* -- moved up --
2248 * if (dst_ptr && sh.flag.reportonce == S_TRUE)
2249 * dummy = dst_ptr->is_reported;
2250 */
[1]2251
[365]2252 checkit = sh_files_filecheck (file_class_next, file_check_mask_next,
[1]2253 iname,
2254 dirlist->sh_d_name,
2255 &dummy, 0);
2256
[373]2257
2258 SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
2259 dst_ptr = (dirstack_t *) zAVLSearch(zfileList, tmpcat);
2260
[1]2261 if (dst_ptr && checked_flag == S_FALSE)
2262 dst_ptr->checked = S_TRUE;
[373]2263
[1]2264 /* Thu Mar 7 15:09:40 CET 2002 Propagate the 'reported' flag
2265 */
2266 if (dst_ptr && sh.flag.reportonce == S_TRUE)
[114]2267 dst_ptr->is_reported = dummy;
[373]2268
2269 if (dst_ptr)
2270 dst_ptr->childs_checked = S_TRUE;
2271 SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
[1]2272 }
2273
2274 if (checkit == SH_FILE_REGULAR)
[227]2275 ++theDir->NumRegular;
[1]2276
2277 else if (checkit == SH_FILE_DIRECTORY)
2278 {
[227]2279 ++theDir->NumDirs;
[365]2280
[1]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
[373]2291 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
2292 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
[19]2293 tmp_ptr = (dirstack_t *) zAVLSearch(zdirListOne, tmpcat);
[1]2294
[19]2295 if (tmp_ptr)
[1]2296 {
2297 TPT((0, FIL__, __LINE__,
2298 _("msg=<%s -> recursion depth %d\n>"),
[19]2299 tmp_ptr->name, tmp_ptr->rdepth));
2300 rdepth_next = tmp_ptr->rdepth;
2301 class_next = tmp_ptr->class;
[365]2302 check_mask_next = tmp_ptr->check_mask;
[1]2303 /* 28. Aug 2001 reversed
2304 */
[19]2305 cchecked_flag = tmp_ptr->childs_checked;
2306 checked_flag = tmp_ptr->checked;
[1]2307 }
2308
2309 if (checked_flag == -1)
2310 {
[19]2311 tmp_ptr = (dirstack_t *) zAVLSearch(zdirListTwo, tmpcat);
[1]2312
[19]2313 if (tmp_ptr)
[1]2314 {
2315 TPT((0, FIL__, __LINE__,
2316 _("msg=<%s -> recursion depth %d\n>"),
[19]2317 tmp_ptr->name, tmp_ptr->rdepth));
2318 rdepth_next = tmp_ptr->rdepth;
2319 class_next = tmp_ptr->class;
[365]2320 check_mask_next = tmp_ptr->check_mask;
[1]2321 /* 28. Aug 2001 reversed
2322 */
[19]2323 cchecked_flag = tmp_ptr->childs_checked;
2324 checked_flag = tmp_ptr->checked;
[1]2325 }
2326 }
[373]2327
2328 if (tmp_ptr && cchecked_flag == S_FALSE)
[1]2329 {
[19]2330 tmp_ptr->childs_checked = S_TRUE;
2331 /*
2332 * 04. Feb 2006 avoid double checking
2333 */
2334 tmp_ptr->checked = S_TRUE;
[1]2335 }
[373]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 }
[1]2347 else if (checked_flag == -1)
[373]2348 sh_files_checkdir (class_next, check_mask_next, rdepth_next,
2349 tmpcat, dirlist->sh_d_name);
[1]2350
2351 }
2352 }
2353
[227]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;
[1]2361
2362 SH_FREE(tmpcat);
2363
2364 if ((sig_termfast == 1) || (sig_terminate == 1))
2365 {
[227]2366 SH_FREE(theDir);
[383]2367 sh_dummy_dirlist = NULL;
[1]2368 SL_RETURN((0), _("sh_files_checkdir"));
2369 }
2370
2371 dirlist = dirlist->next;
[19]2372
[373]2373 /* -- moved up, only affects zfileList anyway
2374 * if (dst_ptr)
2375 * dst_ptr->childs_checked = S_TRUE;
2376 */
2377
[1]2378 } while (dirlist != NULL);
2379
2380 if (flag_err_info == SL_TRUE)
2381 {
2382 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DSUM,
[227]2383 theDir->NumDirs,
2384 theDir->NumRegular,
2385 theDir->NumSymlinks,
2386 theDir->NumFifos,
2387 theDir->NumSockets,
2388 theDir->NumCDev,
2389 theDir->NumBDev);
[1]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 */
[227]2398 if ((sh_check_hardlinks == S_TRUE) && (hardlink_num != theDir->NumDirs))
[1]2399 {
[227]2400 if (0 != sh_files_hle_test(hardlink_num-theDir->NumDirs, iname))
[1]2401 {
[34]2402 len = strlen(tmpname);
2403 if (sl_ok_adds(len, 256))
2404 len += 256;
2405 tmpcat = SH_ALLOC(len);
2406 sl_snprintf(tmpcat, len,
[1]2407 _("%s: subdirectory count (%d) != hardlinks (%d)"),
[227]2408 tmpname, theDir->NumDirs, hardlink_num);
[1]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);
[227]2417 SH_FREE(theDir);
[1]2418
[383]2419 sh_dummy_dirlist = NULL;
2420
[1]2421 SL_RETURN((0), _("sh_files_checkdir"));
2422}
2423
2424int get_the_fd (SL_TICKET ticket);
2425
[254]2426static int sh_use_rsrc = S_FALSE;
[1]2427
[254]2428int sh_files_use_rsrc(const char * str)
2429{
2430 return sh_util_flagval(str, &sh_use_rsrc);
2431}
2432
[365]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,
[373]2438 const char * dirName,
2439 const char * infileName,
[365]2440 int * reported,
2441 int rsrcflag)
[1]2442{
2443 /* 28 Aug 2001 allow NULL fileName
2444 */
[227]2445 char * fullpath;
[19]2446 char fileHash[2*(KEY_LEN + 1)];
[1]2447 int status;
[227]2448 file_type * theFile;
[1]2449 char * tmpdir;
2450 char * tmpname;
[373]2451 const char * fileName;
[1]2452 struct utimbuf utime_buf;
[131]2453 static unsigned int state = 1;
[227]2454 char sc;
[1]2455
2456 SL_ENTER(_("sh_files_filecheck"));
2457
[227]2458 fullpath = SH_ALLOC(PATH_MAX);
2459 theFile = SH_ALLOC(sizeof(file_type));
2460
[365]2461 /* Take the address to keep gcc from putting it into a register.
2462 * Avoids the 'clobbered by longjmp' warning.
2463 */
2464 sh_dummy_fileName = (void *) &fileName;
2465 sh_dummy_tmpname = (void *) &tmpname;
2466 sh_dummy_tmpdir = (void *) &tmpdir;
2467
[1]2468 BREAKEXIT(sh_derr);
2469
[131]2470#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_RAND_R)
2471 if (0 == (rand_r(&state) % 2)) (void) sh_derr();
2472#else
2473 if (0 == state * (rand() % 2)) (void) sh_derr();
2474#endif
2475
[34]2476 if (dirName && infileName && (dirName[0] == '/') && (dirName[1] == '\0')
2477 && (infileName[0] == '/') && (infileName[1] == '\0'))
2478 {
2479 fileName = NULL;
2480 }
2481 else
2482 {
2483 fileName = infileName;
2484 }
2485
[1]2486 /* fileName may be NULL if this is a directory
2487 */
2488 if (dirName == NULL /* || fileName == NULL */)
2489 {
[365]2490 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]2491 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_NULL);
[365]2492 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[227]2493 SH_FREE(fullpath);
2494 SH_FREE(theFile);
[1]2495 SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
2496 }
2497
2498 if ((fileName != NULL) && (class != SH_LEVEL_ALLIGNORE) &&
2499 (0 != sh_util_obscurename (ShDFLevel[SH_ERR_T_NAME],
2500 fileName, S_FALSE)))
2501 {
2502 if ((dirName != NULL) && (dirName[0] == '/') && (dirName[1] == '\0'))
2503 {
2504 tmpname = sh_util_safe_name (fileName);
[365]2505 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]2506 sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, 0,
2507 MSG_FI_OBSC2,
2508 "", tmpname);
[365]2509 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]2510 SH_FREE(tmpname);
2511 }
2512 else
2513 {
2514 tmpdir = sh_util_safe_name (dirName);
2515 tmpname = sh_util_safe_name (fileName);
[365]2516 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]2517 sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, 0,
2518 MSG_FI_OBSC2,
2519 tmpdir, tmpname);
[365]2520 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]2521 SH_FREE(tmpname);
2522 SH_FREE(tmpdir);
2523 }
2524 }
2525
2526 /* sh_files_fullpath accepts NULL fileName
2527 */
2528 if (0 != sh_files_fullpath (dirName, fileName, fullpath))
2529 {
2530 tmpdir = sh_util_safe_name (dirName);
2531 tmpname = sh_util_safe_name (fileName);
[365]2532 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]2533 sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, 0,
2534 MSG_FI_2LONG2,
2535 tmpdir, tmpname);
[365]2536 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]2537 SH_FREE(tmpname);
2538 SH_FREE(tmpdir);
[227]2539 SH_FREE(fullpath);
2540 SH_FREE(theFile);
[1]2541 SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
2542 }
2543
2544
2545 /* stat the file and determine checksum (if a regular file)
2546 */
[227]2547 sl_strlcpy (theFile->fullpath, fullpath, PATH_MAX);
[365]2548 theFile->check_mask = check_mask /* sh_files_maskof(class) */;
[227]2549 theFile->file_reported = (*reported);
2550 theFile->attr_string = NULL;
2551 theFile->link_path = NULL;
[1]2552
2553 TPT(( 0, FIL__, __LINE__, _("msg=<checking file: %s>\n"), fullpath));
2554
2555 status = sh_unix_getinfo ( (class == SH_LEVEL_ALLIGNORE) ?
2556 ShDFLevel[class] : ShDFLevel[SH_ERR_T_FILE],
2557 fileName,
[227]2558 theFile, fileHash, class);
[1]2559
2560 if (status != 0)
2561 {
2562 TPT(( 0, FIL__, __LINE__, _("msg=<file: %s> status=<%d>\n"),
2563 fullpath, status));
2564 if (class == SH_LEVEL_ALLIGNORE && sh.flag.checkSum != SH_CHECK_INIT)
[68]2565 sh_hash_set_visited_true (fullpath);
[227]2566 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2567 if (theFile->link_path) SH_FREE(theFile->link_path);
2568 SH_FREE(fullpath);
2569 SH_FREE(theFile);
[1]2570 SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
2571 }
2572
2573 if (sig_termfast == 1) {
2574 goto ret_point;
2575 }
2576
2577 /* report
2578 */
[227]2579 if ((flag_err_debug == SL_TRUE) && (theFile->c_mode[0] == '-'))
[1]2580 {
2581 tmpname = sh_util_safe_name (fullpath); /* fixed in 1.5.4 */
[365]2582 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]2583 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_CSUM,
2584 fileHash, tmpname);
[365]2585 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]2586 SH_FREE(tmpname);
2587 }
2588 ++sh.statistics.files_checked;
2589
[77]2590 if ( sh.flag.checkSum == SH_CHECK_INIT /* && sh.flag.update == S_FALSE */)
[1]2591 {
[227]2592 sh_hash_pushdata (theFile, fileHash);
[1]2593 }
2594 else if (sh.flag.checkSum == SH_CHECK_CHECK
2595 /* && theFile.c_mode[0] == '-' */
2596 /* && class != SH_LEVEL_ALLIGNORE */
2597 )
2598 {
[227]2599 sh_hash_compdata (class, theFile, fileHash, NULL, -1);
[1]2600 }
2601
[227]2602 (*reported) = theFile->file_reported;
[1]2603
2604 /* reset the access time
2605 */
[227]2606 if (class == SH_LEVEL_NOIGNORE && (theFile->check_mask & MODI_ATM) != 0)
[1]2607 {
[227]2608 utime_buf.actime = (time_t) theFile->atime;
2609 utime_buf.modtime = (time_t) theFile->mtime;
[1]2610#if !defined(O_NOATIME)
2611 retry_aud_utime (FIL__, __LINE__, fullpath, &utime_buf);
2612#endif
2613 }
2614
[254]2615#if defined(HOST_IS_DARWIN)
[1]2616 /*
2617 * Check for resource fork
2618 */
[254]2619 if ( (sh_use_rsrc == S_TRUE) && (theFile->c_mode[0] != 'd') && (rsrcflag == 0) )
[1]2620 {
2621 int dummy;
2622 static int rsrc_init = 0;
2623 static char rsrc[17];
[242]2624 char * testpath = SH_ALLOC(PATH_MAX);
[1]2625
2626 if (rsrc_init == 0) {
2627 sl_strlcpy(rsrc, _("..namedfork/rsrc"), 17);
2628 rsrc_init = 1;
2629 }
2630 sl_strlcpy (testpath, fullpath, PATH_MAX);
2631 sl_strlcat (testpath, "/", PATH_MAX);
2632 sl_strlcat (testpath, rsrc, PATH_MAX);
2633
2634 if (sl_strlen(testpath) == (17 + sl_strlen(fullpath)))
2635 {
[78]2636 if (S_TRUE == sh_unix_file_exists (testpath))
[1]2637 {
[378]2638 sh_files_filecheck (class, check_mask, fullpath, rsrc, &dummy, 1);
[1]2639 }
2640 }
[227]2641 SH_FREE(testpath);
[1]2642 }
2643#else
[68]2644 (void) rsrcflag; /* avoid compiler warning */
[1]2645#endif
2646
2647 ret_point:
2648
[227]2649 sc = theFile->c_mode[0];
[68]2650
[227]2651 if (theFile->attr_string) SH_FREE(theFile->attr_string);
2652 if (theFile->link_path) SH_FREE(theFile->link_path);
2653 SH_FREE(fullpath);
2654 SH_FREE(theFile);
2655
2656 switch (sc)
[1]2657 {
2658 case '-': SL_RETURN(SH_FILE_REGULAR, _("sh_files_filecheck"));
2659 case 'l': SL_RETURN(SH_FILE_SYMLINK, _("sh_files_filecheck"));
2660 case 'd': SL_RETURN(SH_FILE_DIRECTORY, _("sh_files_filecheck"));
2661 case 'c': SL_RETURN(SH_FILE_CDEV, _("sh_files_filecheck"));
2662 case 'b': SL_RETURN(SH_FILE_BDEV, _("sh_files_filecheck"));
2663 case '|': SL_RETURN(SH_FILE_FIFO, _("sh_files_filecheck"));
[40]2664 case 'D': SL_RETURN(SH_FILE_DOOR, _("sh_files_filecheck"));
2665 case 'P': SL_RETURN(SH_FILE_PORT, _("sh_files_filecheck"));
[1]2666 case 's': SL_RETURN(SH_FILE_SOCKET, _("sh_files_filecheck"));
2667 default: SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
2668 }
2669
2670 /* notreached */
2671}
2672
2673/* concatenate statpath = testdir"/"d_name
2674 */
[373]2675static int sh_files_fullpath (const char * testdir, const char * d_name,
2676 char * statpath)
[1]2677{
2678 int llen = 0;
2679
2680 SL_ENTER(_("sh_files_fullpath"));
2681
2682 if (testdir != NULL)
2683 {
2684 if ( (llen = sl_strlen(testdir)) > (PATH_MAX-2) )
2685 SL_RETURN((-1),_("sh_files_fullpath"));
2686 sl_strlcpy(statpath, testdir, PATH_MAX - 1);
2687 }
2688 if (d_name != NULL)
2689 {
2690 if (llen > 1 || statpath[0] != '/')
2691 sl_strlcat(statpath, "/", PATH_MAX);
2692 if ((sl_strlen(d_name) + sl_strlen(statpath)) >= PATH_MAX)
2693 SL_RETURN((-1),_("sh_files_fullpath"));
2694 sl_strlcat(statpath, d_name, PATH_MAX);
2695 }
2696 if (statpath == NULL)
2697 SL_RETURN((-1),_("sh_files_fullpath"));
2698 SL_RETURN((0),_("sh_files_fullpath"));
2699}
2700
2701/* -----------------------------------
[367]2702 * Routines required for inotify
2703 * -----------------------------------
2704 */
[373]2705int sh_files_search_dir(char * name, int * class,
2706 unsigned long *check_mask, int *reported,
2707 int * rdepth)
2708{
[383]2709 volatile int retval = 0;
[373]2710#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
2711 sh_globstack_t * testPattern;
2712 zAVLCursor cursor;
2713#endif
2714 dirstack_t * item;
2715
2716 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
2717 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
2718
2719 item = zAVLSearch(zdirListOne, name);
2720
2721 if (item)
2722 {
2723 *check_mask = item->check_mask;
2724 *class = item->class;
2725 *reported = item->is_reported;
2726 *rdepth = item->rdepth;
2727 item->checked = S_FALSE;
2728 item->childs_checked = S_FALSE;
2729 item->is_reported = S_FALSE;
2730 retval = 1;
2731 goto out;
2732 }
2733
2734 item = zAVLSearch(zdirListTwo, name);
2735
2736 if (item)
2737 {
2738 *check_mask = item->check_mask;
2739 *class = item->class;
2740 *reported = item->is_reported;
2741 *rdepth = item->rdepth;
2742 item->checked = S_FALSE;
2743 item->childs_checked = S_FALSE;
2744 item->is_reported = S_FALSE;
2745 retval = 1;
2746 goto out;
2747 }
2748
2749#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
2750 SH_MUTEX_LOCK(mutex_zglob);
2751 for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList);
2752 testPattern;
2753 testPattern = (sh_globstack_t *) zAVLNext (&cursor))
2754 {
2755 if (testPattern->type == SH_LIST_DIR1 ||
2756 testPattern->type == SH_LIST_DIR2)
2757 {
2758 if (0 == fnmatch(testPattern->name, name, FNM_PATHNAME|FNM_PERIOD))
2759 {
2760 *check_mask = testPattern->check_mask;
2761 *class = testPattern->class;
2762 *rdepth = testPattern->rdepth;
2763 retval = 1;
2764 break;
2765 }
2766
2767 }
2768 }
2769 SH_MUTEX_UNLOCK(mutex_zglob);
2770#endif
2771 out:
[377]2772 ; /* 'label at end of compound statement' */
[373]2773 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
2774 return retval;
2775}
2776
[371]2777int sh_files_search_file(char * name, int * class,
2778 unsigned long *check_mask, int *reported)
[367]2779{
[383]2780 volatile int retval = 0;
[371]2781#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
2782 sh_globstack_t * testPattern;
2783 zAVLCursor cursor;
2784#endif
2785 dirstack_t * item;
[367]2786
[371]2787 SH_MUTEX_LOCK(mutex_zfiles);
2788 item = zAVLSearch(zfileList, name);
2789
[367]2790 if (item)
2791 {
2792 *check_mask = item->check_mask;
2793 *class = item->class;
2794 *reported = item->is_reported;
[371]2795 retval = 1;
[367]2796 }
[397]2797 SH_MUTEX_UNLOCK(mutex_zfiles);
[371]2798
2799#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
[397]2800 if (retval == 0)
[371]2801 {
[397]2802 SH_MUTEX_LOCK(mutex_zglob);
2803 for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList);
2804 testPattern;
2805 testPattern = (sh_globstack_t *) zAVLNext (&cursor))
[371]2806 {
[397]2807 if (testPattern->type == SH_LIST_FILE)
[371]2808 {
[397]2809 if (0 == fnmatch(testPattern->name, name,
2810 FNM_PATHNAME|FNM_PERIOD))
2811 {
2812 *check_mask = testPattern->check_mask;
2813 *class = testPattern->class;
2814 retval = 1;
2815 break;
2816 }
2817
[371]2818 }
2819 }
[397]2820 SH_MUTEX_UNLOCK(mutex_zglob);
[371]2821 }
2822#endif
[397]2823
[371]2824 return retval;
[367]2825}
2826
[373]2827void sh_files_set_file_reported(const char * name)
[367]2828{
[371]2829 dirstack_t * item;
[367]2830
[371]2831 SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
2832 item = zAVLSearch(zfileList, name);
2833
[367]2834 if (item)
2835 {
2836 if (sh.flag.reportonce == S_TRUE)
2837 SET_SH_FFLAG_REPORTED(item->is_reported);
2838 }
[371]2839 SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
[367]2840 return;
2841}
2842
[373]2843void sh_files_clear_file_reported(const char * name)
[367]2844{
[371]2845 dirstack_t * item;
[367]2846
[371]2847 SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
2848 item = zAVLSearch(zfileList, name);
2849
[367]2850 if (item)
2851 {
2852 CLEAR_SH_FFLAG_REPORTED(item->is_reported);
2853 }
[371]2854 SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
[367]2855 return;
2856}
2857
2858/* -----------------------------------
[1]2859 *
2860 * The following two routines serve to
2861 * verify that the user has selected
2862 * a proper setup for file policies.
2863 *
2864 * -----------------------------------
2865 */
2866static int check_file(char * name)
2867{
2868 dirstack_t * pfilL;
2869 zAVLCursor cursor;
[371]2870 volatile int retval = -1;
[1]2871
2872 SL_ENTER(_("check_file"));
2873
2874 if (SH_FILE_DIRECTORY == sh_unix_get_ftype(name))
2875 SL_RETURN(0, _("check_file"));
2876
2877 for (pfilL = (dirstack_t *) zAVLFirst (&cursor, zfileList); pfilL;
2878 pfilL = (dirstack_t *) zAVLNext (&cursor))
2879 {
2880 if (0 == strcmp(name, pfilL->name) &&
2881 (pfilL->check_mask & MODI_ATM) == 0 &&
2882 (pfilL->check_mask & MODI_CTM) == 0 &&
2883 (pfilL->check_mask & MODI_MTM) == 0)
[371]2884 {
2885 retval = 0;
2886 break;
2887 }
[1]2888 }
[371]2889
2890 SL_RETURN(retval, _("check_file"));
[1]2891}
[371]2892
2893static void * sh_dummy_pdirL;
2894
[1]2895int sh_files_test_setup_int (zAVLTree * tree)
2896{
2897 int dlen, flen;
2898 zAVLCursor cursor1;
2899 zAVLCursor cursor2;
2900
2901 dirstack_t * pdirL;
2902 dirstack_t * pfilL;
2903
2904 SL_ENTER(_("sh_files_test_setup"));
2905
[371]2906 sh_dummy_pdirL = (void *) &pdirL;
2907
[1]2908 for (pdirL = (dirstack_t *) zAVLFirst (&cursor1, tree); pdirL;
2909 pdirL = (dirstack_t *) zAVLNext (&cursor1))
2910 {
2911 dlen = strlen(pdirL->name);
2912
[371]2913 SH_MUTEX_LOCK(mutex_zfiles);
[1]2914 for (pfilL = (dirstack_t *) zAVLFirst (&cursor2, zfileList); pfilL;
2915 pfilL = (dirstack_t *) zAVLNext (&cursor2))
2916 {
2917 flen = strlen(pfilL->name);
2918
2919 /* check whether file is in tree of dir
2920 */
2921 if ((pfilL->class == SH_LEVEL_READONLY) ||
2922 (pfilL->class == SH_LEVEL_NOIGNORE))
2923 {
2924 ; /* do nothing */
2925 }
2926 else
2927 {
2928 if ((flen > (dlen+1)) &&
2929 (pfilL->name[dlen] == '/') &&
2930 (NULL == strchr(&(pfilL->name[dlen+1]), '/')) && /*30-5-01*/
2931 (0 == strncmp(pfilL->name, pdirL->name, dlen)))
2932 {
2933 if ((pdirL->check_mask & MODI_ATM) != 0 ||
2934 (pdirL->check_mask & MODI_MTM) != 0 ||
2935 (pdirL->check_mask & MODI_CTM) != 0)
2936 {
2937 if (check_file (pdirL->name) != 0)
2938 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_COLL,
2939 pdirL->name, pfilL->name);
2940 }
2941 }
2942 }
2943 }
[371]2944 SH_MUTEX_UNLOCK(mutex_zfiles);
[1]2945 }
2946
2947 SL_RETURN((0), _("sh_files_test_setup"));
2948}
2949
2950int sh_files_test_double (zAVLTree * firstList, zAVLTree * secondList)
2951{
2952 int retval = 0;
2953 zAVLCursor cursor;
2954 dirstack_t * first;
2955
2956 for (first = (dirstack_t *) zAVLFirst (&cursor, firstList); first;
2957 first = (dirstack_t *) zAVLNext (&cursor))
2958 {
2959
2960 if (NULL != zAVLSearch(secondList, first->name))
2961 {
2962 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
2963 first->name);
2964 retval = 1;
2965 }
2966 }
2967 return retval;
2968}
2969
[170]2970extern void aud_exit (const char * file, int line, int fd);
[1]2971
2972int sh_files_test_setup ()
2973{
[373]2974 int retval;
[1]2975
[373]2976 SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
2977 SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
[1]2978 /* Test for modifications allowed in ReadOnly directory
2979 */
2980 sh_files_test_setup_int (zdirListOne);
2981 sh_files_test_setup_int (zdirListTwo);
2982
2983 /* Test for files/dirz defined twice
2984 */
2985 retval = sh_files_test_double (zdirListOne, zdirListTwo);
2986 if (retval != 0)
2987 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2988
2989 retval = sh_files_test_double (zdirListTwo, zdirListOne);
2990 if (retval != 0)
2991 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
[373]2992 SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
[1]2993
2994 return 0;
2995}
2996
2997#endif
[286]2998
2999#ifdef SH_CUTEST
3000#include "CuTest.h"
3001
3002void Test_file_dequote (CuTest *tc)
3003{
3004#if (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE))
3005
3006 char str1[] = "1234567890";
3007 char str1a[] = "123456\\\"789\\r";
3008 char str1b[] = "12345678\\r9";
3009 char str1c[] = "12345678\\x0a_9";
3010 char str1d[] = "12345678\\007_9";
3011 char str1e[] = "123456789\\\\";
3012
3013 char str2[] = "1234567890\\xw";
3014 char str3[] = "1234567890\\xw99";
3015 char str4[] = "1234567890\\0ww";
3016 char str5[] = "12345\\g67890";
3017 char str6[] = "1234567890\\009a";
3018
3019 char *s, *p, *q;
3020 size_t lo, lr;
3021
3022 s = SH_ALLOC(64); sl_strlcpy(s, str1, 64); p = s; lo = strlen(s); lr = lo;
3023 q = sh_files_C_dequote(s, &lr);
3024 CuAssertPtrNotNull(tc, q);
3025 CuAssertTrue(tc, p == q);
3026 CuAssertTrue(tc, lr == lo);
3027
3028 s = SH_ALLOC(64); sl_strlcpy(s, str1a, 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, 0 == strcmp(q, "123456\"789\r"));
3033 CuAssertTrue(tc, lr == (lo-2));
3034
3035 s = SH_ALLOC(64); sl_strlcpy(s, str1b, 64); p = s; lo = strlen(s); lr = lo;
3036 q = sh_files_C_dequote(s, &lr);
3037 CuAssertPtrNotNull(tc, q);
3038 CuAssertTrue(tc, p != q);
3039 CuAssertTrue(tc, 0 == strcmp(q, "12345678\r9"));
3040 CuAssertTrue(tc, lr == (lo-1));
3041
3042 s = SH_ALLOC(64); sl_strlcpy(s, str1c, 64); p = s; lo = strlen(s); lr = lo;
3043 q = sh_files_C_dequote(s, &lr);
3044 CuAssertPtrNotNull(tc, q);
3045 CuAssertTrue(tc, p != q);
3046 CuAssertTrue(tc, 0 == strcmp(q, "12345678\x0a_9"));
3047 CuAssertTrue(tc, lr == (lo-3));
3048
3049 s = SH_ALLOC(64); sl_strlcpy(s, str1d, 64); p = s; lo = strlen(s); lr = lo;
3050 q = sh_files_C_dequote(s, &lr);
3051 CuAssertPtrNotNull(tc, q);
3052 CuAssertTrue(tc, p != q);
3053 CuAssertTrue(tc, 0 == strcmp(q, "12345678\007_9"));
3054 CuAssertTrue(tc, lr == (lo-3));
3055
3056 s = SH_ALLOC(64); sl_strlcpy(s, str1e, 64); p = s; lo = strlen(s); lr = lo;
3057 q = sh_files_C_dequote(s, &lr);
3058 CuAssertPtrNotNull(tc, q);
3059 CuAssertTrue(tc, p != q);
3060 CuAssertTrue(tc, 0 == strcmp(q, "123456789\\"));
3061 CuAssertTrue(tc, lr == (lo-1));
3062
3063 s = SH_ALLOC(64); sl_strlcpy(s, str2, 64); p = s; lo = strlen(s); lr = lo;
3064 q = sh_files_C_dequote(s, &lr);
3065 CuAssertTrue(tc, q == NULL);
3066 CuAssertTrue(tc, lr == 0);
3067
3068 s = SH_ALLOC(64); sl_strlcpy(s, str3, 64); p = s; lo = strlen(s); lr = lo;
3069 q = sh_files_C_dequote(s, &lr);
3070 CuAssertTrue(tc, q == NULL);
3071 CuAssertTrue(tc, lr == 0);
3072
3073 s = SH_ALLOC(64); sl_strlcpy(s, str4, 64); p = s; lo = strlen(s); lr = lo;
3074 q = sh_files_C_dequote(s, &lr);
3075 CuAssertTrue(tc, q == NULL);
3076 CuAssertTrue(tc, lr == 0);
3077
3078 s = SH_ALLOC(64); sl_strlcpy(s, str5, 64); p = s; lo = strlen(s); lr = lo;
3079 q = sh_files_C_dequote(s, &lr);
3080 CuAssertTrue(tc, q == NULL);
3081 CuAssertTrue(tc, lr == 0);
3082
3083 s = SH_ALLOC(64); sl_strlcpy(s, str6, 64); p = s; lo = strlen(s); lr = lo;
3084 q = sh_files_C_dequote(s, &lr);
3085 CuAssertTrue(tc, q == NULL);
3086 CuAssertTrue(tc, lr == 0);
3087
3088 return;
3089#else
3090 (void) tc; /* fix compiler warning */
3091 return;
3092#endif
3093}
3094#endif
3095
Note: See TracBrowser for help on using the repository browser.