4 * This is an implementation of OpenID 1.1 Relying Party support, in stateless mode.
6 * Copyright (c) 2007-2009 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sys/types.h>
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
38 # include <sys/time.h>
47 #include <curl/curl.h>
48 #include "ctdl_module.h"
50 #include "citserver.h"
53 typedef struct _ctdl_openid {
60 void Free_ctdl_openid(ctdl_openid **FreeMe)
64 FreeStrBuf(&(*FreeMe)->claimed_id);
65 FreeStrBuf(&(*FreeMe)->server);
66 DeleteHash(&(*FreeMe)->sreg_keys);
73 * This cleanup function blows away the temporary memory used by this module.
75 void openid_cleanup_function(void) {
76 struct CitContext *CCC = CC; /* CachedCitContext - performance boost */
78 if (CCC->openid_data != NULL) {
79 CtdlLogPrintf(CTDL_DEBUG, "Clearing OpenID session state\n");
80 Free_ctdl_openid((ctdl_openid **) &CCC->openid_data);
85 /**************************************************************************/
87 /* Functions in this section handle Citadel internal OpenID mapping stuff */
89 /**************************************************************************/
93 * The structure of an openid record *key* is:
95 * |--------------claimed_id-------------|
96 * (actual length of claimed id)
99 * The structure of an openid record *value* is:
101 * |-----user_number----|------------claimed_id---------------|
102 * (sizeof long) (actual length of claimed id)
109 * Attach an OpenID to a Citadel account
111 int attach_openid(struct ctdluser *who, StrBuf *claimed_id)
113 struct cdbdata *cdboi;
114 long fetched_usernum;
120 if (StrLength(claimed_id)==0) return(1);
122 /* Check to see if this OpenID is already in the database */
124 cdboi = cdb_fetch(CDB_OPENID, ChrPtr(claimed_id), StrLength(claimed_id));
126 memcpy(&fetched_usernum, cdboi->ptr, sizeof(long));
129 if (fetched_usernum == who->usernum) {
130 CtdlLogPrintf(CTDL_INFO, "%s already associated; no action is taken\n", claimed_id);
134 CtdlLogPrintf(CTDL_INFO, "%s already belongs to another user\n", claimed_id);
139 /* Not already in the database, so attach it now */
141 data_len = sizeof(long) + StrLength(claimed_id) + 1;
142 data = malloc(data_len);
144 memcpy(data, &who->usernum, sizeof(long));
145 memcpy(&data[sizeof(long)], ChrPtr(claimed_id), StrLength(claimed_id) + 1);
147 cdb_store(CDB_OPENID, ChrPtr(claimed_id), StrLength(claimed_id), data, data_len);
150 snprintf(buf, sizeof buf, "User <%s> (#%ld) has claimed the OpenID URL %s\n",
151 who->fullname, who->usernum, ChrPtr(claimed_id));
152 CtdlAideMessage(buf, "OpenID claim");
153 CtdlLogPrintf(CTDL_INFO, "%s", buf);
160 * When a user is being deleted, we have to delete any OpenID associations
162 void openid_purge(struct ctdluser *usbuf) {
163 struct cdbdata *cdboi;
164 HashList *keys = NULL;
166 char *deleteme = NULL;
172 keys = NewHash(1, NULL);
176 cdb_rewind(CDB_OPENID);
177 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
178 if (cdboi->len > sizeof(long)) {
179 memcpy(&usernum, cdboi->ptr, sizeof(long));
180 if (usernum == usbuf->usernum) {
181 deleteme = strdup(cdboi->ptr + sizeof(long)),
182 Put(keys, deleteme, strlen(deleteme), deleteme, NULL);
188 /* Go through the hash list, deleting keys we stored in it */
190 HashPos = GetNewHashPos(keys, 0);
191 while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
193 CtdlLogPrintf(CTDL_DEBUG, "Deleting associated OpenID <%s>\n", Value);
194 cdb_delete(CDB_OPENID, Value, strlen(Value));
195 /* note: don't free(Value) -- deleting the hash list will handle this for us */
197 DeleteHashPos(&HashPos);
204 * List the OpenIDs associated with the currently logged in account
206 void cmd_oidl(char *argbuf) {
207 struct cdbdata *cdboi;
210 if (CtdlAccessCheck(ac_logged_in)) return;
211 cdb_rewind(CDB_OPENID);
212 cprintf("%d Associated OpenIDs:\n", LISTING_FOLLOWS);
214 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
215 if (cdboi->len > sizeof(long)) {
216 memcpy(&usernum, cdboi->ptr, sizeof(long));
217 if (usernum == CC->user.usernum) {
218 cprintf("%s\n", cdboi->ptr + sizeof(long));
228 * List ALL OpenIDs in the database
230 void cmd_oida(char *argbuf) {
231 struct cdbdata *cdboi;
233 struct ctdluser usbuf;
235 if (CtdlAccessCheck(ac_aide)) return;
236 cdb_rewind(CDB_OPENID);
237 cprintf("%d List of all OpenIDs in the database:\n", LISTING_FOLLOWS);
239 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
240 if (cdboi->len > sizeof(long)) {
241 memcpy(&usernum, cdboi->ptr, sizeof(long));
242 if (CtdlGetUserByNumber(&usbuf, usernum) != 0) {
243 usbuf.fullname[0] = 0;
245 cprintf("%s|%ld|%s\n",
246 cdboi->ptr + sizeof(long),
258 * Attempt to register (populate the vCard) the currently-logged-in user
259 * using the data from Simple Registration Extension, if present.
261 void populate_vcard_from_sreg(HashList *sreg_keys) {
264 int pop = 0; /* number of fields populated */
266 char *postcode = NULL;
267 char *country = NULL;
269 if (!sreg_keys) return;
273 if (GetHash(sreg_keys, "identity", 8, (void *) &data)) {
274 vcard_add_prop(v, "url;type=openid", data);
278 if (GetHash(sreg_keys, "sreg.email", 10, (void *) &data)) {
279 vcard_add_prop(v, "email;internet", data);
283 if (GetHash(sreg_keys, "sreg.nickname", 13, (void *) &data)) {
284 vcard_add_prop(v, "nickname", data);
288 if (GetHash(sreg_keys, "sreg.fullname", 13, (void *) &data)) {
290 vcard_add_prop(v, "fn", data);
291 vcard_fn_to_n(n, data, sizeof n);
292 vcard_add_prop(v, "n", n);
296 if (!GetHash(sreg_keys, "sreg.postcode", 13, (void *) &postcode)) {
300 if (!GetHash(sreg_keys, "sreg.country", 12, (void *) &country)) {
304 if (postcode || country) {
306 snprintf(adr, sizeof adr, ";;;;;%s;%s",
307 (postcode ? postcode : ""),
308 (country ? country : "")
310 vcard_add_prop(v, "adr", adr);
314 if (GetHash(sreg_keys, "sreg.dob", 8, (void *) &data)) {
315 vcard_add_prop(v, "bday", data);
319 if (GetHash(sreg_keys, "sreg.gender", 11, (void *) &data)) {
320 vcard_add_prop(v, "x-funambol-gender", data);
324 /* Only save the vCard if there is some useful data in it */
327 ser = vcard_serialize(v);
329 CtdlWriteObject(USERCONFIGROOM, "text/x-vcard",
330 ser, strlen(ser)+1, &CC->user, 0, 0, 0
340 * Create a new user account, manually specifying the name, after successfully
341 * verifying an OpenID (which will of course be attached to the account)
343 void cmd_oidc(char *argbuf) {
344 ctdl_openid *oiddata = (ctdl_openid *) CC->openid_data;
347 cprintf("%d You have not verified an OpenID yet.\n", ERROR);
351 if (!oiddata->verified) {
352 cprintf("%d You have not verified an OpenID yet.\n", ERROR);
356 /* We can make the semantics of OIDC exactly the same as NEWU, simply
357 * by _calling_ cmd_newu() and letting it run. Very clever!
361 /* Now, if this logged us in, we have to attach the OpenID */
363 attach_openid(&CC->user, oiddata->claimed_id);
364 if (oiddata->sreg_keys != NULL) {
365 populate_vcard_from_sreg(oiddata->sreg_keys);
375 * Detach an OpenID from the currently logged in account
377 void cmd_oidd(char *argbuf) {
378 struct cdbdata *cdboi;
379 char id_to_detach[1024];
380 int this_is_mine = 0;
383 if (CtdlAccessCheck(ac_logged_in)) return;
384 extract_token(id_to_detach, argbuf, 0, '|', sizeof id_to_detach);
385 if (IsEmptyStr(id_to_detach)) {
386 cprintf("%d An empty OpenID URL is not allowed.\n", ERROR + ILLEGAL_VALUE);
389 cdb_rewind(CDB_OPENID);
390 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
391 if (cdboi->len > sizeof(long)) {
392 memcpy(&usernum, cdboi->ptr, sizeof(long));
393 if (usernum == CC->user.usernum) {
401 cprintf("%d That OpenID was not found or not associated with your account.\n",
402 ERROR + ILLEGAL_VALUE);
406 cdb_delete(CDB_OPENID, id_to_detach, strlen(id_to_detach));
407 cprintf("%d %s detached from your account.\n", CIT_OK, id_to_detach);
413 * Attempt to auto-create a new Citadel account using the nickname from Simple Registration Extension
415 int openid_create_user_via_sreg(StrBuf *claimed_id, HashList *sreg_keys)
417 char *desired_name = NULL;
418 char new_password[32];
421 if (config.c_auth_mode != AUTHMODE_NATIVE) return(1);
422 if (config.c_disable_newu) return(2);
423 if (CC->logged_in) return(3);
424 if (!GetHash(sreg_keys, "sreg.nickname", 13, (void *) &desired_name)) return(4);
426 CtdlLogPrintf(CTDL_DEBUG, "The desired account name is <%s>\n", desired_name);
428 len = cutuserkey(desired_name);
429 if (!CtdlGetUser(&CC->user, desired_name)) {
430 CtdlLogPrintf(CTDL_DEBUG, "<%s> is already taken by another user.\n", desired_name);
431 memset(&CC->user, 0, sizeof(struct ctdluser));
435 /* The desired account name is available. Create the account and log it in! */
436 if (create_user(desired_name, len, 1)) return(6);
438 snprintf(new_password, sizeof new_password, "%08lx%08lx", random(), random());
439 CtdlSetPassword(new_password);
440 attach_openid(&CC->user, claimed_id);
441 populate_vcard_from_sreg(sreg_keys);
447 * If a user account exists which is associated with the Claimed ID, log it in and return zero.
448 * Otherwise it returns nonzero.
450 int login_via_openid(StrBuf *claimed_id)
452 struct cdbdata *cdboi;
455 cdboi = cdb_fetch(CDB_OPENID, ChrPtr(claimed_id), StrLength(claimed_id));
460 memcpy(&usernum, cdboi->ptr, sizeof(long));
463 if (!CtdlGetUserByNumber(&CC->user, usernum)) {
464 /* Now become the user we just created */
465 safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
470 memset(&CC->user, 0, sizeof(struct ctdluser));
478 /**************************************************************************/
480 /* Functions in this section handle OpenID protocol */
482 /**************************************************************************/
486 * Locate a <link> tag and, given its 'rel=' parameter, return its 'href' parameter
488 void extract_link(StrBuf *target_buf, const char *rel, long repllen, StrBuf *source_buf)
492 const char *href_start = NULL;
493 const char *href_end = NULL;
494 const char *link_tag_start = NULL;
495 const char *link_tag_end = NULL;
496 const char *rel_start = NULL;
497 const char *rel_end = NULL;
499 if (!target_buf) return;
501 if (!source_buf) return;
503 ptr = ChrPtr(source_buf);
505 FlushStrBuf(target_buf);
506 while (ptr = bmstrcasestr(ptr, "<link"), ptr != NULL) {
508 link_tag_start = ptr;
509 link_tag_end = strchr(ptr, '>');
510 if (link_tag_end == NULL)
512 for (i=0; i < 1; i++ ){
513 len = link_tag_end - link_tag_start;
515 rel_start = bmstrcasestr(link_tag_start, "rel=");
516 if ((rel_start == NULL) ||
517 (rel_start > link_tag_end))
520 rel_start = strchr(rel_start, '\"');
521 if ((rel_start == NULL) ||
522 (rel_start > link_tag_end))
525 rel_end = strchr(rel_start, '\"');
526 if ((rel_end == NULL) ||
527 (rel_end == rel_start) ||
528 (rel_end >= link_tag_end) )
530 if (strncasecmp(rel, rel_start, repllen)!= 0)
531 continue; /* didn't match? never mind... */
533 href_start = bmstrcasestr(link_tag_start, "href=");
534 if ((href_start == NULL) ||
535 (href_start >= link_tag_end))
537 href_start = strchr(href_start, '\"');
538 if ((href_start == NULL) |
539 (href_start >= link_tag_end))
542 href_end = strchr(href_start, '\"');
543 if ((href_end == NULL) ||
544 (href_end == href_start) ||
545 (href_start >= link_tag_end))
547 StrBufPlain(target_buf, href_start, href_end - href_start);
555 * Begin an HTTP fetch (returns number of bytes actually fetched, or -1 for error) using libcurl.
557 * If 'normalize_len' is nonzero, the caller is specifying the buffer size of 'url', and is
558 * requesting that the effective (normalized) URL be copied back to it.
560 int fetch_http(StrBuf *url, StrBuf **target_buf)
565 char errmsg[1024] = "";
566 char *effective_url = NULL;
568 if (StrLength(url) <=0 ) return(-1);
569 ReplyBuf = *target_buf = NewStrBuf ();
570 if (ReplyBuf == 0) return(-1);
572 curl = curl_easy_init();
574 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
578 curl_easy_setopt(curl, CURLOPT_URL, ChrPtr(url));
579 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
580 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
582 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
583 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
585 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
586 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
587 #ifdef CURLOPT_HTTP_CONTENT_DECODING
588 curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
589 curl_easy_setopt(curl, CURLOPT_ENCODING, "");
591 curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
592 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180); /* die after 180 seconds */
593 if (!IsEmptyStr(config.c_ip_addr)) {
594 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
596 res = curl_easy_perform(curl);
598 CtdlLogPrintf(CTDL_DEBUG, "fetch_http() libcurl error %d: %s\n", res, errmsg);
600 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
601 StrBufPlain(url, effective_url, -1);
603 curl_easy_cleanup(curl);
604 return StrLength(ReplyBuf);
609 * Setup an OpenID authentication
611 void cmd_oids(char *argbuf) {
612 const char *Pos = NULL;
613 StrBuf *ArgBuf = NULL;
614 StrBuf *ReplyBuf = NULL;
615 StrBuf *return_to = NULL;
616 StrBuf *trust_root = NULL;
617 StrBuf *openid_delegate = NULL;
618 StrBuf *RedirectUrl = NULL;
620 struct CitContext *CCC = CC; /* CachedCitContext - performance boost */
621 ctdl_openid *oiddata;
623 Free_ctdl_openid ((ctdl_openid**)&CCC->openid_data);
625 CCC->openid_data = oiddata = malloc(sizeof(ctdl_openid));
626 if (oiddata == NULL) {
627 cprintf("%d malloc failed\n", ERROR + INTERNAL_ERROR);
630 memset(oiddata, 0, sizeof(ctdl_openid));
631 CCC->openid_data = (void *) oiddata;
633 ArgBuf = NewStrBufPlain(argbuf, -1);
635 oiddata->claimed_id = NewStrBufPlain(NULL, StrLength(ArgBuf));
636 trust_root = NewStrBufPlain(NULL, StrLength(ArgBuf));
637 return_to = NewStrBufPlain(NULL, StrLength(ArgBuf));
639 StrBufExtract_NextToken(oiddata->claimed_id, ArgBuf, &Pos, '|');
640 StrBufExtract_NextToken(return_to, ArgBuf, &Pos, '|');
641 StrBufExtract_NextToken(trust_root, ArgBuf, &Pos, '|');
643 oiddata->verified = 0;
645 i = fetch_http(oiddata->claimed_id, &ReplyBuf);
646 CtdlLogPrintf(CTDL_DEBUG, "Normalized URL and Claimed ID is: %s\n",
647 ChrPtr(oiddata->claimed_id));
648 if ((StrLength(ReplyBuf) > 0) && (i > 0)) {
650 openid_delegate = NewStrBuf();
651 oiddata->server = NewStrBuf();
652 extract_link(oiddata->server, HKEY("openid.server"), ReplyBuf);
653 extract_link(openid_delegate, HKEY("openid.delegate"), ReplyBuf);
655 if (StrLength(oiddata->server) == 0) {
656 cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
658 FreeStrBuf(&ReplyBuf);
659 FreeStrBuf(&return_to);
660 FreeStrBuf(&trust_root);
661 FreeStrBuf(&openid_delegate);
662 FreeStrBuf(&RedirectUrl);
666 /* Empty delegate is legal; we just use the openid_url instead */
667 if (StrLength(openid_delegate) == 0) {
668 StrBufPlain(openid_delegate, SKEY(oiddata->claimed_id));
671 /* Assemble a URL to which the user-agent will be redirected. */
673 RedirectUrl = NewStrBufDup(oiddata->server);
675 StrBufAppendBufPlain(RedirectUrl, HKEY("?openid.mode=checkid_setup"
676 "&openid.identity="), 0);
677 StrBufUrlescAppend(RedirectUrl, openid_delegate, NULL);
679 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.return_to="), 0);
680 StrBufUrlescAppend(RedirectUrl, return_to, NULL);
682 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.trust_root="), 0);
683 StrBufUrlescAppend(RedirectUrl, trust_root, NULL);
685 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.sreg.optional="), 0);
686 StrBufUrlescAppend(RedirectUrl, NULL, "nickname,email,fullname,postcode,country,dob,gender");
688 cprintf("%d %s\n", CIT_OK, ChrPtr(RedirectUrl));
691 FreeStrBuf(&ReplyBuf);
692 FreeStrBuf(&return_to);
693 FreeStrBuf(&trust_root);
694 FreeStrBuf(&openid_delegate);
695 FreeStrBuf(&RedirectUrl);
700 FreeStrBuf(&ReplyBuf);
701 FreeStrBuf(&return_to);
702 FreeStrBuf(&trust_root);
703 FreeStrBuf(&openid_delegate);
704 FreeStrBuf(&RedirectUrl);
706 cprintf("%d Unable to fetch OpenID URL\n", ERROR);
714 * Finalize an OpenID authentication
716 void cmd_oidf(char *argbuf) {
721 HashList *keys = NULL;
722 ctdl_openid *oiddata = (ctdl_openid *) CC->openid_data;
724 if (oiddata == NULL) {
725 cprintf("%d run OIDS first.\n", ERROR + INTERNAL_ERROR);
728 if (StrLength(oiddata->server) == 0){
729 cprintf("%d need a remote server to authenticate against\n", ERROR + ILLEGAL_VALUE);
732 keys = NewHash(1, NULL);
734 cprintf("%d NewHash() failed\n", ERROR + INTERNAL_ERROR);
737 cprintf("%d Transmit OpenID data now\n", START_CHAT_MODE);
739 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
740 len = extract_token(thiskey, buf, 0, '|', sizeof thiskey);
742 len = sizeof(thiskey) - 1;
743 extract_token(thisdata, buf, 1, '|', sizeof thisdata);
744 CtdlLogPrintf(CTDL_DEBUG, "%s: [%d] %s\n", thiskey, strlen(thisdata), thisdata);
745 Put(keys, thiskey, len, strdup(thisdata), NULL);
749 /* Now that we have all of the parameters, we have to validate the signature against the server */
750 CtdlLogPrintf(CTDL_DEBUG, "About to validate the signature...\n");
754 struct curl_httppost *formpost = NULL;
755 struct curl_httppost *lastptr = NULL;
756 char errmsg[1024] = "";
757 char *o_assoc_handle = NULL;
759 char *o_signed = NULL;
760 int num_signed_values;
763 char k_o_keyname[128];
764 char *k_value = NULL;
767 curl_formadd(&formpost, &lastptr,
768 CURLFORM_COPYNAME, "openid.mode",
769 CURLFORM_COPYCONTENTS, "check_authentication",
771 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.mode", "check_authentication");
773 if (GetHash(keys, "assoc_handle", 12, (void *) &o_assoc_handle)) {
774 curl_formadd(&formpost, &lastptr,
775 CURLFORM_COPYNAME, "openid.assoc_handle",
776 CURLFORM_COPYCONTENTS, o_assoc_handle,
778 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.assoc_handle", o_assoc_handle);
781 if (GetHash(keys, "sig", 3, (void *) &o_sig)) {
782 curl_formadd(&formpost, &lastptr,
783 CURLFORM_COPYNAME, "openid.sig",
784 CURLFORM_COPYCONTENTS, o_sig,
786 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.sig", o_sig);
789 if (GetHash(keys, "signed", 6, (void *) &o_signed)) {
790 curl_formadd(&formpost, &lastptr,
791 CURLFORM_COPYNAME, "openid.signed",
792 CURLFORM_COPYCONTENTS, o_signed,
794 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.signed", o_signed);
796 num_signed_values = num_tokens(o_signed, ',');
797 for (i=0; i<num_signed_values; ++i) {
798 extract_token(k_keyname, o_signed, i, ',', sizeof k_keyname);
799 if (strcasecmp(k_keyname, "mode")) { // work around phpMyID bug
800 if (GetHash(keys, k_keyname, strlen(k_keyname), (void *) &k_value)) {
801 snprintf(k_o_keyname, sizeof k_o_keyname, "openid.%s", k_keyname);
802 curl_formadd(&formpost, &lastptr,
803 CURLFORM_COPYNAME, k_o_keyname,
804 CURLFORM_COPYCONTENTS, k_value,
806 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", k_o_keyname, k_value);
809 CtdlLogPrintf(CTDL_INFO, "OpenID: signed field '%s' is missing\n",
816 ReplyBuf = NewStrBuf();
818 curl = curl_easy_init();
819 curl_easy_setopt(curl, CURLOPT_URL, ChrPtr(oiddata->server));
820 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
821 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
823 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
824 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
825 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
826 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
827 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
828 #ifdef CURLOPT_HTTP_CONTENT_DECODING
829 curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
830 curl_easy_setopt(curl, CURLOPT_ENCODING, "");
832 curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
833 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180); /* die after 180 seconds */
834 if (!IsEmptyStr(config.c_ip_addr)) {
835 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
838 res = curl_easy_perform(curl);
840 CtdlLogPrintf(CTDL_DEBUG, "cmd_oidf() libcurl error %d: %s\n", res, errmsg);
842 curl_easy_cleanup(curl);
843 curl_formfree(formpost);
845 if (bmstrcasestr(ChrPtr(ReplyBuf), "is_valid:true")) {
846 oiddata->verified = 1;
848 FreeStrBuf(&ReplyBuf);
850 CtdlLogPrintf(CTDL_DEBUG, "Authentication %s.\n", (oiddata->verified ? "succeeded" : "failed") );
852 /* Respond to the client */
854 if (oiddata->verified) {
856 /* If we were already logged in, attach the OpenID to the user's account */
858 if (attach_openid(&CC->user, oiddata->claimed_id) == 0) {
860 CtdlLogPrintf(CTDL_DEBUG, "OpenID attach succeeded\n");
864 CtdlLogPrintf(CTDL_DEBUG, "OpenID attach failed\n");
868 /* Otherwise, a user is attempting to log in using the verified OpenID */
871 * Existing user who has claimed this OpenID?
873 * Note: if you think that sending the password back over the wire is insecure,
874 * check your assumptions. If someone has successfully asserted an OpenID that
875 * is associated with the account, they already have password equivalency and can
876 * login, so they could just as easily change the password, etc.
878 if (login_via_openid(oiddata->claimed_id) == 0) {
879 cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
880 logged_in_response();
881 CtdlLogPrintf(CTDL_DEBUG, "Logged in using previously claimed OpenID\n");
885 * If this system does not allow self-service new user registration, the
886 * remaining modes do not apply, so fail here and now.
888 else if (config.c_disable_newu) {
890 CtdlLogPrintf(CTDL_DEBUG, "Creating user failed due to local policy\n");
894 * New user whose OpenID is verified and Simple Registration Extension is in use?
896 else if (openid_create_user_via_sreg(oiddata->claimed_id, keys) == 0) {
897 cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
898 logged_in_response();
899 CtdlLogPrintf(CTDL_DEBUG, "Successfully auto-created new user\n");
903 * OpenID is verified, but the desired username either was not specified or
904 * conflicts with an existing user. Manual account creation is required.
907 char *desired_name = NULL;
908 cprintf("verify_only\n");
909 cprintf("%s\n", ChrPtr(oiddata->claimed_id));
910 if (GetHash(keys, "sreg.nickname", 13, (void *) &desired_name)) {
911 cprintf("%s\n", desired_name);
916 CtdlLogPrintf(CTDL_DEBUG, "The desired Simple Registration name is already taken.\n");
925 if (oiddata->sreg_keys != NULL) {
926 DeleteHash(&oiddata->sreg_keys);
927 oiddata->sreg_keys = NULL;
929 oiddata->sreg_keys = keys;
934 /**************************************************************************/
936 /* Functions in this section handle module initialization and shutdown */
938 /**************************************************************************/
943 CTDL_MODULE_INIT(openid_rp)
946 curl_global_init(CURL_GLOBAL_ALL);
948 /* Only enable the OpenID command set when native mode authentication is in use. */
949 if (config.c_auth_mode == AUTHMODE_NATIVE) {
950 CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
951 CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
952 CtdlRegisterProtoHook(cmd_oidl, "OIDL", "List OpenIDs associated with an account");
953 CtdlRegisterProtoHook(cmd_oidd, "OIDD", "Detach an OpenID from an account");
954 CtdlRegisterProtoHook(cmd_oidc, "OIDC", "Create new user after validating OpenID");
955 CtdlRegisterProtoHook(cmd_oida, "OIDA", "List all OpenIDs in the database");
957 CtdlRegisterSessionHook(openid_cleanup_function, EVT_LOGOUT);
958 CtdlRegisterUserHook(openid_purge, EVT_PURGEUSER);
961 /* return our Subversion id for the Log */