]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/openid/serv_openid_rp.c
* Older versions of libcurl may not have CURLOPT_HTTP_CONTENT_DECODING support. ...
[citadel.git] / citadel / modules / openid / serv_openid_rp.c
index e82f0debd993084eb33c9d0d217c11e1e8d28ce0..4c191e00b1b45d261d3dbc8634111faeb8dcbae0 100644 (file)
@@ -39,6 +39,7 @@ struct ctdl_openid {
        char claimed_id[1024];
        char server[1024];
        int verified;
+       HashList *sreg_keys;
 };
 
 
@@ -77,6 +78,7 @@ int attach_openid(struct ctdluser *who, char *claimed_id)
        long fetched_usernum;
        char *data;
        int data_len;
+       char buf[2048];
 
        if (!who) return(1);
        if (!claimed_id) return(1);
@@ -110,8 +112,10 @@ int attach_openid(struct ctdluser *who, char *claimed_id)
        cdb_store(CDB_OPENID, claimed_id, strlen(claimed_id), data, data_len);
        free(data);
 
-       CtdlLogPrintf(CTDL_INFO, "%s has been attached to %s (%ld)\n",
-               claimed_id, who->fullname, who->usernum);
+       snprintf(buf, sizeof buf, "User <%s> (#%ld) has claimed the OpenID URL %s\n",
+               who->fullname, who->usernum, claimed_id);
+       aide_message(buf, "OpenID claim");
+       CtdlLogPrintf(CTDL_INFO, "%s", buf);
        return(0);
 }
 
@@ -127,7 +131,7 @@ void openid_purge(struct ctdluser *usbuf) {
        char *deleteme = NULL;
        long len;
        void *Value;
-       char *Key;
+       const char *Key;
 
        keys = NewHash(1, NULL);
        if (!keys) return;
@@ -146,7 +150,7 @@ void openid_purge(struct ctdluser *usbuf) {
 
        /* Go through the hash list, deleting keys we stored in it */
 
-       HashPos = GetNewHashPos();
+       HashPos = GetNewHashPos(keys, 0);
        while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
        {
                CtdlLogPrintf(CTDL_DEBUG, "Deleting associated OpenID <%s>\n", Value);
@@ -181,70 +185,6 @@ void cmd_oidl(char *argbuf) {
 }
 
 
-/*
- * Create a new user account, manually specifying the name, after successfully
- * verifying an OpenID (which will of course be attached to the account)
- */
-void cmd_oidc(char *argbuf) {
-       struct ctdl_openid *oiddata = (struct ctdl_openid *) CC->openid_data;
-
-       if (!oiddata->verified) {
-               cprintf("%d You have not verified an OpenID yet.\n", ERROR);
-               return;
-       }
-
-       /* We can make the semantics of OIDC exactly the same as NEWU, simply
-        * by _calling_ cmd_newu() and letting it run.  Very clever!
-        */
-       cmd_newu(argbuf);
-
-       /* Now, if this logged us in, we have to attach the OpenID */
-       if (CC->logged_in) {
-               attach_openid(&CC->user, oiddata->claimed_id);
-               /* populate_vcard_from_sreg(FIXME GET THE SREG DATA IN HERE SOMEHOW); */
-       }
-
-}
-
-
-
-
-/*
- * 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 register (populate the vCard) the currently-logged-in user
  * using the data from Simple Registration Extension, if present.
@@ -314,21 +254,12 @@ void populate_vcard_from_sreg(HashList *sreg_keys) {
 
        /* Only save the vCard if there is some useful data in it */
        if (pop > 0) {
-               char temp[PATH_MAX];
-               FILE *fp;
                char *ser;
-       
-               CtdlMakeTempFileName(temp, sizeof temp);
                ser = vcard_serialize(v);
                if (ser) {
-                       CtdlLogPrintf(CTDL_DEBUG, "--- BEGIN VCARD ---\n%s\n--- END VCARD ---\n", ser);
-                       fp = fopen(temp, "w");
-                       if (fp) {
-                               fwrite(ser, strlen(ser), 1, fp);
-                               fclose(fp);
-                               CtdlWriteObject(USERCONFIGROOM, "text/x-vcard", temp, &CC->user, 0, 0, 0);
-                               unlink(temp);
-                       }
+                       CtdlWriteObject(USERCONFIGROOM, "text/x-vcard",
+                               ser, strlen(ser)+1, &CC->user, 0, 0, 0
+                       );
                        free(ser);
                }
        }
@@ -336,6 +267,72 @@ void populate_vcard_from_sreg(HashList *sreg_keys) {
 }
 
 
+/*
+ * Create a new user account, manually specifying the name, after successfully
+ * verifying an OpenID (which will of course be attached to the account)
+ */
+void cmd_oidc(char *argbuf) {
+       struct ctdl_openid *oiddata = (struct ctdl_openid *) CC->openid_data;
+
+       if (!oiddata->verified) {
+               cprintf("%d You have not verified an OpenID yet.\n", ERROR);
+               return;
+       }
+
+       /* We can make the semantics of OIDC exactly the same as NEWU, simply
+        * by _calling_ cmd_newu() and letting it run.  Very clever!
+        */
+       cmd_newu(argbuf);
+
+       /* Now, if this logged us in, we have to attach the OpenID */
+       if (CC->logged_in) {
+               attach_openid(&CC->user, oiddata->claimed_id);
+               if (oiddata->sreg_keys != NULL) {
+                       populate_vcard_from_sreg(oiddata->sreg_keys);
+               }
+       }
+
+}
+
+
+
+
+/*
+ * 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
  */
@@ -543,7 +540,12 @@ 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);
+#ifdef CURLOPT_HTTP_CONTENT_DECODING
+       curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
+       curl_easy_setopt(curl, CURLOPT_ENCODING, "");
+#endif
        curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
+       curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);           /* die after 180 seconds */
        if (!IsEmptyStr(config.c_ip_addr)) {
                curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
        }
@@ -571,16 +573,13 @@ 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;
-       }
-        */
+       oiddata = (struct ctdl_openid *) CCC->openid_data;
 
