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