summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--include/opkele/iterator.h4
-rw-r--r--lib/discovery.cc4
-rw-r--r--lib/message.cc2
-rw-r--r--lib/prequeue_rp.cc4
-rw-r--r--lib/secret.cc2
-rw-r--r--lib/util.cc6
6 files changed, 11 insertions, 11 deletions
diff --git a/include/opkele/iterator.h b/include/opkele/iterator.h
index 28c1c83..8f86234 100644
--- a/include/opkele/iterator.h
+++ b/include/opkele/iterator.h
@@ -62,155 +62,155 @@ namespace opkele {
virtual ~basic_forward_iterator_proxy_impl() { }
virtual basic_forward_iterator_proxy_impl<T,TR,TP>* dup() const = 0;
virtual bool operator==(const basic_forward_iterator_proxy_impl<T,TR,TP>& x) const = 0;
virtual bool operator!=(const basic_forward_iterator_proxy_impl<T,TR,TP>& x) const {
return !((*this)==x); }
virtual TR operator*() const = 0;
virtual TP operator->() const = 0;
virtual void advance() = 0;
};
template <typename IT>
class forward_iterator_proxy_impl : public basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer> {
public:
IT i;
forward_iterator_proxy_impl(const IT& _i) : i(_i) { }
virtual basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>* dup() const {
return new forward_iterator_proxy_impl<IT>(i); }
virtual bool operator==(const basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>& x) const {
return i==static_cast<const forward_iterator_proxy_impl<IT>*>(&x)->i; }
virtual bool operator!=(const basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>& x) const {
return i!=static_cast<const forward_iterator_proxy_impl<IT>*>(&x)->i; }
virtual typename IT::reference operator*() const { return *i; }
virtual typename IT::pointer operator->() const { return i.operator->(); }
virtual void advance() { ++i; }
};
template<typename T,typename TR=T&,typename TP=T*>
class forward_iterator_proxy : public iterator<forward_iterator_tag,T,void,TP,TR> {
public:
basic_forward_iterator_proxy_impl<T,TR,TP> *I;
template<typename IT>
forward_iterator_proxy(const IT& i)
: I(new forward_iterator_proxy_impl<IT>(i)) { }
forward_iterator_proxy(const forward_iterator_proxy<T,TR,TP>& x)
: I(x.I->dup()) { }
~forward_iterator_proxy() { delete I; }
forward_iterator_proxy& operator=(const forward_iterator_proxy<T,TR,TP>& x) {
delete I; I = x.I->dup(); }
bool operator==(const forward_iterator_proxy<T,TR,TP>& x) const {
return (*I)==(*(x.I)); }
bool operator!=(const forward_iterator_proxy<T,TR,TP>& x) const {
return (*I)!=(*(x.I)); }
TR operator*() const {
return **I; }
TP operator->() const {
return I->operator->(); }
forward_iterator_proxy<T,TR,TP>& operator++() {
I->advance(); return *this; }
forward_iterator_proxy<T,TR,TP>& operator++(int) {
forward_iterator_proxy<T,TR,TP> rv(*this);
I->advance(); return rv; }
};
template<typename IT>
class basic_filterator : public iterator<
typename IT::iterator_category,
typename IT::value_type,
typename IT::difference_type,
typename IT::pointer,
typename IT::reference> {
public:
IT it;
IT ei;
bool empty;
basic_filterator() : empty(true) { }
basic_filterator(const IT& _bi,const IT& _ei)
: it(_bi), ei(_ei) { empty = (it==ei); }
basic_filterator(const basic_filterator<IT>& x)
: it(x.it), ei(x.ei), empty(x.empty) { }
virtual ~basic_filterator() { }
bool operator==(const basic_filterator<IT>& x) const {
return empty?x.empty:(it==x.it); }
bool operator!=(const basic_filterator<IT>& x) const {
return empty!=x.empty || it!=x.it; }
typename IT::reference operator*() const {
assert(!empty);
return *it; }
typename IT::pointer operator->() const {
assert(!empty);
return it.operator->(); }
basic_filterator<IT>& operator++() {
bool found = false;
- for(++it;!(it==ei || (found=is_interesting()));++it);
+ for(++it;!(it==ei || (found=is_interesting()));++it) ;
if(!found) empty=true;
return *this;
}
basic_filterator<IT> operator++(int) {
basic_filterator<IT> rv(*this);
++(*this);
return rv;
}
void prepare() {
bool found = false;
- for(;!(it==ei || (found=is_interesting()));++it);
+ for(;!(it==ei || (found=is_interesting()));++it) ;
if(!found) empty = true;
}
virtual bool is_interesting() const = 0;
};
template<typename IT,typename T=typename IT::value_type::first_type,typename TR=T&,typename TP=T*>
class map_keys_iterator : public iterator<
typename IT::iterator_category,
T,void,TP,TR> {
public:
typedef map_keys_iterator<IT,T,TR,TP> self_type;
IT it;
IT ei;
bool empty;
map_keys_iterator() : empty(true) { }
map_keys_iterator(const IT& _bi,
const IT& _ei)
: it(_bi), ei(_ei) { empty = (it==ei); }
map_keys_iterator(const self_type& x)
: it(x.it), ei(x.ei), empty(x.empty) { }
bool operator==(const self_type& x) const {
return empty?x.empty:(it==x.it); }
bool operator!=(const self_type& x) const {
return empty!=x.empty || it!=x.it; }
TR operator*() const {
assert(!empty);
return it->first; }
TP operator->() const {
assert(!empty);
return &(it->first); }
self_type& operator++() {
assert(!empty);
empty=((++it)==ei); return *this; }
self_type operator++(int) {
self_type rv(*this);
++(*this); return rv; }
};
}
}
#endif /* __OPKELE_ITERATOR_H */
diff --git a/lib/discovery.cc b/lib/discovery.cc
index d1989ec..984e308 100644
--- a/lib/discovery.cc
+++ b/lib/discovery.cc
@@ -246,339 +246,339 @@ namespace opkele {
throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
if(!parser_choked) {
parse(0,0,true);
}else if(xmode&xmode_html){
/* TODO: do not bother if we've seen xml */
try {
util::tidy_doc_t td = util::tidy_doc_t::create();
if(!td)
throw exception_tidy(OPKELE_CP_ "failed to create htmltidy document");
#ifndef NDEBUG
td.opt_set(TidyQuiet,false);
td.opt_set(TidyShowWarnings,false);
#endif /* NDEBUG */
td.opt_set(TidyForceOutput,true);
td.opt_set(TidyXhtmlOut,true);
td.opt_set(TidyDoctypeMode,TidyDoctypeOmit);
td.opt_set(TidyMark,false);
td.opt_set(TidyNumEntities,true);
if(td.parse_string(save_html)<=0)
throw exception_tidy(OPKELE_CP_ "tidy failed to parse document");
if(td.clean_and_repair()<=0)
throw exception_tidy(OPKELE_CP_ "tidy failed to clean and repair");
util::tidy_buf_t tide;
if(td.save_buffer(tide)<=0)
throw exception_tidy(OPKELE_CP_ "tidy failed to save buffer");
prepare_to_parse();
parse(tide.c_str(),tide.size(),true);
}catch(exception_tidy& et) { }
}
save_html.clear();
}
void prepare_to_parse() {
(*(expat_t*)this) = parser_create_ns();
set_user_data(); set_element_handler();
set_character_data_handler();
if(xmode&xmode_html) {
html_openid1.clear(); html_openid2.clear();
parser_choked = false;
}
cdata = 0; xrd_service = 0; skipping = 0;
pt_stack.clear();
status_code = 100; status_string.clear();
}
void html2xrd(endpoint_discovery_iterator& oi,idiscovery_t& id) {
XRD_t& x = id.xrd;
if(!html_openid2.uris.empty()) {
html_openid2.types.insert(STURI_OPENID20);
x.services.add(-1,html_openid2);
queue_endpoints(oi,id,&op_service_types[st_index_2]);
}
if(!html_openid1.uris.empty()) {
html_openid1.types.insert(STURI_OPENID11);
x.services.add(-1,html_openid1);
queue_endpoints(oi,id,&op_service_types[st_index_1]);
}
}
size_t write(void *p,size_t s,size_t nm) {
/* TODO: limit total size */
size_t bytes = s*nm;
const char *inbuf = (const char*)p;
if(xmode&xmode_html) {
size_t mbts = save_html.capacity()-save_html.size();
size_t bts = 0;
if(mbts>0) {
bts = (bytes>mbts)?mbts:bytes;
save_html.append(inbuf,bts);
}
if(skipping<0) return bts;
}
if(skipping<0) return 0;
bool rp = parse(inbuf,bytes,false);
if(!rp) {
parser_choked = true;
skipping = -1;
if(!(xmode&xmode_html))
bytes = 0;
}
return bytes;
}
size_t header(void *p,size_t s,size_t nm) {
size_t bytes = s*nm;
const char *h = (const char*)p;
const char *colon = (const char*)memchr(p,':',bytes);
const char *space = (const char*)memchr(p,' ',bytes);
if(space && ( (!colon) || space<colon ) ) {
xrds_location.clear(); http_content_type.clear();
}else if(colon) {
const char *hv = ++colon;
size_t hnl = colon-h;
int rb;
- for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb);
+ for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb) ;
while(rb>0 && isspace(hv[rb-1])) --rb;
if(rb) {
if( (hnl>=sizeof(XRDS_HEADER))
&& !strncasecmp(h,XRDS_HEADER":",
sizeof(XRDS_HEADER)) ) {
xrds_location.assign(hv,rb);
}else if( (hnl>=sizeof(CT_HEADER))
&& !strncasecmp(h,CT_HEADER":",
sizeof(CT_HEADER)) ) {
const char *sc = (const char*)memchr(
hv,';',rb);
http_content_type.assign(hv,sc?(sc-hv):rb);
}
}
}
return curl_t::header(p,s,nm);
}
void start_element(const XML_Char *n,const XML_Char **a) {
if(skipping<0) return;
if(skipping) {
if(xmode&xmode_html)
html_start_element(n,a);
++skipping; return;
}
if(pt_stack.empty()) {
if(is_qelement(n,NSURI_XRDS "\tXRDS"))
return;
if(is_qelement(n,NSURI_XRD "\tXRD")) {
assert(xrd);
xrd->clear();
pt_stack.push_back(n);
}else if(xmode&xmode_html) {
html_start_element(n,a);
}else{
skipping = -1;
}
}else{
int pt_s = pt_stack.size();
if(pt_s==1) {
if(is_qelement(n,NSURI_XRD "\tCanonicalID")) {
assert(xrd);
cdata = &(xrd->canonical_ids.add(element_priority(a),string()));
}else if(is_qelement(n,NSURI_XRD "\tLocalID")) {
assert(xrd);
cdata = &(xrd->local_ids.add(element_priority(a),string()));
}else if(is_qelement(n,NSURI_XRD "\tProviderID")) {
assert(xrd);
cdata = &(xrd->provider_id);
}else if(is_qelement(n,NSURI_XRD "\tService")) {
assert(xrd);
xrd_service = &(xrd->services.add(element_priority(a),
service_t()));
pt_stack.push_back(n);
}else if(is_qelement(n,NSURI_XRD "\tStatus")) {
for(;*a;) {
if(!strcasecmp(*(a++),"code")) {
if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) {
cdata = &status_string;
pt_stack.push_back(n);
break;
}
}else
++a;
}
}else if(is_qelement(n,NSURI_XRD "\tExpires")) {
assert(xrd);
cdata_buf.clear();
cdata = &cdata_buf;
}else if(xmode&xmode_html) {
html_start_element(n,a);
}else{
skipping = 1;
}
}else if(pt_s==2) {
if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) {
if(is_qelement(n,NSURI_XRD "\tType")) {
assert(xrd); assert(xrd_service);
cdata_buf.clear();
cdata = &cdata_buf;
}else if(is_qelement(n,NSURI_XRD "\tURI")) {
assert(xrd); assert(xrd_service);
const char *append = element_attr(a,"append");
xrd::uri_t& uri = xrd_service->uris.add(element_priority(a),xrd::uri_t("",append?append:""));
cdata = &uri.uri;
}else if(is_qelement(n,NSURI_XRD "\tLocalID")
|| is_qelement(n,NSURI_OPENID10 "\tDelegate") ) {
assert(xrd); assert(xrd_service);
cdata = &(xrd_service->local_ids.add(element_priority(a),string()));
}else if(is_qelement(n,NSURI_XRD "\tProviderID")) {
assert(xrd); assert(xrd_service);
cdata = &(xrd_service->provider_id);
}else{
skipping = 1;
}
}else
skipping = 1;
}else if(xmode&xmode_html) {
html_start_element(n,a);
}else{
skipping = 1;
}
}
}
void end_element(const XML_Char *n) {
if(skipping<0) return;
if(skipping) {
--skipping; return;
}
if(is_qelement(n,NSURI_XRD "\tType")) {
assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf);
xrd_service->types.insert(cdata_buf);
}else if(is_qelement(n,NSURI_XRD "\tService")) {
assert(xrd); assert(xrd_service);
assert(!pt_stack.empty());
assert(pt_stack.back()==(NSURI_XRD "\tService"));
pt_stack.pop_back();
xrd_service = 0;
}else if(is_qelement(n,NSURI_XRD "\tStatus")) {
assert(xrd);
if(is_qelement(pt_stack.back().c_str(),n)) {
assert(cdata==&status_string);
pt_stack.pop_back();
if(status_code!=100)
skipping = -1;
}
}else if(is_qelement(n,NSURI_XRD "\tExpires")) {
assert(xrd);
xrd->expires = util::w3c_to_time(cdata_buf);
}else if((xmode&xmode_html) && is_element(n,"head")) {
skipping = -1;
}
cdata = 0;
}
void character_data(const XML_Char *s,int l) {
if(skipping) return;
if(cdata) cdata->append(s,l);
}
void html_start_element(const XML_Char *n,const XML_Char **a) {
if(is_element(n,"meta")) {
bool heq = false;
string l;
for(;*a;a+=2) {
if(!( strcasecmp(a[0],"http-equiv")
|| strcasecmp(a[1],XRDS_HEADER) ))
heq = true;
else if(!strcasecmp(a[0],"content"))
l.assign(a[1]);
}
if(heq)
xrds_location = l;
}else if(is_element(n,"link")) {
string rels;
string href;
for(;*a;a+=2) {
if( !strcasecmp(a[0],"rel") ) {
rels.assign(a[1]);
}else if( !strcasecmp(a[0],"href") ) {
const char *ns = a[1];
- for(;*ns && isspace(*ns);++ns);
+ for(;*ns && isspace(*ns);++ns) ;
href.assign(ns);
string::size_type lns=href.find_last_not_of(data::_whitespace_chars);
href.erase(lns+1);
}
}
for(string::size_type ns=rels.find_first_not_of(data::_whitespace_chars);
ns!=string::npos; ns=rels.find_first_not_of(data::_whitespace_chars,ns)) {
string::size_type s = rels.find_first_of(data::_whitespace_chars,ns);
string rel;
if(s==string::npos) {
rel.assign(rels,ns,string::npos);
ns = string::npos;
}else{
rel.assign(rels,ns,s-ns);
ns = s;
}
if(rel=="openid.server")
html_openid1.uris.add(-1,xrd::uri_t(href));
else if(rel=="openid.delegate")
html_openid1.local_ids.add(-1,href);
else if(rel=="openid2.provider")
html_openid2.uris.add(-1,xrd::uri_t(href));
else if(rel=="openid2.local_id")
html_openid2.local_ids.add(-1,href);
}
}else if(is_element(n,"body")) {
skipping = -1;
}
}
void queue_endpoints(endpoint_discovery_iterator& oi,
const idiscovery_t &id,
const service_type_t *st) {
openid_endpoint_t ep;
ep.claimed_id = id.canonicalized_id;
for(xrd::services_t::const_iterator isvc=id.xrd.services.begin();
isvc!=id.xrd.services.end(); ++isvc) {
const xrd::service_t svc = isvc->second;
if(svc.types.find(st->uri)==svc.types.end()) continue;
for(xrd::uris_t::const_iterator iu=svc.uris.begin();iu!=svc.uris.end();++iu) {
ep.uri = iu->second.uri;
if(id.xri_identity) {
if(iu->second.append=="qxri") {
ep.uri += id.normalized_id;
} /* TODO: else handle other append attribute values */
}
if(st->forceid) {
ep.local_id = ep.claimed_id = st->forceid;
*(oi++) = ep;
}else{
if(svc.local_ids.empty()) {
ep.local_id = ep.claimed_id;
*(oi++) = ep;
}else{
for(xrd::local_ids_t::const_iterator ilid=svc.local_ids.begin();
ilid!=svc.local_ids.end(); ++ilid) {
ep.local_id = ilid->second;
*(oi++) = ep;
}
}
}
}
}
}
};
string idiscover(endpoint_discovery_iterator oi,const string& identity) {
idigger_t idigger;
return idigger.discover(oi,identity);
}
void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) try {
idigger_t idigger;
idigger.yadiscover(oi,yurl,types,redirs);
}catch(exception_curl& ec) {
if(redirs || ec._error!=CURLE_TOO_MANY_REDIRECTS)
throw;
}
}
diff --git a/lib/message.cc b/lib/message.cc
index 524946a..c1f8088 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -65,132 +65,132 @@ namespace opkele {
}
if(lb>co)
set_field(kv.substr(p,co-p),kv.substr(co+1,lb-co-1));
string::size_type nolb = kv.find_first_not_of("\r\n",lb);
if(nolb==string::npos)
break;
p = nolb;
#endif /* POSTELS_LAW */
}
}
struct __om_kv_outputter : public unary_function<const string&,void> {
public:
const basic_openid_message& om;
ostream& os;
__om_kv_outputter(const basic_openid_message& m,ostream& s)
: om(m), os(s) { }
result_type operator()(argument_type f) {
os << f << ':' << om.get_field(f) << '\n';
}
};
void basic_openid_message::to_keyvalues(ostream& o) const {
for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o));
}
struct __om_html_outputter : public unary_function<const string&,void> {
public:
const basic_openid_message& om;
ostream& os;
const char *pfx;
__om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0)
: om(m), os(s), pfx(p) { }
result_type operator()(argument_type f) {
os <<
"<input type=\"hidden\""
" name=\"";
if(pfx)
os << util::attr_escape(pfx);
os << util::attr_escape(f) << "\""
" value=\"" << util::attr_escape(om.get_field(f)) << "\" />";
}
};
void basic_openid_message::to_htmlhiddens(ostream& o,const char* pfx) const {
for_each(fields_begin(),fields_end(),__om_html_outputter(*this,o,pfx));
}
void basic_openid_message::add_to_signed(const string& fields) {
string::size_type fnc = fields.find_first_not_of(",");
if(fnc==string::npos)
throw bad_input(OPKELE_CP_ "Trying to add nothing in particular to the list of signed fields");
string signeds;
try {
signeds = get_field("signed");
string::size_type lnc = signeds.find_last_not_of(",");
if(lnc==string::npos)
signeds.assign(fields,fnc,fields.size()-fnc);
else{
string::size_type ss = signeds.size();
if(lnc==(ss-1)) {
signeds+= ',';
signeds.append(fields,fnc,fields.size()-fnc);
}else{
if(lnc<(ss-2))
signeds.replace(lnc+2,ss-lnc-2,
fields,fnc,fields.size()-fnc);
else
signeds.append(fields,fnc,fields.size()-fnc);
}
}
}catch(failed_lookup&) {
signeds.assign(fields,fnc,fields.size()-fnc);
}
set_field("signed",signeds);
}
string basic_openid_message::find_ns(const string& uri,const char *pfx) const {
try {
return get_ns(uri);
}catch(failed_lookup&) {
return pfx;
}
}
string basic_openid_message::allocate_ns(const string& uri,const char *pfx) {
if(!has_field("ns"))
return pfx;
if(has_ns(uri))
throw bad_input(OPKELE_CP_ "OpenID message already contains namespace");
string rv = pfx;
if(has_field("ns."+rv)) {
string::reference c=rv[rv.length()];
- for(c='a';c<='z' && has_field("ns."+rv);++c);
+ for(c='a';c<='z' && has_field("ns."+rv);++c) ;
if(c=='z')
throw exception(OPKELE_CP_ "Failed to allocate namespace");
}
set_field("ns."+rv,uri);
return rv;
}
bool openid_message_t::has_field(const string& n) const {
return find(n)!=end();
}
const string& openid_message_t::get_field(const string& n) const {
const_iterator i=find(n);
if(i==end())
throw failed_lookup(OPKELE_CP_ n+": no such field");
return i->second;
}
openid_message_t::fields_iterator openid_message_t::fields_begin() const {
return util::map_keys_iterator<const_iterator,string,const string&,const string*>(begin(),end());
}
openid_message_t::fields_iterator openid_message_t::fields_end() const {
return util::map_keys_iterator<const_iterator,string,const string&,const string*>(end(),end());
}
void openid_message_t::reset_fields() {
clear();
}
void openid_message_t::set_field(const string& n,const string& v) {
(*this)[n]=v;
}
void openid_message_t::reset_field(const string& n) {
erase(n);
}
}
diff --git a/lib/prequeue_rp.cc b/lib/prequeue_rp.cc
index e499d08..886efae 100644
--- a/lib/prequeue_rp.cc
+++ b/lib/prequeue_rp.cc
@@ -1,86 +1,86 @@
#include <iostream>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <opkele/exception.h>
#include <opkele/prequeue_rp.h>
#include <opkele/discovery.h>
#include <opkele/uris.h>
#include <opkele/data.h>
#include <opkele/util.h>
#include <opkele/curl.h>
#include <opkele/debug.h>
namespace opkele {
class __OP_verifier_good_input : public exception {
public:
__OP_verifier_good_input(OPKELE_E_PARS)
: exception(OPKELE_E_CONS) { }
};
class OP_verifier : public iterator<output_iterator_tag,openid_endpoint_t,void> {
public:
const string& OP;
const string& id;
OP_verifier(const string& o,const string& i)
: OP(o), id(i) { }
OP_verifier& operator*() { return *this; }
OP_verifier& operator=(const openid_endpoint_t& oep) {
if(oep.uri==OP) {
if(oep.claimed_id==IDURI_SELECT20
|| oep.local_id==IDURI_SELECT20 )
throw bad_input(OPKELE_CP_ "claimed_id is an OP-Id");
if(oep.local_id==id)
throw __OP_verifier_good_input(OPKELE_CP_ "Found corresponding endpoint");
}
return *this;
}
OP_verifier& operator++() { return *this; }
OP_verifier& operator++(int) { return *this; }
};
- void prequeue_RP::verify_OP(const string& OP,const string& claimed_id,const string& id) const {
+ void prequeue_RP::verify_OP(const string& OP,const string& _claimed_id,const string& id) const {
try {
- discover(OP_verifier(OP,id),claimed_id);
+ discover(OP_verifier(OP,id),_claimed_id);
throw id_res_unauthorized(OPKELE_CP_
"OP is not authorized to make an assertion regarding the identity");
}catch(__OP_verifier_good_input& ovgi) {
}
}
class endpoint_queuer : public iterator<output_iterator_tag,openid_endpoint_t,void> {
public:
prequeue_RP& rp;
endpoint_queuer(prequeue_RP& r) : rp(r) { }
endpoint_queuer& operator*() { return *this; }
endpoint_queuer& operator=(const openid_endpoint_t& oep) {
rp.queue_endpoint(oep); return *this; }
endpoint_queuer& operator++() { return *this; }
endpoint_queuer& operator++(int) { return *this; }
};
void prequeue_RP::initiate(const string& usi) {
begin_queueing();
set_normalized_id( discover(endpoint_queuer(*this),usi) );
end_queueing();
}
void prequeue_RP::set_normalized_id(const string&) {
}
const string prequeue_RP::get_normalized_id() const {
throw not_implemented(OPKELE_CP_ "get_normalized_id() is not implemented");
}
const string prequeue_RP::discover(openid_endpoint_output_iterator it,
const string& id) const {
return idiscover(it,id);
}
}
diff --git a/lib/secret.cc b/lib/secret.cc
index d538890..3f1e39c 100644
--- a/lib/secret.cc
+++ b/lib/secret.cc
@@ -1,45 +1,45 @@
#include <algorithm>
#include <functional>
#include <opkele/types.h>
#include <opkele/exception.h>
#include <opkele/util.h>
namespace opkele {
using namespace std;
template<class __a1,class __a2,class __r>
struct bitwise_xor : public binary_function<__a1,__a2,__r> {
__r operator() (const __a1& a1,const __a2& a2) const {
- return a1^a2;
+ return (__r)(a1^a2);
}
};
void secret_t::enxor_to_base64(const unsigned char *key_d,string& rv) const {
vector<unsigned char> tmp;
transform(
begin(), end(),
key_d,
back_insert_iterator<vector<unsigned char> >(tmp),
bitwise_xor<unsigned char,unsigned char,unsigned char>() );
rv = util::encode_base64(&(tmp.front()),tmp.size());
}
void secret_t::enxor_from_base64(const unsigned char *key_d,const string& b64) {
clear();
util::decode_base64(b64,*this);
transform(
begin(), end(),
key_d,
begin(),
bitwise_xor<unsigned char,unsigned char,unsigned char>() );
}
void secret_t::to_base64(string& rv) const {
rv = util::encode_base64(&(front()),size());
}
void secret_t::from_base64(const string& b64) {
util::decode_base64(b64,*this);
}
}
diff --git a/lib/util.cc b/lib/util.cc
index b702291..d979502 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -118,333 +118,333 @@ namespace opkele {
throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()");
char rv[25];
if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t))
throw failed_conversion(OPKELE_CP_ "failed to strftime()");
return rv;
}
time_t w3c_to_time(const string& w) {
int fraction;
struct tm tm_t;
memset(&tm_t,0,sizeof(tm_t));
if( (
sscanf(
w.c_str(),
"%04d-%02d-%02dT%02d:%02d:%02dZ",
&tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday,
&tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec
) != 6
) && (
sscanf(
w.c_str(),
"%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
&tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday,
&tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec,
&fraction
) != 7
) )
throw failed_conversion(OPKELE_CP_ "failed to sscanf()");
tm_t.tm_mon--;
tm_t.tm_year-=1900;
time_t rv = mktime(&tm_t);
if(rv==(time_t)-1)
throw failed_conversion(OPKELE_CP_ "failed to mktime()");
return rv-timezone;
}
/*
*
*/
static inline bool isrfc3986unreserved(int c) {
if(c<'-') return false;
if(c<='.') return true;
if(c<'0') return false; if(c<='9') return true;
if(c<'A') return false; if(c<='Z') return true;
if(c<'_') return false;
if(c=='_') return true;
if(c<'a') return false; if(c<='z') return true;
if(c=='~') return true;
return false;
}
struct __url_encoder : public unary_function<char,void> {
public:
string& rv;
__url_encoder(string& r) : rv(r) { }
result_type operator()(argument_type c) {
if(isrfc3986unreserved(c))
rv += c;
else{
char tmp[4];
snprintf(tmp,sizeof(tmp),"%%%02X",
(c&0xff));
rv += tmp;
}
}
};
string url_encode(const string& str) {
string rv;
for_each(str.begin(),str.end(),
__url_encoder(rv));
return rv;
}
string url_decode(const string& str) {
string rv;
back_insert_iterator<string> ii(rv);
for(string::const_iterator i=str.begin(),ie=str.end();
i!=ie;++i) {
switch(*i) {
case '+':
*(ii++) = ' '; break;
case '%':
++i;
static char tmp[3] = {0,0,0};
if(i==ie)
throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string");
tmp[0] = *(i++);
if(i==ie)
throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string");
tmp[1] = *i;
if(!(isxdigit(tmp[0]) && isxdigit(tmp[1])))
throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string");
- *(ii++) = strtol(tmp,0,16);
+ *(ii++) = (char)strtol(tmp,0,16);
break;
default:
*(ii++) = *i; break;
}
}
return rv;
}
string attr_escape(const string& str) {
static const char *unsafechars = "<>&\n\"'";
string rv;
string::size_type p=0;
while(true) {
string::size_type us = str.find_first_of(unsafechars,p);
if(us==string::npos) {
if(p!=str.length())
rv.append(str,p,str.length()-p);
return rv;
}
rv.append(str,p,us-p);
rv += "&#";
rv += long_to_string((long)str[us]);
rv += ';';
p = us+1;
}
}
string long_to_string(long l) {
char rv[32];
int r=snprintf(rv,sizeof(rv),"%ld",l);
if(r<0 || r>=(int)sizeof(rv))
throw failed_conversion(OPKELE_CP_ "failed to snprintf()");
return rv;
}
long string_to_long(const string& s) {
char *endptr = 0;
long rv = strtol(s.c_str(),&endptr,10);
if((!endptr) || endptr==s.c_str())
throw failed_conversion(OPKELE_CP_ "failed to strtol()");
return rv;
}
/*
* Normalize URL according to the rules, described in rfc 3986, section 6
*
* - uppercase hex triplets (e.g. %ab -> %AB)
* - lowercase scheme and host
* - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3,
* that is - [:alpha:][:digit:]._~-
* - remove dot segments
* - remove empty and default ports
* - if there's no path component, add '/'
*/
string rfc_3986_normalize_uri(const string& uri) {
string rv;
string::size_type ns = uri.find_first_not_of(data::_whitespace_chars);
if(ns==string::npos)
throw bad_input(OPKELE_CP_ "Can't normalize empty URI");
string::size_type colon = uri.find(':',ns);
if(colon==string::npos)
throw bad_input(OPKELE_CP_ "No scheme specified in URI");
transform(
uri.begin()+ns, uri.begin()+colon+1,
back_inserter(rv), ::tolower );
bool s;
string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1;
if(ul <= (colon+3))
throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered");
if(uri[colon+1]!='/' || uri[colon+2]!='/')
throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component");
if(rv=="http:")
s = false;
else if(rv=="https:")
s = true;
else{
/* TODO: support more schemes. e.g. xri. How do we normalize
* xri?
*/
rv.append(uri,colon+1,ul-colon-1);
return rv;
}
rv += "//";
string::size_type interesting = uri.find_first_of(":/#?",colon+3);
if(interesting==string::npos) {
transform(
uri.begin()+colon+3,uri.begin()+ul,
back_inserter(rv), ::tolower );
rv += '/'; return rv;
}
transform(
uri.begin()+colon+3,uri.begin()+interesting,
back_inserter(rv), ::tolower );
bool qf = false;
char ic = uri[interesting];
if(ic==':') {
string::size_type ni = uri.find_first_of("/#?%",interesting+1);
const char *nptr = uri.data()+interesting+1;
char *eptr = 0;
long port = strtol(nptr,&eptr,10);
if( (port>0) && (port<65535) && port!=(s?443:80) ) {
char tmp[8];
snprintf(tmp,sizeof(tmp),":%ld",port);
rv += tmp;
}
if(ni==string::npos) {
rv += '/'; return rv;
}
interesting = ni;
}else if(ic!='/') {
rv += '/'; rv += ic;
qf = true;
++interesting;
}
string::size_type n = interesting;
char tmp[3] = { 0,0,0 };
stack<string::size_type> psegs; psegs.push(rv.length());
string pseg;
for(;n<ul;) {
string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n);
if(unsafe==string::npos) {
pseg.append(uri,n,ul-n-1); n = ul-1;
}else{
pseg.append(uri,n,unsafe-n);
n = unsafe;
}
char c = uri[n++];
if(c=='%') {
if((n+1)>=ul)
throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character");
tmp[0] = uri[n++];
tmp[1] = uri[n++];
if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) ))
throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized");
int cc = strtol(tmp,0,16);
if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) )
- pseg += cc;
+ pseg += (char)cc;
else{
pseg += '%';
- pseg += toupper(tmp[0]); pseg += toupper(tmp[1]);
+ pseg += (char)toupper(tmp[0]); pseg += (char)toupper(tmp[1]);
}
}else if(qf) {
rv += pseg; rv += c;
pseg.clear();
}else if(n>=ul || strchr("?/#",c)) {
if(pseg.empty() || pseg==".") {
}else if(pseg=="..") {
if(psegs.size()>1) {
rv.resize(psegs.top()); psegs.pop();
}
}else{
psegs.push(rv.length());
if(c!='/') {
pseg += c;
qf = true;
}
rv += '/'; rv += pseg;
}
if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) {
rv += '/';
if(n<ul)
qf = true;
}else if(strchr("?#",c)) {
if(psegs.size()==1 && psegs.top()==rv.length())
rv += '/';
if(pseg.empty())
rv += c;
qf = true;
}
pseg.clear();
}else{
pseg += c;
}
}
if(!pseg.empty()) {
if(!qf) rv += '/';
rv += pseg;
}
return rv;
}
string& strip_uri_fragment_part(string& u) {
string::size_type q = u.find('?'), f = u.find('#');
if(q==string::npos) {
if(f!=string::npos)
u.erase(f);
}else{
if(f!=string::npos) {
if(f<q)
u.erase(f,q-f);
else
u.erase(f);
}
}
return u;
}
bool uri_matches_realm(const string& uri,const string& realm) {
string nrealm = opkele::util::rfc_3986_normalize_uri(realm);
string nu = opkele::util::rfc_3986_normalize_uri(uri);
string::size_type pr = nrealm.find("://");
string::size_type pu = nu.find("://");
assert(!(pr==string::npos || pu==string::npos));
pr += sizeof("://")-1;
pu += sizeof("://")-1;
if(!strncmp(nrealm.c_str()+pr,"*.",2)) {
pr = nrealm.find('.',pr);
pu = nu.find('.',pu);
assert(pr!=string::npos);
if(pu==string::npos)
return false;
// TODO: check for overgeneralized realm
}
string::size_type lr = nrealm.length();
string::size_type lu = nu.length();
if( (lu-pu) < (lr-pr) )
return false;
pair<const char*,const char*> mp = mismatch(
nrealm.c_str()+pr,nrealm.c_str()+lr,
nu.c_str()+pu);
if( (*(mp.first-1))!='/'
&& !strchr("/?#",*mp.second) )
return false;
return true;
}
string abi_demangle(const char *mn) {
#ifndef HAVE_DEMANGLE
return mn;
#else /* !HAVE_DEMANGLE */
int dstat;
char *demangled = abi::__cxa_demangle(mn,0,0,&dstat);
if(dstat)
return mn;
string rv = demangled;
free(demangled);