Debian Codegraph @

agent/mibgroup/utilities/iquery.c

raw
1 /*
2 * Portions of this file are copyrighted by:
3 * Copyright (c) 2016 VMware, Inc. All rights reserved.
4 * Use is subject to license terms specified in the COPYING file
5 * distributed with the Net-SNMP package.
6 */
7 #include <net-snmp/net-snmp-config.h>
8 #include <net-snmp/net-snmp-features.h>
9 #include <net-snmp/net-snmp-includes.h>
10 #include <net-snmp/agent/net-snmp-agent-includes.h>
11
12 #include "agent_global_vars.h"
13 #include "agentx/subagent.h"
14 #include "utilities/iquery.h"
15
16 netsnmp_feature_child_of(iquery_all, libnetsnmpmibs);
17 netsnmp_feature_child_of(iquery, iquery_all);
18 netsnmp_feature_child_of(iquery_community_session, iquery_all);
19 netsnmp_feature_child_of(iquery_pdu_session, iquery_all);
20
21 netsnmp_feature_require(query_set_default_session);
22
23 #ifndef NETSNMP_FEATURE_REMOVE_IQUERY
24
25 void
26 netsnmp_parse_iquerySecLevel(const char *token, char *line)
27 {
28 int secLevel;
29
30 #ifndef NETSNMP_FEATURE_REMOVE_RUNTIME_DISABLE_VERSION
31 if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
32 NETSNMP_DS_LIB_DISABLE_V3)) {
33 netsnmp_config_error("SNMPv3 disabled");
34 } else
35 #endif
36 if ((secLevel = parse_secLevel_conf( token, line )) >= 0 ) {
37 netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
38 NETSNMP_DS_AGENT_INTERNAL_SECLEVEL, secLevel);
39 } else {
40 netsnmp_config_error("Unknown security level: %s", line);
41 }
42 }
43
44 void
45 netsnmp_parse_iqueryVersion(const char *token, char *line)
46 {
47 #ifndef NETSNMP_DISABLE_SNMPV1
48 if (!strcmp( line, "1" )
49 #ifndef NETSNMP_FEATURE_REMOVE_RUNTIME_DISABLE_VERSION
50 && !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
51 NETSNMP_DS_LIB_DISABLE_V1)
52 #endif
53 )
54 netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
55 NETSNMP_DS_AGENT_INTERNAL_VERSION, SNMP_VERSION_1);
56 else
57 #endif
58 #ifndef NETSNMP_DISABLE_SNMPV2C
59 if ((!strcmp( line, "2" ) || !strcasecmp( line, "2c" ))
60 #ifndef NETSNMP_FEATURE_REMOVE_RUNTIME_DISABLE_VERSION
61 && !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
62 NETSNMP_DS_LIB_DISABLE_V2c)
63 #endif
64 )
65 netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
66 NETSNMP_DS_AGENT_INTERNAL_VERSION, SNMP_VERSION_2c);
67 else
68 #endif
69 if (!strcmp( line, "3" )
70 #ifndef NETSNMP_FEATURE_REMOVE_RUNTIME_DISABLE_VERSION
71 && !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
72 NETSNMP_DS_LIB_DISABLE_V3)
73 #endif
74 )
75 netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
76 NETSNMP_DS_AGENT_INTERNAL_VERSION, SNMP_VERSION_3);
77 else {
78 netsnmp_config_error("Unknown/disabled version: %s", line);
79 }
80 }
81
82 /*
83 * Set up a default session for running internal queries.
84 * This needs to be done before the config files are read,
85 * so that it is available for "monitor" directives...
86 */
87 int
88 _init_default_iquery_session( int majorID, int minorID,
89 void *serverargs, void *clientarg)
90 {
91 char *secName = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
92 NETSNMP_DS_AGENT_INTERNAL_SECNAME);
93 if (secName)
94 netsnmp_query_set_default_session(
95 netsnmp_iquery_user_session(secName));
96 return SNMPERR_SUCCESS;
97 }
98
99 /*
100 * ... Unfortunately, the internal engine ID is not set up
101 * until later, so this default session is incomplete.
102 * The resulting engineID probe runs into problems,
103 * causing the very first internal query to time out.
104 * Updating the default session with the internal engineID
105 * once it has been set, fixes this problem.
106 */
107 int
108 _tweak_default_iquery_session( int majorID, int minorID,
109 void *serverargs, void *clientarg)
110 {
111 u_char eID[SNMP_MAXBUF_SMALL];
112 size_t elen;
113 netsnmp_session *s = netsnmp_query_get_default_session_unchecked();
114
115 if ( s && s->securityEngineIDLen == 0 ) {
116 elen = snmpv3_get_engineID(eID, sizeof(eID));
117 s->securityEngineID = netsnmp_memdup(eID, elen);
118 s->securityEngineIDLen = elen;
119 }
120 return SNMPERR_SUCCESS;
121 }
122
123 void init_iquery(void){
124 char *type = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
125 NETSNMP_DS_LIB_APPTYPE);
126 netsnmp_ds_register_premib(ASN_OCTET_STR, type, "agentSecName",
127 NETSNMP_DS_APPLICATION_ID,
128 NETSNMP_DS_AGENT_INTERNAL_SECNAME);
129 netsnmp_ds_register_premib(ASN_OCTET_STR, type, "iquerySecName",
130 NETSNMP_DS_APPLICATION_ID,
131 NETSNMP_DS_AGENT_INTERNAL_SECNAME);
132
133 snmpd_register_config_handler("iqueryVersion",
134 netsnmp_parse_iqueryVersion, NULL,
135 "1 | 2c | 3");
136 snmpd_register_config_handler("iquerySecLevel",
137 netsnmp_parse_iquerySecLevel, NULL,
138 "noAuthNoPriv | authNoPriv | authPriv");
139
140 /*
141 * Set defaults
142 */
143 netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
144 NETSNMP_DS_AGENT_INTERNAL_VERSION, SNMP_VERSION_3);
145 netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
146 NETSNMP_DS_AGENT_INTERNAL_SECLEVEL, SNMP_SEC_LEVEL_AUTHNOPRIV);
147
148 snmp_register_callback(SNMP_CALLBACK_LIBRARY,
149 SNMP_CALLBACK_POST_PREMIB_READ_CONFIG,
150 _init_default_iquery_session, NULL);
151 snmp_register_callback(SNMP_CALLBACK_LIBRARY,
152 SNMP_CALLBACK_POST_READ_CONFIG,
153 _tweak_default_iquery_session, NULL);
154 }
155
156 /**************************
157 *
158 * APIs to construct an "internal query" session
159 *
160 **************************/
161
162 #ifndef NETSNMP_FEATURE_REMOVE_IQUERY_PDU_SESSION
163 netsnmp_session *netsnmp_iquery_pdu_session(netsnmp_pdu* pdu) {
164 if (!pdu || NETSNMP_RUNTIME_PROTOCOL_SKIP(pdu->version))
165 return NULL;
166 if (pdu->version == SNMP_VERSION_3)
167 return netsnmp_iquery_session( pdu->securityName,
168 pdu->version,
169 pdu->securityModel,
170 pdu->securityLevel,
171 pdu->securityEngineID,
172 pdu->securityEngineIDLen);
173 else
174 return netsnmp_iquery_session((char *) pdu->community,
175
176 pdu->version,
177 pdu->version+1,
178 SNMP_SEC_LEVEL_NOAUTH,
179 pdu->securityEngineID,
180 pdu->securityEngineIDLen);
181 }
182 #endif /* NETSNMP_FEATURE_REMOVE_IQUERY_PDU_SESSION */
183
184 netsnmp_session *netsnmp_iquery_user_session(char* secName){
185 u_char eID[SNMP_MAXBUF_SMALL];
186 size_t elen = snmpv3_get_engineID(eID, sizeof(eID));
187
188 return netsnmp_iquery_session( secName,
189 SNMP_VERSION_3,
190 SNMP_SEC_MODEL_USM,
191 SNMP_SEC_LEVEL_AUTHNOPRIV, eID, elen);
192 }
193
194 #ifndef NETSNMP_FEATURE_REMOVE_IQUERY_COMMUNITY_SESSION
195 netsnmp_session *netsnmp_iquery_community_session( char* community, int version ) {
196 u_char eID[SNMP_MAXBUF_SMALL];
197 size_t elen = snmpv3_get_engineID(eID, sizeof(eID));
198
199 return netsnmp_iquery_session( community, version, version+1,
200 SNMP_SEC_LEVEL_NOAUTH, eID, elen);
201 }
202 #endif /* NETSNMP_FEATURE_REMOVE_IQUERY_COMMUNITY_SESSION */
203
204 netsnmp_session *netsnmp_iquery_session(char* secName, int version,
205 int secModel, int secLevel,
206 u_char* engineID, size_t engIDLen) {
207
208 /*
209 * This routine creates a completely new session every time.
210 * It might be worth keeping track of which 'secNames' already
211 * have iquery sessions created, and re-using the appropriate one.
212 */
213 netsnmp_session *ss = NULL;
214
215 NETSNMP_RUNTIME_PROTOCOL_CHECK(version, unsupported_version);
216
217 #ifdef NETSNMP_TRANSPORT_CALLBACK_DOMAIN
218 ss = netsnmp_callback_open( callback_master_num, NULL, NULL, NULL);
219 if (ss) {
220 ss->version = version;
221 ss->securityModel = secModel;
222 ss->securityLevel = secLevel;
223 ss->securityEngineID = netsnmp_memdup(engineID, engIDLen);
224 ss->securityEngineIDLen = engIDLen;
225 if ( version == SNMP_VERSION_3 ) {
226 ss->securityNameLen = strlen(secName);
227 ss->securityName = netsnmp_memdup(secName, ss->securityNameLen + 1);
228 } else {
229 ss->community = netsnmp_memdup(secName, strlen(secName));
230 ss->community_len = strlen(secName);
231 }
232 ss->myvoid = netsnmp_check_outstanding_agent_requests;
233 ss->flags |= SNMP_FLAGS_RESP_CALLBACK | SNMP_FLAGS_DONT_PROBE;
234 }
235 #endif
236
237 unsupported_version:
238 return ss;
239 }
240
241 #else /* NETSNMP_FEATURE_REMOVE_IQUERY */
242 netsnmp_feature_unused(iquery);
243 #endif /* NETSNMP_FEATURE_REMOVE_IQUERY */
244