From 8ad40480f2a78284f58b47d5052860281f27e94b Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Thu, 28 Feb 2002 01:15:44 +0000
Subject: [PATCH] Added setuid callback to startup structure.

---
 src/sbbs3/ftpsrvr.c  | 15 ++++++++++-----
 src/sbbs3/ftpsrvr.h  |  2 +-
 src/sbbs3/mailsrvr.c | 15 ++++++++++-----
 src/sbbs3/mailsrvr.h |  2 +-
 src/sbbs3/main.cpp   | 17 +++++++++++------
 src/sbbs3/sbbscon.c  | 19 +++++++++++++------
 src/sbbs3/services.c | 13 +++++++++----
 src/sbbs3/services.h |  2 +-
 src/sbbs3/startup.h  |  2 +-
 9 files changed, 57 insertions(+), 30 deletions(-)

diff --git a/src/sbbs3/ftpsrvr.c b/src/sbbs3/ftpsrvr.c
index eacf8f0a42..a4569df7b5 100644
--- a/src/sbbs3/ftpsrvr.c
+++ b/src/sbbs3/ftpsrvr.c
@@ -211,11 +211,13 @@ static void client_off(SOCKET sock)
 		startup->client_on(FALSE,sock,NULL,FALSE);
 }
 
-static void thread_up(void)
+static void thread_up(BOOL setuid)
 {
 	thread_count++;
 	if(startup!=NULL && startup->thread_up!=NULL)
 		startup->thread_up(TRUE);
+	if(setuid && startup!=NULL && startup->setuid!=NULL)
+		startup->setuid();
 }
 
 static void thread_down(void)
@@ -1360,7 +1362,7 @@ static void send_thread(void* arg)
 		*xfer.inprogress=FALSE;
 		return;
 	}
-	thread_up();
+	thread_up(TRUE /* setuid */);
 
 #if defined(_DEBUG) && defined(SOCKET_DEBUG_SENDTHREAD)
 			socket_debug[xfer.ctrl_sock]|=SOCKET_DEBUG_SENDTHREAD;
@@ -1571,7 +1573,7 @@ static void receive_thread(void* arg)
 		return;
 	}
 
-	thread_up();
+	thread_up(TRUE /* setuid */);
 
 	*xfer.aborted=FALSE;
 	if(xfer.filepos || startup->options&FTP_OPT_DEBUG_DATA)
@@ -2251,7 +2253,7 @@ static void ctrl_thread(void* arg)
 	JSObject*	js_ftp;
 #endif
 
-	thread_up();
+	thread_up(TRUE /* setuid */);
 
 	lastactive=time(NULL);
 
@@ -4323,7 +4325,7 @@ void DLLCALL ftp_server(void* arg)
 	recycle_server=TRUE;
 	do {
 
-		thread_up();
+		thread_up(FALSE /* setuid */);
 
 		status("Initializing");
 
@@ -4465,6 +4467,9 @@ void DLLCALL ftp_server(void* arg)
 			return;
 		}
 
+		if(startup->setuid!=NULL)
+			startup->setuid();
+
 		/* signal caller that we've started up successfully */
 		if(startup->started!=NULL)
     		startup->started();
diff --git a/src/sbbs3/ftpsrvr.h b/src/sbbs3/ftpsrvr.h
index 6637c3cf0c..2107279094 100644
--- a/src/sbbs3/ftpsrvr.h
+++ b/src/sbbs3/ftpsrvr.h
@@ -69,7 +69,7 @@ typedef struct {
     void	(*thread_up)(BOOL up);
 	void	(*socket_open)(BOOL open);
     void	(*client_on)(BOOL on, int sock, client_t*, BOOL update);
-    void	(*reserved_fptr4)(void);
+    BOOL	(*setuid)(void);
     void	(*reserved_fptr3)(void);
     void	(*reserved_fptr2)(void);
     void	(*reserved_fptr1)(void);
diff --git a/src/sbbs3/mailsrvr.c b/src/sbbs3/mailsrvr.c
index eac7066d26..0a0467cd1a 100644
--- a/src/sbbs3/mailsrvr.c
+++ b/src/sbbs3/mailsrvr.c
@@ -166,11 +166,13 @@ static void client_off(SOCKET sock)
 		startup->client_on(FALSE,sock,NULL,FALSE);
 }
 
