author | Michael Krelin <hacker@klever.net> | 2013-02-13 22:43:26 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2013-02-13 22:43:26 (UTC) |
commit | 2a5950df692f8a2c9d51a2a3e0c4d41fe0d3e7c9 (patch) (side-by-side diff) | |
tree | 9c8a49a1bd96177abdc9cd13435b66a3aea783e2 | |
parent | d6545bd95153a5e41cdae441643f4e4a0af94a49 (diff) | |
download | iii-2a5950df692f8a2c9d51a2a3e0c4d41fe0d3e7c9.zip iii-2a5950df692f8a2c9d51a2a3e0c4d41fe0d3e7c9.tar.gz iii-2a5950df692f8a2c9d51a2a3e0c4d41fe0d3e7c9.tar.bz2 |
stream mime attachments directly to disk
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | src/eyefiworker.cc | 135 | ||||
-rw-r--r-- | src/eyefiworker.h | 19 | ||||
-rw-r--r-- | src/eyetil.cc | 4 | ||||
-rw-r--r-- | src/eyetil.h | 2 |
4 files changed, 90 insertions, 70 deletions
diff --git a/src/eyefiworker.cc b/src/eyefiworker.cc index 645069e..597a3f2 100644 --- a/src/eyefiworker.cc +++ b/src/eyefiworker.cc @@ -1,341 +1,344 @@ #include <signal.h> #ifndef NDEBUG # include <sys/resource.h> #endif #include <syslog.h> #include <cassert> #include <iostream> #include <fstream> #include <stdexcept> #include <iterator> #include <algorithm> #include <sys/wait.h> #include <autosprintf.h> #include "eyekinfig.h" #include "eyetil.h" #include "eyefiworker.h" #ifdef HAVE_SQLITE # include "iiidb.h" #endif eyefiworker::eyefiworker() : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) { bind_flags = SO_REUSEADDR; max_keep_alive = 0; socket_flags = #if defined(MSG_NOSIGNAL) MSG_NOSIGNAL #elif defined(SO_NOSIGPIPE) SO_NOSIGPIPE #else #error Something is wrong with sigpipe prevention on the platform #endif ; #ifdef HAVE_SQLITE sqlite3_initialize(); #endif } -eyefiworker::~eyefiworker() { + +static void *fmimewriteopen_(struct soap *soap, + void *handle, const char *id, const char *type, const char *description, + enum soap_mime_encoding encoding) { + return static_cast<eyefiworker*>(soap)->mime_writeopen(handle,id,type,description,encoding); +} +static int fmimewrite_(struct soap *soap,void *handle,const char *buf,size_t len) { + return static_cast<eyefiworker*>(soap)->mime_write(handle,buf,len); +} +static void fmimewriteclose_(struct soap *soap,void *handle) { + static_cast<eyefiworker*>(soap)->mime_writeclose(handle); } int eyefiworker::run(int bindport) { if(!soap_valid_socket(bind(0,bindport,64))) throw std::runtime_error("failed to bind()"); signal(SIGCHLD,SIG_IGN); + fmimewriteopen=fmimewriteopen_; fmimewrite=fmimewrite_; fmimewriteclose=fmimewriteclose_; while(true) { if(!soap_valid_socket(accept())) throw std::runtime_error("failed to accept()"); pid_t p = fork(); if(p<0) throw std::runtime_error("failed to fork()"); if(!p) { recv_timeout = 600; send_timeout = 120; (void)serve(); soap_destroy(this); soap_end(this); soap_done(this); #ifndef NDEBUG struct rusage ru; if(getrusage(RUSAGE_SELF,&ru)) { syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno); }else{ syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss); } #endif /* NDEBUG */ throw throwable_exit(0); } close(socket); socket = SOAP_INVALID_SOCKET; } } static binary_t session_nonce; #ifdef HAVE_SQLITE static struct { std::string filesignature; long filesize; std::string filename; inline void reset() { filesignature.erase(); filename.erase(); filesize=0; } inline void set(const std::string n,const std::string sig,long siz) { filename = n; filesignature = sig; filesize = siz; } inline bool is(const std::string n,const std::string sig,long siz) { return filesize==siz && filename==n && filesignature==sig; } } already; #endif /* HAVE_SQLITE */ static bool detached_child() { pid_t p = fork(); if(p<0) { syslog(LOG_ERR,"Failed to fork away for hook execution"); _exit(-1); } if(!p) { setsid(); for(int i=getdtablesize();i>=0;--i) close(i); int i=open("/dev/null",O_RDWR); assert(i==0); i = dup(i); assert(i==1); i = dup(i); assert(i==2); return true; } return false; } static int E(eyefiworker* efs,const char *c,const std::exception& e) { efs->keep_alive=0; syslog(LOG_ERR,"error while processing %s: %s",c,e.what()); return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0); } int eyefiworker::StartSession( std::string macaddress,std::string cnonce, int transfermode,long transfermodetimestamp, struct rns__StartSessionResponse &r ) try { syslog(LOG_INFO, "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); - eyekinfig_t eyekinfig(macaddress); - r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); + kinfig.reset(new eyekinfig_t(macaddress)); + umask(kinfig->get_umask()); + + r.credential = binary_t(macaddress+cnonce+kinfig->get_upload_key()).md5().hex(); r.snonce = session_nonce.make_nonce().hex(); r.transfermode=transfermode; r.transfermodetimestamp=transfermodetimestamp; r.upsyncallowed=false; - std::string cmd = eyekinfig.get_on_start_session(); + std::string cmd = kinfig->get_on_start_session(); if(!cmd.empty()) { if(detached_child()) { putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; execv("/bin/sh",argv); syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); _exit(-1); } } return SOAP_OK; }catch(const std::exception& e) { return E(this,"StartSession",e); } int eyefiworker::GetPhotoStatus( std::string credential, std::string macaddress, std::string filename, long filesize, std::string filesignature, int flags, struct rns__GetPhotoStatusResponse &r ) try { syslog(LOG_INFO, "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, session_nonce.hex().c_str() ); - eyekinfig_t eyekinfig(macaddress); - std::string computed_credential = binary_t(macaddress+eyekinfig.get_upload_key()+session_nonce.hex()).md5().hex(); + if(!(kinfig && kinfig->macaddress==macaddress)) + throw std::runtime_error("I'm not talking to this peer"); + + std::string computed_credential = binary_t(macaddress+kinfig->get_upload_key()+session_nonce.hex()).md5().hex(); #ifndef NDEBUG syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); #endif if (credential != computed_credential) throw std::runtime_error("card authentication failed"); + indir.reset(new tmpdir_t(kinfig->get_targetdir()+"/.incoming.XXXXXX")); + #ifdef HAVE_SQLITE - iiidb_t D(eyekinfig); + iiidb_t D(*kinfig); seclude::stmt_t S = D.prepare( "SELECT fileid FROM photo" " WHERE mac=:mac AND filename=:filename" " AND filesize=:filesize AND filesignature=:filesignature" ).bind(":mac",macaddress) .bind(":filename",filename).bind(":filesize",filesize) .bind(":filesignature",filesignature); if(!S.step()) { r.fileid = 1; r.offset = 0; }else{ r.fileid = S.column<long>(0); r.offset = filesize; already.set(filename,filesignature,filesize); } #else /* HAVE_SQLITE */ r.fileid=1, r.offset=0; #endif /* HAVE_SQLITE */ return SOAP_OK; }catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } int eyefiworker::MarkLastPhotoInRoll( std::string macaddress, int mergedelta, struct rns__MarkLastPhotoInRollResponse&/* r */ ) try { syslog(LOG_INFO, "MarkLastPhotoInRoll request from %s with mergedelta=%d", macaddress.c_str(), mergedelta ); - std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); + if(!(kinfig && kinfig->macaddress==macaddress)) + throw std::runtime_error("I'm not talking to this peer"); + + std::string cmd = kinfig->get_on_mark_last_photo_in_roll(); if(!cmd.empty()) { if(detached_child()) { putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; execv("/bin/sh",argv); syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); _exit(-1); } } keep_alive = 0; return SOAP_OK; }catch(const std::exception& e) { return E(this,"MarkLastPhotoInRoll",e); } +void *eyefiworker::mime_writeopen(void *handle,const char *id,const char *type,const char *description, + enum soap_mime_encoding encoding) { + if(!id) return NULL; + if(!strcmp(id,"FILENAME")) { + mime_tarfile.reset(new mimewrite_tarfile(*indir)); + return mime_tarfile.get(); + }else if(!strcmp(id,"INTEGRITYDIGEST")) { + mime_idigest.reset(new mimewrite_string()); + return mime_idigest.get(); + } + return NULL; +} +int eyefiworker::mime_write(void *handle,const char *buf,size_t len) { + if(!handle) return SOAP_ERR; + return static_cast<mimewrite_base*>(handle)->write(buf,len); +} +void eyefiworker::mime_writeclose(void *handle) { + if(handle) static_cast<mimewrite_base*>(handle)->close(); +} + int eyefiworker::UploadPhoto( int fileid, std::string macaddress, std::string filename, long filesize, std::string filesignature, std::string encryption, int flags, struct rns__UploadPhotoResponse& r ) try { syslog(LOG_INFO, "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," " filesignature=%s, encryption=%s, flags=%04X", macaddress.c_str(), fileid, filename.c_str(), filesize, filesignature.c_str(), encryption.c_str(), flags ); + if(!(kinfig && kinfig->macaddress==macaddress)) + throw std::runtime_error("I'm not talking to this peer"); + std::string::size_type fnl=filename.length(); if(fnl<sizeof(".tar") || strncmp(filename.c_str()+fnl-sizeof(".tar")+sizeof(""),".tar",sizeof(".tar"))) throw std::runtime_error(gnu::autosprintf("honestly, I expected the tarball coming here, not '%s'",filename.c_str())); std::string the_file(filename,0,fnl-sizeof(".tar")+sizeof("")); std::string the_log = the_file+".log"; - eyekinfig_t eyekinfig(macaddress); - - umask(eyekinfig.get_umask()); + if(!indir) throw std::runtime_error("I haven't even created a directory!"); + shared_ptr<tmpdir_t> dir; dir.swap(indir); + if(!mime_tarfile) throw std::runtime_error("I haven't written the tarball!"); + shared_ptr<mimewrite_tarfile> file; file.swap(mime_tarfile); + if(!mime_idigest) throw std::runtime_error("I haven't seen the integrity digest!"); + shared_ptr<mimewrite_string> idigest; idigest.swap(mime_idigest); - std::string td = eyekinfig.get_targetdir(); - tmpdir_t indir(td+"/.incoming.XXXXXX"); - - std::string tf,lf; - binary_t digest, idigest; #ifdef HAVE_SQLITE - bool beenthere = false; -#endif - - for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { -#ifndef NDEBUG - syslog(LOG_DEBUG, - " MIME attachment with id=%s, type=%s, size=%ld", - (*i).id, (*i).type, (long)(*i).size ); -#endif - - if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { - std::string idigestr((*i).ptr,(*i).size); -#ifndef NDEBUG - syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); -#endif - idigest.from_hex(idigestr); - } - if( (*i).id && !strcmp((*i).id,"FILENAME") ) { - assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); -#ifdef III_SAVE_TARS - std::string tarfile = indir.get_file(filename); - { - std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); - } -#endif - - if(!tf.empty()) throw std::runtime_error("already seen tarball"); - if(!digest.empty()) throw std::runtime_error("already have integrity digest"); - digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); -#ifndef NDEBUG - syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); -#endif -#ifdef HAVE_SQLITE - if(!(*i).size) { + if(!file->f.tellg()) { if(!already.is(filename,filesignature,filesize)) throw std::runtime_error("got zero-length upload for unknown file"); - beenthere = true; continue; + r.success = true; + return SOAP_OK; } #endif - tarchive_t a((*i).ptr,(*i).size); - while(a.read_next_header()) { - std::string ep = a.entry_pathname(), f = indir.get_file(ep); + if(idigest->str != file->idigest.final(kinfig->get_upload_key()).hex()) + throw std::runtime_error("Integrity digest doesn't match, disintegrating."); + + std::string tf, lf; + for(tarchive_t a(file->fn.c_str());a.read_next_header();) { + std::string ep = a.entry_pathname(), f = dir->get_file(ep); if(ep==the_file) tf = f; else if(ep==the_log) lf = f; else continue; int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666); if(fd<0) throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str())); if(!a.read_data_into_fd(fd)) throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); close(fd); } - } - } - -#ifdef HAVE_SQLITE - if(beenthere) { - r.success=true; - return SOAP_OK; - } -#endif if(tf.empty()) throw std::runtime_error("haven't seen THE file"); - if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); std::string::size_type ls = tf.rfind('/'); // XXX: actually, lack of '/' signifies error here std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); ls = lf.rfind('/'); std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); std::string ttf,tlf; bool success = false; + std::string td = kinfig->get_targetdir(); for(int i=0;i<32767;++i) { const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s"; ttf = (const char*)gnu::autosprintf(fmt,td.c_str(),tbn.c_str(),i); if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i); if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty() || !link(lf.c_str(),tlf.c_str())) ) { unlink(tf.c_str()); if(!lf.empty()) unlink(lf.c_str()); success=true; break; } } - std::string cmd = eyekinfig.get_on_upload_photo(); + std::string cmd = kinfig->get_on_upload_photo(); if(success) { #ifdef HAVE_SQLITE { - iiidb_t D(eyekinfig); + iiidb_t D(*kinfig); D.prepare( "INSERT INTO photo" " (ctime,mac,fileid,filename,filesize,filesignature,encryption,flags)" " VALUES" " (:ctime,:mac,:fileid,:filename,:filesize,:filesignature,:encryption,:flags)" ).bind(":ctime",time(0)) .bind(":mac",macaddress) .bind(":fileid",fileid).bind(":filename",filename) .bind(":filesize",filesize).bind(":filesignature",filesignature) .bind(":encryption",encryption).bind(":flags",flags) .step(); } #endif /* HAVE_SQLITE */ if((!cmd.empty()) && detached_child()) { putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) ); if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) ); char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; execv("/bin/sh",argv); syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); _exit(-1); } } r.success = true; return SOAP_OK; }catch(const std::exception& e) { return E(this,"UploadPhoto",e); } diff --git a/src/eyefiworker.h b/src/eyefiworker.h index 6cdecff..61d92d2 100644 --- a/src/eyefiworker.h +++ b/src/eyefiworker.h @@ -1,30 +1,47 @@ #ifndef __EYEFIWORKER_H #define __EYEFIWORKER_H +#include <tr1/memory> +using std::tr1::shared_ptr; + #include "soapeyefiService.h" +#include "eyekinfig.h" +#include "eyetil.h" + +struct eyefi_session; + class eyefiworker : public eyefiService { public: + shared_ptr<eyekinfig_t> kinfig; + shared_ptr<tmpdir_t> indir; + shared_ptr<mimewrite_tarfile> mime_tarfile; + shared_ptr<mimewrite_string> mime_idigest; eyefiworker(); - ~eyefiworker(); + ~eyefiworker() { } int run(int port) __attribute__ ((noreturn)); int StartSession(std::string macaddress, std::string cnonce, int transfermode, long transfermodetimestamp, struct rns__StartSessionResponse &r); int GetPhotoStatus(std::string credential, std::string macaddress, std::string filename, long filesize, std::string filesignature, int flags, struct rns__GetPhotoStatusResponse &r); int MarkLastPhotoInRoll(std::string macaddress, int mergedelta, struct rns__MarkLastPhotoInRollResponse &r); int UploadPhoto(int fileid, std::string macaddress, std::string filename, long filesize, std::string filesignature, std::string encryption, int flags, struct rns__UploadPhotoResponse &r); + void *mime_writeopen(void *handle,const char *id,const char *type,const char *description, + enum soap_mime_encoding encoding); + int mime_write(void *handle,const char *buf,size_t len); + void mime_writeclose(void *handle); + eyefiService *copy() { throw std::logic_error("Not meant to be called"); } }; #endif /* __EYEFIWORKER_H */ diff --git a/src/eyetil.cc b/src/eyetil.cc index fba8724..5bceec7 100644 --- a/src/eyetil.cc +++ b/src/eyetil.cc @@ -1,203 +1,203 @@ #include <stdlib.h> #include <sys/stat.h> #include <syslog.h> #include <iostream> #include <cassert> #include <stdexcept> #include <algorithm> #include <numeric> #include <openssl/md5.h> #include "eyetil.h" #include "config.h" #ifdef HAVE_LIBUUID # include <uuid/uuid.h> #endif binary_t& binary_t::from_hex(const std::string& h) { std::string::size_type hs = h.length(); if(hs&1) throw std::runtime_error("odd number of characters in hexadecimal number"); size_t rvs = hs>>1; resize(rvs); const unsigned char *hp = (const unsigned char*)h.data(); iterator oi=begin(); char t[3] = { 0,0,0 }; for(size_t i=0;i<rvs;++i) { t[0]=*(hp++); t[1]=*(hp++); *(oi++) = static_cast<binary_t::value_type>(0xff&strtol(t,0,16)); } return *this; } binary_t& binary_t::from_data(const void *d,size_t s) { resize(s); std::copy((const unsigned char*)d,(const unsigned char *)d+s, begin() ); return *this; } binary_t& binary_t::make_nonce() { #ifdef HAVE_LIBUUID uuid_t uuid; uuid_generate(uuid); from_data((unsigned char*)uuid,sizeof(uuid)); #else resize(16); std::generate_n(begin(),16,rand); #endif /* HAVE_LIBUUID */ return *this; } std::string binary_t::hex() const { std::string rv; rv.reserve((size()<<1)+1); char t[3] = {0,0,0}; for(const_iterator i=begin(),ie=end();i!=ie;++i) { size_t rc = snprintf(t,sizeof(t),"%02x",*i); assert(rc<sizeof(t)); rv += t; } return rv; } binary_t binary_t::md5() const { binary_t rv(MD5_DIGEST_LENGTH); if(!MD5( (const unsigned char*)&(front()),size(), (unsigned char*)&(rv.front()) )) throw std::runtime_error("failed to md5()"); return rv; } void md5_digester::init() { if(!MD5_Init(&ctx)) throw std::runtime_error("failed to MD5_Init()"); } void md5_digester::update(const void *d,size_t l) { if(!MD5_Update(&ctx,d,l)) throw std::runtime_error("failed to MD5_Update()"); } binary_t md5_digester::final() { binary_t rv(MD5_DIGEST_LENGTH); if(!MD5_Final((unsigned char*)&(rv.front()), &ctx)) throw std::runtime_error("failed to MD5_Final()"); return rv; } uint16_t block512_t::tcpcksum(block512_t& data) { uint32_t sum = std::accumulate(data.data,data.data+words,0); while(uint32_t hw = sum>>16) sum = (sum&0xffff)+hw; return 0xffff&~sum; } void integrity_digester::update(const void *d_,size_t s) { uint8_t *d=(uint8_t*)d_; if(data_size) { int l = sizeof(data)-data_size; if(l>s) { memmove(data.dptr(data_size),d,s); data_size+=s; return; } memmove(data.dptr(data_size),d,l); d+=l; s-=l; md5.update<uint16_t>( data.tcpcksum(data) ); } if(s<sizeof(data)) { memmove(data.dptr(0),d,s); data_size=s; return; } size_t bb=s/sizeof(block512_t); std::transform((block512_t*)d,((block512_t*)d)+bb, md5.updater<uint16_t>(),block512_t::tcpcksum); size_t ss=bb*sizeof(block512_t); d+=ss; s-=ss; assert(s<sizeof(block512_t)); if(s) memmove(data.dptr(0),d,data_size=s); } binary_t integrity_digester::final(const std::string& ukey) { assert(!data_size); md5.update( binary_t(ukey) ); return md5.final(); } static void make_path_for_template(const std::string& p,mode_t m) { struct stat st; std::string pp; for(std::string::size_type sl=p.find('/',1); sl!=std::string::npos; sl=p.find('/',sl+1)) { if(stat( (pp=p.substr(0,sl)).c_str() ,&st) || !S_ISDIR(st.st_mode)) { if(mkdir(pp.c_str(),m)) throw std::runtime_error("failed to mkdir()"); } } } tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { make_path_for_template(dt,0777); if(!mkdtemp((char*)dir.data())) throw std::runtime_error("failed to mkdtmp()"); } tmpdir_t::~tmpdir_t() { assert(!dir.empty()); if(rmdir(dir.c_str())) { syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str()); } } std::string tmpdir_t::get_file(const std::string& f) { std::string::size_type ls = f.rfind('/'); return dir+'/'+( (ls==std::string::npos) ? f : f.substr(ls+1) ); } -tarchive_t::tarchive_t(void *p,size_t s) : a(archive_read_new()), e(0) { +tarchive_t::tarchive_t(const char *fn) : a(archive_read_new()), e(0) { if(!a) throw std::runtime_error("failed to archive_read_new()"); if(archive_read_support_format_tar(a)) { archive_read_finish(a); throw std::runtime_error("failed to archive_read_support_format_tar()"); } - if(archive_read_open_memory(a,p,s)) { + if(archive_read_open_filename(a,fn,16384)) { archive_read_finish(a); throw std::runtime_error("failed to archive_read_open_memory()"); } } tarchive_t::~tarchive_t() { assert(a); archive_read_finish(a); } bool tarchive_t::read_next_header() { assert(a); return archive_read_next_header(a,&e)==ARCHIVE_OK; } std::string tarchive_t::entry_pathname() { assert(a); assert(e); return archive_entry_pathname(e); } bool tarchive_t::read_data_into_fd(int fd) { assert(a); return archive_read_data_into_fd(a,fd)==ARCHIVE_OK; } binary_t integrity_digest(const void *ptr,size_t size,const std::string& ukey) { md5_digester rv; std::transform( (block512_t*)ptr, ((block512_t*)ptr)+size/sizeof(block512_t), rv.updater<uint16_t>(), block512_t::tcpcksum ); rv.update( binary_t(ukey) ); return rv.final(); } mimewrite_tarfile::mimewrite_tarfile(tmpdir_t& d) { f.open((fn=d.get_file("the-tarfile.tar")).c_str(),std::ios_base::in|std::ios_base::out|std::ios_base::trunc|std::ios_base::binary); } mimewrite_tarfile::~mimewrite_tarfile() { unlink(fn.c_str()); } int mimewrite_tarfile::write(const char *buf,size_t len) { return f.write(buf,len) ? (idigest.update(buf,len),SOAP_OK) : SOAP_ERR; } diff --git a/src/eyetil.h b/src/eyetil.h index 64948d0..8af18a4 100644 --- a/src/eyetil.h +++ b/src/eyetil.h @@ -1,138 +1,138 @@ #ifndef __EYETIL_H #define __EYETIL_H #include <vector> #include <string> #include <fstream> #include <archive.h> #include <archive_entry.h> #include "openssl/md5.h" #include "soapH.h" struct throwable_exit { int rc; throwable_exit(int rc_) : rc(rc_) { } }; class binary_t : public std::vector<unsigned char> { public: binary_t() { } binary_t(size_type n) : std::vector<unsigned char>(n) { } binary_t(const std::string& h) { from_hex(h); } binary_t(const void *d,size_t s) { from_data(d,s); } binary_t& from_hex(const std::string& h); binary_t& from_data(const void *d,size_t s); binary_t& make_nonce(); std::string hex() const; binary_t md5() const; }; struct md5_digester { MD5_CTX ctx; md5_digester() { init(); } void init(); void update(const void *d,size_t l); binary_t final(); template<typename T> void update(const T& x) { update(&x,sizeof(x)); } template<typename T> struct update_iterator : public std::iterator<std::output_iterator_tag,T,void,T*,T&> { md5_digester *d; update_iterator(md5_digester *d_) : d(d_) { } update_iterator(const update_iterator& x) : d(x.d) { } update_iterator& operator*() { return *this; } update_iterator& operator++() { return *this; } update_iterator& operator++(int) { return *this; } update_iterator& operator=(const T& x) { d->update(x); return *this; } }; template<typename T> update_iterator<T> updater() { return update_iterator<T>(this); } }; template<> inline void md5_digester::update<binary_t>(const binary_t& x) { update((const unsigned char*)&(x.front()),x.size()); } #pragma pack(1) struct block512_t { enum { words = 512 / sizeof(uint16_t) }; uint16_t data[words]; inline uint8_t *dptr(size_t o) { return ((uint8_t*)this)+o; } static uint16_t tcpcksum(block512_t& data); }; #pragma pack() struct integrity_digester { md5_digester md5; size_t data_size; block512_t data; integrity_digester() : data_size(0) { } void update(const void *d,size_t s); binary_t final(const std::string& ukey); }; class tmpdir_t { public: std::string dir; tmpdir_t(const std::string& dt); ~tmpdir_t(); std::string get_file(const std::string& f); }; class tarchive_t { public: struct archive *a; struct archive_entry *e; - tarchive_t(void *p,size_t s); + tarchive_t(const char *); ~tarchive_t(); bool read_next_header(); std::string entry_pathname(); bool read_data_into_fd(int fd); }; struct mimewrite_base { virtual ~mimewrite_base() { } virtual int write(const char *buf,size_t len) = 0; virtual void close() = 0; }; struct mimewrite_string : public mimewrite_base { std::string str; int write(const char *buf,size_t len) { str.append(buf,len); return SOAP_OK; }; void close() { } }; struct mimewrite_tarfile : public mimewrite_base { std::string fn; std::fstream f; integrity_digester idigest; mimewrite_tarfile(tmpdir_t& d); ~mimewrite_tarfile(); int write(const char *buf,size_t len); void close() { } }; binary_t integrity_digest(const void *ptr,size_t size, const std::string& ukey); #endif /* __EYETIL_H */ |