Skip to content

Commit

Permalink
ensure input is an IP address or resolves, at least by default
Browse files Browse the repository at this point in the history
This helps address issue#4: we check if the given query string is
an IP address or if it resolves.  If not, we bail out, but the
user can choose to override this by specifying '-f', in which case
we will perform the whois lookups even if the given name doesn't
resolve (nor is an IP address).
  • Loading branch information
jschauma committed May 26, 2024
1 parent bb8c4a8 commit 18bd02c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 32 deletions.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,42 @@ $ make PREFIX=~ install
---
```
NAME
jswhois -- whois lookup results in json format
jswhois - whois lookup results in json format
SYNOPSIS
jswhois [-QRVlv] [-h host] [-p port] domain
jswhois [-QRVflv] [-h host] [-p port] domain [...]
DESCRIPTION
The jswhois tool performs whois(1) lookups and prints results in JSON
format.
Since the WHOIS protocol notoriously does not include a specification of
the WHOIS data's format or how recursive discovery should be handled, the
results -- much like the results of the normal whois(1) command -- tend
to vary significantly.
results -- much like the results of the normal whois(1) command -- tend to
vary significantly.
OPTIONS
The following options are supported by jswhois:
-Q Do a quick lookup; jswhois will not attempt to follow referrals
to other whois servers. This is the default if a server is
-Q Do a quick lookup; jswhois will not attempt to follow referrals to
other whois servers. This is the default if a server is
explicitly specified via the -h flag. See also the -R option.
-R Do a recursive lookup; jswhois will attempt to follow referrals
to other whois servers. This is the default if -h is not speci-
fied. See also the -Q option.
-R Do a recursive lookup; jswhois will attempt to follow referrals to
other whois servers. This is the default if -h is not specified.
See also the -Q option.
-V Print version information and exit.
-f Force whois lookups even if the given input is not an IP address
and it doesn't resolve as a hostname.
-h host Use the specified host instead of the default (whois.iana.org).
-l Only print results from the last WHOIS server queried.
-p port Connect to the whois server on port. If this option is not spec-
ified, jswhois defaults to port 43.
-p port Connect to the whois server on port. If this option is not
specified, jswhois defaults to port 43.
-v Be verbose. Can be specified multiple times.
Expand All @@ -68,8 +71,8 @@ DETAILS
text output in a coherent JSON object.
The query for any domain will always begin at 'whois.iana.org' and then
recurse as per the data encountered. The resulting JSON document will
then contain nested structures indexed by the name of the WHOIS server in
recurse as per the data encountered. The resulting JSON document will then
contain nested structures indexed by the name of the WHOIS server in
question.
Since the data is fundamentally unstructured, attempts to stuff them into
Expand Down
9 changes: 6 additions & 3 deletions doc/jswhois.1
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
.Dd September 28, 2023
.Dd May 26, 2025
.Dt jswhois 1
.Os
.Sh NAME
.Nm jswhois
.Nd whois lookup results in json format
.Sh SYNOPSIS
.Nm
.Op Fl QRVlv
.Op Fl QRVflv
.Op Fl h Ar host
.Op Fl p Ar port
.Ar domain
Expand Down Expand Up @@ -53,6 +53,9 @@ See also the
option.
.It Fl V
Print version information and exit.
.It Fl f
Force whois lookups even if the given input is not an
IP address and it doesn't resolve as a hostname.
.It Fl h Ar host
Use the specified host instead of the default
(whois.iana.org).
Expand Down Expand Up @@ -96,7 +99,7 @@ repeated fields are turned into a list
.It
a chain of WHOIS servers queried is added to the top
object
.Ed
.El
.Sh EXAMPLES
To display the WHOIS information for the domain
\'netmeister.org\':
Expand Down
33 changes: 18 additions & 15 deletions doc/jswhois.1.txt
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
jswhois(1) NetBSD General Commands Manual jswhois(1)
jswhois(1) General Commands Manual jswhois(1)

NAME
jswhois -- whois lookup results in json format
jswhois - whois lookup results in json format

SYNOPSIS
jswhois [-QRVlv] [-h host] [-p port] domain
jswhois [-QRVflv] [-h host] [-p port] domain [...]

DESCRIPTION
The jswhois tool performs whois(1) lookups and prints results in JSON
format.

Since the WHOIS protocol notoriously does not include a specification of
the WHOIS data's format or how recursive discovery should be handled, the
results -- much like the results of the normal whois(1) command -- tend
to vary significantly.
results -- much like the results of the normal whois(1) command -- tend to
vary significantly.

OPTIONS
The following options are supported by jswhois:

-Q Do a quick lookup; jswhois will not attempt to follow referrals
to other whois servers. This is the default if a server is
-Q Do a quick lookup; jswhois will not attempt to follow referrals to
other whois servers. This is the default if a server is
explicitly specified via the -h flag. See also the -R option.

-R Do a recursive lookup; jswhois will attempt to follow referrals
to other whois servers. This is the default if -h is not speci-
fied. See also the -Q option.
-R Do a recursive lookup; jswhois will attempt to follow referrals to
other whois servers. This is the default if -h is not specified.
See also the -Q option.

-V Print version information and exit.

-f Force whois lookups even if the given input is not an IP address
and it doesn't resolve as a hostname.

-h host Use the specified host instead of the default (whois.iana.org).

-l Only print results from the last WHOIS server queried.

-p port Connect to the whois server on port. If this option is not spec-
ified, jswhois defaults to port 43.
-p port Connect to the whois server on port. If this option is not
specified, jswhois defaults to port 43.

-v Be verbose. Can be specified multiple times.

Expand All @@ -45,8 +48,8 @@ DETAILS
text output in a coherent JSON object.

The query for any domain will always begin at 'whois.iana.org' and then
recurse as per the data encountered. The resulting JSON document will
then contain nested structures indexed by the name of the WHOIS server in
recurse as per the data encountered. The resulting JSON document will then
contain nested structures indexed by the name of the WHOIS server in
question.

Since the data is fundamentally unstructured, attempts to stuff them into
Expand Down Expand Up @@ -105,4 +108,4 @@ HISTORY
BUGS
Please file bugs and feature requests by emailing the author.

NetBSD 8.0 January 09, 2022 NetBSD 8.0
NetBSD 9.3 May 26, 2025 NetBSD 9.3
23 changes: 22 additions & 1 deletion src/jswhois.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const IANAWHOIS = "whois.iana.org"

var DEFAULT_WHOIS = IANAWHOIS
var PORT = 43
var FORCE = false
var LEAF_ONLY = false
var OUTPUT = map[string]interface{}{}
var RECURSIVE = true
Expand Down Expand Up @@ -1117,6 +1118,8 @@ func getopts() {
case "-V":
printVersion()
os.Exit(EXIT_SUCCESS)
case "-f":
FORCE = true
case "-h":
eatit = true
argcheck("-h", args, i)
Expand Down Expand Up @@ -1175,6 +1178,8 @@ func oneLookup() (rval map[string]interface{}) {

verbose(2, "Looking up %s...", query)

validateQuery(query)

var chain = []string{DEFAULT_WHOIS}
OUTPUT[DEFAULT_WHOIS] = askWhois(DEFAULT_WHOIS, query)

Expand Down Expand Up @@ -1264,11 +1269,12 @@ func updateTopOrSubobject(thing interface{}, k, v string) interface{} {
}

func usage(out io.Writer) {
usage := `Usage: %v [-?QRVlpv] [-h server] [-p port]
usage := `Usage: %v [-?QRVflpv] [-h server] [-p port]
-? print this help and exit
-Q quick lookup (i.e., do not recurse)
-R recursive lookup (default)
-V print version information and exit
-f force lookups
-h server query this server (default: %s)
-l only print output for the last / leaf whois server
-p port query the whois server on this port (default: %d)
Expand All @@ -1277,6 +1283,21 @@ func usage(out io.Writer) {
fmt.Fprintf(out, usage, PROGNAME, IANAWHOIS, PORT)
}

func validateQuery(query string) {
if FORCE {
return
}

verbose(3, "Validating %s...", query)
if ip := net.ParseIP(query); ip != nil {
return
}

if _, err := net.LookupHost(query); err != nil {
fail("%s does not resolve; use '-f' to proceed anyway\n", query)
}
}

func verbose(level int, format string, v ...interface{}) {
if level <= int(VERBOSITY) {
fmt.Fprintf(os.Stderr, "%s ", time.Now().Format("2006-01-02 15:04:05"))
Expand Down

0 comments on commit 18bd02c

Please sign in to comment.