author | Michael Krelin <hacker@klever.net> | 2013-02-12 21:50:00 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2013-02-12 21:50:00 (UTC) |
commit | 41f437eeadaae0dce4a3dad3da6dde2bd3c3de99 (patch) (unidiff) | |
tree | 0978b584286773cdcc29c3f174fd6ac9cdacee2f /src | |
parent | 13fb4abba3fd3cac0d5cb25d3eccddc298220d41 (diff) | |
download | iii-41f437eeadaae0dce4a3dad3da6dde2bd3c3de99.zip iii-41f437eeadaae0dce4a3dad3da6dde2bd3c3de99.tar.gz iii-41f437eeadaae0dce4a3dad3da6dde2bd3c3de99.tar.bz2 |
moved sqlite initialization
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | src/eyefiworker.cc | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/src/eyefiworker.cc b/src/eyefiworker.cc index 1979b46..645069e 100644 --- a/src/eyefiworker.cc +++ b/src/eyefiworker.cc | |||
@@ -1,341 +1,341 @@ | |||
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 | eyefiworker::eyefiworker() | 21 | eyefiworker::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 | ||
34 | sqlite3_initialize(); | ||
35 | #endif | ||
33 | } | 36 | } |
34 | eyefiworker::~eyefiworker() { | 37 | eyefiworker::~eyefiworker() { |
35 | } | 38 | } |
36 | 39 | ||
37 | int eyefiworker::run(int bindport) { | 40 | int eyefiworker::run(int bindport) { |
38 | #ifdef HAVE_SQLITE | ||
39 | sqlite3_initialize(); | ||
40 | #endif | ||
41 | if(!soap_valid_socket(bind(0,bindport,64))) | 41 | if(!soap_valid_socket(bind(0,bindport,64))) |
42 | throw std::runtime_error("failed to bind()"); | 42 | throw std::runtime_error("failed to bind()"); |
43 | signal(SIGCHLD,SIG_IGN); | 43 | signal(SIGCHLD,SIG_IGN); |
44 | while(true) { | 44 | while(true) { |
45 | if(!soap_valid_socket(accept())) | 45 | if(!soap_valid_socket(accept())) |
46 | throw std::runtime_error("failed to accept()"); | 46 | throw std::runtime_error("failed to accept()"); |
47 | pid_t p = fork(); | 47 | pid_t p = fork(); |
48 | if(p<0) throw std::runtime_error("failed to fork()"); | 48 | if(p<0) throw std::runtime_error("failed to fork()"); |
49 | if(!p) { | 49 | if(!p) { |
50 | recv_timeout = 600; send_timeout = 120; | 50 | recv_timeout = 600; send_timeout = 120; |
51 | (void)serve(); | 51 | (void)serve(); |
52 | soap_destroy(this); soap_end(this); soap_done(this); | 52 | soap_destroy(this); soap_end(this); soap_done(this); |
53 | #ifndef NDEBUG | 53 | #ifndef NDEBUG |
54 | struct rusage ru; | 54 | struct rusage ru; |
55 | if(getrusage(RUSAGE_SELF,&ru)) { | 55 | if(getrusage(RUSAGE_SELF,&ru)) { |
56 | syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno); | 56 | syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno); |
57 | }else{ | 57 | }else{ |
58 | syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss); | 58 | syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss); |
59 | } | 59 | } |
60 | #endif /* NDEBUG */ | 60 | #endif /* NDEBUG */ |
61 | throw throwable_exit(0); | 61 | throw throwable_exit(0); |
62 | } | 62 | } |
63 | close(socket); socket = SOAP_INVALID_SOCKET; | 63 | close(socket); socket = SOAP_INVALID_SOCKET; |
64 | } | 64 | } |
65 | } | 65 | } |
66 | 66 | ||
67 | static binary_t session_nonce; | 67 | static binary_t session_nonce; |
68 | #ifdef HAVE_SQLITE | 68 | #ifdef HAVE_SQLITE |
69 | static struct { | 69 | static struct { |
70 | std::string filesignature; | 70 | std::string filesignature; |
71 | long filesize; | 71 | long filesize; |
72 | std::string filename; | 72 | std::string filename; |
73 | inline void reset() { filesignature.erase(); filename.erase(); filesize=0; } | 73 | inline void reset() { filesignature.erase(); filename.erase(); filesize=0; } |
74 | inline void set(const std::string n,const std::string sig,long siz) { | 74 | inline void set(const std::string n,const std::string sig,long siz) { |
75 | filename = n; filesignature = sig; filesize = siz; | 75 | filename = n; filesignature = sig; filesize = siz; |
76 | } | 76 | } |
77 | inline bool is(const std::string n,const std::string sig,long siz) { | 77 | inline bool is(const std::string n,const std::string sig,long siz) { |
78 | return filesize==siz && filename==n && filesignature==sig; | 78 | return filesize==siz && filename==n && filesignature==sig; |
79 | } | 79 | } |
80 | } already; | 80 | } already; |
81 | #endif /* HAVE_SQLITE */ | 81 | #endif /* HAVE_SQLITE */ |
82 | 82 | ||
83 | static bool detached_child() { | 83 | static bool detached_child() { |
84 | pid_t p = fork(); | 84 | pid_t p = fork(); |
85 | if(p<0) { | 85 | if(p<0) { |
86 | syslog(LOG_ERR,"Failed to fork away for hook execution"); | 86 | syslog(LOG_ERR,"Failed to fork away for hook execution"); |
87 | _exit(-1); | 87 | _exit(-1); |
88 | } | 88 | } |
89 | if(!p) { | 89 | if(!p) { |
90 | setsid(); | 90 | setsid(); |
91 | for(int i=getdtablesize();i>=0;--i) close(i); | 91 | for(int i=getdtablesize();i>=0;--i) close(i); |
92 | int i=open("/dev/null",O_RDWR); assert(i==0); | 92 | int i=open("/dev/null",O_RDWR); assert(i==0); |
93 | i = dup(i); assert(i==1); | 93 | i = dup(i); assert(i==1); |
94 | i = dup(i); assert(i==2); | 94 | i = dup(i); assert(i==2); |
95 | return true; | 95 | return true; |
96 | } | 96 | } |
97 | return false; | 97 | return false; |
98 | } | 98 | } |
99 | 99 | ||
100 | static int E(eyefiworker* efs,const char *c,const std::exception& e) { | 100 | static int E(eyefiworker* efs,const char *c,const std::exception& e) { |
101 | efs->keep_alive=0; | 101 | efs->keep_alive=0; |
102 | syslog(LOG_ERR,"error while processing %s: %s",c,e.what()); | 102 | syslog(LOG_ERR,"error while processing %s: %s",c,e.what()); |
103 | return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0); | 103 | return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0); |
104 | } | 104 | } |
105 | 105 | ||
106 | int eyefiworker::StartSession( | 106 | int eyefiworker::StartSession( |
107 | std::string macaddress,std::string cnonce, | 107 | std::string macaddress,std::string cnonce, |
108 | int transfermode,long transfermodetimestamp, | 108 | int transfermode,long transfermodetimestamp, |
109 | struct rns__StartSessionResponse &r ) try { | 109 | struct rns__StartSessionResponse &r ) try { |
110 | syslog(LOG_INFO, | 110 | syslog(LOG_INFO, |
111 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", | 111 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", |
112 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); | 112 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); |
113 | eyekinfig_t eyekinfig(macaddress); | 113 | eyekinfig_t eyekinfig(macaddress); |
114 | r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); | 114 | r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); |
115 | 115 | ||
116 | r.snonce = session_nonce.make_nonce().hex(); | 116 | r.snonce = session_nonce.make_nonce().hex(); |
117 | r.transfermode=transfermode; | 117 | r.transfermode=transfermode; |
118 | r.transfermodetimestamp=transfermodetimestamp; | 118 | r.transfermodetimestamp=transfermodetimestamp; |
119 | r.upsyncallowed=false; | 119 | r.upsyncallowed=false; |
120 | 120 | ||
121 | std::string cmd = eyekinfig.get_on_start_session(); | 121 | std::string cmd = eyekinfig.get_on_start_session(); |
122 | if(!cmd.empty()) { | 122 | if(!cmd.empty()) { |
123 | if(detached_child()) { | 123 | if(detached_child()) { |
124 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 124 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
125 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); | 125 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); |
126 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); | 126 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); |
127 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 127 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
128 | execv("/bin/sh",argv); | 128 | execv("/bin/sh",argv); |
129 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 129 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
130 | _exit(-1); | 130 | _exit(-1); |
131 | } | 131 | } |
132 | } | 132 | } |
133 | return SOAP_OK; | 133 | return SOAP_OK; |
134 | }catch(const std::exception& e) { return E(this,"StartSession",e); } | 134 | }catch(const std::exception& e) { return E(this,"StartSession",e); } |
135 | 135 | ||
136 | int eyefiworker::GetPhotoStatus( | 136 | int eyefiworker::GetPhotoStatus( |
137 | std::string credential, std::string macaddress, | 137 | std::string credential, std::string macaddress, |
138 | std::string filename, long filesize, std::string filesignature, | 138 | std::string filename, long filesize, std::string filesignature, |
139 | int flags, | 139 | int flags, |
140 | struct rns__GetPhotoStatusResponse &r ) try { | 140 | struct rns__GetPhotoStatusResponse &r ) try { |
141 | syslog(LOG_INFO, | 141 | syslog(LOG_INFO, |
142 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", | 142 | "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, | 143 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, |
144 | session_nonce.hex().c_str() ); | 144 | session_nonce.hex().c_str() ); |
145 | 145 | ||
146 | eyekinfig_t eyekinfig(macaddress); | 146 | eyekinfig_t eyekinfig(macaddress); |
147 | std::string computed_credential = binary_t(macaddress+eyekinfig.get_upload_key()+session_nonce.hex()).md5().hex(); | 147 | std::string computed_credential = binary_t(macaddress+eyekinfig.get_upload_key()+session_nonce.hex()).md5().hex(); |
148 | 148 | ||
149 | #ifndef NDEBUG | 149 | #ifndef NDEBUG |
150 | syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); | 150 | syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); |
151 | #endif | 151 | #endif |
152 | 152 | ||
153 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); | 153 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); |
154 | 154 | ||
155 | #ifdef HAVE_SQLITE | 155 | #ifdef HAVE_SQLITE |
156 | iiidb_t D(eyekinfig); | 156 | iiidb_t D(eyekinfig); |
157 | seclude::stmt_t S = D.prepare( | 157 | seclude::stmt_t S = D.prepare( |
158 | "SELECT fileid FROM photo" | 158 | "SELECT fileid FROM photo" |
159 | " WHERE mac=:mac AND filename=:filename" | 159 | " WHERE mac=:mac AND filename=:filename" |
160 | " AND filesize=:filesize AND filesignature=:filesignature" | 160 | " AND filesize=:filesize AND filesignature=:filesignature" |
161 | ).bind(":mac",macaddress) | 161 | ).bind(":mac",macaddress) |
162 | .bind(":filename",filename).bind(":filesize",filesize) | 162 | .bind(":filename",filename).bind(":filesize",filesize) |
163 | .bind(":filesignature",filesignature); | 163 | .bind(":filesignature",filesignature); |
164 | if(!S.step()) { | 164 | if(!S.step()) { |
165 | r.fileid = 1; r.offset = 0; | 165 | r.fileid = 1; r.offset = 0; |
166 | }else{ | 166 | }else{ |
167 | r.fileid = S.column<long>(0); | 167 | r.fileid = S.column<long>(0); |
168 | r.offset = filesize; | 168 | r.offset = filesize; |
169 | already.set(filename,filesignature,filesize); | 169 | already.set(filename,filesignature,filesize); |
170 | } | 170 | } |
171 | #else /* HAVE_SQLITE */ | 171 | #else /* HAVE_SQLITE */ |
172 | r.fileid=1, r.offset=0; | 172 | r.fileid=1, r.offset=0; |
173 | #endif /* HAVE_SQLITE */ | 173 | #endif /* HAVE_SQLITE */ |
174 | return SOAP_OK; | 174 | return SOAP_OK; |
175 | }catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } | 175 | }catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } |
176 | 176 | ||
177 | int eyefiworker::MarkLastPhotoInRoll( | 177 | int eyefiworker::MarkLastPhotoInRoll( |
178 | std::string macaddress, int mergedelta, | 178 | std::string macaddress, int mergedelta, |
179 | struct rns__MarkLastPhotoInRollResponse&/* r */ ) try { | 179 | struct rns__MarkLastPhotoInRollResponse&/* r */ ) try { |
180 | syslog(LOG_INFO, | 180 | syslog(LOG_INFO, |
181 | "MarkLastPhotoInRoll request from %s with mergedelta=%d", | 181 | "MarkLastPhotoInRoll request from %s with mergedelta=%d", |
182 | macaddress.c_str(), mergedelta ); | 182 | macaddress.c_str(), mergedelta ); |
183 | std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); | 183 | std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); |
184 | if(!cmd.empty()) { | 184 | if(!cmd.empty()) { |
185 | if(detached_child()) { | 185 | if(detached_child()) { |
186 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 186 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
187 | putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); | 187 | putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); |
188 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 188 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
189 | execv("/bin/sh",argv); | 189 | execv("/bin/sh",argv); |
190 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 190 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
191 | _exit(-1); | 191 | _exit(-1); |
192 | } | 192 | } |
193 | } | 193 | } |
194 | keep_alive = 0; | 194 | keep_alive = 0; |
195 | return SOAP_OK; | 195 | return SOAP_OK; |
196 | }catch(const std::exception& e) { return E(this,"MarkLastPhotoInRoll",e); } | 196 | }catch(const std::exception& e) { return E(this,"MarkLastPhotoInRoll",e); } |
197 | 197 | ||
198 | int eyefiworker::UploadPhoto( | 198 | int eyefiworker::UploadPhoto( |
199 | int fileid, std::string macaddress, | 199 | int fileid, std::string macaddress, |
200 | std::string filename, long filesize, std::string filesignature, | 200 | std::string filename, long filesize, std::string filesignature, |
201 | std::string encryption, int flags, | 201 | std::string encryption, int flags, |
202 | struct rns__UploadPhotoResponse& r ) try { | 202 | struct rns__UploadPhotoResponse& r ) try { |
203 | syslog(LOG_INFO, | 203 | syslog(LOG_INFO, |
204 | "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," | 204 | "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," |
205 | " filesignature=%s, encryption=%s, flags=%04X", | 205 | " filesignature=%s, encryption=%s, flags=%04X", |
206 | macaddress.c_str(), fileid, filename.c_str(), filesize, | 206 | macaddress.c_str(), fileid, filename.c_str(), filesize, |
207 | filesignature.c_str(), encryption.c_str(), flags ); | 207 | filesignature.c_str(), encryption.c_str(), flags ); |
208 | std::string::size_type fnl=filename.length(); | 208 | std::string::size_type fnl=filename.length(); |
209 | if(fnl<sizeof(".tar") || strncmp(filename.c_str()+fnl-sizeof(".tar")+sizeof(""),".tar",sizeof(".tar"))) | 209 | 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())); | 210 | 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("")); | 211 | std::string the_file(filename,0,fnl-sizeof(".tar")+sizeof("")); |
212 | std::string the_log = the_file+".log"; | 212 | std::string the_log = the_file+".log"; |
213 | 213 | ||
214 | eyekinfig_t eyekinfig(macaddress); | 214 | eyekinfig_t eyekinfig(macaddress); |
215 | 215 | ||
216 | umask(eyekinfig.get_umask()); | 216 | umask(eyekinfig.get_umask()); |
217 | 217 | ||
218 | std::string td = eyekinfig.get_targetdir(); | 218 | std::string td = eyekinfig.get_targetdir(); |
219 | tmpdir_t indir(td+"/.incoming.XXXXXX"); | 219 | tmpdir_t indir(td+"/.incoming.XXXXXX"); |
220 | 220 | ||
221 | std::string tf,lf; | 221 | std::string tf,lf; |
222 | binary_t digest, idigest; | 222 | binary_t digest, idigest; |
223 | #ifdef HAVE_SQLITE | 223 | #ifdef HAVE_SQLITE |
224 | bool beenthere = false; | 224 | bool beenthere = false; |
225 | #endif | 225 | #endif |
226 | 226 | ||
227 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { | 227 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { |
228 | #ifndef NDEBUG | 228 | #ifndef NDEBUG |
229 | syslog(LOG_DEBUG, | 229 | syslog(LOG_DEBUG, |
230 | " MIME attachment with id=%s, type=%s, size=%ld", | 230 | " MIME attachment with id=%s, type=%s, size=%ld", |
231 | (*i).id, (*i).type, (long)(*i).size ); | 231 | (*i).id, (*i).type, (long)(*i).size ); |
232 | #endif | 232 | #endif |
233 | 233 | ||
234 | if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { | 234 | if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { |
235 | std::string idigestr((*i).ptr,(*i).size); | 235 | std::string idigestr((*i).ptr,(*i).size); |
236 | #ifndef NDEBUG | 236 | #ifndef NDEBUG |
237 | syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); | 237 | syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); |
238 | #endif | 238 | #endif |
239 | idigest.from_hex(idigestr); | 239 | idigest.from_hex(idigestr); |
240 | } | 240 | } |
241 | if( (*i).id && !strcmp((*i).id,"FILENAME") ) { | 241 | if( (*i).id && !strcmp((*i).id,"FILENAME") ) { |
242 | assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); | 242 | assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); |
243 | #ifdef III_SAVE_TARS | 243 | #ifdef III_SAVE_TARS |
244 | std::string tarfile = indir.get_file(filename); | 244 | std::string tarfile = indir.get_file(filename); |
245 | { | 245 | { |
246 | std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); | 246 | std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); |
247 | } | 247 | } |
248 | #endif | 248 | #endif |
249 | 249 | ||
250 | if(!tf.empty()) throw std::runtime_error("already seen tarball"); | 250 | if(!tf.empty()) throw std::runtime_error("already seen tarball"); |
251 | if(!digest.empty()) throw std::runtime_error("already have integrity digest"); | 251 | if(!digest.empty()) throw std::runtime_error("already have integrity digest"); |
252 | digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); | 252 | digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); |
253 | #ifndef NDEBUG | 253 | #ifndef NDEBUG |
254 | syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); | 254 | syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); |
255 | #endif | 255 | #endif |
256 | #ifdef HAVE_SQLITE | 256 | #ifdef HAVE_SQLITE |
257 | if(!(*i).size) { | 257 | if(!(*i).size) { |
258 | if(!already.is(filename,filesignature,filesize)) | 258 | if(!already.is(filename,filesignature,filesize)) |
259 | throw std::runtime_error("got zero-length upload for unknown file"); | 259 | throw std::runtime_error("got zero-length upload for unknown file"); |
260 | beenthere = true; continue; | 260 | beenthere = true; continue; |
261 | } | 261 | } |
262 | #endif | 262 | #endif |
263 | 263 | ||
264 | tarchive_t a((*i).ptr,(*i).size); | 264 | tarchive_t a((*i).ptr,(*i).size); |
265 | while(a.read_next_header()) { | 265 | while(a.read_next_header()) { |
266 | std::string ep = a.entry_pathname(), f = indir.get_file(ep); | 266 | std::string ep = a.entry_pathname(), f = indir.get_file(ep); |
267 | if(ep==the_file) tf = f; | 267 | if(ep==the_file) tf = f; |
268 | else if(ep==the_log) lf = f; | 268 | else if(ep==the_log) lf = f; |
269 | else continue; | 269 | else continue; |
270 | int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666); | 270 | int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666); |
271 | if(fd<0) | 271 | if(fd<0) |
272 | throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str())); | 272 | throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str())); |
273 | if(!a.read_data_into_fd(fd)) | 273 | if(!a.read_data_into_fd(fd)) |
274 | throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); | 274 | throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); |
275 | close(fd); | 275 | close(fd); |
276 | } | 276 | } |
277 | } | 277 | } |
278 | } | 278 | } |
279 | 279 | ||
280 | #ifdef HAVE_SQLITE | 280 | #ifdef HAVE_SQLITE |
281 | if(beenthere) { | 281 | if(beenthere) { |
282 | r.success=true; | 282 | r.success=true; |
283 | return SOAP_OK; | 283 | return SOAP_OK; |
284 | } | 284 | } |
285 | #endif | 285 | #endif |
286 | 286 | ||
287 | if(tf.empty()) throw std::runtime_error("haven't seen THE file"); | 287 | if(tf.empty()) throw std::runtime_error("haven't seen THE file"); |
288 | if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); | 288 | if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); |
289 | 289 | ||
290 | std::string::size_type ls = tf.rfind('/'); | 290 | std::string::size_type ls = tf.rfind('/'); |
291 | // XXX: actually, lack of '/' signifies error here | 291 | // XXX: actually, lack of '/' signifies error here |
292 | std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); | 292 | std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); |
293 | ls = lf.rfind('/'); | 293 | ls = lf.rfind('/'); |
294 | std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); | 294 | std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); |
295 | std::string ttf,tlf; | 295 | std::string ttf,tlf; |
296 | bool success = false; | 296 | bool success = false; |
297 | for(int i=0;i<32767;++i) { | 297 | for(int i=0;i<32767;++i) { |
298 | const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s"; | 298 | 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); | 299 | 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); | 300 | 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())) ) { | 301 | if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty() || !link(lf.c_str(),tlf.c_str())) ) { |
302 | unlink(tf.c_str()); | 302 | unlink(tf.c_str()); |
303 | if(!lf.empty()) unlink(lf.c_str()); | 303 | if(!lf.empty()) unlink(lf.c_str()); |
304 | success=true; | 304 | success=true; |
305 | break; | 305 | break; |
306 | } | 306 | } |
307 | } | 307 | } |
308 | std::string cmd = eyekinfig.get_on_upload_photo(); | 308 | std::string cmd = eyekinfig.get_on_upload_photo(); |
309 | if(success) { | 309 | if(success) { |
310 | #ifdef HAVE_SQLITE | 310 | #ifdef HAVE_SQLITE |
311 | { | 311 | { |
312 | iiidb_t D(eyekinfig); | 312 | iiidb_t D(eyekinfig); |
313 | D.prepare( | 313 | D.prepare( |
314 | "INSERT INTO photo" | 314 | "INSERT INTO photo" |
315 | " (ctime,mac,fileid,filename,filesize,filesignature,encryption,flags)" | 315 | " (ctime,mac,fileid,filename,filesize,filesignature,encryption,flags)" |
316 | " VALUES" | 316 | " VALUES" |
317 | " (:ctime,:mac,:fileid,:filename,:filesize,:filesignature,:encryption,:flags)" | 317 | " (:ctime,:mac,:fileid,:filename,:filesize,:filesignature,:encryption,:flags)" |
318 | ).bind(":ctime",time(0)) | 318 | ).bind(":ctime",time(0)) |
319 | .bind(":mac",macaddress) | 319 | .bind(":mac",macaddress) |
320 | .bind(":fileid",fileid).bind(":filename",filename) | 320 | .bind(":fileid",fileid).bind(":filename",filename) |
321 | .bind(":filesize",filesize).bind(":filesignature",filesignature) | 321 | .bind(":filesize",filesize).bind(":filesignature",filesignature) |
322 | .bind(":encryption",encryption).bind(":flags",flags) | 322 | .bind(":encryption",encryption).bind(":flags",flags) |
323 | .step(); | 323 | .step(); |
324 | } | 324 | } |
325 | #endif /* HAVE_SQLITE */ | 325 | #endif /* HAVE_SQLITE */ |
326 | if((!cmd.empty()) && detached_child()) { | 326 | if((!cmd.empty()) && detached_child()) { |
327 | putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); | 327 | putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); |
328 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 328 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
329 | putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) ); | 329 | putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) ); |
330 | if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) ); | 330 | 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 }; | 331 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
332 | execv("/bin/sh",argv); | 332 | execv("/bin/sh",argv); |
333 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 333 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
334 | _exit(-1); | 334 | _exit(-1); |
335 | } | 335 | } |
336 | } | 336 | } |
337 | 337 | ||
338 | r.success = true; | 338 | r.success = true; |
339 | return SOAP_OK; | 339 | return SOAP_OK; |
340 | }catch(const std::exception& e) { return E(this,"UploadPhoto",e); } | 340 | }catch(const std::exception& e) { return E(this,"UploadPhoto",e); } |
341 | 341 | ||