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 | |
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 | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc index 153a7c4..d233a07 100644 --- a/src/eyefiservice.cc +++ b/src/eyefiservice.cc | |||
@@ -122,6 +122,9 @@ int eyefiService::UploadPhoto( | |||
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, |
@@ -129,12 +132,13 @@ int eyefiService::UploadPhoto( | |||
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 |
@@ -143,16 +147,30 @@ int eyefiService::UploadPhoto( | |||
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); |
166 | } | ||
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); | ||
156 | std::string tf = td+'/'+jbn; | 174 | std::string tf = td+'/'+jbn; |
157 | bool success = false; | 175 | bool success = false; |
158 | if(!link(jf.c_str(), tf.c_str())) { | 176 | if(!link(jf.c_str(), tf.c_str())) { |
@@ -178,8 +196,7 @@ int eyefiService::UploadPhoto( | |||
178 | _exit(-1); | 196 | _exit(-1); |
179 | } | 197 | } |
180 | } | 198 | } |
181 | } | 199 | |
182 | } | ||
183 | r.success = true; | 200 | r.success = true; |
184 | return SOAP_OK; | 201 | return SOAP_OK; |
185 | } | 202 | } |