1 /*
2  * ProFTPD - mod_snmp SMI routines
3  * Copyright (c) 2008-2016 TJ Saunders
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  *
19  * As a special exemption, TJ Saunders and other respective copyright holders
20  * give permission to link this program with OpenSSL, and distribute the
21  * resulting executable, without including the source code for OpenSSL in the
22  * source distribution.
23  */
24 
25 #ifndef MOD_SNMP_SMI_H
26 #define MOD_SNMP_SMI_H
27 
28 #include "mod_snmp.h"
29 #include "asn1.h"
30 
31 /* RFC1902 Structure of Management Information (SMI) for SNMPv2 */
32 #define SNMP_SMI_INTEGER	SNMP_ASN1_TYPE_INTEGER
33 #define SNMP_SMI_STRING		SNMP_ASN1_TYPE_OCTETSTRING
34 #define SNMP_SMI_OID		SNMP_ASN1_TYPE_OID
35 #define SNMP_SMI_NULL		SNMP_ASN1_TYPE_NULL
36 
37 /* OCTET_STRING, network byte order */
38 #define SNMP_SMI_IPADDR		(SNMP_ASN1_CLASS_APPLICATION|0)
39 
40 /* INTEGER */
41 #define SNMP_SMI_COUNTER32	(SNMP_ASN1_CLASS_APPLICATION|1)
42 
43 /* INTEGER */
44 #define SNMP_SMI_GAUGE32	(SNMP_ASN1_CLASS_APPLICATION|2)
45 
46 /* INTEGER */
47 #define SNMP_SMI_TIMETICKS	(SNMP_ASN1_CLASS_APPLICATION|3)
48 
49 /* OCTET_STRING */
50 #define SNMP_SMI_OPAQUE		(SNMP_ASN1_CLASS_APPLICATION|4)
51 
52 /* INTEGER */
53 #define SNMP_SMI_COUNTER64	(SNMP_ASN1_CLASS_APPLICATION|6)
54 
55 #define SNMP_SMI_NO_SUCH_OBJECT \
56   (SNMP_ASN1_CLASS_CONTEXT|SNMP_ASN1_PRIMITIVE|0x0)
57 
58 #define SNMP_SMI_NO_SUCH_INSTANCE \
59   (SNMP_ASN1_CLASS_CONTEXT|SNMP_ASN1_PRIMITIVE|0x1)
60 
61 #define SNMP_SMI_END_OF_MIB_VIEW \
62   (SNMP_ASN1_CLASS_CONTEXT|SNMP_ASN1_PRIMITIVE|0x2)
63 
64 /* Maximum length/number of sub-identifiers in an OID that we will accept. */
65 #define SNMP_SMI_MAX_NAMELEN	64
66 
67 struct snmp_var {
68   pool *pool;
69 
70   struct snmp_var *next;
71 
72   /* OID identifier of this variable */
73   oid_t *name;
74   unsigned int namelen;
75 
76   /* SMI/ASN.1 type of this variable */
77   unsigned char smi_type;
78 
79   union {
80     long *integer;
81     char *string;
82     oid_t *oid;
83   } value;
84 
85   unsigned int valuelen;
86 };
87 
88 const char *snmp_smi_get_varstr(pool *p, unsigned char var_type);
89 
90 struct snmp_var *snmp_smi_alloc_var(pool *p, oid_t *name, unsigned int namelen);
91 struct snmp_var *snmp_smi_create_var(pool *p, oid_t *name,
92   unsigned int namelen, unsigned char smi_type, int32_t int_value,
93   char *str_value, size_t str_valuelen);
94 struct snmp_var *snmp_smi_create_int(pool *p, oid_t *name, unsigned int namelen,
95   unsigned char smi_type, int32_t value);
96 struct snmp_var *snmp_smi_create_string(pool *p, oid_t *name,
97   unsigned int namelen, unsigned char smi_type, char *value, size_t valuelen);
98 struct snmp_var *snmp_smi_create_oid(pool *p, oid_t *name,
99   unsigned int namelen, unsigned char smi_type, oid_t *value,
100   unsigned int valuelen);
101 struct snmp_var *snmp_smi_create_exception(pool *p, oid_t *name,
102   unsigned int namelen, unsigned char smi_type);
103 struct snmp_var *snmp_smi_dup_var(pool *p, struct snmp_var *var);
104 
105 int snmp_smi_read_vars(pool *p, unsigned char **buf, size_t *buflen,
106     struct snmp_var **varlist, int snmp_version);
107 int snmp_smi_write_vars(pool *p, unsigned char **buf, size_t *buflen,
108     struct snmp_var *varlist, int snmp_version);
109 
110 unsigned int snmp_smi_util_add_list_var(struct snmp_var **head,
111   struct snmp_var **tail, struct snmp_var *var);
112 
113 #endif /* MOD_SNMP_SMI_H */
114