-       if (CCC->openid_data != NULL) {
+       if (oiddata != NULL) {
+               if (oiddata->sreg_keys != NULL) {
+                       DeleteHash(&oiddata->sreg_keys);
+                       oiddata->sreg_keys = NULL;
+               }
                free(CCC->openid_data);
        }
        oiddata = malloc(sizeof(struct ctdl_openid));
@@ -761,7 +760,12 @@ void cmd_oidf(char *argbuf) {
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
        curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
+#ifdef CURLOPT_HTTP_CONTENT_DECODING
+       curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
+       curl_easy_setopt(curl, CURLOPT_ENCODING, "");
+#endif
        curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
+       curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);           /* die after 180 seconds */
        if (!IsEmptyStr(config.c_ip_addr)) {
                curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
        }
@@ -810,6 +814,14 @@ void cmd_oidf(char *argbuf) {
                                logged_in_response();
                        }
 
+                       /*
+                        * If this system does not allow self-service new user registration, the
+                        * remaining modes do not apply, so fail here and now.
+                        */
+                       else if (config.c_disable_newu) {
+                               cprintf("fail\n");
+                       }
+
                        /*
                         * New user whose OpenID is verified and Simple Registration Extension is in use?
                         */
@@ -840,7 +852,11 @@ void cmd_oidf(char *argbuf) {
        }
        cprintf("000\n");
 
-       DeleteHash(&keys);              /* This will free() all the key data for us */
+       if (oiddata->sreg_keys != NULL) {
+               DeleteHash(&oiddata->sreg_keys);
+               oiddata->sreg_keys = NULL;
+       }
+       oiddata->sreg_keys = keys;
 }
 
 
@@ -856,24 +872,32 @@ void cmd_oidf(char *argbuf) {
  * This cleanup function blows away the temporary memory used by this module.
  */
 void openid_cleanup_function(void) {
+       struct ctdl_openid *oiddata = (struct ctdl_openid *) CC->openid_data;
 
-       if (CC->openid_data != NULL) {
+       if (oiddata != NULL) {
                CtdlLogPrintf(CTDL_DEBUG, "Clearing OpenID session state\n");
-               free(CC->openid_data);
+               if (oiddata->sreg_keys != NULL) {
+                       DeleteHash(&oiddata->sreg_keys);
+                       oiddata->sreg_keys = NULL;
+               }
+               free(oiddata);
        }
 }
 
 
 CTDL_MODULE_INIT(openid_rp)
 {
-       if (!threading)
-       {
+       if (!threading) {
                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");
-               CtdlRegisterProtoHook(cmd_oidc, "OIDC", "Create a new user after validating an OpenID");
+
+               /* Only enable the OpenID command set when native mode authentication is in use. */
+               if (config.c_auth_mode == AUTHMODE_NATIVE) {
+                       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");
+                       CtdlRegisterProtoHook(cmd_oidc, "OIDC", "Create new user after validating OpenID");
+               }
                CtdlRegisterSessionHook(openid_cleanup_function, EVT_LOGOUT);
                CtdlRegisterUserHook(openid_purge, EVT_PURGEUSER);
        }