author | Michael Krelin <hacker@klever.net> | 2011-05-24 20:27:01 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2011-05-24 21:30:57 (UTC) |
commit | 09b6e86aaad70bc7488604b2e291d098a7de0eac (patch) (unidiff) | |
tree | 1f79c3eea40a555cc5f354563d55ee773f75a6c7 /src/eyefiservice.cc | |
parent | f9b17f10f8f43da6998bbd1fb13d3a48c35ec653 (diff) | |
download | iii-09b6e86aaad70bc7488604b2e291d098a7de0eac.zip iii-09b6e86aaad70bc7488604b2e291d098a7de0eac.tar.gz iii-09b6e86aaad70bc7488604b2e291d098a7de0eac.tar.bz2 |
don't whine there's no jpeg if there was avi
Signed-off-by: Michael Krelin <hacker@klever.net>
-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 | |||
@@ -1,223 +1,223 @@ | |||
1 | #include <cassert> | 1 | #include <cassert> |
2 | #include <iostream> | 2 | #include <iostream> |
3 | #include <fstream> | 3 | #include <fstream> |
4 | #include <stdexcept> | 4 | #include <stdexcept> |
5 | #include <iterator> | 5 | #include <iterator> |
6 | #include <syslog.h> | 6 | #include <syslog.h> |
7 | #include <sys/wait.h> | 7 | #include <sys/wait.h> |
8 | #include <autosprintf.h> | 8 | #include <autosprintf.h> |
9 | #include "eyekinfig.h" | 9 | #include "eyekinfig.h" |
10 | #include "eyetil.h" | 10 | #include "eyetil.h" |
11 | #include "soapeyefiService.h" | 11 | #include "soapeyefiService.h" |
12 | 12 | ||
13 | static binary_t session_nonce; | 13 | static binary_t session_nonce; |
14 | 14 | ||
15 | static bool detached_child() { | 15 | static bool detached_child() { |
16 | pid_t p = fork(); | 16 | pid_t p = fork(); |
17 | if(p<0) { | 17 | if(p<0) { |
18 | syslog(LOG_ERR,"Failed to fork away for hook execution"); | 18 | syslog(LOG_ERR,"Failed to fork away for hook execution"); |
19 | _exit(-1); | 19 | _exit(-1); |
20 | } | 20 | } |
21 | if(!p) { | 21 | if(!p) { |
22 | setsid(); | 22 | setsid(); |
23 | for(int i=getdtablesize();i>=0;--i) close(i); | 23 | for(int i=getdtablesize();i>=0;--i) close(i); |
24 | int i=open("/dev/null",O_RDWR); assert(i==0); | 24 | int i=open("/dev/null",O_RDWR); assert(i==0); |
25 | i = dup(i); assert(i==1); | 25 | i = dup(i); assert(i==1); |
26 | i = dup(i); assert(i==2); | 26 | i = dup(i); assert(i==2); |
27 | return true; | 27 | return true; |
28 | } | 28 | } |
29 | return false; | 29 | return false; |
30 | } | 30 | } |
31 | 31 | ||
32 | int eyefiService::StartSession( | 32 | int eyefiService::StartSession( |
33 | std::string macaddress,std::string cnonce, | 33 | std::string macaddress,std::string cnonce, |
34 | int transfermode,long transfermodetimestamp, | 34 | int transfermode,long transfermodetimestamp, |
35 | struct rns__StartSessionResponse &r ) try { | 35 | struct rns__StartSessionResponse &r ) try { |
36 | #ifndef NDEBUG | 36 | #ifndef NDEBUG |
37 | syslog(LOG_DEBUG, | 37 | syslog(LOG_DEBUG, |
38 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", | 38 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", |
39 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); | 39 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); |
40 | #endif | 40 | #endif |
41 | eyekinfig_t eyekinfig(macaddress); | 41 | eyekinfig_t eyekinfig(macaddress); |
42 | r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); | 42 | r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); |
43 | 43 | ||
44 | r.snonce = session_nonce.make_nonce().hex(); | 44 | r.snonce = session_nonce.make_nonce().hex(); |
45 | r.transfermode=transfermode; | 45 | r.transfermode=transfermode; |
46 | r.transfermodetimestamp=transfermodetimestamp; | 46 | r.transfermodetimestamp=transfermodetimestamp; |
47 | r.upsyncallowed=false; | 47 | r.upsyncallowed=false; |
48 | 48 | ||
49 | std::string cmd = eyekinfig.get_on_start_session(); | 49 | std::string cmd = eyekinfig.get_on_start_session(); |
50 | if(!cmd.empty()) { | 50 | if(!cmd.empty()) { |
51 | if(detached_child()) { | 51 | if(detached_child()) { |
52 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 52 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
53 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); | 53 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); |
54 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); | 54 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); |
55 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 55 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
56 | execv("/bin/sh",argv); | 56 | execv("/bin/sh",argv); |
57 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 57 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
58 | _exit(-1); | 58 | _exit(-1); |
59 | } | 59 | } |
60 | } | 60 | } |
61 | return SOAP_OK; | 61 | return SOAP_OK; |
62 | }catch(std::runtime_error& e) { | 62 | }catch(std::runtime_error& e) { |
63 | syslog(LOG_ERR,"error while processing StartSession: %s",e.what()); | 63 | syslog(LOG_ERR,"error while processing StartSession: %s",e.what()); |
64 | } | 64 | } |
65 | 65 | ||
66 | int eyefiService::GetPhotoStatus( | 66 | int eyefiService::GetPhotoStatus( |
67 | std::string credential, std::string macaddress, | 67 | std::string credential, std::string macaddress, |
68 | std::string filename, long filesize, std::string filesignature, | 68 | std::string filename, long filesize, std::string filesignature, |
69 | int flags, | 69 | int flags, |
70 | struct rns__GetPhotoStatusResponse &r ) { | 70 | struct rns__GetPhotoStatusResponse &r ) { |
71 | #ifndef NDEBUG | 71 | #ifndef NDEBUG |
72 | syslog(LOG_DEBUG, | 72 | syslog(LOG_DEBUG, |
73 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", | 73 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", |
74 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, | 74 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, |
75 | session_nonce.hex().c_str() ); | 75 | session_nonce.hex().c_str() ); |
76 | #endif | 76 | #endif |
77 | 77 | ||
78 | std::string computed_credential = binary_t(macaddress+eyekinfig_t(macaddress).get_upload_key()+session_nonce.hex()).md5().hex(); | 78 | std::string computed_credential = binary_t(macaddress+eyekinfig_t(macaddress).get_upload_key()+session_nonce.hex()).md5().hex(); |
79 | 79 | ||
80 | #ifndef NDEBUG | 80 | #ifndef NDEBUG |
81 | syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); | 81 | syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); |
82 | #endif | 82 | #endif |
83 | 83 | ||
84 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); | 84 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); |
85 | 85 | ||
86 | r.fileid = 1; r.offset = 0; | 86 | r.fileid = 1; r.offset = 0; |
87 | return SOAP_OK; | 87 | return SOAP_OK; |
88 | } | 88 | } |
89 | 89 | ||
90 | int eyefiService::MarkLastPhotoInRoll( | 90 | int eyefiService::MarkLastPhotoInRoll( |
91 | std::string macaddress, int mergedelta, | 91 | std::string macaddress, int mergedelta, |
92 | struct rns__MarkLastPhotoInRollResponse &r ) { | 92 | struct rns__MarkLastPhotoInRollResponse &r ) { |
93 | #ifndef NDEBUG | 93 | #ifndef NDEBUG |
94 | syslog(LOG_DEBUG, | 94 | syslog(LOG_DEBUG, |
95 | "MarkLastPhotoInRoll request from %s with mergedelta=%d", | 95 | "MarkLastPhotoInRoll request from %s with mergedelta=%d", |
96 | macaddress.c_str(), mergedelta ); | 96 | macaddress.c_str(), mergedelta ); |
97 | #endif | 97 | #endif |
98 | std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); | 98 | std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); |
99 | if(!cmd.empty()) { | 99 | if(!cmd.empty()) { |
100 | if(detached_child()) { | 100 | if(detached_child()) { |
101 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 101 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
102 | putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); | 102 | putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); |
103 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 103 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
104 | execv("/bin/sh",argv); | 104 | execv("/bin/sh",argv); |
105 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 105 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
106 | _exit(-1); | 106 | _exit(-1); |
107 | } | 107 | } |
108 | } | 108 | } |
109 | keep_alive = 0; | 109 | keep_alive = 0; |
110 | return SOAP_OK; | 110 | return SOAP_OK; |
111 | } | 111 | } |
112 | 112 | ||
113 | int eyefiService::UploadPhoto( | 113 | int eyefiService::UploadPhoto( |
114 | int fileid, std::string macaddress, | 114 | int fileid, std::string macaddress, |
115 | std::string filename, long filesize, std::string filesignature, | 115 | std::string filename, long filesize, std::string filesignature, |
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 | } |