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