]> 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 54294747767b9107d386882dff997cc9ddbb9838..0a676f70016bd0170da8845cc52dd66c68d6d5c9 100644 (file)
@@ -33,6 +33,7 @@
 #include "ctdl_module.h"
 #include "config.h"
 #include "citserver.h"
+#include "user_ops.h"
 
 struct ctdl_openid {
        char claimed_id[1024];
@@ -41,9 +42,7 @@ struct ctdl_openid {
 };
 
 
-/* This is all disabled temporarily */
 
-#if 0
 
 
 /**************************************************************************/
@@ -57,7 +56,7 @@ struct ctdl_openid {
  * The structure of an openid record *key* is:
  *
  * |--------------claimed_id-------------|
- *     (auctual length of claimed id)
+ *     (actual length of claimed id)
  *
  *
  * The structure of an openid record *value* is:
@@ -68,6 +67,7 @@ struct ctdl_openid {
  */
 
 
+
 /*
  * Attach an OpenID to a Citadel account
  */
@@ -120,7 +120,40 @@ int attach_openid(struct ctdluser *who, char *claimed_id)
  * 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);
 }
 
 
@@ -137,14 +170,119 @@ void cmd_oidl(char *argbuf) {
 
        while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
                if (cdboi->len > sizeof(long)) {
-               cprintf("%s\n", cdboi->ptr + 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);
+}
+
+
+
+/*
+ * Attempt to auto-create a new Citadel account using the nickname from Simple Registration Extension
+ */
+int openid_create_user_via_sri(char *claimed_id, HashList *sri_keys)
+{
+       char *desired_name = NULL;
+
+       if (config.c_auth_mode != AUTHMODE_NATIVE) return(1);
+       if (config.c_disable_newu) return(2);
+       if (CC->logged_in) return(3);
+       if (!GetHash(sri_keys, "sreg.nickname", 13, (void *) &desired_name)) return(4);
+
+       CtdlLogPrintf(CTDL_DEBUG, "The desired account name is <%s>\n", desired_name);
+
+       if (!getuser(&CC->user, desired_name)) {
+               CtdlLogPrintf(CTDL_DEBUG, "<%s> is already taken by another user.\n", desired_name);
+               memset(&CC->user, 0, sizeof(struct ctdluser));
+               return(5);
+       }
+
+       /* The desired account name is available.  Create the account and log it in! */
+       if (create_user(desired_name, 1)) return(6);
+
+       attach_openid(&CC->user, claimed_id);
+       return(0);
+}
+
+
+// identity = [50]  http://uncensored.citadel.org/~ajc/MyID.config.php
+// sreg.nickname = [17]  IGnatius T Foobar
+// sreg.email = [26]  ajc@uncensored.citadel.org
+// sreg.fullname = [10]  Art Cancro
+// sreg.postcode = [5]  10549
+// sreg.country = [2]  US
+
+
+
+/*
+ * If a user account exists which is associated with the Claimed ID, log it in and return zero.
+ * Otherwise it returns nonzero.
+ */
+int login_via_openid(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);
+
+       if (!getuserbynumber(&CC->user, usernum)) {
+               do_login();
+               return(0);
+       }
+       else {
+               memset(&CC->user, 0, sizeof(struct ctdluser));
+               return(-1);
+       }
+}
+
+
+
 
 /**************************************************************************/
 /*                                                                        */
@@ -397,12 +535,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);
-}
 
 
 /*
@@ -413,7 +545,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);
@@ -428,7 +559,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);
        }
 
 
@@ -550,41 +681,55 @@ 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!!
-               }
+                       /*
+                        * 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 (login_via_openid(oiddata->claimed_id) == 0) {
+                               cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
+                               logged_in_response();
+                       }
+
+                       /*
+                        * New user whose OpenID is verified and Simple Registration Extension is in use?
+                        */
+                       else if (openid_create_user_via_sri(oiddata->claimed_id, keys) == 0) {
+                               cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
+                               logged_in_response();
+                       }
+
+                       /* FIXME right here we have to handle manual account creation */
 
+                       else {
+                               cprintf("fail\n");
+                       }
+               }
        }
        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);
-}
-
-
-// mode = [6]  id_res
-// identity = [50]  http://uncensored.citadel.org/~ajc/MyID.config.php
-// assoc_handle = [26]  6ekac3ju181tgepk7v4h9r7ui7
-// return_to = [42]  http://jemcaterers.net/finish_openid_login
-// sreg.nickname = [17]  IGnatius T Foobar
-// sreg.email = [26]  ajc@uncensored.citadel.org
-// sreg.fullname = [10]  Art Cancro
-// sreg.postcode = [5]  10549
-// sreg.country = [2]  US
-// signed = [102]  mode,identity,assoc_handle,return_to,sreg.nickname,sreg.email,sreg.fullname,sreg.postcode,sreg.country
-// sig = [28]  vixxxU4MAqWfxxxxCfrHv3TxxxhEw=
+        */
 
+       DeleteHash(&keys);              /* This will free() all the key data for us */
+}
 
 
 
@@ -614,6 +759,7 @@ CTDL_MODULE_INIT(openid_rp)
                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);
        }
@@ -625,9 +771,3 @@ CTDL_MODULE_INIT(openid_rp)
 
 /* FIXME ... we have to add the new openid database to serv_vandelay.c */
 
-
-#endif
-CTDL_MODULE_INIT(openid_rp)
-{
-       return "$Id$";
-}