xref: /illumos-gate/usr/src/lib/libinetsvc/common/inetsvc.h (revision a07094369b21309434206d9b3601d162693466fc)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _INETSVC_H
28 #define	_INETSVC_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <libscf.h>
33 #include <sys/socket.h>
34 #include <libuutil.h>
35 #include <rpc/rpc.h>
36 
37 /*
38  * Interfaces shared by usr.lib/inetd and its administrative commands.
39  */
40 
41 #ifdef	__cplusplus
42 extern "C" {
43 #endif
44 
45 #define	PROTO_DELIMITERS		" ,"
46 
47 #define	INETD_UDS_PATH			"/var/run/.inetd.uds"
48 #define	INETD_INSTANCE_FMRI		"svc:/network/inetd:default"
49 
50 #define	PG_NAME_SERVICE_CONFIG		"inetd"
51 #define	PG_NAME_SERVICE_DEFAULTS	"defaults"
52 #define	PG_NAME_INETCONV		"inetconv"
53 
54 #define	PR_SVC_NAME_NAME		"name"
55 #define	PR_SOCK_TYPE_NAME		"endpoint_type"
56 #define	PR_PROTO_NAME			"proto"
57 #define	PR_ISRPC_NAME			"isrpc"
58 #define	PR_RPC_LW_VER_NAME		"rpc_low_version"
59 #define	PR_RPC_HI_VER_NAME		"rpc_high_version"
60 #define	PR_ISWAIT_NAME			"wait"
61 #define	PR_CON_RATE_MAX_NAME		"max_con_rate"
62 #define	PR_CON_RATE_OFFLINE_NAME	"con_rate_offline"
63 #define	PR_BIND_ADDR_NAME		"bind_addr"
64 #define	PR_BIND_FAIL_MAX_NAME		"bind_fail_max"
65 #define	PR_BIND_FAIL_INTVL_NAME		"bind_fail_interval"
66 #define	PR_MAX_COPIES_NAME		"max_copies"
67 #define	PR_MAX_FAIL_RATE_CNT_NAME	"failrate_cnt"
68 #define	PR_MAX_FAIL_RATE_INTVL_NAME	"failrate_interval"
69 #define	PR_INHERIT_ENV_NAME		"inherit_env"
70 #define	PR_DO_TCP_WRAPPERS_NAME		"tcp_wrappers"
71 #define	PR_DO_TCP_TRACE_NAME		"tcp_trace"
72 #define	PR_AUTO_CONVERTED_NAME		"converted"
73 #define	PR_VERSION_NAME			"version"
74 #define	PR_SOURCE_LINE_NAME		"source_line"
75 
76 /*
77  * Provide index values for inetd property locations in the property table, for
78  * convenience.  If the array is modified, these values MUST be updated.
79  */
80 #define	PT_SVC_NAME_INDEX		0
81 #define	PT_SOCK_TYPE_INDEX		1
82 #define	PT_PROTO_INDEX			2
83 #define	PT_ISRPC_INDEX			3
84 #define	PT_RPC_LW_VER_INDEX		4
85 #define	PT_RPC_HI_VER_INDEX		5
86 #define	PT_ISWAIT_INDEX			6
87 #define	PT_EXEC_INDEX			7
88 #define	PT_ARG0_INDEX			8
89 #define	PT_USER_INDEX			9
90 #define	PT_BIND_ADDR_INDEX		10
91 #define	PT_BIND_FAIL_MAX_INDEX		11
92 #define	PT_BIND_FAIL_INTVL_INDEX	12
93 #define	PT_CON_RATE_MAX_INDEX		13
94 #define	PT_MAX_COPIES_INDEX		14
95 #define	PT_CON_RATE_OFFLINE_INDEX	15
96 #define	PT_MAX_FAIL_RATE_CNT_INDEX	16
97 #define	PT_MAX_FAIL_RATE_INTVL_INDEX	17
98 #define	PT_INHERIT_ENV_INDEX		18
99 #define	PT_DO_TCP_TRACE_INDEX		19
100 #define	PT_DO_TCP_WRAPPERS_INDEX	20
101 
102 /*
103  * Names of method properties.
104  */
105 #define	PR_EXEC_NAME			"exec"
106 #define	PR_ARG0_NAME			"arg0"
107 #define	PR_USER_NAME			"user"
108 
109 /*
110  * Method property group names.
111  */
112 #define	START_METHOD_NAME		"inetd_start"
113 #define	OFFLINE_METHOD_NAME		"inetd_offline"
114 #define	ONLINE_METHOD_NAME		"inetd_online"
115 #define	DISABLE_METHOD_NAME		"inetd_disable"
116 #define	REFRESH_METHOD_NAME		"inetd_refresh"
117 
118 /*
119  * Valid socket type values.
120  */
121 #define	SOCKTYPE_STREAM_STR	"stream"
122 #define	SOCKTYPE_DGRAM_STR	"dgram"
123 #define	SOCKTYPE_RAW_STR	"raw"
124 #define	SOCKTYPE_SEQPKT_STR	"seqpacket"
125 #define	SOCKTYPE_TLI_STR	"tli"
126 #define	SOCKTYPE_XTI_STR	"xti"
127 
128 /*
129  * Valid socket based service protocols.
130  */
131 #define	SOCKET_PROTO_SCTP6	"sctp6"
132 #define	SOCKET_PROTO_SCTP6_ONLY	"sctp6only"
133 #define	SOCKET_PROTO_SCTP	"sctp"
134 #define	SOCKET_PROTO_TCP6	"tcp6"
135 #define	SOCKET_PROTO_TCP6_ONLY	"tcp6only"
136 #define	SOCKET_PROTO_TCP	"tcp"
137 #define	SOCKET_PROTO_UDP6	"udp6"
138 #define	SOCKET_PROTO_UDP6_ONLY	"udp6only"
139 #define	SOCKET_PROTO_UDP	"udp"
140 
141 /*
142  * Return codes for the methods of inetd managed services.
143  */
144 #define	IMRET_SUCCESS	0
145 /*
146  * Set this value above the range used by unix commands so theres minimal chance
147  * of a non-GL cognizant command accidentally returning this code.
148  */
149 #define	IMRET_FAILURE	100
150 
151 /*
152  * Macros for differentiating between sockaddr_in & sockaddr_in6 when
153  * dealing with the contents of a sockaddr_storage structure.
154  * These differentiate based on the contents of ss_family (either AF_INET
155  * or AF_INET6).
156  */
157 #define	SS_ADDRLEN(s)	((s).ss_family == AF_INET ? \
158 	sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6))
159 #define	SS_PORT(s)	((s).ss_family == AF_INET ? \
160 	((struct sockaddr_in *)&(s))->sin_port : \
161 	((struct sockaddr_in6 *)&(s))->sin6_port)
162 #define	SS_SETPORT(s, port)	((s).ss_family == AF_INET ? \
163 	(((struct sockaddr_in *)&(s))->sin_port = port) : \
164 	(((struct sockaddr_in6 *)&(s))->sin6_port = port))
165 #define	SS_SINADDR(s)	((s).ss_family == AF_INET ? \
166 	((void *) &(((struct sockaddr_in *)&(s))->sin_addr)) : \
167 	((void *) &(((struct sockaddr_in6 *)&(s))->sin6_addr)))
168 
169 /* Collection of information pertaining to rpc based services. */
170 typedef struct {
171 	struct netbuf	netbuf;
172 	int		prognum;
173 	int		lowver;
174 	int		highver;
175 	char		*netid;
176 	boolean_t	is_loopback;
177 } rpc_info_t;
178 
179 /*
180  * Structure containing the common elements of both the socket_info_t and the
181  * tlx_info_t structures.
182  */
183 typedef struct {
184 	/* proto string causing this entry */
185 	char		*proto;
186 
187 	/* network fd we're listening on; -1 if not listening */
188 	int		listen_fd;
189 
190 	/* associate RPC info structure, if any (NULL if none). */
191 	rpc_info_t	*ri;
192 
193 	uu_list_node_t	link;
194 
195 	/* should this fd have the v6 socket option set? */
196 	boolean_t	v6only;
197 } proto_info_t;
198 
199 
200 /* TLI/XTI connection indication list construct. */
201 typedef struct {
202 	struct t_call	*call;
203 	uu_list_node_t	link;
204 } tlx_conn_ind_t;
205 
206 /* Collection of information pertaining to tli/xti based services. */
207 typedef struct {
208 	/* protocol information common to tlx and socket based services */
209 	proto_info_t	pr_info;
210 
211 	/* address we're bound to */
212 	struct netbuf	local_addr;
213 
214 	/* device name supplied to t_open() */
215 	char		*dev_name;
216 
217 	/* queue of pending connection indications */
218 	uu_list_t	*conn_ind_queue;
219 } tlx_info_t;
220 
221 /* Collection of information pertaining to socket based services. */
222 typedef struct {
223 	/* protocol information common to tlx and socket based services */
224 	proto_info_t		pr_info;
225 
226 	/* address we're bound to */
227 	struct sockaddr_storage local_addr;
228 
229 	/* SOCK_STREAM/SOCK_DGRAM/SOCK_RAW/SOCK_SEQPACKET */
230 	int			type;
231 
232 	int			protocol;
233 } socket_info_t;
234 
235 /* Basic configuration properties for an instance. */
236 typedef struct {
237 	/* getservbyname() recognized service name */
238 	char		*svc_name;
239 
240 	/* TLI/XTI type service ? */
241 	boolean_t	istlx;
242 
243 	/* list of protocols and associated info */
244 	uu_list_t	*proto_list;
245 
246 	/* wait type service ? */
247 	boolean_t	iswait;
248 
249 	/*
250 	 * Properties from here onwards all have default values in the inetd
251 	 * service instance.
252 	 */
253 
254 	boolean_t	do_tcp_wrappers;
255 	boolean_t	do_tcp_trace;
256 
257 	/* inherit inetd's environment, or take an empty one */
258 	boolean_t	inherit_env;
259 
260 	/* failure rate configuration */
261 	int64_t		wait_fail_cnt;
262 	int		wait_fail_interval;
263 
264 	/* maximum concurrent copies limit */
265 	int64_t		max_copies;
266 
267 	/* connection rate configuration */
268 	int		conn_rate_offline;
269 	int64_t		conn_rate_max;
270 
271 	/* bind failure retries configuration */
272 	int		bind_fail_interval;
273 	int64_t		bind_fail_max;
274 
275 	/* specific address to bind instance to */
276 	char		*bind_addr;
277 } basic_cfg_t;
278 
279 typedef enum uds_request {
280 	UR_REFRESH_INETD,
281 	UR_STOP_INETD
282 } uds_request_t;
283 
284 typedef union {
285 	int64_t		iv_int;
286 	uint64_t	iv_cnt;
287 	boolean_t	iv_boolean;
288 	char		*iv_string;
289 	char		**iv_string_list;
290 } inetd_value_t;
291 
292 typedef enum {
293 	IVE_VALID,
294 	IVE_UNSET,
295 	IVE_INVALID
296 } iv_error_t;
297 
298 /*
299  * Operations on these types (like valid_default_prop()) need to be modified
300  * when this list is changed.
301  */
302 typedef enum {
303 	INET_TYPE_INVALID = 0,
304 
305 	INET_TYPE_BOOLEAN,
306 	INET_TYPE_COUNT,
307 	INET_TYPE_INTEGER,
308 	INET_TYPE_STRING,
309 	INET_TYPE_STRING_LIST
310 } inet_type_t;
311 
312 typedef struct {
313 	const char	*ip_name;
314 	const char	*ip_pg;
315 	inet_type_t	ip_type;
316 	boolean_t	ip_default;
317 	iv_error_t	ip_error;
318 	inetd_value_t	ip_value;
319 	boolean_t	from_inetd;
320 } inetd_prop_t;
321 
322 inetd_prop_t *get_prop_table(size_t *);
323 inetd_prop_t *find_prop(const inetd_prop_t *, const char *, inet_type_t);
324 int64_t get_prop_value_int(const inetd_prop_t *, const char *);
325 uint64_t get_prop_value_count(const inetd_prop_t *, const char *);
326 boolean_t get_prop_value_boolean(const inetd_prop_t *, const char *);
327 const char *get_prop_value_string(const inetd_prop_t *, const char *);
328 const char **get_prop_value_string_list(const inetd_prop_t *, const char *);
329 void put_prop_value_int(inetd_prop_t *, const char *, int64_t);
330 void put_prop_value_count(inetd_prop_t *, const char *, uint64_t);
331 void put_prop_value_boolean(inetd_prop_t *, const char *, boolean_t);
332 boolean_t put_prop_value_string(inetd_prop_t *, const char *, const char *);
333 void put_prop_value_string_list(inetd_prop_t *, const char *, char **);
334 boolean_t valid_props(inetd_prop_t *, const char *fmri, basic_cfg_t **,
335     uu_list_pool_t *, uu_list_pool_t *);
336 void destroy_basic_cfg(basic_cfg_t *);
337 void destroy_proto_list(basic_cfg_t *);
338 boolean_t valid_default_prop(const char *, const void *);
339 scf_error_t read_prop(scf_handle_t *, inetd_prop_t *, int, const char *,
340     const char *);
341 inetd_prop_t *read_instance_props(scf_handle_t *, const char *, size_t *,
342     scf_error_t *);
343 inetd_prop_t *read_default_props(scf_handle_t *, size_t *, scf_error_t *);
344 void free_instance_props(inetd_prop_t *);
345 int connect_to_inetd(void);
346 int refresh_inetd(void);
347 int get_sock_type_id(const char *);
348 int get_rpc_prognum(const char *);
349 int calculate_hash(const char *, char **);
350 scf_error_t retrieve_inetd_hash(char **);
351 scf_error_t store_inetd_hash(const char *);
352 const char *inet_ntop_native(int, const void *, char *, size_t);
353 void setproctitle(const char *, int, char **);
354 void dg_template(
355     void (*)(int, const struct sockaddr *, int, const void *, size_t), int,
356     void *, size_t);
357 int safe_write(int, const void *, size_t);
358 int safe_sendto(int, const void *, size_t, int, const struct sockaddr *, int);
359 char **get_protos(const char *);
360 char **get_netids(char *);
361 void destroy_strings(char **);
362 
363 #ifdef	__cplusplus
364 }
365 #endif
366 
367 #endif /* _INETSVC_H */
368