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 {
40 char assoc_handle[128];
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[1024];
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);
181 res = curl_easy_perform(curl);
183 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
186 curl_easy_cleanup(curl);
187 return fh.total_bytes_received;
191 #define ASSOCIATE_RESPONSE_SIZE 4096
194 * libcurl callback function for prepare_openid_associate_request()
196 size_t associate_callback(void *ptr, size_t size, size_t nmemb, void *stream)
198 char *response = (char *) stream;
199 int got_bytes = (size * nmemb);
200 int len = strlen(response);
202 if ((len + got_bytes + 1) < ASSOCIATE_RESPONSE_SIZE) {
203 memcpy(&response[len], ptr, got_bytes);
204 response[len+got_bytes] = 0;
212 * Process the response from an "associate" request
214 void process_associate_response(associate_response)
222 * Establish a shared secret with an OpenID Identity Provider by sending
223 * an "associate" request.
225 void prepare_openid_associate_request(char *openid_server, char *openid_delegate)
229 struct curl_httppost *formpost=NULL;
230 struct curl_httppost *lastptr=NULL;
231 char associate_response[ASSOCIATE_RESPONSE_SIZE];
233 memset(associate_response, 0, ASSOCIATE_RESPONSE_SIZE);
235 curl_formadd(&formpost,
237 CURLFORM_COPYNAME, "openid.mode",
238 CURLFORM_COPYCONTENTS, "associate",
242 curl_formadd(&formpost,
244 CURLFORM_COPYNAME, "openid.session_type",
245 CURLFORM_COPYCONTENTS, "",
249 curl = curl_easy_init();
251 curl_easy_setopt(curl, CURLOPT_URL, openid_server);
252 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
253 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
254 curl_easy_setopt(curl, CURLOPT_WRITEDATA, associate_response);
255 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, associate_callback);
256 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
258 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
259 res = curl_easy_perform(curl);
260 process_associate_response(associate_response);
261 curl_easy_cleanup(curl);
263 curl_formfree(formpost);
271 * Begin the first portion of an OpenID checkid_setup operation.
273 void cmd_oid1(char *argbuf) {
274 char openid_url[1024];
275 char return_to[1024];
276 char trust_root[1024];
281 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
285 extract_token(openid_url, argbuf, 0, '|', sizeof openid_url);
286 extract_token(return_to, argbuf, 1, '|', sizeof return_to);
287 extract_token(trust_root, argbuf, 2, '|', sizeof trust_root);
289 i = fetch_http(openid_url, buf, sizeof buf - 1);
290 buf[sizeof buf - 1] = 0;
292 char openid_server[1024];
293 char openid_delegate[1024];
295 extract_link(openid_server, sizeof openid_server, "openid.server", buf);
296 extract_link(openid_delegate, sizeof openid_delegate, "openid.delegate", buf);
298 if (IsEmptyStr(openid_server)) {
299 cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
303 /* Empty delegate is legal; we just use the openid_url instead */
304 if (IsEmptyStr(openid_delegate)) {
305 safestrncpy(openid_delegate, openid_url, sizeof openid_delegate);
308 /* Prepare an "associate" request */
309 prepare_openid_associate_request(openid_server, openid_delegate);
311 /* Now we know where to redirect to. */
312 char redirect_string[4096];
313 char escaped_identity[1024];
314 char escaped_return_to[1024];
315 char escaped_trust_root[1024];
316 char escaped_sreg_optional[256];
318 urlesc(escaped_identity, sizeof escaped_identity, openid_delegate);
319 urlesc(escaped_return_to, sizeof escaped_return_to, return_to);
320 urlesc(escaped_trust_root, sizeof escaped_trust_root, trust_root);
321 urlesc(escaped_sreg_optional, sizeof escaped_sreg_optional,
322 "nickname,email,fullname,postcode,country");
324 snprintf(redirect_string, sizeof redirect_string,
326 "?openid.mode=checkid_setup"
327 "&openid_identity=%s"
328 "&openid.return_to=%s"
329 "&openid.trust_root=%s"
330 "&openid.sreg.optional=%s"
332 openid_server, escaped_identity, escaped_return_to,
333 escaped_trust_root, escaped_sreg_optional
335 cprintf("%d %s\n", CIT_OK, redirect_string);
339 cprintf("%d Unable to fetch OpenID URL\n", ERROR);
346 /* To insert this module into the server activate the next block by changing the #if 0 to #if 1 */
347 CTDL_MODULE_INIT(openid_rp)
351 curl_global_init(CURL_GLOBAL_ALL);
352 CtdlRegisterProtoHook(cmd_oid1, "OID1", "Begin OpenID checkid_setup operation");
355 /* return our Subversion id for the Log */