* Began implenmenting OpenID table import/export
[citadel.git] / citadel / modules / openid / serv_openid_rp.c
1 /*
2  * $Id$
3  *
4  * This is an implementation of OpenID 1.1 Relying Party support, in stateless mode.
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 #include "config.h"
35 #include "citserver.h"
36 #include "user_ops.h"
37
38 struct ctdl_openid {
39         char claimed_id[1024];
40         char server[1024];
41         int validated;
42 };
43
44
45
46
47
48 /**************************************************************************/
49 /*                                                                        */
50 /* Functions in this section handle Citadel internal OpenID mapping stuff */
51 /*                                                                        */
52 /**************************************************************************/
53
54
55 /*
56  * The structure of an openid record *key* is:
57  *
58  * |--------------claimed_id-------------|
59  *     (actual length of claimed id)
60  *
61  *
62  * The structure of an openid record *value* is:
63  *
64  * |-----user_number----|------------claimed_id---------------|
65  *    (sizeof long)          (actual length of claimed id)
66  *
67  */
68
69
70
71 /*
72  * Attach an OpenID to a Citadel account
73  */
74 int attach_openid(struct ctdluser *who, char *claimed_id)
75 {
76         struct cdbdata *cdboi;
77         long fetched_usernum;
78         char *data;
79         int data_len;
80
81         if (!who) return(1);
82         if (!claimed_id) return(1);
83         if (IsEmptyStr(claimed_id)) return(1);
84
85         /* Check to see if this OpenID is already in the database */
86
87         cdboi = cdb_fetch(CDB_OPENID, claimed_id, strlen(claimed_id));
88         if (cdboi != NULL) {
89                 memcpy(&fetched_usernum, cdboi->ptr, sizeof(long));
90                 cdb_free(cdboi);
91
92                 if (fetched_usernum == who->usernum) {
93                         CtdlLogPrintf(CTDL_INFO, "%s already associated; no action is taken\n", claimed_id);
94                         return(0);
95                 }
96                 else {
97                         CtdlLogPrintf(CTDL_INFO, "%s already belongs to another user\n", claimed_id);
98                         return(3);
99                 }
100         }
101
102         /* Not already in the database, so attach it now */
103
104         data_len = sizeof(long) + strlen(claimed_id) + 1;
105         data = malloc(data_len);
106
107         memcpy(data, &who->usernum, sizeof(long));
108         memcpy(&data[sizeof(long)], claimed_id, strlen(claimed_id) + 1);
109
110         cdb_store(CDB_OPENID, claimed_id, strlen(claimed_id), data, data_len);
111         free(data);
112
113         CtdlLogPrintf(CTDL_INFO, "%s has been associated with %s (%ld)\n",
114                 claimed_id, who->fullname, who->usernum);
115         return(0);
116 }
117
118
119 /*
120  * When a user is being deleted, we have to delete any OpenID associations
121  */
122 void openid_purge(struct ctdluser *usbuf) {
123         struct cdbdata *cdboi;
124         HashList *keys = NULL;
125         HashPos *HashPos;
126         char *deleteme = NULL;
127         long len;
128         void *Value;
129         char *Key;
130
131         keys = NewHash(1, NULL);
132         if (!keys) return;
133
134
135         cdb_rewind(CDB_OPENID);
136         while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
137                 if (cdboi->len > sizeof(long)) {
138                         if (((long)*(cdboi->ptr)) == usbuf->usernum) {
139                                 deleteme = strdup(cdboi->ptr + sizeof(long)),
140                                 Put(keys, deleteme, strlen(deleteme), deleteme, generic_free_handler);
141                         }
142                 }
143                 cdb_free(cdboi);
144         }
145
146         /* Go through the hash list, deleting keys we stored in it */
147
148         HashPos = GetNewHashPos();
149         while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
150         {
151                 CtdlLogPrintf(CTDL_DEBUG, "Deleting associated OpenID <%s>\n", Value);
152                 cdb_delete(CDB_OPENID, Value, strlen(Value));
153                 /* note: don't free(Value) -- deleting the hash list will handle this for us */
154         }
155         DeleteHashPos(&HashPos);
156         DeleteHash(&keys);
157 }
158
159
160
161 /*
162  * List the OpenIDs associated with the currently logged in account
163  */
164 void cmd_oidl(char *argbuf) {
165         struct cdbdata *cdboi;
166
167         if (CtdlAccessCheck(ac_logged_in)) return;
168         cdb_rewind(CDB_OPENID);
169         cprintf("%d Associated OpenIDs:\n", LISTING_FOLLOWS);
170
171         while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
172                 if (cdboi->len > sizeof(long)) {
173                         if (((long)*(cdboi->ptr)) == CC->user.usernum) {
174                                 cprintf("%s\n", cdboi->ptr + sizeof(long));
175                         }
176                 }
177                 cdb_free(cdboi);
178         }
179         cprintf("000\n");
180 }
181
182
183
184 /*
185  * Detach an OpenID from the currently logged in account
186  */
187 void cmd_oidd(char *argbuf) {
188         struct cdbdata *cdboi;
189         char id_to_detach[1024];
190         int this_is_mine = 0;
191
192         if (CtdlAccessCheck(ac_logged_in)) return;
193         extract_token(id_to_detach, argbuf, 0, '|', sizeof id_to_detach);
194         if (IsEmptyStr(id_to_detach)) {
195                 cprintf("%d An empty OpenID URL is not allowed.\n", ERROR + ILLEGAL_VALUE);
196         }
197
198         cdb_rewind(CDB_OPENID);
199         while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
200                 if (cdboi->len > sizeof(long)) {
201                         if (((long)*(cdboi->ptr)) == CC->user.usernum) {
202                                 this_is_mine = 1;
203                         }
204                 }
205                 cdb_free(cdboi);
206         }
207
208         if (!this_is_mine) {
209                 cprintf("%d That OpenID was not found or not associated with your account.\n",
210                         ERROR + ILLEGAL_VALUE);
211                 return;
212         }
213
214         cdb_delete(CDB_OPENID, id_to_detach, strlen(id_to_detach));
215         cprintf("%d %s detached from your account.\n", CIT_OK, id_to_detach);
216 }
217
218
219
220 /*
221  * getuserbyopenid() works the same way as getuser() and getuserbynumber().
222  * If a user account exists which is associated with the Claimed ID, it fills usbuf and returns zero.
223  * Otherwise it returns nonzero.
224  */
225 int getuserbyopenid(struct ctdluser *usbuf, char *claimed_id)
226 {
227         struct cdbdata *cdboi;
228         long usernum = 0;
229
230         cdboi = cdb_fetch(CDB_OPENID, claimed_id, strlen(claimed_id));
231         if (cdboi == NULL) {
232                 return(-1);
233         }
234
235         memcpy(&usernum, cdboi->ptr, sizeof(long));
236         cdb_free(cdboi);
237
238         return(getuserbynumber(usbuf, usernum));
239 }
240
241
242
243 /**************************************************************************/
244 /*                                                                        */
245 /* Functions in this section handle OpenID protocol                       */
246 /*                                                                        */
247 /**************************************************************************/
248
249
250 /* 
251  * Locate a <link> tag and, given its 'rel=' parameter, return its 'href' parameter
252  */
253 void extract_link(char *target_buf, int target_size, char *rel, char *source_buf)
254 {
255         char *ptr = source_buf;
256
257         if (!target_buf) return;
258         if (!rel) return;
259         if (!source_buf) return;
260
261         target_buf[0] = 0;
262
263         while (ptr = bmstrcasestr(ptr, "<link"), ptr != NULL) {
264
265                 char work_buffer[2048];
266                 char *link_tag_start = NULL;
267                 char *link_tag_end = NULL;
268
269                 char rel_tag[2048];
270                 char href_tag[2048];
271
272                 link_tag_start = ptr;
273                 link_tag_end = strchr(ptr, '>');
274                 rel_tag[0] = 0;
275                 href_tag[0] = 0;
276
277                 if ((link_tag_end) && (link_tag_end > link_tag_start)) {
278                         int len;
279                         len = link_tag_end - link_tag_start;
280                         if (len > sizeof work_buffer) len = sizeof work_buffer;
281                         memcpy(work_buffer, link_tag_start, len);
282                 
283                         char *rel_start = NULL;
284                         char *rel_end = NULL;
285                         rel_start = bmstrcasestr(work_buffer, "rel=");
286                         if (rel_start) {
287                                 rel_start = strchr(rel_start, '\"');
288                                 if (rel_start) {
289                                         ++rel_start;
290                                         rel_end = strchr(rel_start, '\"');
291                                         if ((rel_end) && (rel_end > rel_start)) {
292                                                 safestrncpy(rel_tag, rel_start, rel_end - rel_start + 1);
293                                         }
294                                 }
295                         }
296
297                         char *href_start = NULL;
298                         char *href_end = NULL;
299                         href_start = bmstrcasestr(work_buffer, "href=");
300                         if (href_start) {
301                                 href_start = strchr(href_start, '\"');
302                                 if (href_start) {
303                                         ++href_start;
304                                         href_end = strchr(href_start, '\"');
305                                         if ((href_end) && (href_end > href_start)) {
306                                                 safestrncpy(href_tag, href_start, href_end - href_start + 1);
307                                         }
308                                 }
309                         }
310
311                         if (!strcasecmp(rel, rel_tag)) {
312                                 safestrncpy(target_buf, href_tag, target_size);
313                                 return;
314                         }
315
316                 }
317
318         ++ptr;
319         }
320
321
322 }
323
324
325
326 struct fh_data {
327         char *buf;
328         int total_bytes_received;
329         int maxbytes;
330 };
331
332
333 size_t fh_callback(void *ptr, size_t size, size_t nmemb, void *stream)
334 {
335         struct fh_data *fh = (struct fh_data *) stream;
336         int got_bytes = (size * nmemb);
337
338         if (fh->total_bytes_received + got_bytes > fh->maxbytes) {
339                 got_bytes = fh->maxbytes - fh->total_bytes_received;
340         }
341         if (got_bytes > 0) {
342                 memcpy(&fh->buf[fh->total_bytes_received], ptr, got_bytes);
343                 fh->total_bytes_received += got_bytes;
344         }
345
346         return (size * nmemb);  /* always succeed; libcurl doesn't need to know if we truncated it */
347 }
348
349
350
351 /*
352  * Begin an HTTP fetch (returns number of bytes actually fetched, or -1 for error) using libcurl.
353  *
354  * If 'normalize_len' is nonzero, the caller is specifying the buffer size of 'url', and is
355  * requesting that the effective (normalized) URL be copied back to it.
356  */
357 int fetch_http(char *url, char *target_buf, int maxbytes, int normalize_len)
358 {
359         CURL *curl;
360         CURLcode res;
361         char errmsg[1024] = "";
362         struct fh_data fh = {
363                 target_buf,
364                 0,
365                 maxbytes
366         };
367         char *effective_url = NULL;
368
369         if (!url) return(-1);
370         if (!target_buf) return(-1);
371         memset(target_buf, 0, maxbytes);
372
373         curl = curl_easy_init();
374         if (!curl) {
375                 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
376                 return(-1);
377         }
378
379         curl_easy_setopt(curl, CURLOPT_URL, url);
380         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
381         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
382         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fh);
383         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fh_callback);
384         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
385         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
386         curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
387         if (!IsEmptyStr(config.c_ip_addr)) {
388                 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
389         }
390         res = curl_easy_perform(curl);
391         if (res) {
392                 CtdlLogPrintf(CTDL_DEBUG, "fetch_http() libcurl error %d: %s\n", res, errmsg);
393         }
394         if (normalize_len > 0) {
395                 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
396                 safestrncpy(url, effective_url, normalize_len);
397         }
398         curl_easy_cleanup(curl);
399         return fh.total_bytes_received;
400 }
401
402
403 /*
404  * Setup an OpenID authentication
405  */
406 void cmd_oids(char *argbuf) {
407         char return_to[1024];
408         char trust_root[1024];
409         int i;
410         char buf[SIZ];
411         struct CitContext *CCC = CC;    /* CachedCitContext - performance boost */
412         struct ctdl_openid *oiddata;
413
414         /* commented out because we may be attempting to attach an OpenID to
415          * an existing account that is logged in
416          *
417         if (CCC->logged_in) {
418                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
419                 return;
420         }
421          */
422
423         if (CCC->openid_data != NULL) {
424                 free(CCC->openid_data);
425         }
426         oiddata = malloc(sizeof(struct ctdl_openid));
427         if (oiddata == NULL) {
428                 cprintf("%d malloc failed\n", ERROR + INTERNAL_ERROR);
429                 return;
430         }
431         memset(oiddata, 0, sizeof(struct ctdl_openid));
432         CCC->openid_data = (void *) oiddata;
433
434         extract_token(oiddata->claimed_id, argbuf, 0, '|', sizeof oiddata->claimed_id);
435         extract_token(return_to, argbuf, 1, '|', sizeof return_to);
436         extract_token(trust_root, argbuf, 2, '|', sizeof trust_root);
437         oiddata->validated = 0;
438
439         i = fetch_http(oiddata->claimed_id, buf, sizeof buf - 1, sizeof oiddata->claimed_id);
440         CtdlLogPrintf(CTDL_DEBUG, "Normalized URL and Claimed ID is: %s\n", oiddata->claimed_id);
441         buf[sizeof buf - 1] = 0;
442         if (i > 0) {
443                 char openid_delegate[1024];
444
445                 extract_link(oiddata->server, sizeof oiddata->server, "openid.server", buf);
446                 extract_link(openid_delegate, sizeof openid_delegate, "openid.delegate", buf);
447
448                 if (IsEmptyStr(oiddata->server)) {
449                         cprintf("%d There is no OpenID identity provider at this URL.\n", ERROR);
450                         return;
451                 }
452
453                 /* Empty delegate is legal; we just use the openid_url instead */
454                 if (IsEmptyStr(openid_delegate)) {
455                         safestrncpy(openid_delegate, oiddata->claimed_id, sizeof openid_delegate);
456                 }
457
458                 /* Assemble a URL to which the user-agent will be redirected. */
459                 char redirect_string[4096];
460                 char escaped_identity[512];
461                 char escaped_return_to[2048];
462                 char escaped_trust_root[1024];
463                 char escaped_sreg_optional[256];
464
465                 urlesc(escaped_identity, sizeof escaped_identity, openid_delegate);
466                 urlesc(escaped_return_to, sizeof escaped_return_to, return_to);
467                 urlesc(escaped_trust_root, sizeof escaped_trust_root, trust_root);
468                 urlesc(escaped_sreg_optional, sizeof escaped_sreg_optional,
469                         "nickname,email,fullname,postcode,country");
470
471                 snprintf(redirect_string, sizeof redirect_string,
472                         "%s"
473                         "?openid.mode=checkid_setup"
474                         "&openid.identity=%s"
475                         "&openid.return_to=%s"
476                         "&openid.trust_root=%s"
477                         "&openid.sreg.optional=%s"
478                         ,
479                         oiddata->server,
480                         escaped_identity,
481                         escaped_return_to,
482                         escaped_trust_root,
483                         escaped_sreg_optional
484                 );
485                 cprintf("%d %s\n", CIT_OK, redirect_string);
486                 return;
487         }
488
489         cprintf("%d Unable to fetch OpenID URL\n", ERROR);
490 }
491
492
493
494
495
496 /*
497  * Finalize an OpenID authentication
498  */
499 void cmd_oidf(char *argbuf) {
500         char buf[2048];
501         char thiskey[1024];
502         char thisdata[1024];
503         HashList *keys = NULL;
504         struct ctdl_openid *oiddata = (struct ctdl_openid *) CC->openid_data;
505
506         keys = NewHash(1, NULL);
507         if (!keys) {
508                 cprintf("%d NewHash() failed\n", ERROR + INTERNAL_ERROR);
509                 return;
510         }
511         
512         cprintf("%d Transmit OpenID data now\n", START_CHAT_MODE);
513
514         while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
515                 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
516                 extract_token(thisdata, buf, 1, '|', sizeof thisdata);
517                 CtdlLogPrintf(CTDL_DEBUG, "%s: [%d] %s\n", thiskey, strlen(thisdata), thisdata);
518                 Put(keys, thiskey, strlen(thiskey), strdup(thisdata), generic_free_handler);
519         }
520
521
522         /* Now that we have all of the parameters, we have to validate the signature against the server */
523         CtdlLogPrintf(CTDL_DEBUG, "About to validate the signature...\n");
524
525         CURL *curl;
526         CURLcode res;
527         struct curl_httppost *formpost = NULL;
528         struct curl_httppost *lastptr = NULL;
529         char errmsg[1024] = "";
530         char *o_assoc_handle = NULL;
531         char *o_sig = NULL;
532         char *o_signed = NULL;
533         int num_signed_values;
534         int i;
535         char k_keyname[128];
536         char k_o_keyname[128];
537         char *k_value = NULL;
538         char valbuf[1024];
539
540         struct fh_data fh = {
541                 valbuf,
542                 0,
543                 sizeof valbuf
544         };
545
546         curl_formadd(&formpost, &lastptr,
547                 CURLFORM_COPYNAME,      "openid.mode",
548                 CURLFORM_COPYCONTENTS,  "check_authentication",
549                 CURLFORM_END);
550         CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.mode", "check_authentication");
551
552         if (GetHash(keys, "assoc_handle", 12, (void *) &o_assoc_handle)) {
553                 curl_formadd(&formpost, &lastptr,
554                         CURLFORM_COPYNAME,      "openid.assoc_handle",
555                         CURLFORM_COPYCONTENTS,  o_assoc_handle,
556                         CURLFORM_END);
557                 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.assoc_handle", o_assoc_handle);
558         }
559
560         if (GetHash(keys, "sig", 3, (void *) &o_sig)) {
561                 curl_formadd(&formpost, &lastptr,
562                         CURLFORM_COPYNAME,      "openid.sig",
563                         CURLFORM_COPYCONTENTS,  o_sig,
564                         CURLFORM_END);
565                         CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.sig", o_sig);
566         }
567
568         if (GetHash(keys, "signed", 6, (void *) &o_signed)) {
569                 curl_formadd(&formpost, &lastptr,
570                         CURLFORM_COPYNAME,      "openid.signed",
571                         CURLFORM_COPYCONTENTS,  o_signed,
572                         CURLFORM_END);
573                 CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.signed", o_signed);
574
575                 num_signed_values = num_tokens(o_signed, ',');
576                 for (i=0; i<num_signed_values; ++i) {
577                         extract_token(k_keyname, o_signed, i, ',', sizeof k_keyname);
578                         if (strcasecmp(k_keyname, "mode")) {    // work around phpMyID bug
579                                 if (GetHash(keys, k_keyname, strlen(k_keyname), (void *) &k_value)) {
580                                         snprintf(k_o_keyname, sizeof k_o_keyname, "openid.%s", k_keyname);
581                                         curl_formadd(&formpost, &lastptr,
582                                                 CURLFORM_COPYNAME,      k_o_keyname,
583                                                 CURLFORM_COPYCONTENTS,  k_value,
584                                                 CURLFORM_END);
585                                         CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", k_o_keyname, k_value);
586                                 }
587                                 else {
588                                         CtdlLogPrintf(CTDL_INFO, "OpenID: signed field '%s' is missing\n",
589                                                 k_keyname);
590                                 }
591                         }
592                 }
593         }
594
595         curl = curl_easy_init();
596         curl_easy_setopt(curl, CURLOPT_URL, oiddata->server);
597         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
598         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
599         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fh);
600         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fh_callback);
601         curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
602         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
603         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
604         curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
605         if (!IsEmptyStr(config.c_ip_addr)) {
606                 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
607         }
608
609         res = curl_easy_perform(curl);
610         if (res) {
611                 CtdlLogPrintf(CTDL_DEBUG, "cmd_oidf() libcurl error %d: %s\n", res, errmsg);
612         }
613         curl_easy_cleanup(curl);
614         curl_formfree(formpost);
615
616         valbuf[fh.total_bytes_received] = 0;
617
618         if (bmstrcasestr(valbuf, "is_valid:true")) {
619                 oiddata->validated = 1;
620         }
621
622         CtdlLogPrintf(CTDL_DEBUG, "Authentication %s.\n", (oiddata->validated ? "succeeded" : "failed") );
623
624         /* Respond to the client */
625
626         if (oiddata->validated) {
627
628                 /* If we were already logged in, attach the OpenID to the user's account */
629                 if (CC->logged_in) {
630                         if (attach_openid(&CC->user, oiddata->claimed_id) == 0) {
631                                 cprintf("attach\n");
632                         }
633                         else {
634                                 cprintf("fail\n");
635                         }
636                 }
637
638                 /* Otherwise, a user is attempting to log in using the validated OpenID */      
639                 else {
640                         struct ctdluser usbuf;
641
642                         /*
643                          * Existing user who has claimed this OpenID?
644                          *
645                          * Note: if you think that sending the password back over the wire is insecure,
646                          * check your assumptions.  If someone has successfully asserted an OpenID that
647                          * is associated with the account, they already have password equivalency and can
648                          * login, so they could just as easily change the password, etc.
649                          */
650                         if (getuserbyopenid(&usbuf, oiddata->claimed_id) == 0) {
651                                 cprintf("authenticate\n%s\n%s\n", usbuf.fullname, usbuf.password);
652                         }
653
654                         else {
655                                 cprintf("fail\n");              // FIXME do the login here!!
656                         }
657                 }
658         }
659         else {
660                 cprintf("fail\n");
661         }
662         cprintf("000\n");
663
664         /*
665          * We will eventually do something with the data in the hash list.
666          *
667         long len;
668         void *Value;
669         char *Key;
670         HashPos *HashPos;
671         HashPos = GetNewHashPos();
672         while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
673         {
674         }
675         DeleteHashPos(&HashPos);
676          */
677
678         DeleteHash(&keys);              /* This will free() all the key data for us */
679 }
680
681
682 // mode = [6]  id_res
683 // identity = [50]  http://uncensored.citadel.org/~ajc/MyID.config.php
684 // assoc_handle = [26]  6ekac3ju181tgepk7v4h9r7ui7
685 // return_to = [42]  http://jemcaterers.net/finish_openid_login
686 // sreg.nickname = [17]  IGnatius T Foobar
687 // sreg.email = [26]  ajc@uncensored.citadel.org
688 // sreg.fullname = [10]  Art Cancro
689 // sreg.postcode = [5]  10549
690 // sreg.country = [2]  US
691 // signed = [102]  mode,identity,assoc_handle,return_to,sreg.nickname,sreg.email,sreg.fullname,sreg.postcode,sreg.country
692 // sig = [28]  vixxxU4MAqWfxxxxCfrHv3TxxxhEw=
693
694
695
696
697 /**************************************************************************/
698 /*                                                                        */
699 /* Functions in this section handle module initialization and shutdown    */
700 /*                                                                        */
701 /**************************************************************************/
702
703
704 /*
705  * This cleanup function blows away the temporary memory used by this module.
706  */
707 void openid_cleanup_function(void) {
708
709         if (CC->openid_data != NULL) {
710                 free(CC->openid_data);
711         }
712 }
713
714
715 CTDL_MODULE_INIT(openid_rp)
716 {
717         if (!threading)
718         {
719                 curl_global_init(CURL_GLOBAL_ALL);
720                 CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
721                 CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
722                 CtdlRegisterProtoHook(cmd_oidl, "OIDL", "List OpenIDs associated with an account");
723                 CtdlRegisterProtoHook(cmd_oidd, "OIDD", "Detach an OpenID from an account");
724                 CtdlRegisterSessionHook(openid_cleanup_function, EVT_STOP);
725                 CtdlRegisterUserHook(openid_purge, EVT_PURGEUSER);
726         }
727
728         /* return our Subversion id for the Log */
729         return "$Id$";
730 }
731
732
733 /* FIXME ... we have to add the new openid database to serv_vandelay.c */
734