4 * This is an implementation of OpenID 1.1 Relying Party support, in stateless mode.
6 * Copyright (c) 2007-2010 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 = cbmstrcasestr(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 = cbmstrcasestr(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 = cbmstrcasestr(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 */
594 (!IsEmptyStr(config.c_ip_addr))
595 && (strcmp(config.c_ip_addr, "*"))
596 && (strcmp(config.c_ip_addr, "::"))
597 && (strcmp(config.c_ip_addr, "0.0.0.0"))
599 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
601 res = curl_easy_perform(curl);
603 CtdlLogPrintf(CTDL_DEBUG, "fetch_http() libcurl error %d: %s\n", res, errmsg);
605 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
606 StrBufPlain(url, effective_url, -1);
608 curl_easy_cleanup(curl);
609 return StrLength(ReplyBuf);
614 * Setup an OpenID authentication
616 void cmd_oids(char *argbuf) {
617 const char *Pos = NULL;
618 StrBuf *ArgBuf = NULL;
619 StrBuf *ReplyBuf = NULL;
620 StrBuf *return_to = NULL;
621 StrBuf *trust_root = NULL;
622 StrBuf *openid_delegate = NULL;
623 StrBuf *RedirectUrl = NULL;
625 struct CitContext *CCC = CC; /* CachedCitContext - performance boost */
626 ctdl_openid *oiddata;
628 Free_ctdl_openid ((ctdl_openid**)&CCC->openid_data);
630 CCC->openid_data = oiddata = malloc(sizeof(ctdl_openid));
631 if (oiddata == NULL) {
632 cprintf("%d malloc failed\n", ERROR + INTERNAL_ERROR);
635 memset(oiddata, 0, sizeof(ctdl_openid));
636 CCC->openid_data = (void *) oiddata;
638 ArgBuf = NewStrBufPlain(argbuf, -1);
640 oiddata->claimed_id = NewStrBufPlain(NULL, StrLength(ArgBuf));
641 trust_root = NewStrBufPlain(NULL, StrLength(ArgBuf));
642 return_to = NewStrBufPlain(NULL, StrLength(ArgBuf));
644 StrBufExtract_NextToken(oiddata->claimed_id, ArgBuf, &Pos, '|');
645 StrBufExtract_NextToken(return_to, ArgBuf, &Pos, '|');
646 StrBufExtract_NextToken(trust_root, ArgBuf, &Pos, '|');
648 oiddata->verified = 0;
650 i = fetch_http(oiddata->claimed_id, &ReplyBuf);
651 CtdlLogPrintf(CTDL_DEBUG, "Normalized URL and Claimed ID is: %s\n",
652 ChrPtr(oiddata->claimed_id));
653 if ((StrLength(ReplyBuf) > 0) && (i > 0)) {
655 openid_delegate = NewStrBuf();
656 oiddata->server = NewStrBuf();
657 extract_link(oiddata->server, HKEY("openid.server"), ReplyBuf);
658 extract_link(openid_delegate, HKEY("openid.delegate"), ReplyBuf);
660 if (StrLength(oiddata->server) == 0) {
661 cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
663 FreeStrBuf(&ReplyBuf);
664 FreeStrBuf(&return_to);
665 FreeStrBuf(&trust_root);
666 FreeStrBuf(&openid_delegate);
667 FreeStrBuf(&RedirectUrl);
671 /* Empty delegate is legal; we just use the openid_url instead */
672 if (StrLength(openid_delegate) == 0) {
673 StrBufPlain(openid_delegate, SKEY(oiddata->claimed_id));
676 /* Assemble a URL to which the user-agent will be redirected. */
678 RedirectUrl = NewStrBufDup(oiddata->server);
680 StrBufAppendBufPlain(RedirectUrl, HKEY("?openid.mode=checkid_setup"
681 "&openid.identity="), 0);
682 StrBufUrlescAppend(RedirectUrl, openid_delegate, NULL);
684 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.return_to="), 0);
685 StrBufUrlescAppend(RedirectUrl, return_to, NULL);
687 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.trust_root="), 0);
688 StrBufUrlescAppend(RedirectUrl, trust_root, NULL);
690 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.sreg.optional="), 0);
691 StrBufUrlescAppend(RedirectUrl, NULL, "nickname,email,fullname,postcode,country,dob,gender");
693 cprintf("%d %s\n", CIT_OK, ChrPtr(RedirectUrl));
696 FreeStrBuf(&ReplyBuf);
697 FreeStrBuf(&return_to);
698 FreeStrBuf(&trust_root);
699 FreeStrBuf(&openid_delegate);
700 FreeStrBuf(&RedirectUrl);
705 FreeStrBuf(&ReplyBuf);
706 FreeStrBuf(&return_to);
707 FreeStrBuf(&trust_root);
708 FreeStrBuf(&openid_delegate);
709 FreeStrBuf(&RedirectUrl);
711 cprintf("%d Unable to fetch OpenID URL\n", ERROR);
719 * Finalize an OpenID authentication
721 void cmd_oidf(char *argbuf) {
726 HashList *keys = NULL;
727 ctdl_openid *oiddata = (ctdl_openid *) CC->openid_data;
729 if (oiddata == NULL) {
730 cprintf("%d run OIDS first.\n", ERROR + INTERNAL_ERROR);
733 if (StrLength(oiddata->server) == 0){
734 cprintf("%d need a remote server to authenticate against\n", ERROR + ILLEGAL_VALUE);
737 keys = NewHash(1, NULL);
739 cprintf("%d NewHash() failed\n", ERROR + INTERNAL_ERROR);
742 cprintf("%d Transmit OpenID data now\n", START_CHAT_MODE);
744 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
745 len = extract_token(thiskey, buf, 0, '|', sizeof thiskey);
747 len = sizeof(thiskey) - 1;
748 extract_token(thisdata, buf, 1, '|', sizeof thisdata);
749 CtdlLogPrintf(CTDL_DEBUG, "%s: [%d] %s\n", thiskey, strlen(thisdata), thisdata);
750 Put(keys, thiskey, len, strdup(thisdata), NULL);
754 /* Now that we have all of the parameters, we have to validate the signature against the server */
755 CtdlLogPrintf(CTDL_DEBUG, "About to validate the signature...\n");
759 struct curl_httppost *formpost = NULL;
760 struct curl_httppost *lastptr = NULL;
761 char errmsg[1024] = "";
762 char *o_assoc_handle = NULL;
764 char *o_signed = NULL;
765 int num_signed_values;
768 char k_o_keyname[128];
769 char *k_value = NULL;
772 curl_formadd(&formpost, &lastptr,
773 CURLFORM_COPYNAME, "openid.mode",
774 CURLFORM_COPYCONTENTS, "check_authentication",
776 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.mode", "check_authentication");
778 if (GetHash(keys, "assoc_handle", 12, (void *) &o_assoc_handle)) {
779 curl_formadd(&formpost, &lastptr,
780 CURLFORM_COPYNAME, "openid.assoc_handle",
781 CURLFORM_COPYCONTENTS, o_assoc_handle,
783 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.assoc_handle", o_assoc_handle);
786 if (GetHash(keys, "sig", 3, (void *) &o_sig)) {
787 curl_formadd(&formpost, &lastptr,
788 CURLFORM_COPYNAME, "openid.sig",
789 CURLFORM_COPYCONTENTS, o_sig,
791 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.sig", o_sig);
794 if (GetHash(keys, "signed", 6, (void *) &o_signed)) {
795 curl_formadd(&formpost, &lastptr,
796 CURLFORM_COPYNAME, "openid.signed",
797 CURLFORM_COPYCONTENTS, o_signed,
799 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.signed", o_signed);
801 num_signed_values = num_tokens(o_signed, ',');
802 for (i=0; i<num_signed_values; ++i) {
803 extract_token(k_keyname, o_signed, i, ',', sizeof k_keyname);
804 if (strcasecmp(k_keyname, "mode")) { // work around phpMyID bug
805 if (GetHash(keys, k_keyname, strlen(k_keyname), (void *) &k_value)) {
806 snprintf(k_o_keyname, sizeof k_o_keyname, "openid.%s", k_keyname);
807 curl_formadd(&formpost, &lastptr,
808 CURLFORM_COPYNAME, k_o_keyname,
809 CURLFORM_COPYCONTENTS, k_value,
811 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", k_o_keyname, k_value);
814 CtdlLogPrintf(CTDL_INFO, "OpenID: signed field '%s' is missing\n",
821 ReplyBuf = NewStrBuf();
823 curl = curl_easy_init();
824 curl_easy_setopt(curl, CURLOPT_URL, ChrPtr(oiddata->server));
825 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
826 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
828 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
829 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
830 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
831 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
832 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
833 #ifdef CURLOPT_HTTP_CONTENT_DECODING
834 curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
835 curl_easy_setopt(curl, CURLOPT_ENCODING, "");
837 curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
838 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180); /* die after 180 seconds */
840 (!IsEmptyStr(config.c_ip_addr))
841 && (strcmp(config.c_ip_addr, "*"))
842 && (strcmp(config.c_ip_addr, "::"))
843 && (strcmp(config.c_ip_addr, "0.0.0.0"))
845 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
848 res = curl_easy_perform(curl);
850 CtdlLogPrintf(CTDL_DEBUG, "cmd_oidf() libcurl error %d: %s\n", res, errmsg);
852 curl_easy_cleanup(curl);
853 curl_formfree(formpost);
855 if (cbmstrcasestr(ChrPtr(ReplyBuf), "is_valid:true")) {
856 oiddata->verified = 1;
858 FreeStrBuf(&ReplyBuf);
860 CtdlLogPrintf(CTDL_DEBUG, "Authentication %s.\n", (oiddata->verified ? "succeeded" : "failed") );
862 /* Respond to the client */
864 if (oiddata->verified) {
866 /* If we were already logged in, attach the OpenID to the user's account */
868 if (attach_openid(&CC->user, oiddata->claimed_id) == 0) {
870 CtdlLogPrintf(CTDL_DEBUG, "OpenID attach succeeded\n");
874 CtdlLogPrintf(CTDL_DEBUG, "OpenID attach failed\n");
878 /* Otherwise, a user is attempting to log in using the verified OpenID */
881 * Existing user who has claimed this OpenID?
883 * Note: if you think that sending the password back over the wire is insecure,
884 * check your assumptions. If someone has successfully asserted an OpenID that
885 * is associated with the account, they already have password equivalency and can
886 * login, so they could just as easily change the password, etc.
888 if (login_via_openid(oiddata->claimed_id) == 0) {
889 cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
890 logged_in_response();
891 CtdlLogPrintf(CTDL_DEBUG, "Logged in using previously claimed OpenID\n");
895 * If this system does not allow self-service new user registration, the
896 * remaining modes do not apply, so fail here and now.
898 else if (config.c_disable_newu) {
900 CtdlLogPrintf(CTDL_DEBUG, "Creating user failed due to local policy\n");
904 * New user whose OpenID is verified and Simple Registration Extension is in use?
906 else if (openid_create_user_via_sreg(oiddata->claimed_id, keys) == 0) {
907 cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
908 logged_in_response();
909 CtdlLogPrintf(CTDL_DEBUG, "Successfully auto-created new user\n");
913 * OpenID is verified, but the desired username either was not specified or
914 * conflicts with an existing user. Manual account creation is required.
917 char *desired_name = NULL;
918 cprintf("verify_only\n");
919 cprintf("%s\n", ChrPtr(oiddata->claimed_id));
920 if (GetHash(keys, "sreg.nickname", 13, (void *) &desired_name)) {
921 cprintf("%s\n", desired_name);
926 CtdlLogPrintf(CTDL_DEBUG, "The desired Simple Registration name is already taken.\n");
935 if (oiddata->sreg_keys != NULL) {
936 DeleteHash(&oiddata->sreg_keys);
937 oiddata->sreg_keys = NULL;
939 oiddata->sreg_keys = keys;
944 /**************************************************************************/
946 /* Functions in this section handle module initialization and shutdown */
948 /**************************************************************************/
953 CTDL_MODULE_INIT(openid_rp)
956 curl_global_init(CURL_GLOBAL_ALL);
958 /* Only enable the OpenID command set when native mode authentication is in use. */
959 if (config.c_auth_mode == AUTHMODE_NATIVE) {
960 CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
961 CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
962 CtdlRegisterProtoHook(cmd_oidl, "OIDL", "List OpenIDs associated with an account");
963 CtdlRegisterProtoHook(cmd_oidd, "OIDD", "Detach an OpenID from an account");
964 CtdlRegisterProtoHook(cmd_oidc, "OIDC", "Create new user after validating OpenID");
965 CtdlRegisterProtoHook(cmd_oida, "OIDA", "List all OpenIDs in the database");
967 CtdlRegisterSessionHook(openid_cleanup_function, EVT_LOGOUT);
968 CtdlRegisterUserHook(openid_purge, EVT_PURGEUSER);
971 /* return our Subversion id for the Log */