-rw-r--r-- | src/eyefiworker.cc | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/src/eyefiworker.cc b/src/eyefiworker.cc index 597a3f2..ac96c7f 100644 --- a/src/eyefiworker.cc +++ b/src/eyefiworker.cc | |||
@@ -1,344 +1,350 @@ | |||
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 | ||
21 | #ifdef WITH_IPV6 | ||
22 | # define BINDTO "::" | ||
23 | #else | ||
24 | # define BINDTO 0 | ||
25 | #endif | ||
26 | |||
21 | eyefiworker::eyefiworker() | 27 | eyefiworker::eyefiworker() |
22 | : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) { | 28 | : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) { |
23 | bind_flags = SO_REUSEADDR; max_keep_alive = 0; | 29 | bind_flags = SO_REUSEADDR; max_keep_alive = 0; |
24 | socket_flags = | 30 | socket_flags = |
25 | #if defined(MSG_NOSIGNAL) | 31 | #if defined(MSG_NOSIGNAL) |
26 | MSG_NOSIGNAL | 32 | MSG_NOSIGNAL |
27 | #elif defined(SO_NOSIGPIPE) | 33 | #elif defined(SO_NOSIGPIPE) |
28 | SO_NOSIGPIPE | 34 | SO_NOSIGPIPE |
29 | #else | 35 | #else |
30 | #error Something is wrong with sigpipe prevention on the platform | 36 | #error Something is wrong with sigpipe prevention on the platform |
31 | #endif | 37 | #endif |
32 | ; | 38 | ; |
33 | #ifdef HAVE_SQLITE | 39 | #ifdef HAVE_SQLITE |
34 | sqlite3_initialize(); | 40 | sqlite3_initialize(); |
35 | #endif | 41 | #endif |
36 | } | 42 | } |
37 | 43 | ||
38 | static void *fmimewriteopen_(struct soap *soap, | 44 | static void *fmimewriteopen_(struct soap *soap, |
39 | void *handle, const char *id, const char *type, const char *description, | 45 | void *handle, const char *id, const char *type, const char *description, |
40 | enum soap_mime_encoding encoding) { | 46 | enum soap_mime_encoding encoding) { |
41 | return static_cast<eyefiworker*>(soap)->mime_writeopen(handle,id,type,description,encoding); | 47 | return static_cast<eyefiworker*>(soap)->mime_writeopen(handle,id,type,description,encoding); |
42 | } | 48 | } |
43 | static int fmimewrite_(struct soap *soap,void *handle,const char *buf,size_t len) { | 49 | static int fmimewrite_(struct soap *soap,void *handle,const char *buf,size_t len) { |
44 | return static_cast<eyefiworker*>(soap)->mime_write(handle,buf,len); | 50 | return static_cast<eyefiworker*>(soap)->mime_write(handle,buf,len); |
45 | } | 51 | } |
46 | static void fmimewriteclose_(struct soap *soap,void *handle) { | 52 | static void fmimewriteclose_(struct soap *soap,void *handle) { |
47 | static_cast<eyefiworker*>(soap)->mime_writeclose(handle); | 53 | static_cast<eyefiworker*>(soap)->mime_writeclose(handle); |
48 | } | 54 | } |
49 | 55 | ||
50 | int eyefiworker::run(int bindport) { | 56 | int eyefiworker::run(int bindport) { |
51 | if(!soap_valid_socket(bind(0,bindport,64))) | 57 | if(!soap_valid_socket(bind(BINDTO,bindport,64))) |
52 | throw std::runtime_error("failed to bind()"); | 58 | throw std::runtime_error("failed to bind()"); |
53 | signal(SIGCHLD,SIG_IGN); | 59 | signal(SIGCHLD,SIG_IGN); |
54 | fmimewriteopen=fmimewriteopen_; fmimewrite=fmimewrite_; fmimewriteclose=fmimewriteclose_; | 60 | fmimewriteopen=fmimewriteopen_; fmimewrite=fmimewrite_; fmimewriteclose=fmimewriteclose_; |
55 | while(true) { | 61 | while(true) { |
56 | if(!soap_valid_socket(accept())) | 62 | if(!soap_valid_socket(accept())) |
57 | throw std::runtime_error("failed to accept()"); | 63 | throw std::runtime_error("failed to accept()"); |
58 | pid_t p = fork(); | 64 | pid_t p = fork(); |
59 | if(p<0) throw std::runtime_error("failed to fork()"); | 65 | if(p<0) throw std::runtime_error("failed to fork()"); |
60 | if(!p) { | 66 | if(!p) { |
61 | recv_timeout = 600; send_timeout = 120; | 67 | recv_timeout = 600; send_timeout = 120; |
62 | (void)serve(); | 68 | (void)serve(); |
63 | soap_destroy(this); soap_end(this); soap_done(this); | 69 | soap_destroy(this); soap_end(this); soap_done(this); |
64 | #ifndef NDEBUG | 70 | #ifndef NDEBUG |
65 | struct rusage ru; | 71 | struct rusage ru; |
66 | if(getrusage(RUSAGE_SELF,&ru)) { | 72 | if(getrusage(RUSAGE_SELF,&ru)) { |
67 | syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno); | 73 | syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno); |
68 | }else{ | 74 | }else{ |
69 | syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss); | 75 | syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss); |
70 | } | 76 | } |
71 | #endif /* NDEBUG */ | 77 | #endif /* NDEBUG */ |
72 | throw throwable_exit(0); | 78 | throw throwable_exit(0); |
73 | } | 79 | } |
74 | close(socket); socket = SOAP_INVALID_SOCKET; | 80 | close(socket); socket = SOAP_INVALID_SOCKET; |
75 | } | 81 | } |
76 | } | 82 | } |
77 | 83 | ||
78 | static binary_t session_nonce; | 84 | static binary_t session_nonce; |
79 | #ifdef HAVE_SQLITE | 85 | #ifdef HAVE_SQLITE |
80 | static struct { | 86 | static struct { |
81 | std::string filesignature; | 87 | std::string filesignature; |
82 | long filesize; | 88 | long filesize; |
83 | std::string filename; | 89 | std::string filename; |
84 | inline void reset() { filesignature.erase(); filename.erase(); filesize=0; } | 90 | inline void reset() { filesignature.erase(); filename.erase(); filesize=0; } |
85 | inline void set(const std::string n,const std::string sig,long siz) { | 91 | inline void set(const std::string n,const std::string sig,long siz) { |
86 | filename = n; filesignature = sig; filesize = siz; | 92 | filename = n; filesignature = sig; filesize = siz; |
87 | } | 93 | } |
88 | inline bool is(const std::string n,const std::string sig,long siz) { | 94 | inline bool is(const std::string n,const std::string sig,long siz) { |
89 | return filesize==siz && filename==n && filesignature==sig; | 95 | return filesize==siz && filename==n && filesignature==sig; |
90 | } | 96 | } |
91 | } already; | 97 | } already; |
92 | #endif /* HAVE_SQLITE */ | 98 | #endif /* HAVE_SQLITE */ |
93 | 99 | ||
94 | static bool detached_child() { | 100 | static bool detached_child() { |
95 | pid_t p = fork(); | 101 | pid_t p = fork(); |
96 | if(p<0) { | 102 | if(p<0) { |
97 | syslog(LOG_ERR,"Failed to fork away for hook execution"); | 103 | syslog(LOG_ERR,"Failed to fork away for hook execution"); |
98 | _exit(-1); | 104 | _exit(-1); |
99 | } | 105 | } |
100 | if(!p) { | 106 | if(!p) { |
101 | setsid(); | 107 | setsid(); |
102 | for(int i=getdtablesize();i>=0;--i) close(i); | 108 | for(int i=getdtablesize();i>=0;--i) close(i); |
103 | int i=open("/dev/null",O_RDWR); assert(i==0); | 109 | int i=open("/dev/null",O_RDWR); assert(i==0); |
104 | i = dup(i); assert(i==1); | 110 | i = dup(i); assert(i==1); |
105 | i = dup(i); assert(i==2); | 111 | i = dup(i); assert(i==2); |
106 | return true; | 112 | return true; |
107 | } | 113 | } |
108 | return false; | 114 | return false; |
109 | } | 115 | } |
110 | 116 | ||
111 | static int E(eyefiworker* efs,const char *c,const std::exception& e) { | 117 | static int E(eyefiworker* efs,const char *c,const std::exception& e) { |
112 | efs->keep_alive=0; | 118 | efs->keep_alive=0; |
113 | syslog(LOG_ERR,"error while processing %s: %s",c,e.what()); | 119 | syslog(LOG_ERR,"error while processing %s: %s",c,e.what()); |
114 | return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0); | 120 | return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0); |
115 | } | 121 | } |
116 | 122 | ||
117 | int eyefiworker::StartSession( | 123 | int eyefiworker::StartSession( |
118 | std::string macaddress,std::string cnonce, | 124 | std::string macaddress,std::string cnonce, |
119 | int transfermode,long transfermodetimestamp, | 125 | int transfermode,long transfermodetimestamp, |
120 | struct rns__StartSessionResponse &r ) try { | 126 | struct rns__StartSessionResponse &r ) try { |
121 | syslog(LOG_INFO, | 127 | syslog(LOG_INFO, |
122 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", | 128 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", |
123 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); | 129 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); |
124 | kinfig.reset(new eyekinfig_t(macaddress)); | 130 | kinfig.reset(new eyekinfig_t(macaddress)); |
125 | umask(kinfig->get_umask()); | 131 | umask(kinfig->get_umask()); |
126 | 132 | ||
127 | r.credential = binary_t(macaddress+cnonce+kinfig->get_upload_key()).md5().hex(); | 133 | r.credential = binary_t(macaddress+cnonce+kinfig->get_upload_key()).md5().hex(); |
128 | 134 | ||
129 | r.snonce = session_nonce.make_nonce().hex(); | 135 | r.snonce = session_nonce.make_nonce().hex(); |
130 | r.transfermode=transfermode; | 136 | r.transfermode=transfermode; |
131 | r.transfermodetimestamp=transfermodetimestamp; | 137 | r.transfermodetimestamp=transfermodetimestamp; |
132 | r.upsyncallowed=false; | 138 | r.upsyncallowed=false; |
133 | 139 | ||
134 | std::string cmd = kinfig->get_on_start_session(); | 140 | std::string cmd = kinfig->get_on_start_session(); |
135 | if(!cmd.empty()) { | 141 | if(!cmd.empty()) { |
136 | if(detached_child()) { | 142 | if(detached_child()) { |
137 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 143 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
138 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); | 144 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); |
139 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); | 145 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); |
140 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 146 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
141 | execv("/bin/sh",argv); | 147 | execv("/bin/sh",argv); |
142 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 148 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
143 | _exit(-1); | 149 | _exit(-1); |
144 | } | 150 | } |
145 | } | 151 | } |
146 | return SOAP_OK; | 152 | return SOAP_OK; |
147 | }catch(const std::exception& e) { return E(this,"StartSession",e); } | 153 | }catch(const std::exception& e) { return E(this,"StartSession",e); } |
148 | 154 | ||
149 | int eyefiworker::GetPhotoStatus( | 155 | int eyefiworker::GetPhotoStatus( |
150 | std::string credential, std::string macaddress, | 156 | std::string credential, std::string macaddress, |
151 | std::string filename, long filesize, std::string filesignature, | 157 | std::string filename, long filesize, std::string filesignature, |
152 | int flags, | 158 | int flags, |
153 | struct rns__GetPhotoStatusResponse &r ) try { | 159 | struct rns__GetPhotoStatusResponse &r ) try { |
154 | syslog(LOG_INFO, | 160 | syslog(LOG_INFO, |
155 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", | 161 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", |
156 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, | 162 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, |
157 | session_nonce.hex().c_str() ); | 163 | session_nonce.hex().c_str() ); |
158 | 164 | ||
159 | if(!(kinfig && kinfig->macaddress==macaddress)) | 165 | if(!(kinfig && kinfig->macaddress==macaddress)) |
160 | throw std::runtime_error("I'm not talking to this peer"); | 166 | throw std::runtime_error("I'm not talking to this peer"); |
161 | 167 | ||
162 | std::string computed_credential = binary_t(macaddress+kinfig->get_upload_key()+session_nonce.hex()).md5().hex(); | 168 | std::string computed_credential = binary_t(macaddress+kinfig->get_upload_key()+session_nonce.hex()).md5().hex(); |
163 | 169 | ||
164 | #ifndef NDEBUG | 170 | #ifndef NDEBUG |
165 | syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); | 171 | syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); |
166 | #endif | 172 | #endif |
167 | 173 | ||
168 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); | 174 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); |
169 | 175 | ||
170 | indir.reset(new tmpdir_t(kinfig->get_targetdir()+"/.incoming.XXXXXX")); | 176 | indir.reset(new tmpdir_t(kinfig->get_targetdir()+"/.incoming.XXXXXX")); |
171 | 177 | ||
172 | #ifdef HAVE_SQLITE | 178 | #ifdef HAVE_SQLITE |
173 | iiidb_t D(*kinfig); | 179 | iiidb_t D(*kinfig); |
174 | seclude::stmt_t S = D.prepare( | 180 | seclude::stmt_t S = D.prepare( |
175 | "SELECT fileid FROM photo" | 181 | "SELECT fileid FROM photo" |
176 | " WHERE mac=:mac AND filename=:filename" | 182 | " WHERE mac=:mac AND filename=:filename" |
177 | " AND filesize=:filesize AND filesignature=:filesignature" | 183 | " AND filesize=:filesize AND filesignature=:filesignature" |
178 | ).bind(":mac",macaddress) | 184 | ).bind(":mac",macaddress) |
179 | .bind(":filename",filename).bind(":filesize",filesize) | 185 | .bind(":filename",filename).bind(":filesize",filesize) |
180 | .bind(":filesignature",filesignature); | 186 | .bind(":filesignature",filesignature); |
181 | if(!S.step()) { | 187 | if(!S.step()) { |
182 | r.fileid = 1; r.offset = 0; | 188 | r.fileid = 1; r.offset = 0; |
183 | }else{ | 189 | }else{ |
184 | r.fileid = S.column<long>(0); | 190 | r.fileid = S.column<long>(0); |
185 | r.offset = filesize; | 191 | r.offset = filesize; |
186 | already.set(filename,filesignature,filesize); | 192 | already.set(filename,filesignature,filesize); |
187 | } | 193 | } |
188 | #else /* HAVE_SQLITE */ | 194 | #else /* HAVE_SQLITE */ |
189 | r.fileid=1, r.offset=0; | 195 | r.fileid=1, r.offset=0; |
190 | #endif /* HAVE_SQLITE */ | 196 | #endif /* HAVE_SQLITE */ |
191 | return SOAP_OK; | 197 | return SOAP_OK; |
192 | }catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } | 198 | }catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } |
193 | 199 | ||
194 | int eyefiworker::MarkLastPhotoInRoll( | 200 | int eyefiworker::MarkLastPhotoInRoll( |
195 | std::string macaddress, int mergedelta, | 201 | std::string macaddress, int mergedelta, |
196 | struct rns__MarkLastPhotoInRollResponse&/* r */ ) try { | 202 | struct rns__MarkLastPhotoInRollResponse&/* r */ ) try { |
197 | syslog(LOG_INFO, | 203 | syslog(LOG_INFO, |
198 | "MarkLastPhotoInRoll request from %s with mergedelta=%d", | 204 | "MarkLastPhotoInRoll request from %s with mergedelta=%d", |
199 | macaddress.c_str(), mergedelta ); | 205 | macaddress.c_str(), mergedelta ); |
200 | if(!(kinfig && kinfig->macaddress==macaddress)) | 206 | if(!(kinfig && kinfig->macaddress==macaddress)) |
201 | throw std::runtime_error("I'm not talking to this peer"); | 207 | throw std::runtime_error("I'm not talking to this peer"); |
202 | 208 | ||
203 | std::string cmd = kinfig->get_on_mark_last_photo_in_roll(); | 209 | std::string cmd = kinfig->get_on_mark_last_photo_in_roll(); |
204 | if(!cmd.empty()) { | 210 | if(!cmd.empty()) { |
205 | if(detached_child()) { | 211 | if(detached_child()) { |
206 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 212 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
207 | putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); | 213 | putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); |
208 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 214 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
209 | execv("/bin/sh",argv); | 215 | execv("/bin/sh",argv); |
210 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 216 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
211 | _exit(-1); | 217 | _exit(-1); |
212 | } | 218 | } |
213 | } | 219 | } |
214 | keep_alive = 0; | 220 | keep_alive = 0; |
215 | return SOAP_OK; | 221 | return SOAP_OK; |
216 | }catch(const std::exception& e) { return E(this,"MarkLastPhotoInRoll",e); } | 222 | }catch(const std::exception& e) { return E(this,"MarkLastPhotoInRoll",e); } |
217 | 223 | ||
218 | void *eyefiworker::mime_writeopen(void *handle,const char *id,const char *type,const char *description, | 224 | void *eyefiworker::mime_writeopen(void *handle,const char *id,const char *type,const char *description, |
219 | enum soap_mime_encoding encoding) { | 225 | enum soap_mime_encoding encoding) { |
220 | if(!id) return NULL; | 226 | if(!id) return NULL; |
221 | if(!strcmp(id,"FILENAME")) { | 227 | if(!strcmp(id,"FILENAME")) { |
222 | mime_tarfile.reset(new mimewrite_tarfile(*indir)); | 228 | mime_tarfile.reset(new mimewrite_tarfile(*indir)); |
223 | return mime_tarfile.get(); | 229 | return mime_tarfile.get(); |
224 | }else if(!strcmp(id,"INTEGRITYDIGEST")) { | 230 | }else if(!strcmp(id,"INTEGRITYDIGEST")) { |
225 | mime_idigest.reset(new mimewrite_string()); | 231 | mime_idigest.reset(new mimewrite_string()); |
226 | return mime_idigest.get(); | 232 | return mime_idigest.get(); |
227 | } | 233 | } |
228 | return NULL; | 234 | return NULL; |
229 | } | 235 | } |
230 | int eyefiworker::mime_write(void *handle,const char *buf,size_t len) { | 236 | int eyefiworker::mime_write(void *handle,const char *buf,size_t len) { |
231 | if(!handle) return SOAP_ERR; | 237 | if(!handle) return SOAP_ERR; |
232 | return static_cast<mimewrite_base*>(handle)->write(buf,len); | 238 | return static_cast<mimewrite_base*>(handle)->write(buf,len); |
233 | } | 239 | } |
234 | void eyefiworker::mime_writeclose(void *handle) { | 240 | void eyefiworker::mime_writeclose(void *handle) { |
235 | if(handle) static_cast<mimewrite_base*>(handle)->close(); | 241 | if(handle) static_cast<mimewrite_base*>(handle)->close(); |
236 | } | 242 | } |
237 | 243 | ||
238 | int eyefiworker::UploadPhoto( | 244 | int eyefiworker::UploadPhoto( |
239 | int fileid, std::string macaddress, | 245 | int fileid, std::string macaddress, |
240 | std::string filename, long filesize, std::string filesignature, | 246 | std::string filename, long filesize, std::string filesignature, |
241 | std::string encryption, int flags, | 247 | std::string encryption, int flags, |
242 | struct rns__UploadPhotoResponse& r ) try { | 248 | struct rns__UploadPhotoResponse& r ) try { |
243 | syslog(LOG_INFO, | 249 | syslog(LOG_INFO, |
244 | "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," | 250 | "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," |
245 | " filesignature=%s, encryption=%s, flags=%04X", | 251 | " filesignature=%s, encryption=%s, flags=%04X", |
246 | macaddress.c_str(), fileid, filename.c_str(), filesize, | 252 | macaddress.c_str(), fileid, filename.c_str(), filesize, |
247 | filesignature.c_str(), encryption.c_str(), flags ); | 253 | filesignature.c_str(), encryption.c_str(), flags ); |
248 | if(!(kinfig && kinfig->macaddress==macaddress)) | 254 | if(!(kinfig && kinfig->macaddress==macaddress)) |
249 | throw std::runtime_error("I'm not talking to this peer"); | 255 | throw std::runtime_error("I'm not talking to this peer"); |
250 | 256 | ||
251 | std::string::size_type fnl=filename.length(); | 257 | std::string::size_type fnl=filename.length(); |
252 | if(fnl<sizeof(".tar") || strncmp(filename.c_str()+fnl-sizeof(".tar")+sizeof(""),".tar",sizeof(".tar"))) | 258 | if(fnl<sizeof(".tar") || strncmp(filename.c_str()+fnl-sizeof(".tar")+sizeof(""),".tar",sizeof(".tar"))) |
253 | throw std::runtime_error(gnu::autosprintf("honestly, I expected the tarball coming here, not '%s'",filename.c_str())); | 259 | throw std::runtime_error(gnu::autosprintf("honestly, I expected the tarball coming here, not '%s'",filename.c_str())); |
254 | std::string the_file(filename,0,fnl-sizeof(".tar")+sizeof("")); | 260 | std::string the_file(filename,0,fnl-sizeof(".tar")+sizeof("")); |
255 | std::string the_log = the_file+".log"; | 261 | std::string the_log = the_file+".log"; |
256 | 262 | ||
257 | if(!indir) throw std::runtime_error("I haven't even created a directory!"); | 263 | if(!indir) throw std::runtime_error("I haven't even created a directory!"); |
258 | shared_ptr<tmpdir_t> dir; dir.swap(indir); | 264 | shared_ptr<tmpdir_t> dir; dir.swap(indir); |
259 | if(!mime_tarfile) throw std::runtime_error("I haven't written the tarball!"); | 265 | if(!mime_tarfile) throw std::runtime_error("I haven't written the tarball!"); |
260 | shared_ptr<mimewrite_tarfile> file; file.swap(mime_tarfile); | 266 | shared_ptr<mimewrite_tarfile> file; file.swap(mime_tarfile); |
261 | if(!mime_idigest) throw std::runtime_error("I haven't seen the integrity digest!"); | 267 | if(!mime_idigest) throw std::runtime_error("I haven't seen the integrity digest!"); |
262 | shared_ptr<mimewrite_string> idigest; idigest.swap(mime_idigest); | 268 | shared_ptr<mimewrite_string> idigest; idigest.swap(mime_idigest); |
263 | 269 | ||
264 | #ifdef HAVE_SQLITE | 270 | #ifdef HAVE_SQLITE |
265 | if(!file->f.tellg()) { | 271 | if(!file->f.tellg()) { |
266 | if(!already.is(filename,filesignature,filesize)) | 272 | if(!already.is(filename,filesignature,filesize)) |
267 | throw std::runtime_error("got zero-length upload for unknown file"); | 273 | throw std::runtime_error("got zero-length upload for unknown file"); |
268 | r.success = true; | 274 | r.success = true; |
269 | return SOAP_OK; | 275 | return SOAP_OK; |
270 | } | 276 | } |
271 | #endif | 277 | #endif |
272 | 278 | ||
273 | if(idigest->str != file->idigest.final(kinfig->get_upload_key()).hex()) | 279 | if(idigest->str != file->idigest.final(kinfig->get_upload_key()).hex()) |
274 | throw std::runtime_error("Integrity digest doesn't match, disintegrating."); | 280 | throw std::runtime_error("Integrity digest doesn't match, disintegrating."); |
275 | 281 | ||
276 | std::string tf, lf; | 282 | std::string tf, lf; |
277 | for(tarchive_t a(file->fn.c_str());a.read_next_header();) { | 283 | for(tarchive_t a(file->fn.c_str());a.read_next_header();) { |
278 | std::string ep = a.entry_pathname(), f = dir->get_file(ep); | 284 | std::string ep = a.entry_pathname(), f = dir->get_file(ep); |
279 | if(ep==the_file) tf = f; | 285 | if(ep==the_file) tf = f; |
280 | else if(ep==the_log) lf = f; | 286 | else if(ep==the_log) lf = f; |
281 | else continue; | 287 | else continue; |
282 | int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666); | 288 | int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666); |
283 | if(fd<0) | 289 | if(fd<0) |
284 | throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str())); | 290 | throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str())); |
285 | if(!a.read_data_into_fd(fd)) | 291 | if(!a.read_data_into_fd(fd)) |
286 | throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); | 292 | throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); |
287 | close(fd); | 293 | close(fd); |
288 | } | 294 | } |
289 | 295 | ||
290 | if(tf.empty()) throw std::runtime_error("haven't seen THE file"); | 296 | if(tf.empty()) throw std::runtime_error("haven't seen THE file"); |
291 | 297 | ||
292 | std::string::size_type ls = tf.rfind('/'); | 298 | std::string::size_type ls = tf.rfind('/'); |
293 | // XXX: actually, lack of '/' signifies error here | 299 | // XXX: actually, lack of '/' signifies error here |
294 | std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); | 300 | std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); |
295 | ls = lf.rfind('/'); | 301 | ls = lf.rfind('/'); |
296 | std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); | 302 | std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); |
297 | std::string ttf,tlf; | 303 | std::string ttf,tlf; |
298 | bool success = false; | 304 | bool success = false; |
299 | std::string td = kinfig->get_targetdir(); | 305 | std::string td = kinfig->get_targetdir(); |
300 | for(int i=0;i<32767;++i) { | 306 | for(int i=0;i<32767;++i) { |
301 | const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s"; | 307 | const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s"; |
302 | ttf = (const char*)gnu::autosprintf(fmt,td.c_str(),tbn.c_str(),i); | 308 | ttf = (const char*)gnu::autosprintf(fmt,td.c_str(),tbn.c_str(),i); |
303 | if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i); | 309 | if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i); |
304 | if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty() || !link(lf.c_str(),tlf.c_str())) ) { | 310 | if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty() || !link(lf.c_str(),tlf.c_str())) ) { |
305 | unlink(tf.c_str()); | 311 | unlink(tf.c_str()); |
306 | if(!lf.empty()) unlink(lf.c_str()); | 312 | if(!lf.empty()) unlink(lf.c_str()); |
307 | success=true; | 313 | success=true; |
308 | break; | 314 | break; |
309 | } | 315 | } |
310 | } | 316 | } |
311 | std::string cmd = kinfig->get_on_upload_photo(); | 317 | std::string cmd = kinfig->get_on_upload_photo(); |
312 | if(success) { | 318 | if(success) { |
313 | #ifdef HAVE_SQLITE | 319 | #ifdef HAVE_SQLITE |
314 | { | 320 | { |
315 | iiidb_t D(*kinfig); | 321 | iiidb_t D(*kinfig); |
316 | D.prepare( | 322 | D.prepare( |
317 | "INSERT INTO photo" | 323 | "INSERT INTO photo" |
318 | " (ctime,mac,fileid,filename,filesize,filesignature,encryption,flags)" | 324 | " (ctime,mac,fileid,filename,filesize,filesignature,encryption,flags)" |
319 | " VALUES" | 325 | " VALUES" |
320 | " (:ctime,:mac,:fileid,:filename,:filesize,:filesignature,:encryption,:flags)" | 326 | " (:ctime,:mac,:fileid,:filename,:filesize,:filesignature,:encryption,:flags)" |
321 | ).bind(":ctime",time(0)) | 327 | ).bind(":ctime",time(0)) |
322 | .bind(":mac",macaddress) | 328 | .bind(":mac",macaddress) |
323 | .bind(":fileid",fileid).bind(":filename",filename) | 329 | .bind(":fileid",fileid).bind(":filename",filename) |
324 | .bind(":filesize",filesize).bind(":filesignature",filesignature) | 330 | .bind(":filesize",filesize).bind(":filesignature",filesignature) |
325 | .bind(":encryption",encryption).bind(":flags",flags) | 331 | .bind(":encryption",encryption).bind(":flags",flags) |
326 | .step(); | 332 | .step(); |
327 | } | 333 | } |
328 | #endif /* HAVE_SQLITE */ | 334 | #endif /* HAVE_SQLITE */ |
329 | if((!cmd.empty()) && detached_child()) { | 335 | if((!cmd.empty()) && detached_child()) { |
330 | putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); | 336 | putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); |
331 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 337 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
332 | putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) ); | 338 | putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) ); |
333 | if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) ); | 339 | if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) ); |
334 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 340 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
335 | execv("/bin/sh",argv); | 341 | execv("/bin/sh",argv); |
336 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 342 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
337 | _exit(-1); | 343 | _exit(-1); |
338 | } | 344 | } |
339 | } | 345 | } |
340 | 346 | ||
341 | r.success = true; | 347 | r.success = true; |
342 | return SOAP_OK; | 348 | return SOAP_OK; |
343 | }catch(const std::exception& e) { return E(this,"UploadPhoto",e); } | 349 | }catch(const std::exception& e) { return E(this,"UploadPhoto",e); } |
344 | 350 | ||