-static void thread_up(void)
+static void thread_up(BOOL setuid)
 {
 	thread_count++;
 	if(startup!=NULL && startup->thread_up!=NULL)
 		startup->thread_up(TRUE);
+	if(setuid && startup!=NULL && startup->setuid!=NULL)
+		startup->setuid();
 }
 
 static void thread_down(void)
@@ -558,7 +560,7 @@ static void pop3_thread(void* arg)
 	mail_t*		mail;
 	pop3_t		pop3=*(pop3_t*)arg;
 
-	thread_up();
+	thread_up(TRUE /* setuid */);
 
 	free(arg);
 
@@ -1180,7 +1182,7 @@ static void smtp_thread(void* arg)
 
 	} cmd = SMTP_CMD_NONE;
 
-	thread_up();
+	thread_up(TRUE /* setuid */);
 
 	free(arg);
 
@@ -2345,7 +2347,7 @@ static void sendmail_thread(void* arg)
 
 	sendmail_running=TRUE;
 
-	thread_up();
+	thread_up(TRUE /* setuid */);
 
 	lprintf("0000 SendMail thread started");
 
@@ -2749,7 +2751,7 @@ void DLLCALL mail_server(void* arg)
 	if(startup->max_delivery_attempts==0)	startup->max_delivery_attempts=50;
 	if(startup->max_inactivity==0) 			startup->max_inactivity=120; /* seconds */
 
-	thread_up();
+	thread_up(FALSE /* setuid */);
 
 	status("Initializing");
 
@@ -2928,6 +2930,9 @@ void DLLCALL mail_server(void* arg)
 		}
 	}
 
+	if(startup->setuid!=NULL)
+		startup->setuid();
+
 	/* signal caller that we've started up successfully */
     if(startup->started!=NULL)
     	startup->started();
diff --git a/src/sbbs3/mailsrvr.h b/src/sbbs3/mailsrvr.h
index e4ed345edf..b32b969c27 100644
--- a/src/sbbs3/mailsrvr.h
+++ b/src/sbbs3/mailsrvr.h
@@ -71,7 +71,7 @@ typedef struct {
     void	(*thread_up)(BOOL up);
 	void	(*socket_open)(BOOL open);
     void	(*client_on)(BOOL on, int sock, client_t*, BOOL update);
-    void	(*reserved_fptr4)(void);
+    BOOL	(*setuid)(void);
     void	(*reserved_fptr3)(void);
     void	(*reserved_fptr2)(void);
     void	(*reserved_fptr1)(void);
diff --git a/src/sbbs3/main.cpp b/src/sbbs3/main.cpp
index 43071c49c1..fb0fe30039 100644
--- a/src/sbbs3/main.cpp
+++ b/src/sbbs3/main.cpp
@@ -115,11 +115,13 @@ static void client_off(SOCKET sock)
 		startup->client_on(FALSE,sock,NULL,FALSE);
 }
 
-static void thread_up()
+static void thread_up(BOOL setuid)
 {
 	thread_count++;
 	if(startup!=NULL && startup->thread_up!=NULL)
 		startup->thread_up(TRUE);
+	if(setuid && startup!=NULL && startup->setuid!=NULL)
+		startup->setuid();
 }
 
 static void thread_down()
@@ -700,7 +702,7 @@ void input_thread(void *arg)
 	sbbs_t*		sbbs = (sbbs_t*) arg;
 	struct timeval	tv;
 
-	thread_up();
+	thread_up(TRUE /* setuid */);
 
 #ifdef _DEBUG
 	lprintf("Node %d input thread started",sbbs->cfg.node_num);
