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