source: trunk/src/sh_dbIO.c@ 587

Last change on this file since 587 was 587, checked in by katerina, 21 hours ago

Fix for ticket #475 (compiler warnings about unused variables).

File size: 44.2 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 2015 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#include <stdio.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
27
28#include "samhain.h"
29#include "sh_utils.h"
30#include "sh_dbIO_int.h"
31#include "sh_hash.h"
32#include "sh_dbIO.h"
33#include "sh_sig.h"
34#include "sh_tiger.h"
35#include "sh_xfer.h"
36#include "sh_pthread.h"
37#include "sh_socket.h"
38#include "sh_files.h"
39
40#undef FIL__
41#define FIL__ _("sh_dbIO.c")
42
43#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
44
45/* external prototypes */
46
47extern int get_the_fd (SL_TICKET ticket);
48
49SH_MUTEX_EXTERN(mutex_hash);
50
51/******************************************************************
52 *
53 * Get a single line
54 *
55 ******************************************************************/
56static FILE * sh_fin_fd = NULL;
57
58int sh_dbIO_getline (FILE * fd, char * line, const size_t sizeofline)
59{
60 size_t n = 0;
61
62 SL_REQUIRE(sizeofline >= SH_MINIBUF, _("sizeofline >= SH_MINIBUF"));
63
64 if (NULL != fgets(line, sizeofline, fd))
65 {
66 n = strlen(line);
67 if (n > 0 && line[n-1] == '\n') {
68 n--; line[n] = '\0';
69 }
70 }
71 else {
72 line[0] = '\0';
73 return -1;
74 }
75
76 return n;
77}
78
79/******************************************************************
80 *
81 * Fast forward to start of data
82 *
83 ******************************************************************/
84
85static void reopen_fin_fd(SL_TICKET fd)
86{
87 if (sh_fin_fd != NULL)
88 {
89 sl_fclose (FIL__, __LINE__, sh_fin_fd);
90 sh_fin_fd = NULL;
91 }
92
93 sh_fin_fd = fdopen(dup(get_the_fd(fd)), "rb");
94 return;
95}
96
97
98static int seek_sof(FILE * fd, char * line, int size, const char * file)
99{
100 long i;
101
102 while (1)
103 {
104 i = sh_dbIO_getline (fd, line, size);
105 if (i < 0 )
106 {
107 SH_FREE(line);
108 dlog(1, FIL__, __LINE__,
109 _("The file signature database: %s does not\ncontain any data, or the start-of-file marker is missing (unlikely,\nunless modified by hand).\n"),
110 (NULL == file) ? _("(null)") : file);
111
112 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_P_NODATA,
113 ( (NULL == file) ? _("(null)") : file)
114 );
115 return -1;
116 }
117
118#if defined(SH_STEALTH)
119 if (0 == sl_strncmp (line, N_("[SOF]"), 5))
120#else
121 if (0 == sl_strncmp (line, _("[SOF]"), 5))
122#endif
123 break;
124 }
125 fflush(fd);
126 return 0;
127}
128
129static int sh_dbIO_setdataent (SL_TICKET fd, char * line, int size,
130 const char * file)
131{
132 int retval;
133
134 SL_ENTER(_("sh_dbIO_setdataent"));
135
136 sl_rewind (fd);
137 reopen_fin_fd(fd);
138
139 if (!sh_fin_fd)
140 {
141 dlog(1, FIL__, __LINE__,
142 _("The file signature database: %s is not readable.\n"),
143 (NULL == file) ? _("(null)") : file);
144 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_P_NODATA,
145 ( (NULL == file) ? _("(null)") : file)
146 );
147 SL_RETURN( -1, _("sh_dbIO_setdataent"));
148 }
149
150 retval = seek_sof(sh_fin_fd, line, size, file);
151 SL_RETURN( retval, _("sh_dbIO_setdataent"));
152}
153
154/* Seek to [SOF] and truncate remainder of file
155 */
156static int sh_dbIO_setdataent_old (SL_TICKET fd, char * line, int size,
157 const char * file)
158{
159 FILE * fdp;
160
161 SL_ENTER(_("sh_dbIO_setdataent_old"));
162
163 sl_rewind (fd);
164 fdp = sl_stream(fd, "r+");
165 if (0 != seek_sof(fdp, line, size, file))
166 SL_RETURN( SL_EREAD, _("sh_dbIO_setdataent_old"));
167
168 lseek(fileno(fdp), ftello(fdp), SEEK_SET);
169
170 if (0 != ftruncate(fileno(fdp), ftello(fdp)))
171 {
172 char ebuf[SH_ERRBUF_SIZE];
173 int errnum = errno;
174 sh_error_message(errnum, ebuf, sizeof(ebuf));
175 sh_error_handle ((-1), FIL__, __LINE__, errnum, MSG_E_SUBGEN,
176 ebuf, _("sh_dbIO_setdataent_old") );
177 SL_RETURN( SL_EWRITE, _("sh_dbIO_setdataent_old"));
178 }
179 SL_RETURN( 0, _("sh_dbIO_setdataent_old"));
180}
181
182/******************************************************************
183 *
184 * IO helper functions
185 *
186 ******************************************************************/
187
188
189static UINT32 * swap_32 (UINT32 * iptr)
190{
191#ifdef WORDS_BIGENDIAN
192 unsigned char swap;
193 unsigned char * ii = (unsigned char *) iptr;
194 swap = ii[0]; ii[0] = ii[3]; ii[3] = swap;
195 swap = ii[1]; ii[1] = ii[2]; ii[2] = swap;
196 return iptr;
197#else
198 return iptr;
199#endif
200}
201
202static UINT64 * swap_64 (UINT64 * iptr)
203{
204#ifdef WORDS_BIGENDIAN
205#ifdef UINT64_IS_32
206 swap_32 ((UINT32*) iptr);
207#else
208 unsigned char swap;
209 unsigned char * ii = (unsigned char *) iptr;
210 swap = ii[0]; ii[0] = ii[7]; ii[7] = swap;
211 swap = ii[1]; ii[1] = ii[6]; ii[6] = swap;
212 swap = ii[2]; ii[2] = ii[5]; ii[5] = swap;
213 swap = ii[3]; ii[3] = ii[4]; ii[4] = swap;
214#endif
215 return iptr;
216#else
217 return iptr;
218#endif
219}
220
221static unsigned short * swap_short (unsigned short * iptr)
222{
223#ifdef WORDS_BIGENDIAN
224 if (sizeof(short) == 4)
225 swap_32 ((UINT32*) iptr);
226 else
227 {
228 /* alignment problem */
229 static unsigned short ooop = *iptr;
230 unsigned short hi = (ooop & 0xff00);
231 unsigned short lo = (ooop & 0xff);
232 ooop = (lo << 8) | (hi >> 8);
233 return &ooop;
234 }
235 return iptr;
236#else
237 return iptr;
238#endif
239}
240
241static void swap_data(sh_filestore_t * ft)
242{
243 swap_32(&(ft->mode));
244 swap_32(&(ft->linkmode));
245 swap_64(&(ft->dev));
246 swap_64(&(ft->rdev));
247 swap_32(&(ft->hardlinks));
248 swap_32(&(ft->ino));
249 swap_64(&(ft->size));
250 swap_64(&(ft->atime));
251 swap_64(&(ft->mtime));
252 swap_64(&(ft->ctime));
253 swap_32(&(ft->owner));
254 swap_32(&(ft->group));
255 swap_32(&(ft->checkflags));
256#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
257 swap_32(&(ft->attributes));
258#endif
259 ft->mark = *(swap_short(&(ft->mark)));
260 return;
261}
262
263#define QUOTE_CHAR '='
264
265char * unquote_string (const char * str, size_t len)
266{
267 int i = 0, t1, t2;
268 char * tmp = NULL;
269 size_t l2, j, k = 0;
270
271 SL_ENTER(_("unquote_string"));
272
273 if (str != NULL)
274 {
275 l2 = len - 2;
276 tmp = SH_ALLOC(len + 1);
277
278 for (j = 0; j <= len; ++j)
279 {
280 if (str[j] != QUOTE_CHAR)
281 {
282 tmp[k] = str[j];
283 }
284 else if (str[j] == QUOTE_CHAR && j < l2)
285 {
286 t1 = sh_util_hexchar(str[j+1]);
287 t2 = sh_util_hexchar(str[j+2]);
288 if ((t1|t2) >= 0)
289 {
290 i = 16 * t1 + t2;
291 tmp[k] = i;
292 j += 2;
293 }
294 else
295 {
296 tmp[k] = str[j];
297 }
298 }
299 else
300 tmp[k] = str[j];
301 ++k;
302 }
303 }
304 SL_RETURN(tmp, _("unquote_string"));
305}
306
307static char * int2hex (unsigned char i, char * i2h)
308{
309 static char hexchars[] = "0123456789ABCDEF";
310
311 i2h[0] = hexchars[(((i) & 0xF0) >> 4)]; /* high */
312 i2h[1] = hexchars[((i) & 0x0F)]; /* low */
313
314 return i2h;
315}
316
317char * quote_string (const char * str, size_t len)
318{
319 char * tmp;
320 char * tmp2;
321 size_t l2, j, i = 0, k = 0;
322 char i2h[2];
323
324 SL_ENTER(_("quote_string"));
325
326 if (str == NULL)
327 {
328 SL_RETURN(NULL, _("quote_string"));
329 }
330
331 for (j = 0; j < len; ++j)
332 if (str[j] == '\n' || str[j] == QUOTE_CHAR) ++i;
333
334 l2 = len + 1;
335 if (sl_ok_muls(3, i) && sl_ok_adds(l2, (3*i)))
336 {
337 tmp = SH_ALLOC(len + 1 + 3*i);
338 }
339 else
340 {
341 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
342 _("integer overflow"),
343 _("quote_string"));
344 SL_RETURN(NULL, _("quote_string"));
345 }
346
347 for (j = 0; j <= len; ++j)
348 {
349 if (str[j] == '\n')
350 {
351 tmp2 = int2hex((unsigned char) '\n', i2h);
352 tmp[k] = QUOTE_CHAR; ++k;
353 tmp[k] = tmp2[0]; ++k;
354 tmp[k] = tmp2[1];
355 }
356 else if (str[j] == QUOTE_CHAR)
357 {
358 tmp2 = int2hex((unsigned char) QUOTE_CHAR, i2h);
359 tmp[k] = QUOTE_CHAR; ++k;
360 tmp[k] = tmp2[0]; ++k;
361 tmp[k] = tmp2[1];
362 }
363 else
364 {
365 tmp[k] = str[j];
366 }
367 ++k;
368 }
369 SL_RETURN(tmp, _("quote_string"));
370}
371
372static char * unquote_path(char * line, long i)
373{
374 char * tmp = unquote_string (line, i);
375 size_t len = sl_strlen(tmp)+1;
376 char * path = SH_ALLOC(len);
377
378 (void) sl_strlcpy (path, tmp, len);
379 if (tmp)
380 SH_FREE(tmp);
381 /* do not strip newline twice...
382 if (len > 1) {
383 if (path[len-2] == '\n')
384 path[len-2] = '\0';
385 }
386 ****/
387 return path;
388}
389
390/******************************************************************
391 *
392 * Use different init rootfs (patch by Kemal H.)
393 *
394 ******************************************************************/
395
396static char * sh_dbIO_rootfs = NULL;
397static size_t sh_dbIO_rootfs_len = 0;
398
399int sh_dbIO_init_rootfs (const char * rootfs)
400{
401 if (NULL == sh_dbIO_rootfs)
402 {
403 sh_dbIO_rootfs = sh_util_strdup (rootfs);
404 sh_dbIO_rootfs_len = sl_strlen(sh_dbIO_rootfs);
405 return 0;
406 }
407 return -1;
408}
409
410size_t sh_dbIO_get_rootfs_len()
411{
412 return sh_dbIO_rootfs_len;
413}
414
415/* Prepend rootfs when reading from config file ('path' must be allocated with sufficient space).
416 */
417char * sh_dbIO_rootfs_prepend(char * path)
418{
419 if (0 == sh_dbIO_rootfs_len)
420 return path;
421
422 memmove (path + sh_dbIO_rootfs_len, path, sl_strlen(path) + 1);
423 memcpy (path, sh_dbIO_rootfs, sh_dbIO_rootfs_len);
424
425 return path;
426}
427
428
429/* Strip rootfs when writing to database file.
430 */
431char * sh_dbIO_rootfs_strip(char * path)
432{
433 if (sh_dbIO_rootfs_len == 0)
434 {
435 return path;
436 }
437 else
438 {
439 size_t len = sl_strlen(path);
440
441 memmove (path, path + sh_dbIO_rootfs_len, len + 1 - sh_dbIO_rootfs_len);
442 if(path[0] != '/')
443 {
444 path[0]='/';
445 path[1]='\0';
446 }
447 }
448
449 return path;
450}
451
452char * sh_dbIO_rootfs_strip_link(char * path)
453{
454 if (sh_dbIO_rootfs_len == 0)
455 return path;
456 if (strstr(path, sh_dbIO_rootfs) == path)
457 {
458 size_t len = sl_strlen(path);
459
460 memmove (path, path + sh_dbIO_rootfs_len, len + 1 - sh_dbIO_rootfs_len);
461 }
462 return path;
463}
464
465/******************************************************************
466 *
467 * Read next record and return it
468 *
469 ******************************************************************/
470
471static void corrupt_record(char * file, int line, const char * filepath)
472{
473 dlog(1, file, line,
474 _("There is a corrupt record in the file signature database: %s\n"),
475 (NULL == filepath)? _("(null)") : filepath);
476 sh_error_handle ((-1), file, line, 0, MSG_E_SUBGPATH,
477 _("Corrupt record in file signature database"),
478 _("sh_dbIO_getdataent"),
479 ( (NULL == filepath) ? _("(null)") : filepath) );
480 return;
481}
482
483static void wrong_version(char * file, int line, const char * filepath)
484{
485 dlog(1, file, line,
486 _("There is a record with a bad version number in the file signature database: %s\nThis may be caused by using '-t init' repeatedly to initialise the database, without (re)moving the database file.\n"),
487 (NULL == filepath) ? _("(null)") : filepath);
488 sh_error_handle((-1), file, line, 0, MSG_E_SUBGPATH,
489 _("Record with bad version number in file signature database"),
490 _("sh_dbIO_getdataent"),
491 (NULL == filepath) ? _("(null)") : filepath);
492 return;
493}
494
495static void hexdump(unsigned char * data, size_t size)
496{
497 unsigned int count =0;
498 char ith[3];
499
500 do {
501 int2hex(data[count], ith); ith[2] = '\0';
502 printf("%2s", ith);
503 ++count;
504 if (count % 40 == 0) putc('\n', stdout);
505 } while (count < size);
506}
507
508static size_t dbIO_fread_struct (sh_filestore_t * ptr, FILE *stream,
509 const char * path, int * errflag)
510{
511 sh_filestore_old_t old_struct;
512 fpos_t position;
513 static int oldflag = -1;
514
515 start:
516 if (oldflag != -1) /* 'initialized' case first */
517 {
518 if (oldflag == 0)
519 return fread (ptr, sizeof(sh_filestore_t), 1, stream);
520
521 else
522 {
523 unsigned short mark;
524 if (1 != fread (&old_struct, sizeof(old_struct), 1, stream))
525 return 0;
526
527 /* set mark to current version */
528 mark = old_struct.mark;
529 mark = *(swap_short(&(mark)));
530 if ((mark & ~REC_FLAGS_MASK) != OLD_REC_MAGIC)
531 {
532 sh_filestore_old_t try_struct;
533 char try[5];
534
535 if (1 == 0)
536 hexdump((unsigned char *)&old_struct, sizeof(old_struct));
537 memset(&try_struct, 0, sizeof(try_struct));
538 if (!memcmp(&old_struct, &try_struct, sizeof(try_struct)))
539 return 0; /* NULL read */
540 if (1 != fread (try, sizeof(try), 1, stream))
541 return 0;
542 if (feof(stream))
543 return 0;
544
545 wrong_version(FIL__, __LINE__, path);
546 *errflag = -1;
547 return 0;
548 }
549 if ((mark & REC_FLAGS_ATTR) != 0)
550 mark = REC_MAGIC|REC_FLAGS_ATTR;
551 else
552 mark = REC_MAGIC;
553 mark = *(swap_short(&(mark)));
554 old_struct.mark = mark;
555
556 /* copy into current struct version */
557 memcpy(ptr, &old_struct, sizeof(old_struct));
558 ptr->checkflags = 0;
559 return 1;
560 }
561 }
562 else /* not initialized yet, test DB version */
563 {
564 if (0 == fgetpos(stream, &position))
565 {
566 unsigned short mark;
567
568 if (1 != fread (&old_struct, sizeof(old_struct), 1, stream))
569 return 0;
570
571 mark = old_struct.mark;
572 mark = *(swap_short(&(mark)));
573 if ((mark & ~REC_FLAGS_MASK) == REC_MAGIC)
574 oldflag = 0;
575 else if ((mark & ~REC_FLAGS_MASK) == OLD_REC_MAGIC)
576 oldflag = 1;
577 else
578 {
579 wrong_version(FIL__, __LINE__, path);
580 *errflag = -1;
581 return 0;
582 }
583
584 /* return to previous position and read data */
585 if (0 != fsetpos(stream, &position))
586 return 0;
587 goto start;
588 }
589 return 0;
590 }
591}
592
593int sig_end_detected (void * ft)
594{
595 char * str = (char *) ft;
596 char cmp[SH_MINIBUF];
597
598 sl_strlcpy(cmp, _("-----BEGIN PGP SIGNATURE-----"), sizeof(cmp));
599
600 if ( 0 == memcmp(str, cmp, strlen(cmp)) )
601 return S_TRUE;
602 return S_FALSE;
603}
604
605static sh_file_t * sh_dbIO_getdataent (char * line, int size,
606 const char * filepath, int * errflag)
607{
608 sh_file_t * p;
609 sh_filestore_t ft;
610 long i;
611 char * fullpath;
612 char * linkpath;
613 char * attr_string = NULL;
614
615 SL_ENTER(_("sh_dbIO_getdataent"));
616
617 *errflag = 0;
618
619 p = SH_ALLOC(sizeof(sh_file_t));
620
621 /* Read next record -- Part One
622 */
623 if (1 != dbIO_fread_struct (&ft, sh_fin_fd, filepath, errflag))
624 {
625 SH_FREE(p);
626 SL_RETURN( NULL, _("sh_dbIO_getdataent"));
627 }
628
629 ft.mark = *(swap_short(&(ft.mark)));
630
631 if ((ft.mark & ~REC_FLAGS_MASK) != REC_MAGIC)
632 {
633 if (sig_end_detected(&ft))
634 {
635 SH_FREE(p);
636 SL_RETURN( NULL, _("sh_dbIO_getdataent"));
637 }
638 SH_FREE(p);
639 wrong_version(FIL__, __LINE__, filepath);
640 *errflag = -1;
641 SL_RETURN( NULL, _("sh_dbIO_getdataent"));
642 }
643
644 ft.mark = *(swap_short(&(ft.mark)));
645 swap_data(&ft);
646
647 /* Read next record -- Part Two -- Fullpath
648 */
649 i = sh_dbIO_getline (sh_fin_fd, line, size);
650
651 if (i <= 0 )
652 {
653 SH_FREE(p);
654 corrupt_record(FIL__, __LINE__, filepath);
655 *errflag = -1;
656 SL_RETURN( NULL, _("sh_dbIO_getdataent"));
657 }
658
659 fullpath = unquote_path(line, i);
660
661 /* Read next record -- Part Three -- Linkpath
662 */
663 i = sh_dbIO_getline (sh_fin_fd, line, size);
664
665 if (i <= 0 )
666 {
667 SH_FREE(fullpath); SH_FREE(p);
668 corrupt_record(FIL__, __LINE__, filepath);
669 *errflag = -1;
670 SL_RETURN( NULL, _("sh_dbIO_getdataent"));
671 }
672
673 linkpath = unquote_path(line, i);
674
675 /* Read next record -- Part Four -- attr_string
676 */
677 if ((ft.mark & REC_FLAGS_ATTR) != 0)
678 {
679 i = sh_dbIO_getline (sh_fin_fd, line, size);
680 if (i <= 0 )
681 {
682 SH_FREE(fullpath); SH_FREE(linkpath); SH_FREE(p);
683 corrupt_record(FIL__, __LINE__, filepath);
684 *errflag = -1;
685 SL_RETURN( NULL, _("sh_dbIO_getdataent"));
686 }
687
688 attr_string = unquote_path(line, i);
689 }
690
691 /* Read next record -- Part Four -- Decode
692 */
693#if defined(SH_STEALTH)
694 sh_do_decode(fullpath, sl_strlen(fullpath));
695#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
696 sh_do_decode(ft.c_attributes, sl_strlen(ft.c_attributes));
697#endif
698 sh_do_decode(ft.c_mode, sl_strlen(ft.c_mode));
699 sh_do_decode(ft.c_owner, sl_strlen(ft.c_owner));
700 sh_do_decode(ft.c_group, sl_strlen(ft.c_group));
701 sh_do_decode(ft.checksum, sl_strlen(ft.checksum));
702 /*
703 * TXT entries are c_mode[0] != 'l' and do not get decoded
704 */
705 if (ft.c_mode[0] == 'l' && linkpath[0] != '-')
706 {
707 sh_do_decode(linkpath, sl_strlen(linkpath));
708 }
709 if ((ft.mark & REC_FLAGS_ATTR) != 0)
710 {
711 sh_do_decode(attr_string, sl_strlen(attr_string));
712 }
713#endif
714
715 memcpy( &(*p).theFile, &ft, sizeof(sh_filestore_t) );
716
717 /* init fflags, such that suid files in
718 * database are recognized as such
719 */
720 {
721 mode_t mode = (mode_t) ft.mode;
722
723 if (S_ISREG(mode) &&
724 (0 !=(S_ISUID & mode) ||
725#if defined(HOST_IS_LINUX)
726 (0 !=(S_ISGID & mode) &&
727 0 !=(S_IXGRP & mode))
728#else
729 0 !=(S_ISGID & mode)
730#endif
731 )
732 )
733 p->fflags = SH_FFLAG_SUIDCHK;
734
735 else
736 p->fflags = 0;
737 }
738
739 p->modi_mask = ft.checkflags;
740 if (MODI_ISSET(ft.checkflags, MODI_ALLIGNORE))
741 SET_SH_FFLAG_ALLIGNORE(p->fflags);
742 p->fullpath = fullpath;
743 p->linkpath = linkpath;
744 p->attr_string = attr_string;
745
746 /* set to an invalid value
747 */
748 ft.mark = (REC_MAGIC + 5);
749
750 SL_REQUIRE((*errflag == 0), _("errflag not set correctly"));
751 SL_RETURN( p, _("sh_dbIO_getdataent"));
752}
753
754/******************************************************************
755 *
756 * Data loading routines
757 *
758 ******************************************************************/
759static SL_TICKET load_data_from_server(const char * uuid)
760{
761 SL_TICKET fd = -1;
762
763#if defined(SH_WITH_CLIENT)
764 char hashbuf[KEYBUF_SIZE];
765
766 /* Data file from Server
767 */
768 if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
769 return -1;
770
771 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_D_DSTART);
772 fd = sh_xfer_request_file((!uuid) ? _("DATA") : uuid);
773
774 if (SL_ISERROR(fd))
775 {
776 if (!uuid)
777 {
778 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FBAD);
779 dlog(1, FIL__, __LINE__,
780 _("Could not retrieve the file signature database from the server(errnum = %ld).\nPossible reasons include:\n - the server is not running,\n - session key negotiation failed (see the manual for proper setup), or\n - the server cannot access the file.\n"), fd);
781 }
782 else
783 sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_TCP_FBAD);
784 return fd;
785 }
786 sl_rewind (fd);
787
788 if (!uuid)
789 {
790 sl_strlcpy (sh.data.hash,
791 sh_tiger_hash (file_path('D', 'R'),
792 fd, TIGER_NOLIM, hashbuf, sizeof(hashbuf)),
793 KEY_LEN+1);
794 sl_rewind (fd);
795 }
796#else
797 (void) uuid;
798#endif
799 return fd;
800}
801
802static SL_TICKET load_data_from_disk(const char * filepath)
803{
804 char hashbuf[KEYBUF_SIZE];
805 SL_TICKET fd = -1;
806
807 /* Local data file
808 */
809 if ( SL_ISERROR(fd = sl_open_read(FIL__, __LINE__, filepath, SL_YESPRIV)) )
810 {
811 TPT(( 0, FIL__, __LINE__, _("msg=<Error opening: %s>\n"), filepath));
812 dlog(1, FIL__, __LINE__,
813 _("Could not open the local file signature database for reading because\nof the following error: %s (errnum = %ld)\nIf this is a permission problem, you need to change file permissions\nto make the file readable for the effective UID: %d\n"),
814 sl_get_errmsg(), fd, (int) sl_ret_euid());
815 sh_error_handle ((-1), FIL__, __LINE__, fd, MSG_EXIT_ABORT1,
816 sh.prg_name);
817 return -1;
818 }
819
820 TPT(( 0, FIL__, __LINE__, _("msg=<Opened database: %s>\n"),
821 filepath));
822
823 if (sh.data.hash[0] == '\0')
824 {
825 char hashbuf[KEYBUF_SIZE];
826 sl_strlcpy(sh.data.hash,
827 sh_tiger_hash (filepath, TIGER_FILE, TIGER_NOLIM, hashbuf, sizeof(hashbuf)),
828 KEY_LEN+1);
829 }
830 else
831 {
832 if (0 != sl_strncmp(sh.data.hash,
833 sh_tiger_hash (filepath, fd, TIGER_NOLIM,
834 hashbuf, sizeof(hashbuf)),
835 KEY_LEN)
836 && sh.flag.checkSum != SH_CHECK_INIT)
837 {
838 dlog(1, FIL__, __LINE__,
839 _("The checksum of the file signature database has changed since startup: %s -> %s\n"),
840 sh.data.hash, sh_tiger_hash (filepath, fd, TIGER_NOLIM,
841 hashbuf, sizeof(hashbuf)));
842 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_AUTH,
843 ( (NULL == filepath) ? _("(null)") :
844 filepath )
845 );
846 }
847 }
848 sl_rewind (fd);
849 return fd;
850}
851
852static SL_TICKET verify_data (SL_TICKET fd)
853{
854#if defined(WITH_SIG)
855 SL_TICKET fdTmp;
856
857 /* extract the data and copy to temporary file
858 */
859 fdTmp = sh_sig_extract_signed(fd);
860
861 if (sig_termfast == 1) /* SIGTERM */
862 {
863 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
864 --sig_raised; --sig_urgent;
865 return -1;
866 }
867
868 sl_close(fd);
869 fd = fdTmp;
870
871 /* Validate signature of open file.
872 */
873 if (0 != sh_sig_check_signature (fd, SIG_DATA))
874 {
875 sl_close(fd);
876 return -1;
877 }
878 sl_rewind (fd);
879
880 fdTmp = sh_sig_extract_signed_data(fd);
881 sl_close(fd);
882 fd = fdTmp;
883#endif
884
885 return fd;
886}
887
888static int read_data(SL_TICKET fd, sh_file_t * tab[TABSIZE],
889 const char * filepath)
890{
891 sh_file_t * p;
892 int errflag = 0;
893 char * line = SH_ALLOC(MAX_PATH_STORE+2);
894
895 /* fast forward to start of data
896 */
897 if (0 != sh_dbIO_setdataent(fd, line, MAX_PATH_STORE+1, filepath))
898 return -1;
899
900 while (1)
901 {
902 if (sig_termfast == 1) /* SIGTERM */
903 {
904 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
905 --sig_raised; --sig_urgent;
906 SH_FREE(line);
907 return -1;
908 }
909
910 p = sh_dbIO_getdataent (line, MAX_PATH_STORE+1, filepath, &errflag);
911 if (p != NULL)
912 {
913 if (!sh_hash_is_null_record(&(p->theFile)))
914 hashinsert (tab, p);
915 else
916 sh_hash_remove_unconditional (p->fullpath);
917 }
918 else
919 break;
920 }
921
922 if (line != NULL)
923 SH_FREE(line);
924
925 /* Always keep db in memory, so we have no open file
926 */
927 sl_close (fd);
928
929 sl_fclose (FIL__, __LINE__, sh_fin_fd);
930 sh_fin_fd = NULL;
931
932 return errflag;
933}
934
935
936static int sh_dbIO_load_db_int(sh_file_t * tab[TABSIZE],
937 const char * filepath, const char * uuid)
938{
939#define FGETS_BUF 16384
940
941 SL_TICKET fd = -1;
942
943 if (uuid)
944 {
945 fd = load_data_from_server(uuid);
946 if (SL_ISERROR(fd))
947 return -1;
948 }
949 else if (!filepath)
950 {
951 char * dbpath = file_path('D', 'R');
952
953 fd = load_data_from_server(NULL);
954
955 if (SL_ISERROR(fd))
956 {
957 if (*dbpath == '/')
958 fd = load_data_from_disk(dbpath);
959 }
960 }
961 else
962 {
963 fd = load_data_from_disk(filepath);
964 }
965
966 if (SL_ISERROR(fd))
967 return -1;
968
969 if (sig_termfast == 1) /* SIGTERM */
970 {
971 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
972 --sig_raised; --sig_urgent;
973 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
974 }
975
976 fd = verify_data(fd);
977 if (SL_ISERROR(fd))
978 return -1;
979
980 if (!uuid) { int i; for (i = 0; i < TABSIZE; ++i) tab[i] = NULL; }
981
982 return read_data (fd, tab, filepath);
983}
984
985
986int sh_dbIO_load_db(sh_file_t * tab[TABSIZE])
987{
988 return sh_dbIO_load_db_int(tab, NULL, NULL);
989}
990int sh_dbIO_load_db_file(sh_file_t * tab[TABSIZE], const char * filepath)
991{
992 return sh_dbIO_load_db_int(tab, filepath, NULL);
993}
994
995int sh_dbIO_load_delta()
996{
997 int status = 0;
998#if defined(SH_WITH_CLIENT)
999 sh_file_t ** mtab = get_default_data_table();
1000 int errflag = 0;
1001 unsigned int count;
1002 time_t last;
1003
1004 if ( sh.flag.checkSum != SH_CHECK_INIT )
1005 {
1006 if (sh_hash_get_initialized() != 0)
1007 {
1008 char * uuid = sh_socket_get_uuid(&errflag, &count, &last);
1009
1010 if (!uuid)
1011 return errflag;
1012
1013 if (count > 0)
1014 sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, count, MSG_E_SUBGEN,
1015 _("Retrying download of delta DB"),
1016 _("sh_dbIO_load_delta"));
1017
1018 status = sh_dbIO_load_db_int(mtab, NULL, uuid);
1019 if (status < 0)
1020 {
1021 /* Return status < 0 indicates that max_try is exceeded
1022 */
1023 if (sh_socket_return_uuid(uuid, count, last) < 0)
1024 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_D_DELTAFAIL, uuid);
1025 }
1026 else
1027 {
1028 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_D_DELTAOK, uuid);
1029 }
1030 SH_FREE(uuid);
1031 }
1032 else
1033 {
1034 /* not initialized yet */
1035 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, -1, MSG_E_SUBGEN,
1036 _("Download of delta DB skipped, not initialized yet"),
1037 _("sh_dbIO_load_delta"));
1038 return -1;
1039 }
1040 }
1041#endif
1042 return status;
1043}
1044
1045/******************************************************************
1046 *
1047 * Writing out a file to the database.
1048 *
1049 ******************************************************************/
1050static int pushdata_isfirst = 1;
1051static SL_TICKET pushdata_fd = -1;
1052
1053static int pushdata_stdout = S_FALSE;
1054
1055static char * sh_db_version_string = NULL;
1056
1057int sh_dbIO_writeout_stdout (const char * str)
1058{
1059 if (!str)
1060 { pushdata_stdout = S_TRUE; return 0; }
1061 return -1;
1062}
1063
1064int sh_dbIO_version_string(const char * str)
1065{
1066 if (str)
1067 {
1068 if (sh_db_version_string != NULL) {
1069 SH_FREE(sh_db_version_string);
1070 }
1071 if (0 == sl_strncmp(str, _("NULL"), 4))
1072 {
1073 sh_db_version_string = NULL;
1074 return 0;
1075 }
1076 sh_db_version_string = sh_util_strdup(str);
1077 return 0;
1078 }
1079 return -1;
1080}
1081
1082void do_writeout_checks(const char * outpath)
1083{
1084 if ((pushdata_stdout == S_TRUE) && (sh.flag.update == S_TRUE))
1085 {
1086 dlog(1, FIL__, __LINE__,
1087 _("You cannot write the database to stdout when you use update rather than init.\n"));
1088 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORTS,
1089 _("Writing database to stdout with update"),
1090 sh.prg_name,
1091 _("sh_dbIO_data_write_int"));
1092 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1093 }
1094
1095 if ((pushdata_stdout == S_TRUE) && (sl_is_suid()))
1096 {
1097 dlog(1, FIL__, __LINE__,
1098 _("You cannot write the database to stdout when running with suid privileges.\n"));
1099 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORTS,
1100 _("Writing database to stdout when suid"),
1101 sh.prg_name,
1102 _("sh_dbIO_data_write_int"));
1103 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1104 }
1105
1106
1107 if ( (pushdata_isfirst == 1) && (pushdata_stdout == S_FALSE) &&
1108 ( (NULL == outpath) || (0 == sl_strcmp(outpath, _("REQ_FROM_SERVER"))) ) )
1109 {
1110 dlog(1, FIL__, __LINE__,
1111 _("You need to configure a local path for initializing the database\nlike ./configure --with-data-file=REQ_FROM_SERVER/some/local/path\n"));
1112 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORTS,
1113 _("No local path for database specified"),
1114 sh.prg_name,
1115 _("sh_dbIO_data_write_int"));
1116 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1117 }
1118
1119 if ((pushdata_isfirst == 1) && (pushdata_stdout == S_FALSE))
1120 {
1121 /* Warn that file already exists; file_path != NULL here because
1122 * checked above
1123 */
1124 struct stat sbuf;
1125
1126 if (0 == retry_lstat(FIL__, __LINE__, outpath, &sbuf))
1127 {
1128 if (sh.flag.update == S_FALSE)
1129 {
1130 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_FI_DBEX,
1131 file_path('D', 'W'));
1132 }
1133 }
1134 }
1135
1136 return;
1137}
1138
1139static SL_TICKET open_writeout_data_truncate(const char * path)
1140{
1141 int status;
1142 SL_TICKET fd;
1143
1144 if ( SL_ISERROR(fd = sl_open_rdwr_trunc(FIL__, __LINE__, path, SL_YESPRIV)))
1145 {
1146 sh_error_handle((-1), FIL__, __LINE__, fd, MSG_E_ACCESS,
1147 geteuid(), path);
1148 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1149 }
1150
1151 if (SL_ISERROR(status = sl_lock (fd)))
1152 {
1153 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGPATH,
1154 _("Failed to lock baseline database"), _("sh_dbIO_data_write_int"),
1155 path);
1156 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1157 }
1158 return fd;
1159}
1160
1161static SL_TICKET open_writeout_data(const char * path)
1162{
1163 int status;
1164 SL_TICKET fd;
1165
1166 if ( SL_ISERROR(fd = sl_open_rdwr(FIL__, __LINE__, path, SL_YESPRIV)))
1167 {
1168 sh_error_handle((-1), FIL__, __LINE__, fd, MSG_E_ACCESS,
1169 geteuid(), path);
1170 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1171 }
1172
1173 if (SL_ISERROR(status = sl_lock (fd)))
1174 {
1175 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGPATH,
1176 _("Failed to lock baseline database"), _("sh_dbIO_data_write_int"),
1177 path);
1178 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1179 }
1180 return fd;
1181}
1182
1183static void seek_writeout_data(SL_TICKET fd, const char * path)
1184{
1185 int status;
1186
1187 if ( SL_ISERROR(status = sl_forward(fd)))
1188 {
1189 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGPATH,
1190 _("Failed to seek to end of baseline database"),
1191 _("seek_writeout_data"),
1192 path);
1193 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1194 }
1195 return;
1196}
1197
1198/* Seek to [SOF] and truncate remainder
1199 */
1200static int seek_writeout_data_old(SL_TICKET fd, const char * path)
1201{
1202 char * line = SH_ALLOC(MAX_PATH_STORE+1);
1203
1204 /* This will do an ftruncate() after the sof marker
1205 */
1206 if (SL_ISERROR(sh_dbIO_setdataent_old (fd, line, MAX_PATH_STORE, path)))
1207 {
1208 SH_FREE(line);
1209 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
1210 _("Failed to seek to start of baseline database"),
1211 _("seek_writeout_data_old"),
1212 path);
1213 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1214 }
1215 SH_FREE(line);
1216 return 0;
1217}
1218
1219char * prep_path(char * path, int flag)
1220{
1221 size_t old_len = sl_strlen(path);
1222 char * tmp;
1223 size_t tmp_len;
1224 size_t path_len;
1225 char * outpath = NULL;
1226#if !defined(SH_STEALTH)
1227 (void) flag;
1228#endif
1229
1230#if defined(SH_STEALTH)
1231 if (flag == S_TRUE)
1232 sh_do_encode(path, old_len);
1233#endif
1234 tmp = quote_string(path, old_len);
1235 tmp_len = sl_strlen(tmp);
1236#if defined(SH_STEALTH)
1237 if (flag == S_TRUE)
1238 sh_do_decode(path, old_len);
1239#endif
1240
1241 if (tmp && tmp_len <= MAX_PATH_STORE)
1242 {
1243 outpath = sh_util_strdup(path);
1244 }
1245 else
1246 {
1247 char hashbuf[KEYBUF_SIZE];
1248
1249 outpath = sh_util_strdup(sh_tiger_hash (path,
1250 TIGER_DATA, old_len,
1251 hashbuf, sizeof(hashbuf)));
1252 }
1253 if (tmp)
1254 SH_FREE(tmp);
1255
1256 path_len = sl_strlen(outpath);
1257#if defined(SH_STEALTH)
1258 if (flag == S_TRUE)
1259 sh_do_encode(outpath, path_len);
1260#endif
1261
1262 tmp = quote_string(outpath, path_len);
1263 if (tmp) {
1264 SH_FREE(outpath);
1265 outpath = tmp;
1266 }
1267 return outpath;
1268}
1269
1270static char * prep_attr(char * attr_str)
1271{
1272 char * tmp;
1273 char * outstr = NULL;
1274 size_t old_len = sl_strlen(attr_str);
1275
1276#if defined(SH_STEALTH)
1277 sh_do_encode(attr_str, old_len);
1278#endif
1279
1280 tmp = quote_string(attr_str, old_len);
1281 if (tmp)
1282 {
1283 outstr = tmp;
1284 }
1285
1286#if defined(SH_STEALTH)
1287 sh_do_decode(attr_str, old_len);
1288#endif
1289 return outstr;
1290}
1291
1292static void prep_encode(sh_filestore_t * p)
1293{
1294#if defined(SH_STEALTH)
1295 sh_do_encode(p->c_mode, sl_strlen(p->c_mode));
1296 sh_do_encode(p->c_owner, sl_strlen(p->c_owner));
1297 sh_do_encode(p->c_group, sl_strlen(p->c_group));
1298 sh_do_encode(p->checksum, sl_strlen(p->checksum));
1299 sh_do_encode(p->c_attributes, sl_strlen(p->c_attributes));
1300#else
1301 (void) p;
1302#endif
1303 return;
1304}
1305
1306static void prep_struct(sh_filestore_t * p, file_type * buf, char * fileHash)
1307{
1308#if !defined(__linux__) && !defined(HAVE_STAT_FLAGS)
1309 int i;
1310#endif
1311 p->mark = REC_MAGIC;
1312 sl_strlcpy(p->c_mode, buf->c_mode, CMODE_SIZE);
1313 sl_strlcpy(p->c_group, buf->c_group, GROUP_MAX+1);
1314 sl_strlcpy(p->c_owner, buf->c_owner, USER_MAX+1);
1315 if (fileHash) {
1316 sl_strlcpy(p->checksum, fileHash, KEY_LEN+1);
1317 }
1318#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
1319 sl_strlcpy(p->c_attributes, buf->c_attributes, ATTRBUF_SIZE);
1320#else
1321 for (i = 0; i < ATTRBUF_USED; ++i) p->c_attributes[i] = '-';
1322 p->c_attributes[ATTRBUF_USED] = '\0';
1323#endif
1324
1325 prep_encode(p);
1326
1327#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
1328 p->attributes = (UINT32) buf->attributes;
1329#else
1330 p->attributes = 0;
1331#endif
1332 p->linkmode = (UINT32) buf->linkmode;
1333 p->hardlinks = (UINT32) buf->hardlinks;
1334 p->dev = (UINT64) buf->dev;
1335 p->rdev = (UINT64) buf->rdev;
1336 p->mode = (UINT32) buf->mode;
1337 p->ino = (UINT32) buf->ino;
1338 p->size = (UINT64) buf->size;
1339 p->mtime = (UINT64) buf->mtime;
1340 p->atime = (UINT64) buf->atime;
1341 p->ctime = (UINT64) buf->ctime;
1342 p->owner = (UINT32) buf->owner;
1343 p->group = (UINT32) buf->group;
1344
1345 p->checkflags = (UINT32) buf->check_flags;
1346
1347 return;
1348}
1349
1350
1351static void write_start_header(SL_TICKET fd)
1352{
1353 char timestring[81];
1354
1355 if (pushdata_stdout == S_FALSE)
1356 {
1357 sl_write (fd, _("\n#Host "), 7);
1358 sl_write (fd, sh.host.name,
1359 sl_strlen(sh.host.name));
1360 sl_write (fd, _(" Version "), 9);
1361 sl_write (fd, sh_db_version_string,
1362 sl_strlen(sh_db_version_string));
1363 sl_write (fd, _(" Date "), 6);
1364 (void) sh_unix_time(0, timestring, sizeof(timestring));
1365 sl_write (fd, timestring, strlen(timestring));
1366 sl_write (fd, "\n", 1);
1367 }
1368 else
1369 {
1370 printf ("%s",_("\n#Host "));
1371 printf ("%s", sh.host.name);
1372 printf ("%s",_(" Version "));
1373 printf ("%s", sh_db_version_string);
1374 printf ("%s",_(" Date "));
1375 (void) sh_unix_time(0, timestring, sizeof(timestring));
1376 printf ("%s\n", timestring);
1377 }
1378}
1379
1380static void write_start_marker(SL_TICKET fd)
1381{
1382 if (sh_db_version_string != NULL)
1383 {
1384 write_start_header(fd);
1385 }
1386
1387 if (pushdata_stdout == S_FALSE)
1388 {
1389#if defined(SH_STEALTH)
1390 sl_write (fd, "\n", 1);
1391 sl_write_line (fd, N_("[SOF]"), 5);
1392#else
1393 sl_write_line (fd, _("\n[SOF]"), 6);
1394#endif
1395 }
1396 else
1397 {
1398#if defined(SH_STEALTH)
1399 puts (N_("[SOF]"));
1400#else
1401 puts (_("\n[SOF]"));
1402#endif
1403 }
1404}
1405
1406static void write_record(SL_TICKET fd, sh_filestore_t * p,
1407 char * fullpath, char * linkpath, char * attr_string)
1408{
1409 static char ll[2] = { '-', '\0' };
1410 char * lpath;
1411
1412 if (!linkpath || 0 == sl_strlen(linkpath))
1413 lpath = ll;
1414 else
1415 /* cppcheck-suppress uninitvar */
1416 lpath = linkpath;
1417
1418 if (pushdata_stdout == S_FALSE)
1419 {
1420 if (SL_ENONE != sl_write (fd, p, sizeof(sh_filestore_t)))
1421 {
1422 char * tmp = sh_util_safe_name(fullpath);
1423 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
1424 _("Failed to write record to baseline database"),
1425 _("write_record"),
1426 tmp);
1427 SH_FREE(tmp);
1428 aud_exit(FIL__, __LINE__, EXIT_FAILURE );
1429 }
1430 if (SL_ENONE != sl_write_line_fast (fd, fullpath, sl_strlen(fullpath)))
1431 {
1432 char * tmp = sh_util_safe_name(fullpath);
1433 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
1434 _("Failed to write path to baseline database"),
1435 _("write_record"),
1436 tmp);
1437 SH_FREE(tmp);
1438 aud_exit(FIL__, __LINE__, EXIT_FAILURE );
1439 }
1440 if (SL_ENONE != sl_write_line_fast (fd, lpath, sl_strlen(lpath)))
1441 {
1442 char * tmp = sh_util_safe_name(fullpath);
1443 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
1444 _("Failed to write lpath to baseline database"),
1445 _("write_record"),
1446 tmp);
1447 SH_FREE(tmp);
1448 aud_exit(FIL__, __LINE__, EXIT_FAILURE );
1449 }
1450 if (attr_string)
1451 sl_write_line_fast (fd, attr_string, sl_strlen(attr_string));
1452 }
1453 else
1454 {
1455 if (fwrite (p, sizeof(sh_filestore_t), 1, stdout))
1456 {
1457 puts (fullpath);
1458 puts (lpath);
1459 if (attr_string)
1460 puts (attr_string);
1461 }
1462 else
1463 {
1464 perror(_("Error writing database"));
1465 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1466 }
1467 }
1468
1469 SH_FREE(fullpath);
1470 if (linkpath)
1471 SH_FREE(linkpath);
1472 if (attr_string)
1473 SH_FREE(attr_string);
1474
1475 return;
1476}
1477
1478static void sh_dbIO_data_write_int (file_type * buf, char * fileHash,
1479 const char * outpath, int truncate)
1480{
1481 sh_filestore_t p;
1482 char * fullpath = NULL;
1483 char * linkpath = NULL;
1484 char * attr_string = NULL;
1485
1486 SL_ENTER(_("sh_dbIO_data_write_int"));
1487
1488 do_writeout_checks(outpath);
1489
1490 if (sh.flag.update == S_FALSE)
1491 {
1492 if (pushdata_stdout == S_FALSE && pushdata_fd == -1)
1493 {
1494 if (truncate == S_TRUE)
1495 pushdata_fd = open_writeout_data_truncate(outpath);
1496 else
1497 {
1498 pushdata_fd = open_writeout_data(outpath);
1499 /* Seek to eof */
1500 seek_writeout_data(pushdata_fd, outpath);
1501 }
1502 }
1503 }
1504 else /* update == TRUE */
1505 {
1506 if (pushdata_isfirst == 1)
1507 {
1508 TPT((0, FIL__, __LINE__, _("msg=<Update.>\n")));
1509 pushdata_fd = open_writeout_data(outpath);
1510 /* Seek to sof and truncate */
1511 seek_writeout_data_old(pushdata_fd, outpath);
1512 }
1513 }
1514
1515 /* unconditionally initialize the structure */
1516 memset(&p, 0, sizeof(sh_filestore_t));
1517
1518 if (buf != NULL)
1519 {
1520 fullpath = prep_path(buf->fullpath, S_TRUE);
1521 }
1522
1523 /* NOTE: TXT entries are c_mode[0] != 'l' and do not get decoded
1524 */
1525 if (buf != NULL /* && buf->c_mode[0] == 'l' */ && buf->link_path != NULL)
1526 {
1527 if (buf->c_mode[0] == 'l')
1528 linkpath = prep_path(buf->link_path, S_TRUE);
1529 else
1530 linkpath = prep_path(buf->link_path, S_FALSE);
1531 }
1532
1533 if (buf != NULL && buf->attr_string != NULL)
1534 {
1535 attr_string = prep_attr(buf->attr_string);
1536 }
1537
1538 if (buf != NULL)
1539 {
1540 prep_struct(&p, buf, fileHash);
1541 if (attr_string)
1542 p.mark |= REC_FLAGS_ATTR;
1543 swap_data(&p);
1544 }
1545
1546 /* write the start marker
1547 */
1548 if (pushdata_isfirst == 1)
1549 {
1550 if (sh.flag.update == S_FALSE)
1551 write_start_marker(pushdata_fd);
1552 pushdata_isfirst = 0;
1553 }
1554
1555 if (buf && fullpath)
1556 {
1557 write_record(pushdata_fd, &p, fullpath, linkpath, attr_string);
1558 }
1559
1560 if ((sh.flag.update != S_TRUE) && (pushdata_stdout == S_FALSE))
1561 {
1562 if (sh.flag.checkSum != SH_CHECK_INIT || (buf == NULL && fileHash == NULL))
1563 {
1564 if (SL_ISERROR(sl_close (pushdata_fd)))
1565 {
1566 char * tmp = sh_util_safe_name(outpath);
1567 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
1568 _("Failed to close baseline database"),
1569 _("sh_dbIO_data_write_int"),
1570 tmp);
1571 SH_FREE(tmp);
1572 }
1573 else {
1574 if (sh.flag.checkSum == SH_CHECK_INIT)
1575 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_DCLOSE);
1576 }
1577 pushdata_fd = -1;
1578 }
1579 }
1580
1581 SL_RET0(_("sh_dbIO_data_write_int"));
1582}
1583
1584SH_MUTEX_STATIC(mutex_writeout,PTHREAD_MUTEX_INITIALIZER);
1585
1586void sh_dbIO_data_write (file_type * buf, char * fileHash)
1587{
1588 SH_MUTEX_LOCK(mutex_writeout);
1589 sh_dbIO_data_write_int (buf, fileHash, file_path('D', 'W'), S_FALSE);
1590 SH_MUTEX_UNLOCK(mutex_writeout);
1591 return;
1592}
1593
1594
1595static int dbIO_writeout(sh_file_t * mtab[TABSIZE], const char * outpath, int truncate)
1596{
1597 sh_file_t * p;
1598 int i;
1599 file_type * f;
1600 char fileHash[KEY_LEN + 1];
1601
1602 SL_ENTER(_("dbIO_writeout"));
1603
1604 SH_MUTEX_LOCK(mutex_writeout);
1605 if (!SL_ISERROR(pushdata_fd))
1606 {
1607 sl_close(pushdata_fd);
1608 pushdata_fd = -1;
1609 }
1610 pushdata_isfirst = 1;
1611
1612
1613 SH_MUTEX_LOCK(mutex_hash);
1614 for (i = 0; i < TABSIZE; ++i)
1615 {
1616 for (p = mtab[i]; p; p = p->next)
1617 {
1618 f = sh_hash_create_ft (p, fileHash);
1619 sh_dbIO_data_write_int (f, fileHash, outpath, (i == 0) ? truncate : S_FALSE);
1620 if (f->attr_string) SH_FREE(f->attr_string);
1621 if (f->link_path) SH_FREE(f->link_path);
1622 SH_FREE(f);
1623 }
1624 }
1625 SH_MUTEX_UNLOCK(mutex_hash);
1626
1627 if (!SL_ISERROR(pushdata_fd))
1628 {
1629 sl_close(pushdata_fd);
1630 pushdata_fd = -1;
1631 }
1632 pushdata_isfirst = 1;
1633 SH_MUTEX_UNLOCK(mutex_writeout);
1634
1635 SL_RETURN (0, _("dbIO_writeout"));
1636}
1637
1638int sh_dbIO_writeout_update()
1639{
1640 sh_file_t ** mtab = get_default_data_table();
1641
1642 if (S_TRUE == file_is_remote())
1643 {
1644 sh_error_handle((-1), FIL__, __LINE__, S_FALSE, MSG_E_SUBGEN,
1645 _("Baseline database is remote"), _("sh_dbIO_writeout"));
1646 SL_RETURN (1, _("sh_dbIO_writeout_update"));
1647 }
1648
1649 return dbIO_writeout(mtab, file_path('D', 'W'), S_FALSE);
1650}
1651
1652int sh_dbIO_writeout_to_path(const char * path)
1653{
1654 sh_file_t ** mtab = get_default_data_table();
1655 return dbIO_writeout(mtab, path, S_TRUE);
1656}
1657
1658static void dbIO_write_record(sh_file_t * record, SL_TICKET fd)
1659{
1660 sh_filestore_t * p = &(record->theFile);
1661 char * fullpath = NULL;
1662 char * linkpath = NULL;
1663 char * attr_string = NULL;
1664
1665 fullpath = prep_path(record->fullpath, S_TRUE);
1666
1667 /* NOTE: TXT entries are c_mode[0] != 'l' and do not get decoded
1668 */
1669 if (record->linkpath != NULL && 0 != strcmp("-", record->linkpath))
1670 {
1671 if (p->c_mode[0] == 'l')
1672 linkpath = prep_path(record->linkpath, S_TRUE);
1673 else
1674 linkpath = prep_path(record->linkpath, S_FALSE);
1675 }
1676
1677 if (record->attr_string != NULL)
1678 attr_string = prep_attr(record->attr_string);
1679
1680 prep_encode(p);
1681 swap_data(p);
1682
1683 write_record(fd, p, fullpath, linkpath, attr_string);
1684 return;
1685}
1686
1687static void dbIO_write_entry(sh_file_t * p)
1688{
1689 static int is_first = 1;
1690
1691 if (is_first)
1692 {
1693 pushdata_isfirst = 1;
1694 if (!sh.outpath || sh.outpath[0] == '\0')
1695 pushdata_stdout = S_TRUE;
1696 else
1697 pushdata_fd = open_writeout_data_truncate(sh.outpath);
1698 write_start_marker(pushdata_fd);
1699 pushdata_isfirst = 0;
1700 is_first = 0;
1701 }
1702
1703 dbIO_write_record(p, pushdata_fd);
1704
1705}
1706
1707
1708/******************************************************************
1709 *
1710 * Listing the database.
1711 *
1712 ******************************************************************/
1713
1714static int ListBinary = S_FALSE;
1715static char * ListFilter = NULL;
1716
1717int sh_dbIO_list_binary (const char * c)
1718{
1719 (void) c;
1720 ListBinary = S_TRUE;
1721 return 0;
1722}
1723int sh_dbIO_list_filter (const char * c)
1724{
1725 ListFilter = sh_util_strdup(c);
1726 return 0;
1727}
1728
1729#include "zAVLTree.h"
1730
1731static zAVLTree * filter_list = NULL;
1732extern char * rtrim (char * str);
1733
1734#include <ctype.h>
1735static void read_filter()
1736{
1737 int i, n = 0;
1738 size_t len;
1739 char * key;
1740 char * str;
1741 char * line = SH_ALLOC(SH_MAXBUF);
1742 FILE * fd = fopen(ListFilter, "r");
1743
1744 if (!fd)
1745 {
1746 perror(_("read_filter: fopen:"));
1747 _exit(EXIT_FAILURE);
1748 }
1749 do {
1750 i = sh_dbIO_getline (fd, line, SH_MAXBUF);
1751 str = rtrim(line);
1752 while (isspace((int)*str)) ++str;
1753
1754 key = sh_files_parse_input(str, &len);
1755
1756 if (key && *key == '/')
1757 {
1758 zAVL_string_set(&filter_list, key);
1759 ++n;
1760 }
1761 } while (i >= 0);
1762
1763 fclose(fd);
1764 SH_FREE(line);
1765
1766 if (n == 0)
1767 {
1768 fprintf(stderr, _("read_filter: empty file <%s>\n"), ListFilter);
1769 _exit (EXIT_FAILURE);
1770 }
1771 return;
1772}
1773
1774static int check_filter(char * path)
1775{
1776 if (NULL == zAVL_string_get(filter_list, path))
1777 return S_FALSE;
1778 return S_TRUE;
1779}
1780
1781int sh_dbIO_list_db (const char * db_file)
1782{
1783 sh_file_t * p;
1784 SL_TICKET fd;
1785 char * line;
1786 int errflag = 0;
1787 int flag = 0;
1788 char * ListFile = get_list_file();
1789
1790 if (!db_file)
1791 {
1792 fputs(_("ERROR: no database file given\n"), stderr);
1793 _exit(EXIT_FAILURE);
1794 return -1;
1795 }
1796 if (sl_is_suid())
1797 {
1798 fputs(_("ERROR: insufficient privilege\n"), stderr);
1799 _exit (EXIT_FAILURE);
1800 return -1; /* for Mac OSX compiler */
1801 }
1802 if (0 == strcmp(db_file, _("default")))
1803 db_file = file_path('D', 'W');
1804 if (!db_file)
1805 {
1806 fputs(_("ERROR: no filename\n"), stderr);
1807 _exit(EXIT_FAILURE);
1808 return -1;
1809 }
1810
1811 if (ListFilter)
1812 read_filter();
1813
1814 line = SH_ALLOC(MAX_PATH_STORE+2);
1815
1816 if ( SL_ISERROR(fd = sl_open_read(FIL__, __LINE__, db_file, SL_YESPRIV)))
1817 {
1818 fprintf(stderr, _("ERROR: can't open %s for read (errnum = %ld)\n"),
1819 db_file, fd);
1820 _exit(EXIT_FAILURE);
1821 return -1;
1822 }
1823
1824 /* fast forward to start of data
1825 */
1826 if (0 != sh_dbIO_setdataent(fd, line, MAX_PATH_STORE+1, db_file))
1827 {
1828 fprintf(stderr, _("ERROR: can't find start marker in %s\n"),
1829 db_file);
1830 _exit(EXIT_FAILURE);
1831 return -1;
1832 }
1833
1834 while (1)
1835 {
1836 p = sh_dbIO_getdataent (line, MAX_PATH_STORE+1, db_file, &errflag);
1837 if ((p != NULL) && (p->fullpath[0] == '/'))
1838 {
1839 if (!ListFile)
1840 {
1841 flag = 1;
1842 if (ListFilter && S_FALSE == check_filter(p->fullpath))
1843 continue;
1844 if (ListBinary)
1845 dbIO_write_entry (p);
1846 else
1847 sh_hash_list_db_entry (p);
1848 }
1849 else
1850 {
1851 if (0 != sl_strcmp(ListFile, p->fullpath))
1852 {
1853 continue;
1854 }
1855 flag = 1;
1856 if ('l' != p->theFile.c_mode[0])
1857 {
1858 if (sh_hash_printcontent(p->linkpath) < 0)
1859 {
1860 fputs(_("Error listing file content\n"), stderr);
1861 _exit(EXIT_FAILURE);
1862 return -1;
1863 }
1864 }
1865 else
1866 {
1867 fputs(_("File is a link\n"), stderr);
1868 _exit(EXIT_FAILURE);
1869 return -1;
1870 }
1871 break;
1872 }
1873 }
1874 else if (p == NULL)
1875 {
1876 break;
1877 }
1878 }
1879
1880 if (line != NULL)
1881 SH_FREE(line);
1882 sl_close (fd);
1883
1884 fflush(NULL);
1885
1886 if (flag == 0)
1887 {
1888 fputs(_("File not found.\n"), stderr);
1889 _exit(EXIT_FAILURE);
1890 }
1891 else if (errflag < 0)
1892 {
1893 fputs(_("Error while reading file.\n"), stderr);
1894 _exit(EXIT_FAILURE);
1895 }
1896
1897 _exit(EXIT_SUCCESS);
1898 return 0;
1899}
1900
1901/* if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE) */
1902#endif
Note: See TracBrowser for help on using the repository browser.