-rw-r--r-- | src/eyefiservice.cc | 75 |
1 files changed, 46 insertions, 29 deletions
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc index 153a7c4..d233a07 100644 --- a/src/eyefiservice.cc +++ b/src/eyefiservice.cc | |||
@@ -119,67 +119,84 @@ int eyefiService::UploadPhoto( | |||
119 | 119 | ||
120 | umask(eyekinfig.get_umask()); | 120 | umask(eyekinfig.get_umask()); |
121 | 121 | ||
122 | std::string td = eyekinfig.get_targetdir(); | 122 | std::string td = eyekinfig.get_targetdir(); |
123 | tmpdir_t indir(td+"/.incoming.XXXXXX"); | 123 | tmpdir_t indir(td+"/.incoming.XXXXXX"); |
124 | 124 | ||
125 | std::string jf; | ||
126 | binary_t digest, idigest; | ||
127 | |||
125 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { | 128 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { |
126 | #ifndef NDEBUG | 129 | #ifndef NDEBUG |
127 | syslog(LOG_DEBUG, | 130 | syslog(LOG_DEBUG, |
128 | " MIME attachment with id=%s, type=%s, size=%ld", | 131 | " MIME attachment with id=%s, type=%s, size=%ld", |
129 | (*i).id, (*i).type, (long)(*i).size ); | 132 | (*i).id, (*i).type, (long)(*i).size ); |
130 | #endif | 133 | #endif |
131 | 134 | ||
132 | #ifndef NDEBUG | ||
133 | if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { | 135 | if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { |
134 | std::string idigest((*i).ptr,(*i).size); | 136 | std::string idigestr((*i).ptr,(*i).size); |
135 | syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigest.c_str()); | 137 | #ifndef NDEBUG |
136 | } | 138 | syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); |
137 | #endif | 139 | #endif |
140 | idigest.from_hex(idigestr); | ||
141 | } | ||
138 | if( (*i).id && !strcmp((*i).id,"FILENAME") ) { | 142 | if( (*i).id && !strcmp((*i).id,"FILENAME") ) { |
139 | assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); | 143 | assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); |
140 | #ifdef III_SAVE_TARS | 144 | #ifdef III_SAVE_TARS |
141 | std::string tarfile = indir.get_file(filename); | 145 | std::string tarfile = indir.get_file(filename); |
142 | { | 146 | { |
143 | std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); | 147 | std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); |
144 | } | 148 | } |
145 | #endif | 149 | #endif |
150 | |||
151 | if(!jf.empty()) throw std::runtime_error("already seen tarball"); | ||
152 | if(!digest.empty()) throw std::runtime_error("already have integrity digest"); | ||
153 | digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); | ||
154 | #ifndef NDEBUG | ||
155 | syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); | ||
156 | #endif | ||
157 | |||
146 | tarchive_t a((*i).ptr,(*i).size); | 158 | tarchive_t a((*i).ptr,(*i).size); |
147 | if(!a.read_next_header()) | 159 | if(!a.read_next_header()) |
148 | throw std::runtime_error("failed to tarchive_t::read_next_header())"); | 160 | throw std::runtime_error("failed to tarchive_t::read_next_header())"); |
149 | std::string jf = indir.get_file(a.entry_pathname()); | 161 | jf = indir.get_file(a.entry_pathname()); |
150 | std::string::size_type ls = jf.rfind('/'); | ||
151 | std::string jbn = (ls==std::string::npos)?jf:jf.substr(ls+1); | ||
152 | int fd=open(jf.c_str(),O_CREAT|O_WRONLY,0666); | 162 | int fd=open(jf.c_str(),O_CREAT|O_WRONLY,0666); |
153 | assert(fd>0); | 163 | assert(fd>0); |
154 | a.read_data_into_fd(fd); | 164 | a.read_data_into_fd(fd); |
155 | close(fd); | 165 | close(fd); |
156 | std::string tf = td+'/'+jbn; | 166 | } |
157 | bool success = false; | 167 | } |
168 | |||
169 | if(jf.empty()) throw std::runtime_error("haven't seen jpeg file"); | ||
170 | if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); | ||
171 | |||
172 | std::string::size_type ls = jf.rfind('/'); | ||
173 | std::string jbn = (ls==std::string::npos)?jf:jf.substr(ls+1); | ||
174 | std::string tf = td+'/'+jbn; | ||
175 | bool success = false; | ||
176 | if(!link(jf.c_str(), tf.c_str())) { | ||
177 | unlink(jf.c_str()); success = true; | ||
178 | }else{ | ||
179 | for(int i=1;i<32767;++i) { | ||
180 | tf = (const char*)gnu::autosprintf( "%s/(%05d)%s", | ||
181 | td.c_str(), i, jbn.c_str() ); | ||
158 | if(!link(jf.c_str(), tf.c_str())) { | 182 | if(!link(jf.c_str(), tf.c_str())) { |
159 | unlink(jf.c_str()); success = true; | 183 | unlink(jf.c_str()); success = true; |
160 | }else{ | 184 | break; |
161 | for(int i=1;i<32767;++i) { | ||
162 | tf = (const char*)gnu::autosprintf( "%s/(%05d)%s", | ||
163 | td.c_str(), i, jbn.c_str() ); | ||
164 | if(!link(jf.c_str(), tf.c_str())) { | ||
165 | unlink(jf.c_str()); success = true; | ||
166 | break; | ||
167 | } | ||
168 | } | ||
169 | } | ||
170 | std::string cmd = eyekinfig.get_on_upload_photo(); | ||
171 | if(success && !cmd.empty()) { | ||
172 | if(detached_child()) { | ||
173 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | ||
174 | putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",tf.c_str()) ); | ||
175 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | ||
176 | execv("/bin/sh",argv); | ||
177 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | ||
178 | _exit(-1); | ||
179 | } | ||
180 | } | 185 | } |
181 | } | 186 | } |
182 | } | 187 | } |
188 | std::string cmd = eyekinfig.get_on_upload_photo(); | ||
189 | if(success && !cmd.empty()) { | ||
190 | if(detached_child()) { | ||
191 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | ||
192 | putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",tf.c_str()) ); | ||
193 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | ||
194 | execv("/bin/sh",argv); | ||
195 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | ||
196 | _exit(-1); | ||
197 | } | ||
198 | } | ||
199 | |||
183 | r.success = true; | 200 | r.success = true; |
184 | return SOAP_OK; | 201 | return SOAP_OK; |
185 | } | 202 | } |