whois.c revision 1.12.4.2 1 1.12.4.2 jhawk /* $NetBSD: whois.c,v 1.12.4.2 2000/09/30 14:07:53 jhawk Exp $ */
2 1.5 jtc
3 1.1 cgd /*
4 1.9 tron * RIPE version marten (at) ripe.net
5 1.9 tron * many changes & networkupdate by david (at) ripe.net
6 1.9 tron * cosmetics by steven (at) dante.org.uk -- gcc stopped complaining mostly,
7 1.9 tron * code is still messy, though.
8 1.9 tron *
9 1.9 tron * 1.15 94/09/07
10 1.9 tron *
11 1.9 tron * 1.2 9705/02
12 1.9 tron * "-v" option added; ambrose (at) ripe.net
13 1.9 tron * "whois.ripe.net" replaced by "bsdbase.ripe.net"; ambrose (at) ripe.net
14 1.9 tron * "bsdbase.ripe.net" replaced by "joshua.ripe.net"; marek (at) ripe.net
15 1.9 tron * "joshua.ripe.net" replaced by "whois.ripe.net"; roman (at) ripe.net 981105
16 1.9 tron *
17 1.9 tron * Copyright (c) 1980 Regents of the University of California.
18 1.9 tron * All rights reserved.
19 1.1 cgd *
20 1.1 cgd * Redistribution and use in source and binary forms, with or without
21 1.1 cgd * modification, are permitted provided that the following conditions
22 1.1 cgd * are met:
23 1.1 cgd * 1. Redistributions of source code must retain the above copyright
24 1.1 cgd * notice, this list of conditions and the following disclaimer.
25 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
26 1.1 cgd * notice, this list of conditions and the following disclaimer in the
27 1.1 cgd * documentation and/or other materials provided with the distribution.
28 1.1 cgd * 3. All advertising materials mentioning features or use of this software
29 1.1 cgd * must display the following acknowledgement:
30 1.1 cgd * This product includes software developed by the University of
31 1.1 cgd * California, Berkeley and its contributors.
32 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
33 1.1 cgd * may be used to endorse or promote products derived from this software
34 1.1 cgd * without specific prior written permission.
35 1.1 cgd *
36 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 1.1 cgd * SUCH DAMAGE.
47 1.1 cgd */
48 1.1 cgd
49 1.6 mrg #include <sys/cdefs.h>
50 1.9 tron #if defined(sun) && defined(solaris)
51 1.9 tron #define SYSV
52 1.9 tron #endif
53 1.9 tron
54 1.1 cgd #ifndef lint
55 1.6 mrg __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
56 1.6 mrg The Regents of the University of California. All rights reserved.\n");
57 1.1 cgd #endif /* not lint */
58 1.1 cgd
59 1.9 tron #ifndef RIPE
60 1.1 cgd #ifndef lint
61 1.5 jtc #if 0
62 1.5 jtc static char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
63 1.6 mrg #else
64 1.12.4.2 jhawk <<<<<<< whois.c
65 1.12.4.2 jhawk __RCSID("$NetBSD: whois.c,v 1.12.4.2 2000/09/30 14:07:53 jhawk Exp $");
66 1.12.4.2 jhawk =======
67 1.12.4.2 jhawk __RCSID("$NetBSD: whois.c,v 1.12.4.2 2000/09/30 14:07:53 jhawk Exp $");
68 1.12.4.2 jhawk >>>>>>> 1.14
69 1.5 jtc #endif
70 1.1 cgd #endif /* not lint */
71 1.9 tron #endif /* not RIPE */
72 1.9 tron
73 1.9 tron #ifdef RIPE
74 1.9 tron #ifndef lint
75 1.9 tron char sccsid[] =
76 1.9 tron "@(#)whois.c 5.11 (Berkeley) 3/2/91 - RIPE 1.15 94/09/07 marten (at) ripe.net";
77 1.9 tron #endif /* not lint */
78 1.9 tron #endif /* RIPE */
79 1.1 cgd
80 1.1 cgd #include <sys/types.h>
81 1.1 cgd #include <sys/socket.h>
82 1.9 tron #include <sys/param.h>
83 1.1 cgd #include <netinet/in.h>
84 1.7 lukem #include <err.h>
85 1.1 cgd #include <netdb.h>
86 1.1 cgd #include <stdio.h>
87 1.4 jtc #include <stdlib.h>
88 1.9 tron #include <ctype.h>
89 1.4 jtc #include <string.h>
90 1.9 tron #include <errno.h>
91 1.4 jtc #include <unistd.h>
92 1.9 tron #include <pwd.h>
93 1.9 tron #include <signal.h>
94 1.9 tron
95 1.9 tron #if defined(SYSV)
96 1.9 tron #include <crypt.h>
97 1.9 tron #endif /* SYSV */
98 1.9 tron
99 1.9 tron #ifndef __NetBSD__
100 1.9 tron #ifdef __STDC__
101 1.9 tron extern int getopt(int argc, char * const *argv, const char *optstring);
102 1.9 tron extern int kill(pid_t pid, int sig);
103 1.9 tron extern FILE *fdopen(int fildes, const char *type);
104 1.9 tron extern int gethostname(char *name, int namelen);
105 1.9 tron #else /* !__STDC__ */
106 1.9 tron extern int gethostname();
107 1.9 tron #endif /* __STDC__ */
108 1.9 tron #endif /* __NetBSD__ */
109 1.9 tron
110 1.9 tron #if defined(SYSV) || defined(__STDC__)
111 1.9 tron
112 1.9 tron #define index(s,c) strchr((const char*)(s),(int)(c))
113 1.9 tron #define rindex(s,c) strrchr((const char*)(s),(int)(c))
114 1.9 tron #define bzero(s,n) memset((void*)s,0,(size_t)n)
115 1.9 tron
116 1.9 tron #ifdef HASMEMMOVE
117 1.9 tron # define bcopy(s,d,n) memmove((void*)(d),(void*)(s),(size_t)(n))
118 1.9 tron #else
119 1.9 tron # define bcopy(s,d,n) memcpy((void*)(d),(void*)(s),(size_t)(n))
120 1.9 tron #endif /* HASMEMMOVE */
121 1.1 cgd
122 1.9 tron #endif /* SYSV || __STDC__ */
123 1.9 tron
124 1.9 tron #ifdef GLIBC
125 1.9 tron typedef __u_short u_short;
126 1.9 tron typedef __caddr_t caddr_t;
127 1.9 tron #endif /* GLIBC */
128 1.9 tron
129 1.9 tron /*
130 1.9 tron
131 1.9 tron # the following defines can be used but are not fully functional anymore...
132 1.9 tron #
133 1.9 tron # CLEVER- Use a educated guess of the whereabouts of the nearest server
134 1.9 tron # This is done by taking the top-level domain of the current
135 1.9 tron # machine, and looking for a CNAME record for a server with name
136 1.9 tron # <top-level-domain>-whois.ripe.net
137 1.9 tron # If this machine does not exsist or the current machine's top-level
138 1.9 tron # domain could not be found,it will fall back to whois.ripe.net
139 1.9 tron # the default for this RIPE version of whois
140 1.9 tron # The CLEVER option implies the RIPE option.
141 1.9 tron
142 1.9 tron # TOPDOMAIN=\"<top-level-domain>\"
143 1.9 tron # - This option will fix the default host to be
144 1.9 tron # <top-level-domain>-whois.ripe.net, which may point to a secondary
145 1.9 tron # server inside your top-level domain. If there is no such secondary
146 1.9 tron # server, it will point to whois.ripe.net, the default. This option
147 1.9 tron # overrules the CLEVER option.
148 1.9 tron # The TOPDOMAIN option implies the RIPE option.
149 1.9 tron
150 1.9 tron */
151 1.9 tron
152 1.9 tron #if defined(TOPDOMAIN) || defined(CLEVER)
153 1.9 tron #ifndef RIPE
154 1.9 tron #define RIPE
155 1.9 tron #endif /* !RIPE */
156 1.9 tron #endif /* TOPDOMAIN || CLEVER */
157 1.9 tron
158 1.9 tron #if defined(RIPE) && !defined(__NetBSD__)
159 1.9 tron #include <sys/param.h>
160 1.9 tron #define NICHOST "whois.ripe.net"
161 1.9 tron #else
162 1.12.4.2 jhawk #define NICHOST "whois.internic.net"
163 1.9 tron #endif
164 1.1 cgd
165 1.6 mrg int main __P((int, char **));
166 1.6 mrg static void usage __P((void));
167 1.9 tron static void closesocket __P((int, int));
168 1.9 tron static void termhandler __P((int));
169 1.9 tron
170 1.9 tron void usage()
171 1.9 tron {
172 1.9 tron #ifdef RIPE
173 1.9 tron #ifdef NETWORKUPDATE
174 1.10 itojun (void)fprintf(stderr, "\nUsage: networkupdate [-46] [-h hostname] [-p port]");
175 1.9 tron #else
176 1.10 itojun (void)fprintf(stderr, "\nUsage: whois [-46aFLmMrSvR] [-h hostname] [-s sources] [-T types] [-i attr] keys\n");
177 1.9 tron (void)fprintf(stderr, " whois -t type");
178 1.9 tron (void)fprintf(stderr, " whois -v type");
179 1.9 tron #endif
180 1.9 tron #else
181 1.10 itojun (void)fprintf(stderr, "\nUsage: whois [-46] [-h hostname] [-p port] name ...");
182 1.9 tron #endif
183 1.9 tron (void)fprintf(stderr, "\n\nWhere:\n\n");
184 1.10 itojun (void)fprintf(stderr, "-4 Use IPv4 Only\n");
185 1.10 itojun (void)fprintf(stderr, "-6 Use IPv6 Only\n");
186 1.9 tron #ifdef RIPE
187 1.9 tron #ifndef NETWORKUPDATE
188 1.9 tron (void)fprintf(stderr, "-a search all databases\n");
189 1.9 tron (void)fprintf(stderr, "-F fast raw output\n");
190 1.9 tron #endif
191 1.9 tron #endif
192 1.9 tron (void)fprintf(stderr, "-h hostname search alternate server\n");
193 1.9 tron #ifdef RIPE
194 1.9 tron #ifndef NETWORKUPDATE
195 1.9 tron (void)fprintf(stderr, "-i [attr][[,attr] ... ] do an inverse lookup for specified attributes\n");
196 1.9 tron (void)fprintf(stderr, "-L find all Less specific matches\n");
197 1.9 tron (void)fprintf(stderr, "-m find first level more specific matches\n");
198 1.9 tron (void)fprintf(stderr, "-M find all More specific matches\n");
199 1.9 tron #endif
200 1.9 tron #endif
201 1.9 tron (void)fprintf(stderr, "-p port port to connect to\n");
202 1.9 tron #ifdef RIPE
203 1.9 tron #ifndef NETWORKUPDATE
204 1.9 tron (void)fprintf(stderr, "-r turn off recursive lookups\n");
205 1.9 tron (void)fprintf(stderr, "-s source[[,source] ... ] search databases with source 'source'\n");
206 1.9 tron (void)fprintf(stderr, "-S tell server to leave out 'syntactic sugar'\n");
207 1.9 tron (void)fprintf(stderr, "-t type requests template for object of type 'type'\n");
208 1.9 tron (void)fprintf(stderr, "-v type requests verbose template for object of type 'type'\n");
209 1.9 tron (void)fprintf(stderr, "-R force to show local copy of the domain object even if it contains referral\n");
210 1.9 tron (void)fprintf(stderr, "-T type[[,type] ... ] only look for objects of type 'type'\n\n");
211 1.9 tron (void)fprintf(stderr, "Please note that most of these flags are NOT understood by\n");
212 1.9 tron (void)fprintf(stderr, "non RIPE whois servers\n");
213 1.9 tron #endif
214 1.9 tron #endif
215 1.9 tron (void)fprintf(stderr, "\n");
216 1.9 tron
217 1.9 tron exit(1);
218 1.9 tron }
219 1.4 jtc
220 1.9 tron int s;
221 1.9 tron
222 1.9 tron void closesocket(s, child)
223 1.9 tron int s, child;
224 1.9 tron {
225 1.9 tron /* printf("close connection child=%i\n", child); */
226 1.9 tron
227 1.9 tron close(s);
228 1.9 tron
229 1.9 tron #ifdef NETWORKUPDATE
230 1.9 tron if (child==0) {
231 1.9 tron kill(getppid(), SIGTERM);
232 1.9 tron }
233 1.9 tron #endif
234 1.9 tron
235 1.9 tron exit(0);
236 1.9 tron
237 1.9 tron }
238 1.9 tron
239 1.9 tron void termhandler(sig)
240 1.9 tron int sig;
241 1.1 cgd {
242 1.9 tron closesocket(s,1);
243 1.9 tron }
244 1.9 tron
245 1.9 tron
246 1.9 tron #ifdef RIPE
247 1.9 tron #if defined(__STDC__) || defined(SYSV)
248 1.9 tron #define occurs(str,pat) ((int) strstr((str),(pat)))
249 1.9 tron #else /* !__STDC__ && !SYSV */
250 1.9 tron int occurs(str, pat)
251 1.9 tron char *str, *pat;
252 1.9 tron {
253 1.9 tron register char *point = str;
254 1.9 tron
255 1.9 tron while ((point=index(point, *pat)))
256 1.9 tron {
257 1.9 tron if (strncmp(point, pat, strlen(pat)) == 0)
258 1.9 tron return(1);
259 1.9 tron point++;
260 1.9 tron }
261 1.9 tron return(0);
262 1.1 cgd }
263 1.9 tron #endif
264 1.9 tron #endif
265 1.1 cgd
266 1.9 tron int main(argc, argv)
267 1.9 tron int argc;
268 1.9 tron char **argv;
269 1.1 cgd {
270 1.9 tron extern char *optarg;
271 1.9 tron extern int optind;
272 1.9 tron FILE *sfi;
273 1.9 tron FILE *sfo;
274 1.9 tron int ch;
275 1.10 itojun struct addrinfo *dst, hints;
276 1.11 itojun int af=PF_UNSPEC;
277 1.10 itojun int error;
278 1.9 tron char *host, *whoishost;
279 1.10 itojun int optp=0;
280 1.10 itojun char *optport="whois";
281 1.10 itojun #ifdef DEBUG
282 1.10 itojun int verb=1;
283 1.10 itojun #else /*DEBUG */
284 1.10 itojun int verb=0;
285 1.10 itojun #endif
286 1.9 tron #ifdef RIPE
287 1.10 itojun int opthost=0;
288 1.9 tron #ifndef NETWORKUPDATE
289 1.9 tron /* normal whois client */
290 1.9 tron char *string;
291 1.9 tron int alldatabases=0;
292 1.9 tron int optsource=0, optrecur=0, optfast=0, opttempl=0, optverbose=0;
293 1.9 tron int optobjtype=0, optsugar=0, optinverselookup=0, optgetupdates=0;
294 1.9 tron int optL=0, optm=0, optM=0, optchanged=0, optnonreferral=0;
295 1.9 tron char *source=NULL, *templ=NULL, *verbose=NULL, *objtype=NULL,
296 1.9 tron *inverselookup=NULL, *getupdates=NULL;
297 1.9 tron #else /* NETWORKUPDATE */
298 1.9 tron /* networkupdate client */
299 1.9 tron int prev;
300 1.9 tron char domainname[64]; /* that's what sys/param.h says */
301 1.9 tron struct passwd *passwdentry;
302 1.9 tron int child;
303 1.9 tron #endif
304 1.9 tron #ifdef CLEVER
305 1.9 tron int myerror;
306 1.9 tron char *mytoplevel;
307 1.9 tron char *myhost;
308 1.9 tron #endif
309 1.9 tron #endif
310 1.9 tron
311 1.9 tron #ifdef TOPDOMAIN
312 1.9 tron host = strcat(TOPDOMAIN, "-whois.ripe.net");
313 1.9 tron #else
314 1.9 tron host = NICHOST;
315 1.9 tron #endif
316 1.9 tron
317 1.9 tron #ifdef RIPE
318 1.9 tron #ifdef NETWORKUPDATE
319 1.10 itojun while ((ch = getopt(argc, argv, "46h:p:")) != EOF)
320 1.9 tron #else
321 1.10 itojun while ((ch = getopt(argc, argv, "46acFg:h:i:LmMp:rs:SRt:T:v:")) != EOF)
322 1.9 tron #endif
323 1.9 tron #else
324 1.10 itojun while ((ch = getopt(argc, argv, "46h:p:")) != EOF)
325 1.9 tron #endif
326 1.9 tron switch((char)ch) {
327 1.10 itojun case '4':
328 1.11 itojun af = PF_INET;
329 1.10 itojun break;
330 1.10 itojun case '6':
331 1.11 itojun af = PF_INET6;
332 1.10 itojun break;
333 1.9 tron case 'h':
334 1.9 tron host = optarg;
335 1.9 tron opthost = 1;
336 1.9 tron break;
337 1.9 tron case 'p':
338 1.10 itojun optport=optarg;
339 1.9 tron optp =1;
340 1.9 tron break;
341 1.9 tron #ifdef RIPE
342 1.9 tron #ifndef NETWORKUPDATE
343 1.9 tron case 'a':
344 1.9 tron alldatabases=1;
345 1.9 tron break;
346 1.9 tron case 'c':
347 1.9 tron optchanged=1;
348 1.9 tron break;
349 1.9 tron case 'F':
350 1.9 tron optfast = 1;
351 1.9 tron break;
352 1.9 tron case 'g':
353 1.9 tron getupdates=optarg;
354 1.9 tron optgetupdates=1;
355 1.9 tron break;
356 1.9 tron case 'i':
357 1.9 tron inverselookup=optarg;
358 1.9 tron optinverselookup = 1;
359 1.9 tron break;
360 1.9 tron case 'L':
361 1.9 tron if (optM || optm) {
362 1.9 tron fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
363 1.9 tron usage();
364 1.9 tron }
365 1.9 tron optL=1;
366 1.9 tron break;
367 1.9 tron case 'm':
368 1.9 tron if (optM || optL) {
369 1.9 tron fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
370 1.9 tron usage();
371 1.9 tron }
372 1.9 tron optm=1;
373 1.9 tron break;
374 1.9 tron case 'M':
375 1.9 tron if (optL || optm) {
376 1.9 tron fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
377 1.9 tron usage();
378 1.9 tron }
379 1.9 tron optM=1;
380 1.9 tron break;
381 1.9 tron
382 1.9 tron case 's':
383 1.9 tron source = optarg;
384 1.9 tron optsource=1;
385 1.9 tron break;
386 1.9 tron case 'S':
387 1.9 tron optsugar=1;
388 1.9 tron break;
389 1.9 tron case 'R':
390 1.9 tron optnonreferral=1;
391 1.9 tron break;
392 1.9 tron case 'r':
393 1.9 tron optrecur=1;
394 1.9 tron break;
395 1.9 tron case 't':
396 1.9 tron templ=optarg;
397 1.9 tron opttempl=1;
398 1.9 tron break;
399 1.9 tron case 'v':
400 1.9 tron verbose=optarg;
401 1.9 tron optverbose=1;
402 1.9 tron break;
403 1.9 tron case 'T':
404 1.9 tron objtype=optarg;
405 1.9 tron optobjtype=1;
406 1.9 tron break;
407 1.9 tron
408 1.9 tron #endif
409 1.9 tron #endif
410 1.9 tron case '?':
411 1.9 tron default:
412 1.9 tron usage();
413 1.9 tron }
414 1.9 tron argc -= optind;
415 1.9 tron argv += optind;
416 1.9 tron
417 1.9 tron #ifdef RIPE
418 1.9 tron #ifdef NETWORKUPDATE
419 1.9 tron if (argc>0)
420 1.9 tron usage();
421 1.9 tron #else
422 1.9 tron if ((argc<=0) && !opttempl && !optverbose && !optgetupdates && (!(opttempl && optgetupdates)))
423 1.9 tron usage();
424 1.9 tron #endif
425 1.9 tron #else
426 1.9 tron if (argc<=0)
427 1.9 tron usage();
428 1.9 tron #endif
429 1.9 tron
430 1.9 tron if (!opthost) {
431 1.9 tron
432 1.9 tron #ifdef CLEVER
433 1.9 tron whoishost=(char *)calloc(MAXHOSTNAMELEN, sizeof(char));
434 1.9 tron myhost =(char *)calloc(MAXHOSTNAMELEN, sizeof(char));
435 1.9 tron myerror = gethostname(myhost, MAXHOSTNAMELEN);
436 1.9 tron if (myerror >= 0) {
437 1.9 tron if (occurs(myhost, ".")) {
438 1.9 tron mytoplevel = rindex(myhost,'.');
439 1.9 tron mytoplevel++;
440 1.9 tron (void) sprintf(whoishost, "%s-whois.ripe.net", mytoplevel);
441 1.9 tron if (verb) fprintf(stderr, "Clever guess: %s\n", whoishost);
442 1.9 tron }
443 1.9 tron }
444 1.10 itojun
445 1.10 itojun memset(&hints, 0, sizeof(hints));
446 1.10 itojun hints.ai_flags = AI_CANONNAME;
447 1.11 itojun hints.ai_family = af;
448 1.10 itojun hints.ai_socktype = SOCK_STREAM;
449 1.10 itojun hints.ai_protocol = 0;
450 1.10 itojun error = getaddrinfo(host, optport, &hints, &dst);
451 1.10 itojun if ((error) && (verb))
452 1.9 tron fprintf(stderr,"No such host: %s\n", whoishost);
453 1.10 itojun if (error) {
454 1.9 tron #endif
455 1.9 tron
456 1.9 tron whoishost=NICHOST;
457 1.9 tron
458 1.11 itojun if (verb)
459 1.11 itojun fprintf(stderr, "Default host: %s\n\n", whoishost);
460 1.10 itojun memset(&hints, 0, sizeof(hints));
461 1.10 itojun hints.ai_flags = AI_CANONNAME;
462 1.11 itojun hints.ai_family = af;
463 1.10 itojun hints.ai_socktype = SOCK_STREAM;
464 1.10 itojun hints.ai_protocol = 0;
465 1.10 itojun error = getaddrinfo(host, optport , &hints, &dst);
466 1.10 itojun if (error) {
467 1.9 tron fprintf(stderr,"No such host: %s\n", whoishost);
468 1.9 tron if (verb) fprintf(stderr, "Now I give up ...\n");
469 1.9 tron perror("Unknown host");
470 1.10 itojun exit(1);
471 1.9 tron }
472 1.9 tron
473 1.9 tron #ifdef CLEVER
474 1.9 tron }
475 1.9 tron #endif
476 1.9 tron }
477 1.9 tron else {
478 1.11 itojun if (verb)
479 1.11 itojun fprintf(stderr, "Trying: %s\n\n", host);
480 1.10 itojun memset(&hints, 0, sizeof(hints));
481 1.10 itojun hints.ai_flags = AI_CANONNAME;
482 1.11 itojun hints.ai_family = af;
483 1.10 itojun hints.ai_socktype = SOCK_STREAM;
484 1.10 itojun hints.ai_protocol = 0;
485 1.10 itojun error = getaddrinfo(host, optport, &hints, &dst);
486 1.10 itojun if (error) {
487 1.9 tron (void)fprintf(stderr, "whois: %s: ", host);
488 1.9 tron perror("Unknown host");
489 1.9 tron exit(1);
490 1.9 tron }
491 1.9 tron }
492 1.9 tron
493 1.11 itojun for (/*nothing*/; dst; dst = dst->ai_next) {
494 1.10 itojun s = socket(dst->ai_family, dst->ai_socktype, dst->ai_protocol);
495 1.11 itojun if (s < 0)
496 1.11 itojun continue;
497 1.10 itojun if (connect(s, dst->ai_addr, dst->ai_addrlen) < 0) {
498 1.11 itojun close(s);
499 1.10 itojun if (verb) (void)fprintf(stderr, "whois: connect miss\n");
500 1.10 itojun continue;
501 1.10 itojun }
502 1.11 itojun /*okay*/
503 1.10 itojun break;
504 1.11 itojun }
505 1.11 itojun if (dst == NULL) {
506 1.11 itojun perror("whois: connect");
507 1.11 itojun exit(1);
508 1.9 tron }
509 1.10 itojun if (verb) (void)fprintf(stderr, "whois: connect success\n");
510 1.9 tron
511 1.9 tron #ifndef NETWORKUPDATE
512 1.9 tron sfi = fdopen(s, "r");
513 1.9 tron sfo = fdopen(s, "w");
514 1.9 tron if (sfi == NULL || sfo == NULL) {
515 1.9 tron perror("whois: fdopen");
516 1.9 tron (void)close(s);
517 1.9 tron exit(1);
518 1.9 tron }
519 1.9 tron #endif
520 1.9 tron
521 1.9 tron signal(SIGTERM, termhandler);
522 1.9 tron
523 1.9 tron #ifdef RIPE
524 1.9 tron #ifdef NETWORKUPDATE
525 1.9 tron
526 1.9 tron if ((child=fork())==0) {
527 1.9 tron
528 1.9 tron sfo = fdopen(s, "w");
529 1.9 tron if (sfo == NULL) {
530 1.9 tron perror("whois: fdopen");
531 1.9 tron (void)close(s);
532 1.9 tron exit(1);
533 1.9 tron }
534 1.9 tron
535 1.9 tron if (gethostname(domainname, sizeof(domainname))) {
536 1.9 tron fprintf(stderr, "error when doing gethostname()");
537 1.9 tron exit(-1);
538 1.9 tron }
539 1.9 tron
540 1.9 tron passwdentry=getpwuid(getuid());
541 1.9 tron
542 1.9 tron fprintf(sfo, "-Vnc2.0 -U %s %s\n", passwdentry->pw_name, domainname);
543 1.9 tron fflush(sfo);
544 1.9 tron
545 1.9 tron prev='\0';
546 1.9 tron
547 1.9 tron while ((ch=getchar()) != EOF) {
548 1.9 tron
549 1.9 tron fputc(ch, sfo);
550 1.9 tron
551 1.9 tron if (ch=='\n') fflush(sfo);
552 1.9 tron if (feof(sfo)) closesocket(s, child);
553 1.9 tron if ((ch=='.') && (prev=='\n')) closesocket(s, child);
554 1.9 tron if (!isspace(ch) || ((!isspace(prev)) && (ch=='\n'))) prev=ch;
555 1.9 tron }
556 1.9 tron
557 1.9 tron closesocket(s, child);
558 1.9 tron
559 1.9 tron }
560 1.9 tron
561 1.9 tron sfi = fdopen(s, "r");
562 1.9 tron if (sfi == NULL) {
563 1.9 tron perror("whois: fdopen");
564 1.9 tron (void)close(s);
565 1.9 tron exit(1);
566 1.9 tron }
567 1.9 tron
568 1.9 tron #else
569 1.9 tron
570 1.9 tron if (alldatabases)
571 1.9 tron (void)fprintf(sfo, "-a ");
572 1.9 tron if (optchanged)
573 1.9 tron (void)fprintf(sfo, "-c ");
574 1.9 tron if (optfast)
575 1.9 tron (void)fprintf(sfo, "-F ");
576 1.9 tron if (optgetupdates)
577 1.9 tron (void)fprintf(sfo, "-g %s ", getupdates);
578 1.9 tron if (optinverselookup)
579 1.9 tron (void)fprintf(sfo, "-i %s ", inverselookup);
580 1.9 tron if (optL)
581 1.9 tron (void)fprintf(sfo, "-L ");
582 1.9 tron if (optm)
583 1.9 tron (void)fprintf(sfo, "-m ");
584 1.9 tron if (optM)
585 1.9 tron (void)fprintf(sfo, "-M ");
586 1.9 tron if (optrecur)
587 1.9 tron (void)fprintf(sfo, "-r ");
588 1.9 tron if (optsource)
589 1.9 tron (void)fprintf(sfo, "-s %s ", source);
590 1.9 tron if (optsugar)
591 1.9 tron (void)fprintf(sfo, "-S ");
592 1.9 tron if (optnonreferral)
593 1.9 tron (void)fprintf(sfo, "-R ");
594 1.9 tron if (opttempl)
595 1.9 tron (void)fprintf(sfo, "-t %s ", templ);
596 1.9 tron if (optverbose)
597 1.9 tron (void)fprintf(sfo, "-v %s ", verbose);
598 1.9 tron if (optobjtype)
599 1.9 tron (void)fprintf(sfo, "-T %s ", objtype);
600 1.9 tron
601 1.9 tron /* we can only send the -V when we are sure that we are dealing with
602 1.9 tron a RIPE whois server :-( */
603 1.9 tron
604 1.9 tron whoishost=(char *)calloc(strlen(host)+1, sizeof(char));
605 1.9 tron strcpy(whoishost, host);
606 1.9 tron for (string=whoishost;(*string=(char)tolower(*string));string++);
607 1.9 tron
608 1.9 tron if (strstr(whoishost, "ripe.net") ||
609 1.9 tron strstr(whoishost, "ra.net") ||
610 1.9 tron strstr(whoishost, "apnic.net") ||
611 1.9 tron strstr(whoishost, "mci.net") ||
612 1.9 tron strstr(whoishost, "isi.edu") ||
613 1.9 tron strstr(whoishost, "garr.it") ||
614 1.9 tron strstr(whoishost, "ans.net") ||
615 1.9 tron alldatabases || optfast || optgetupdates || optinverselookup ||
616 1.9 tron optL || optm || optM || optrecur || optsugar || optsource ||
617 1.9 tron opttempl || optverbose || optobjtype)
618 1.9 tron (void)fprintf(sfo, "-VwC2.0 ");
619 1.9 tron #endif
620 1.9 tron #endif
621 1.9 tron
622 1.9 tron #ifndef NETWORKUPDATE
623 1.9 tron while (argc-- > 1)
624 1.9 tron (void)fprintf(sfo, "%s ", *argv++);
625 1.9 tron if (*argv) (void)fputs(*argv, sfo);
626 1.9 tron (void)fputs("\r\n", sfo);
627 1.9 tron (void)fflush(sfo);
628 1.9 tron #endif
629 1.9 tron
630 1.9 tron while ((ch = getc(sfi)) != EOF)
631 1.9 tron putchar(ch);
632 1.9 tron
633 1.9 tron closesocket(s, 1);
634 1.9 tron
635 1.9 tron exit(0);
636 1.9 tron
637 1.1 cgd }
638