-rw-r--r-- | src/eyefiservice.cc | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc index 4ca2777..d7f0f04 100644 --- a/src/eyefiservice.cc +++ b/src/eyefiservice.cc | |||
@@ -116,108 +116,108 @@ int eyefiService::UploadPhoto( | |||
116 | std::string encryption, int flags, | 116 | std::string encryption, int flags, |
117 | struct rns__UploadPhotoResponse& r ) { | 117 | struct rns__UploadPhotoResponse& r ) { |
118 | #ifndef NDEBUG | 118 | #ifndef NDEBUG |
119 | syslog(LOG_DEBUG, | 119 | syslog(LOG_DEBUG, |
120 | "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," | 120 | "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," |
121 | " filesignature=%s, encryption=%s, flags=%04X", | 121 | " filesignature=%s, encryption=%s, flags=%04X", |
122 | macaddress.c_str(), fileid, filename.c_str(), filesize, | 122 | macaddress.c_str(), fileid, filename.c_str(), filesize, |
123 | filesignature.c_str(), encryption.c_str(), flags ); | 123 | filesignature.c_str(), encryption.c_str(), flags ); |
124 | #endif | 124 | #endif |
125 | eyekinfig_t eyekinfig(macaddress); | 125 | eyekinfig_t eyekinfig(macaddress); |
126 | 126 | ||
127 | umask(eyekinfig.get_umask()); | 127 | umask(eyekinfig.get_umask()); |
128 | 128 | ||
129 | std::string td = eyekinfig.get_targetdir(); | 129 | std::string td = eyekinfig.get_targetdir(); |
130 | tmpdir_t indir(td+"/.incoming.XXXXXX"); | 130 | tmpdir_t indir(td+"/.incoming.XXXXXX"); |
131 | 131 | ||
132 | std::string jf,lf; | 132 | std::string tf,lf; |
133 | binary_t digest, idigest; | 133 | binary_t digest, idigest; |
134 | 134 | ||
135 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { | 135 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { |
136 | #ifndef NDEBUG | 136 | #ifndef NDEBUG |
137 | syslog(LOG_DEBUG, | 137 | syslog(LOG_DEBUG, |
138 | " MIME attachment with id=%s, type=%s, size=%ld", | 138 | " MIME attachment with id=%s, type=%s, size=%ld", |
139 | (*i).id, (*i).type, (long)(*i).size ); | 139 | (*i).id, (*i).type, (long)(*i).size ); |
140 | #endif | 140 | #endif |
141 | 141 | ||
142 | if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { | 142 | if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { |
143 | std::string idigestr((*i).ptr,(*i).size); | 143 | std::string idigestr((*i).ptr,(*i).size); |
144 | #ifndef NDEBUG | 144 | #ifndef NDEBUG |
145 | syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); | 145 | syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); |
146 | #endif | 146 | #endif |
147 | idigest.from_hex(idigestr); | 147 | idigest.from_hex(idigestr); |
148 | } | 148 | } |
149 | if( (*i).id && !strcmp((*i).id,"FILENAME") ) { | 149 | if( (*i).id && !strcmp((*i).id,"FILENAME") ) { |
150 | assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); | 150 | assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); |
151 | #ifdef III_SAVE_TARS | 151 | #ifdef III_SAVE_TARS |
152 | std::string tarfile = indir.get_file(filename); | 152 | std::string tarfile = indir.get_file(filename); |
153 | { | 153 | { |
154 | std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); | 154 | std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); |
155 | } | 155 | } |
156 | #endif | 156 | #endif |
157 | 157 | ||
158 | if(!jf.empty()) throw std::runtime_error("already seen tarball"); | 158 | if(!tf.empty()) throw std::runtime_error("already seen tarball"); |
159 | if(!digest.empty()) throw std::runtime_error("already have integrity digest"); | 159 | if(!digest.empty()) throw std::runtime_error("already have integrity digest"); |
160 | digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); | 160 | digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); |
161 | #ifndef NDEBUG | 161 | #ifndef NDEBUG |
162 | syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); | 162 | syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); |
163 | #endif | 163 | #endif |
164 | 164 | ||
165 | tarchive_t a((*i).ptr,(*i).size); | 165 | tarchive_t a((*i).ptr,(*i).size); |
166 | while(a.read_next_header()) { | 166 | while(a.read_next_header()) { |
167 | std::string f = indir.get_file(a.entry_pathname()); | 167 | std::string f = indir.get_file(a.entry_pathname()); |
168 | std::string::size_type fl = f.length(); | 168 | std::string::size_type fl = f.length(); |
169 | if(fl<4) continue; | 169 | if(fl<4) continue; |
170 | const char *s = f.c_str()+fl-4; | 170 | const char *s = f.c_str()+fl-4; |
171 | if(!strcasecmp(s,".JPG")) | 171 | if(!(strcasecmp(s,".JPG") && strcasecmp(s,".AVI"))) |
172 | jf = f; | 172 | tf = f; |
173 | else if(!strcasecmp(s,".log")) | 173 | else if(!strcasecmp(s,".log")) |
174 | lf = f; | 174 | lf = f; |
175 | else continue; | 175 | else continue; |
176 | int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666); | 176 | int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666); |
177 | if(fd<0) | 177 | if(fd<0) |
178 | throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str())); | 178 | throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str())); |
179 | if(!a.read_data_into_fd(fd)) | 179 | if(!a.read_data_into_fd(fd)) |
180 | throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); | 180 | throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); |
181 | close(fd); | 181 | close(fd); |
182 | } | 182 | } |
183 | } | 183 | } |
184 | } | 184 | } |
185 | 185 | ||
186 | if(jf.empty()) throw std::runtime_error("haven't seen jpeg file"); | 186 | if(tf.empty()) throw std::runtime_error("haven't seen THE file"); |
187 | if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); | 187 | if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); |
188 | 188 | ||
189 | std::string::size_type ls = jf.rfind('/'); | 189 | std::string::size_type ls = tf.rfind('/'); |
190 | // XXX: actually, lack of '/' signifies error here | 190 | // XXX: actually, lack of '/' signifies error here |
191 | std::string jbn = (ls==std::string::npos)?jf:jf.substr(ls+1); | 191 | std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); |
192 | ls = lf.rfind('/'); | 192 | ls = lf.rfind('/'); |
193 | std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); | 193 | std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); |
194 | std::string tjf,tlf; | 194 | std::string ttf,tlf; |
195 | bool success = false; | 195 | bool success = false; |
196 | for(int i=0;i<32767;++i) { | 196 | for(int i=0;i<32767;++i) { |
197 | const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s"; | 197 | const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s"; |
198 | tjf = (const char*)gnu::autosprintf(fmt,td.c_str(),jbn.c_str(),i); | 198 | ttf = (const char*)gnu::autosprintf(fmt,td.c_str(),tbn.c_str(),i); |
199 | if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i); | 199 | if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i); |
200 | if( (!link(jf.c_str(),tjf.c_str())) && (lf.empty()) || !link(lf.c_str(),tlf.c_str()) ) { | 200 | if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty()) || !link(lf.c_str(),tlf.c_str()) ) { |
201 | unlink(jf.c_str()); | 201 | unlink(tf.c_str()); |
202 | if(!lf.empty()) unlink(lf.c_str()); | 202 | if(!lf.empty()) unlink(lf.c_str()); |
203 | success=true; | 203 | success=true; |
204 | break; | 204 | break; |
205 | } | 205 | } |
206 | } | 206 | } |
207 | std::string cmd = eyekinfig.get_on_upload_photo(); | 207 | std::string cmd = eyekinfig.get_on_upload_photo(); |
208 | if(success && !cmd.empty()) { | 208 | if(success && !cmd.empty()) { |
209 | if(detached_child()) { | 209 | if(detached_child()) { |
210 | putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",jbn.c_str()) ); | 210 | putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); |
211 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 211 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
212 | putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",tjf.c_str()) ); | 212 | putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) ); |
213 | if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) ); | 213 | if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) ); |
214 | 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 }; |
215 | execv("/bin/sh",argv); | 215 | execv("/bin/sh",argv); |
216 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 216 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
217 | _exit(-1); | 217 | _exit(-1); |
218 | } | 218 | } |
219 | } | 219 | } |
220 | 220 | ||
221 | r.success = true; | 221 | r.success = true; |
222 | return SOAP_OK; | 222 | return SOAP_OK; |
223 | } | 223 | } |