From 2a5950df692f8a2c9d51a2a3e0c4d41fe0d3e7c9 Mon Sep 17 00:00:00 2001 From: Michael Krelin Date: Wed, 13 Feb 2013 22:43:26 +0000 Subject: stream mime attachments directly to disk Signed-off-by: Michael Krelin --- diff --git a/src/eyefiworker.cc b/src/eyefiworker.cc index 645069e..597a3f2 100644 --- a/src/eyefiworker.cc +++ b/src/eyefiworker.cc @@ -34,13 +34,24 @@ eyefiworker::eyefiworker() 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(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(soap)->mime_write(handle,buf,len); +} +static void fmimewriteclose_(struct soap *soap,void *handle) { + static_cast(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()"); @@ -110,15 +121,17 @@ int eyefiworker::StartSession( 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()) ); @@ -143,8 +156,10 @@ int eyefiworker::GetPhotoStatus( 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()); @@ -152,8 +167,10 @@ int eyefiworker::GetPhotoStatus( 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" @@ -180,7 +197,10 @@ int eyefiworker::MarkLastPhotoInRoll( 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()) ); @@ -195,6 +215,26 @@ int eyefiworker::MarkLastPhotoInRoll( 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(handle)->write(buf,len); +} +void eyefiworker::mime_writeclose(void *handle) { + if(handle) static_cast(handle)->close(); +} + int eyefiworker::UploadPhoto( int fileid, std::string macaddress, std::string filename, long filesize, std::string filesignature, @@ -205,87 +245,49 @@ int eyefiworker::UploadPhoto( " 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 dir; dir.swap(indir); + if(!mime_tarfile) throw std::runtime_error("I haven't written the tarball!"); + shared_ptr file; file.swap(mime_tarfile); + if(!mime_idigest) throw std::runtime_error("I haven't seen the integrity digest!"); + shared_ptr idigest; idigest.swap(mime_idigest); - 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(!already.is(filename,filesignature,filesize)) - throw std::runtime_error("got zero-length upload for unknown file"); - beenthere = true; continue; - } + if(!file->f.tellg()) { + if(!already.is(filename,filesignature,filesize)) + throw std::runtime_error("got zero-length upload for unknown file"); + 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(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); - } - } - } + if(idigest->str != file->idigest.final(kinfig->get_upload_key()).hex()) + throw std::runtime_error("Integrity digest doesn't match, disintegrating."); -#ifdef HAVE_SQLITE - if(beenthere) { - r.success=true; - return SOAP_OK; + 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); } -#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 @@ -294,6 +296,7 @@ int eyefiworker::UploadPhoto( 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); @@ -305,11 +308,11 @@ int eyefiworker::UploadPhoto( 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)" diff --git a/src/eyefiworker.h b/src/eyefiworker.h index 6cdecff..61d92d2 100644 --- a/src/eyefiworker.h +++ b/src/eyefiworker.h @@ -1,13 +1,25 @@ #ifndef __EYEFIWORKER_H #define __EYEFIWORKER_H +#include +using std::tr1::shared_ptr; + #include "soapeyefiService.h" +#include "eyekinfig.h" +#include "eyetil.h" + +struct eyefi_session; + class eyefiworker : public eyefiService { public: + shared_ptr kinfig; + shared_ptr indir; + shared_ptr mime_tarfile; + shared_ptr mime_idigest; eyefiworker(); - ~eyefiworker(); + ~eyefiworker() { } int run(int port) __attribute__ ((noreturn)); @@ -24,6 +36,11 @@ class eyefiworker : public eyefiService { 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"); } }; diff --git a/src/eyetil.cc b/src/eyetil.cc index fba8724..5bceec7 100644 --- a/src/eyetil.cc +++ b/src/eyetil.cc @@ -152,13 +152,13 @@ std::string tmpdir_t::get_file(const std::string& f) { ); } -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()"); } diff --git a/src/eyetil.h b/src/eyetil.h index 64948d0..8af18a4 100644 --- a/src/eyetil.h +++ b/src/eyetil.h @@ -102,7 +102,7 @@ class tarchive_t { struct archive *a; struct archive_entry *e; - tarchive_t(void *p,size_t s); + tarchive_t(const char *); ~tarchive_t(); bool read_next_header(); -- cgit v0.9.0.2