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