bin/109494: [PATCH] ypserv: Add option to bind to specific port
Maxim Konovalov
maxim at macomnet.ru
Sun Feb 25 10:10:14 UTC 2007
The following reply was made to PR bin/109494; it has been noted by GNATS.
From: Maxim Konovalov <maxim at macomnet.ru>
To: Shaun Amott <shaun at freebsd.org>
Cc: bug-followup at freebsd.org
Subject: Re: bin/109494: [PATCH] ypserv: Add option to bind to specific port
Date: Sun, 25 Feb 2007 13:07:43 +0300 (MSK)
Hi Shaun,
[...]
> >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;
strtoul() usage above is not entirely correct. Better to use
strtonum(3) instead.
> 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) {
[...]
That breaks ypserv(8) for ipv6. Look at NetBSD ypserv -p
implementation.
-- maxim
More information about the freebsd-bugs
mailing list