author | Michael Krelin <hacker@klever.net> | 2008-02-02 21:10:12 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-02-02 21:10:12 (UTC) |
commit | 3658759966cbadb7b50457d446f3436b6f7987da (patch) (unidiff) | |
tree | b215da5b5212b60aa1ec965df28070b4bff587bc /lib/util.cc | |
parent | a8f733c88d87abe422ecaa405df385bad562e60f (diff) | |
download | libopkele-3658759966cbadb7b50457d446f3436b6f7987da.zip libopkele-3658759966cbadb7b50457d446f3436b6f7987da.tar.gz libopkele-3658759966cbadb7b50457d446f3436b6f7987da.tar.bz2 |
moved uri matching into separate procedure
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | lib/util.cc | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/util.cc b/lib/util.cc index b7bc437..b85a377 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,31 +1,32 @@ | |||
1 | #include <errno.h> | 1 | #include <errno.h> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <cctype> | 3 | #include <cctype> |
4 | #include <cstring> | 4 | #include <cstring> |
5 | #include <vector> | 5 | #include <vector> |
6 | #include <string> | 6 | #include <string> |
7 | #include <stack> | 7 | #include <stack> |
8 | #include <algorithm> | ||
8 | #include <openssl/bio.h> | 9 | #include <openssl/bio.h> |
9 | #include <openssl/evp.h> | 10 | #include <openssl/evp.h> |
10 | #include <openssl/hmac.h> | 11 | #include <openssl/hmac.h> |
11 | #include <curl/curl.h> | 12 | #include <curl/curl.h> |
12 | #include "opkele/util.h" | 13 | #include "opkele/util.h" |
13 | #include "opkele/exception.h" | 14 | #include "opkele/exception.h" |
14 | 15 | ||
15 | #include <config.h> | 16 | #include <config.h> |
16 | #ifdef HAVE_DEMANGLE | 17 | #ifdef HAVE_DEMANGLE |
17 | # include <cxxabi.h> | 18 | # include <cxxabi.h> |
18 | #endif | 19 | #endif |
19 | 20 | ||
20 | namespace opkele { | 21 | namespace opkele { |
21 | using namespace std; | 22 | using namespace std; |
22 | 23 | ||
23 | namespace util { | 24 | namespace util { |
24 | 25 | ||
25 | /* | 26 | /* |
26 | * base64 | 27 | * base64 |
27 | */ | 28 | */ |
28 | string encode_base64(const void *data,size_t length) { | 29 | string encode_base64(const void *data,size_t length) { |
29 | BIO *b64 = 0, *bmem = 0; | 30 | BIO *b64 = 0, *bmem = 0; |
30 | try { | 31 | try { |
31 | b64 = BIO_new(BIO_f_base64()); | 32 | b64 = BIO_new(BIO_f_base64()); |
@@ -330,48 +331,77 @@ namespace opkele { | |||
330 | } | 331 | } |
331 | if(!pseg.empty()) { | 332 | if(!pseg.empty()) { |
332 | if(!qf) rv += '/'; | 333 | if(!qf) rv += '/'; |
333 | rv += pseg; | 334 | rv += pseg; |
334 | } | 335 | } |
335 | return rv; | 336 | return rv; |
336 | } | 337 | } |
337 | 338 | ||
338 | string& strip_uri_fragment_part(string& u) { | 339 | string& strip_uri_fragment_part(string& u) { |
339 | string::size_type q = u.find('?'), f = u.find('#'); | 340 | string::size_type q = u.find('?'), f = u.find('#'); |
340 | if(q==string::npos) { | 341 | if(q==string::npos) { |
341 | if(f!=string::npos) | 342 | if(f!=string::npos) |
342 | u.erase(f); | 343 | u.erase(f); |
343 | }else{ | 344 | }else{ |
344 | if(f!=string::npos) { | 345 | if(f!=string::npos) { |
345 | if(f<q) | 346 | if(f<q) |
346 | u.erase(f,q-f); | 347 | u.erase(f,q-f); |
347 | else | 348 | else |
348 | u.erase(f); | 349 | u.erase(f); |
349 | } | 350 | } |
350 | } | 351 | } |
351 | return u; | 352 | return u; |
352 | } | 353 | } |
353 | 354 | ||
355 | bool uri_matches_realm(const string& uri,const string& realm) { | ||
356 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); | ||
357 | string nu = opkele::util::rfc_3986_normalize_uri(uri); | ||
358 | string::size_type pr = nrealm.find("://"); | ||
359 | string::size_type pu = nu.find("://"); | ||
360 | assert(!(pr==string::npos || pu==string::npos)); | ||
361 | pr += sizeof("://")-1; | ||
362 | pu += sizeof("://")-1; | ||
363 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { | ||
364 | pr = nrealm.find('.',pr); | ||
365 | pu = nu.find('.',pu); | ||
366 | assert(pr!=string::npos); | ||
367 | if(pu==string::npos) | ||
368 | return false; | ||
369 | // TODO: check for overgeneralized realm | ||
370 | } | ||
371 | string::size_type lr = nrealm.length(); | ||
372 | string::size_type lu = nu.length(); | ||
373 | if( (lu-pu) < (lr-pr) ) | ||
374 | return false; | ||
375 | pair<const char*,const char*> mp = mismatch( | ||
376 | nrealm.c_str()+pr,nrealm.c_str()+lr, | ||
377 | nu.c_str()+pu); | ||
378 | if( (*(mp.first-1))!='/' | ||
379 | && !strchr("/?#",*mp.second) ) | ||
380 | return false; | ||
381 | return true; | ||
382 | } | ||
383 | |||
354 | string abi_demangle(const char *mn) { | 384 | string abi_demangle(const char *mn) { |
355 | #ifndef HAVE_DEMANGLE | 385 | #ifndef HAVE_DEMANGLE |
356 | return mn; | 386 | return mn; |
357 | #else /* !HAVE_DEMANGLE */ | 387 | #else /* !HAVE_DEMANGLE */ |
358 | int dstat; | 388 | int dstat; |
359 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); | 389 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); |
360 | if(dstat) | 390 | if(dstat) |
361 | return mn; | 391 | return mn; |
362 | string rv = demangled; | 392 | string rv = demangled; |
363 | free(demangled); | 393 | free(demangled); |
364 | return rv; | 394 | return rv; |
365 | #endif /* !HAVE_DEMANGLE */ | 395 | #endif /* !HAVE_DEMANGLE */ |
366 | } | 396 | } |
367 | 397 | ||
368 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { | 398 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { |
369 | const string& slist = om.get_field("signed"); | 399 | const string& slist = om.get_field("signed"); |
370 | string kv; | 400 | string kv; |
371 | string::size_type p=0; | 401 | string::size_type p=0; |
372 | while(true) { | 402 | while(true) { |
373 | string::size_type co = slist.find(',',p); | 403 | string::size_type co = slist.find(',',p); |
374 | string f = (co==string::npos) | 404 | string f = (co==string::npos) |
375 | ?slist.substr(p):slist.substr(p,co-p); | 405 | ?slist.substr(p):slist.substr(p,co-p); |
376 | kv += f; | 406 | kv += f; |
377 | kv += ':'; | 407 | kv += ':'; |