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[2048];
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);
170 res = curl_easy_perform(curl);
172 CtdlLogPrintf(CTDL_DEBUG, "fetch_http() libcurl error %d: %s\n", res, errmsg);
174 curl_easy_cleanup(curl);
175 return fh.total_bytes_received;
180 * Setup an OpenID authentication
182 void cmd_oids(char *argbuf) {
183 char openid_url[1024];
184 char return_to[1024];
185 char trust_root[1024];
190 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
194 extract_token(openid_url, argbuf, 0, '|', sizeof openid_url);
195 extract_token(return_to, argbuf, 1, '|', sizeof return_to);
196 extract_token(trust_root, argbuf, 2, '|', sizeof trust_root);
198 i = fetch_http(openid_url, buf, sizeof buf - 1);
199 buf[sizeof buf - 1] = 0;
201 char openid_server[1024];
202 char openid_delegate[1024];
204 extract_link(openid_server, sizeof openid_server, "openid.server", buf);
205 extract_link(openid_delegate, sizeof openid_delegate, "openid.delegate", buf);
207 if (IsEmptyStr(openid_server)) {
208 cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
212 /* Empty delegate is legal; we just use the openid_url instead */
213 if (IsEmptyStr(openid_delegate)) {
214 safestrncpy(openid_delegate, openid_url, sizeof openid_delegate);
217 /* Assemble a URL to which the user-agent will be redirected. */
218 char redirect_string[4096];
219 char escaped_identity[512];
220 char escaped_return_to[2048];
221 char escaped_trust_root[1024];
222 char escaped_sreg_optional[256];
224 urlesc(escaped_identity, sizeof escaped_identity, openid_delegate);
225 urlesc(escaped_return_to, sizeof escaped_return_to, return_to);
226 urlesc(escaped_trust_root, sizeof escaped_trust_root, trust_root);
227 urlesc(escaped_sreg_optional, sizeof escaped_sreg_optional,
228 "nickname,email,fullname,postcode,country");
230 snprintf(redirect_string, sizeof redirect_string,
232 "?openid.mode=checkid_setup"
233 "&openid.identity=%s"
234 "&openid.return_to=%s"
235 "&openid.trust_root=%s"
236 "&openid.sreg.optional=%s"
242 escaped_sreg_optional
244 cprintf("%d %s\n", CIT_OK, redirect_string);
248 cprintf("%d Unable to fetch OpenID URL\n", ERROR);
254 * Finalize an OpenID authentication
256 void cmd_oidf(char *argbuf) {
261 cprintf("%d Transmit OpenID data now\n", START_CHAT_MODE);
263 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
264 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
265 extract_token(thisdata, buf, 1, '|', sizeof thisdata);
266 CtdlLogPrintf(CTDL_DEBUG, "%s: [%d] %s\n", thiskey, strlen(thisdata), thisdata);
269 cprintf("message|FIXME finish this\n");
276 CTDL_MODULE_INIT(openid_rp)
280 curl_global_init(CURL_GLOBAL_ALL);
281 CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
282 CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
285 /* return our Subversion id for the Log */