-rw-r--r-- | include/Makefile.am | 3 | ||||
-rw-r--r-- | include/opkele/curl.h | 28 | ||||
-rw-r--r-- | lib/consumer.cc | 25 |
3 files changed, 35 insertions, 21 deletions
diff --git a/include/Makefile.am b/include/Makefile.am index f3551f5..b31786d 100644 --- a/include/Makefile.am +++ b/include/Makefile.am | |||
@@ -1,16 +1,17 @@ | |||
1 | nobase_include_HEADERS = \ | 1 | nobase_include_HEADERS = \ |
2 | opkele/acconfig.h \ | 2 | opkele/acconfig.h \ |
3 | opkele/opkele-config.h \ | 3 | opkele/opkele-config.h \ |
4 | opkele/types.h \ | 4 | opkele/types.h \ |
5 | opkele/association.h \ | 5 | opkele/association.h \ |
6 | opkele/exception.h \ | 6 | opkele/exception.h \ |
7 | opkele/server.h \ | 7 | opkele/server.h \ |
8 | opkele/consumer.h \ | 8 | opkele/consumer.h \ |
9 | opkele/extension.h \ | 9 | opkele/extension.h \ |
10 | opkele/sreg.h \ | 10 | opkele/sreg.h \ |
11 | opkele/extension_chain.h \ | 11 | opkele/extension_chain.h \ |
12 | opkele/xconsumer.h \ | 12 | opkele/xconsumer.h \ |
13 | opkele/xserver.h | 13 | opkele/xserver.h |
14 | EXTRA_DIST = \ | 14 | EXTRA_DIST = \ |
15 | opkele/data.h \ | 15 | opkele/data.h \ |
16 | opkele/util.h | 16 | opkele/util.h \ |
17 | opkele/curl.h | ||
diff --git a/include/opkele/curl.h b/include/opkele/curl.h new file mode 100644 index 0000000..8794ece --- a/dev/null +++ b/include/opkele/curl.h | |||
@@ -0,0 +1,28 @@ | |||
1 | #ifndef __OPKELE_CURL_H | ||
2 | #define __OPKELE_CURL_H | ||
3 | |||
4 | #include <curl/curl.h> | ||
5 | |||
6 | namespace opkele { | ||
7 | |||
8 | namespace util { | ||
9 | |||
10 | class curl_t { | ||
11 | public: | ||
12 | CURL *_c; | ||
13 | |||
14 | curl_t() : _c(0) { } | ||
15 | curl_t(CURL *c) : _c(c) { } | ||
16 | ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } | ||
17 | |||
18 | curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; } | ||
19 | |||
20 | operator const CURL*(void) const { return _c; } | ||
21 | operator CURL*(void) { return _c; } | ||
22 | }; | ||
23 | |||
24 | } | ||
25 | |||
26 | } | ||
27 | |||
28 | #endif /* __OPKELE_CURL_H */ | ||
diff --git a/lib/consumer.cc b/lib/consumer.cc index 7881f5f..20f4174 100644 --- a/lib/consumer.cc +++ b/lib/consumer.cc | |||
@@ -1,27 +1,26 @@ | |||
1 | #include <algorithm> | 1 | #include <algorithm> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <cstring> | 3 | #include <cstring> |
4 | #include <opkele/util.h> | 4 | #include <opkele/util.h> |
5 | #include <opkele/curl.h> | ||
5 | #include <opkele/exception.h> | 6 | #include <opkele/exception.h> |
6 | #include <opkele/data.h> | 7 | #include <opkele/data.h> |
7 | #include <opkele/consumer.h> | 8 | #include <opkele/consumer.h> |
8 | #include <openssl/sha.h> | 9 | #include <openssl/sha.h> |
9 | #include <openssl/hmac.h> | 10 | #include <openssl/hmac.h> |
10 | #include <curl/curl.h> | ||
11 | |||
12 | #include <iostream> | 11 | #include <iostream> |
13 | 12 | ||
14 | #include "config.h" | 13 | #include "config.h" |
15 | 14 | ||
16 | #include <pcre.h> | 15 | #include <pcre.h> |
17 | 16 | ||
18 | namespace opkele { | 17 | namespace opkele { |
19 | using namespace std; | 18 | using namespace std; |
20 | 19 | ||
21 | class pcre_matches_t { | 20 | class pcre_matches_t { |
22 | public: | 21 | public: |
23 | int *_ov; | 22 | int *_ov; |
24 | int _s; | 23 | int _s; |
25 | 24 | ||
26 | pcre_matches_t() : _ov(0), _s(0) { } | 25 | pcre_matches_t() : _ov(0), _s(0) { } |
27 | pcre_matches_t(int s) : _ov(0), _s(s) { | 26 | pcre_matches_t(int s) : _ov(0), _s(s) { |
@@ -49,46 +48,32 @@ namespace opkele { | |||
49 | throw internal_error(OPKELE_CP_ string("Failed to compile regexp: ")+errptr); | 48 | throw internal_error(OPKELE_CP_ string("Failed to compile regexp: ")+errptr); |
50 | } | 49 | } |
51 | ~pcre_t() throw() { if(_p) (*pcre_free)(_p); } | 50 | ~pcre_t() throw() { if(_p) (*pcre_free)(_p); } |
52 | 51 | ||
53 | pcre_t& operator=(pcre *p) { if(_p) (*pcre_free)(_p); _p=p; return *this; } | 52 | pcre_t& operator=(pcre *p) { if(_p) (*pcre_free)(_p); _p=p; return *this; } |
54 | 53 | ||
55 | operator const pcre*(void) const { return _p; } | 54 | operator const pcre*(void) const { return _p; } |
56 | operator pcre*(void) { return _p; } | 55 | operator pcre*(void) { return _p; } |
57 | 56 | ||
58 | int exec(const string& s,pcre_matches_t& m) { | 57 | int exec(const string& s,pcre_matches_t& m) { |
59 | if(!_p) | 58 | if(!_p) |
60 | throw internal_error(OPKELE_CP_ "Trying to execute absent regexp"); | 59 | throw internal_error(OPKELE_CP_ "Trying to execute absent regexp"); |
61 | return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s); | 60 | return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s); |
62 | } | 61 | } |
63 | }; | 62 | }; |
64 | 63 | ||
65 | class curl_t { | ||
66 | public: | ||
67 | CURL *_c; | ||
68 | |||
69 | curl_t() : _c(0) { } | ||
70 | curl_t(CURL *c) : _c(c) { } | ||
71 | ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } | ||
72 | |||
73 | curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; } | ||
74 | |||
75 | operator const CURL*(void) const { return _c; } | ||
76 | operator CURL*(void) { return _c; } | ||
77 | }; | ||
78 | |||
79 | static CURLcode curl_misc_sets(CURL* c) { | 64 | static CURLcode curl_misc_sets(CURL* c) { |
80 | CURLcode r; | 65 | CURLcode r; |
81 | (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1)) | 66 | (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1)) |
82 | || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5)) | 67 | || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5)) |
83 | || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120)) | 68 | || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120)) |
84 | || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1)) | 69 | || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1)) |
85 | || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_SRC_VERSION)) | 70 | || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_SRC_VERSION)) |
86 | || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20)) | 71 | || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20)) |
87 | #ifdefDISABLE_CURL_SSL_VERIFYHOST | 72 | #ifdefDISABLE_CURL_SSL_VERIFYHOST |
88 | || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYHOST,0)) | 73 | || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYHOST,0)) |
89 | #endif | 74 | #endif |
90 | #ifdefDISABLE_CURL_SSL_VERIFYPEER | 75 | #ifdefDISABLE_CURL_SSL_VERIFYPEER |
91 | || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYPEER,0)) | 76 | || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYPEER,0)) |
92 | #endif | 77 | #endif |
93 | ; | 78 | ; |
94 | return r; | 79 | return r; |
@@ -103,33 +88,33 @@ namespace opkele { | |||
103 | } | 88 | } |
104 | 89 | ||
105 | assoc_t consumer_t::associate(const string& server) { | 90 | assoc_t consumer_t::associate(const string& server) { |
106 | util::dh_t dh = DH_new(); | 91 | util::dh_t dh = DH_new(); |
107 | if(!dh) | 92 | if(!dh) |
108 | throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); | 93 | throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); |
109 | dh->p = util::dec_to_bignum(data::_default_p); | 94 | dh->p = util::dec_to_bignum(data::_default_p); |
110 | dh->g = util::dec_to_bignum(data::_default_g); | 95 | dh->g = util::dec_to_bignum(data::_default_g); |
111 | if(!DH_generate_key(dh)) | 96 | if(!DH_generate_key(dh)) |
112 | throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); | 97 | throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); |
113 | string request = | 98 | string request = |
114 | "openid.mode=associate" | 99 | "openid.mode=associate" |
115 | "&openid.assoc_type=HMAC-SHA1" | 100 | "&openid.assoc_type=HMAC-SHA1" |
116 | "&openid.session_type=DH-SHA1" | 101 | "&openid.session_type=DH-SHA1" |
117 | "&openid.dh_consumer_public="; | 102 | "&openid.dh_consumer_public="; |
118 | request += util::url_encode(util::bignum_to_base64(dh->pub_key)); | 103 | request += util::url_encode(util::bignum_to_base64(dh->pub_key)); |
119 | curl_t curl = curl_easy_init(); | 104 | util::curl_t curl = curl_easy_init(); |
120 | if(!curl) | 105 | if(!curl) |
121 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); | 106 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); |
122 | string response; | 107 | string response; |
123 | CURLcode r; | 108 | CURLcode r; |
124 | (r=curl_misc_sets(curl)) | 109 | (r=curl_misc_sets(curl)) |
125 | || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) | 110 | || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) |
126 | || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) | 111 | || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) |
127 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) | 112 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) |
128 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) | 113 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) |
129 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) | 114 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) |
130 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) | 115 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) |
131 | ; | 116 | ; |
132 | if(r) | 117 | if(r) |
133 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 118 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); |
134 | if( (r=curl_easy_perform(curl)) ) | 119 | if( (r=curl_easy_perform(curl)) ) |
135 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 120 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); |
@@ -263,66 +248,66 @@ namespace opkele { | |||
263 | throw id_res_failed(OPKELE_CP_ "failed to check_authentication()"); | 248 | throw id_res_failed(OPKELE_CP_ "failed to check_authentication()"); |
264 | } | 249 | } |
265 | } | 250 | } |
266 | if(ext) ext->id_res_hook(pin,ps,identity); | 251 | if(ext) ext->id_res_hook(pin,ps,identity); |
267 | } | 252 | } |
268 | 253 | ||
269 | void consumer_t::check_authentication(const string& server,const params_t& p) { | 254 | void consumer_t::check_authentication(const string& server,const params_t& p) { |
270 | string request = "openid.mode=check_authentication"; | 255 | string request = "openid.mode=check_authentication"; |
271 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) { | 256 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) { |
272 | if(i->first!="openid.mode") { | 257 | if(i->first!="openid.mode") { |
273 | request += '&'; | 258 | request += '&'; |
274 | request += i->first; | 259 | request += i->first; |
275 | request += '='; | 260 | request += '='; |
276 | request += util::url_encode(i->second); | 261 | request += util::url_encode(i->second); |
277 | } | 262 | } |
278 | } | 263 | } |
279 | curl_t curl = curl_easy_init(); | 264 | util::curl_t curl = curl_easy_init(); |
280 | if(!curl) | 265 | if(!curl) |
281 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); | 266 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); |
282 | string response; | 267 | string response; |
283 | CURLcode r; | 268 | CURLcode r; |
284 | (r=curl_misc_sets(curl)) | 269 | (r=curl_misc_sets(curl)) |
285 | || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) | 270 | || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) |
286 | || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) | 271 | || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) |
287 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) | 272 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) |
288 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) | 273 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) |
289 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) | 274 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) |
290 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) | 275 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) |
291 | ; | 276 | ; |
292 | if(r) | 277 | if(r) |
293 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 278 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); |
294 | if( (r=curl_easy_perform(curl)) ) | 279 | if( (r=curl_easy_perform(curl)) ) |
295 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 280 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); |
296 | params_t pp; pp.parse_keyvalues(response); | 281 | params_t pp; pp.parse_keyvalues(response); |
297 | if(pp.has_param("invalidate_handle")) | 282 | if(pp.has_param("invalidate_handle")) |
298 | invalidate_assoc(server,pp.get_param("invalidate_handle")); | 283 | invalidate_assoc(server,pp.get_param("invalidate_handle")); |
299 | if(pp.has_param("is_valid")) { | 284 | if(pp.has_param("is_valid")) { |
300 | if(pp.get_param("is_valid")=="true") | 285 | if(pp.get_param("is_valid")=="true") |
301 | return; | 286 | return; |
302 | }else if(pp.has_param("lifetime")) { | 287 | }else if(pp.has_param("lifetime")) { |
303 | if(util::string_to_long(pp.get_param("lifetime"))) | 288 | if(util::string_to_long(pp.get_param("lifetime"))) |
304 | return; | 289 | return; |
305 | } | 290 | } |
306 | throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); | 291 | throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); |
307 | } | 292 | } |
308 | 293 | ||
309 | void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { | 294 | void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { |
310 | server.erase(); | 295 | server.erase(); |
311 | delegate.erase(); | 296 | delegate.erase(); |
312 | curl_t curl = curl_easy_init(); | 297 | util::curl_t curl = curl_easy_init(); |
313 | if(!curl) | 298 | if(!curl) |
314 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); | 299 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); |
315 | string html; | 300 | string html; |
316 | CURLcode r; | 301 | CURLcode r; |
317 | (r=curl_misc_sets(curl)) | 302 | (r=curl_misc_sets(curl)) |
318 | || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) | 303 | || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) |
319 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) | 304 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) |
320 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) | 305 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) |
321 | ; | 306 | ; |
322 | if(r) | 307 | if(r) |
323 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 308 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); |
324 | r = curl_easy_perform(curl); | 309 | r = curl_easy_perform(curl); |
325 | if(r && r!=CURLE_WRITE_ERROR) | 310 | if(r && r!=CURLE_WRITE_ERROR) |
326 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 311 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); |
327 | static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>", | 312 | static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>", |
328 | *re_lre = "<\\s*link\\b([^>]+)>", | 313 | *re_lre = "<\\s*link\\b([^>]+)>", |
@@ -402,33 +387,33 @@ namespace opkele { | |||
402 | i += sizeof("://")-1; | 387 | i += sizeof("://")-1; |
403 | } | 388 | } |
404 | string::size_type qm = rv.find('?',i); | 389 | string::size_type qm = rv.find('?',i); |
405 | string::size_type sl = rv.find('/',i); | 390 | string::size_type sl = rv.find('/',i); |
406 | if(qm!=string::npos) { | 391 | if(qm!=string::npos) { |
407 | if(sl==string::npos || sl>qm) | 392 | if(sl==string::npos || sl>qm) |
408 | rv.insert(qm,1,'/'); | 393 | rv.insert(qm,1,'/'); |
409 | }else{ | 394 | }else{ |
410 | if(sl==string::npos) | 395 | if(sl==string::npos) |
411 | rv += '/'; | 396 | rv += '/'; |
412 | } | 397 | } |
413 | return rv; | 398 | return rv; |
414 | } | 399 | } |
415 | 400 | ||
416 | string consumer_t::canonicalize(const string& url) { | 401 | string consumer_t::canonicalize(const string& url) { |
417 | string rv = normalize(url); | 402 | string rv = normalize(url); |
418 | curl_t curl = curl_easy_init(); | 403 | util::curl_t curl = curl_easy_init(); |
419 | if(!curl) | 404 | if(!curl) |
420 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); | 405 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); |
421 | string html; | 406 | string html; |
422 | CURLcode r; | 407 | CURLcode r; |
423 | (r=curl_misc_sets(curl)) | 408 | (r=curl_misc_sets(curl)) |
424 | || (r=curl_easy_setopt(curl,CURLOPT_URL,rv.c_str())) | 409 | || (r=curl_easy_setopt(curl,CURLOPT_URL,rv.c_str())) |
425 | || (r=curl_easy_setopt(curl,CURLOPT_NOBODY,1)) | 410 | || (r=curl_easy_setopt(curl,CURLOPT_NOBODY,1)) |
426 | ; | 411 | ; |
427 | if(r) | 412 | if(r) |
428 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 413 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); |
429 | r = curl_easy_perform(curl); | 414 | r = curl_easy_perform(curl); |
430 | if(r) | 415 | if(r) |
431 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 416 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); |
432 | const char *eu = 0; | 417 | const char *eu = 0; |
433 | r = curl_easy_getinfo(curl,CURLINFO_EFFECTIVE_URL,&eu); | 418 | r = curl_easy_getinfo(curl,CURLINFO_EFFECTIVE_URL,&eu); |
434 | if(r) | 419 | if(r) |