1 /*
2 
3     File: file_fs.c
4 
5     Copyright (C) 2007 Christophe GRENIER <grenier@cgsecurity.org>
6 
7     This software is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write the Free Software Foundation, Inc., 51
19     Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29 #include <stdio.h>
30 #include "types.h"
31 #include "common.h"
32 #include "filegen.h"
33 #include "common.h"
34 #include "log.h"
35 
36 static void register_header_check_fs(file_stat_t *file_stat);
37 static int header_check_fs(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new);
38 
39 const file_hint_t file_hint_fs= {
40   .extension="fs",
41   .description="Zope",
42   .max_filesize=200*1024*1024,
43   .recover=1,
44   .enable_by_default=1,
45   .register_header_check=&register_header_check_fs
46 };
47 
48 /* See http://www.linkitsolutions.org/uploader/dilu/lib/python2.6/site-packages/ZODB/FileStorage/FileStorage.py for more information */
49 struct transaction_header
50 {
51   uint64_t id;
52   uint64_t len;
53   char     status;
54   uint16_t len_username;
55   uint16_t len_descr;
56   uint16_t len_ext;
57 } __attribute__ ((gcc_struct, __packed__));
58 
data_check_fs(const unsigned char * buffer,const unsigned int buffer_size,file_recovery_t * file_recovery)59 static data_check_t data_check_fs(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
60 {
61   while(file_recovery->calculated_file_size + buffer_size/2  >= file_recovery->file_size &&
62       file_recovery->calculated_file_size + 0x11 < file_recovery->file_size + buffer_size/2)
63   {
64     const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
65     const struct transaction_header *hdr=(const struct transaction_header *)&buffer[i];
66     const uint64_t len=be64(hdr->len);
67     if(len < sizeof(struct transaction_header)-8)
68       return DC_STOP;
69     if(hdr->status!=' ' && hdr->status!='p' && hdr->status!='c' &&  hdr->status!='u')
70       return DC_STOP;
71 #ifdef DEBUG_FS
72     log_info("0x%08llx len=%llu status=%c\n", (long long unsigned)file_recovery->calculated_file_size, (long long unsigned)len, hdr->status);
73 #endif
74     file_recovery->calculated_file_size+=(uint64_t)8+len;
75 #ifdef DEBUG_FS
76     log_info("0x%08llx\n", (long long unsigned)file_recovery->calculated_file_size);
77 #endif
78   }
79   return DC_CONTINUE;
80 }
81 
header_check_fs(const unsigned char * buffer,const unsigned int buffer_size,const unsigned int safe_header_only,const file_recovery_t * file_recovery,file_recovery_t * file_recovery_new)82 static int header_check_fs(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
83 {
84   const struct transaction_header *hdr=(const struct transaction_header *)&buffer[4];
85   const uint64_t len=be64(hdr->len);
86   if(len < sizeof(struct transaction_header)-8)
87     return 0;
88   if(hdr->status!=' ' && hdr->status!='p' && hdr->status!='c' &&  hdr->status!='u')
89     return 0;
90   reset_file_recovery(file_recovery_new);
91   file_recovery_new->extension=file_hint_fs.extension;
92   if(file_recovery_new->blocksize < 0x11)
93     return 1;
94   file_recovery_new->data_check=&data_check_fs;
95   file_recovery_new->file_check=&file_check_size;
96   file_recovery_new->calculated_file_size=4;
97   return 1;
98 }
99 
register_header_check_fs(file_stat_t * file_stat)100 static void register_header_check_fs(file_stat_t *file_stat)
101 {
102   static const unsigned char fs_header[4]={ 'F', 'S','2','1' };
103   register_header_check(0, fs_header,sizeof(fs_header), &header_check_fs, file_stat);
104 }
105