From owner-svn-src-head@freebsd.org Thu Jul 9 17:27:14 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id D936135077A; Thu, 9 Jul 2020 17:27:14 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4B2jm25S05z4KbQ; Thu, 9 Jul 2020 17:27:14 +0000 (UTC) (envelope-from markj@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 mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 9E15714AB4; Thu, 9 Jul 2020 17:27:14 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 069HREnn011261; Thu, 9 Jul 2020 17:27:14 GMT (envelope-from markj@FreeBSD.org) Received: (from markj@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 069HREhk011260; Thu, 9 Jul 2020 17:27:14 GMT (envelope-from markj@FreeBSD.org) Message-Id: <202007091727.069HREhk011260@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: markj set sender to markj@FreeBSD.org using -f From: Mark Johnston Date: Thu, 9 Jul 2020 17:27:14 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r363052 - head/usr.bin/whois X-SVN-Group: head X-SVN-Commit-Author: markj X-SVN-Commit-Paths: head/usr.bin/whois X-SVN-Commit-Revision: 363052 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 09 Jul 2020 17:27:14 -0000 Author: markj Date: Thu Jul 9 17:27:14 2020 New Revision: 363052 URL: https://svnweb.freebsd.org/changeset/base/363052 Log: whois: Handle referrals to rwhois servers. PR: 243862 Submitted by: ben@desync.com Differential Revision: https://reviews.freebsd.org/D25156 Modified: head/usr.bin/whois/whois.c Modified: head/usr.bin/whois/whois.c ============================================================================== --- head/usr.bin/whois/whois.c Thu Jul 9 17:26:49 2020 (r363051) +++ head/usr.bin/whois/whois.c Thu Jul 9 17:27:14 2020 (r363052) @@ -117,6 +117,7 @@ static struct { WHOIS_REFERRAL("Whois Server:"), WHOIS_REFERRAL("Registrar WHOIS Server:"), /* corporatedomains.com */ WHOIS_REFERRAL("ReferralServer: whois://"), /* ARIN */ + WHOIS_REFERRAL("ReferralServer: rwhois://"), /* ARIN */ WHOIS_REFERRAL("descr: region. Please query"), /* AfriNIC */ { NULL, 0 } }; @@ -156,10 +157,10 @@ reset_rir(void) { static const char *port = DEFAULT_PORT; static const char *choose_server(char *); -static struct addrinfo *gethostinfo(char const *host, int exitnoname); +static struct addrinfo *gethostinfo(const char *, const char *, int); static void s_asprintf(char **ret, const char *format, ...) __printflike(2, 3); static void usage(void); -static void whois(const char *, const char *, int); +static void whois(const char *, const char *, const char *, int); int main(int argc, char *argv[]) @@ -255,11 +256,11 @@ main(int argc, char *argv[]) if (country != NULL) { char *qnichost; s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL); - whois(*argv, qnichost, flags); + whois(*argv, qnichost, port, flags); free(qnichost); } else whois(*argv, host != NULL ? host : - choose_server(*argv), flags); + choose_server(*argv), port, flags); reset_rir(); argv++; } @@ -283,7 +284,7 @@ choose_server(char *domain) } static struct addrinfo * -gethostinfo(char const *host, int exit_on_noname) +gethostinfo(const char *host, const char *hport, int exit_on_noname) { struct addrinfo hints, *res; int error; @@ -293,7 +294,7 @@ gethostinfo(char const *host, int exit_on_noname) hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; res = NULL; - error = getaddrinfo(host, port, &hints, &res); + error = getaddrinfo(host, hport, &hints, &res); if (error && (exit_on_noname || error != EAI_NONAME)) err(EX_NOHOST, "%s: %s", host, gai_strerror(error)); return (res); @@ -444,15 +445,15 @@ done: } static void -whois(const char *query, const char *hostname, int flags) +whois(const char *query, const char *hostname, const char *hostport, int flags) { FILE *fp; struct addrinfo *hostres; - char *buf, *host, *nhost, *p; + char *buf, *host, *nhost, *nport, *p; int comment, s, f; size_t len, i; - hostres = gethostinfo(hostname, 1); + hostres = gethostinfo(hostname, hostport, 1); s = connect_to_any_host(hostres); if (s == -1) err(EX_OSERR, "connect()"); @@ -532,14 +533,35 @@ whois(const char *query, const char *hostname, int fla SCAN(p, buf+len, *p == ' '); host = p; SCAN(p, buf+len, ishost(*p)); - if (p > host) + if (p > host) { + char *pstr; + s_asprintf(&nhost, "%.*s", (int)(p - host), host); + + if (*p != ':') { + s_asprintf(&nport, "%s", port); + break; + } + + pstr = ++p; + SCAN(p, buf+len, isdigit(*p)); + if (p > pstr && (p - pstr) < 6) { + s_asprintf(&nport, "%.*s", + (int)(p - pstr), pstr); + break; + } + + /* Invalid port; don't recurse */ + free(nhost); + nhost = NULL; + } break; } for (i = 0; actually_arin[i] != NULL; i++) { if (strncmp(buf, actually_arin[i], len) == 0) { s_asprintf(&nhost, "%s", ANICHOST); + s_asprintf(&nport, "%s", port); break; } } @@ -565,17 +587,19 @@ whois(const char *query, const char *hostname, int fla /* 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; + free(nhost); + nhost = NULL; + free(nport); + nport = 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); + s_asprintf(&nhost, "%s", try_rir[i].host); + s_asprintf(&nport, "%s", port); break; } } @@ -584,9 +608,10 @@ whois(const char *query, const char *hostname, int fla /* Ignore self-referrals */ if (strcasecmp(hostname, nhost) != 0) { printf("# %s\n\n", nhost); - whois(query, nhost, flags); + whois(query, nhost, nport, flags); } free(nhost); + free(nport); } }