bin/109494: [PATCH] ypserv: Add option to bind to specific port

Shaun Amott shaun at FreeBSD.org
Sat Feb 24 17:40:09 UTC 2007


>Number:         109494
>Category:       bin
>Synopsis:       [PATCH] ypserv: Add option to bind to specific port
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sat Feb 24 17:40:08 GMT 2007
>Closed-Date:
>Last-Modified:
>Originator:     Shaun Amott
>Release:        FreeBSD 6.2-PRERELEASE i386
>Organization:
>Environment:

>Description:

The attached patch adds adds an option to the ypserv daemon which
allows the user to specify a specific port number to bind to, making
(e.g.) packet filtering easier.

>How-To-Repeat:

>Fix:

--- ypserv.diff begins here ---
Index: yp_main.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/ypserv/yp_main.c,v
retrieving revision 1.28
diff -u -r1.28 yp_main.c
--- yp_main.c	20 May 2005 13:04:10 -0000	1.28
+++ yp_main.c	24 Feb 2007 16:34:03 -0000
@@ -80,6 +80,7 @@
 extern int _rpcsvcstate;	 /* Set when a request is serviced */
 char *progname = "ypserv";
 char *yp_dir = _PATH_YP;
+long yp_port = -1;
 /*int debug = 0;*/
 int do_dns = 0;
 int resfd;
@@ -231,7 +232,7 @@
 	socklen_t asize = sizeof (saddr);
 	int ch;
 
-	while ((ch = getopt(argc, argv, "hdnp:")) != -1) {
+	while ((ch = getopt(argc, argv, "hdnp:P:")) != -1) {
 		switch (ch) {
 		case 'd':
 			debug = ypdb_debug = 1;
@@ -242,6 +243,13 @@
 		case 'p':
 			yp_dir = optarg;
 			break;
+		case 'P':
+			if ((yp_port = strtoul(optarg, NULL, 10)) == ULONG_MAX) {
+				err(1,"invalid port number provided");
+			} else if (yp_port > IPPORT_MAX) {
+				errx(1,"port number out of range");
+			}
+			break;
 		case 'h':
 		default:
 			usage();
@@ -277,6 +285,21 @@
 		(void) pmap_unset(YPPROG, 1);
 	}
 
+	if (yp_port >= 0) {
+		if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+			err(1,"cannot create udp socket");
+		}
+
+		memset((char *)&saddr, 0, sizeof(saddr));
+		saddr.sin_family = AF_INET;
+		saddr.sin_addr.s_addr = htonl(INADDR_ANY);
+		saddr.sin_port = htons(yp_port);
+
+		if (bind(sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) {
+			err(1,"cannot bind udp socket");
+		}
+	}
+
 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
 		transp = svcudp_create(sock);
 		if (transp == NULL) {
@@ -295,6 +318,21 @@
 		}
 	}
 
+	if (yp_port >= 0) {
+		if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+			err(1,"cannot create tcp socket");
+		}
+
+		memset((char *)&saddr, 0, sizeof(saddr));
+		saddr.sin_family = AF_INET;
+		saddr.sin_addr.s_addr = htonl(INADDR_ANY);
+		saddr.sin_port = htons(yp_port);
+
+		if (bind(sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) {
+			err(1,"cannot bind tcp socket");
+		}
+	}
+
 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
 		transp = svctcp_create(sock, 0, 0);
 		if (transp == NULL) {
Index: ypserv.8
===================================================================
RCS file: /home/ncvs/src/usr.sbin/ypserv/ypserv.8,v
retrieving revision 1.41
diff -u -r1.41 ypserv.8
--- ypserv.8	13 Feb 2005 23:45:54 -0000	1.41
+++ ypserv.8	24 Feb 2007 16:34:03 -0000
@@ -40,6 +40,7 @@
 .Nm
 .Op Fl n
 .Op Fl d
+.Op Fl P Ar port
 .Op Fl p Ar path
 .Sh DESCRIPTION
 .Tn NIS
@@ -403,6 +404,9 @@
 other requests.)
 This makes it easier to trace the server with
 a debugging tool.
+.It Fl P Ar port
+Force ypserv to bind to a specific TCP/UDP port, rather than selecting
+its own.
 .It Fl p Ar path
 Normally,
 .Nm
--- ypserv.diff ends here ---
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list