@@ -842,7 +844,7 @@ void output_thread(void* arg)
 	fd_set		socket_set;
 	struct timeval tv;
 
-	thread_up();
+	thread_up(TRUE /* setuid */);
 
     if(sbbs->cfg.node_num)
     	sprintf(node,"Node %d",sbbs->cfg.node_num);
@@ -975,7 +977,7 @@ void event_thread(void* arg)
 	srand(clock());		/* Seed random number generator */
 	sbbs_random(10);	/* Throw away first number */
 
-	thread_up();
+	thread_up(TRUE /* setuid */);
 
 #ifdef JAVASCRIPT
 	if(!(startup->options&BBS_OPT_NO_JAVASCRIPT)) {
@@ -2596,7 +2598,7 @@ void node_thread(void* arg)
 	sbbs_t*			sbbs = (sbbs_t*) arg;
 
 	update_clients();
-	thread_up();
+	thread_up(TRUE /* setuid */);
 
 	srand(clock());		/* Seed random number generator */
 	sbbs_random(10);	/* Throw away first number */
@@ -3008,7 +3010,7 @@ void DLLCALL bbs_thread(void* arg)
 	if(startup->js_max_bytes==0)			startup->js_max_bytes=JAVASCRIPT_MAX_BYTES;
 #endif
 
-	thread_up();
+	thread_up(FALSE /* setuid */);
 
 	status("Initializing");
 
@@ -3280,6 +3282,9 @@ void DLLCALL bbs_thread(void* arg)
 		lprintf("RLogin server listening on port %d",startup->rlogin_port);
 	}
 
+	if(startup->setuid!=NULL)
+		startup->setuid();
+
 	/* signal caller that we've started up successfully */
     if(startup->started!=NULL)
     	startup->started();
diff --git a/src/sbbs3/sbbscon.c b/src/sbbs3/sbbscon.c
index 156a5bad68..03aea3a108 100644
--- a/src/sbbs3/sbbscon.c
+++ b/src/sbbs3/sbbscon.c
@@ -141,13 +141,8 @@ static void thread_up(BOOL up)
 	}
 
 	pthread_mutex_lock(&mutex);
-	if(up) {
+	if(up)
 	    thread_count++;
-#ifdef __unix__
-		do_setuid();
-#endif
-	}
-
     else if(thread_count>0)
     	thread_count--;
 	pthread_mutex_unlock(&mutex);
@@ -429,6 +424,9 @@ int main(int argc, char** argv)
     bbs_startup.thread_up=thread_up;
     bbs_startup.socket_open=socket_open;
     bbs_startup.client_on=client_on;
+#ifdef __unix__
+	bbs_startup.setuid=do_setuid;
+#endif
 /*	These callbacks haven't been created yet
     bbs_startup.status=bbs_status;
     bbs_startup.clients=bbs_clients;
@@ -445,6 +443,9 @@ int main(int argc, char** argv)
     ftp_startup.socket_open=socket_open;
     ftp_startup.client_on=client_on;
 	ftp_startup.options=FTP_OPT_INDEX_FILE|FTP_OPT_ALLOW_QWK;
+#ifdef __unix__
+	ftp_startup.setuid=do_setuid;
+#endif
     strcpy(ftp_startup.index_file_name,"00index");
     strcpy(ftp_startup.ctrl_dir,ctrl_dir);
 
@@ -458,6 +459,9 @@ int main(int argc, char** argv)
     mail_startup.socket_open=socket_open;
     mail_startup.client_on=client_on;
 	mail_startup.options|=MAIL_OPT_ALLOW_POP3;
+#ifdef __unix__
+	mail_startup.setuid=do_setuid;
+#endif
 	/* Spam filtering */
 	mail_startup.options|=MAIL_OPT_USE_RBL;	/* Realtime Blackhole List */
 	mail_startup.options|=MAIL_OPT_USE_RSS;	/* Relay Spam Stopper */
