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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/*
* rsyncrypto - an rsync friendly encryption
* Copyright (C) 2005-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/
*/
#include <precomp.h>
#include "rsyncrypto.h"
#include "file.h"
#include "crypto.h"
#include "argtable2.h"
#include "filemap.h"
std::unique_ptr<std::ostream> changes_log;
void version()
{
printf("%s by Shachar Shemesh\n", PACKAGE_STRING);
printf("This program was developed as part of Lingnu Open Source Consulting's online\n"
"backup service. For further details check out http://www.lingnu.com\n");
}
void usage()
{
fprintf(stderr, "%s ", PACKAGE_STRING );
arg_print_syntax(stderr, options.argtable, "\n\n");
arg_print_glossary(stderr, options.argtable, "%-22s %s\n");
exit(0);
}
startup_options options;
void parse_cmdline( int argc, char *argv[] )
{
int nerrors=arg_parse( argc, argv, options.argtable );
if( nerrors!=0 ) {
if( !EXISTS(version) && !EXISTS(help) ) {
std::cerr<<"Incorrect arguments"<<std::endl;
usage();
} else {
// Do not complain about missing arguments if the user asked for --help or --version
return;
}
}
if( EXISTS(trim) && !EXISTS(recurse) && !EXISTS(filelist) )
throw rscerror("Cannot trim names when not doing directory recursion or filelist");
if( EXISTS(nameenc) && !EXISTS(recurse) && !EXISTS(filelist) )
throw rscerror("Cannot encrypt names when not doing directory recursion or filelist");
if( EXISTS(delkey) )
ARG(del).count=1;
// Some sanity check of the options
if( EXISTS(decrypt) ) {
if( EXISTS(keysize) )
throw rscerror("Cannot specify key size for decryption");
if( EXISTS(fr) || EXISTS(fk) )
throw rscerror("\"force\" options incompatible with -d option");
if( strcmp(FILENAME(src), "-")==0 && !EXISTS(filelist) ) {
// Plaintext file is standard input/output
if( !EXISTS(noarch) ) {
throw rscerror("Must use \"--no-archive-mode\" if plaintext file is stdin");
}
}
if( EXISTS(export_changes) ) {
throw rscerror("Log export not supported in decrypt mode");
}
}
// Apply default values
if( !EXISTS(rollsens) )
VAL(rollsens)=VAL(rollmin);
if( !EXISTS(noatime) )
VAL(noatime)=1;
if( !EXISTS(mod_win) )
VAL(mod_win)=0;
}
int main( int argc, char *argv[] )
{
int ret=0;
ERR_load_crypto_strings();
try {
parse_cmdline( argc, argv );
if( EXISTS(version) ) {
version();
exit(0);
}
if( EXISTS(help) )
usage();
#if HAVE_NOATIME
// Sometimes we can always use O_NOATIME without a problem
if( VAL(noatime)==1 && geteuid()==0 ) {
// We are root - O_NOATIME will succeed anyways
VAL(noatime)=2;
}
#endif
RSA *rsa_key=extract_private_key(FILENAME(master));
if( rsa_key==NULL ) {
rsa_key=extract_public_key(FILENAME(master));
if( rsa_key==NULL ) {
throw rscerror( "Couldn't parse RSA key", 0, FILENAME(master) );
}
}
if( EXISTS(export_changes) ) {
changes_log=std::unique_ptr<std::ofstream>(new std::ofstream(FILENAME(export_changes), std::ofstream::trunc));
}
const char *opname=NULL;
encryptfunc op;
namefunc srcnameop=name_concat, dstnameop=name_concat, keynameop=name_concat;
prefix_func prefix_op=cond_name_concat;
bool encrypt=true;
if( EXISTS(decrypt) ) {
op=file_decrypt;
prefix_op=left_only_concat;
opname="Decrypting";
encrypt=false;
} else {
op=file_encrypt;
opname="Encrypting";
}
if( EXISTS(nameenc) ) {
if( encrypt ) {
dstnameop=filemap::namecat_encrypt;
keynameop=dstnameop;
} else {
if( EXISTS(recurse) || EXISTS(filelist) ) {
// First decrypt the encrypted file map
file_decrypt(autofd::combine_paths(FILENAME(src), FILEMAPNAME).c_str(),
FILENAME(nameenc), autofd::combine_paths(FILENAME(key),
FILEMAPNAME).c_str(), rsa_key, NULL );
}
dstnameop=filemap::namecat_decrypt;
}
filemap::fill_map(FILENAME(nameenc), encrypt);
}
if( EXISTS(recurse) || EXISTS(filelist) ) {
if( EXISTS(filelist) )
filelist_encrypt( FILENAME(src), FILENAME(dst), FILENAME(key), rsa_key, op, opname,
prefix_op, srcnameop, dstnameop, keynameop);
else
dir_encrypt(FILENAME(src), FILENAME(dst), FILENAME(key), rsa_key, op, opname,
dstnameop, keynameop);
if( encrypt && EXISTS(nameenc) ) {
// Write the (possibly changed) filelist back to the file
filemap::write_map(FILENAME(nameenc));
// Encrypt the filelist file itself
file_encrypt(FILENAME(nameenc), autofd::combine_paths(FILENAME(dst), FILEMAPNAME).
c_str(), autofd::combine_paths(FILENAME(key), FILEMAPNAME).c_str(), rsa_key,
NULL );
}
} else {
struct stat status, *pstat=&status;
if( strcmp( FILENAME(src), "-" )!=0 ) {
status=autofd::stat(FILENAME(src));
} else
pstat=NULL;
op( FILENAME(src), FILENAME(dst), FILENAME(key), rsa_key, pstat );
}
} catch( const rscerror &err ) {
std::cerr<<err.error()<<std::endl;
ret=1;
}
return ret;
}
|