summaryrefslogtreecommitdiffabout
path: root/src/eyefiservice.cc
Unidiff
Diffstat (limited to 'src/eyefiservice.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyefiservice.cc77
1 files changed, 73 insertions, 4 deletions
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc
index 93bbcca..4a4a179 100644
--- a/src/eyefiservice.cc
+++ b/src/eyefiservice.cc
@@ -7,14 +7,31 @@
7#include <syslog.h> 7#include <syslog.h>
8#include <sys/wait.h> 8#include <sys/wait.h>
9#include <autosprintf.h> 9#include <autosprintf.h>
10#include "eyekinfig.h" 10#include "eyekinfig.h"
11#include "eyetil.h" 11#include "eyetil.h"
12#include "soapeyefiService.h" 12#include "soapeyefiService.h"
13#ifdef HAVE_SQLITE
14# include "iiidb.h"
15#endif
13 16
14static binary_t session_nonce; 17static binary_t session_nonce;
18#ifdef HAVE_SQLITE
19 static struct {
20 std::string filesignature;
21 long filesize;
22 std::string filename;
23 inline void reset() { filesignature.erase(); filename.erase(); filesize=0; }
24 inline void set(const std::string n,const std::string sig,long siz) {
25 filename = n; filesignature = sig; filesize = siz;
26 }
27 inline bool is(const std::string n,const std::string sig,long siz) {
28 return filesize==siz && filename==n && filesignature==sig;
29 }
30 } already;
31#endif /* HAVE_SQLITE */
15 32
16static bool detached_child() { 33static bool detached_child() {
17 pid_t p = fork(); 34 pid_t p = fork();
18 if(p<0) { 35 if(p<0) {
19 syslog(LOG_ERR,"Failed to fork away for hook execution"); 36 syslog(LOG_ERR,"Failed to fork away for hook execution");
20 _exit(-1); 37 _exit(-1);
@@ -73,21 +90,40 @@ int eyefiService::GetPhotoStatus(
73 struct rns__GetPhotoStatusResponse &r ) try { 90 struct rns__GetPhotoStatusResponse &r ) try {
74 syslog(LOG_INFO, 91 syslog(LOG_INFO,
75 "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", 92 "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s",
76 macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, 93 macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags,
77 session_nonce.hex().c_str() ); 94 session_nonce.hex().c_str() );
78 95
79 std::string computed_credential = binary_t(macaddress+eyekinfig_t(macaddress).get_upload_key()+session_nonce.hex()).md5().hex(); 96 eyekinfig_t eyekinfig(macaddress);
97 std::string computed_credential = binary_t(macaddress+eyekinfig.get_upload_key()+session_nonce.hex()).md5().hex();
80 98
81#ifndef NDEBUG 99#ifndef NDEBUG
82 syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); 100 syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str());
83#endif 101#endif
84 102
85 if (credential != computed_credential) throw std::runtime_error("card authentication failed"); 103 if (credential != computed_credential) throw std::runtime_error("card authentication failed");
86 104
87 r.fileid = 1; r.offset = 0; 105#ifdef HAVE_SQLITE
106 iiidb_t D(eyekinfig);
107 seclude::stmt_t S = D.prepare(
108 "SELECT fileid FROM photo"
109 " WHERE mac=:mac AND filename=:filename"
110 " AND filesize=:filesize AND filesignature=:filesignature"
111 ).bind(":mac",macaddress)
112 .bind(":filename",filename).bind(":filesize",filesize)
113 .bind(":filesignature",filesignature);
114 if(!S.step()) {
115 r.fileid = 1; r.offset = 0;
116 }else{
117 r.fileid = S.column<long>(0);
118 r.offset = filesize;
119 already.set(filename,filesignature,filesize);
120 }
121#else /* HAVE_SQLITE */
122 r.fileid=1, r.offset=0;
123#endif /* HAVE_SQLITE */
88 return SOAP_OK; 124 return SOAP_OK;
89}catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } 125}catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); }
90 126
91int eyefiService::MarkLastPhotoInRoll( 127int eyefiService::MarkLastPhotoInRoll(
92 std::string macaddress, int mergedelta, 128 std::string macaddress, int mergedelta,
93 struct rns__MarkLastPhotoInRollResponse&/* r */ ) try { 129 struct rns__MarkLastPhotoInRollResponse&/* r */ ) try {
@@ -131,12 +167,15 @@ int eyefiService::UploadPhoto(
131 167
132 std::string td = eyekinfig.get_targetdir(); 168 std::string td = eyekinfig.get_targetdir();
133 tmpdir_t indir(td+"/.incoming.XXXXXX"); 169 tmpdir_t indir(td+"/.incoming.XXXXXX");
134 170
135 std::string tf,lf; 171 std::string tf,lf;
136 binary_t digest, idigest; 172 binary_t digest, idigest;
173#ifdef HAVE_SQLITE
174 bool beenthere = false;
175#endif
137 176
138 for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { 177 for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) {
139#ifndef NDEBUG 178#ifndef NDEBUG
140 syslog(LOG_DEBUG, 179 syslog(LOG_DEBUG,
141 " MIME attachment with id=%s, type=%s, size=%ld", 180 " MIME attachment with id=%s, type=%s, size=%ld",
142 (*i).id, (*i).type, (long)(*i).size ); 181 (*i).id, (*i).type, (long)(*i).size );
@@ -161,12 +200,19 @@ int eyefiService::UploadPhoto(
161 if(!tf.empty()) throw std::runtime_error("already seen tarball"); 200 if(!tf.empty()) throw std::runtime_error("already seen tarball");
162 if(!digest.empty()) throw std::runtime_error("already have integrity digest"); 201 if(!digest.empty()) throw std::runtime_error("already have integrity digest");
163 digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); 202 digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key());
164#ifndef NDEBUG 203#ifndef NDEBUG
165 syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); 204 syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str());
166#endif 205#endif
206#ifdef HAVE_SQLITE
207 if(!(*i).size) {
208 if(!already.is(filename,filesignature,filesize))
209 throw std::runtime_error("got zero-length upload for unknown file");
210 beenthere = true; continue;
211 }
212#endif
167 213
168 tarchive_t a((*i).ptr,(*i).size); 214 tarchive_t a((*i).ptr,(*i).size);
169 while(a.read_next_header()) { 215 while(a.read_next_header()) {
170 std::string ep = a.entry_pathname(), f = indir.get_file(ep); 216 std::string ep = a.entry_pathname(), f = indir.get_file(ep);
171 if(ep==the_file) tf = f; 217 if(ep==the_file) tf = f;
172 else if(ep==the_log) lf = f; 218 else if(ep==the_log) lf = f;
@@ -178,12 +224,19 @@ int eyefiService::UploadPhoto(
178 throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); 224 throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str()));
179 close(fd); 225 close(fd);
180 } 226 }
181 } 227 }
182 } 228 }
183 229
230#ifdef HAVE_SQLITE
231 if(beenthere) {
232 r.success=true;
233 return SOAP_OK;
234 }
235#endif
236
184 if(tf.empty()) throw std::runtime_error("haven't seen THE file"); 237 if(tf.empty()) throw std::runtime_error("haven't seen THE file");
185 if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); 238 if(digest!=idigest) throw std::runtime_error("integrity digest verification failed");
186 239
187 std::string::size_type ls = tf.rfind('/'); 240 std::string::size_type ls = tf.rfind('/');
188 // XXX: actually, lack of '/' signifies error here 241 // XXX: actually, lack of '/' signifies error here
189 std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); 242 std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1);
@@ -200,14 +253,30 @@ int eyefiService::UploadPhoto(
200 if(!lf.empty()) unlink(lf.c_str()); 253 if(!lf.empty()) unlink(lf.c_str());
201 success=true; 254 success=true;
202 break; 255 break;
203 } 256 }
204 } 257 }
205 std::string cmd = eyekinfig.get_on_upload_photo(); 258 std::string cmd = eyekinfig.get_on_upload_photo();
206 if(success && !cmd.empty()) { 259 if(success) {
207 if(detached_child()) { 260#ifdef HAVE_SQLITE
261 {
262 iiidb_t D(eyekinfig);
263 D.prepare(
264 "INSERT INTO photo"
265 " (ctime,mac,fileid,filename,filesize,filesignature,encryption,flags)"
266 " VALUES"
267 " (:ctime,:mac,:fileid,:filename,:filesize,:filesignature,:encryption,:flags)"
268 ).bind(":ctime",time(0))
269 .bind(":mac",macaddress)
270 .bind(":fileid",fileid).bind(":filename",filename)
271 .bind(":filesize",filesize).bind(":filesignature",filesignature)
272 .bind(":encryption",encryption).bind(":flags",flags)
273 .step();
274 }
275#endif /* HAVE_SQLITE */
276 if((!cmd.empty()) && detached_child()) {
208 putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); 277 putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) );
209 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); 278 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) );
210 putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) ); 279 putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) );
211 if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) ); 280 if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) );
212 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; 281 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 };
213 execv("/bin/sh",argv); 282 execv("/bin/sh",argv);