xref: /illumos-gate/usr/src/lib/libwrap/tcpd.h (revision 9b5f5885)
17c478bd9Sstevel@tonic-gate /*
2*9b5f5885SSachidananda Urs  * Copyright 2014 Sachidananda Urs <sacchi@gmail.com>
37c478bd9Sstevel@tonic-gate  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
47c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
57c478bd9Sstevel@tonic-gate  */
7*9b5f5885SSachidananda Urs /*
8*9b5f5885SSachidananda Urs  * @(#) tcpd.h 1.5 96/03/19 16:22:24
9*9b5f5885SSachidananda Urs  *
10*9b5f5885SSachidananda Urs  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
11*9b5f5885SSachidananda Urs  */
12*9b5f5885SSachidananda Urs 
13*9b5f5885SSachidananda Urs #ifndef	_TCPD_H
14*9b5f5885SSachidananda Urs #define	_TCPD_H
167c478bd9Sstevel@tonic-gate /*
177c478bd9Sstevel@tonic-gate  * HAVE_IPV6 is traditionally configured at tcp_wrappers build time but for
187c478bd9Sstevel@tonic-gate  * Solaris it must always be defined to keep the library interface binary
197c478bd9Sstevel@tonic-gate  * compatible.
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate #define	HAVE_IPV6
237c478bd9Sstevel@tonic-gate /* Structure to describe one communications endpoint. */
25*9b5f5885SSachidananda Urs #define	STRING_LENGTH	128		/* hosts, users, processes */
277c478bd9Sstevel@tonic-gate #include <sys/socket.h>
287c478bd9Sstevel@tonic-gate #include <netinet/in.h>
307c478bd9Sstevel@tonic-gate typedef struct sockaddr_gen {
317c478bd9Sstevel@tonic-gate     union {
327c478bd9Sstevel@tonic-gate 	struct sockaddr	_sg_sa;
337c478bd9Sstevel@tonic-gate 	struct sockaddr_in	_sg_sin;
347c478bd9Sstevel@tonic-gate #ifdef HAVE_IPV6
357c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	_sg_sin6;
367c478bd9Sstevel@tonic-gate #endif
37*9b5f5885SSachidananda Urs 	} sg_addr;
387c478bd9Sstevel@tonic-gate } sockaddr_gen;
407c478bd9Sstevel@tonic-gate typedef union gen_addr {
417c478bd9Sstevel@tonic-gate     struct in_addr	ga_in;
427c478bd9Sstevel@tonic-gate #ifdef HAVE_IPV6
437c478bd9Sstevel@tonic-gate     struct in6_addr	ga_in6;
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate } gen_addr;
477c478bd9Sstevel@tonic-gate extern void sockgen_simplify();
49*9b5f5885SSachidananda Urs #define	sg_sa		sg_addr._sg_sa
50*9b5f5885SSachidananda Urs #define	sg_sin		sg_addr._sg_sin
51*9b5f5885SSachidananda Urs #define	sg_sin6		sg_addr._sg_sin6
52*9b5f5885SSachidananda Urs #define	sg_family	sg_sa.sa_family
537c478bd9Sstevel@tonic-gate #ifdef HAVE_IPV6
54*9b5f5885SSachidananda Urs #define	SGADDRSZ(sag)		((sag)->sg_family == AF_INET6 ? \
557c478bd9Sstevel@tonic-gate 				    sizeof (struct in6_addr) : \
567c478bd9Sstevel@tonic-gate 				    sizeof (struct in_addr))
57*9b5f5885SSachidananda Urs #define	SGSOCKADDRSZ(sag)	((sag)->sg_family == AF_INET6 ? \
587c478bd9Sstevel@tonic-gate 				    sizeof (struct sockaddr_in6) : \
597c478bd9Sstevel@tonic-gate 				    sizeof (struct sockaddr_in))
60*9b5f5885SSachidananda Urs #define	SGPORT(sag)		(*((sag)->sg_family == AF_INET6 ? \
617c478bd9Sstevel@tonic-gate 				    &(sag)->sg_sin6.sin6_port : \
627c478bd9Sstevel@tonic-gate 				    &(sag)->sg_sin.sin_port))
63*9b5f5885SSachidananda Urs #define	SGADDRP(sag)		(((sag)->sg_family == AF_INET6 ? \
64*9b5f5885SSachidananda Urs 				    (char *)&(sag)->sg_sin6.sin6_addr : \
65*9b5f5885SSachidananda Urs 				    (char *)&(sag)->sg_sin.sin_addr))
66*9b5f5885SSachidananda Urs #define	SGFAM(sag)		((sag)->sg_family == AF_INET6 ? \
677c478bd9Sstevel@tonic-gate 				    AF_INET6 : AF_INET)
69*9b5f5885SSachidananda Urs #define	SG_IS_UNSPECIFIED(sag) \
707c478bd9Sstevel@tonic-gate 		((sag)->sg_family == AF_INET6 ? \
717c478bd9Sstevel@tonic-gate 			IN6_IS_ADDR_UNSPECIFIED(&(sag)->sg_sin6.sin6_addr) : \
727c478bd9Sstevel@tonic-gate 			(sag)->sg_sin.sin_addr.s_addr == 0)
74*9b5f5885SSachidananda Urs #define	VALID_ADDRTYPE(t)	((t) == AF_INET || (t) == AF_INET6)
767c478bd9Sstevel@tonic-gate #ifndef IPV6_ABITS
77*9b5f5885SSachidananda Urs #define	IPV6_ABITS 128			/* Size of IPV6 address in bits */
787c478bd9Sstevel@tonic-gate #endif
807c478bd9Sstevel@tonic-gate #else /* HAVE_IPV6 */
82*9b5f5885SSachidananda Urs #define	SGADDRSZ(sag)		sizeof (struct in_addr)
83*9b5f5885SSachidananda Urs #define	SGSOCKADDRSZ(sag)	sizeof (struct sockaddr_in)
84*9b5f5885SSachidananda Urs #define	SGPORT(sag)		((sag)->sg_sin.sin_port)
85*9b5f5885SSachidananda Urs #define	SGADDRP(sag)		((char *)&(sag)->sg_sin.sin_addr)
86*9b5f5885SSachidananda Urs #define	SGFAM(sag)		AF_INET
87*9b5f5885SSachidananda Urs #define	SG_IS_UNSPECIFIED(sag)  ((sag)->sg_sin.sin_addr.s_addr == 0)
89*9b5f5885SSachidananda Urs #define	VALID_ADDRTYPE(t)	((t) == AF_INET)
917c478bd9Sstevel@tonic-gate #endif /* HAVE_IPV6 */
937c478bd9Sstevel@tonic-gate struct host_info {
947c478bd9Sstevel@tonic-gate     char    name[STRING_LENGTH];	/* access via eval_hostname(host) */
957c478bd9Sstevel@tonic-gate     char    addr[STRING_LENGTH];	/* access via eval_hostaddr(host) */
967c478bd9Sstevel@tonic-gate     struct sockaddr_gen *sin;		/* socket address or 0 */
977c478bd9Sstevel@tonic-gate     struct t_unitdata *unit;		/* TLI transport address or 0 */
987c478bd9Sstevel@tonic-gate     struct request_info *request;	/* for shared information */
997c478bd9Sstevel@tonic-gate };
1017c478bd9Sstevel@tonic-gate /* Structure to describe what we know about a service request. */
1037c478bd9Sstevel@tonic-gate struct request_info {
104*9b5f5885SSachidananda Urs     int	    fd;				/* socket handle */
1057c478bd9Sstevel@tonic-gate     char    user[STRING_LENGTH];	/* access via eval_user(request) */
1067c478bd9Sstevel@tonic-gate     char    daemon[STRING_LENGTH];	/* access via eval_daemon(request) */
1077c478bd9Sstevel@tonic-gate     char    pid[10];			/* access via eval_pid(request) */
1087c478bd9Sstevel@tonic-gate     struct host_info client[1];		/* client endpoint info */
1097c478bd9Sstevel@tonic-gate     struct host_info server[1];		/* server endpoint info */
1107c478bd9Sstevel@tonic-gate     void  (*sink) ();			/* datagram sink function or 0 */
1117c478bd9Sstevel@tonic-gate     void  (*hostname) ();		/* address to printable hostname */
1127c478bd9Sstevel@tonic-gate     void  (*hostaddr) ();		/* address to printable address */
1137c478bd9Sstevel@tonic-gate     void  (*cleanup) ();		/* cleanup function or 0 */
1147c478bd9Sstevel@tonic-gate     struct netconfig *config;		/* netdir handle */
1157c478bd9Sstevel@tonic-gate };
1177c478bd9Sstevel@tonic-gate /* Common string operations. Less clutter should be more readable. */
119*9b5f5885SSachidananda Urs #define	STRN_CPY(d, s, l)	{ strncpy((d), (s), (l)); (d)[(l)-1] = 0; }
121*9b5f5885SSachidananda Urs #define	STRN_EQ(x, y, l)	(strncasecmp((x), (y), (l)) == 0)
122*9b5f5885SSachidananda Urs #define	STRN_NE(x, y, l)	(strncasecmp((x), (y), (l)) != 0)
123*9b5f5885SSachidananda Urs #define	STR_EQ(x, y)		(strcasecmp((x), (y)) == 0)
124*9b5f5885SSachidananda Urs #define	STR_NE(x, y)		(strcasecmp((x), (y)) != 0)
126*9b5f5885SSachidananda Urs /*
127*9b5f5885SSachidananda Urs  * Initially, all above strings have the empty value. Information that
128*9b5f5885SSachidananda Urs  * cannot be determined at runtime is set to "unknown", so that we can
129*9b5f5885SSachidananda Urs  * distinguish between `unavailable' and `not yet looked up'. A hostname
130*9b5f5885SSachidananda Urs  * that we do not believe in is set to "paranoid".
131*9b5f5885SSachidananda Urs  */
133*9b5f5885SSachidananda Urs #define	STRING_UNKNOWN	"unknown"	/* lookup failed */
134*9b5f5885SSachidananda Urs #define	STRING_PARANOID	"paranoid"	/* hostname conflict */
1367c478bd9Sstevel@tonic-gate extern char unknown[];
1377c478bd9Sstevel@tonic-gate extern char paranoid[];
139*9b5f5885SSachidananda Urs #define	HOSTNAME_KNOWN(s) (STR_NE((s), unknown) && STR_NE((s), paranoid))
1417c478bd9Sstevel@tonic-gate #ifdef HAVE_IPV6
142*9b5f5885SSachidananda Urs #define	NOT_INADDR(s) (strchr(s, ':') == 0 && s[strspn(s, "0123456789./")] != 0)
1437c478bd9Sstevel@tonic-gate #else
144*9b5f5885SSachidananda Urs #define	NOT_INADDR(s) (s[strspn(s, "0123456789./")] != 0)
1457c478bd9Sstevel@tonic-gate #endif
1477c478bd9Sstevel@tonic-gate /* Global functions. */
1497c478bd9Sstevel@tonic-gate #if defined(TLI) || defined(PTX) || defined(TLI_SEQUENT)
1507c478bd9Sstevel@tonic-gate extern void fromhost();			/* get/validate client host info */
1517c478bd9Sstevel@tonic-gate #else
152*9b5f5885SSachidananda Urs #define	fromhost sock_host		/* no TLI support needed */
1537c478bd9Sstevel@tonic-gate #endif
155*9b5f5885SSachidananda Urs extern int hosts_ctl();			/* wrapper around request_init() */
1567c478bd9Sstevel@tonic-gate extern int hosts_access();		/* access control */
1577c478bd9Sstevel@tonic-gate extern void shell_cmd();		/* execute shell command */
1587c478bd9Sstevel@tonic-gate extern char *percent_x();		/* do %<char> expansion */
1597c478bd9Sstevel@tonic-gate extern void rfc931();			/* client name from RFC 931 daemon */
1607c478bd9Sstevel@tonic-gate extern void clean_exit();		/* clean up and exit */
1617c478bd9Sstevel@tonic-gate extern void refuse();			/* clean up and exit */
1627c478bd9Sstevel@tonic-gate extern char *xgets();			/* fgets() on steroids */
1637c478bd9Sstevel@tonic-gate extern char *split_at();		/* strchr() and split */
1647c478bd9Sstevel@tonic-gate extern unsigned long dot_quad_addr();	/* restricted inet_addr() */
1657c478bd9Sstevel@tonic-gate extern int numeric_addr();		/* IP4/IP6 inet_addr (restricted) */
1667c478bd9Sstevel@tonic-gate extern struct hostent *tcpd_gethostbyname();
1677c478bd9Sstevel@tonic-gate 					/* IP4/IP6 gethostbyname */
1687c478bd9Sstevel@tonic-gate #ifdef HAVE_IPV6
1697c478bd9Sstevel@tonic-gate extern char *skip_ipv6_addrs();		/* skip over colons in IPv6 addrs */
1707c478bd9Sstevel@tonic-gate #else
171*9b5f5885SSachidananda Urs #define	skip_ipv6_addrs(x)	x
1727c478bd9Sstevel@tonic-gate #endif
1747c478bd9Sstevel@tonic-gate /* Global variables. */
1767c478bd9Sstevel@tonic-gate extern int allow_severity;		/* for connection logging */
1777c478bd9Sstevel@tonic-gate extern int deny_severity;		/* for connection logging */
1787c478bd9Sstevel@tonic-gate extern char *hosts_allow_table;		/* for verification mode redirection */
1797c478bd9Sstevel@tonic-gate extern char *hosts_deny_table;		/* for verification mode redirection */
1807c478bd9Sstevel@tonic-gate extern int hosts_access_verbose;	/* for verbose matching mode */
1817c478bd9Sstevel@tonic-gate extern int rfc931_timeout;		/* user lookup timeout */
1827c478bd9Sstevel@tonic-gate extern int resident;			/* > 0 if resident process */
184*9b5f5885SSachidananda Urs /*
185*9b5f5885SSachidananda Urs  * Routines for controlled initialization and update of request structure
186*9b5f5885SSachidananda Urs  * attributes. Each attribute has its own key.
187*9b5f5885SSachidananda Urs  */
1897c478bd9Sstevel@tonic-gate #ifdef __STDC__
190*9b5f5885SSachidananda Urs extern struct request_info *request_init(struct request_info *, ...);
191*9b5f5885SSachidananda Urs extern struct request_info *request_set(struct request_info *, ...);
1927c478bd9Sstevel@tonic-gate #else
1937c478bd9Sstevel@tonic-gate extern struct request_info *request_init();	/* initialize request */
1947c478bd9Sstevel@tonic-gate extern struct request_info *request_set();	/* update request structure */
1957c478bd9Sstevel@tonic-gate #endif
197*9b5f5885SSachidananda Urs #define	RQ_FILE		1		/* file descriptor */
198*9b5f5885SSachidananda Urs #define	RQ_DAEMON	2		/* server process (argv[0]) */
199*9b5f5885SSachidananda Urs #define	RQ_USER		3		/* client user name */
200*9b5f5885SSachidananda Urs #define	RQ_CLIENT_NAME	4		/* client host name */
201*9b5f5885SSachidananda Urs #define	RQ_CLIENT_ADDR	5		/* client host address */
202*9b5f5885SSachidananda Urs #define	RQ_CLIENT_SIN	6		/* client endpoint (internal) */
203*9b5f5885SSachidananda Urs #define	RQ_SERVER_NAME	7		/* server host name */
204*9b5f5885SSachidananda Urs #define	RQ_SERVER_ADDR	8		/* server host address */
205*9b5f5885SSachidananda Urs #define	RQ_SERVER_SIN	9		/* server endpoint (internal) */
206*9b5f5885SSachidananda Urs 
207*9b5f5885SSachidananda Urs /*
208*9b5f5885SSachidananda Urs  * Routines for delayed evaluation of request attributes. Each attribute
209*9b5f5885SSachidananda Urs  * type has its own access method. The trivial ones are implemented by
210*9b5f5885SSachidananda Urs  * macros. The other ones are wrappers around the transport-specific host
211*9b5f5885SSachidananda Urs  * name, address, and client user lookup methods. The request_info and
212*9b5f5885SSachidananda Urs  * host_info structures serve as caches for the lookup results.
213*9b5f5885SSachidananda Urs  */
2157c478bd9Sstevel@tonic-gate extern char *eval_user();		/* client user */
2167c478bd9Sstevel@tonic-gate extern char *eval_hostname();		/* printable hostname */
2177c478bd9Sstevel@tonic-gate extern char *eval_hostaddr();		/* printable host address */
2187c478bd9Sstevel@tonic-gate extern char *eval_hostinfo();		/* host name or address */
2197c478bd9Sstevel@tonic-gate extern char *eval_client();		/* whatever is available */
2207c478bd9Sstevel@tonic-gate extern char *eval_server();		/* whatever is available */
221*9b5f5885SSachidananda Urs #define	eval_daemon(r)	((r)->daemon)	/* daemon process name */
222*9b5f5885SSachidananda Urs #define	eval_pid(r)	((r)->pid)	/* process id */
2247c478bd9Sstevel@tonic-gate /* Socket-specific methods, including DNS hostname lookups. */
2267c478bd9Sstevel@tonic-gate extern void sock_host();		/* look up endpoint addresses */
2277c478bd9Sstevel@tonic-gate extern void sock_hostname();		/* translate address to hostname */
2287c478bd9Sstevel@tonic-gate extern void sock_hostaddr();		/* address to printable address */
229*9b5f5885SSachidananda Urs #define	sock_methods(r) \
2307c478bd9Sstevel@tonic-gate 	{ (r)->hostname = sock_hostname; (r)->hostaddr = sock_hostaddr; }
2327c478bd9Sstevel@tonic-gate /* The System V Transport-Level Interface (TLI) interface. */
2347c478bd9Sstevel@tonic-gate #if defined(TLI) || defined(PTX) || defined(TLI_SEQUENT)
2357c478bd9Sstevel@tonic-gate extern void tli_host();			/* look up endpoint addresses etc. */
2367c478bd9Sstevel@tonic-gate #endif
238*9b5f5885SSachidananda Urs /*
239*9b5f5885SSachidananda Urs  * Problem reporting interface. Additional file/line context is reported
240*9b5f5885SSachidananda Urs  * when available. The jump buffer (tcpd_buf) is not declared here, or
241*9b5f5885SSachidananda Urs  * everyone would have to include <setjmp.h>.
242*9b5f5885SSachidananda Urs  */
2447c478bd9Sstevel@tonic-gate #ifdef __STDC__
2457c478bd9Sstevel@tonic-gate extern void tcpd_warn(char *, ...);	/* report problem and proceed */
2467c478bd9Sstevel@tonic-gate extern void tcpd_jump(char *, ...);	/* report problem and jump */
2477c478bd9Sstevel@tonic-gate #else
2487c478bd9Sstevel@tonic-gate extern void tcpd_warn();
2497c478bd9Sstevel@tonic-gate extern void tcpd_jump();
2507c478bd9Sstevel@tonic-gate #endif
2527c478bd9Sstevel@tonic-gate struct tcpd_context {
2537c478bd9Sstevel@tonic-gate     char   *file;			/* current file */
254*9b5f5885SSachidananda Urs     int	    line;			/* current line */
2557c478bd9Sstevel@tonic-gate };
2567c478bd9Sstevel@tonic-gate extern struct tcpd_context tcpd_context;
258*9b5f5885SSachidananda Urs /*
259*9b5f5885SSachidananda Urs  * While processing access control rules, error conditions are handled by
260*9b5f5885SSachidananda Urs  * jumping back into the hosts_access() routine. This is cleaner than
261*9b5f5885SSachidananda Urs  * checking the return value of each and every silly little function. The
262*9b5f5885SSachidananda Urs  * (-1) returns are here because zero is already taken by longjmp().
263*9b5f5885SSachidananda Urs  */
265*9b5f5885SSachidananda Urs #define	AC_PERMIT	1		/* permit access */
266*9b5f5885SSachidananda Urs #define	AC_DENY		(-1)		/* deny_access */
267*9b5f5885SSachidananda Urs #define	AC_ERROR	AC_DENY		/* XXX */
269*9b5f5885SSachidananda Urs /*
270*9b5f5885SSachidananda Urs  * In verification mode an option function should just say what it would do,
271*9b5f5885SSachidananda Urs  * instead of really doing it. An option function that would not return
272*9b5f5885SSachidananda Urs  * should clear the dry_run flag to inform the caller of this unusual
273*9b5f5885SSachidananda Urs  * behavior.
274*9b5f5885SSachidananda Urs  */
2767c478bd9Sstevel@tonic-gate extern void process_options();		/* execute options */
2777c478bd9Sstevel@tonic-gate extern int dry_run;			/* verification flag */
2797c478bd9Sstevel@tonic-gate /* Bug workarounds. */
2817c478bd9Sstevel@tonic-gate #ifdef INET_ADDR_BUG			/* inet_addr() returns struct */
282*9b5f5885SSachidananda Urs #define	inet_addr fix_inet_addr
2837c478bd9Sstevel@tonic-gate extern long fix_inet_addr();
2847c478bd9Sstevel@tonic-gate #endif
2867c478bd9Sstevel@tonic-gate #ifdef BROKEN_FGETS			/* partial reads from sockets */
287*9b5f5885SSachidananda Urs #define	fgets fix_fgets
2887c478bd9Sstevel@tonic-gate extern char *fix_fgets();
2897c478bd9Sstevel@tonic-gate #endif
2917c478bd9Sstevel@tonic-gate #ifdef RECVFROM_BUG			/* no address family info */
292*9b5f5885SSachidananda Urs #define	recvfrom fix_recvfrom
2937c478bd9Sstevel@tonic-gate extern int fix_recvfrom();
2947c478bd9Sstevel@tonic-gate #endif
2967c478bd9Sstevel@tonic-gate #ifdef GETPEERNAME_BUG			/* claims success with UDP */
297*9b5f5885SSachidananda Urs #define	getpeername fix_getpeername
2987c478bd9Sstevel@tonic-gate extern int fix_getpeername();
2997c478bd9Sstevel@tonic-gate #endif
3017c478bd9Sstevel@tonic-gate #ifdef SOLARIS_24_GETHOSTBYNAME_BUG	/* lists addresses as aliases */
302*9b5f5885SSachidananda Urs #define	gethostbyname fix_gethostbyname
3037c478bd9Sstevel@tonic-gate extern struct hostent *fix_gethostbyname();
3047c478bd9Sstevel@tonic-gate #endif
3067c478bd9Sstevel@tonic-gate #ifdef USE_STRSEP			/* libc calls strtok() */
307*9b5f5885SSachidananda Urs #define	strtok	fix_strtok
3087c478bd9Sstevel@tonic-gate extern char *fix_strtok();
3097c478bd9Sstevel@tonic-gate #endif
3117c478bd9Sstevel@tonic-gate #ifdef LIBC_CALLS_STRTOK		/* libc calls strtok() */
312*9b5f5885SSachidananda Urs #define	strtok	my_strtok
3137c478bd9Sstevel@tonic-gate extern char *my_strtok();
3147c478bd9Sstevel@tonic-gate #endif
315*9b5f5885SSachidananda Urs 
316*9b5f5885SSachidananda Urs #endif /* _TCPD_H */