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