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"
36 char claimed_id[1024];
42 * Locate a <link> tag and, given its 'rel=' parameter, return its 'href' parameter
44 void extract_link(char *target_buf, int target_size, char *rel, char *source_buf)
46 char *ptr = source_buf;
48 if (!target_buf) return;
50 if (!source_buf) return;
54 while (ptr = bmstrcasestr(ptr, "<link"), ptr != NULL) {
56 char work_buffer[2048];
57 char *link_tag_start = NULL;
58 char *link_tag_end = NULL;
64 link_tag_end = strchr(ptr, '>');
68 if ((link_tag_end) && (link_tag_end > link_tag_start)) {
70 len = link_tag_end - link_tag_start;
71 if (len > sizeof work_buffer) len = sizeof work_buffer;
72 memcpy(work_buffer, link_tag_start, len);
74 char *rel_start = NULL;
76 rel_start = bmstrcasestr(work_buffer, "rel=");
78 rel_start = strchr(rel_start, '\"');
81 rel_end = strchr(rel_start, '\"');
82 if ((rel_end) && (rel_end > rel_start)) {
83 safestrncpy(rel_tag, rel_start, rel_end - rel_start + 1);
88 char *href_start = NULL;
89 char *href_end = NULL;
90 href_start = bmstrcasestr(work_buffer, "href=");
92 href_start = strchr(href_start, '\"');
95 href_end = strchr(href_start, '\"');
96 if ((href_end) && (href_end > href_start)) {
97 safestrncpy(href_tag, href_start, href_end - href_start + 1);
102 if (!strcasecmp(rel, rel_tag)) {
103 safestrncpy(target_buf, href_tag, target_size);
119 int total_bytes_received;
124 size_t fh_callback(void *ptr, size_t size, size_t nmemb, void *stream)
126 struct fh_data *fh = (struct fh_data *) stream;
127 int got_bytes = (size * nmemb);
129 if (fh->total_bytes_received + got_bytes > fh->maxbytes) {
130 got_bytes = fh->maxbytes - fh->total_bytes_received;
133 memcpy(&fh->buf[fh->total_bytes_received], ptr, got_bytes);
134 fh->total_bytes_received += got_bytes;
143 * Begin an HTTP fetch (returns number of bytes actually fetched, or -1 for error) using libcurl.
145 * If 'normalize_len' is nonzero, the caller is specifying the buffer size of 'url', and is
146 * requesting that the effective (normalized) URL be copied back to it.
148 int fetch_http(char *url, char *target_buf, int maxbytes, int normalize_len)
152 char errmsg[1024] = "";
153 struct fh_data fh = {
158 char *effective_url = NULL;
160 if (!url) return(-1);
161 if (!target_buf) return(-1);
162 memset(target_buf, 0, maxbytes);
164 curl = curl_easy_init();
166 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
170 curl_easy_setopt(curl, CURLOPT_URL, url);
171 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
172 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
173 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fh);
174 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fh_callback);
175 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
176 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
177 res = curl_easy_perform(curl);
179 CtdlLogPrintf(CTDL_DEBUG, "fetch_http() libcurl error %d: %s\n", res, errmsg);
181 if (normalize_len > 0) {
182 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
183 safestrncpy(url, effective_url, normalize_len);
185 curl_easy_cleanup(curl);
186 return fh.total_bytes_received;
191 * Setup an OpenID authentication
193 void cmd_oids(char *argbuf) {
194 char return_to[1024];
195 char trust_root[1024];
198 struct CitContext *CCC = CC; /* CachedCitContext - performance boost */
199 struct ctdl_openid *oiddata;
201 if (CCC->logged_in) {
202 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
206 if (CCC->openid_data != NULL) {
207 free(CCC->openid_data);
209 oiddata = malloc(sizeof(struct ctdl_openid));
210 if (oiddata == NULL) {
211 cprintf("%d malloc failed\n", ERROR + INTERNAL_ERROR);
214 memset(oiddata, 0, sizeof(struct ctdl_openid));
215 CCC->openid_data = (void *) oiddata;
217 extract_token(oiddata->claimed_id, argbuf, 0, '|', sizeof oiddata->claimed_id);
218 extract_token(return_to, argbuf, 1, '|', sizeof return_to);
219 extract_token(trust_root, argbuf, 2, '|', sizeof trust_root);
221 i = fetch_http(oiddata->claimed_id, buf, sizeof buf - 1, sizeof oiddata->claimed_id);
222 CtdlLogPrintf(CTDL_DEBUG, "Normalized URL and Claimed ID is: %s\n", oiddata->claimed_id);
223 buf[sizeof buf - 1] = 0;
225 char openid_delegate[1024];
227 extract_link(oiddata->server, sizeof oiddata->server, "openid.server", buf);
228 extract_link(openid_delegate, sizeof openid_delegate, "openid.delegate", buf);
230 if (IsEmptyStr(oiddata->server)) {
231 cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
235 /* Empty delegate is legal; we just use the openid_url instead */
236 if (IsEmptyStr(openid_delegate)) {
237 safestrncpy(openid_delegate, oiddata->claimed_id, sizeof openid_delegate);
240 /* Assemble a URL to which the user-agent will be redirected. */
241 char redirect_string[4096];
242 char escaped_identity[512];
243 char escaped_return_to[2048];
244 char escaped_trust_root[1024];
245 char escaped_sreg_optional[256];
247 urlesc(escaped_identity, sizeof escaped_identity, openid_delegate);
248 urlesc(escaped_return_to, sizeof escaped_return_to, return_to);
249 urlesc(escaped_trust_root, sizeof escaped_trust_root, trust_root);
250 urlesc(escaped_sreg_optional, sizeof escaped_sreg_optional,
251 "nickname,email,fullname,postcode,country");
253 snprintf(redirect_string, sizeof redirect_string,
255 "?openid.mode=checkid_setup"
256 "&openid.identity=%s"
257 "&openid.return_to=%s"
258 "&openid.trust_root=%s"
259 "&openid.sreg.optional=%s"
265 escaped_sreg_optional
267 cprintf("%d %s\n", CIT_OK, redirect_string);
271 cprintf("%d Unable to fetch OpenID URL\n", ERROR);
277 * Callback function to free a pointer (used below in the hash list)
279 void free_oid_key(void *ptr) {
285 * Finalize an OpenID authentication
287 void cmd_oidf(char *argbuf) {
291 HashList *keys = NULL;
294 keys = NewHash(1, NULL);
296 cprintf("%d NewHash() failed\n", ERROR + INTERNAL_ERROR);
300 cprintf("%d Transmit OpenID data now\n", START_CHAT_MODE);
302 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
303 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
304 extract_token(thisdata, buf, 1, '|', sizeof thisdata);
305 CtdlLogPrintf(CTDL_DEBUG, "%s: [%d] %s\n", thiskey, strlen(thisdata), thisdata);
306 Put(keys, thiskey, strlen(thiskey), strdup(thisdata), free_oid_key);
310 /* Now that we have all of the parameters, we have to validate the signature against the server */
314 struct curl_httppost *formpost = NULL;
315 struct curl_httppost *lastptr = NULL;
316 char errmsg[1024] = "";
318 curl_formadd(&formpost, &lastptr,
319 CURLFORM_COPYNAME, "openid.mode",
320 CURLFORM_COPYCONTENTS, "check_authentication",
323 curl = curl_easy_init();
324 // curl_easy_setopt(curl, CURLOPT_URL, FIXME ); /* FIXME need to bring this over */
325 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
326 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
327 // curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fh);
328 // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fh_callback);
329 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
330 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
331 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
332 res = curl_easy_perform(curl);
334 CtdlLogPrintf(CTDL_DEBUG, "cmd_oidf() libcurl error %d: %s\n", res, errmsg);
336 curl_easy_cleanup(curl);
337 curl_formfree(formpost);
339 /* FIXME do something with the results */
341 /* Respond to the client */
342 cprintf("message|FIXME finish this\n");
345 /* Free the hash list */
350 HashPos = GetNewHashPos();
351 while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
355 DeleteHashPos(&HashPos);
360 // identity = [50] http://uncensored.citadel.org/~ajc/MyID.config.php
361 // assoc_handle = [26] 6ekac3ju181tgepk7v4h9r7ui7
362 // return_to = [42] http://jemcaterers.net/finish_openid_login
363 // sreg.nickname = [17] IGnatius T Foobar
364 // sreg.email = [26] ajc@uncensored.citadel.org
365 // sreg.fullname = [10] Art Cancro
366 // sreg.postcode = [5] 10549
367 // sreg.country = [2] US
368 // signed = [102] mode,identity,assoc_handle,return_to,sreg.nickname,sreg.email,sreg.fullname,sreg.postcode,sreg.country
369 // sig = [28] vixxxU4MAqWfxxxxCfrHv3TxxxhEw=
373 * This cleanup function blows away the temporary memory used by this module.
375 void openid_cleanup_function(void) {
377 if (CC->openid_data != NULL) {
378 free(CC->openid_data);
384 CTDL_MODULE_INIT(openid_rp)
388 curl_global_init(CURL_GLOBAL_ALL);
389 CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
390 CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
391 CtdlRegisterSessionHook(openid_cleanup_function, EVT_STOP);
394 /* return our Subversion id for the Log */