author | Michael Krelin <hacker@klever.net> | 2009-04-06 20:27:39 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2009-04-06 20:27:39 (UTC) |
commit | 51d8a8a4ac6ef6096c393fd602df34c6bf8f6366 (patch) (unidiff) | |
tree | 8126f60fac6562b14c965e41d19983d81312638a /src | |
parent | de964540e5a58b3a9195c642ef7a0745ee3b2344 (diff) | |
download | iii-51d8a8a4ac6ef6096c393fd602df34c6bf8f6366.zip iii-51d8a8a4ac6ef6096c393fd602df34c6bf8f6366.tar.gz iii-51d8a8a4ac6ef6096c393fd602df34c6bf8f6366.tar.bz2 |
better nonce generation and session credentials verification
based on the patch from Chris Davies
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/eyefiservice.cc | 21 | ||||
-rw-r--r-- | src/eyetil.cc | 17 | ||||
-rw-r--r-- | src/eyetil.h | 1 |
4 files changed, 36 insertions, 7 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 09f698e..b5b7d5c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
@@ -1,32 +1,32 @@ | |||
1 | sbin_PROGRAMS=iiid | 1 | sbin_PROGRAMS=iiid |
2 | noinst_HEADERS = \ | 2 | noinst_HEADERS = \ |
3 | eyefi.h \ | 3 | eyefi.h \ |
4 | eyekinfig.h eyetil.h \ | 4 | eyekinfig.h eyetil.h \ |
5 | eyefiworker.h | 5 | eyefiworker.h |
6 | 6 | ||
7 | AM_CPPFLAGS = ${CPPFLAGS_DEBUG} \ | 7 | AM_CPPFLAGS = ${CPPFLAGS_DEBUG} \ |
8 | -DEYEKIN_CONF_DIR=\"${sysconfdir}/${PACKAGE}\" | 8 | -DEYEKIN_CONF_DIR=\"${sysconfdir}/${PACKAGE}\" |
9 | DEFAULT_INCLUDES = -I${top_builddir} -I${builddir} -I${srcdir} | 9 | DEFAULT_INCLUDES = -I${top_builddir} -I${builddir} -I${srcdir} |
10 | INCLUDES = ${MODULES_CFLAGS} | 10 | INCLUDES = ${MODULES_CFLAGS} ${UUID_CFLAGS} |
11 | 11 | ||
12 | iiid_SOURCES = iiid.cc \ | 12 | iiid_SOURCES = iiid.cc \ |
13 | eyekinfig.cc eyetil.cc \ | 13 | eyekinfig.cc eyetil.cc \ |
14 | eyefiservice.cc eyefiworker.cc | 14 | eyefiservice.cc eyefiworker.cc |
15 | nodist_iiid_SOURCES = \ | 15 | nodist_iiid_SOURCES = \ |
16 | ${builddir}/soapC.cpp ${builddir}/soapeyefiService.cpp \ | 16 | ${builddir}/soapC.cpp ${builddir}/soapeyefiService.cpp \ |
17 | COPYING.cc | 17 | COPYING.cc |
18 | iiid_LDADD = ${MODULES_LIBS} | 18 | iiid_LDADD = ${MODULES_LIBS} ${UUID_LIBS} |
19 | 19 | ||
20 | COPYING.cc: ${top_srcdir}/COPYING | 20 | COPYING.cc: ${top_srcdir}/COPYING |
21 | echo "const char * COPYING = " >$@ || (rm $@;exit 1) | 21 | echo "const char * COPYING = " >$@ || (rm $@;exit 1) |
22 | sed -e 's/"/\\"/g' -e 's/^/\"/' -e 's/$$/\\n\"/' $< >>$@ || (rm $@;exit 1) | 22 | sed -e 's/"/\\"/g' -e 's/^/\"/' -e 's/$$/\\n\"/' $< >>$@ || (rm $@;exit 1) |
23 | echo ';' >>$@ || (rm $@;exit 1) | 23 | echo ';' >>$@ || (rm $@;exit 1) |
24 | 24 | ||
25 | ${srcdir}/eyefiservice.cc: ${builddir}/soapeyefiService.h | 25 | ${srcdir}/eyefiservice.cc: ${builddir}/soapeyefiService.h |
26 | ${srcdir}/iiid.cc: ${builddir}/eyefi.nsmap | 26 | ${srcdir}/iiid.cc: ${builddir}/eyefi.nsmap |
27 | 27 | ||
28 | ${builddir}soapC.cpp ${builddir}/soapeyefiService.cpp ${builddir}/eyefi.nsmap ${builddir}/soapeyefiService.h: ${srcdir}/eyefi.h | 28 | ${builddir}soapC.cpp ${builddir}/soapeyefiService.cpp ${builddir}/eyefi.nsmap ${builddir}/soapeyefiService.h: ${srcdir}/eyefi.h |
29 | ${SOAPCPP2} -d${builddir} -S -L -a -i -w -x $< | 29 | ${SOAPCPP2} -d${builddir} -S -L -a -i -w -x $< |
30 | 30 | ||
31 | clean-local: | 31 | clean-local: |
32 | rm -f soap{{H,Stub,eyefiService}.h,{C,eyefiService}.cpp} eyefi.nsmap COPYING.cc | 32 | 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 d233a07..1a21c02 100644 --- a/src/eyefiservice.cc +++ b/src/eyefiservice.cc | |||
@@ -1,202 +1,213 @@ | |||
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 <openssl/rand.h> | ||
9 | #include "eyekinfig.h" | 10 | #include "eyekinfig.h" |
10 | #include "eyetil.h" | 11 | #include "eyetil.h" |
11 | #include "soapeyefiService.h" | 12 | #include "soapeyefiService.h" |
12 | 13 | ||
14 | static binary_t session_nonce; | ||
15 | |||
13 | static bool detached_child() { | 16 | static bool detached_child() { |
14 | pid_t p = fork(); | 17 | pid_t p = fork(); |
15 | if(p<0) throw std::runtime_error("failed to fork()"); | 18 | if(p<0) throw std::runtime_error("failed to fork()"); |
16 | if(!p) { | 19 | if(!p) { |
17 | p = fork(); | 20 | p = fork(); |
18 | if(p<0) { | 21 | if(p<0) { |
19 | syslog(LOG_ERR,"Failed to re-fork child process"); | 22 | syslog(LOG_ERR,"Failed to re-fork child process"); |
20 | _exit(-1); | 23 | _exit(-1); |
21 | } | 24 | } |
22 | if(!p) { | 25 | if(!p) { |
23 | setsid(); | 26 | setsid(); |
24 | for(int i=getdtablesize();i>=0;--i) close(i); | 27 | for(int i=getdtablesize();i>=0;--i) close(i); |
25 | int i=open("/dev/null",O_RDWR); assert(i==0); | 28 | int i=open("/dev/null",O_RDWR); assert(i==0); |
26 | i = dup(i); assert(i==1); | 29 | i = dup(i); assert(i==1); |
27 | i = dup(i); assert(i==2); | 30 | i = dup(i); assert(i==2); |
28 | return true; | 31 | return true; |
29 | } | 32 | } |
30 | _exit(0); | 33 | _exit(0); |
31 | } | 34 | } |
32 | int rc; | 35 | int rc; |
33 | if(waitpid(p,&rc,0)<0) throw std::runtime_error("failed to waitpid()"); | 36 | if(waitpid(p,&rc,0)<0) throw std::runtime_error("failed to waitpid()"); |
34 | if(!WIFEXITED(rc)) throw std::runtime_error("error in forked process"); | 37 | if(!WIFEXITED(rc)) throw std::runtime_error("error in forked process"); |
35 | if(WEXITSTATUS(rc)) throw std::runtime_error("forked process signalled error"); | 38 | if(WEXITSTATUS(rc)) throw std::runtime_error("forked process signalled error"); |
36 | return false; | 39 | return false; |
37 | } | 40 | } |
38 | 41 | ||
39 | int eyefiService::StartSession( | 42 | int eyefiService::StartSession( |
40 | std::string macaddress,std::string cnonce, | 43 | std::string macaddress,std::string cnonce, |
41 | int transfermode,long transfermodetimestamp, | 44 | int transfermode,long transfermodetimestamp, |
42 | struct rns__StartSessionResponse &r ) { | 45 | struct rns__StartSessionResponse &r ) { |
43 | #ifndef NDEBUG | 46 | #ifndef NDEBUG |
44 | syslog(LOG_DEBUG, | 47 | syslog(LOG_DEBUG, |
45 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", | 48 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", |
46 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); | 49 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); |
47 | #endif | 50 | #endif |
48 | r.credential = binary_t(macaddress+cnonce+eyekinfig_t(macaddress).get_upload_key()).md5().hex(); | 51 | r.credential = binary_t(macaddress+cnonce+eyekinfig_t(macaddress).get_upload_key()).md5().hex(); |
49 | /* TODO: better nonce generator */ | 52 | |
50 | time_t t = time(0); | 53 | r.snonce = session_nonce.make_nonce().hex(); |
51 | r.snonce = binary_t(&t,sizeof(t)).md5().hex(); | ||
52 | r.transfermode=transfermode; | 54 | r.transfermode=transfermode; |
53 | r.transfermodetimestamp=transfermodetimestamp; | 55 | r.transfermodetimestamp=transfermodetimestamp; |
54 | r.upsyncallowed=false; | 56 | r.upsyncallowed=false; |
55 | 57 | ||
56 | std::string cmd = eyekinfig_t(macaddress).get_on_start_session(); | 58 | std::string cmd = eyekinfig_t(macaddress).get_on_start_session(); |
57 | if(!cmd.empty()) { | 59 | if(!cmd.empty()) { |
58 | if(detached_child()) { | 60 | if(detached_child()) { |
59 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 61 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
60 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); | 62 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); |
61 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); | 63 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); |
62 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 64 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
63 | execv("/bin/sh",argv); | 65 | execv("/bin/sh",argv); |
64 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 66 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
65 | _exit(-1); | 67 | _exit(-1); |
66 | } | 68 | } |
67 | } | 69 | } |
68 | return SOAP_OK; | 70 | return SOAP_OK; |
69 | } | 71 | } |
70 | 72 | ||
71 | int eyefiService::GetPhotoStatus( | 73 | int eyefiService::GetPhotoStatus( |
72 | std::string credential, std::string macaddress, | 74 | std::string credential, std::string macaddress, |
73 | std::string filename, long filesize, std::string filesignature, | 75 | std::string filename, long filesize, std::string filesignature, |
74 | struct rns__GetPhotoStatusResponse &r ) { | 76 | struct rns__GetPhotoStatusResponse &r ) { |
75 | #ifndef NDEBUG | 77 | #ifndef NDEBUG |
76 | syslog(LOG_DEBUG, | 78 | syslog(LOG_DEBUG, |
77 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s", | 79 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s; session nonce=%s", |
78 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str() ); | 80 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), session_nonce.hex().c_str() ); |
81 | #endif | ||
82 | |||
83 | std::string computed_credential = binary_t(macaddress+eyekinfig_t(macaddress).get_upload_key()+session_nonce.hex()).md5().hex(); | ||
84 | |||
85 | #ifndef NDEBUG | ||
86 | syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); | ||
79 | #endif | 87 | #endif |
88 | |||
89 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); | ||
90 | |||
80 | r.fileid = 1; r.offset = 0; | 91 | r.fileid = 1; r.offset = 0; |
81 | return SOAP_OK; | 92 | return SOAP_OK; |
82 | } | 93 | } |
83 | 94 | ||
84 | int eyefiService::MarkLastPhotoInRoll( | 95 | int eyefiService::MarkLastPhotoInRoll( |
85 | std::string macaddress, int mergedelta, | 96 | std::string macaddress, int mergedelta, |
86 | struct rns__MarkLastPhotoInRollResponse &r ) { | 97 | struct rns__MarkLastPhotoInRollResponse &r ) { |
87 | #ifndef NDEBUG | 98 | #ifndef NDEBUG |
88 | syslog(LOG_DEBUG, | 99 | syslog(LOG_DEBUG, |
89 | "MarkLastPhotoInRoll request from %s with mergedelta=%d", | 100 | "MarkLastPhotoInRoll request from %s with mergedelta=%d", |
90 | macaddress.c_str(), mergedelta ); | 101 | macaddress.c_str(), mergedelta ); |
91 | #endif | 102 | #endif |
92 | std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); | 103 | std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); |
93 | if(!cmd.empty()) { | 104 | if(!cmd.empty()) { |
94 | if(detached_child()) { | 105 | if(detached_child()) { |
95 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 106 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
96 | putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); | 107 | putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); |
97 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 108 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
98 | execv("/bin/sh",argv); | 109 | execv("/bin/sh",argv); |
99 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 110 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
100 | _exit(-1); | 111 | _exit(-1); |
101 | } | 112 | } |
102 | } | 113 | } |
103 | return SOAP_OK; | 114 | return SOAP_OK; |
104 | } | 115 | } |
105 | 116 | ||
106 | int eyefiService::UploadPhoto( | 117 | int eyefiService::UploadPhoto( |
107 | int fileid, std::string macaddress, | 118 | int fileid, std::string macaddress, |
108 | std::string filename, long filesize, std::string filesignature, | 119 | std::string filename, long filesize, std::string filesignature, |
109 | std::string encryption, int flags, | 120 | std::string encryption, int flags, |
110 | struct rns__UploadPhotoResponse& r ) { | 121 | struct rns__UploadPhotoResponse& r ) { |
111 | #ifndef NDEBUG | 122 | #ifndef NDEBUG |
112 | syslog(LOG_DEBUG, | 123 | syslog(LOG_DEBUG, |
113 | "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," | 124 | "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," |
114 | " filesignature=%s, encryption=%s, flags=%04X", | 125 | " filesignature=%s, encryption=%s, flags=%04X", |
115 | macaddress.c_str(), fileid, filename.c_str(), filesize, | 126 | macaddress.c_str(), fileid, filename.c_str(), filesize, |
116 | filesignature.c_str(), encryption.c_str(), flags ); | 127 | filesignature.c_str(), encryption.c_str(), flags ); |
117 | #endif | 128 | #endif |
118 | eyekinfig_t eyekinfig(macaddress); | 129 | eyekinfig_t eyekinfig(macaddress); |
119 | 130 | ||
120 | umask(eyekinfig.get_umask()); | 131 | umask(eyekinfig.get_umask()); |
121 | 132 | ||
122 | std::string td = eyekinfig.get_targetdir(); | 133 | std::string td = eyekinfig.get_targetdir(); |
123 | tmpdir_t indir(td+"/.incoming.XXXXXX"); | 134 | tmpdir_t indir(td+"/.incoming.XXXXXX"); |
124 | 135 | ||
125 | std::string jf; | 136 | std::string jf; |
126 | binary_t digest, idigest; | 137 | binary_t digest, idigest; |
127 | 138 | ||
128 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { | 139 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { |
129 | #ifndef NDEBUG | 140 | #ifndef NDEBUG |
130 | syslog(LOG_DEBUG, | 141 | syslog(LOG_DEBUG, |
131 | " MIME attachment with id=%s, type=%s, size=%ld", | 142 | " MIME attachment with id=%s, type=%s, size=%ld", |
132 | (*i).id, (*i).type, (long)(*i).size ); | 143 | (*i).id, (*i).type, (long)(*i).size ); |
133 | #endif | 144 | #endif |
134 | 145 | ||
135 | if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { | 146 | if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { |
136 | std::string idigestr((*i).ptr,(*i).size); | 147 | std::string idigestr((*i).ptr,(*i).size); |
137 | #ifndef NDEBUG | 148 | #ifndef NDEBUG |
138 | syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); | 149 | syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); |
139 | #endif | 150 | #endif |
140 | idigest.from_hex(idigestr); | 151 | idigest.from_hex(idigestr); |
141 | } | 152 | } |
142 | if( (*i).id && !strcmp((*i).id,"FILENAME") ) { | 153 | if( (*i).id && !strcmp((*i).id,"FILENAME") ) { |
143 | assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); | 154 | assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); |
144 | #ifdef III_SAVE_TARS | 155 | #ifdef III_SAVE_TARS |
145 | std::string tarfile = indir.get_file(filename); | 156 | std::string tarfile = indir.get_file(filename); |
146 | { | 157 | { |
147 | std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); | 158 | std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); |
148 | } | 159 | } |
149 | #endif | 160 | #endif |
150 | 161 | ||
151 | if(!jf.empty()) throw std::runtime_error("already seen tarball"); | 162 | if(!jf.empty()) throw std::runtime_error("already seen tarball"); |
152 | if(!digest.empty()) throw std::runtime_error("already have integrity digest"); | 163 | if(!digest.empty()) throw std::runtime_error("already have integrity digest"); |
153 | digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); | 164 | digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); |
154 | #ifndef NDEBUG | 165 | #ifndef NDEBUG |
155 | syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); | 166 | syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); |
156 | #endif | 167 | #endif |
157 | 168 | ||
158 | tarchive_t a((*i).ptr,(*i).size); | 169 | tarchive_t a((*i).ptr,(*i).size); |
159 | if(!a.read_next_header()) | 170 | if(!a.read_next_header()) |
160 | throw std::runtime_error("failed to tarchive_t::read_next_header())"); | 171 | throw std::runtime_error("failed to tarchive_t::read_next_header())"); |
161 | jf = indir.get_file(a.entry_pathname()); | 172 | jf = indir.get_file(a.entry_pathname()); |
162 | int fd=open(jf.c_str(),O_CREAT|O_WRONLY,0666); | 173 | int fd=open(jf.c_str(),O_CREAT|O_WRONLY,0666); |
163 | assert(fd>0); | 174 | assert(fd>0); |
164 | a.read_data_into_fd(fd); | 175 | a.read_data_into_fd(fd); |
165 | close(fd); | 176 | close(fd); |
166 | } | 177 | } |
167 | } | 178 | } |
168 | 179 | ||
169 | if(jf.empty()) throw std::runtime_error("haven't seen jpeg file"); | 180 | if(jf.empty()) throw std::runtime_error("haven't seen jpeg file"); |
170 | if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); | 181 | if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); |
171 | 182 | ||
172 | std::string::size_type ls = jf.rfind('/'); | 183 | std::string::size_type ls = jf.rfind('/'); |
173 | std::string jbn = (ls==std::string::npos)?jf:jf.substr(ls+1); | 184 | std::string jbn = (ls==std::string::npos)?jf:jf.substr(ls+1); |
174 | std::string tf = td+'/'+jbn; | 185 | std::string tf = td+'/'+jbn; |
175 | bool success = false; | 186 | bool success = false; |
176 | if(!link(jf.c_str(), tf.c_str())) { | 187 | if(!link(jf.c_str(), tf.c_str())) { |
177 | unlink(jf.c_str()); success = true; | 188 | unlink(jf.c_str()); success = true; |
178 | }else{ | 189 | }else{ |
179 | for(int i=1;i<32767;++i) { | 190 | for(int i=1;i<32767;++i) { |
180 | tf = (const char*)gnu::autosprintf( "%s/(%05d)%s", | 191 | tf = (const char*)gnu::autosprintf( "%s/(%05d)%s", |
181 | td.c_str(), i, jbn.c_str() ); | 192 | td.c_str(), i, jbn.c_str() ); |
182 | if(!link(jf.c_str(), tf.c_str())) { | 193 | if(!link(jf.c_str(), tf.c_str())) { |
183 | unlink(jf.c_str()); success = true; | 194 | unlink(jf.c_str()); success = true; |
184 | break; | 195 | break; |
185 | } | 196 | } |
186 | } | 197 | } |
187 | } | 198 | } |
188 | std::string cmd = eyekinfig.get_on_upload_photo(); | 199 | std::string cmd = eyekinfig.get_on_upload_photo(); |
189 | if(success && !cmd.empty()) { | 200 | if(success && !cmd.empty()) { |
190 | if(detached_child()) { | 201 | if(detached_child()) { |
191 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 202 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
192 | putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",tf.c_str()) ); | 203 | putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",tf.c_str()) ); |
193 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 204 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
194 | execv("/bin/sh",argv); | 205 | execv("/bin/sh",argv); |
195 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 206 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
196 | _exit(-1); | 207 | _exit(-1); |
197 | } | 208 | } |
198 | } | 209 | } |
199 | 210 | ||
200 | r.success = true; | 211 | r.success = true; |
201 | return SOAP_OK; | 212 | return SOAP_OK; |
202 | } | 213 | } |
diff --git a/src/eyetil.cc b/src/eyetil.cc index fe816a6..7669cb6 100644 --- a/src/eyetil.cc +++ b/src/eyetil.cc | |||
@@ -1,146 +1,163 @@ | |||
1 | #include <stdlib.h> | 1 | #include <stdlib.h> |
2 | #include <sys/stat.h> | 2 | #include <sys/stat.h> |
3 | #include <syslog.h> | 3 | #include <syslog.h> |
4 | #include <iostream> | 4 | #include <iostream> |
5 | #include <cassert> | 5 | #include <cassert> |
6 | #include <stdexcept> | 6 | #include <stdexcept> |
7 | #include <algorithm> | 7 | #include <algorithm> |
8 | #include <numeric> | 8 | #include <numeric> |
9 | #include <openssl/md5.h> | 9 | #include <openssl/md5.h> |
10 | #include "eyetil.h" | 10 | #include "eyetil.h" |
11 | 11 | ||
12 | #include "config.h" | ||
13 | #ifdef HAVE_LIBUUID | ||
14 | # include <uuid/uuid.h> | ||
15 | #endif | ||
16 | |||
12 | binary_t& binary_t::from_hex(const std::string& h) { | 17 | binary_t& binary_t::from_hex(const std::string& h) { |
13 | std::string::size_type hs = h.length(); | 18 | std::string::size_type hs = h.length(); |
14 | if(hs&1) | 19 | if(hs&1) |
15 | throw std::runtime_error("odd number of characters in hexadecimal number"); | 20 | throw std::runtime_error("odd number of characters in hexadecimal number"); |
16 | int rvs = hs>>1; | 21 | int rvs = hs>>1; |
17 | resize(rvs); | 22 | resize(rvs); |
18 | const unsigned char *hp = (const unsigned char*)h.data(); | 23 | const unsigned char *hp = (const unsigned char*)h.data(); |
19 | iterator oi=begin(); | 24 | iterator oi=begin(); |
20 | char t[3] = { 0,0,0 }; | 25 | char t[3] = { 0,0,0 }; |
21 | for(int i=0;i<rvs;++i) { | 26 | for(int i=0;i<rvs;++i) { |
22 | t[0]=*(hp++); t[1]=*(hp++); | 27 | t[0]=*(hp++); t[1]=*(hp++); |
23 | *(oi++) = strtol(t,0,16); | 28 | *(oi++) = strtol(t,0,16); |
24 | } | 29 | } |
25 | return *this; | 30 | return *this; |
26 | } | 31 | } |
27 | 32 | ||
28 | binary_t& binary_t::from_data(const void *d,size_t s) { | 33 | binary_t& binary_t::from_data(const void *d,size_t s) { |
29 | resize(s); | 34 | resize(s); |
30 | std::copy((const unsigned char*)d,(const unsigned char *)d+s, | 35 | std::copy((const unsigned char*)d,(const unsigned char *)d+s, |
31 | begin() ); | 36 | begin() ); |
32 | return *this; | 37 | return *this; |
33 | } | 38 | } |
34 | 39 | ||
40 | binary_t& binary_t::make_nonce() { | ||
41 | #ifdef HAVE_LIBUUID | ||
42 | uuid_t uuid; | ||
43 | uuid_generate(uuid); | ||
44 | from_data((unsigned char*)uuid,sizeof(uuid)); | ||
45 | #else | ||
46 | resize(16); | ||
47 | std::generate_n(begin(),16,rand); | ||
48 | #endif /* HAVE_LIBUUID */ | ||
49 | return *this; | ||
50 | } | ||
51 | |||
35 | std::string binary_t::hex() const { | 52 | std::string binary_t::hex() const { |
36 | std::string rv; | 53 | std::string rv; |
37 | rv.reserve((size()<<1)+1); | 54 | rv.reserve((size()<<1)+1); |
38 | char t[3] = {0,0,0}; | 55 | char t[3] = {0,0,0}; |
39 | for(const_iterator i=begin(),ie=end();i!=ie;++i) { | 56 | for(const_iterator i=begin(),ie=end();i!=ie;++i) { |
40 | int rc = snprintf(t,sizeof(t),"%02x",*i); | 57 | int rc = snprintf(t,sizeof(t),"%02x",*i); |
41 | assert(rc<sizeof(t)); | 58 | assert(rc<sizeof(t)); |
42 | rv += t; | 59 | rv += t; |
43 | } | 60 | } |
44 | return rv; | 61 | return rv; |
45 | } | 62 | } |
46 | 63 | ||
47 | binary_t binary_t::md5() const { | 64 | binary_t binary_t::md5() const { |
48 | binary_t rv(MD5_DIGEST_LENGTH); | 65 | binary_t rv(MD5_DIGEST_LENGTH); |
49 | if(!MD5( | 66 | if(!MD5( |
50 | (const unsigned char*)&(front()),size(), | 67 | (const unsigned char*)&(front()),size(), |
51 | (unsigned char*)&(rv.front()) )) | 68 | (unsigned char*)&(rv.front()) )) |
52 | throw std::runtime_error("failed to md5()"); | 69 | throw std::runtime_error("failed to md5()"); |
53 | return rv; | 70 | return rv; |
54 | } | 71 | } |
55 | 72 | ||
56 | static void make_path_for_template(const std::string& p,mode_t m) { | 73 | static void make_path_for_template(const std::string& p,mode_t m) { |
57 | struct stat st; | 74 | struct stat st; |
58 | std::string pp; | 75 | std::string pp; |
59 | for(std::string::size_type sl=p.find('/',1); | 76 | for(std::string::size_type sl=p.find('/',1); |
60 | sl!=std::string::npos; | 77 | sl!=std::string::npos; |
61 | sl=p.find('/',sl+1)) { | 78 | sl=p.find('/',sl+1)) { |
62 | if(stat( (pp=p.substr(0,sl)).c_str() ,&st) | 79 | if(stat( (pp=p.substr(0,sl)).c_str() ,&st) |
63 | || !S_ISDIR(st.st_mode)) { | 80 | || !S_ISDIR(st.st_mode)) { |
64 | if(mkdir(pp.c_str(),m)) | 81 | if(mkdir(pp.c_str(),m)) |
65 | throw std::runtime_error("failed to mkdir()"); | 82 | throw std::runtime_error("failed to mkdir()"); |
66 | } | 83 | } |
67 | } | 84 | } |
68 | } | 85 | } |
69 | 86 | ||
70 | tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { | 87 | tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { |
71 | make_path_for_template(dt,0777); | 88 | make_path_for_template(dt,0777); |
72 | if(!mkdtemp((char*)dir.data())) | 89 | if(!mkdtemp((char*)dir.data())) |
73 | throw std::runtime_error("failed to mkdtmp()"); | 90 | throw std::runtime_error("failed to mkdtmp()"); |
74 | } | 91 | } |
75 | tmpdir_t::~tmpdir_t() { | 92 | tmpdir_t::~tmpdir_t() { |
76 | assert(!dir.empty()); | 93 | assert(!dir.empty()); |
77 | if(rmdir(dir.c_str())) { | 94 | if(rmdir(dir.c_str())) { |
78 | syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str()); | 95 | syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str()); |
79 | } | 96 | } |
80 | } | 97 | } |
81 | 98 | ||
82 | std::string tmpdir_t::get_file(const std::string& f) { | 99 | std::string tmpdir_t::get_file(const std::string& f) { |
83 | std::string::size_type ls = f.rfind('/'); | 100 | std::string::size_type ls = f.rfind('/'); |
84 | return dir+'/'+( | 101 | return dir+'/'+( |
85 | (ls==std::string::npos) | 102 | (ls==std::string::npos) |
86 | ? f | 103 | ? f |
87 | : f.substr(ls+1) | 104 | : f.substr(ls+1) |
88 | ); | 105 | ); |
89 | } | 106 | } |
90 | 107 | ||
91 | tarchive_t::tarchive_t(void *p,size_t s) : a(archive_read_new()), e(0) { | 108 | tarchive_t::tarchive_t(void *p,size_t s) : a(archive_read_new()), e(0) { |
92 | if(!a) throw std::runtime_error("failed to archive_read_new()"); | 109 | if(!a) throw std::runtime_error("failed to archive_read_new()"); |
93 | if(archive_read_support_format_tar(a)) { | 110 | if(archive_read_support_format_tar(a)) { |
94 | archive_read_finish(a); | 111 | archive_read_finish(a); |
95 | throw std::runtime_error("failed to archive_read_support_format_tar()"); | 112 | throw std::runtime_error("failed to archive_read_support_format_tar()"); |
96 | } | 113 | } |
97 | if(archive_read_open_memory(a,p,s)) { | 114 | if(archive_read_open_memory(a,p,s)) { |
98 | archive_read_finish(a); | 115 | archive_read_finish(a); |
99 | throw std::runtime_error("failed to archive_read_open_memory()"); | 116 | throw std::runtime_error("failed to archive_read_open_memory()"); |
100 | } | 117 | } |
101 | } | 118 | } |
102 | tarchive_t::~tarchive_t() { | 119 | tarchive_t::~tarchive_t() { |
103 | assert(a); | 120 | assert(a); |
104 | archive_read_finish(a); | 121 | archive_read_finish(a); |
105 | } | 122 | } |
106 | 123 | ||
107 | bool tarchive_t::read_next_header() { | 124 | bool tarchive_t::read_next_header() { |
108 | assert(a); | 125 | assert(a); |
109 | return archive_read_next_header(a,&e)==ARCHIVE_OK; | 126 | return archive_read_next_header(a,&e)==ARCHIVE_OK; |
110 | } | 127 | } |
111 | 128 | ||
112 | std::string tarchive_t::entry_pathname() { | 129 | std::string tarchive_t::entry_pathname() { |
113 | assert(a); assert(e); | 130 | assert(a); assert(e); |
114 | return archive_entry_pathname(e); | 131 | return archive_entry_pathname(e); |
115 | } | 132 | } |
116 | 133 | ||
117 | bool tarchive_t::read_data_into_fd(int fd) { | 134 | bool tarchive_t::read_data_into_fd(int fd) { |
118 | assert(a); | 135 | assert(a); |
119 | return archive_read_data_into_fd(a,fd)==ARCHIVE_OK; | 136 | return archive_read_data_into_fd(a,fd)==ARCHIVE_OK; |
120 | } | 137 | } |
121 | 138 | ||
122 | #pragma pack(1) | 139 | #pragma pack(1) |
123 | struct block512_t { | 140 | struct block512_t { |
124 | enum { words = 512 / sizeof(uint16_t) }; | 141 | enum { words = 512 / sizeof(uint16_t) }; |
125 | uint16_t data[words]; | 142 | uint16_t data[words]; |
126 | 143 | ||
127 | static uint16_t tcpcksum(block512_t& data) { | 144 | static uint16_t tcpcksum(block512_t& data) { |
128 | uint32_t sum = std::accumulate(data.data,data.data+words,0); | 145 | uint32_t sum = std::accumulate(data.data,data.data+words,0); |
129 | while(uint32_t hw = sum>>16) sum = (sum&0xffff)+hw; | 146 | while(uint32_t hw = sum>>16) sum = (sum&0xffff)+hw; |
130 | return ~sum; | 147 | return ~sum; |
131 | } | 148 | } |
132 | 149 | ||
133 | }; | 150 | }; |
134 | #pragma pack() | 151 | #pragma pack() |
135 | 152 | ||
136 | binary_t integrity_digest(const void *ptr,size_t size,const std::string& ukey) { | 153 | binary_t integrity_digest(const void *ptr,size_t size,const std::string& ukey) { |
137 | binary_t key; key.from_hex(ukey); | 154 | binary_t key; key.from_hex(ukey); |
138 | std::vector<uint16_t> blksums; blksums.reserve(size/sizeof(block512_t)); | 155 | std::vector<uint16_t> blksums; blksums.reserve(size/sizeof(block512_t)); |
139 | block512_t *db = (block512_t*)ptr, | 156 | block512_t *db = (block512_t*)ptr, |
140 | *de = db + size/sizeof(block512_t); | 157 | *de = db + size/sizeof(block512_t); |
141 | std::transform( db, de, std::back_inserter(blksums), block512_t::tcpcksum ); | 158 | std::transform( db, de, std::back_inserter(blksums), block512_t::tcpcksum ); |
142 | binary_t subject; | 159 | binary_t subject; |
143 | subject.from_data((void*)&(blksums.front()),blksums.size()*sizeof(uint16_t)); | 160 | subject.from_data((void*)&(blksums.front()),blksums.size()*sizeof(uint16_t)); |
144 | std::copy( key.begin(), key.end(), std::back_inserter(subject) ); | 161 | std::copy( key.begin(), key.end(), std::back_inserter(subject) ); |
145 | return subject.md5(); | 162 | return subject.md5(); |
146 | } | 163 | } |
diff --git a/src/eyetil.h b/src/eyetil.h index 378f703..d946e71 100644 --- a/src/eyetil.h +++ b/src/eyetil.h | |||
@@ -1,51 +1,52 @@ | |||
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 | 8 | ||
9 | class binary_t : public std::vector<unsigned char> { | 9 | class binary_t : public std::vector<unsigned char> { |
10 | public: | 10 | public: |
11 | binary_t() { } | 11 | binary_t() { } |
12 | binary_t(size_type n) : std::vector<unsigned char>(n) { } | 12 | binary_t(size_type n) : std::vector<unsigned char>(n) { } |
13 | binary_t(const std::string& h) { from_hex(h); } | 13 | binary_t(const std::string& h) { from_hex(h); } |
14 | binary_t(const void *d,size_t s) { from_data(d,s); } | 14 | binary_t(const void *d,size_t s) { from_data(d,s); } |
15 | 15 | ||
16 | binary_t& from_hex(const std::string& h); | 16 | binary_t& from_hex(const std::string& h); |
17 | binary_t& from_data(const void *d,size_t s); | 17 | binary_t& from_data(const void *d,size_t s); |
18 | binary_t& make_nonce(); | ||
18 | 19 | ||
19 | std::string hex() const; | 20 | std::string hex() const; |
20 | binary_t md5() const; | 21 | binary_t md5() const; |
21 | }; | 22 | }; |
22 | 23 | ||
23 | class tmpdir_t { | 24 | class tmpdir_t { |
24 | public: | 25 | public: |
25 | std::string dir; | 26 | std::string dir; |
26 | 27 | ||
27 | tmpdir_t(const std::string& dt); | 28 | tmpdir_t(const std::string& dt); |
28 | ~tmpdir_t(); | 29 | ~tmpdir_t(); |
29 | 30 | ||
30 | std::string get_file(const std::string& f); | 31 | std::string get_file(const std::string& f); |
31 | }; | 32 | }; |
32 | 33 | ||
33 | class tarchive_t { | 34 | class tarchive_t { |
34 | public: | 35 | public: |
35 | struct archive *a; | 36 | struct archive *a; |
36 | struct archive_entry *e; | 37 | struct archive_entry *e; |
37 | 38 | ||
38 | tarchive_t(void *p,size_t s); | 39 | tarchive_t(void *p,size_t s); |
39 | ~tarchive_t(); | 40 | ~tarchive_t(); |
40 | 41 | ||
41 | bool read_next_header(); | 42 | bool read_next_header(); |
42 | 43 | ||
43 | std::string entry_pathname(); | 44 | std::string entry_pathname(); |
44 | 45 | ||
45 | bool read_data_into_fd(int fd); | 46 | bool read_data_into_fd(int fd); |
46 | }; | 47 | }; |
47 | 48 | ||
48 | binary_t integrity_digest(const void *ptr,size_t size, | 49 | binary_t integrity_digest(const void *ptr,size_t size, |
49 | const std::string& ukey); | 50 | const std::string& ukey); |
50 | 51 | ||
51 | #endif /* __EYETIL_H */ | 52 | #endif /* __EYETIL_H */ |