458 lines
12 KiB
C
458 lines
12 KiB
C
/* $AmigaOS: whois_amiga.c,v 1.37 2015/11/21 18:25:49 Exp $ */
|
|
|
|
/*-
|
|
* Copyright (c) 1980, 1993
|
|
* The Regents of the University of California. 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.
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
const char copyright[] =
|
|
"@(#) Copyright (c) 1980, 1993\n"
|
|
"The Regents of the University of California. All rights reserved.\n";
|
|
const char sccsid[] =
|
|
"@(#)whois.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#include "version.h"
|
|
const char *vers = PACKAGE_VERSION_STRING;
|
|
|
|
#include <sys/cdefs.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <ctype.h>
|
|
#include <err.h>
|
|
#include <netdb.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
|
|
#define ABUSEHOST "whois.abuse.net"
|
|
#define NICHOST "whois.crsnic.net"
|
|
#define INICHOST "whois.networksolutions.com"
|
|
#define GNICHOST "whois.nic.gov"
|
|
#define ANICHOST "whois.arin.net"
|
|
#define LNICHOST "whois.lacnic.net"
|
|
#define KNICHOST "whois.krnic.net"
|
|
#define RNICHOST "whois.ripe.net"
|
|
#define PNICHOST "whois.apnic.net"
|
|
#define MNICHOST "whois.ra.net"
|
|
#define QNICHOST_TAIL ".whois-servers.net"
|
|
#define BNICHOST "whois.registro.br"
|
|
#define NORIDHOST "whois.norid.no"
|
|
#define IANAHOST "whois.iana.org"
|
|
#define GERMNICHOST "de.whois-servers.net"
|
|
#define FNICHOST "whois.afrinic.net"
|
|
|
|
#ifdef __AMIGA__
|
|
#define DEFAULT_PORT "whois"
|
|
#endif
|
|
|
|
#ifdef __AROS__
|
|
#define DEFAULT_PORT "43"
|
|
#endif
|
|
|
|
#define WHOIS_SERVER_ID "Whois Server: "
|
|
#define WHOIS_ORG_SERVER_ID "Registrant Street1:Whois Server:"
|
|
|
|
#define WHOIS_RECURSE 0x01
|
|
#define WHOIS_QUICK 0x02
|
|
|
|
#define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
|
|
|
|
static const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST,
|
|
FNICHOST, NULL
|
|
};
|
|
static const char *port = DEFAULT_PORT;
|
|
|
|
static char *choose_server(char *);
|
|
static void usage(void);
|
|
static void whois(const char *, const char *, int);
|
|
|
|
#ifdef __AMIGA__
|
|
int h_errno = 0;
|
|
#endif
|
|
|
|
/****************************************************************************/
|
|
|
|
char *progname_storage = NULL;
|
|
|
|
const char *getprogname(void) {
|
|
return (const char*)progname_storage;
|
|
}
|
|
|
|
void setprogname(const char *progname) {
|
|
if (progname == NULL) {
|
|
return;
|
|
}
|
|
|
|
if (progname_storage != NULL) {
|
|
free(progname_storage);
|
|
}
|
|
|
|
progname_storage = malloc(strlen(progname));
|
|
strcpy(progname_storage, progname);
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
struct Library *DOSBase = NULL;
|
|
struct Library *SocketBase = NULL;
|
|
|
|
void close_libs()
|
|
{
|
|
if (DOSBase != NULL) {
|
|
CloseLibrary(DOSBase);
|
|
DOSBase = NULL;
|
|
}
|
|
|
|
if (SocketBase != NULL) {
|
|
CloseLibrary(SocketBase);
|
|
SocketBase = NULL;
|
|
}
|
|
|
|
if (progname_storage != NULL) {
|
|
free(progname_storage);
|
|
}
|
|
}
|
|
|
|
int open_libs()
|
|
{
|
|
atexit(close_libs);
|
|
InitSemaphore(&GetaddrinfoSemaphore);
|
|
|
|
if(!(DOSBase = OpenLibrary((STRPTR)DOSLIB_NAME, DOSLIB_REV))) {
|
|
warnx2(OPEN_VER_ERROR, DOSLIB_NAME, (void*)DOSLIB_REV);
|
|
return 5;
|
|
}
|
|
|
|
if(!(SocketBase = OpenLibrary((STRPTR)BSDLIB_NAME, BSDLIB_REV))) {
|
|
warnx2(OPEN_VER_ERROR, BSDLIB_NAME, (void*)BSDLIB_REV);
|
|
return 5;
|
|
} else {
|
|
SocketBaseTags(
|
|
SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (IPTR) &errno,
|
|
SBTM_SETVAL(SBTC_HERRNOLONGPTR), (IPTR) &h_errno,
|
|
SBTM_SETVAL(SBTC_LOGTAGPTR), (IPTR) &progname_storage,
|
|
TAG_DONE );
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
const char *country, *host;
|
|
char *qnichost;
|
|
int ch, flags, use_qnichost;
|
|
|
|
if (argc > 0) {
|
|
setprogname(argv[0]);
|
|
open_libs();
|
|
} else {
|
|
return -1;
|
|
}
|
|
|
|
country = host = qnichost = NULL;
|
|
flags = use_qnichost = 0;
|
|
while ((ch = getopt(argc, argv, "aAbc:fgh:iIklmp:QrR6")) != -1) {
|
|
switch (ch) {
|
|
case 'a':
|
|
host = ANICHOST;
|
|
break;
|
|
case 'A':
|
|
host = PNICHOST;
|
|
break;
|
|
case 'b':
|
|
host = ABUSEHOST;
|
|
break;
|
|
case 'c':
|
|
country = optarg;
|
|
break;
|
|
case 'f':
|
|
host = FNICHOST;
|
|
break;
|
|
case 'g':
|
|
host = GNICHOST;
|
|
break;
|
|
case 'h':
|
|
host = optarg;
|
|
break;
|
|
case 'i':
|
|
host = INICHOST;
|
|
break;
|
|
case 'I':
|
|
host = IANAHOST;
|
|
break;
|
|
case 'k':
|
|
host = KNICHOST;
|
|
break;
|
|
case 'l':
|
|
host = LNICHOST;
|
|
break;
|
|
case 'm':
|
|
host = MNICHOST;
|
|
break;
|
|
case 'p':
|
|
port = optarg;
|
|
break;
|
|
case 'Q':
|
|
flags |= WHOIS_QUICK;
|
|
break;
|
|
case 'r':
|
|
host = RNICHOST;
|
|
break;
|
|
case 'R':
|
|
warnx0("-R is deprecated; use '-c ru' instead");
|
|
country = "ru";
|
|
break;
|
|
/* Remove in FreeBSD 10 */
|
|
case '6':
|
|
errx0(EX_USAGE, "-6 is deprecated; use -[aAflr] instead");
|
|
break;
|
|
case '?':
|
|
default:
|
|
usage();
|
|
/* NOTREACHED */
|
|
}
|
|
}
|
|
argc -= optind;
|
|
argv += optind;
|
|
|
|
if (!argc || (country != NULL && host != NULL))
|
|
usage();
|
|
|
|
/*
|
|
* If no host or country is specified determine the top level domain
|
|
* from the query. If the TLD is a number, query ARIN. Otherwise, use
|
|
* TLD.whois-server.net. If the domain does not contain '.', fall
|
|
* back to NICHOST.
|
|
*/
|
|
if (host == NULL && country == NULL) {
|
|
if ((host = getenv("RA_SERVER")) == NULL) {
|
|
use_qnichost = 1;
|
|
host = NICHOST;
|
|
if (!(flags & WHOIS_QUICK))
|
|
flags |= WHOIS_RECURSE;
|
|
}
|
|
}
|
|
|
|
while (argc-- > 0) {
|
|
if (country != NULL) {
|
|
cpy2(&qnichost, country, QNICHOST_TAIL);
|
|
whois(*argv, qnichost, flags);
|
|
} else if (use_qnichost)
|
|
if ((qnichost = choose_server(*argv)) != NULL)
|
|
whois(*argv, qnichost, flags);
|
|
|
|
if (qnichost == NULL)
|
|
whois(*argv, host, flags);
|
|
|
|
free(qnichost);
|
|
qnichost = NULL;
|
|
argv++;
|
|
}
|
|
|
|
exit(0);
|
|
}
|
|
|
|
/*
|
|
* This function will remove any trailing periods from domain, after which it
|
|
* returns a pointer to newly allocated memory containing the whois server to
|
|
* be queried, or a NULL if the correct server couldn't be determined. The
|
|
* caller must remember to free(3) the allocated memory.
|
|
*/
|
|
static char *
|
|
choose_server(char *domain)
|
|
{
|
|
char *pos, *retval;
|
|
|
|
if (strchr(domain, ':')) {
|
|
cpy1(&retval, ANICHOST);
|
|
return (retval);
|
|
}
|
|
|
|
for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';)
|
|
*pos = '\0';
|
|
|
|
if (*domain == '\0')
|
|
errx0(EX_USAGE, "can't search for a null string");
|
|
|
|
if (strlen(domain) > sizeof("-NORID")-1 &&
|
|
strcasecmp(domain + strlen(domain) - sizeof("-NORID") + 1,
|
|
"-NORID") == 0) {
|
|
cpy1(&retval, NORIDHOST);
|
|
return (retval);
|
|
}
|
|
|
|
while (pos > domain && *pos != '.')
|
|
--pos;
|
|
|
|
if (pos <= domain)
|
|
return (NULL);
|
|
|
|
if (isdigit((unsigned char)*++pos))
|
|
cpy1(&retval, ANICHOST);
|
|
else
|
|
cpy2(&retval, pos, QNICHOST_TAIL);
|
|
|
|
return (retval);
|
|
}
|
|
|
|
static void
|
|
whois(const char *query, const char *hostname, int flags)
|
|
{
|
|
const short BUFSIZE = 256;
|
|
char message[BUFSIZE];
|
|
struct addrinfo *hostres;
|
|
char *buf, *host, *nhost, *p, *tbuf;
|
|
size_t c, len;
|
|
int i, s, error;
|
|
const char *reason = NULL;
|
|
struct addrinfo hints, *ai;
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
hints.ai_flags = 0;
|
|
hints.ai_family = AF_UNSPEC;
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
error = getaddrinfo(hostname, port, &hints, &hostres);
|
|
if (error) {
|
|
warnx2("%s: %s", hostname, gai_strerror(error));
|
|
return;
|
|
}
|
|
|
|
for (s = -1, ai = hostres; ai != NULL; ai = ai->ai_next) {
|
|
s = Socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
|
if (s < 0) {
|
|
error = errno;
|
|
reason = "socket";
|
|
continue;
|
|
}
|
|
if (Connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
|
|
error = errno;
|
|
reason = "connect";
|
|
CloseSocket(s);
|
|
s = -1;
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
|
|
if (s < 0) {
|
|
if (reason) {
|
|
errno = error;
|
|
warn2("%s: %s", hostname, reason);
|
|
} else {
|
|
warnx0("Unknown error in connection attempt");
|
|
}
|
|
freeaddrinfo(hostres);
|
|
return;
|
|
}
|
|
|
|
if (strcmp(hostname, GERMNICHOST) == 0) {
|
|
snprintf(message, BUFSIZE, "-T dn,ace -C US-ASCII %s\r\n", query);
|
|
} else if (strcmp(hostname, "dk" QNICHOST_TAIL) == 0) {
|
|
snprintf(message, BUFSIZE, "--show-handles %s\r\n", query);
|
|
} else if (isdigit(query[0])) {
|
|
snprintf(message, BUFSIZE, "n + %s\r\n", query);
|
|
} else {
|
|
snprintf(message, BUFSIZE, "%s\r\n", query);
|
|
}
|
|
|
|
if(Send(s, message, strlen(message), 0) < 0) {
|
|
err0(1, "send failed");
|
|
}
|
|
|
|
nhost = NULL;
|
|
while ((buf = sgetln(s, &len)) != NULL) {
|
|
|
|
while (len > 0 && isspace(buf[len - 1]))
|
|
buf[--len] = '\0';
|
|
|
|
cpy1pad(&tbuf, len, buf);
|
|
printf("%s\n", tbuf);
|
|
free(tbuf);
|
|
|
|
if ((flags & WHOIS_RECURSE) && nhost == NULL) {
|
|
host = strnstr(buf, WHOIS_SERVER_ID, len);
|
|
if (host != NULL) {
|
|
host += sizeof(WHOIS_SERVER_ID) - 1;
|
|
for (p = host; p < buf + len; p++) {
|
|
if (!ishost(*p)) {
|
|
*p = '\0';
|
|
break;
|
|
}
|
|
}
|
|
cpy1pad(&nhost, (int)(buf + len - host), host);
|
|
} else if ((host =
|
|
strnstr(buf, WHOIS_ORG_SERVER_ID, len)) != NULL) {
|
|
host += sizeof(WHOIS_ORG_SERVER_ID) - 1;
|
|
for (p = host; p < buf + len; p++) {
|
|
if (!ishost(*p)) {
|
|
*p = '\0';
|
|
break;
|
|
}
|
|
}
|
|
cpy1pad(&nhost, (int)(buf + len - host), host);
|
|
} else if (strcmp(hostname, ANICHOST) == 0) {
|
|
for (c = 0; c <= len; c++)
|
|
buf[c] = tolower((unsigned char)buf[c]);
|
|
for (i = 0; ip_whois[i] != NULL; i++) {
|
|
if (strnstr(buf, ip_whois[i], len) !=
|
|
NULL) {
|
|
cpy1(&nhost, ip_whois[i]);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
CloseSocket(s);
|
|
|
|
if (nhost != NULL) {
|
|
whois(query, nhost, 0);
|
|
free(nhost);
|
|
}
|
|
}
|
|
|
|
static void
|
|
usage(void)
|
|
{
|
|
fprintf(stderr,
|
|
"usage: whois [-aAbfgiIklmQrR6] [-c country-code | -h hostname] "
|
|
"[-p port] name ...\n");
|
|
exit(EX_USAGE);
|
|
}
|