]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/openid/serv_openid_rp.c
* Removed all cases of '((long)*(cdboi->ptr))' in serv_openid_rp.c and replaced them...
[citadel.git] / citadel / modules / openid / serv_openid_rp.c
index 44f087d8016df1486c8366f0f9799e7045ccd393..cfd85cd4722dfb7d0b1efdf5be6d92fed0f961d2 100644 (file)
@@ -78,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);
@@ -111,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);
 }
 
@@ -128,7 +131,8 @@ void openid_purge(struct ctdluser *usbuf) {
        char *deleteme = NULL;
        long len;
        void *Value;
-       char *Key;
+       const char *Key;
+       long usernum = 0L;
 
        keys = NewHash(1, NULL);
        if (!keys) return;
@@ -137,7 +141,8 @@ void openid_purge(struct ctdluser *usbuf) {
        cdb_rewind(CDB_OPENID);
        while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
                if (cdboi->len > sizeof(long)) {
-                       if (((long)*(cdboi->ptr)) == usbuf->usernum) {
+                       memcpy(&usernum, cdboi->ptr, sizeof(long));
+                       if (usernum == usbuf->usernum) {
                                deleteme = strdup(cdboi->ptr + sizeof(long)),
                                Put(keys, deleteme, strlen(deleteme), deleteme, generic_free_handler);
                        }
@@ -147,7 +152,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);
@@ -165,6 +170,7 @@ void openid_purge(struct ctdluser *usbuf) {
  */
 void cmd_oidl(char *argbuf) {
        struct cdbdata *cdboi;
+       long usernum = 0L;
 
        if (CtdlAccessCheck(ac_logged_in)) return;
        cdb_rewind(CDB_OPENID);
@@ -172,7 +178,8 @@ void cmd_oidl(char *argbuf) {
 
        while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
                if (cdboi->len > sizeof(long)) {
-                       if (((long)*(cdboi->ptr)) == CC->user.usernum) {
+                       memcpy(&usernum, cdboi->ptr, sizeof(long));
+                       if (usernum == CC->user.usernum) {
                                cprintf("%s\n", cdboi->ptr + sizeof(long));
                        }
                }
@@ -182,6 +189,36 @@ void cmd_oidl(char *argbuf) {
 }
 
 
+/*
+ * List ALL OpenIDs in the database
+ */
+void cmd_oida(char *argbuf) {
+       struct cdbdata *cdboi;
+       long usernum;
+       struct ctdluser usbuf;
+
+       if (CtdlAccessCheck(ac_aide)) return;
+       cdb_rewind(CDB_OPENID);
+       cprintf("%d List of all OpenIDs in the database:\n", LISTING_FOLLOWS);
+
+       while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
+               if (cdboi->len > sizeof(long)) {
+                       memcpy(&usernum, cdboi->ptr, sizeof(long));
+                       if (getuserbynumber(&usbuf, usernum) != 0) {
+                               usbuf.fullname[0] = 0;
+                       } 
+                       cprintf("%s|%ld|%s\n",
+                               cdboi->ptr + sizeof(long),
+                               usernum,
+                               usbuf.fullname
+                       );
+               }
+               cdb_free(cdboi);
+       }
+       cprintf("000\n");
+}
+
+
 /*
  * Attempt to register (populate the vCard) the currently-logged-in user
  * using the data from Simple Registration Extension, if present.
@@ -251,20 +288,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) {
-                       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);
                }
        }
@@ -279,6 +308,11 @@ void populate_vcard_from_sreg(HashList *sreg_keys) {
 void cmd_oidc(char *argbuf) {
        struct ctdl_openid *oiddata = (struct ctdl_openid *) CC->openid_data;
 
+       if (!oiddata) {
+               cprintf("%d You have not verified an OpenID yet.\n", ERROR);
+               return;
+       }
+
        if (!oiddata->verified) {
                cprintf("%d You have not verified an OpenID yet.\n", ERROR);
                return;
@@ -309,6 +343,7 @@ void cmd_oidd(char *argbuf) {
        struct cdbdata *cdboi;
        char id_to_detach[1024];
        int this_is_mine = 0;
+       long usernum = 0L;
 
        if (CtdlAccessCheck(ac_logged_in)) return;
        extract_token(id_to_detach, argbuf, 0, '|', sizeof id_to_detach);
@@ -319,7 +354,8 @@ void cmd_oidd(char *argbuf) {
        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) {
+                       memcpy(&usernum, cdboi->ptr, sizeof(long));
+                       if (usernum == CC->user.usernum) {
                                this_is_mine = 1;
                        }
                }
@@ -545,7 +581,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);
        }
@@ -760,7 +801,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);
        }
@@ -788,9 +834,11 @@ void cmd_oidf(char *argbuf) {
                if (CC->logged_in) {
                        if (attach_openid(&CC->user, oiddata->claimed_id) == 0) {
                                cprintf("attach\n");
+                               CtdlLogPrintf(CTDL_DEBUG, "OpenID attach succeeded\n");
                        }
                        else {
                                cprintf("fail\n");
+                               CtdlLogPrintf(CTDL_DEBUG, "OpenID attach failed\n");
                        }
                }
 
@@ -807,6 +855,16 @@ void cmd_oidf(char *argbuf) {
                        if (login_via_openid(oiddata->claimed_id) == 0) {
                                cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
                                logged_in_response();
+                               CtdlLogPrintf(CTDL_DEBUG, "Logged in using previously claimed OpenID\n");
+                       }
+
+                       /*
+                        * 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");
+                               CtdlLogPrintf(CTDL_DEBUG, "Creating user failed due to local policy\n");
                        }
 
                        /*
@@ -815,6 +873,7 @@ void cmd_oidf(char *argbuf) {
                        else if (openid_create_user_via_sreg(oiddata->claimed_id, keys) == 0) {
                                cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
                                logged_in_response();
+                               CtdlLogPrintf(CTDL_DEBUG, "Successfully auto-created new user\n");
                        }
 
                        /*
@@ -831,6 +890,7 @@ void cmd_oidf(char *argbuf) {
                                else {
                                        cprintf("\n");
                                }
+                               CtdlLogPrintf(CTDL_DEBUG, "The desired Simple Registration name is already taken.\n");
                        }
                }
        }
@@ -874,14 +934,18 @@ void openid_cleanup_function(void) {
 
 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");
+                       CtdlRegisterProtoHook(cmd_oida, "OIDA", "List all OpenIDs in the database");
+               }
                CtdlRegisterSessionHook(openid_cleanup_function, EVT_LOGOUT);
                CtdlRegisterUserHook(openid_purge, EVT_PURGEUSER);
        }