From 67c3a42e039629dfb7077f72e8d902012bf73bd9 Mon Sep 17 00:00:00 2001
From: Rob Swindell <rob@synchro.net>
Date: Tue, 10 Nov 2020 21:58:34 -0800
Subject: [PATCH] Insure that message base is closed before opening another.

This should address the issue raised by Michael J. Ryan in issue #4.
The command in question ("GROUP") would attempt to open the message
base associated with the specified newsgroup name without closing
the currently open/selected message base (if there was one). This would
result in multiple simultaneously open message bases and could result
in an exhaustion of open file descriptors leading to a failure to open
any additional files (includign message bases) and thus a subsequent
"GROUP" command would fail with the wrong "!no such group" error.
---
 exec/nntpservice.js | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/exec/nntpservice.js b/exec/nntpservice.js
index e682d2f4f0..e8a32a1bf6 100644
--- a/exec/nntpservice.js
+++ b/exec/nntpservice.js
@@ -317,6 +317,8 @@ while(client.socket.is_connected && !quit) {
 				pattern=cmd[2];
  				writeln("215 list of newsgroups follows");
 				if(include_mail && user.security.level == 99 && wildmatch("mail", pattern)) {
+					if(msgbase && msgbase.is_open)
+						msgbase.close();
 					msgbase=new MsgBase("mail");
 					if(msgbase.open()==true) {
 						writeln(format("mail %u %u n", msgbase.last_msg, msgbase.first_msg));
@@ -327,6 +329,8 @@ while(client.socket.is_connected && !quit) {
 					for(s in msg_area.grp_list[g].sub_list) {
 						if(!wildmatch(msg_area.grp_list[g].sub_list[s].newsgroup, pattern))
 							continue;
+						if(msgbase && msgbase.is_open)
+							msgbase.close();
 						msgbase=new MsgBase(msg_area.grp_list[g].sub_list[s].code);
 						if(msgbase.open!=undefined && msgbase.open()==false)
 							continue;
@@ -443,6 +447,8 @@ while(client.socket.is_connected && !quit) {
 			writeln("231 list of new newsgroups since " + compare.toISOString() + " follows");
 			for(g in msg_area.grp_list) {
 				for(s in msg_area.grp_list[g].sub_list) {
+					if(msgbase && msgbase.is_open)
+						msgbase.close();
 					msgbase=new MsgBase(msg_area.grp_list[g].sub_list[s].code);
 					var ini_file = new File(msgbase.file + ".ini");
 					if(ini_file.open("r")) {
@@ -481,6 +487,8 @@ while(client.socket.is_connected && !quit) {
 				found=true;
 			}
 			else if(include_mail && user.security.level==99 && cmd[1].toLowerCase()=="mail") {
+				if(msgbase && msgbase.is_open)
+					msgbase.close();
 				msgbase=new MsgBase("mail");
 				if(msgbase.open()==true) {
 					selected = { newsgroup: "mail" };
@@ -488,6 +496,8 @@ while(client.socket.is_connected && !quit) {
 				}
 			}
 			if(!found) {
+				if(msgbase && msgbase.is_open)
+					msgbase.close();
 				for(g in msg_area.grp_list)
 					for(s in msg_area.grp_list[g].sub_list)
 						if(msg_area.grp_list[g].sub_list[s].newsgroup.toLowerCase()==cmd[1].toLowerCase()) {
-- 
GitLab