+
+
+/**************************************************************************/
+/* */
+/* Functions in this section handle Citadel internal OpenID mapping stuff */
+/* */
+/**************************************************************************/
+
+
+/*
+ * The structure of an openid record *key* is:
+ *
+ * |--------------claimed_id-------------|
+ * (actual 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;
+ char buf[2048];
+
+ 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);
+
+ 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);
+}
+
+
+
+/*
+ * When a user is being deleted, we have to delete any OpenID associations
+ */
+void openid_purge(struct ctdluser *usbuf) {
+ struct cdbdata *cdboi;
+ HashList *keys = NULL;
+ HashPos *HashPos;
+ char *deleteme = NULL;
+ long len;
+ void *Value;
+ const char *Key;
+ long usernum = 0L;
+
+ 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)) {
+ 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);
+ }
+ }
+ cdb_free(cdboi);
+ }
+
+ /* Go through the hash list, deleting keys we stored in it */
+
+ HashPos = GetNewHashPos(keys, 0);
+ 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;
+ long usernum = 0L;
+
+ 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)) {
+ memcpy(&usernum, cdboi->ptr, sizeof(long));
+ if (usernum == CC->user.usernum) {
+ cprintf("%s\n", cdboi->ptr + sizeof(long));
+ }
+ }
+ cdb_free(cdboi);
+ }
+ cprintf("000\n");
+}
+
+
+/*
+ * 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.
+ */
+void populate_vcard_from_sreg(HashList *sreg_keys) {
+
+ struct vCard *v;
+ int pop = 0; /* number of fields populated */
+ char *data = NULL;
+ char *postcode = NULL;
+ char *country = NULL;
+
+ if (!sreg_keys) return;
+ v = vcard_new();
+ if (!v) return;
+
+ if (GetHash(sreg_keys, "identity", 8, (void *) &data)) {
+ vcard_add_prop(v, "url;type=openid", data);
+ ++pop;
+ }
+
+ if (GetHash(sreg_keys, "sreg.email", 10, (void *) &data)) {
+ vcard_add_prop(v, "email;internet", data);
+ ++pop;
+ }
+
+ if (GetHash(sreg_keys, "sreg.nickname", 13, (void *) &data)) {
+ vcard_add_prop(v, "nickname", data);
+ ++pop;
+ }
+
+ if (GetHash(sreg_keys, "sreg.fullname", 13, (void *) &data)) {
+ char n[256];
+ vcard_add_prop(v, "fn", data);
+ vcard_fn_to_n(n, data, sizeof n);
+ vcard_add_prop(v, "n", n);
+ ++pop;
+ }
+
+ if (!GetHash(sreg_keys, "sreg.postcode", 13, (void *) &postcode)) {
+ postcode = NULL;
+ }
+
+ if (!GetHash(sreg_keys, "sreg.country", 12, (void *) &country)) {
+ country = NULL;
+ }
+
+ if (postcode || country) {
+ char adr[256];
+ snprintf(adr, sizeof adr, ";;;;;%s;%s",
+ (postcode ? postcode : ""),
+ (country ? country : "")
+ );
+ vcard_add_prop(v, "adr", adr);
+ ++pop;
+ }
+
+ if (GetHash(sreg_keys, "sreg.dob", 8, (void *) &data)) {
+ vcard_add_prop(v, "bday", data);
+ ++pop;
+ }
+
+ if (GetHash(sreg_keys, "sreg.gender", 11, (void *) &data)) {
+ vcard_add_prop(v, "x-funambol-gender", data);
+ ++pop;
+ }
+
+ /* Only save the vCard if there is some useful data in it */
+ if (pop > 0) {
+ char *ser;
+ ser = vcard_serialize(v);
+ if (ser) {
+ CtdlWriteObject(USERCONFIGROOM, "text/x-vcard",
+ ser, strlen(ser)+1, &CC->user, 0, 0, 0
+ );
+ free(ser);
+ }
+ }
+ vcard_free(v);
+}
+
+
+/*
+ * 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) {
+ 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;
+ }
+
+ /* 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;
+ long usernum = 0L;
+
+ 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)) {
+ memcpy(&usernum, cdboi->ptr, sizeof(long));
+ if (usernum == 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_sreg(char *claimed_id, HashList *sreg_keys)
+{
+ char *desired_name = NULL;
+ char new_password[32];
+
+ if (config.c_auth_mode != AUTHMODE_NATIVE) return(1);
+ if (config.c_disable_newu) return(2);
+ if (CC->logged_in) return(3);
+ if (!GetHash(sreg_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);
+
+ snprintf(new_password, sizeof new_password, "%08lx%08lx", random(), random());
+ CtdlSetPassword(new_password);
+ attach_openid(&CC->user, claimed_id);
+ populate_vcard_from_sreg(sreg_keys);
+ return(0);
+}
+
+
+/*
+ * 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)) {
+ /* Now become the user we just created */
+ safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
+ do_login();
+ return(0);
+ }
+ else {
+ memset(&CC->user, 0, sizeof(struct ctdluser));
+ return(-1);
+ }
+}
+
+
+
+
+/**************************************************************************/
+/* */
+/* Functions in this section handle OpenID protocol */
+/* */
+/**************************************************************************/
+