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$
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
50229933Ssyrinx#define	SNMP_MAX_REPETITIONS	10
51229933Ssyrinx
52216295Ssyrinxenum snmp_access {
53216295Ssyrinx	SNMP_ACCESS_NONE = 0,
54216295Ssyrinx	SNMP_ACCESS_GET,
55216295Ssyrinx	SNMP_ACCESS_SET,
56216295Ssyrinx	SNMP_ACCESS_GETSET,
57216295Ssyrinx};
58216295Ssyrinx
59216295Ssyrinx/* A structure for integer-string enumerations. */
60216295Ssyrinxstruct enum_pair {
61216295Ssyrinx	int32_t	enum_val;
62216295Ssyrinx	char	*enum_str;
63216295Ssyrinx	STAILQ_ENTRY(enum_pair)	link;
64216295Ssyrinx};
65216295Ssyrinx
66216295SsyrinxSTAILQ_HEAD(enum_pairs, enum_pair);
67216295Ssyrinx
68216295Ssyrinxstruct enum_type {
69216295Ssyrinx	char		*name;
70216295Ssyrinx	uint32_t	syntax;
71216295Ssyrinx	int32_t		is_enum;
72216295Ssyrinx	int32_t		is_bits;
73216295Ssyrinx	struct enum_pairs	*snmp_enum;
74216295Ssyrinx	SLIST_ENTRY(enum_type)	link;
75216295Ssyrinx};
76216295Ssyrinx
77216295SsyrinxSLIST_HEAD(snmp_enum_tc, enum_type);
78216295Ssyrinx
79216295Ssyrinxstruct index {
80216295Ssyrinx	enum snmp_tc		tc;
81216295Ssyrinx	enum snmp_syntax	syntax;
82216295Ssyrinx	struct enum_pairs	*snmp_enum;
83216295Ssyrinx	STAILQ_ENTRY(index)	link;
84216295Ssyrinx};
85216295Ssyrinx
86216295SsyrinxSTAILQ_HEAD(snmp_idxlist, index);
87216295Ssyrinx
88216295Ssyrinxstruct snmp_index_entry {
89216295Ssyrinx	char			*string;
90216295Ssyrinx	uint32_t		strlen;
91216295Ssyrinx	struct asn_oid		var;
92216295Ssyrinx	struct snmp_idxlist	index_list;
93216295Ssyrinx	SLIST_ENTRY(snmp_index_entry)	link;
94216295Ssyrinx};
95216295Ssyrinx
96216295Ssyrinx/* Information needed for oid to string conversion. */
97216295Ssyrinxstruct snmp_oid2str {
98216295Ssyrinx	char			*string;
99216295Ssyrinx	uint32_t		strlen;
100216295Ssyrinx	enum snmp_tc		tc;
101216295Ssyrinx	enum snmp_syntax	syntax;
102216295Ssyrinx	enum snmp_access	access;
103216295Ssyrinx	struct asn_oid		var;
104216295Ssyrinx	/* A pointer to a entry from the table list - OK if NULL. */
105216295Ssyrinx	struct snmp_index_entry	*table_idx;
106216295Ssyrinx	/*
107216295Ssyrinx	 * A singly-linked tail queue of all (int, string) pairs -
108216295Ssyrinx	 * for INTEGER syntax only.
109216295Ssyrinx	 */
110216295Ssyrinx	struct enum_pairs	*snmp_enum;
111216295Ssyrinx	SLIST_ENTRY(snmp_oid2str)	link;
112216295Ssyrinx};
113216295Ssyrinx
114216295Ssyrinx/* A structure to hold each oid input by user. */
115216295Ssyrinxstruct snmp_object {
116216295Ssyrinx	/* Flag - if set, the variable caused error in a previous request. */
117216295Ssyrinx	int32_t			error;
118216295Ssyrinx	/*
119216295Ssyrinx	 * A pointer in the mapping lists - not used if OIDs are input as
120216295Ssyrinx	 * numericals.
121216295Ssyrinx	 */
122216295Ssyrinx	struct snmp_oid2str	*info;
123216295Ssyrinx	/* A snmp value to hold the actual oid, syntax and value. */
124216295Ssyrinx	struct snmp_value	val;
125216295Ssyrinx	SLIST_ENTRY(snmp_object)	link;
126216295Ssyrinx};
127216295Ssyrinx
128216295Ssyrinxstruct fname {
129216295Ssyrinx	char		*name;
130216295Ssyrinx	int32_t		done;
131216295Ssyrinx	struct asn_oid	cut;
132216295Ssyrinx	SLIST_ENTRY(fname)	link;
133216295Ssyrinx};
134216295Ssyrinx
135216295SsyrinxSLIST_HEAD(snmp_mapping, snmp_oid2str);
136216295SsyrinxSLIST_HEAD(fname_list, fname);
137216295SsyrinxSLIST_HEAD(snmp_table_index, snmp_index_entry);
138216295Ssyrinx
139216295Ssyrinx/*
140216295Ssyrinx * Keep a list for every syntax type.
141216295Ssyrinx */
142216295Ssyrinxstruct snmp_mappings {
143216295Ssyrinx	/* The list containing all non-leaf nodes. */
144216295Ssyrinx	struct snmp_mapping		nodelist;
145216295Ssyrinx	/* INTEGER/INTEGER32 types. */
146216295Ssyrinx	struct snmp_mapping		intlist;
147216295Ssyrinx	/* OCTETSTRING types. */
148216295Ssyrinx	struct snmp_mapping		octlist;
149216295Ssyrinx	/* OID types. */
150216295Ssyrinx	struct snmp_mapping		oidlist;
151216295Ssyrinx	/* IPADDRESS types. */
152216295Ssyrinx	struct snmp_mapping		iplist;
153216295Ssyrinx	/* TIMETICKS types. */
154216295Ssyrinx	struct snmp_mapping		ticklist;
155216295Ssyrinx	/* COUNTER types. */
156216295Ssyrinx	struct snmp_mapping		cntlist;
157216295Ssyrinx	/* GAUGE types. */
158216295Ssyrinx	struct snmp_mapping		gaugelist;
159216295Ssyrinx	/* COUNTER64 types. */
160216295Ssyrinx	struct snmp_mapping		cnt64list;
161216295Ssyrinx	/* ENUM values for oid types. */
162216295Ssyrinx	struct snmp_mapping		enumlist;
163216295Ssyrinx	/* Description of all table entry types. */
164216295Ssyrinx	struct snmp_table_index		tablelist;
165216295Ssyrinx	/* Defined enumerated textual conventions. */
166216295Ssyrinx	struct snmp_enum_tc		tclist;
167216295Ssyrinx};
168216295Ssyrinx
169216295Ssyrinxstruct snmp_toolinfo {
170216295Ssyrinx	uint32_t					flags;
171216295Ssyrinx	/* Number of initially input OIDs. */
172216295Ssyrinx	int32_t						objects;
173216295Ssyrinx	/* List of all input OIDs. */
174216295Ssyrinx	SLIST_HEAD(snmp_objectlist, snmp_object)	snmp_objectlist;
175216295Ssyrinx	/* All known OID to string mapping data. */
176216295Ssyrinx	struct snmp_mappings				*mappings;
177216295Ssyrinx	/* A list of .defs filenames to search oid<->string mapping. */
178216295Ssyrinx	struct fname_list				filelist;
179216295Ssyrinx	/* SNMPv3 USM user credentials */
180216295Ssyrinx	char						*passwd;
181216295Ssyrinx};
182216295Ssyrinx
183216295Ssyrinx/* XXX we might want to get away with this and will need to touch
184216295Ssyrinx * XXX the MACROS then too */
185216295Ssyrinxextern struct snmp_toolinfo snmptool;
186216295Ssyrinx
187216295Ssyrinx/* Definitions for some flags' bits. */
188216295Ssyrinx#define	OUTPUT_BITS	0x00000003	/* bits 0-1 for output type */
189216295Ssyrinx#define	NUMERIC_BIT	0x00000004	/* bit 2 for numeric oids */
190228990Suqs#define	RETRY_BIT	0x00000008 	/* bit 3 for retry on error response */
191216295Ssyrinx#define	ERRIGNORE_BIT	0x00000010	/* bit 4 for skip sanity checking */
192216295Ssyrinx#define	ERRIGNORE_BIT	0x00000010	/* bit 4 for skip sanity checking */
193216295Ssyrinx#define	EDISCOVER_BIT	0x00000020	/* bit 5 for SNMP Engine Discovery */
194216295Ssyrinx#define	LOCALKEY_BIT	0x00000040	/* bit 6 for using localized key */
195228990Suqs		/*	0x00000080 */	/* bit 7 reserved */
196216295Ssyrinx#define	PDUTYPE_BITS	0x00000f00	/* bits 8-11 for pdu type */
197228990Suqs		/*	0x0000f000 */	/* bit 12-15 reserved */
198216295Ssyrinx#define	MAXREP_BITS	0x00ff0000	/* bits 16-23 for max-repetit. value */
199216295Ssyrinx#define	NONREP_BITS	0xff000000	/* bits 24-31 for non-repeaters value */
200216295Ssyrinx
201216295Ssyrinx#define	OUTPUT_SHORT		0x0
202216295Ssyrinx#define	OUTPUT_VERBOSE		0x1
203216295Ssyrinx#define	OUTPUT_TABULAR		0x2
204216295Ssyrinx#define	OUTPUT_QUIET		0x3
205216295Ssyrinx
206216295Ssyrinx/* Macros for playing with flags' bits. */
207216295Ssyrinx#define	SET_OUTPUT(ctx, type)	((ctx)->flags |= ((type) & OUTPUT_BITS))
208216295Ssyrinx#define	GET_OUTPUT(ctx)		((ctx)->flags & OUTPUT_BITS)
209216295Ssyrinx
210216295Ssyrinx#define	SET_NUMERIC(ctx)	((ctx)->flags |= NUMERIC_BIT)
211216295Ssyrinx#define	ISSET_NUMERIC(ctx)	((ctx)->flags & NUMERIC_BIT)
212216295Ssyrinx
213216295Ssyrinx#define	SET_RETRY(ctx)		((ctx)->flags |= RETRY_BIT)
214216295Ssyrinx#define	ISSET_RETRY(ctx)	((ctx)->flags & RETRY_BIT)
215216295Ssyrinx
216216295Ssyrinx#define	SET_ERRIGNORE(ctx)	((ctx)->flags |= ERRIGNORE_BIT)
217216295Ssyrinx#define	ISSET_ERRIGNORE(ctx)	((ctx)->flags & ERRIGNORE_BIT)
218216295Ssyrinx
219216295Ssyrinx#define	SET_EDISCOVER(ctx)	((ctx)->flags |= EDISCOVER_BIT)
220216295Ssyrinx#define	ISSET_EDISCOVER(ctx)	((ctx)->flags & EDISCOVER_BIT)
221216295Ssyrinx
222216295Ssyrinx#define	SET_LOCALKEY(ctx)	((ctx)->flags |= LOCALKEY_BIT)
223216295Ssyrinx#define	ISSET_LOCALKEY(ctx)	((ctx)->flags & LOCALKEY_BIT)
224216295Ssyrinx
225216295Ssyrinx#define	SET_PDUTYPE(ctx, type)	(((ctx)->flags |= (((type) & 0xf) << 8)))
226216295Ssyrinx#define	GET_PDUTYPE(ctx)	(((ctx)->flags & PDUTYPE_BITS) >> 8)
227216295Ssyrinx
228216295Ssyrinx#define	SET_MAXREP(ctx, i)	(((ctx)->flags |= (((i) & 0xff) << 16)))
229216295Ssyrinx#define	GET_MAXREP(ctx)		(((ctx)->flags & MAXREP_BITS) >> 16)
230216295Ssyrinx
231216295Ssyrinx#define	SET_NONREP(ctx, i)	(((ctx)->flags |= (((i) & 0xff) << 24)))
232216295Ssyrinx#define	GET_NONREP(ctx)		(((ctx)->flags & NONREP_BITS) >> 24)
233216295Ssyrinx
234216295Ssyrinx
235216295Ssyrinxextern const struct asn_oid IsoOrgDod_OID;
236216295Ssyrinx
237216295Ssyrinxint snmptool_init(struct snmp_toolinfo *);
238216295Ssyrinxint32_t snmp_import_file(struct snmp_toolinfo *, struct fname *);
239216295Ssyrinxint32_t snmp_import_all(struct snmp_toolinfo *);
240216295Ssyrinxint32_t add_filename(struct snmp_toolinfo *, const char *,
241216295Ssyrinx    const struct asn_oid *, int32_t);
242216295Ssyrinxvoid free_filelist(struct snmp_toolinfo *);
243216295Ssyrinxvoid snmp_tool_freeall(struct snmp_toolinfo *);
244216295Ssyrinxvoid snmp_import_dump(int);
245216295Ssyrinx
246216295Ssyrinx/* bsnmpmap.c */
247216295Ssyrinxstruct snmp_mappings *snmp_mapping_init(void);
248216295Ssyrinxint32_t snmp_mapping_free(struct snmp_toolinfo *);
249216295Ssyrinxvoid snmp_index_listfree(struct snmp_idxlist *);
250216295Ssyrinxvoid snmp_dump_oid2str(struct snmp_oid2str *);
251216295Ssyrinxint32_t snmp_node_insert(struct snmp_toolinfo *, struct snmp_oid2str *);
252216295Ssyrinxint32_t snmp_leaf_insert(struct snmp_toolinfo *, struct snmp_oid2str *);
253216295Ssyrinxint32_t snmp_enum_insert(struct snmp_toolinfo *, struct snmp_oid2str *);
254216295Ssyrinxstruct enum_pairs *enum_pairs_init(void);
255216295Ssyrinxvoid enum_pairs_free(struct enum_pairs *);
256216295Ssyrinxvoid snmp_mapping_entryfree(struct snmp_oid2str *);
257216295Ssyrinxint32_t enum_pair_insert(struct enum_pairs *, int32_t, char *);
258216295Ssyrinxchar *enum_string_lookup(struct enum_pairs *, int32_t);
259216295Ssyrinxint32_t enum_number_lookup(struct enum_pairs *, char *);
260216295Ssyrinxint32_t snmp_syntax_insert(struct snmp_idxlist *, struct enum_pairs *,
261216295Ssyrinx    enum snmp_syntax, enum snmp_tc);
262216295Ssyrinxint32_t snmp_table_insert(struct snmp_toolinfo *, struct snmp_index_entry *);
263216295Ssyrinx
264216295Ssyrinxstruct enum_type *snmp_enumtc_init(char *);
265216295Ssyrinxvoid snmp_enumtc_free(struct enum_type *);
266216295Ssyrinxvoid snmp_enumtc_insert(struct snmp_toolinfo *, struct enum_type *);
267216295Ssyrinxstruct enum_type *snmp_enumtc_lookup(struct snmp_toolinfo *, char *);
268216295Ssyrinx
269216295Ssyrinxvoid snmp_mapping_dump(struct snmp_toolinfo *);
270216295Ssyrinxint32_t snmp_lookup_leafstring(struct snmp_toolinfo *, struct snmp_object *);
271216295Ssyrinxint32_t snmp_lookup_enumstring(struct snmp_toolinfo *, struct snmp_object *);
272216295Ssyrinxint32_t snmp_lookup_oidstring(struct snmp_toolinfo *, struct snmp_object *);
273216295Ssyrinxint32_t snmp_lookup_nonleaf_string(struct snmp_toolinfo *, struct snmp_object *);
274216295Ssyrinxint32_t snmp_lookup_allstring(struct snmp_toolinfo *, struct snmp_object *);
275216295Ssyrinxint32_t snmp_lookup_nodestring(struct snmp_toolinfo *, struct snmp_object *);
276216295Ssyrinxint32_t snmp_lookup_oidall(struct snmp_toolinfo *, struct snmp_object *, char *);
277216295Ssyrinxint32_t snmp_lookup_enumoid(struct snmp_toolinfo *, struct snmp_object *, char *);
278216295Ssyrinxint32_t snmp_lookup_oid(struct snmp_toolinfo *, struct snmp_object *, char *);
279216295Ssyrinx
280216295Ssyrinx/* Functions parsing common options for all tools. */
281216295Ssyrinxint32_t parse_server(char *);
282216295Ssyrinxint32_t parse_timeout(char *);
283216295Ssyrinxint32_t parse_retry(char *);
284216295Ssyrinxint32_t parse_version(char *);
285216295Ssyrinxint32_t parse_local_path(char *);
286216295Ssyrinxint32_t parse_buflen(char *);
287216295Ssyrinxint32_t parse_debug(void);
288216295Ssyrinxint32_t parse_discovery(struct snmp_toolinfo *);
289216295Ssyrinxint32_t parse_local_key(struct snmp_toolinfo *);
290216295Ssyrinxint32_t parse_num_oids(struct snmp_toolinfo *);
291216295Ssyrinxint32_t parse_file(struct snmp_toolinfo *, char *);
292216295Ssyrinxint32_t parse_include(struct snmp_toolinfo *, char *);
293216295Ssyrinxint32_t parse_output(struct snmp_toolinfo *, char *);
294216295Ssyrinxint32_t parse_errors(struct snmp_toolinfo *);
295216295Ssyrinxint32_t parse_skip_access(struct snmp_toolinfo *);
296216295Ssyrinxint32_t parse_authentication(struct snmp_toolinfo *, char *);
297216295Ssyrinxint32_t parse_privacy(struct snmp_toolinfo *, char *);
298216295Ssyrinxint32_t parse_context(struct snmp_toolinfo *, char *);
299216295Ssyrinxint32_t parse_user_security(struct snmp_toolinfo *, char *);
300216295Ssyrinx
301216295Ssyrinxtypedef int32_t (*snmp_verify_inoid_f) (struct snmp_toolinfo *,
302216295Ssyrinx    struct snmp_object *, char *);
303216295Ssyrinxint32_t snmp_object_add(struct snmp_toolinfo *, snmp_verify_inoid_f, char *);
304216295Ssyrinxint32_t snmp_object_remove(struct snmp_toolinfo *, struct asn_oid *oid);
305216295Ssyrinxint32_t snmp_object_seterror(struct snmp_toolinfo *, struct snmp_value *,
306216295Ssyrinx    int32_t);
307216295Ssyrinx
308216295Ssyrinxenum snmp_syntax parse_syntax(char *);
309216295Ssyrinxchar *snmp_parse_suboid(char *, struct asn_oid *);
310216295Ssyrinxchar *snmp_parse_index(struct snmp_toolinfo *, char *, struct snmp_object *);
311216295Ssyrinxint32_t snmp_parse_numoid(char *, struct asn_oid *);
312216295Ssyrinxint32_t snmp_suboid_append(struct asn_oid *, asn_subid_t);
313216295Ssyrinxint32_t snmp_suboid_pop(struct asn_oid *);
314216295Ssyrinx
315216295Ssyrinxtypedef int32_t (*snmp_verify_vbind_f) (struct snmp_toolinfo *,
316216295Ssyrinx    struct snmp_pdu *, struct snmp_object *);
317216295Ssyrinxtypedef int32_t (*snmp_add_vbind_f) (struct snmp_pdu *, struct snmp_object *);
318216295Ssyrinxint32_t snmp_pdu_add_bindings(struct snmp_toolinfo *, snmp_verify_vbind_f,
319216295Ssyrinx    snmp_add_vbind_f, struct snmp_pdu *, int32_t);
320216295Ssyrinx
321216295Ssyrinxint32_t snmp_parse_get_resp(struct snmp_pdu *, struct snmp_pdu *);
322216295Ssyrinxint32_t snmp_parse_getbulk_resp(struct snmp_pdu *, struct snmp_pdu *);
323216295Ssyrinxint32_t snmp_parse_getnext_resp(struct snmp_pdu *, struct snmp_pdu *);
324216295Ssyrinxint32_t snmp_parse_resp(struct snmp_pdu *, struct snmp_pdu *);
325216295Ssyrinxint32_t snmp_output_numval(struct snmp_toolinfo *, struct snmp_value *,
326216295Ssyrinx    struct snmp_oid2str *);
327216295Ssyrinxvoid snmp_output_val(struct snmp_value *);
328229933Ssyrinxint32_t snmp_output_resp(struct snmp_toolinfo *, struct snmp_pdu *, struct asn_oid *);
329216295Ssyrinxvoid snmp_output_err_resp(struct snmp_toolinfo *, struct snmp_pdu *);
330216295Ssyrinxvoid snmp_output_engine(void);
331216295Ssyrinxvoid snmp_output_keys(void);
332216295Ssyrinx
333216295Ssyrinx#endif /* _BSNMP_TOOLS_H_ */
334