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