xref: /dragonfly/contrib/tcp_wrappers/tcpd.h (revision 3d201fd0)
1  /*
2   * @(#) tcpd.h 1.5 96/03/19 16:22:24
3   *
4   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
5   *
6   * $FreeBSD: src/contrib/tcp_wrappers/tcpd.h,v 1.2 2000/02/03 10:26:59 shin Exp $
7   * $DragonFly: src/contrib/tcp_wrappers/tcpd.h,v 1.4 2005/09/15 04:34:52 sephe Exp $
8   */
9 
10 #ifndef _LIBWRAP_TCPD_H
11 #define _LIBWRAP_TCPD_H
12 
13 #include <sys/cdefs.h>
14 #include <stdio.h>
15 
16 /* Structure to describe one communications endpoint. */
17 
18 #define STRING_LENGTH	128		/* hosts, users, processes */
19 
20 struct host_info {
21     char    name[STRING_LENGTH];	/* access via eval_hostname(host) */
22     char    addr[STRING_LENGTH];	/* access via eval_hostaddr(host) */
23 #ifdef INET6
24     struct sockaddr *sin;		/* socket address or 0 */
25 #else
26     struct sockaddr_in *sin;		/* socket address or 0 */
27 #endif
28     struct t_unitdata *unit;		/* TLI transport address or 0 */
29     struct request_info *request;	/* for shared information */
30 };
31 
32 /* Structure to describe what we know about a service request. */
33 
34 struct request_info {
35     int     fd;				/* socket handle */
36     char    user[STRING_LENGTH];	/* access via eval_user(request) */
37     char    daemon[STRING_LENGTH];	/* access via eval_daemon(request) */
38     char    pid[10];			/* access via eval_pid(request) */
39     struct host_info client[1];		/* client endpoint info */
40     struct host_info server[1];		/* server endpoint info */
41     void  (*sink) (int);		/* datagram sink function or 0 */
42     void  (*hostname)			/* address to printable hostname */
43 	  (struct host_info *);
44     void  (*hostaddr)			/* address to printable address */
45 	  (struct host_info *);
46     void  (*cleanup) (void);		/* cleanup function or 0 */
47     struct netconfig *config;		/* netdir handle */
48 };
49 
50 /* Common string operations. Less clutter should be more readable. */
51 
52 #define STRN_CPY(d,s,l)	{ strncpy((d),(s),(l)); (d)[(l)-1] = 0; }
53 
54 #define STRN_EQ(x,y,l)	(strncasecmp((x),(y),(l)) == 0)
55 #define STRN_NE(x,y,l)	(strncasecmp((x),(y),(l)) != 0)
56 #define STR_EQ(x,y)	(strcasecmp((x),(y)) == 0)
57 #define STR_NE(x,y)	(strcasecmp((x),(y)) != 0)
58 
59  /*
60   * Initially, all above strings have the empty value. Information that
61   * cannot be determined at runtime is set to "unknown", so that we can
62   * distinguish between `unavailable' and `not yet looked up'. A hostname
63   * that we do not believe in is set to "paranoid".
64   */
65 
66 #define STRING_UNKNOWN	"unknown"	/* lookup failed */
67 #define STRING_PARANOID	"paranoid"	/* hostname conflict */
68 
69 __BEGIN_DECLS
70 extern char unknown[];
71 extern char paranoid[];
72 __END_DECLS
73 
74 #define HOSTNAME_KNOWN(s) (STR_NE((s),unknown) && STR_NE((s),paranoid))
75 
76 #define NOT_INADDR(s) (s[strspn(s,"01234567890./")] != 0)
77 
78 /* Global functions. */
79 
80 __BEGIN_DECLS
81 #if defined(TLI) || defined(PTX) || defined(TLI_SEQUENT)
82 extern void fromhost();			/* get/validate client host info */
83 #else
84 #define fromhost sock_host		/* no TLI support needed */
85 #endif
86 
87 int		hosts_access(struct request_info *);/* access control */
88 void		shell_cmd(char *);		/* execute shell command */
89 char		*percent_x(char *, int, char *, struct request_info *);/* do %<char> expansion */
90 void		rfc931(struct sockaddr *, struct sockaddr *, char *);/* client name from RFC 931 daemon */
91 void		clean_exit(struct request_info *);/* clean up and exit */
92 void		refuse(struct request_info *);	/* clean up and exit */
93 char		*xgets(char *, int, FILE *);	/* fgets() on steroids */
94 char		*split_at(char *, int);		/* strchr() and split */
95 unsigned long	dot_quad_addr(char *);		/* restricted inet_addr() */
96 
97 /* Global variables. */
98 
99 extern int allow_severity;		/* for connection logging */
100 extern int deny_severity;		/* for connection logging */
101 extern char *hosts_allow_table;		/* for verification mode redirection */
102 extern char *hosts_deny_table;		/* for verification mode redirection */
103 extern int hosts_access_verbose;	/* for verbose matching mode */
104 extern int rfc931_timeout;		/* user lookup timeout */
105 extern int resident;			/* > 0 if resident process */
106 
107  /*
108   * Routines for controlled initialization and update of request structure
109   * attributes. Each attribute has its own key.
110   */
111 
112 struct request_info	*request_init(struct request_info *,...);/* initialize request */
113 struct request_info	*request_set(struct request_info *,...);/* update request structure */
114 
115 #define RQ_FILE		1		/* file descriptor */
116 #define RQ_DAEMON	2		/* server process (argv[0]) */
117 #define RQ_USER		3		/* client user name */
118 #define RQ_CLIENT_NAME	4		/* client host name */
119 #define RQ_CLIENT_ADDR	5		/* client host address */
120 #define RQ_CLIENT_SIN	6		/* client endpoint (internal) */
121 #define RQ_SERVER_NAME	7		/* server host name */
122 #define RQ_SERVER_ADDR	8		/* server host address */
123 #define RQ_SERVER_SIN	9		/* server endpoint (internal) */
124 
125  /*
126   * Routines for delayed evaluation of request attributes. Each attribute
127   * type has its own access method. The trivial ones are implemented by
128   * macros. The other ones are wrappers around the transport-specific host
129   * name, address, and client user lookup methods. The request_info and
130   * host_info structures serve as caches for the lookup results.
131   */
132 
133 char	*eval_user(struct request_info *);	/* client user */
134 char	*eval_hostname(struct host_info *);	/* printable hostname */
135 char	*eval_hostaddr(struct host_info *);	/* printable host address */
136 char	*eval_hostinfo(struct host_info *);	/* host name or address */
137 char	*eval_client(struct request_info *);	/* whatever is available */
138 char	*eval_server(struct request_info *);	/* whatever is available */
139 #define eval_daemon(r)	((r)->daemon)	/* daemon process name */
140 #define eval_pid(r)	((r)->pid)	/* process id */
141 
142 /* Socket-specific methods, including DNS hostname lookups. */
143 
144 void	sock_host(struct request_info *);	/* look up endpoint addresses */
145 void	sock_hostname(struct host_info *);	/* translate address to hostname */
146 void	sock_hostaddr(struct host_info *);	/* address to printable address */
147 #define sock_methods(r) \
148 	{ (r)->hostname = sock_hostname; (r)->hostaddr = sock_hostaddr; }
149 
150 /* The System V Transport-Level Interface (TLI) interface. */
151 
152 #if defined(TLI) || defined(PTX) || defined(TLI_SEQUENT)
153 extern void tli_host();			/* look up endpoint addresses etc. */
154 #endif
155 
156  /*
157   * Problem reporting interface. Additional file/line context is reported
158   * when available. The jump buffer (tcpd_buf) is not declared here, or
159   * everyone would have to include <setjmp.h>.
160   */
161 
162 void	tcpd_warn(const char *, ...) __printflike(1, 2);/* report problem and proceed */
163 void	tcpd_jump(const char *, ...) __printflike(1, 2);/* report problem and jump */
164 __END_DECLS
165 
166 struct tcpd_context {
167     char   *file;			/* current file */
168     int     line;			/* current line */
169 };
170 __BEGIN_DECLS
171 extern struct tcpd_context tcpd_context;
172 __END_DECLS
173 
174  /*
175   * While processing access control rules, error conditions are handled by
176   * jumping back into the hosts_access() routine. This is cleaner than
177   * checking the return value of each and every silly little function. The
178   * (-1) returns are here because zero is already taken by longjmp().
179   */
180 
181 #define AC_PERMIT	1		/* permit access */
182 #define AC_DENY		(-1)		/* deny_access */
183 #define AC_ERROR	AC_DENY		/* XXX */
184 
185  /*
186   * In verification mode an option function should just say what it would do,
187   * instead of really doing it. An option function that would not return
188   * should clear the dry_run flag to inform the caller of this unusual
189   * behavior.
190   */
191 
192 __BEGIN_DECLS
193 void	process_options(char *, struct request_info *);	/* execute options */
194 extern int dry_run;			/* verification flag */
195 
196 /* Bug workarounds. */
197 
198 #ifdef INET_ADDR_BUG			/* inet_addr() returns struct */
199 #define inet_addr fix_inet_addr
200 extern long fix_inet_addr();
201 #endif
202 
203 #ifdef BROKEN_FGETS			/* partial reads from sockets */
204 #define fgets fix_fgets
205 extern char *fix_fgets();
206 #endif
207 
208 #ifdef RECVFROM_BUG			/* no address family info */
209 #define recvfrom fix_recvfrom
210 extern int fix_recvfrom();
211 #endif
212 
213 #ifdef GETPEERNAME_BUG			/* claims success with UDP */
214 #define getpeername fix_getpeername
215 extern int fix_getpeername();
216 #endif
217 
218 #ifdef SOLARIS_24_GETHOSTBYNAME_BUG	/* lists addresses as aliases */
219 #define gethostbyname fix_gethostbyname
220 extern struct hostent *fix_gethostbyname();
221 #endif
222 
223 #ifdef USE_STRSEP			/* libc calls strtok() */
224 #define strtok	fix_strtok
225 extern char *fix_strtok();
226 #endif
227 
228 #ifdef LIBC_CALLS_STRTOK		/* libc calls strtok() */
229 #define strtok	my_strtok
230 extern char *my_strtok();
231 #endif
232 __END_DECLS
233 
234 #endif	/* !_LIBWRAP_TCPD_H */
235