18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 45ff3afceSSheldon Hearn * Copyright (c) 1983, 1991, 1993, 1994 55ff3afceSSheldon Hearn * The Regents of the University of California. All rights reserved. 65ff3afceSSheldon Hearn * 75ff3afceSSheldon Hearn * Redistribution and use in source and binary forms, with or without 85ff3afceSSheldon Hearn * modification, are permitted provided that the following conditions 95ff3afceSSheldon Hearn * are met: 105ff3afceSSheldon Hearn * 1. Redistributions of source code must retain the above copyright 115ff3afceSSheldon Hearn * notice, this list of conditions and the following disclaimer. 125ff3afceSSheldon Hearn * 2. Redistributions in binary form must reproduce the above copyright 135ff3afceSSheldon Hearn * notice, this list of conditions and the following disclaimer in the 145ff3afceSSheldon Hearn * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 165ff3afceSSheldon Hearn * may be used to endorse or promote products derived from this software 175ff3afceSSheldon Hearn * without specific prior written permission. 185ff3afceSSheldon Hearn * 195ff3afceSSheldon Hearn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 205ff3afceSSheldon Hearn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 215ff3afceSSheldon Hearn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 225ff3afceSSheldon Hearn * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 235ff3afceSSheldon Hearn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 245ff3afceSSheldon Hearn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 255ff3afceSSheldon Hearn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 265ff3afceSSheldon Hearn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 275ff3afceSSheldon Hearn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 285ff3afceSSheldon Hearn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 295ff3afceSSheldon Hearn * SUCH DAMAGE. 305ff3afceSSheldon Hearn * 3197d92980SPeter Wemm * $FreeBSD$ 325ff3afceSSheldon Hearn */ 335ff3afceSSheldon Hearn 345ff3afceSSheldon Hearn #include <sys/time.h> 355ff3afceSSheldon Hearn #include <sys/socket.h> 361c8d1174SDavid Malone #include <sys/un.h> 3709b1c357SHajimu UMEMOTO #include <sys/queue.h> 385ff3afceSSheldon Hearn 395ff3afceSSheldon Hearn #include <netinet/in.h> 405ff3afceSSheldon Hearn 415ff3afceSSheldon Hearn #include <stdio.h> 425ff3afceSSheldon Hearn 435ff3afceSSheldon Hearn #define BUFSIZE 8192 445ff3afceSSheldon Hearn #define LINESIZ 72 455ff3afceSSheldon Hearn 465ff3afceSSheldon Hearn #define NORM_TYPE 0 475ff3afceSSheldon Hearn #define MUX_TYPE 1 485ff3afceSSheldon Hearn #define MUXPLUS_TYPE 2 490cac72f4SYoshinobu Inoue #define FAITH_TYPE 4 505ff3afceSSheldon Hearn #define ISMUX(sep) (((sep)->se_type == MUX_TYPE) || \ 515ff3afceSSheldon Hearn ((sep)->se_type == MUXPLUS_TYPE)) 525ff3afceSSheldon Hearn #define ISMUXPLUS(sep) ((sep)->se_type == MUXPLUS_TYPE) 535ff3afceSSheldon Hearn 5409b1c357SHajimu UMEMOTO struct procinfo { 5509b1c357SHajimu UMEMOTO LIST_ENTRY(procinfo) pr_link; 5609b1c357SHajimu UMEMOTO pid_t pr_pid; /* child pid */ 5709b1c357SHajimu UMEMOTO struct conninfo *pr_conn; 5809b1c357SHajimu UMEMOTO }; 5909b1c357SHajimu UMEMOTO 6009b1c357SHajimu UMEMOTO struct conninfo { 6109b1c357SHajimu UMEMOTO LIST_ENTRY(conninfo) co_link; 6209b1c357SHajimu UMEMOTO struct sockaddr_storage co_addr; /* source address */ 6309b1c357SHajimu UMEMOTO int co_numchild; /* current number of children */ 6409b1c357SHajimu UMEMOTO struct procinfo **co_proc; /* array of child proc entry */ 6509b1c357SHajimu UMEMOTO }; 6609b1c357SHajimu UMEMOTO 6709b1c357SHajimu UMEMOTO #define PERIPSIZE 256 6809b1c357SHajimu UMEMOTO 69*c53b0f40SKyle Evans struct stabchild { 70*c53b0f40SKyle Evans LIST_ENTRY(stabchild) sc_link; 71*c53b0f40SKyle Evans pid_t sc_pid; 72*c53b0f40SKyle Evans }; 73*c53b0f40SKyle Evans 745ff3afceSSheldon Hearn struct servtab { 755ff3afceSSheldon Hearn char *se_service; /* name of service */ 765ff3afceSSheldon Hearn int se_socktype; /* type of socket to use */ 770cac72f4SYoshinobu Inoue int se_family; /* address family */ 785ff3afceSSheldon Hearn char *se_proto; /* protocol used */ 795ff3afceSSheldon Hearn int se_maxchild; /* max number of children */ 805ff3afceSSheldon Hearn int se_maxcpm; /* max connects per IP per minute */ 815ff3afceSSheldon Hearn int se_numchild; /* current number of children */ 825ff3afceSSheldon Hearn char *se_user; /* user name to run as */ 835ff3afceSSheldon Hearn char *se_group; /* group name to run as */ 845ff3afceSSheldon Hearn #ifdef LOGIN_CAP 855ff3afceSSheldon Hearn char *se_class; /* login class name to run with */ 865ff3afceSSheldon Hearn #endif 875ff3afceSSheldon Hearn struct biltin *se_bi; /* if built-in, description */ 885ff3afceSSheldon Hearn char *se_server; /* server program */ 895ff3afceSSheldon Hearn char *se_server_name; /* server program without path */ 905ff3afceSSheldon Hearn #define MAXARGV 20 915ff3afceSSheldon Hearn char *se_argv[MAXARGV+1]; /* program arguments */ 920cac72f4SYoshinobu Inoue #ifdef IPSEC 93caf60155SDavid Malone char *se_policy; /* IPsec policy string */ 940cac72f4SYoshinobu Inoue #endif 955ff3afceSSheldon Hearn int se_fd; /* open descriptor */ 960cac72f4SYoshinobu Inoue union { /* bound address */ 970cac72f4SYoshinobu Inoue struct sockaddr se_un_ctrladdr; 980cac72f4SYoshinobu Inoue struct sockaddr_in se_un_ctrladdr4; 990cac72f4SYoshinobu Inoue struct sockaddr_in6 se_un_ctrladdr6; 1001c8d1174SDavid Malone struct sockaddr_un se_un_ctrladdr_un; 1010cac72f4SYoshinobu Inoue } se_un; 1020cac72f4SYoshinobu Inoue #define se_ctrladdr se_un.se_un_ctrladdr 1030cac72f4SYoshinobu Inoue #define se_ctrladdr4 se_un.se_un_ctrladdr4 1040cac72f4SYoshinobu Inoue #define se_ctrladdr6 se_un.se_un_ctrladdr6 1051c8d1174SDavid Malone #define se_ctrladdr_un se_un.se_un_ctrladdr_un 1061c3b5f22SDavid Malone socklen_t se_ctrladdr_size; 1071c8d1174SDavid Malone uid_t se_sockuid; /* Owner for unix domain socket */ 1081c8d1174SDavid Malone gid_t se_sockgid; /* Group for unix domain socket */ 1091c8d1174SDavid Malone mode_t se_sockmode; /* Mode for unix domain socket */ 1105ff3afceSSheldon Hearn u_char se_type; /* type: normal, mux, or mux+ */ 1115ff3afceSSheldon Hearn u_char se_checked; /* looked at during merge */ 1125ff3afceSSheldon Hearn u_char se_accept; /* i.e., wait/nowait mode */ 1135ff3afceSSheldon Hearn u_char se_rpc; /* ==1 if RPC service */ 1145ff3afceSSheldon Hearn int se_rpc_prog; /* RPC program number */ 1155ff3afceSSheldon Hearn u_int se_rpc_lowvers; /* RPC low version */ 1165ff3afceSSheldon Hearn u_int se_rpc_highvers; /* RPC high version */ 1175ff3afceSSheldon Hearn int se_count; /* number started since se_time */ 1184f985ef6SXin LI struct timespec se_time; /* start of se_count */ 1195ff3afceSSheldon Hearn struct servtab *se_next; 1200cac72f4SYoshinobu Inoue struct se_flags { 1210cac72f4SYoshinobu Inoue u_int se_nomapped : 1; 1220cac72f4SYoshinobu Inoue u_int se_reset : 1; 1230cac72f4SYoshinobu Inoue } se_flags; 12409b1c357SHajimu UMEMOTO int se_maxperip; /* max number of children per src */ 12509b1c357SHajimu UMEMOTO LIST_HEAD(, conninfo) se_conn[PERIPSIZE]; 126*c53b0f40SKyle Evans LIST_HEAD(, stabchild) se_children; 1275ff3afceSSheldon Hearn }; 1285ff3afceSSheldon Hearn 1290cac72f4SYoshinobu Inoue #define se_nomapped se_flags.se_nomapped 1300cac72f4SYoshinobu Inoue #define se_reset se_flags.se_reset 1310cac72f4SYoshinobu Inoue 132d6272fceSKyle Evans #define SERVTAB_AT_LIMIT(sep) \ 133d6272fceSKyle Evans ((sep)->se_maxchild > 0 && (sep)->se_numchild == (sep)->se_maxchild) 134d6272fceSKyle Evans #define SERVTAB_EXCEEDS_LIMIT(sep) \ 135d6272fceSKyle Evans ((sep)->se_maxchild > 0 && (sep)->se_numchild >= (sep)->se_maxchild) 136d6272fceSKyle Evans 137edb616bbSJuli Mallett int check_loop(const struct sockaddr *, const struct servtab *sep); 138edb616bbSJuli Mallett void inetd_setproctitle(const char *, int); 139edb616bbSJuli Mallett struct servtab *tcpmux(int); 1405ff3afceSSheldon Hearn 141b585f768SDavid Malone extern int debug; 142b585f768SDavid Malone extern struct servtab *servtab; 143b585f768SDavid Malone 144edb616bbSJuli Mallett typedef void (bi_fn_t)(int, struct servtab *); 1455ff3afceSSheldon Hearn 1465ff3afceSSheldon Hearn struct biltin { 147b585f768SDavid Malone const char *bi_service; /* internally provided service name */ 1485ff3afceSSheldon Hearn int bi_socktype; /* type of socket supported */ 1495ff3afceSSheldon Hearn short bi_fork; /* 1 if should fork before call */ 1505ff3afceSSheldon Hearn int bi_maxchild; /* max number of children, -1=default */ 151b585f768SDavid Malone bi_fn_t *bi_fn; /* function which performs it */ 1525ff3afceSSheldon Hearn }; 1534909085fSHiroki Sato extern struct biltin biltins[]; 154