xref: /freebsd/usr.bin/whois/whois.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 6/6/93";
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <netdb.h>
48 #include <stdio.h>
49 
50 #define	NICHOST	"whois.internic.net"
51 
52 main(argc, argv)
53 	int argc;
54 	char **argv;
55 {
56 	extern char *optarg;
57 	extern int optind;
58 	register FILE *sfi, *sfo;
59 	register int ch;
60 	struct sockaddr_in sin;
61 	struct hostent *hp;
62 	struct servent *sp;
63 	int s;
64 	char *host;
65 
66 #ifdef	SOCKS
67 	SOCKSinit(argv[0]);
68 #endif
69 
70 	host = NICHOST;
71 	while ((ch = getopt(argc, argv, "h:")) != -1)
72 		switch((char)ch) {
73 		case 'h':
74 			host = optarg;
75 			break;
76 		case '?':
77 		default:
78 			usage();
79 		}
80 	argc -= optind;
81 	argv += optind;
82 
83 	if (!argc)
84 		usage();
85 
86 	hp = gethostbyname(host);
87 	if (hp == NULL) {
88 		(void)fprintf(stderr, "whois: %s: ", host);
89 		herror((char *)NULL);
90 		exit(1);
91 	}
92 	host = hp->h_name;
93 	s = socket(hp->h_addrtype, SOCK_STREAM, 0);
94 	if (s < 0) {
95 		perror("whois: socket");
96 		exit(1);
97 	}
98 	bzero((caddr_t)&sin, sizeof (sin));
99 	sin.sin_family = hp->h_addrtype;
100 	bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
101 	sp = getservbyname("whois", "tcp");
102 	if (sp == NULL) {
103 		(void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
104 		exit(1);
105 	}
106 	sin.sin_port = sp->s_port;
107 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
108 		perror("whois: connect");
109 		exit(1);
110 	}
111 	sfi = fdopen(s, "r");
112 	sfo = fdopen(s, "w");
113 	if (sfi == NULL || sfo == NULL) {
114 		perror("whois: fdopen");
115 		(void)close(s);
116 		exit(1);
117 	}
118 	while (argc-- > 1)
119 		(void)fprintf(sfo, "%s ", *argv++);
120 	(void)fprintf(sfo, "%s\r\n", *argv);
121 	(void)fflush(sfo);
122 	while ((ch = getc(sfi)) != EOF)
123 		putchar(ch);
124 	exit(0);
125 }
126 
127 usage()
128 {
129 	(void)fprintf(stderr, "usage: whois [-h hostname] name ...\n");
130 	exit(1);
131 }
132