[PATCH]:bindresvport should check /etc/services

H. J. Lu hjl@lucon.org
Tue Nov 26 02:14:00 GMT 2002


On Mon, Nov 25, 2002 at 10:47:39PM -0800, Ulrich Drepper wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> H. J. Lu wrote:
> 
> > Not quite, at least the usage of bindresvport in glibc is questionable.
> 
> No, it's not.  If this is a problem your setup is wrong.  How many times
> do I have to repeat this?
> 

I disagree. I just don't like the default behavior of svctcp_create and
svcudp_create in glibc. I wound up fixing all rpc servers I cared with
a patch similar to the one enclosed here.


H.J.
-------------- next part --------------
--- quota-tools/Makefile.in.rpc-socket	Thu Sep 12 20:53:12 2002
+++ quota-tools/Makefile.in	Thu Sep 12 20:53:43 2002
@@ -1,5 +1,5 @@
 PROGS         = quotacheck quotaon quota quot repquota warnquota quotastats xqmstats edquota setquota convertquota rpc.rquotad 
-SOURCES       = bylabel.c common.c convertquota.c edquota.c pot.c quot.c quota.c quotacheck.c quotacheck_v1.c quotacheck_v2.c quotaio.c quotaio_rpc.c quotaio_v1.c quotaio_v2.c quotaio_xfs.c quotaon.c quotaon_xfs.c quotaops.c quotastats.c quotasys.c repquota.c rquota_client.c rquota_server.c rquota_svc.c setquota.c warnquota.c xqmstats.c
+SOURCES       = bylabel.c common.c convertquota.c edquota.c pot.c quot.c quota.c quotacheck.c quotacheck_v1.c quotacheck_v2.c quotaio.c quotaio_rpc.c quotaio_v1.c quotaio_v2.c quotaio_xfs.c quotaon.c quotaon_xfs.c quotaops.c quotastats.c quotasys.c repquota.c rquota_client.c rquota_server.c rquota_svc.c setquota.c warnquota.c xqmstats.c svc_socket.c
 VERSIONDEF    = -DQUOTA_VERSION=\"3.01\"
 CFLAGS        = @CFLAGS@ @EXT2_DIRECT@ -D_GNU_SOURCE -Wall -D_LARGEFILE64_SOURCE $(VERSIONDEF)
 EXT2LIBS      = @EXT2LIBS@
@@ -35,6 +35,7 @@ locale_dir    = $(prefix)/share/locale
 RPCCLNTOBJS = rquota_xdr.o rquota_client.o rquota_clnt.o
 IOOBJS = quotaio.o quotaio_v1.o quotaio_v2.o quotaio_rpc.o quotaio_xfs.o
 IOOBJS += $(RPCCLNTOBJS)
+IOOBJS += svc_socket.o
 LIBOBJS = bylabel.o common.o quotasys.o pot.o $(IOOBJS)
 LIBOBJS += @LIBMALLOC@
 
--- quota-tools/rquota_svc.c.rpc-socket	Thu Aug 23 06:56:02 2001
+++ quota-tools/rquota_svc.c	Thu Sep 12 20:53:12 2002
@@ -27,6 +27,7 @@
 #include <string.h>		/* strcmp */
 #include <memory.h>
 #include <unistd.h>
+#include <signal.h>
 #ifdef HOSTS_ACCESS
 #include <tcpd.h>
 #include <netdb.h>
@@ -37,6 +38,9 @@
 #define SIG_PF void(*)(int)
 #endif
 
+extern int svctcp_socket (u_long __number, int __reuse);
+extern int svcudp_socket (u_long __number, int __reuse);
+
 #include "pot.h"
 #include "common.h"
 #include "rquota.h"
@@ -260,9 +264,17 @@ static void rquotaprog_2(struct svc_req 
 	return;
 }
 
