Moved all the OpenID Relying Party code that I've written so far
[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[1024];
52                 char *link_tag_start = NULL;
53                 char *link_tag_end = NULL;
54
55                 char rel_tag[1024];
56                 char href_tag[1024];
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
171         res = curl_easy_perform(curl);
172         if (res) {
173                 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
174         }
175
176         curl_easy_cleanup(curl);
177         return fh.total_bytes_received;
178 }
179
180
181
182
183 /*
184  * Begin the first portion of an OpenID checkid_setup operation.
185  */
186 void cmd_oid1(char *argbuf) {
187         char openid_url[1024];
188         char return_to[1024];
189         char trust_root[1024];
190         int i;
191         char buf[SIZ];
192
193         if (CC->logged_in) {
194                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
195                 return;
196         }
197
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);
201
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);
205         if (i > 0) {
206                 char openid_server[1024];
207                 char openid_delegate[1024];
208
209                 extract_link(openid_server, sizeof openid_server, "openid.server", buf);
210                 extract_link(openid_delegate, sizeof openid_delegate, "openid.delegate", buf);
211
212                 if (IsEmptyStr(openid_server)) {
213                         cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
214                         return;
215                 }
216
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);
220                 }
221
222                 /* Now we know where to redirect to. */
223
224                 char redirect_string[4096];
225                 char escaped_identity[1024];
226                 char escaped_return_to[1024];
227                 char escaped_trust_root[1024];
228
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);
232
233                 snprintf(redirect_string, sizeof redirect_string,
234                         "%s"
235                         "?openid.mode=checkid_setup"
236                         "&openid_identity=%s"
237                         "&openid.return_to=%s"
238                         "&openid.trust_root=%s"
239                         ,
240                         openid_server, escaped_identity, escaped_return_to, escaped_trust_root
241                 );
242                 cprintf("%d %s\n", CIT_OK, redirect_string);
243                 return;
244         }
245
246         cprintf("%d Unable to fetch OpenID URL\n", ERROR);
247 }
248
249
250
251
252
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)
255 {
256         if (!threading)
257         {
258                 CtdlRegisterProtoHook(cmd_oid1, "OID1", "Begin OpenID checkid_setup operation");
259         }
260
261    /* return our Subversion id for the Log */
262    return "$Id$";
263 }