]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/openid/serv_openid_rp.c
more openid work
[citadel.git] / citadel / modules / openid / serv_openid_rp.c
index 7be8bacc0203e905d8863b7399d40ac38d50b94a..1c98d5372770f6ef8f12669d2305cf5ec1ad8b8a 100644 (file)
 #include <limits.h>
 #include <curl/curl.h>
 #include "ctdl_module.h"
+#include "config.h"
+#include "citserver.h"
 
 struct ctdl_openid {
        char claimed_id[1024];
        char server[1024];
+       int validated;
 };
 
 
+
+
+
+/**************************************************************************/
+/*                                                                        */
+/* Functions in this section handle Citadel internal OpenID mapping stuff */
+/*                                                                        */
+/**************************************************************************/
+
+
+/*
+ * The structure of an openid record *key* is:
+ *
+ * |--------------claimed_id-------------|
+ *     (auctual length of claimed id)
+ *
+ *
+ * The structure of an openid record *value* is:
+ *
+ * |-----user_number----|------------claimed_id---------------|
+ *    (sizeof long)          (actual length of claimed id)
+ *
+ */
+
+
+/*
+ * Attach an OpenID to a Citadel account
+ */
+int attach_openid(struct ctdluser *who, char *claimed_id)
+{
+       struct cdbdata *cdboi;
+       long fetched_usernum;
+       char *data;
+       int data_len;
+
+       if (!who) return(1);
+       if (!claimed_id) return(1);
+       if (IsEmptyStr(claimed_id)) return(1);
+
+       /* Check to see if this OpenID is already in the database */
+
+       cdboi = cdb_fetch(CDB_OPENID, claimed_id, strlen(claimed_id));
+       if (cdboi != NULL) {
+               memcpy(&fetched_usernum, cdboi->ptr, sizeof(long));
+               cdb_free(cdboi);
+
+               if (fetched_usernum == who->usernum) {
+                       CtdlLogPrintf(CTDL_INFO, "%s already associated; no action is taken\n", claimed_id);
+                       return(0);
+               }
+               else {
+                       CtdlLogPrintf(CTDL_INFO, "%s already belongs to another user\n", claimed_id);
+                       return(3);
+               }
+       }
+
+       /* Not already in the database, so attach it now */
+
+       data_len = sizeof(long) + strlen(claimed_id) + 1;
+       data = malloc(data_len);
+
+       memcpy(data, &who->usernum, sizeof(long));
+       memcpy(&data[sizeof(long)], claimed_id, strlen(claimed_id) + 1);
+
+       cdb_store(CDB_OPENID, claimed_id, strlen(claimed_id), data, data_len);
+       free(data);
+
+       CtdlLogPrintf(CTDL_INFO, "%s has been associated with %s (%ld)\n",
+               claimed_id, who->fullname, who->usernum);
+       return(0);
+}
+
+
+/*
+ * When a user is being deleted, we have to delete any OpenID associations
+ */
+void openid_purge(struct ctdluser *usbuf) {
+       /* FIXME finish this */
+}
+
+
+
+/*
+ * List the OpenIDs associated with the currently logged in account
+ */
+void cmd_oidl(char *argbuf) {
+       struct cdbdata *cdboi;
+
+       if (CtdlAccessCheck(ac_logged_in)) return;
+       cdb_rewind(CDB_OPENID);
+       cprintf("%d Associated OpenIDs:\n", LISTING_FOLLOWS);
+
+       while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
+               if (cdboi->len > sizeof(long)) {
+               cprintf("%s\n", cdboi->ptr + sizeof(long));
+               }
+       }
+       cprintf("000\n");
+}
+
+
+
+
+/**************************************************************************/
+/*                                                                        */
+/* Functions in this section handle OpenID protocol                       */
+/*                                                                        */
+/**************************************************************************/
+
+
 /* 
  * Locate a <link> tag and, given its 'rel=' parameter, return its 'href' parameter
  */
@@ -134,7 +247,7 @@ size_t fh_callback(void *ptr, size_t size, size_t nmemb, void *stream)
                fh->total_bytes_received += got_bytes;
        }
 
-       return got_bytes;
+       return (size * nmemb);  /* always succeed; libcurl doesn't need to know if we truncated it */
 }
 
 
