bsnmptools.h revision 216295
1216295Ssyrinx/*- 2216295Ssyrinx * Copyright (c) 2005-2006 The FreeBSD Project 3216295Ssyrinx * All rights reserved. 4216295Ssyrinx * 5216295Ssyrinx * Author: Shteryana Shopova <syrinx@FreeBSD.org> 6216295Ssyrinx * 7216295Ssyrinx * Redistribution of this software and documentation and use in source and 8216295Ssyrinx * binary forms, with or without modification, are permitted provided that 9216295Ssyrinx * the following conditions are met: 10216295Ssyrinx * 11216295Ssyrinx * 1. Redistributions of source code or documentation must retain the above 12216295Ssyrinx * copyright notice, this list of conditions and the following disclaimer. 13216295Ssyrinx * 2. Redistributions in binary form must reproduce the above copyright 14216295Ssyrinx * notice, this list of conditions and the following disclaimer in the 15216295Ssyrinx * documentation and/or other materials provided with the distribution. 16216295Ssyrinx * 17216295Ssyrinx * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18216295Ssyrinx * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19216295Ssyrinx * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20216295Ssyrinx * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21216295Ssyrinx * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22216295Ssyrinx * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23216295Ssyrinx * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24216295Ssyrinx * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25216295Ssyrinx * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26216295Ssyrinx * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27216295Ssyrinx * SUCH DAMAGE. 28216295Ssyrinx * 29216295Ssyrinx * Helper functions common for all tools. 30216295Ssyrinx * 31216295Ssyrinx * $FreeBSD: head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.h 216295 2010-12-08 14:30:25Z syrinx $ 32216295Ssyrinx */ 33216295Ssyrinx 34216295Ssyrinx#ifndef _BSNMP_TOOLS_H_ 35216295Ssyrinx#define _BSNMP_TOOLS_H_ 36216295Ssyrinx 37216295Ssyrinx/* From asn1.h + 1 byte for trailing zero. */ 38216295Ssyrinx#define MAX_OCTSTRING_LEN ASN_MAXOCTETSTRING + 1 39216295Ssyrinx#define MAX_CMD_SYNTAX_LEN 12 40216295Ssyrinx 41216295Ssyrinx/* Arbitrary upper limit on node names and function names - gensnmptree.c. */ 42216295Ssyrinx#define MAXSTR 1000 43216295Ssyrinx 44216295Ssyrinx/* Should be enough to fetch the biggest allowed octet string. */ 45216295Ssyrinx#define MAX_BUFF_SIZE (ASN_MAXOCTETSTRING + 50) 46216295Ssyrinx 47216295Ssyrinx#define SNMP_DEFS_DIR "/usr/share/snmp/defs/" 48216295Ssyrinx#define SNMP_DEFAULT_LOCAL "/var/run/snmpd.sock" 49216295Ssyrinx 50216295Ssyrinxenum snmp_access { 51216295Ssyrinx SNMP_ACCESS_NONE = 0, 52216295Ssyrinx SNMP_ACCESS_GET, 53216295Ssyrinx SNMP_ACCESS_SET, 54216295Ssyrinx SNMP_ACCESS_GETSET, 55216295Ssyrinx}; 56216295Ssyrinx 57216295Ssyrinx/* A structure for integer-string enumerations. */ 58216295Ssyrinxstruct enum_pair { 59216295Ssyrinx int32_t enum_val; 60216295Ssyrinx char *enum_str; 61216295Ssyrinx STAILQ_ENTRY(enum_pair) link; 62216295Ssyrinx}; 63216295Ssyrinx 64216295SsyrinxSTAILQ_HEAD(enum_pairs, enum_pair); 65216295Ssyrinx 66216295Ssyrinxstruct enum_type { 67216295Ssyrinx char *name; 68216295Ssyrinx uint32_t syntax; 69216295Ssyrinx int32_t is_enum; 70216295Ssyrinx int32_t is_bits; 71216295Ssyrinx struct enum_pairs *snmp_enum; 72216295Ssyrinx SLIST_ENTRY(enum_type) link; 73216295Ssyrinx}; 74216295Ssyrinx 75216295SsyrinxSLIST_HEAD(snmp_enum_tc, enum_type); 76216295Ssyrinx 77216295Ssyrinxstruct index { 78216295Ssyrinx enum snmp_tc tc; 79216295Ssyrinx enum snmp_syntax syntax; 80216295Ssyrinx struct enum_pairs *snmp_enum; 81216295Ssyrinx STAILQ_ENTRY(index) link; 82216295Ssyrinx}; 83216295Ssyrinx 84216295SsyrinxSTAILQ_HEAD(snmp_idxlist, index); 85216295Ssyrinx 86216295Ssyrinxstruct snmp_index_entry { 87216295Ssyrinx char *string; 88216295Ssyrinx uint32_t strlen; 89216295Ssyrinx struct asn_oid var; 90216295Ssyrinx struct snmp_idxlist index_list; 91216295Ssyrinx SLIST_ENTRY(snmp_index_entry) link; 92216295Ssyrinx}; 93216295Ssyrinx 94216295Ssyrinx/* Information needed for oid to string conversion. */ 95216295Ssyrinxstruct snmp_oid2str { 96216295Ssyrinx char *string; 97216295Ssyrinx uint32_t strlen; 98216295Ssyrinx enum snmp_tc tc; 99216295Ssyrinx enum snmp_syntax syntax; 100216295Ssyrinx enum snmp_access access; 101216295Ssyrinx struct asn_oid var; 102216295Ssyrinx /* A pointer to a entry from the table list - OK if NULL. */ 103216295Ssyrinx struct snmp_index_entry *table_idx; 104216295Ssyrinx /* 105216295Ssyrinx * A singly-linked tail queue of all (int, string) pairs - 106216295Ssyrinx * for INTEGER syntax only. 107216295Ssyrinx */ 108216295Ssyrinx struct enum_pairs *snmp_enum; 109216295Ssyrinx SLIST_ENTRY(snmp_oid2str) link; 110216295Ssyrinx}; 111216295Ssyrinx 112216295Ssyrinx/* A structure to hold each oid input by user. */ 113216295Ssyrinxstruct snmp_object { 114216295Ssyrinx /* Flag - if set, the variable caused error in a previous request. */ 115216295Ssyrinx int32_t error; 116216295Ssyrinx /* 117216295Ssyrinx * A pointer in the mapping lists - not used if OIDs are input as 118216295Ssyrinx * numericals. 119216295Ssyrinx */ 120216295Ssyrinx struct snmp_oid2str *info; 121216295Ssyrinx /* A snmp value to hold the actual oid, syntax and value. */ 122216295Ssyrinx struct snmp_value val; 123216295Ssyrinx SLIST_ENTRY(snmp_object) link; 124216295Ssyrinx}; 125216295Ssyrinx 126216295Ssyrinxstruct fname { 127216295Ssyrinx char *name; 128216295Ssyrinx int32_t done; 129216295Ssyrinx struct asn_oid cut; 130216295Ssyrinx SLIST_ENTRY(fname) link; 131216295Ssyrinx}; 132216295Ssyrinx 133216295SsyrinxSLIST_HEAD(snmp_mapping, snmp_oid2str); 134216295SsyrinxSLIST_HEAD(fname_list, fname); 135216295SsyrinxSLIST_HEAD(snmp_table_index, snmp_index_entry); 136216295Ssyrinx 137216295Ssyrinx/* 138216295Ssyrinx * Keep a list for every syntax type. 139216295Ssyrinx */ 140216295Ssyrinxstruct snmp_mappings { 141216295Ssyrinx /* The list containing all non-leaf nodes. */ 142216295Ssyrinx struct snmp_mapping nodelist; 143216295Ssyrinx /* INTEGER/INTEGER32 types. */ 144216295Ssyrinx struct snmp_mapping intlist; 145216295Ssyrinx /* OCTETSTRING types. */ 146216295Ssyrinx struct snmp_mapping octlist; 147216295Ssyrinx /* OID types. */ 148216295Ssyrinx struct snmp_mapping oidlist; 149216295Ssyrinx /* IPADDRESS types. */ 150216295Ssyrinx struct snmp_mapping iplist; 151216295Ssyrinx /* TIMETICKS types. */ 152216295Ssyrinx struct snmp_mapping ticklist; 153216295Ssyrinx /* COUNTER types. */ 154216295Ssyrinx struct snmp_mapping cntlist; 155216295Ssyrinx /* GAUGE types. */ 156216295Ssyrinx struct snmp_mapping gaugelist; 157216295Ssyrinx /* COUNTER64 types. */ 158216295Ssyrinx struct snmp_mapping cnt64list; 159216295Ssyrinx /* ENUM values for oid types. */ 160216295Ssyrinx struct snmp_mapping enumlist; 161216295Ssyrinx /* Description of all table entry types. */ 162216295Ssyrinx struct snmp_table_index tablelist; 163216295Ssyrinx /* Defined enumerated textual conventions. */ 164216295Ssyrinx struct snmp_enum_tc tclist; 165216295Ssyrinx}; 166216295Ssyrinx 167216295Ssyrinxstruct snmp_toolinfo { 168216295Ssyrinx uint32_t flags; 169216295Ssyrinx /* Number of initially input OIDs. */ 170216295Ssyrinx int32_t objects; 171216295Ssyrinx /* List of all input OIDs. */ 172216295Ssyrinx SLIST_HEAD(snmp_objectlist, snmp_object) snmp_objectlist; 173216295Ssyrinx /* All known OID to string mapping data. */ 174216295Ssyrinx struct snmp_mappings *mappings; 175216295Ssyrinx /* A list of .defs filenames to search oid<->string mapping. */ 176216295Ssyrinx struct fname_list filelist; 177216295Ssyrinx /* SNMPv3 USM user credentials */ 178216295Ssyrinx char *passwd; 179216295Ssyrinx}; 180216295Ssyrinx 181216295Ssyrinx/* XXX we might want to get away with this and will need to touch 182216295Ssyrinx * XXX the MACROS then too */ 183216295Ssyrinxextern struct snmp_toolinfo snmptool; 184216295Ssyrinx 185216295Ssyrinx/* Definitions for some flags' bits. */ 186216295Ssyrinx#define OUTPUT_BITS 0x00000003 /* bits 0-1 for output type */ 187216295Ssyrinx#define NUMERIC_BIT 0x00000004 /* bit 2 for numeric oids */ 188216295Ssyrinx#define RETRY_BIT 0x00000008 /* bit 3 for retry on error responce */ 189216295Ssyrinx#define ERRIGNORE_BIT 0x00000010 /* bit 4 for skip sanity checking */ 190216295Ssyrinx#define ERRIGNORE_BIT 0x00000010 /* bit 4 for skip sanity checking */ 191216295Ssyrinx#define EDISCOVER_BIT 0x00000020 /* bit 5 for SNMP Engine Discovery */ 192216295Ssyrinx#define LOCALKEY_BIT 0x00000040 /* bit 6 for using localized key */ 193216295Ssyrinx /* 0x00000080 */ /* bit 7 reserverd */ 194216295Ssyrinx#define PDUTYPE_BITS 0x00000f00 /* bits 8-11 for pdu type */ 195216295Ssyrinx /* 0x0000f000 */ /* bit 12-15 reserverd */ 196216295Ssyrinx#define MAXREP_BITS 0x00ff0000 /* bits 16-23 for max-repetit. value */ 197216295Ssyrinx#define NONREP_BITS 0xff000000 /* bits 24-31 for non-repeaters value */ 198216295Ssyrinx 199216295Ssyrinx#define OUTPUT_SHORT 0x0 200216295Ssyrinx#define OUTPUT_VERBOSE 0x1 201216295Ssyrinx#define OUTPUT_TABULAR 0x2 202216295Ssyrinx#define OUTPUT_QUIET 0x3 203216295Ssyrinx 204216295Ssyrinx/* Macros for playing with flags' bits. */ 205216295Ssyrinx#define SET_OUTPUT(ctx, type) ((ctx)->flags |= ((type) & OUTPUT_BITS)) 206216295Ssyrinx#define GET_OUTPUT(ctx) ((ctx)->flags & OUTPUT_BITS) 207216295Ssyrinx 208216295Ssyrinx#define SET_NUMERIC(ctx) ((ctx)->flags |= NUMERIC_BIT) 209216295Ssyrinx#define ISSET_NUMERIC(ctx) ((ctx)->flags & NUMERIC_BIT) 210216295Ssyrinx 211216295Ssyrinx#define SET_RETRY(ctx) ((ctx)->flags |= RETRY_BIT) 212216295Ssyrinx#define ISSET_RETRY(ctx) ((ctx)->flags & RETRY_BIT) 213216295Ssyrinx 214216295Ssyrinx#define SET_ERRIGNORE(ctx) ((ctx)->flags |= ERRIGNORE_BIT) 215216295Ssyrinx#define ISSET_ERRIGNORE(ctx) ((ctx)->flags & ERRIGNORE_BIT) 216216295Ssyrinx 217216295Ssyrinx#define SET_EDISCOVER(ctx) ((ctx)->flags |= EDISCOVER_BIT) 218216295Ssyrinx#define ISSET_EDISCOVER(ctx) ((ctx)->flags & EDISCOVER_BIT) 219216295Ssyrinx 220216295Ssyrinx#define SET_LOCALKEY(ctx) ((ctx)->flags |= LOCALKEY_BIT) 221216295Ssyrinx#define ISSET_LOCALKEY(ctx) ((ctx)->flags & LOCALKEY_BIT) 222216295Ssyrinx 223216295Ssyrinx#define SET_PDUTYPE(ctx, type) (((ctx)->flags |= (((type) & 0xf) << 8))) 224216295Ssyrinx#define GET_PDUTYPE(ctx) (((ctx)->flags & PDUTYPE_BITS) >> 8) 225216295Ssyrinx 226216295Ssyrinx#define SET_MAXREP(ctx, i) (((ctx)->flags |= (((i) & 0xff) << 16))) 227216295Ssyrinx#define GET_MAXREP(ctx) (((ctx)->flags & MAXREP_BITS) >> 16) 228216295Ssyrinx 229216295Ssyrinx#define SET_NONREP(ctx, i) (((ctx)->flags |= (((i) & 0xff) << 24))) 230216295Ssyrinx#define GET_NONREP(ctx) (((ctx)->flags & NONREP_BITS) >> 24) 231216295Ssyrinx 232216295Ssyrinx 233216295Ssyrinxextern const struct asn_oid IsoOrgDod_OID; 234216295Ssyrinx 235216295Ssyrinxint snmptool_init(struct snmp_toolinfo *); 236216295Ssyrinxint32_t snmp_import_file(struct snmp_toolinfo *, struct fname *); 237216295Ssyrinxint32_t snmp_import_all(struct snmp_toolinfo *); 238216295Ssyrinxint32_t add_filename(struct snmp_toolinfo *, const char *, 239216295Ssyrinx const struct asn_oid *, int32_t); 240216295Ssyrinxvoid free_filelist(struct snmp_toolinfo *); 241216295Ssyrinxvoid snmp_tool_freeall(struct snmp_toolinfo *); 242216295Ssyrinxvoid snmp_import_dump(int); 243216295Ssyrinx 244216295Ssyrinx/* bsnmpmap.c */ 245216295Ssyrinxstruct snmp_mappings *snmp_mapping_init(void); 246216295Ssyrinxint32_t snmp_mapping_free(struct snmp_toolinfo *); 247216295Ssyrinxvoid snmp_index_listfree(struct snmp_idxlist *); 248216295Ssyrinxvoid snmp_dump_oid2str(struct snmp_oid2str *); 249216295Ssyrinxint32_t snmp_node_insert(struct snmp_toolinfo *, struct snmp_oid2str *); 250216295Ssyrinxint32_t snmp_leaf_insert(struct snmp_toolinfo *, struct snmp_oid2str *); 251216295Ssyrinxint32_t snmp_enum_insert(struct snmp_toolinfo *, struct snmp_oid2str *); 252216295Ssyrinxstruct enum_pairs *enum_pairs_init(void); 253216295Ssyrinxvoid enum_pairs_free(struct enum_pairs *); 254216295Ssyrinxvoid snmp_mapping_entryfree(struct snmp_oid2str *); 255216295Ssyrinxint32_t enum_pair_insert(struct enum_pairs *, int32_t, char *); 256216295Ssyrinxchar *enum_string_lookup(struct enum_pairs *, int32_t); 257216295Ssyrinxint32_t enum_number_lookup(struct enum_pairs *, char *); 258216295Ssyrinxint32_t snmp_syntax_insert(struct snmp_idxlist *, struct enum_pairs *, 259216295Ssyrinx enum snmp_syntax, enum snmp_tc); 260216295Ssyrinxint32_t snmp_table_insert(struct snmp_toolinfo *, struct snmp_index_entry *); 261216295Ssyrinx 262216295Ssyrinxstruct enum_type *snmp_enumtc_init(char *); 263216295Ssyrinxvoid snmp_enumtc_free(struct enum_type *); 264216295Ssyrinxvoid snmp_enumtc_insert(struct snmp_toolinfo *, struct enum_type *); 265216295Ssyrinxstruct enum_type *snmp_enumtc_lookup(struct snmp_toolinfo *, char *); 266216295Ssyrinx 267216295Ssyrinxvoid snmp_mapping_dump(struct snmp_toolinfo *); 268216295Ssyrinxint32_t snmp_lookup_leafstring(struct snmp_toolinfo *, struct snmp_object *); 269216295Ssyrinxint32_t snmp_lookup_enumstring(struct snmp_toolinfo *, struct snmp_object *); 270216295Ssyrinxint32_t snmp_lookup_oidstring(struct snmp_toolinfo *, struct snmp_object *); 271216295Ssyrinxint32_t snmp_lookup_nonleaf_string(struct snmp_toolinfo *, struct snmp_object *); 272216295Ssyrinxint32_t snmp_lookup_allstring(struct snmp_toolinfo *, struct snmp_object *); 273216295Ssyrinxint32_t snmp_lookup_nodestring(struct snmp_toolinfo *, struct snmp_object *); 274216295Ssyrinxint32_t snmp_lookup_oidall(struct snmp_toolinfo *, struct snmp_object *, char *); 275216295Ssyrinxint32_t snmp_lookup_enumoid(struct snmp_toolinfo *, struct snmp_object *, char *); 276216295Ssyrinxint32_t snmp_lookup_oid(struct snmp_toolinfo *, struct snmp_object *, char *); 277216295Ssyrinx 278216295Ssyrinx/* Functions parsing common options for all tools. */ 279216295Ssyrinxint32_t parse_server(char *); 280216295Ssyrinxint32_t parse_timeout(char *); 281216295Ssyrinxint32_t parse_retry(char *); 282216295Ssyrinxint32_t parse_version(char *); 283216295Ssyrinxint32_t parse_local_path(char *); 284216295Ssyrinxint32_t parse_buflen(char *); 285216295Ssyrinxint32_t parse_debug(void); 286216295Ssyrinxint32_t parse_discovery(struct snmp_toolinfo *); 287216295Ssyrinxint32_t parse_local_key(struct snmp_toolinfo *); 288216295Ssyrinxint32_t parse_num_oids(struct snmp_toolinfo *); 289216295Ssyrinxint32_t parse_file(struct snmp_toolinfo *, char *); 290216295Ssyrinxint32_t parse_include(struct snmp_toolinfo *, char *); 291216295Ssyrinxint32_t parse_output(struct snmp_toolinfo *, char *); 292216295Ssyrinxint32_t parse_errors(struct snmp_toolinfo *); 293216295Ssyrinxint32_t parse_skip_access(struct snmp_toolinfo *); 294216295Ssyrinxint32_t parse_authentication(struct snmp_toolinfo *, char *); 295216295Ssyrinxint32_t parse_privacy(struct snmp_toolinfo *, char *); 296216295Ssyrinxint32_t parse_context(struct snmp_toolinfo *, char *); 297216295Ssyrinxint32_t parse_user_security(struct snmp_toolinfo *, char *); 298216295Ssyrinx 299216295Ssyrinxtypedef int32_t (*snmp_verify_inoid_f) (struct snmp_toolinfo *, 300216295Ssyrinx struct snmp_object *, char *); 301216295Ssyrinxint32_t snmp_object_add(struct snmp_toolinfo *, snmp_verify_inoid_f, char *); 302216295Ssyrinxint32_t snmp_object_remove(struct snmp_toolinfo *, struct asn_oid *oid); 303216295Ssyrinxint32_t snmp_object_seterror(struct snmp_toolinfo *, struct snmp_value *, 304216295Ssyrinx int32_t); 305216295Ssyrinx 306216295Ssyrinxenum snmp_syntax parse_syntax(char *); 307216295Ssyrinxchar *snmp_parse_suboid(char *, struct asn_oid *); 308216295Ssyrinxchar *snmp_parse_index(struct snmp_toolinfo *, char *, struct snmp_object *); 309216295Ssyrinxint32_t snmp_parse_numoid(char *, struct asn_oid *); 310216295Ssyrinxint32_t snmp_suboid_append(struct asn_oid *, asn_subid_t); 311216295Ssyrinxint32_t snmp_suboid_pop(struct asn_oid *); 312216295Ssyrinx 313216295Ssyrinxtypedef int32_t (*snmp_verify_vbind_f) (struct snmp_toolinfo *, 314216295Ssyrinx struct snmp_pdu *, struct snmp_object *); 315216295Ssyrinxtypedef int32_t (*snmp_add_vbind_f) (struct snmp_pdu *, struct snmp_object *); 316216295Ssyrinxint32_t snmp_pdu_add_bindings(struct snmp_toolinfo *, snmp_verify_vbind_f, 317216295Ssyrinx snmp_add_vbind_f, struct snmp_pdu *, int32_t); 318216295Ssyrinx 319216295Ssyrinxint32_t snmp_parse_get_resp(struct snmp_pdu *, struct snmp_pdu *); 320216295Ssyrinxint32_t snmp_parse_getbulk_resp(struct snmp_pdu *, struct snmp_pdu *); 321216295Ssyrinxint32_t snmp_parse_getnext_resp(struct snmp_pdu *, struct snmp_pdu *); 322216295Ssyrinxint32_t snmp_parse_resp(struct snmp_pdu *, struct snmp_pdu *); 323216295Ssyrinxint32_t snmp_output_numval(struct snmp_toolinfo *, struct snmp_value *, 324216295Ssyrinx struct snmp_oid2str *); 325216295Ssyrinxvoid snmp_output_val(struct snmp_value *); 326216295Ssyrinxint32_t snmp_output_resp(struct snmp_toolinfo *, struct snmp_pdu *); 327216295Ssyrinxvoid snmp_output_err_resp(struct snmp_toolinfo *, struct snmp_pdu *); 328216295Ssyrinxvoid snmp_output_engine(void); 329216295Ssyrinxvoid snmp_output_keys(void); 330216295Ssyrinx 331216295Ssyrinx#endif /* _BSNMP_TOOLS_H_ */ 332