From 955cde6bbb43f6854c07bcc773c2152d77ccfd26 Mon Sep 17 00:00:00 2001 From: Gavin Romig-Koch Date: Sat, 15 Aug 2009 11:19:03 -0400 Subject: add md5sum and encryption --- fastback.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 92 insertions(+), 18 deletions(-) diff --git a/fastback.cpp b/fastback.cpp index 49d8533..5377121 100644 --- a/fastback.cpp +++ b/fastback.cpp @@ -409,6 +409,30 @@ string ReadCommand(string cmd) return output_stream.str(); } +void WriteCommand(string cmd, string input) +{ + FILE* fp = popen(cmd.c_str(),"w"); + if (!fp) + { + Error("WriteCommand:", "error: could not start subshell: " + cmd); + } + + size_t input_length = input.length(); + size_t check = fwrite(input.c_str(),1,input_length,fp); + if (input_length != check) + { + Error("WriteCommand:", "error: could not send input to subshell: " + cmd); + } + + int retcode = pclose(fp); + if (retcode) + { + std::ostringstream msg; + msg << "error: subshell failed (rc=" << retcode << "):" << cmd; + Error("WriteCommand:", msg.str()); + } +} + static string rand_base64(int i) { // return a string of random base64 characters of 'i' length @@ -471,14 +495,29 @@ readable(string filename) static string filename_basename(const string& filename) { - return basename(filename.c_str()); + string::size_type p = filename.find_last_of('/'); + if (p == string::npos) + return filename; + else + return filename.substr(p+1,string::npos); +} + +static string +filename_dirname(const string& filename) +{ + string::size_type p = filename.find_last_of('/'); + if (p == string::npos) + return "."; + else + return filename.substr(0,p); } static string -create_temporary_file(string filename) +filename_temporary(string filename) { - // create a filename in a temporary directory - // for now you must make sure that the name is unique to this run + // ensure that a temporary directory (for this run) exists + // 'filename' must be the basename of a file + // 'filename' must be unique to this run if (fastback_tmpdir == "") { char TEMPLATE[] = "/tmp/fastbackXXXXXX"; @@ -562,35 +601,63 @@ main(int argc, char** argv) exit(3); } - string final_filename = fastback_filename; + string outfile_name = fastback_filename; - if (!is_compressed(final_filename)) + // compress the file if it isn't already + if (!is_compressed(outfile_name)) { string compressed_filename = - create_temporary_file(filename_basename(final_filename) + ".gz"); - compress(final_filename,compressed_filename); - final_filename = compressed_filename; + filename_temporary(filename_basename(outfile_name) + ".gz"); + compress(outfile_name,compressed_filename); + outfile_name = compressed_filename; + } + + // encrypt if requested + string key; + if (fastback_encrypt) + { + string cmd = string("openssl rand -base64 48"); + key = ReadCommand(cmd); + + string infile_name = outfile_name; + outfile_name = + filename_temporary(filename_basename(outfile_name) + ".aes"); + + cmd = string("openssl aes-128-cbc -in ") + infile_name + + " -out " + outfile_name + " -pass stdin"; + WriteCommand(cmd,key); } + + // generate md5sum + string cmd = string("md5sum ") + outfile_name; + string md5sum = ReadCommand(cmd); + + // upload the file CURL* handle; CURLcode curl_err; FILE* file; - string url; - file = fopen(final_filename.c_str(),"r"); + file = fopen(outfile_name.c_str(),"r"); if (!file) { string msg = fastback_name; msg += ": error: could not open: "; - msg += final_filename; + msg += outfile_name; perror(msg.c_str()); exit(3); } - url = fastback_URLDIR; + // randomize the remote file name + // and alter the md5sum output to contain the remote file name + string remotefile_name = randomize_filename(filename_basename(outfile_name.c_str())); + md5sum = md5sum.substr(0,md5sum.find_first_of(' ')) + " " + remotefile_name + '\n'; + + // create the full remote file name, 'url' + string url = fastback_URLDIR; if (url[url.length()-1] != '/') url += '/'; - url += randomize_filename(filename_basename(final_filename.c_str())); + url += remotefile_name; if (fastback_verbose) show(stdout,url); @@ -640,13 +707,20 @@ main(int argc, char** argv) fclose(file); if (fastback_ticket) - printf("Please paste this into %s:\n", fastback_ticket); + printf("Please copy this into %s:\n", fastback_ticket); else - printf("Please create a new ticket, and paste this into it:\n"); - printf("FASTBACK:\n"); + printf("Please send this to your technical support:\n"); + printf("FASTBACK: This report was sent to %s\n", filename_dirname(url).c_str()); if (fastback_ticket) printf("TICKET: %s\n", fastback_ticket); - printf("UPLOAD: %s\n", url.c_str()); + printf("FILE: %s\n", filename_basename(url).c_str()); + printf("MD5SUM:\n"); + printf("%s", md5sum.c_str()); + if (fastback_encrypt) + { + printf("KEY: aes-128-cbc\n"); + printf("%s", key.c_str()); + } printf("END:\n"); cleanup(); -- cgit