Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 5 Jul 2001 00:41:46 -0400 (EDT)
From:      Mike Barcroft <mike@q9media.com>
To:        audit@FreeBSD.org
Subject:   src/usr.bin/whois patch
Message-ID:  <200107050441.f654fko09366@coffee.q9media.com>

next in thread | raw e-mail | index | archive | help
I would appreciate it if someone would review and commit the patch at
the end of this message.  Also available at:
http://testbed.q9media.net/freebsd/whois.20010705.patch

Best regards,
Mike Barcroft

-----------------------------------------------------------------------

whois.20010705.patch

o Re-write the logic that finds the whois server to query.
  [This fixes a bug where someone would type 'whois foo.bar.'
  and get an error because of the trailing period.]


Index: whois/whois.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/whois/whois.c,v
retrieving revision 1.20
diff -u -r1.20 whois.c
--- whois/whois.c	2001/06/27 23:06:47	1.20
+++ whois/whois.c	2001/07/05 04:08:04
@@ -79,6 +79,7 @@
 
 const char *ip_whois[] = { RNICHOST, PNICHOST, NULL };
 
+static char *choose_server(const char *);
 static void usage(void);
 static void whois(char *, struct addrinfo *, int);
 
@@ -88,7 +89,7 @@
 	struct addrinfo hints, *res;
 	const char *host;
 	char *qnichost;
-	int ch, error, flags, i, j, use_qnichost;
+	int ch, error, flags, use_qnichost;
 
 #ifdef	SOCKS
 	SOCKSinit(argv[0]);
@@ -159,31 +160,17 @@
 	}
 	while (argc--) {
 		if (use_qnichost) {
-			for (i = j = 0; (*argv)[i]; i++)
-				if ((*argv)[i] == '.')
-					j = i;
-			if (j != 0) {
-				if (isdigit(*(*argv + j + 1))) {
-					asprintf(&qnichost, "%s", ANICHOST);
-					if (qnichost == NULL)
-						err(1, "asprintf()");
-				} else {
-					asprintf(&qnichost, "%s%s",
-					    *argv + j + 1, QNICHOST_TAIL);
-					if (qnichost == NULL)
-						err(1, "asprintf()");
-				}
-
-				memset(&hints, 0, sizeof(hints));
-				hints.ai_flags = 0;
-				hints.ai_family = AF_UNSPEC;
-				hints.ai_socktype = SOCK_STREAM;
-				error = getaddrinfo(qnichost, "whois",
-				    &hints, &res);
-				if (error != 0)
-					errx(EX_NOHOST, "%s: %s", qnichost,
-					    gai_strerror(error));
-			}
+			qnichost = choose_server(*argv);
+
+			memset(&hints, 0, sizeof(hints));
+			hints.ai_flags = 0;
+			hints.ai_family = AF_UNSPEC;
+			hints.ai_socktype = SOCK_STREAM;
+			error = getaddrinfo(qnichost, "whois",
+			    &hints, &res);
+			if (error != 0)
+				errx(EX_NOHOST, "%s: %s", qnichost,
+				    gai_strerror(error));
 		}
 		if (qnichost == NULL) {
 			memset(&hints, 0, sizeof(hints));
@@ -202,6 +189,40 @@
 		freeaddrinfo(res);
 	}
 	exit(0);
+}
+
+/*
+ * Returns a pointer to newly allocated memory containing the whois server to
+ * be queried, or a NULL pointer, if the correct server couldn't be determined.
+ * The caller must remember to free(3) the allocated memory.
+ */
+static char *
+choose_server(const char *domain)
+{
+	size_t len;
+	char *buf, *pos, *retval;
+
+	if ((buf = strdup(domain)) == NULL)
+		err(1, "strdup()");
+	len = strlen(buf);
+	while (len && buf[len - 1] == '.')
+		buf[--len] = '\0';
+	if ((pos = strrchr(buf, '.')) == NULL) {
+		free(buf);
+		return (NULL);
+	}
+	pos++;
+	if (isdigit(*pos)) {
+		asprintf(&retval, "%s", ANICHOST);
+		if (retval == NULL)
+			err(1, "asprintf()");
+	} else {
+		asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
+		if (retval == NULL)
+			err(1, "asprintf()");
+	}
+	free(buf);
+	return (retval);
 }
 
 static void

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-audit" in the body of the message




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