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,143 +1,154 @@
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") );