summaryrefslogtreecommitdiffabout
path: root/src
Unidiff
Diffstat (limited to 'src') (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyefiservice.cc29
1 files changed, 10 insertions, 19 deletions
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc
index 0bf26c4..4ca2777 100644
--- a/src/eyefiservice.cc
+++ b/src/eyefiservice.cc
@@ -1,133 +1,124 @@
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 "eyekinfig.h" 9#include "eyekinfig.h"
10#include "eyetil.h" 10#include "eyetil.h"
11#include "soapeyefiService.h" 11#include "soapeyefiService.h"
12 12
13static binary_t session_nonce; 13static binary_t session_nonce;
14 14
15static bool detached_child() { 15static bool detached_child() {
16 pid_t p = fork(); 16 pid_t p = fork();
17 if(p<0) throw std::runtime_error("failed to fork()"); 17 if(p<0) {
18 syslog(LOG_ERR,"Failed to fork away for hook execution");
19 _exit(-1);
20 }
18 if(!p) { 21 if(!p) {
19 p = fork(); 22 setsid();
20 if(p<0) { 23 for(int i=getdtablesize();i>=0;--i) close(i);
21 syslog(LOG_ERR,"Failed to re-fork child process"); 24 int i=open("/dev/null",O_RDWR); assert(i==0);
22 _exit(-1); 25 i = dup(i); assert(i==1);
23 } 26 i = dup(i); assert(i==2);
24 if(!p) { 27 return true;
25 setsid();
26 for(int i=getdtablesize();i>=0;--i) close(i);
27 int i=open("/dev/null",O_RDWR); assert(i==0);
28 i = dup(i); assert(i==1);
29 i = dup(i); assert(i==2);
30 return true;
31 }
32 _exit(0);
33 } 28 }
34 int rc;
35 if(waitpid(p,&rc,0)<0) throw std::runtime_error("failed to waitpid()");
36 if(!WIFEXITED(rc)) throw std::runtime_error("error in forked process");
37 if(WEXITSTATUS(rc)) throw std::runtime_error("forked process signalled error");
38 return false; 29 return false;
39} 30}
40 31
41int eyefiService::StartSession( 32int eyefiService::StartSession(
42 std::string macaddress,std::string cnonce, 33 std::string macaddress,std::string cnonce,
43 int transfermode,long transfermodetimestamp, 34 int transfermode,long transfermodetimestamp,
44 struct rns__StartSessionResponse &r ) try { 35 struct rns__StartSessionResponse &r ) try {
45#ifndef NDEBUG 36#ifndef NDEBUG
46 syslog(LOG_DEBUG, 37 syslog(LOG_DEBUG,
47 "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", 38 "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld",
48 macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); 39 macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp );
49#endif 40#endif
50 eyekinfig_t eyekinfig(macaddress); 41 eyekinfig_t eyekinfig(macaddress);
51 r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); 42 r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex();
52 43
53 r.snonce = session_nonce.make_nonce().hex(); 44 r.snonce = session_nonce.make_nonce().hex();
54 r.transfermode=transfermode; 45 r.transfermode=transfermode;
55 r.transfermodetimestamp=transfermodetimestamp; 46 r.transfermodetimestamp=transfermodetimestamp;
56 r.upsyncallowed=false; 47 r.upsyncallowed=false;
57 48
58 std::string cmd = eyekinfig.get_on_start_session(); 49 std::string cmd = eyekinfig.get_on_start_session();
59 if(!cmd.empty()) { 50 if(!cmd.empty()) {
60 if(detached_child()) { 51 if(detached_child()) {
61 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); 52 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) );
62 putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); 53 putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) );
63 putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); 54 putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) );
64 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; 55 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 };
65 execv("/bin/sh",argv); 56 execv("/bin/sh",argv);
66 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); 57 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str());
67 _exit(-1); 58 _exit(-1);
68 } 59 }
69 } 60 }
70 return SOAP_OK; 61 return SOAP_OK;
71}catch(std::runtime_error& e) { 62}catch(std::runtime_error& e) {
72 syslog(LOG_ERR,"error while processing StartSession: %s",e.what()); 63 syslog(LOG_ERR,"error while processing StartSession: %s",e.what());
73} 64}
74 65
75int eyefiService::GetPhotoStatus( 66int eyefiService::GetPhotoStatus(
76 std::string credential, std::string macaddress, 67 std::string credential, std::string macaddress,
77 std::string filename, long filesize, std::string filesignature, 68 std::string filename, long filesize, std::string filesignature,
78 int flags, 69 int flags,
79 struct rns__GetPhotoStatusResponse &r ) { 70 struct rns__GetPhotoStatusResponse &r ) {
80#ifndef NDEBUG 71#ifndef NDEBUG
81 syslog(LOG_DEBUG, 72 syslog(LOG_DEBUG,
82 "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", 73 "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s",
83 macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, 74 macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags,
84 session_nonce.hex().c_str() ); 75 session_nonce.hex().c_str() );
85#endif 76#endif
86 77
87 std::string computed_credential = binary_t(macaddress+eyekinfig_t(macaddress).get_upload_key()+session_nonce.hex()).md5().hex(); 78 std::string computed_credential = binary_t(macaddress+eyekinfig_t(macaddress).get_upload_key()+session_nonce.hex()).md5().hex();
88 79
89#ifndef NDEBUG 80#ifndef NDEBUG
90 syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); 81 syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str());
91#endif 82#endif
92 83
93 if (credential != computed_credential) throw std::runtime_error("card authentication failed"); 84 if (credential != computed_credential) throw std::runtime_error("card authentication failed");
94 85
95 r.fileid = 1; r.offset = 0; 86 r.fileid = 1; r.offset = 0;
96 return SOAP_OK; 87 return SOAP_OK;
97} 88}
98 89
99int eyefiService::MarkLastPhotoInRoll( 90int eyefiService::MarkLastPhotoInRoll(
100 std::string macaddress, int mergedelta, 91 std::string macaddress, int mergedelta,
101 struct rns__MarkLastPhotoInRollResponse &r ) { 92 struct rns__MarkLastPhotoInRollResponse &r ) {
102#ifndef NDEBUG 93#ifndef NDEBUG
103 syslog(LOG_DEBUG, 94 syslog(LOG_DEBUG,
104 "MarkLastPhotoInRoll request from %s with mergedelta=%d", 95 "MarkLastPhotoInRoll request from %s with mergedelta=%d",
105 macaddress.c_str(), mergedelta ); 96 macaddress.c_str(), mergedelta );
106#endif 97#endif
107 std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); 98 std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll();
108 if(!cmd.empty()) { 99 if(!cmd.empty()) {
109 if(detached_child()) { 100 if(detached_child()) {
110 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); 101 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) );
111 putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); 102 putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) );
112 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; 103 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 };
113 execv("/bin/sh",argv); 104 execv("/bin/sh",argv);
114 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); 105 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str());
115 _exit(-1); 106 _exit(-1);
116 } 107 }
117 } 108 }
118 keep_alive = 0; 109 keep_alive = 0;
119 return SOAP_OK; 110 return SOAP_OK;
120} 111}
121 112
122int eyefiService::UploadPhoto( 113int eyefiService::UploadPhoto(
123 int fileid, std::string macaddress, 114 int fileid, std::string macaddress,
124 std::string filename, long filesize, std::string filesignature, 115 std::string filename, long filesize, std::string filesignature,
125 std::string encryption, int flags, 116 std::string encryption, int flags,
126 struct rns__UploadPhotoResponse& r ) { 117 struct rns__UploadPhotoResponse& r ) {
127#ifndef NDEBUG 118#ifndef NDEBUG
128 syslog(LOG_DEBUG, 119 syslog(LOG_DEBUG,
129 "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," 120 "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld,"
130 " filesignature=%s, encryption=%s, flags=%04X", 121 " filesignature=%s, encryption=%s, flags=%04X",
131 macaddress.c_str(), fileid, filename.c_str(), filesize, 122 macaddress.c_str(), fileid, filename.c_str(), filesize,
132 filesignature.c_str(), encryption.c_str(), flags ); 123 filesignature.c_str(), encryption.c_str(), flags );
133#endif 124#endif