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