@@ -498,6 +502,9 @@ int main(int argc, char** argv)
 	services_startup.thread_up=thread_up;
     services_startup.socket_open=socket_open;
     services_startup.client_on=client_on;
+#ifdef __unix__
+	services_startup.setuid=do_setuid;
+#endif
     strcpy(services_startup.ctrl_dir,ctrl_dir);
 
 	/* Process arguments */
diff --git a/src/sbbs3/services.c b/src/sbbs3/services.c
index 340a4e80be..3730949d8c 100644
--- a/src/sbbs3/services.c
+++ b/src/sbbs3/services.c
@@ -167,10 +167,12 @@ static void client_off(SOCKET sock)
 		startup->client_on(FALSE,sock,NULL,FALSE);
 }
 
-static void thread_up(void)
+static void thread_up(BOOL setuid)
 {
 	if(startup!=NULL && startup->thread_up!=NULL)
 		startup->thread_up(TRUE);
+	if(setuid && startup!=NULL && startup->setuid!=NULL)
+		startup->setuid();
 }
 
 static void thread_down(void)
@@ -592,7 +594,7 @@ static void js_service_thread(void* arg)
 
 	lprintf("%04d %s JavaScript service thread started", socket, service->protocol);
 
-	thread_up();
+	thread_up(TRUE /* setuid */);
 
 	/* Host name lookup and filtering */
 	if(service->options&BBS_OPT_NO_HOST_LOOKUP 
@@ -762,7 +764,7 @@ static void native_service_thread(void* arg)
 
 	lprintf("%04d %s service thread started", socket, service->protocol);
 
-	thread_up();
+	thread_up(TRUE /* setuid */);
 
 	/* Host name lookup and filtering */
 	if(service->options&BBS_OPT_NO_HOST_LOOKUP 
@@ -1004,7 +1006,7 @@ void DLLCALL services_thread(void* arg)
 	/* Setup intelligent defaults */
 	if(startup->js_max_bytes==0)			startup->js_max_bytes=JAVASCRIPT_MAX_BYTES;
 
-	thread_up();
+	thread_up(FALSE /* setuid */);
 
 	status("Initializing");
 
@@ -1110,6 +1112,9 @@ void DLLCALL services_thread(void* arg)
 		service[i].socket=socket;
 	}
 
+	if(startup->setuid!=NULL)
+		startup->setuid();
+
 	/* signal caller that we've started up successfully */
     if(startup->started!=NULL)
     	startup->started();
diff --git a/src/sbbs3/services.h b/src/sbbs3/services.h
index e8457426ee..6ab1ea915e 100644
--- a/src/sbbs3/services.h
+++ b/src/sbbs3/services.h
@@ -62,7 +62,7 @@ typedef struct {
     void	(*thread_up)(BOOL up);
 	void	(*socket_open)(BOOL open);
     void	(*client_on)(BOOL on, int sock, client_t*, BOOL update);
-    void	(*reserved_fptr4)(void);
+    BOOL	(*setuid)(void);
     void	(*reserved_fptr3)(void);
     void	(*reserved_fptr2)(void);
     void	(*reserved_fptr1)(void);
diff --git a/src/sbbs3/startup.h b/src/sbbs3/startup.h
index 494ad11b6e..21d8c86b2a 100644
--- a/src/sbbs3/startup.h
+++ b/src/sbbs3/startup.h
@@ -75,7 +75,7 @@ typedef struct {
     void	(*thread_up)(BOOL up);
 	void	(*socket_open)(BOOL open);
     void	(*client_on)(BOOL on, int sock, client_t*, BOOL update);
-    void	(*reserved_fptr4)(void);
+    BOOL	(*setuid)(void);	// Set Unix uid for thread
     void	(*reserved_fptr3)(void);
     void	(*reserved_fptr2)(void);
     void	(*reserved_fptr1)(void);
-- 
GitLab