From dd38606e246a2a4e2179d407208200468f0162e6 Mon Sep 17 00:00:00 2001
From: "Rob Swindell (in GitKraken)" <rob@synchro.net>
Date: Sun, 19 Feb 2023 15:25:51 -0800
Subject: [PATCH] find_login_id() will now return 0 if login ID is an invalid
 user number

If login by number is supported and a client attempts login with an invalid usernumber, don't log an error, e.g.
mail 3264 SMTPS [46.148.x.x] !ERROR -2 getting data on user (6123)

But rather treat it as an invalid login attempt.

Also change lastuser() and total_users() to return int instead of uint. 2 billion users should be plenty.
---
 src/sbbs3/userdat.c | 16 ++++++++++------
 src/sbbs3/userdat.h |  4 ++--
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/sbbs3/userdat.c b/src/sbbs3/userdat.c
index d032000c44..258727ea9c 100644
--- a/src/sbbs3/userdat.c
+++ b/src/sbbs3/userdat.c
@@ -150,8 +150,12 @@ uint find_login_id(scfg_t* cfg, const char* user_id)
 {
 	int usernum;
 
-	if((cfg->sys_login & LOGIN_USERNUM) && IS_DIGIT(user_id[0]))
-		return atoi(user_id);
+	if((cfg->sys_login & LOGIN_USERNUM) && IS_DIGIT(user_id[0])) {
+		usernum = atoi(user_id);
+		if(usernum > lastuser(cfg))
+			usernum = 0;
+		return usernum;
+	}
 
 	usernum = matchuser(cfg, user_id, /* sysop_alias: */FALSE);
 	if(usernum < 1 && check_realname(cfg, user_id) && (cfg->sys_login & LOGIN_REALNAME))
@@ -162,9 +166,9 @@ uint find_login_id(scfg_t* cfg, const char* user_id)
 }
 
 /****************************************************************************/
-uint total_users(scfg_t* cfg)
+int total_users(scfg_t* cfg)
 {
-    uint	total_users=0;
+    int		total_users=0;
 	int		file;
 	bool	success = true;
 
@@ -192,7 +196,7 @@ uint total_users(scfg_t* cfg)
 /****************************************************************************/
 /* Returns the number of the last user in user.tab (deleted ones too)		*/
 /****************************************************************************/
-uint lastuser(scfg_t* cfg)
+int lastuser(scfg_t* cfg)
 {
 	char path[MAX_PATH + 1];
 	off_t length;
@@ -201,7 +205,7 @@ uint lastuser(scfg_t* cfg)
 		return(0);
 
 	if((length = flength(userdat_filename(cfg, path, sizeof(path)))) > 0)
-		return (uint)(length / USER_RECORD_LINE_LEN);
+		return (int)(length / USER_RECORD_LINE_LEN);
 	return 0;
 }
 
diff --git a/src/sbbs3/userdat.h b/src/sbbs3/userdat.h
index d41980ad78..64c17a3c93 100644
--- a/src/sbbs3/userdat.h
+++ b/src/sbbs3/userdat.h
@@ -55,8 +55,8 @@ DLLEXPORT uint	matchuser(scfg_t*, const char *str, BOOL sysop_alias); // Checks
 DLLEXPORT BOOL	matchusername(scfg_t*, const char* name, const char* compare);
 DLLEXPORT char* alias(scfg_t*, const char* name, char* buf);
 DLLEXPORT int	putusername(scfg_t*, int number, const char* name);
-DLLEXPORT uint	total_users(scfg_t*);
-DLLEXPORT uint	lastuser(scfg_t*);
+DLLEXPORT int	total_users(scfg_t*);
+DLLEXPORT int	lastuser(scfg_t*);
 DLLEXPORT BOOL	del_lastuser(scfg_t*);
 DLLEXPORT int	getage(scfg_t*, const char* birthdate);
 DLLEXPORT int	getbirthmonth(scfg_t*, const char* birthdate);
-- 
GitLab