summaryrefslogtreecommitdiffabout
path: root/lib/consumer.cc
authorMichael Krelin <hacker@klever.net>2007-09-22 20:56:57 (UTC)
committer Michael Krelin <hacker@klever.net>2007-09-22 20:56:57 (UTC)
commit5cb71518360f865cd4448a083b5d481e6f356a3e (patch) (unidiff)
tree4faeab2b1a1a14c589745be714be18471879e781 /lib/consumer.cc
parent5cfe3f2743c2b2eb2689090516a4e667fd8ca29a (diff)
downloadlibopkele-5cb71518360f865cd4448a083b5d481e6f356a3e.zip
libopkele-5cb71518360f865cd4448a083b5d481e6f356a3e.tar.gz
libopkele-5cb71518360f865cd4448a083b5d481e6f356a3e.tar.bz2
got rid of all pcre wrappers altogether
although all claims against pcre++ make absolutely no sense. But for now I'll live with no wrappers at all. I may reconsider it when implementing more sophisticated discovery mechanisms for upcoming extensions. Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (limited to 'lib/consumer.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/consumer.cc124
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,412 +1,418 @@
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
23namespace opkele { 17namespace 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))
50#endif 88#endif
51 #ifdefDISABLE_CURL_SSL_VERIFYPEER 89 #ifdefDISABLE_CURL_SSL_VERIFYPEER
52 || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYPEER,0)) 90 || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYPEER,0))
53#endif 91#endif
54 ; 92 ;
55 return r; 93 return r;
56 } 94 }
57 95
58 static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) { 96 static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) {
59 string *str = (string*)stream; 97 string *str = (string*)stream;
60 size_t bytes = size*nmemb; 98 size_t bytes = size*nmemb;
61 size_t get = min(16384-str->length(),bytes); 99 size_t get = min(16384-str->length(),bytes);
62 str->append((const char*)ptr,get); 100 str->append((const char*)ptr,get);
63 return get; 101 return get;
64 } 102 }
65 103
66 assoc_t consumer_t::associate(const string& server) { 104 assoc_t consumer_t::associate(const string& server) {
67 util::dh_t dh = DH_new(); 105 util::dh_t dh = DH_new();
68 if(!dh) 106 if(!dh)
69 throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); 107 throw exception_openssl(OPKELE_CP_ "failed to DH_new()");
70 dh->p = util::dec_to_bignum(data::_default_p); 108 dh->p = util::dec_to_bignum(data::_default_p);
71 dh->g = util::dec_to_bignum(data::_default_g); 109 dh->g = util::dec_to_bignum(data::_default_g);
72 if(!DH_generate_key(dh)) 110 if(!DH_generate_key(dh))
73 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); 111 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()");
74 string request = 112 string request =
75 "openid.mode=associate" 113 "openid.mode=associate"
76 "&openid.assoc_type=HMAC-SHA1" 114 "&openid.assoc_type=HMAC-SHA1"
77 "&openid.session_type=DH-SHA1" 115 "&openid.session_type=DH-SHA1"
78 "&openid.dh_consumer_public="; 116 "&openid.dh_consumer_public=";
79 request += util::url_encode(util::bignum_to_base64(dh->pub_key)); 117 request += util::url_encode(util::bignum_to_base64(dh->pub_key));
80 curl_t curl = curl_easy_init(); 118 curl_t curl = curl_easy_init();
81 if(!curl) 119 if(!curl)
82 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 120 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
83 string response; 121 string response;
84 CURLcode r; 122 CURLcode r;
85 (r=curl_misc_sets(curl)) 123 (r=curl_misc_sets(curl))
86 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) 124 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str()))
87 || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) 125 || (r=curl_easy_setopt(curl,CURLOPT_POST,1))
88 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) 126 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data()))
89 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) 127 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length()))
90 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 128 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
91 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) 129 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response))
92 ; 130 ;
93 if(r) 131 if(r)
94 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 132 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
95 if(r=curl_easy_perform(curl)) 133 if(r=curl_easy_perform(curl))
96 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 134 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
97 params_t p; p.parse_keyvalues(response); 135 params_t p; p.parse_keyvalues(response);
98 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1") 136 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1")
99 throw bad_input(OPKELE_CP_ "unsupported assoc_type"); 137 throw bad_input(OPKELE_CP_ "unsupported assoc_type");
100 string st; 138 string st;
101 if(p.has_param("session_type")) st = p.get_param("session_type"); 139 if(p.has_param("session_type")) st = p.get_param("session_type");
102 if((!st.empty()) && st!="DH-SHA1") 140 if((!st.empty()) && st!="DH-SHA1")
103 throw bad_input(OPKELE_CP_ "unsupported session_type"); 141 throw bad_input(OPKELE_CP_ "unsupported session_type");
104 secret_t secret; 142 secret_t secret;
105 if(st.empty()) { 143 if(st.empty()) {
106 secret.from_base64(p.get_param("mac_key")); 144 secret.from_base64(p.get_param("mac_key"));
107 }else{ 145 }else{
108 util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public")); 146 util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public"));
109 vector<unsigned char> ck(DH_size(dh)+1); 147 vector<unsigned char> ck(DH_size(dh)+1);
110 unsigned char *ckptr = &(ck.front())+1; 148 unsigned char *ckptr = &(ck.front())+1;
111 int cklen = DH_compute_key(ckptr,s_pub,dh); 149 int cklen = DH_compute_key(ckptr,s_pub,dh);
112 if(cklen<0) 150 if(cklen<0)
113 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); 151 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()");
114 if(cklen && (*ckptr)&0x80) { 152 if(cklen && (*ckptr)&0x80) {
115 (*(--ckptr)) = 0; ++cklen; 153 (*(--ckptr)) = 0; ++cklen;
116 } 154 }
117 unsigned char key_sha1[SHA_DIGEST_LENGTH]; 155 unsigned char key_sha1[SHA_DIGEST_LENGTH];
118 SHA1(ckptr,cklen,key_sha1); 156 SHA1(ckptr,cklen,key_sha1);
119 secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key")); 157 secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key"));
120 } 158 }
121 int expires_in = 0; 159 int expires_in = 0;
122 if(p.has_param("expires_in")) { 160 if(p.has_param("expires_in")) {
123 expires_in = util::string_to_long(p.get_param("expires_in")); 161 expires_in = util::string_to_long(p.get_param("expires_in"));
124 }else if(p.has_param("issued") && p.has_param("expiry")) { 162 }else if(p.has_param("issued") && p.has_param("expiry")) {
125 expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued")); 163 expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued"));
126 }else 164 }else
127 throw bad_input(OPKELE_CP_ "no expiration information"); 165 throw bad_input(OPKELE_CP_ "no expiration information");
128 return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in); 166 return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in);
129 } 167 }
130 168
131 string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 169 string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
132 return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext); 170 return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext);
133 } 171 }
134 string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 172 string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
135 return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext); 173 return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext);
136 } 174 }
137 string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 175 string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
138 params_t p; 176 params_t p;
139 if(mode==mode_checkid_immediate) 177 if(mode==mode_checkid_immediate)
140 p["mode"]="checkid_immediate"; 178 p["mode"]="checkid_immediate";
141 else if(mode==mode_checkid_setup) 179 else if(mode==mode_checkid_setup)
142 p["mode"]="checkid_setup"; 180 p["mode"]="checkid_setup";
143 else 181 else
144 throw bad_input(OPKELE_CP_ "unknown checkid_* mode"); 182 throw bad_input(OPKELE_CP_ "unknown checkid_* mode");
145 string iurl = canonicalize(identity); 183 string iurl = canonicalize(identity);
146 string server, delegate; 184 string server, delegate;
147 retrieve_links(iurl,server,delegate); 185 retrieve_links(iurl,server,delegate);
148 p["identity"] = delegate.empty()?iurl:delegate; 186 p["identity"] = delegate.empty()?iurl:delegate;
149 if(!trust_root.empty()) 187 if(!trust_root.empty())
150 p["trust_root"] = trust_root; 188 p["trust_root"] = trust_root;
151 p["return_to"] = return_to; 189 p["return_to"] = return_to;
152 try { 190 try {
153 string ah = find_assoc(server)->handle(); 191 string ah = find_assoc(server)->handle();
154 p["assoc_handle"] = ah; 192 p["assoc_handle"] = ah;
155 }catch(failed_lookup& fl) { 193 }catch(failed_lookup& fl) {
156 string ah = associate(server)->handle(); 194 string ah = associate(server)->handle();
157 p["assoc_handle"] = ah; 195 p["assoc_handle"] = ah;
158 } 196 }
159 if(ext) ext->checkid_hook(p,identity); 197 if(ext) ext->checkid_hook(p,identity);
160 return p.append_query(server); 198 return p.append_query(server);
161 } 199 }
162 200
163 void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) { 201 void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) {
164 if(pin.has_param("openid.user_setup_url")) 202 if(pin.has_param("openid.user_setup_url"))
165 throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url")); 203 throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url"));
166 string server,delegate; 204 string server,delegate;
167 retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate); 205 retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate);
168 params_t ps; 206 params_t ps;
169 try { 207 try {
170 assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle")); 208 assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle"));
171 if(assoc->is_expired()) /* TODO: or should I throw some other exception to force programmer fix his implementation? */ 209 if(assoc->is_expired()) /* TODO: or should I throw some other exception to force programmer fix his implementation? */
172 throw failed_lookup(OPKELE_CP_ "retrieve_assoc() has returned expired handle"); 210 throw failed_lookup(OPKELE_CP_ "retrieve_assoc() has returned expired handle");
173 const string& sigenc = pin.get_param("openid.sig"); 211 const string& sigenc = pin.get_param("openid.sig");
174 vector<unsigned char> sig; 212 vector<unsigned char> sig;
175 util::decode_base64(sigenc,sig); 213 util::decode_base64(sigenc,sig);
176 const string& slist = pin.get_param("openid.signed"); 214 const string& slist = pin.get_param("openid.signed");
177 string kv; 215 string kv;
178 string::size_type p = 0; 216 string::size_type p = 0;
179 while(true) { 217 while(true) {
180 string::size_type co = slist.find(',',p); 218 string::size_type co = slist.find(',',p);
181 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); 219 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p);
182 kv += f; 220 kv += f;
183 kv += ':'; 221 kv += ':';
184 f.insert(0,"openid."); 222 f.insert(0,"openid.");
185 kv += pin.get_param(f); 223 kv += pin.get_param(f);
186 kv += '\n'; 224 kv += '\n';
187 if(ext) ps[f.substr(sizeof("openid.")-1)] = pin.get_param(f); 225 if(ext) ps[f.substr(sizeof("openid.")-1)] = pin.get_param(f);
188 if(co==string::npos) 226 if(co==string::npos)
189 break; 227 break;
190 p = co+1; 228 p = co+1;
191 } 229 }
192 secret_t secret = assoc->secret(); 230 secret_t secret = assoc->secret();
193 unsigned int md_len = 0; 231 unsigned int md_len = 0;
194 unsigned char *md = HMAC( 232 unsigned char *md = HMAC(
195 EVP_sha1(), 233 EVP_sha1(),
196 &(secret.front()),secret.size(), 234 &(secret.front()),secret.size(),
197 (const unsigned char *)kv.data(),kv.length(), 235 (const unsigned char *)kv.data(),kv.length(),
198 0,&md_len); 236 0,&md_len);
199 if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len)) 237 if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len))
200 throw id_res_mismatch(OPKELE_CP_ "signature mismatch"); 238 throw id_res_mismatch(OPKELE_CP_ "signature mismatch");
201 }catch(failed_lookup& e) { /* XXX: more specific? */ 239 }catch(failed_lookup& e) { /* XXX: more specific? */
202 const string& slist = pin.get_param("openid.signed"); 240 const string& slist = pin.get_param("openid.signed");
203 string::size_type pp = 0; 241 string::size_type pp = 0;
204 params_t p; 242 params_t p;
205 while(true) { 243 while(true) {
206 string::size_type co = slist.find(',',pp); 244 string::size_type co = slist.find(',',pp);
207 string f = "openid."; 245 string f = "openid.";
208 f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp); 246 f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp);
209 p[f] = pin.get_param(f); 247 p[f] = pin.get_param(f);
210 if(co==string::npos) 248 if(co==string::npos)
211 break; 249 break;
212 pp = co+1; 250 pp = co+1;
213 } 251 }
214 p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle"); 252 p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle");
215 p["openid.sig"] = pin.get_param("openid.sig"); 253 p["openid.sig"] = pin.get_param("openid.sig");
216 p["openid.signed"] = pin.get_param("openid.signed"); 254 p["openid.signed"] = pin.get_param("openid.signed");
217 try { 255 try {
218 string ih = pin.get_param("openid.invalidate_handle"); 256 string ih = pin.get_param("openid.invalidate_handle");
219 p["openid.invalidate_handle"] = ih; 257 p["openid.invalidate_handle"] = ih;
220 }catch(failed_lookup& fl) { } 258 }catch(failed_lookup& fl) { }
221 try { 259 try {
222 check_authentication(server,p); 260 check_authentication(server,p);
223 }catch(failed_check_authentication& fca) { 261 }catch(failed_check_authentication& fca) {
224 throw id_res_failed(OPKELE_CP_ "failed to check_authentication()"); 262 throw id_res_failed(OPKELE_CP_ "failed to check_authentication()");
225 } 263 }
226 } 264 }
227 if(ext) ext->id_res_hook(pin,ps,identity); 265 if(ext) ext->id_res_hook(pin,ps,identity);
228 } 266 }
229 267
230 void consumer_t::check_authentication(const string& server,const params_t& p) { 268 void consumer_t::check_authentication(const string& server,const params_t& p) {
231 string request = "openid.mode=check_authentication"; 269 string request = "openid.mode=check_authentication";
232 for(params_t::const_iterator i=p.begin();i!=p.end();++i) { 270 for(params_t::const_iterator i=p.begin();i!=p.end();++i) {
233 if(i->first!="openid.mode") { 271 if(i->first!="openid.mode") {
234 request += '&'; 272 request += '&';
235 request += i->first; 273 request += i->first;
236 request += '='; 274 request += '=';
237 request += util::url_encode(i->second); 275 request += util::url_encode(i->second);
238 } 276 }
239 } 277 }
240 curl_t curl = curl_easy_init(); 278 curl_t curl = curl_easy_init();
241 if(!curl) 279 if(!curl)
242 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 280 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
243 string response; 281 string response;
244 CURLcode r; 282 CURLcode r;
245 (r=curl_misc_sets(curl)) 283 (r=curl_misc_sets(curl))
246 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) 284 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str()))
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{
374 i += sizeof("://")-1; 380 i += sizeof("://")-1;
375 } 381 }
376 string::size_type qm = rv.find('?',i); 382 string::size_type qm = rv.find('?',i);
377 string::size_type sl = rv.find('/',i); 383 string::size_type sl = rv.find('/',i);
378 if(qm!=string::npos) { 384 if(qm!=string::npos) {
379 if(sl==string::npos || sl>qm) 385 if(sl==string::npos || sl>qm)
380 rv.insert(qm,1,'/'); 386 rv.insert(qm,1,'/');
381 }else{ 387 }else{
382 if(sl==string::npos) 388 if(sl==string::npos)
383 rv += '/'; 389 rv += '/';
384 } 390 }
385 return rv; 391 return rv;
386 } 392 }
387 393
388 string consumer_t::canonicalize(const string& url) { 394 string consumer_t::canonicalize(const string& url) {
389 string rv = normalize(url); 395 string rv = normalize(url);
390 curl_t curl = curl_easy_init(); 396 curl_t curl = curl_easy_init();
391 if(!curl) 397 if(!curl)
392 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 398 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
393 string html; 399 string html;
394 CURLcode r; 400 CURLcode r;
395 (r=curl_misc_sets(curl)) 401 (r=curl_misc_sets(curl))
396 || (r=curl_easy_setopt(curl,CURLOPT_URL,rv.c_str())) 402 || (r=curl_easy_setopt(curl,CURLOPT_URL,rv.c_str()))
397 || (r=curl_easy_setopt(curl,CURLOPT_NOBODY,1)) 403 || (r=curl_easy_setopt(curl,CURLOPT_NOBODY,1))
398 ; 404 ;
399 if(r) 405 if(r)
400 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 406 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
401 r = curl_easy_perform(curl); 407 r = curl_easy_perform(curl);
402 if(r) 408 if(r)
403 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 409 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
404 const char *eu = 0; 410 const char *eu = 0;
405 r = curl_easy_getinfo(curl,CURLINFO_EFFECTIVE_URL,&eu); 411 r = curl_easy_getinfo(curl,CURLINFO_EFFECTIVE_URL,&eu);
406 if(r) 412 if(r)
407 throw exception_curl(OPKELE_CP_ "failed to curl_easy_getinfo(..CURLINFO_EFFECTIVE_URL..)",r); 413 throw exception_curl(OPKELE_CP_ "failed to curl_easy_getinfo(..CURLINFO_EFFECTIVE_URL..)",r);
408 rv = eu; 414 rv = eu;
409 return normalize(rv); 415 return normalize(rv);
410 } 416 }
411 417
412} 418}