author | Michael Krelin <hacker@klever.net> | 2013-02-12 20:37:10 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2013-02-12 20:37:10 (UTC) |
commit | 13fb4abba3fd3cac0d5cb25d3eccddc298220d41 (patch) (unidiff) | |
tree | 8a086ffef06f153e739674a5c8beab1db9388238 /src/eyefiworker.cc | |
parent | b80844f51353339cfbb8b35a5585911cdb4301e2 (diff) | |
download | iii-13fb4abba3fd3cac0d5cb25d3eccddc298220d41.zip iii-13fb4abba3fd3cac0d5cb25d3eccddc298220d41.tar.gz iii-13fb4abba3fd3cac0d5cb25d3eccddc298220d41.tar.bz2 |
introduce throwable_exit for nicer stack unwinding
yes, I enjoy abusing features
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | src/eyefiworker.cc | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/src/eyefiworker.cc b/src/eyefiworker.cc index ac75fc1..1979b46 100644 --- a/src/eyefiworker.cc +++ b/src/eyefiworker.cc | |||
@@ -1,155 +1,157 @@ | |||
1 | #include <signal.h> | 1 | #include <signal.h> |
2 | #ifndef NDEBUG | 2 | #ifndef NDEBUG |
3 | # include <sys/resource.h> | 3 | # include <sys/resource.h> |
4 | #endif | 4 | #endif |
5 | #include <syslog.h> | 5 | #include <syslog.h> |
6 | #include <cassert> | 6 | #include <cassert> |
7 | #include <iostream> | 7 | #include <iostream> |
8 | #include <fstream> | 8 | #include <fstream> |
9 | #include <stdexcept> | 9 | #include <stdexcept> |
10 | #include <iterator> | 10 | #include <iterator> |
11 | #include <algorithm> | 11 | #include <algorithm> |
12 | #include <sys/wait.h> | 12 | #include <sys/wait.h> |
13 | #include <autosprintf.h> | 13 | #include <autosprintf.h> |
14 | #include "eyekinfig.h" | 14 | #include "eyekinfig.h" |
15 | #include "eyetil.h" | 15 | #include "eyetil.h" |
16 | #include "eyefiworker.h" | 16 | #include "eyefiworker.h" |
17 | #ifdef HAVE_SQLITE | 17 | #ifdef HAVE_SQLITE |
18 | # include "iiidb.h" | 18 | # include "iiidb.h" |
19 | #endif | 19 | #endif |
20 | 20 | ||
21 | eyefiworker::eyefiworker() | 21 | eyefiworker::eyefiworker() |
22 | : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) { | 22 | : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) { |
23 | bind_flags = SO_REUSEADDR; max_keep_alive = 0; | 23 | bind_flags = SO_REUSEADDR; max_keep_alive = 0; |
24 | socket_flags = | 24 | socket_flags = |
25 | #if defined(MSG_NOSIGNAL) | 25 | #if defined(MSG_NOSIGNAL) |
26 | MSG_NOSIGNAL | 26 | MSG_NOSIGNAL |
27 | #elif defined(SO_NOSIGPIPE) | 27 | #elif defined(SO_NOSIGPIPE) |
28 | SO_NOSIGPIPE | 28 | SO_NOSIGPIPE |
29 | #else | 29 | #else |
30 | #error Something is wrong with sigpipe prevention on the platform | 30 | #error Something is wrong with sigpipe prevention on the platform |
31 | #endif | 31 | #endif |
32 | ; | 32 | ; |
33 | } | 33 | } |
34 | eyefiworker::~eyefiworker() { | ||
35 | } | ||
34 | 36 | ||
35 | int eyefiworker::run(int bindport) { | 37 | int eyefiworker::run(int bindport) { |
36 | #ifdef HAVE_SQLITE | 38 | #ifdef HAVE_SQLITE |
37 | sqlite3_initialize(); | 39 | sqlite3_initialize(); |
38 | #endif | 40 | #endif |
39 | if(!soap_valid_socket(bind(0,bindport,64))) | 41 | if(!soap_valid_socket(bind(0,bindport,64))) |
40 | throw std::runtime_error("failed to bind()"); | 42 | throw std::runtime_error("failed to bind()"); |
41 | signal(SIGCHLD,SIG_IGN); | 43 | signal(SIGCHLD,SIG_IGN); |
42 | while(true) { | 44 | while(true) { |
43 | if(!soap_valid_socket(accept())) | 45 | if(!soap_valid_socket(accept())) |
44 | throw std::runtime_error("failed to accept()"); | 46 | throw std::runtime_error("failed to accept()"); |
45 | pid_t p = fork(); | 47 | pid_t p = fork(); |
46 | if(p<0) throw std::runtime_error("failed to fork()"); | 48 | if(p<0) throw std::runtime_error("failed to fork()"); |
47 | if(!p) { | 49 | if(!p) { |
48 | recv_timeout = 600; send_timeout = 120; | 50 | recv_timeout = 600; send_timeout = 120; |
49 | (void)serve(); | 51 | (void)serve(); |
50 | soap_destroy(this); soap_end(this); soap_done(this); | 52 | soap_destroy(this); soap_end(this); soap_done(this); |
51 | #ifndef NDEBUG | 53 | #ifndef NDEBUG |
52 | struct rusage ru; | 54 | struct rusage ru; |
53 | if(getrusage(RUSAGE_SELF,&ru)) { | 55 | if(getrusage(RUSAGE_SELF,&ru)) { |
54 | syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno); | 56 | syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno); |
55 | }else{ | 57 | }else{ |
56 | syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss); | 58 | syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss); |
57 | } | 59 | } |
58 | #endif /* NDEBUG */ | 60 | #endif /* NDEBUG */ |
59 | _exit(0); | 61 | throw throwable_exit(0); |
60 | } | 62 | } |
61 | close(socket); socket = SOAP_INVALID_SOCKET; | 63 | close(socket); socket = SOAP_INVALID_SOCKET; |
62 | } | 64 | } |
63 | } | 65 | } |
64 | 66 | ||
65 | static binary_t session_nonce; | 67 | static binary_t session_nonce; |
66 | #ifdef HAVE_SQLITE | 68 | #ifdef HAVE_SQLITE |
67 | static struct { | 69 | static struct { |
68 | std::string filesignature; | 70 | std::string filesignature; |
69 | long filesize; | 71 | long filesize; |
70 | std::string filename; | 72 | std::string filename; |
71 | inline void reset() { filesignature.erase(); filename.erase(); filesize=0; } | 73 | inline void reset() { filesignature.erase(); filename.erase(); filesize=0; } |
72 | inline void set(const std::string n,const std::string sig,long siz) { | 74 | inline void set(const std::string n,const std::string sig,long siz) { |
73 | filename = n; filesignature = sig; filesize = siz; | 75 | filename = n; filesignature = sig; filesize = siz; |
74 | } | 76 | } |
75 | inline bool is(const std::string n,const std::string sig,long siz) { | 77 | inline bool is(const std::string n,const std::string sig,long siz) { |
76 | return filesize==siz && filename==n && filesignature==sig; | 78 | return filesize==siz && filename==n && filesignature==sig; |
77 | } | 79 | } |
78 | } already; | 80 | } already; |
79 | #endif /* HAVE_SQLITE */ | 81 | #endif /* HAVE_SQLITE */ |
80 | 82 | ||
81 | static bool detached_child() { | 83 | static bool detached_child() { |
82 | pid_t p = fork(); | 84 | pid_t p = fork(); |
83 | if(p<0) { | 85 | if(p<0) { |
84 | syslog(LOG_ERR,"Failed to fork away for hook execution"); | 86 | syslog(LOG_ERR,"Failed to fork away for hook execution"); |
85 | _exit(-1); | 87 | _exit(-1); |
86 | } | 88 | } |
87 | if(!p) { | 89 | if(!p) { |
88 | setsid(); | 90 | setsid(); |
89 | for(int i=getdtablesize();i>=0;--i) close(i); | 91 | for(int i=getdtablesize();i>=0;--i) close(i); |
90 | int i=open("/dev/null",O_RDWR); assert(i==0); | 92 | int i=open("/dev/null",O_RDWR); assert(i==0); |
91 | i = dup(i); assert(i==1); | 93 | i = dup(i); assert(i==1); |
92 | i = dup(i); assert(i==2); | 94 | i = dup(i); assert(i==2); |
93 | return true; | 95 | return true; |
94 | } | 96 | } |
95 | return false; | 97 | return false; |
96 | } | 98 | } |
97 | 99 | ||
98 | static int E(eyefiworker* efs,const char *c,const std::exception& e) { | 100 | static int E(eyefiworker* efs,const char *c,const std::exception& e) { |
99 | efs->keep_alive=0; | 101 | efs->keep_alive=0; |
100 | syslog(LOG_ERR,"error while processing %s: %s",c,e.what()); | 102 | syslog(LOG_ERR,"error while processing %s: %s",c,e.what()); |
101 | return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0); | 103 | return soap_sender_fault(efs,gnu::autosprintf("error processing %s",c),0); |
102 | } | 104 | } |
103 | 105 | ||
104 | int eyefiworker::StartSession( | 106 | int eyefiworker::StartSession( |
105 | std::string macaddress,std::string cnonce, | 107 | std::string macaddress,std::string cnonce, |
106 | int transfermode,long transfermodetimestamp, | 108 | int transfermode,long transfermodetimestamp, |
107 | struct rns__StartSessionResponse &r ) try { | 109 | struct rns__StartSessionResponse &r ) try { |
108 | syslog(LOG_INFO, | 110 | syslog(LOG_INFO, |
109 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", | 111 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", |
110 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); | 112 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); |
111 | eyekinfig_t eyekinfig(macaddress); | 113 | eyekinfig_t eyekinfig(macaddress); |
112 | r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); | 114 | r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); |
113 | 115 | ||
114 | r.snonce = session_nonce.make_nonce().hex(); | 116 | r.snonce = session_nonce.make_nonce().hex(); |
115 | r.transfermode=transfermode; | 117 | r.transfermode=transfermode; |
116 | r.transfermodetimestamp=transfermodetimestamp; | 118 | r.transfermodetimestamp=transfermodetimestamp; |
117 | r.upsyncallowed=false; | 119 | r.upsyncallowed=false; |
118 | 120 | ||
119 | std::string cmd = eyekinfig.get_on_start_session(); | 121 | std::string cmd = eyekinfig.get_on_start_session(); |
120 | if(!cmd.empty()) { | 122 | if(!cmd.empty()) { |
121 | if(detached_child()) { | 123 | if(detached_child()) { |
122 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 124 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
123 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); | 125 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); |
124 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); | 126 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); |
125 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 127 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
126 | execv("/bin/sh",argv); | 128 | execv("/bin/sh",argv); |
127 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 129 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
128 | _exit(-1); | 130 | _exit(-1); |
129 | } | 131 | } |
130 | } | 132 | } |
131 | return SOAP_OK; | 133 | return SOAP_OK; |
132 | }catch(const std::exception& e) { return E(this,"StartSession",e); } | 134 | }catch(const std::exception& e) { return E(this,"StartSession",e); } |
133 | 135 | ||
134 | int eyefiworker::GetPhotoStatus( | 136 | int eyefiworker::GetPhotoStatus( |
135 | std::string credential, std::string macaddress, | 137 | std::string credential, std::string macaddress, |
136 | std::string filename, long filesize, std::string filesignature, | 138 | std::string filename, long filesize, std::string filesignature, |
137 | int flags, | 139 | int flags, |
138 | struct rns__GetPhotoStatusResponse &r ) try { | 140 | struct rns__GetPhotoStatusResponse &r ) try { |
139 | syslog(LOG_INFO, | 141 | syslog(LOG_INFO, |
140 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", | 142 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", |
141 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, | 143 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, |
142 | session_nonce.hex().c_str() ); | 144 | session_nonce.hex().c_str() ); |
143 | 145 | ||
144 | eyekinfig_t eyekinfig(macaddress); | 146 | eyekinfig_t eyekinfig(macaddress); |
145 | std::string computed_credential = binary_t(macaddress+eyekinfig.get_upload_key()+session_nonce.hex()).md5().hex(); | 147 | std::string computed_credential = binary_t(macaddress+eyekinfig.get_upload_key()+session_nonce.hex()).md5().hex(); |
146 | 148 | ||
147 | #ifndef NDEBUG | 149 | #ifndef NDEBUG |
148 | syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); | 150 | syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); |
149 | #endif | 151 | #endif |
150 | 152 | ||
151 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); | 153 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); |
152 | 154 | ||
153 | #ifdef HAVE_SQLITE | 155 | #ifdef HAVE_SQLITE |
154 | iiidb_t D(eyekinfig); | 156 | iiidb_t D(eyekinfig); |
155 | seclude::stmt_t S = D.prepare( | 157 | seclude::stmt_t S = D.prepare( |