git: 078b0010cfaf - main - net-mgmt/mac-telnet: Several fixes

From: Joe Marcus Clarke <marcus_at_FreeBSD.org>
Date: Thu, 15 Aug 2024 17:23:38 UTC
The branch main has been updated by marcus:

URL: https://cgit.FreeBSD.org/ports/commit/?id=078b0010cfafc42a073fe1898f8bf0baa41879a4

commit 078b0010cfafc42a073fe1898f8bf0baa41879a4
Author:     Joe Marcus Clarke <marcus@FreeBSD.org>
AuthorDate: 2024-08-15 17:21:49 +0000
Commit:     Joe Marcus Clarke <marcus@FreeBSD.org>
CommitDate: 2024-08-15 17:21:49 +0000

    net-mgmt/mac-telnet: Several fixes
    
    * Fix building with OpenSSL 1.1
    * Replace use of undirected broadcast with directed broadcast when
      sockets are bound to an interface IP
    * Do not override the mactelnetd.users file
---
 net-mgmt/mac-telnet/Makefile                     |  8 ++-
 net-mgmt/mac-telnet/files/patch-configure.ac     |  4 +-
 net-mgmt/mac-telnet/files/patch-src_interfaces.c | 40 +++++++++++
 net-mgmt/mac-telnet/files/patch-src_interfaces.h | 17 +++++
 net-mgmt/mac-telnet/files/patch-src_mactelnet.c  | 38 ++++++++++
 net-mgmt/mac-telnet/files/patch-src_mactelnetd.c | 33 +++++++++
 net-mgmt/mac-telnet/files/patch-src_mndp.c       |  4 +-
 net-mgmt/mac-telnet/files/patch-src_users.c      | 90 ++++++++++++++++++++++++
 net-mgmt/mac-telnet/pkg-plist                    |  2 +-
 9 files changed, 229 insertions(+), 7 deletions(-)

diff --git a/net-mgmt/mac-telnet/Makefile b/net-mgmt/mac-telnet/Makefile
index 51051a7ac0e7..df259fb074e4 100644
--- a/net-mgmt/mac-telnet/Makefile
+++ b/net-mgmt/mac-telnet/Makefile
@@ -1,5 +1,6 @@
 PORTNAME=	mac-telnet
 PORTVERSION=	0.6.1
+PORTREVISION=	1
 PORTEPOCH=	1
 CATEGORIES=	net-mgmt net
 
@@ -20,10 +21,13 @@ GNU_CONFIGURE=	yes
 GNU_CONFIGURE_MANPREFIX=	${PREFIX}/share
 LDFLAGS+=	-L${LOCALBASE}/lib
 CPPFLAGS+=	-I${LOCALBASE}/include
+CONFIGURE_ARGS+=--without-config
 CONFIGURE_ENV+=	CRYPTO_CFLAGS="-I${OPENSSLINC}" CRYPTO_LIBS="-L${OPENSSLLIB} -lssl -lcrypto"
 
 post-install:
-	@${CHMOD} 0600 ${STAGEDIR}${PREFIX}/etc/mactelnetd.users
-	@-${CHOWN} root ${STAGEDIR}${PREFIX}/etc/mactelnetd.users
+	${INSTALL_DATA} ${BUILD_WRKSRC}/config/mactelnetd.users \
+		${STAGEDIR}${PREFIX}/etc/mactelnetd.users.sample
+	@${CHMOD} 0600 ${STAGEDIR}${PREFIX}/etc/mactelnetd.users.sample
+	@-${CHOWN} root ${STAGEDIR}${PREFIX}/etc/mactelnetd.users.sample
 
 .include <bsd.port.mk>
diff --git a/net-mgmt/mac-telnet/files/patch-configure.ac b/net-mgmt/mac-telnet/files/patch-configure.ac
index 5a37adf61b4f..25d07c19154f 100644
--- a/net-mgmt/mac-telnet/files/patch-configure.ac
+++ b/net-mgmt/mac-telnet/files/patch-configure.ac
@@ -1,6 +1,6 @@
---- configure.ac.orig	2024-06-19 14:50:22 UTC
+--- configure.ac.orig	2024-08-12 21:08:19 UTC
 +++ configure.ac
-@@ -79,6 +79,10 @@ case "$host_os" in
+@@ -93,6 +93,10 @@ case "$host_os" in
          AC_MSG_ERROR([pthreads library not found])
      fi
      ;;
