-rw-r--r-- | include/opkele/curl.h | 15 | ||||
-rw-r--r-- | lib/Makefile.am | 3 | ||||
-rw-r--r-- | lib/consumer.cc | 105 | ||||
-rw-r--r-- | lib/curl.cc | 42 |
4 files changed, 101 insertions, 64 deletions
diff --git a/include/opkele/curl.h b/include/opkele/curl.h index 8794ece..6a7d084 100644 --- a/include/opkele/curl.h +++ b/include/opkele/curl.h | |||
@@ -1,28 +1,39 @@ | |||
1 | #ifndef __OPKELE_CURL_H | 1 | #ifndef __OPKELE_CURL_H |
2 | #define __OPKELE_CURL_H | 2 | #define __OPKELE_CURL_H |
3 | 3 | ||
4 | #include <cassert> | ||
4 | #include <curl/curl.h> | 5 | #include <curl/curl.h> |
5 | 6 | ||
6 | namespace opkele { | 7 | namespace opkele { |
7 | 8 | ||
8 | namespace util { | 9 | namespace util { |
9 | 10 | ||
10 | class curl_t { | 11 | class curl_t { |
11 | public: | 12 | public: |
12 | CURL *_c; | 13 | CURL *_c; |
13 | 14 | ||
14 | curl_t() : _c(0) { } | 15 | curl_t() : _c(0) { } |
15 | curl_t(CURL *c) : _c(c) { } | 16 | curl_t(CURL *c) : _c(c) { } |
16 | ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } | 17 | virtual ~curl_t() throw(); |
17 | 18 | ||
18 | curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; } | 19 | curl_t& operator=(CURL *c); |
19 | 20 | ||
20 | operator const CURL*(void) const { return _c; } | 21 | operator const CURL*(void) const { return _c; } |
21 | operator CURL*(void) { return _c; } | 22 | operator CURL*(void) { return _c; } |
23 | |||
24 | CURLcode misc_sets(); | ||
25 | |||
26 | template<typename PT> | ||
27 | inline CURLcode easy_setopt(CURLoption o,PT p) { assert(_c); return curl_easy_setopt(_c,o,p); } | ||
28 | CURLcode easy_perform() { assert(_c); return curl_easy_perform(_c); } | ||
29 | template<typename IT> | ||
30 | inline CURLcode easy_getinfo(CURLINFO i,IT p) { assert(_c); return curl_easy_getinfo(_c,i,p); } | ||
31 | |||
32 | static inline CURL *easy_init() { return curl_easy_init(); } | ||
22 | }; | 33 | }; |
23 | 34 | ||
24 | } | 35 | } |
25 | 36 | ||
26 | } | 37 | } |
27 | 38 | ||
28 | #endif /* __OPKELE_CURL_H */ | 39 | #endif /* __OPKELE_CURL_H */ |
diff --git a/lib/Makefile.am b/lib/Makefile.am index b008a52..0fe705a 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am | |||
@@ -17,11 +17,12 @@ libopkele_la_SOURCES = \ | |||
17 | util.cc \ | 17 | util.cc \ |
18 | server.cc \ | 18 | server.cc \ |
19 | secret.cc \ | 19 | secret.cc \ |
20 | data.cc \ | 20 | data.cc \ |
21 | consumer.cc \ | 21 | consumer.cc \ |
22 | exception.cc \ | 22 | exception.cc \ |
23 | extension.cc \ | 23 | extension.cc \ |
24 | sreg.cc \ | 24 | sreg.cc \ |
25 | extension_chain.cc | 25 | extension_chain.cc \ |
26 | curl.cc | ||
26 | libopkele_la_LDFLAGS = \ | 27 | libopkele_la_LDFLAGS = \ |
27 | -version-info 2:0:0 | 28 | -version-info 2:0:0 |
diff --git a/lib/consumer.cc b/lib/consumer.cc index 20f4174..c155157 100644 --- a/lib/consumer.cc +++ b/lib/consumer.cc | |||
@@ -11,16 +11,17 @@ | |||
11 | #include <iostream> | 11 | #include <iostream> |
12 | 12 | ||
13 | #include "config.h" | 13 | #include "config.h" |
14 | 14 | ||
15 | #include <pcre.h> | 15 | #include <pcre.h> |
16 | 16 | ||
17 | namespace opkele { | 17 | namespace opkele { |
18 | using namespace std; | 18 | using namespace std; |
19 | using util::curl_t; | ||
19 | 20 | ||
20 | class pcre_matches_t { | 21 | class pcre_matches_t { |
21 | public: | 22 | public: |
22 | int *_ov; | 23 | int *_ov; |
23 | int _s; | 24 | int _s; |
24 | 25 | ||
25 | pcre_matches_t() : _ov(0), _s(0) { } | 26 | pcre_matches_t() : _ov(0), _s(0) { } |
26 | pcre_matches_t(int s) : _ov(0), _s(s) { | 27 | pcre_matches_t(int s) : _ov(0), _s(s) { |
@@ -56,34 +57,16 @@ namespace opkele { | |||
56 | 57 | ||
57 | int exec(const string& s,pcre_matches_t& m) { | 58 | int exec(const string& s,pcre_matches_t& m) { |
58 | if(!_p) | 59 | if(!_p) |
59 | throw internal_error(OPKELE_CP_ "Trying to execute absent regexp"); | 60 | throw internal_error(OPKELE_CP_ "Trying to execute absent regexp"); |
60 | return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s); | 61 | return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s); |
61 | } | 62 | } |
62 | }; | 63 | }; |
63 | 64 | ||
64 | static CURLcode curl_misc_sets(CURL* c) { | ||
65 | CURLcode r; | ||
66 | (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1)) | ||
67 | || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5)) | ||
68 | || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120)) | ||
69 | || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1)) | ||
70 | || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_SRC_VERSION)) | ||
71 | || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20)) | ||
72 | #ifdefDISABLE_CURL_SSL_VERIFYHOST | ||
73 | || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYHOST,0)) | ||
74 | #endif | ||
75 | #ifdefDISABLE_CURL_SSL_VERIFYPEER | ||
76 | || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYPEER,0)) | ||
77 | #endif | ||
78 | ; | ||
79 | return r; | ||
80 | } | ||
81 | |||
82 | static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) { | 65 | static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) { |
83 | string *str = (string*)stream; | 66 | string *str = (string*)stream; |
84 | size_t bytes = size*nmemb; | 67 | size_t bytes = size*nmemb; |
85 | size_t get = min(16384-str->length(),bytes); | 68 | size_t get = min(16384-str->length(),bytes); |
86 | str->append((const char*)ptr,get); | 69 | str->append((const char*)ptr,get); |
87 | return get; | 70 | return get; |
88 | } | 71 | } |
89 | 72 | ||
@@ -96,33 +79,33 @@ namespace opkele { | |||
96 | if(!DH_generate_key(dh)) | 79 | if(!DH_generate_key(dh)) |
97 | throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); | 80 | throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); |
98 | string request = | 81 | string request = |
99 | "openid.mode=associate" | 82 | "openid.mode=associate" |
100 | "&openid.assoc_type=HMAC-SHA1" | 83 | "&openid.assoc_type=HMAC-SHA1" |
101 | "&openid.session_type=DH-SHA1" | 84 | "&openid.session_type=DH-SHA1" |
102 | "&openid.dh_consumer_public="; | 85 | "&openid.dh_consumer_public="; |
103 | request += util::url_encode(util::bignum_to_base64(dh->pub_key)); | 86 | request += util::url_encode(util::bignum_to_base64(dh->pub_key)); |
104 | util::curl_t curl = curl_easy_init(); | 87 | curl_t curl = curl_t::easy_init(); |
105 | if(!curl) | 88 | if(!curl) |
106 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); | 89 | throw exception_curl(OPKELE_CP_ "failed to initialize curl"); |
107 | string response; | 90 | string response; |
108 | CURLcode r; | 91 | CURLcode r; |
109 | (r=curl_misc_sets(curl)) | 92 | (r=curl.misc_sets()) |
110 | || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) | 93 | || (r=curl.easy_setopt(CURLOPT_URL,server.c_str())) |
111 | || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) | 94 | || (r=curl.easy_setopt(CURLOPT_POST,1)) |
112 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) | 95 | || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data())) |
113 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) | 96 | || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length())) |
114 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) | 97 | || (r=curl.easy_setopt(CURLOPT_WRITEFUNCTION,_curl_tostring)) |
115 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) | 98 | || (r=curl.easy_setopt(CURLOPT_WRITEDATA,&response)) |
116 | ; | 99 | ; |
117 | if(r) | 100 | if(r) |
118 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 101 | throw exception_curl(OPKELE_CP_ "failed to set curly options",r); |
119 | if( (r=curl_easy_perform(curl)) ) | 102 | if( (r=curl.easy_perform()) ) |
120 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 103 | throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); |
121 | params_t p; p.parse_keyvalues(response); | 104 | params_t p; p.parse_keyvalues(response); |
122 | if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1") | 105 | if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1") |
123 | throw bad_input(OPKELE_CP_ "unsupported assoc_type"); | 106 | throw bad_input(OPKELE_CP_ "unsupported assoc_type"); |
124 | string st; | 107 | string st; |
125 | if(p.has_param("session_type")) st = p.get_param("session_type"); | 108 | if(p.has_param("session_type")) st = p.get_param("session_type"); |
126 | if((!st.empty()) && st!="DH-SHA1") | 109 | if((!st.empty()) && st!="DH-SHA1") |
127 | throw bad_input(OPKELE_CP_ "unsupported session_type"); | 110 | throw bad_input(OPKELE_CP_ "unsupported session_type"); |
128 | secret_t secret; | 111 | secret_t secret; |
@@ -256,64 +239,64 @@ namespace opkele { | |||
256 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) { | 239 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) { |
257 | if(i->first!="openid.mode") { | 240 | if(i->first!="openid.mode") { |
258 | request += '&'; | 241 | request += '&'; |
259 | request += i->first; | 242 | request += i->first; |
260 | request += '='; | 243 | request += '='; |
261 | request += util::url_encode(i->second); | 244 | request += util::url_encode(i->second); |
262 | } | 245 | } |
263 | } | 246 | } |
264 | util::curl_t curl = curl_easy_init(); | 247 | curl_t curl = curl_t::easy_init(); |
265 | if(!curl) | 248 | if(!curl) |
266 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); | 249 | throw exception_curl(OPKELE_CP_ "failed to initialize curl"); |
267 | string response; | 250 | string response; |
268 | CURLcode r; | 251 | CURLcode r; |
269 | (r=curl_misc_sets(curl)) | 252 | (r=curl.misc_sets()) |
270 | || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) | 253 | || (r=curl.easy_setopt(CURLOPT_URL,server.c_str())) |
271 | || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) | 254 | || (r=curl.easy_setopt(CURLOPT_POST,1)) |
272 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) | 255 | || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data())) |
273 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) | 256 | || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length())) |
274 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) | 257 | || (r=curl.easy_setopt(CURLOPT_WRITEFUNCTION,_curl_tostring)) |
275 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) | 258 | || (r=curl.easy_setopt(CURLOPT_WRITEDATA,&response)) |
276 | ; | 259 | ; |
277 | if(r) | 260 | if(r) |
278 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 261 | throw exception_curl(OPKELE_CP_ "failed to set curly options",r); |
279 | if( (r=curl_easy_perform(curl)) ) | 262 | if( (r=curl.easy_perform()) ) |
280 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 263 | throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); |
281 | params_t pp; pp.parse_keyvalues(response); | 264 | params_t pp; pp.parse_keyvalues(response); |
282 | if(pp.has_param("invalidate_handle")) | 265 | if(pp.has_param("invalidate_handle")) |
283 | invalidate_assoc(server,pp.get_param("invalidate_handle")); | 266 | invalidate_assoc(server,pp.get_param("invalidate_handle")); |
284 | if(pp.has_param("is_valid")) { | 267 | if(pp.has_param("is_valid")) { |
285 | if(pp.get_param("is_valid")=="true") | 268 | if(pp.get_param("is_valid")=="true") |
286 | return; | 269 | return; |
287 | }else if(pp.has_param("lifetime")) { | 270 | }else if(pp.has_param("lifetime")) { |
288 | if(util::string_to_long(pp.get_param("lifetime"))) | 271 | if(util::string_to_long(pp.get_param("lifetime"))) |
289 | return; | 272 | return; |
290 | } | 273 | } |
291 | throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); | 274 | throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); |
292 | } | 275 | } |
293 | 276 | ||
294 | void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { | 277 | void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { |
295 | server.erase(); | 278 | server.erase(); |
296 | delegate.erase(); | 279 | delegate.erase(); |
297 | util::curl_t curl = curl_easy_init(); | 280 | curl_t curl = curl_t::easy_init(); |
298 | if(!curl) | 281 | if(!curl) |
299 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); | 282 | throw exception_curl(OPKELE_CP_ "failed to initialize curl"); |
300 | string html; | 283 | string html; |
301 | CURLcode r; | 284 | CURLcode r; |
302 | (r=curl_misc_sets(curl)) | 285 | (r=curl.misc_sets()) |
303 | || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) | 286 | || (r=curl.easy_setopt(CURLOPT_URL,url.c_str())) |
304 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) | 287 | || (r=curl.easy_setopt(CURLOPT_WRITEFUNCTION,_curl_tostring)) |
305 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) | 288 | || (r=curl.easy_setopt(CURLOPT_WRITEDATA,&html)) |
306 | ; | 289 | ; |
307 | if(r) | 290 | if(r) |
308 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 291 | throw exception_curl(OPKELE_CP_ "failed to set curly options",r); |
309 | r = curl_easy_perform(curl); | 292 | r = curl.easy_perform(); |
310 | if(r && r!=CURLE_WRITE_ERROR) | 293 | if(r && r!=CURLE_WRITE_ERROR) |
311 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 294 | throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); |
312 | static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>", | 295 | static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>", |
313 | *re_lre = "<\\s*link\\b([^>]+)>", | 296 | *re_lre = "<\\s*link\\b([^>]+)>", |
314 | *re_rre = "\\brel\\s*=\\s*['\"]([^'\"]+)['\"]", | 297 | *re_rre = "\\brel\\s*=\\s*['\"]([^'\"]+)['\"]", |
315 | *re_hre = "\\bhref\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]"; | 298 | *re_hre = "\\bhref\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]"; |
316 | pcre_matches_t m1(3), m2(3); | 299 | pcre_matches_t m1(3), m2(3); |
317 | pcre_t bre(re_bre,PCRE_CASELESS); | 300 | pcre_t bre(re_bre,PCRE_CASELESS); |
318 | if(bre.exec(html,m1)>0) | 301 | if(bre.exec(html,m1)>0) |
319 | html.erase(m1.begin(0)); | 302 | html.erase(m1.begin(0)); |
@@ -395,31 +378,31 @@ namespace opkele { | |||
395 | if(sl==string::npos) | 378 | if(sl==string::npos) |
396 | rv += '/'; | 379 | rv += '/'; |
397 | } | 380 | } |
398 | return rv; | 381 | return rv; |
399 | } | 382 | } |
400 | 383 | ||
401 | string consumer_t::canonicalize(const string& url) { | 384 | string consumer_t::canonicalize(const string& url) { |
402 | string rv = normalize(url); | 385 | string rv = normalize(url); |
403 | util::curl_t curl = curl_easy_init(); | 386 | curl_t curl = curl_t::easy_init(); |
404 | if(!curl) | 387 | if(!curl) |
405 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); | 388 | throw exception_curl(OPKELE_CP_ "failed to initialize curl()"); |
406 | string html; | 389 | string html; |
407 | CURLcode r; | 390 | CURLcode r; |
408 | (r=curl_misc_sets(curl)) | 391 | (r=curl.misc_sets()) |
409 | || (r=curl_easy_setopt(curl,CURLOPT_URL,rv.c_str())) | 392 | || (r=curl.easy_setopt(CURLOPT_URL,rv.c_str())) |
410 | || (r=curl_easy_setopt(curl,CURLOPT_NOBODY,1)) | 393 | || (r=curl.easy_setopt(CURLOPT_NOBODY,1)) |
411 | ; | 394 | ; |
412 | if(r) | 395 | if(r) |
413 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 396 | throw exception_curl(OPKELE_CP_ "failed to set curly options",r); |
414 | r = curl_easy_perform(curl); | 397 | r = curl.easy_perform(); |
415 | if(r) | 398 | if(r) |
416 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 399 | throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); |
417 | const char *eu = 0; | 400 | const char *eu = 0; |
418 | r = curl_easy_getinfo(curl,CURLINFO_EFFECTIVE_URL,&eu); | 401 | r = curl.easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu); |
419 | if(r) | 402 | if(r) |
420 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_getinfo(..CURLINFO_EFFECTIVE_URL..)",r); | 403 | throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r); |
421 | rv = eu; | 404 | rv = eu; |
422 | return normalize(rv); | 405 | return normalize(rv); |
423 | } | 406 | } |
424 | 407 | ||
425 | } | 408 | } |
diff --git a/lib/curl.cc b/lib/curl.cc new file mode 100644 index 0000000..418aa79 --- a/dev/null +++ b/lib/curl.cc | |||
@@ -0,0 +1,42 @@ | |||
1 | #include <opkele/curl.h> | ||
2 | |||
3 | #include "config.h" | ||
4 | |||
5 | namespace opkele { | ||
6 | |||
7 | namespace util { | ||
8 | |||
9 | curl_t::~curl_t() throw() { | ||
10 | if(_c) | ||
11 | curl_easy_cleanup(_c); | ||
12 | } | ||
13 | |||
14 | curl_t& curl_t::operator=(CURL *c) { | ||
15 | if(_c) | ||
16 | curl_easy_cleanup(_c); | ||
17 | _c = c; | ||
18 | return *this; | ||
19 | } | ||
20 | |||
21 | CURLcode curl_t::misc_sets() { | ||
22 | assert(_c); | ||
23 | CURLcode r; | ||
24 | (r=easy_setopt(CURLOPT_FOLLOWLOCATION,1)) | ||
25 | || (r=easy_setopt(CURLOPT_MAXREDIRS,5)) | ||
26 | || (r=easy_setopt(CURLOPT_DNS_CACHE_TIMEOUT,120)) | ||
27 | || (r=easy_setopt(CURLOPT_DNS_USE_GLOBAL_CACHE,1)) | ||
28 | || (r=easy_setopt(CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_SRC_VERSION)) | ||
29 | || (r=easy_setopt(CURLOPT_TIMEOUT,20)) | ||
30 | #ifdefDISABLE_CURL_SSL_VERIFYHOST | ||
31 | || (r=easy_setopt(CURLOPT_SSL_VERIFYHOST,0)) | ||
32 | #endif | ||
33 | #ifdefDISABLE_CURL_SSL_VERIFYPEER | ||
34 | || (r=easy_setopt(CURLOPT_SSL_VERIFYPEER,0)) | ||
35 | #endif | ||
36 | ; | ||
37 | return r; | ||
38 | } | ||
39 | |||
40 | } | ||
41 | |||
42 | } | ||