4 * This is an implementation of OpenID 1.1 Relying Party support, in stateless mode.
16 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
32 #include <curl/curl.h>
33 #include "ctdl_module.h"
35 #include "citserver.h"
39 char claimed_id[1024];
49 /**************************************************************************/
51 /* Functions in this section handle Citadel internal OpenID mapping stuff */
53 /**************************************************************************/
57 * The structure of an openid record *key* is:
59 * |--------------claimed_id-------------|
60 * (actual length of claimed id)
63 * The structure of an openid record *value* is:
65 * |-----user_number----|------------claimed_id---------------|
66 * (sizeof long) (actual length of claimed id)
73 * Attach an OpenID to a Citadel account
75 int attach_openid(struct ctdluser *who, char *claimed_id)
77 struct cdbdata *cdboi;
84 if (!claimed_id) return(1);
85 if (IsEmptyStr(claimed_id)) return(1);
87 /* Check to see if this OpenID is already in the database */
89 cdboi = cdb_fetch(CDB_OPENID, claimed_id, strlen(claimed_id));
91 memcpy(&fetched_usernum, cdboi->ptr, sizeof(long));
94 if (fetched_usernum == who->usernum) {
95 CtdlLogPrintf(CTDL_INFO, "%s already associated; no action is taken\n", claimed_id);
99 CtdlLogPrintf(CTDL_INFO, "%s already belongs to another user\n", claimed_id);
104 /* Not already in the database, so attach it now */
106 data_len = sizeof(long) + strlen(claimed_id) + 1;
107 data = malloc(data_len);
109 memcpy(data, &who->usernum, sizeof(long));
110 memcpy(&data[sizeof(long)], claimed_id, strlen(claimed_id) + 1);
112 cdb_store(CDB_OPENID, claimed_id, strlen(claimed_id), data, data_len);
115 snprintf(buf, sizeof buf, "User <%s> (#%ld) has claimed the OpenID URL %s\n",
116 who->fullname, who->usernum, claimed_id);
117 aide_message(buf, "OpenID claim");
118 CtdlLogPrintf(CTDL_INFO, "%s", buf);
125 * When a user is being deleted, we have to delete any OpenID associations
127 void openid_purge(struct ctdluser *usbuf) {
128 struct cdbdata *cdboi;
129 HashList *keys = NULL;
131 char *deleteme = NULL;
136 keys = NewHash(1, NULL);
140 cdb_rewind(CDB_OPENID);
141 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
142 if (cdboi->len > sizeof(long)) {
143 if (((long)*(cdboi->ptr)) == usbuf->usernum) {
144 deleteme = strdup(cdboi->ptr + sizeof(long)),
145 Put(keys, deleteme, strlen(deleteme), deleteme, generic_free_handler);
151 /* Go through the hash list, deleting keys we stored in it */
153 HashPos = GetNewHashPos(keys, 0);
154 while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
156 CtdlLogPrintf(CTDL_DEBUG, "Deleting associated OpenID <%s>\n", Value);
157 cdb_delete(CDB_OPENID, Value, strlen(Value));
158 /* note: don't free(Value) -- deleting the hash list will handle this for us */
160 DeleteHashPos(&HashPos);
167 * List the OpenIDs associated with the currently logged in account
169 void cmd_oidl(char *argbuf) {
170 struct cdbdata *cdboi;
172 if (CtdlAccessCheck(ac_logged_in)) return;
173 cdb_rewind(CDB_OPENID);
174 cprintf("%d Associated OpenIDs:\n", LISTING_FOLLOWS);
176 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
177 if (cdboi->len > sizeof(long)) {
178 if (((long)*(cdboi->ptr)) == CC->user.usernum) {
179 cprintf("%s\n", cdboi->ptr + sizeof(long));
189 * List ALL OpenIDs in the database
191 void cmd_oida(char *argbuf) {
192 struct cdbdata *cdboi;
195 if (CtdlAccessCheck(ac_aide)) return;
196 cdb_rewind(CDB_OPENID);
197 cprintf("%d List of all OpenIDs in the database:\n", LISTING_FOLLOWS);
199 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
200 if (cdboi->len > sizeof(long)) {
201 memcpy(&usernum, cdboi->ptr, sizeof(long));
203 cdboi->ptr + sizeof(long),
214 * Attempt to register (populate the vCard) the currently-logged-in user
215 * using the data from Simple Registration Extension, if present.
217 void populate_vcard_from_sreg(HashList *sreg_keys) {
220 int pop = 0; /* number of fields populated */
222 char *postcode = NULL;
223 char *country = NULL;
225 if (!sreg_keys) return;
229 if (GetHash(sreg_keys, "identity", 8, (void *) &data)) {
230 vcard_add_prop(v, "url;type=openid", data);
234 if (GetHash(sreg_keys, "sreg.email", 10, (void *) &data)) {
235 vcard_add_prop(v, "email;internet", data);
239 if (GetHash(sreg_keys, "sreg.nickname", 13, (void *) &data)) {
240 vcard_add_prop(v, "nickname", data);
244 if (GetHash(sreg_keys, "sreg.fullname", 13, (void *) &data)) {
246 vcard_add_prop(v, "fn", data);
247 vcard_fn_to_n(n, data, sizeof n);
248 vcard_add_prop(v, "n", n);
252 if (!GetHash(sreg_keys, "sreg.postcode", 13, (void *) &postcode)) {
256 if (!GetHash(sreg_keys, "sreg.country", 12, (void *) &country)) {
260 if (postcode || country) {
262 snprintf(adr, sizeof adr, ";;;;;%s;%s",
263 (postcode ? postcode : ""),
264 (country ? country : "")
266 vcard_add_prop(v, "adr", adr);
270 if (GetHash(sreg_keys, "sreg.dob", 8, (void *) &data)) {
271 vcard_add_prop(v, "bday", data);
275 if (GetHash(sreg_keys, "sreg.gender", 11, (void *) &data)) {
276 vcard_add_prop(v, "x-funambol-gender", data);
280 /* Only save the vCard if there is some useful data in it */
283 ser = vcard_serialize(v);
285 CtdlWriteObject(USERCONFIGROOM, "text/x-vcard",
286 ser, strlen(ser)+1, &CC->user, 0, 0, 0
296 * Create a new user account, manually specifying the name, after successfully
297 * verifying an OpenID (which will of course be attached to the account)
299 void cmd_oidc(char *argbuf) {
300 struct ctdl_openid *oiddata = (struct ctdl_openid *) CC->openid_data;
303 cprintf("%d You have not verified an OpenID yet.\n", ERROR);
307 if (!oiddata->verified) {
308 cprintf("%d You have not verified an OpenID yet.\n", ERROR);
312 /* We can make the semantics of OIDC exactly the same as NEWU, simply
313 * by _calling_ cmd_newu() and letting it run. Very clever!
317 /* Now, if this logged us in, we have to attach the OpenID */
319 attach_openid(&CC->user, oiddata->claimed_id);
320 if (oiddata->sreg_keys != NULL) {
321 populate_vcard_from_sreg(oiddata->sreg_keys);
331 * Detach an OpenID from the currently logged in account
333 void cmd_oidd(char *argbuf) {
334 struct cdbdata *cdboi;
335 char id_to_detach[1024];
336 int this_is_mine = 0;
338 if (CtdlAccessCheck(ac_logged_in)) return;
339 extract_token(id_to_detach, argbuf, 0, '|', sizeof id_to_detach);
340 if (IsEmptyStr(id_to_detach)) {
341 cprintf("%d An empty OpenID URL is not allowed.\n", ERROR + ILLEGAL_VALUE);
344 cdb_rewind(CDB_OPENID);
345 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
346 if (cdboi->len > sizeof(long)) {
347 if (((long)*(cdboi->ptr)) == CC->user.usernum) {
355 cprintf("%d That OpenID was not found or not associated with your account.\n",
356 ERROR + ILLEGAL_VALUE);
360 cdb_delete(CDB_OPENID, id_to_detach, strlen(id_to_detach));
361 cprintf("%d %s detached from your account.\n", CIT_OK, id_to_detach);
367 * Attempt to auto-create a new Citadel account using the nickname from Simple Registration Extension
369 int openid_create_user_via_sreg(char *claimed_id, HashList *sreg_keys)
371 char *desired_name = NULL;
372 char new_password[32];
374 if (config.c_auth_mode != AUTHMODE_NATIVE) return(1);
375 if (config.c_disable_newu) return(2);
376 if (CC->logged_in) return(3);
377 if (!GetHash(sreg_keys, "sreg.nickname", 13, (void *) &desired_name)) return(4);
379 CtdlLogPrintf(CTDL_DEBUG, "The desired account name is <%s>\n", desired_name);
381 if (!getuser(&CC->user, desired_name)) {
382 CtdlLogPrintf(CTDL_DEBUG, "<%s> is already taken by another user.\n", desired_name);
383 memset(&CC->user, 0, sizeof(struct ctdluser));
387 /* The desired account name is available. Create the account and log it in! */
388 if (create_user(desired_name, 1)) return(6);
390 snprintf(new_password, sizeof new_password, "%08lx%08lx", random(), random());
391 CtdlSetPassword(new_password);
392 attach_openid(&CC->user, claimed_id);
393 populate_vcard_from_sreg(sreg_keys);
399 * If a user account exists which is associated with the Claimed ID, log it in and return zero.
400 * Otherwise it returns nonzero.
402 int login_via_openid(char *claimed_id)
404 struct cdbdata *cdboi;
407 cdboi = cdb_fetch(CDB_OPENID, claimed_id, strlen(claimed_id));
412 memcpy(&usernum, cdboi->ptr, sizeof(long));
415 if (!getuserbynumber(&CC->user, usernum)) {
416 /* Now become the user we just created */
417 safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
422 memset(&CC->user, 0, sizeof(struct ctdluser));
430 /**************************************************************************/
432 /* Functions in this section handle OpenID protocol */
434 /**************************************************************************/
438 * Locate a <link> tag and, given its 'rel=' parameter, return its 'href' parameter
440 void extract_link(char *target_buf, int target_size, char *rel, char *source_buf)
442 char *ptr = source_buf;
444 if (!target_buf) return;
446 if (!source_buf) return;
450 while (ptr = bmstrcasestr(ptr, "<link"), ptr != NULL) {
452 char work_buffer[2048];
453 char *link_tag_start = NULL;
454 char *link_tag_end = NULL;
459 link_tag_start = ptr;
460 link_tag_end = strchr(ptr, '>');
464 if ((link_tag_end) && (link_tag_end > link_tag_start)) {
466 len = link_tag_end - link_tag_start;
467 if (len > sizeof work_buffer) len = sizeof work_buffer;
468 memcpy(work_buffer, link_tag_start, len);
470 char *rel_start = NULL;
471 char *rel_end = NULL;
472 rel_start = bmstrcasestr(work_buffer, "rel=");
474 rel_start = strchr(rel_start, '\"');
477 rel_end = strchr(rel_start, '\"');
478 if ((rel_end) && (rel_end > rel_start)) {
479 safestrncpy(rel_tag, rel_start, rel_end - rel_start + 1);
484 char *href_start = NULL;
485 char *href_end = NULL;
486 href_start = bmstrcasestr(work_buffer, "href=");
488 href_start = strchr(href_start, '\"');
491 href_end = strchr(href_start, '\"');
492 if ((href_end) && (href_end > href_start)) {
493 safestrncpy(href_tag, href_start, href_end - href_start + 1);
498 if (!strcasecmp(rel, rel_tag)) {
499 safestrncpy(target_buf, href_tag, target_size);
515 int total_bytes_received;
520 size_t fh_callback(void *ptr, size_t size, size_t nmemb, void *stream)
522 struct fh_data *fh = (struct fh_data *) stream;
523 int got_bytes = (size * nmemb);
525 if (fh->total_bytes_received + got_bytes > fh->maxbytes) {
526 got_bytes = fh->maxbytes - fh->total_bytes_received;
529 memcpy(&fh->buf[fh->total_bytes_received], ptr, got_bytes);
530 fh->total_bytes_received += got_bytes;
533 return (size * nmemb); /* always succeed; libcurl doesn't need to know if we truncated it */
539 * Begin an HTTP fetch (returns number of bytes actually fetched, or -1 for error) using libcurl.
541 * If 'normalize_len' is nonzero, the caller is specifying the buffer size of 'url', and is
542 * requesting that the effective (normalized) URL be copied back to it.
544 int fetch_http(char *url, char *target_buf, int maxbytes, int normalize_len)
548 char errmsg[1024] = "";
549 struct fh_data fh = {
554 char *effective_url = NULL;
556 if (!url) return(-1);
557 if (!target_buf) return(-1);
558 memset(target_buf, 0, maxbytes);
560 curl = curl_easy_init();
562 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
566 curl_easy_setopt(curl, CURLOPT_URL, url);
567 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
568 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
569 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fh);
570 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fh_callback);
571 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
572 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
573 #ifdef CURLOPT_HTTP_CONTENT_DECODING
574 curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
575 curl_easy_setopt(curl, CURLOPT_ENCODING, "");
577 curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
578 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180); /* die after 180 seconds */
579 if (!IsEmptyStr(config.c_ip_addr)) {
580 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
582 res = curl_easy_perform(curl);
584 CtdlLogPrintf(CTDL_DEBUG, "fetch_http() libcurl error %d: %s\n", res, errmsg);
586 if (normalize_len > 0) {
587 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
588 safestrncpy(url, effective_url, normalize_len);
590 curl_easy_cleanup(curl);
591 return fh.total_bytes_received;
596 * Setup an OpenID authentication
598 void cmd_oids(char *argbuf) {
599 char return_to[1024];
600 char trust_root[1024];
603 struct CitContext *CCC = CC; /* CachedCitContext - performance boost */
604 struct ctdl_openid *oiddata;
606 oiddata = (struct ctdl_openid *) CCC->openid_data;
608 if (oiddata != NULL) {
609 if (oiddata->sreg_keys != NULL) {
610 DeleteHash(&oiddata->sreg_keys);
611 oiddata->sreg_keys = NULL;
613 free(CCC->openid_data);
615 oiddata = malloc(sizeof(struct ctdl_openid));
616 if (oiddata == NULL) {
617 cprintf("%d malloc failed\n", ERROR + INTERNAL_ERROR);
620 memset(oiddata, 0, sizeof(struct ctdl_openid));
621 CCC->openid_data = (void *) oiddata;
623 extract_token(oiddata->claimed_id, argbuf, 0, '|', sizeof oiddata->claimed_id);
624 extract_token(return_to, argbuf, 1, '|', sizeof return_to);
625 extract_token(trust_root, argbuf, 2, '|', sizeof trust_root);
626 oiddata->verified = 0;
628 i = fetch_http(oiddata->claimed_id, buf, sizeof buf - 1, sizeof oiddata->claimed_id);
629 CtdlLogPrintf(CTDL_DEBUG, "Normalized URL and Claimed ID is: %s\n", oiddata->claimed_id);
630 buf[sizeof buf - 1] = 0;
632 char openid_delegate[1024];
634 extract_link(oiddata->server, sizeof oiddata->server, "openid.server", buf);
635 extract_link(openid_delegate, sizeof openid_delegate, "openid.delegate", buf);
637 if (IsEmptyStr(oiddata->server)) {
638 cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
642 /* Empty delegate is legal; we just use the openid_url instead */
643 if (IsEmptyStr(openid_delegate)) {
644 safestrncpy(openid_delegate, oiddata->claimed_id, sizeof openid_delegate);
647 /* Assemble a URL to which the user-agent will be redirected. */
648 char redirect_string[4096];
649 char escaped_identity[512];
650 char escaped_return_to[2048];
651 char escaped_trust_root[1024];
652 char escaped_sreg_optional[256];
654 urlesc(escaped_identity, sizeof escaped_identity, openid_delegate);
655 urlesc(escaped_return_to, sizeof escaped_return_to, return_to);
656 urlesc(escaped_trust_root, sizeof escaped_trust_root, trust_root);
657 urlesc(escaped_sreg_optional, sizeof escaped_sreg_optional,
658 "nickname,email,fullname,postcode,country,dob,gender");
660 snprintf(redirect_string, sizeof redirect_string,
662 "?openid.mode=checkid_setup"
663 "&openid.identity=%s"
664 "&openid.return_to=%s"
665 "&openid.trust_root=%s"
666 "&openid.sreg.optional=%s"
672 escaped_sreg_optional
674 cprintf("%d %s\n", CIT_OK, redirect_string);
678 cprintf("%d Unable to fetch OpenID URL\n", ERROR);
686 * Finalize an OpenID authentication
688 void cmd_oidf(char *argbuf) {
692 HashList *keys = NULL;
693 struct ctdl_openid *oiddata = (struct ctdl_openid *) CC->openid_data;
695 keys = NewHash(1, NULL);
697 cprintf("%d NewHash() failed\n", ERROR + INTERNAL_ERROR);
701 cprintf("%d Transmit OpenID data now\n", START_CHAT_MODE);
703 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
704 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
705 extract_token(thisdata, buf, 1, '|', sizeof thisdata);
706 CtdlLogPrintf(CTDL_DEBUG, "%s: [%d] %s\n", thiskey, strlen(thisdata), thisdata);
707 Put(keys, thiskey, strlen(thiskey), strdup(thisdata), generic_free_handler);
711 /* Now that we have all of the parameters, we have to validate the signature against the server */
712 CtdlLogPrintf(CTDL_DEBUG, "About to validate the signature...\n");
716 struct curl_httppost *formpost = NULL;
717 struct curl_httppost *lastptr = NULL;
718 char errmsg[1024] = "";
719 char *o_assoc_handle = NULL;
721 char *o_signed = NULL;
722 int num_signed_values;
725 char k_o_keyname[128];
726 char *k_value = NULL;
729 struct fh_data fh = {
735 curl_formadd(&formpost, &lastptr,
736 CURLFORM_COPYNAME, "openid.mode",
737 CURLFORM_COPYCONTENTS, "check_authentication",
739 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.mode", "check_authentication");
741 if (GetHash(keys, "assoc_handle", 12, (void *) &o_assoc_handle)) {
742 curl_formadd(&formpost, &lastptr,
743 CURLFORM_COPYNAME, "openid.assoc_handle",
744 CURLFORM_COPYCONTENTS, o_assoc_handle,
746 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.assoc_handle", o_assoc_handle);
749 if (GetHash(keys, "sig", 3, (void *) &o_sig)) {
750 curl_formadd(&formpost, &lastptr,
751 CURLFORM_COPYNAME, "openid.sig",
752 CURLFORM_COPYCONTENTS, o_sig,
754 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.sig", o_sig);
757 if (GetHash(keys, "signed", 6, (void *) &o_signed)) {
758 curl_formadd(&formpost, &lastptr,
759 CURLFORM_COPYNAME, "openid.signed",
760 CURLFORM_COPYCONTENTS, o_signed,
762 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.signed", o_signed);
764 num_signed_values = num_tokens(o_signed, ',');
765 for (i=0; i<num_signed_values; ++i) {
766 extract_token(k_keyname, o_signed, i, ',', sizeof k_keyname);
767 if (strcasecmp(k_keyname, "mode")) { // work around phpMyID bug
768 if (GetHash(keys, k_keyname, strlen(k_keyname), (void *) &k_value)) {
769 snprintf(k_o_keyname, sizeof k_o_keyname, "openid.%s", k_keyname);
770 curl_formadd(&formpost, &lastptr,
771 CURLFORM_COPYNAME, k_o_keyname,
772 CURLFORM_COPYCONTENTS, k_value,
774 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", k_o_keyname, k_value);
777 CtdlLogPrintf(CTDL_INFO, "OpenID: signed field '%s' is missing\n",
784 curl = curl_easy_init();
785 curl_easy_setopt(curl, CURLOPT_URL, oiddata->server);
786 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
787 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
788 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fh);
789 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fh_callback);
790 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
791 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
792 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
793 #ifdef CURLOPT_HTTP_CONTENT_DECODING
794 curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
795 curl_easy_setopt(curl, CURLOPT_ENCODING, "");
797 curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
798 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180); /* die after 180 seconds */
799 if (!IsEmptyStr(config.c_ip_addr)) {
800 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
803 res = curl_easy_perform(curl);
805 CtdlLogPrintf(CTDL_DEBUG, "cmd_oidf() libcurl error %d: %s\n", res, errmsg);
807 curl_easy_cleanup(curl);
808 curl_formfree(formpost);
810 valbuf[fh.total_bytes_received] = 0;
812 if (bmstrcasestr(valbuf, "is_valid:true")) {
813 oiddata->verified = 1;
816 CtdlLogPrintf(CTDL_DEBUG, "Authentication %s.\n", (oiddata->verified ? "succeeded" : "failed") );
818 /* Respond to the client */
820 if (oiddata->verified) {
822 /* If we were already logged in, attach the OpenID to the user's account */
824 if (attach_openid(&CC->user, oiddata->claimed_id) == 0) {
826 CtdlLogPrintf(CTDL_DEBUG, "OpenID attach succeeded\n");
830 CtdlLogPrintf(CTDL_DEBUG, "OpenID attach failed\n");
834 /* Otherwise, a user is attempting to log in using the verified OpenID */
837 * Existing user who has claimed this OpenID?
839 * Note: if you think that sending the password back over the wire is insecure,
840 * check your assumptions. If someone has successfully asserted an OpenID that
841 * is associated with the account, they already have password equivalency and can
842 * login, so they could just as easily change the password, etc.
844 if (login_via_openid(oiddata->claimed_id) == 0) {
845 cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
846 logged_in_response();
847 CtdlLogPrintf(CTDL_DEBUG, "Logged in using previously claimed OpenID\n");
851 * If this system does not allow self-service new user registration, the
852 * remaining modes do not apply, so fail here and now.
854 else if (config.c_disable_newu) {
856 CtdlLogPrintf(CTDL_DEBUG, "Creating user failed due to local policy\n");
860 * New user whose OpenID is verified and Simple Registration Extension is in use?
862 else if (openid_create_user_via_sreg(oiddata->claimed_id, keys) == 0) {
863 cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
864 logged_in_response();
865 CtdlLogPrintf(CTDL_DEBUG, "Successfully auto-created new user\n");
869 * OpenID is verified, but the desired username either was not specified or
870 * conflicts with an existing user. Manual account creation is required.
873 char *desired_name = NULL;
874 cprintf("verify_only\n");
875 cprintf("%s\n", oiddata->claimed_id);
876 if (GetHash(keys, "sreg.nickname", 13, (void *) &desired_name)) {
877 cprintf("%s\n", desired_name);
882 CtdlLogPrintf(CTDL_DEBUG, "The desired Simple Registration name is already taken.\n");
891 if (oiddata->sreg_keys != NULL) {
892 DeleteHash(&oiddata->sreg_keys);
893 oiddata->sreg_keys = NULL;
895 oiddata->sreg_keys = keys;
900 /**************************************************************************/
902 /* Functions in this section handle module initialization and shutdown */
904 /**************************************************************************/
908 * This cleanup function blows away the temporary memory used by this module.
910 void openid_cleanup_function(void) {
911 struct ctdl_openid *oiddata = (struct ctdl_openid *) CC->openid_data;
913 if (oiddata != NULL) {
914 CtdlLogPrintf(CTDL_DEBUG, "Clearing OpenID session state\n");
915 if (oiddata->sreg_keys != NULL) {
916 DeleteHash(&oiddata->sreg_keys);
917 oiddata->sreg_keys = NULL;
924 CTDL_MODULE_INIT(openid_rp)
927 curl_global_init(CURL_GLOBAL_ALL);
929 /* Only enable the OpenID command set when native mode authentication is in use. */
930 if (config.c_auth_mode == AUTHMODE_NATIVE) {
931 CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
932 CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
933 CtdlRegisterProtoHook(cmd_oidl, "OIDL", "List OpenIDs associated with an account");
934 CtdlRegisterProtoHook(cmd_oidd, "OIDD", "Detach an OpenID from an account");
935 CtdlRegisterProtoHook(cmd_oidc, "OIDC", "Create new user after validating OpenID");
936 CtdlRegisterProtoHook(cmd_oida, "OIDA", "List all OpenIDs in the database");
938 CtdlRegisterSessionHook(openid_cleanup_function, EVT_LOGOUT);
939 CtdlRegisterUserHook(openid_purge, EVT_PURGEUSER);
942 /* return our Subversion id for the Log */