4 * OpenID 1.1 "relying party" implementation
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 struct associate_handle {
39 time_t expiration_time;
40 char assoc_handle[256];
44 HashList *HL = NULL; // hash table of assoc_handle
47 * Locate a <link> tag and, given its 'rel=' parameter, return its 'href' parameter
49 void extract_link(char *target_buf, int target_size, char *rel, char *source_buf)
51 char *ptr = source_buf;
53 if (!target_buf) return;
55 if (!source_buf) return;
59 while (ptr = bmstrcasestr(ptr, "<link"), ptr != NULL) {
61 char work_buffer[2048];
62 char *link_tag_start = NULL;
63 char *link_tag_end = NULL;
69 link_tag_end = strchr(ptr, '>');
73 if ((link_tag_end) && (link_tag_end > link_tag_start)) {
75 len = link_tag_end - link_tag_start;
76 if (len > sizeof work_buffer) len = sizeof work_buffer;
77 memcpy(work_buffer, link_tag_start, len);
79 char *rel_start = NULL;
81 rel_start = bmstrcasestr(work_buffer, "rel=");
83 rel_start = strchr(rel_start, '\"');
86 rel_end = strchr(rel_start, '\"');
87 if ((rel_end) && (rel_end > rel_start)) {
88 safestrncpy(rel_tag, rel_start, rel_end - rel_start + 1);
93 char *href_start = NULL;
94 char *href_end = NULL;
95 href_start = bmstrcasestr(work_buffer, "href=");
97 href_start = strchr(href_start, '\"');
100 href_end = strchr(href_start, '\"');
101 if ((href_end) && (href_end > href_start)) {
102 safestrncpy(href_tag, href_start, href_end - href_start + 1);
107 if (!strcasecmp(rel, rel_tag)) {
108 safestrncpy(target_buf, href_tag, target_size);
124 int total_bytes_received;
129 size_t fh_callback(void *ptr, size_t size, size_t nmemb, void *stream)
131 struct fh_data *fh = (struct fh_data *) stream;
132 int got_bytes = (size * nmemb);
134 if (fh->total_bytes_received + got_bytes > fh->maxbytes) {
135 got_bytes = fh->maxbytes - fh->total_bytes_received;
138 memcpy(&fh->buf[fh->total_bytes_received], ptr, got_bytes);
139 fh->total_bytes_received += got_bytes;
148 * Begin an HTTP fetch (returns number of bytes actually fetched, or -1 for error)
149 * We first try 'curl' or 'wget' because they have more robust HTTP handling, and also
150 * support HTTPS. If neither one works, we fall back to a built in mini HTTP client.
152 int fetch_http(char *url, char *target_buf, int maxbytes)
156 char errmsg[1024] = "";
157 struct fh_data fh = {
163 if (!url) return(-1);
164 if (!target_buf) return(-1);
165 memset(target_buf, 0, maxbytes);
167 curl = curl_easy_init();
169 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
173 curl_easy_setopt(curl, CURLOPT_URL, url);
174 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
175 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
176 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fh);
177 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fh_callback);
178 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
179 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
180 res = curl_easy_perform(curl);
182 CtdlLogPrintf(CTDL_DEBUG, "fetch_http() libcurl error %d: %s\n", res, errmsg);
184 curl_easy_cleanup(curl);
185 return fh.total_bytes_received;
189 #define ASSOCIATE_RESPONSE_SIZE 4096
192 * libcurl callback function for prepare_openid_associate_request()
194 size_t associate_callback(void *ptr, size_t size, size_t nmemb, void *stream)
196 char *response = (char *) stream;
197 int got_bytes = (size * nmemb);
198 int len = strlen(response);
200 if ((len + got_bytes + 1) < ASSOCIATE_RESPONSE_SIZE) {
201 memcpy(&response[len], ptr, got_bytes);
202 response[len+got_bytes] = 0;
210 * Helper function for process_associate_response()
211 * (Delete function for hash table)
213 void delete_assoc_handle(void *data) {
214 if (data) free(data);
219 * Process the response from an "associate" request
221 struct associate_handle *process_associate_response(char *claimed_id, char *associate_response)
223 struct associate_handle *h = NULL;
224 char *ptr = associate_response;
229 h = (struct associate_handle *) malloc(sizeof(struct associate_handle));
230 safestrncpy(h->claimed_id, claimed_id, sizeof h->claimed_id);
233 ptr = memreadline(ptr, thisline, sizeof thisline);
234 extract_token(thiskey, thisline, 0, ':', sizeof thiskey);
235 extract_token(thisdata, thisline, 1, ':', sizeof thisdata);
237 CtdlLogPrintf(CTDL_DEBUG, "Associate response: key:<%s> data:<%s>\n", thiskey, thisdata);
239 if (!strcasecmp(thiskey, "assoc_type")) {
240 safestrncpy(h->assoc_type, thisdata, sizeof h->assoc_type);
242 else if (!strcasecmp(thiskey, "expires_in")) {
243 h->expiration_time = time(NULL) + atol(thisdata);
245 else if (!strcasecmp(thiskey, "assoc_handle")) {
246 safestrncpy(h->assoc_handle, thisdata, sizeof h->assoc_handle);
248 else if (!strcasecmp(thiskey, "mac_key")) {
249 safestrncpy(h->mac_key, thisdata, sizeof h->mac_key);
254 /* Add this data structure into the hash table */
255 Put(HL, h->assoc_handle, strlen(h->assoc_handle), h, delete_assoc_handle);
257 /* FIXME periodically purge the hash table of expired handles */
265 * Establish a shared secret with an OpenID Identity Provider by sending
266 * an "associate" request.
268 struct associate_handle *prepare_openid_associate_request(
269 char *claimed_id, char *openid_server, char *openid_delegate)
273 struct curl_httppost *formpost=NULL;
274 struct curl_httppost *lastptr=NULL;
275 char associate_response[ASSOCIATE_RESPONSE_SIZE];
276 struct associate_handle *h = NULL;
278 memset(associate_response, 0, ASSOCIATE_RESPONSE_SIZE);
280 curl_formadd(&formpost,
282 CURLFORM_COPYNAME, "openid.mode",
283 CURLFORM_COPYCONTENTS, "associate",
287 curl_formadd(&formpost,
289 CURLFORM_COPYNAME, "openid.session_type",
290 CURLFORM_COPYCONTENTS, "",
294 curl = curl_easy_init();
296 curl_easy_setopt(curl, CURLOPT_URL, openid_server);
297 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
298 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
299 curl_easy_setopt(curl, CURLOPT_WRITEDATA, associate_response);
300 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, associate_callback);
301 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
303 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
304 res = curl_easy_perform(curl);
305 h = process_associate_response(claimed_id, associate_response);
306 curl_easy_cleanup(curl);
308 curl_formfree(formpost);
318 * Setup an OpenID authentication
320 void cmd_oids(char *argbuf) {
321 char openid_url[1024];
322 char return_to[1024];
323 char trust_root[1024];
326 struct associate_handle *h = NULL;
329 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
333 extract_token(openid_url, argbuf, 0, '|', sizeof openid_url);
334 extract_token(return_to, argbuf, 1, '|', sizeof return_to);
335 extract_token(trust_root, argbuf, 2, '|', sizeof trust_root);
337 i = fetch_http(openid_url, buf, sizeof buf - 1);
338 buf[sizeof buf - 1] = 0;
340 char openid_server[1024];
341 char openid_delegate[1024];
343 extract_link(openid_server, sizeof openid_server, "openid.server", buf);
344 extract_link(openid_delegate, sizeof openid_delegate, "openid.delegate", buf);
346 if (IsEmptyStr(openid_server)) {
347 cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
351 /* Empty delegate is legal; we just use the openid_url instead */
352 if (IsEmptyStr(openid_delegate)) {
353 safestrncpy(openid_delegate, openid_url, sizeof openid_delegate);
357 * Prepare an "associate" request. This contacts the IdP and fetches
358 * a data structure containing an assoc_handle plus a shared secret.
360 h = prepare_openid_associate_request(openid_url, openid_server, openid_delegate);
362 /* Assemble a URL to which the user-agent will be redirected. */
363 char redirect_string[4096];
364 char escaped_identity[512];
365 char escaped_return_to[2048];
366 char escaped_trust_root[1024];
367 char escaped_sreg_optional[256];
368 char escaped_assoc_handle[512];
370 urlesc(escaped_identity, sizeof escaped_identity, openid_delegate);
371 urlesc(escaped_assoc_handle, sizeof escaped_assoc_handle, h->assoc_handle);
372 urlesc(escaped_return_to, sizeof escaped_return_to, return_to);
373 urlesc(escaped_trust_root, sizeof escaped_trust_root, trust_root);
374 urlesc(escaped_sreg_optional, sizeof escaped_sreg_optional,
375 "nickname,email,fullname,postcode,country");
377 snprintf(redirect_string, sizeof redirect_string,
379 "?openid.mode=checkid_setup"
380 "&openid.identity=%s"
381 "&openid.assoc_handle=%s"
382 "&openid.return_to=%s"
383 "&openid.trust_root=%s"
384 "&openid.sreg.optional=%s"
388 escaped_assoc_handle,
391 escaped_sreg_optional
393 CtdlLogPrintf(CTDL_DEBUG, "Telling client about assoc_handle <%s>\n", h->assoc_handle);
394 cprintf("%d %s\n", CIT_OK, redirect_string);
398 cprintf("%d Unable to fetch OpenID URL\n", ERROR);
404 * Finalize an OpenID authentication
406 void cmd_oidf(char *argbuf) {
407 char assoc_handle[256];
408 char invalidate_handle[256];
409 struct associate_handle *h = NULL;
411 extract_token(assoc_handle, argbuf, 0, '|', sizeof assoc_handle);
412 extract_token(invalidate_handle, argbuf, 1, '|', sizeof assoc_handle);
414 if (GetHash(HL, assoc_handle, strlen(assoc_handle), (void *)&h)) {
415 cprintf("%d handle is good\n", CIT_OK);
417 // FIXME now do something with it
421 if (GetHash(HL, invalidate_handle, strlen(invalidate_handle), (void *)&h)) {
422 cprintf("%d assoc_handle not found, but invalidate_handle was found\n", ERROR);
425 cprintf("%d neither assoc_handle nor invalidate_handle found, wtf?\n", ERROR);
433 CTDL_MODULE_INIT(openid_rp)
437 curl_global_init(CURL_GLOBAL_ALL);
438 HL = NewHash(1, NULL);
439 CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
440 CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
443 /* return our Subversion id for the Log */