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"
37 * Locate a <link> tag and, given its 'rel=' parameter, return its 'href' parameter
39 void extract_link(char *target_buf, int target_size, char *rel, char *source_buf)
41 char *ptr = source_buf;
43 if (!target_buf) return;
45 if (!source_buf) return;
49 while (ptr = bmstrcasestr(ptr, "<link"), ptr != NULL) {
51 char work_buffer[1024];
52 char *link_tag_start = NULL;
53 char *link_tag_end = NULL;
59 link_tag_end = strchr(ptr, '>');
63 if ((link_tag_end) && (link_tag_end > link_tag_start)) {
65 len = link_tag_end - link_tag_start;
66 if (len > sizeof work_buffer) len = sizeof work_buffer;
67 memcpy(work_buffer, link_tag_start, len);
69 char *rel_start = NULL;
71 rel_start = bmstrcasestr(work_buffer, "rel=");
73 rel_start = strchr(rel_start, '\"');
76 rel_end = strchr(rel_start, '\"');
77 if ((rel_end) && (rel_end > rel_start)) {
78 safestrncpy(rel_tag, rel_start, rel_end - rel_start + 1);
83 char *href_start = NULL;
84 char *href_end = NULL;
85 href_start = bmstrcasestr(work_buffer, "href=");
87 href_start = strchr(href_start, '\"');
90 href_end = strchr(href_start, '\"');
91 if ((href_end) && (href_end > href_start)) {
92 safestrncpy(href_tag, href_start, href_end - href_start + 1);
97 if (!strcasecmp(rel, rel_tag)) {
98 safestrncpy(target_buf, href_tag, target_size);
114 int total_bytes_received;
119 size_t fh_callback(void *ptr, size_t size, size_t nmemb, void *stream)
121 struct fh_data *fh = (struct fh_data *) stream;
122 int got_bytes = (size * nmemb);
124 if (fh->total_bytes_received + got_bytes > fh->maxbytes) {
125 got_bytes = fh->maxbytes - fh->total_bytes_received;
128 memcpy(&fh->buf[fh->total_bytes_received], ptr, got_bytes);
129 fh->total_bytes_received += got_bytes;
138 * Begin an HTTP fetch (returns number of bytes actually fetched, or -1 for error)
139 * We first try 'curl' or 'wget' because they have more robust HTTP handling, and also
140 * support HTTPS. If neither one works, we fall back to a built in mini HTTP client.
142 int fetch_http(char *url, char *target_buf, int maxbytes)
146 char errmsg[1024] = "";
147 struct fh_data fh = {
153 if (!url) return(-1);
154 if (!target_buf) return(-1);
155 memset(target_buf, 0, maxbytes);
157 curl = curl_easy_init();
159 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
163 curl_easy_setopt(curl, CURLOPT_URL, url);
164 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
165 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
166 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fh);
167 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fh_callback);
168 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
169 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
171 res = curl_easy_perform(curl);
173 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
176 curl_easy_cleanup(curl);
177 return fh.total_bytes_received;
182 * Establish a shared secret with an OpenID Identity Provider by sending
183 * an "associate" request.
185 void prepare_openid_associate_request(char *openid_server, char *openid_delegate)
189 struct curl_httppost *formpost=NULL;
190 struct curl_httppost *lastptr=NULL;
192 curl_formadd(&formpost,
194 CURLFORM_COPYNAME, "openid.mode",
195 CURLFORM_COPYCONTENTS, "associate",
199 curl_formadd(&formpost,
201 CURLFORM_COPYNAME, "openid.session_type",
202 CURLFORM_COPYCONTENTS, "",
206 curl = curl_easy_init();
208 curl_easy_setopt(curl, CURLOPT_URL, openid_server);
209 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
210 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
211 //curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fh);
212 //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fh_callback);
213 //curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
214 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
216 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
217 res = curl_easy_perform(curl);
219 // FIXME not finished
221 curl_easy_cleanup(curl);
223 curl_formfree(formpost);
231 * Begin the first portion of an OpenID checkid_setup operation.
233 void cmd_oid1(char *argbuf) {
234 char openid_url[1024];
235 char return_to[1024];
236 char trust_root[1024];
241 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
245 extract_token(openid_url, argbuf, 0, '|', sizeof openid_url);
246 extract_token(return_to, argbuf, 1, '|', sizeof return_to);
247 extract_token(trust_root, argbuf, 2, '|', sizeof trust_root);
249 i = fetch_http(openid_url, buf, sizeof buf - 1);
250 buf[sizeof buf - 1] = 0;
252 char openid_server[1024];
253 char openid_delegate[1024];
255 extract_link(openid_server, sizeof openid_server, "openid.server", buf);
256 extract_link(openid_delegate, sizeof openid_delegate, "openid.delegate", buf);
258 if (IsEmptyStr(openid_server)) {
259 cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
263 /* Empty delegate is legal; we just use the openid_url instead */
264 if (IsEmptyStr(openid_delegate)) {
265 safestrncpy(openid_delegate, openid_url, sizeof openid_delegate);
268 /* Prepare an "associate" request */
269 prepare_openid_associate_request(openid_server, openid_delegate);
271 /* Now we know where to redirect to. */
272 char redirect_string[4096];
273 char escaped_identity[1024];
274 char escaped_return_to[1024];
275 char escaped_trust_root[1024];
276 char escaped_sreg_optional[256];
278 urlesc(escaped_identity, sizeof escaped_identity, openid_delegate);
279 urlesc(escaped_return_to, sizeof escaped_return_to, return_to);
280 urlesc(escaped_trust_root, sizeof escaped_trust_root, trust_root);
281 urlesc(escaped_sreg_optional, sizeof escaped_sreg_optional,
282 "nickname,email,fullname,postcode,country");
284 snprintf(redirect_string, sizeof redirect_string,
286 "?openid.mode=checkid_setup"
287 "&openid_identity=%s"
288 "&openid.return_to=%s"
289 "&openid.trust_root=%s"
290 "&openid.sreg.optional=%s"
292 openid_server, escaped_identity, escaped_return_to,
293 escaped_trust_root, escaped_sreg_optional
295 cprintf("%d %s\n", CIT_OK, redirect_string);
299 cprintf("%d Unable to fetch OpenID URL\n", ERROR);
306 /* To insert this module into the server activate the next block by changing the #if 0 to #if 1 */
307 CTDL_MODULE_INIT(openid_rp)
311 curl_global_init(CURL_GLOBAL_ALL);
312 CtdlRegisterProtoHook(cmd_oid1, "OID1", "Begin OpenID checkid_setup operation");
315 /* return our Subversion id for the Log */