2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#ifndef _IPSEC_UTIL_H
2N/A#define _IPSEC_UTIL_H
2N/A
2N/A/*
2N/A * Headers and definitions for support functions that are shared by
2N/A * the ipsec utilities ipseckey and ikeadm.
2N/A */
2N/A
2N/A#ifdef __cplusplus
2N/Aextern "C" {
2N/A#endif
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/socket.h>
2N/A#include <net/pfkeyv2.h>
2N/A#include <netinet/in.h>
2N/A#include <inet/ip.h>
2N/A#include <setjmp.h>
2N/A#include <stdio.h>
2N/A#include <err.h>
2N/A#include <errfp.h>
2N/A#include <net/pfpolicy.h>
2N/A#include <libtecla.h>
2N/A#include <paths.h>
2N/A
2N/A#ifndef A_CNT
2N/A/* macros for array manipulation */
2N/A#define A_CNT(arr) (sizeof (arr)/sizeof (arr[0]))
2N/A#define A_END(arr) (&arr[A_CNT(arr)])
2N/A#endif
2N/A
2N/A/* used for file parsing */
2N/A#define NBUF_SIZE 16
2N/A#define COMMENT_CHAR '#'
2N/A#define CONT_CHAR '\\'
2N/A#define QUOTE_CHAR '"'
2N/A/*
2N/A * Input buffer size limits maximum line length for both file parsing and
2N/A * interactive mode. 4K chars should be enough even for broad commands and
2N/A * all possible key lengths of today's symmetric ciphers entered via
2N/A * ipseckey(1M) which has the most bifurcated grammar from all IPsec commands.
2N/A */
2N/A#define IBUF_SIZE 4096
2N/A
2N/A/* used for command-line parsing */
2N/A#define START_ARG 8
2N/A#define TOO_MANY_ARGS (START_ARG << 9)
2N/A
2N/A/* Return codes for argv/argc vector creation */
2N/A#define TOO_MANY_TOKENS -3
2N/A#define MEMORY_ALLOCATION -2
2N/A#define COMMENT_LINE 1
2N/A#define SUCCESS 0
2N/A
2N/A/* Flags for {bytecnt,secs}2out() */
2N/A#define SPC_NOSPACES 0x00000000 /* no space prefix/suffix */
2N/A#define SPC_BEGIN 0x00000001 /* put space at the beginning */
2N/A#define SPC_END 0x00000002 /* put space at the end */
2N/A#define SPC_BOTH SPC_BEGIN|SPC_END /* print both spaces */
2N/A
2N/A/*
2N/A * The following lengths should be sufficient for character buffers passed to
2N/A * bytecnt2str(),secs2str(). This is because the string output of these
2N/A * functions consists of limited number and units. The lengths should be also
2N/A * sufficient for bytecnt2out(),secs2out() functions.
2N/A */
2N/A#define BYTE_STR_SIZE 16
2N/A#define SECS_STR_SIZE 20
2N/A
2N/A/*
2N/A * Time printing defines...
2N/A *
2N/A * TBUF_SIZE is pretty arbitrary. Perhaps it shouldn't be.
2N/A */
2N/A#define TBUF_SIZE 50
2N/A#define TIME_MAX LONG_MAX
2N/A
2N/A#ifndef INSECURE_PERMS
2N/A#define INSECURE_PERMS(sbuf) (((sbuf).st_uid != 0) || \
2N/A ((sbuf).st_mode & S_IRWXG) || ((sbuf).st_mode & S_IRWXO))
2N/A#endif
2N/A
2N/A#ifndef PKCS11_TOKSIZE
2N/A#define PKCS11_TOKSIZE 32 /* Fixed length of PKCS#11 token string len. */
2N/A#endif
2N/A
2N/A/*
2N/A * Solaris UDP port used to communicate with the Solaris Cluster
2N/A * daemon. It is used only when the node is booted in cluster mode.
2N/A */
2N/A#define CLUSTER_UDP_PORT 2005
2N/A
2N/A/* For keyword-lookup tables */
2N/Atypedef struct keywdtab {
2N/A uint_t kw_tag;
2N/A char *kw_str;
2N/A} keywdtab_t;
2N/A
2N/A/*
2N/A * These different exit states are designed to give consistant behaviour
2N/A * when a program needs to exit because of an error. These exit_types
2N/A * are used in macros, defined later in this file, which call ipsecutil_exit().
2N/A * What happens when ipsecutil_exit() may differ if the command was started
2N/A * on the command line or via smf(5), See ipsecutil_exit() source for details.
2N/A *
2N/A * Note: The calling function should decide what "debug mode" is before calling
2N/A * ipsecutil_exit() with DEBUG_FATAL.
2N/A */
2N/Atypedef enum exit_type {
2N/A SERVICE_EXIT_OK, /* Exit without error. */
2N/A SERVICE_DEGRADE, /* A hint that service should be degraded. */
2N/A SERVICE_BADPERM, /* A Permission error occured. */
2N/A SERVICE_BADCONF, /* Misconfiguration. */
2N/A SERVICE_MAINTAIN, /* smf(5) to put service in maintenance mode. */
2N/A SERVICE_DISABLE, /* Tell smf(5) to disable me. */
2N/A SERVICE_FATAL, /* Whatever happened is not fixable. */
2N/A SERVICE_RESTART, /* Tell smf(5) to restart the service. */
2N/A DEBUG_FATAL /* Exit in debug mode. */
2N/A} exit_type_t;
2N/A
2N/A/*
2N/A * Function Prototypes
2N/A */
2N/A
2N/A/*
2N/A * Print errno and if cmdline or readfile, exit; if interactive reset state
2N/A */
2N/Aextern void ipsecutil_exit(exit_type_t, char *, FILE *, const char *fmt, ...);
2N/Aextern void bail(char *);
2N/A
2N/A/*
2N/A * Localization macro - Only to be used from usr/src/cmd because Macros
2N/A * are not expanded in usr/src/lib when message catalogs are built.
2N/A */
2N/A#define Bail(s) bail(dgettext(TEXT_DOMAIN, s))
2N/A
2N/A/*
2N/A * Print caller-supplied, variable-arg error message, then exit if cmdline
2N/A * or readfile, or reset state if interactive.
2N/A */
2N/Aextern void bail_msg(char *, ...);
2N/A
2N/A/*
2N/A * dump_XXX functions produce ASCII output from the passed in data.
2N/A *
2N/A * Because certain errors need to do this stderr, dump_XXX functions
2N/A * take a FILE pointer.
2N/A */
2N/A
2N/Aextern int dump_sockaddr(struct sockaddr *, uint8_t, boolean_t, FILE *,
2N/A boolean_t);
2N/A
2N/Aextern int dump_key(uint8_t *, uint_t, uint_t, FILE *, boolean_t);
2N/A
2N/Aextern int dump_aalg(uint8_t, FILE *);
2N/A
2N/Aextern int dump_ealg(uint8_t, FILE *);
2N/A
2N/A/* return true if sadb string is printable (based on type), false otherwise */
2N/Aextern boolean_t dump_sadb_idtype(uint8_t, FILE *, int *);
2N/A
2N/A/*
2N/A * do_interactive: Enter a mode where commands are read from a file;
2N/A * treat stdin special. infile is the file cmds are read from;
2N/A * promptstring is the string printed to stdout (if the cmds are
2N/A * being read from stdin) to prompt for a new command; parseit is
2N/A * the function to be called to process the command line once it's
2N/A * been read in and broken up into an argv/argc vector.
2N/A */
2N/A
2N/A/* callback function passed in to do_interactive() */
2N/Atypedef void (*parse_cmdln_fn)(int, char **, char *, boolean_t);
2N/A
2N/Aextern void do_interactive(FILE *, char *, char *, char *, parse_cmdln_fn,
2N/A CplMatchFn *);
2N/A
2N/Aextern uint_t lines_parsed;
2N/Aextern uint_t lines_added;
2N/A
2N/A/* convert a string to an IKE_PRIV_* constant */
2N/Aextern int privstr2num(char *);
2N/A
2N/A/* convert a string to a D_* debug flag */
2N/Aextern int dbgstr2num(char *);
2N/A
2N/A/* convert a string of debug strings with +|- delimiters to a debug level */
2N/Aextern int parsedbgopts(char *);
2N/A
2N/A/*
2N/A * SSL library (OpenSSL)
2N/A */
2N/A#define LIBSSL "libssl.so"
2N/A
2N/Avoid libssl_load(void);
2N/A
2N/A/*
2N/A * crypto library (OpenSSL)
2N/A */
2N/A#define LIBCRYPTO "libcrypto.so"
2N/A
2N/Avoid libcrypto_load(void);
2N/A
2N/A/*
2N/A * functions to manipulate the kmcookie-label mapping file
2N/A */
2N/A
2N/A#define KMCFILE _PATH_SYSVOL "/ipsec_kmc_map"
2N/A
2N/A/*
2N/A * Insert a mapping into the file (if it's not already there), given the
2N/A * new label. Return the assigned cookie, or -1 on error.
2N/A */
2N/Aextern int kmc_insert_mapping(char *);
2N/A
2N/A/*
2N/A * Lookup the given cookie and return its corresponding label. Return
2N/A * a pointer to the label on success, NULL on error (or if the label is
2N/A * not found).
2N/A */
2N/Aextern char *kmc_lookup_by_cookie(int);
2N/A
2N/A/*
2N/A * These globals are declared for us in ipsec_util.c, since it needs to
2N/A * refer to them also...
2N/A */
2N/Aextern boolean_t nflag; /* Avoid nameservice? */
2N/Aextern boolean_t pflag; /* Paranoid w.r.t. printing keying material? */
2N/Aextern boolean_t interactive;
2N/Aextern boolean_t readfile;
2N/Aextern uint_t lineno;
2N/Aextern char numprint[NBUF_SIZE];
2N/A
2N/A/* For error recovery in interactive or read-file mode. */
2N/Aextern jmp_buf env;
2N/A
2N/A/*
2N/A * Back-end stuff for getalgby*().
2N/A */
2N/A
2N/A#define INET_IPSECALGSPATH "/etc/inet/"
2N/A#define INET_IPSECALGSFILE (INET_IPSECALGSPATH "ipsecalgs")
2N/A
2N/A/* To preserve packages delimiters in /etc/inet/ipsecalgs */
2N/Atypedef struct ipsecalgs_pkg {
2N/A int alg_num;
2N/A char *pkg_name;
2N/A} ipsecalgs_pkg_t;
2N/A
2N/A/*
2N/A * The cached representation of /etc/inet/ipsecalgs is represented by:
2N/A * - A dynamically-grown (optionally sorted) array of IPsec protocols
2N/A * - Each protocol has an array (again, dynamically grown and sorted)
2N/A * of algorithms, each a full-fledged struct ipsecalgent.
2N/A * - The getipsecalg*() routines will search the list, then duplicate the
2N/A * struct ipsecalgent and return it.
2N/A */
2N/A
2N/Atypedef enum {
2N/A LIBIPSEC_ALGS_EXEC_SYNC,
2N/A LIBIPSEC_ALGS_EXEC_ASYNC
2N/A} ipsecalgs_exec_mode_t;
2N/A
2N/Atypedef struct ipsec_proto {
2N/A int proto_num;
2N/A char *proto_name;
2N/A char *proto_pkg;
2N/A int proto_numalgs;
2N/A struct ipsecalgent **proto_algs;
2N/A ipsecalgs_pkg_t *proto_algs_pkgs;
2N/A int proto_algs_npkgs;
2N/A ipsecalgs_exec_mode_t proto_exec_mode;
2N/A} ipsec_proto_t;
2N/A
2N/Aextern void _build_internal_algs(ipsec_proto_t **, int *);
2N/Aextern int _str_to_ipsec_exec_mode(char *, ipsecalgs_exec_mode_t *);
2N/A
2N/Aextern int addipsecalg(struct ipsecalgent *, uint_t);
2N/Aextern int delipsecalgbyname(const char *, int);
2N/Aextern int delipsecalgbynum(int, int);
2N/Aextern int addipsecproto(const char *, int, ipsecalgs_exec_mode_t, uint_t);
2N/Aextern int delipsecprotobyname(const char *);
2N/Aextern int delipsecprotobynum(int);
2N/Aextern int *getipsecprotos(int *);
2N/Aextern int *getipsecalgs(int *, int);
2N/Aextern int list_ints(FILE *, int *);
2N/Aextern const char *ipsecalgs_diag(int);
2N/Aextern int ipsecproto_get_exec_mode(int, ipsecalgs_exec_mode_t *);
2N/Aextern int ipsecproto_set_exec_mode(int, ipsecalgs_exec_mode_t);
2N/A
2N/A/* Flags for add/delete routines. */
2N/A#define LIBIPSEC_ALGS_ADD_FORCE 0x00000001
2N/A
2N/A/*
2N/A * Helper definitions for indices into array of key sizes when key sizes
2N/A * are defined by range.
2N/A */
2N/A#define LIBIPSEC_ALGS_KEY_DEF_IDX 0 /* default key size */
2N/A#define LIBIPSEC_ALGS_KEY_MIN_IDX 1 /* min key size */
2N/A#define LIBIPSEC_ALGS_KEY_MAX_IDX 2 /* max key size */
2N/A#define LIBIPSEC_ALGS_KEY_NUM_VAL 4 /* def, min, max, 0 */
2N/A
2N/A/* Error codes for IPsec algorithms management */
2N/A#define LIBIPSEC_ALGS_DIAG_ALG_EXISTS -1
2N/A#define LIBIPSEC_ALGS_DIAG_PROTO_EXISTS -2
2N/A#define LIBIPSEC_ALGS_DIAG_UNKN_PROTO -3
2N/A#define LIBIPSEC_ALGS_DIAG_UNKN_ALG -4
2N/A#define LIBIPSEC_ALGS_DIAG_NOMEM -5
2N/A#define LIBIPSEC_ALGS_DIAG_ALGSFILEOPEN -6
2N/A#define LIBIPSEC_ALGS_DIAG_ALGSFILEFDOPEN -7
2N/A#define LIBIPSEC_ALGS_DIAG_ALGSFILELOCK -8
2N/A#define LIBIPSEC_ALGS_DIAG_ALGSFILERENAME -9
2N/A#define LIBIPSEC_ALGS_DIAG_ALGSFILEWRITE -10
2N/A#define LIBIPSEC_ALGS_DIAG_ALGSFILECHMOD -11
2N/A#define LIBIPSEC_ALGS_DIAG_ALGSFILECHOWN -12
2N/A#define LIBIPSEC_ALGS_DIAG_ALGSFILECLOSE -13
2N/A
2N/A/* /etc/inet/ipsecalgs keywords and package sections delimiters */
2N/A#define LIBIPSEC_ALGS_LINE_PROTO "PROTO|"
2N/A#define LIBIPSEC_ALGS_LINE_ALG "ALG|"
2N/A#define LIBIPSEC_ALGS_LINE_PKGSTART "# Start "
2N/A#define LIBIPSEC_ALGS_LINE_PKGEND "# End "
2N/A
2N/A/* Put these in libnsl for and process caching testing. */
2N/Aextern int *_real_getipsecprotos(int *);
2N/Aextern int *_real_getipsecalgs(int *, int);
2N/Aextern struct ipsecalgent *_duplicate_alg(struct ipsecalgent *);
2N/Aextern void _clean_trash(ipsec_proto_t *, int);
2N/A
2N/A/* spdsock support functions */
2N/A
2N/A/* Return values for spdsock_get_ext(). */
2N/A#define KGE_OK 0
2N/A#define KGE_DUP 1
2N/A#define KGE_UNK 2
2N/A#define KGE_LEN 3
2N/A#define KGE_CHK 4
2N/A
2N/Aextern int spdsock_get_ext(spd_ext_t *[], spd_msg_t *, uint_t, char *, uint_t);
2N/Aextern const char *spdsock_diag(int);
2N/A
2N/A/* PF_KEY (keysock) support functions */
2N/Aextern const char *keysock_diag(int);
2N/Aextern int in_masktoprefix(uint8_t *, boolean_t);
2N/A
2N/A/* SA support functions */
2N/A
2N/Aextern char *secs2out(unsigned int, char *, int, int);
2N/Aextern char *secs2str(unsigned int, char *, int);
2N/Aextern char *bytecnt2out(uint64_t, char *, size_t, int);
2N/Aextern char *bytecnt2str(uint64_t, char *, size_t);
2N/Aextern void print_diagnostic(FILE *, uint16_t);
2N/Aextern void print_sadb_msg(FILE *, struct sadb_msg *, time_t, boolean_t);
2N/Aextern void print_sa(FILE *, char *, struct sadb_sa *);
2N/Aextern void printsatime(FILE *, int64_t, const char *, const char *,
2N/A const char *, boolean_t);
2N/Aextern void print_lifetimes(FILE *, time_t, struct sadb_lifetime *,
2N/A struct sadb_lifetime *, struct sadb_lifetime *, struct sadb_lifetime *,
2N/A boolean_t vflag);
2N/Aextern void print_address(FILE *, char *, struct sadb_address *, boolean_t);
2N/Aextern void print_asn1_name(FILE *, const unsigned char *, long);
2N/Aextern void print_key(FILE *, char *, struct sadb_key *);
2N/Aextern void print_ident(FILE *, char *, struct sadb_ident *);
2N/Aextern void print_sens(FILE *, char *, const struct sadb_sens *, boolean_t);
2N/Aextern void print_prop(FILE *, char *, struct sadb_prop *);
2N/Aextern void print_eprop(FILE *, char *, struct sadb_prop *);
2N/Aextern void print_supp(FILE *, char *, struct sadb_supported *);
2N/Aextern void print_spirange(FILE *, char *, struct sadb_spirange *);
2N/Aextern void print_kmc(FILE *, char *, struct sadb_x_kmc *);
2N/Aextern void print_samsg(FILE *, uint64_t *, boolean_t, boolean_t, boolean_t);
2N/Aextern char *rparsesatype(int);
2N/Aextern char *rparsealg(uint8_t, int);
2N/Aextern char *rparseidtype(uint16_t);
2N/Aextern boolean_t save_lifetime(struct sadb_lifetime *, FILE *);
2N/Aextern boolean_t save_address(struct sadb_address *, FILE *);
2N/Aextern boolean_t save_key(struct sadb_key *, FILE *);
2N/Aextern boolean_t save_ident(struct sadb_ident *, FILE *);
2N/Aextern void save_assoc(uint64_t *, FILE *);
2N/Aextern FILE *opensavefile(char *);
2N/Aextern const char *do_inet_ntop(const void *, char *, size_t);
2N/A
2N/A/*
2N/A * Label conversion convenience functions.
2N/A */
2N/A
2N/A#include <tsol/label.h>
2N/A
2N/Aextern void ipsec_convert_sens_to_bslabel(const struct sadb_sens *,
2N/A bslabel_t *);
2N/Aextern int ipsec_convert_sl_to_sens(int doi, bslabel_t *, struct sadb_sens *);
2N/Aextern void ipsec_convert_bslabel_to_string(bslabel_t *, char **);
2N/Aextern void ipsec_convert_bslabel_to_hex(bslabel_t *, char **);
2N/A
2N/A/*
2N/A * These exit macros give a consistent exit behaviour for all
2N/A * programs that use libipsecutil. These wll work in usr/src/cmd
2N/A * and usr/src/lib, but because macros in usr/src/lib don't get
2N/A * expanded when I18N message catalogs are built, avoid using
2N/A * these with text inside libipsecutil. See source of ipsecutil_exit()
2N/A * for more details.
2N/A */
2N/A#define EXIT_OK(x) \
2N/A ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x))
2N/A#define EXIT_OK2(x, y) \
2N/A ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x), y)
2N/A#define EXIT_OK3(x, y, z) \
2N/A ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x), y, z)
2N/A#define EXIT_BADCONFIG(x) \
2N/A ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x))
2N/A#define EXIT_BADCONFIG2(x, y) \
2N/A ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x), y)
2N/A#define EXIT_BADCONFIG3(x, y, z) \
2N/A ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x), y, z)
2N/A#define EXIT_MAINTAIN(x) \
2N/A ipsecutil_exit(SERVICE_MAINTAIN, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x))
2N/A#define EXIT_MAINTAIN2(x, y) \
2N/A ipsecutil_exit(SERVICE_MAINTAIN, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x), y)
2N/A#define EXIT_DEGRADE(x) \
2N/A ipsecutil_exit(SERVICE_DEGRADE, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x))
2N/A#define EXIT_BADPERM(x) \
2N/A ipsecutil_exit(SERVICE_BADPERM, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x))
2N/A#define EXIT_BADPERM2(x, y) \
2N/A ipsecutil_exit(SERVICE_BADPERM, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x), y)
2N/A#define EXIT_FATAL(x) \
2N/A ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x))
2N/A#define EXIT_FATAL2(x, y) \
2N/A ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x), y)
2N/A#define EXIT_FATAL3(x, y, z) \
2N/A ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x), y, z)
2N/A#define EXIT_RESTART(x) \
2N/A ipsecutil_exit(SERVICE_RESTART, my_fmri, debugfile, \
2N/A dgettext(TEXT_DOMAIN, x))
2N/A
2N/A#ifdef __cplusplus
2N/A}
2N/A#endif
2N/A
2N/A#endif /* _IPSEC_UTIL_H */