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