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;
184 * Begin the first portion of an OpenID checkid_setup operation.
186 void cmd_oid1(char *argbuf) {
187 char openid_url[1024];
188 char return_to[1024];
189 char trust_root[1024];
194 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
198 extract_token(openid_url, argbuf, 0, '|', sizeof openid_url);
199 extract_token(return_to, argbuf, 1, '|', sizeof return_to);
200 extract_token(trust_root, argbuf, 2, '|', sizeof trust_root);
202 i = fetch_http(openid_url, buf, sizeof buf - 1);
203 buf[sizeof buf - 1] = 0;
204 CtdlLogPrintf(CTDL_DEBUG, "fetch got %d bytes:\n", i);
206 char openid_server[1024];
207 char openid_delegate[1024];
209 extract_link(openid_server, sizeof openid_server, "openid.server", buf);
210 extract_link(openid_delegate, sizeof openid_delegate, "openid.delegate", buf);
212 if (IsEmptyStr(openid_server)) {
213 cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
217 /* Empty delegate is legal; we just use the openid_url instead */
218 if (IsEmptyStr(openid_delegate)) {
219 safestrncpy(openid_delegate, openid_url, sizeof openid_delegate);
222 /* Now we know where to redirect to. */
224 char redirect_string[4096];
225 char escaped_identity[1024];
226 char escaped_return_to[1024];
227 char escaped_trust_root[1024];
229 urlesc(escaped_identity, sizeof escaped_identity, openid_delegate);
230 urlesc(escaped_return_to, sizeof escaped_return_to, return_to);
231 urlesc(escaped_trust_root, sizeof escaped_trust_root, trust_root);
233 snprintf(redirect_string, sizeof redirect_string,
235 "?openid.mode=checkid_setup"
236 "&openid_identity=%s"
237 "&openid.return_to=%s"
238 "&openid.trust_root=%s"
240 openid_server, escaped_identity, escaped_return_to, escaped_trust_root
242 cprintf("%d %s\n", CIT_OK, redirect_string);
246 cprintf("%d Unable to fetch OpenID URL\n", ERROR);
253 /* To insert this module into the server activate the next block by changing the #if 0 to #if 1 */
254 CTDL_MODULE_INIT(openid_rp)
258 CtdlRegisterProtoHook(cmd_oid1, "OID1", "Begin OpenID checkid_setup operation");
261 /* return our Subversion id for the Log */