Path: blob/main/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.h
107607 views
/*-1* Copyright (c) 2005-2006 The FreeBSD Project2* All rights reserved.3*4* Author: Shteryana Shopova <[email protected]>5*6* Redistribution of this software and documentation and use in source and7* binary forms, with or without modification, are permitted provided that8* the following conditions are met:9*10* 1. Redistributions of source code or documentation must retain the above11* copyright notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*28* Helper functions common for all tools.29*/3031#ifndef _BSNMP_TOOLS_H_32#define _BSNMP_TOOLS_H_3334/* From asn1.h + 1 byte for trailing zero. */35#define MAX_OCTSTRING_LEN ASN_MAXOCTETSTRING + 136#define MAX_CMD_SYNTAX_LEN 123738/* Arbitrary upper limit on node names and function names - gensnmptree.c. */39#define MAXSTR 10004041/* Should be enough to fetch the biggest allowed octet string. */42#define MAX_BUFF_SIZE (ASN_MAXOCTETSTRING + 50)4344#define SNMP_DEFS_DIR "/usr/share/snmp/defs/"4546#define SNMP_MAX_REPETITIONS 104748enum snmp_access {49SNMP_ACCESS_NONE = 0,50SNMP_ACCESS_GET,51SNMP_ACCESS_SET,52SNMP_ACCESS_GETSET,53};5455/* A structure for integer-string enumerations. */56struct enum_pair {57int32_t enum_val;58char *enum_str;59STAILQ_ENTRY(enum_pair) link;60};6162STAILQ_HEAD(enum_pairs, enum_pair);6364struct enum_type {65char *name;66uint32_t syntax;67int32_t is_enum;68int32_t is_bits;69struct enum_pairs *snmp_enum;70SLIST_ENTRY(enum_type) link;71};7273SLIST_HEAD(snmp_enum_tc, enum_type);7475struct index {76enum snmp_tc tc;77enum snmp_syntax syntax;78struct enum_pairs *snmp_enum;79STAILQ_ENTRY(index) link;80};8182STAILQ_HEAD(snmp_idxlist, index);8384struct snmp_index_entry {85char *string;86uint32_t strlen;87struct asn_oid var;88struct snmp_idxlist index_list;89SLIST_ENTRY(snmp_index_entry) link;90};9192/* Information needed for oid to string conversion. */93struct snmp_oid2str {94char *string;95uint32_t strlen;96enum snmp_tc tc;97enum snmp_syntax syntax;98enum snmp_access access;99struct asn_oid var;100/* A pointer to a entry from the table list - OK if NULL. */101struct snmp_index_entry *table_idx;102/*103* A singly-linked tail queue of all (int, string) pairs -104* for INTEGER syntax only.105*/106struct enum_pairs *snmp_enum;107SLIST_ENTRY(snmp_oid2str) link;108};109110/* A structure to hold each oid input by user. */111struct snmp_object {112/* Flag - if set, the variable caused error in a previous request. */113int32_t error;114/*115* A pointer in the mapping lists - not used if OIDs are input as116* numericals.117*/118struct snmp_oid2str *info;119/* A snmp value to hold the actual oid, syntax and value. */120struct snmp_value val;121SLIST_ENTRY(snmp_object) link;122};123124struct fname {125char *name;126int32_t done;127struct asn_oid cut;128SLIST_ENTRY(fname) link;129};130131SLIST_HEAD(snmp_mapping, snmp_oid2str);132SLIST_HEAD(fname_list, fname);133SLIST_HEAD(snmp_table_index, snmp_index_entry);134135/*136* Keep a list for every syntax type.137*/138struct snmp_mappings {139/* The list containing all non-leaf nodes. */140struct snmp_mapping nodelist;141/* INTEGER/INTEGER32 types. */142struct snmp_mapping intlist;143/* OCTETSTRING types. */144struct snmp_mapping octlist;145/* OID types. */146struct snmp_mapping oidlist;147/* IPADDRESS types. */148struct snmp_mapping iplist;149/* TIMETICKS types. */150struct snmp_mapping ticklist;151/* COUNTER types. */152struct snmp_mapping cntlist;153/* GAUGE types. */154struct snmp_mapping gaugelist;155/* COUNTER64 types. */156struct snmp_mapping cnt64list;157/* ENUM values for oid types. */158struct snmp_mapping enumlist;159/* Description of all table entry types. */160struct snmp_table_index tablelist;161/* Defined enumerated textual conventions. */162struct snmp_enum_tc tclist;163};164165struct snmp_toolinfo {166uint32_t flags;167/* Number of initially input OIDs. */168int32_t objects;169/* List of all input OIDs. */170SLIST_HEAD(snmp_objectlist, snmp_object) snmp_objectlist;171/* All known OID to string mapping data. */172struct snmp_mappings *mappings;173/* A list of .defs filenames to search oid<->string mapping. */174struct fname_list filelist;175/* SNMPv3 USM user credentials */176char *passwd;177};178179/* XXX we might want to get away with this and will need to touch180* XXX the MACROS then too */181extern struct snmp_toolinfo snmptool;182183/* Definitions for some flags' bits. */184#define OUTPUT_BITS 0x00000003 /* bits 0-1 for output type */185#define NUMERIC_BIT 0x00000004 /* bit 2 for numeric oids */186#define RETRY_BIT 0x00000008 /* bit 3 for retry on error response */187#define ERRIGNORE_BIT 0x00000010 /* bit 4 for skip sanity checking */188#define ERRIGNORE_BIT 0x00000010 /* bit 4 for skip sanity checking */189#define EDISCOVER_BIT 0x00000020 /* bit 5 for SNMP Engine Discovery */190#define LOCALKEY_BIT 0x00000040 /* bit 6 for using localized key */191/* 0x00000080 */ /* bit 7 reserved */192#define PDUTYPE_BITS 0x00000f00 /* bits 8-11 for pdu type */193/* 0x0000f000 */ /* bit 12-15 reserved */194#define MAXREP_BITS 0x00ff0000 /* bits 16-23 for max-repetit. value */195#define NONREP_BITS 0xff000000 /* bits 24-31 for non-repeaters value */196197#define OUTPUT_SHORT 0x0198#define OUTPUT_VERBOSE 0x1199#define OUTPUT_TABULAR 0x2200#define OUTPUT_QUIET 0x3201202/* Macros for playing with flags' bits. */203#define SET_OUTPUT(ctx, type) ((ctx)->flags |= ((type) & OUTPUT_BITS))204#define GET_OUTPUT(ctx) ((ctx)->flags & OUTPUT_BITS)205206#define SET_NUMERIC(ctx) ((ctx)->flags |= NUMERIC_BIT)207#define ISSET_NUMERIC(ctx) ((ctx)->flags & NUMERIC_BIT)208209#define SET_RETRY(ctx) ((ctx)->flags |= RETRY_BIT)210#define ISSET_RETRY(ctx) ((ctx)->flags & RETRY_BIT)211212#define SET_ERRIGNORE(ctx) ((ctx)->flags |= ERRIGNORE_BIT)213#define ISSET_ERRIGNORE(ctx) ((ctx)->flags & ERRIGNORE_BIT)214215#define SET_EDISCOVER(ctx) ((ctx)->flags |= EDISCOVER_BIT)216#define ISSET_EDISCOVER(ctx) ((ctx)->flags & EDISCOVER_BIT)217218#define SET_LOCALKEY(ctx) ((ctx)->flags |= LOCALKEY_BIT)219#define ISSET_LOCALKEY(ctx) ((ctx)->flags & LOCALKEY_BIT)220221#define SET_PDUTYPE(ctx, type) (((ctx)->flags |= (((type) & 0xf) << 8)))222#define GET_PDUTYPE(ctx) (((ctx)->flags & PDUTYPE_BITS) >> 8)223224#define SET_MAXREP(ctx, i) (((ctx)->flags |= (((i) & 0xff) << 16)))225#define GET_MAXREP(ctx) (((ctx)->flags & MAXREP_BITS) >> 16)226227#define SET_NONREP(ctx, i) (((ctx)->flags |= (((i) & 0xff) << 24)))228#define GET_NONREP(ctx) (((ctx)->flags & NONREP_BITS) >> 24)229230extern int _bsnmptools_debug;231extern const struct asn_oid IsoOrgDod_OID;232233int snmptool_init(struct snmp_toolinfo *);234int32_t snmp_import_file(struct snmp_toolinfo *, struct fname *);235int32_t snmp_import_all(struct snmp_toolinfo *);236int32_t add_filename(struct snmp_toolinfo *, const char *,237const struct asn_oid *, int32_t);238void free_filelist(struct snmp_toolinfo *);239void snmp_tool_freeall(struct snmp_toolinfo *);240void snmp_import_dump(int);241242/* bsnmpmap.c */243struct snmp_mappings *snmp_mapping_init(void);244int32_t snmp_mapping_free(struct snmp_toolinfo *);245void snmp_index_listfree(struct snmp_idxlist *);246void snmp_dump_oid2str(struct snmp_oid2str *);247int32_t snmp_node_insert(struct snmp_toolinfo *, struct snmp_oid2str *);248int32_t snmp_leaf_insert(struct snmp_toolinfo *, struct snmp_oid2str *);249int32_t snmp_enum_insert(struct snmp_toolinfo *, struct snmp_oid2str *);250struct enum_pairs *enum_pairs_init(void);251void enum_pairs_free(struct enum_pairs *);252void snmp_mapping_entryfree(struct snmp_oid2str *);253int32_t enum_pair_insert(struct enum_pairs *, int32_t, char *);254char *enum_string_lookup(struct enum_pairs *, int32_t);255int32_t enum_number_lookup(struct enum_pairs *, char *);256int32_t snmp_syntax_insert(struct snmp_idxlist *, struct enum_pairs *,257enum snmp_syntax, enum snmp_tc);258int32_t snmp_table_insert(struct snmp_toolinfo *, struct snmp_index_entry *);259260struct enum_type *snmp_enumtc_init(char *);261void snmp_enumtc_free(struct enum_type *);262void snmp_enumtc_insert(struct snmp_toolinfo *, struct enum_type *);263struct enum_type *snmp_enumtc_lookup(struct snmp_toolinfo *, char *);264265void snmp_mapping_dump(struct snmp_toolinfo *);266int32_t snmp_lookup_leafstring(struct snmp_toolinfo *, struct snmp_object *);267int32_t snmp_lookup_enumstring(struct snmp_toolinfo *, struct snmp_object *);268int32_t snmp_lookup_oidstring(struct snmp_toolinfo *, struct snmp_object *);269int32_t snmp_lookup_nonleaf_string(struct snmp_toolinfo *, struct snmp_object *);270int32_t snmp_lookup_allstring(struct snmp_toolinfo *, struct snmp_object *);271int32_t snmp_lookup_nodestring(struct snmp_toolinfo *, struct snmp_object *);272int32_t snmp_lookup_oidall(struct snmp_toolinfo *, struct snmp_object *, char *);273int32_t snmp_lookup_enumoid(struct snmp_toolinfo *, struct snmp_object *, char *);274int32_t snmp_lookup_oid(struct snmp_toolinfo *, struct snmp_object *, char *);275276/* Functions parsing common options for all tools. */277int32_t parse_server(char *);278int32_t parse_timeout(char *);279int32_t parse_retry(char *);280int32_t parse_version(char *);281int32_t parse_local_path(char *);282int32_t parse_buflen(char *);283int32_t parse_debug(void);284int32_t parse_discovery(struct snmp_toolinfo *);285int32_t parse_local_key(struct snmp_toolinfo *);286int32_t parse_num_oids(struct snmp_toolinfo *);287int32_t parse_file(struct snmp_toolinfo *, char *);288int32_t parse_include(struct snmp_toolinfo *, char *);289int32_t parse_output(struct snmp_toolinfo *, char *);290int32_t parse_errors(struct snmp_toolinfo *);291int32_t parse_skip_access(struct snmp_toolinfo *);292int32_t parse_authentication(struct snmp_toolinfo *, char *);293int32_t parse_privacy(struct snmp_toolinfo *, char *);294int32_t parse_context(struct snmp_toolinfo *, char *);295int32_t parse_user_security(struct snmp_toolinfo *, char *);296297typedef int32_t (*snmp_verify_inoid_f) (struct snmp_toolinfo *,298struct snmp_object *, char *);299int32_t snmp_object_add(struct snmp_toolinfo *, snmp_verify_inoid_f, char *);300int32_t snmp_object_remove(struct snmp_toolinfo *, struct asn_oid *oid);301int32_t snmp_object_seterror(struct snmp_toolinfo *, struct snmp_value *,302int32_t);303304enum snmp_syntax parse_syntax(char *);305char *snmp_parse_suboid(char *, struct asn_oid *);306char *snmp_parse_index(struct snmp_toolinfo *, char *, struct snmp_object *);307int32_t snmp_parse_numoid(char *, struct asn_oid *);308int32_t snmp_suboid_append(struct asn_oid *, asn_subid_t);309int32_t snmp_suboid_pop(struct asn_oid *);310311typedef int32_t (*snmp_verify_vbind_f) (struct snmp_toolinfo *,312struct snmp_pdu *, struct snmp_object *);313typedef int32_t (*snmp_add_vbind_f) (struct snmp_pdu *, struct snmp_object *);314int32_t snmp_pdu_add_bindings(struct snmp_toolinfo *, snmp_verify_vbind_f,315snmp_add_vbind_f, struct snmp_pdu *, int32_t);316317int32_t snmp_parse_get_resp(struct snmp_pdu *, struct snmp_pdu *);318int32_t snmp_parse_getbulk_resp(struct snmp_pdu *, struct snmp_pdu *);319int32_t snmp_parse_getnext_resp(struct snmp_pdu *, struct snmp_pdu *);320int32_t snmp_parse_resp(struct snmp_pdu *, struct snmp_pdu *);321int32_t snmp_output_numval(struct snmp_toolinfo *, struct snmp_value *,322struct snmp_oid2str *);323void snmp_output_val(struct snmp_value *);324int32_t snmp_output_resp(struct snmp_toolinfo *, struct snmp_pdu *, struct asn_oid *);325void snmp_output_err_resp(struct snmp_toolinfo *, struct snmp_pdu *);326void snmp_output_engine(void);327void snmp_output_keys(void);328329#endif /* _BSNMP_TOOLS_H_ */330331332