1 /*
2 * rsyncrypto - an rsync friendly encryption
3 * Copyright (C) 2008 Shachar Shemesh for Lingnu Open Source Consulting ltd.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * In addition, as a special exception, the rsyncrypto authors give permission
20 * to link the code of this program with the OpenSSL library (or with modified
21 * versions of OpenSSL that use the same license as OpenSSL), and distribute
22 * linked combinations including the two. You must obey the GNU General Public
23 * License in all respects for all of the code used other than OpenSSL. If you
24 * modify this file, you may extend this exception to your version of the file,
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
27 *
28 * The project's homepage is at http://rsyncrypto.lingnu.com/
29 */
30
31 // This is a quick and dirty implementation, designed to plug the corrupted
32 // files potentially generated by rsyncrypto version 1.07 and 1.08
33 // The implementation is neither clean nor complete, just "good enough"
34
35 #include <precomp.h>
36
usage()37 static void usage()
38 {
39 std::cerr<<"rsyncrypto_recover: Recover corrupted rsyncrypto file maps"<<std::endl<<
40 "Usage: rsyncrypto_recover filename > good_file"<<std::endl;
41 exit(0);
42 }
43
44 struct file_record {
45 char dirsep;
46 char crypt[32];
47 std::string plain;
48 };
49
main(int argc,char * argv[])50 int main( int argc, char *argv[] )
51 {
52 int ret=0;
53 if( argc!=2 )
54 usage();
55
56 try {
57 autofd fd(argv[1], O_RDONLY);
58 autommap map(fd, PROT_READ);
59
60 size_t offset=0;
61 enum {DIRSEP, CIPHERNAME, SPACE, PLAINNAME, INVALID, RSYNC107_INVALID} state=DIRSEP;
62 int line_state=0;
63 file_record record;
64
65 while( offset<map.getsize() ) {
66 unsigned char ch=map.get_uc()[offset];
67 switch( state ) {
68 case DIRSEP:
69 record.plain="";
70
71 // The dir separator may either be a slash or a backslash
72 if( ch=='/' || ch=='\\' ) {
73 record.dirsep=ch;
74 state=CIPHERNAME;
75 line_state=0;
76 } else if( ch=='\0' ) {
77 std::cerr<<"Offset "<<offset<<": Rsyncrypto 1.07 corrupted record - safetly discarded"<<std::endl;
78 } else if( ch==' ' ) {
79 state=RSYNC107_INVALID;
80 } else {
81 state=INVALID;
82 std::cerr<<"Offset "<<offset<<": Invalid dir separator char '"<<ch<<"'"<<std::endl;
83 }
84 break;
85 case CIPHERNAME:
86 if( (ch>='0' && ch<='9') || (ch>='A' && ch<='F') ) {
87 record.crypt[line_state]=ch;
88
89 // A legal hexadecimal character
90 if( (++line_state)==32 ) {
91 state=SPACE;
92 }
93 } else {
94 std::cerr<<"Offset "<<offset<<": Invalid character '"<<ch<<"' in crypt name area"<<std::endl;
95 state=INVALID;
96 }
97 break;
98 case SPACE:
99 if( ch==' ' )
100 state=PLAINNAME;
101 else {
102 std::cerr<<"Offset "<<offset<<": Got a '"<<ch<<"' instead of a space"<<std::endl;
103 state=INVALID;
104 }
105 break;
106 case PLAINNAME:
107 if( ch!='\0' ) {
108 record.plain+=ch;
109 } else {
110 if( record.plain.length()!=0 ) {
111 // Everything is ok - print the correct record
112 std::cout<<record.dirsep<<std::string(record.crypt,32)<<" "<<record.plain<<'\0';
113 std::cerr<<"Successfully recovered data for "<<record.plain<<std::endl;
114 } else {
115 std::cerr<<"Offset "<<offset<<": Plain name for cipher "<<std::string(record.crypt,32)<<" is empty"<<std::endl;
116 }
117
118 state=DIRSEP;
119 }
120 break;
121 case INVALID:
122 break;
123 case RSYNC107_INVALID:
124 if( ch=='\0' ) {
125 std::cerr<<"Offset "<<offset<<": Rsyncrypto 1.07 corrupted record - safetly discarded"<<std::endl;
126 state=DIRSEP;
127 } else {
128 state=INVALID;
129 std::cerr<<"Offset "<<offset-1<<": Invalid dir separator char ' '"<<std::endl;
130 }
131 break;
132 }
133
134 if( state==INVALID && ch=='\0' ) {
135 state=DIRSEP;
136 }
137
138 offset++;
139 }
140
141 if( state!=DIRSEP ) {
142 std::cerr<<"Last record is either corrupt or truncated"<<std::endl;
143 }
144 } catch( const rscerror &err ) {
145 std::cerr<<err.error()<<std::endl;
146 ret=1;
147 }
148
149 return ret;
150 }
151