1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/*
* ucdDemoPublic.c
*/
#include <net-snmp/net-snmp-config.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#if TIME_WITH_SYS_TIME
# ifdef WIN32
# include <sys/timeb.h>
# else
# include <sys/time.h>
# endif
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#if HAVE_WINSOCK_H
#include <winsock.h>
#endif
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "util_funcs.h"
#include "ucdDemoPublic.h"
#define MYMAX 1024
#define MAXUSERS 10
int num = 0;
static char demoUsers[MAXUSERS][MYMAX + 1];
static char demopass[MYMAX + 1];
void
ucdDemo_parse_user(const char *word, char *line)
{
if (num == MAXUSERS)
return;
if (strlen(line) > MYMAX)
return;
strcpy(demoUsers[num++], line);
}
void
ucdDemo_parse_userpass(const char *word, char *line)
{
if (strlen(line) > MYMAX)
return;
strcpy(demopass, line);
}
/*
* this variable defines function callbacks and type return information
* for the ucdDemoPublic mib
*/
struct variable2 ucdDemoPublic_variables[] = {
{UCDDEMORESETKEYS, ASN_INTEGER, RWRITE, var_ucdDemoPublic, 1, {1}},
{UCDDEMOPUBLICSTRING, ASN_OCTET_STR, RWRITE, var_ucdDemoPublic, 1,
{2}},
{UCDDEMOUSERLIST, ASN_OCTET_STR, RWRITE, var_ucdDemoPublic, 1, {3}},
{UCDDEMOPASSPHRASE, ASN_OCTET_STR, RWRITE, var_ucdDemoPublic, 1, {4}},
};
/*
* Define the OID pointer to the top of the mib tree that we're
* registering underneath
*/
oid ucdDemoPublic_variables_oid[] =
{ 1, 3, 6, 1, 4, 1, 2021, 14, 1, 1 };
void
init_ucdDemoPublic(void)
{
REGISTER_MIB("examples/ucdDemoPublic", ucdDemoPublic_variables,
variable2, ucdDemoPublic_variables_oid);
snmpd_register_config_handler("demoUser",
ucdDemo_parse_user, NULL, "USER");
snmpd_register_config_handler("demoPass",
ucdDemo_parse_userpass, NULL,
"PASSPHASE");
}
unsigned char publicString[MYMAX + 1];
unsigned char *
var_ucdDemoPublic(struct variable *vp,
oid * name,
size_t * length,
int exact, size_t * var_len, WriteMethod ** write_method)
{
static long long_ret;
static char string[MYMAX + 1], *cp;
int i;
*write_method = 0; /* assume it isnt writable for the time being */
*var_len = sizeof(long_ret); /* assume an integer and change later if not */
if (header_generic(vp, name, length, exact, var_len, write_method))
return 0;
/*
* this is where we do the value assignments for the mib results.
*/
switch (vp->magic) {
case UCDDEMORESETKEYS:
*write_method = write_ucdDemoResetKeys;
long_ret = 0;
return (unsigned char *) &long_ret;
case UCDDEMOPUBLICSTRING:
*write_method = write_ucdDemoPublicString;
*var_len = strlen(publicString);
return (unsigned char *) publicString;
case UCDDEMOUSERLIST:
cp = string;
for (i = 0; i < num; i++) {
snprintf(cp, sizeof(string)-strlen(string), " %s", demoUsers[i]);
string[MYMAX] = 0;
cp = cp + strlen(cp);
}
*var_len = strlen(string);
return (unsigned char *) string;
case UCDDEMOPASSPHRASE:
*var_len = strlen(demopass);
return (unsigned char *) demopass;
default:
DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_ucdDemoPublic\n",
vp->magic));
}
return 0;
}
int
write_ucdDemoResetKeys(int action,
u_char * var_val,
u_char var_val_type,
size_t var_val_len,
u_char * statP, oid * name, size_t name_len)
{
/*
* variables we may use later
*/
static long long_ret;
unsigned char *engineID;
size_t engineIDLen;
int i;
struct usmUser *user;
if (var_val_type != ASN_INTEGER) {
DEBUGMSGTL(("ucdDemoPublic",
"write to ucdDemoResetKeys not ASN_INTEGER\n"));
return SNMP_ERR_WRONGTYPE;
}
if (var_val_len > sizeof(long_ret)) {
DEBUGMSGTL(("ucdDemoPublic",
"write to ucdDemoResetKeys: bad length\n"));
return SNMP_ERR_WRONGLENGTH;
}
if (action == COMMIT) {
long_ret = *((long *) var_val);
if (long_ret == 1) {
engineID = snmpv3_generate_engineID(&engineIDLen);
for (i = 0; i < num; i++) {
user = usm_get_user(engineID, engineIDLen, demoUsers[i]);
if (user) {
usm_set_user_password(user, "userSetAuthPass",
demopass);
usm_set_user_password(user, "userSetPrivPass",
demopass);
}
}
/*
* reset the keys
*/
}
}
return SNMP_ERR_NOERROR;
}
int
write_ucdDemoPublicString(int action,
u_char * var_val,
u_char var_val_type,
size_t var_val_len,
u_char * statP, oid * name, size_t name_len)
{
if (var_val_type != ASN_OCTET_STR) {
DEBUGMSGTL(("ucdDemoPublic",
"write to ucdDemoPublicString not ASN_OCTET_STR\n"));
return SNMP_ERR_WRONGTYPE;
}
if (var_val_len > MYMAX) {
DEBUGMSGTL(("ucdDemoPublic",
"write to ucdDemoPublicString: bad length\n"));
return SNMP_ERR_WRONGLENGTH;
}
if (action == COMMIT) {
if (var_val_len != 0) {
strncpy(publicString, var_val, sizeof(publicString)-1);
/* some sanity checks */
if (strlen(var_val) > sizeof(publicString)-1 ||
strlen(var_val) != var_val_len)
publicString[sizeof(publicString)-1] = '\0';
else
publicString[var_val_len] = '\0';
} else
publicString[0] = '\0';
}
return SNMP_ERR_NOERROR;
}
|