-rw-r--r-- | src/eyefiworker.cc | 4 | ||||
-rw-r--r-- | src/eyefiworker.h | 1 | ||||
-rw-r--r-- | src/eyetil.h | 5 | ||||
-rw-r--r-- | src/iiid.cc | 4 |
4 files changed, 12 insertions, 2 deletions
diff --git a/src/eyefiworker.cc b/src/eyefiworker.cc index ac75fc1..1979b46 100644 --- a/src/eyefiworker.cc +++ b/src/eyefiworker.cc | |||
@@ -1,315 +1,317 @@ | |||
1 | #include <signal.h> | 1 | #include <signal.h> |
2 | #ifndef NDEBUG | 2 | #ifndef NDEBUG |
3 | # include <sys/resource.h> | 3 | # include <sys/resource.h> |
4 | #endif | 4 | #endif |
5 | #include <syslog.h> | 5 | #include <syslog.h> |
6 | #include <cassert> | 6 | #include <cassert> |
7 | #include <iostream> | 7 | #include <iostream> |
8 | #include <fstream> | 8 | #include <fstream> |
9 | #include <stdexcept> | 9 | #include <stdexcept> |
10 | #include <iterator> | 10 | #include <iterator> |
11 | #include <algorithm> | 11 | #include <algorithm> |
12 | #include <sys/wait.h> | 12 | #include <sys/wait.h> |
13 | #include <autosprintf.h> | 13 | #include <autosprintf.h> |
14 | #include "eyekinfig.h" | 14 | #include "eyekinfig.h" |
15 | #include "eyetil.h" | 15 | #include "eyetil.h" |
16 | #include "eyefiworker.h" | 16 | #include "eyefiworker.h" |
17 | #ifdef HAVE_SQLITE | 17 | #ifdef HAVE_SQLITE |
18 | # include "iiidb.h" | 18 | # include "iiidb.h" |
19 | #endif | 19 | #endif |
20 | 20 | ||
21 | eyefiworker::eyefiworker() | 21 | eyefiworker::eyefiworker() |
22 | : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) { | 22 | : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) { |
23 | bind_flags = SO_REUSEADDR; max_keep_alive = 0; | 23 | bind_flags = SO_REUSEADDR; max_keep_alive = 0; |
24 | socket_flags = | 24 | socket_flags = |
25 | #if defined(MSG_NOSIGNAL) | 25 | #if defined(MSG_NOSIGNAL) |
26 | MSG_NOSIGNAL | 26 | MSG_NOSIGNAL |
27 | #elif defined(SO_NOSIGPIPE) | 27 | #elif defined(SO_NOSIGPIPE) |
28 | SO_NOSIGPIPE | 28 | SO_NOSIGPIPE |
29 | #else | 29 | #else |
30 | #error Something is wrong with sigpipe prevention on the platform | 30 | #error Something is wrong with sigpipe prevention on the platform |
31 | #endif | 31 | #endif |
32 | ; | 32 | ; |
33 | } | 33 | } |
34 | eyefiworker::~eyefiworker() { | ||
35 | } | ||
34 | 36 | ||
35 | int eyefiworker::run(int bindport) { | 37 | int eyefiworker::run(int bindport) { |
36 | #ifdef HAVE_SQLITE | 38 | #ifdef HAVE_SQLITE |
37 | sqlite3_initialize(); | 39 | sqlite3_initialize(); |
38 | #endif | 40 | #endif |
39 | if(!soap_valid_socket(bind(0,bindport,64))) | 41 | if(!soap_valid_socket(bind(0,bindport,64))) |
40 | throw std::runtime_error("failed to bind()"); | 42 | throw std::runtime_error("failed to bind()"); |
41 | signal(SIGCHLD,SIG_IGN); | 43 | signal(SIGCHLD,SIG_IGN); |
42 | while(true) { | 44 | while(true) { |
43 | if(!soap_valid_socket(accept())) | 45 | if(!soap_valid_socket(accept())) |
44 | throw std::runtime_error("failed to accept()"); | 46 | throw std::runtime_error("failed to accept()"); |
45 | pid_t p = fork(); | 47 | pid_t p = fork(); |
46 | if(p<0) throw std::runtime_error("failed to fork()"); | 48 | if(p<0) throw std::runtime_error("failed to fork()"); |
47 | if(!p) { | 49 | if(!p) { |
48 | recv_timeout = 600; send_timeout = 120; | 50 | recv_timeout = 600; send_timeout = 120; |
49 | (void)serve(); | 51 | (void)serve(); |
50 | soap_destroy(this); soap_end(this); soap_done(this); | 52 | soap_destroy(this); soap_end(this); soap_done(this); |
51 | #ifndef NDEBUG | 53 | #ifndef NDEBUG |
52 | struct rusage ru; | 54 | struct rusage ru; |
53 | if(getrusage(RUSAGE_SELF,&ru)) { | 55 | if(getrusage(RUSAGE_SELF,&ru)) { |
54 | syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno); | 56 | syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno); |
55 | }else{ | 57 | }else{ |
56 | syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss); | 58 | syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss); |
57 | } | 59 | } |
58 | #endif /* NDEBUG */ | 60 | #endif /* NDEBUG */ |
59 | _exit(0); | 61 | throw throwable_exit(0); |
60 | } | 62 | } |
61 | close(socket); socket = SOAP_INVALID_SOCKET; | 63 | close(socket); socket = SOAP_INVALID_SOCKET; |
62 | } | 64 | } |
63 | } | 65 | } |
64 | 66 | ||
65 | static binary_t session_nonce; | 67 | static binary_t session_nonce; |
66 | #ifdef HAVE_SQLITE | 68 | #ifdef HAVE_SQLITE |
67 | static struct { | 69 | static struct { |
68 | std::string filesignature; | 70 | std::string filesignature; |
69 | long filesize; | 71 | long filesize; |
70 | std::string filename; | 72 | std::string filename; |
71 | inline void reset() { filesignature.erase(); filename.erase(); filesize=0; } | 73 | inline void reset() { filesignature.erase(); filename.erase(); filesize=0; } |
72 | inline void set(const std::string n,const std::string sig,long siz) { | 74 | inline void set(const std::string n,const std::string sig,long siz) { |
73 | filename = n; filesignature = sig; filesize = siz; | 75 | filename = n; filesignature = sig; filesize = siz; |
74 | } | 76 | } |
75 | inline bool is(const std::string n,const std::string sig,long siz) { | 77 | inline bool is(const std::string n,const std::string sig,long siz) { |
76 | return filesize==siz && filename==n && filesignature==sig; | 78 | return filesize==siz && filename==n && filesignature==sig; |
77 | } | 79 | } |
78 | } already; | 80 | } already; |
79 | #endif /* HAVE_SQLITE */ | 81 | #endif /* HAVE_SQLITE */ |
80 | 82 | ||
81 | static bool detached_child() { | 83 | static bool detached_child() { |
82 | pid_t p = fork(); | 84 | pid_t p = fork(); |
83 | if(p<0) { | 85 | if(p<0) { |
84 | syslog(LOG_ERR,"Failed to fork away for hook execution"); | 86 | syslog(LOG_ERR,"Failed to fork away for hook execution"); |
85 | _exit(-1); | 87 | _exit(-1); |
86 | } | 88 | } |
87 | if(!p) { | 89 | if(!p) { |
88 | setsid(); | 90 | setsid(); |
89 | for(int i=getdtablesize();i>=0;--i) close(i); | 91 | for(int i=getdtablesize();i>=0;--i) close(i); |
90 | int i=open("/dev/null",O_RDWR); assert(i==0); | 92 | int i=open("/dev/null",O_RDWR); assert(i==0); |
91 | i = dup(i); assert(i==1); | 93 | i = dup(i); assert(i==1); |
92 | i = dup(i); assert(i==2); | 94 | i = dup(i); assert(i==2); |
93 | return true; | 95 | return true; |
94 | } | 96 | } |
95 | return false; | 97 | return false; |
96 | } | 98 | } |
97 | 99 | ||
98 | static int E(eyefiworker* efs,const char *c,const std::exception& e) { | 100 | static int E(eyefiworker* efs,const char *c,const std::exception& e) { |
99 | efs->keep_alive=0; | 101 | efs->keep_alive=0; |
100 | syslog(LOG_ERR,"error while processing %s: %s",c,e.what()); | 102 | syslog(LOG_ERR,"error while processing %s: %s",c,e.what()); |
101 | return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0); | 103 | return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0); |
102 | } | 104 | } |
103 | 105 | ||
104 | int eyefiworker::StartSession( | 106 | int eyefiworker::StartSession( |
105 | std::string macaddress,std::string cnonce, | 107 | std::string macaddress,std::string cnonce, |
106 | int transfermode,long transfermodetimestamp, | 108 | int transfermode,long transfermodetimestamp, |
107 | struct rns__StartSessionResponse &r ) try { | 109 | struct rns__StartSessionResponse &r ) try { |
108 | syslog(LOG_INFO, | 110 | syslog(LOG_INFO, |
109 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", | 111 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", |
110 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); | 112 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); |
111 | eyekinfig_t eyekinfig(macaddress); | 113 | eyekinfig_t eyekinfig(macaddress); |
112 | r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); | 114 | r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); |
113 | 115 | ||
114 | r.snonce = session_nonce.make_nonce().hex(); | 116 | r.snonce = session_nonce.make_nonce().hex(); |
115 | r.transfermode=transfermode; | 117 | r.transfermode=transfermode; |
116 | r.transfermodetimestamp=transfermodetimestamp; | 118 | r.transfermodetimestamp=transfermodetimestamp; |
117 | r.upsyncallowed=false; | 119 | r.upsyncallowed=false; |
118 | 120 | ||
119 | std::string cmd = eyekinfig.get_on_start_session(); | 121 | std::string cmd = eyekinfig.get_on_start_session(); |
120 | if(!cmd.empty()) { | 122 | if(!cmd.empty()) { |
121 | if(detached_child()) { | 123 | if(detached_child()) { |
122 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 124 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
123 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); | 125 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); |
124 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); | 126 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); |
125 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 127 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
126 | execv("/bin/sh",argv); | 128 | execv("/bin/sh",argv); |
127 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 129 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
128 | _exit(-1); | 130 | _exit(-1); |
129 | } | 131 | } |
130 | } | 132 | } |
131 | return SOAP_OK; | 133 | return SOAP_OK; |
132 | }catch(const std::exception& e) { return E(this,"StartSession",e); } | 134 | }catch(const std::exception& e) { return E(this,"StartSession",e); } |
133 | 135 | ||
134 | int eyefiworker::GetPhotoStatus( | 136 | int eyefiworker::GetPhotoStatus( |
135 | std::string credential, std::string macaddress, | 137 | std::string credential, std::string macaddress, |
136 | std::string filename, long filesize, std::string filesignature, | 138 | std::string filename, long filesize, std::string filesignature, |
137 | int flags, | 139 | int flags, |
138 | struct rns__GetPhotoStatusResponse &r ) try { | 140 | struct rns__GetPhotoStatusResponse &r ) try { |
139 | syslog(LOG_INFO, | 141 | syslog(LOG_INFO, |
140 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", | 142 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", |
141 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, | 143 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, |
142 | session_nonce.hex().c_str() ); | 144 | session_nonce.hex().c_str() ); |
143 | 145 | ||
144 | eyekinfig_t eyekinfig(macaddress); | 146 | eyekinfig_t eyekinfig(macaddress); |
145 | std::string computed_credential = binary_t(macaddress+eyekinfig.get_upload_key()+session_nonce.hex()).md5().hex(); | 147 | std::string computed_credential = binary_t(macaddress+eyekinfig.get_upload_key()+session_nonce.hex()).md5().hex(); |
146 | 148 | ||
147 | #ifndef NDEBUG | 149 | #ifndef NDEBUG |
148 | syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); | 150 | syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); |
149 | #endif | 151 | #endif |
150 | 152 | ||
151 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); | 153 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); |
152 | 154 | ||
153 | #ifdef HAVE_SQLITE | 155 | #ifdef HAVE_SQLITE |
154 | iiidb_t D(eyekinfig); | 156 | iiidb_t D(eyekinfig); |
155 | seclude::stmt_t S = D.prepare( | 157 | seclude::stmt_t S = D.prepare( |
156 | "SELECT fileid FROM photo" | 158 | "SELECT fileid FROM photo" |
157 | " WHERE mac=:mac AND filename=:filename" | 159 | " WHERE mac=:mac AND filename=:filename" |
158 | " AND filesize=:filesize AND filesignature=:filesignature" | 160 | " AND filesize=:filesize AND filesignature=:filesignature" |
159 | ).bind(":mac",macaddress) | 161 | ).bind(":mac",macaddress) |
160 | .bind(":filename",filename).bind(":filesize",filesize) | 162 | .bind(":filename",filename).bind(":filesize",filesize) |
161 | .bind(":filesignature",filesignature); | 163 | .bind(":filesignature",filesignature); |
162 | if(!S.step()) { | 164 | if(!S.step()) { |
163 | r.fileid = 1; r.offset = 0; | 165 | r.fileid = 1; r.offset = 0; |
164 | }else{ | 166 | }else{ |
165 | r.fileid = S.column<long>(0); | 167 | r.fileid = S.column<long>(0); |
166 | r.offset = filesize; | 168 | r.offset = filesize; |
167 | already.set(filename,filesignature,filesize); | 169 | already.set(filename,filesignature,filesize); |
168 | } | 170 | } |
169 | #else /* HAVE_SQLITE */ | 171 | #else /* HAVE_SQLITE */ |
170 | r.fileid=1, r.offset=0; | 172 | r.fileid=1, r.offset=0; |
171 | #endif /* HAVE_SQLITE */ | 173 | #endif /* HAVE_SQLITE */ |
172 | return SOAP_OK; | 174 | return SOAP_OK; |
173 | }catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } | 175 | }catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } |
174 | 176 | ||
175 | int eyefiworker::MarkLastPhotoInRoll( | 177 | int eyefiworker::MarkLastPhotoInRoll( |
176 | std::string macaddress, int mergedelta, | 178 | std::string macaddress, int mergedelta, |
177 | struct rns__MarkLastPhotoInRollResponse&/* r */ ) try { | 179 | struct rns__MarkLastPhotoInRollResponse&/* r */ ) try { |
178 | syslog(LOG_INFO, | 180 | syslog(LOG_INFO, |
179 | "MarkLastPhotoInRoll request from %s with mergedelta=%d", | 181 | "MarkLastPhotoInRoll request from %s with mergedelta=%d", |
180 | macaddress.c_str(), mergedelta ); | 182 | macaddress.c_str(), mergedelta ); |
181 | std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); | 183 | std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); |
182 | if(!cmd.empty()) { | 184 | if(!cmd.empty()) { |
183 | if(detached_child()) { | 185 | if(detached_child()) { |
184 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 186 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
185 | putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); | 187 | putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); |
186 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 188 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
187 | execv("/bin/sh",argv); | 189 | execv("/bin/sh",argv); |
188 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 190 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
189 | _exit(-1); | 191 | _exit(-1); |
190 | } | 192 | } |
191 | } | 193 | } |
192 | keep_alive = 0; | 194 | keep_alive = 0; |
193 | return SOAP_OK; | 195 | return SOAP_OK; |
194 | }catch(const std::exception& e) { return E(this,"MarkLastPhotoInRoll",e); } | 196 | }catch(const std::exception& e) { return E(this,"MarkLastPhotoInRoll",e); } |
195 | 197 | ||
196 | int eyefiworker::UploadPhoto( | 198 | int eyefiworker::UploadPhoto( |
197 | int fileid, std::string macaddress, | 199 | int fileid, std::string macaddress, |
198 | std::string filename, long filesize, std::string filesignature, | 200 | std::string filename, long filesize, std::string filesignature, |
199 | std::string encryption, int flags, | 201 | std::string encryption, int flags, |
200 | struct rns__UploadPhotoResponse& r ) try { | 202 | struct rns__UploadPhotoResponse& r ) try { |
201 | syslog(LOG_INFO, | 203 | syslog(LOG_INFO, |
202 | "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," | 204 | "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," |
203 | " filesignature=%s, encryption=%s, flags=%04X", | 205 | " filesignature=%s, encryption=%s, flags=%04X", |
204 | macaddress.c_str(), fileid, filename.c_str(), filesize, | 206 | macaddress.c_str(), fileid, filename.c_str(), filesize, |
205 | filesignature.c_str(), encryption.c_str(), flags ); | 207 | filesignature.c_str(), encryption.c_str(), flags ); |
206 | std::string::size_type fnl=filename.length(); | 208 | std::string::size_type fnl=filename.length(); |
207 | if(fnl<sizeof(".tar") || strncmp(filename.c_str()+fnl-sizeof(".tar")+sizeof(""),".tar",sizeof(".tar"))) | 209 | if(fnl<sizeof(".tar") || strncmp(filename.c_str()+fnl-sizeof(".tar")+sizeof(""),".tar",sizeof(".tar"))) |
208 | throw std::runtime_error(gnu::autosprintf("honestly, I expected the tarball coming here, not '%s'",filename.c_str())); | 210 | throw std::runtime_error(gnu::autosprintf("honestly, I expected the tarball coming here, not '%s'",filename.c_str())); |
209 | std::string the_file(filename,0,fnl-sizeof(".tar")+sizeof("")); | 211 | std::string the_file(filename,0,fnl-sizeof(".tar")+sizeof("")); |
210 | std::string the_log = the_file+".log"; | 212 | std::string the_log = the_file+".log"; |
211 | 213 | ||
212 | eyekinfig_t eyekinfig(macaddress); | 214 | eyekinfig_t eyekinfig(macaddress); |
213 | 215 | ||
214 | umask(eyekinfig.get_umask()); | 216 | umask(eyekinfig.get_umask()); |
215 | 217 | ||
216 | std::string td = eyekinfig.get_targetdir(); | 218 | std::string td = eyekinfig.get_targetdir(); |
217 | tmpdir_t indir(td+"/.incoming.XXXXXX"); | 219 | tmpdir_t indir(td+"/.incoming.XXXXXX"); |
218 | 220 | ||
219 | std::string tf,lf; | 221 | std::string tf,lf; |
220 | binary_t digest, idigest; | 222 | binary_t digest, idigest; |
221 | #ifdef HAVE_SQLITE | 223 | #ifdef HAVE_SQLITE |
222 | bool beenthere = false; | 224 | bool beenthere = false; |
223 | #endif | 225 | #endif |
224 | 226 | ||
225 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { | 227 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { |
226 | #ifndef NDEBUG | 228 | #ifndef NDEBUG |
227 | syslog(LOG_DEBUG, | 229 | syslog(LOG_DEBUG, |
228 | " MIME attachment with id=%s, type=%s, size=%ld", | 230 | " MIME attachment with id=%s, type=%s, size=%ld", |
229 | (*i).id, (*i).type, (long)(*i).size ); | 231 | (*i).id, (*i).type, (long)(*i).size ); |
230 | #endif | 232 | #endif |
231 | 233 | ||
232 | if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { | 234 | if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { |
233 | std::string idigestr((*i).ptr,(*i).size); | 235 | std::string idigestr((*i).ptr,(*i).size); |
234 | #ifndef NDEBUG | 236 | #ifndef NDEBUG |
235 | syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); | 237 | syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); |
236 | #endif | 238 | #endif |
237 | idigest.from_hex(idigestr); | 239 | idigest.from_hex(idigestr); |
238 | } | 240 | } |
239 | if( (*i).id && !strcmp((*i).id,"FILENAME") ) { | 241 | if( (*i).id && !strcmp((*i).id,"FILENAME") ) { |
240 | assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); | 242 | assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); |
241 | #ifdef III_SAVE_TARS | 243 | #ifdef III_SAVE_TARS |
242 | std::string tarfile = indir.get_file(filename); | 244 | std::string tarfile = indir.get_file(filename); |
243 | { | 245 | { |
244 | std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); | 246 | std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); |
245 | } | 247 | } |
246 | #endif | 248 | #endif |
247 | 249 | ||
248 | if(!tf.empty()) throw std::runtime_error("already seen tarball"); | 250 | if(!tf.empty()) throw std::runtime_error("already seen tarball"); |
249 | if(!digest.empty()) throw std::runtime_error("already have integrity digest"); | 251 | if(!digest.empty()) throw std::runtime_error("already have integrity digest"); |
250 | digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); | 252 | digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); |
251 | #ifndef NDEBUG | 253 | #ifndef NDEBUG |
252 | syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); | 254 | syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); |
253 | #endif | 255 | #endif |
254 | #ifdef HAVE_SQLITE | 256 | #ifdef HAVE_SQLITE |
255 | if(!(*i).size) { | 257 | if(!(*i).size) { |
256 | if(!already.is(filename,filesignature,filesize)) | 258 | if(!already.is(filename,filesignature,filesize)) |
257 | throw std::runtime_error("got zero-length upload for unknown file"); | 259 | throw std::runtime_error("got zero-length upload for unknown file"); |
258 | beenthere = true; continue; | 260 | beenthere = true; continue; |
259 | } | 261 | } |
260 | #endif | 262 | #endif |
261 | 263 | ||
262 | tarchive_t a((*i).ptr,(*i).size); | 264 | tarchive_t a((*i).ptr,(*i).size); |
263 | while(a.read_next_header()) { | 265 | while(a.read_next_header()) { |
264 | std::string ep = a.entry_pathname(), f = indir.get_file(ep); | 266 | std::string ep = a.entry_pathname(), f = indir.get_file(ep); |
265 | if(ep==the_file) tf = f; | 267 | if(ep==the_file) tf = f; |
266 | else if(ep==the_log) lf = f; | 268 | else if(ep==the_log) lf = f; |
267 | else continue; | 269 | else continue; |
268 | int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666); | 270 | int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666); |
269 | if(fd<0) | 271 | if(fd<0) |
270 | throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str())); | 272 | throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str())); |
271 | if(!a.read_data_into_fd(fd)) | 273 | if(!a.read_data_into_fd(fd)) |
272 | throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); | 274 | throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); |
273 | close(fd); | 275 | close(fd); |
274 | } | 276 | } |
275 | } | 277 | } |
276 | } | 278 | } |
277 | 279 | ||
278 | #ifdef HAVE_SQLITE | 280 | #ifdef HAVE_SQLITE |
279 | if(beenthere) { | 281 | if(beenthere) { |
280 | r.success=true; | 282 | r.success=true; |
281 | return SOAP_OK; | 283 | return SOAP_OK; |
282 | } | 284 | } |
283 | #endif | 285 | #endif |
284 | 286 | ||
285 | if(tf.empty()) throw std::runtime_error("haven't seen THE file"); | 287 | if(tf.empty()) throw std::runtime_error("haven't seen THE file"); |
286 | if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); | 288 | if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); |
287 | 289 | ||
288 | std::string::size_type ls = tf.rfind('/'); | 290 | std::string::size_type ls = tf.rfind('/'); |
289 | // XXX: actually, lack of '/' signifies error here | 291 | // XXX: actually, lack of '/' signifies error here |
290 | std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); | 292 | std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); |
291 | ls = lf.rfind('/'); | 293 | ls = lf.rfind('/'); |
292 | std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); | 294 | std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); |
293 | std::string ttf,tlf; | 295 | std::string ttf,tlf; |
294 | bool success = false; | 296 | bool success = false; |
295 | for(int i=0;i<32767;++i) { | 297 | for(int i=0;i<32767;++i) { |
296 | const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s"; | 298 | const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s"; |
297 | ttf = (const char*)gnu::autosprintf(fmt,td.c_str(),tbn.c_str(),i); | 299 | ttf = (const char*)gnu::autosprintf(fmt,td.c_str(),tbn.c_str(),i); |
298 | if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i); | 300 | if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i); |
299 | if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty() || !link(lf.c_str(),tlf.c_str())) ) { | 301 | if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty() || !link(lf.c_str(),tlf.c_str())) ) { |
300 | unlink(tf.c_str()); | 302 | unlink(tf.c_str()); |
301 | if(!lf.empty()) unlink(lf.c_str()); | 303 | if(!lf.empty()) unlink(lf.c_str()); |
302 | success=true; | 304 | success=true; |
303 | break; | 305 | break; |
304 | } | 306 | } |
305 | } | 307 | } |
306 | std::string cmd = eyekinfig.get_on_upload_photo(); | 308 | std::string cmd = eyekinfig.get_on_upload_photo(); |
307 | if(success) { | 309 | if(success) { |
308 | #ifdef HAVE_SQLITE | 310 | #ifdef HAVE_SQLITE |
309 | { | 311 | { |
310 | iiidb_t D(eyekinfig); | 312 | iiidb_t D(eyekinfig); |
311 | D.prepare( | 313 | D.prepare( |
312 | "INSERT INTO photo" | 314 | "INSERT INTO photo" |
313 | " (ctime,mac,fileid,filename,filesize,filesignature,encryption,flags)" | 315 | " (ctime,mac,fileid,filename,filesize,filesignature,encryption,flags)" |
314 | " VALUES" | 316 | " VALUES" |
315 | " (:ctime,:mac,:fileid,:filename,:filesize,:filesignature,:encryption,:flags)" | 317 | " (:ctime,:mac,:fileid,:filename,:filesize,:filesignature,:encryption,:flags)" |
diff --git a/src/eyefiworker.h b/src/eyefiworker.h index 6d4082c..6cdecff 100644 --- a/src/eyefiworker.h +++ b/src/eyefiworker.h | |||
@@ -1,29 +1,30 @@ | |||
1 | #ifndef __EYEFIWORKER_H | 1 | #ifndef __EYEFIWORKER_H |
2 | #define __EYEFIWORKER_H | 2 | #define __EYEFIWORKER_H |
3 | 3 | ||
4 | #include "soapeyefiService.h" | 4 | #include "soapeyefiService.h" |
5 | 5 | ||
6 | class eyefiworker : public eyefiService { | 6 | class eyefiworker : public eyefiService { |
7 | public: | 7 | public: |
8 | 8 | ||
9 | eyefiworker(); | 9 | eyefiworker(); |
10 | ~eyefiworker(); | ||
10 | 11 | ||
11 | int run(int port) __attribute__ ((noreturn)); | 12 | int run(int port) __attribute__ ((noreturn)); |
12 | 13 | ||
13 | int StartSession(std::string macaddress, std::string cnonce, | 14 | int StartSession(std::string macaddress, std::string cnonce, |
14 | int transfermode, long transfermodetimestamp, | 15 | int transfermode, long transfermodetimestamp, |
15 | struct rns__StartSessionResponse &r); | 16 | struct rns__StartSessionResponse &r); |
16 | int GetPhotoStatus(std::string credential, std::string macaddress, | 17 | int GetPhotoStatus(std::string credential, std::string macaddress, |
17 | std::string filename, long filesize, std::string filesignature, int flags, | 18 | std::string filename, long filesize, std::string filesignature, int flags, |
18 | struct rns__GetPhotoStatusResponse &r); | 19 | struct rns__GetPhotoStatusResponse &r); |
19 | int MarkLastPhotoInRoll(std::string macaddress, int mergedelta, | 20 | int MarkLastPhotoInRoll(std::string macaddress, int mergedelta, |
20 | struct rns__MarkLastPhotoInRollResponse &r); | 21 | struct rns__MarkLastPhotoInRollResponse &r); |
21 | int UploadPhoto(int fileid, std::string macaddress, | 22 | int UploadPhoto(int fileid, std::string macaddress, |
22 | std::string filename, long filesize, std::string filesignature, | 23 | std::string filename, long filesize, std::string filesignature, |
23 | std::string encryption, int flags, | 24 | std::string encryption, int flags, |
24 | struct rns__UploadPhotoResponse &r); | 25 | struct rns__UploadPhotoResponse &r); |
25 | 26 | ||
26 | eyefiService *copy() { throw std::logic_error("Not meant to be called"); } | 27 | eyefiService *copy() { throw std::logic_error("Not meant to be called"); } |
27 | }; | 28 | }; |
28 | 29 | ||
29 | #endif /* __EYEFIWORKER_H */ | 30 | #endif /* __EYEFIWORKER_H */ |
diff --git a/src/eyetil.h b/src/eyetil.h index eff2c43..03b9ba8 100644 --- a/src/eyetil.h +++ b/src/eyetil.h | |||
@@ -1,89 +1,94 @@ | |||
1 | #ifndef __EYETIL_H | 1 | #ifndef __EYETIL_H |
2 | #define __EYETIL_H | 2 | #define __EYETIL_H |
3 | 3 | ||
4 | #include <vector> | 4 | #include <vector> |
5 | #include <string> | 5 | #include <string> |
6 | #include <archive.h> | 6 | #include <archive.h> |
7 | #include <archive_entry.h> | 7 | #include <archive_entry.h> |
8 | #include "openssl/md5.h" | 8 | #include "openssl/md5.h" |
9 | 9 | ||
10 | struct throwable_exit { | ||
11 | int rc; | ||
12 | throwable_exit(int rc_) : rc(rc_) { } | ||
13 | }; | ||
14 | |||
10 | class binary_t : public std::vector<unsigned char> { | 15 | class binary_t : public std::vector<unsigned char> { |
11 | public: | 16 | public: |
12 | binary_t() { } | 17 | binary_t() { } |
13 | binary_t(size_type n) : std::vector<unsigned char>(n) { } | 18 | binary_t(size_type n) : std::vector<unsigned char>(n) { } |
14 | binary_t(const std::string& h) { from_hex(h); } | 19 | binary_t(const std::string& h) { from_hex(h); } |
15 | binary_t(const void *d,size_t s) { from_data(d,s); } | 20 | binary_t(const void *d,size_t s) { from_data(d,s); } |
16 | 21 | ||
17 | binary_t& from_hex(const std::string& h); | 22 | binary_t& from_hex(const std::string& h); |
18 | binary_t& from_data(const void *d,size_t s); | 23 | binary_t& from_data(const void *d,size_t s); |
19 | binary_t& make_nonce(); | 24 | binary_t& make_nonce(); |
20 | 25 | ||
21 | std::string hex() const; | 26 | std::string hex() const; |
22 | binary_t md5() const; | 27 | binary_t md5() const; |
23 | }; | 28 | }; |
24 | 29 | ||
25 | struct md5_digester { | 30 | struct md5_digester { |
26 | MD5_CTX ctx; | 31 | MD5_CTX ctx; |
27 | md5_digester() { init(); } | 32 | md5_digester() { init(); } |
28 | 33 | ||
29 | void init(); | 34 | void init(); |
30 | void update(const void *d,size_t l); | 35 | void update(const void *d,size_t l); |
31 | binary_t final(); | 36 | binary_t final(); |
32 | 37 | ||
33 | template<typename T> | 38 | template<typename T> |
34 | void update(const T& x) { update(&x,sizeof(x)); } | 39 | void update(const T& x) { update(&x,sizeof(x)); } |
35 | 40 | ||
36 | template<typename T> | 41 | template<typename T> |
37 | struct update_iterator : public std::iterator<std::output_iterator_tag,T,void,T*,T&> { | 42 | struct update_iterator : public std::iterator<std::output_iterator_tag,T,void,T*,T&> { |
38 | md5_digester *d; | 43 | md5_digester *d; |
39 | update_iterator(md5_digester *d_) : d(d_) { } | 44 | update_iterator(md5_digester *d_) : d(d_) { } |
40 | update_iterator(const update_iterator& x) : d(x.d) { } | 45 | update_iterator(const update_iterator& x) : d(x.d) { } |
41 | 46 | ||
42 | update_iterator& operator*() { return *this; } | 47 | update_iterator& operator*() { return *this; } |
43 | update_iterator& operator++() { return *this; } | 48 | update_iterator& operator++() { return *this; } |
44 | update_iterator& operator++(int) { return *this; } | 49 | update_iterator& operator++(int) { return *this; } |
45 | 50 | ||
46 | update_iterator& operator=(const T& x) { | 51 | update_iterator& operator=(const T& x) { |
47 | d->update(x); return *this; | 52 | d->update(x); return *this; |
48 | } | 53 | } |
49 | }; | 54 | }; |
50 | 55 | ||
51 | template<typename T> | 56 | template<typename T> |
52 | update_iterator<T> updater() { | 57 | update_iterator<T> updater() { |
53 | return update_iterator<T>(this); | 58 | return update_iterator<T>(this); |
54 | } | 59 | } |
55 | 60 | ||
56 | }; | 61 | }; |
57 | template<> inline void md5_digester::update<binary_t>(const binary_t& x) { | 62 | template<> inline void md5_digester::update<binary_t>(const binary_t& x) { |
58 | update((const unsigned char*)&(x.front()),x.size()); | 63 | update((const unsigned char*)&(x.front()),x.size()); |
59 | } | 64 | } |
60 | 65 | ||
61 | class tmpdir_t { | 66 | class tmpdir_t { |
62 | public: | 67 | public: |
63 | std::string dir; | 68 | std::string dir; |
64 | 69 | ||
65 | tmpdir_t(const std::string& dt); | 70 | tmpdir_t(const std::string& dt); |
66 | ~tmpdir_t(); | 71 | ~tmpdir_t(); |
67 | 72 | ||
68 | std::string get_file(const std::string& f); | 73 | std::string get_file(const std::string& f); |
69 | }; | 74 | }; |
70 | 75 | ||
71 | class tarchive_t { | 76 | class tarchive_t { |
72 | public: | 77 | public: |
73 | struct archive *a; | 78 | struct archive *a; |
74 | struct archive_entry *e; | 79 | struct archive_entry *e; |
75 | 80 | ||
76 | tarchive_t(void *p,size_t s); | 81 | tarchive_t(void *p,size_t s); |
77 | ~tarchive_t(); | 82 | ~tarchive_t(); |
78 | 83 | ||
79 | bool read_next_header(); | 84 | bool read_next_header(); |
80 | 85 | ||
81 | std::string entry_pathname(); | 86 | std::string entry_pathname(); |
82 | 87 | ||
83 | bool read_data_into_fd(int fd); | 88 | bool read_data_into_fd(int fd); |
84 | }; | 89 | }; |
85 | 90 | ||
86 | binary_t integrity_digest(const void *ptr,size_t size, | 91 | binary_t integrity_digest(const void *ptr,size_t size, |
87 | const std::string& ukey); | 92 | const std::string& ukey); |
88 | 93 | ||
89 | #endif /* __EYETIL_H */ | 94 | #endif /* __EYETIL_H */ |
diff --git a/src/iiid.cc b/src/iiid.cc index d655fe3..b3dd3bf 100644 --- a/src/iiid.cc +++ b/src/iiid.cc | |||
@@ -1,97 +1,99 @@ | |||
1 | #include <syslog.h> | 1 | #include <syslog.h> |
2 | #include <getopt.h> | 2 | #include <getopt.h> |
3 | #include <sys/stat.h> | 3 | #include <sys/stat.h> |
4 | #include <glob.h> | 4 | #include <glob.h> |
5 | #include <iostream> | 5 | #include <iostream> |
6 | #include <cassert> | 6 | #include <cassert> |
7 | #include <stdexcept> | 7 | #include <stdexcept> |
8 | #include "eyetil.h" | 8 | #include "eyetil.h" |
9 | #include "eyefiworker.h" | 9 | #include "eyefiworker.h" |
10 | 10 | ||
11 | #include "config.h" | 11 | #include "config.h" |
12 | 12 | ||
13 | #include "eyefi.nsmap" | 13 | #include "eyefi.nsmap" |
14 | 14 | ||
15 | #define PHEADER \ | 15 | #define PHEADER \ |
16 | PACKAGE " Version " VERSION "\n" \ | 16 | PACKAGE " Version " VERSION "\n" \ |
17 | "Copyright (c) 2009 Klever Group" | 17 | "Copyright (c) 2009 Klever Group" |
18 | 18 | ||
19 | int main(int argc,char **argv) try { | 19 | int main(int argc,char **argv) try { |
20 | 20 | ||
21 | int port = 59278; | 21 | int port = 59278; |
22 | 22 | ||
23 | while(true) { | 23 | while(true) { |
24 | static struct option opts[] = { | 24 | static struct option opts[] = { |
25 | { "help", no_argument, 0, 'h' }, | 25 | { "help", no_argument, 0, 'h' }, |
26 | { "usage", no_argument, 0, 'h' }, | 26 | { "usage", no_argument, 0, 'h' }, |
27 | { "version", no_argument, 0, 'V' }, | 27 | { "version", no_argument, 0, 'V' }, |
28 | { "license", no_argument, 0, 'L' }, | 28 | { "license", no_argument, 0, 'L' }, |
29 | { "port", required_argument, 0, 'p' }, | 29 | { "port", required_argument, 0, 'p' }, |
30 | { NULL, 0, 0, 0 } | 30 | { NULL, 0, 0, 0 } |
31 | }; | 31 | }; |
32 | int c = getopt_long(argc,argv,"hVLp:",opts,NULL); | 32 | int c = getopt_long(argc,argv,"hVLp:",opts,NULL); |
33 | if(c==-1) break; | 33 | if(c==-1) break; |
34 | switch(c) { | 34 | switch(c) { |
35 | case 'h': | 35 | case 'h': |
36 | std::cerr << PHEADER << std::endl << std::endl | 36 | std::cerr << PHEADER << std::endl << std::endl |
37 | << " " << argv[0] << " [options]" << std::endl | 37 | << " " << argv[0] << " [options]" << std::endl |
38 | << std::endl << | 38 | << std::endl << |
39 | " -h, --help,\n" | 39 | " -h, --help,\n" |
40 | " --usage display this text\n" | 40 | " --usage display this text\n" |
41 | " -V, --version display version information\n" | 41 | " -V, --version display version information\n" |
42 | " -L, --license show license\n" | 42 | " -L, --license show license\n" |
43 | " -p <port>, --port=<port> port to listen to\n" | 43 | " -p <port>, --port=<port> port to listen to\n" |
44 | " (you're not likely to ever need it)\n" | 44 | " (you're not likely to ever need it)\n" |
45 | << std::endl << std::endl; | 45 | << std::endl << std::endl; |
46 | exit(0); | 46 | exit(0); |
47 | break; | 47 | break; |
48 | case 'V': | 48 | case 'V': |
49 | std::cerr << VERSION << std::endl; | 49 | std::cerr << VERSION << std::endl; |
50 | exit(0); | 50 | exit(0); |
51 | break; | 51 | break; |
52 | case 'L': | 52 | case 'L': |
53 | extern const char *COPYING; | 53 | extern const char *COPYING; |
54 | std::cerr << COPYING << std::endl; | 54 | std::cerr << COPYING << std::endl; |
55 | exit(0); | 55 | exit(0); |
56 | break; | 56 | break; |
57 | case 'p': | 57 | case 'p': |
58 | port = 0xffff&strtol(optarg,0,0); | 58 | port = 0xffff&strtol(optarg,0,0); |
59 | if(errno) { | 59 | if(errno) { |
60 | std::cerr << "Failed to parse port number" << std::endl; | 60 | std::cerr << "Failed to parse port number" << std::endl; |
61 | exit(1); | 61 | exit(1); |
62 | } | 62 | } |
63 | break; | 63 | break; |
64 | default: | 64 | default: |
65 | std::cerr << "Huh?" << std::endl; | 65 | std::cerr << "Huh?" << std::endl; |
66 | exit(1); | 66 | exit(1); |
67 | break; | 67 | break; |
68 | } | 68 | } |
69 | } | 69 | } |
70 | 70 | ||
71 | const char *ident = rindex(*argv,'/'); | 71 | const char *ident = rindex(*argv,'/'); |
72 | if(ident) | 72 | if(ident) |
73 | ++ident; | 73 | ++ident; |
74 | else | 74 | else |
75 | ident = *argv; | 75 | ident = *argv; |
76 | openlog(ident,LOG_PERROR|LOG_PID,LOG_DAEMON); | 76 | openlog(ident,LOG_PERROR|LOG_PID,LOG_DAEMON); |
77 | syslog(LOG_INFO,"Starting iii eye-fi manager"); | 77 | syslog(LOG_INFO,"Starting iii eye-fi manager"); |
78 | 78 | ||
79 | struct stat st; | 79 | struct stat st; |
80 | if(stat(EYEKIN_CONF_DIR,&st) || !S_ISDIR(st.st_mode)) | 80 | if(stat(EYEKIN_CONF_DIR,&st) || !S_ISDIR(st.st_mode)) |
81 | syslog(LOG_WARNING,"configuration directory '%s' does not exist or is not a directory",EYEKIN_CONF_DIR); | 81 | syslog(LOG_WARNING,"configuration directory '%s' does not exist or is not a directory",EYEKIN_CONF_DIR); |
82 | glob_t g; int rg = glob(EYEKIN_CONF_DIR"/????????????.conf",GLOB_NOSORT,NULL,&g); | 82 | glob_t g; int rg = glob(EYEKIN_CONF_DIR"/????????????.conf",GLOB_NOSORT,NULL,&g); |
83 | if(rg || !g.gl_pathc) | 83 | if(rg || !g.gl_pathc) |
84 | syslog(LOG_WARNING,"I see nothing resembling a card config in '%s'",EYEKIN_CONF_DIR); | 84 | syslog(LOG_WARNING,"I see nothing resembling a card config in '%s'",EYEKIN_CONF_DIR); |
85 | else | 85 | else |
86 | globfree(&g); | 86 | globfree(&g); |
87 | 87 | ||
88 | eyefiworker().run(port); | 88 | eyefiworker().run(port); |
89 | 89 | ||
90 | closelog(); | 90 | closelog(); |
91 | return 0; | 91 | return 0; |
92 | } catch(std::exception& e) { | 92 | } catch(const throwable_exit& e) { |
93 | return e.rc; | ||
94 | } catch(const std::exception& e) { | ||
93 | syslog(LOG_CRIT,"Exiting iii daemon, because of error condition"); | 95 | syslog(LOG_CRIT,"Exiting iii daemon, because of error condition"); |
94 | syslog(LOG_CRIT,"Exception: %s",e.what()); | 96 | syslog(LOG_CRIT,"Exception: %s",e.what()); |
95 | return 1; | 97 | return 1; |
96 | } | 98 | } |
97 | 99 | ||