+static void
+unregister (int sig)
+{
+	(void)pmap_unset(RQUOTAPROG, RQUOTAVERS);
+	(void)pmap_unset(RQUOTAPROG, EXT_RQUOTAVERS);
+}
+
 int main(int argc, char **argv)
 {
 	register SVCXPRT *transp;
+	struct sigaction sa;
 
 	argcargs = argc;
 	argvargs = argv;
@@ -275,7 +287,17 @@ int main(int argc, char **argv)
 	(void)pmap_unset(RQUOTAPROG, RQUOTAVERS);
 	(void)pmap_unset(RQUOTAPROG, EXT_RQUOTAVERS);
 
-	transp = svcudp_create(RPC_ANYSOCK);
+	sa.sa_handler = SIG_IGN;
+	sa.sa_flags = 0;
+	sigemptyset(&sa.sa_mask);
+	sigaction(SIGCHLD, &sa, NULL);
+
+	sa.sa_handler = unregister;
+	sigaction(SIGHUP, &sa, NULL);
+	sigaction(SIGINT, &sa, NULL);
+	sigaction(SIGTERM, &sa, NULL);
+
+	transp = svcudp_create(svcudp_socket (RQUOTAPROG, 1));
 	if (transp == NULL) {
 		errstr(_("cannot create udp service.\n"));
 		exit(1);
@@ -289,7 +311,7 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 
-	transp = svctcp_create(RPC_ANYSOCK, 0, 0);
+	transp = svctcp_create(svctcp_socket (RQUOTAPROG, 1), 0, 0);
 	if (transp == NULL) {
 		errstr(_("cannot create tcp service.\n"));
 		exit(1);
--- quota-tools/svc_socket.c.rpc-socket	Thu Sep 12 20:53:12 2002
+++ quota-tools/svc_socket.c	Thu Sep 12 20:53:12 2002
@@ -0,0 +1,180 @@
+/* Copyright (C) 2002 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <netdb.h>
+#include <rpc/rpc.h>
+#include <sys/socket.h>
+#include <errno.h>
+
+#ifdef _LIBC
+# include <libintl.h>
+#else
+# ifndef _
+#  define _(s)			(s)
+# endif
+# define __socket(d, t, p)	socket ((d), (t), (p))
+# define __close(f)		close ((f))
+#endif
+
+static int
+svc_socket (u_long number, int type, int protocol, int reuse)
+{
+  struct sockaddr_in addr;
+  socklen_t len = sizeof (struct sockaddr_in);
+  char rpcdata [1024], servdata [1024];
+  struct rpcent rpcbuf, *rpcp;
+  struct servent servbuf, *servp;
+  int sock, ret;
+  const char *proto = protocol == IPPROTO_TCP ? "tcp" : "udp";
+
+  if ((sock = __socket (AF_INET, type, protocol)) < 0)
+    {
+      perror (_("svc_socket: socket creation problem"));
+      return sock;
+    }
+
+  if (reuse)
+    {
+      ret = 1;
+      ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &ret,
+			sizeof (ret));
+      if (ret < 0)
+	{
+	  perror (_("svc_socket: socket reuse problem"));
+	  return ret;
+	}
+    }
+
+  __bzero ((char *) &addr, sizeof (addr));
+  addr.sin_family = AF_INET;
+
+  ret = getrpcbynumber_r (number, &rpcbuf, rpcdata, sizeof rpcdata,
+			  &rpcp);
+  if (ret == 0 && rpcp != NULL)
+    {
+      /* First try name.  */
+      ret = getservbyname_r (rpcp->r_name, proto, &servbuf, servdata,
+			     sizeof servdata, &servp);
+      if ((ret != 0 || servp == NULL) && rpcp->r_aliases)
+	{
+	  const char **a;
+
+	  /* Then we try aliases.  */
+	  for (a = (const char **) rpcp->r_aliases; *a != NULL; a++) 
+	    {
+	      ret = getservbyname_r (*a, proto, &servbuf, servdata,
+				     sizeof servdata, &servp);
+	      if (ret == 0 && servp != NULL)
+		break;
+	    }
+	}
+    }
+
+  if (ret == 0 && servp != NULL)
+    {
+      addr.sin_port = servp->s_port;
+      if (bind (sock, (struct sockaddr *) &addr, len) < 0)
+	{
+	  perror (_("svc_socket: bind problem"));
+	  (void) __close (sock);
+	  sock = -1;
+	}
+    }
+  else
+    {
+      if (bindresvport (sock, &addr))
+	{
+	  addr.sin_port = 0;
+	  if (bind (sock, (struct sockaddr *) &addr, len) < 0)
+	    {
+	      perror (_("svc_socket: bind problem"));
+	      (void) __close (sock);
+	      sock = -1;
+	    }
+	}
+    }
+
+  return sock;
+}
+
+/*
+ * Create and bind a TCP socket based on program number
+ */
+int
+svctcp_socket (u_long number, int reuse)
+{
+  return svc_socket (number, SOCK_STREAM, IPPROTO_TCP, reuse);
+}
+
+/*
+ * Create and bind a UDP socket based on program number
+ */
+int
+svcudp_socket (u_long number, int reuse)
+{
+  return svc_socket (number, SOCK_DGRAM, IPPROTO_UDP, reuse);
+}
+
+#ifdef TEST
+static int
+check (u_long number, u_short port, int protocol, int reuse)
+{
+  int socket;
+  int result;
+  struct sockaddr_in addr;
+  socklen_t len = sizeof (struct sockaddr_in);
+
+  if (protocol == IPPROTO_TCP)
+    socket = svctcp_socket (number, reuse);
+  else
+    socket = svcudp_socket (number, reuse);
+
+  if (socket < 0)
+    return 1;
+
+  result = getsockname (socket, (struct sockaddr *) &addr, &len);
+  if (result == 0)
+    {
+      if (port != 0 && ntohs (addr.sin_port) != port)
+	printf ("Program: %ld, expect port: %d, got: %d\n",
+		number, port, ntohs (addr.sin_port)); 
+      else
+	printf ("Program: %ld, port: %d\n",
+		number, ntohs (addr.sin_port)); 
+    }
+
+  close (socket);
+  return result;
+}
+
+int
+main (void)
+{
+  int result = 0;
+
+  result += check (100001, 0, IPPROTO_TCP, 0);
+  result += check (100001, 0, IPPROTO_UDP, 0);
+  result += check (100003, 2049, IPPROTO_TCP, 1);
+  result += check (100003, 2049, IPPROTO_UDP, 1);
+
+  return result;
+}
+#endif


More information about the Libc-alpha mailing list