diff --git a/net-mgmt/mac-telnet/files/patch-src_interfaces.c b/net-mgmt/mac-telnet/files/patch-src_interfaces.c
new file mode 100644
index 000000000000..e3857f1ce4be
--- /dev/null
+++ b/net-mgmt/mac-telnet/files/patch-src_interfaces.c
@@ -0,0 +1,40 @@
+--- src/interfaces.c.orig	2024-08-15 15:58:48 UTC
++++ src/interfaces.c
+@@ -150,6 +150,9 @@ int net_get_interfaces(struct net_interface **interfac
+ 	static const struct ifaddrs *ifaddrsp;
+ 	const struct sockaddr_in *dl_addr;
+ 	int found = 0;
++#if !defined(__FreeBSD__)
++	long allones_bcast = htonl(INADDR_BROADCAST);
++#endif
+ 
+ 	if (getifaddrs(&int_addrs) < 0) {
+ 		perror("getifaddrs");
+@@ -173,8 +176,14 @@ int net_get_interfaces(struct net_interface **interfac
+ 
+ 				if (ifaddrsp->ifa_addr->sa_family == AF_INET) {
+ 					memcpy(interface->ipv4_addr, &dl_addr->sin_addr, IPV4_ALEN);
++#if defined(__FreeBSD__)
++					memcpy(interface->bcast_addr, &((const struct sockaddr_in *)ifaddrsp->ifa_broadaddr)->sin_addr, IPV4_ALEN);
++#else
++					memcpy(interface->bcast_addr, &allones_bcast, IPV4_ALEN);
++#endif
+ 				} else {
+ 					memset(interface->ipv4_addr, 0, IPV4_ALEN);
++					memset(interface->bcast_addr, 0, IPV4_ALEN);
+ 				}
+ 			}
+ #ifdef __linux__
+@@ -208,9 +217,12 @@ int net_get_interfaces(struct net_interface **interfac
+ 		DL_FOREACH(*interfaces, interface) {
+ 			struct in_addr *addr =
+ 			  (struct in_addr *)interface->ipv4_addr;
++			struct in_addr *bcast =
++			  (struct in_addr *)interface->bcast_addr;
+ 
+ 			printf("Interface %s:\n", interface->name);
+ 			printf("\tIP: %s\n", inet_ntoa(*addr));
++			printf("\tBCAST: %s\n", inet_ntoa(*bcast));
+ 			printf("\tMAC: %s\n",
+ 			  ether_ntoa((struct ether_addr *)interface->mac_addr));
+ #ifdef __linux__
diff --git a/net-mgmt/mac-telnet/files/patch-src_interfaces.h b/net-mgmt/mac-telnet/files/patch-src_interfaces.h
new file mode 100644
index 000000000000..6f0fc858ea62
--- /dev/null
+++ b/net-mgmt/mac-telnet/files/patch-src_interfaces.h
@@ -0,0 +1,17 @@
+--- src/interfaces.h.orig	2024-08-15 16:06:02 UTC
++++ src/interfaces.h
+@@ -25,6 +25,7 @@ struct net_interface {
+ 	char name[256];
+ 	unsigned char ipv4_addr[IPV4_ALEN];
+ 	unsigned char mac_addr[ETH_ALEN];
++	unsigned char bcast_addr[IPV4_ALEN];
+ 
+ 	/* used by mactelnetd */
+ 	int socketfd;
+@@ -57,4 +58,4 @@ extern int net_send_udp(const int socket, struct net_i
+ 						const unsigned char *destmac, const struct in_addr *sourceip, const int sourceport,
+ 						const struct in_addr *destip, const int destport, const unsigned char *data, const int datalen);
+ extern unsigned short in_cksum(unsigned short *addr, int len);
+-#endif
+\ No newline at end of file
++#endif
diff --git a/net-mgmt/mac-telnet/files/patch-src_mactelnet.c b/net-mgmt/mac-telnet/files/patch-src_mactelnet.c
new file mode 100644
index 000000000000..e61d3dd6ecc4
--- /dev/null
+++ b/net-mgmt/mac-telnet/files/patch-src_mactelnet.c
@@ -0,0 +1,38 @@
+--- src/mactelnet.c.orig	2024-08-15 16:46:24 UTC
++++ src/mactelnet.c
+@@ -126,6 +126,8 @@ unsigned char mt_direction_fromserver = 0;
+ 
+ static unsigned int send_socket;
+ 
++static unsigned char *bcast_addr;
++
+ static int handle_packet(unsigned char *data, int data_len);
+ 
+ static void print_version() {
+@@ -168,7 +170,7 @@ static int send_udp(struct mt_packet *packet, int retr
+ 		struct sockaddr_in socket_address;
+ 		socket_address.sin_family = AF_INET;
+ 		socket_address.sin_port = htons(MT_MACTELNET_PORT);
+-		socket_address.sin_addr.s_addr = htonl(INADDR_BROADCAST);
++		memcpy(&(socket_address.sin_addr), bcast_addr, IPV4_ALEN);
+ 
+ 		sent_bytes = sendto(send_socket, packet->data, packet->size, 0, (struct sockaddr *)&socket_address,
+ 							sizeof(socket_address));
+@@ -470,6 +472,9 @@ static int find_interface() {
+ 			continue;
+ 		}
+ 
++#if defined(__FreeBSD__)
++		setsockopt(testsocket, IPPROTO_IP, IP_ONESBCAST, &optval, sizeof(optval));
++#endif
+ 		setsockopt(testsocket, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
+ 		setsockopt(testsocket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
+ 
+@@ -488,6 +493,7 @@ static int find_interface() {
+ 		send_socket = testsocket;
+ 		memcpy(srcmac, interface->mac_addr, ETH_ALEN);
+ 		active_interface = interface;
++		bcast_addr = interface->bcast_addr;
+ 
+ 		/* Send a SESSIONSTART message with the current device */
+ 		init_packet(&data, MT_PTYPE_SESSIONSTART, srcmac, dstmac, sessionkey, 0);
diff --git a/net-mgmt/mac-telnet/files/patch-src_mactelnetd.c b/net-mgmt/mac-telnet/files/patch-src_mactelnetd.c
new file mode 100644
index 000000000000..24c25cd212ee
--- /dev/null
+++ b/net-mgmt/mac-telnet/files/patch-src_mactelnetd.c
@@ -0,0 +1,33 @@
+--- src/mactelnetd.c.orig	2024-08-15 17:08:22 UTC
++++ src/mactelnetd.c
+@@ -254,6 +254,12 @@ static void setup_sockets() {
+ 				perror("SO_BROADCAST");
+ 				continue;
+ 			}
++#if defined(__FreeBSD__)
++			if (setsockopt(interface->socketfd, IPPROTO_IP, IP_ONESBCAST, &optval, sizeof(optval)) == -1) {
++				perror("IP_ONESBCAST");
++				continue;
++			}
++#endif
+ 
+ 			setsockopt(interface->socketfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
+ 
+@@ -290,7 +296,7 @@ static int send_udp(const struct mt_connection *conn, 
+ 		struct sockaddr_in socket_address;
+ 		socket_address.sin_family = AF_INET;
+ 		socket_address.sin_port = htons(conn->srcport);
+-		socket_address.sin_addr.s_addr = htonl(INADDR_BROADCAST);
++		memcpy(&(socket_address.sin_addr), &conn->interface->bcast_addr, IPV4_ALEN);
+ 
+ 		return sendto(conn->interface->socketfd, packet->data, packet->size, 0, (struct sockaddr *)&socket_address,
+ 					  sizeof(socket_address));
+@@ -315,7 +321,7 @@ static int send_special_udp(struct net_interface *inte
+ 		struct sockaddr_in socket_address;
+ 		socket_address.sin_family = AF_INET;
+ 		socket_address.sin_port = htons(port);
+-		socket_address.sin_addr.s_addr = htonl(INADDR_BROADCAST);
++		memcpy(&(socket_address.sin_addr), &interface->bcast_addr, IPV4_ALEN);
+ 
+ 		return sendto(interface->socketfd, packet->data, packet->size, 0, (struct sockaddr *)&socket_address,
+ 					  sizeof(socket_address));
diff --git a/net-mgmt/mac-telnet/files/patch-src_mndp.c b/net-mgmt/mac-telnet/files/patch-src_mndp.c
index 5e5f11e0bde4..63e50d20effd 100644
--- a/net-mgmt/mac-telnet/files/patch-src_mndp.c
+++ b/net-mgmt/mac-telnet/files/patch-src_mndp.c
@@ -1,6 +1,6 @@
---- src/mndp.c.orig	2024-06-19 14:43:14 UTC
+--- src/mndp.c.orig	2024-08-12 21:08:19 UTC
 +++ src/mndp.c
-@@ -170,9 +170,12 @@ int mndp(int timeout, int batch_mode) {
+@@ -177,9 +177,12 @@ int mndp(int timeout, int batch_mode) {
  	return 0;
  }
  
diff --git a/net-mgmt/mac-telnet/files/patch-src_users.c b/net-mgmt/mac-telnet/files/patch-src_users.c
new file mode 100644
index 000000000000..65cc4329acd0
--- /dev/null
+++ b/net-mgmt/mac-telnet/files/patch-src_users.c
@@ -0,0 +1,90 @@
+--- src/users.c.orig	2024-08-15 14:34:02 UTC
++++ src/users.c
+@@ -211,6 +211,24 @@ struct mt_credentials *find_user(char *username) {
+ 	return NULL;
+ }
+ 
++#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
++/*
++ * Filter out colons from the decoded string.
++ * By default, the OPENSSL_buf2hexstr function in OpenSSL 1.1
++ * uses colons as a byte separator, and this cannot be overridden.
++ */
++static void remove_colons(char *s) {
++	const char *p = s;
++	char *q = s;
++	while (*p != '\0') {
++		*q = *p++;
++		q += (*q != ':');
++	}
++
++	*q = '\0';
++}
++#endif
++
+ int add_user(const char *username, const char *password) {
+ 	FILE *rfile;
+ 	FILE *wfile;
+@@ -289,12 +307,27 @@ int add_user(const char *username, const char *passwor
+ 				continue;
+ 			}
+ 			fprintf(wfile, "%s:", username);
++#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
++			char *output;
++			output = OPENSSL_buf2hexstr(newhash, MT_CRED_HASHLEN);
++			remove_colons(output);
++#else
+ 			char output[MT_CRED_HASHLEN * 2 + 1];
+ 			OPENSSL_buf2hexstr_ex(output, sizeof(output), NULL, newhash, MT_CRED_HASHLEN, '\0');
++#endif
+ 			fputs(output, wfile);
+ 			fputs(":", wfile);
++#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
++			OPENSSL_free(output);
++			output = OPENSSL_buf2hexstr(newsalt, MT_CRED_SALTLEN);
++			remove_colons(output);
++#else
+ 			OPENSSL_buf2hexstr_ex(output, sizeof(output), NULL, newsalt, MT_CRED_SALTLEN, '\0');
++#endif
+ 			fputs(output, wfile);
++#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
++			OPENSSL_free(output);
++#endif
+ 			fputs("\n", wfile);
+ 			found = 1;
+ 		} else {
+@@ -306,12 +339,27 @@ int add_user(const char *username, const char *passwor
+ 	if (!found && password != NULL) {
+ 		// Write username, salt, and hashed password to the file
+ 		fprintf(wfile, "%s:", username);
++#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
++		char *output;
++		output = OPENSSL_buf2hexstr(newhash, MT_CRED_HASHLEN);
++		remove_colons(output);
++#else
+ 		char output[MT_CRED_HASHLEN * 2 + 1];
+ 		OPENSSL_buf2hexstr_ex(output, sizeof(output), NULL, newhash, MT_CRED_HASHLEN, '\0');
++#endif
+ 		fputs(output, wfile);
+ 		fputs(":", wfile);
++#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
++		OPENSSL_free(output);
++		output = OPENSSL_buf2hexstr(newsalt, MT_CRED_SALTLEN);
++		remove_colons(output);
++#else
+ 		OPENSSL_buf2hexstr_ex(output, sizeof(output), NULL, newsalt, MT_CRED_SALTLEN, '\0');
++#endif
+ 		fputs(output, wfile);
++#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
++		OPENSSL_free(output);
++#endif
+ 		fputs("\n", wfile);
+ 	}
+ 
+@@ -327,4 +375,4 @@ int add_user(const char *username, const char *passwor
+ 	}
+ 
+ 	return found ? 2 : 1;
+-}
+\ No newline at end of file
++}
diff --git a/net-mgmt/mac-telnet/pkg-plist b/net-mgmt/mac-telnet/pkg-plist
index e89013930934..35a54cebd956 100644
--- a/net-mgmt/mac-telnet/pkg-plist
+++ b/net-mgmt/mac-telnet/pkg-plist
@@ -1,7 +1,7 @@
 bin/macping
 bin/mactelnet
 bin/mndp
-etc/mactelnetd.users
+@sample etc/mactelnetd.users.sample
 sbin/mactelnetd
 share/locale/bg/LC_MESSAGES/mactelnet.mo
 share/locale/nb/LC_MESSAGES/mactelnet.mo