-rw-r--r-- | src/eyefiservice.cc | 290 |
1 files changed, 0 insertions, 290 deletions
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc deleted file mode 100644 index 4a4a179..0000000 --- a/src/eyefiservice.cc +++ b/dev/null | |||
@@ -1,290 +0,0 @@ | |||
1 | #include <cassert> | ||
2 | #include <iostream> | ||
3 | #include <fstream> | ||
4 | #include <stdexcept> | ||
5 | #include <iterator> | ||
6 | #include <algorithm> | ||
7 | #include <syslog.h> | ||
8 | #include <sys/wait.h> | ||
9 | #include <autosprintf.h> | ||
10 | #include "eyekinfig.h" | ||
11 | #include "eyetil.h" | ||
12 | #include "soapeyefiService.h" | ||
13 | #ifdef HAVE_SQLITE | ||
14 | # include "iiidb.h" | ||
15 | #endif | ||
16 | |||
17 | static 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 */ | ||
32 | |||
33 | static bool detached_child() { | ||
34 | pid_t p = fork(); | ||
35 | if(p<0) { | ||
36 | syslog(LOG_ERR,"Failed to fork away for hook execution"); | ||
37 | _exit(-1); | ||
38 | } | ||
39 | if(!p) { | ||
40 | setsid(); | ||
41 | for(int i=getdtablesize();i>=0;--i) close(i); | ||
42 | int i=open("/dev/null",O_RDWR); assert(i==0); | ||
43 | i = dup(i); assert(i==1); | ||
44 | i = dup(i); assert(i==2); | ||
45 | return true; | ||
46 | } | ||
47 | return false; | ||
48 | } | ||
49 | |||
50 | static int E(eyefiService* efs,const char *c,const std::exception& e) { | ||
51 | efs->keep_alive=0; | ||
52 | syslog(LOG_ERR,"error while processing %s: %s",c,e.what()); | ||
53 | return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0); | ||
54 | } | ||
55 | |||
56 | int eyefiService::StartSession( | ||
57 | std::string macaddress,std::string cnonce, | ||
58 | int transfermode,long transfermodetimestamp, | ||
59 | struct rns__StartSessionResponse &r ) try { | ||
60 | syslog(LOG_INFO, | ||
61 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", | ||
62 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); | ||
63 | eyekinfig_t eyekinfig(macaddress); | ||
64 | r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); | ||
65 | |||
66 | r.snonce = session_nonce.make_nonce().hex(); | ||
67 | r.transfermode=transfermode; | ||
68 | r.transfermodetimestamp=transfermodetimestamp; | ||
69 | r.upsyncallowed=false; | ||
70 | |||
71 | std::string cmd = eyekinfig.get_on_start_session(); | ||
72 | if(!cmd.empty()) { | ||
73 | if(detached_child()) { | ||
74 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | ||
75 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); | ||
76 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); | ||
77 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | ||
78 | execv("/bin/sh",argv); | ||
79 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | ||
80 | _exit(-1); | ||
81 | } | ||
82 | } | ||
83 | return SOAP_OK; | ||
84 | }catch(const std::exception& e) { return E(this,"StartSession",e); } | ||
85 | |||
86 | int eyefiService::GetPhotoStatus( | ||
87 | std::string credential, std::string macaddress, | ||
88 | std::string filename, long filesize, std::string filesignature, | ||
89 | int flags, | ||
90 | struct rns__GetPhotoStatusResponse &r ) try { | ||
91 | syslog(LOG_INFO, | ||
92 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", | ||
93 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, | ||
94 | session_nonce.hex().c_str() ); | ||
95 | |||
96 | eyekinfig_t eyekinfig(macaddress); | ||
97 | std::string computed_credential = binary_t(macaddress+eyekinfig.get_upload_key()+session_nonce.hex()).md5().hex(); | ||
98 | |||
99 | #ifndef NDEBUG | ||
100 | syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); | ||
101 | #endif | ||
102 | |||
103 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); | ||
104 | |||
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 */ | ||
124 | return SOAP_OK; | ||
125 | }catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } | ||
126 | |||
127 | int eyefiService::MarkLastPhotoInRoll( | ||
128 | std::string macaddress, int mergedelta, | ||
129 | struct rns__MarkLastPhotoInRollResponse&/* r */ ) try { | ||
130 | syslog(LOG_INFO, | ||
131 | "MarkLastPhotoInRoll request from %s with mergedelta=%d", | ||
132 | macaddress.c_str(), mergedelta ); | ||
133 | std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); | ||
134 | if(!cmd.empty()) { | ||
135 | if(detached_child()) { | ||
136 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | ||
137 | putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); | ||
138 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | ||
139 | execv("/bin/sh",argv); | ||
140 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | ||
141 | _exit(-1); | ||
142 | } | ||
143 | } | ||
144 | keep_alive = 0; | ||
145 | return SOAP_OK; | ||
146 | }catch(const std::exception& e) { return E(this,"MarkLastPhotoInRoll",e); } | ||
147 | |||
148 | int eyefiService::UploadPhoto( | ||
149 | int fileid, std::string macaddress, | ||
150 | std::string filename, long filesize, std::string filesignature, | ||
151 | std::string encryption, int flags, | ||
152 | struct rns__UploadPhotoResponse& r ) try { | ||
153 | syslog(LOG_INFO, | ||
154 | "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," | ||
155 | " filesignature=%s, encryption=%s, flags=%04X", | ||
156 | macaddress.c_str(), fileid, filename.c_str(), filesize, | ||
157 | filesignature.c_str(), encryption.c_str(), flags ); | ||
158 | std::string::size_type fnl=filename.length(); | ||
159 | if(fnl<sizeof(".tar") || strncmp(filename.c_str()+fnl-sizeof(".tar")+sizeof(""),".tar",sizeof(".tar"))) | ||
160 | throw std::runtime_error(gnu::autosprintf("honestly, I expected the tarball coming here, not '%s'",filename.c_str())); | ||
161 | std::string the_file(filename,0,fnl-sizeof(".tar")+sizeof("")); | ||
162 | std::string the_log = the_file+".log"; | ||
163 | |||
164 | eyekinfig_t eyekinfig(macaddress); | ||
165 | |||
166 | umask(eyekinfig.get_umask()); | ||
167 | |||
168 | std::string td = eyekinfig.get_targetdir(); | ||
169 | tmpdir_t indir(td+"/.incoming.XXXXXX"); | ||
170 | |||
171 | std::string tf,lf; | ||
172 | binary_t digest, idigest; | ||
173 | #ifdef HAVE_SQLITE | ||
174 | bool beenthere = false; | ||
175 | #endif | ||
176 | |||
177 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { | ||
178 | #ifndef NDEBUG | ||
179 | syslog(LOG_DEBUG, | ||
180 | " MIME attachment with id=%s, type=%s, size=%ld", | ||
181 | (*i).id, (*i).type, (long)(*i).size ); | ||
182 | #endif | ||
183 | |||
184 | if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { | ||
185 | std::string idigestr((*i).ptr,(*i).size); | ||
186 | #ifndef NDEBUG | ||
187 | syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); | ||
188 | #endif | ||
189 | idigest.from_hex(idigestr); | ||
190 | } | ||
191 | if( (*i).id && !strcmp((*i).id,"FILENAME") ) { | ||
192 | assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); | ||
193 | #ifdef III_SAVE_TARS | ||
194 | std::string tarfile = indir.get_file(filename); | ||
195 | { | ||
196 | std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); | ||
197 | } | ||
198 | #endif | ||
199 | |||
200 | if(!tf.empty()) throw std::runtime_error("already seen tarball"); | ||
201 | if(!digest.empty()) throw std::runtime_error("already have integrity digest"); | ||
202 | digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); | ||
203 | #ifndef NDEBUG | ||
204 | syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); | ||
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 | ||
213 | |||
214 | tarchive_t a((*i).ptr,(*i).size); | ||
215 | while(a.read_next_header()) { | ||
216 | std::string ep = a.entry_pathname(), f = indir.get_file(ep); | ||
217 | if(ep==the_file) tf = f; | ||
218 | else if(ep==the_log) lf = f; | ||
219 | else continue; | ||
220 | int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666); | ||
221 | if(fd<0) | ||
222 | throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str())); | ||
223 | if(!a.read_data_into_fd(fd)) | ||
224 | throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); | ||
225 | close(fd); | ||
226 | } | ||
227 | } | ||
228 | } | ||
229 | |||
230 | #ifdef HAVE_SQLITE | ||
231 | if(beenthere) { | ||
232 | r.success=true; | ||
233 | return SOAP_OK; | ||
234 | } | ||
235 | #endif | ||
236 | |||
237 | if(tf.empty()) throw std::runtime_error("haven't seen THE file"); | ||
238 | if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); | ||
239 | |||
240 | std::string::size_type ls = tf.rfind('/'); | ||
241 | // XXX: actually, lack of '/' signifies error here | ||
242 | std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); | ||
243 | ls = lf.rfind('/'); | ||
244 | std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); | ||
245 | std::string ttf,tlf; | ||
246 | bool success = false; | ||
247 | for(int i=0;i<32767;++i) { | ||
248 | const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s"; | ||
249 | ttf = (const char*)gnu::autosprintf(fmt,td.c_str(),tbn.c_str(),i); | ||
250 | if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i); | ||
251 | if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty() || !link(lf.c_str(),tlf.c_str())) ) { | ||
252 | unlink(tf.c_str()); | ||
253 | if(!lf.empty()) unlink(lf.c_str()); | ||
254 | success=true; | ||
255 | break; | ||
256 | } | ||
257 | } | ||
258 | std::string cmd = eyekinfig.get_on_upload_photo(); | ||
259 | if(success) { | ||
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()) { | ||
277 | putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); | ||
278 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | ||
279 | putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) ); | ||
280 | if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) ); | ||
281 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | ||
282 | execv("/bin/sh",argv); | ||
283 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | ||
284 | _exit(-1); | ||
285 | } | ||
286 | } | ||
287 | |||
288 | r.success = true; | ||
289 | return SOAP_OK; | ||
290 | }catch(const std::exception& e) { return E(this,"UploadPhoto",e); } | ||