identd.c revision 1.15 1 /* $NetBSD: identd.c,v 1.15 2002/09/23 03:32:35 itojun Exp $ */
2
3 /*
4 ** identd.c A TCP/IP link identification protocol server
5 **
6 ** This program is in the public domain and may be used freely by anyone
7 ** who wants to.
8 **
9 ** Last update: 7 Oct 1993
10 **
11 ** Please send bug fixes/bug reports to: Peter Eriksson <pen (at) lysator.liu.se>
12 */
13
14 #if defined(IRIX) || defined(SVR4) || defined(NeXT) || (defined(sco) && sco >= 42) || defined(_AIX4) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(ultrix)
15 # define SIGRETURN_TYPE void
16 # define SIGRETURN_TYPE_IS_VOID
17 #else
18 # define SIGRETURN_TYPE int
19 #endif
20
21 #ifdef SVR4
22 # define STRNET
23 #endif
24
25 #ifdef NeXT31
26 # include <libc.h>
27 #endif
28
29 #ifdef sco
30 # define USE_SIGALARM
31 #endif
32
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <netdb.h>
37 #include <signal.h>
38 #include <fcntl.h>
39 #include <poll.h>
40
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/ioctl.h>
44 #include <sys/socket.h>
45 #ifndef _AUX_SOURCE
46 # include <sys/file.h>
47 #endif
48 #include <sys/time.h>
49 #include <sys/wait.h>
50
51 #include <pwd.h>
52 #include <grp.h>
53
54 #include <netinet/in.h>
55
56 #ifndef HPUX7
57 # include <arpa/inet.h>
58 #endif
59
60 #if defined(MIPS) || defined(BSD43)
61 extern int errno;
62 #endif
63
64 #if defined(SOLARIS) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(_AIX)
65 # include <unistd.h>
66 # include <stdlib.h>
67 # include <string.h>
68 #endif
69
70 #include "identd.h"
71 #include "error.h"
72 #include "paths.h"
73
74
75 /* Antique unixes do not have these things defined... */
76 #ifndef FD_SETSIZE
77 # define FD_SETSIZE 256
78 #endif
79
80 #ifndef FD_SET
81 # ifndef NFDBITS
82 # define NFDBITS (sizeof(int) * NBBY) /* bits per mask */
83 # endif
84 # define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
85 #endif
86
87 #ifndef FD_ZERO
88 # define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
89 #endif
90
91
92 char *path_unix = (char *) NULL;
93 char *path_kmem = (char *) NULL;
94
95 int verbose_flag = 0;
96 int debug_flag = 0;
97 int syslog_flag = 0;
98 int multi_flag = 0;
99 int other_flag = 0;
100 int unknown_flag = 0;
101 int noident_flag = 0;
102 int crypto_flag = 0;
103 int liar_flag = 0;
104
105 int lport = 0;
106 int fport = 0;
107
108 char *charset_name = (char *) NULL;
109 char *indirect_host = (char *) NULL;
110 char *indirect_password = (char *) NULL;
111 char *lie_string = (char *) NULL;
112
113 #ifdef ALLOW_FORMAT
114 int format_flag = 0;
115 char *format = "%u";
116 #endif
117
118 static int child_pid;
119
120 #ifdef LOG_DAEMON
121 static int syslog_facility = LOG_DAEMON;
122 #endif
123
124 static int comparemem __P((void *, void *, int));
125 char *clearmem __P((void *, int));
126 static SIGRETURN_TYPE child_handler __P((int));
127 int main __P((int, char *[]));
128
129 /*
130 ** The structure passing convention for GCC is incompatible with
131 ** Suns own C compiler, so we define our own inet_ntoa() function.
132 ** (This should only affect GCC version 1 I think, a well, this works
133 ** for version 2 also so why bother.. :-)
134 */
135 #if defined(__GNUC__) && defined(__sparc__) && !defined(NeXT)
136
137 #ifdef inet_ntoa
138 #undef inet_ntoa
139 #endif
140
141 char *inet_ntoa(ad)
142 struct in_addr ad;
143 {
144 unsigned long int s_ad;
145 int a, b, c, d;
146 static char addr[20];
147
148 s_ad = ad.s_addr;
149 d = s_ad % 256;
150 s_ad /= 256;
151 c = s_ad % 256;
152 s_ad /= 256;
153 b = s_ad % 256;
154 a = s_ad / 256;
155 sprintf(addr, "%d.%d.%d.%d", a, b, c, d);
156
157 return addr;
158 }
159 #endif
160
161 static int comparemem(vp1, vp2, len)
162 void *vp1;
163 void *vp2;
164 int len;
165 {
166 unsigned char *p1 = (unsigned char *) vp1;
167 unsigned char *p2 = (unsigned char *) vp2;
168 int c;
169
170 while (len-- > 0)
171 if ((c = (int) *p1++ - (int) *p2++) != 0)
172 return c;
173
174 return 0;
175 }
176
177 /*
178 ** Return the name of the connecting host, or the IP number as a string.
179 */
180 char *gethost(addr)
181 struct in_addr *addr;
182 {
183 int i;
184 struct hostent *hp;
185
186
187 hp = gethostbyaddr((char *) addr, sizeof(struct in_addr), AF_INET);
188 if (hp)
189 {
190 char *hname = strdup(hp->h_name);
191
192 if (! hname) {
193 syslog(LOG_ERR, "strdup(%s): %m", hp->h_name);
194 exit(1);
195 }
196 /* Found a IP -> Name match, now try the reverse for security reasons */
197 hp = gethostbyname(hname);
198 (void) free(hname);
199 if (hp)
200 #ifdef h_addr
201 for (i = 0; hp->h_addr_list[i]; i++)
202 if (comparemem(hp->h_addr_list[i],
203 (unsigned char *) addr,
204 (int) sizeof(struct in_addr)) == 0)
205 return (char *) hp->h_name;
206 #else
207 if (comparemem(hp->h_addr, addr, sizeof(struct in_addr)) == 0)
208 return hp->h_name;
209 #endif
210 }
211
212 return inet_ntoa(*addr);
213 }
214
215 #ifdef USE_SIGALARM
216 /*
217 ** Exit cleanly after our time's up.
218 */
219 static SIGRETURN_TYPE
220 alarm_handler(int s)
221 {
222 if (syslog_flag)
223 syslog(LOG_DEBUG, "SIGALRM triggered, exiting");
224
225 exit(0);
226 }
227 #endif
228
229
230 #if !defined(hpux) && !defined(__hpux) && !defined(SVR4) && \
231 !defined(_CRAY) && !defined(sco) && !defined(LINUX)
232 /*
233 ** This is used to clean up zombie child processes
234 ** if the -w or -b options are used.
235 */
236 static SIGRETURN_TYPE
237 child_handler(dummy)
238 int dummy;
239 {
240 #if defined(NeXT) || (defined(__sgi) && defined(__SVR3))
241 union wait status;
242 #else
243 int status;
244 #endif
245 int saved_errno = errno;
246
247 while (wait3(&status, WNOHANG, NULL) > 0)
248 ;
249
250 errno = saved_errno;
251
252 #ifndef SIGRETURN_TYPE_IS_VOID
253 return 0;
254 #endif
255 }
256 #endif
257
258
259 char *clearmem(vbp, len)
260 void *vbp;
261 int len;
262 {
263 char *bp = (char *) vbp;
264 char *cp;
265
266 cp = bp;
267 while (len-- > 0)
268 *cp++ = 0;
269
270 return bp;
271 }
272
273
274 /*
275 ** Main entry point into this daemon
276 */
277 int main(argc,argv)
278 int argc;
279 char *argv[];
280 {
281 int i, len;
282 struct sockaddr_in sin;
283 struct in_addr laddr, faddr;
284 int one = 1;
285
286 int background_flag = 0;
287 int timeout = 0;
288 char *portno = "113";
289 char *bind_address = (char *) NULL;
290 int set_uid = 0;
291 int set_gid = 0;
292 int inhibit_default_config = 0;
293 int opt_count = 0; /* Count of option flags */
294
295 #ifdef __convex__
296 argc--; /* get rid of extra argument passed by inetd */
297 #endif
298
299
300 if (isatty(0))
301 background_flag = 1;
302
303 /*
304 ** Prescan the arguments for "-f<config-file>" switches
305 */
306 inhibit_default_config = 0;
307 for (i = 1; i < argc && argv[i][0] == '-'; i++)
308 if (argv[i][1] == 'f')
309 inhibit_default_config = 1;
310
311 /*
312 ** Parse the default config file - if it exists
313 */
314 if (!inhibit_default_config)
315 parse_config(NULL, 1);
316
317 /*
318 ** Parse the command line arguments
319 */
320 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
321 opt_count++;
322 switch (argv[i][1])
323 {
324 case 'b': /* Start as standalone daemon */
325 background_flag = 1;
326 break;
327
328 case 'w': /* Start from Inetd, wait mode */
329 background_flag = 2;
330 break;
331
332 case 'i': /* Start from Inetd, nowait mode */
333 background_flag = 0;
334 break;
335
336 case 't':
337 timeout = atoi(argv[i]+2);
338 break;
339
340 case 'p':
341 portno = argv[i]+2;
342 break;
343
344 case 'a':
345 bind_address = argv[i]+2;
346 break;
347
348 case 'u':
349 if (isdigit(argv[i][2]))
350 set_uid = atoi(argv[i]+2);
351 else
352 {
353 struct passwd *pwd;
354
355 pwd = getpwnam(argv[i]+2);
356 if (!pwd)
357 ERROR1("no such user (%s) for -u option", argv[i]+2);
358 else
359 {
360 set_uid = pwd->pw_uid;
361 set_gid = pwd->pw_gid;
362 }
363 }
364 break;
365
366 case 'g':
367 if (isdigit(argv[i][2]))
368 set_gid = atoi(argv[i]+2);
369 else
370 {
371 struct group *grp;
372
373 grp = getgrnam(argv[i]+2);
374 if (!grp)
375 ERROR1("no such group (%s) for -g option", argv[i]+2);
376 else
377 set_gid = grp->gr_gid;
378 }
379 break;
380
381 case 'c':
382 charset_name = argv[i]+2;
383 break;
384
385 case 'r':
386 indirect_host = argv[i]+2;
387 break;
388
389 case 'l': /* Use the Syslog daemon for logging */
390 syslog_flag++;
391 #ifdef LOG_DAEMON
392 openlog("identd", LOG_PID, syslog_facility);
393 #else
394 openlog("identd", LOG_PID);
395 #endif
396 break;
397
398 case 'o':
399 other_flag = 1;
400 break;
401
402 case 'e':
403 unknown_flag = 1;
404 break;
405
406 case 'V': /* Give version of this daemon */
407 printf("[in.identd, version %s]\r\n", version);
408 exit(0);
409 break;
410
411 case 'v': /* Be verbose */
412 verbose_flag++;
413 break;
414
415 case 'd': /* Enable debugging */
416 debug_flag++;
417 break;
418
419 case 'm': /* Enable multiline queries */
420 multi_flag++;
421 break;
422
423 case 'N': /* Enable users ".noident" files */
424 noident_flag++;
425 break;
426
427 #ifdef INCLUDE_CRYPT
428 case 'C': /* Enable encryption. */
429 {
430 FILE *keyfile;
431
432 if (argv[i][2])
433 keyfile = fopen(argv[i]+2, "r");
434 else
435 keyfile = fopen(PATH_DESKEY, "r");
436
437 if (keyfile == NULL)
438 {
439 ERROR("cannot open key file for option -C");
440 }
441 else
442 {
443 char buf[1024];
444
445 if (fgets(buf, 1024, keyfile) == NULL)
446 {
447 ERROR("cannot read key file for option -C");
448 }
449 else
450 {
451 init_encryption(buf);
452 crypto_flag++;
453 }
454 fclose(keyfile);
455 }
456 }
457 break;
458 #endif
459
460 #ifdef ALLOW_FORMAT
461 case 'n': /* Compatibility flag - just send the user number */
462 format_flag = 1;
463 format = "%U";
464 break;
465
466 case 'F': /* Output format */
467 format_flag = 1;
468 format = argv[i]+2;
469 break;
470 #endif
471
472 case 'L': /* lie brazenly */
473 liar_flag = 1;
474 if (*(argv[i]+2) != '\0')
475 lie_string = argv[i]+2;
476 else
477 #ifdef DEFAULT_LIE_USER
478 lie_string = DEFAULT_LIE_USER;
479 #else
480 ERROR("-L specified with no user name");
481 #endif
482 break;
483
484 default:
485 ERROR1("Bad option %s", argv[i]);
486 break;
487 }
488 }
489
490 #if defined(_AUX_SOURCE) || defined (SUNOS35)
491 /* A/UX 2.0* & SunOS 3.5 calls us with an argument XXXXXXXX.YYYY
492 ** where XXXXXXXXX is the hexadecimal version of the callers
493 ** IP number, and YYYY is the port/socket or something.
494 ** It seems to be impossible to pass arguments to a daemon started
495 ** by inetd.
496 **
497 ** Just in case it is started from something else, then we only
498 ** skip the argument if no option flags have been seen.
499 */
500 if (opt_count == 0)
501 argc--;
502 #endif
503
504 /*
505 ** Path to kernel namelist file specified on command line
506 */
507 if (i < argc)
508 path_unix = argv[i++];
509
510 /*
511 ** Path to kernel memory device specified on command line
512 */
513 if (i < argc)
514 path_kmem = argv[i++];
515
516
517 if (i < argc)
518 ERROR1("Too many arguments: ignored from %s", argv[i]);
519
520
521 /*
522 ** We used to call k_open here. But then the file descriptor
523 ** kd->fd open on /dev/kmem is shared by all child processes.
524 ** From the fork(2) man page:
525 ** o The child process has its own copy of the parent's descriptors. These
526 ** descriptors reference the same underlying objects. For instance, file
527 ** pointers in file objects are shared between the child and the parent
528 ** so that an lseek(2) on a descriptor in the child process can affect a
529 ** subsequent read(2) or write(2) by the parent.
530 ** Thus with concurrent (simultaneous) identd client processes,
531 ** they step on each other's toes when they use kvm_read.
532 **
533 ** Calling k_open here was a mistake for another reason too: we
534 ** did not yet honor -u and -g options. Presumably we are
535 ** running as root (unless the in.identd file is setuid), and
536 ** then we can open kmem regardless of -u and -g values.
537 **
538 **
539 ** Open the kernel memory device and read the nlist table
540 **
541 ** if (k_open() < 0)
542 ** ERROR("main: k_open");
543 */
544
545 /*
546 ** Do the special handling needed for the "-b" flag
547 */
548 if (background_flag == 1)
549 {
550 struct sockaddr_in addr;
551 struct servent *sp;
552 int fd;
553
554
555 if (!debug_flag)
556 {
557 if (fork())
558 exit(0);
559
560 close(0);
561 close(1);
562 close(2);
563
564 if (fork())
565 exit(0);
566 }
567
568 fd = socket(AF_INET, SOCK_STREAM, 0);
569 if (fd == -1)
570 ERROR("main: socket");
571
572 if (fd != 0)
573 dup2(fd, 0);
574
575 clearmem((void *) &addr, (int) sizeof(addr));
576
577 addr.sin_family = AF_INET;
578 if (bind_address == (char *) NULL)
579 addr.sin_addr.s_addr = htonl(INADDR_ANY);
580 else
581 {
582 if (isdigit(bind_address[0]))
583 addr.sin_addr.s_addr = inet_addr(bind_address);
584 else
585 {
586 struct hostent *hp;
587
588 hp = gethostbyname(bind_address);
589 if (!hp)
590 ERROR1("no such address (%s) for -a switch", bind_address);
591
592 /* This is ugly, should use memcpy() or bcopy() but... */
593 addr.sin_addr.s_addr = * (unsigned long *) (hp->h_addr);
594 }
595 }
596
597 if (isdigit(portno[0]))
598 addr.sin_port = htons(atoi(portno));
599 else
600 {
601 sp = getservbyname(portno, "tcp");
602 if (sp == (struct servent *) NULL)
603 ERROR1("main: getservbyname: %s", portno);
604 addr.sin_port = sp->s_port;
605 }
606
607 #ifdef SO_REUSEADDR
608 setsockopt(0, SOL_SOCKET, SO_REUSEADDR, (void *) &one, sizeof(one));
609 #endif
610
611 if (bind(0, (struct sockaddr *) &addr, sizeof(addr)) < 0)
612 ERROR("main: bind");
613 }
614
615 if (background_flag)
616 {
617 if (listen(0, 3) < 0)
618 ERROR("main: listen");
619 }
620
621 if (set_gid)
622 {
623 if (setgid(set_gid) == -1)
624 ERROR("main: setgid");
625 /* Call me paranoid... PSz */
626 if (getgid() != set_gid)
627 ERROR2("main: setgid failed: wanted %d, got GID %d", set_gid, getgid());
628 if (getegid() != set_gid)
629 ERROR2("main: setgid failed: wanted %d, got EGID %d", set_gid, getegid());
630 }
631
632 if (set_uid)
633 {
634 if (setuid(set_uid) == -1)
635 ERROR("main: setuid");
636 /* Call me paranoid... PSz */
637 if (getuid() != set_uid)
638 ERROR2("main: setuid failed: wanted %d, got UID %d", set_uid, getuid());
639 if (geteuid() != set_uid)
640 ERROR2("main: setuid failed: wanted %d, got EUID %d", set_uid, geteuid());
641 }
642
643 /*
644 ** Do some special handling if the "-b" or "-w" flags are used
645 */
646 if (background_flag)
647 {
648 int nfds, fd;
649 struct pollfd set[1];
650 struct sockaddr sad;
651 int sadlen;
652
653
654 /*
655 ** Set up the SIGCHLD signal child termination handler so
656 ** that we can avoid zombie processes hanging around and
657 ** handle childs terminating before being able to complete the
658 ** handshake.
659 */
660 #if (defined(SVR4) || defined(hpux) || defined(__hpux) || defined(IRIX) || \
661 defined(_CRAY) || defined(_AUX_SOURCE) || defined(sco) || \
662 defined(LINUX))
663 signal(SIGCHLD, SIG_IGN);
664 #else
665 signal(SIGCHLD, child_handler);
666 #endif
667
668 set[0].fd = 0;
669 set[0].events = POLLIN;
670
671 /*
672 ** Loop and dispatch client handling processes
673 */
674 do
675 {
676 #ifdef USE_SIGALARM
677 /*
678 ** Terminate if we've been idle for 'timeout' seconds
679 */
680 if (background_flag == 2 && timeout)
681 {
682 signal(SIGALRM, alarm_handler);
683 alarm(timeout);
684 }
685 #endif
686
687 /*
688 ** Wait for a connection request to occur.
689 ** Ignore EINTR (Interrupted System Call).
690 */
691 do
692 {
693 #ifndef USE_SIGALARM
694 if (timeout)
695 nfds = poll(set, 1, timeout * 1000);
696 else
697 #endif
698 nfds = poll(set, 1, INFTIM);
699 } while (nfds < 0 && errno == EINTR);
700
701 /*
702 ** An error occurred in select? Just die
703 */
704 if (nfds < 0)
705 ERROR("main: poll");
706
707 /*
708 ** Timeout limit reached. Exit nicely
709 */
710 if (nfds == 0)
711 exit(0);
712
713 #ifdef USE_SIGALARM
714 /*
715 ** Disable the alarm timeout
716 */
717 alarm(0);
718 #endif
719
720 /*
721 ** Accept the new client
722 */
723 sadlen = sizeof(sad);
724 errno = 0;
725 fd = accept(0, &sad, &sadlen);
726 if (fd == -1)
727 ERROR1("main: accept. errno = %d", errno);
728
729 /*
730 ** And fork, then close the fd if we are the parent.
731 */
732 child_pid = fork();
733 } while (child_pid && (close(fd), 1));
734
735 /*
736 ** We are now in child, the parent has returned to "do" above.
737 */
738 if (dup2(fd, 0) == -1)
739 ERROR("main: dup2: failed fd 0");
740
741 if (dup2(fd, 1) == -1)
742 ERROR("main: dup2: failed fd 1");
743
744 if (dup2(fd, 2) == -1)
745 ERROR("main: dup2: failed fd 2");
746 }
747
748 /*
749 ** Get foreign internet address
750 */
751 len = sizeof(sin);
752 if (getpeername(0, (struct sockaddr *) &sin, &len) == -1)
753 {
754 /*
755 ** A user has tried to start us from the command line or
756 ** the network link died, in which case this message won't
757 ** reach to other end anyway, so lets give the poor user some
758 ** errors.
759 */
760 perror("in.identd: getpeername()");
761 exit(1);
762 }
763
764 faddr = sin.sin_addr;
765
766
767 #ifdef STRONG_LOG
768 if (syslog_flag)
769 syslog(LOG_INFO, "Connection from %s", gethost(&faddr));
770 #endif
771
772
773 /*
774 ** Get local internet address
775 */
776 len = sizeof(sin);
777 #ifdef ATTSVR4
778 if (t_getsockname(0, (struct sockaddr *) &sin, &len) == -1)
779 #else
780 if (getsockname(0, (struct sockaddr *) &sin, &len) == -1)
781 #endif
782 {
783 /*
784 ** We can just die here, because if this fails then the
785 ** network has died and we haven't got anyone to return
786 ** errors to.
787 */
788 exit(1);
789 }
790 laddr = sin.sin_addr;
791
792
793 /*
794 ** Get the local/foreign port pair from the luser
795 */
796 parse(stdin, &laddr, &faddr);
797
798 exit(0);
799 }
800