xref: /titanic_41/usr/src/lib/libipsecutil/common/ipsec_util.h (revision e11c3f44f531fdff80941ce57c065d2ae861cefc)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_IPSEC_UTIL_H
27 #define	_IPSEC_UTIL_H
28 
29 /*
30  * Headers and definitions for support functions that are shared by
31  * the ipsec utilities ipseckey and ikeadm.
32  */
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <net/pfkeyv2.h>
41 #include <netinet/in.h>
42 #include <inet/ip.h>
43 #include <setjmp.h>
44 #include <stdio.h>
45 #include <err.h>
46 #include <errfp.h>
47 #include <net/pfpolicy.h>
48 
49 #ifndef A_CNT
50 /* macros for array manipulation */
51 #define	A_CNT(arr)	(sizeof (arr)/sizeof (arr[0]))
52 #define	A_END(arr)	(&arr[A_CNT(arr)])
53 #endif
54 
55 /* used for file parsing */
56 #define	NBUF_SIZE	16
57 #define	IBUF_SIZE	2048
58 #define	COMMENT_CHAR	'#'
59 #define	CONT_CHAR	'\\'
60 #define	QUOTE_CHAR	'"'
61 
62 /* used for command-line parsing */
63 #define	START_ARG	8
64 #define	TOO_MANY_ARGS	(START_ARG << 9)
65 
66 /* Return codes for argv/argc vector creation */
67 #define	TOO_MANY_TOKENS		-3
68 #define	MEMORY_ALLOCATION	-2
69 #define	COMMENT_LINE		1
70 #define	SUCCESS			0
71 
72 /*
73  * Time printing defines...
74  *
75  * TBUF_SIZE is pretty arbitrary.  Perhaps it shouldn't be.
76  */
77 #define	TBUF_SIZE	50
78 #define	TIME_MAX	LONG_MAX
79 
80 #ifndef INSECURE_PERMS
81 #define	INSECURE_PERMS(sbuf)	(((sbuf).st_uid != 0) || \
82 	((sbuf).st_mode & S_IRWXG) || ((sbuf).st_mode & S_IRWXO))
83 #endif
84 
85 /*
86  * Solaris UDP port used to communicate with the Solaris Cluster
87  * daemon. It is used only when the node is booted in cluster mode.
88  */
89 #define	CLUSTER_UDP_PORT	2005
90 
91 /* For keyword-lookup tables */
92 typedef struct keywdtab {
93 	uint_t	kw_tag;
94 	char	*kw_str;
95 } keywdtab_t;
96 
97 /* Exit the programe and enter new state */
98 typedef enum exit_type {
99 	SERVICE_EXIT_OK,
100 	SERVICE_DEGRADE,
101 	SERVICE_BADPERM,
102 	SERVICE_BADCONF,
103 	SERVICE_MAINTAIN,
104 	SERVICE_DISABLE,
105 	SERVICE_FATAL,
106 	SERVICE_RESTART
107 } exit_type_t;
108 
109 /*
110  * Function Prototypes
111  */
112 
113 /*
114  * Print errno and if cmdline or readfile, exit; if interactive reset state
115  */
116 extern void ipsecutil_exit(exit_type_t, char *, FILE *, const char *fmt, ...);
117 extern void bail(char *);
118 
119 /*
120  * Localization macro - Only to be used from usr/src/cmd because Macros
121  * are not expanded in usr/src/lib when message catalogs are built.
122  */
123 #define	Bail(s)	bail(dgettext(TEXT_DOMAIN, s))
124 
125 /*
126  * Print caller-supplied, variable-arg error message, then exit if cmdline
127  * or readfile, or reset state if interactive.
128  */
129 extern void bail_msg(char *, ...);
130 
131 /*
132  * dump_XXX functions produce ASCII output from the passed in data.
133  *
134  * Because certain errors need to do this stderr, dump_XXX functions
135  * take a FILE pointer.
136  */
137 
138 extern int dump_sockaddr(struct sockaddr *, uint8_t, boolean_t, FILE *,
139     boolean_t);
140 
141 extern int dump_key(uint8_t *, uint_t, FILE *);
142 
143 extern int dump_aalg(uint8_t, FILE *);
144 
145 extern int dump_ealg(uint8_t, FILE *);
146 
147 /* return true if sadb string is printable (based on type), false otherwise */
148 extern boolean_t dump_sadb_idtype(uint8_t, FILE *, int *);
149 
150 /*
151  * do_interactive: Enter a mode where commands are read from a file;
152  * treat stdin special.  infile is the file cmds are read from;
153  * promptstring is the string printed to stdout (if the cmds are
154  * being read from stdin) to prompt for a new command; parseit is
155  * the function to be called to process the command line once it's
156  * been read in and broken up into an argv/argc vector.
157  */
158 
159 /* callback function passed in to do_interactive() */
160 typedef void (*parse_cmdln_fn)(int, char **, char *, boolean_t);
161 
162 extern void do_interactive(FILE *, char *, char *, char *, parse_cmdln_fn);
163 
164 extern uint_t lines_parsed;
165 extern uint_t lines_added;
166 
167 /* convert a string to an IKE_PRIV_* constant */
168 extern int privstr2num(char *);
169 
170 /* convert a string to a D_* debug flag */
171 extern int dbgstr2num(char *);
172 
173 /* convert a string of debug strings with +|- delimiters to a debug level */
174 extern int parsedbgopts(char *);
175 
176 /*
177  * OpenSSL library
178  */
179 #define	LIBSSL	"libssl.so"
180 
181 void libssl_load(void);
182 boolean_t libssl_loaded;
183 
184 /*
185  * functions to manipulate the kmcookie-label mapping file
186  */
187 
188 #define	KMCFILE		"/var/run/ipsec_kmc_map"
189 
190 /*
191  * Insert a mapping into the file (if it's not already there), given the
192  * new label.  Return the assigned cookie, or -1 on error.
193  */
194 extern int kmc_insert_mapping(char *);
195 
196 /*
197  * Lookup the given cookie and return its corresponding label.  Return
198  * a pointer to the label on success, NULL on error (or if the label is
199  * not found).
200  */
201 extern char *kmc_lookup_by_cookie(int);
202 
203 /*
204  * These globals are declared for us in ipsec_util.c, since it needs to
205  * refer to them also...
206  */
207 extern boolean_t nflag;	/* Avoid nameservice? */
208 extern boolean_t pflag;	/* Paranoid w.r.t. printing keying material? */
209 extern boolean_t interactive;
210 extern boolean_t readfile;
211 extern uint_t lineno;
212 extern char numprint[NBUF_SIZE];
213 
214 /* For error recovery in interactive or read-file mode. */
215 extern jmp_buf env;
216 
217 /*
218  * Back-end stuff for getalgby*().
219  */
220 
221 #define	INET_IPSECALGSPATH	"/etc/inet/"
222 #define	INET_IPSECALGSFILE	(INET_IPSECALGSPATH "ipsecalgs")
223 
224 /* To preserve packages delimiters in /etc/inet/ipsecalgs */
225 typedef struct ipsecalgs_pkg {
226 	int alg_num;
227 	char *pkg_name;
228 } ipsecalgs_pkg_t;
229 
230 /*
231  * The cached representation of /etc/inet/ipsecalgs is represented by:
232  * - A dynamically-grown (optionally sorted) array of IPsec protocols
233  * - Each protocol has an array (again, dynamically grown and sorted)
234  *   of algorithms, each a full-fledged struct ipsecalgent.
235  * - The getipsecalg*() routines will search the list, then duplicate the
236  *   struct ipsecalgent and return it.
237  */
238 
239 typedef enum {
240 	LIBIPSEC_ALGS_EXEC_SYNC,
241 	LIBIPSEC_ALGS_EXEC_ASYNC
242 } ipsecalgs_exec_mode_t;
243 
244 typedef struct ipsec_proto {
245 	int proto_num;
246 	char *proto_name;
247 	char *proto_pkg;
248 	int proto_numalgs;
249 	struct ipsecalgent **proto_algs;
250 	ipsecalgs_pkg_t *proto_algs_pkgs;
251 	int proto_algs_npkgs;
252 	ipsecalgs_exec_mode_t proto_exec_mode;
253 } ipsec_proto_t;
254 
255 extern void _build_internal_algs(ipsec_proto_t **, int *);
256 extern int _str_to_ipsec_exec_mode(char *, ipsecalgs_exec_mode_t *);
257 
258 extern int addipsecalg(struct ipsecalgent *, uint_t);
259 extern int delipsecalgbyname(const char *, int);
260 extern int delipsecalgbynum(int, int);
261 extern int addipsecproto(const char *, int, ipsecalgs_exec_mode_t, uint_t);
262 extern int delipsecprotobyname(const char *);
263 extern int delipsecprotobynum(int);
264 extern int *getipsecprotos(int *);
265 extern int *getipsecalgs(int *, int);
266 extern int list_ints(FILE *, int *);
267 extern const char *ipsecalgs_diag(int);
268 extern int ipsecproto_get_exec_mode(int, ipsecalgs_exec_mode_t *);
269 extern int ipsecproto_set_exec_mode(int, ipsecalgs_exec_mode_t);
270 
271 /* Flags for add/delete routines. */
272 #define	LIBIPSEC_ALGS_ADD_FORCE 0x00000001
273 
274 /*
275  * Helper definitions for indices into array of key sizes when key sizes
276  * are defined by range.
277  */
278 #define	LIBIPSEC_ALGS_KEY_DEF_IDX	0	/* default key size */
279 #define	LIBIPSEC_ALGS_KEY_MIN_IDX	1	/* min key size */
280 #define	LIBIPSEC_ALGS_KEY_MAX_IDX	2	/* max key size */
281 #define	LIBIPSEC_ALGS_KEY_NUM_VAL	4	/* def, min, max, 0 */
282 
283 /* Error codes for IPsec algorithms management */
284 #define	LIBIPSEC_ALGS_DIAG_ALG_EXISTS		-1
285 #define	LIBIPSEC_ALGS_DIAG_PROTO_EXISTS		-2
286 #define	LIBIPSEC_ALGS_DIAG_UNKN_PROTO		-3
287 #define	LIBIPSEC_ALGS_DIAG_UNKN_ALG		-4
288 #define	LIBIPSEC_ALGS_DIAG_NOMEM		-5
289 #define	LIBIPSEC_ALGS_DIAG_ALGSFILEOPEN		-6
290 #define	LIBIPSEC_ALGS_DIAG_ALGSFILEFDOPEN	-7
291 #define	LIBIPSEC_ALGS_DIAG_ALGSFILELOCK		-8
292 #define	LIBIPSEC_ALGS_DIAG_ALGSFILERENAME	-9
293 #define	LIBIPSEC_ALGS_DIAG_ALGSFILEWRITE	-10
294 #define	LIBIPSEC_ALGS_DIAG_ALGSFILECHMOD	-11
295 #define	LIBIPSEC_ALGS_DIAG_ALGSFILECHOWN	-12
296 #define	LIBIPSEC_ALGS_DIAG_ALGSFILECLOSE	-13
297 
298 /* /etc/inet/ipsecalgs keywords and package sections delimiters */
299 #define	LIBIPSEC_ALGS_LINE_PROTO		"PROTO|"
300 #define	LIBIPSEC_ALGS_LINE_ALG			"ALG|"
301 #define	LIBIPSEC_ALGS_LINE_PKGSTART		"# Start "
302 #define	LIBIPSEC_ALGS_LINE_PKGEND		"# End "
303 
304 /* Put these in libnsl for and process caching testing. */
305 extern int *_real_getipsecprotos(int *);
306 extern int *_real_getipsecalgs(int *, int);
307 extern struct ipsecalgent *_duplicate_alg(struct ipsecalgent *);
308 extern void _clean_trash(ipsec_proto_t *, int);
309 
310 /* spdsock support functions */
311 
312 /* Return values for spdsock_get_ext(). */
313 #define	KGE_OK	0
314 #define	KGE_DUP	1
315 #define	KGE_UNK	2
316 #define	KGE_LEN	3
317 #define	KGE_CHK	4
318 
319 extern int spdsock_get_ext(spd_ext_t *[], spd_msg_t *, uint_t, char *, uint_t);
320 extern const char *spdsock_diag(int);
321 
322 /* PF_KEY (keysock) support functions */
323 extern const char *keysock_diag(int);
324 extern int in_masktoprefix(uint8_t *, boolean_t);
325 
326 /* SA support functions */
327 
328 extern void print_diagnostic(FILE *, uint16_t);
329 extern void print_sadb_msg(FILE *, struct sadb_msg *, time_t, boolean_t);
330 extern void print_sa(FILE *, char *, struct sadb_sa *);
331 extern void printsatime(FILE *, int64_t, const char *, const char *,
332     const char *, boolean_t);
333 extern void print_lifetimes(FILE *, time_t, struct sadb_lifetime *,
334     struct sadb_lifetime *, struct sadb_lifetime *, struct sadb_lifetime *,
335     boolean_t vflag);
336 extern void print_address(FILE *, char *, struct sadb_address *, boolean_t);
337 extern void print_asn1_name(FILE *, const unsigned char *, long);
338 extern void print_key(FILE *, char *, struct sadb_key *);
339 extern void print_ident(FILE *, char *, struct sadb_ident *);
340 extern void print_sens(FILE *, char *, struct sadb_sens *);
341 extern void print_prop(FILE *, char *, struct sadb_prop *);
342 extern void print_eprop(FILE *, char *, struct sadb_prop *);
343 extern void print_supp(FILE *, char *, struct sadb_supported *);
344 extern void print_spirange(FILE *, char *, struct sadb_spirange *);
345 extern void print_kmc(FILE *, char *, struct sadb_x_kmc *);
346 extern void print_samsg(FILE *, uint64_t *, boolean_t, boolean_t, boolean_t);
347 extern char *rparsesatype(int);
348 extern char *rparsealg(uint8_t, int);
349 extern char *rparseidtype(uint16_t);
350 extern boolean_t save_lifetime(struct sadb_lifetime *, FILE *);
351 extern boolean_t save_address(struct sadb_address *, FILE *);
352 extern boolean_t save_key(struct sadb_key *, FILE *);
353 extern boolean_t save_ident(struct sadb_ident *, FILE *);
354 extern void save_assoc(uint64_t *, FILE *);
355 extern FILE *opensavefile(char *);
356 extern const char *do_inet_ntop(const void *, char *, size_t);
357 
358 /*
359  * These exit macros give a consistent exit behaviour for all
360  * programs that use libipsecutil. These wll work in usr/src/cmd
361  * and usr/src/lib, but because macros in usr/src/lib don't get
362  * expanded when I18N message catalogs are built, avoid using
363  * these with text inside libipsecutil.
364  */
365 #define	EXIT_OK(x) \
366 	ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile, \
367 	dgettext(TEXT_DOMAIN, x))
368 #define	EXIT_OK2(x, y) \
369 	ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile, \
370 	dgettext(TEXT_DOMAIN, x), y)
371 #define	EXIT_OK3(x, y, z) \
372 	ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile, \
373 	dgettext(TEXT_DOMAIN, x), y, z)
374 #define	EXIT_BADCONFIG(x) \
375 	ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, \
376 	dgettext(TEXT_DOMAIN, x))
377 #define	EXIT_BADCONFIG2(x, y) \
378 	ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, \
379 	dgettext(TEXT_DOMAIN, x), y)
380 #define	EXIT_BADCONFIG3(x, y, z) \
381 	ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, \
382 	dgettext(TEXT_DOMAIN, x), y, z)
383 #define	EXIT_MAINTAIN(x) \
384 	ipsecutil_exit(SERVICE_MAINTAIN, my_fmri, debugfile, \
385 	dgettext(TEXT_DOMAIN, x))
386 #define	EXIT_MAINTAIN2(x, y) \
387 	ipsecutil_exit(SERVICE_MAINTAIN, my_fmri, debugfile, \
388 	dgettext(TEXT_DOMAIN, x), y)
389 #define	EXIT_DEGRADE(x) \
390 	ipsecutil_exit(SERVICE_DEGRADE, my_fmri, debugfile, \
391 	dgettext(TEXT_DOMAIN, x))
392 #define	EXIT_BADPERM(x) \
393 	ipsecutil_exit(SERVICE_BADPERM, my_fmri, debugfile, \
394 	dgettext(TEXT_DOMAIN, x))
395 #define	EXIT_BADPERM2(x, y) \
396 	ipsecutil_exit(SERVICE_BADPERM, my_fmri, debugfile, \
397 	dgettext(TEXT_DOMAIN, x), y)
398 #define	EXIT_FATAL(x) \
399 	ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, \
400 	dgettext(TEXT_DOMAIN, x))
401 #define	EXIT_FATAL2(x, y) \
402 	ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, \
403 	dgettext(TEXT_DOMAIN, x), y)
404 #define	EXIT_FATAL3(x, y, z) \
405 	ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, \
406 	dgettext(TEXT_DOMAIN, x), y, z)
407 #define	EXIT_RESTART(x) \
408 	ipsecutil_exit(SERVICE_RESTART, my_fmri, debugfile, \
409 	dgettext(TEXT_DOMAIN, x))
410 
411 #ifdef __cplusplus
412 }
413 #endif
414 
415 #endif	/* _IPSEC_UTIL_H */
416