1 // This is an implementation of OpenID 2.0 relying party support in stateless mode.
3 // Copyright (c) 2007-2022 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
8 #include "../../sysdep.h"
16 #include <sys/types.h>
21 #include <curl/curl.h>
23 #include "../../ctdl_module.h"
24 #include "../../config.h"
25 #include "../../citserver.h"
26 #include "../../user_ops.h"
28 typedef struct _ctdl_openid {
29 StrBuf *op_url; // OpenID Provider Endpoint URL
30 StrBuf *claimed_id; // Claimed Identifier
42 void Free_ctdl_openid(ctdl_openid **FreeMe) {
43 if (*FreeMe == NULL) {
46 FreeStrBuf(&(*FreeMe)->op_url);
47 FreeStrBuf(&(*FreeMe)->claimed_id);
48 DeleteHash(&(*FreeMe)->sreg_keys);
54 // This cleanup function blows away the temporary memory used by this module.
55 void openid_cleanup_function(void) {
57 if (CC->openid_data != NULL) {
58 syslog(LOG_DEBUG, "openid: Clearing OpenID session state");
59 Free_ctdl_openid((ctdl_openid **) &CC->openid_data);
64 // Functions in this section handle Citadel internal OpenID mapping stuff
67 // The structure of an openid record *key* is:
69 // |--------------claimed_id-------------|
70 // (actual length of claimed id)
73 // The structure of an openid record *value* is:
75 // |-----user_number----|------------claimed_id---------------|
76 // (sizeof long) (actual length of claimed id)
80 // Attach an external authenticator (such as an OpenID) to a Citadel account
81 int attach_extauth(struct ctdluser *who, StrBuf *claimed_id) {
82 struct cdbdata *cdboi;
89 if (StrLength(claimed_id)==0) return(1);
91 // Check to see if this authenticator is already in the database
93 cdboi = cdb_fetch(CDB_EXTAUTH, ChrPtr(claimed_id), StrLength(claimed_id));
95 memcpy(&fetched_usernum, cdboi->ptr, sizeof(long));
98 if (fetched_usernum == who->usernum) {
99 syslog(LOG_INFO, "openid: %s already associated; no action is taken", ChrPtr(claimed_id));
103 syslog(LOG_INFO, "openid: %s already belongs to another user", ChrPtr(claimed_id));
108 // Not already in the database, so attach it now
110 data_len = sizeof(long) + StrLength(claimed_id) + 1;
111 data = malloc(data_len);
113 memcpy(data, &who->usernum, sizeof(long));
114 memcpy(&data[sizeof(long)], ChrPtr(claimed_id), StrLength(claimed_id) + 1);
116 cdb_store(CDB_EXTAUTH, ChrPtr(claimed_id), StrLength(claimed_id), data, data_len);
119 snprintf(buf, sizeof buf, "User <%s> (#%ld) is now associated with %s\n", who->fullname, who->usernum, ChrPtr(claimed_id));
120 CtdlAideMessage(buf, "External authenticator claim");
121 syslog(LOG_INFO, "openid: %s", buf);
126 // When a user is being deleted, we have to delete any OpenID associations
127 void extauth_purge(struct ctdluser *usbuf) {
128 struct cdbdata *cdboi;
129 HashList *keys = NULL;
131 char *deleteme = NULL;
137 keys = NewHash(1, NULL);
140 cdb_rewind(CDB_EXTAUTH);
141 while (cdboi = cdb_next_item(CDB_EXTAUTH), cdboi != NULL) {
142 if (cdboi->len > sizeof(long)) {
143 memcpy(&usernum, cdboi->ptr, sizeof(long));
144 if (usernum == usbuf->usernum) {
145 deleteme = strdup(cdboi->ptr + sizeof(long)),
146 Put(keys, deleteme, strlen(deleteme), deleteme, NULL);
152 // Go through the hash list, deleting keys we stored in it
154 HashPos = GetNewHashPos(keys, 0);
155 while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
157 syslog(LOG_DEBUG, "openid: deleting associated external authenticator <%s>", (char*)Value);
158 cdb_delete(CDB_EXTAUTH, Value, strlen(Value));
159 // note: don't free(Value) -- deleting the hash list will handle this for us
161 DeleteHashPos(&HashPos);
166 // List the OpenIDs associated with the currently logged in account
167 void cmd_oidl(char *argbuf) {
168 struct cdbdata *cdboi;
171 if (CtdlGetConfigInt("c_disable_newu")) {
172 cprintf("%d this system does not support openid.\n", ERROR + CMD_NOT_SUPPORTED);
175 if (CtdlAccessCheck(ac_logged_in)) return;
177 cdb_rewind(CDB_EXTAUTH);
178 cprintf("%d Associated external authenticators:\n", LISTING_FOLLOWS);
180 while (cdboi = cdb_next_item(CDB_EXTAUTH), cdboi != NULL) {
181 if (cdboi->len > sizeof(long)) {
182 memcpy(&usernum, cdboi->ptr, sizeof(long));
183 if (usernum == CC->user.usernum) {
184 cprintf("%s\n", cdboi->ptr + sizeof(long));
193 // List ALL OpenIDs in the database
194 void cmd_oida(char *argbuf) {
195 struct cdbdata *cdboi;
197 struct ctdluser usbuf;
199 if (CtdlGetConfigInt("c_disable_newu")) {
200 cprintf("%d this system does not support openid.\n",
201 ERROR + CMD_NOT_SUPPORTED);
204 if (CtdlAccessCheck(ac_aide)) return;
205 cdb_rewind(CDB_EXTAUTH);
206 cprintf("%d List of all OpenIDs in the database:\n", LISTING_FOLLOWS);
208 while (cdboi = cdb_next_item(CDB_EXTAUTH), cdboi != NULL) {
209 if (cdboi->len > sizeof(long)) {
210 memcpy(&usernum, cdboi->ptr, sizeof(long));
211 if (CtdlGetUserByNumber(&usbuf, usernum) != 0) {
212 usbuf.fullname[0] = 0;
214 cprintf("%s|%ld|%s\n",
215 cdboi->ptr + sizeof(long),
226 // Create a new user account, manually specifying the name, after successfully
227 // verifying an OpenID (which will of course be attached to the account)
228 void cmd_oidc(char *argbuf) {
229 ctdl_openid *oiddata = (ctdl_openid *) CC->openid_data;
231 if (CtdlGetConfigInt("c_disable_newu")) {
232 cprintf("%d this system does not support openid.\n",
233 ERROR + CMD_NOT_SUPPORTED);
236 if ( (!oiddata) || (!oiddata->verified) ) {
237 cprintf("%d You have not verified an OpenID yet.\n", ERROR);
241 // We can make the semantics of OIDC exactly the same as NEWU, simply
242 // by _calling_ cmd_newu() and letting it run. Very clever!
245 // Now, if this logged us in, we have to attach the OpenID
247 attach_extauth(&CC->user, oiddata->claimed_id);
253 // Detach an OpenID from the currently logged in account
254 void cmd_oidd(char *argbuf) {
255 struct cdbdata *cdboi;
256 char id_to_detach[1024];
257 int this_is_mine = 0;
260 if (CtdlGetConfigInt("c_disable_newu")) {
261 cprintf("%d this system does not support openid.\n",
262 ERROR + CMD_NOT_SUPPORTED);
265 if (CtdlAccessCheck(ac_logged_in)) return;
266 extract_token(id_to_detach, argbuf, 0, '|', sizeof id_to_detach);
267 if (IsEmptyStr(id_to_detach)) {
268 cprintf("%d An empty OpenID URL is not allowed.\n", ERROR + ILLEGAL_VALUE);
271 cdb_rewind(CDB_EXTAUTH);
272 while (cdboi = cdb_next_item(CDB_EXTAUTH), cdboi != NULL) {
273 if (cdboi->len > sizeof(long)) {
274 memcpy(&usernum, cdboi->ptr, sizeof(long));
275 if (usernum == CC->user.usernum) {
283 cprintf("%d That OpenID was not found or not associated with your account.\n",
284 ERROR + ILLEGAL_VALUE);
288 cdb_delete(CDB_EXTAUTH, id_to_detach, strlen(id_to_detach));
289 cprintf("%d %s detached from your account.\n", CIT_OK, id_to_detach);
293 // Attempt to auto-create a new Citadel account using the nickname from Attribute Exchange
294 int openid_create_user_via_ax(StrBuf *claimed_id, HashList *sreg_keys) {
295 char *nickname = NULL;
296 char *firstname = NULL;
297 char *lastname = NULL;
298 char new_password[32];
303 if (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_NATIVE) return(1);
304 if (CtdlGetConfigInt("c_disable_newu")) return(2);
305 if (CC->logged_in) return(3);
307 HashPos *HashPos = GetNewHashPos(sreg_keys, 0);
308 while (GetNextHashPos(sreg_keys, HashPos, &len, &Key, &Value) != 0) {
309 syslog(LOG_DEBUG, "openid: %s = %s", Key, (char *)Value);
311 if (cbmstrcasestr(Key, "value.nickname") != NULL) {
312 nickname = (char *)Value;
314 else if ( (nickname == NULL) && (cbmstrcasestr(Key, "value.nickname") != NULL)) {
315 nickname = (char *)Value;
317 else if (cbmstrcasestr(Key, "value.firstname") != NULL) {
318 firstname = (char *)Value;
320 else if (cbmstrcasestr(Key, "value.lastname") != NULL) {
321 lastname = (char *)Value;
325 DeleteHashPos(&HashPos);
327 if (nickname == NULL) {
328 if ((firstname != NULL) || (lastname != NULL)) {
329 char fullname[1024] = "";
330 if (firstname) strcpy(fullname, firstname);
331 if (firstname && lastname) strcat(fullname, " ");
332 if (lastname) strcat(fullname, lastname);
337 if (nickname == NULL) {
340 syslog(LOG_DEBUG, "openid: the desired account name is <%s>", nickname);
342 if (!CtdlGetUser(&CC->user, nickname)) {
343 syslog(LOG_DEBUG, "openid: <%s> is already taken by another user.", nickname);
344 memset(&CC->user, 0, sizeof(struct ctdluser));
348 // The desired account name is available. Create the account and log it in!
349 if (create_user(nickname, CREATE_USER_BECOME_USER, NATIVE_AUTH_UID)) return(6);
351 // Generate a random password.
352 // The user doesn't care what the password is since he is using OpenID.
353 snprintf(new_password, sizeof new_password, "%08lx%08lx", random(), random());
354 CtdlSetPassword(new_password);
356 // Now attach the verified OpenID to this account.
357 attach_extauth(&CC->user, claimed_id);
363 // If a user account exists which is associated with the Claimed ID, log it in and return zero.
364 // Otherwise it returns nonzero.
365 int login_via_extauth(StrBuf *claimed_id) {
366 struct cdbdata *cdboi;
369 cdboi = cdb_fetch(CDB_EXTAUTH, ChrPtr(claimed_id), StrLength(claimed_id));
374 memcpy(&usernum, cdboi->ptr, sizeof(long));
377 if (!CtdlGetUserByNumber(&CC->user, usernum)) {
378 // Now become the user we just created
379 safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
384 memset(&CC->user, 0, sizeof(struct ctdluser));
390 // Functions in this section handle OpenID protocol
393 // Locate a <link> tag and, given its 'rel=' parameter, return its 'href' parameter
394 void extract_link(StrBuf *target_buf, const char *rel, long repllen, StrBuf *source_buf) {
397 const char *href_start = NULL;
398 const char *href_end = NULL;
399 const char *link_tag_start = NULL;
400 const char *link_tag_end = NULL;
401 const char *rel_start = NULL;
402 const char *rel_end = NULL;
404 if (!target_buf) return;
406 if (!source_buf) return;
408 ptr = ChrPtr(source_buf);
410 FlushStrBuf(target_buf);
411 while (ptr = cbmstrcasestr(ptr, "<link"), ptr != NULL) {
413 link_tag_start = ptr;
414 link_tag_end = strchr(ptr, '>');
415 if (link_tag_end == NULL)
417 for (i=0; i < 1; i++ ){
418 rel_start = cbmstrcasestr(link_tag_start, "rel=");
419 if ((rel_start == NULL) ||
420 (rel_start > link_tag_end))
423 rel_start = strchr(rel_start, '\"');
424 if ((rel_start == NULL) ||
425 (rel_start > link_tag_end))
428 rel_end = strchr(rel_start, '\"');
429 if ((rel_end == NULL) ||
430 (rel_end == rel_start) ||
431 (rel_end >= link_tag_end) )
433 if (strncasecmp(rel, rel_start, repllen)!= 0)
434 continue; // didn't match? never mind...
436 href_start = cbmstrcasestr(link_tag_start, "href=");
437 if ((href_start == NULL) ||
438 (href_start >= link_tag_end))
440 href_start = strchr(href_start, '\"');
441 if ((href_start == NULL) |
442 (href_start >= link_tag_end))
445 href_end = strchr(href_start, '\"');
446 if ((href_end == NULL) ||
447 (href_end == href_start) ||
448 (href_start >= link_tag_end))
450 StrBufPlain(target_buf, href_start, href_end - href_start);
457 // Wrapper for curl_easy_init() that includes the options common to all calls used in this module.
458 CURL *ctdl_openid_curl_easy_init(char *errmsg) {
461 curl = curl_easy_init();
466 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
467 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
470 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
472 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
473 #ifdef CURLOPT_HTTP_CONTENT_DECODING
474 curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
475 curl_easy_setopt(curl, CURLOPT_ENCODING, "");
477 curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
478 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30); // die after 30 seconds
481 (!IsEmptyStr(CtdlGetConfigStr("c_ip_addr")))
482 && (strcmp(CtdlGetConfigStr("c_ip_addr"), "*"))
483 && (strcmp(CtdlGetConfigStr("c_ip_addr"), "::"))
484 && (strcmp(CtdlGetConfigStr("c_ip_addr"), "0.0.0.0"))
486 curl_easy_setopt(curl, CURLOPT_INTERFACE, CtdlGetConfigStr("c_ip_addr"));
497 int current_service_priority;
498 int selected_service_priority;
499 StrBuf *current_service_uri;
500 StrBuf *selected_service_uri;
501 int current_service_is_oid2auth;
505 void xrds_xml_start(void *data, const char *supplied_el, const char **attr) {
506 struct xrds *xrds = (struct xrds *) data;
509 ++xrds->nesting_level;
511 if (!strcasecmp(supplied_el, "XRD")) {
515 else if (!strcasecmp(supplied_el, "service")) {
516 xrds->current_service_priority = 0;
517 xrds->current_service_is_oid2auth = 0;
518 for (i=0; attr[i] != NULL; i+=2) {
519 if (!strcasecmp(attr[i], "priority")) {
520 xrds->current_service_priority = atoi(attr[i+1]);
525 FlushStrBuf(xrds->CharData);
529 void xrds_xml_end(void *data, const char *supplied_el) {
530 struct xrds *xrds = (struct xrds *) data;
532 --xrds->nesting_level;
534 if (!strcasecmp(supplied_el, "XRD")) {
538 else if (!strcasecmp(supplied_el, "type")) {
540 && (!strcasecmp(ChrPtr(xrds->CharData), "http://specs.openid.net/auth/2.0/server"))
542 xrds->current_service_is_oid2auth = 1;
545 && (!strcasecmp(ChrPtr(xrds->CharData), "http://specs.openid.net/auth/2.0/signon"))
547 xrds->current_service_is_oid2auth = 1;
548 // FIXME in this case, the Claimed ID should be considered immutable
552 else if (!strcasecmp(supplied_el, "uri")) {
554 FlushStrBuf(xrds->current_service_uri);
555 StrBufAppendBuf(xrds->current_service_uri, xrds->CharData, 0);
559 else if (!strcasecmp(supplied_el, "service")) {
561 && (xrds->current_service_priority < xrds->selected_service_priority)
562 && (xrds->current_service_is_oid2auth)
564 xrds->selected_service_priority = xrds->current_service_priority;
565 FlushStrBuf(xrds->selected_service_uri);
566 StrBufAppendBuf(xrds->selected_service_uri, xrds->current_service_uri, 0);
571 FlushStrBuf(xrds->CharData);
575 void xrds_xml_chardata(void *data, const XML_Char *s, int len) {
576 struct xrds *xrds = (struct xrds *) data;
578 StrBufAppendBufPlain (xrds->CharData, s, len, 0);
582 // Parse an XRDS document.
583 // If an OpenID Provider URL is discovered, op_url to that value and return nonzero.
584 // If nothing useful happened, return 0.
585 int parse_xrds_document(StrBuf *ReplyBuf) {
586 ctdl_openid *oiddata = (ctdl_openid *) CC->openid_data;
588 int return_value = 0;
590 memset(&xrds, 0, sizeof (struct xrds));
591 xrds.selected_service_priority = INT_MAX;
592 xrds.CharData = NewStrBuf();
593 xrds.current_service_uri = NewStrBuf();
594 xrds.selected_service_uri = NewStrBuf();
595 XML_Parser xp = XML_ParserCreate(NULL);
597 XML_SetUserData(xp, &xrds);
598 XML_SetElementHandler(xp, xrds_xml_start, xrds_xml_end);
599 XML_SetCharacterDataHandler(xp, xrds_xml_chardata);
600 XML_Parse(xp, ChrPtr(ReplyBuf), StrLength(ReplyBuf), 0);
601 XML_Parse(xp, "", 0, 1);
605 syslog(LOG_ERR, "openid: cannot create XML parser");
608 if (xrds.selected_service_priority < INT_MAX) {
609 if (oiddata->op_url == NULL) {
610 oiddata->op_url = NewStrBuf();
612 FlushStrBuf(oiddata->op_url);
613 StrBufAppendBuf(oiddata->op_url, xrds.selected_service_uri, 0);
614 return_value = openid_disco_xrds;
617 FreeStrBuf(&xrds.CharData);
618 FreeStrBuf(&xrds.current_service_uri);
619 FreeStrBuf(&xrds.selected_service_uri);
621 return(return_value);
625 // Callback function for perform_openid2_discovery()
626 // We're interested in the X-XRDS-Location: header.
627 size_t yadis_headerfunction(void *ptr, size_t size, size_t nmemb, void *userdata) {
629 StrBuf **x_xrds_location = (StrBuf **) userdata;
631 memcpy(hdr, ptr, (size*nmemb));
634 if (!strncasecmp(hdr, "X-XRDS-Location:", 16)) {
635 *x_xrds_location = NewStrBufPlain(&hdr[16], ((size*nmemb)-16));
636 StrBufTrim(*x_xrds_location);
639 return(size * nmemb);
643 // Attempt to perform Yadis discovery as specified in Yadis 1.0 section 6.2.5.
645 // If Yadis fails, we then attempt HTML discovery using the same document.
647 // If successful, returns nonzero and calls parse_xrds_document() to act upon the received data.
648 // If fails, returns 0 and does nothing else.
649 int perform_openid2_discovery(StrBuf *SuppliedURL) {
650 ctdl_openid *oiddata = (ctdl_openid *) CC->openid_data;
652 StrBuf *ReplyBuf = NULL;
653 int return_value = 0;
656 char errmsg[1024] = "";
657 struct curl_slist *my_headers = NULL;
658 StrBuf *x_xrds_location = NULL;
660 if (!SuppliedURL) return(0);
661 syslog(LOG_DEBUG, "openid: perform_openid2_discovery(%s)", ChrPtr(SuppliedURL));
662 if (StrLength(SuppliedURL) == 0) return(0);
664 ReplyBuf = NewStrBuf();
665 if (!ReplyBuf) return(0);
667 curl = ctdl_openid_curl_easy_init(errmsg);
668 if (!curl) return(0);
670 curl_easy_setopt(curl, CURLOPT_URL, ChrPtr(SuppliedURL));
671 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
672 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
674 my_headers = curl_slist_append(my_headers, "Accept:"); // disable the default Accept: header
675 my_headers = curl_slist_append(my_headers, "Accept: application/xrds+xml");
676 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, my_headers);
678 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &x_xrds_location);
679 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, yadis_headerfunction);
681 result = curl_easy_perform(curl);
683 syslog(LOG_DEBUG, "openid: libcurl error %d: %s", result, errmsg);
685 curl_slist_free_all(my_headers);
686 curl_easy_cleanup(curl);
687 docbytes = StrLength(ReplyBuf);
689 // The response from the server will be one of:
691 // Option 1: An HTML document with a <head> element that includes a <meta> element with http-equiv
692 // attribute, X-XRDS-Location,
694 // Does any provider actually do this? If so then we will implement it in the future.
696 // Option 2: HTTP response-headers that include an X-XRDS-Location response-header,
697 // together with a document.
698 // Option 3: HTTP response-headers only, which MAY include an X-XRDS-Location response-header,
699 // a contenttype response-header specifying MIME media type,
700 // application/xrds+xml, or both.
702 // If the X-XRDS-Location header was delivered, we know about it at this point...
703 if ( (x_xrds_location)
704 && (strcmp(ChrPtr(x_xrds_location), ChrPtr(SuppliedURL)))
706 syslog(LOG_DEBUG, "openid: X-XRDS-Location: %s ... recursing!", ChrPtr(x_xrds_location));
707 return_value = perform_openid2_discovery(x_xrds_location);
708 FreeStrBuf(&x_xrds_location);
711 // Option 4: the returned web page may *be* an XRDS document. Try to parse it.
712 if ( (return_value == 0) && (docbytes >= 0)) {
713 return_value = parse_xrds_document(ReplyBuf);
716 // Option 5: if all else fails, attempt HTML based discovery.
717 if ( (return_value == 0) && (docbytes >= 0)) {
718 if (oiddata->op_url == NULL) {
719 oiddata->op_url = NewStrBuf();
721 extract_link(oiddata->op_url, HKEY("openid2.provider"), ReplyBuf);
722 if (StrLength(oiddata->op_url) > 0) {
723 return_value = openid_disco_html;
727 if (ReplyBuf != NULL) {
728 FreeStrBuf(&ReplyBuf);
730 return(return_value);
734 // Setup an OpenID authentication
735 void cmd_oids(char *argbuf) {
736 const char *Pos = NULL;
737 StrBuf *ArgBuf = NULL;
738 StrBuf *ReplyBuf = NULL;
739 StrBuf *return_to = NULL;
740 StrBuf *RedirectUrl = NULL;
741 ctdl_openid *oiddata;
742 int discovery_succeeded = 0;
744 if (CtdlGetConfigInt("c_disable_newu")) {
745 cprintf("%d this system does not support openid.\n", ERROR + CMD_NOT_SUPPORTED);
748 Free_ctdl_openid ((ctdl_openid**)&CC->openid_data);
750 CC->openid_data = oiddata = malloc(sizeof(ctdl_openid));
751 if (oiddata == NULL) {
752 syslog(LOG_ERR, "openid: malloc() failed: %m");
753 cprintf("%d malloc failed\n", ERROR + INTERNAL_ERROR);
756 memset(oiddata, 0, sizeof(ctdl_openid));
758 ArgBuf = NewStrBufPlain(argbuf, -1);
760 oiddata->verified = 0;
761 oiddata->claimed_id = NewStrBufPlain(NULL, StrLength(ArgBuf));
762 return_to = NewStrBufPlain(NULL, StrLength(ArgBuf));
764 StrBufExtract_NextToken(oiddata->claimed_id, ArgBuf, &Pos, '|');
765 StrBufExtract_NextToken(return_to, ArgBuf, &Pos, '|');
767 syslog(LOG_DEBUG, "openid: user-Supplied Identifier is: %s", ChrPtr(oiddata->claimed_id));
769 // ********* OpenID 2.0 section 7.3 - Discovery *********
771 // Section 7.3.1 says we have to attempt XRI based discovery.
772 // No one is using this, no one is asking for it, no one wants it.
773 // So we're not even going to bother attempting this mode.
775 // Attempt section 7.3.2 (Yadis discovery) and section 7.3.3 (HTML discovery);
776 discovery_succeeded = perform_openid2_discovery(oiddata->claimed_id);
778 if (discovery_succeeded == 0) {
779 cprintf("%d There is no OpenID identity provider at this location.\n", ERROR);
783 // If we get to this point we are in possession of a valid OpenID Provider URL.
784 syslog(LOG_DEBUG, "openid: OP URI '%s' discovered using method %d",
785 ChrPtr(oiddata->op_url),
789 // We have to "normalize" our Claimed ID otherwise it will cause some OP's to barf
790 if (cbmstrcasestr(ChrPtr(oiddata->claimed_id), "://") == NULL) {
791 StrBuf *cid = oiddata->claimed_id;
792 oiddata->claimed_id = NewStrBufPlain(HKEY("http://"));
793 StrBufAppendBuf(oiddata->claimed_id, cid, 0);
797 // OpenID 2.0 section 9: request authentication
798 // Assemble a URL to which the user-agent will be redirected.
800 RedirectUrl = NewStrBufDup(oiddata->op_url);
802 StrBufAppendBufPlain(RedirectUrl, HKEY("?openid.ns="), 0);
803 StrBufUrlescAppend(RedirectUrl, NULL, "http://specs.openid.net/auth/2.0");
805 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.mode=checkid_setup"), 0);
807 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.claimed_id="), 0);
808 StrBufUrlescAppend(RedirectUrl, oiddata->claimed_id, NULL);
810 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.identity="), 0);
811 StrBufUrlescAppend(RedirectUrl, oiddata->claimed_id, NULL);
813 // return_to tells the provider how to complete the round trip back to our site
814 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.return_to="), 0);
815 StrBufUrlescAppend(RedirectUrl, return_to, NULL);
817 // Attribute Exchange
819 // http://openid.net/specs/openid-attribute-exchange-1_0.html
820 // http://code.google.com/apis/accounts/docs/OpenID.html#endpoint
821 // http://test-id.net/OP/AXFetch.aspx
823 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.ns.ax="), 0);
824 StrBufUrlescAppend(RedirectUrl, NULL, "http://openid.net/srv/ax/1.0");
826 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.ax.mode=fetch_request"), 0);
828 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.ax.required=firstname,lastname,friendly,nickname"), 0);
830 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.ax.type.firstname="), 0);
831 StrBufUrlescAppend(RedirectUrl, NULL, "http://axschema.org/namePerson/first");
833 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.ax.type.lastname="), 0);
834 StrBufUrlescAppend(RedirectUrl, NULL, "http://axschema.org/namePerson/last");
836 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.ax.type.friendly="), 0);
837 StrBufUrlescAppend(RedirectUrl, NULL, "http://axschema.org/namePerson/friendly");
839 StrBufAppendBufPlain(RedirectUrl, HKEY("&openid.ax.type.nickname="), 0);
840 StrBufUrlescAppend(RedirectUrl, NULL, "http://axschema.org/namePerson/nickname");
842 syslog(LOG_DEBUG, "openid: redirecting client to %s", ChrPtr(RedirectUrl));
843 cprintf("%d %s\n", CIT_OK, ChrPtr(RedirectUrl));
847 FreeStrBuf(&ReplyBuf);
848 FreeStrBuf(&return_to);
849 FreeStrBuf(&RedirectUrl);
853 // Finalize an OpenID authentication
854 void cmd_oidf(char *argbuf) {
859 HashList *keys = NULL;
862 ctdl_openid *oiddata = (ctdl_openid *) CC->openid_data;
864 if (CtdlGetConfigInt("c_disable_newu")) {
865 cprintf("%d this system does not support openid.\n",
866 ERROR + CMD_NOT_SUPPORTED);
869 if (oiddata == NULL) {
870 cprintf("%d run OIDS first.\n", ERROR + INTERNAL_ERROR);
873 if (StrLength(oiddata->op_url) == 0){
874 cprintf("%d No OpenID Endpoint URL has been obtained.\n", ERROR + ILLEGAL_VALUE);
877 keys = NewHash(1, NULL);
879 cprintf("%d NewHash() failed\n", ERROR + INTERNAL_ERROR);
882 cprintf("%d Transmit OpenID data now\n", START_CHAT_MODE);
884 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
885 len = extract_token(thiskey, buf, 0, '|', sizeof thiskey);
887 len = sizeof(thiskey) - 1;
889 extract_token(thisdata, buf, 1, '|', sizeof thisdata);
890 Put(keys, thiskey, len, strdup(thisdata), NULL);
893 // Check to see if this is a correct response.
894 // Start with verified=1 but then set it to 0 if anything looks wrong.
895 oiddata->verified = 1;
897 char *openid_ns = NULL;
898 if ( (!GetHash(keys, "ns", 2, (void *) &openid_ns))
899 || (strcasecmp(openid_ns, "http://specs.openid.net/auth/2.0"))
901 syslog(LOG_DEBUG, "openid: this is not an an OpenID assertion");
902 oiddata->verified = 0;
905 char *openid_mode = NULL;
906 if ( (!GetHash(keys, "mode", 4, (void *) &openid_mode))
907 || (strcasecmp(openid_mode, "id_res"))
909 oiddata->verified = 0;
912 char *openid_claimed_id = NULL;
913 if (GetHash(keys, "claimed_id", 10, (void *) &openid_claimed_id)) {
914 FreeStrBuf(&oiddata->claimed_id);
915 oiddata->claimed_id = NewStrBufPlain(openid_claimed_id, -1);
916 syslog(LOG_DEBUG, "openid: provider is asserting the Claimed ID '%s'", ChrPtr(oiddata->claimed_id));
919 // Validate the assertion against the server
920 syslog(LOG_DEBUG, "openid: validating...");
924 struct curl_httppost *formpost = NULL;
925 struct curl_httppost *lastptr = NULL;
926 char errmsg[1024] = "";
927 StrBuf *ReplyBuf = NewStrBuf();
929 curl_formadd(&formpost, &lastptr,
930 CURLFORM_COPYNAME, "openid.mode",
931 CURLFORM_COPYCONTENTS, "check_authentication",
935 HashPos *HashPos = GetNewHashPos(keys, 0);
936 while (GetNextHashPos(keys, HashPos, &len, &Key, &Value) != 0) {
937 if (strcasecmp(Key, "mode")) {
938 char k_o_keyname[1024];
939 snprintf(k_o_keyname, sizeof k_o_keyname, "openid.%s", (const char *)Key);
940 curl_formadd(&formpost, &lastptr,
941 CURLFORM_COPYNAME, k_o_keyname,
942 CURLFORM_COPYCONTENTS, (char *)Value,
947 DeleteHashPos(&HashPos);
949 curl = ctdl_openid_curl_easy_init(errmsg);
950 curl_easy_setopt(curl, CURLOPT_URL, ChrPtr(oiddata->op_url));
951 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
952 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
953 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
955 res = curl_easy_perform(curl);
957 syslog(LOG_DEBUG, "openid: cmd_oidf() libcurl error %d: %s", res, errmsg);
958 oiddata->verified = 0;
960 curl_easy_cleanup(curl);
961 curl_formfree(formpost);
963 if (cbmstrcasestr(ChrPtr(ReplyBuf), "is_valid:true") == NULL) {
964 oiddata->verified = 0;
966 FreeStrBuf(&ReplyBuf);
968 syslog(LOG_DEBUG, "openid: authentication %s", (oiddata->verified ? "succeeded" : "failed") );
970 // Respond to the client
972 if (oiddata->verified) {
974 // If we were already logged in, attach the OpenID to the user's account
976 if (attach_extauth(&CC->user, oiddata->claimed_id) == 0) {
978 syslog(LOG_DEBUG, "openid: attach succeeded");
982 syslog(LOG_DEBUG, "openid: attach failed");
986 // Otherwise, a user is attempting to log in using the verified OpenID
988 // Existing user who has claimed this OpenID?
990 // Note: if you think that sending the password back over the wire is insecure,
991 // check your assumptions. If someone has successfully asserted an OpenID that
992 // is associated with the account, they already have password equivalency and can
993 // login, so they could just as easily change the password, etc.
994 if (login_via_extauth(oiddata->claimed_id) == 0) {
995 cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
996 logged_in_response();
997 syslog(LOG_DEBUG, "openid: logged in using previously claimed OpenID");
1000 // If this system does not allow self-service new user registration, the
1001 // remaining modes do not apply, so fail here and now.
1002 else if (CtdlGetConfigInt("c_disable_newu")) {
1004 syslog(LOG_DEBUG, "openid: creating user failed due to local policy");
1007 // New user whose OpenID is verified and Attribute Exchange gave us a name?
1008 else if (openid_create_user_via_ax(oiddata->claimed_id, keys) == 0) {
1009 cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
1010 logged_in_response();
1011 syslog(LOG_DEBUG, "openid: successfully auto-created new user");
1014 // OpenID is verified, but the desired username either was not specified or
1015 // conflicts with an existing user. Manual account creation is required.
1017 char *desired_name = NULL;
1018 cprintf("verify_only\n");
1019 cprintf("%s\n", ChrPtr(oiddata->claimed_id));
1020 if (GetHash(keys, "sreg.nickname", 13, (void *) &desired_name)) {
1021 cprintf("%s\n", desired_name);
1026 syslog(LOG_DEBUG, "openid: the desired display name is already taken.");
1035 if (oiddata->sreg_keys != NULL) {
1036 DeleteHash(&oiddata->sreg_keys);
1037 oiddata->sreg_keys = NULL;
1039 oiddata->sreg_keys = keys;
1043 // Initialization function, called from modules_init.c
1044 char *ctdl_module_init_openid_rp(void) {
1046 // Only enable the OpenID command set when native mode authentication is in use.
1047 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_NATIVE) {
1048 CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
1049 CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
1050 CtdlRegisterProtoHook(cmd_oidl, "OIDL", "List OpenIDs associated with an account");
1051 CtdlRegisterProtoHook(cmd_oidd, "OIDD", "Detach an OpenID from an account");
1052 CtdlRegisterProtoHook(cmd_oidc, "OIDC", "Create new user after validating OpenID");
1053 CtdlRegisterProtoHook(cmd_oida, "OIDA", "List all OpenIDs in the database");
1055 CtdlRegisterSessionHook(openid_cleanup_function, EVT_LOGOUT, PRIO_LOGOUT + 10);
1056 CtdlRegisterUserHook(extauth_purge, EVT_PURGEUSER);
1057 openid_level_supported = 2; // This module supports OpenID 2.0 only
1060 // return our module name for the log