-rw-r--r-- | lib/consumer.cc | 124 |
1 files changed, 65 insertions, 59 deletions
diff --git a/lib/consumer.cc b/lib/consumer.cc index ff5da91..df95b64 100644 --- a/lib/consumer.cc +++ b/lib/consumer.cc | |||
@@ -1,49 +1,87 @@ | |||
1 | #include <algorithm> | 1 | #include <algorithm> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <opkele/util.h> | 3 | #include <opkele/util.h> |
4 | #include <opkele/exception.h> | 4 | #include <opkele/exception.h> |
5 | #include <opkele/data.h> | 5 | #include <opkele/data.h> |
6 | #include <opkele/consumer.h> | 6 | #include <opkele/consumer.h> |
7 | #include <openssl/sha.h> | 7 | #include <openssl/sha.h> |
8 | #include <openssl/hmac.h> | 8 | #include <openssl/hmac.h> |
9 | #include <curl/curl.h> | 9 | #include <curl/curl.h> |
10 | 10 | ||
11 | #include <iostream> | 11 | #include <iostream> |
12 | 12 | ||
13 | #include "config.h" | 13 | #include "config.h" |
14 | 14 | ||
15 | #if defined(USE_LIBPCRECPP) | 15 | #include <pcre.h> |
16 | # include <pcrecpp.h> | ||
17 | #elif defined(USE_PCREPP) | ||
18 | # include <pcre++.h> | ||
19 | #else | ||
20 | /* internal implementation won't be built */ | ||
21 | #endif | ||
22 | 16 | ||
23 | namespace opkele { | 17 | namespace opkele { |
24 | using namespace std; | 18 | using namespace std; |
25 | 19 | ||
20 | class pcre_matches_t { | ||
21 | public: | ||
22 | int *_ov; | ||
23 | int _s; | ||
24 | |||
25 | pcre_matches_t() : _ov(0), _s(0) { } | ||
26 | pcre_matches_t(int s) : _ov(0), _s(s) { | ||
27 | if(_s&1) ++_s; | ||
28 | _s += _s>>1; | ||
29 | _ov = new int[_s]; | ||
30 | } | ||
31 | ~pcre_matches_t() throw() { if(_ov) delete[] _ov; } | ||
32 | |||
33 | int begin(int i) const { return _ov[i<<1]; } | ||
34 | int end(int i) const { return _ov[(i<<1)+1]; } | ||
35 | int length(int i) const { int t=i<<1; return _ov[t+1]-_ov[t]; } | ||
36 | }; | ||
37 | |||
38 | class pcre_t { | ||
39 | public: | ||
40 | pcre *_p; | ||
41 | |||
42 | pcre_t() : _p(0) { } | ||
43 | pcre_t(pcre *p) : _p(p) { } | ||
44 | pcre_t(const char *re,int opts) : _p(0) { | ||
45 | static const char *errptr; static int erroffset; | ||
46 | _p = pcre_compile(re,opts,&errptr,&erroffset,NULL); | ||
47 | if(!_p) | ||
48 | throw internal_error(OPKELE_CP_ string("Failed to compile regexp: ")+errptr); | ||
49 | } | ||
50 | ~pcre_t() throw() { if(_p) (*pcre_free)(_p); } | ||
51 | |||
52 | pcre_t& operator=(pcre *p) { if(_p) (*pcre_free)(_p); _p=p; return *this; } | ||
53 | |||
54 | operator const pcre*(void) const { return _p; } | ||
55 | operator pcre*(void) { return _p; } | ||
56 | |||
57 | int exec(const string& s,pcre_matches_t& m) { | ||
58 | if(!_p) | ||
59 | 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 | } | ||
62 | }; | ||
63 | |||
26 | class curl_t { | 64 | class curl_t { |
27 | public: | 65 | public: |
28 | CURL *_c; | 66 | CURL *_c; |
29 | 67 | ||
30 | curl_t() : _c(0) { } | 68 | curl_t() : _c(0) { } |
31 | curl_t(CURL *c) : _c(c) { } | 69 | curl_t(CURL *c) : _c(c) { } |
32 | ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } | 70 | ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } |
33 | 71 | ||
34 | curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; } | 72 | curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; } |
35 | 73 | ||
36 | operator const CURL*(void) const { return _c; } | 74 | operator const CURL*(void) const { return _c; } |
37 | operator CURL*(void) { return _c; } | 75 | operator CURL*(void) { return _c; } |
38 | }; | 76 | }; |
39 | 77 | ||
40 | static CURLcode curl_misc_sets(CURL* c) { | 78 | static CURLcode curl_misc_sets(CURL* c) { |
41 | CURLcode r; | 79 | CURLcode r; |
42 | (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1)) | 80 | (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1)) |
43 | || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5)) | 81 | || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5)) |
44 | || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120)) | 82 | || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120)) |
45 | || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1)) | 83 | || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1)) |
46 | || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_VERSION)) | 84 | || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_VERSION)) |
47 | || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20)) | 85 | || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20)) |
48 | #ifdefDISABLE_CURL_SSL_VERIFYHOST | 86 | #ifdefDISABLE_CURL_SSL_VERIFYHOST |
49 | || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYHOST,0)) | 87 | || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYHOST,0)) |
@@ -247,127 +285,95 @@ namespace opkele { | |||
247 | || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) | 285 | || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) |
248 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) | 286 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) |
249 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) | 287 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) |
250 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) | 288 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) |
251 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) | 289 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) |
252 | ; | 290 | ; |
253 | if(r) | 291 | if(r) |
254 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 292 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); |
255 | if(r=curl_easy_perform(curl)) | 293 | if(r=curl_easy_perform(curl)) |
256 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 294 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); |
257 | params_t pp; pp.parse_keyvalues(response); | 295 | params_t pp; pp.parse_keyvalues(response); |
258 | if(pp.has_param("invalidate_handle")) | 296 | if(pp.has_param("invalidate_handle")) |
259 | invalidate_assoc(server,pp.get_param("invalidate_handle")); | 297 | invalidate_assoc(server,pp.get_param("invalidate_handle")); |
260 | if(pp.has_param("is_valid")) { | 298 | if(pp.has_param("is_valid")) { |
261 | if(pp.get_param("is_valid")=="true") | 299 | if(pp.get_param("is_valid")=="true") |
262 | return; | 300 | return; |
263 | }else if(pp.has_param("lifetime")) { | 301 | }else if(pp.has_param("lifetime")) { |
264 | if(util::string_to_long(pp.get_param("lifetime"))) | 302 | if(util::string_to_long(pp.get_param("lifetime"))) |
265 | return; | 303 | return; |
266 | } | 304 | } |
267 | throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); | 305 | throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); |
268 | } | 306 | } |
269 | 307 | ||
270 | void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { | 308 | void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { |
271 | #if defined(USE_LIBPCRECPP) || defined(USE_PCREPP) | ||
272 | server.erase(); | 309 | server.erase(); |
273 | delegate.erase(); | 310 | delegate.erase(); |
274 | curl_t curl = curl_easy_init(); | 311 | curl_t curl = curl_easy_init(); |
275 | if(!curl) | 312 | if(!curl) |
276 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); | 313 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); |
277 | string html; | 314 | string html; |
278 | CURLcode r; | 315 | CURLcode r; |
279 | (r=curl_misc_sets(curl)) | 316 | (r=curl_misc_sets(curl)) |
280 | || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) | 317 | || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) |
281 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) | 318 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) |
282 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) | 319 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) |
283 | ; | 320 | ; |
284 | if(r) | 321 | if(r) |
285 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 322 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); |
286 | r = curl_easy_perform(curl); | 323 | r = curl_easy_perform(curl); |
287 | if(r && r!=CURLE_WRITE_ERROR) | 324 | if(r && r!=CURLE_WRITE_ERROR) |
288 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 325 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); |
289 | // strip out everything past body | 326 | static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>", |
290 | static const char *re_hdre = "<\\s*head[^>]*>", | ||
291 | *re_lre = "<\\s*link\\b([^>]+)>", | 327 | *re_lre = "<\\s*link\\b([^>]+)>", |
292 | *re_rre = "\\brel\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]", | 328 | *re_rre = "\\brel\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]", |
293 | *re_hre = "\\bhref\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]"; | 329 | *re_hre = "\\bhref\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]"; |
294 | #if defined(USE_LIBPCRECPP) | 330 | pcre_matches_t m1(3), m2(3); |
295 | static pcrecpp::RE_Options ro(PCRE_CASELESS|PCRE_DOTALL); | 331 | pcre_t bre(re_bre,PCRE_CASELESS); |
296 | static pcrecpp::RE | 332 | if(bre.exec(html,m1)>0) |
297 | bre("<body\\b.*",ro), hdre(re_hdre,ro), | 333 | html.erase(m1.begin(0)); |
298 | lre(re_lre,ro), rre(re_rre), hre(re_hre,ro); | 334 | pcre_t hdre(re_hdre,PCRE_CASELESS); |
299 | bre.Replace("",&html); | 335 | if(hdre.exec(html,m1)<=0) |
300 | pcrecpp::StringPiece hpiece(html); | 336 | throw bad_input(OPKELE_CP_ "failed to find <head>"); |
301 | if(!hdre.FindAndConsume(&hpiece)) | 337 | html.erase(0,m1.end(0)+1); |
302 | throw bad_input(OPKELE_CP_ "failed to find head"); | 338 | pcre_t lre(re_lre,PCRE_CASELESS), rre(re_rre,PCRE_CASELESS), hre(re_hre,PCRE_CASELESS); |
303 | string attrs; | 339 | while(lre.exec(html,m1)>=2) { |
304 | while(lre.FindAndConsume(&hpiece,&attrs)) { | 340 | string attrs(html,m1.begin(1),m1.length(1)); |
305 | pcrecpp::StringPiece rel, href; | 341 | html.erase(0,m1.end(0)+1); |
306 | if(!(rre.PartialMatch(attrs,&rel) && hre.PartialMatch(attrs,&href))) | 342 | if(!( rre.exec(attrs,m1)>=2 && hre.exec(attrs,m2)>=2 )) |
307 | continue; | 343 | continue; |
344 | string rel(attrs,m1.begin(1),m1.length(1)); | ||
345 | string href(attrs,m2.begin(1),m2.length(1)); | ||
308 | if(rel=="openid.server") { | 346 | if(rel=="openid.server") { |
309 | href.CopyToString(&server); | 347 | server = href; |
310 | if(!delegate.empty()) | 348 | if(!delegate.empty()) break; |
311 | break; | ||
312 | }else if(rel=="openid.delegate") { | 349 | }else if(rel=="openid.delegate") { |
313 | href.CopyToString(&delegate); | 350 | delegate = href; |
314 | if(!server.empty()) | 351 | if(!server.empty()) break; |
315 | break; | ||
316 | } | ||
317 | } | ||
318 | #elif defined(USE_PCREPP) | ||
319 | pcrepp::Pcre bre("<body\\b",PCRE_CASELESS); | ||
320 | if(bre.search(html)) | ||
321 | html.erase(bre.get_match_start()); | ||
322 | pcrepp::Pcre hdre(re_hdre,PCRE_CASELESS); | ||
323 | if(!hdre.search(html)) | ||
324 | throw bad_input(OPKELE_CP_ "failed to find head"); | ||
325 | html.erase(0,hdre.get_match_end()+1); | ||
326 | pcrepp::Pcre lre(re_lre,PCRE_CASELESS), rre(re_rre,PCRE_CASELESS), hre(re_hre,PCRE_CASELESS); | ||
327 | while(lre.search(html)) { | ||
328 | string attrs = lre[0]; | ||
329 | html.erase(0,lre.get_match_end()+1); | ||
330 | if(!(rre.search(attrs)&&hre.search(attrs))) | ||
331 | continue; | ||
332 | if(rre[0]=="openid.server") { | ||
333 | server = hre[0]; | ||
334 | if(!delegate.empty()) | ||
335 | break; | ||
336 | }else if(rre[0]=="openid.delegate") { | ||
337 | delegate = hre[0]; | ||
338 | if(!server.empty()) | ||
339 | break; | ||
340 | } | 352 | } |
341 | } | 353 | } |
342 | #else | ||
343 | #error "I must have gone crazy" | ||
344 | #endif | ||
345 | if(server.empty()) | 354 | if(server.empty()) |
346 | throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration"); | 355 | throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration"); |
347 | #else /* none of the RE bindings enabled */ | ||
348 | throw not_implemented(OPKELE_CP_ "No internal implementation of retrieve_links were provided at compile-time"); | ||
349 | #endif | ||
350 | } | 356 | } |
351 | 357 | ||
352 | assoc_t consumer_t::find_assoc(const string& server) { | 358 | assoc_t consumer_t::find_assoc(const string& server) { |
353 | throw failed_lookup(OPKELE_CP_ "no find_assoc() provided"); | 359 | throw failed_lookup(OPKELE_CP_ "no find_assoc() provided"); |
354 | } | 360 | } |
355 | 361 | ||
356 | string consumer_t::normalize(const string& url) { | 362 | string consumer_t::normalize(const string& url) { |
357 | string rv = url; | 363 | string rv = url; |
358 | // strip leading and trailing spaces | 364 | // strip leading and trailing spaces |
359 | string::size_type i = rv.find_first_not_of(" \t\r\n"); | 365 | string::size_type i = rv.find_first_not_of(" \t\r\n"); |
360 | if(i==string::npos) | 366 | if(i==string::npos) |
361 | throw bad_input(OPKELE_CP_ "empty URL"); | 367 | throw bad_input(OPKELE_CP_ "empty URL"); |
362 | if(i) | 368 | if(i) |
363 | rv.erase(0,i); | 369 | rv.erase(0,i); |
364 | i = rv.find_last_not_of(" \t\r\n"); | 370 | i = rv.find_last_not_of(" \t\r\n"); |
365 | assert(i!=string::npos); | 371 | assert(i!=string::npos); |
366 | if(i<(rv.length()-1)) | 372 | if(i<(rv.length()-1)) |
367 | rv.erase(i+1); | 373 | rv.erase(i+1); |
368 | // add missing http:// | 374 | // add missing http:// |
369 | i = rv.find("://"); | 375 | i = rv.find("://"); |
370 | if(i==string::npos) { // primitive. but do we need more? | 376 | if(i==string::npos) { // primitive. but do we need more? |
371 | rv.insert(0,"http://"); | 377 | rv.insert(0,"http://"); |
372 | i = sizeof("http://")-1; | 378 | i = sizeof("http://")-1; |
373 | }else{ | 379 | }else{ |