author | Michael Krelin <hacker@klever.net> | 2009-04-05 13:32:38 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2009-04-05 13:32:38 (UTC) |
commit | 702c6bc69bb9bf511f6f5639d90d49139674339c (patch) (unidiff) | |
tree | 9672670cc28f5e743f372a82fd1aefb2bb87479e /src | |
parent | 878315238f71307b5b62ed314096f4a7c465bf3e (diff) | |
download | iii-702c6bc69bb9bf511f6f5639d90d49139674339c.zip iii-702c6bc69bb9bf511f6f5639d90d49139674339c.tar.gz iii-702c6bc69bb9bf511f6f5639d90d49139674339c.tar.bz2 |
check uploaded file integrity
Signed-off-by: Michael Krelin <hacker@klever.net>
-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 | |||
@@ -124,2 +124,5 @@ int eyefiService::UploadPhoto( | |||
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) { |
@@ -131,8 +134,9 @@ int eyefiService::UploadPhoto( | |||
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") ) { |
@@ -145,2 +149,10 @@ int eyefiService::UploadPhoto( | |||
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); |
@@ -148,5 +160,3 @@ int eyefiService::UploadPhoto( | |||
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); |
@@ -155,26 +165,21 @@ int eyefiService::UploadPhoto( | |||
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 | } |
@@ -182,2 +187,14 @@ int eyefiService::UploadPhoto( | |||
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; |