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