]> code.citadel.org Git - citadel.git/blob - citadel/modules/openid/serv_openid_rp.c
In order to circumvent AOL's broken OpenID server, and save
[citadel.git] / citadel / modules / openid / serv_openid_rp.c
1 /*
2  * $Id$
3  *
4  * OpenID 1.1 "relying party" implementation
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <sys/wait.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <curl/curl.h>
33 #include "ctdl_module.h"
34
35
36 /* 
37  * Locate a <link> tag and, given its 'rel=' parameter, return its 'href' parameter
38  */
39 void extract_link(char *target_buf, int target_size, char *rel, char *source_buf)
40 {
41         char *ptr = source_buf;
42
43         if (!target_buf) return;
44         if (!rel) return;
45         if (!source_buf) return;
46
47         target_buf[0] = 0;
48
49         while (ptr = bmstrcasestr(ptr, "<link"), ptr != NULL) {
50
51                 char work_buffer[2048];
52                 char *link_tag_start = NULL;
53                 char *link_tag_end = NULL;
54
55                 char rel_tag[2048];
56                 char href_tag[2048];
57
58                 link_tag_start = ptr;
59                 link_tag_end = strchr(ptr, '>');
60                 rel_tag[0] = 0;
61                 href_tag[0] = 0;
62
63                 if ((link_tag_end) && (link_tag_end > link_tag_start)) {
64                         int len;
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);
68                 
69                         char *rel_start = NULL;
70                         char *rel_end = NULL;
71                         rel_start = bmstrcasestr(work_buffer, "rel=");
72                         if (rel_start) {
73                                 rel_start = strchr(rel_start, '\"');
74                                 if (rel_start) {
75                                         ++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);
79                                         }
80                                 }
81                         }
82
83                         char *href_start = NULL;
84                         char *href_end = NULL;
85                         href_start = bmstrcasestr(work_buffer, "href=");
86                         if (href_start) {
87                                 href_start = strchr(href_start, '\"');
88                                 if (href_start) {
89                                         ++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);
93                                         }
94                                 }
95                         }
96
97                         if (!strcasecmp(rel, rel_tag)) {
98                                 safestrncpy(target_buf, href_tag, target_size);
99                                 return;
100                         }
101
102                 }
103
104         ++ptr;
105         }
106
107
108 }
109
110
111
112 struct fh_data {
113         char *buf;
114         int total_bytes_received;
115         int maxbytes;
116 };
117
118
119 size_t fh_callback(void *ptr, size_t size, size_t nmemb, void *stream)
120 {
121         struct fh_data *fh = (struct fh_data *) stream;
122         int got_bytes = (size * nmemb);
123
124         if (fh->total_bytes_received + got_bytes > fh->maxbytes) {
125                 got_bytes = fh->maxbytes - fh->total_bytes_received;
126         }
127         if (got_bytes > 0) {
128                 memcpy(&fh->buf[fh->total_bytes_received], ptr, got_bytes);
129                 fh->total_bytes_received += got_bytes;
130         }
131
132         return got_bytes;
133 }
134
135
136
137 /*
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.
141  */
142 int fetch_http(char *url, char *target_buf, int maxbytes)
143 {
144         CURL *curl;
145         CURLcode res;
146         char errmsg[1024] = "";
147         struct fh_data fh = {
148                 target_buf,
149                 0,
150                 maxbytes
151         };
152
153         if (!url) return(-1);
154         if (!target_buf) return(-1);
155         memset(target_buf, 0, maxbytes);
156
157         curl = curl_easy_init();
158         if (!curl) {
159                 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
160                 return(-1);
161         }
162
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);
171         if (res) {
172                 CtdlLogPrintf(CTDL_DEBUG, "fetch_http() libcurl error %d: %s\n", res, errmsg);
173         }
174         curl_easy_cleanup(curl);
175         return fh.total_bytes_received;
176 }
177
178
179 /*
180  * Setup an OpenID authentication
181  */
182 void cmd_oids(char *argbuf) {
183         char openid_url[1024];
184         char return_to[1024];
185         char trust_root[1024];
186         int i;
187         char buf[SIZ];
188
189         if (CC->logged_in) {
190                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
191                 return;
192         }
193
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);
197
198         i = fetch_http(openid_url, buf, sizeof buf - 1);
199         buf[sizeof buf - 1] = 0;
200         if (i > 0) {
201                 char openid_server[1024];
202                 char openid_delegate[1024];
203
204                 extract_link(openid_server, sizeof openid_server, "openid.server", buf);
205                 extract_link(openid_delegate, sizeof openid_delegate, "openid.delegate", buf);
206
207                 if (IsEmptyStr(openid_server)) {
208                         cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
209                         return;
210                 }
211
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);
215                 }
216
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];
223
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");
229
230                 snprintf(redirect_string, sizeof redirect_string,
231                         "%s"
232                         "?openid.mode=checkid_setup"
233                         "&openid.identity=%s"
234                         "&openid.return_to=%s"
235                         "&openid.trust_root=%s"
236                         "&openid.sreg.optional=%s"
237                         ,
238                         openid_server,
239                         escaped_identity,
240                         escaped_return_to,
241                         escaped_trust_root,
242                         escaped_sreg_optional
243                 );
244                 cprintf("%d %s\n", CIT_OK, redirect_string);
245                 return;
246         }
247
248         cprintf("%d Unable to fetch OpenID URL\n", ERROR);
249 }
250
251
252
253 /*
254  * Finalize an OpenID authentication
255  */
256 void cmd_oidf(char *argbuf) {
257         char buf[2048];
258         char thiskey[1024];
259         char thisdata[1024];
260         
261         cprintf("%d Transmit OpenID data now\n", START_CHAT_MODE);
262
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);
267         }
268
269         cprintf("message|FIXME finish this\n");
270         cprintf("000\n");
271 }
272
273
274
275
276 CTDL_MODULE_INIT(openid_rp)
277 {
278         if (!threading)
279         {
280                 curl_global_init(CURL_GLOBAL_ALL);
281                 CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
282                 CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
283         }
284
285         /* return our Subversion id for the Log */
286         return "$Id$";
287 }