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