]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/openid/serv_openid_rp.c
Fixed a double free error in serv_openid_rp.c
[citadel.git] / citadel / modules / openid / serv_openid_rp.c
index c6765517f20d7db1b450d53a84b5affd4c03a42e..bd883bf31e4b6110463879a2218a8aa0eaeca3d0 100644 (file)
@@ -32,6 +32,8 @@
 #include <curl/curl.h>
 #include "ctdl_module.h"
 #include "config.h"
+#include "citserver.h"
+#include "user_ops.h"
 
 struct ctdl_openid {
        char claimed_id[1024];
@@ -51,21 +53,66 @@ struct ctdl_openid {
 
 
 /*
- * Attach or detach an OpenID to a Citadel account
+ * 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)
+ *
  */
 
-enum {
-       moa_detach,
-       moa_attach
-};
 
-int modify_openid_associations(struct ctdluser *who, char *claimed_id, int operation)
+
+/*
+ * 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);
 
-       return(2);              // error because we are not done yet FIXME
+       /* 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);
 }
 
 
@@ -73,12 +120,123 @@ int modify_openid_associations(struct ctdluser *who, char *claimed_id, int opera
  * When a user is being deleted, we have to delete any OpenID associations
  */
 void openid_purge(struct ctdluser *usbuf) {
-       /* FIXME finish this */
+       struct cdbdata *cdboi;
+       HashList *keys = NULL;
+       HashPos *HashPos;
+       char *deleteme = NULL;
+       long len;
+       void *Value;
+       char *Key;
+
+       keys = NewHash(1, NULL);
+       if (!keys) return;
+
+
+       cdb_rewind(CDB_OPENID);
+       while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
+               if (cdboi->len > sizeof(long)) {
+                       if (((long)*(cdboi->ptr)) == usbuf->usernum) {
+                               deleteme = strdup(cdboi->ptr + sizeof(long)),
+                               Put(keys, deleteme, strlen(deleteme), deleteme, generic_free_handler);
+                       }
+               }
+               cdb_free(cdboi);
+       }
+
+       /* Go through the hash list, deleting keys we stored in it */
+
+       HashPos = GetNewHashPos();
+       while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
+       {
+               CtdlLogPrintf(CTDL_DEBUG, "Deleting associated OpenID <%s>\n", Value);
+               cdb_delete(CDB_OPENID, Value, strlen(Value));
+               /* note: don't free(Value) -- deleting the hash list will handle this for us */
+       }
+       DeleteHashPos(&HashPos);
+       DeleteHash(&keys);
+}
+
+
+
+/*
+ * 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)) {
+                       if (((long)*(cdboi->ptr)) == CC->user.usernum) {
+                               cprintf("%s\n", cdboi->ptr + sizeof(long));
+                       }
+               }
+               cdb_free(cdboi);
+       }
+       cprintf("000\n");
 }
 
 
 
+/*
+ * Detach an OpenID from the currently logged in account
+ */
+void cmd_oidd(char *argbuf) {
+       struct cdbdata *cdboi;
+       char id_to_detach[1024];
+       int this_is_mine = 0;
+
+       if (CtdlAccessCheck(ac_logged_in)) return;
+       extract_token(id_to_detach, argbuf, 0, '|', sizeof id_to_detach);
+       if (IsEmptyStr(id_to_detach)) {
+               cprintf("%d An empty OpenID URL is not allowed.\n", ERROR + ILLEGAL_VALUE);
+       }
+
+       cdb_rewind(CDB_OPENID);
+       while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
+               if (cdboi->len > sizeof(long)) {
+                       if (((long)*(cdboi->ptr)) == CC->user.usernum) {
+                               this_is_mine = 1;
+                       }
+               }
+               cdb_free(cdboi);
+       }
+
+       if (!this_is_mine) {
+               cprintf("%d That OpenID was not found or not associated with your account.\n",
+                       ERROR + ILLEGAL_VALUE);
+               return;
+       }
+
+       cdb_delete(CDB_OPENID, id_to_detach, strlen(id_to_detach));
+       cprintf("%d %s detached from your account.\n", CIT_OK, id_to_detach);
+}
+
+
+
+/*
+ * getuserbyopenid() works the same way as getuser() and getuserbynumber().
+ * If a user account exists which is associated with the Claimed ID, it fills usbuf and returns zero.
+ * Otherwise it returns nonzero.
+ */
+int getuserbyopenid(struct ctdluser *usbuf, char *claimed_id)
+{
+       struct cdbdata *cdboi;
+       long usernum = 0;
+
+       cdboi = cdb_fetch(CDB_OPENID, claimed_id, strlen(claimed_id));
+       if (cdboi == NULL) {
+               return(-1);
+       }
+
+       memcpy(&usernum, cdboi->ptr, sizeof(long));
+       cdb_free(cdboi);
 
+       return(getuserbynumber(usbuf, usernum));
+}
 
 
 
