From owner-svn-src-all@freebsd.org Thu Nov 17 15:19:07 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id A63E7C45CF3; Thu, 17 Nov 2016 15:19:07 +0000 (UTC) (envelope-from fanf@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 834DF1005; Thu, 17 Nov 2016 15:19:07 +0000 (UTC) (envelope-from fanf@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id uAHFJ6uG072879; Thu, 17 Nov 2016 15:19:06 GMT (envelope-from fanf@FreeBSD.org) Received: (from fanf@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id uAHFJ6me072878; Thu, 17 Nov 2016 15:19:06 GMT (envelope-from fanf@FreeBSD.org) Message-Id: <201611171519.uAHFJ6me072878@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: fanf set sender to fanf@FreeBSD.org using -f From: Tony Finch Date: Thu, 17 Nov 2016 15:19:06 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r308762 - head/usr.bin/whois X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Nov 2016 15:19:07 -0000 Author: fanf Date: Thu Nov 17 15:19:06 2016 New Revision: 308762 URL: https://svnweb.freebsd.org/changeset/base/308762 Log: More robust handling of whois referrals from RIRs. An example problem case is 163.1.0.0 (University of Oxford) which is in an APNIC ERX address range. Previously we assumed that ARIN has the correct information for all ERX allocations, but in this case ARIN refers back to APNIC, rather than referring to RIPE. This caused whois to loop. Whois will no longer loop back and forth forever between two RIRs that don't have an answer, but instead try the other RIRs in turn. Modified: head/usr.bin/whois/whois.c Modified: head/usr.bin/whois/whois.c ============================================================================== --- head/usr.bin/whois/whois.c Thu Nov 17 15:17:01 2016 (r308761) +++ head/usr.bin/whois/whois.c Thu Nov 17 15:19:06 2016 (r308762) @@ -119,12 +119,38 @@ static struct { { NULL, 0 } }; +/* + * We have a list of patterns for RIRs that assert ignorance rather than + * providing referrals. If that happens, we guess that ARIN will be more + * helpful. But, before following a referral to an RIR, we check if we have + * asked that RIR already, and if so we make another guess. + */ static const char *actually_arin[] = { "netname: ERX-NETBLOCK\n", /* APNIC */ "netname: NON-RIPE-NCC-MANAGED-ADDRESS-BLOCK\n", NULL }; +static struct { + int loop; + const char *host; +} try_rir[] = { + { 0, ANICHOST }, + { 0, RNICHOST }, + { 0, PNICHOST }, + { 0, FNICHOST }, + { 0, LNICHOST }, + { 0, NULL } +}; + +static void +reset_rir(void) { + int i; + + for (i = 0; try_rir[i].host != NULL; i++) + try_rir[i].loop = 0; +} + static const char *port = DEFAULT_PORT; static const char *choose_server(char *); @@ -232,6 +258,7 @@ main(int argc, char *argv[]) } else whois(*argv, host != NULL ? host : choose_server(*argv), flags); + reset_rir(); argv++; } exit(0); @@ -420,7 +447,7 @@ whois(const char *query, const char *hos FILE *fp; struct addrinfo *hostres; char *buf, *host, *nhost, *p; - int s, f; + int comment, s, f; size_t len, i; hostres = gethostinfo(hostname, 1); @@ -467,12 +494,28 @@ whois(const char *query, const char *hos fprintf(fp, "%s\r\n", query); fflush(fp); + comment = 0; + if (!(flags & WHOIS_SPAM_ME) && + (strcasecmp(hostname, ANICHOST) == 0 || + strcasecmp(hostname, RNICHOST) == 0)) { + comment = 2; + } + nhost = NULL; while ((buf = fgetln(fp, &len)) != NULL) { /* Nominet */ if (!(flags & WHOIS_SPAM_ME) && len == 5 && strncmp(buf, "-- \r\n", 5) == 0) break; + /* RIRs */ + if (comment == 1 && buf[0] == '#') + break; + else if (comment == 2) { + if (strchr("#%\r\n", buf[0]) != NULL) + continue; + else + comment = 1; + } printf("%.*s", (int)len, buf); @@ -487,8 +530,7 @@ whois(const char *query, const char *hos SCAN(p, buf+len, *p == ' '); host = p; SCAN(p, buf+len, ishost(*p)); - /* avoid loops */ - if (strncmp(hostname, host, p - host) != 0) + if (p > host) s_asprintf(&nhost, "%.*s", (int)(p - host), host); break; @@ -511,8 +553,37 @@ whois(const char *query, const char *hos } fclose(fp); freeaddrinfo(hostres); + + f = 0; + for (i = 0; try_rir[i].host != NULL; i++) { + /* Remember visits to RIRs */ + if (try_rir[i].loop == 0 && + strcasecmp(try_rir[i].host, hostname) == 0) + try_rir[i].loop = 1; + /* Do we need to find an alternative RIR? */ + if (try_rir[i].loop != 0 && nhost != NULL && + strcasecmp(try_rir[i].host, nhost) == 0) { + free(nhost); + nhost = NULL; + f = 1; + } + } + if (f) { + /* Find a replacement RIR */ + for (i = 0; try_rir[i].host != NULL; i++) { + if (try_rir[i].loop == 0) { + s_asprintf(&nhost, "%s", + try_rir[i].host); + break; + } + } + } if (nhost != NULL) { - whois(query, nhost, flags); + /* Ignore self-referrals */ + if (strcasecmp(hostname, nhost) != 0) { + printf("# %s\n\n", nhost); + whois(query, nhost, flags); + } free(nhost); } }