source: trunk/src/sh_dbIO.c@ 549

Last change on this file since 549 was 543, checked in by katerina, 6 years ago

Fix for ticket #434 (option to init for alternative root fs).

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_gpg.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 unsigned char swap;
230 static unsigned short ooop;
231 unsigned char * ii;
232 ooop = *iptr;
233 ii = (unsigned char *) &ooop;
234 swap = ii[0]; ii[0] = ii[1]; ii[1] = swap;
235 return &ooop;
236 }
237 return iptr;
238#else
239 return iptr;
240#endif
241}
242
243static void swap_data(sh_filestore_t * ft)
244{
245 swap_32(&(ft->mode));
246 swap_32(&(ft->linkmode));
247 swap_64(&(ft->dev));
248 swap_64(&(ft->rdev));
249 swap_32(&(ft->hardlinks));
250 swap_32(&(ft->ino));
251 swap_64(&(ft->size));
252 swap_64(&(ft->atime));
253 swap_64(&(ft->mtime));
254 swap_64(&(ft->ctime));
255 swap_32(&(ft->owner));
256 swap_32(&(ft->group));
257 swap_32(&(ft->checkflags));
258#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
259 swap_32(&(ft->attributes));
260#endif
261 ft->mark = *(swap_short(&(ft->mark)));
262 return;
263}
264
265#define QUOTE_CHAR '='
266
267char * unquote_string (const char * str, size_t len)
268{
269 int i = 0, t1, t2;
270 char * tmp = NULL;
271 size_t l2, j, k = 0;
272
273 SL_ENTER(_("unquote_string"));
274
275 if (str != NULL)
276 {
277 l2 = len - 2;
278 tmp = SH_ALLOC(len + 1);
279
280 for (j = 0; j <= len; ++j)
281 {
282 if (str[j] != QUOTE_CHAR)
283 {
284 tmp[k] = str[j];
285 }
286 else if (str[j] == QUOTE_CHAR && j < l2)
287 {
288 t1 = sh_util_hexchar(str[j+1]);
289 t2 = sh_util_hexchar(str[j+2]);
290 if ((t1|t2) >= 0)
291 {
292 i = 16 * t1 + t2;
293 tmp[k] = i;
294 j += 2;
295 }
296 else
297 {
298 tmp[k] = str[j];
299 }
300 }
301 else
302 tmp[k] = str[j];
303 ++k;
304 }
305 }
306 SL_RETURN(tmp, _("unquote_string"));
307}
308
309static char * int2hex (unsigned char i, char * i2h)
310{
311 static char hexchars[] = "0123456789ABCDEF";
312
313 i2h[0] = hexchars[(((i) & 0xF0) >> 4)]; /* high */
314 i2h[1] = hexchars[((i) & 0x0F)]; /* low */
315
316 return i2h;
317}
318
319char * quote_string (const char * str, size_t len)
320{
321 char * tmp;
322 char * tmp2;
323 size_t l2, j, i = 0, k = 0;
324 char i2h[2];
325
326 SL_ENTER(_("quote_string"));
327
328 if (str == NULL)
329 {
330 SL_RETURN(NULL, _("quote_string"));
331 }
332
333 for (j = 0; j < len; ++j)
334 if (str[j] == '\n' || str[j] == QUOTE_CHAR) ++i;
335
336 l2 = len + 1;
337 if (sl_ok_muls(3, i) && sl_ok_adds(l2, (3*i)))
338 {
339 tmp = SH_ALLOC(len + 1 + 3*i);
340 }
341 else
342 {
343 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
344 _("integer overflow"),
345 _("quote_string"));
346 SL_RETURN(NULL, _("quote_string"));
347 }
348
349 for (j = 0; j <= len; ++j)
350 {
351 if (str[j] == '\n')
352 {
353 tmp2 = int2hex((unsigned char) '\n', i2h);
354 tmp[k] = QUOTE_CHAR; ++k;
355 tmp[k] = tmp2[0]; ++k;
356 tmp[k] = tmp2[1];
357 }
358 else if (str[j] == QUOTE_CHAR)
359 {
360 tmp2 = int2hex((unsigned char) QUOTE_CHAR, i2h);
361 tmp[k] = QUOTE_CHAR; ++k;
362 tmp[k] = tmp2[0]; ++k;
363 tmp[k] = tmp2[1];
364 }
365 else
366 {
367 tmp[k] = str[j];
368 }
369 ++k;
370 }
371 SL_RETURN(tmp, _("quote_string"));
372}
373
374static char * unquote_path(char * line, long i)
375{
376 char * tmp = unquote_string (line, i);
377 size_t len = sl_strlen(tmp)+1;
378 char * path = SH_ALLOC(len);
379
380 (void) sl_strlcpy (path, tmp, len);
381 if (tmp)
382 SH_FREE(tmp);
383 if (len > 1) {
384 if (path[len-2] == '\n')
385 path[len-2] = '\0';
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_GPG) || defined(WITH_PGP)
855 SL_TICKET fdTmp;
856
857 /* extract the data and copy to temporary file
858 */
859 fdTmp = sh_gpg_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_gpg_check_sign (fd, SIG_DATA))
874 {
875 sl_close(fd);
876 return -1;
877 }
878 sl_rewind (fd);
879#endif
880
881 return fd;
882}
883
884static int read_data(SL_TICKET fd, sh_file_t * tab[TABSIZE],
885 const char * filepath)
886{
887 sh_file_t * p;
888 int count = 0;
889 int errflag = 0;
890 char * line = SH_ALLOC(MAX_PATH_STORE+2);
891
892 /* fast forward to start of data
893 */
894 if (0 != sh_dbIO_setdataent(fd, line, MAX_PATH_STORE+1, filepath))
895 return -1;
896
897 while (1)
898 {
899 if (sig_termfast == 1) /* SIGTERM */
900 {
901 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
902 --sig_raised; --sig_urgent;
903 SH_FREE(line);
904 return -1;
905 }
906
907 p = sh_dbIO_getdataent (line, MAX_PATH_STORE+1, filepath, &errflag);
908 if (p != NULL)
909 {
910 if (!sh_hash_is_null_record(&(p->theFile)))
911 hashinsert (tab, p);
912 else
913 sh_hash_remove_unconditional (p->fullpath);
914 ++count;
915 }
916 else
917 break;
918 }
919
920 if (line != NULL)
921 SH_FREE(line);
922
923 /* Always keep db in memory, so we have no open file
924 */
925 sl_close (fd);
926
927 sl_fclose (FIL__, __LINE__, sh_fin_fd);
928 sh_fin_fd = NULL;
929
930 return errflag;
931}
932
933
934static int sh_dbIO_load_db_int(sh_file_t * tab[TABSIZE],
935 const char * filepath, const char * uuid)
936{
937#define FGETS_BUF 16384
938
939 SL_TICKET fd = -1;
940
941 if (uuid)
942 {
943 fd = load_data_from_server(uuid);
944 if (SL_ISERROR(fd))
945 return -1;
946 }
947 else if (!filepath)
948 {
949 char * dbpath = file_path('D', 'R');
950
951 fd = load_data_from_server(NULL);
952
953 if (SL_ISERROR(fd))
954 {
955 if (*dbpath == '/')
956 fd = load_data_from_disk(dbpath);
957 }
958 }
959 else
960 {
961 fd = load_data_from_disk(filepath);
962 }
963
964 if (SL_ISERROR(fd))
965 return -1;
966
967 if (sig_termfast == 1) /* SIGTERM */
968 {
969 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
970 --sig_raised; --sig_urgent;
971 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
972 }
973
974 fd = verify_data(fd);
975 if (SL_ISERROR(fd))
976 return -1;
977
978 if (!uuid) { int i; for (i = 0; i < TABSIZE; ++i) tab[i] = NULL; }
979
980 return read_data (fd, tab, filepath);
981}
982
983
984int sh_dbIO_load_db(sh_file_t * tab[TABSIZE])
985{
986 return sh_dbIO_load_db_int(tab, NULL, NULL);
987}
988int sh_dbIO_load_db_file(sh_file_t * tab[TABSIZE], const char * filepath)
989{
990 return sh_dbIO_load_db_int(tab, filepath, NULL);
991}
992
993int sh_dbIO_load_delta()
994{
995 int status = 0;
996#if defined(SH_WITH_CLIENT)
997 sh_file_t ** mtab = get_default_data_table();
998 int errflag = 0;
999 unsigned int count;
1000 time_t last;
1001
1002 if ( sh.flag.checkSum != SH_CHECK_INIT )
1003 {
1004 if (sh_hash_get_initialized() != 0)
1005 {
1006 char * uuid = sh_socket_get_uuid(&errflag, &count, &last);
1007
1008 if (!uuid)
1009 return errflag;
1010
1011 if (count > 0)
1012 sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, count, MSG_E_SUBGEN,
1013 _("Retrying download of delta DB"),
1014 _("sh_dbIO_load_delta"));
1015
1016 status = sh_dbIO_load_db_int(mtab, NULL, uuid);
1017 if (status < 0)
1018 {
1019 /* Return status < 0 indicates that max_try is exceeded
1020 */
1021 if (sh_socket_return_uuid(uuid, count, last) < 0)
1022 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_D_DELTAFAIL, uuid);
1023 }
1024 else
1025 {
1026 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_D_DELTAOK, uuid);
1027 }
1028 SH_FREE(uuid);
1029 }
1030 else
1031 {
1032 /* not initialized yet */
1033 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, -1, MSG_E_SUBGEN,
1034 _("Download of delta DB skipped, not initialized yet"),
1035 _("sh_dbIO_load_delta"));
1036 return -1;
1037 }
1038 }
1039#endif
1040 return status;
1041}
1042
1043/******************************************************************
1044 *
1045 * Writing out a file to the database.
1046 *
1047 ******************************************************************/
1048static int pushdata_isfirst = 1;
1049static SL_TICKET pushdata_fd = -1;
1050
1051static int pushdata_stdout = S_FALSE;
1052
1053static char * sh_db_version_string = NULL;
1054
1055int sh_dbIO_writeout_stdout (const char * str)
1056{
1057 if (!str)
1058 { pushdata_stdout = S_TRUE; return 0; }
1059 return -1;
1060}
1061
1062int sh_dbIO_version_string(const char * str)
1063{
1064 if (str)
1065 {
1066 if (sh_db_version_string != NULL) {
1067 SH_FREE(sh_db_version_string);
1068 }
1069 if (0 == sl_strncmp(str, _("NULL"), 4))
1070 {
1071 sh_db_version_string = NULL;
1072 return 0;
1073 }
1074 sh_db_version_string = sh_util_strdup(str);
1075 return 0;
1076 }
1077 return -1;
1078}
1079
1080void do_writeout_checks(const char * outpath)
1081{
1082 if ((pushdata_stdout == S_TRUE) && (sh.flag.update == S_TRUE))
1083 {
1084 dlog(1, FIL__, __LINE__,
1085 _("You cannot write the database to stdout when you use update rather than init.\n"));
1086 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORTS,
1087 _("Writing database to stdout with update"),
1088 sh.prg_name,
1089 _("sh_dbIO_data_write_int"));
1090 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1091 }
1092
1093 if ((pushdata_stdout == S_TRUE) && (sl_is_suid()))
1094 {
1095 dlog(1, FIL__, __LINE__,
1096 _("You cannot write the database to stdout when running with suid privileges.\n"));
1097 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORTS,
1098 _("Writing database to stdout when suid"),
1099 sh.prg_name,
1100 _("sh_dbIO_data_write_int"));
1101 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1102 }
1103
1104
1105 if ( (pushdata_isfirst == 1) && (pushdata_stdout == S_FALSE) &&
1106 ( (NULL == outpath) || (0 == sl_strcmp(outpath, _("REQ_FROM_SERVER"))) ) )
1107 {
1108 dlog(1, FIL__, __LINE__,
1109 _("You need to configure a local path for initializing the database\nlike ./configure --with-data-file=REQ_FROM_SERVER/some/local/path\n"));
1110 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORTS,
1111 _("No local path for database specified"),
1112 sh.prg_name,
1113 _("sh_dbIO_data_write_int"));
1114 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1115 }
1116
1117 if ((pushdata_isfirst == 1) && (pushdata_stdout == S_FALSE))
1118 {
1119 /* Warn that file already exists; file_path != NULL here because
1120 * checked above
1121 */
1122 struct stat sbuf;
1123
1124 if (0 == retry_lstat(FIL__, __LINE__, outpath, &sbuf))
1125 {
1126 if (sh.flag.update == S_FALSE)
1127 {
1128 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_FI_DBEX,
1129 file_path('D', 'W'));
1130 }
1131 }
1132 }
1133
1134 return;
1135}
1136
1137static SL_TICKET open_writeout_data_truncate(const char * path)
1138{
1139 int status;
1140 SL_TICKET fd;
1141
1142 if ( SL_ISERROR(fd = sl_open_rdwr_trunc(FIL__, __LINE__, path, SL_YESPRIV)))
1143 {
1144 sh_error_handle((-1), FIL__, __LINE__, fd, MSG_E_ACCESS,
1145 geteuid(), path);
1146 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1147 }
1148
1149 if (SL_ISERROR(status = sl_lock (fd)))
1150 {
1151 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGPATH,
1152 _("Failed to lock baseline database"), _("sh_dbIO_data_write_int"),
1153 path);
1154 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1155 }
1156 return fd;
1157}
1158
1159static SL_TICKET open_writeout_data(const char * path)
1160{
1161 int status;
1162 SL_TICKET fd;
1163
1164 if ( SL_ISERROR(fd = sl_open_rdwr(FIL__, __LINE__, path, SL_YESPRIV)))
1165 {
1166 sh_error_handle((-1), FIL__, __LINE__, fd, MSG_E_ACCESS,
1167 geteuid(), path);
1168 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1169 }
1170
1171 if (SL_ISERROR(status = sl_lock (fd)))
1172 {
1173 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGPATH,
1174 _("Failed to lock baseline database"), _("sh_dbIO_data_write_int"),
1175 path);
1176 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1177 }
1178 return fd;
1179}
1180
1181static void seek_writeout_data(SL_TICKET fd, const char * path)
1182{
1183 int status;
1184
1185 if ( SL_ISERROR(status = sl_forward(fd)))
1186 {
1187 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGPATH,
1188 _("Failed to seek to end of baseline database"),
1189 _("seek_writeout_data"),
1190 path);
1191 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1192 }
1193 return;
1194}
1195
1196/* Seek to [SOF] and truncate remainder
1197 */
1198static int seek_writeout_data_old(SL_TICKET fd, const char * path)
1199{
1200 char * line = SH_ALLOC(MAX_PATH_STORE+1);
1201
1202 /* This will do an ftruncate() after the sof marker
1203 */
1204 if (SL_ISERROR(sh_dbIO_setdataent_old (fd, line, MAX_PATH_STORE, path)))
1205 {
1206 SH_FREE(line);
1207 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
1208 _("Failed to seek to start of baseline database"),
1209 _("seek_writeout_data_old"),
1210 path);
1211 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1212 }
1213 SH_FREE(line);
1214 return 0;
1215}
1216
1217char * prep_path(char * path, int flag)
1218{
1219 size_t old_len = sl_strlen(path);
1220 char * tmp;
1221 size_t tmp_len;
1222 size_t path_len;
1223 char * outpath = NULL;
1224#if !defined(SH_STEALTH)
1225 (void) flag;
1226#endif
1227
1228#if defined(SH_STEALTH)
1229 if (flag == S_TRUE)
1230 sh_do_encode(path, old_len);
1231#endif
1232 tmp = quote_string(path, old_len);
1233 tmp_len = sl_strlen(tmp);
1234#if defined(SH_STEALTH)
1235 if (flag == S_TRUE)
1236 sh_do_decode(path, old_len);
1237#endif
1238
1239 if (tmp && tmp_len <= MAX_PATH_STORE)
1240 {
1241 outpath = sh_util_strdup(path);
1242 }
1243 else
1244 {
1245 char hashbuf[KEYBUF_SIZE];
1246
1247 outpath = sh_util_strdup(sh_tiger_hash (path,
1248 TIGER_DATA, old_len,
1249 hashbuf, sizeof(hashbuf)));
1250 }
1251 if (tmp)
1252 SH_FREE(tmp);
1253
1254 path_len = sl_strlen(outpath);
1255#if defined(SH_STEALTH)
1256 if (flag == S_TRUE)
1257 sh_do_encode(outpath, path_len);
1258#endif
1259
1260 tmp = quote_string(outpath, path_len);
1261 if (tmp) {
1262 SH_FREE(outpath);
1263 outpath = tmp;
1264 }
1265 return outpath;
1266}
1267
1268static char * prep_attr(char * attr_str)
1269{
1270 char * tmp;
1271 char * outstr = NULL;
1272 size_t old_len = sl_strlen(attr_str);
1273
1274#if defined(SH_STEALTH)
1275 sh_do_encode(attr_str, old_len);
1276#endif
1277
1278 tmp = quote_string(attr_str, old_len);
1279 if (tmp)
1280 {
1281 outstr = tmp;
1282 }
1283
1284#if defined(SH_STEALTH)
1285 sh_do_decode(attr_str, old_len);
1286#endif
1287 return outstr;
1288}
1289
1290static void prep_encode(sh_filestore_t * p)
1291{
1292#if defined(SH_STEALTH)
1293 sh_do_encode(p->c_mode, sl_strlen(p->c_mode));
1294 sh_do_encode(p->c_owner, sl_strlen(p->c_owner));
1295 sh_do_encode(p->c_group, sl_strlen(p->c_group));
1296 sh_do_encode(p->checksum, sl_strlen(p->checksum));
1297 sh_do_encode(p->c_attributes, sl_strlen(p->c_attributes));
1298#else
1299 (void) p;
1300#endif
1301 return;
1302}
1303
1304static void prep_struct(sh_filestore_t * p, file_type * buf, char * fileHash)
1305{
1306#if !defined(__linux__) && !defined(HAVE_STAT_FLAGS)
1307 int i;
1308#endif
1309 p->mark = REC_MAGIC;
1310 sl_strlcpy(p->c_mode, buf->c_mode, CMODE_SIZE);
1311 sl_strlcpy(p->c_group, buf->c_group, GROUP_MAX+1);
1312 sl_strlcpy(p->c_owner, buf->c_owner, USER_MAX+1);
1313 if (fileHash) {
1314 sl_strlcpy(p->checksum, fileHash, KEY_LEN+1);
1315 }
1316#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
1317 sl_strlcpy(p->c_attributes, buf->c_attributes, ATTRBUF_SIZE);
1318#else
1319 for (i = 0; i < ATTRBUF_USED; ++i) p->c_attributes[i] = '-';
1320 p->c_attributes[ATTRBUF_USED] = '\0';
1321#endif
1322
1323 prep_encode(p);
1324
1325#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
1326 p->attributes = (UINT32) buf->attributes;
1327#else
1328 p->attributes = 0;
1329#endif
1330 p->linkmode = (UINT32) buf->linkmode;
1331 p->hardlinks = (UINT32) buf->hardlinks;
1332 p->dev = (UINT64) buf->dev;
1333 p->rdev = (UINT64) buf->rdev;
1334 p->mode = (UINT32) buf->mode;
1335 p->ino = (UINT32) buf->ino;
1336 p->size = (UINT64) buf->size;
1337 p->mtime = (UINT64) buf->mtime;
1338 p->atime = (UINT64) buf->atime;
1339 p->ctime = (UINT64) buf->ctime;
1340 p->owner = (UINT32) buf->owner;
1341 p->group = (UINT32) buf->group;
1342
1343 p->checkflags = (UINT32) buf->check_flags;
1344
1345 return;
1346}
1347
1348
1349static void write_start_header(SL_TICKET fd)
1350{
1351 char timestring[81];
1352
1353 if (pushdata_stdout == S_FALSE)
1354 {
1355 sl_write (fd, _("\n#Host "), 7);
1356 sl_write (fd, sh.host.name,
1357 sl_strlen(sh.host.name));
1358 sl_write (fd, _(" Version "), 9);
1359 sl_write (fd, sh_db_version_string,
1360 sl_strlen(sh_db_version_string));
1361 sl_write (fd, _(" Date "), 6);
1362 (void) sh_unix_time(0, timestring, sizeof(timestring));
1363 sl_write (fd, timestring, strlen(timestring));
1364 sl_write (fd, "\n", 1);
1365 }
1366 else
1367 {
1368 printf ("%s",_("\n#Host "));
1369 printf ("%s", sh.host.name);
1370 printf ("%s",_(" Version "));
1371 printf ("%s", sh_db_version_string);
1372 printf ("%s",_(" Date "));
1373 (void) sh_unix_time(0, timestring, sizeof(timestring));
1374 printf ("%s\n", timestring);
1375 }
1376}
1377
1378static void write_start_marker(SL_TICKET fd)
1379{
1380 if (sh_db_version_string != NULL)
1381 {
1382 write_start_header(fd);
1383 }
1384
1385 if (pushdata_stdout == S_FALSE)
1386 {
1387#if defined(SH_STEALTH)
1388 sl_write (fd, "\n", 1);
1389 sl_write_line (fd, N_("[SOF]"), 5);
1390#else
1391 sl_write_line (fd, _("\n[SOF]"), 6);
1392#endif
1393 }
1394 else
1395 {
1396#if defined(SH_STEALTH)
1397 puts (N_("[SOF]"));
1398#else
1399 puts (_("\n[SOF]"));
1400#endif
1401 }
1402}
1403
1404static void write_record(SL_TICKET fd, sh_filestore_t * p,
1405 char * fullpath, char * linkpath, char * attr_string)
1406{
1407 static char ll[2] = { '-', '\0' };
1408 char * lpath;
1409
1410 if (!linkpath || 0 == sl_strlen(linkpath))
1411 lpath = ll;
1412 else
1413 lpath = linkpath;
1414
1415 if (pushdata_stdout == S_FALSE)
1416 {
1417 if (SL_ENONE != sl_write (fd, p, sizeof(sh_filestore_t)))
1418 {
1419 char * tmp = sh_util_safe_name(fullpath);
1420 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
1421 _("Failed to write record to baseline database"),
1422 _("write_record"),
1423 tmp);
1424 SH_FREE(tmp);
1425 aud_exit(FIL__, __LINE__, EXIT_FAILURE );
1426 }
1427 if (SL_ENONE != sl_write_line_fast (fd, fullpath, sl_strlen(fullpath)))
1428 {
1429 char * tmp = sh_util_safe_name(fullpath);
1430 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
1431 _("Failed to write path to baseline database"),
1432 _("write_record"),
1433 tmp);
1434 SH_FREE(tmp);
1435 aud_exit(FIL__, __LINE__, EXIT_FAILURE );
1436 }
1437 if (SL_ENONE != sl_write_line_fast (fd, lpath, sl_strlen(lpath)))
1438 {
1439 char * tmp = sh_util_safe_name(fullpath);
1440 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
1441 _("Failed to write lpath to baseline database"),
1442 _("write_record"),
1443 tmp);
1444 SH_FREE(tmp);
1445 aud_exit(FIL__, __LINE__, EXIT_FAILURE );
1446 }
1447 if (attr_string)
1448 sl_write_line_fast (fd, attr_string, sl_strlen(attr_string));
1449 }
1450 else
1451 {
1452 if (fwrite (p, sizeof(sh_filestore_t), 1, stdout))
1453 {
1454 puts (fullpath);
1455 puts (lpath);
1456 if (attr_string)
1457 puts (attr_string);
1458 }
1459 else
1460 {
1461 perror(_("Error writing database"));
1462 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1463 }
1464 }
1465
1466 SH_FREE(fullpath);
1467 if (linkpath)
1468 SH_FREE(linkpath);
1469 if (attr_string)
1470 SH_FREE(attr_string);
1471
1472 return;
1473}
1474
1475static void sh_dbIO_data_write_int (file_type * buf, char * fileHash,
1476 const char * outpath, int truncate)
1477{
1478 static long p_count = 0;
1479 sh_filestore_t p;
1480 char * fullpath = NULL;
1481 char * linkpath = NULL;
1482 char * attr_string = NULL;
1483
1484 SL_ENTER(_("sh_dbIO_data_write_int"));
1485
1486 do_writeout_checks(outpath);
1487
1488 if (sh.flag.update == S_FALSE)
1489 {
1490 if (pushdata_stdout == S_FALSE && pushdata_fd == -1)
1491 {
1492 if (truncate == S_TRUE)
1493 pushdata_fd = open_writeout_data_truncate(outpath);
1494 else
1495 {
1496 pushdata_fd = open_writeout_data(outpath);
1497 /* Seek to eof */
1498 seek_writeout_data(pushdata_fd, outpath);
1499 }
1500 }
1501 }
1502 else /* update == TRUE */
1503 {
1504 if (pushdata_isfirst == 1)
1505 {
1506 TPT((0, FIL__, __LINE__, _("msg=<Update.>\n")));
1507 pushdata_fd = open_writeout_data(outpath);
1508 /* Seek to sof and truncate */
1509 seek_writeout_data_old(pushdata_fd, outpath);
1510 }
1511 }
1512
1513 if (!buf) {
1514 memset(&p, 0, sizeof(sh_filestore_t));
1515 }
1516
1517 if (buf != NULL)
1518 {
1519 fullpath = prep_path(buf->fullpath, S_TRUE);
1520 }
1521
1522 /* NOTE: TXT entries are c_mode[0] != 'l' and do not get decoded
1523 */
1524 if (buf != NULL /* && buf->c_mode[0] == 'l' */ && buf->link_path != NULL)
1525 {
1526 if (buf->c_mode[0] == 'l')
1527 linkpath = prep_path(buf->link_path, S_TRUE);
1528 else
1529 linkpath = prep_path(buf->link_path, S_FALSE);
1530 }
1531
1532 if (buf != NULL && buf->attr_string != NULL)
1533 {
1534 attr_string = prep_attr(buf->attr_string);
1535 }
1536
1537 if (buf != NULL)
1538 {
1539 prep_struct(&p, buf, fileHash);
1540 if (attr_string)
1541 p.mark |= REC_FLAGS_ATTR;
1542 swap_data(&p);
1543 }
1544
1545 /* write the start marker
1546 */
1547 if (pushdata_isfirst == 1)
1548 {
1549 if (sh.flag.update == S_FALSE)
1550 write_start_marker(pushdata_fd);
1551 pushdata_isfirst = 0;
1552 }
1553
1554 if (buf && fullpath)
1555 {
1556 write_record(pushdata_fd, &p, fullpath, linkpath, attr_string);
1557 ++p_count;
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.