From 803ef7605c568b2d115d84e1a71d53ecee9a1035 Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Debian Linux)" <rob@synchro.net>
Date: Mon, 4 Nov 2024 15:24:44 -0800
Subject: [PATCH] Address sysops sending emails to net-coordinators w/invalid
 reply-to address

Issue raised by Dumas Walker (CAPCITY2) on DOVE-Net: apparently some new
sysops run this script before their configured hostname (e.g.
mybbs.synchro.net) is valid or their mail server has been tested (can
successfully receive Internet e-mail).

This attempts to address this concern by:
1. displaying a warning that it's important that the address given is valid
2. attempt to validate that the host portion of the provided address is valid
   (has a DNS address record or MX record)

Uses dns.js for MX-record lookup - thanks Deuce!

This script does not validate that the email host can actually receive mail
or that the name portion of the mail address is valid: that would require an
outbound connection to the host's TCP port 25, which may be blocked by ISPs
(if remote) or if its a local server (e.g. the same machine as the BBS), that
wouldn't confirm that its reachable by Internet hosts. And not all mail
servers support the necessary SMTP commands to validate recipient addresses.
---
 exec/init-fidonet.js | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/exec/init-fidonet.js b/exec/init-fidonet.js
index 2b035bd5a7..e88e8eef2b 100644
--- a/exec/init-fidonet.js
+++ b/exec/init-fidonet.js
@@ -20,8 +20,10 @@
 
 "use strict";
 
-const REVISION = "2.0";
+const REVISION = "2.1";
 require('sbbsdefs.js', 'SUB_NAME');
+require('dns.js', 'DNS');
+const dns = new DNS(/* synchronous: */true);
 const temp_node = 9999;
 var netname;
 var netdns;
@@ -295,6 +297,27 @@ function get_binkp_sysop()
 	return result;
 }
 
+function valid_email_host(host)
+{
+	return resolve_ip(host) || dns.resolveMX(host).length > 0;
+}
+
+function check_email_addr(addr)
+{
+	if(!addr)
+		return false;
+	if(netaddr_type(addr) != NET_INTERNET) {
+		alert(format("'%s' does not appear to be a valid Internet e-mail address!", addr));
+		return false;
+	}
+	var host = addr.slice(addr.indexOf('@') + 1);
+	if(!valid_email_host(host)) {
+		alert(format("'%s' does not appear to be a valid/working Internet e-mail host!", host));
+		return false;
+	}
+	return true;
+}
+
 function update_sbbsecho_ini(hub, link, domain, echolist_fname, areamgr)
 {
 	function makepath(path)
@@ -569,6 +592,8 @@ if(system.stats.total_users) {
 			sysop = u.name;
 		if(netaddr_type(u.netmail) == NET_INTERNET)
 			sysop_email = u.netmail;
+		else
+			sysop_email = u.email;
 	}
 }
 sysop = get_binkp_sysop() || sysop;
@@ -578,7 +603,8 @@ if(!sysop_email) {
 	sysop_email = sysop.replace(' ', '.');
 	sysop_email += '@' + system.inet_addr;
 }
-while((netaddr_type(sysop_email) != NET_INTERNET || !confirm("Your Internet e-mail address is " + sysop_email)) && !aborted())
+alert("It is important that you provide a valid/working Internet email address:");
+while((!check_email_addr(sysop_email) || !confirm("Your Internet e-mail address is " + sysop_email)) && !aborted())
 	sysop_email = prompt("Your Internet e-mail address");
 
 /* Get/Confirm passwords */
@@ -592,7 +618,7 @@ while(((!link.TicFilePwd && (link.TicFilePwd !== "")) || !confirm("Your TIC File
 /***********************************************/
 /* SEND NODE NUMBER REQUEST NETMAIL (Internet) */
 /***********************************************/
-if(your.node === temp_node && network.email && network.email.indexOf('@') > 0
+if(your.node === temp_node && check_email_addr(network.email)
 	&& confirm("Send a node number application to " + network.email)) {
 	var result = send_app_netmail(network.email);
 	if(typeof result !== 'boolean') {
-- 
GitLab