2 * This is an implementation of OpenID 2.0 RELYING PARTY SUPPORT CURRENTLY B0RKEN AND BEING DEVEL0PZ0RED
4 * Copyright (c) 2007-2011 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
35 # include <sys/time.h>
44 #include <curl/curl.h>
46 #include "ctdl_module.h"
48 #include "citserver.h"
51 typedef struct _ctdl_openid {
58 void Free_ctdl_openid(ctdl_openid **FreeMe)
62 FreeStrBuf(&(*FreeMe)->claimed_id);
63 FreeStrBuf(&(*FreeMe)->server);
64 DeleteHash(&(*FreeMe)->sreg_keys);
71 * This cleanup function blows away the temporary memory used by this module.
73 void openid_cleanup_function(void) {
74 struct CitContext *CCC = CC; /* CachedCitContext - performance boost */
76 if (CCC->openid_data != NULL) {
77 syslog(LOG_DEBUG, "Clearing OpenID session state");
78 Free_ctdl_openid((ctdl_openid **) &CCC->openid_data);
83 /**************************************************************************/
85 /* Functions in this section handle Citadel internal OpenID mapping stuff */
87 /**************************************************************************/
91 * The structure of an openid record *key* is:
93 * |--------------claimed_id-------------|
94 * (actual length of claimed id)
97 * The structure of an openid record *value* is:
99 * |-----user_number----|------------claimed_id---------------|
100 * (sizeof long) (actual length of claimed id)
107 * Attach an OpenID to a Citadel account
109 int attach_openid(struct ctdluser *who, StrBuf *claimed_id)
111 struct cdbdata *cdboi;
112 long fetched_usernum;
118 if (StrLength(claimed_id)==0) return(1);
120 /* Check to see if this OpenID is already in the database */
122 cdboi = cdb_fetch(CDB_OPENID, ChrPtr(claimed_id), StrLength(claimed_id));
124 memcpy(&fetched_usernum, cdboi->ptr, sizeof(long));
127 if (fetched_usernum == who->usernum) {
128 syslog(LOG_INFO, "%s already associated; no action is taken", ChrPtr(claimed_id));
132 syslog(LOG_INFO, "%s already belongs to another user", ChrPtr(claimed_id));
137 /* Not already in the database, so attach it now */
139 data_len = sizeof(long) + StrLength(claimed_id) + 1;
140 data = malloc(data_len);
142 memcpy(data, &who->usernum, sizeof(long));
143 memcpy(&data[sizeof(long)], ChrPtr(claimed_id), StrLength(claimed_id) + 1);
145 cdb_store(CDB_OPENID, ChrPtr(claimed_id), StrLength(claimed_id), data, data_len);
148 snprintf(buf, sizeof buf, "User <%s> (#%ld) has claimed the OpenID URL %s\n",
149 who->fullname, who->usernum, ChrPtr(claimed_id));
150 CtdlAideMessage(buf, "OpenID claim");
151 syslog(LOG_INFO, "%s", buf);
158 * When a user is being deleted, we have to delete any OpenID associations
160 void openid_purge(struct ctdluser *usbuf) {
161 struct cdbdata *cdboi;
162 HashList *keys = NULL;
164 char *deleteme = NULL;
170 keys = NewHash(1, NULL);
173 cdb_rewind(CDB_OPENID);
174 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
175 if (cdboi->len > sizeof(long)) {
176 memcpy(&usernum, cdboi->ptr, sizeof(long));
177 if (usernum == usbuf->usernum) {
178 deleteme = strdup(cdboi->ptr + sizeof(long)),
179 Put(keys, deleteme, strlen(deleteme), deleteme, NULL);
185 /* Go through the hash list, deleting keys we stored in it */
187 HashPos = GetNewHashPos(keys, 0);
188 while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
190 syslog(LOG_DEBUG, "Deleting associated OpenID <%s>", (char*)Value);
191 cdb_delete(CDB_OPENID, Value, strlen(Value));
192 /* note: don't free(Value) -- deleting the hash list will handle this for us */
194 DeleteHashPos(&HashPos);
200 * List the OpenIDs associated with the currently logged in account
202 void cmd_oidl(char *argbuf) {
203 struct cdbdata *cdboi;
206 if (CtdlAccessCheck(ac_logged_in)) return;
207 cdb_rewind(CDB_OPENID);
208 cprintf("%d Associated OpenIDs:\n", LISTING_FOLLOWS);
210 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
211 if (cdboi->len > sizeof(long)) {
212 memcpy(&usernum, cdboi->ptr, sizeof(long));
213 if (usernum == CC->user.usernum) {
214 cprintf("%s\n", cdboi->ptr + sizeof(long));
224 * List ALL OpenIDs in the database
226 void cmd_oida(char *argbuf) {
227 struct cdbdata *cdboi;
229 struct ctdluser usbuf;
231 if (CtdlAccessCheck(ac_aide)) return;
232 cdb_rewind(CDB_OPENID);
233 cprintf("%d List of all OpenIDs in the database:\n", LISTING_FOLLOWS);
235 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
236 if (cdboi->len > sizeof(long)) {
237 memcpy(&usernum, cdboi->ptr, sizeof(long));
238 if (CtdlGetUserByNumber(&usbuf, usernum) != 0) {
239 usbuf.fullname[0] = 0;
241 cprintf("%s|%ld|%s\n",
242 cdboi->ptr + sizeof(long),
254 * Attempt to register (populate the vCard) the currently-logged-in user
255 * using the data from Simple Registration Extension, if present.
257 void populate_vcard_from_sreg(HashList *sreg_keys) {
260 int pop = 0; /* number of fields populated */
262 char *postcode = NULL;
263 char *country = NULL;
265 if (!sreg_keys) return;
269 if (GetHash(sreg_keys, "identity", 8, (void *) &data)) {
270 vcard_add_prop(v, "url;type=openid", data);
274 if (GetHash(sreg_keys, "sreg.email", 10, (void *) &data)) {
275 vcard_add_prop(v, "email;internet", data);
279 if (GetHash(sreg_keys, "sreg.nickname", 13, (void *) &data)) {
280 vcard_add_prop(v, "nickname", data);
284 if (GetHash(sreg_keys, "sreg.fullname", 13, (void *) &data)) {
286 vcard_add_prop(v, "fn", data);
287 vcard_fn_to_n(n, data, sizeof n);
288 vcard_add_prop(v, "n", n);
292 if (!GetHash(sreg_keys, "sreg.postcode", 13, (void *) &postcode)) {
296 if (!GetHash(sreg_keys, "sreg.country", 12, (void *) &country)) {
300 if (postcode || country) {
302 snprintf(adr, sizeof adr, ";;;;;%s;%s",
303 (postcode ? postcode : ""),
304 (country ? country : "")
306 vcard_add_prop(v, "adr", adr);
310 if (GetHash(sreg_keys, "sreg.dob", 8, (void *) &data)) {
311 vcard_add_prop(v, "bday", data);
315 if (GetHash(sreg_keys, "sreg.gender", 11, (void *) &data)) {
316 vcard_add_prop(v, "x-funambol-gender", data);
320 /* Only save the vCard if there is some useful data in it */
323 ser = vcard_serialize(v);
325 CtdlWriteObject(USERCONFIGROOM, "text/x-vcard",
326 ser, strlen(ser)+1, &CC->user, 0, 0, 0
336 * Create a new user account, manually specifying the name, after successfully
337 * verifying an OpenID (which will of course be attached to the account)
339 void cmd_oidc(char *argbuf) {
340 ctdl_openid *oiddata = (ctdl_openid *) CC->openid_data;
343 cprintf("%d You have not verified an OpenID yet.\n", ERROR);
347 if (!oiddata->verified) {
348 cprintf("%d You have not verified an OpenID yet.\n", ERROR);
352 /* We can make the semantics of OIDC exactly the same as NEWU, simply
353 * by _calling_ cmd_newu() and letting it run. Very clever!
357 /* Now, if this logged us in, we have to attach the OpenID */
359 attach_openid(&CC->user, oiddata->claimed_id);
360 if (oiddata->sreg_keys != NULL) {
361 populate_vcard_from_sreg(oiddata->sreg_keys);
369 * Detach an OpenID from the currently logged in account
371 void cmd_oidd(char *argbuf) {
372 struct cdbdata *cdboi;
373 char id_to_detach[1024];
374 int this_is_mine = 0;
377 if (CtdlAccessCheck(ac_logged_in)) return;
378 extract_token(id_to_detach, argbuf, 0, '|', sizeof id_to_detach);
379 if (IsEmptyStr(id_to_detach)) {
380 cprintf("%d An empty OpenID URL is not allowed.\n", ERROR + ILLEGAL_VALUE);
383 cdb_rewind(CDB_OPENID);
384 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
385 if (cdboi->len > sizeof(long)) {
386 memcpy(&usernum, cdboi->ptr, sizeof(long));
387 if (usernum == CC->user.usernum) {
395 cprintf("%d That OpenID was not found or not associated with your account.\n",
396 ERROR + ILLEGAL_VALUE);
400 cdb_delete(CDB_OPENID, id_to_detach, strlen(id_to_detach));
401 cprintf("%d %s detached from your account.\n", CIT_OK, id_to_detach);
407 * Attempt to auto-create a new Citadel account using the nickname from Simple Registration Extension
409 int openid_create_user_via_sreg(StrBuf *claimed_id, HashList *sreg_keys)
411 char *desired_name = NULL;
412 char new_password[32];
415 if (config.c_auth_mode != AUTHMODE_NATIVE) return(1);
416 if (config.c_disable_newu) return(2);
417 if (CC->logged_in) return(3);
418 if (!GetHash(sreg_keys, "sreg.nickname", 13, (void *) &desired_name)) return(4);
420 syslog(LOG_DEBUG, "The desired account name is <%s>", desired_name);
422 len = cutuserkey(desired_name);
423 if (!CtdlGetUser(&CC->user, desired_name)) {
424 syslog(LOG_DEBUG, "<%s> is already taken by another user.", desired_name);
425 memset(&CC->user, 0, sizeof(struct ctdluser));
429 /* The desired account name is available. Create the account and log it in! */
430 if (create_user(desired_name, len, 1)) return(6);
432 snprintf(new_password, sizeof new_password, "%08lx%08lx", random(), random());
433 CtdlSetPassword(new_password);
434 attach_openid(&CC->user, claimed_id);
435 populate_vcard_from_sreg(sreg_keys);
441 * If a user account exists which is associated with the Claimed ID, log it in and return zero.
442 * Otherwise it returns nonzero.
444 int login_via_openid(StrBuf *claimed_id)
446 struct cdbdata *cdboi;
449 cdboi = cdb_fetch(CDB_OPENID, ChrPtr(claimed_id), StrLength(claimed_id));
454 memcpy(&usernum, cdboi->ptr, sizeof(long));
457 if (!CtdlGetUserByNumber(&CC->user, usernum)) {
458 /* Now become the user we just created */
459 safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
464 memset(&CC->user, 0, sizeof(struct ctdluser));
472 /**************************************************************************/
474 /* Functions in this section handle OpenID protocol */
476 /**************************************************************************/
480 * Locate a <link> tag and, given its 'rel=' parameter, return its 'href' parameter
482 void extract_link(StrBuf *target_buf, const char *rel, long repllen, StrBuf *source_buf)
486 const char *href_start = NULL;
487 const char *href_end = NULL;
488 const char *link_tag_start = NULL;
489 const char *link_tag_end = NULL;
490 const char *rel_start = NULL;
491 const char *rel_end = NULL;
493 if (!target_buf) return;
495 if (!source_buf) return;
497 ptr = ChrPtr(source_buf);
499 FlushStrBuf(target_buf);
500 while (ptr = cbmstrcasestr(ptr, "<link"), ptr != NULL) {
502 link_tag_start = ptr;
503 link_tag_end = strchr(ptr, '>');
504 if (link_tag_end == NULL)
506 for (i=0; i < 1; i++ ){
507 rel_start = cbmstrcasestr(link_tag_start, "rel=");
508 if ((rel_start == NULL) ||
509 (rel_start > link_tag_end))
512 rel_start = strchr(rel_start, '\"');
513 if ((rel_start == NULL) ||
514 (rel_start > link_tag_end))
517 rel_end = strchr(rel_start, '\"');
518 if ((rel_end == NULL) ||
519 (rel_end == rel_start) ||
520 (rel_end >= link_tag_end) )
522 if (strncasecmp(rel, rel_start, repllen)!= 0)
523 continue; /* didn't match? never mind... */
525 href_start = cbmstrcasestr(link_tag_start, "href=");
526 if ((href_start == NULL) ||
527 (href_start >= link_tag_end))
529 href_start = strchr(href_start, '\"');
530 if ((href_start == NULL) |
531 (href_start >= link_tag_end))
534 href_end = strchr(href_start, '\"');
535 if ((href_end == NULL) ||
536 (href_end == href_start) ||
537 (href_start >= link_tag_end))
539 StrBufPlain(target_buf, href_start, href_end - href_start);
547 * Begin an HTTP fetch (returns number of bytes actually fetched, or -1 for error) using libcurl.
549 int fetch_http(StrBuf *url, StrBuf **target_buf)
554 char errmsg[1024] = "";
555 char *effective_url = NULL;
557 if (StrLength(url) <=0 ) return(-1);
558 ReplyBuf = *target_buf = NewStrBuf ();
559 if (ReplyBuf == 0) return(-1);
561 curl = curl_easy_init();
563 syslog(LOG_ALERT, "Unable to initialize libcurl.");
567 curl_easy_setopt(curl, CURLOPT_URL, ChrPtr(url));
568 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
569 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
571 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
572 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
574 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
575 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
576 #ifdef CURLOPT_HTTP_CONTENT_DECODING
577 curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
578 curl_easy_setopt(curl, CURLOPT_ENCODING, "");
580 curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
581 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180); /* die after 180 seconds */
583 (!IsEmptyStr(config.c_ip_addr))
584 && (strcmp(config.c_ip_addr, "*"))
585 && (strcmp(config.c_ip_addr, "::"))
586 && (strcmp(config.c_ip_addr, "0.0.0.0"))
588 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
590 res = curl_easy_perform(curl);
592 syslog(LOG_DEBUG, "libcurl error %d: %s", res, errmsg);
594 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
595 StrBufPlain(url, effective_url, -1);
597 curl_easy_cleanup(curl);
598 return StrLength(ReplyBuf);
603 * Attempt to perform YADIS discovery.
604 * If successful, returns nonzero and fills the session's claimed ID blah FIXME this comment
605 * If YADIS fails, returns 0 and does nothing else.
607 int perform_yadis_discovery(StrBuf *YadisURL) {
609 StrBuf *ReplyBuf = NULL;
611 docbytes = fetch_http(YadisURL, &ReplyBuf);
616 FreeStrBuf(&ReplyBuf);
620 /* If we get to this point, something was retrieved.
621 * Feed it to the XML parser to see if it's an XRDS document.
623 XML_Parser xp = XML_ParserCreateNS(NULL, ':');
625 XML_Parse(xp, ChrPtr(ReplyBuf), docbytes, 0);
626 XML_Parse(xp, "", 0, 1);
630 syslog(LOG_ALERT, "Cannot create XML parser");
633 FreeStrBuf(&ReplyBuf);
639 * Setup an OpenID authentication
641 void cmd_oids(char *argbuf) {
642 const char *Pos = NULL;
643 StrBuf *ArgBuf = NULL;
644 StrBuf *ReplyBuf = NULL;
645 StrBuf *return_to = NULL;
646 StrBuf *trust_root = NULL;
647 StrBuf *openid_delegate = NULL;
648 StrBuf *RedirectUrl = NULL;
649 struct CitContext *CCC = CC; /* CachedCitContext - performance boost */
650 ctdl_openid *oiddata;
652 Free_ctdl_openid ((ctdl_openid**)&CCC->openid_data);
654 CCC->openid_data = oiddata = malloc(sizeof(ctdl_openid));
655 if (oiddata == NULL) {
656 cprintf("%d malloc failed\n", ERROR + INTERNAL_ERROR);
659 memset(oiddata, 0, sizeof(ctdl_openid));
660 CCC->openid_data = (void *) oiddata;
662 ArgBuf = NewStrBufPlain(argbuf, -1);
664 oiddata->verified = 0;
665 oiddata->claimed_id = NewStrBufPlain(NULL, StrLength(ArgBuf));
666 trust_root = NewStrBufPlain(NULL, StrLength(ArgBuf));
667 return_to = NewStrBufPlain(NULL, StrLength(ArgBuf));
669 StrBufExtract_NextToken(oiddata->claimed_id, ArgBuf, &Pos, '|');
670 StrBufExtract_NextToken(return_to, ArgBuf, &Pos, '|');
671 StrBufExtract_NextToken(trust_root, ArgBuf, &Pos, '|');
673 syslog(LOG_DEBUG, "User-Supplied Identifier is: %s", ChrPtr(oiddata->claimed_id));
676 /********** OpenID 2.0 section 7.3 - Discovery **********/
678 /* First we're supposed to attempt XRI based resolution.
679 * No one is using this, no one is asking for it, no one wants it.
680 * So we're not even going to bother attempting this mode.
683 /* Second we attempt YADIS.
684 * Google uses this so we'd better do our best to implement it.
686 int yadis_succeeded = perform_yadis_discovery(oiddata->claimed_id);
688 /* Third we attempt HTML-based discovery. Here we go! */
689 if ( (yadis_succeeded == 0)
690 && (fetch_http(oiddata->claimed_id, &ReplyBuf) > 0)
691 && (StrLength(ReplyBuf) > 0)
693 openid_delegate = NewStrBuf();
694 oiddata->server = NewStrBuf();
695 extract_link(oiddata->server, HKEY("openid.server"), ReplyBuf);
696 extract_link(openid_delegate, HKEY("openid.delegate"), ReplyBuf);
698 if (StrLength(oiddata->server) == 0) {
699 cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
701 FreeStrBuf(&ReplyBuf);
702 FreeStrBuf(&return_to);
703 FreeStrBuf(&trust_root);
704 FreeStrBuf(&openid_delegate);
705 FreeStrBuf(&RedirectUrl);
709 /* Empty delegate is legal; we just use the openid_url instead */
710 if (StrLength(openid_delegate) == 0) {
711 StrBufPlain(openid_delegate, SKEY(oiddata->claimed_id));
714 /* Assemble a URL to which the user-agent will be redirected. */
716 RedirectUrl = NewStrBufDup(oiddata->server);
718 StrBufAppendBufPlain(RedirectUrl, HKEY("?openid.mode=checkid_setup"
719 "&openid.identity="), 0);
720 StrBufUrlescAppend(RedirectUrl, openid_delegate, NULL);
722 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.return_to="), 0);
723 StrBufUrlescAppend(RedirectUrl, return_to, NULL);
725 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.trust_root="), 0);
726 StrBufUrlescAppend(RedirectUrl, trust_root, NULL);
728 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.sreg.optional="), 0);
729 StrBufUrlescAppend(RedirectUrl, NULL, "nickname,email,fullname,postcode,country,dob,gender");
731 cprintf("%d %s\n", CIT_OK, ChrPtr(RedirectUrl));
734 FreeStrBuf(&ReplyBuf);
735 FreeStrBuf(&return_to);
736 FreeStrBuf(&trust_root);
737 FreeStrBuf(&openid_delegate);
738 FreeStrBuf(&RedirectUrl);
744 FreeStrBuf(&ReplyBuf);
745 FreeStrBuf(&return_to);
746 FreeStrBuf(&trust_root);
747 FreeStrBuf(&openid_delegate);
748 FreeStrBuf(&RedirectUrl);
750 cprintf("%d Unable to fetch OpenID URL\n", ERROR);
758 * Finalize an OpenID authentication
760 void cmd_oidf(char *argbuf) {
765 HashList *keys = NULL;
766 ctdl_openid *oiddata = (ctdl_openid *) CC->openid_data;
768 if (oiddata == NULL) {
769 cprintf("%d run OIDS first.\n", ERROR + INTERNAL_ERROR);
772 if (StrLength(oiddata->server) == 0){
773 cprintf("%d need a remote server to authenticate against\n", ERROR + ILLEGAL_VALUE);
776 keys = NewHash(1, NULL);
778 cprintf("%d NewHash() failed\n", ERROR + INTERNAL_ERROR);
781 cprintf("%d Transmit OpenID data now\n", START_CHAT_MODE);
783 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
784 len = extract_token(thiskey, buf, 0, '|', sizeof thiskey);
786 len = sizeof(thiskey) - 1;
787 extract_token(thisdata, buf, 1, '|', sizeof thisdata);
788 syslog(LOG_DEBUG, "%s: ["SIZE_T_FMT"] %s", thiskey, strlen(thisdata), thisdata);
789 Put(keys, thiskey, len, strdup(thisdata), NULL);
793 /* Now that we have all of the parameters, we have to validate the signature against the server */
794 syslog(LOG_DEBUG, "About to validate the signature...");
798 struct curl_httppost *formpost = NULL;
799 struct curl_httppost *lastptr = NULL;
800 char errmsg[1024] = "";
801 char *o_assoc_handle = NULL;
803 char *o_signed = NULL;
804 int num_signed_values;
807 char k_o_keyname[128];
808 char *k_value = NULL;
811 curl_formadd(&formpost, &lastptr,
812 CURLFORM_COPYNAME, "openid.mode",
813 CURLFORM_COPYCONTENTS, "check_authentication",
815 syslog(LOG_DEBUG, "%25s : %s", "openid.mode", "check_authentication");
817 if (GetHash(keys, "assoc_handle", 12, (void *) &o_assoc_handle)) {
818 curl_formadd(&formpost, &lastptr,
819 CURLFORM_COPYNAME, "openid.assoc_handle",
820 CURLFORM_COPYCONTENTS, o_assoc_handle,
822 syslog(LOG_DEBUG, "%25s : %s", "openid.assoc_handle", o_assoc_handle);
825 if (GetHash(keys, "sig", 3, (void *) &o_sig)) {
826 curl_formadd(&formpost, &lastptr,
827 CURLFORM_COPYNAME, "openid.sig",
828 CURLFORM_COPYCONTENTS, o_sig,
830 syslog(LOG_DEBUG, "%25s : %s", "openid.sig", o_sig);
833 if (GetHash(keys, "signed", 6, (void *) &o_signed)) {
834 curl_formadd(&formpost, &lastptr,
835 CURLFORM_COPYNAME, "openid.signed",
836 CURLFORM_COPYCONTENTS, o_signed,
838 syslog(LOG_DEBUG, "%25s : %s", "openid.signed", o_signed);
840 num_signed_values = num_tokens(o_signed, ',');
841 for (i=0; i<num_signed_values; ++i) {
842 extract_token(k_keyname, o_signed, i, ',', sizeof k_keyname);
843 if (strcasecmp(k_keyname, "mode")) { // work around phpMyID bug
844 if (GetHash(keys, k_keyname, strlen(k_keyname), (void *) &k_value)) {
845 snprintf(k_o_keyname, sizeof k_o_keyname, "openid.%s", k_keyname);
846 curl_formadd(&formpost, &lastptr,
847 CURLFORM_COPYNAME, k_o_keyname,
848 CURLFORM_COPYCONTENTS, k_value,
850 syslog(LOG_DEBUG, "%25s : %s", k_o_keyname, k_value);
853 syslog(LOG_INFO, "OpenID: signed field '%s' is missing",
860 ReplyBuf = NewStrBuf();
862 curl = curl_easy_init();
863 curl_easy_setopt(curl, CURLOPT_URL, ChrPtr(oiddata->server));
864 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
865 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
867 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
868 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
869 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
870 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
871 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
872 #ifdef CURLOPT_HTTP_CONTENT_DECODING
873 curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
874 curl_easy_setopt(curl, CURLOPT_ENCODING, "");
876 curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
877 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180); /* die after 180 seconds */
879 (!IsEmptyStr(config.c_ip_addr))
880 && (strcmp(config.c_ip_addr, "*"))
881 && (strcmp(config.c_ip_addr, "::"))
882 && (strcmp(config.c_ip_addr, "0.0.0.0"))
884 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
887 res = curl_easy_perform(curl);
889 syslog(LOG_DEBUG, "cmd_oidf() libcurl error %d: %s", res, errmsg);
891 curl_easy_cleanup(curl);
892 curl_formfree(formpost);
894 if (cbmstrcasestr(ChrPtr(ReplyBuf), "is_valid:true")) {
895 oiddata->verified = 1;
897 FreeStrBuf(&ReplyBuf);
899 syslog(LOG_DEBUG, "Authentication %s.", (oiddata->verified ? "succeeded" : "failed") );
901 /* Respond to the client */
903 if (oiddata->verified) {
905 /* If we were already logged in, attach the OpenID to the user's account */
907 if (attach_openid(&CC->user, oiddata->claimed_id) == 0) {
909 syslog(LOG_DEBUG, "OpenID attach succeeded");
913 syslog(LOG_DEBUG, "OpenID attach failed");
917 /* Otherwise, a user is attempting to log in using the verified OpenID */
920 * Existing user who has claimed this OpenID?
922 * Note: if you think that sending the password back over the wire is insecure,
923 * check your assumptions. If someone has successfully asserted an OpenID that
924 * is associated with the account, they already have password equivalency and can
925 * login, so they could just as easily change the password, etc.
927 if (login_via_openid(oiddata->claimed_id) == 0) {
928 cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
929 logged_in_response();
930 syslog(LOG_DEBUG, "Logged in using previously claimed OpenID");
934 * If this system does not allow self-service new user registration, the
935 * remaining modes do not apply, so fail here and now.
937 else if (config.c_disable_newu) {
939 syslog(LOG_DEBUG, "Creating user failed due to local policy");
943 * New user whose OpenID is verified and Simple Registration Extension is in use?
945 else if (openid_create_user_via_sreg(oiddata->claimed_id, keys) == 0) {
946 cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
947 logged_in_response();
948 syslog(LOG_DEBUG, "Successfully auto-created new user");
952 * OpenID is verified, but the desired username either was not specified or
953 * conflicts with an existing user. Manual account creation is required.
956 char *desired_name = NULL;
957 cprintf("verify_only\n");
958 cprintf("%s\n", ChrPtr(oiddata->claimed_id));
959 if (GetHash(keys, "sreg.nickname", 13, (void *) &desired_name)) {
960 cprintf("%s\n", desired_name);
965 syslog(LOG_DEBUG, "The desired Simple Registration name is already taken.");
974 if (oiddata->sreg_keys != NULL) {
975 DeleteHash(&oiddata->sreg_keys);
976 oiddata->sreg_keys = NULL;
978 oiddata->sreg_keys = keys;
983 /**************************************************************************/
985 /* Functions in this section handle module initialization and shutdown */
987 /**************************************************************************/
992 CTDL_MODULE_INIT(openid_rp)
995 curl_global_init(CURL_GLOBAL_ALL);
997 /* Only enable the OpenID command set when native mode authentication is in use. */
998 if (config.c_auth_mode == AUTHMODE_NATIVE) {
999 CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
1000 CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
1001 CtdlRegisterProtoHook(cmd_oidl, "OIDL", "List OpenIDs associated with an account");
1002 CtdlRegisterProtoHook(cmd_oidd, "OIDD", "Detach an OpenID from an account");
1003 CtdlRegisterProtoHook(cmd_oidc, "OIDC", "Create new user after validating OpenID");
1004 CtdlRegisterProtoHook(cmd_oida, "OIDA", "List all OpenIDs in the database");
1006 CtdlRegisterSessionHook(openid_cleanup_function, EVT_LOGOUT);
1007 CtdlRegisterUserHook(openid_purge, EVT_PURGEUSER);
1008 openid_level_supported = 1; /* This module supports OpenID 1.0 only */
1011 /* return our module name for the log */