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