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