summaryrefslogtreecommitdiffabout
path: root/src/eyefiservice.cc
Unidiff
Diffstat (limited to 'src/eyefiservice.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyefiservice.cc21
1 files changed, 16 insertions, 5 deletions
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
14static binary_t session_nonce;
15
13static bool detached_child() { 16static 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
39int eyefiService::StartSession( 42int 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
71int eyefiService::GetPhotoStatus( 73int 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
84int eyefiService::MarkLastPhotoInRoll( 95int 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
106int eyefiService::UploadPhoto( 117int 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}