jcs
/subtext
/amendments
/119
user_settings: Support changing usernames
jcs made amendment 119 over 2 years ago
--- console.c Wed Jun 1 13:08:39 2022
+++ console.c Fri Jun 3 23:38:17 2022
@@ -61,6 +61,7 @@ struct console *
console_init(void)
{
char title[64];
+ char *sysop_username = NULL;
struct console *console;
struct focusable *focusable;
Rect bounds;
@@ -108,8 +109,12 @@ console_init(void)
console->session->vt100 = 1;
console->session->cp437 = 1;
console->session->tspeed = 19200;
- strlcpy(console->session->autologin_username, "sysop",
- sizeof(console->session->autologin_username));
+
+ if ((sysop_username = user_first_sysop_username()) != NULL) {
+ strlcpy(console->session->autologin_username, sysop_username,
+ sizeof(console->session->autologin_username));
+ free(sysop_username);
+ }
return console;
}
--- signup.c Wed Jun 1 13:10:21 2022
+++ signup.c Fri Jun 3 22:56:12 2022
@@ -53,7 +53,7 @@ signup(struct session *s)
if (username == NULL || username[0] == '\0' || s->ending)
goto signup_done;
- if (user_valid_username(username, &error) != 1) {
+ if (user_valid_username(NULL, username, &error) != 1) {
session_printf(s, "{{B}}Error:{{/B}} %s\r\n", error);
free(error);
continue;
--- user.c Wed Jun 1 13:10:51 2022
+++ user.c Fri Jun 3 23:16:35 2022
@@ -214,10 +214,11 @@ user_set_password(struct user *user, const char *passw
}
short
-user_valid_username(char *username, char **error)
+user_valid_username(struct user *user, char *username, char **error)
{
char *lower = NULL;
- struct user *user;
+ struct user *ouser;
+ unsigned long ouser_id;
size_t len, n;
char c;
short ret = 0;
@@ -256,17 +257,23 @@ user_valid_username(char *username, char **error)
goto done;
}
- for (n = 0; n < nitems(BANNED_USERNAMES); n++) {
- if (strcmp(lower, BANNED_USERNAMES[n]) == 0) {
- *error = xstrdup("username is not permitted");
- goto done;
+ if (!(user != NULL && user->is_sysop)) {
+ for (n = 0; n < nitems(BANNED_USERNAMES); n++) {
+ if (strcmp(lower, BANNED_USERNAMES[n]) == 0) {
+ *error = xstrdup("username is not permitted");
+ goto done;
+ }
}
}
- if ((user = user_find_by_username(username))) {
- free(user);
- *error = xstrdup("username is already in use");
- goto done;
+ if ((ouser = user_find_by_username(username))) {
+ ouser_id = ouser->id;
+ free(ouser);
+
+ if (user == NULL || ouser_id != user->id) {
+ *error = xstrdup("username is already in use");
+ goto done;
+ }
}
ret = 1;
@@ -274,5 +281,28 @@ user_valid_username(char *username, char **error)
done:
if (lower != NULL)
free(lower);
+ return ret;
+}
+
+char *
+user_first_sysop_username(void)
+{
+ struct user user;
+ struct bile_object *o;
+ char *ret = NULL;
+ size_t nuser, len;
+
+ nuser = 0;
+ while ((o = bile_get_nth_of_type(db->bile, nuser, DB_USER_RTYPE))) {
+ len = bile_read(db->bile, DB_USER_RTYPE, o->id, (char *)&user,
+ sizeof(user));
+ free(o);
+ if (user.is_sysop) {
+ ret = xstrdup(user.username);
+ break;
+ }
+ nuser++;
+ }
+
return ret;
}
--- user.h Mon May 23 13:57:39 2022
+++ user.h Fri Jun 3 23:16:49 2022
@@ -46,6 +46,7 @@ struct user * user_find_by_username(const char *userna
struct username_cache * user_find_username(unsigned long id);
short user_authenticate(struct user *user, const char *password);
void user_set_password(struct user *user, const char *password);
-short user_valid_username(char *username, char **error);
+short user_valid_username(struct user *user, char *username, char **error);
+char * user_first_sysop_username(void);
#endif
--- user_settings.c Wed Jun 1 13:11:01 2022
+++ user_settings.c Fri Jun 3 23:11:26 2022
@@ -28,6 +28,7 @@
void user_settings_renegotiate(struct session *s);
void user_settings_password(struct session *s);
+void user_settings_username(struct session *s);
void
user_settings_renegotiate(struct session *s)
@@ -129,7 +130,7 @@ user_settings_password(struct session *s)
return;
}
- for (;;) {
+ while (!s->ending) {
session_output_template(s, "{{B}}New Password:{{/B}} ");
session_flush(s);
password = session_field_input(s, 64, 64, NULL, false, '*');
@@ -143,6 +144,7 @@ user_settings_password(struct session *s)
session_output_template(s, "{{B}}Error:{{/B}} "
"Password cannot be blank\r\n");
free(password);
+ password = NULL;
continue;
}
@@ -159,12 +161,15 @@ user_settings_password(struct session *s)
session_output_template(s, "{{B}}Error:{{/B}} "
"Passwords do not match\r\n");
free(password);
+ password = NULL;
free(password_confirm);
+ password_confirm = NULL;
continue;
}
user_set_password(s->user, password);
user_save(s->user);
+ session_log(s, "User changed password");
session_output_template(s, "{{B}}Your password has been "
"changed{{/B}}\r\n");
break;
@@ -177,11 +182,66 @@ user_settings_password(struct session *s)
}
void
+user_settings_username(struct session *s)
+{
+ char *username = NULL, *error = NULL;
+
+ if (!s->user) {
+ session_output_template(s, "{{B}}Error{{/B}}: Guest accounts "
+ "cannot change username\r\n");
+ return;
+ }
+
+ while (!s->ending) {
+ session_output_template(s, "{{B}}Username:{{/B}} ");
+ session_flush(s);
+ username = session_field_input(s, DB_USERNAME_LENGTH,
+ DB_USERNAME_LENGTH, s->user->username, false, 0);
+ session_output(s, "\r\n", 2);
+ session_flush(s);
+
+ if (username == NULL || s->ending)
+ break;
+
+ if (username[0] == '\0') {
+ session_output_template(s, "{{B}}Error:{{/B}} "
+ "Username cannot be blank\r\n");
+ free(username);
+ username = NULL;
+ continue;
+ }
+
+ if (strcmp(s->user->username, username) == 0)
+ break;
+
+ if (user_valid_username(s->user, username, &error) != 1) {
+ session_printf(s, "{{B}}Error:{{/B}} %s\r\n", error);
+ free(error);
+ free(username);
+ username = NULL;
+ continue;
+ }
+
+ session_log(s, "User changed username from %s to %s",
+ s->user->username, username);
+ strlcpy(s->user->username, username, sizeof(s->user->username));
+ user_save(s->user);
+ session_printf(s, "{{B}}Your username has been "
+ "changed to %s{{/B}}\r\n", s->user->username);
+ break;
+ }
+
+ if (username != NULL)
+ free(username);
+}
+
+void
user_settings_menu(struct session *s)
{
static struct session_menu_option opts[] = {
{ 'r', "Rr", "Renegotiate terminal" },
{ 'p', "Pp", "Change password" },
+ { 'u', "Uu", "Change username" },
{ 'q', "QqXx", "Return to main menu" },
{ '?', "?", "List menu options" },
};
@@ -202,6 +262,9 @@ user_settings_menu(struct session *s)
break;
case 'p':
user_settings_password(s);
+ break;
+ case 'u':
+ user_settings_username(s);
break;
case '?':
show_help = true;