Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1983,1991 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | char copyright[] = | |
36 | "@(#) Copyright (c) 1983 Regents of the University of California.\n\ | |
37 | All rights reserved.\n"; | |
38 | #endif /* not lint */ | |
39 | ||
40 | #ifndef lint | |
b41acc45 | 41 | /*static char sccsid[] = "from: @(#)inetd.c 5.30 (Berkeley) 6/3/91";*/ |
a4318d6a | 42 | static char rcsid[] = "$Id: inetd.c,v 1.6 1994/01/14 12:26:19 jkh Exp $"; |
15637ed4 RG |
43 | #endif /* not lint */ |
44 | ||
45 | /* | |
46 | * Inetd - Internet super-server | |
47 | * | |
48 | * This program invokes all internet services as needed. | |
49 | * connection-oriented services are invoked each time a | |
50 | * connection is made, by creating a process. This process | |
51 | * is passed the connection as file descriptor 0 and is | |
52 | * expected to do a getpeername to find out the source host | |
53 | * and port. | |
54 | * | |
55 | * Datagram oriented services are invoked when a datagram | |
56 | * arrives; a process is created and passed a pending message | |
57 | * on file descriptor 0. Datagram servers may either connect | |
58 | * to their peer, freeing up the original socket for inetd | |
59 | * to receive further messages on, or ``take over the socket'', | |
60 | * processing all arriving datagrams and, eventually, timing | |
61 | * out. The first type of server is said to be ``multi-threaded''; | |
62 | * the second type of server ``single-threaded''. | |
63 | * | |
64 | * Inetd uses a configuration file which is read at startup | |
65 | * and, possibly, at some later time in response to a hangup signal. | |
66 | * The configuration file is ``free format'' with fields given in the | |
159f9a1c | 67 | * order shown below. Continuation lines for an entry must begin with |
15637ed4 RG |
68 | * a space or tab. All fields must be present in each entry. |
69 | * | |
70 | * service name must be in /etc/services | |
71 | * socket type stream/dgram/raw/rdm/seqpacket | |
72 | * protocol must be in /etc/protocols | |
73 | * wait/nowait single-threaded/multi-threaded | |
74 | * user user to run daemon as | |
75 | * server program full path name | |
76 | * server program arguments maximum of MAXARGS (20) | |
77 | * | |
b41acc45 C |
78 | * For RPC services |
79 | * service name/version must be in /etc/rpc | |
80 | * socket type stream/dgram/raw/rdm/seqpacket | |
81 | * protocol must be in /etc/protocols | |
82 | * wait/nowait single-threaded/multi-threaded | |
83 | * user user to run daemon as | |
84 | * server program full path name | |
85 | * server program arguments maximum of MAXARGS (20) | |
86 | * | |
15637ed4 RG |
87 | * Comment lines are indicated by a `#' in column 1. |
88 | */ | |
89 | #include <sys/param.h> | |
90 | #include <sys/stat.h> | |
91 | #include <sys/ioctl.h> | |
92 | #include <sys/socket.h> | |
93 | #include <sys/file.h> | |
94 | #include <sys/wait.h> | |
95 | #include <sys/time.h> | |
96 | #include <sys/resource.h> | |
97 | ||
98 | #include <netinet/in.h> | |
99 | #include <arpa/inet.h> | |
100 | ||
101 | #include <errno.h> | |
102 | #include <signal.h> | |
103 | #include <netdb.h> | |
104 | #include <syslog.h> | |
105 | #include <pwd.h> | |
106 | #include <stdio.h> | |
159f9a1c RG |
107 | #include <stdlib.h> |
108 | #include <unistd.h> | |
15637ed4 | 109 | #include <string.h> |
b41acc45 | 110 | #include <rpc/rpc.h> |
15637ed4 RG |
111 | #include "pathnames.h" |
112 | ||
a4318d6a | 113 | #define TOOMANY 256 /* don't start more than TOOMANY */ |
15637ed4 RG |
114 | #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */ |
115 | #define RETRYTIME (60*10) /* retry after bind or server fail */ | |
116 | ||
117 | #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM)) | |
118 | ||
15637ed4 | 119 | int debug = 0; |
159f9a1c | 120 | int log = 0; |
15637ed4 RG |
121 | int nsock, maxsock; |
122 | fd_set allsock; | |
123 | int options; | |
124 | int timingout; | |
125 | struct servent *sp; | |
b41acc45 | 126 | struct rpcent *rpc; |
15637ed4 RG |
127 | |
128 | struct servtab { | |
129 | char *se_service; /* name of service */ | |
130 | int se_socktype; /* type of socket to use */ | |
131 | char *se_proto; /* protocol used */ | |
132 | short se_wait; /* single threaded server */ | |
133 | short se_checked; /* looked at during merge */ | |
134 | char *se_user; /* user name to run as */ | |
135 | struct biltin *se_bi; /* if built-in, description */ | |
136 | char *se_server; /* server program */ | |
137 | #define MAXARGV 20 | |
138 | char *se_argv[MAXARGV+1]; /* program arguments */ | |
139 | int se_fd; /* open descriptor */ | |
140 | struct sockaddr_in se_ctrladdr;/* bound address */ | |
b41acc45 C |
141 | int se_rpc; /* ==1 if this is an RPC service */ |
142 | int se_rpc_prog; /* RPC program number */ | |
143 | u_int se_rpc_lowvers; /* RPC low version */ | |
144 | u_int se_rpc_highvers; /* RPC high version */ | |
15637ed4 RG |
145 | int se_count; /* number started since se_time */ |
146 | struct timeval se_time; /* start of se_count */ | |
147 | struct servtab *se_next; | |
148 | } *servtab; | |
149 | ||
159f9a1c RG |
150 | /* Externals */ |
151 | ||
152 | /* should be in <rpc/rpc.h> ?? */ | |
153 | int pmap_unset(u_long prognum,u_long versnum); | |
154 | int pmap_set(u_long prognum,u_long versnum,int protocol,u_short port); | |
155 | ||
156 | /* comes from -lutil ?? */ | |
157 | void daemon(); | |
568ac9e5 JH |
158 | char * config_open(const char *filename,int contlines); |
159 | void config_close(); | |
160 | char * config_next(); | |
161 | char * config_skip(char **p); | |
159f9a1c RG |
162 | |
163 | /* Prototypes */ | |
164 | void reapchild(); | |
165 | void config(); | |
166 | void unregisterrpc(struct servtab *sep); | |
167 | void retry(); | |
168 | void setup(struct servtab *sep); | |
169 | struct servtab * enter(struct servtab *cp); | |
159f9a1c RG |
170 | struct servtab * getconfigent(); |
171 | void freeconfig(struct servtab *cp); | |
159f9a1c RG |
172 | char * newstr(char *cp); |
173 | void setproctitle(char *a, int s); | |
174 | void echo_stream(int s,struct servtab *sep); | |
175 | void echo_dg(int s,struct servtab *sep); | |
176 | void discard_stream(int s,struct servtab *sep); | |
177 | void discard_dg(int s,struct servtab *sep); | |
178 | void initring(); | |
179 | void chargen_stream(int s,struct servtab *sep); | |
180 | void chargen_dg(int s,struct servtab *sep); | |
181 | long machtime(); | |
182 | void machtime_stream(int s,struct servtab *sep); | |
183 | void machtime_dg(int s,struct servtab *sep); | |
184 | void daytime_stream(int s,struct servtab *sep); | |
185 | void daytime_dg(int s,struct servtab *sep); | |
186 | void print_service(char *action, struct servtab *sep); | |
15637ed4 RG |
187 | |
188 | struct biltin { | |
189 | char *bi_service; /* internally provided service name */ | |
190 | int bi_socktype; /* type of socket supported */ | |
191 | short bi_fork; /* 1 if should fork before call */ | |
192 | short bi_wait; /* 1 if should wait for child */ | |
159f9a1c | 193 | void (*bi_fn)(); /* function which performs it */ |
15637ed4 RG |
194 | } biltins[] = { |
195 | /* Echo received data */ | |
159f9a1c RG |
196 | { "echo", SOCK_STREAM, 1, 0, echo_stream }, |
197 | { "echo", SOCK_DGRAM, 0, 0, echo_dg }, | |
15637ed4 RG |
198 | |
199 | /* Internet /dev/null */ | |
159f9a1c RG |
200 | { "discard", SOCK_STREAM, 1, 0, discard_stream }, |
201 | { "discard", SOCK_DGRAM, 0, 0, discard_dg }, | |
15637ed4 RG |
202 | |
203 | /* Return 32 bit time since 1970 */ | |
159f9a1c RG |
204 | { "time", SOCK_STREAM, 0, 0, machtime_stream }, |
205 | { "time", SOCK_DGRAM, 0, 0, machtime_dg }, | |
15637ed4 RG |
206 | |
207 | /* Return human-readable time */ | |
159f9a1c RG |
208 | { "daytime", SOCK_STREAM, 0, 0, daytime_stream }, |
209 | { "daytime", SOCK_DGRAM, 0, 0, daytime_dg }, | |
15637ed4 RG |
210 | |
211 | /* Familiar character generator */ | |
159f9a1c RG |
212 | { "chargen", SOCK_STREAM, 1, 0, chargen_stream }, |
213 | { "chargen", SOCK_DGRAM, 0, 0, chargen_dg }, | |
214 | { 0 } | |
15637ed4 RG |
215 | }; |
216 | ||
217 | #define NUMINT (sizeof(intab) / sizeof(struct inent)) | |
218 | char *CONFIG = _PATH_INETDCONF; | |
219 | char **Argv; | |
220 | char *LastArg; | |
221 | ||
159f9a1c RG |
222 | int |
223 | main(int argc, char **argv, char **envp) | |
15637ed4 | 224 | { |
159f9a1c RG |
225 | struct servtab *sep; |
226 | struct passwd *pwd; | |
227 | int tmpint; | |
15637ed4 RG |
228 | struct sigvec sv; |
229 | int ch, pid, dofork; | |
230 | char buf[50]; | |
159f9a1c RG |
231 | struct sockaddr_in peer; |
232 | int i; | |
15637ed4 RG |
233 | |
234 | Argv = argv; | |
235 | if (envp == 0 || *envp == 0) | |
236 | envp = argv; | |
237 | while (*envp) | |
238 | envp++; | |
239 | LastArg = envp[-1] + strlen(envp[-1]); | |
240 | ||
159f9a1c | 241 | while ((ch = getopt(argc, argv, "dl")) != EOF) |
15637ed4 RG |
242 | switch(ch) { |
243 | case 'd': | |
244 | debug = 1; | |
245 | options |= SO_DEBUG; | |
246 | break; | |
159f9a1c RG |
247 | case 'l': |
248 | log = 1; | |
249 | break; | |
15637ed4 RG |
250 | case '?': |
251 | default: | |
568ac9e5 | 252 | fprintf(stderr, "usage: inetd [-dl]"); |
15637ed4 RG |
253 | exit(1); |
254 | } | |
255 | argc -= optind; | |
256 | argv += optind; | |
257 | ||
258 | if (argc > 0) | |
259 | CONFIG = argv[0]; | |
260 | if (debug == 0) | |
261 | daemon(0, 0); | |
262 | openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON); | |
263 | bzero((char *)&sv, sizeof(sv)); | |
264 | sv.sv_mask = SIGBLOCK; | |
265 | sv.sv_handler = retry; | |
266 | sigvec(SIGALRM, &sv, (struct sigvec *)0); | |
267 | config(); | |
268 | sv.sv_handler = config; | |
269 | sigvec(SIGHUP, &sv, (struct sigvec *)0); | |
270 | sv.sv_handler = reapchild; | |
271 | sigvec(SIGCHLD, &sv, (struct sigvec *)0); | |
272 | ||
273 | { | |
274 | /* space for daemons to overwrite environment for ps */ | |
275 | #define DUMMYSIZE 100 | |
276 | char dummy[DUMMYSIZE]; | |
277 | ||
278 | (void)memset(dummy, 'x', sizeof(DUMMYSIZE) - 1); | |
279 | dummy[DUMMYSIZE - 1] = '\0'; | |
280 | (void)setenv("inetd_dummy", dummy, 1); | |
281 | } | |
282 | ||
283 | for (;;) { | |
284 | int n, ctrl; | |
285 | fd_set readable; | |
286 | ||
287 | if (nsock == 0) { | |
288 | (void) sigblock(SIGBLOCK); | |
289 | while (nsock == 0) | |
290 | sigpause(0L); | |
291 | (void) sigsetmask(0L); | |
292 | } | |
293 | readable = allsock; | |
294 | if ((n = select(maxsock + 1, &readable, (fd_set *)0, | |
295 | (fd_set *)0, (struct timeval *)0)) <= 0) { | |
296 | if (n < 0 && errno != EINTR) | |
297 | syslog(LOG_WARNING, "select: %m\n"); | |
298 | sleep(1); | |
299 | continue; | |
300 | } | |
301 | for (sep = servtab; n && sep; sep = sep->se_next) | |
302 | if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) { | |
303 | n--; | |
304 | if (debug) | |
305 | fprintf(stderr, "someone wants %s\n", | |
306 | sep->se_service); | |
307 | if (sep->se_socktype == SOCK_STREAM) { | |
308 | ctrl = accept(sep->se_fd, (struct sockaddr *)0, | |
309 | (int *)0); | |
310 | if (debug) | |
311 | fprintf(stderr, "accept, ctrl %d\n", ctrl); | |
312 | if (ctrl < 0) { | |
313 | if (errno == EINTR) | |
314 | continue; | |
315 | syslog(LOG_WARNING, "accept (for %s): %m", | |
316 | sep->se_service); | |
317 | continue; | |
318 | } | |
159f9a1c RG |
319 | if(log) { |
320 | i = sizeof peer; | |
321 | if(getpeername(ctrl,(struct sockaddr *)&peer,&i)) { | |
322 | syslog(LOG_WARNING, | |
323 | "getpeername(for %s): %m", | |
324 | sep->se_service); | |
325 | continue; | |
326 | } | |
327 | syslog(LOG_INFO,"%s from %s", | |
328 | sep->se_service, | |
329 | inet_ntoa(peer.sin_addr)); | |
330 | ||
331 | } | |
15637ed4 RG |
332 | } else |
333 | ctrl = sep->se_fd; | |
334 | (void) sigblock(SIGBLOCK); | |
335 | pid = 0; | |
336 | dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork); | |
337 | if (dofork) { | |
338 | if (sep->se_count++ == 0) | |
339 | (void)gettimeofday(&sep->se_time, | |
340 | (struct timezone *)0); | |
341 | else if (sep->se_count >= TOOMANY) { | |
342 | struct timeval now; | |
343 | ||
344 | (void)gettimeofday(&now, (struct timezone *)0); | |
345 | if (now.tv_sec - sep->se_time.tv_sec > | |
346 | CNT_INTVL) { | |
347 | sep->se_time = now; | |
348 | sep->se_count = 1; | |
349 | } else { | |
350 | syslog(LOG_ERR, | |
351 | "%s/%s server failing (looping), service terminated\n", | |
352 | sep->se_service, sep->se_proto); | |
353 | FD_CLR(sep->se_fd, &allsock); | |
354 | (void) close(sep->se_fd); | |
355 | sep->se_fd = -1; | |
356 | sep->se_count = 0; | |
357 | nsock--; | |
358 | if (!timingout) { | |
359 | timingout = 1; | |
360 | alarm(RETRYTIME); | |
361 | } | |
a4318d6a GW |
362 | /* |
363 | * DON'T fork! | |
364 | */ | |
365 | sigsetmask(0L); | |
366 | continue; | |
15637ed4 RG |
367 | } |
368 | } | |
369 | pid = fork(); | |
370 | } | |
371 | if (pid < 0) { | |
372 | syslog(LOG_ERR, "fork: %m"); | |
373 | if (sep->se_socktype == SOCK_STREAM) | |
374 | close(ctrl); | |
375 | sigsetmask(0L); | |
376 | sleep(1); | |
377 | continue; | |
378 | } | |
379 | if (pid && sep->se_wait) { | |
380 | sep->se_wait = pid; | |
381 | if (sep->se_fd >= 0) { | |
382 | FD_CLR(sep->se_fd, &allsock); | |
383 | nsock--; | |
384 | } | |
385 | } | |
386 | sigsetmask(0L); | |
387 | if (pid == 0) { | |
388 | if (debug && dofork) | |
389 | setsid(); | |
390 | if (dofork) | |
391 | for (tmpint = maxsock; --tmpint > 2; ) | |
392 | if (tmpint != ctrl) | |
393 | close(tmpint); | |
394 | if (sep->se_bi) | |
395 | (*sep->se_bi->bi_fn)(ctrl, sep); | |
396 | else { | |
397 | if (debug) | |
398 | fprintf(stderr, "%d execl %s\n", | |
399 | getpid(), sep->se_server); | |
400 | dup2(ctrl, 0); | |
401 | close(ctrl); | |
402 | dup2(0, 1); | |
403 | dup2(0, 2); | |
404 | if ((pwd = getpwnam(sep->se_user)) == NULL) { | |
405 | syslog(LOG_ERR, | |
406 | "getpwnam: %s: No such user", | |
407 | sep->se_user); | |
408 | if (sep->se_socktype != SOCK_STREAM) | |
409 | recv(0, buf, sizeof (buf), 0); | |
410 | _exit(1); | |
411 | } | |
412 | if (pwd->pw_uid) { | |
413 | (void) setgid((gid_t)pwd->pw_gid); | |
414 | initgroups(pwd->pw_name, pwd->pw_gid); | |
415 | (void) setuid((uid_t)pwd->pw_uid); | |
416 | } | |
417 | execv(sep->se_server, sep->se_argv); | |
418 | if (sep->se_socktype != SOCK_STREAM) | |
419 | recv(0, buf, sizeof (buf), 0); | |
420 | syslog(LOG_ERR, "execv %s: %m", sep->se_server); | |
421 | _exit(1); | |
422 | } | |
423 | } | |
424 | if (sep->se_socktype == SOCK_STREAM) | |
425 | close(ctrl); | |
426 | } | |
427 | } | |
428 | } | |
429 | ||
430 | void | |
431 | reapchild() | |
432 | { | |
433 | int status; | |
434 | int pid; | |
159f9a1c | 435 | struct servtab *sep; |
15637ed4 RG |
436 | |
437 | for (;;) { | |
438 | pid = wait3(&status, WNOHANG, (struct rusage *)0); | |
439 | if (pid <= 0) | |
440 | break; | |
441 | if (debug) | |
442 | fprintf(stderr, "%d reaped\n", pid); | |
443 | for (sep = servtab; sep; sep = sep->se_next) | |
444 | if (sep->se_wait == pid) { | |
445 | if (status) | |
446 | syslog(LOG_WARNING, | |
447 | "%s: exit status 0x%x", | |
448 | sep->se_server, status); | |
449 | if (debug) | |
450 | fprintf(stderr, "restored %s, fd %d\n", | |
451 | sep->se_service, sep->se_fd); | |
452 | FD_SET(sep->se_fd, &allsock); | |
453 | nsock++; | |
454 | sep->se_wait = 1; | |
455 | } | |
456 | } | |
457 | } | |
458 | ||
459 | void | |
460 | config() | |
461 | { | |
159f9a1c | 462 | struct servtab *sep, *cp, **sepp; |
15637ed4 | 463 | long omask; |
568ac9e5 | 464 | char *p; |
15637ed4 | 465 | |
568ac9e5 JH |
466 | p = config_open(CONFIG,1); /* 1 means allow continuation lines */ |
467 | if(p) { | |
468 | syslog(LOG_ERR, "%s on %s: %m", p, CONFIG); | |
15637ed4 RG |
469 | return; |
470 | } | |
471 | for (sep = servtab; sep; sep = sep->se_next) | |
472 | sep->se_checked = 0; | |
159f9a1c | 473 | while ((cp = getconfigent())) { |
15637ed4 RG |
474 | for (sep = servtab; sep; sep = sep->se_next) |
475 | if (strcmp(sep->se_service, cp->se_service) == 0 && | |
476 | strcmp(sep->se_proto, cp->se_proto) == 0) | |
477 | break; | |
478 | if (sep != 0) { | |
479 | int i; | |
480 | ||
481 | omask = sigblock(SIGBLOCK); | |
482 | /* | |
483 | * sep->se_wait may be holding the pid of a daemon | |
484 | * that we're waiting for. If so, don't overwrite | |
485 | * it unless the config file explicitly says don't | |
486 | * wait. | |
487 | */ | |
488 | if (cp->se_bi == 0 && | |
489 | (sep->se_wait == 1 || cp->se_wait == 0)) | |
490 | sep->se_wait = cp->se_wait; | |
491 | #define SWAP(a, b) { char *c = a; a = b; b = c; } | |
492 | if (cp->se_user) | |
493 | SWAP(sep->se_user, cp->se_user); | |
494 | if (cp->se_server) | |
495 | SWAP(sep->se_server, cp->se_server); | |
496 | for (i = 0; i < MAXARGV; i++) | |
497 | SWAP(sep->se_argv[i], cp->se_argv[i]); | |
498 | sigsetmask(omask); | |
499 | freeconfig(cp); | |
500 | if (debug) | |
501 | print_service("REDO", sep); | |
502 | } else { | |
503 | sep = enter(cp); | |
504 | if (debug) | |
505 | print_service("ADD ", sep); | |
506 | } | |
507 | sep->se_checked = 1; | |
b41acc45 C |
508 | if (!sep->se_rpc) { |
509 | sp = getservbyname(sep->se_service, sep->se_proto); | |
510 | if (sp == 0) { | |
511 | syslog(LOG_ERR, "%s/%s: unknown service", | |
512 | sep->se_service, sep->se_proto); | |
513 | if (sep->se_fd != -1) | |
514 | (void) close(sep->se_fd); | |
515 | sep->se_fd = -1; | |
516 | continue; | |
517 | } | |
518 | if (sp->s_port != sep->se_ctrladdr.sin_port) { | |
519 | sep->se_ctrladdr.sin_port = sp->s_port; | |
520 | if (sep->se_fd != -1) | |
521 | (void) close(sep->se_fd); | |
522 | sep->se_fd = -1; | |
523 | } | |
15637ed4 | 524 | } |
b41acc45 C |
525 | else { |
526 | rpc = getrpcbyname(sep->se_service); | |
527 | if (rpc == 0) { | |
528 | syslog(LOG_ERR, "%s/%s unknown RPC service.", | |
529 | sep->se_service, sep->se_proto); | |
530 | if (sep->se_fd != -1) | |
531 | (void) close(sep->se_fd); | |
532 | sep->se_fd = -1; | |
533 | continue; | |
534 | } | |
535 | if (rpc->r_number != sep->se_rpc_prog) { | |
536 | if (sep->se_rpc_prog) | |
537 | unregisterrpc(sep); | |
538 | sep->se_rpc_prog = rpc->r_number; | |
539 | if (sep->se_fd != -1) | |
540 | (void) close(sep->se_fd); | |
541 | sep->se_fd = -1; | |
542 | } | |
543 | } | |
544 | ||
15637ed4 RG |
545 | if (sep->se_fd == -1) |
546 | setup(sep); | |
547 | } | |
568ac9e5 | 548 | config_close(); |
15637ed4 RG |
549 | /* |
550 | * Purge anything not looked at above. | |
551 | */ | |
552 | omask = sigblock(SIGBLOCK); | |
553 | sepp = &servtab; | |
159f9a1c | 554 | while ((sep = *sepp)) { |
15637ed4 RG |
555 | if (sep->se_checked) { |
556 | sepp = &sep->se_next; | |
557 | continue; | |
558 | } | |
559 | *sepp = sep->se_next; | |
560 | if (sep->se_fd != -1) { | |
561 | FD_CLR(sep->se_fd, &allsock); | |
562 | nsock--; | |
563 | (void) close(sep->se_fd); | |
564 | } | |
565 | if (debug) | |
566 | print_service("FREE", sep); | |
b41acc45 C |
567 | if (sep->se_rpc && sep->se_rpc_prog > 0) |
568 | unregisterrpc(sep); | |
15637ed4 RG |
569 | freeconfig(sep); |
570 | free((char *)sep); | |
571 | } | |
572 | (void) sigsetmask(omask); | |
573 | } | |
574 | ||
b41acc45 | 575 | void |
159f9a1c | 576 | unregisterrpc(struct servtab *sep) |
b41acc45 C |
577 | { |
578 | int i; | |
579 | struct servtab *sepp; | |
580 | long omask; | |
581 | ||
582 | omask = sigblock(SIGBLOCK); | |
583 | for (sepp = servtab; sepp; sepp = sepp->se_next) { | |
584 | if (sepp == sep) | |
585 | continue; | |
586 | if (sep->se_checked == 0 || | |
587 | !sepp->se_rpc || | |
588 | sep->se_rpc_prog != sepp->se_rpc_prog) | |
589 | continue; | |
590 | return; | |
591 | } | |
592 | if (debug) | |
593 | print_service("UNREG", sep); | |
594 | for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) | |
595 | pmap_unset(sep->se_rpc_prog, i); | |
596 | if (sep->se_fd != -1) | |
597 | (void) close(sep->se_fd); | |
598 | sep->se_fd = -1; | |
599 | (void) sigsetmask(omask); | |
600 | } | |
601 | ||
15637ed4 RG |
602 | void |
603 | retry() | |
604 | { | |
159f9a1c | 605 | struct servtab *sep; |
15637ed4 RG |
606 | |
607 | timingout = 0; | |
608 | for (sep = servtab; sep; sep = sep->se_next) | |
609 | if (sep->se_fd == -1) | |
610 | setup(sep); | |
611 | } | |
612 | ||
159f9a1c RG |
613 | void |
614 | setup(struct servtab *sep) | |
15637ed4 RG |
615 | { |
616 | int on = 1; | |
617 | ||
618 | if ((sep->se_fd = socket(AF_INET, sep->se_socktype, 0)) < 0) { | |
619 | syslog(LOG_ERR, "%s/%s: socket: %m", | |
620 | sep->se_service, sep->se_proto); | |
621 | return; | |
622 | } | |
623 | #define turnon(fd, opt) \ | |
624 | setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on)) | |
625 | if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) && | |
626 | turnon(sep->se_fd, SO_DEBUG) < 0) | |
627 | syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); | |
628 | if (turnon(sep->se_fd, SO_REUSEADDR) < 0) | |
629 | syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m"); | |
630 | #undef turnon | |
631 | if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr, | |
632 | sizeof (sep->se_ctrladdr)) < 0) { | |
633 | syslog(LOG_ERR, "%s/%s: bind: %m", | |
634 | sep->se_service, sep->se_proto); | |
635 | (void) close(sep->se_fd); | |
636 | sep->se_fd = -1; | |
637 | if (!timingout) { | |
638 | timingout = 1; | |
639 | alarm(RETRYTIME); | |
640 | } | |
641 | return; | |
642 | } | |
b41acc45 | 643 | if (sep->se_rpc) { |
278a761d JH |
644 | int i, len = sizeof(struct sockaddr); |
645 | ||
b41acc45 C |
646 | if (getsockname(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr, |
647 | &len) < 0) { | |
648 | syslog(LOG_ERR, "%s/%s: getsockname: %m", | |
649 | sep->se_service, sep->se_proto); | |
650 | (void) close(sep->se_fd); | |
651 | sep->se_fd = -1; | |
652 | return; | |
653 | } | |
654 | if (debug) | |
655 | print_service("REG ", sep); | |
656 | for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) { | |
657 | pmap_unset(sep->se_rpc_prog, i); | |
658 | pmap_set(sep->se_rpc_prog, i, | |
659 | (sep->se_socktype == SOCK_DGRAM) | |
660 | ? IPPROTO_UDP : IPPROTO_TCP, | |
661 | ntohs(sep->se_ctrladdr.sin_port)); | |
662 | } | |
663 | ||
664 | } | |
15637ed4 RG |
665 | if (sep->se_socktype == SOCK_STREAM) |
666 | listen(sep->se_fd, 10); | |
667 | FD_SET(sep->se_fd, &allsock); | |
668 | nsock++; | |
669 | if (sep->se_fd > maxsock) | |
670 | maxsock = sep->se_fd; | |
671 | } | |
672 | ||
673 | struct servtab * | |
159f9a1c | 674 | enter(struct servtab *cp) |
15637ed4 | 675 | { |
159f9a1c | 676 | struct servtab *sep; |
15637ed4 RG |
677 | long omask; |
678 | ||
679 | sep = (struct servtab *)malloc(sizeof (*sep)); | |
680 | if (sep == (struct servtab *)0) { | |
681 | syslog(LOG_ERR, "Out of memory."); | |
682 | exit(-1); | |
683 | } | |
684 | *sep = *cp; | |
685 | sep->se_fd = -1; | |
686 | omask = sigblock(SIGBLOCK); | |
687 | sep->se_next = servtab; | |
688 | servtab = sep; | |
689 | sigsetmask(omask); | |
690 | return (sep); | |
691 | } | |
692 | ||
15637ed4 | 693 | struct servtab serv; |
15637ed4 RG |
694 | |
695 | struct servtab * | |
696 | getconfigent() | |
697 | { | |
159f9a1c | 698 | struct servtab *sep = &serv; |
15637ed4 | 699 | int argc; |
159f9a1c | 700 | char *cp, *arg; |
b41acc45 C |
701 | char *versp; |
702 | ||
568ac9e5 JH |
703 | more: |
704 | cp = config_next(); | |
705 | if(!cp) | |
706 | return 0; | |
707 | fprintf(stderr,"<%s>\n",cp); | |
708 | sep->se_service = newstr(config_skip(&cp)); | |
709 | arg = config_skip(&cp); | |
15637ed4 RG |
710 | if (strcmp(arg, "stream") == 0) |
711 | sep->se_socktype = SOCK_STREAM; | |
712 | else if (strcmp(arg, "dgram") == 0) | |
713 | sep->se_socktype = SOCK_DGRAM; | |
714 | else if (strcmp(arg, "rdm") == 0) | |
715 | sep->se_socktype = SOCK_RDM; | |
716 | else if (strcmp(arg, "seqpacket") == 0) | |
717 | sep->se_socktype = SOCK_SEQPACKET; | |
718 | else if (strcmp(arg, "raw") == 0) | |
719 | sep->se_socktype = SOCK_RAW; | |
720 | else | |
721 | sep->se_socktype = -1; | |
568ac9e5 | 722 | sep->se_proto = newstr(config_skip(&cp)); |
b41acc45 C |
723 | sep->se_rpc = 0; |
724 | if (strncmp(sep->se_proto, "rpc/", 4) == 0) { | |
725 | sep->se_proto += 4; | |
726 | sep->se_rpc = 1; | |
727 | sep->se_rpc_prog = sep->se_rpc_lowvers = sep->se_rpc_lowvers = 0; | |
728 | sep->se_ctrladdr.sin_family = AF_INET; | |
729 | sep->se_ctrladdr.sin_port = 0; | |
730 | sep->se_ctrladdr.sin_addr.s_addr = htonl(INADDR_ANY); | |
159f9a1c | 731 | if ((versp = rindex(sep->se_service, '/'))) { |
b41acc45 C |
732 | *versp++ = '\0'; |
733 | switch (sscanf(versp, "%d-%d", | |
734 | &sep->se_rpc_lowvers, | |
735 | &sep->se_rpc_highvers)) { | |
736 | case 2: | |
737 | break; | |
738 | case 1: | |
739 | sep->se_rpc_highvers = | |
740 | sep->se_rpc_lowvers; | |
741 | break; | |
742 | default: | |
743 | syslog(LOG_ERR, "bad RPC version specifier; %s\n", sep->se_service); | |
744 | freeconfig(sep); | |
745 | goto more; | |
746 | } | |
747 | } | |
748 | else { | |
749 | sep->se_rpc_lowvers = | |
750 | sep->se_rpc_highvers = 1; | |
751 | } | |
752 | } | |
568ac9e5 JH |
753 | arg = config_skip(&cp); |
754 | if(!strcmp(arg,"wait")) | |
755 | sep->se_wait = 1; | |
756 | else if(!strcmp(arg,"nowait")) | |
757 | sep->se_wait = 0; | |
758 | else { | |
759 | syslog(LOG_ERR, "should be wait or nowait: %s unknown\n", arg); | |
760 | goto more; | |
761 | } | |
762 | ||
763 | sep->se_user = newstr(config_skip(&cp)); | |
764 | sep->se_server = newstr(config_skip(&cp)); | |
15637ed4 | 765 | if (strcmp(sep->se_server, "internal") == 0) { |
159f9a1c | 766 | struct biltin *bi; |
15637ed4 RG |
767 | |
768 | for (bi = biltins; bi->bi_service; bi++) | |
769 | if (bi->bi_socktype == sep->se_socktype && | |
770 | strcmp(bi->bi_service, sep->se_service) == 0) | |
771 | break; | |
772 | if (bi->bi_service == 0) { | |
773 | syslog(LOG_ERR, "internal service %s unknown\n", | |
774 | sep->se_service); | |
775 | goto more; | |
776 | } | |
777 | sep->se_bi = bi; | |
778 | sep->se_wait = bi->bi_wait; | |
779 | } else | |
780 | sep->se_bi = NULL; | |
781 | argc = 0; | |
568ac9e5 | 782 | for (arg = config_skip(&cp); arg; arg = config_skip(&cp)) |
15637ed4 RG |
783 | if (argc < MAXARGV) |
784 | sep->se_argv[argc++] = newstr(arg); | |
785 | while (argc <= MAXARGV) | |
786 | sep->se_argv[argc++] = NULL; | |
787 | return (sep); | |
788 | } | |
789 | ||
159f9a1c RG |
790 | void |
791 | freeconfig(struct servtab *cp) | |
15637ed4 RG |
792 | { |
793 | int i; | |
794 | ||
795 | if (cp->se_service) | |
796 | free(cp->se_service); | |
797 | if (cp->se_proto) | |
798 | free(cp->se_proto); | |
799 | if (cp->se_user) | |
800 | free(cp->se_user); | |
801 | if (cp->se_server) | |
802 | free(cp->se_server); | |
803 | for (i = 0; i < MAXARGV; i++) | |
804 | if (cp->se_argv[i]) | |
805 | free(cp->se_argv[i]); | |
806 | } | |
807 | ||
15637ed4 | 808 | char * |
159f9a1c | 809 | newstr(char *cp) |
15637ed4 | 810 | { |
159f9a1c | 811 | if ((cp = strdup(cp ? cp : ""))) |
15637ed4 RG |
812 | return(cp); |
813 | syslog(LOG_ERR, "strdup: %m"); | |
814 | exit(-1); | |
815 | } | |
816 | ||
159f9a1c RG |
817 | void |
818 | setproctitle(char *a, int s) | |
15637ed4 RG |
819 | { |
820 | int size; | |
159f9a1c | 821 | char *cp; |
15637ed4 RG |
822 | struct sockaddr_in sin; |
823 | char buf[80]; | |
824 | ||
825 | cp = Argv[0]; | |
826 | size = sizeof(sin); | |
827 | if (getpeername(s, (struct sockaddr *)&sin, &size) == 0) | |
828 | (void) sprintf(buf, "-%s [%s]", a, inet_ntoa(sin.sin_addr)); | |
829 | else | |
830 | (void) sprintf(buf, "-%s", a); | |
831 | strncpy(cp, buf, LastArg - cp); | |
832 | cp += strlen(cp); | |
833 | while (cp < LastArg) | |
834 | *cp++ = ' '; | |
835 | } | |
836 | ||
837 | /* | |
838 | * Internet services provided internally by inetd: | |
839 | */ | |
840 | #define BUFSIZE 8192 | |
841 | ||
842 | /* ARGSUSED */ | |
159f9a1c RG |
843 | void |
844 | echo_stream(int s,struct servtab *sep) /* Echo service -- echo data back */ | |
15637ed4 RG |
845 | { |
846 | char buffer[BUFSIZE]; | |
847 | int i; | |
848 | ||
849 | setproctitle(sep->se_service, s); | |
850 | while ((i = read(s, buffer, sizeof(buffer))) > 0 && | |
159f9a1c | 851 | write(s, buffer, i) == i) |
15637ed4 RG |
852 | ; |
853 | exit(0); | |
854 | } | |
855 | ||
856 | /* ARGSUSED */ | |
159f9a1c RG |
857 | void |
858 | echo_dg(int s,struct servtab *sep) /* Echo service -- echo data back */ | |
15637ed4 RG |
859 | { |
860 | char buffer[BUFSIZE]; | |
861 | int i, size; | |
862 | struct sockaddr sa; | |
863 | ||
864 | size = sizeof(sa); | |
865 | if ((i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size)) < 0) | |
866 | return; | |
867 | (void) sendto(s, buffer, i, 0, &sa, sizeof(sa)); | |
868 | } | |
869 | ||
870 | /* ARGSUSED */ | |
159f9a1c RG |
871 | void |
872 | discard_stream(int s,struct servtab *sep) /* Discard service -- ignore data */ | |
15637ed4 RG |
873 | { |
874 | int ret; | |
875 | char buffer[BUFSIZE]; | |
876 | ||
877 | setproctitle(sep->se_service, s); | |
878 | while (1) { | |
879 | while ((ret = read(s, buffer, sizeof(buffer))) > 0) | |
880 | ; | |
881 | if (ret == 0 || errno != EINTR) | |
882 | break; | |
883 | } | |
884 | exit(0); | |
885 | } | |
886 | ||
887 | /* ARGSUSED */ | |
159f9a1c RG |
888 | void |
889 | discard_dg(int s,struct servtab *sep) /* Discard service -- ignore data */ | |
15637ed4 RG |
890 | { |
891 | char buffer[BUFSIZE]; | |
892 | ||
893 | (void) read(s, buffer, sizeof(buffer)); | |
894 | } | |
895 | ||
896 | #include <ctype.h> | |
897 | #define LINESIZ 72 | |
898 | char ring[128]; | |
899 | char *endring; | |
900 | ||
159f9a1c | 901 | void |
15637ed4 RG |
902 | initring() |
903 | { | |
159f9a1c | 904 | int i; |
15637ed4 RG |
905 | |
906 | endring = ring; | |
907 | ||
908 | for (i = 0; i <= 128; ++i) | |
909 | if (isprint(i)) | |
910 | *endring++ = i; | |
911 | } | |
912 | ||
913 | /* ARGSUSED */ | |
159f9a1c RG |
914 | void |
915 | chargen_stream(int s,struct servtab *sep) /* Character generator */ | |
15637ed4 | 916 | { |
159f9a1c | 917 | char *rs; |
15637ed4 RG |
918 | int len; |
919 | char text[LINESIZ+2]; | |
920 | ||
921 | setproctitle(sep->se_service, s); | |
922 | ||
923 | if (!endring) { | |
924 | initring(); | |
925 | rs = ring; | |
926 | } | |
927 | ||
928 | text[LINESIZ] = '\r'; | |
929 | text[LINESIZ + 1] = '\n'; | |
930 | for (rs = ring;;) { | |
931 | if ((len = endring - rs) >= LINESIZ) | |
932 | bcopy(rs, text, LINESIZ); | |
933 | else { | |
934 | bcopy(rs, text, len); | |
935 | bcopy(ring, text + len, LINESIZ - len); | |
936 | } | |
937 | if (++rs == endring) | |
938 | rs = ring; | |
939 | if (write(s, text, sizeof(text)) != sizeof(text)) | |
940 | break; | |
941 | } | |
942 | exit(0); | |
943 | } | |
944 | ||
945 | /* ARGSUSED */ | |
159f9a1c RG |
946 | void |
947 | chargen_dg(int s,struct servtab *sep) /* Character generator */ | |
15637ed4 RG |
948 | { |
949 | struct sockaddr sa; | |
950 | static char *rs; | |
951 | int len, size; | |
952 | char text[LINESIZ+2]; | |
953 | ||
954 | if (endring == 0) { | |
955 | initring(); | |
956 | rs = ring; | |
957 | } | |
958 | ||
959 | size = sizeof(sa); | |
960 | if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0) | |
961 | return; | |
962 | ||
963 | if ((len = endring - rs) >= LINESIZ) | |
964 | bcopy(rs, text, LINESIZ); | |
965 | else { | |
966 | bcopy(rs, text, len); | |
967 | bcopy(ring, text + len, LINESIZ - len); | |
968 | } | |
969 | if (++rs == endring) | |
970 | rs = ring; | |
971 | text[LINESIZ] = '\r'; | |
972 | text[LINESIZ + 1] = '\n'; | |
973 | (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa)); | |
974 | } | |
975 | ||
976 | /* | |
977 | * Return a machine readable date and time, in the form of the | |
978 | * number of seconds since midnight, Jan 1, 1900. Since gettimeofday | |
979 | * returns the number of seconds since midnight, Jan 1, 1970, | |
980 | * we must add 2208988800 seconds to this figure to make up for | |
981 | * some seventy years Bell Labs was asleep. | |
982 | */ | |
983 | ||
984 | long | |
985 | machtime() | |
986 | { | |
987 | struct timeval tv; | |
988 | ||
989 | if (gettimeofday(&tv, (struct timezone *)0) < 0) { | |
990 | fprintf(stderr, "Unable to get time of day\n"); | |
991 | return (0L); | |
992 | } | |
159f9a1c | 993 | return (htonl((long)tv.tv_sec + 2208988800U)); |
15637ed4 RG |
994 | } |
995 | ||
996 | /* ARGSUSED */ | |
159f9a1c RG |
997 | void |
998 | machtime_stream(int s,struct servtab *sep) | |
15637ed4 RG |
999 | { |
1000 | long result; | |
1001 | ||
1002 | result = machtime(); | |
1003 | (void) write(s, (char *) &result, sizeof(result)); | |
1004 | } | |
1005 | ||
1006 | /* ARGSUSED */ | |
159f9a1c RG |
1007 | void |
1008 | machtime_dg(int s,struct servtab *sep) | |
15637ed4 RG |
1009 | { |
1010 | long result; | |
1011 | struct sockaddr sa; | |
1012 | int size; | |
1013 | ||
1014 | size = sizeof(sa); | |
1015 | if (recvfrom(s, (char *)&result, sizeof(result), 0, &sa, &size) < 0) | |
1016 | return; | |
1017 | result = machtime(); | |
1018 | (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa)); | |
1019 | } | |
1020 | ||
1021 | /* ARGSUSED */ | |
159f9a1c RG |
1022 | void |
1023 | daytime_stream(int s,struct servtab *sep) /* Return human-readable time of day */ | |
15637ed4 RG |
1024 | { |
1025 | char buffer[256]; | |
159f9a1c | 1026 | time_t clock; |
15637ed4 RG |
1027 | |
1028 | clock = time((time_t *) 0); | |
1029 | ||
1030 | (void) sprintf(buffer, "%.24s\r\n", ctime(&clock)); | |
1031 | (void) write(s, buffer, strlen(buffer)); | |
1032 | } | |
1033 | ||
1034 | /* ARGSUSED */ | |
159f9a1c RG |
1035 | void |
1036 | daytime_dg(int s,struct servtab *sep) /* Return human-readable time of day */ | |
15637ed4 RG |
1037 | { |
1038 | char buffer[256]; | |
159f9a1c | 1039 | time_t clock; |
15637ed4 RG |
1040 | struct sockaddr sa; |
1041 | int size; | |
15637ed4 RG |
1042 | |
1043 | clock = time((time_t *) 0); | |
1044 | ||
1045 | size = sizeof(sa); | |
1046 | if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0) | |
1047 | return; | |
1048 | (void) sprintf(buffer, "%.24s\r\n", ctime(&clock)); | |
1049 | (void) sendto(s, buffer, strlen(buffer), 0, &sa, sizeof(sa)); | |
1050 | } | |
1051 | ||
1052 | /* | |
1053 | * print_service: | |
1054 | * Dump relevant information to stderr | |
1055 | */ | |
159f9a1c RG |
1056 | void |
1057 | print_service(char *action, struct servtab *sep) | |
15637ed4 | 1058 | { |
b41acc45 C |
1059 | if (sep->se_rpc) |
1060 | fprintf(stderr, | |
1061 | "%s: %s rpcnum=%d, rpcvers=%d/%d, wait=%d, user=%s builtin=%x server=%s\n", | |
1062 | action, sep->se_service, | |
1063 | sep->se_rpc_prog, sep->se_rpc_lowvers, sep->se_rpc_highvers, | |
1064 | sep->se_wait, sep->se_user, (int)sep->se_bi, sep->se_server); | |
1065 | else | |
1066 | fprintf(stderr, | |
1067 | "%s: %s proto=%s, wait=%d, user=%s builtin=%x server=%s\n", | |
1068 | action, sep->se_service, sep->se_proto, | |
1069 | sep->se_wait, sep->se_user, (int)sep->se_bi, sep->se_server); | |
15637ed4 | 1070 | } |