From ae2a77e2bf77b76ccc7fa1228351b51265adcea2 Mon Sep 17 00:00:00 2001
From: Brian Somers <brian@FreeBSD.org>
Date: Wed, 12 Jan 2000 21:53:46 +0000
Subject: [PATCH] As we parse the interface list, ignore sockaddr_dl's that
 don't have an interface index that's the same as the if_msghdr interface
 index.

This prevents the occasional perror("SIOCGIFFLAGS") from appearing
at boot time.

While I'm there:
  Make a couple of error messages more useful.
  Add a missing include.
  Add some braces to silence gccs dumb complaints.
  Add some consts
  Ansify decls
  Add copyright to pmap_check.h (well, you could say it's been rewritten)
---
 usr.sbin/portmap/from_local.c          | 34 ++++++++++-----
 usr.sbin/portmap/pmap_check.c          | 60 +++++++++++---------------
 usr.sbin/portmap/pmap_check.h          | 43 +++++++++++++++---
 usr.sbin/portmap/pmap_dump/pmap_dump.c | 10 ++---
 usr.sbin/portmap/pmap_set/pmap_set.c   | 15 ++-----
 usr.sbin/portmap/portmap.c             | 49 +++++++--------------
 6 files changed, 108 insertions(+), 103 deletions(-)

diff --git a/usr.sbin/portmap/from_local.c b/usr.sbin/portmap/from_local.c
index dbff1d6e6541..7f844558a602 100644
--- a/usr.sbin/portmap/from_local.c
+++ b/usr.sbin/portmap/from_local.c
@@ -54,7 +54,10 @@ static const char rcsid[] =
 #include <sys/time.h>
 
 #include <netdb.h>
+#include <errno.h>
+#include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <syslog.h>
 #include <unistd.h>
 
@@ -62,6 +65,8 @@ static const char rcsid[] =
 #include <net/if_dl.h>
 #include <netinet/in.h>
 
+#include "pmap_check.h"
+
 #ifndef TRUE
 #define	TRUE	1
 #define FALSE	0
@@ -75,8 +80,8 @@ static struct in_addr *addrs;
 
 /* find_local - find all IP addresses for this host */
 
-int
-find_local()
+static int
+find_local(void)
 {
   int mib[6], n, s, alloced;
   size_t needed;
@@ -122,19 +127,25 @@ find_local()
   for (ptr = buf; ptr < end; ptr += ifm->ifm_msglen) {
     ifm = (struct if_msghdr *)ptr;
     dl = (struct sockaddr_dl *)(ifm + 1);
+
+    if (ifm->ifm_index != dl->sdl_index || dl->sdl_nlen == 0)
+      /* We only want to see each interface once */
+      continue;
+
     n = dl->sdl_nlen > sizeof ifr.ifr_name ?
         sizeof ifr.ifr_name : dl->sdl_nlen;
-    if (n == 0)
-      continue;
     strncpy(ifr.ifr_name, dl->sdl_data, n);
     if (n < sizeof ifr.ifr_name)
       ifr.ifr_name[n] = '\0';
+
     /* we only want the first address from each interface */
     if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
-      perror("SIOCGIFFLAGS");
-    else if (ifr.ifr_flags & IFF_UP)    /* active interface */
+      fprintf(stderr, "%.*s: SIOCGIFFLAGS: %s\n", n, ifr.ifr_name,
+              strerror(errno));
+    else if (ifr.ifr_flags & IFF_UP) {    /* active interface */
       if (ioctl(s, SIOCGIFADDR, &ifr) < 0)
-        perror("SIOCGIFADDR");
+        fprintf(stderr, "%.*s: SIOCGIFADDR: %s\n", n, ifr.ifr_name,
+                strerror(errno));
       else {
         if (alloced < num_local + 1) {
           alloced += ESTIMATED_LOCAL;
@@ -150,6 +161,7 @@ find_local()
         }
         addrs[num_local++] = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
       }
+    }
   }
   free(buf);
   close(s);
@@ -160,8 +172,7 @@ find_local()
 /* from_local - determine whether request comes from the local system */
 
 int
-from_local(addr)
-struct sockaddr_in *addr;
+from_local(struct sockaddr_in *addr)
 {
     int     i;
 
@@ -178,7 +189,8 @@ struct sockaddr_in *addr;
 
 #ifdef TEST
 
-main()
+int
+main(int argc, char **argv)
 {
     char   *inet_ntoa();
     int     i;
@@ -186,6 +198,8 @@ main()
     find_local();
     for (i = 0; i < num_local; i++)
 	printf("%s\n", inet_ntoa(addrs[i]));
+
+    return 0;
 }
 
 #endif
diff --git a/usr.sbin/portmap/pmap_check.c b/usr.sbin/portmap/pmap_check.c
index 818c0d1d4d51..0776388e470d 100644
--- a/usr.sbin/portmap/pmap_check.c
+++ b/usr.sbin/portmap/pmap_check.c
@@ -63,8 +63,9 @@ static const char rcsid[] =
 #define YPPROC_DOMAIN_NONACK ((u_long) 2)
 #define MOUNTPROC_MNT	((u_long) 1)
 
-static void logit();
-static void toggle_verboselog();
+static void logit(int, struct sockaddr_in *, u_long, u_long, const char *);
+static void toggle_verboselog(int);
+
 int     verboselog = 0;
 int     allow_severity = LOG_INFO;
 int     deny_severity = LOG_WARNING;
@@ -93,7 +94,8 @@ int     deny_severity = LOG_WARNING;
 
 /* check_startup - additional startup code */
 
-void    check_startup()
+void
+check_startup()
 {
 
     /*
@@ -110,10 +112,7 @@ void    check_startup()
 /* check_default - additional checks for NULL, DUMP, GETPORT and unknown */
 
 int
-check_default(addr, proc, prog)
-struct sockaddr_in *addr;
-u_long  proc;
-u_long  prog;
+check_default(struct sockaddr_in *addr, u_long proc, u_long prog)
 {
 #ifdef HOSTS_ACCESS
     if (!(from_local(addr) || good_client(addr))) {
@@ -129,11 +128,8 @@ u_long  prog;
 /* check_privileged_port - additional checks for privileged-port updates */
 
 int
-check_privileged_port(addr, proc, prog, port)
-struct sockaddr_in *addr;
-u_long  proc;
-u_long  prog;
-u_long  port;
+check_privileged_port(struct sockaddr_in *addr, u_long proc, u_long prog,
+	u_long port)
 {
 #ifdef CHECK_PORT
     if (!legal_port(addr, port)) {
@@ -147,11 +143,7 @@ u_long  port;
 /* check_setunset - additional checks for update requests */
 
 int
-check_setunset(addr, proc, prog, port)
-struct sockaddr_in *addr;
-u_long  proc;
-u_long  prog;
-u_long  port;
+check_setunset(struct sockaddr_in *addr, u_long proc, u_long prog, u_long port)
 {
     if (!from_local(addr)) {
 #ifdef HOSTS_ACCESS
@@ -170,11 +162,7 @@ u_long  port;
 /* check_callit - additional checks for forwarded requests */
 
 int
-check_callit(addr, proc, prog, aproc)
-struct sockaddr_in *addr;
-u_long  proc;
-u_long  prog;
-u_long  aproc;
+check_callit(struct sockaddr_in *addr, u_long proc, u_long prog, u_long aproc)
 {
 #ifdef HOSTS_ACCESS
     if (!(from_local(addr) || good_client(addr))) {
@@ -195,8 +183,8 @@ u_long  aproc;
 
 /* toggle_verboselog - toggle verbose logging flag */
 
-static void toggle_verboselog(sig)
-int     sig;
+static void
+toggle_verboselog(int sig)
 {
     (void) signal(sig, toggle_verboselog);
     verboselog = !verboselog;
@@ -204,21 +192,18 @@ int     sig;
 
 /* logit - report events of interest via the syslog daemon */
 
-static void logit(severity, addr, procnum, prognum, text)
-int     severity;
-struct sockaddr_in *addr;
-u_long  procnum;
-u_long  prognum;
-char   *text;
+static void
+logit(int severity, struct sockaddr_in *addr, u_long procnum, u_long prognum,
+    const char *text)
 {
-    char   *procname;
+    const char *procname;
     char    procbuf[4 * sizeof(u_long)];
-    char   *progname;
+    const char *progname;
     char    progbuf[4 * sizeof(u_long)];
     struct rpcent *rpc;
     struct proc_map {
 	u_long  code;
-	char   *proc;
+	const char *proc;
     };
     struct proc_map *procp;
     static struct proc_map procmap[] = {
@@ -245,15 +230,18 @@ char   *text;
 	} else if ((rpc = getrpcbynumber((int) prognum))) {
 	    progname = rpc->r_name;
 	} else {
-	    sprintf(progname = progbuf, "%lu", prognum);
+	    sprintf(progbuf, "%lu", prognum);
+	    progname = progbuf;
 	}
 
 	/* Try to map procedure number to name. */
 
 	for (procp = procmap; procp->proc && procp->code != procnum; procp++)
 	     /* void */ ;
-	if ((procname = procp->proc) == 0)
-	    sprintf(procname = procbuf, "%lu", (u_long) procnum);
+	if ((procname = procp->proc) == 0) {
+	    sprintf(procbuf, "%lu", (u_long) procnum);
+	    procname = procbuf;
+	}
 
 	/* Write syslog record. */
 
diff --git a/usr.sbin/portmap/pmap_check.h b/usr.sbin/portmap/pmap_check.h
index 2c240df82f18..c5927a475d2f 100644
--- a/usr.sbin/portmap/pmap_check.h
+++ b/usr.sbin/portmap/pmap_check.h
@@ -1,11 +1,40 @@
-/* @(#) pmap_check.h 1.3 93/11/21 16:18:53 */
+/*-
+ * Copyright (c) 2000 Brian Somers <brian@Awfulhak.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#) pmap_check.h 1.3 93/11/21 16:18:53
+ *
+ * $FreeBSD$
+ */
+
+extern int from_local(struct sockaddr_in *);
+extern void check_startup(void);
+extern int check_default(struct sockaddr_in *, u_long, u_long);
+extern int check_setunset(struct sockaddr_in *, u_long, u_long, u_long);
+extern int check_privileged_port(struct sockaddr_in *, u_long, u_long, u_long);
+extern int check_callit(struct sockaddr_in *, u_long, u_long, u_long);
 
-extern int from_local();
-extern void check_startup();
-extern int check_default();
-extern int check_setunset();
-extern int check_privileged_port();
-extern int check_callit();
 extern int verboselog;
 extern int allow_severity;
 extern int deny_severity;
diff --git a/usr.sbin/portmap/pmap_dump/pmap_dump.c b/usr.sbin/portmap/pmap_dump/pmap_dump.c
index 944081fe1c2f..a23896e95982 100644
--- a/usr.sbin/portmap/pmap_dump/pmap_dump.c
+++ b/usr.sbin/portmap/pmap_dump/pmap_dump.c
@@ -25,12 +25,10 @@ static const char rcsid[] =
 #include <rpc/pmap_clnt.h>
 #include <rpc/pmap_prot.h>
 
-static char *protoname();
+static const char *protoname(u_long);
 
 int
-main(argc, argv)
-int     argc;
-char  **argv;
+main(int argc, char **argv)
 {
     struct sockaddr_in addr;
     register struct pmaplist *list;
@@ -51,8 +49,8 @@ char  **argv;
     return (fclose(stdout) ? (perror(argv[0]), 1) : 0);
 }
 
-static char *protoname(proto)
-u_long  proto;
+static const char *
+protoname(u_long proto)
 {
     static char buf[BUFSIZ];
 
diff --git a/usr.sbin/portmap/pmap_set/pmap_set.c b/usr.sbin/portmap/pmap_set/pmap_set.c
index 34b4bc1a9d6e..deeb6860e871 100644
--- a/usr.sbin/portmap/pmap_set/pmap_set.c
+++ b/usr.sbin/portmap/pmap_set/pmap_set.c
@@ -22,12 +22,10 @@ static const char rcsid[] =
 #include <rpc/rpc.h>
 #include <rpc/pmap_clnt.h>
 
-int parse_line __P((char *, u_long *, u_long *, int *, unsigned *));
+static int parse_line (char *, u_long *, u_long *, int *, unsigned *);
 
 int
-main(argc, argv)
-int     argc;
-char  **argv;
+main(int argc, char **argv)
 {
     struct sockaddr_in addr;
     char    buf[BUFSIZ];
@@ -51,13 +49,8 @@ char  **argv;
 
 /* parse_line - convert line to numbers */
 
-int
-parse_line(buf, prog, vers, prot, port)
-char   *buf;
-u_long *prog;
-u_long *vers;
-int    *prot;
-unsigned *port;
+static int
+parse_line(char *buf, u_long *prog, u_long *vers, int *prot, unsigned *port)
 {
     char    proto_name[BUFSIZ];
 
diff --git a/usr.sbin/portmap/portmap.c b/usr.sbin/portmap/portmap.c
index 28dbda4ac468..38efae473622 100644
--- a/usr.sbin/portmap/portmap.c
+++ b/usr.sbin/portmap/portmap.c
@@ -102,18 +102,16 @@ static char sccsid[] = "@(#)portmap.c 1.32 87/08/06 Copyr 1984 Sun Micro";
 
 #include "pmap_check.h"
 
-void reg_service();
-void reap();
-static void callit();
-static void usage __P((void));
+static void reg_service(struct svc_req *, SVCXPRT *);
+static void reap(int);
+static void callit(struct svc_req *, SVCXPRT *);
+static void usage(void);
 
 struct pmaplist *pmaplist;
 int debugging = 0;
 
 int
-main(argc, argv)
-	int argc;
-	char **argv;
+main(int argc, char **argv)
 {
 	SVCXPRT *xprt;
 	int sock, c;
@@ -220,8 +218,7 @@ perror(what)
 #endif
 
 static struct pmaplist *
-find_service(prog, vers, prot)
-	u_long prog, vers, prot;
+find_service(u_long prog, u_long vers, u_long prot)
 {
 	register struct pmaplist *hit = NULL;
 	register struct pmaplist *pml;
@@ -240,10 +237,8 @@ find_service(prog, vers, prot)
 /*
  * 1 OK, 0 not
  */
-void
-reg_service(rqstp, xprt)
-	struct svc_req *rqstp;
-	SVCXPRT *xprt;
+static void
+reg_service(struct svc_req *rqstp, SVCXPRT *xprt)
 {
 	struct pmap reg;
 	struct pmaplist *pml, *prevpml, *fnd;
@@ -453,9 +448,7 @@ struct encap_parms {
 };
 
 static bool_t
-xdr_encap_parms(xdrs, epp)
-	XDR *xdrs;
-	struct encap_parms *epp;
+xdr_encap_parms(XDR *xdrs, struct encap_parms *epp)
 {
 
 	return (xdr_bytes(xdrs, &(epp->args), &(epp->arglen), ARGSIZE));
@@ -470,9 +463,7 @@ struct rmtcallargs {
 };
 
 static bool_t
-xdr_rmtcall_args(xdrs, cap)
-	register XDR *xdrs;
-	register struct rmtcallargs *cap;
+xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
 {
 
 	/* does not get a port number */
@@ -485,9 +476,7 @@ xdr_rmtcall_args(xdrs, cap)
 }
 
 static bool_t
-xdr_rmtcall_result(xdrs, cap)
-	register XDR *xdrs;
-	register struct rmtcallargs *cap;
+xdr_rmtcall_result(XDR *xdrs, struct rmtcallargs *cap)
 {
 	if (xdr_u_long(xdrs, &(cap->rmt_port)))
 		return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
@@ -499,9 +488,7 @@ xdr_rmtcall_result(xdrs, cap)
  * The arglen must already be set!!
  */
 static bool_t
-xdr_opaque_parms(xdrs, cap)
-	XDR *xdrs;
-	struct rmtcallargs *cap;
+xdr_opaque_parms(XDR *xdrs, struct rmtcallargs *cap)
 {
 
 	return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
@@ -512,9 +499,7 @@ xdr_opaque_parms(xdrs, cap)
  * and then calls xdr_opaque_parms.
  */
 static bool_t
-xdr_len_opaque_parms(xdrs, cap)
-	register XDR *xdrs;
-	struct rmtcallargs *cap;
+xdr_len_opaque_parms(XDR *xdrs, struct rmtcallargs *cap)
 {
 	register u_int beginpos, lowpos, highpos, currpos, pos;
 
@@ -545,9 +530,7 @@ xdr_len_opaque_parms(xdrs, cap)
  * back to the portmapper.
  */
 static void
-callit(rqstp, xprt)
-	struct svc_req *rqstp;
-	SVCXPRT *xprt;
+callit(struct svc_req *rqstp, SVCXPRT *xprt)
 {
 	struct rmtcallargs a;
 	struct pmaplist *pml;
@@ -602,8 +585,8 @@ callit(rqstp, xprt)
 	exit(0);
 }
 
-void
-reap()
+static void
+reap(int sig)
 {
 	int save_errno;