Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 21 Oct 2007 18:30:04 GMT
From:      Mike Makonnen <mtm@FreeBSD.org>
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: bin/109494: [PATCH] ypserv(8): Add option to bind to specific port
Message-ID:  <200710211830.l9LIU4U0025797@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/109494; it has been noted by GNATS.

From: Mike Makonnen <mtm@FreeBSD.Org>
To: Shaun Amott <shaun@FreeBSD.org>
Cc: bug-followup@FreeBSD.Org, Maxim Konovalov <maxim@macomnet.ru>
Subject: Re: bin/109494: [PATCH] ypserv(8): Add option to bind to specific port
Date: Sun, 21 Oct 2007 21:24:36 +0300

 --IS0zKkzwUGydFO0o
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 Hi guys,
 
 > [...]
 > 
 > That breaks ypserv(8) for ipv6.  Look at NetBSD ypserv -p
 > implementation.
 > 
 > -- maxim
 
 I'm not sure I agree. As far as I can see our version of ypserv does
 not support IPv6:
 
 yp_main.c:
 %%%
     256         if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
     257                 int ssize = sizeof (int);
     258 
     259                 if (saddr.sin_family != AF_INET)
     260                         exit(1);
 %%% 
 
 So, I think for the purposes of this PR it's ok if it doesn't include
 a patch for IPv6. And even if it did, none of the other yp* daemons
 seem to support IPv6. Also, it appears to be using the older Transport-
 Dependant RPC functions. With that in mind what do you think of the
 following patch? It's based on the submitted patch but cleaned up to
 be more concise, style(9)'ed, and made to fit in better with the
 existing code.
 
 Cheers.
 -- 
 Mike Makonnen         | GPG-KEY: http://people.freebsd.org/~mtm/mtm.asc
 mmakonnen @ gmail.com | AC7B 5672 2D11 F4D0 EBF8  5279 5359 2B82 7CD4 1F55
 mtm @ FreeBSD.Org     | FreeBSD - http://www.freebsd.org
 
 --IS0zKkzwUGydFO0o
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: attachment; filename=ypserv-patch
 
 Index: usr.sbin/ypserv/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
 --- usr.sbin/ypserv/yp_main.c	20 May 2005 13:04:10 -0000	1.28
 +++ usr.sbin/ypserv/yp_main.c	20 Oct 2007 21:48:55 -0000
 @@ -84,6 +84,16 @@
  int do_dns = 0;
  int resfd;
  
 +struct socktype {
 +	const char *st_name;
 +	int	   st_type;
 +};
 +static struct socktype stlist[] = {
 +	{ "tcp", SOCK_STREAM },
 +	{ "udp", SOCK_DGRAM },
 +	{ NULL, 0 }
 +};
 +
  static
  void _msgout(char* msg)
  {
 @@ -230,8 +240,11 @@
  	struct sockaddr_in saddr;
  	socklen_t asize = sizeof (saddr);
  	int ch;
 +	in_port_t yp_port = 0;
 +	char *errstr;
 +	struct socktype *st;
  
 -	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 +255,14 @@
  		case 'p':
  			yp_dir = optarg;
  			break;
 +		case 'P':
 +			yp_port = (in_port_t)strtonum(optarg, 1, 65535,
 +			    (const char **)&errstr);
 +			if (yp_port == 0 && errstr != NULL) {
 +				_msgout("invalid port number provided");
 +				exit(1);
 +			}
 +			break;
  		case 'h':
  		default:
  			usage();
 @@ -277,6 +298,39 @@
  		(void) pmap_unset(YPPROG, 1);
  	}
  
 +	/*
 +	 * Initialize TCP/UDP sockets.
 +	 */
 +	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);
 +	for (st = stlist; st->st_name != NULL; st++) {
 +		/* Do not bind the socket if the user didn't specify a port */
 +		if (yp_port == 0)
 +			break;
 +
 +		sock = socket(AF_INET, st->st_type, 0);
 +		if (sock == -1) {
 +			if ((asprintf(&errstr, "cannot create a %s socket",
 +			    st->st_name)) == -1)
 +				err(1, "unexpected failure in asprintf()");
 +			_msgout(errstr);
 +			free((void *)errstr);
 +			exit(1);
 +		}
 +		if (bind(sock, (struct sockaddr *) &saddr, sizeof(saddr))
 +		    == -1) {
 +			if ((asprintf(&errstr, "cannot bind %s socket",
 +			    st->st_name)) == -1)
 +				err(1, "unexpected failure in asprintf()");
 +			_msgout(errstr);
 +			free((void *)errstr);
 +			exit(1);
 +		}
 +		errstr = NULL;
 +	}
 +
  	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
  		transp = svcudp_create(sock);
  		if (transp == NULL) {
 Index: usr.sbin/ypserv/ypserv.8
 ===================================================================
 RCS file: /home/ncvs/src/usr.sbin/ypserv/ypserv.8,v
 retrieving revision 1.41
 diff -u -r1.41 ypserv.8
 --- usr.sbin/ypserv/ypserv.8	13 Feb 2005 23:45:54 -0000	1.41
 +++ usr.sbin/ypserv/ypserv.8	20 Oct 2007 19:30:01 -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
 
 --IS0zKkzwUGydFO0o--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200710211830.l9LIU4U0025797>