summaryrefslogtreecommitdiffabout
path: root/src
authorMichael Krelin <hacker@klever.net>2013-01-29 18:28:58 (UTC)
committer Michael Krelin <hacker@klever.net>2013-02-02 12:42:52 (UTC)
commit8cef826610f171b25a7a4aa3a764b1fb04c24d2f (patch) (unidiff)
tree0f4cd74156e998931955c9117fb263a37ba1f98c /src
parent7e4f9ebf4a76de3adafba6b32620d4610341897b (diff)
downloadiii-8cef826610f171b25a7a4aa3a764b1fb04c24d2f.zip
iii-8cef826610f171b25a7a4aa3a764b1fb04c24d2f.tar.gz
iii-8cef826610f171b25a7a4aa3a764b1fb04c24d2f.tar.bz2
added persistent store for photo status
Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (limited to 'src') (more/less context) (ignore whitespace changes)
-rw-r--r--src/Makefile.am7
-rw-r--r--src/eyefiservice.cc77
-rw-r--r--src/eyefiworker.cc6
-rw-r--r--src/iiidb.h28
4 files changed, 111 insertions, 7 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index b31bed5..f2ff772 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,36 +1,37 @@
1bin_PROGRAMS=iii-extract-riff-chunk 1bin_PROGRAMS=iii-extract-riff-chunk
2sbin_PROGRAMS=iiid 2sbin_PROGRAMS=iiid
3noinst_HEADERS = \ 3noinst_HEADERS = \
4 eyefi.h \ 4 eyefi.h \
5 eyekinfig.h eyetil.h \ 5 eyekinfig.h eyetil.h \
6 eyefiworker.h 6 eyefiworker.h iiidb.h \
7 seclude.h
7 8
8AM_CPPFLAGS = ${CPPFLAGS_DEBUG} \ 9AM_CPPFLAGS = ${CPPFLAGS_DEBUG} \
9 -DEYEKIN_CONF_DIR=\"${sysconfdir}/${PACKAGE}\" 10 -DEYEKIN_CONF_DIR=\"${sysconfdir}/${PACKAGE}\"
10DEFAULT_INCLUDES = -I${top_builddir} -I${builddir} -I${srcdir} 11DEFAULT_INCLUDES = -I${top_builddir} -I${builddir} -I${srcdir}
11INCLUDES = ${MODULES_CFLAGS} ${UUID_CFLAGS} 12INCLUDES = ${MODULES_CFLAGS} ${UUID_CFLAGS} ${SQLITE_CFLAGS}
12 13
13iiid_SOURCES = iiid.cc \ 14iiid_SOURCES = iiid.cc \
14 eyekinfig.cc eyetil.cc \ 15 eyekinfig.cc eyetil.cc \
15 eyefiservice.cc eyefiworker.cc 16 eyefiservice.cc eyefiworker.cc
16nodist_iiid_SOURCES = \ 17nodist_iiid_SOURCES = \
17 ${builddir}/soapC.cpp ${builddir}/soapeyefiService.cpp \ 18 ${builddir}/soapC.cpp ${builddir}/soapeyefiService.cpp \
18 COPYING.cc 19 COPYING.cc
19iiid_LDADD = ${MODULES_LIBS} ${UUID_LIBS} 20iiid_LDADD = ${MODULES_LIBS} ${UUID_LIBS} ${SQLITE_LIBS}
20 21
21iii_extract_riff_chunk_SOURCES = iii-extract-riff-chunk.cc 22iii_extract_riff_chunk_SOURCES = iii-extract-riff-chunk.cc
22nodist_iii_extract_riff_chunk_SOURCES = COPYING.cc 23nodist_iii_extract_riff_chunk_SOURCES = COPYING.cc
23 24
24COPYING.cc: ${top_srcdir}/COPYING 25COPYING.cc: ${top_srcdir}/COPYING
25 echo "const char * COPYING = " >$@ || (rm $@;exit 1) 26 echo "const char * COPYING = " >$@ || (rm $@;exit 1)
26 sed -e 's/"/\\"/g' -e 's/^/\"/' -e 's/$$/\\n\"/' $< >>$@ || (rm $@;exit 1) 27 sed -e 's/"/\\"/g' -e 's/^/\"/' -e 's/$$/\\n\"/' $< >>$@ || (rm $@;exit 1)
27 echo ';' >>$@ || (rm $@;exit 1) 28 echo ';' >>$@ || (rm $@;exit 1)
28 29
29${srcdir}/eyefiservice.cc: ${builddir}/soapeyefiService.h 30${srcdir}/eyefiservice.cc: ${builddir}/soapeyefiService.h
30${srcdir}/iiid.cc: ${builddir}/eyefi.nsmap 31${srcdir}/iiid.cc: ${builddir}/eyefi.nsmap
31 32
32${builddir}soapC.cpp ${builddir}/soapeyefiService.cpp ${builddir}/eyefi.nsmap ${builddir}/soapeyefiService.h: ${srcdir}/eyefi.h 33${builddir}soapC.cpp ${builddir}/soapeyefiService.cpp ${builddir}/eyefi.nsmap ${builddir}/soapeyefiService.h: ${srcdir}/eyefi.h
33 ${SOAPCPP2} -d${builddir} -S -L -a -i -w -x $< 34 ${SOAPCPP2} -d${builddir} -S -L -a -i -w -x $<
34 35
35clean-local: 36clean-local:
36 rm -f soap{{H,Stub,eyefiService}.h,{C,eyefiService}.cpp} eyefi.nsmap COPYING.cc 37 rm -f soap{{H,Stub,eyefiService}.h,{C,eyefiService}.cpp} eyefi.nsmap COPYING.cc
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc
index 93bbcca..4a4a179 100644
--- a/src/eyefiservice.cc
+++ b/src/eyefiservice.cc
@@ -1,221 +1,290 @@
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 <algorithm> 6#include <algorithm>
7#include <syslog.h> 7#include <syslog.h>
8#include <sys/wait.h> 8#include <sys/wait.h>
9#include <autosprintf.h> 9#include <autosprintf.h>
10#include "eyekinfig.h" 10#include "eyekinfig.h"
11#include "eyetil.h" 11#include "eyetil.h"
12#include "soapeyefiService.h" 12#include "soapeyefiService.h"
13#ifdef HAVE_SQLITE
14# include "iiidb.h"
15#endif
13 16
14static binary_t session_nonce; 17static binary_t session_nonce;
18#ifdef HAVE_SQLITE
19 static struct {
20 std::string filesignature;
21 long filesize;
22 std::string filename;
23 inline void reset() { filesignature.erase(); filename.erase(); filesize=0; }
24 inline void set(const std::string n,const std::string sig,long siz) {
25 filename = n; filesignature = sig; filesize = siz;
26 }
27 inline bool is(const std::string n,const std::string sig,long siz) {
28 return filesize==siz && filename==n && filesignature==sig;
29 }
30 } already;
31#endif /* HAVE_SQLITE */
15 32
16static bool detached_child() { 33static bool detached_child() {
17 pid_t p = fork(); 34 pid_t p = fork();
18 if(p<0) { 35 if(p<0) {
19 syslog(LOG_ERR,"Failed to fork away for hook execution"); 36 syslog(LOG_ERR,"Failed to fork away for hook execution");
20 _exit(-1); 37 _exit(-1);
21 } 38 }
22 if(!p) { 39 if(!p) {
23 setsid(); 40 setsid();
24 for(int i=getdtablesize();i>=0;--i) close(i); 41 for(int i=getdtablesize();i>=0;--i) close(i);
25 int i=open("/dev/null",O_RDWR); assert(i==0); 42 int i=open("/dev/null",O_RDWR); assert(i==0);
26 i = dup(i); assert(i==1); 43 i = dup(i); assert(i==1);
27 i = dup(i); assert(i==2); 44 i = dup(i); assert(i==2);
28 return true; 45 return true;
29 } 46 }
30 return false; 47 return false;
31} 48}
32 49
33static int E(eyefiService* efs,const char *c,const std::exception& e) { 50static int E(eyefiService* efs,const char *c,const std::exception& e) {
34 efs->keep_alive=0; 51 efs->keep_alive=0;
35 syslog(LOG_ERR,"error while processing %s: %s",c,e.what()); 52 syslog(LOG_ERR,"error while processing %s: %s",c,e.what());
36 return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0); 53 return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0);
37} 54}
38 55
39int eyefiService::StartSession( 56int eyefiService::StartSession(
40 std::string macaddress,std::string cnonce, 57 std::string macaddress,std::string cnonce,
41 int transfermode,long transfermodetimestamp, 58 int transfermode,long transfermodetimestamp,
42 struct rns__StartSessionResponse &r ) try { 59 struct rns__StartSessionResponse &r ) try {
43 syslog(LOG_INFO, 60 syslog(LOG_INFO,
44 "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", 61 "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld",
45 macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); 62 macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp );
46 eyekinfig_t eyekinfig(macaddress); 63 eyekinfig_t eyekinfig(macaddress);
47 r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); 64 r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex();
48 65
49 r.snonce = session_nonce.make_nonce().hex(); 66 r.snonce = session_nonce.make_nonce().hex();
50 r.transfermode=transfermode; 67 r.transfermode=transfermode;
51 r.transfermodetimestamp=transfermodetimestamp; 68 r.transfermodetimestamp=transfermodetimestamp;
52 r.upsyncallowed=false; 69 r.upsyncallowed=false;
53 70
54 std::string cmd = eyekinfig.get_on_start_session(); 71 std::string cmd = eyekinfig.get_on_start_session();
55 if(!cmd.empty()) { 72 if(!cmd.empty()) {
56 if(detached_child()) { 73 if(detached_child()) {
57 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); 74 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) );
58 putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); 75 putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) );
59 putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); 76 putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) );
60 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; 77 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 };
61 execv("/bin/sh",argv); 78 execv("/bin/sh",argv);
62 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); 79 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str());
63 _exit(-1); 80 _exit(-1);
64 } 81 }
65 } 82 }
66 return SOAP_OK; 83 return SOAP_OK;
67}catch(const std::exception& e) { return E(this,"StartSession",e); } 84}catch(const std::exception& e) { return E(this,"StartSession",e); }
68 85
69int eyefiService::GetPhotoStatus( 86int eyefiService::GetPhotoStatus(
70 std::string credential, std::string macaddress, 87 std::string credential, std::string macaddress,
71 std::string filename, long filesize, std::string filesignature, 88 std::string filename, long filesize, std::string filesignature,
72 int flags, 89 int flags,
73 struct rns__GetPhotoStatusResponse &r ) try { 90 struct rns__GetPhotoStatusResponse &r ) try {
74 syslog(LOG_INFO, 91 syslog(LOG_INFO,
75 "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", 92 "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s",
76 macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, 93 macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags,
77 session_nonce.hex().c_str() ); 94 session_nonce.hex().c_str() );
78 95
79 std::string computed_credential = binary_t(macaddress+eyekinfig_t(macaddress).get_upload_key()+session_nonce.hex()).md5().hex(); 96 eyekinfig_t eyekinfig(macaddress);
97 std::string computed_credential = binary_t(macaddress+eyekinfig.get_upload_key()+session_nonce.hex()).md5().hex();
80 98
81#ifndef NDEBUG 99#ifndef NDEBUG
82 syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); 100 syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str());
83#endif 101#endif
84 102
85 if (credential != computed_credential) throw std::runtime_error("card authentication failed"); 103 if (credential != computed_credential) throw std::runtime_error("card authentication failed");
86 104
87 r.fileid = 1; r.offset = 0; 105#ifdef HAVE_SQLITE
106 iiidb_t D(eyekinfig);
107 seclude::stmt_t S = D.prepare(
108 "SELECT fileid FROM photo"
109 " WHERE mac=:mac AND filename=:filename"
110 " AND filesize=:filesize AND filesignature=:filesignature"
111 ).bind(":mac",macaddress)
112 .bind(":filename",filename).bind(":filesize",filesize)
113 .bind(":filesignature",filesignature);
114 if(!S.step()) {
115 r.fileid = 1; r.offset = 0;
116 }else{
117 r.fileid = S.column<long>(0);
118 r.offset = filesize;
119 already.set(filename,filesignature,filesize);
120 }
121#else /* HAVE_SQLITE */
122 r.fileid=1, r.offset=0;
123#endif /* HAVE_SQLITE */
88 return SOAP_OK; 124 return SOAP_OK;
89}catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } 125}catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); }
90 126
91int eyefiService::MarkLastPhotoInRoll( 127int eyefiService::MarkLastPhotoInRoll(
92 std::string macaddress, int mergedelta, 128 std::string macaddress, int mergedelta,
93 struct rns__MarkLastPhotoInRollResponse&/* r */ ) try { 129 struct rns__MarkLastPhotoInRollResponse&/* r */ ) try {
94 syslog(LOG_INFO, 130 syslog(LOG_INFO,
95 "MarkLastPhotoInRoll request from %s with mergedelta=%d", 131 "MarkLastPhotoInRoll request from %s with mergedelta=%d",
96 macaddress.c_str(), mergedelta ); 132 macaddress.c_str(), mergedelta );
97 std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); 133 std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll();
98 if(!cmd.empty()) { 134 if(!cmd.empty()) {
99 if(detached_child()) { 135 if(detached_child()) {
100 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); 136 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) );
101 putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); 137 putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) );
102 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; 138 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 };
103 execv("/bin/sh",argv); 139 execv("/bin/sh",argv);
104 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); 140 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str());
105 _exit(-1); 141 _exit(-1);
106 } 142 }
107 } 143 }
108 keep_alive = 0; 144 keep_alive = 0;
109 return SOAP_OK; 145 return SOAP_OK;
110}catch(const std::exception& e) { return E(this,"MarkLastPhotoInRoll",e); } 146}catch(const std::exception& e) { return E(this,"MarkLastPhotoInRoll",e); }
111 147
112int eyefiService::UploadPhoto( 148int eyefiService::UploadPhoto(
113 int fileid, std::string macaddress, 149 int fileid, std::string macaddress,
114 std::string filename, long filesize, std::string filesignature, 150 std::string filename, long filesize, std::string filesignature,
115 std::string encryption, int flags, 151 std::string encryption, int flags,
116 struct rns__UploadPhotoResponse& r ) try { 152 struct rns__UploadPhotoResponse& r ) try {
117 syslog(LOG_INFO, 153 syslog(LOG_INFO,
118 "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," 154 "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld,"
119 " filesignature=%s, encryption=%s, flags=%04X", 155 " filesignature=%s, encryption=%s, flags=%04X",
120 macaddress.c_str(), fileid, filename.c_str(), filesize, 156 macaddress.c_str(), fileid, filename.c_str(), filesize,
121 filesignature.c_str(), encryption.c_str(), flags ); 157 filesignature.c_str(), encryption.c_str(), flags );
122 std::string::size_type fnl=filename.length(); 158 std::string::size_type fnl=filename.length();
123 if(fnl<sizeof(".tar") || strncmp(filename.c_str()+fnl-sizeof(".tar")+sizeof(""),".tar",sizeof(".tar"))) 159 if(fnl<sizeof(".tar") || strncmp(filename.c_str()+fnl-sizeof(".tar")+sizeof(""),".tar",sizeof(".tar")))
124 throw std::runtime_error(gnu::autosprintf("honestly, I expected the tarball coming here, not '%s'",filename.c_str())); 160 throw std::runtime_error(gnu::autosprintf("honestly, I expected the tarball coming here, not '%s'",filename.c_str()));
125 std::string the_file(filename,0,fnl-sizeof(".tar")+sizeof("")); 161 std::string the_file(filename,0,fnl-sizeof(".tar")+sizeof(""));
126 std::string the_log = the_file+".log"; 162 std::string the_log = the_file+".log";
127 163
128 eyekinfig_t eyekinfig(macaddress); 164 eyekinfig_t eyekinfig(macaddress);
129 165
130 umask(eyekinfig.get_umask()); 166 umask(eyekinfig.get_umask());
131 167
132 std::string td = eyekinfig.get_targetdir(); 168 std::string td = eyekinfig.get_targetdir();
133 tmpdir_t indir(td+"/.incoming.XXXXXX"); 169 tmpdir_t indir(td+"/.incoming.XXXXXX");
134 170
135 std::string tf,lf; 171 std::string tf,lf;
136 binary_t digest, idigest; 172 binary_t digest, idigest;
173#ifdef HAVE_SQLITE
174 bool beenthere = false;
175#endif
137 176
138 for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { 177 for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) {
139#ifndef NDEBUG 178#ifndef NDEBUG
140 syslog(LOG_DEBUG, 179 syslog(LOG_DEBUG,
141 " MIME attachment with id=%s, type=%s, size=%ld", 180 " MIME attachment with id=%s, type=%s, size=%ld",
142 (*i).id, (*i).type, (long)(*i).size ); 181 (*i).id, (*i).type, (long)(*i).size );
143#endif 182#endif
144 183
145 if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { 184 if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) {
146 std::string idigestr((*i).ptr,(*i).size); 185 std::string idigestr((*i).ptr,(*i).size);
147#ifndef NDEBUG 186#ifndef NDEBUG
148 syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); 187 syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str());
149#endif 188#endif
150 idigest.from_hex(idigestr); 189 idigest.from_hex(idigestr);
151 } 190 }
152 if( (*i).id && !strcmp((*i).id,"FILENAME") ) { 191 if( (*i).id && !strcmp((*i).id,"FILENAME") ) {
153 assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); 192 assert( (*i).type && !strcmp((*i).type,"application/x-tar") );
154#ifdef III_SAVE_TARS 193#ifdef III_SAVE_TARS
155 std::string tarfile = indir.get_file(filename); 194 std::string tarfile = indir.get_file(filename);
156 { 195 {
157 std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); 196 std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size);
158 } 197 }
159#endif 198#endif
160 199
161 if(!tf.empty()) throw std::runtime_error("already seen tarball"); 200 if(!tf.empty()) throw std::runtime_error("already seen tarball");
162 if(!digest.empty()) throw std::runtime_error("already have integrity digest"); 201 if(!digest.empty()) throw std::runtime_error("already have integrity digest");
163 digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); 202 digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key());
164#ifndef NDEBUG 203#ifndef NDEBUG
165 syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); 204 syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str());
166#endif 205#endif
206#ifdef HAVE_SQLITE
207 if(!(*i).size) {
208 if(!already.is(filename,filesignature,filesize))
209 throw std::runtime_error("got zero-length upload for unknown file");
210 beenthere = true; continue;
211 }
212#endif
167 213
168 tarchive_t a((*i).ptr,(*i).size); 214 tarchive_t a((*i).ptr,(*i).size);
169 while(a.read_next_header()) { 215 while(a.read_next_header()) {
170 std::string ep = a.entry_pathname(), f = indir.get_file(ep); 216 std::string ep = a.entry_pathname(), f = indir.get_file(ep);
171 if(ep==the_file) tf = f; 217 if(ep==the_file) tf = f;
172 else if(ep==the_log) lf = f; 218 else if(ep==the_log) lf = f;
173 else continue; 219 else continue;
174 int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666); 220 int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666);
175 if(fd<0) 221 if(fd<0)
176 throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str())); 222 throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str()));
177 if(!a.read_data_into_fd(fd)) 223 if(!a.read_data_into_fd(fd))
178 throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); 224 throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str()));
179 close(fd); 225 close(fd);
180 } 226 }
181 } 227 }
182 } 228 }
183 229
230#ifdef HAVE_SQLITE
231 if(beenthere) {
232 r.success=true;
233 return SOAP_OK;
234 }
235#endif
236
184 if(tf.empty()) throw std::runtime_error("haven't seen THE file"); 237 if(tf.empty()) throw std::runtime_error("haven't seen THE file");
185 if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); 238 if(digest!=idigest) throw std::runtime_error("integrity digest verification failed");
186 239
187 std::string::size_type ls = tf.rfind('/'); 240 std::string::size_type ls = tf.rfind('/');
188 // XXX: actually, lack of '/' signifies error here 241 // XXX: actually, lack of '/' signifies error here
189 std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); 242 std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1);
190 ls = lf.rfind('/'); 243 ls = lf.rfind('/');
191 std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); 244 std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1);
192 std::string ttf,tlf; 245 std::string ttf,tlf;
193 bool success = false; 246 bool success = false;
194 for(int i=0;i<32767;++i) { 247 for(int i=0;i<32767;++i) {
195 const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s"; 248 const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s";
196 ttf = (const char*)gnu::autosprintf(fmt,td.c_str(),tbn.c_str(),i); 249 ttf = (const char*)gnu::autosprintf(fmt,td.c_str(),tbn.c_str(),i);
197 if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i); 250 if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i);
198 if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty() || !link(lf.c_str(),tlf.c_str())) ) { 251 if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty() || !link(lf.c_str(),tlf.c_str())) ) {
199 unlink(tf.c_str()); 252 unlink(tf.c_str());
200 if(!lf.empty()) unlink(lf.c_str()); 253 if(!lf.empty()) unlink(lf.c_str());
201 success=true; 254 success=true;
202 break; 255 break;
203 } 256 }
204 } 257 }
205 std::string cmd = eyekinfig.get_on_upload_photo(); 258 std::string cmd = eyekinfig.get_on_upload_photo();
206 if(success && !cmd.empty()) { 259 if(success) {
207 if(detached_child()) { 260#ifdef HAVE_SQLITE
261 {
262 iiidb_t D(eyekinfig);
263 D.prepare(
264 "INSERT INTO photo"
265 " (ctime,mac,fileid,filename,filesize,filesignature,encryption,flags)"
266 " VALUES"
267 " (:ctime,:mac,:fileid,:filename,:filesize,:filesignature,:encryption,:flags)"
268 ).bind(":ctime",time(0))
269 .bind(":mac",macaddress)
270 .bind(":fileid",fileid).bind(":filename",filename)
271 .bind(":filesize",filesize).bind(":filesignature",filesignature)
272 .bind(":encryption",encryption).bind(":flags",flags)
273 .step();
274 }
275#endif /* HAVE_SQLITE */
276 if((!cmd.empty()) && detached_child()) {
208 putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); 277 putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) );
209 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); 278 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) );
210 putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) ); 279 putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) );
211 if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) ); 280 if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) );
212 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; 281 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 };
213 execv("/bin/sh",argv); 282 execv("/bin/sh",argv);
214 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); 283 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str());
215 _exit(-1); 284 _exit(-1);
216 } 285 }
217 } 286 }
218 287
219 r.success = true; 288 r.success = true;
220 return SOAP_OK; 289 return SOAP_OK;
221}catch(const std::exception& e) { return E(this,"UploadPhoto",e); } 290}catch(const std::exception& e) { return E(this,"UploadPhoto",e); }
diff --git a/src/eyefiworker.cc b/src/eyefiworker.cc
index c051482..9dcd048 100644
--- a/src/eyefiworker.cc
+++ b/src/eyefiworker.cc
@@ -1,36 +1,42 @@
1#include <signal.h> 1#include <signal.h>
2#include <stdexcept> 2#include <stdexcept>
3#include "eyefiworker.h" 3#include "eyefiworker.h"
4#ifdef HAVE_SQLITE
5# include "sqlite3.h"
6#endif
4 7
5eyefiworker::eyefiworker() 8eyefiworker::eyefiworker()
6 : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) { 9 : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) {
7 bind_flags = SO_REUSEADDR; max_keep_alive = 0; 10 bind_flags = SO_REUSEADDR; max_keep_alive = 0;
8 socket_flags = 11 socket_flags =
9#if defined(MSG_NOSIGNAL) 12#if defined(MSG_NOSIGNAL)
10 MSG_NOSIGNAL 13 MSG_NOSIGNAL
11#elif defined(SO_NOSIGPIPE) 14#elif defined(SO_NOSIGPIPE)
12 SO_NOSIGPIPE 15 SO_NOSIGPIPE
13#else 16#else
14#error Something is wrong with sigpipe prevention on the platform 17#error Something is wrong with sigpipe prevention on the platform
15#endif 18#endif
16 ; 19 ;
17 } 20 }
18 21
19int eyefiworker::run(int bindport) { 22int eyefiworker::run(int bindport) {
23#ifdef HAVE_SQLITE
24 sqlite3_initialize();
25#endif
20 if(!soap_valid_socket(bind(0,bindport,64))) 26 if(!soap_valid_socket(bind(0,bindport,64)))
21 throw std::runtime_error("failed to bind()"); 27 throw std::runtime_error("failed to bind()");
22 signal(SIGCHLD,SIG_IGN); 28 signal(SIGCHLD,SIG_IGN);
23 while(true) { 29 while(true) {
24 if(!soap_valid_socket(accept())) 30 if(!soap_valid_socket(accept()))
25 throw std::runtime_error("failed to accept()"); 31 throw std::runtime_error("failed to accept()");
26 pid_t p = fork(); 32 pid_t p = fork();
27 if(p<0) throw std::runtime_error("failed to fork()"); 33 if(p<0) throw std::runtime_error("failed to fork()");
28 if(!p) { 34 if(!p) {
29 recv_timeout = 600; send_timeout = 120; 35 recv_timeout = 600; send_timeout = 120;
30 (void)serve(); 36 (void)serve();
31 soap_destroy(this); soap_end(this); soap_done(this); 37 soap_destroy(this); soap_end(this); soap_done(this);
32 _exit(0); 38 _exit(0);
33 } 39 }
34 close(socket); socket = SOAP_INVALID_SOCKET; 40 close(socket); socket = SOAP_INVALID_SOCKET;
35 } 41 }
36} 42}
diff --git a/src/iiidb.h b/src/iiidb.h
new file mode 100644
index 0000000..e77fa09
--- a/dev/null
+++ b/src/iiidb.h
@@ -0,0 +1,28 @@
1#ifndef __IIIDB_H
2#define __IIIDB_H
3
4#include <autosprintf.h>
5#include "seclude.h"
6#include "eyekinfig.h"
7
8struct iiidb_t : public seclude::db_t {
9 iiidb_t(eyekinfig_t& k) : seclude::db_t(gnu::autosprintf("%s/.iii.db",k.get_targetdir().c_str())) {
10 try {
11 exec("SELECT 1 FROM photo LIMIT 0");
12 }catch(const seclude::sqlite3_error& e) {
13 exec( "CREATE TABLE photo ("
14 " id integer PRIMARY KEY AUTOINCREMENT,"
15 " ctime integer NOT NULL,"
16 " mac text NOT NULL,"
17 " fileid integer NOT NULL,"
18 " filename text NOT NULL,"
19 " filesize integer NOT NULL,"
20 " filesignature text NOT NULL UNIQUE,"
21 " encryption text NOT NULL,"
22 " flags integer NOT NULL"
23 ")" );
24 }
25 }
26};
27
28#endif /* __IIIDB_H */