1 | /* $NetBSD: inetd.c,v 1.38.2.2 1998/05/05 08:12:06 mycroft Exp $ */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 1983, 1991, 1993, 1994 |
---|
5 | * The Regents of the University of California. All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * 3. All advertising materials mentioning features or use of this software |
---|
16 | * must display the following acknowledgement: |
---|
17 | * This product includes software developed by the University of |
---|
18 | * California, Berkeley and its contributors. |
---|
19 | * 4. Neither the name of the University nor the names of its contributors |
---|
20 | * may be used to endorse or promote products derived from this software |
---|
21 | * without specific prior written permission. |
---|
22 | * |
---|
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
33 | * SUCH DAMAGE. |
---|
34 | */ |
---|
35 | |
---|
36 | #ifdef __NetBSD__ |
---|
37 | #include <sys/cdefs.h> |
---|
38 | #else |
---|
39 | #define __COPYRIGHT(x) char *copyright = x |
---|
40 | #define __RCSID(x) char *rcsid = x |
---|
41 | char *__progname; |
---|
42 | #endif |
---|
43 | |
---|
44 | #ifndef lint |
---|
45 | __COPYRIGHT("@(#) Copyright (c) 1983, 1991, 1993, 1994\n\ |
---|
46 | The Regents of the University of California. All rights reserved.\n"); |
---|
47 | #if 0 |
---|
48 | static char sccsid[] = "@(#)inetd.c 8.4 (Berkeley) 4/13/94"; |
---|
49 | #else |
---|
50 | __RCSID("$NetBSD: inetd.c,v 1.38.2.2 1998/05/05 08:12:06 mycroft Exp $"); |
---|
51 | #endif |
---|
52 | #endif /* not lint */ |
---|
53 | |
---|
54 | /* |
---|
55 | * Inetd - Internet super-server |
---|
56 | * |
---|
57 | * This program invokes all internet services as needed. Connection-oriented |
---|
58 | * services are invoked each time a connection is made, by creating a process. |
---|
59 | * This process is passed the connection as file descriptor 0 and is expected |
---|
60 | * to do a getpeername to find out the source host and port. |
---|
61 | * |
---|
62 | * Datagram oriented services are invoked when a datagram |
---|
63 | * arrives; a process is created and passed a pending message |
---|
64 | * on file descriptor 0. Datagram servers may either connect |
---|
65 | * to their peer, freeing up the original socket for inetd |
---|
66 | * to receive further messages on, or ``take over the socket'', |
---|
67 | * processing all arriving datagrams and, eventually, timing |
---|
68 | * out. The first type of server is said to be ``multi-threaded''; |
---|
69 | * the second type of server ``single-threaded''. |
---|
70 | * |
---|
71 | * Inetd uses a configuration file which is read at startup |
---|
72 | * and, possibly, at some later time in response to a hangup signal. |
---|
73 | * The configuration file is ``free format'' with fields given in the |
---|
74 | * order shown below. Continuation lines for an entry must being with |
---|
75 | * a space or tab. All fields must be present in each entry. |
---|
76 | * |
---|
77 | * service name must be in /etc/services or must |
---|
78 | * name a tcpmux service |
---|
79 | * socket type stream/dgram/raw/rdm/seqpacket |
---|
80 | * protocol must be in /etc/protocols |
---|
81 | * wait/nowait[.max] single-threaded/multi-threaded, max # |
---|
82 | * user[.group] user/group to run daemon as |
---|
83 | * server program full path name |
---|
84 | * server program arguments maximum of MAXARGS (20) |
---|
85 | * |
---|
86 | * For RPC services |
---|
87 | * service name/version must be in /etc/rpc |
---|
88 | * socket type stream/dgram/raw/rdm/seqpacket |
---|
89 | * protocol must be in /etc/protocols |
---|
90 | * wait/nowait[.max] single-threaded/multi-threaded |
---|
91 | * user[.group] user to run daemon as |
---|
92 | * server program full path name |
---|
93 | * server program arguments maximum of MAXARGS (20) |
---|
94 | * |
---|
95 | * For non-RPC services, the "service name" can be of the form |
---|
96 | * hostaddress:servicename, in which case the hostaddress is used |
---|
97 | * as the host portion of the address to listen on. If hostaddress |
---|
98 | * consists of a single `*' character, INADDR_ANY is used. |
---|
99 | * |
---|
100 | * A line can also consist of just |
---|
101 | * hostaddress: |
---|
102 | * where hostaddress is as in the preceding paragraph. Such a line must |
---|
103 | * have no further fields; the specified hostaddress is remembered and |
---|
104 | * used for all further lines that have no hostaddress specified, |
---|
105 | * until the next such line (or EOF). (This is why * is provided to |
---|
106 | * allow explicit specification of INADDR_ANY.) A line |
---|
107 | * *: |
---|
108 | * is implicitly in effect at the beginning of the file. |
---|
109 | * |
---|
110 | * The hostaddress specifier may (and often will) contain dots; |
---|
111 | * the service name must not. |
---|
112 | * |
---|
113 | * For RPC services, host-address specifiers are accepted and will |
---|
114 | * work to some extent; however, because of limitations in the |
---|
115 | * portmapper interface, it will not work to try to give more than |
---|
116 | * one line for any given RPC service, even if the host-address |
---|
117 | * specifiers are different. |
---|
118 | * |
---|
119 | * TCP services without official port numbers are handled with the |
---|
120 | * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for |
---|
121 | * requests. When a connection is made from a foreign host, the service |
---|
122 | * requested is passed to tcpmux, which looks it up in the servtab list |
---|
123 | * and returns the proper entry for the service. Tcpmux returns a |
---|
124 | * negative reply if the service doesn't exist, otherwise the invoked |
---|
125 | * server is expected to return the positive reply if the service type in |
---|
126 | * inetd.conf file has the prefix "tcpmux/". If the service type has the |
---|
127 | * prefix "tcpmux/+", tcpmux will return the positive reply for the |
---|
128 | * process; this is for compatibility with older server code, and also |
---|
129 | * allows you to invoke programs that use stdin/stdout without putting any |
---|
130 | * special server code in them. Services that use tcpmux are "nowait" |
---|
131 | * because they do not have a well-known port and hence cannot listen |
---|
132 | * for new requests. |
---|
133 | * |
---|
134 | * Comment lines are indicated by a `#' in column 1. |
---|
135 | */ |
---|
136 | |
---|
137 | /* |
---|
138 | * Here's the scoop concerning the user.group feature: |
---|
139 | * |
---|
140 | * 1) set-group-option off. |
---|
141 | * |
---|
142 | * a) user = root: NO setuid() or setgid() is done |
---|
143 | * |
---|
144 | * b) other: setuid() |
---|
145 | * setgid(primary group as found in passwd) |
---|
146 | * initgroups(name, primary group) |
---|
147 | * |
---|
148 | * 2) set-group-option on. |
---|
149 | * |
---|
150 | * a) user = root: NO setuid() |
---|
151 | * setgid(specified group) |
---|
152 | * NO initgroups() |
---|
153 | * |
---|
154 | * b) other: setuid() |
---|
155 | * setgid(specified group) |
---|
156 | * initgroups(name, specified group) |
---|
157 | * |
---|
158 | */ |
---|
159 | |
---|
160 | #include <sys/types.h> |
---|
161 | #include <sys/param.h> |
---|
162 | #include <sys/stat.h> |
---|
163 | #include <sys/ioctl.h> |
---|
164 | #include <sys/socket.h> |
---|
165 | #include <sys/un.h> |
---|
166 | #include <sys/wait.h> |
---|
167 | #include <sys/time.h> |
---|
168 | #include <sys/resource.h> |
---|
169 | |
---|
170 | #ifndef RLIMIT_NOFILE |
---|
171 | #define RLIMIT_NOFILE RLIMIT_OFILE |
---|
172 | #endif |
---|
173 | |
---|
174 | #define RPC |
---|
175 | |
---|
176 | #include <netinet/in.h> |
---|
177 | #include <arpa/inet.h> |
---|
178 | #ifdef RPC |
---|
179 | #include <rpc/rpc.h> |
---|
180 | #include <rpc/pmap_clnt.h> |
---|
181 | #ifdef HAVE_RPC_RPCENT_H |
---|
182 | #include <rpc/rpcent.h> |
---|
183 | #endif |
---|
184 | #endif |
---|
185 | |
---|
186 | #include <errno.h> |
---|
187 | #include <fcntl.h> |
---|
188 | #include <grp.h> |
---|
189 | #include <netdb.h> |
---|
190 | #include <pwd.h> |
---|
191 | #include <signal.h> |
---|
192 | #include <stdio.h> |
---|
193 | #include <stdlib.h> |
---|
194 | #include <string.h> |
---|
195 | #include <syslog.h> |
---|
196 | #include <unistd.h> |
---|
197 | |
---|
198 | #include "pathnames.h" |
---|
199 | |
---|
200 | #ifdef LIBWRAP |
---|
201 | # include <tcpd.h> |
---|
202 | #ifndef LIBWRAP_ALLOW_FACILITY |
---|
203 | # define LIBWRAP_ALLOW_FACILITY LOG_AUTH |
---|
204 | #endif |
---|
205 | #ifndef LIBWRAP_ALLOW_SEVERITY |
---|
206 | # define LIBWRAP_ALLOW_SEVERITY LOG_INFO |
---|
207 | #endif |
---|
208 | #ifndef LIBWRAP_DENY_FACILITY |
---|
209 | # define LIBWRAP_DENY_FACILITY LOG_AUTH |
---|
210 | #endif |
---|
211 | #ifndef LIBWRAP_DENY_SEVERITY |
---|
212 | # define LIBWRAP_DENY_SEVERITY LOG_WARNING |
---|
213 | #endif |
---|
214 | int allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY; |
---|
215 | int deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY; |
---|
216 | #endif |
---|
217 | |
---|
218 | #define TOOMANY 40 /* don't start more than TOOMANY */ |
---|
219 | #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */ |
---|
220 | #define RETRYTIME (60*10) /* retry after bind or server fail */ |
---|
221 | |
---|
222 | sigset_t sig_block; |
---|
223 | |
---|
224 | int debug; |
---|
225 | #ifdef LIBWRAP |
---|
226 | int lflag; |
---|
227 | #endif |
---|
228 | int access_on; |
---|
229 | int nsock, maxsock; |
---|
230 | fd_set allsock; |
---|
231 | int options; |
---|
232 | int timingout; |
---|
233 | struct servent *sp; |
---|
234 | char *curdom; |
---|
235 | |
---|
236 | #ifndef OPEN_MAX |
---|
237 | #define OPEN_MAX 64 |
---|
238 | #endif |
---|
239 | |
---|
240 | /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */ |
---|
241 | #define FD_MARGIN (8) |
---|
242 | rlim_t rlim_ofile_cur = OPEN_MAX; |
---|
243 | |
---|
244 | #ifdef RLIMIT_NOFILE |
---|
245 | struct rlimit rlim_ofile; |
---|
246 | #endif |
---|
247 | |
---|
248 | struct servtab { |
---|
249 | char *se_hostaddr; /* host address to listen on */ |
---|
250 | char *se_service; /* name of service */ |
---|
251 | int se_socktype; /* type of socket to use */ |
---|
252 | int se_family; /* address family */ |
---|
253 | char *se_proto; /* protocol used */ |
---|
254 | int se_rpcprog; /* rpc program number */ |
---|
255 | int se_rpcversl; /* rpc program lowest version */ |
---|
256 | int se_rpcversh; /* rpc program highest version */ |
---|
257 | #define isrpcservice(sep) ((sep)->se_rpcversl != 0) |
---|
258 | pid_t se_wait; /* single threaded server */ |
---|
259 | short se_switched; /* switched by access_on/off */ |
---|
260 | short se_checked; /* looked at during merge */ |
---|
261 | char *se_user; /* user name to run as */ |
---|
262 | char *se_group; /* group name to run as */ |
---|
263 | struct biltin *se_bi; /* if built-in, description */ |
---|
264 | char *se_server; /* server program */ |
---|
265 | #define MAXARGV 20 |
---|
266 | char *se_argv[MAXARGV+1]; /* program arguments */ |
---|
267 | int se_fd; /* open descriptor */ |
---|
268 | int se_type; /* type */ |
---|
269 | union { |
---|
270 | struct sockaddr se_un_ctrladdr; |
---|
271 | struct sockaddr_in se_un_ctrladdr_in; |
---|
272 | struct sockaddr_un se_un_ctrladdr_un; |
---|
273 | } se_un; /* bound address */ |
---|
274 | #define se_ctrladdr se_un.se_un_ctrladdr |
---|
275 | #define se_ctrladdr_in se_un.se_un_ctrladdr_in |
---|
276 | #define se_ctrladdr_un se_un.se_un_ctrladdr_un |
---|
277 | int se_ctrladdr_size; |
---|
278 | int se_max; /* max # of instances of this service */ |
---|
279 | int se_count; /* number started since se_time */ |
---|
280 | struct timeval se_time; /* start of se_count */ |
---|
281 | #ifdef MULOG |
---|
282 | int se_log; |
---|
283 | #define MULOG_RFC931 0x40000000 |
---|
284 | #endif |
---|
285 | struct servtab *se_next; |
---|
286 | } *servtab; |
---|
287 | |
---|
288 | #define NORM_TYPE 0 |
---|
289 | #define MUX_TYPE 1 |
---|
290 | #define MUXPLUS_TYPE 2 |
---|
291 | #define ISMUX(sep) (((sep)->se_type == MUX_TYPE) || \ |
---|
292 | ((sep)->se_type == MUXPLUS_TYPE)) |
---|
293 | #define ISMUXPLUS(sep) ((sep)->se_type == MUXPLUS_TYPE) |
---|
294 | |
---|
295 | |
---|
296 | #ifndef __P |
---|
297 | #define __P(x) x |
---|
298 | #endif |
---|
299 | |
---|
300 | void chargen_dg __P((int, struct servtab *)); |
---|
301 | void chargen_stream __P((int, struct servtab *)); |
---|
302 | void close_sep __P((struct servtab *)); |
---|
303 | void config __P((int)); |
---|
304 | void access_switch __P((int)); |
---|
305 | void daytime_dg __P((int, struct servtab *)); |
---|
306 | void daytime_stream __P((int, struct servtab *)); |
---|
307 | void discard_dg __P((int, struct servtab *)); |
---|
308 | void discard_stream __P((int, struct servtab *)); |
---|
309 | void echo_dg __P((int, struct servtab *)); |
---|
310 | void echo_stream __P((int, struct servtab *)); |
---|
311 | void endconfig __P((void)); |
---|
312 | struct servtab *enter __P((struct servtab *)); |
---|
313 | void freeconfig __P((struct servtab *)); |
---|
314 | struct servtab *getconfigent __P((void)); |
---|
315 | void goaway __P((int)); |
---|
316 | void machtime_dg __P((int, struct servtab *)); |
---|
317 | void machtime_stream __P((int, struct servtab *)); |
---|
318 | char *newstr __P((char *)); |
---|
319 | char *nextline __P((FILE *)); |
---|
320 | void print_service __P((char *, struct servtab *)); |
---|
321 | void reapchild __P((int)); |
---|
322 | void retry __P((int)); |
---|
323 | void run_service __P((int, struct servtab *)); |
---|
324 | int setconfig __P((void)); |
---|
325 | void setup __P((struct servtab *)); |
---|
326 | char *sskip __P((char **)); |
---|
327 | char *skip __P((char **)); |
---|
328 | void tcpmux __P((int, struct servtab *)); |
---|
329 | void usage __P((void)); |
---|
330 | void logpid __P((void)); |
---|
331 | void register_rpc __P((struct servtab *sep)); |
---|
332 | void unregister_rpc __P((struct servtab *sep)); |
---|
333 | void bump_nofile __P((void)); |
---|
334 | void inetd_setproctitle __P((char *, int)); |
---|
335 | void initring __P((void)); |
---|
336 | long machtime __P((void)); |
---|
337 | static int getline __P((int, char *, int)); |
---|
338 | int main __P((int, char *[], char *[])); |
---|
339 | |
---|
340 | struct biltin { |
---|
341 | char *bi_service; /* internally provided service name */ |
---|
342 | int bi_socktype; /* type of socket supported */ |
---|
343 | short bi_fork; /* 1 if should fork before call */ |
---|
344 | short bi_wait; /* 1 if should wait for child */ |
---|
345 | void (*bi_fn) __P((int, struct servtab *)); |
---|
346 | /* function which performs it */ |
---|
347 | } biltins[] = { |
---|
348 | /* Echo received data */ |
---|
349 | { "echo", SOCK_STREAM, 1, 0, echo_stream }, |
---|
350 | { "echo", SOCK_DGRAM, 0, 0, echo_dg }, |
---|
351 | |
---|
352 | /* Internet /dev/null */ |
---|
353 | { "discard", SOCK_STREAM, 1, 0, discard_stream }, |
---|
354 | { "discard", SOCK_DGRAM, 0, 0, discard_dg }, |
---|
355 | |
---|
356 | /* Return 32 bit time since 1970 */ |
---|
357 | { "time", SOCK_STREAM, 0, 0, machtime_stream }, |
---|
358 | { "time", SOCK_DGRAM, 0, 0, machtime_dg }, |
---|
359 | |
---|
360 | /* Return human-readable time */ |
---|
361 | { "daytime", SOCK_STREAM, 0, 0, daytime_stream }, |
---|
362 | { "daytime", SOCK_DGRAM, 0, 0, daytime_dg }, |
---|
363 | |
---|
364 | /* Familiar character generator */ |
---|
365 | { "chargen", SOCK_STREAM, 1, 0, chargen_stream }, |
---|
366 | { "chargen", SOCK_DGRAM, 0, 0, chargen_dg }, |
---|
367 | |
---|
368 | { "tcpmux", SOCK_STREAM, 1, 0, tcpmux }, |
---|
369 | |
---|
370 | { NULL } |
---|
371 | }; |
---|
372 | |
---|
373 | #define NUMINT (sizeof(intab) / sizeof(struct inent)) |
---|
374 | char *CONFIG = _PATH_INETDCONF; |
---|
375 | char *PIDFILE = _PATH_INETPID; |
---|
376 | char **Argv; |
---|
377 | char *LastArg; |
---|
378 | extern char *__progname; |
---|
379 | |
---|
380 | #ifdef sun |
---|
381 | /* |
---|
382 | * Sun's RPC library caches the result of `dtablesize()' |
---|
383 | * This is incompatible with our "bumping" of file descriptors "on demand" |
---|
384 | */ |
---|
385 | int |
---|
386 | _rpc_dtablesize() |
---|
387 | { |
---|
388 | return rlim_ofile_cur; |
---|
389 | } |
---|
390 | #endif |
---|
391 | |
---|
392 | int |
---|
393 | main(argc, argv, envp) |
---|
394 | int argc; |
---|
395 | char *argv[], *envp[]; |
---|
396 | { |
---|
397 | struct servtab *sep, *nsep; |
---|
398 | struct sigaction sa; |
---|
399 | int ch, dofork; |
---|
400 | pid_t pid; |
---|
401 | |
---|
402 | __progname = argv[0]; |
---|
403 | if (strchr(__progname, '/')) |
---|
404 | __progname = strrchr(__progname, '/') + 1; |
---|
405 | |
---|
406 | Argv = argv; |
---|
407 | if (envp == 0 || *envp == 0) |
---|
408 | envp = argv; |
---|
409 | while (*envp) |
---|
410 | envp++; |
---|
411 | LastArg = envp[-1] + strlen(envp[-1]); |
---|
412 | |
---|
413 | while ((ch = getopt(argc, argv, |
---|
414 | #ifdef LIBWRAP |
---|
415 | "dlnp:" |
---|
416 | #else |
---|
417 | "dnp:" |
---|
418 | #endif |
---|
419 | )) != -1) |
---|
420 | switch(ch) { |
---|
421 | case 'd': |
---|
422 | debug = 1; |
---|
423 | options |= SO_DEBUG; |
---|
424 | break; |
---|
425 | #ifdef LIBWRAP |
---|
426 | case 'l': |
---|
427 | lflag = 1; |
---|
428 | break; |
---|
429 | #endif |
---|
430 | case 'n': |
---|
431 | access_on = 1; |
---|
432 | break; |
---|
433 | case 'p': |
---|
434 | PIDFILE = optarg; |
---|
435 | break; |
---|
436 | case '?': |
---|
437 | default: |
---|
438 | usage(); |
---|
439 | } |
---|
440 | argc -= optind; |
---|
441 | argv += optind; |
---|
442 | |
---|
443 | if (argc > 0) |
---|
444 | CONFIG = argv[0]; |
---|
445 | |
---|
446 | if (debug == 0) { |
---|
447 | int fd; |
---|
448 | |
---|
449 | switch (fork ()) { |
---|
450 | case -1: |
---|
451 | return (-1); |
---|
452 | case 0: |
---|
453 | break; |
---|
454 | default: |
---|
455 | _exit(0); |
---|
456 | } |
---|
457 | |
---|
458 | if (setsid() == -1) { |
---|
459 | fprintf(stderr, "%s: couldn't setsid\n", __progname); |
---|
460 | exit(1); |
---|
461 | } |
---|
462 | chdir("/"); |
---|
463 | if ((fd = open("/dev/null", O_RDWR, 0)) != -1) { |
---|
464 | dup2(fd, STDIN_FILENO); |
---|
465 | dup2(fd, STDOUT_FILENO); |
---|
466 | dup2(fd, STDERR_FILENO); |
---|
467 | if (fd > STDERR_FILENO) |
---|
468 | close(fd); |
---|
469 | } |
---|
470 | } |
---|
471 | openlog(__progname, LOG_PID | LOG_NOWAIT, LOG_DAEMON); |
---|
472 | logpid(); |
---|
473 | |
---|
474 | #ifdef RLIMIT_NOFILE |
---|
475 | if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) { |
---|
476 | syslog(LOG_ERR, "getrlimit: %m"); |
---|
477 | } else { |
---|
478 | rlim_ofile_cur = rlim_ofile.rlim_cur; |
---|
479 | if (rlim_ofile_cur == RLIM_INFINITY) /* ! */ |
---|
480 | rlim_ofile_cur = OPEN_MAX; |
---|
481 | } |
---|
482 | #endif |
---|
483 | |
---|
484 | memset(&sa, 0, sizeof(sa)); |
---|
485 | sigemptyset(&sig_block); |
---|
486 | sigaddset(&sig_block, SIGCHLD); |
---|
487 | sigaddset(&sig_block, SIGHUP); |
---|
488 | sigaddset(&sig_block, SIGALRM); |
---|
489 | sigaddset(&sig_block, SIGUSR1); |
---|
490 | sigaddset(&sig_block, SIGUSR2); |
---|
491 | memcpy(&sa.sa_mask, &sig_block, sizeof(sigset_t)); |
---|
492 | sa.sa_handler = retry; |
---|
493 | sigaction(SIGALRM, &sa, NULL); |
---|
494 | config(SIGHUP); |
---|
495 | sa.sa_handler = config; |
---|
496 | sigaction(SIGHUP, &sa, NULL); |
---|
497 | sa.sa_handler = access_switch; |
---|
498 | sigaction(SIGUSR1, &sa, NULL); |
---|
499 | sigaction(SIGUSR2, &sa, NULL); |
---|
500 | sa.sa_handler = reapchild; |
---|
501 | sigaction(SIGCHLD, &sa, NULL); |
---|
502 | sa.sa_handler = goaway; |
---|
503 | sigaction(SIGTERM, &sa, NULL); |
---|
504 | sa.sa_handler = goaway; |
---|
505 | sigaction(SIGINT, &sa, NULL); |
---|
506 | sigemptyset(&sa.sa_mask); |
---|
507 | sa.sa_handler = SIG_IGN; |
---|
508 | sigaction(SIGPIPE, &sa, NULL); |
---|
509 | |
---|
510 | { |
---|
511 | /* space for daemons to overwrite environment for ps */ |
---|
512 | #define DUMMYSIZE 100 |
---|
513 | char dummy[DUMMYSIZE]; |
---|
514 | |
---|
515 | (void)memset(dummy, 'x', DUMMYSIZE - 1); |
---|
516 | dummy[DUMMYSIZE - 1] = '\0'; |
---|
517 | |
---|
518 | (void)setenv("inetd_dummy", dummy, 1); |
---|
519 | } |
---|
520 | |
---|
521 | for (;;) { |
---|
522 | int n, ctrl; |
---|
523 | fd_set readable; |
---|
524 | |
---|
525 | if (nsock == 0) { |
---|
526 | sigset_t prev_mask; |
---|
527 | sigprocmask(SIG_SETMASK, &sig_block, &prev_mask); |
---|
528 | while (nsock == 0) |
---|
529 | sigsuspend(&prev_mask); |
---|
530 | sigprocmask(SIG_SETMASK, &prev_mask, NULL); |
---|
531 | } |
---|
532 | readable = allsock; |
---|
533 | if ((n = select(maxsock + 1, &readable, (fd_set *)0, |
---|
534 | (fd_set *)0, (struct timeval *)0)) <= 0) { |
---|
535 | if (n == -1 && errno != EINTR) { |
---|
536 | syslog(LOG_WARNING, "select: %m"); |
---|
537 | sleep(1); |
---|
538 | } |
---|
539 | continue; |
---|
540 | } |
---|
541 | for (sep = servtab; n && sep; sep = nsep) { |
---|
542 | nsep = sep->se_next; |
---|
543 | if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) { |
---|
544 | n--; |
---|
545 | if (debug) |
---|
546 | fprintf(stderr, "someone wants %s\n", sep->se_service); |
---|
547 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) { |
---|
548 | /* XXX here do the libwrap check-before-accept */ |
---|
549 | ctrl = accept(sep->se_fd, (struct sockaddr *)0, |
---|
550 | (int *)0); |
---|
551 | if (debug) |
---|
552 | fprintf(stderr, "accept, ctrl %d\n", ctrl); |
---|
553 | if (ctrl < 0) { |
---|
554 | if (errno != EINTR) |
---|
555 | syslog(LOG_WARNING, |
---|
556 | "accept (for %s): %m", |
---|
557 | sep->se_service); |
---|
558 | continue; |
---|
559 | } |
---|
560 | } else |
---|
561 | ctrl = sep->se_fd; |
---|
562 | sigprocmask(SIG_SETMASK, &sig_block, NULL); |
---|
563 | pid = 0; |
---|
564 | #ifdef LIBWRAP_INTERNAL |
---|
565 | dofork = 1; |
---|
566 | #else |
---|
567 | dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork); |
---|
568 | #endif |
---|
569 | if (dofork) { |
---|
570 | if (sep->se_count++ == 0) |
---|
571 | (void)gettimeofday(&sep->se_time, |
---|
572 | (struct timezone *)0); |
---|
573 | else if (sep->se_count >= sep->se_max) { |
---|
574 | struct timeval now; |
---|
575 | |
---|
576 | (void)gettimeofday(&now, (struct timezone *)0); |
---|
577 | if (now.tv_sec - sep->se_time.tv_sec > |
---|
578 | CNT_INTVL) { |
---|
579 | sep->se_time = now; |
---|
580 | sep->se_count = 1; |
---|
581 | } else { |
---|
582 | syslog(LOG_ERR, |
---|
583 | "%s/%s server failing (looping), service terminated\n", |
---|
584 | sep->se_service, sep->se_proto); |
---|
585 | close_sep(sep); |
---|
586 | sigprocmask(SIG_UNBLOCK, |
---|
587 | &sig_block, NULL); |
---|
588 | if (!timingout) { |
---|
589 | timingout = 1; |
---|
590 | alarm(RETRYTIME); |
---|
591 | } |
---|
592 | continue; |
---|
593 | } |
---|
594 | } |
---|
595 | pid = fork(); |
---|
596 | if (pid < 0) { |
---|
597 | syslog(LOG_ERR, "fork: %m"); |
---|
598 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) |
---|
599 | close(ctrl); |
---|
600 | sigprocmask(SIG_UNBLOCK, &sig_block, NULL); |
---|
601 | sleep(1); |
---|
602 | continue; |
---|
603 | } |
---|
604 | if (pid != 0 && sep->se_wait) { |
---|
605 | sep->se_wait = pid; |
---|
606 | FD_CLR(sep->se_fd, &allsock); |
---|
607 | nsock--; |
---|
608 | } |
---|
609 | if (pid == 0) { |
---|
610 | sigemptyset(&sa.sa_mask); |
---|
611 | sa.sa_handler = SIG_DFL; |
---|
612 | sigaction(SIGPIPE, &sa, NULL); |
---|
613 | if (debug) |
---|
614 | setsid(); |
---|
615 | } |
---|
616 | } |
---|
617 | sigprocmask(SIG_UNBLOCK, &sig_block, NULL); |
---|
618 | if (pid == 0) { |
---|
619 | run_service(ctrl, sep); |
---|
620 | if (dofork) |
---|
621 | exit(0); |
---|
622 | } |
---|
623 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) |
---|
624 | close(ctrl); |
---|
625 | } |
---|
626 | } |
---|
627 | } |
---|
628 | } |
---|
629 | |
---|
630 | void |
---|
631 | run_service(ctrl, sep) |
---|
632 | int ctrl; |
---|
633 | struct servtab *sep; |
---|
634 | { |
---|
635 | struct passwd *pwd; |
---|
636 | struct group *grp = NULL; /* XXX gcc */ |
---|
637 | char buf[7]; |
---|
638 | #ifdef LIBWRAP |
---|
639 | struct request_info req; |
---|
640 | int denied; |
---|
641 | char *service = NULL; /* XXX gcc */ |
---|
642 | #endif |
---|
643 | |
---|
644 | #ifdef LIBWRAP |
---|
645 | #ifndef LIBWRAP_INTERNAL |
---|
646 | if (sep->se_bi == 0) |
---|
647 | #endif |
---|
648 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) { |
---|
649 | request_init(&req, RQ_DAEMON, sep->se_argv[0] ? |
---|
650 | sep->se_argv[0] : sep->se_service, RQ_FILE, ctrl, NULL); |
---|
651 | fromhost(&req); |
---|
652 | denied = !hosts_access(&req); |
---|
653 | if (denied || lflag) { |
---|
654 | sp = getservbyport(sep->se_ctrladdr_in.sin_port, |
---|
655 | sep->se_proto); |
---|
656 | if (sp == NULL) { |
---|
657 | (void)sprintf(buf, "%d", |
---|
658 | ntohs(sep->se_ctrladdr_in.sin_port)); |
---|
659 | service = buf; |
---|
660 | } else |
---|
661 | service = sp->s_name; |
---|
662 | } |
---|
663 | if (denied) { |
---|
664 | syslog(deny_severity, |
---|
665 | "refused connection from %.500s, service %s (%s)", |
---|
666 | eval_client(&req), service, sep->se_proto); |
---|
667 | goto reject; |
---|
668 | } |
---|
669 | if (lflag) { |
---|
670 | syslog(allow_severity, |
---|
671 | "connection from %.500s, service %s (%s)", |
---|
672 | eval_client(&req), service, sep->se_proto); |
---|
673 | } |
---|
674 | } |
---|
675 | #endif /* LIBWRAP */ |
---|
676 | |
---|
677 | if (sep->se_bi) { |
---|
678 | (*sep->se_bi->bi_fn)(ctrl, sep); |
---|
679 | } else { |
---|
680 | if ((pwd = getpwnam(sep->se_user)) == NULL) { |
---|
681 | syslog(LOG_ERR, "%s/%s: %s: No such user", |
---|
682 | sep->se_service, sep->se_proto, sep->se_user); |
---|
683 | goto reject; |
---|
684 | } |
---|
685 | if (sep->se_group && |
---|
686 | (grp = getgrnam(sep->se_group)) == NULL) { |
---|
687 | syslog(LOG_ERR, "%s/%s: %s: No such group", |
---|
688 | sep->se_service, sep->se_proto, sep->se_group); |
---|
689 | goto reject; |
---|
690 | } |
---|
691 | if (pwd->pw_uid) { |
---|
692 | if (sep->se_group) |
---|
693 | pwd->pw_gid = grp->gr_gid; |
---|
694 | if (setgid(pwd->pw_gid) < 0) { |
---|
695 | syslog(LOG_ERR, |
---|
696 | "%s/%s: can't set gid %d: %m", sep->se_service, |
---|
697 | sep->se_proto, pwd->pw_gid); |
---|
698 | goto reject; |
---|
699 | } |
---|
700 | (void) initgroups(pwd->pw_name, |
---|
701 | pwd->pw_gid); |
---|
702 | if (setuid(pwd->pw_uid) < 0) { |
---|
703 | syslog(LOG_ERR, |
---|
704 | "%s/%s: can't set uid %d: %m", sep->se_service, |
---|
705 | sep->se_proto, pwd->pw_uid); |
---|
706 | goto reject; |
---|
707 | } |
---|
708 | } else if (sep->se_group) { |
---|
709 | (void) setgid((gid_t)grp->gr_gid); |
---|
710 | } |
---|
711 | if (debug) |
---|
712 | fprintf(stderr, "%d execl %s\n", |
---|
713 | getpid(), sep->se_server); |
---|
714 | #ifdef MULOG |
---|
715 | if (sep->se_log) |
---|
716 | dolog(sep, ctrl); |
---|
717 | #endif |
---|
718 | /* Set our control descriptor to not close-on-exec... */ |
---|
719 | if (fcntl(ctrl, F_SETFD, 0) < 0) |
---|
720 | syslog(LOG_ERR, "fcntl (F_SETFD, 0): %m"); |
---|
721 | /* ...and dup it to stdin, stdout, and stderr. */ |
---|
722 | if (ctrl != 0) { |
---|
723 | dup2(ctrl, 0); |
---|
724 | close(ctrl); |
---|
725 | ctrl = 0; |
---|
726 | } |
---|
727 | dup2(0, 1); |
---|
728 | dup2(0, 2); |
---|
729 | #ifdef RLIMIT_NOFILE |
---|
730 | if (rlim_ofile.rlim_cur != rlim_ofile_cur && |
---|
731 | setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) |
---|
732 | syslog(LOG_ERR, "setrlimit: %m"); |
---|
733 | #endif |
---|
734 | execv(sep->se_server, sep->se_argv); |
---|
735 | syslog(LOG_ERR, "cannot execute %s: %m", sep->se_server); |
---|
736 | reject: |
---|
737 | if (sep->se_socktype != SOCK_STREAM) |
---|
738 | recv(ctrl, buf, sizeof (buf), 0); |
---|
739 | _exit(1); |
---|
740 | } |
---|
741 | } |
---|
742 | |
---|
743 | void |
---|
744 | reapchild(signo) |
---|
745 | int signo; |
---|
746 | { |
---|
747 | int status; |
---|
748 | pid_t pid; |
---|
749 | struct servtab *sep; |
---|
750 | |
---|
751 | for (;;) { |
---|
752 | pid = wait3(&status, WNOHANG, (struct rusage *)0); |
---|
753 | if (pid <= 0) |
---|
754 | break; |
---|
755 | if (debug) |
---|
756 | fprintf(stderr, "%d reaped, status %#x\n", |
---|
757 | pid, status); |
---|
758 | for (sep = servtab; sep; sep = sep->se_next) |
---|
759 | if (sep->se_wait == pid) { |
---|
760 | if (WIFEXITED(status) && WEXITSTATUS(status)) |
---|
761 | syslog(LOG_WARNING, |
---|
762 | "%s: exit status 0x%x", |
---|
763 | sep->se_server, WEXITSTATUS(status)); |
---|
764 | else if (WIFSIGNALED(status)) |
---|
765 | syslog(LOG_WARNING, |
---|
766 | "%s: exit signal 0x%x", |
---|
767 | sep->se_server, WTERMSIG(status)); |
---|
768 | sep->se_wait = 1; |
---|
769 | FD_SET(sep->se_fd, &allsock); |
---|
770 | nsock++; |
---|
771 | if (debug) |
---|
772 | fprintf(stderr, "restored %s, fd %d\n", |
---|
773 | sep->se_service, sep->se_fd); |
---|
774 | } |
---|
775 | } |
---|
776 | } |
---|
777 | |
---|
778 | void |
---|
779 | config(signo) |
---|
780 | int signo; |
---|
781 | { |
---|
782 | struct servtab *sep, *cp, **sepp; |
---|
783 | sigset_t omask; |
---|
784 | int n; |
---|
785 | |
---|
786 | if (!setconfig()) { |
---|
787 | syslog(LOG_ERR, "%s: %m", CONFIG); |
---|
788 | return; |
---|
789 | } |
---|
790 | for (sep = servtab; sep; sep = sep->se_next) |
---|
791 | sep->se_checked = 0; |
---|
792 | while ((cp = getconfigent())) { |
---|
793 | for (sep = servtab; sep; sep = sep->se_next) |
---|
794 | if (strcmp(sep->se_service, cp->se_service) == 0 && |
---|
795 | strcmp(sep->se_hostaddr, cp->se_hostaddr) == 0 && |
---|
796 | strcmp(sep->se_proto, cp->se_proto) == 0 && |
---|
797 | ISMUX(sep) == ISMUX(cp)) |
---|
798 | break; |
---|
799 | if (sep != 0) { |
---|
800 | int i; |
---|
801 | |
---|
802 | #define SWAP(type, a, b) {type c=a; a=b; b=c;} |
---|
803 | |
---|
804 | sigprocmask(SIG_BLOCK, &sig_block, &omask); |
---|
805 | /* |
---|
806 | * sep->se_wait may be holding the pid of a daemon |
---|
807 | * that we're waiting for. If so, don't overwrite |
---|
808 | * it unless the config file explicitly says don't |
---|
809 | * wait. |
---|
810 | */ |
---|
811 | if (cp->se_bi == 0 && |
---|
812 | (sep->se_wait == 1 || cp->se_wait == 0)) |
---|
813 | sep->se_wait = cp->se_wait; |
---|
814 | SWAP(char *, sep->se_user, cp->se_user); |
---|
815 | SWAP(char *, sep->se_group, cp->se_group); |
---|
816 | SWAP(char *, sep->se_server, cp->se_server); |
---|
817 | for (i = 0; i < MAXARGV; i++) |
---|
818 | SWAP(char *, sep->se_argv[i], cp->se_argv[i]); |
---|
819 | SWAP(int, cp->se_type, sep->se_type); |
---|
820 | SWAP(int, cp->se_max, sep->se_max); |
---|
821 | #undef SWAP |
---|
822 | if (isrpcservice(sep)) |
---|
823 | unregister_rpc(sep); |
---|
824 | sep->se_rpcversl = cp->se_rpcversl; |
---|
825 | sep->se_rpcversh = cp->se_rpcversh; |
---|
826 | sep->se_switched = cp->se_switched; |
---|
827 | sigprocmask(SIG_SETMASK, &omask, NULL); |
---|
828 | freeconfig(cp); |
---|
829 | if (debug) |
---|
830 | print_service("REDO", sep); |
---|
831 | } else { |
---|
832 | sep = enter(cp); |
---|
833 | if (debug) |
---|
834 | print_service("ADD ", sep); |
---|
835 | } |
---|
836 | sep->se_checked = 1; |
---|
837 | |
---|
838 | switch (sep->se_family) { |
---|
839 | case AF_UNIX: |
---|
840 | if (sep->se_fd != -1) { |
---|
841 | if (!ISMUX(sep) && |
---|
842 | (sep->se_switched && !access_on)) { |
---|
843 | close_sep(sep); |
---|
844 | (void)unlink(sep->se_service); |
---|
845 | } |
---|
846 | break; |
---|
847 | } |
---|
848 | n = strlen(sep->se_service); |
---|
849 | if (n > sizeof(sep->se_ctrladdr_un.sun_path)) { |
---|
850 | syslog(LOG_ERR, "%s: address too long", |
---|
851 | sep->se_service); |
---|
852 | sep->se_checked = 0; |
---|
853 | continue; |
---|
854 | } |
---|
855 | (void)unlink(sep->se_service); |
---|
856 | strncpy(sep->se_ctrladdr_un.sun_path, |
---|
857 | sep->se_service, n); |
---|
858 | sep->se_ctrladdr_un.sun_family = AF_UNIX; |
---|
859 | sep->se_ctrladdr_size = n + |
---|
860 | sizeof(sep->se_ctrladdr_un) - |
---|
861 | sizeof(sep->se_ctrladdr_un.sun_path); |
---|
862 | if (!ISMUX(sep) && (!sep->se_switched || access_on)) |
---|
863 | setup(sep); |
---|
864 | break; |
---|
865 | case AF_INET: |
---|
866 | sep->se_ctrladdr_in.sin_family = AF_INET; |
---|
867 | if (!strcmp(sep->se_hostaddr,"*")) |
---|
868 | sep->se_ctrladdr_in.sin_addr.s_addr = |
---|
869 | INADDR_ANY; |
---|
870 | else if ((sep->se_ctrladdr_in.sin_addr.s_addr = |
---|
871 | inet_addr(sep->se_hostaddr)) == -1) { |
---|
872 | /* Do we really want to support hostname lookups here? */ |
---|
873 | struct hostent *hp; |
---|
874 | hp = gethostbyname(sep->se_hostaddr); |
---|
875 | if (hp == 0) { |
---|
876 | syslog(LOG_ERR, "%s: unknown host", |
---|
877 | sep->se_hostaddr); |
---|
878 | sep->se_checked = 0; |
---|
879 | continue; |
---|
880 | } else if (hp->h_addrtype != AF_INET) { |
---|
881 | syslog(LOG_ERR, |
---|
882 | "%s: address isn't an Internet address", |
---|
883 | sep->se_hostaddr); |
---|
884 | sep->se_checked = 0; |
---|
885 | continue; |
---|
886 | } else if (hp->h_length != sizeof(struct in_addr)) { |
---|
887 | syslog(LOG_ERR, |
---|
888 | "%s: address size wrong (under DNS corruption attack?)", |
---|
889 | sep->se_hostaddr); |
---|
890 | sep->se_checked = 0; |
---|
891 | continue; |
---|
892 | } else { |
---|
893 | memcpy(&sep->se_ctrladdr_in.sin_addr, |
---|
894 | hp->h_addr_list[0], |
---|
895 | sizeof(struct in_addr)); |
---|
896 | } |
---|
897 | } |
---|
898 | if (ISMUX(sep)) { |
---|
899 | sep->se_fd = -1; |
---|
900 | continue; |
---|
901 | } |
---|
902 | sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr_in); |
---|
903 | if (isrpcservice(sep)) { |
---|
904 | struct rpcent *rp; |
---|
905 | |
---|
906 | sep->se_rpcprog = atoi(sep->se_service); |
---|
907 | if (sep->se_rpcprog == 0) { |
---|
908 | rp = getrpcbyname(sep->se_service); |
---|
909 | if (rp == 0) { |
---|
910 | syslog(LOG_ERR, |
---|
911 | "%s/%s: unknown service", |
---|
912 | sep->se_service, |
---|
913 | sep->se_proto); |
---|
914 | sep->se_checked = 0; |
---|
915 | continue; |
---|
916 | } |
---|
917 | sep->se_rpcprog = rp->r_number; |
---|
918 | } |
---|
919 | if (sep->se_fd == -1 && !ISMUX(sep) && |
---|
920 | (!sep->se_switched || access_on)) |
---|
921 | setup(sep); |
---|
922 | else if (sep->se_fd != -1 && !ISMUX(sep) && |
---|
923 | (sep->se_switched && !access_on)) { |
---|
924 | close_sep(sep); |
---|
925 | unregister_rpc(sep); |
---|
926 | } |
---|
927 | if (sep->se_fd != -1) |
---|
928 | register_rpc(sep); |
---|
929 | } else { |
---|
930 | u_short port = htons(atoi(sep->se_service)); |
---|
931 | |
---|
932 | if (!port) { |
---|
933 | sp = getservbyname(sep->se_service, |
---|
934 | sep->se_proto); |
---|
935 | if (sp == 0) { |
---|
936 | syslog(LOG_ERR, |
---|
937 | "%s/%s: unknown service", |
---|
938 | sep->se_service, |
---|
939 | sep->se_proto); |
---|
940 | sep->se_checked = 0; |
---|
941 | continue; |
---|
942 | } |
---|
943 | port = sp->s_port; |
---|
944 | } |
---|
945 | if (port != sep->se_ctrladdr_in.sin_port) { |
---|
946 | sep->se_ctrladdr_in.sin_port = port; |
---|
947 | if (sep->se_fd >= 0) |
---|
948 | close_sep(sep); |
---|
949 | } |
---|
950 | if (sep->se_fd == -1 && !ISMUX(sep) && |
---|
951 | (!sep->se_switched || access_on)) |
---|
952 | setup(sep); |
---|
953 | else if (sep->se_fd != -1 && !ISMUX(sep) && |
---|
954 | (sep->se_switched && !access_on)) |
---|
955 | close_sep(sep); |
---|
956 | } |
---|
957 | } |
---|
958 | } |
---|
959 | endconfig(); |
---|
960 | /* |
---|
961 | * Purge anything not looked at above. |
---|
962 | */ |
---|
963 | sigprocmask(SIG_BLOCK, &sig_block, &omask); |
---|
964 | sepp = &servtab; |
---|
965 | while ((sep = *sepp)) { |
---|
966 | if (sep->se_checked) { |
---|
967 | sepp = &sep->se_next; |
---|
968 | continue; |
---|
969 | } |
---|
970 | *sepp = sep->se_next; |
---|
971 | if (sep->se_fd >= 0) |
---|
972 | close_sep(sep); |
---|
973 | if (isrpcservice(sep)) |
---|
974 | unregister_rpc(sep); |
---|
975 | if (sep->se_family == AF_UNIX) |
---|
976 | (void)unlink(sep->se_service); |
---|
977 | if (debug) |
---|
978 | print_service("FREE", sep); |
---|
979 | freeconfig(sep); |
---|
980 | free((char *)sep); |
---|
981 | } |
---|
982 | sigprocmask(SIG_SETMASK, &omask, NULL); |
---|
983 | } |
---|
984 | |
---|
985 | void |
---|
986 | access_switch(signo) |
---|
987 | int signo; |
---|
988 | { |
---|
989 | struct servtab *sep, **sepp; |
---|
990 | sigset_t omask; |
---|
991 | int on; |
---|
992 | |
---|
993 | on = signo == SIGUSR1; |
---|
994 | if (on == access_on) |
---|
995 | return; |
---|
996 | |
---|
997 | sigprocmask(SIG_BLOCK, &sig_block, &omask); |
---|
998 | access_on = on; |
---|
999 | sepp = &servtab; |
---|
1000 | while (sep = *sepp) { |
---|
1001 | sepp = &sep->se_next; |
---|
1002 | if (!sep->se_switched) |
---|
1003 | continue; |
---|
1004 | if (access_on) |
---|
1005 | setup(sep); |
---|
1006 | else |
---|
1007 | close_sep(sep); |
---|
1008 | } |
---|
1009 | sigprocmask(SIG_SETMASK, &omask, NULL); |
---|
1010 | } |
---|
1011 | |
---|
1012 | void |
---|
1013 | retry(signo) |
---|
1014 | int signo; |
---|
1015 | { |
---|
1016 | struct servtab *sep; |
---|
1017 | |
---|
1018 | timingout = 0; |
---|
1019 | for (sep = servtab; sep; sep = sep->se_next) { |
---|
1020 | if (sep->se_fd == -1 && !ISMUX(sep) && |
---|
1021 | (!sep->se_switched || access_on)) { |
---|
1022 | switch (sep->se_family) { |
---|
1023 | case AF_UNIX: |
---|
1024 | case AF_INET: |
---|
1025 | setup(sep); |
---|
1026 | if (sep->se_fd != -1 && isrpcservice(sep)) |
---|
1027 | register_rpc(sep); |
---|
1028 | break; |
---|
1029 | } |
---|
1030 | } |
---|
1031 | } |
---|
1032 | } |
---|
1033 | |
---|
1034 | void |
---|
1035 | goaway(signo) |
---|
1036 | int signo; |
---|
1037 | { |
---|
1038 | struct servtab *sep; |
---|
1039 | |
---|
1040 | for (sep = servtab; sep; sep = sep->se_next) { |
---|
1041 | if (sep->se_fd == -1) |
---|
1042 | continue; |
---|
1043 | |
---|
1044 | switch (sep->se_family) { |
---|
1045 | case AF_UNIX: |
---|
1046 | (void)unlink(sep->se_service); |
---|
1047 | break; |
---|
1048 | case AF_INET: |
---|
1049 | if (sep->se_wait == 1 && isrpcservice(sep)) |
---|
1050 | unregister_rpc(sep); |
---|
1051 | break; |
---|
1052 | } |
---|
1053 | (void)close(sep->se_fd); |
---|
1054 | } |
---|
1055 | (void)unlink(PIDFILE); |
---|
1056 | exit(0); |
---|
1057 | } |
---|
1058 | |
---|
1059 | void |
---|
1060 | setup(sep) |
---|
1061 | struct servtab *sep; |
---|
1062 | { |
---|
1063 | int on = 1; |
---|
1064 | |
---|
1065 | if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) { |
---|
1066 | if (debug) |
---|
1067 | fprintf(stderr, "socket failed on %s/%s: %s\n", |
---|
1068 | sep->se_service, sep->se_proto, strerror(errno)); |
---|
1069 | syslog(LOG_ERR, "%s/%s: socket: %m", |
---|
1070 | sep->se_service, sep->se_proto); |
---|
1071 | return; |
---|
1072 | } |
---|
1073 | /* Set all listening sockets to close-on-exec. */ |
---|
1074 | if (fcntl(sep->se_fd, F_SETFD, FD_CLOEXEC) < 0) |
---|
1075 | syslog(LOG_ERR, "fcntl (F_SETFD, FD_CLOEXEC): %m"); |
---|
1076 | #define turnon(fd, opt) \ |
---|
1077 | setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on)) |
---|
1078 | if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) && |
---|
1079 | turnon(sep->se_fd, SO_DEBUG) < 0) |
---|
1080 | syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); |
---|
1081 | if (turnon(sep->se_fd, SO_REUSEADDR) < 0) |
---|
1082 | syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m"); |
---|
1083 | #undef turnon |
---|
1084 | if (bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size) < 0) { |
---|
1085 | if (debug) |
---|
1086 | fprintf(stderr, "bind failed on %s/%s: %s\n", |
---|
1087 | sep->se_service, sep->se_proto, strerror(errno)); |
---|
1088 | syslog(LOG_ERR, "%s/%s: bind: %m", |
---|
1089 | sep->se_service, sep->se_proto); |
---|
1090 | (void) close(sep->se_fd); |
---|
1091 | sep->se_fd = -1; |
---|
1092 | if (!timingout) { |
---|
1093 | timingout = 1; |
---|
1094 | alarm(RETRYTIME); |
---|
1095 | } |
---|
1096 | return; |
---|
1097 | } |
---|
1098 | if (sep->se_socktype == SOCK_STREAM) |
---|
1099 | listen(sep->se_fd, 10); |
---|
1100 | |
---|
1101 | FD_SET(sep->se_fd, &allsock); |
---|
1102 | nsock++; |
---|
1103 | if (sep->se_fd > maxsock) { |
---|
1104 | maxsock = sep->se_fd; |
---|
1105 | if (maxsock > rlim_ofile_cur - FD_MARGIN) |
---|
1106 | bump_nofile(); |
---|
1107 | } |
---|
1108 | if (debug) |
---|
1109 | fprintf(stderr, "registered %s on %d\n", |
---|
1110 | sep->se_server, sep->se_fd); |
---|
1111 | } |
---|
1112 | |
---|
1113 | /* |
---|
1114 | * Finish with a service and its socket. |
---|
1115 | */ |
---|
1116 | void |
---|
1117 | close_sep(sep) |
---|
1118 | struct servtab *sep; |
---|
1119 | { |
---|
1120 | if (sep->se_fd >= 0) { |
---|
1121 | nsock--; |
---|
1122 | FD_CLR(sep->se_fd, &allsock); |
---|
1123 | (void) close(sep->se_fd); |
---|
1124 | sep->se_fd = -1; |
---|
1125 | } |
---|
1126 | sep->se_count = 0; |
---|
1127 | /* |
---|
1128 | * Don't keep the pid of this running deamon: when reapchild() |
---|
1129 | * reaps this pid, it would erroneously increment nsock. |
---|
1130 | */ |
---|
1131 | if (sep->se_wait > 1) |
---|
1132 | sep->se_wait = 1; |
---|
1133 | } |
---|
1134 | |
---|
1135 | void |
---|
1136 | register_rpc(sep) |
---|
1137 | struct servtab *sep; |
---|
1138 | { |
---|
1139 | #ifdef RPC |
---|
1140 | int n; |
---|
1141 | struct sockaddr_in sin; |
---|
1142 | struct protoent *pp; |
---|
1143 | |
---|
1144 | if ((pp = getprotobyname(sep->se_proto+4)) == NULL) { |
---|
1145 | syslog(LOG_ERR, "%s: getproto: %m", |
---|
1146 | sep->se_proto); |
---|
1147 | return; |
---|
1148 | } |
---|
1149 | n = sizeof sin; |
---|
1150 | if (getsockname(sep->se_fd, (struct sockaddr *)&sin, &n) < 0) { |
---|
1151 | syslog(LOG_ERR, "%s/%s: getsockname: %m", |
---|
1152 | sep->se_service, sep->se_proto); |
---|
1153 | return; |
---|
1154 | } |
---|
1155 | |
---|
1156 | for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) { |
---|
1157 | if (debug) |
---|
1158 | fprintf(stderr, "pmap_set: %u %u %u %u\n", |
---|
1159 | sep->se_rpcprog, n, pp->p_proto, |
---|
1160 | ntohs(sin.sin_port)); |
---|
1161 | (void)pmap_unset(sep->se_rpcprog, n); |
---|
1162 | if (!pmap_set(sep->se_rpcprog, n, pp->p_proto, ntohs(sin.sin_port))) |
---|
1163 | syslog(LOG_ERR, "pmap_set: %u %u %u %u: %m", |
---|
1164 | sep->se_rpcprog, n, pp->p_proto, |
---|
1165 | ntohs(sin.sin_port)); |
---|
1166 | } |
---|
1167 | #endif /* RPC */ |
---|
1168 | } |
---|
1169 | |
---|
1170 | void |
---|
1171 | unregister_rpc(sep) |
---|
1172 | struct servtab *sep; |
---|
1173 | { |
---|
1174 | #ifdef RPC |
---|
1175 | int n; |
---|
1176 | |
---|
1177 | for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) { |
---|
1178 | if (debug) |
---|
1179 | fprintf(stderr, "pmap_unset(%u, %u)\n", |
---|
1180 | sep->se_rpcprog, n); |
---|
1181 | if (!pmap_unset(sep->se_rpcprog, n)) |
---|
1182 | syslog(LOG_ERR, "pmap_unset(%u, %u)\n", |
---|
1183 | sep->se_rpcprog, n); |
---|
1184 | } |
---|
1185 | #endif /* RPC */ |
---|
1186 | } |
---|
1187 | |
---|
1188 | |
---|
1189 | struct servtab * |
---|
1190 | enter(cp) |
---|
1191 | struct servtab *cp; |
---|
1192 | { |
---|
1193 | struct servtab *sep; |
---|
1194 | sigset_t omask; |
---|
1195 | |
---|
1196 | sep = (struct servtab *)malloc(sizeof (*sep)); |
---|
1197 | if (sep == (struct servtab *)0) { |
---|
1198 | syslog(LOG_ERR, "Out of memory."); |
---|
1199 | exit(-1); |
---|
1200 | } |
---|
1201 | *sep = *cp; |
---|
1202 | sep->se_fd = -1; |
---|
1203 | sep->se_rpcprog = -1; |
---|
1204 | sigprocmask(SIG_BLOCK, &sig_block, &omask); |
---|
1205 | sep->se_next = servtab; |
---|
1206 | servtab = sep; |
---|
1207 | sigprocmask(SIG_SETMASK, &omask, NULL); |
---|
1208 | return (sep); |
---|
1209 | } |
---|
1210 | |
---|
1211 | FILE *fconfig = NULL; |
---|
1212 | struct servtab serv; |
---|
1213 | char line[BUFSIZ]; |
---|
1214 | char *defhost; |
---|
1215 | |
---|
1216 | int |
---|
1217 | setconfig() |
---|
1218 | { |
---|
1219 | if (defhost) free(defhost); |
---|
1220 | defhost = newstr("*"); |
---|
1221 | if (fconfig != NULL) { |
---|
1222 | fseek(fconfig, 0L, SEEK_SET); |
---|
1223 | return (1); |
---|
1224 | } |
---|
1225 | fconfig = fopen(CONFIG, "r"); |
---|
1226 | return (fconfig != NULL); |
---|
1227 | } |
---|
1228 | |
---|
1229 | void |
---|
1230 | endconfig() |
---|
1231 | { |
---|
1232 | if (fconfig) { |
---|
1233 | (void) fclose(fconfig); |
---|
1234 | fconfig = NULL; |
---|
1235 | } |
---|
1236 | if (defhost) { |
---|
1237 | free(defhost); |
---|
1238 | defhost = 0; |
---|
1239 | } |
---|
1240 | } |
---|
1241 | |
---|
1242 | struct servtab * |
---|
1243 | getconfigent() |
---|
1244 | { |
---|
1245 | struct servtab *sep = &serv; |
---|
1246 | int argc; |
---|
1247 | char *cp, *arg; |
---|
1248 | static char TCPMUX_TOKEN[] = "tcpmux/"; |
---|
1249 | #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1) |
---|
1250 | char *hostdelim; |
---|
1251 | |
---|
1252 | more: |
---|
1253 | #ifdef MULOG |
---|
1254 | while ((cp = nextline(fconfig)) && (*cp == '#' || *cp == '\0')) { |
---|
1255 | /* Avoid use of `skip' if there is a danger of it looking |
---|
1256 | * at continuation lines. |
---|
1257 | */ |
---|
1258 | do { |
---|
1259 | cp++; |
---|
1260 | } while (*cp == ' ' || *cp == '\t'); |
---|
1261 | if (*cp == '\0') |
---|
1262 | continue; |
---|
1263 | if ((arg = skip(&cp)) == NULL) |
---|
1264 | continue; |
---|
1265 | if (strcmp(arg, "DOMAIN")) |
---|
1266 | continue; |
---|
1267 | if (curdom) |
---|
1268 | free(curdom); |
---|
1269 | curdom = NULL; |
---|
1270 | while (*cp == ' ' || *cp == '\t') |
---|
1271 | cp++; |
---|
1272 | if (*cp == '\0') |
---|
1273 | continue; |
---|
1274 | arg = cp; |
---|
1275 | while (*cp && *cp != ' ' && *cp != '\t') |
---|
1276 | cp++; |
---|
1277 | if (*cp != '\0') |
---|
1278 | *cp++ = '\0'; |
---|
1279 | curdom = newstr(arg); |
---|
1280 | } |
---|
1281 | #else |
---|
1282 | while ((cp = nextline(fconfig)) && (*cp == '#' || *cp == '\0')) |
---|
1283 | ; |
---|
1284 | #endif |
---|
1285 | if (cp == NULL) |
---|
1286 | return ((struct servtab *)0); |
---|
1287 | /* |
---|
1288 | * clear the static buffer, since some fields (se_ctrladdr, |
---|
1289 | * for example) don't get initialized here. |
---|
1290 | */ |
---|
1291 | memset((caddr_t)sep, 0, sizeof *sep); |
---|
1292 | arg = skip(&cp); |
---|
1293 | if (cp == NULL) { |
---|
1294 | /* got an empty line containing just blanks/tabs. */ |
---|
1295 | goto more; |
---|
1296 | } |
---|
1297 | /* Check for a host name. */ |
---|
1298 | hostdelim = strrchr(arg, ':'); |
---|
1299 | if (hostdelim) { |
---|
1300 | *hostdelim = '\0'; |
---|
1301 | sep->se_hostaddr = newstr(arg); |
---|
1302 | arg = hostdelim + 1; |
---|
1303 | /* |
---|
1304 | * If the line is of the form `host:', then just change the |
---|
1305 | * default host for the following lines. |
---|
1306 | */ |
---|
1307 | if (*arg == '\0') { |
---|
1308 | arg = skip(&cp); |
---|
1309 | if (cp == NULL) { |
---|
1310 | free(defhost); |
---|
1311 | defhost = sep->se_hostaddr; |
---|
1312 | goto more; |
---|
1313 | } |
---|
1314 | } |
---|
1315 | } else |
---|
1316 | sep->se_hostaddr = newstr(defhost); |
---|
1317 | if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) { |
---|
1318 | char *c = arg + MUX_LEN; |
---|
1319 | if (*c == '+') { |
---|
1320 | sep->se_type = MUXPLUS_TYPE; |
---|
1321 | c++; |
---|
1322 | } else |
---|
1323 | sep->se_type = MUX_TYPE; |
---|
1324 | sep->se_service = newstr(c); |
---|
1325 | } else { |
---|
1326 | sep->se_service = newstr(arg); |
---|
1327 | sep->se_type = NORM_TYPE; |
---|
1328 | } |
---|
1329 | |
---|
1330 | arg = sskip(&cp); |
---|
1331 | if (strcmp(arg, "stream") == 0) |
---|
1332 | sep->se_socktype = SOCK_STREAM; |
---|
1333 | else if (strcmp(arg, "dgram") == 0) |
---|
1334 | sep->se_socktype = SOCK_DGRAM; |
---|
1335 | else if (strcmp(arg, "rdm") == 0) |
---|
1336 | sep->se_socktype = SOCK_RDM; |
---|
1337 | else if (strcmp(arg, "seqpacket") == 0) |
---|
1338 | sep->se_socktype = SOCK_SEQPACKET; |
---|
1339 | else if (strcmp(arg, "raw") == 0) |
---|
1340 | sep->se_socktype = SOCK_RAW; |
---|
1341 | else |
---|
1342 | sep->se_socktype = -1; |
---|
1343 | |
---|
1344 | sep->se_proto = newstr(sskip(&cp)); |
---|
1345 | if (strcmp(sep->se_proto, "unix") == 0) { |
---|
1346 | sep->se_family = AF_UNIX; |
---|
1347 | } else { |
---|
1348 | sep->se_family = AF_INET; |
---|
1349 | if (strncmp(sep->se_proto, "rpc/", 4) == 0) { |
---|
1350 | #ifdef RPC |
---|
1351 | char *cp, *ccp; |
---|
1352 | cp = strchr(sep->se_service, '/'); |
---|
1353 | if (cp == 0) { |
---|
1354 | syslog(LOG_ERR, "%s: no rpc version", |
---|
1355 | sep->se_service); |
---|
1356 | goto more; |
---|
1357 | } |
---|
1358 | *cp++ = '\0'; |
---|
1359 | sep->se_rpcversl = sep->se_rpcversh = |
---|
1360 | strtol(cp, &ccp, 0); |
---|
1361 | if (ccp == cp) { |
---|
1362 | badafterall: |
---|
1363 | syslog(LOG_ERR, "%s/%s: bad rpc version", |
---|
1364 | sep->se_service, cp); |
---|
1365 | goto more; |
---|
1366 | } |
---|
1367 | if (*ccp == '-') { |
---|
1368 | cp = ccp + 1; |
---|
1369 | sep->se_rpcversh = strtol(cp, &ccp, 0); |
---|
1370 | if (ccp == cp) |
---|
1371 | goto badafterall; |
---|
1372 | } |
---|
1373 | #else |
---|
1374 | syslog(LOG_ERR, "%s: rpc services not suported", |
---|
1375 | sep->se_service); |
---|
1376 | goto more; |
---|
1377 | #endif /* RPC */ |
---|
1378 | } |
---|
1379 | } |
---|
1380 | arg = sskip(&cp); |
---|
1381 | { |
---|
1382 | char *cp; |
---|
1383 | cp = strchr(arg, '.'); |
---|
1384 | if (cp) { |
---|
1385 | *cp++ = '\0'; |
---|
1386 | sep->se_max = atoi(cp); |
---|
1387 | } else |
---|
1388 | sep->se_max = TOOMANY; |
---|
1389 | } |
---|
1390 | sep->se_wait = strcmp(arg, "wait") == 0; |
---|
1391 | if (ISMUX(sep)) { |
---|
1392 | /* |
---|
1393 | * Silently enforce "nowait" for TCPMUX services since |
---|
1394 | * they don't have an assigned port to listen on. |
---|
1395 | */ |
---|
1396 | sep->se_wait = 0; |
---|
1397 | |
---|
1398 | if (strcmp(sep->se_proto, "tcp")) { |
---|
1399 | syslog(LOG_ERR, |
---|
1400 | "%s: bad protocol for tcpmux service %s", |
---|
1401 | CONFIG, sep->se_service); |
---|
1402 | goto more; |
---|
1403 | } |
---|
1404 | if (sep->se_socktype != SOCK_STREAM) { |
---|
1405 | syslog(LOG_ERR, |
---|
1406 | "%s: bad socket type for tcpmux service %s", |
---|
1407 | CONFIG, sep->se_service); |
---|
1408 | goto more; |
---|
1409 | } |
---|
1410 | } |
---|
1411 | arg = sskip(&cp); |
---|
1412 | sep->se_switched = strcmp(arg, "switched") == 0; |
---|
1413 | sep->se_user = newstr(sskip(&cp)); |
---|
1414 | if ((sep->se_group = strchr(sep->se_user, '.'))) |
---|
1415 | *sep->se_group++ = '\0'; |
---|
1416 | sep->se_server = newstr(sskip(&cp)); |
---|
1417 | if (strcmp(sep->se_server, "internal") == 0) { |
---|
1418 | struct biltin *bi; |
---|
1419 | |
---|
1420 | for (bi = biltins; bi->bi_service; bi++) |
---|
1421 | if (bi->bi_socktype == sep->se_socktype && |
---|
1422 | strcmp(bi->bi_service, sep->se_service) == 0) |
---|
1423 | break; |
---|
1424 | if (bi->bi_service == 0) { |
---|
1425 | syslog(LOG_ERR, "internal service %s unknown", |
---|
1426 | sep->se_service); |
---|
1427 | goto more; |
---|
1428 | } |
---|
1429 | sep->se_bi = bi; |
---|
1430 | sep->se_wait = bi->bi_wait; |
---|
1431 | } else |
---|
1432 | sep->se_bi = NULL; |
---|
1433 | argc = 0; |
---|
1434 | for (arg = skip(&cp); cp; arg = skip(&cp)) { |
---|
1435 | #if MULOG |
---|
1436 | char *colon; |
---|
1437 | |
---|
1438 | if (argc == 0 && (colon = strrchr(arg, ':'))) { |
---|
1439 | while (arg < colon) { |
---|
1440 | int x; |
---|
1441 | char *ccp; |
---|
1442 | |
---|
1443 | switch (*arg++) { |
---|
1444 | case 'l': |
---|
1445 | x = 1; |
---|
1446 | if (isdigit(*arg)) { |
---|
1447 | x = strtol(arg, &ccp, 0); |
---|
1448 | if (ccp == arg) |
---|
1449 | break; |
---|
1450 | arg = ccp; |
---|
1451 | } |
---|
1452 | sep->se_log &= ~MULOG_RFC931; |
---|
1453 | sep->se_log |= x; |
---|
1454 | break; |
---|
1455 | case 'a': |
---|
1456 | sep->se_log |= MULOG_RFC931; |
---|
1457 | break; |
---|
1458 | default: |
---|
1459 | break; |
---|
1460 | } |
---|
1461 | } |
---|
1462 | arg = colon + 1; |
---|
1463 | } |
---|
1464 | #endif |
---|
1465 | if (argc < MAXARGV) |
---|
1466 | sep->se_argv[argc++] = newstr(arg); |
---|
1467 | } |
---|
1468 | while (argc <= MAXARGV) |
---|
1469 | sep->se_argv[argc++] = NULL; |
---|
1470 | return (sep); |
---|
1471 | } |
---|
1472 | |
---|
1473 | void |
---|
1474 | freeconfig(cp) |
---|
1475 | struct servtab *cp; |
---|
1476 | { |
---|
1477 | int i; |
---|
1478 | |
---|
1479 | if (cp->se_hostaddr) |
---|
1480 | free(cp->se_hostaddr); |
---|
1481 | if (cp->se_service) |
---|
1482 | free(cp->se_service); |
---|
1483 | if (cp->se_proto) |
---|
1484 | free(cp->se_proto); |
---|
1485 | if (cp->se_user) |
---|
1486 | free(cp->se_user); |
---|
1487 | /* Note: se_group is part of the newstr'ed se_user */ |
---|
1488 | if (cp->se_server) |
---|
1489 | free(cp->se_server); |
---|
1490 | for (i = 0; i < MAXARGV; i++) |
---|
1491 | if (cp->se_argv[i]) |
---|
1492 | free(cp->se_argv[i]); |
---|
1493 | } |
---|
1494 | |
---|
1495 | |
---|
1496 | /* |
---|
1497 | * Safe skip - if skip returns null, log a syntax error in the |
---|
1498 | * configuration file and exit. |
---|
1499 | */ |
---|
1500 | char * |
---|
1501 | sskip(cpp) |
---|
1502 | char **cpp; |
---|
1503 | { |
---|
1504 | char *cp; |
---|
1505 | |
---|
1506 | cp = skip(cpp); |
---|
1507 | if (cp == NULL) { |
---|
1508 | syslog(LOG_ERR, "%s: syntax error", CONFIG); |
---|
1509 | exit(-1); |
---|
1510 | } |
---|
1511 | return (cp); |
---|
1512 | } |
---|
1513 | |
---|
1514 | char * |
---|
1515 | skip(cpp) |
---|
1516 | char **cpp; |
---|
1517 | { |
---|
1518 | char *cp = *cpp; |
---|
1519 | char *start; |
---|
1520 | |
---|
1521 | if (*cpp == NULL) |
---|
1522 | return ((char *)0); |
---|
1523 | |
---|
1524 | again: |
---|
1525 | while (*cp == ' ' || *cp == '\t') |
---|
1526 | cp++; |
---|
1527 | if (*cp == '\0') { |
---|
1528 | int c; |
---|
1529 | |
---|
1530 | c = getc(fconfig); |
---|
1531 | (void) ungetc(c, fconfig); |
---|
1532 | if (c == ' ' || c == '\t') |
---|
1533 | if ((cp = nextline(fconfig))) |
---|
1534 | goto again; |
---|
1535 | *cpp = (char *)0; |
---|
1536 | return ((char *)0); |
---|
1537 | } |
---|
1538 | start = cp; |
---|
1539 | while (*cp && *cp != ' ' && *cp != '\t') |
---|
1540 | cp++; |
---|
1541 | if (*cp != '\0') |
---|
1542 | *cp++ = '\0'; |
---|
1543 | *cpp = cp; |
---|
1544 | return (start); |
---|
1545 | } |
---|
1546 | |
---|
1547 | char * |
---|
1548 | nextline(fd) |
---|
1549 | FILE *fd; |
---|
1550 | { |
---|
1551 | char *cp; |
---|
1552 | |
---|
1553 | if (fgets(line, sizeof (line), fd) == NULL) |
---|
1554 | return ((char *)0); |
---|
1555 | cp = strchr(line, '\n'); |
---|
1556 | if (cp) |
---|
1557 | *cp = '\0'; |
---|
1558 | return (line); |
---|
1559 | } |
---|
1560 | |
---|
1561 | char * |
---|
1562 | newstr(cp) |
---|
1563 | char *cp; |
---|
1564 | { |
---|
1565 | if ((cp = strdup(cp ? cp : ""))) |
---|
1566 | return (cp); |
---|
1567 | syslog(LOG_ERR, "strdup: %m"); |
---|
1568 | exit(-1); |
---|
1569 | } |
---|
1570 | |
---|
1571 | void |
---|
1572 | inetd_setproctitle(a, s) |
---|
1573 | char *a; |
---|
1574 | int s; |
---|
1575 | { |
---|
1576 | int size; |
---|
1577 | char *cp; |
---|
1578 | struct sockaddr_in sin; |
---|
1579 | char buf[80]; |
---|
1580 | |
---|
1581 | cp = Argv[0]; |
---|
1582 | size = sizeof(sin); |
---|
1583 | if (getpeername(s, (struct sockaddr *)&sin, &size) == 0) |
---|
1584 | (void)sprintf(buf, "-%.*s [%s]", sizeof(buf) - 20, a, |
---|
1585 | inet_ntoa(sin.sin_addr)); |
---|
1586 | else |
---|
1587 | (void)sprintf(buf, "-%.*s", sizeof(buf) - 2, a); |
---|
1588 | strncpy(cp, buf, LastArg - cp); |
---|
1589 | cp += strlen(cp); |
---|
1590 | while (cp < LastArg) |
---|
1591 | *cp++ = ' '; |
---|
1592 | } |
---|
1593 | |
---|
1594 | void |
---|
1595 | logpid() |
---|
1596 | { |
---|
1597 | FILE *fp; |
---|
1598 | |
---|
1599 | if ((fp = fopen(PIDFILE, "w")) != NULL) { |
---|
1600 | fprintf(fp, "%u\n", getpid()); |
---|
1601 | (void)fclose(fp); |
---|
1602 | } |
---|
1603 | } |
---|
1604 | |
---|
1605 | void |
---|
1606 | bump_nofile() |
---|
1607 | { |
---|
1608 | #ifdef RLIMIT_NOFILE |
---|
1609 | |
---|
1610 | #define FD_CHUNK 32 |
---|
1611 | |
---|
1612 | struct rlimit rl; |
---|
1613 | |
---|
1614 | if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { |
---|
1615 | syslog(LOG_ERR, "getrlimit: %m"); |
---|
1616 | return; |
---|
1617 | } |
---|
1618 | #ifndef MIN |
---|
1619 | #define MIN(x,y) ( (x) < (y) ? (x) : (y) ) |
---|
1620 | #endif |
---|
1621 | rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK); |
---|
1622 | if (rl.rlim_cur <= rlim_ofile_cur) { |
---|
1623 | syslog(LOG_ERR, |
---|
1624 | "bump_nofile: cannot extend file limit, max = %d", |
---|
1625 | (int)rl.rlim_cur); |
---|
1626 | return; |
---|
1627 | } |
---|
1628 | |
---|
1629 | if (setrlimit(RLIMIT_NOFILE, &rl) < 0) { |
---|
1630 | syslog(LOG_ERR, "setrlimit: %m"); |
---|
1631 | return; |
---|
1632 | } |
---|
1633 | |
---|
1634 | rlim_ofile_cur = rl.rlim_cur; |
---|
1635 | return; |
---|
1636 | |
---|
1637 | #else |
---|
1638 | syslog(LOG_ERR, "bump_nofile: cannot extend file limit"); |
---|
1639 | return; |
---|
1640 | #endif |
---|
1641 | } |
---|
1642 | |
---|
1643 | /* |
---|
1644 | * Internet services provided internally by inetd: |
---|
1645 | */ |
---|
1646 | #define BUFSIZE 4096 |
---|
1647 | |
---|
1648 | /* ARGSUSED */ |
---|
1649 | void |
---|
1650 | echo_stream(s, sep) /* Echo service -- echo data back */ |
---|
1651 | int s; |
---|
1652 | struct servtab *sep; |
---|
1653 | { |
---|
1654 | char buffer[BUFSIZE]; |
---|
1655 | int i; |
---|
1656 | |
---|
1657 | inetd_setproctitle(sep->se_service, s); |
---|
1658 | while ((i = read(s, buffer, sizeof(buffer))) > 0 && |
---|
1659 | write(s, buffer, i) > 0) |
---|
1660 | ; |
---|
1661 | } |
---|
1662 | |
---|
1663 | /* ARGSUSED */ |
---|
1664 | void |
---|
1665 | echo_dg(s, sep) /* Echo service -- echo data back */ |
---|
1666 | int s; |
---|
1667 | struct servtab *sep; |
---|
1668 | { |
---|
1669 | char buffer[BUFSIZE]; |
---|
1670 | int i, size; |
---|
1671 | struct sockaddr sa; |
---|
1672 | |
---|
1673 | size = sizeof(sa); |
---|
1674 | if ((i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size)) < 0) |
---|
1675 | return; |
---|
1676 | (void) sendto(s, buffer, i, 0, &sa, sizeof(sa)); |
---|
1677 | } |
---|
1678 | |
---|
1679 | /* ARGSUSED */ |
---|
1680 | void |
---|
1681 | discard_stream(s, sep) /* Discard service -- ignore data */ |
---|
1682 | int s; |
---|
1683 | struct servtab *sep; |
---|
1684 | { |
---|
1685 | char buffer[BUFSIZE]; |
---|
1686 | |
---|
1687 | inetd_setproctitle(sep->se_service, s); |
---|
1688 | while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) || |
---|
1689 | errno == EINTR) |
---|
1690 | ; |
---|
1691 | } |
---|
1692 | |
---|
1693 | /* ARGSUSED */ |
---|
1694 | void |
---|
1695 | discard_dg(s, sep) /* Discard service -- ignore data */ |
---|
1696 | int s; |
---|
1697 | struct servtab *sep; |
---|
1698 | { |
---|
1699 | char buffer[BUFSIZE]; |
---|
1700 | |
---|
1701 | (void) read(s, buffer, sizeof(buffer)); |
---|
1702 | } |
---|
1703 | |
---|
1704 | #include <ctype.h> |
---|
1705 | #define LINESIZ 72 |
---|
1706 | char ring[128]; |
---|
1707 | char *endring; |
---|
1708 | |
---|
1709 | void |
---|
1710 | initring() |
---|
1711 | { |
---|
1712 | int i; |
---|
1713 | |
---|
1714 | endring = ring; |
---|
1715 | |
---|
1716 | for (i = 0; i <= 128; ++i) |
---|
1717 | if (isprint(i)) |
---|
1718 | *endring++ = i; |
---|
1719 | } |
---|
1720 | |
---|
1721 | /* ARGSUSED */ |
---|
1722 | void |
---|
1723 | chargen_stream(s, sep) /* Character generator */ |
---|
1724 | int s; |
---|
1725 | struct servtab *sep; |
---|
1726 | { |
---|
1727 | int len; |
---|
1728 | char *rs, text[LINESIZ+2]; |
---|
1729 | |
---|
1730 | inetd_setproctitle(sep->se_service, s); |
---|
1731 | |
---|
1732 | if (!endring) { |
---|
1733 | initring(); |
---|
1734 | rs = ring; |
---|
1735 | } |
---|
1736 | |
---|
1737 | text[LINESIZ] = '\r'; |
---|
1738 | text[LINESIZ + 1] = '\n'; |
---|
1739 | for (rs = ring;;) { |
---|
1740 | if ((len = endring - rs) >= LINESIZ) |
---|
1741 | memmove(text, rs, LINESIZ); |
---|
1742 | else { |
---|
1743 | memmove(text, rs, len); |
---|
1744 | memmove(text + len, ring, LINESIZ - len); |
---|
1745 | } |
---|
1746 | if (++rs == endring) |
---|
1747 | rs = ring; |
---|
1748 | if (write(s, text, sizeof(text)) != sizeof(text)) |
---|
1749 | break; |
---|
1750 | } |
---|
1751 | } |
---|
1752 | |
---|
1753 | /* ARGSUSED */ |
---|
1754 | void |
---|
1755 | chargen_dg(s, sep) /* Character generator */ |
---|
1756 | int s; |
---|
1757 | struct servtab *sep; |
---|
1758 | { |
---|
1759 | struct sockaddr sa; |
---|
1760 | static char *rs; |
---|
1761 | int len, size; |
---|
1762 | char text[LINESIZ+2]; |
---|
1763 | |
---|
1764 | if (endring == 0) { |
---|
1765 | initring(); |
---|
1766 | rs = ring; |
---|
1767 | } |
---|
1768 | |
---|
1769 | size = sizeof(sa); |
---|
1770 | if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0) |
---|
1771 | return; |
---|
1772 | |
---|
1773 | if ((len = endring - rs) >= LINESIZ) |
---|
1774 | memmove(text, rs, LINESIZ); |
---|
1775 | else { |
---|
1776 | memmove(text, rs, len); |
---|
1777 | memmove(text + len, ring, LINESIZ - len); |
---|
1778 | } |
---|
1779 | if (++rs == endring) |
---|
1780 | rs = ring; |
---|
1781 | text[LINESIZ] = '\r'; |
---|
1782 | text[LINESIZ + 1] = '\n'; |
---|
1783 | (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa)); |
---|
1784 | } |
---|
1785 | |
---|
1786 | /* |
---|
1787 | * Return a machine readable date and time, in the form of the |
---|
1788 | * number of seconds since midnight, Jan 1, 1900. Since gettimeofday |
---|
1789 | * returns the number of seconds since midnight, Jan 1, 1970, |
---|
1790 | * we must add 2208988800 seconds to this figure to make up for |
---|
1791 | * some seventy years Bell Labs was asleep. |
---|
1792 | */ |
---|
1793 | |
---|
1794 | long |
---|
1795 | machtime() |
---|
1796 | { |
---|
1797 | struct timeval tv; |
---|
1798 | |
---|
1799 | if (gettimeofday(&tv, (struct timezone *)0) < 0) { |
---|
1800 | if (debug) |
---|
1801 | fprintf(stderr, "Unable to get time of day\n"); |
---|
1802 | return (0L); |
---|
1803 | } |
---|
1804 | #define OFFSET ((u_long)25567 * 24*60*60) |
---|
1805 | return (htonl((long)(tv.tv_sec + OFFSET))); |
---|
1806 | #undef OFFSET |
---|
1807 | } |
---|
1808 | |
---|
1809 | /* ARGSUSED */ |
---|
1810 | void |
---|
1811 | machtime_stream(s, sep) |
---|
1812 | int s; |
---|
1813 | struct servtab *sep; |
---|
1814 | { |
---|
1815 | long result; |
---|
1816 | |
---|
1817 | result = machtime(); |
---|
1818 | (void) write(s, (char *) &result, sizeof(result)); |
---|
1819 | } |
---|
1820 | |
---|
1821 | /* ARGSUSED */ |
---|
1822 | void |
---|
1823 | machtime_dg(s, sep) |
---|
1824 | int s; |
---|
1825 | struct servtab *sep; |
---|
1826 | { |
---|
1827 | long result; |
---|
1828 | struct sockaddr sa; |
---|
1829 | int size; |
---|
1830 | |
---|
1831 | size = sizeof(sa); |
---|
1832 | if (recvfrom(s, (char *)&result, sizeof(result), 0, &sa, &size) < 0) |
---|
1833 | return; |
---|
1834 | result = machtime(); |
---|
1835 | (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa)); |
---|
1836 | } |
---|
1837 | |
---|
1838 | /* ARGSUSED */ |
---|
1839 | void |
---|
1840 | daytime_stream(s, sep) /* Return human-readable time of day */ |
---|
1841 | int s; |
---|
1842 | struct servtab *sep; |
---|
1843 | { |
---|
1844 | char buffer[256]; |
---|
1845 | time_t clock; |
---|
1846 | int len; |
---|
1847 | |
---|
1848 | clock = time((time_t *) 0); |
---|
1849 | |
---|
1850 | len = sprintf(buffer, "%.24s\r\n", ctime(&clock)); |
---|
1851 | (void) write(s, buffer, len); |
---|
1852 | } |
---|
1853 | |
---|
1854 | /* ARGSUSED */ |
---|
1855 | void |
---|
1856 | daytime_dg(s, sep) /* Return human-readable time of day */ |
---|
1857 | int s; |
---|
1858 | struct servtab *sep; |
---|
1859 | { |
---|
1860 | char buffer[256]; |
---|
1861 | time_t clock; |
---|
1862 | struct sockaddr sa; |
---|
1863 | int size, len; |
---|
1864 | |
---|
1865 | clock = time((time_t *) 0); |
---|
1866 | |
---|
1867 | size = sizeof(sa); |
---|
1868 | if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0) |
---|
1869 | return; |
---|
1870 | len = sprintf(buffer, "%.24s\r\n", ctime(&clock)); |
---|
1871 | (void) sendto(s, buffer, len, 0, &sa, sizeof(sa)); |
---|
1872 | } |
---|
1873 | |
---|
1874 | /* |
---|
1875 | * print_service: |
---|
1876 | * Dump relevant information to stderr |
---|
1877 | */ |
---|
1878 | void |
---|
1879 | print_service(action, sep) |
---|
1880 | char *action; |
---|
1881 | struct servtab *sep; |
---|
1882 | { |
---|
1883 | if (isrpcservice(sep)) |
---|
1884 | fprintf(stderr, |
---|
1885 | "%s: %s rpcprog=%d, rpcvers = %d/%d, proto=%s, wait.max=%d.%d, switched=%d, user.group=%s.%s builtin=%lx server=%s\n", |
---|
1886 | action, sep->se_service, |
---|
1887 | sep->se_rpcprog, sep->se_rpcversh, sep->se_rpcversl, sep->se_proto, |
---|
1888 | sep->se_wait, sep->se_max, sep->se_switched, sep->se_user, |
---|
1889 | sep->se_group ? sep->se_group : "", |
---|
1890 | (long)sep->se_bi, sep->se_server); |
---|
1891 | else |
---|
1892 | fprintf(stderr, |
---|
1893 | "%s: %s proto=%s, wait.max=%d.%d, switched=%d, user.group=%s.%s builtin=%lx server=%s\n", |
---|
1894 | action, sep->se_service, sep->se_proto, |
---|
1895 | sep->se_wait, sep->se_max, sep->se_switched, sep->se_user, |
---|
1896 | sep->se_group ? sep->se_group : "", |
---|
1897 | (long)sep->se_bi, sep->se_server); |
---|
1898 | } |
---|
1899 | |
---|
1900 | void |
---|
1901 | usage() |
---|
1902 | { |
---|
1903 | |
---|
1904 | #ifdef LIBWRAP |
---|
1905 | (void)fprintf(stderr, "usage: %s [-dl] [conf]\n", __progname); |
---|
1906 | #else |
---|
1907 | (void)fprintf(stderr, "usage: %s [-d] [conf]\n", __progname); |
---|
1908 | #endif |
---|
1909 | exit(1); |
---|
1910 | } |
---|
1911 | |
---|
1912 | |
---|
1913 | /* |
---|
1914 | * Based on TCPMUX.C by Mark K. Lottor November 1988 |
---|
1915 | * sri-nic::ps:<mkl>tcpmux.c |
---|
1916 | */ |
---|
1917 | |
---|
1918 | static int /* # of characters upto \r,\n or \0 */ |
---|
1919 | getline(fd, buf, len) |
---|
1920 | int fd; |
---|
1921 | char *buf; |
---|
1922 | int len; |
---|
1923 | { |
---|
1924 | int count = 0, n; |
---|
1925 | |
---|
1926 | do { |
---|
1927 | n = read(fd, buf, len-count); |
---|
1928 | if (n == 0) |
---|
1929 | return (count); |
---|
1930 | if (n < 0) |
---|
1931 | return (-1); |
---|
1932 | while (--n >= 0) { |
---|
1933 | if (*buf == '\r' || *buf == '\n' || *buf == '\0') |
---|
1934 | return (count); |
---|
1935 | count++; |
---|
1936 | buf++; |
---|
1937 | } |
---|
1938 | } while (count < len); |
---|
1939 | return (count); |
---|
1940 | } |
---|
1941 | |
---|
1942 | #define MAX_SERV_LEN (256+2) /* 2 bytes for \r\n */ |
---|
1943 | |
---|
1944 | #define strwrite(fd, buf) (void) write(fd, buf, sizeof(buf)-1) |
---|
1945 | |
---|
1946 | void |
---|
1947 | tcpmux(ctrl, sep) |
---|
1948 | int ctrl; |
---|
1949 | struct servtab *sep; |
---|
1950 | { |
---|
1951 | char service[MAX_SERV_LEN+1]; |
---|
1952 | int len; |
---|
1953 | |
---|
1954 | /* Get requested service name */ |
---|
1955 | if ((len = getline(ctrl, service, MAX_SERV_LEN)) < 0) { |
---|
1956 | strwrite(ctrl, "-Error reading service name\r\n"); |
---|
1957 | goto reject; |
---|
1958 | } |
---|
1959 | service[len] = '\0'; |
---|
1960 | |
---|
1961 | if (debug) |
---|
1962 | fprintf(stderr, "tcpmux: someone wants %s\n", service); |
---|
1963 | |
---|
1964 | /* |
---|
1965 | * Help is a required command, and lists available services, |
---|
1966 | * one per line. |
---|
1967 | */ |
---|
1968 | if (!strcasecmp(service, "help")) { |
---|
1969 | strwrite(ctrl, "+Available services:\r\n"); |
---|
1970 | strwrite(ctrl, "help\r\n"); |
---|
1971 | for (sep = servtab; sep; sep = sep->se_next) { |
---|
1972 | if (!ISMUX(sep)) |
---|
1973 | continue; |
---|
1974 | (void)write(ctrl, sep->se_service, |
---|
1975 | strlen(sep->se_service)); |
---|
1976 | strwrite(ctrl, "\r\n"); |
---|
1977 | } |
---|
1978 | goto reject; |
---|
1979 | } |
---|
1980 | |
---|
1981 | /* Try matching a service in inetd.conf with the request */ |
---|
1982 | for (sep = servtab; sep; sep = sep->se_next) { |
---|
1983 | if (!ISMUX(sep)) |
---|
1984 | continue; |
---|
1985 | if (!strcasecmp(service, sep->se_service)) { |
---|
1986 | if (ISMUXPLUS(sep)) |
---|
1987 | strwrite(ctrl, "+Go\r\n"); |
---|
1988 | run_service(ctrl, sep); |
---|
1989 | return; |
---|
1990 | } |
---|
1991 | } |
---|
1992 | strwrite(ctrl, "-Service not available\r\n"); |
---|
1993 | reject: |
---|
1994 | _exit(1); |
---|
1995 | } |
---|
1996 | |
---|
1997 | |
---|
1998 | #ifdef MULOG |
---|
1999 | dolog(sep, ctrl) |
---|
2000 | struct servtab *sep; |
---|
2001 | int ctrl; |
---|
2002 | { |
---|
2003 | struct sockaddr sa; |
---|
2004 | struct sockaddr_in *sin = (struct sockaddr_in *)&sa; |
---|
2005 | int len = sizeof(sa); |
---|
2006 | struct hostent *hp; |
---|
2007 | char *host, *dp, buf[BUFSIZ], *rfc931_name(); |
---|
2008 | int connected = 1; |
---|
2009 | |
---|
2010 | if (sep->se_family != AF_INET) |
---|
2011 | return; |
---|
2012 | |
---|
2013 | if (getpeername(ctrl, &sa, &len) < 0) { |
---|
2014 | if (errno != ENOTCONN) { |
---|
2015 | syslog(LOG_ERR, "getpeername: %m"); |
---|
2016 | return; |
---|
2017 | } |
---|
2018 | if (recvfrom(ctrl, buf, sizeof(buf), MSG_PEEK, &sa, &len) < 0) { |
---|
2019 | syslog(LOG_ERR, "recvfrom: %m"); |
---|
2020 | return; |
---|
2021 | } |
---|
2022 | connected = 0; |
---|
2023 | } |
---|
2024 | if (sa.sa_family != AF_INET) { |
---|
2025 | syslog(LOG_ERR, "unexpected address family %u", sa.sa_family); |
---|
2026 | return; |
---|
2027 | } |
---|
2028 | |
---|
2029 | hp = gethostbyaddr((char *) &sin->sin_addr.s_addr, |
---|
2030 | sizeof (sin->sin_addr.s_addr), AF_INET); |
---|
2031 | |
---|
2032 | host = hp?hp->h_name:inet_ntoa(sin->sin_addr); |
---|
2033 | |
---|
2034 | switch (sep->se_log & ~MULOG_RFC931) { |
---|
2035 | case 0: |
---|
2036 | return; |
---|
2037 | case 1: |
---|
2038 | if (curdom == NULL || *curdom == '\0') |
---|
2039 | break; |
---|
2040 | dp = host + strlen(host) - strlen(curdom); |
---|
2041 | if (dp < host) |
---|
2042 | break; |
---|
2043 | if (debug) |
---|
2044 | fprintf(stderr, "check \"%s\" against curdom \"%s\"\n", |
---|
2045 | host, curdom); |
---|
2046 | if (strcasecmp(dp, curdom) == 0) |
---|
2047 | return; |
---|
2048 | break; |
---|
2049 | case 2: |
---|
2050 | default: |
---|
2051 | break; |
---|
2052 | } |
---|
2053 | |
---|
2054 | openlog("", LOG_NOWAIT, MULOG); |
---|
2055 | |
---|
2056 | if (connected && (sep->se_log & MULOG_RFC931)) |
---|
2057 | syslog(LOG_INFO, "%s@%s wants %s", |
---|
2058 | rfc931_name(sin, ctrl), host, sep->se_service); |
---|
2059 | else |
---|
2060 | syslog(LOG_INFO, "%s wants %s", |
---|
2061 | host, sep->se_service); |
---|
2062 | } |
---|
2063 | |
---|
2064 | /* |
---|
2065 | * From tcp_log by |
---|
2066 | * Wietse Venema, Eindhoven University of Technology, The Netherlands. |
---|
2067 | */ |
---|
2068 | #if 0 |
---|
2069 | static char sccsid[] = "@(#) rfc931.c 1.3 92/08/31 22:54:46"; |
---|
2070 | #endif |
---|
2071 | |
---|
2072 | #include <setjmp.h> |
---|
2073 | |
---|
2074 | #define RFC931_PORT 113 /* Semi-well-known port */ |
---|
2075 | #define TIMEOUT 4 |
---|
2076 | #define TIMEOUT2 10 |
---|
2077 | |
---|
2078 | static sigjmp_buf timebuf; |
---|
2079 | |
---|
2080 | /* timeout - handle timeouts */ |
---|
2081 | |
---|
2082 | static void timeout(sig) |
---|
2083 | int sig; |
---|
2084 | { |
---|
2085 | siglongjmp(timebuf, sig); |
---|
2086 | } |
---|
2087 | |
---|
2088 | /* rfc931_name - return remote user name */ |
---|
2089 | |
---|
2090 | char * |
---|
2091 | rfc931_name(there, ctrl) |
---|
2092 | struct sockaddr_in *there; /* remote link information */ |
---|
2093 | int ctrl; |
---|
2094 | { |
---|
2095 | struct sockaddr_in here; /* local link information */ |
---|
2096 | struct sockaddr_in sin; /* for talking to RFC931 daemon */ |
---|
2097 | int length; |
---|
2098 | int s; |
---|
2099 | unsigned remote; |
---|
2100 | unsigned local; |
---|
2101 | static char user[256]; /* XXX */ |
---|
2102 | char buf[256]; |
---|
2103 | char *cp; |
---|
2104 | char *result = "USER_UNKNOWN"; |
---|
2105 | int len; |
---|
2106 | struct sigaction sa; |
---|
2107 | |
---|
2108 | /* Find out local port number of our stdin. */ |
---|
2109 | |
---|
2110 | length = sizeof(here); |
---|
2111 | if (getsockname(ctrl, (struct sockaddr *) &here, &length) == -1) { |
---|
2112 | syslog(LOG_ERR, "getsockname: %m"); |
---|
2113 | return (result); |
---|
2114 | } |
---|
2115 | /* Set up timer so we won't get stuck. */ |
---|
2116 | |
---|
2117 | if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
---|
2118 | syslog(LOG_ERR, "socket: %m"); |
---|
2119 | return (result); |
---|
2120 | } |
---|
2121 | |
---|
2122 | sin = here; |
---|
2123 | sin.sin_port = htons(0); |
---|
2124 | if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) { |
---|
2125 | syslog(LOG_ERR, "bind: %m"); |
---|
2126 | return (result); |
---|
2127 | } |
---|
2128 | |
---|
2129 | memset(&sa, 0, sizeof(sa)); |
---|
2130 | sa_handler = timeout; |
---|
2131 | sigaction(SIGALRM, &sa, NULL); |
---|
2132 | if (sigsetjmp(timebuf)) { |
---|
2133 | close(s); /* not: fclose(fp) */ |
---|
2134 | return (result); |
---|
2135 | } |
---|
2136 | alarm(TIMEOUT); |
---|
2137 | |
---|
2138 | /* Connect to the RFC931 daemon. */ |
---|
2139 | |
---|
2140 | sin = *there; |
---|
2141 | sin.sin_port = htons(RFC931_PORT); |
---|
2142 | if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) { |
---|
2143 | close(s); |
---|
2144 | alarm(0); |
---|
2145 | return (result); |
---|
2146 | } |
---|
2147 | |
---|
2148 | /* Query the RFC 931 server. Would 13-byte writes ever be broken up? */ |
---|
2149 | (void)sprintf(buf, "%u,%u\r\n", ntohs(there->sin_port), |
---|
2150 | ntohs(here.sin_port)); |
---|
2151 | |
---|
2152 | |
---|
2153 | for (len = 0, cp = buf; len < strlen(buf); ) { |
---|
2154 | int n; |
---|
2155 | |
---|
2156 | if ((n = write(s, cp, strlen(buf) - len)) == -1) { |
---|
2157 | close(s); |
---|
2158 | alarm(0); |
---|
2159 | return (result); |
---|
2160 | } |
---|
2161 | cp += n; |
---|
2162 | len += n; |
---|
2163 | } |
---|
2164 | |
---|
2165 | /* Read response */ |
---|
2166 | for (cp = buf; cp < buf + sizeof(buf) - 1; ) { |
---|
2167 | char c; |
---|
2168 | if (read(s, &c, 1) != 1) { |
---|
2169 | close(s); |
---|
2170 | alarm(0); |
---|
2171 | return (result); |
---|
2172 | } |
---|
2173 | if (c == '\n') |
---|
2174 | break; |
---|
2175 | *cp++ = c; |
---|
2176 | } |
---|
2177 | *cp = '\0'; |
---|
2178 | |
---|
2179 | if (sscanf(buf, "%u , %u : USERID :%*[^:]:%255s", &remote, &local, user) == 3 |
---|
2180 | && ntohs(there->sin_port) == remote |
---|
2181 | && ntohs(here.sin_port) == local) { |
---|
2182 | |
---|
2183 | /* Strip trailing carriage return. */ |
---|
2184 | if (cp = strchr(user, '\r')) |
---|
2185 | *cp = 0; |
---|
2186 | result = user; |
---|
2187 | } |
---|
2188 | |
---|
2189 | alarm(0); |
---|
2190 | close(s); |
---|
2191 | return (result); |
---|
2192 | } |
---|
2193 | #endif |
---|