@@ -333,12 +491,6 @@ void cmd_oids(char *argbuf) {
 
 
 
-/*
- * Callback function to free a pointer (used below in the hash list)
- */
-void free_oid_key(void *ptr) {
-       free(ptr);
-}
 
 
 /*
@@ -349,7 +501,6 @@ void cmd_oidf(char *argbuf) {
        char thiskey[1024];
        char thisdata[1024];
        HashList *keys = NULL;
-       HashPos *HashPos;
        struct ctdl_openid *oiddata = (struct ctdl_openid *) CC->openid_data;
 
        keys = NewHash(1, NULL);
@@ -364,7 +515,7 @@ void cmd_oidf(char *argbuf) {
                extract_token(thiskey, buf, 0, '|', sizeof thiskey);
                extract_token(thisdata, buf, 1, '|', sizeof thisdata);
                CtdlLogPrintf(CTDL_DEBUG, "%s: [%d] %s\n", thiskey, strlen(thisdata), thisdata);
-               Put(keys, thiskey, strlen(thiskey), strdup(thisdata), free_oid_key);
+               Put(keys, thiskey, strlen(thiskey), strdup(thisdata), generic_free_handler);
        }
 
 
@@ -476,7 +627,7 @@ void cmd_oidf(char *argbuf) {
 
                /* If we were already logged in, attach the OpenID to the user's account */
                if (CC->logged_in) {
-                       if (modify_openid_associations(&CC->user, oiddata->claimed_id, moa_attach) == 0) {
+                       if (attach_openid(&CC->user, oiddata->claimed_id) == 0) {
                                cprintf("attach\n");
                        }
                        else {
@@ -486,26 +637,45 @@ void cmd_oidf(char *argbuf) {
 
                /* Otherwise, a user is attempting to log in using the validated OpenID */      
                else {
-                       cprintf("fail\n");              // FIXME do the login here!!
-               }
+                       struct ctdluser usbuf;
+
+                       /*
+                        * Existing user who has claimed this OpenID?
+                        *
+                        * Note: if you think that sending the password back over the wire is insecure,
+                        * check your assumptions.  If someone has successfully asserted an OpenID that
+                        * is associated with the account, they already have password equivalency and can
+                        * login, so they could just as easily change the password, etc.
+                        */
+                       if (getuserbyopenid(&usbuf, oiddata->claimed_id) == 0) {
+                               cprintf("authenticate\n%s\n%s\n", usbuf.fullname, usbuf.password);
+                       }
 
+                       else {
+                               cprintf("fail\n");              // FIXME do the login here!!
+                       }
+               }
        }
        else {
                cprintf("fail\n");
        }
        cprintf("000\n");
 
-       /* Free the hash list */
+       /*
+        * We will eventually do something with the data in the hash list.
+        *
        long len;
        void *Value;
        char *Key;
-
+       HashPos *HashPos;
        HashPos = GetNewHashPos();
        while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
        {
-               free(Value);
        }
        DeleteHashPos(&HashPos);
+        */
+
+       DeleteHash(&keys);              /* This will free() all the key data for us */
 }
 
 
@@ -549,6 +719,8 @@ 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");
+               CtdlRegisterProtoHook(cmd_oidd, "OIDD", "Detach an OpenID from an account");
                CtdlRegisterSessionHook(openid_cleanup_function, EVT_STOP);
                CtdlRegisterUserHook(openid_purge, EVT_PURGEUSER);
        }
@@ -556,3 +728,7 @@ CTDL_MODULE_INIT(openid_rp)
        /* return our Subversion id for the Log */
        return "$Id$";
 }
+
+
+/* FIXME ... we have to add the new openid database to serv_vandelay.c */
+