-rw-r--r-- | src/eyefiservice.cc | 21 |
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,28 +1,31 @@ | |||
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; |
@@ -33,63 +36,71 @@ static bool detached_child() { | |||
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()) ); |