diff --git a/contrib/tcp_wrappers/hosts_access.c b/contrib/tcp_wrappers/hosts_access.c index 58998055b516..140225fabace 100644 --- a/contrib/tcp_wrappers/hosts_access.c +++ b/contrib/tcp_wrappers/hosts_access.c @@ -1,503 +1,501 @@ /* * This module implements a simple access control language that is based on * host (or domain) names, NIS (host) netgroup names, IP addresses (or * network numbers) and daemon process names. When a match is found the * search is terminated, and depending on whether PROCESS_OPTIONS is defined, * a list of options is executed or an optional shell command is executed. * * Host and user names are looked up on demand, provided that suitable endpoint * information is available as sockaddr_in structures or TLI netbufs. As a * side effect, the pattern matching process may change the contents of * request structure fields. * * Diagnostics are reported through syslog(3). * * Compile with -DNETGROUP if your library provides support for netgroups. * * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. * * $FreeBSD$ */ #ifndef lint static char sccsid[] = "@(#) hosts_access.c 1.21 97/02/12 02:13:22"; #endif /* System libraries. */ #include #ifdef INT32_T typedef uint32_t u_int32_t; #endif #include #ifdef INET6 #include #endif #include #include #include #include #include #include #include #include #ifdef INET6 #include #endif #include -extern int errno; - #ifndef INADDR_NONE #define INADDR_NONE (-1) /* XXX should be 0xffffffff */ #endif /* Local stuff. */ #include "tcpd.h" /* Error handling. */ extern jmp_buf tcpd_buf; /* Delimiters for lists of daemons or clients. */ static char sep[] = ", \t\r\n"; /* Constants to be used in assignments only, not in comparisons... */ #define YES 1 #define NO 0 /* * These variables are globally visible so that they can be redirected in * verification mode. */ char *hosts_allow_table = HOSTS_ALLOW; char *hosts_deny_table = HOSTS_DENY; int hosts_access_verbose = 0; /* * In a long-running process, we are not at liberty to just go away. */ int resident = (-1); /* -1, 0: unknown; +1: yes */ /* Forward declarations. */ static int table_match(char *table, struct request_info *request); static int list_match(char *list, struct request_info *request, int (*match_fn)(char *, struct request_info *)); static int server_match(char *tok, struct request_info *request); static int client_match(char *tok, struct request_info *request); static int host_match(char *tok, struct host_info *host); static int string_match(char *tok, char *string); static int masked_match(char *net_tok, char *mask_tok, char *string); #ifdef INET6 static int masked_match4(char *net_tok, char *mask_tok, char *string); static int masked_match6(char *net_tok, char *mask_tok, char *string); #endif /* Size of logical line buffer. */ #define BUFLEN 2048 /* definition to be used from workarounds.c */ #ifdef NETGROUP int yp_get_default_domain(char **); #endif /* hosts_access - host access control facility */ int hosts_access(request) struct request_info *request; { int verdict; /* * If the (daemon, client) pair is matched by an entry in the file * /etc/hosts.allow, access is granted. Otherwise, if the (daemon, * client) pair is matched by an entry in the file /etc/hosts.deny, * access is denied. Otherwise, access is granted. A non-existent * access-control file is treated as an empty file. * * After a rule has been matched, the optional language extensions may * decide to grant or refuse service anyway. Or, while a rule is being * processed, a serious error is found, and it seems better to play safe * and deny service. All this is done by jumping back into the * hosts_access() routine, bypassing the regular return from the * table_match() function calls below. */ if (resident <= 0) resident++; verdict = setjmp(tcpd_buf); if (verdict != 0) return (verdict == AC_PERMIT); if (table_match(hosts_allow_table, request)) return (YES); if (table_match(hosts_deny_table, request)) return (NO); return (YES); } /* table_match - match table entries with (daemon, client) pair */ static int table_match(table, request) char *table; struct request_info *request; { FILE *fp; char sv_list[BUFLEN]; /* becomes list of daemons */ char *cl_list; /* becomes list of clients */ char *sh_cmd; /* becomes optional shell command */ int match = NO; struct tcpd_context saved_context; char *cp; saved_context = tcpd_context; /* stupid compilers */ /* * Between the fopen() and fclose() calls, avoid jumps that may cause * file descriptor leaks. */ if ((fp = fopen(table, "r")) != 0) { tcpd_context.file = table; tcpd_context.line = 0; while (match == NO && xgets(sv_list, sizeof(sv_list), fp) != 0) { if (sv_list[strlen(sv_list) - 1] != '\n') { tcpd_warn("missing newline or line too long"); continue; } /* Ignore anything after unescaped # character */ for (cp = strchr(sv_list, '#'); cp != NULL;) { if (cp > sv_list && cp[-1] == '\\') { cp = strchr(cp + 1, '#'); continue; } *cp = '\0'; break; } if (sv_list[strspn(sv_list, " \t\r\n")] == 0) continue; if ((cl_list = split_at(sv_list, ':')) == 0) { tcpd_warn("missing \":\" separator"); continue; } sh_cmd = split_at(cl_list, ':'); match = list_match(sv_list, request, server_match) && list_match(cl_list, request, client_match); } (void) fclose(fp); } else if (errno != ENOENT) { tcpd_warn("cannot open %s: %m", table); } if (match) { if (hosts_access_verbose > 1) syslog(LOG_DEBUG, "matched: %s line %d", tcpd_context.file, tcpd_context.line); if (sh_cmd) { #ifdef PROCESS_OPTIONS process_options(sh_cmd, request); #else char cmd[BUFSIZ]; shell_cmd(percent_x(cmd, sizeof(cmd), sh_cmd, request)); #endif } } tcpd_context = saved_context; return (match); } /* list_match - match a request against a list of patterns with exceptions */ static int list_match(char *list, struct request_info *request, int (*match_fn)(char *, struct request_info *)) { char *tok; /* * Process tokens one at a time. We have exhausted all possible matches * when we reach an "EXCEPT" token or the end of the list. If we do find * a match, look for an "EXCEPT" list and recurse to determine whether * the match is affected by any exceptions. */ for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) { if (STR_EQ(tok, "EXCEPT")) /* EXCEPT: give up */ return (NO); if (match_fn(tok, request)) { /* YES: look for exceptions */ while ((tok = strtok((char *) 0, sep)) && STR_NE(tok, "EXCEPT")) /* VOID */ ; return (tok == 0 || list_match((char *) 0, request, match_fn) == 0); } } return (NO); } /* server_match - match server information */ static int server_match(tok, request) char *tok; struct request_info *request; { char *host; if ((host = split_at(tok + 1, '@')) == 0) { /* plain daemon */ return (string_match(tok, eval_daemon(request))); } else { /* daemon@host */ return (string_match(tok, eval_daemon(request)) && host_match(host, request->server)); } } /* client_match - match client information */ static int client_match(tok, request) char *tok; struct request_info *request; { char *host; if ((host = split_at(tok + 1, '@')) == 0) { /* plain host */ return (host_match(tok, request->client)); } else { /* user@host */ return (host_match(host, request->client) && string_match(tok, eval_user(request))); } } /* hostfile_match - look up host patterns from file */ static int hostfile_match(path, host) char *path; struct host_info *host; { char tok[BUFSIZ]; int match = NO; FILE *fp; if ((fp = fopen(path, "r")) != 0) { while (fscanf(fp, "%s", tok) == 1 && !(match = host_match(tok, host))) /* void */ ; fclose(fp); } else if (errno != ENOENT) { tcpd_warn("open %s: %m", path); } return (match); } /* host_match - match host name and/or address against pattern */ static int host_match(tok, host) char *tok; struct host_info *host; { char *mask; /* * This code looks a little hairy because we want to avoid unnecessary * hostname lookups. * * The KNOWN pattern requires that both address AND name be known; some * patterns are specific to host names or to host addresses; all other * patterns are satisfied when either the address OR the name match. */ if (tok[0] == '@') { /* netgroup: look it up */ #ifdef NETGROUP static char *mydomain = 0; if (mydomain == 0) yp_get_default_domain(&mydomain); return (innetgr(tok + 1, eval_hostname(host), (char *) 0, mydomain)); #else tcpd_warn("netgroup support is disabled"); /* not tcpd_jump() */ return (NO); #endif } else if (tok[0] == '/') { /* /file hack */ return (hostfile_match(tok, host)); } else if (STR_EQ(tok, "KNOWN")) { /* check address and name */ char *name = eval_hostname(host); return (STR_NE(eval_hostaddr(host), unknown) && HOSTNAME_KNOWN(name)); } else if (STR_EQ(tok, "LOCAL")) { /* local: no dots in name */ char *name = eval_hostname(host); return (strchr(name, '.') == 0 && HOSTNAME_KNOWN(name)); } else if ((mask = split_at(tok, '/')) != 0) { /* net/mask */ return (masked_match(tok, mask, eval_hostaddr(host))); } else { /* anything else */ return (string_match(tok, eval_hostaddr(host)) || (NOT_INADDR(tok) && string_match(tok, eval_hostname(host)))); } } /* string_match - match string against pattern */ static int string_match(tok, string) char *tok; char *string; { int n; #ifdef INET6 /* convert IPv4 mapped IPv6 address to IPv4 address */ if (STRN_EQ(string, "::ffff:", 7) && dot_quad_addr(string + 7) != INADDR_NONE) { string += 7; } #endif if (tok[0] == '.') { /* suffix */ n = strlen(string) - strlen(tok); return (n > 0 && STR_EQ(tok, string + n)); } else if (STR_EQ(tok, "ALL")) { /* all: match any */ return (YES); } else if (STR_EQ(tok, "KNOWN")) { /* not unknown */ return (STR_NE(string, unknown)); } else if (tok[(n = strlen(tok)) - 1] == '.') { /* prefix */ return (STRN_EQ(tok, string, n)); } else { /* exact match */ #ifdef INET6 struct addrinfo hints, *res; struct sockaddr_in6 pat, addr; int len, ret; char ch; len = strlen(tok); if (*tok == '[' && tok[len - 1] == ']') { ch = tok[len - 1]; tok[len - 1] = '\0'; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; if ((ret = getaddrinfo(tok + 1, NULL, &hints, &res)) == 0) { memcpy(&pat, res->ai_addr, sizeof(pat)); freeaddrinfo(res); } tok[len - 1] = ch; if (ret != 0 || getaddrinfo(string, NULL, &hints, &res) != 0) return NO; memcpy(&addr, res->ai_addr, sizeof(addr)); freeaddrinfo(res); if (pat.sin6_scope_id != 0 && addr.sin6_scope_id != pat.sin6_scope_id) return NO; return (!memcmp(&pat.sin6_addr, &addr.sin6_addr, sizeof(struct in6_addr))); return (ret); } #endif return (STR_EQ(tok, string)); } } /* masked_match - match address against netnumber/netmask */ #ifdef INET6 static int masked_match(net_tok, mask_tok, string) char *net_tok; char *mask_tok; char *string; { return (masked_match4(net_tok, mask_tok, string) || masked_match6(net_tok, mask_tok, string)); } static int masked_match4(net_tok, mask_tok, string) #else static int masked_match(net_tok, mask_tok, string) #endif char *net_tok; char *mask_tok; char *string; { #ifdef INET6 u_int32_t net; u_int32_t mask; u_int32_t addr; #else unsigned long net; unsigned long mask; unsigned long addr; #endif /* * Disallow forms other than dotted quad: the treatment that inet_addr() * gives to forms with less than four components is inconsistent with the * access control language. John P. Rouillard . */ if ((addr = dot_quad_addr(string)) == INADDR_NONE) return (NO); if ((net = dot_quad_addr(net_tok)) == INADDR_NONE || (mask = dot_quad_addr(mask_tok)) == INADDR_NONE) { #ifndef INET6 tcpd_warn("bad net/mask expression: %s/%s", net_tok, mask_tok); #endif return (NO); /* not tcpd_jump() */ } return ((addr & mask) == net); } #ifdef INET6 static int masked_match6(net_tok, mask_tok, string) char *net_tok; char *mask_tok; char *string; { struct addrinfo hints, *res; struct sockaddr_in6 net, addr; u_int32_t mask; int len, mask_len, i = 0; char ch; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; if (getaddrinfo(string, NULL, &hints, &res) != 0) return NO; memcpy(&addr, res->ai_addr, sizeof(addr)); freeaddrinfo(res); if (IN6_IS_ADDR_V4MAPPED(&addr.sin6_addr)) { if ((*(u_int32_t *)&net.sin6_addr.s6_addr[12] = dot_quad_addr(net_tok)) == INADDR_NONE || (mask = dot_quad_addr(mask_tok)) == INADDR_NONE) return (NO); return ((*(u_int32_t *)&addr.sin6_addr.s6_addr[12] & mask) == *(u_int32_t *)&net.sin6_addr.s6_addr[12]); } /* match IPv6 address against netnumber/prefixlen */ len = strlen(net_tok); if (*net_tok != '[' || net_tok[len - 1] != ']') return NO; ch = net_tok[len - 1]; net_tok[len - 1] = '\0'; if (getaddrinfo(net_tok + 1, NULL, &hints, &res) != 0) { net_tok[len - 1] = ch; return NO; } memcpy(&net, res->ai_addr, sizeof(net)); freeaddrinfo(res); net_tok[len - 1] = ch; if ((mask_len = atoi(mask_tok)) < 0 || mask_len > 128) return NO; if (net.sin6_scope_id != 0 && addr.sin6_scope_id != net.sin6_scope_id) return NO; while (mask_len > 0) { if (mask_len < 32) { mask = htonl(~(0xffffffff >> mask_len)); if ((*(u_int32_t *)&addr.sin6_addr.s6_addr[i] & mask) != (*(u_int32_t *)&net.sin6_addr.s6_addr[i] & mask)) return NO; break; } if (*(u_int32_t *)&addr.sin6_addr.s6_addr[i] != *(u_int32_t *)&net.sin6_addr.s6_addr[i]) return NO; i += 4; mask_len -= 32; } return YES; } #endif /* INET6 */ diff --git a/contrib/tcp_wrappers/percent_m.c b/contrib/tcp_wrappers/percent_m.c index bb11b22dd81f..1019b82d8c36 100644 --- a/contrib/tcp_wrappers/percent_m.c +++ b/contrib/tcp_wrappers/percent_m.c @@ -1,43 +1,42 @@ /* * Replace %m by system error message. * * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. */ #ifndef lint static char sccsid[] = "@(#) percent_m.c 1.1 94/12/28 17:42:37"; #endif #include #include #include -extern int errno; #ifndef SYS_ERRLIST_DEFINED extern char *sys_errlist[]; extern int sys_nerr; #endif #include "mystdarg.h" char *percent_m(obuf, ibuf) char *obuf; char *ibuf; { char *bp = obuf; char *cp = ibuf; while (*bp = *cp) if (*cp == '%' && cp[1] == 'm') { if (errno < sys_nerr && errno > 0) { strcpy(bp, sys_errlist[errno]); } else { sprintf(bp, "Unknown error %d", errno); } bp += strlen(bp); cp += 2; } else { bp++, cp++; } return (obuf); } diff --git a/contrib/tcp_wrappers/tli-sequent.c b/contrib/tcp_wrappers/tli-sequent.c index 8858966876a4..c0ad75685f4c 100644 --- a/contrib/tcp_wrappers/tli-sequent.c +++ b/contrib/tcp_wrappers/tli-sequent.c @@ -1,193 +1,192 @@ /* * Warning - this relies heavily on the TLI implementation in PTX 2.X and will * probably not work under PTX 4. * * Author: Tim Wright, Sequent Computer Systems Ltd., UK. * * Modified slightly to conform to the new internal interfaces - Wietse */ #ifndef lint static char sccsid[] = "@(#) tli-sequent.c 1.1 94/12/28 17:42:51"; #endif #ifdef TLI_SEQUENT /* System libraries. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include -extern int errno; extern char *sys_errlist[]; extern int sys_nerr; extern int t_errno; extern char *t_errlist[]; extern int t_nerr; /* Local stuff. */ #include "tcpd.h" #include "tli-sequent.h" /* Forward declarations. */ static char *tli_error(); static void tli_sink(); /* tli_host - determine endpoint info */ int tli_host(request) struct request_info *request; { static struct sockaddr_in client; static struct sockaddr_in server; struct _ti_user *tli_state_ptr; union T_primitives *TSI_prim_ptr; struct strpeek peek; int len; /* * Use DNS and socket routines for name and address conversions. */ sock_methods(request); /* * Find out the client address using getpeerinaddr(). This call is the * TLI equivalent to getpeername() under Dynix/ptx. */ len = sizeof(client); t_sync(request->fd); if (getpeerinaddr(request->fd, &client, len) < 0) { tcpd_warn("can't get client address: %s", tli_error()); return; } request->client->sin = &client; /* Call TLI utility routine to get information on endpoint */ if ((tli_state_ptr = _t_checkfd(request->fd)) == NULL) return; if (tli_state_ptr->ti_servtype == T_CLTS) { /* UDP - may need to get address the hard way */ if (client.sin_addr.s_addr == 0) { /* The UDP endpoint is not connected so we didn't get the */ /* remote address - get it the hard way ! */ /* Look at the control part of the top message on the stream */ /* we don't want to remove it from the stream so we use I_PEEK */ peek.ctlbuf.maxlen = tli_state_ptr->ti_ctlsize; peek.ctlbuf.len = 0; peek.ctlbuf.buf = tli_state_ptr->ti_ctlbuf; /* Don't even look at the data */ peek.databuf.maxlen = -1; peek.databuf.len = 0; peek.databuf.buf = 0; peek.flags = 0; switch (ioctl(request->fd, I_PEEK, &peek)) { case -1: tcpd_warn("can't peek at endpoint: %s", tli_error()); return; case 0: /* No control part - we're hosed */ tcpd_warn("can't get UDP info: %s", tli_error()); return; default: /* FALL THROUGH */ ; } /* Can we even check the PRIM_type ? */ if (peek.ctlbuf.len < sizeof(long)) { tcpd_warn("UDP control info garbage"); return; } TSI_prim_ptr = (union T_primitives *) peek.ctlbuf.buf; if (TSI_prim_ptr->type != T_UNITDATA_IND) { tcpd_warn("wrong type for UDP control info"); return; } /* Validate returned unitdata indication packet */ if ((peek.ctlbuf.len < sizeof(struct T_unitdata_ind)) || ((TSI_prim_ptr->unitdata_ind.OPT_length != 0) && (peek.ctlbuf.len < TSI_prim_ptr->unitdata_ind.OPT_length + TSI_prim_ptr->unitdata_ind.OPT_offset))) { tcpd_warn("UDP control info garbaged"); return; } /* Extract the address */ memcpy(&client, peek.ctlbuf.buf + TSI_prim_ptr->unitdata_ind.SRC_offset, TSI_prim_ptr->unitdata_ind.SRC_length); } request->sink = tli_sink; } if (getmyinaddr(request->fd, &server, len) < 0) tcpd_warn("can't get local address: %s", tli_error()); else request->server->sin = &server; } /* tli_error - convert tli error number to text */ static char *tli_error() { static char buf[40]; if (t_errno != TSYSERR) { if (t_errno < 0 || t_errno >= t_nerr) { sprintf(buf, "Unknown TLI error %d", t_errno); return (buf); } else { return (t_errlist[t_errno]); } } else { if (errno < 0 || errno >= sys_nerr) { sprintf(buf, "Unknown UNIX error %d", errno); return (buf); } else { return (sys_errlist[errno]); } } } /* tli_sink - absorb unreceived datagram */ static void tli_sink(fd) int fd; { struct t_unitdata *unit; int flags; /* * Something went wrong. Absorb the datagram to keep inetd from looping. * Allocate storage for address, control and data. If that fails, sleep * for a couple of seconds in an attempt to keep inetd from looping too * fast. */ if ((unit = (struct t_unitdata *) t_alloc(fd, T_UNITDATA, T_ALL)) == 0) { tcpd_warn("t_alloc: %s", tli_error()); sleep(5); } else { (void) t_rcvudata(fd, unit, &flags); t_free((void *) unit, T_UNITDATA); } } #endif /* TLI_SEQUENT */ diff --git a/contrib/tcp_wrappers/tli.c b/contrib/tcp_wrappers/tli.c index 36d6f7eff398..d05f1156b0fb 100644 --- a/contrib/tcp_wrappers/tli.c +++ b/contrib/tcp_wrappers/tli.c @@ -1,372 +1,371 @@ /* * tli_host() determines the type of transport (connected, connectionless), * the transport address of a client host, and the transport address of a * server endpoint. In addition, it provides methods to map a transport * address to a printable host name or address. Socket address results are * in static memory; tli structures are allocated from the heap. * * The result from the hostname lookup method is STRING_PARANOID when a host * pretends to have someone elses name, or when a host name is available but * could not be verified. * * Diagnostics are reported through syslog(3). * * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. * * $FreeBSD$ */ #ifndef lint static char sccsid[] = "@(#) tli.c 1.15 97/03/21 19:27:25"; #endif #ifdef TLI /* System libraries. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include extern char *nc_sperror(); -extern int errno; extern char *sys_errlist[]; extern int sys_nerr; extern int t_errno; extern char *t_errlist[]; extern int t_nerr; /* Local stuff. */ #include "tcpd.h" /* Forward declarations. */ static void tli_endpoints(); static struct netconfig *tli_transport(); static void tli_hostname(); static void tli_hostaddr(); static void tli_cleanup(); static char *tli_error(); static void tli_sink(); /* tli_host - look up endpoint addresses and install conversion methods */ void tli_host(request) struct request_info *request; { #ifdef INET6 static struct sockaddr_storage client; static struct sockaddr_storage server; #else static struct sockaddr_in client; static struct sockaddr_in server; #endif /* * If we discover that we are using an IP transport, pretend we never * were here. Otherwise, use the transport-independent method and stick * to generic network addresses. XXX hard-coded protocol family name. */ tli_endpoints(request); #ifdef INET6 if ((request->config = tli_transport(request->fd)) != 0 && (STR_EQ(request->config->nc_protofmly, "inet") || STR_EQ(request->config->nc_protofmly, "inet6"))) { #else if ((request->config = tli_transport(request->fd)) != 0 && STR_EQ(request->config->nc_protofmly, "inet")) { #endif if (request->client->unit != 0) { #ifdef INET6 client = *(struct sockaddr_storage *) request->client->unit->addr.buf; request->client->sin = (struct sockaddr *) &client; #else client = *(struct sockaddr_in *) request->client->unit->addr.buf; request->client->sin = &client; #endif } if (request->server->unit != 0) { #ifdef INET6 server = *(struct sockaddr_storage *) request->server->unit->addr.buf; request->server->sin = (struct sockaddr *) &server; #else server = *(struct sockaddr_in *) request->server->unit->addr.buf; request->server->sin = &server; #endif } tli_cleanup(request); sock_methods(request); } else { request->hostname = tli_hostname; request->hostaddr = tli_hostaddr; request->cleanup = tli_cleanup; } } /* tli_cleanup - cleanup some dynamically-allocated data structures */ static void tli_cleanup(request) struct request_info *request; { if (request->config != 0) freenetconfigent(request->config); if (request->client->unit != 0) t_free((char *) request->client->unit, T_UNITDATA); if (request->server->unit != 0) t_free((char *) request->server->unit, T_UNITDATA); } /* tli_endpoints - determine TLI client and server endpoint information */ static void tli_endpoints(request) struct request_info *request; { struct t_unitdata *server; struct t_unitdata *client; int fd = request->fd; int flags; /* * Determine the client endpoint address. With unconnected services, peek * at the sender address of the pending protocol data unit without * popping it off the receive queue. This trick works because only the * address member of the unitdata structure has been allocated. * * Beware of successful returns with zero-length netbufs (for example, * Solaris 2.3 with ticlts transport). The netdir(3) routines can't * handle that. Assume connection-less transport when TI_GETPEERNAME * produces no usable result, even when t_rcvudata() is unable to figure * out the peer address. Better to hang than to loop. */ if ((client = (struct t_unitdata *) t_alloc(fd, T_UNITDATA, T_ADDR)) == 0) { tcpd_warn("t_alloc: %s", tli_error()); return; } if (ioctl(fd, TI_GETPEERNAME, &client->addr) < 0 || client->addr.len == 0) { request->sink = tli_sink; if (t_rcvudata(fd, client, &flags) < 0 || client->addr.len == 0) { tcpd_warn("can't get client address: %s", tli_error()); t_free((void *) client, T_UNITDATA); return; } } request->client->unit = client; /* * Look up the server endpoint address. This can be used for filtering on * server address or name, or to look up the client user. */ if ((server = (struct t_unitdata *) t_alloc(fd, T_UNITDATA, T_ADDR)) == 0) { tcpd_warn("t_alloc: %s", tli_error()); return; } if (ioctl(fd, TI_GETMYNAME, &server->addr) < 0) { tcpd_warn("TI_GETMYNAME: %m"); t_free((void *) server, T_UNITDATA); return; } request->server->unit = server; } /* tli_transport - find out TLI transport type */ static struct netconfig *tli_transport(fd) int fd; { struct stat from_client; struct stat from_config; void *handlep; struct netconfig *config; /* * Assuming that the network device is a clone device, we must compare * the major device number of stdin to the minor device number of the * devices listed in the netconfig table. */ if (fstat(fd, &from_client) != 0) { tcpd_warn("fstat(fd %d): %m", fd); return (0); } if ((handlep = setnetconfig()) == 0) { tcpd_warn("setnetconfig: %m"); return (0); } while (config = getnetconfig(handlep)) { if (stat(config->nc_device, &from_config) == 0) { #ifdef NO_CLONE_DEVICE /* * If the network devices are not cloned (as is the case for * Solaris 8 Beta), we must compare the major device numbers. */ if (major(from_config.st_rdev) == major(from_client.st_rdev)) #else if (minor(from_config.st_rdev) == major(from_client.st_rdev)) #endif break; } } if (config == 0) { tcpd_warn("unable to identify transport protocol"); return (0); } /* * Something else may clobber our getnetconfig() result, so we'd better * acquire our private copy. */ if ((config = getnetconfigent(config->nc_netid)) == 0) { tcpd_warn("getnetconfigent(%s): %s", config->nc_netid, nc_sperror()); return (0); } return (config); } /* tli_hostaddr - map TLI transport address to printable address */ static void tli_hostaddr(host) struct host_info *host; { struct request_info *request = host->request; struct netconfig *config = request->config; struct t_unitdata *unit = host->unit; char *uaddr; if (config != 0 && unit != 0 && (uaddr = taddr2uaddr(config, &unit->addr)) != 0) { STRN_CPY(host->addr, uaddr, sizeof(host->addr)); free(uaddr); } } /* tli_hostname - map TLI transport address to hostname */ static void tli_hostname(host) struct host_info *host; { struct request_info *request = host->request; struct netconfig *config = request->config; struct t_unitdata *unit = host->unit; struct nd_hostservlist *servlist; if (config != 0 && unit != 0 && netdir_getbyaddr(config, &servlist, &unit->addr) == ND_OK) { struct nd_hostserv *service = servlist->h_hostservs; struct nd_addrlist *addr_list; int found = 0; if (netdir_getbyname(config, service, &addr_list) != ND_OK) { /* * Unable to verify that the name matches the address. This may * be a transient problem or a botched name server setup. We * decide to play safe. */ tcpd_warn("can't verify hostname: netdir_getbyname(%.*s) failed", STRING_LENGTH, service->h_host); } else { /* * Look up the host address in the address list we just got. The * comparison is done on the textual representation, because the * transport address is an opaque structure that may have holes * with uninitialized garbage. This approach obviously loses when * the address does not have a textual representation. */ char *uaddr = eval_hostaddr(host); char *ua; int i; for (i = 0; found == 0 && i < addr_list->n_cnt; i++) { if ((ua = taddr2uaddr(config, &(addr_list->n_addrs[i]))) != 0) { found = !strcmp(ua, uaddr); free(ua); } } netdir_free((void *) addr_list, ND_ADDRLIST); /* * When the host name does not map to the initial address, assume * someone has compromised a name server. More likely someone * botched it, but that could be dangerous, too. */ if (found == 0) tcpd_warn("host name/address mismatch: %s != %.*s", host->addr, STRING_LENGTH, service->h_host); } STRN_CPY(host->name, found ? service->h_host : paranoid, sizeof(host->name)); netdir_free((void *) servlist, ND_HOSTSERVLIST); } } /* tli_error - convert tli error number to text */ static char *tli_error() { static char buf[40]; if (t_errno != TSYSERR) { if (t_errno < 0 || t_errno >= t_nerr) { sprintf(buf, "Unknown TLI error %d", t_errno); return (buf); } else { return (t_errlist[t_errno]); } } else { if (errno < 0 || errno >= sys_nerr) { sprintf(buf, "Unknown UNIX error %d", errno); return (buf); } else { return (sys_errlist[errno]); } } } /* tli_sink - absorb unreceived datagram */ static void tli_sink(fd) int fd; { struct t_unitdata *unit; int flags; /* * Something went wrong. Absorb the datagram to keep inetd from looping. * Allocate storage for address, control and data. If that fails, sleep * for a couple of seconds in an attempt to keep inetd from looping too * fast. */ if ((unit = (struct t_unitdata *) t_alloc(fd, T_UNITDATA, T_ALL)) == 0) { tcpd_warn("t_alloc: %s", tli_error()); sleep(5); } else { (void) t_rcvudata(fd, unit, &flags); t_free((void *) unit, T_UNITDATA); } } #endif /* TLI */ diff --git a/contrib/tcp_wrappers/workarounds.c b/contrib/tcp_wrappers/workarounds.c index 3d4e9e6783d1..38e653993f40 100644 --- a/contrib/tcp_wrappers/workarounds.c +++ b/contrib/tcp_wrappers/workarounds.c @@ -1,324 +1,322 @@ /* * Workarounds for known system software bugs. This module provides wrappers * around library functions and system calls that are known to have problems * on some systems. Most of these workarounds won't do any harm on regular * systems. * * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. * * $FreeBSD$ */ #ifndef lint char sccsid[] = "@(#) workarounds.c 1.6 96/03/19 16:22:25"; #endif #include #include #include #include #include #include #include #include #include #include #ifdef USE_GETDOMAIN #include #endif -extern int errno; - #include "tcpd.h" /* * Some AIX versions advertise a too small MAXHOSTNAMELEN value (32). * Result: long hostnames would be truncated, and connections would be * dropped because of host name verification failures. Adrian van Bloois * (A.vanBloois@info.nic.surfnet.nl) figured out what was the problem. */ #if (MAXHOSTNAMELEN < 64) #undef MAXHOSTNAMELEN #endif /* In case not defined in . */ #ifndef MAXHOSTNAMELEN #define MAXHOSTNAMELEN 256 /* storage for host name */ #endif /* * Some DG/UX inet_addr() versions return a struct/union instead of a long. * You have this problem when the compiler complains about illegal lvalues * or something like that. The following code fixes this mutant behaviour. * It should not be enabled on "normal" systems. * * Bug reported by ben@piglet.cr.usgs.gov (Rev. Ben A. Mesander). */ #ifdef INET_ADDR_BUG #undef inet_addr long fix_inet_addr(string) char *string; { return (inet_addr(string).s_addr); } #endif /* INET_ADDR_BUG */ /* * With some System-V versions, the fgets() library function does not * account for partial reads from e.g. sockets. The result is that fgets() * gives up too soon, causing username lookups to fail. Problem first * reported for IRIX 4.0.5, by Steve Kotsopoulos . * The following code works around the problem. It does no harm on "normal" * systems. */ #ifdef BROKEN_FGETS #undef fgets char *fix_fgets(buf, len, fp) char *buf; int len; FILE *fp; { char *cp = buf; int c; /* * Copy until the buffer fills up, until EOF, or until a newline is * found. */ while (len > 1 && (c = getc(fp)) != EOF) { len--; *cp++ = c; if (c == '\n') break; } /* * Return 0 if nothing was read. This is correct even when a silly buffer * length was specified. */ if (cp > buf) { *cp = 0; return (buf); } else { return (0); } } #endif /* BROKEN_FGETS */ /* * With early SunOS 5 versions, recvfrom() does not completely fill in the * source address structure when doing a non-destructive read. The following * code works around the problem. It does no harm on "normal" systems. */ #ifdef RECVFROM_BUG #undef recvfrom int fix_recvfrom(sock, buf, buflen, flags, from, fromlen) int sock; char *buf; int buflen; int flags; struct sockaddr *from; int *fromlen; { int ret; /* Assume that both ends of a socket belong to the same address family. */ if ((ret = recvfrom(sock, buf, buflen, flags, from, fromlen)) >= 0) { if (from->sa_family == 0) { struct sockaddr my_addr; int my_addr_len = sizeof(my_addr); if (getsockname(0, &my_addr, &my_addr_len)) { tcpd_warn("getsockname: %m"); } else { from->sa_family = my_addr.sa_family; } } } return (ret); } #endif /* RECVFROM_BUG */ /* * The Apollo SR10.3 and some SYSV4 getpeername(2) versions do not return an * error in case of a datagram-oriented socket. Instead, they claim that all * UDP requests come from address 0.0.0.0. The following code works around * the problem. It does no harm on "normal" systems. */ #ifdef GETPEERNAME_BUG #undef getpeername int fix_getpeername(sock, sa, len) int sock; struct sockaddr *sa; int *len; { int ret; #ifdef INET6 struct sockaddr *sin = sa; #else struct sockaddr_in *sin = (struct sockaddr_in *) sa; #endif if ((ret = getpeername(sock, sa, len)) >= 0 #ifdef INET6 && ((sin->su_si.si_family == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(&sin->su_sin6.sin6_addr)) || (sin->su_si.si_family == AF_INET && sin->su_sin.sin_addr.s_addr == 0))) { #else && sa->sa_family == AF_INET && sin->sin_addr.s_addr == 0) { #endif errno = ENOTCONN; return (-1); } else { return (ret); } } #endif /* GETPEERNAME_BUG */ /* * According to Karl Vogel (vogelke@c-17igp.wpafb.af.mil) some Pyramid * versions have no yp_default_domain() function. We use getdomainname() * instead. */ #ifdef USE_GETDOMAIN int yp_get_default_domain(ptr) char **ptr; { static char mydomain[MAXHOSTNAMELEN]; *ptr = mydomain; return (getdomainname(mydomain, MAXHOSTNAMELEN)); } #endif /* USE_GETDOMAIN */ #ifndef INADDR_NONE #define INADDR_NONE 0xffffffff #endif /* * Solaris 2.4 gethostbyname() has problems with multihomed hosts. When * doing DNS through NIS, only one host address ends up in the address list. * All other addresses end up in the hostname alias list, interspersed with * copies of the official host name. This would wreak havoc with tcpd's * hostname double checks. Below is a workaround that should do no harm when * accidentally left in. A side effect of the workaround is that address * list members are no longer properly aligned for structure access. */ #ifdef SOLARIS_24_GETHOSTBYNAME_BUG #undef gethostbyname struct hostent *fix_gethostbyname(name) char *name; { struct hostent *hp; struct in_addr addr; char **o_addr_list; char **o_aliases; char **n_addr_list; int broken_gethostbyname = 0; if ((hp = gethostbyname(name)) && !hp->h_addr_list[1] && hp->h_aliases[1]) { for (o_aliases = n_addr_list = hp->h_aliases; *o_aliases; o_aliases++) { if ((addr.s_addr = inet_addr(*o_aliases)) != INADDR_NONE) { memcpy(*n_addr_list++, (char *) &addr, hp->h_length); broken_gethostbyname = 1; } } if (broken_gethostbyname) { o_addr_list = hp->h_addr_list; memcpy(*n_addr_list++, *o_addr_list, hp->h_length); *n_addr_list = 0; hp->h_addr_list = hp->h_aliases; hp->h_aliases = o_addr_list + 1; } } return (hp); } #endif /* SOLARIS_24_GETHOSTBYNAME_BUG */ /* * Horror! Some FreeBSD 2.0 libc routines call strtok(). Since tcpd depends * heavily on strtok(), strange things may happen. Workaround: use our * private strtok(). This has been fixed in the meantime. */ #ifdef USE_STRSEP char *fix_strtok(buf, sep) char *buf; char *sep; { static char *state; char *result; if (buf) state = buf; while ((result = strsep(&state, sep)) && result[0] == 0) /* void */ ; return (result); } #endif /* USE_STRSEP */ /* * IRIX 5.3 (and possibly earlier versions, too) library routines call the * non-reentrant strtok() library routine, causing hosts to slip through * allow/deny filters. Workaround: don't rely on the vendor and use our own * strtok() function. FreeBSD 2.0 has a similar problem (fixed in 2.0.5). */ #ifdef LIBC_CALLS_STRTOK char *my_strtok(buf, sep) char *buf; char *sep; { static char *state; char *result; if (buf) state = buf; /* * Skip over separator characters and detect end of string. */ if (*(state += strspn(state, sep)) == 0) return (0); /* * Skip over non-separator characters and terminate result. */ result = state; if (*(state += strcspn(state, sep)) != 0) *state++ = 0; return (result); } #endif /* LIBC_CALLS_STRTOK */