]>
Commit | Line | Data |
---|---|---|
b7080c8e A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights | |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.0 (the 'License'). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
13 | * | |
14 | * The Original Code and all software distributed under the License are | |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License." | |
21 | * | |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* | |
25 | * Copyright (c) 1983, 1991, 1993, 1994 | |
26 | * The Regents of the University of California. All rights reserved. | |
27 | * | |
28 | * Redistribution and use in source and binary forms, with or without | |
29 | * modification, are permitted provided that the following conditions | |
30 | * are met: | |
31 | * 1. Redistributions of source code must retain the above copyright | |
32 | * notice, this list of conditions and the following disclaimer. | |
33 | * 2. Redistributions in binary form must reproduce the above copyright | |
34 | * notice, this list of conditions and the following disclaimer in the | |
35 | * documentation and/or other materials provided with the distribution. | |
36 | * 3. All advertising materials mentioning features or use of this software | |
37 | * must display the following acknowledgement: | |
38 | * This product includes software developed by the University of | |
39 | * California, Berkeley and its contributors. | |
40 | * 4. Neither the name of the University nor the names of its contributors | |
41 | * may be used to endorse or promote products derived from this software | |
42 | * without specific prior written permission. | |
43 | * | |
44 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
45 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
47 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
54 | * SUCH DAMAGE. | |
55 | */ | |
56 | ||
57 | #ifndef lint | |
58 | static char copyright[] = | |
59 | "@(#) Copyright (c) 1983, 1991, 1993, 1994\n\ | |
60 | The Regents of the University of California. All rights reserved.\n"; | |
61 | #endif /* not lint */ | |
62 | ||
63 | #ifndef lint | |
64 | static char sccsid[] = "@(#)inetd.c 8.4 (Berkeley) 4/13/94"; | |
65 | #endif /* not lint */ | |
66 | ||
67 | /* | |
68 | * Inetd - Internet super-server | |
69 | * | |
70 | * This program invokes all internet services as needed. Connection-oriented | |
71 | * services are invoked each time a connection is made, by creating a process. | |
72 | * This process is passed the connection as file descriptor 0 and is expected | |
73 | * to do a getpeername to find out the source host and port. | |
74 | * | |
75 | * Datagram oriented services are invoked when a datagram | |
76 | * arrives; a process is created and passed a pending message | |
77 | * on file descriptor 0. Datagram servers may either connect | |
78 | * to their peer, freeing up the original socket for inetd | |
79 | * to receive further messages on, or ``take over the socket'', | |
80 | * processing all arriving datagrams and, eventually, timing | |
81 | * out. The first type of server is said to be ``multi-threaded''; | |
82 | * the second type of server ``single-threaded''. | |
83 | * | |
84 | * Inetd uses a configuration file which is read at startup | |
85 | * and, possibly, at some later time in response to a hangup signal. | |
86 | * The configuration file is ``free format'' with fields given in the | |
87 | * order shown below. Continuation lines for an entry must being with | |
88 | * a space or tab. All fields must be present in each entry. | |
89 | * | |
90 | * service name must be in /etc/services or must | |
91 | * name a tcpmux service | |
92 | * socket type stream/dgram/raw/rdm/seqpacket | |
93 | * protocol must be in /etc/protocols | |
94 | * wait/nowait single-threaded/multi-threaded | |
95 | * user user to run daemon as | |
96 | * server program full path name | |
97 | * server program arguments maximum of MAXARGS (20) | |
98 | * | |
99 | * TCP services without official port numbers are handled with the | |
100 | * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for | |
101 | * requests. When a connection is made from a foreign host, the service | |
102 | * requested is passed to tcpmux, which looks it up in the servtab list | |
103 | * and returns the proper entry for the service. Tcpmux returns a | |
104 | * negative reply if the service doesn't exist, otherwise the invoked | |
105 | * server is expected to return the positive reply if the service type in | |
106 | * inetd.conf file has the prefix "tcpmux/". If the service type has the | |
107 | * prefix "tcpmux/+", tcpmux will return the positive reply for the | |
108 | * process; this is for compatibility with older server code, and also | |
109 | * allows you to invoke programs that use stdin/stdout without putting any | |
110 | * special server code in them. Services that use tcpmux are "nowait" | |
111 | * because they do not have a well-known port and hence cannot listen | |
112 | * for new requests. | |
113 | * | |
114 | * Comment lines are indicated by a `#' in column 1. | |
115 | */ | |
116 | #include <sys/param.h> | |
117 | #include <sys/stat.h> | |
118 | #include <sys/ioctl.h> | |
119 | #include <sys/socket.h> | |
120 | #include <sys/wait.h> | |
121 | #include <sys/time.h> | |
122 | #include <sys/resource.h> | |
123 | ||
124 | #include <netinet/in.h> | |
125 | #include <arpa/inet.h> | |
126 | ||
127 | #include <errno.h> | |
128 | #include <fcntl.h> | |
129 | #include <netdb.h> | |
130 | #include <pwd.h> | |
131 | #include <signal.h> | |
132 | #include <stdio.h> | |
133 | #include <stdlib.h> | |
134 | #include <string.h> | |
135 | #include <syslog.h> | |
136 | #include <unistd.h> | |
137 | ||
138 | #include "pathnames.h" | |
139 | ||
140 | #define TOOMANY 100 /* don't start more than TOOMANY */ | |
141 | #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */ | |
142 | #define RETRYTIME (60*10) /* retry after bind or server fail */ | |
143 | ||
144 | #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM)) | |
145 | ||
146 | ||
147 | int debug = 0; | |
148 | int nsock, maxsock; | |
149 | fd_set allsock; | |
150 | int options; | |
151 | int timingout; | |
152 | int toomany = TOOMANY; | |
153 | struct servent *sp; | |
154 | ||
155 | struct servtab { | |
156 | char *se_service; /* name of service */ | |
157 | int se_socktype; /* type of socket to use */ | |
158 | char *se_proto; /* protocol used */ | |
159 | short se_wait; /* single threaded server */ | |
160 | short se_checked; /* looked at during merge */ | |
161 | char *se_user; /* user name to run as */ | |
162 | struct biltin *se_bi; /* if built-in, description */ | |
163 | char *se_server; /* server program */ | |
164 | #define MAXARGV 20 | |
165 | char *se_argv[MAXARGV+1]; /* program arguments */ | |
166 | int se_fd; /* open descriptor */ | |
167 | int se_type; /* type */ | |
168 | struct sockaddr_in se_ctrladdr;/* bound address */ | |
169 | int se_count; /* number started since se_time */ | |
170 | struct timeval se_time; /* start of se_count */ | |
171 | struct servtab *se_next; | |
172 | } *servtab; | |
173 | ||
174 | #define NORM_TYPE 0 | |
175 | #define MUX_TYPE 1 | |
176 | #define MUXPLUS_TYPE 2 | |
177 | #define ISMUX(sep) (((sep)->se_type == MUX_TYPE) || \ | |
178 | ((sep)->se_type == MUXPLUS_TYPE)) | |
179 | #define ISMUXPLUS(sep) ((sep)->se_type == MUXPLUS_TYPE) | |
180 | ||
181 | ||
182 | void chargen_dg __P((int, struct servtab *)); | |
183 | void chargen_stream __P((int, struct servtab *)); | |
184 | void close_sep __P((struct servtab *)); | |
185 | void config __P((int)); | |
186 | void daytime_dg __P((int, struct servtab *)); | |
187 | void daytime_stream __P((int, struct servtab *)); | |
188 | void discard_dg __P((int, struct servtab *)); | |
189 | void discard_stream __P((int, struct servtab *)); | |
190 | void echo_dg __P((int, struct servtab *)); | |
191 | void echo_stream __P((int, struct servtab *)); | |
192 | void endconfig __P((void)); | |
193 | struct servtab *enter __P((struct servtab *)); | |
194 | void freeconfig __P((struct servtab *)); | |
195 | struct servtab *getconfigent __P((void)); | |
196 | void machtime_dg __P((int, struct servtab *)); | |
197 | void machtime_stream __P((int, struct servtab *)); | |
198 | char *newstr __P((char *)); | |
199 | char *nextline __P((FILE *)); | |
200 | void print_service __P((char *, struct servtab *)); | |
201 | void reapchild __P((int)); | |
202 | void retry __P((int)); | |
203 | int setconfig __P((void)); | |
204 | void setup __P((struct servtab *)); | |
205 | char *sskip __P((char **)); | |
206 | char *skip __P((char **)); | |
207 | struct servtab *tcpmux __P((int)); | |
208 | ||
209 | struct biltin { | |
210 | char *bi_service; /* internally provided service name */ | |
211 | int bi_socktype; /* type of socket supported */ | |
212 | short bi_fork; /* 1 if should fork before call */ | |
213 | short bi_wait; /* 1 if should wait for child */ | |
214 | void (*bi_fn)(); /* function which performs it */ | |
215 | } biltins[] = { | |
216 | /* Echo received data */ | |
217 | { "echo", SOCK_STREAM, 1, 0, echo_stream }, | |
218 | { "echo", SOCK_DGRAM, 0, 0, echo_dg }, | |
219 | ||
220 | /* Internet /dev/null */ | |
221 | { "discard", SOCK_STREAM, 1, 0, discard_stream }, | |
222 | { "discard", SOCK_DGRAM, 0, 0, discard_dg }, | |
223 | ||
224 | /* Return 32 bit time since 1970 */ | |
225 | { "time", SOCK_STREAM, 0, 0, machtime_stream }, | |
226 | { "time", SOCK_DGRAM, 0, 0, machtime_dg }, | |
227 | ||
228 | /* Return human-readable time */ | |
229 | { "daytime", SOCK_STREAM, 0, 0, daytime_stream }, | |
230 | { "daytime", SOCK_DGRAM, 0, 0, daytime_dg }, | |
231 | ||
232 | /* Familiar character generator */ | |
233 | { "chargen", SOCK_STREAM, 1, 0, chargen_stream }, | |
234 | { "chargen", SOCK_DGRAM, 0, 0, chargen_dg }, | |
235 | ||
236 | { "tcpmux", SOCK_STREAM, 1, 0, (void (*)())tcpmux }, | |
237 | ||
238 | { NULL } | |
239 | }; | |
240 | ||
241 | #define NUMINT (sizeof(intab) / sizeof(struct inent)) | |
242 | char *CONFIG = _PATH_INETDCONF; | |
243 | char *pid_file = _PATH_INETDPID; | |
244 | char **Argv; | |
245 | char *LastArg; | |
246 | ||
247 | int | |
248 | main(argc, argv, envp) | |
249 | int argc; | |
250 | char *argv[], *envp[]; | |
251 | { | |
252 | struct servtab *sep; | |
253 | struct passwd *pwd; | |
254 | struct sigvec sv; | |
255 | int tmpint, ch, dofork; | |
256 | pid_t pid; | |
257 | char buf[50]; | |
258 | ||
259 | Argv = argv; | |
260 | if (envp == 0 || *envp == 0) | |
261 | envp = argv; | |
262 | while (*envp) | |
263 | envp++; | |
264 | LastArg = envp[-1] + strlen(envp[-1]); | |
265 | ||
266 | openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON); | |
267 | ||
268 | while ((ch = getopt(argc, argv, "dR:p:")) != EOF) | |
269 | switch(ch) { | |
270 | case 'd': | |
271 | debug = 1; | |
272 | options |= SO_DEBUG; | |
273 | break; | |
274 | case 'R': { /* invocation rate */ | |
275 | char *p; | |
276 | ||
277 | tmpint = strtol(optarg, &p, 0); | |
278 | if (tmpint < 1 || *p) | |
279 | syslog(LOG_ERR, | |
280 | "-R %s: bad value for service invocation rate", | |
281 | optarg); | |
282 | else | |
283 | toomany = tmpint; | |
284 | break; | |
285 | } | |
286 | case 'p': | |
287 | pid_file = optarg; | |
288 | break; | |
289 | case '?': | |
290 | default: | |
291 | syslog(LOG_ERR, | |
292 | "usage: inetd [-d] [-R rate] [conf-file]"); | |
293 | exit(1); | |
294 | } | |
295 | argc -= optind; | |
296 | argv += optind; | |
297 | ||
298 | if (argc > 0) | |
299 | CONFIG = argv[0]; | |
300 | if (debug == 0) { | |
301 | FILE *fp; | |
302 | daemon(0, 0); | |
303 | ||
304 | pid = getpid(); | |
305 | fp = fopen(pid_file, "w"); | |
306 | if( fp ) { | |
307 | fprintf(fp, "%ld\n", (long)pid); | |
308 | fclose(fp); | |
309 | } else { | |
310 | syslog(LOG_WARNING, "%s: %m", pid_file); | |
311 | } | |
312 | } | |
313 | memset(&sv, 0, sizeof(sv)); | |
314 | sv.sv_mask = SIGBLOCK; | |
315 | sv.sv_handler = retry; | |
316 | sigvec(SIGALRM, &sv, (struct sigvec *)0); | |
317 | config(SIGHUP); | |
318 | sv.sv_handler = config; | |
319 | sigvec(SIGHUP, &sv, (struct sigvec *)0); | |
320 | sv.sv_handler = reapchild; | |
321 | sigvec(SIGCHLD, &sv, (struct sigvec *)0); | |
322 | ||
323 | { | |
324 | /* space for daemons to overwrite environment for ps */ | |
325 | #define DUMMYSIZE 100 | |
326 | char dummy[DUMMYSIZE]; | |
327 | ||
328 | (void)memset(dummy, 'x', sizeof(DUMMYSIZE) - 1); | |
329 | dummy[DUMMYSIZE - 1] = '\0'; | |
330 | (void)setenv("inetd_dummy", dummy, 1); | |
331 | } | |
332 | ||
333 | for (;;) { | |
334 | int n, ctrl; | |
335 | fd_set readable; | |
336 | ||
337 | if (nsock == 0) { | |
338 | (void) sigblock(SIGBLOCK); | |
339 | while (nsock == 0) | |
340 | sigpause(0L); | |
341 | (void) sigsetmask(0L); | |
342 | } | |
343 | readable = allsock; | |
344 | if ((n = select(maxsock + 1, &readable, (fd_set *)0, | |
345 | (fd_set *)0, (struct timeval *)0)) <= 0) { | |
346 | if (n < 0 && errno != EINTR) { | |
347 | syslog(LOG_WARNING, "select: %m"); | |
348 | sleep(1); | |
349 | } | |
350 | continue; | |
351 | } | |
352 | for (sep = servtab; n && sep; sep = sep->se_next) | |
353 | if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) { | |
354 | n--; | |
355 | if (debug) | |
356 | fprintf(stderr, "someone wants %s\n", | |
357 | sep->se_service); | |
358 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) { | |
359 | ctrl = accept(sep->se_fd, (struct sockaddr *)0, | |
360 | (int *)0); | |
361 | if (debug) | |
362 | fprintf(stderr, "accept, ctrl %d\n", ctrl); | |
363 | if (ctrl < 0) { | |
364 | if (errno != EINTR) | |
365 | syslog(LOG_WARNING, | |
366 | "accept (for %s): %m", | |
367 | sep->se_service); | |
368 | continue; | |
369 | } | |
370 | /* | |
371 | * Call tcpmux to find the real service to exec. | |
372 | */ | |
373 | if (sep->se_bi && | |
374 | sep->se_bi->bi_fn == (void (*)()) tcpmux) { | |
375 | sep = tcpmux(ctrl); | |
376 | if (sep == NULL) { | |
377 | close(ctrl); | |
378 | continue; | |
379 | } | |
380 | } | |
381 | } else | |
382 | ctrl = sep->se_fd; | |
383 | (void) sigblock(SIGBLOCK); | |
384 | pid = 0; | |
385 | dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork); | |
386 | if (dofork) { | |
387 | if (sep->se_count++ == 0) | |
388 | (void)gettimeofday(&sep->se_time, | |
389 | (struct timezone *)0); | |
390 | else if (sep->se_count >= toomany) { | |
391 | struct timeval now; | |
392 | ||
393 | (void)gettimeofday(&now, (struct timezone *)0); | |
394 | if (now.tv_sec - sep->se_time.tv_sec > | |
395 | CNT_INTVL) { | |
396 | sep->se_time = now; | |
397 | sep->se_count = 1; | |
398 | } else { | |
399 | syslog(LOG_ERR, | |
400 | "%s/%s server failing (looping), service terminated", | |
401 | sep->se_service, sep->se_proto); | |
402 | close_sep(sep); | |
403 | sigsetmask(0L); | |
404 | if (!timingout) { | |
405 | timingout = 1; | |
406 | alarm(RETRYTIME); | |
407 | } | |
408 | continue; | |
409 | } | |
410 | } | |
411 | pid = fork(); | |
412 | } | |
413 | if (pid < 0) { | |
414 | syslog(LOG_ERR, "fork: %m"); | |
415 | if (!sep->se_wait && | |
416 | sep->se_socktype == SOCK_STREAM) | |
417 | close(ctrl); | |
418 | sigsetmask(0L); | |
419 | sleep(1); | |
420 | continue; | |
421 | } | |
422 | if (pid && sep->se_wait) { | |
423 | sep->se_wait = pid; | |
424 | if (sep->se_fd >= 0) { | |
425 | FD_CLR(sep->se_fd, &allsock); | |
426 | nsock--; | |
427 | } | |
428 | } | |
429 | sigsetmask(0L); | |
430 | if (pid == 0) { | |
431 | if (debug && dofork) | |
432 | setsid(); | |
433 | if (dofork) { | |
434 | if (debug) | |
435 | fprintf(stderr, "+ Closing from %d\n", | |
436 | maxsock); | |
437 | for (tmpint = maxsock; tmpint > 2; tmpint--) | |
438 | if (tmpint != ctrl) | |
439 | close(tmpint); | |
440 | } | |
441 | if (sep->se_bi) | |
442 | (*sep->se_bi->bi_fn)(ctrl, sep); | |
443 | else { | |
444 | if (debug) | |
445 | fprintf(stderr, "%d execl %s\n", | |
446 | getpid(), sep->se_server); | |
447 | dup2(ctrl, 0); | |
448 | close(ctrl); | |
449 | dup2(0, 1); | |
450 | dup2(0, 2); | |
451 | if ((pwd = getpwnam(sep->se_user)) == NULL) { | |
452 | syslog(LOG_ERR, | |
453 | "%s/%s: %s: No such user", | |
454 | sep->se_service, sep->se_proto, | |
455 | sep->se_user); | |
456 | if (sep->se_socktype != SOCK_STREAM) | |
457 | recv(0, buf, sizeof (buf), 0); | |
458 | _exit(1); | |
459 | } | |
460 | if (pwd->pw_uid) { | |
461 | if (setgid(pwd->pw_gid) < 0) { | |
462 | syslog(LOG_ERR, | |
463 | "%s: can't set gid %d: %m", | |
464 | sep->se_service, pwd->pw_gid); | |
465 | _exit(1); | |
466 | } | |
467 | (void) initgroups(pwd->pw_name, | |
468 | pwd->pw_gid); | |
469 | if (setuid(pwd->pw_uid) < 0) { | |
470 | syslog(LOG_ERR, | |
471 | "%s: can't set uid %d: %m", | |
472 | sep->se_service, pwd->pw_uid); | |
473 | _exit(1); | |
474 | } | |
475 | } | |
476 | execv(sep->se_server, sep->se_argv); | |
477 | if (sep->se_socktype != SOCK_STREAM) | |
478 | recv(0, buf, sizeof (buf), 0); | |
479 | syslog(LOG_ERR, | |
480 | "cannot execute %s: %m", sep->se_server); | |
481 | _exit(1); | |
482 | } | |
483 | } | |
484 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) | |
485 | close(ctrl); | |
486 | } | |
487 | } | |
488 | } | |
489 | ||
490 | void | |
491 | reapchild(signo) | |
492 | int signo; | |
493 | { | |
494 | int status; | |
495 | pid_t pid; | |
496 | struct servtab *sep; | |
497 | ||
498 | for (;;) { | |
499 | pid = wait3(&status, WNOHANG, (struct rusage *)0); | |
500 | if (pid <= 0) | |
501 | break; | |
502 | if (debug) | |
503 | fprintf(stderr, "%d reaped, status %#x\n", | |
504 | pid, status); | |
505 | for (sep = servtab; sep; sep = sep->se_next) | |
506 | if (sep->se_wait == pid) { | |
507 | if (status) | |
508 | syslog(LOG_WARNING, | |
509 | "%s: exit status 0x%x", | |
510 | sep->se_server, status); | |
511 | if (debug) | |
512 | fprintf(stderr, "restored %s, fd %d\n", | |
513 | sep->se_service, sep->se_fd); | |
514 | FD_SET(sep->se_fd, &allsock); | |
515 | nsock++; | |
516 | sep->se_wait = 1; | |
517 | } | |
518 | } | |
519 | } | |
520 | ||
521 | void | |
522 | config(signo) | |
523 | int signo; | |
524 | { | |
525 | struct servtab *sep, *cp, **sepp; | |
526 | struct passwd *pwd; | |
527 | long omask; | |
528 | ||
529 | if (!setconfig()) { | |
530 | syslog(LOG_ERR, "%s: %m", CONFIG); | |
531 | return; | |
532 | } | |
533 | for (sep = servtab; sep; sep = sep->se_next) | |
534 | sep->se_checked = 0; | |
535 | while (cp = getconfigent()) { | |
536 | if ((pwd = getpwnam(cp->se_user)) == NULL) { | |
537 | syslog(LOG_ERR, | |
538 | "%s/%s: No such user '%s', service ignored", | |
539 | cp->se_service, cp->se_proto, cp->se_user); | |
540 | continue; | |
541 | } | |
542 | for (sep = servtab; sep; sep = sep->se_next) | |
543 | if (strcmp(sep->se_service, cp->se_service) == 0 && | |
544 | strcmp(sep->se_proto, cp->se_proto) == 0) | |
545 | break; | |
546 | if (sep != 0) { | |
547 | int i; | |
548 | ||
549 | omask = sigblock(SIGBLOCK); | |
550 | /* | |
551 | * sep->se_wait may be holding the pid of a daemon | |
552 | * that we're waiting for. If so, don't overwrite | |
553 | * it unless the config file explicitly says don't | |
554 | * wait. | |
555 | */ | |
556 | if (cp->se_bi == 0 && | |
557 | (sep->se_wait == 1 || cp->se_wait == 0)) | |
558 | sep->se_wait = cp->se_wait; | |
559 | #define SWAP(a, b) { char *c = a; a = b; b = c; } | |
560 | if (cp->se_user) | |
561 | SWAP(sep->se_user, cp->se_user); | |
562 | if (cp->se_server) | |
563 | SWAP(sep->se_server, cp->se_server); | |
564 | for (i = 0; i < MAXARGV; i++) | |
565 | SWAP(sep->se_argv[i], cp->se_argv[i]); | |
566 | sigsetmask(omask); | |
567 | freeconfig(cp); | |
568 | if (debug) | |
569 | print_service("REDO", sep); | |
570 | } else { | |
571 | sep = enter(cp); | |
572 | if (debug) | |
573 | print_service("ADD ", sep); | |
574 | } | |
575 | sep->se_checked = 1; | |
576 | if (ISMUX(sep)) { | |
577 | sep->se_fd = -1; | |
578 | continue; | |
579 | } | |
580 | sp = getservbyname(sep->se_service, sep->se_proto); | |
581 | if (sp == 0) { | |
582 | syslog(LOG_ERR, "%s/%s: unknown service", | |
583 | sep->se_service, sep->se_proto); | |
584 | sep->se_checked = 0; | |
585 | continue; | |
586 | } | |
587 | if (sp->s_port != sep->se_ctrladdr.sin_port) { | |
588 | sep->se_ctrladdr.sin_family = AF_INET; | |
589 | sep->se_ctrladdr.sin_port = sp->s_port; | |
590 | if (sep->se_fd >= 0) | |
591 | close_sep(sep); | |
592 | } | |
593 | if (sep->se_fd == -1) | |
594 | setup(sep); | |
595 | } | |
596 | endconfig(); | |
597 | /* | |
598 | * Purge anything not looked at above. | |
599 | */ | |
600 | omask = sigblock(SIGBLOCK); | |
601 | sepp = &servtab; | |
602 | while (sep = *sepp) { | |
603 | if (sep->se_checked) { | |
604 | sepp = &sep->se_next; | |
605 | continue; | |
606 | } | |
607 | *sepp = sep->se_next; | |
608 | if (sep->se_fd >= 0) | |
609 | close_sep(sep); | |
610 | if (debug) | |
611 | print_service("FREE", sep); | |
612 | freeconfig(sep); | |
613 | free((char *)sep); | |
614 | } | |
615 | (void) sigsetmask(omask); | |
616 | } | |
617 | ||
618 | void | |
619 | retry(signo) | |
620 | int signo; | |
621 | { | |
622 | struct servtab *sep; | |
623 | ||
624 | timingout = 0; | |
625 | for (sep = servtab; sep; sep = sep->se_next) | |
626 | if (sep->se_fd == -1) | |
627 | setup(sep); | |
628 | } | |
629 | ||
630 | void | |
631 | setup(sep) | |
632 | struct servtab *sep; | |
633 | { | |
634 | int on = 1; | |
635 | ||
636 | if ((sep->se_fd = socket(AF_INET, sep->se_socktype, 0)) < 0) { | |
637 | if (debug) | |
638 | fprintf(stderr, "socket failed on %s/%s: %s\n", | |
639 | sep->se_service, sep->se_proto, | |
640 | strerror(errno)); | |
641 | syslog(LOG_ERR, "%s/%s: socket: %m", | |
642 | sep->se_service, sep->se_proto); | |
643 | return; | |
644 | } | |
645 | #define turnon(fd, opt) \ | |
646 | setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on)) | |
647 | if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) && | |
648 | turnon(sep->se_fd, SO_DEBUG) < 0) | |
649 | syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); | |
650 | if (turnon(sep->se_fd, SO_REUSEADDR) < 0) | |
651 | syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m"); | |
652 | #undef turnon | |
653 | if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr, | |
654 | sizeof (sep->se_ctrladdr)) < 0) { | |
655 | if (debug) | |
656 | fprintf(stderr, "bind failed on %s/%s: %s\n", | |
657 | sep->se_service, sep->se_proto, | |
658 | strerror(errno)); | |
659 | syslog(LOG_ERR, "%s/%s: bind: %m", | |
660 | sep->se_service, sep->se_proto); | |
661 | (void) close(sep->se_fd); | |
662 | sep->se_fd = -1; | |
663 | if (!timingout) { | |
664 | timingout = 1; | |
665 | alarm(RETRYTIME); | |
666 | } | |
667 | return; | |
668 | } | |
669 | if (sep->se_socktype == SOCK_STREAM) | |
670 | listen(sep->se_fd, 10); | |
671 | FD_SET(sep->se_fd, &allsock); | |
672 | nsock++; | |
673 | if (sep->se_fd > maxsock) | |
674 | maxsock = sep->se_fd; | |
675 | if (debug) { | |
676 | fprintf(stderr, "registered %s on %d\n", | |
677 | sep->se_server, sep->se_fd); | |
678 | } | |
679 | } | |
680 | ||
681 | /* | |
682 | * Finish with a service and its socket. | |
683 | */ | |
684 | void | |
685 | close_sep(sep) | |
686 | struct servtab *sep; | |
687 | { | |
688 | if (sep->se_fd >= 0) { | |
689 | nsock--; | |
690 | FD_CLR(sep->se_fd, &allsock); | |
691 | (void) close(sep->se_fd); | |
692 | sep->se_fd = -1; | |
693 | } | |
694 | sep->se_count = 0; | |
695 | /* | |
696 | * Don't keep the pid of this running deamon: when reapchild() | |
697 | * reaps this pid, it would erroneously increment nsock. | |
698 | */ | |
699 | if (sep->se_wait > 1) | |
700 | sep->se_wait = 1; | |
701 | } | |
702 | ||
703 | struct servtab * | |
704 | enter(cp) | |
705 | struct servtab *cp; | |
706 | { | |
707 | struct servtab *sep; | |
708 | long omask; | |
709 | ||
710 | sep = (struct servtab *)malloc(sizeof (*sep)); | |
711 | if (sep == (struct servtab *)0) { | |
712 | syslog(LOG_ERR, "Out of memory."); | |
713 | exit(-1); | |
714 | } | |
715 | *sep = *cp; | |
716 | sep->se_fd = -1; | |
717 | omask = sigblock(SIGBLOCK); | |
718 | sep->se_next = servtab; | |
719 | servtab = sep; | |
720 | sigsetmask(omask); | |
721 | return (sep); | |
722 | } | |
723 | ||
724 | FILE *fconfig = NULL; | |
725 | struct servtab serv; | |
726 | char line[LINE_MAX]; | |
727 | ||
728 | int | |
729 | setconfig() | |
730 | { | |
731 | ||
732 | if (fconfig != NULL) { | |
733 | fseek(fconfig, 0L, SEEK_SET); | |
734 | return (1); | |
735 | } | |
736 | fconfig = fopen(CONFIG, "r"); | |
737 | return (fconfig != NULL); | |
738 | } | |
739 | ||
740 | void | |
741 | endconfig() | |
742 | { | |
743 | if (fconfig) { | |
744 | (void) fclose(fconfig); | |
745 | fconfig = NULL; | |
746 | } | |
747 | } | |
748 | ||
749 | struct servtab * | |
750 | getconfigent() | |
751 | { | |
752 | struct servtab *sep = &serv; | |
753 | int argc; | |
754 | char *cp, *arg; | |
755 | static char TCPMUX_TOKEN[] = "tcpmux/"; | |
756 | #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1) | |
757 | ||
758 | more: | |
759 | while ((cp = nextline(fconfig)) && (*cp == '#' || *cp == '\0')) | |
760 | ; | |
761 | if (cp == NULL) | |
762 | return ((struct servtab *)0); | |
763 | /* | |
764 | * clear the static buffer, since some fields (se_ctrladdr, | |
765 | * for example) don't get initialized here. | |
766 | */ | |
767 | memset((caddr_t)sep, 0, sizeof *sep); | |
768 | arg = skip(&cp); | |
769 | if (cp == NULL) { | |
770 | /* got an empty line containing just blanks/tabs. */ | |
771 | goto more; | |
772 | } | |
773 | if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) { | |
774 | char *c = arg + MUX_LEN; | |
775 | if (*c == '+') { | |
776 | sep->se_type = MUXPLUS_TYPE; | |
777 | c++; | |
778 | } else | |
779 | sep->se_type = MUX_TYPE; | |
780 | sep->se_service = newstr(c); | |
781 | } else { | |
782 | sep->se_service = newstr(arg); | |
783 | sep->se_type = NORM_TYPE; | |
784 | } | |
785 | arg = sskip(&cp); | |
786 | if (strcmp(arg, "stream") == 0) | |
787 | sep->se_socktype = SOCK_STREAM; | |
788 | else if (strcmp(arg, "dgram") == 0) | |
789 | sep->se_socktype = SOCK_DGRAM; | |
790 | else if (strcmp(arg, "rdm") == 0) | |
791 | sep->se_socktype = SOCK_RDM; | |
792 | else if (strcmp(arg, "seqpacket") == 0) | |
793 | sep->se_socktype = SOCK_SEQPACKET; | |
794 | else if (strcmp(arg, "raw") == 0) | |
795 | sep->se_socktype = SOCK_RAW; | |
796 | else | |
797 | sep->se_socktype = -1; | |
798 | sep->se_proto = newstr(sskip(&cp)); | |
799 | arg = sskip(&cp); | |
800 | sep->se_wait = strcmp(arg, "wait") == 0; | |
801 | if (ISMUX(sep)) { | |
802 | /* | |
803 | * Silently enforce "nowait" for TCPMUX services since | |
804 | * they don't have an assigned port to listen on. | |
805 | */ | |
806 | sep->se_wait = 0; | |
807 | ||
808 | if (strcmp(sep->se_proto, "tcp")) { | |
809 | syslog(LOG_ERR, | |
810 | "%s: bad protocol for tcpmux service %s", | |
811 | CONFIG, sep->se_service); | |
812 | goto more; | |
813 | } | |
814 | if (sep->se_socktype != SOCK_STREAM) { | |
815 | syslog(LOG_ERR, | |
816 | "%s: bad socket type for tcpmux service %s", | |
817 | CONFIG, sep->se_service); | |
818 | goto more; | |
819 | } | |
820 | } | |
821 | sep->se_user = newstr(sskip(&cp)); | |
822 | sep->se_server = newstr(sskip(&cp)); | |
823 | if (strcmp(sep->se_server, "internal") == 0) { | |
824 | struct biltin *bi; | |
825 | ||
826 | for (bi = biltins; bi->bi_service; bi++) | |
827 | if (bi->bi_socktype == sep->se_socktype && | |
828 | strcmp(bi->bi_service, sep->se_service) == 0) | |
829 | break; | |
830 | if (bi->bi_service == 0) { | |
831 | syslog(LOG_ERR, "internal service %s unknown", | |
832 | sep->se_service); | |
833 | goto more; | |
834 | } | |
835 | sep->se_bi = bi; | |
836 | sep->se_wait = bi->bi_wait; | |
837 | } else | |
838 | sep->se_bi = NULL; | |
839 | argc = 0; | |
840 | for (arg = skip(&cp); cp; arg = skip(&cp)) | |
841 | if (argc < MAXARGV) | |
842 | sep->se_argv[argc++] = newstr(arg); | |
843 | while (argc <= MAXARGV) | |
844 | sep->se_argv[argc++] = NULL; | |
845 | return (sep); | |
846 | } | |
847 | ||
848 | void | |
849 | freeconfig(cp) | |
850 | struct servtab *cp; | |
851 | { | |
852 | int i; | |
853 | ||
854 | if (cp->se_service) | |
855 | free(cp->se_service); | |
856 | if (cp->se_proto) | |
857 | free(cp->se_proto); | |
858 | if (cp->se_user) | |
859 | free(cp->se_user); | |
860 | if (cp->se_server) | |
861 | free(cp->se_server); | |
862 | for (i = 0; i < MAXARGV; i++) | |
863 | if (cp->se_argv[i]) | |
864 | free(cp->se_argv[i]); | |
865 | } | |
866 | ||
867 | ||
868 | /* | |
869 | * Safe skip - if skip returns null, log a syntax error in the | |
870 | * configuration file and exit. | |
871 | */ | |
872 | char * | |
873 | sskip(cpp) | |
874 | char **cpp; | |
875 | { | |
876 | char *cp; | |
877 | ||
878 | cp = skip(cpp); | |
879 | if (cp == NULL) { | |
880 | syslog(LOG_ERR, "%s: syntax error", CONFIG); | |
881 | exit(-1); | |
882 | } | |
883 | return (cp); | |
884 | } | |
885 | ||
886 | char * | |
887 | skip(cpp) | |
888 | char **cpp; | |
889 | { | |
890 | char *cp = *cpp; | |
891 | char *start; | |
892 | ||
893 | again: | |
894 | while (*cp == ' ' || *cp == '\t') | |
895 | cp++; | |
896 | if (*cp == '\0') { | |
897 | int c; | |
898 | ||
899 | c = getc(fconfig); | |
900 | (void) ungetc(c, fconfig); | |
901 | if (c == ' ' || c == '\t') | |
902 | if (cp = nextline(fconfig)) | |
903 | goto again; | |
904 | *cpp = (char *)0; | |
905 | return ((char *)0); | |
906 | } | |
907 | start = cp; | |
908 | while (*cp && *cp != ' ' && *cp != '\t') | |
909 | cp++; | |
910 | if (*cp != '\0') | |
911 | *cp++ = '\0'; | |
912 | *cpp = cp; | |
913 | return (start); | |
914 | } | |
915 | ||
916 | char * | |
917 | nextline(fd) | |
918 | FILE *fd; | |
919 | { | |
920 | char *cp; | |
921 | ||
922 | if (fgets(line, sizeof (line), fd) == NULL) | |
923 | return ((char *)0); | |
924 | cp = strchr(line, '\n'); | |
925 | if (cp) | |
926 | *cp = '\0'; | |
927 | return (line); | |
928 | } | |
929 | ||
930 | char * | |
931 | newstr(cp) | |
932 | char *cp; | |
933 | { | |
934 | if (cp = strdup(cp ? cp : "")) | |
935 | return (cp); | |
936 | syslog(LOG_ERR, "strdup: %m"); | |
937 | exit(-1); | |
938 | } | |
939 | ||
940 | void | |
941 | setproctitle(a, s) | |
942 | char *a; | |
943 | int s; | |
944 | { | |
945 | int size; | |
946 | char *cp; | |
947 | struct sockaddr_in sin; | |
948 | char buf[80]; | |
949 | ||
950 | cp = Argv[0]; | |
951 | size = sizeof(sin); | |
952 | if (getpeername(s, (struct sockaddr *)&sin, &size) == 0) | |
953 | (void) sprintf(buf, "-%s [%s]", a, inet_ntoa(sin.sin_addr)); | |
954 | else | |
955 | (void) sprintf(buf, "-%s", a); | |
956 | strncpy(cp, buf, LastArg - cp); | |
957 | cp += strlen(cp); | |
958 | while (cp < LastArg) | |
959 | *cp++ = ' '; | |
960 | } | |
961 | ||
962 | /* | |
963 | * Internet services provided internally by inetd: | |
964 | */ | |
965 | #define BUFSIZE 8192 | |
966 | ||
967 | /* ARGSUSED */ | |
968 | void | |
969 | echo_stream(s, sep) /* Echo service -- echo data back */ | |
970 | int s; | |
971 | struct servtab *sep; | |
972 | { | |
973 | char buffer[BUFSIZE]; | |
974 | int i; | |
975 | ||
976 | setproctitle(sep->se_service, s); | |
977 | while ((i = read(s, buffer, sizeof(buffer))) > 0 && | |
978 | write(s, buffer, i) > 0) | |
979 | ; | |
980 | exit(0); | |
981 | } | |
982 | ||
983 | /* ARGSUSED */ | |
984 | void | |
985 | echo_dg(s, sep) /* Echo service -- echo data back */ | |
986 | int s; | |
987 | struct servtab *sep; | |
988 | { | |
989 | char buffer[BUFSIZE]; | |
990 | int i, size; | |
991 | struct sockaddr sa; | |
992 | ||
993 | size = sizeof(sa); | |
994 | if ((i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size)) < 0) | |
995 | return; | |
996 | (void) sendto(s, buffer, i, 0, &sa, sizeof(sa)); | |
997 | } | |
998 | ||
999 | /* ARGSUSED */ | |
1000 | void | |
1001 | discard_stream(s, sep) /* Discard service -- ignore data */ | |
1002 | int s; | |
1003 | struct servtab *sep; | |
1004 | { | |
1005 | int ret; | |
1006 | char buffer[BUFSIZE]; | |
1007 | ||
1008 | setproctitle(sep->se_service, s); | |
1009 | while (1) { | |
1010 | while ((ret = read(s, buffer, sizeof(buffer))) > 0) | |
1011 | ; | |
1012 | if (ret == 0 || errno != EINTR) | |
1013 | break; | |
1014 | } | |
1015 | exit(0); | |
1016 | } | |
1017 | ||
1018 | /* ARGSUSED */ | |
1019 | void | |
1020 | discard_dg(s, sep) /* Discard service -- ignore data */ | |
1021 | int s; | |
1022 | struct servtab *sep; | |
1023 | { | |
1024 | char buffer[BUFSIZE]; | |
1025 | ||
1026 | (void) read(s, buffer, sizeof(buffer)); | |
1027 | } | |
1028 | ||
1029 | #include <ctype.h> | |
1030 | #define LINESIZ 72 | |
1031 | char ring[128]; | |
1032 | char *endring; | |
1033 | ||
1034 | void | |
1035 | initring() | |
1036 | { | |
1037 | int i; | |
1038 | ||
1039 | endring = ring; | |
1040 | ||
1041 | for (i = 0; i <= 128; ++i) | |
1042 | if (isprint(i)) | |
1043 | *endring++ = i; | |
1044 | } | |
1045 | ||
1046 | /* ARGSUSED */ | |
1047 | void | |
1048 | chargen_stream(s, sep) /* Character generator */ | |
1049 | int s; | |
1050 | struct servtab *sep; | |
1051 | { | |
1052 | int len; | |
1053 | char *rs, text[LINESIZ+2]; | |
1054 | ||
1055 | setproctitle(sep->se_service, s); | |
1056 | ||
1057 | if (!endring) { | |
1058 | initring(); | |
1059 | rs = ring; | |
1060 | } | |
1061 | ||
1062 | text[LINESIZ] = '\r'; | |
1063 | text[LINESIZ + 1] = '\n'; | |
1064 | for (rs = ring;;) { | |
1065 | if ((len = endring - rs) >= LINESIZ) | |
1066 | memmove(text, rs, LINESIZ); | |
1067 | else { | |
1068 | memmove(text, rs, len); | |
1069 | memmove(text + len, ring, LINESIZ - len); | |
1070 | } | |
1071 | if (++rs == endring) | |
1072 | rs = ring; | |
1073 | if (write(s, text, sizeof(text)) != sizeof(text)) | |
1074 | break; | |
1075 | } | |
1076 | exit(0); | |
1077 | } | |
1078 | ||
1079 | /* ARGSUSED */ | |
1080 | void | |
1081 | chargen_dg(s, sep) /* Character generator */ | |
1082 | int s; | |
1083 | struct servtab *sep; | |
1084 | { | |
1085 | struct sockaddr sa; | |
1086 | static char *rs; | |
1087 | int len, size; | |
1088 | char text[LINESIZ+2]; | |
1089 | ||
1090 | if (endring == 0) { | |
1091 | initring(); | |
1092 | rs = ring; | |
1093 | } | |
1094 | ||
1095 | size = sizeof(sa); | |
1096 | if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0) | |
1097 | return; | |
1098 | ||
1099 | if ((len = endring - rs) >= LINESIZ) | |
1100 | memmove(text, rs, LINESIZ); | |
1101 | else { | |
1102 | memmove(text, rs, len); | |
1103 | memmove(text + len, ring, LINESIZ - len); | |
1104 | } | |
1105 | if (++rs == endring) | |
1106 | rs = ring; | |
1107 | text[LINESIZ] = '\r'; | |
1108 | text[LINESIZ + 1] = '\n'; | |
1109 | (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa)); | |
1110 | } | |
1111 | ||
1112 | /* | |
1113 | * Return a machine readable date and time, in the form of the | |
1114 | * number of seconds since midnight, Jan 1, 1900. Since gettimeofday | |
1115 | * returns the number of seconds since midnight, Jan 1, 1970, | |
1116 | * we must add 2208988800 seconds to this figure to make up for | |
1117 | * some seventy years Bell Labs was asleep. | |
1118 | */ | |
1119 | ||
1120 | long | |
1121 | machtime() | |
1122 | { | |
1123 | struct timeval tv; | |
1124 | ||
1125 | if (gettimeofday(&tv, (struct timezone *)0) < 0) { | |
1126 | if (debug) | |
1127 | fprintf(stderr, "Unable to get time of day\n"); | |
1128 | return (0L); | |
1129 | } | |
1130 | #define OFFSET ((u_long)25567 * 24*60*60) | |
1131 | return (htonl((long)(tv.tv_sec + OFFSET))); | |
1132 | #undef OFFSET | |
1133 | } | |
1134 | ||
1135 | /* ARGSUSED */ | |
1136 | void | |
1137 | machtime_stream(s, sep) | |
1138 | int s; | |
1139 | struct servtab *sep; | |
1140 | { | |
1141 | long result; | |
1142 | ||
1143 | result = machtime(); | |
1144 | (void) write(s, (char *) &result, sizeof(result)); | |
1145 | } | |
1146 | ||
1147 | /* ARGSUSED */ | |
1148 | void | |
1149 | machtime_dg(s, sep) | |
1150 | int s; | |
1151 | struct servtab *sep; | |
1152 | { | |
1153 | long result; | |
1154 | struct sockaddr sa; | |
1155 | int size; | |
1156 | ||
1157 | size = sizeof(sa); | |
1158 | if (recvfrom(s, (char *)&result, sizeof(result), 0, &sa, &size) < 0) | |
1159 | return; | |
1160 | result = machtime(); | |
1161 | (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa)); | |
1162 | } | |
1163 | ||
1164 | /* ARGSUSED */ | |
1165 | void | |
1166 | daytime_stream(s, sep) /* Return human-readable time of day */ | |
1167 | int s; | |
1168 | struct servtab *sep; | |
1169 | { | |
1170 | char buffer[256]; | |
1171 | time_t clock; | |
1172 | ||
1173 | clock = time((time_t *) 0); | |
1174 | ||
1175 | (void) sprintf(buffer, "%.24s\r\n", ctime(&clock)); | |
1176 | (void) write(s, buffer, strlen(buffer)); | |
1177 | } | |
1178 | ||
1179 | /* ARGSUSED */ | |
1180 | void | |
1181 | daytime_dg(s, sep) /* Return human-readable time of day */ | |
1182 | int s; | |
1183 | struct servtab *sep; | |
1184 | { | |
1185 | char buffer[256]; | |
1186 | time_t clock; | |
1187 | struct sockaddr sa; | |
1188 | int size; | |
1189 | ||
1190 | clock = time((time_t *) 0); | |
1191 | ||
1192 | size = sizeof(sa); | |
1193 | if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0) | |
1194 | return; | |
1195 | (void) sprintf(buffer, "%.24s\r\n", ctime(&clock)); | |
1196 | (void) sendto(s, buffer, strlen(buffer), 0, &sa, sizeof(sa)); | |
1197 | } | |
1198 | ||
1199 | /* | |
1200 | * print_service: | |
1201 | * Dump relevant information to stderr | |
1202 | */ | |
1203 | void | |
1204 | print_service(action, sep) | |
1205 | char *action; | |
1206 | struct servtab *sep; | |
1207 | { | |
1208 | fprintf(stderr, | |
1209 | "%s: %s proto=%s, wait=%d, user=%s builtin=%x server=%s\n", | |
1210 | action, sep->se_service, sep->se_proto, | |
1211 | sep->se_wait, sep->se_user, (int)sep->se_bi, sep->se_server); | |
1212 | } | |
1213 | ||
1214 | /* | |
1215 | * Based on TCPMUX.C by Mark K. Lottor November 1988 | |
1216 | * sri-nic::ps:<mkl>tcpmux.c | |
1217 | */ | |
1218 | ||
1219 | ||
1220 | static int /* # of characters upto \r,\n or \0 */ | |
1221 | getline(fd, buf, len) | |
1222 | int fd; | |
1223 | char *buf; | |
1224 | int len; | |
1225 | { | |
1226 | int count = 0, n; | |
1227 | ||
1228 | do { | |
1229 | n = read(fd, buf, len-count); | |
1230 | if (n == 0) | |
1231 | return (count); | |
1232 | if (n < 0) | |
1233 | return (-1); | |
1234 | while (--n >= 0) { | |
1235 | if (*buf == '\r' || *buf == '\n' || *buf == '\0') | |
1236 | return (count); | |
1237 | count++; | |
1238 | buf++; | |
1239 | } | |
1240 | } while (count < len); | |
1241 | return (count); | |
1242 | } | |
1243 | ||
1244 | #define MAX_SERV_LEN (256+2) /* 2 bytes for \r\n */ | |
1245 | ||
1246 | #define strwrite(fd, buf) (void) write(fd, buf, sizeof(buf)-1) | |
1247 | ||
1248 | struct servtab * | |
1249 | tcpmux(s) | |
1250 | int s; | |
1251 | { | |
1252 | struct servtab *sep; | |
1253 | char service[MAX_SERV_LEN+1]; | |
1254 | int len; | |
1255 | ||
1256 | /* Get requested service name */ | |
1257 | if ((len = getline(s, service, MAX_SERV_LEN)) < 0) { | |
1258 | strwrite(s, "-Error reading service name\r\n"); | |
1259 | return (NULL); | |
1260 | } | |
1261 | service[len] = '\0'; | |
1262 | ||
1263 | if (debug) | |
1264 | fprintf(stderr, "tcpmux: someone wants %s\n", service); | |
1265 | ||
1266 | /* | |
1267 | * Help is a required command, and lists available services, | |
1268 | * one per line. | |
1269 | */ | |
1270 | if (!strcasecmp(service, "help")) { | |
1271 | for (sep = servtab; sep; sep = sep->se_next) { | |
1272 | if (!ISMUX(sep)) | |
1273 | continue; | |
1274 | (void)write(s,sep->se_service,strlen(sep->se_service)); | |
1275 | strwrite(s, "\r\n"); | |
1276 | } | |
1277 | return (NULL); | |
1278 | } | |
1279 | ||
1280 | /* Try matching a service in inetd.conf with the request */ | |
1281 | for (sep = servtab; sep; sep = sep->se_next) { | |
1282 | if (!ISMUX(sep)) | |
1283 | continue; | |
1284 | if (!strcasecmp(service, sep->se_service)) { | |
1285 | if (ISMUXPLUS(sep)) { | |
1286 | strwrite(s, "+Go\r\n"); | |
1287 | } | |
1288 | return (sep); | |
1289 | } | |
1290 | } | |
1291 | strwrite(s, "-Service not available\r\n"); | |
1292 | return (NULL); | |
1293 | } |