@@ -174,6 +287,10 @@ int fetch_http(char *url, char *target_buf, int maxbytes, int normalize_len)
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fh_callback);
        curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
+       curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
+       if (!IsEmptyStr(config.c_ip_addr)) {
+               curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
+       }
        res = curl_easy_perform(curl);
        if (res) {
                CtdlLogPrintf(CTDL_DEBUG, "fetch_http() libcurl error %d: %s\n", res, errmsg);
@@ -198,10 +315,14 @@ void cmd_oids(char *argbuf) {
        struct CitContext *CCC = CC;    /* CachedCitContext - performance boost */
        struct ctdl_openid *oiddata;
 
+       /* commented out because we may be attempting to attach an OpenID to
+        * an existing account that is logged in
+        *
        if (CCC->logged_in) {
                cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
                return;
        }
+        */
 
        if (CCC->openid_data != NULL) {
                free(CCC->openid_data);
@@ -217,6 +338,7 @@ void cmd_oids(char *argbuf) {
        extract_token(oiddata->claimed_id, argbuf, 0, '|', sizeof oiddata->claimed_id);
        extract_token(return_to, argbuf, 1, '|', sizeof return_to);
        extract_token(trust_root, argbuf, 2, '|', sizeof trust_root);
+       oiddata->validated = 0;
 
        i = fetch_http(oiddata->claimed_id, buf, sizeof buf - 1, sizeof oiddata->claimed_id);
        CtdlLogPrintf(CTDL_DEBUG, "Normalized URL and Claimed ID is: %s\n", oiddata->claimed_id);
@@ -290,6 +412,7 @@ void cmd_oidf(char *argbuf) {
        char thisdata[1024];
        HashList *keys = NULL;
        HashPos *HashPos;
+       struct ctdl_openid *oiddata = (struct ctdl_openid *) CC->openid_data;
 
        keys = NewHash(1, NULL);
        if (!keys) {
@@ -308,27 +431,92 @@ void cmd_oidf(char *argbuf) {
 
 
        /* Now that we have all of the parameters, we have to validate the signature against the server */
+       CtdlLogPrintf(CTDL_DEBUG, "About to validate the signature...\n");
 
        CURL *curl;
        CURLcode res;
        struct curl_httppost *formpost = NULL;
        struct curl_httppost *lastptr = NULL;
        char errmsg[1024] = "";
+       char *o_assoc_handle = NULL;
+       char *o_sig = NULL;
+       char *o_signed = NULL;
+       int num_signed_values;
+       int i;
+       char k_keyname[128];
+       char k_o_keyname[128];
+       char *k_value = NULL;
+       char valbuf[1024];
+
+       struct fh_data fh = {
+               valbuf,
+               0,
+               sizeof valbuf
+       };
 
        curl_formadd(&formpost, &lastptr,
                CURLFORM_COPYNAME,      "openid.mode",
                CURLFORM_COPYCONTENTS,  "check_authentication",
                CURLFORM_END);
+       CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.mode", "check_authentication");
+
+       if (GetHash(keys, "assoc_handle", 12, (void *) &o_assoc_handle)) {
+               curl_formadd(&formpost, &lastptr,
+                       CURLFORM_COPYNAME,      "openid.assoc_handle",
+                       CURLFORM_COPYCONTENTS,  o_assoc_handle,
+                       CURLFORM_END);
+               CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.assoc_handle", o_assoc_handle);
+       }
+
+       if (GetHash(keys, "sig", 3, (void *) &o_sig)) {
+               curl_formadd(&formpost, &lastptr,
+                       CURLFORM_COPYNAME,      "openid.sig",
+                       CURLFORM_COPYCONTENTS,  o_sig,
+                       CURLFORM_END);
+                       CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.sig", o_sig);
+       }
+
+       if (GetHash(keys, "signed", 6, (void *) &o_signed)) {
+               curl_formadd(&formpost, &lastptr,
+                       CURLFORM_COPYNAME,      "openid.signed",
+                       CURLFORM_COPYCONTENTS,  o_signed,
+                       CURLFORM_END);
+               CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", "openid.signed", o_signed);
+
+               num_signed_values = num_tokens(o_signed, ',');
+               for (i=0; i<num_signed_values; ++i) {
+                       extract_token(k_keyname, o_signed, i, ',', sizeof k_keyname);
+                       if (strcasecmp(k_keyname, "mode")) {    // work around phpMyID bug
+                               if (GetHash(keys, k_keyname, strlen(k_keyname), (void *) &k_value)) {
+                                       snprintf(k_o_keyname, sizeof k_o_keyname, "openid.%s", k_keyname);
+                                       curl_formadd(&formpost, &lastptr,
+                                               CURLFORM_COPYNAME,      k_o_keyname,
+                                               CURLFORM_COPYCONTENTS,  k_value,
+                                               CURLFORM_END);
+                                       CtdlLogPrintf(CTDL_DEBUG, "%25s : %s\n", k_o_keyname, k_value);
+                               }
+                               else {
+                                       CtdlLogPrintf(CTDL_INFO, "OpenID: signed field '%s' is missing\n",
+                                               k_keyname);
+                               }
+                       }
+               }
+       }
 
        curl = curl_easy_init();
-       // curl_easy_setopt(curl, CURLOPT_URL, FIXME );         /* FIXME need to bring this over */
+       curl_easy_setopt(curl, CURLOPT_URL, oiddata->server);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
-       // curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fh);
-       // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fh_callback);
+       curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fh);
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fh_callback);
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
        curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
+       curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
+       if (!IsEmptyStr(config.c_ip_addr)) {
+               curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
+       }
+
        res = curl_easy_perform(curl);
        if (res) {
                CtdlLogPrintf(CTDL_DEBUG, "cmd_oidf() libcurl error %d: %s\n", res, errmsg);
@@ -336,10 +524,37 @@ void cmd_oidf(char *argbuf) {
        curl_easy_cleanup(curl);
        curl_formfree(formpost);
 
-       /* FIXME do something with the results */
+       valbuf[fh.total_bytes_received] = 0;
+
+       if (bmstrcasestr(valbuf, "is_valid:true")) {
+               oiddata->validated = 1;
+       }
+
+       CtdlLogPrintf(CTDL_DEBUG, "Authentication %s.\n", (oiddata->validated ? "succeeded" : "failed") );
 
        /* Respond to the client */
-       cprintf("message|FIXME finish this\n");
+
+       if (oiddata->validated) {
+
+               /* If we were already logged in, attach the OpenID to the user's account */
+               if (CC->logged_in) {
+                       if (attach_openid(&CC->user, oiddata->claimed_id) == 0) {
+                               cprintf("attach\n");
+                       }
+                       else {
+                               cprintf("fail\n");
+                       }
+               }
+
+               /* Otherwise, a user is attempting to log in using the validated OpenID */      
+               else {
+                       cprintf("fail\n");              // FIXME do the login here!!
+               }
+
+       }
+       else {
+               cprintf("fail\n");
+       }
        cprintf("000\n");
 
        /* Free the hash list */
@@ -369,6 +584,15 @@ void cmd_oidf(char *argbuf) {
 // sig = [28]  vixxxU4MAqWfxxxxCfrHv3TxxxhEw=
 
 
+
+
+/**************************************************************************/
+/*                                                                        */
+/* Functions in this section handle module initialization and shutdown    */
+/*                                                                        */
+/**************************************************************************/
+
+
 /*
  * This cleanup function blows away the temporary memory used by this module.
  */
@@ -380,7 +604,6 @@ void openid_cleanup_function(void) {
 }
 
 
-
 CTDL_MODULE_INIT(openid_rp)
 {
        if (!threading)
@@ -388,9 +611,14 @@ CTDL_MODULE_INIT(openid_rp)
                curl_global_init(CURL_GLOBAL_ALL);
                CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
                CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
+               CtdlRegisterProtoHook(cmd_oidl, "OIDL", "List OpenIDs associated with an account");
                CtdlRegisterSessionHook(openid_cleanup_function, EVT_STOP);
+               CtdlRegisterUserHook(openid_purge, EVT_PURGEUSER);
        }
 
        /* return our Subversion id for the Log */
        return "$Id$";
 }
+
+
+/* FIXME ... we have to add the new openid database to serv_vandelay.c */