summaryrefslogtreecommitdiffabout
authorMichael Krelin <hacker@klever.net>2013-02-13 22:43:26 (UTC)
committer Michael Krelin <hacker@klever.net>2013-02-13 22:43:26 (UTC)
commit2a5950df692f8a2c9d51a2a3e0c4d41fe0d3e7c9 (patch) (unidiff)
tree9c8a49a1bd96177abdc9cd13435b66a3aea783e2
parentd6545bd95153a5e41cdae441643f4e4a0af94a49 (diff)
downloadiii-2a5950df692f8a2c9d51a2a3e0c4d41fe0d3e7c9.zip
iii-2a5950df692f8a2c9d51a2a3e0c4d41fe0d3e7c9.tar.gz
iii-2a5950df692f8a2c9d51a2a3e0c4d41fe0d3e7c9.tar.bz2
stream mime attachments directly to disk
Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyefiworker.cc157
-rw-r--r--src/eyefiworker.h19
-rw-r--r--src/eyetil.cc4
-rw-r--r--src/eyetil.h2
4 files changed, 101 insertions, 81 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 @@
1#include <signal.h> 1#include <signal.h>
2#ifndef NDEBUG 2#ifndef NDEBUG
3# include <sys/resource.h> 3# include <sys/resource.h>
4#endif 4#endif
5#include <syslog.h> 5#include <syslog.h>
6#include <cassert> 6#include <cassert>
7#include <iostream> 7#include <iostream>
8#include <fstream> 8#include <fstream>
9#include <stdexcept> 9#include <stdexcept>
10#include <iterator> 10#include <iterator>
11#include <algorithm> 11#include <algorithm>
12#include <sys/wait.h> 12#include <sys/wait.h>
13#include <autosprintf.h> 13#include <autosprintf.h>
14#include "eyekinfig.h" 14#include "eyekinfig.h"
15#include "eyetil.h" 15#include "eyetil.h"
16#include "eyefiworker.h" 16#include "eyefiworker.h"
17#ifdef HAVE_SQLITE 17#ifdef HAVE_SQLITE
18# include "iiidb.h" 18# include "iiidb.h"
19#endif 19#endif
20 20
21eyefiworker::eyefiworker() 21eyefiworker::eyefiworker()
22 : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) { 22 : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) {
23 bind_flags = SO_REUSEADDR; max_keep_alive = 0; 23 bind_flags = SO_REUSEADDR; max_keep_alive = 0;
24 socket_flags = 24 socket_flags =
25#if defined(MSG_NOSIGNAL) 25#if defined(MSG_NOSIGNAL)
26 MSG_NOSIGNAL 26 MSG_NOSIGNAL
27#elif defined(SO_NOSIGPIPE) 27#elif defined(SO_NOSIGPIPE)
28 SO_NOSIGPIPE 28 SO_NOSIGPIPE
29#else 29#else
30#error Something is wrong with sigpipe prevention on the platform 30#error Something is wrong with sigpipe prevention on the platform
31#endif 31#endif
32 ; 32 ;
33#ifdef HAVE_SQLITE 33#ifdef HAVE_SQLITE
34 sqlite3_initialize(); 34 sqlite3_initialize();
35#endif 35#endif
36 } 36 }
37eyefiworker::~eyefiworker() { 37
38static void *fmimewriteopen_(struct soap *soap,
39 void *handle, const char *id, const char *type, const char *description,
40 enum soap_mime_encoding encoding) {
41 return static_cast<eyefiworker*>(soap)->mime_writeopen(handle,id,type,description,encoding);
42}
43static int fmimewrite_(struct soap *soap,void *handle,const char *buf,size_t len) {
44 return static_cast<eyefiworker*>(soap)->mime_write(handle,buf,len);
45}
46static void fmimewriteclose_(struct soap *soap,void *handle) {
47 static_cast<eyefiworker*>(soap)->mime_writeclose(handle);
38} 48}
39 49
40int eyefiworker::run(int bindport) { 50int eyefiworker::run(int bindport) {
41 if(!soap_valid_socket(bind(0,bindport,64))) 51 if(!soap_valid_socket(bind(0,bindport,64)))
42 throw std::runtime_error("failed to bind()"); 52 throw std::runtime_error("failed to bind()");
43 signal(SIGCHLD,SIG_IGN); 53 signal(SIGCHLD,SIG_IGN);
54 fmimewriteopen=fmimewriteopen_; fmimewrite=fmimewrite_; fmimewriteclose=fmimewriteclose_;
44 while(true) { 55 while(true) {
45 if(!soap_valid_socket(accept())) 56 if(!soap_valid_socket(accept()))
46 throw std::runtime_error("failed to accept()"); 57 throw std::runtime_error("failed to accept()");
47 pid_t p = fork(); 58 pid_t p = fork();
48 if(p<0) throw std::runtime_error("failed to fork()"); 59 if(p<0) throw std::runtime_error("failed to fork()");
49 if(!p) { 60 if(!p) {
50 recv_timeout = 600; send_timeout = 120; 61 recv_timeout = 600; send_timeout = 120;
51 (void)serve(); 62 (void)serve();
52 soap_destroy(this); soap_end(this); soap_done(this); 63 soap_destroy(this); soap_end(this); soap_done(this);
53#ifndef NDEBUG 64#ifndef NDEBUG
54 struct rusage ru; 65 struct rusage ru;
55 if(getrusage(RUSAGE_SELF,&ru)) { 66 if(getrusage(RUSAGE_SELF,&ru)) {
56 syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno); 67 syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno);
57 }else{ 68 }else{
58 syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss); 69 syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss);
59 } 70 }
60#endif /* NDEBUG */ 71#endif /* NDEBUG */
61 throw throwable_exit(0); 72 throw throwable_exit(0);
62 } 73 }
63 close(socket); socket = SOAP_INVALID_SOCKET; 74 close(socket); socket = SOAP_INVALID_SOCKET;
64 } 75 }
65} 76}
66 77
67static binary_t session_nonce; 78static binary_t session_nonce;
68#ifdef HAVE_SQLITE 79#ifdef HAVE_SQLITE
69 static struct { 80 static struct {
70 std::string filesignature; 81 std::string filesignature;
71 long filesize; 82 long filesize;
72 std::string filename; 83 std::string filename;
73 inline void reset() { filesignature.erase(); filename.erase(); filesize=0; } 84 inline void reset() { filesignature.erase(); filename.erase(); filesize=0; }
74 inline void set(const std::string n,const std::string sig,long siz) { 85 inline void set(const std::string n,const std::string sig,long siz) {
75 filename = n; filesignature = sig; filesize = siz; 86 filename = n; filesignature = sig; filesize = siz;
76 } 87 }
77 inline bool is(const std::string n,const std::string sig,long siz) { 88 inline bool is(const std::string n,const std::string sig,long siz) {
78 return filesize==siz && filename==n && filesignature==sig; 89 return filesize==siz && filename==n && filesignature==sig;
79 } 90 }
80 } already; 91 } already;
81#endif /* HAVE_SQLITE */ 92#endif /* HAVE_SQLITE */
82 93
83static bool detached_child() { 94static bool detached_child() {
84 pid_t p = fork(); 95 pid_t p = fork();
85 if(p<0) { 96 if(p<0) {
86 syslog(LOG_ERR,"Failed to fork away for hook execution"); 97 syslog(LOG_ERR,"Failed to fork away for hook execution");
87 _exit(-1); 98 _exit(-1);
88 } 99 }
89 if(!p) { 100 if(!p) {
90 setsid(); 101 setsid();
91 for(int i=getdtablesize();i>=0;--i) close(i); 102 for(int i=getdtablesize();i>=0;--i) close(i);
92 int i=open("/dev/null",O_RDWR); assert(i==0); 103 int i=open("/dev/null",O_RDWR); assert(i==0);
93 i = dup(i); assert(i==1); 104 i = dup(i); assert(i==1);
94 i = dup(i); assert(i==2); 105 i = dup(i); assert(i==2);
95 return true; 106 return true;
96 } 107 }
97 return false; 108 return false;
98} 109}
99 110
100static int E(eyefiworker* efs,const char *c,const std::exception& e) { 111static int E(eyefiworker* efs,const char *c,const std::exception& e) {
101 efs->keep_alive=0; 112 efs->keep_alive=0;
102 syslog(LOG_ERR,"error while processing %s: %s",c,e.what()); 113 syslog(LOG_ERR,"error while processing %s: %s",c,e.what());
103 return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0); 114 return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0);
104} 115}
105 116
106int eyefiworker::StartSession( 117int eyefiworker::StartSession(
107 std::string macaddress,std::string cnonce, 118 std::string macaddress,std::string cnonce,
108 int transfermode,long transfermodetimestamp, 119 int transfermode,long transfermodetimestamp,
109 struct rns__StartSessionResponse &r ) try { 120 struct rns__StartSessionResponse &r ) try {
110 syslog(LOG_INFO, 121 syslog(LOG_INFO,
111 "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", 122 "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld",
112 macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); 123 macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp );
113 eyekinfig_t eyekinfig(macaddress); 124 kinfig.reset(new eyekinfig_t(macaddress));
114 r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); 125 umask(kinfig->get_umask());
126
127 r.credential = binary_t(macaddress+cnonce+kinfig->get_upload_key()).md5().hex();
115 128
116 r.snonce = session_nonce.make_nonce().hex(); 129 r.snonce = session_nonce.make_nonce().hex();
117 r.transfermode=transfermode; 130 r.transfermode=transfermode;
118 r.transfermodetimestamp=transfermodetimestamp; 131 r.transfermodetimestamp=transfermodetimestamp;
119 r.upsyncallowed=false; 132 r.upsyncallowed=false;
120 133
121 std::string cmd = eyekinfig.get_on_start_session(); 134 std::string cmd = kinfig->get_on_start_session();
122 if(!cmd.empty()) { 135 if(!cmd.empty()) {
123 if(detached_child()) { 136 if(detached_child()) {
124 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); 137 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) );
125 putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); 138 putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) );
126 putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); 139 putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) );
127 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; 140 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 };
128 execv("/bin/sh",argv); 141 execv("/bin/sh",argv);
129 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); 142 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str());
130 _exit(-1); 143 _exit(-1);
131 } 144 }
132 } 145 }
133 return SOAP_OK; 146 return SOAP_OK;
134}catch(const std::exception& e) { return E(this,"StartSession",e); } 147}catch(const std::exception& e) { return E(this,"StartSession",e); }
135 148
136int eyefiworker::GetPhotoStatus( 149int eyefiworker::GetPhotoStatus(
137 std::string credential, std::string macaddress, 150 std::string credential, std::string macaddress,
138 std::string filename, long filesize, std::string filesignature, 151 std::string filename, long filesize, std::string filesignature,
139 int flags, 152 int flags,
140 struct rns__GetPhotoStatusResponse &r ) try { 153 struct rns__GetPhotoStatusResponse &r ) try {
141 syslog(LOG_INFO, 154 syslog(LOG_INFO,
142 "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", 155 "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s",
143 macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, 156 macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags,
144 session_nonce.hex().c_str() ); 157 session_nonce.hex().c_str() );
145 158
146 eyekinfig_t eyekinfig(macaddress); 159 if(!(kinfig && kinfig->macaddress==macaddress))
147 std::string computed_credential = binary_t(macaddress+eyekinfig.get_upload_key()+session_nonce.hex()).md5().hex(); 160 throw std::runtime_error("I'm not talking to this peer");
161
162 std::string computed_credential = binary_t(macaddress+kinfig->get_upload_key()+session_nonce.hex()).md5().hex();
148 163
149#ifndef NDEBUG 164#ifndef NDEBUG
150 syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); 165 syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str());
151#endif 166#endif
152 167
153 if (credential != computed_credential) throw std::runtime_error("card authentication failed"); 168 if (credential != computed_credential) throw std::runtime_error("card authentication failed");
154 169
170 indir.reset(new tmpdir_t(kinfig->get_targetdir()+"/.incoming.XXXXXX"));
171
155#ifdef HAVE_SQLITE 172#ifdef HAVE_SQLITE
156 iiidb_t D(eyekinfig); 173 iiidb_t D(*kinfig);
157 seclude::stmt_t S = D.prepare( 174 seclude::stmt_t S = D.prepare(
158 "SELECT fileid FROM photo" 175 "SELECT fileid FROM photo"
159 " WHERE mac=:mac AND filename=:filename" 176 " WHERE mac=:mac AND filename=:filename"
160 " AND filesize=:filesize AND filesignature=:filesignature" 177 " AND filesize=:filesize AND filesignature=:filesignature"
161 ).bind(":mac",macaddress) 178 ).bind(":mac",macaddress)
162 .bind(":filename",filename).bind(":filesize",filesize) 179 .bind(":filename",filename).bind(":filesize",filesize)
163 .bind(":filesignature",filesignature); 180 .bind(":filesignature",filesignature);
164 if(!S.step()) { 181 if(!S.step()) {
165 r.fileid = 1; r.offset = 0; 182 r.fileid = 1; r.offset = 0;
166 }else{ 183 }else{
167 r.fileid = S.column<long>(0); 184 r.fileid = S.column<long>(0);
168 r.offset = filesize; 185 r.offset = filesize;
169 already.set(filename,filesignature,filesize); 186 already.set(filename,filesignature,filesize);
170 } 187 }
171#else /* HAVE_SQLITE */ 188#else /* HAVE_SQLITE */
172 r.fileid=1, r.offset=0; 189 r.fileid=1, r.offset=0;
173#endif /* HAVE_SQLITE */ 190#endif /* HAVE_SQLITE */
174 return SOAP_OK; 191 return SOAP_OK;
175}catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } 192}catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); }
176 193
177int eyefiworker::MarkLastPhotoInRoll( 194int eyefiworker::MarkLastPhotoInRoll(
178 std::string macaddress, int mergedelta, 195 std::string macaddress, int mergedelta,
179 struct rns__MarkLastPhotoInRollResponse&/* r */ ) try { 196 struct rns__MarkLastPhotoInRollResponse&/* r */ ) try {
180 syslog(LOG_INFO, 197 syslog(LOG_INFO,
181 "MarkLastPhotoInRoll request from %s with mergedelta=%d", 198 "MarkLastPhotoInRoll request from %s with mergedelta=%d",
182 macaddress.c_str(), mergedelta ); 199 macaddress.c_str(), mergedelta );
183 std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); 200 if(!(kinfig && kinfig->macaddress==macaddress))
201 throw std::runtime_error("I'm not talking to this peer");
202
203 std::string cmd = kinfig->get_on_mark_last_photo_in_roll();
184 if(!cmd.empty()) { 204 if(!cmd.empty()) {
185 if(detached_child()) { 205 if(detached_child()) {
186 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); 206 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) );
187 putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); 207 putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) );
188 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; 208 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 };
189 execv("/bin/sh",argv); 209 execv("/bin/sh",argv);
190 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); 210 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str());
191 _exit(-1); 211 _exit(-1);
192 } 212 }
193 } 213 }
194 keep_alive = 0; 214 keep_alive = 0;
195 return SOAP_OK; 215 return SOAP_OK;
196}catch(const std::exception& e) { return E(this,"MarkLastPhotoInRoll",e); } 216}catch(const std::exception& e) { return E(this,"MarkLastPhotoInRoll",e); }
197 217
218void *eyefiworker::mime_writeopen(void *handle,const char *id,const char *type,const char *description,
219 enum soap_mime_encoding encoding) {
220 if(!id) return NULL;
221 if(!strcmp(id,"FILENAME")) {
222 mime_tarfile.reset(new mimewrite_tarfile(*indir));
223 return mime_tarfile.get();
224 }else if(!strcmp(id,"INTEGRITYDIGEST")) {
225 mime_idigest.reset(new mimewrite_string());
226 return mime_idigest.get();
227 }
228 return NULL;
229}
230int eyefiworker::mime_write(void *handle,const char *buf,size_t len) {
231 if(!handle) return SOAP_ERR;
232 return static_cast<mimewrite_base*>(handle)->write(buf,len);
233}
234void eyefiworker::mime_writeclose(void *handle) {
235 if(handle) static_cast<mimewrite_base*>(handle)->close();
236}
237
198int eyefiworker::UploadPhoto( 238int eyefiworker::UploadPhoto(
199 int fileid, std::string macaddress, 239 int fileid, std::string macaddress,
200 std::string filename, long filesize, std::string filesignature, 240 std::string filename, long filesize, std::string filesignature,
201 std::string encryption, int flags, 241 std::string encryption, int flags,
202 struct rns__UploadPhotoResponse& r ) try { 242 struct rns__UploadPhotoResponse& r ) try {
203 syslog(LOG_INFO, 243 syslog(LOG_INFO,
204 "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," 244 "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld,"
205 " filesignature=%s, encryption=%s, flags=%04X", 245 " filesignature=%s, encryption=%s, flags=%04X",
206 macaddress.c_str(), fileid, filename.c_str(), filesize, 246 macaddress.c_str(), fileid, filename.c_str(), filesize,
207 filesignature.c_str(), encryption.c_str(), flags ); 247 filesignature.c_str(), encryption.c_str(), flags );
248 if(!(kinfig && kinfig->macaddress==macaddress))
249 throw std::runtime_error("I'm not talking to this peer");
250
208 std::string::size_type fnl=filename.length(); 251 std::string::size_type fnl=filename.length();
209 if(fnl<sizeof(".tar") || strncmp(filename.c_str()+fnl-sizeof(".tar")+sizeof(""),".tar",sizeof(".tar"))) 252 if(fnl<sizeof(".tar") || strncmp(filename.c_str()+fnl-sizeof(".tar")+sizeof(""),".tar",sizeof(".tar")))
210 throw std::runtime_error(gnu::autosprintf("honestly, I expected the tarball coming here, not '%s'",filename.c_str())); 253 throw std::runtime_error(gnu::autosprintf("honestly, I expected the tarball coming here, not '%s'",filename.c_str()));
211 std::string the_file(filename,0,fnl-sizeof(".tar")+sizeof("")); 254 std::string the_file(filename,0,fnl-sizeof(".tar")+sizeof(""));
212 std::string the_log = the_file+".log"; 255 std::string the_log = the_file+".log";
213 256
214 eyekinfig_t eyekinfig(macaddress); 257 if(!indir) throw std::runtime_error("I haven't even created a directory!");
215 258 shared_ptr<tmpdir_t> dir; dir.swap(indir);
216 umask(eyekinfig.get_umask()); 259 if(!mime_tarfile) throw std::runtime_error("I haven't written the tarball!");
217 260 shared_ptr<mimewrite_tarfile> file; file.swap(mime_tarfile);
218 std::string td = eyekinfig.get_targetdir(); 261 if(!mime_idigest) throw std::runtime_error("I haven't seen the integrity digest!");
219 tmpdir_t indir(td+"/.incoming.XXXXXX"); 262 shared_ptr<mimewrite_string> idigest; idigest.swap(mime_idigest);
220
221 std::string tf,lf;
222 binary_t digest, idigest;
223#ifdef HAVE_SQLITE
224 bool beenthere = false;
225#endif
226
227 for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) {
228#ifndef NDEBUG
229 syslog(LOG_DEBUG,
230 " MIME attachment with id=%s, type=%s, size=%ld",
231 (*i).id, (*i).type, (long)(*i).size );
232#endif
233
234 if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) {
235 std::string idigestr((*i).ptr,(*i).size);
236#ifndef NDEBUG
237 syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str());
238#endif
239 idigest.from_hex(idigestr);
240 }
241 if( (*i).id && !strcmp((*i).id,"FILENAME") ) {
242 assert( (*i).type && !strcmp((*i).type,"application/x-tar") );
243#ifdef III_SAVE_TARS
244 std::string tarfile = indir.get_file(filename);
245 {
246 std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size);
247 }
248#endif
249 263
250 if(!tf.empty()) throw std::runtime_error("already seen tarball");
251 if(!digest.empty()) throw std::runtime_error("already have integrity digest");
252 digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key());
253#ifndef NDEBUG
254 syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str());
255#endif
256#ifdef HAVE_SQLITE 264#ifdef HAVE_SQLITE
257 if(!(*i).size) { 265 if(!file->f.tellg()) {
258 if(!already.is(filename,filesignature,filesize)) 266 if(!already.is(filename,filesignature,filesize))
259 throw std::runtime_error("got zero-length upload for unknown file"); 267 throw std::runtime_error("got zero-length upload for unknown file");
260 beenthere = true; continue; 268 r.success = true;
261 } 269 return SOAP_OK;
270 }
262#endif 271#endif
263 272
264 tarchive_t a((*i).ptr,(*i).size); 273 if(idigest->str != file->idigest.final(kinfig->get_upload_key()).hex())
265 while(a.read_next_header()) { 274 throw std::runtime_error("Integrity digest doesn't match, disintegrating.");
266 std::string ep = a.entry_pathname(), f = indir.get_file(ep);
267 if(ep==the_file) tf = f;
268 else if(ep==the_log) lf = f;
269 else continue;
270 int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666);
271 if(fd<0)
272 throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str()));
273 if(!a.read_data_into_fd(fd))
274 throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str()));
275 close(fd);
276 }
277 }
278 }
279 275
280#ifdef HAVE_SQLITE 276 std::string tf, lf;
281 if(beenthere) { 277 for(tarchive_t a(file->fn.c_str());a.read_next_header();) {
282 r.success=true; 278 std::string ep = a.entry_pathname(), f = dir->get_file(ep);
283 return SOAP_OK; 279 if(ep==the_file) tf = f;
280 else if(ep==the_log) lf = f;
281 else continue;
282 int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666);
283 if(fd<0)
284 throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str()));
285 if(!a.read_data_into_fd(fd))
286 throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str()));
287 close(fd);
284 } 288 }
285#endif
286 289
287 if(tf.empty()) throw std::runtime_error("haven't seen THE file"); 290 if(tf.empty()) throw std::runtime_error("haven't seen THE file");
288 if(digest!=idigest) throw std::runtime_error("integrity digest verification failed");
289 291
290 std::string::size_type ls = tf.rfind('/'); 292 std::string::size_type ls = tf.rfind('/');
291 // XXX: actually, lack of '/' signifies error here 293 // XXX: actually, lack of '/' signifies error here
292 std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); 294 std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1);
293 ls = lf.rfind('/'); 295 ls = lf.rfind('/');
294 std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); 296 std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1);
295 std::string ttf,tlf; 297 std::string ttf,tlf;
296 bool success = false; 298 bool success = false;
299 std::string td = kinfig->get_targetdir();
297 for(int i=0;i<32767;++i) { 300 for(int i=0;i<32767;++i) {
298 const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s"; 301 const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s";
299 ttf = (const char*)gnu::autosprintf(fmt,td.c_str(),tbn.c_str(),i); 302 ttf = (const char*)gnu::autosprintf(fmt,td.c_str(),tbn.c_str(),i);
300 if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i); 303 if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i);
301 if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty() || !link(lf.c_str(),tlf.c_str())) ) { 304 if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty() || !link(lf.c_str(),tlf.c_str())) ) {
302 unlink(tf.c_str()); 305 unlink(tf.c_str());
303 if(!lf.empty()) unlink(lf.c_str()); 306 if(!lf.empty()) unlink(lf.c_str());
304 success=true; 307 success=true;
305 break; 308 break;
306 } 309 }
307 } 310 }
308 std::string cmd = eyekinfig.get_on_upload_photo(); 311 std::string cmd = kinfig->get_on_upload_photo();
309 if(success) { 312 if(success) {
310#ifdef HAVE_SQLITE 313#ifdef HAVE_SQLITE
311 { 314 {
312 iiidb_t D(eyekinfig); 315 iiidb_t D(*kinfig);
313 D.prepare( 316 D.prepare(
314 "INSERT INTO photo" 317 "INSERT INTO photo"
315 " (ctime,mac,fileid,filename,filesize,filesignature,encryption,flags)" 318 " (ctime,mac,fileid,filename,filesize,filesignature,encryption,flags)"
316 " VALUES" 319 " VALUES"
317 " (:ctime,:mac,:fileid,:filename,:filesize,:filesignature,:encryption,:flags)" 320 " (:ctime,:mac,:fileid,:filename,:filesize,:filesignature,:encryption,:flags)"
318 ).bind(":ctime",time(0)) 321 ).bind(":ctime",time(0))
319 .bind(":mac",macaddress) 322 .bind(":mac",macaddress)
320 .bind(":fileid",fileid).bind(":filename",filename) 323 .bind(":fileid",fileid).bind(":filename",filename)
321 .bind(":filesize",filesize).bind(":filesignature",filesignature) 324 .bind(":filesize",filesize).bind(":filesignature",filesignature)
322 .bind(":encryption",encryption).bind(":flags",flags) 325 .bind(":encryption",encryption).bind(":flags",flags)
323 .step(); 326 .step();
324 } 327 }
325#endif /* HAVE_SQLITE */ 328#endif /* HAVE_SQLITE */
326 if((!cmd.empty()) && detached_child()) { 329 if((!cmd.empty()) && detached_child()) {
327 putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); 330 putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) );
328 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); 331 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) );
329 putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) ); 332 putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) );
330 if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) ); 333 if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) );
331 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; 334 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 };
332 execv("/bin/sh",argv); 335 execv("/bin/sh",argv);
333 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); 336 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str());
334 _exit(-1); 337 _exit(-1);
335 } 338 }
336 } 339 }
337 340
338 r.success = true; 341 r.success = true;
339 return SOAP_OK; 342 return SOAP_OK;
340}catch(const std::exception& e) { return E(this,"UploadPhoto",e); } 343}catch(const std::exception& e) { return E(this,"UploadPhoto",e); }
341 344
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 @@
1#ifndef __EYEFIWORKER_H 1#ifndef __EYEFIWORKER_H
2#define __EYEFIWORKER_H 2#define __EYEFIWORKER_H
3 3
4#include <tr1/memory>
5using std::tr1::shared_ptr;
6
4#include "soapeyefiService.h" 7#include "soapeyefiService.h"
5 8
9#include "eyekinfig.h"
10#include "eyetil.h"
11
12struct eyefi_session;
13
6class eyefiworker : public eyefiService { 14class eyefiworker : public eyefiService {
7 public: 15 public:
16 shared_ptr<eyekinfig_t> kinfig;
17 shared_ptr<tmpdir_t> indir;
18 shared_ptr<mimewrite_tarfile> mime_tarfile;
19 shared_ptr<mimewrite_string> mime_idigest;
8 20
9 eyefiworker(); 21 eyefiworker();
10 ~eyefiworker(); 22 ~eyefiworker() { }
11 23
12 int run(int port) __attribute__ ((noreturn)); 24 int run(int port) __attribute__ ((noreturn));
13 25
14 int StartSession(std::string macaddress, std::string cnonce, 26 int StartSession(std::string macaddress, std::string cnonce,
15 int transfermode, long transfermodetimestamp, 27 int transfermode, long transfermodetimestamp,
16 struct rns__StartSessionResponse &r); 28 struct rns__StartSessionResponse &r);
17 int GetPhotoStatus(std::string credential, std::string macaddress, 29 int GetPhotoStatus(std::string credential, std::string macaddress,
18 std::string filename, long filesize, std::string filesignature, int flags, 30 std::string filename, long filesize, std::string filesignature, int flags,
19 struct rns__GetPhotoStatusResponse &r); 31 struct rns__GetPhotoStatusResponse &r);
20 int MarkLastPhotoInRoll(std::string macaddress, int mergedelta, 32 int MarkLastPhotoInRoll(std::string macaddress, int mergedelta,
21 struct rns__MarkLastPhotoInRollResponse &r); 33 struct rns__MarkLastPhotoInRollResponse &r);
22 int UploadPhoto(int fileid, std::string macaddress, 34 int UploadPhoto(int fileid, std::string macaddress,
23 std::string filename, long filesize, std::string filesignature, 35 std::string filename, long filesize, std::string filesignature,
24 std::string encryption, int flags, 36 std::string encryption, int flags,
25 struct rns__UploadPhotoResponse &r); 37 struct rns__UploadPhotoResponse &r);
26 38
39 void *mime_writeopen(void *handle,const char *id,const char *type,const char *description,
40 enum soap_mime_encoding encoding);
41 int mime_write(void *handle,const char *buf,size_t len);
42 void mime_writeclose(void *handle);
43
27 eyefiService *copy() { throw std::logic_error("Not meant to be called"); } 44 eyefiService *copy() { throw std::logic_error("Not meant to be called"); }
28}; 45};
29 46
30#endif /* __EYEFIWORKER_H */ 47#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 @@
1#include <stdlib.h> 1#include <stdlib.h>
2#include <sys/stat.h> 2#include <sys/stat.h>
3#include <syslog.h> 3#include <syslog.h>
4#include <iostream> 4#include <iostream>
5#include <cassert> 5#include <cassert>
6#include <stdexcept> 6#include <stdexcept>
7#include <algorithm> 7#include <algorithm>
8#include <numeric> 8#include <numeric>
9#include <openssl/md5.h> 9#include <openssl/md5.h>
10#include "eyetil.h" 10#include "eyetil.h"
11 11
12#include "config.h" 12#include "config.h"
13#ifdef HAVE_LIBUUID 13#ifdef HAVE_LIBUUID
14# include <uuid/uuid.h> 14# include <uuid/uuid.h>
15#endif 15#endif
16 16
17binary_t& binary_t::from_hex(const std::string& h) { 17binary_t& binary_t::from_hex(const std::string& h) {
18 std::string::size_type hs = h.length(); 18 std::string::size_type hs = h.length();
19 if(hs&1) 19 if(hs&1)
20 throw std::runtime_error("odd number of characters in hexadecimal number"); 20 throw std::runtime_error("odd number of characters in hexadecimal number");
21 size_t rvs = hs>>1; 21 size_t rvs = hs>>1;
22 resize(rvs); 22 resize(rvs);
23 const unsigned char *hp = (const unsigned char*)h.data(); 23 const unsigned char *hp = (const unsigned char*)h.data();
24 iterator oi=begin(); 24 iterator oi=begin();
25 char t[3] = { 0,0,0 }; 25 char t[3] = { 0,0,0 };
26 for(size_t i=0;i<rvs;++i) { 26 for(size_t i=0;i<rvs;++i) {
27 t[0]=*(hp++); t[1]=*(hp++); 27 t[0]=*(hp++); t[1]=*(hp++);
28 *(oi++) = static_cast<binary_t::value_type>(0xff&strtol(t,0,16)); 28 *(oi++) = static_cast<binary_t::value_type>(0xff&strtol(t,0,16));
29 } 29 }
30 return *this; 30 return *this;
31} 31}
32 32
33binary_t& binary_t::from_data(const void *d,size_t s) { 33binary_t& binary_t::from_data(const void *d,size_t s) {
34 resize(s); 34 resize(s);
35 std::copy((const unsigned char*)d,(const unsigned char *)d+s, 35 std::copy((const unsigned char*)d,(const unsigned char *)d+s,
36 begin() ); 36 begin() );
37 return *this; 37 return *this;
38} 38}
39 39
40binary_t& binary_t::make_nonce() { 40binary_t& binary_t::make_nonce() {
41#ifdef HAVE_LIBUUID 41#ifdef HAVE_LIBUUID
42 uuid_t uuid; 42 uuid_t uuid;
43 uuid_generate(uuid); 43 uuid_generate(uuid);
44 from_data((unsigned char*)uuid,sizeof(uuid)); 44 from_data((unsigned char*)uuid,sizeof(uuid));
45#else 45#else
46 resize(16); 46 resize(16);
47 std::generate_n(begin(),16,rand); 47 std::generate_n(begin(),16,rand);
48#endif /* HAVE_LIBUUID */ 48#endif /* HAVE_LIBUUID */
49 return *this; 49 return *this;
50} 50}
51 51
52std::string binary_t::hex() const { 52std::string binary_t::hex() const {
53 std::string rv; 53 std::string rv;
54 rv.reserve((size()<<1)+1); 54 rv.reserve((size()<<1)+1);
55 char t[3] = {0,0,0}; 55 char t[3] = {0,0,0};
56 for(const_iterator i=begin(),ie=end();i!=ie;++i) { 56 for(const_iterator i=begin(),ie=end();i!=ie;++i) {
57 size_t rc = snprintf(t,sizeof(t),"%02x",*i); 57 size_t rc = snprintf(t,sizeof(t),"%02x",*i);
58 assert(rc<sizeof(t)); 58 assert(rc<sizeof(t));
59 rv += t; 59 rv += t;
60 } 60 }
61 return rv; 61 return rv;
62} 62}
63 63
64binary_t binary_t::md5() const { 64binary_t binary_t::md5() const {
65 binary_t rv(MD5_DIGEST_LENGTH); 65 binary_t rv(MD5_DIGEST_LENGTH);
66 if(!MD5( 66 if(!MD5(
67 (const unsigned char*)&(front()),size(), 67 (const unsigned char*)&(front()),size(),
68 (unsigned char*)&(rv.front()) )) 68 (unsigned char*)&(rv.front()) ))
69 throw std::runtime_error("failed to md5()"); 69 throw std::runtime_error("failed to md5()");
70 return rv; 70 return rv;
71} 71}
72 72
73void md5_digester::init() { 73void md5_digester::init() {
74 if(!MD5_Init(&ctx)) throw std::runtime_error("failed to MD5_Init()"); 74 if(!MD5_Init(&ctx)) throw std::runtime_error("failed to MD5_Init()");
75} 75}
76void md5_digester::update(const void *d,size_t l) { 76void md5_digester::update(const void *d,size_t l) {
77 if(!MD5_Update(&ctx,d,l)) throw std::runtime_error("failed to MD5_Update()"); 77 if(!MD5_Update(&ctx,d,l)) throw std::runtime_error("failed to MD5_Update()");
78} 78}
79binary_t md5_digester::final() { 79binary_t md5_digester::final() {
80 binary_t rv(MD5_DIGEST_LENGTH); 80 binary_t rv(MD5_DIGEST_LENGTH);
81 if(!MD5_Final((unsigned char*)&(rv.front()), &ctx)) 81 if(!MD5_Final((unsigned char*)&(rv.front()), &ctx))
82 throw std::runtime_error("failed to MD5_Final()"); 82 throw std::runtime_error("failed to MD5_Final()");
83 return rv; 83 return rv;
84} 84}
85 85
86uint16_t block512_t::tcpcksum(block512_t& data) { 86uint16_t block512_t::tcpcksum(block512_t& data) {
87 uint32_t sum = std::accumulate(data.data,data.data+words,0); 87 uint32_t sum = std::accumulate(data.data,data.data+words,0);
88 while(uint32_t hw = sum>>16) sum = (sum&0xffff)+hw; 88 while(uint32_t hw = sum>>16) sum = (sum&0xffff)+hw;
89 return 0xffff&~sum; 89 return 0xffff&~sum;
90} 90}
91 91
92void integrity_digester::update(const void *d_,size_t s) { 92void integrity_digester::update(const void *d_,size_t s) {
93 uint8_t *d=(uint8_t*)d_; 93 uint8_t *d=(uint8_t*)d_;
94 if(data_size) { 94 if(data_size) {
95 int l = sizeof(data)-data_size; 95 int l = sizeof(data)-data_size;
96 if(l>s) { 96 if(l>s) {
97 memmove(data.dptr(data_size),d,s); data_size+=s; return; 97 memmove(data.dptr(data_size),d,s); data_size+=s; return;
98 } 98 }
99 memmove(data.dptr(data_size),d,l); d+=l; s-=l; 99 memmove(data.dptr(data_size),d,l); d+=l; s-=l;
100 md5.update<uint16_t>( data.tcpcksum(data) ); 100 md5.update<uint16_t>( data.tcpcksum(data) );
101 } 101 }
102 if(s<sizeof(data)) { 102 if(s<sizeof(data)) {
103 memmove(data.dptr(0),d,s); data_size=s; return; 103 memmove(data.dptr(0),d,s); data_size=s; return;
104 } 104 }
105 size_t bb=s/sizeof(block512_t); 105 size_t bb=s/sizeof(block512_t);
106 std::transform((block512_t*)d,((block512_t*)d)+bb, 106 std::transform((block512_t*)d,((block512_t*)d)+bb,
107 md5.updater<uint16_t>(),block512_t::tcpcksum); 107 md5.updater<uint16_t>(),block512_t::tcpcksum);
108 size_t ss=bb*sizeof(block512_t); 108 size_t ss=bb*sizeof(block512_t);
109 d+=ss; s-=ss; 109 d+=ss; s-=ss;
110 assert(s<sizeof(block512_t)); 110 assert(s<sizeof(block512_t));
111 if(s) memmove(data.dptr(0),d,data_size=s); 111 if(s) memmove(data.dptr(0),d,data_size=s);
112} 112}
113 113
114binary_t integrity_digester::final(const std::string& ukey) { 114binary_t integrity_digester::final(const std::string& ukey) {
115 assert(!data_size); 115 assert(!data_size);
116 md5.update( binary_t(ukey) ); 116 md5.update( binary_t(ukey) );
117 return md5.final(); 117 return md5.final();
118} 118}
119 119
120static void make_path_for_template(const std::string& p,mode_t m) { 120static void make_path_for_template(const std::string& p,mode_t m) {
121 struct stat st; 121 struct stat st;
122 std::string pp; 122 std::string pp;
123 for(std::string::size_type sl=p.find('/',1); 123 for(std::string::size_type sl=p.find('/',1);
124 sl!=std::string::npos; 124 sl!=std::string::npos;
125 sl=p.find('/',sl+1)) { 125 sl=p.find('/',sl+1)) {
126 if(stat( (pp=p.substr(0,sl)).c_str() ,&st) 126 if(stat( (pp=p.substr(0,sl)).c_str() ,&st)
127 || !S_ISDIR(st.st_mode)) { 127 || !S_ISDIR(st.st_mode)) {
128 if(mkdir(pp.c_str(),m)) 128 if(mkdir(pp.c_str(),m))
129 throw std::runtime_error("failed to mkdir()"); 129 throw std::runtime_error("failed to mkdir()");
130 } 130 }
131 } 131 }
132} 132}
133 133
134tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { 134tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) {
135 make_path_for_template(dt,0777); 135 make_path_for_template(dt,0777);
136 if(!mkdtemp((char*)dir.data())) 136 if(!mkdtemp((char*)dir.data()))
137 throw std::runtime_error("failed to mkdtmp()"); 137 throw std::runtime_error("failed to mkdtmp()");
138} 138}
139tmpdir_t::~tmpdir_t() { 139tmpdir_t::~tmpdir_t() {
140 assert(!dir.empty()); 140 assert(!dir.empty());
141 if(rmdir(dir.c_str())) { 141 if(rmdir(dir.c_str())) {
142 syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str()); 142 syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str());
143 } 143 }
144} 144}
145 145
146std::string tmpdir_t::get_file(const std::string& f) { 146std::string tmpdir_t::get_file(const std::string& f) {
147 std::string::size_type ls = f.rfind('/'); 147 std::string::size_type ls = f.rfind('/');
148 return dir+'/'+( 148 return dir+'/'+(
149 (ls==std::string::npos) 149 (ls==std::string::npos)
150 ? f 150 ? f
151 : f.substr(ls+1) 151 : f.substr(ls+1)
152 ); 152 );
153} 153}
154 154
155tarchive_t::tarchive_t(void *p,size_t s) : a(archive_read_new()), e(0) { 155tarchive_t::tarchive_t(const char *fn) : a(archive_read_new()), e(0) {
156 if(!a) throw std::runtime_error("failed to archive_read_new()"); 156 if(!a) throw std::runtime_error("failed to archive_read_new()");
157 if(archive_read_support_format_tar(a)) { 157 if(archive_read_support_format_tar(a)) {
158 archive_read_finish(a); 158 archive_read_finish(a);
159 throw std::runtime_error("failed to archive_read_support_format_tar()"); 159 throw std::runtime_error("failed to archive_read_support_format_tar()");
160 } 160 }
161 if(archive_read_open_memory(a,p,s)) { 161 if(archive_read_open_filename(a,fn,16384)) {
162 archive_read_finish(a); 162 archive_read_finish(a);
163 throw std::runtime_error("failed to archive_read_open_memory()"); 163 throw std::runtime_error("failed to archive_read_open_memory()");
164 } 164 }
165} 165}
166tarchive_t::~tarchive_t() { 166tarchive_t::~tarchive_t() {
167 assert(a); 167 assert(a);
168 archive_read_finish(a); 168 archive_read_finish(a);
169} 169}
170 170
171bool tarchive_t::read_next_header() { 171bool tarchive_t::read_next_header() {
172 assert(a); 172 assert(a);
173 return archive_read_next_header(a,&e)==ARCHIVE_OK; 173 return archive_read_next_header(a,&e)==ARCHIVE_OK;
174} 174}
175 175
176std::string tarchive_t::entry_pathname() { 176std::string tarchive_t::entry_pathname() {
177 assert(a); assert(e); 177 assert(a); assert(e);
178 return archive_entry_pathname(e); 178 return archive_entry_pathname(e);
179} 179}
180 180
181bool tarchive_t::read_data_into_fd(int fd) { 181bool tarchive_t::read_data_into_fd(int fd) {
182 assert(a); 182 assert(a);
183 return archive_read_data_into_fd(a,fd)==ARCHIVE_OK; 183 return archive_read_data_into_fd(a,fd)==ARCHIVE_OK;
184} 184}
185 185
186 186
187binary_t integrity_digest(const void *ptr,size_t size,const std::string& ukey) { 187binary_t integrity_digest(const void *ptr,size_t size,const std::string& ukey) {
188 md5_digester rv; 188 md5_digester rv;
189 std::transform( (block512_t*)ptr, ((block512_t*)ptr)+size/sizeof(block512_t), 189 std::transform( (block512_t*)ptr, ((block512_t*)ptr)+size/sizeof(block512_t),
190 rv.updater<uint16_t>(), block512_t::tcpcksum ); 190 rv.updater<uint16_t>(), block512_t::tcpcksum );
191 rv.update( binary_t(ukey) ); 191 rv.update( binary_t(ukey) );
192 return rv.final(); 192 return rv.final();
193} 193}
194 194
195mimewrite_tarfile::mimewrite_tarfile(tmpdir_t& d) { 195mimewrite_tarfile::mimewrite_tarfile(tmpdir_t& d) {
196 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); 196 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);
197} 197}
198mimewrite_tarfile::~mimewrite_tarfile() { 198mimewrite_tarfile::~mimewrite_tarfile() {
199 unlink(fn.c_str()); 199 unlink(fn.c_str());
200} 200}
201int mimewrite_tarfile::write(const char *buf,size_t len) { 201int mimewrite_tarfile::write(const char *buf,size_t len) {
202 return f.write(buf,len) ? (idigest.update(buf,len),SOAP_OK) : SOAP_ERR; 202 return f.write(buf,len) ? (idigest.update(buf,len),SOAP_OK) : SOAP_ERR;
203} 203}
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 @@
1#ifndef __EYETIL_H 1#ifndef __EYETIL_H
2#define __EYETIL_H 2#define __EYETIL_H
3 3
4#include <vector> 4#include <vector>
5#include <string> 5#include <string>
6#include <fstream> 6#include <fstream>
7#include <archive.h> 7#include <archive.h>
8#include <archive_entry.h> 8#include <archive_entry.h>
9#include "openssl/md5.h" 9#include "openssl/md5.h"
10#include "soapH.h" 10#include "soapH.h"
11 11
12struct throwable_exit { 12struct throwable_exit {
13 int rc; 13 int rc;
14 throwable_exit(int rc_) : rc(rc_) { } 14 throwable_exit(int rc_) : rc(rc_) { }
15}; 15};
16 16
17class binary_t : public std::vector<unsigned char> { 17class binary_t : public std::vector<unsigned char> {
18 public: 18 public:
19 binary_t() { } 19 binary_t() { }
20 binary_t(size_type n) : std::vector<unsigned char>(n) { } 20 binary_t(size_type n) : std::vector<unsigned char>(n) { }
21 binary_t(const std::string& h) { from_hex(h); } 21 binary_t(const std::string& h) { from_hex(h); }
22 binary_t(const void *d,size_t s) { from_data(d,s); } 22 binary_t(const void *d,size_t s) { from_data(d,s); }
23 23
24 binary_t& from_hex(const std::string& h); 24 binary_t& from_hex(const std::string& h);
25 binary_t& from_data(const void *d,size_t s); 25 binary_t& from_data(const void *d,size_t s);
26 binary_t& make_nonce(); 26 binary_t& make_nonce();
27 27
28 std::string hex() const; 28 std::string hex() const;
29 binary_t md5() const; 29 binary_t md5() const;
30}; 30};
31 31
32struct md5_digester { 32struct md5_digester {
33 MD5_CTX ctx; 33 MD5_CTX ctx;
34 md5_digester() { init(); } 34 md5_digester() { init(); }
35 35
36 void init(); 36 void init();
37 void update(const void *d,size_t l); 37 void update(const void *d,size_t l);
38 binary_t final(); 38 binary_t final();
39 39
40 template<typename T> 40 template<typename T>
41 void update(const T& x) { update(&x,sizeof(x)); } 41 void update(const T& x) { update(&x,sizeof(x)); }
42 42
43 template<typename T> 43 template<typename T>
44 struct update_iterator : public std::iterator<std::output_iterator_tag,T,void,T*,T&> { 44 struct update_iterator : public std::iterator<std::output_iterator_tag,T,void,T*,T&> {
45 md5_digester *d; 45 md5_digester *d;
46 update_iterator(md5_digester *d_) : d(d_) { } 46 update_iterator(md5_digester *d_) : d(d_) { }
47 update_iterator(const update_iterator& x) : d(x.d) { } 47 update_iterator(const update_iterator& x) : d(x.d) { }
48 48
49 update_iterator& operator*() { return *this; } 49 update_iterator& operator*() { return *this; }
50 update_iterator& operator++() { return *this; } 50 update_iterator& operator++() { return *this; }
51 update_iterator& operator++(int) { return *this; } 51 update_iterator& operator++(int) { return *this; }
52 52
53 update_iterator& operator=(const T& x) { 53 update_iterator& operator=(const T& x) {
54 d->update(x); return *this; 54 d->update(x); return *this;
55 } 55 }
56 }; 56 };
57 57
58 template<typename T> 58 template<typename T>
59 update_iterator<T> updater() { 59 update_iterator<T> updater() {
60 return update_iterator<T>(this); 60 return update_iterator<T>(this);
61 } 61 }
62 62
63}; 63};
64template<> inline void md5_digester::update<binary_t>(const binary_t& x) { 64template<> inline void md5_digester::update<binary_t>(const binary_t& x) {
65 update((const unsigned char*)&(x.front()),x.size()); 65 update((const unsigned char*)&(x.front()),x.size());
66} 66}
67 67
68#pragma pack(1) 68#pragma pack(1)
69struct block512_t { 69struct block512_t {
70 enum { words = 512 / sizeof(uint16_t) }; 70 enum { words = 512 / sizeof(uint16_t) };
71 uint16_t data[words]; 71 uint16_t data[words];
72 72
73 inline uint8_t *dptr(size_t o) { return ((uint8_t*)this)+o; } 73 inline uint8_t *dptr(size_t o) { return ((uint8_t*)this)+o; }
74 74
75 static uint16_t tcpcksum(block512_t& data); 75 static uint16_t tcpcksum(block512_t& data);
76}; 76};
77#pragma pack() 77#pragma pack()
78 78
79struct integrity_digester { 79struct integrity_digester {
80 md5_digester md5; 80 md5_digester md5;
81 size_t data_size; 81 size_t data_size;
82 block512_t data; 82 block512_t data;
83 83
84 integrity_digester() : data_size(0) { } 84 integrity_digester() : data_size(0) { }
85 void update(const void *d,size_t s); 85 void update(const void *d,size_t s);
86 binary_t final(const std::string& ukey); 86 binary_t final(const std::string& ukey);
87}; 87};
88 88
89 89
90class tmpdir_t { 90class tmpdir_t {
91 public: 91 public:
92 std::string dir; 92 std::string dir;
93 93
94 tmpdir_t(const std::string& dt); 94 tmpdir_t(const std::string& dt);
95 ~tmpdir_t(); 95 ~tmpdir_t();
96 96
97 std::string get_file(const std::string& f); 97 std::string get_file(const std::string& f);
98}; 98};
99 99
100class tarchive_t { 100class tarchive_t {
101 public: 101 public:
102 struct archive *a; 102 struct archive *a;
103 struct archive_entry *e; 103 struct archive_entry *e;
104 104
105 tarchive_t(void *p,size_t s); 105 tarchive_t(const char *);
106 ~tarchive_t(); 106 ~tarchive_t();
107 107
108 bool read_next_header(); 108 bool read_next_header();
109 109
110 std::string entry_pathname(); 110 std::string entry_pathname();
111 111
112 bool read_data_into_fd(int fd); 112 bool read_data_into_fd(int fd);
113}; 113};
114 114
115struct mimewrite_base { 115struct mimewrite_base {
116 virtual ~mimewrite_base() { } 116 virtual ~mimewrite_base() { }
117 117
118 virtual int write(const char *buf,size_t len) = 0; 118 virtual int write(const char *buf,size_t len) = 0;
119 virtual void close() = 0; 119 virtual void close() = 0;
120}; 120};
121struct mimewrite_string : public mimewrite_base { 121struct mimewrite_string : public mimewrite_base {
122 std::string str; 122 std::string str;
123 int write(const char *buf,size_t len) { str.append(buf,len); return SOAP_OK; }; 123 int write(const char *buf,size_t len) { str.append(buf,len); return SOAP_OK; };
124 void close() { } 124 void close() { }
125}; 125};
126struct mimewrite_tarfile : public mimewrite_base { 126struct mimewrite_tarfile : public mimewrite_base {
127 std::string fn; 127 std::string fn;
128 std::fstream f; 128 std::fstream f;
129 integrity_digester idigest; 129 integrity_digester idigest;
130 mimewrite_tarfile(tmpdir_t& d); 130 mimewrite_tarfile(tmpdir_t& d);
131 ~mimewrite_tarfile(); 131 ~mimewrite_tarfile();
132 int write(const char *buf,size_t len); 132 int write(const char *buf,size_t len);
133 void close() { } 133 void close() { }
134}; 134};
135binary_t integrity_digest(const void *ptr,size_t size, 135binary_t integrity_digest(const void *ptr,size_t size,
136 const std::string& ukey); 136 const std::string& ukey);
137 137
138#endif /* __EYETIL_H */ 138#endif /* __EYETIL_H */