1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// ./lib/current/src/snmpv3.c
|
4 |
|
|
//
|
5 |
|
|
//
|
6 |
|
|
//==========================================================================
|
7 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
8 |
|
|
// -------------------------------------------
|
9 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
10 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
11 |
|
|
//
|
12 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
13 |
|
|
// the terms of the GNU General Public License as published by the Free
|
14 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
15 |
|
|
//
|
16 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
17 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
18 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
19 |
|
|
// for more details.
|
20 |
|
|
//
|
21 |
|
|
// You should have received a copy of the GNU General Public License along
|
22 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
23 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
24 |
|
|
//
|
25 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
26 |
|
|
// or inline functions from this file, or you compile this file and link it
|
27 |
|
|
// with other works to produce a work based on this file, this file does not
|
28 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
29 |
|
|
// License. However the source code for this file must still be made available
|
30 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
31 |
|
|
//
|
32 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
33 |
|
|
// this file might be covered by the GNU General Public License.
|
34 |
|
|
//
|
35 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
36 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//####UCDSNMPCOPYRIGHTBEGIN####
|
40 |
|
|
//
|
41 |
|
|
// -------------------------------------------
|
42 |
|
|
//
|
43 |
|
|
// Portions of this software may have been derived from the UCD-SNMP
|
44 |
|
|
// project, <http://ucd-snmp.ucdavis.edu/> from the University of
|
45 |
|
|
// California at Davis, which was originally based on the Carnegie Mellon
|
46 |
|
|
// University SNMP implementation. Portions of this software are therefore
|
47 |
|
|
// covered by the appropriate copyright disclaimers included herein.
|
48 |
|
|
//
|
49 |
|
|
// The release used was version 4.1.2 of May 2000. "ucd-snmp-4.1.2"
|
50 |
|
|
// -------------------------------------------
|
51 |
|
|
//
|
52 |
|
|
//####UCDSNMPCOPYRIGHTEND####
|
53 |
|
|
//==========================================================================
|
54 |
|
|
//#####DESCRIPTIONBEGIN####
|
55 |
|
|
//
|
56 |
|
|
// Author(s): hmt
|
57 |
|
|
// Contributors: hmt
|
58 |
|
|
// Date: 2000-05-30
|
59 |
|
|
// Purpose: Port of UCD-SNMP distribution to eCos.
|
60 |
|
|
// Description:
|
61 |
|
|
//
|
62 |
|
|
//
|
63 |
|
|
//####DESCRIPTIONEND####
|
64 |
|
|
//
|
65 |
|
|
//==========================================================================
|
66 |
|
|
/********************************************************************
|
67 |
|
|
Copyright 1989, 1991, 1992 by Carnegie Mellon University
|
68 |
|
|
|
69 |
|
|
Derivative Work -
|
70 |
|
|
Copyright 1996, 1998, 1999, 2000 The Regents of the University of California
|
71 |
|
|
|
72 |
|
|
All Rights Reserved
|
73 |
|
|
|
74 |
|
|
Permission to use, copy, modify and distribute this software and its
|
75 |
|
|
documentation for any purpose and without fee is hereby granted,
|
76 |
|
|
provided that the above copyright notice appears in all copies and
|
77 |
|
|
that both that copyright notice and this permission notice appear in
|
78 |
|
|
supporting documentation, and that the name of CMU and The Regents of
|
79 |
|
|
the University of California not be used in advertising or publicity
|
80 |
|
|
pertaining to distribution of the software without specific written
|
81 |
|
|
permission.
|
82 |
|
|
|
83 |
|
|
CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALL
|
84 |
|
|
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
|
85 |
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL CMU OR
|
86 |
|
|
THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,
|
87 |
|
|
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
88 |
|
|
FROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
|
89 |
|
|
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
90 |
|
|
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
91 |
|
|
*********************************************************************/
|
92 |
|
|
/*
|
93 |
|
|
* snmpv3.c
|
94 |
|
|
*/
|
95 |
|
|
|
96 |
|
|
#include <config.h>
|
97 |
|
|
|
98 |
|
|
#ifdef CYGPKG_SNMPAGENT_V3_SUPPORT
|
99 |
|
|
#include <stdio.h>
|
100 |
|
|
#include <sys/types.h>
|
101 |
|
|
#if TIME_WITH_SYS_TIME
|
102 |
|
|
# ifdef WIN32
|
103 |
|
|
# include <sys/timeb.h>
|
104 |
|
|
# else
|
105 |
|
|
# include <sys/time.h>
|
106 |
|
|
# endif
|
107 |
|
|
# include <time.h>
|
108 |
|
|
#else
|
109 |
|
|
# if HAVE_SYS_TIME_H
|
110 |
|
|
# include <sys/time.h>
|
111 |
|
|
# else
|
112 |
|
|
# include <time.h>
|
113 |
|
|
# endif
|
114 |
|
|
#endif
|
115 |
|
|
#if HAVE_STRING_H
|
116 |
|
|
#include <string.h>
|
117 |
|
|
#else
|
118 |
|
|
#include <strings.h>
|
119 |
|
|
#endif
|
120 |
|
|
#if HAVE_NETINET_IN_H
|
121 |
|
|
#include <netinet/in.h>
|
122 |
|
|
#endif
|
123 |
|
|
#if HAVE_UNISTD_H
|
124 |
|
|
#include <unistd.h>
|
125 |
|
|
#endif
|
126 |
|
|
#if HAVE_WINSOCK_H
|
127 |
|
|
#include <winsock.h>
|
128 |
|
|
#endif
|
129 |
|
|
#if HAVE_SYS_SOCKET_H
|
130 |
|
|
#include <sys/socket.h>
|
131 |
|
|
#endif
|
132 |
|
|
#if HAVE_NETDB_H
|
133 |
|
|
#include <netdb.h>
|
134 |
|
|
#endif
|
135 |
|
|
#if HAVE_STDLIB_H
|
136 |
|
|
# include <stdlib.h>
|
137 |
|
|
#endif
|
138 |
|
|
|
139 |
|
|
#if HAVE_DMALLOC_H
|
140 |
|
|
#include <dmalloc.h>
|
141 |
|
|
#endif
|
142 |
|
|
|
143 |
|
|
#include "system.h"
|
144 |
|
|
#include "asn1.h"
|
145 |
|
|
#include "snmpv3.h"
|
146 |
|
|
#include "callback.h"
|
147 |
|
|
#include "snmpusm.h"
|
148 |
|
|
#include "snmp.h"
|
149 |
|
|
#include "snmp_api.h"
|
150 |
|
|
#include "snmp_impl.h"
|
151 |
|
|
#include "read_config.h"
|
152 |
|
|
#include "lcd_time.h"
|
153 |
|
|
#include "scapi.h"
|
154 |
|
|
#include "tools.h"
|
155 |
|
|
#include "keytools.h"
|
156 |
|
|
#include "lcd_time.h"
|
157 |
|
|
#include "snmp_debug.h"
|
158 |
|
|
#include "snmp_logging.h"
|
159 |
|
|
#include "default_store.h"
|
160 |
|
|
|
161 |
|
|
#include "transform_oids.h"
|
162 |
|
|
|
163 |
|
|
static u_long engineBoots = 1;
|
164 |
|
|
static unsigned char *engineID = NULL;
|
165 |
|
|
static size_t engineIDLength = 0;
|
166 |
|
|
static unsigned char *oldEngineID = NULL;
|
167 |
|
|
static size_t oldEngineIDLength = 0;
|
168 |
|
|
static struct timeval snmpv3starttime;
|
169 |
|
|
|
170 |
|
|
/*
|
171 |
|
|
* Set up default snmpv3 parameter value storage.
|
172 |
|
|
*/
|
173 |
|
|
static oid *defaultAuthType = NULL;
|
174 |
|
|
static size_t defaultAuthTypeLen = 0;
|
175 |
|
|
static oid *defaultPrivType = NULL;
|
176 |
|
|
static size_t defaultPrivTypeLen = 0;
|
177 |
|
|
|
178 |
|
|
void
|
179 |
|
|
snmpv3_authtype_conf(const char *word, char *cptr)
|
180 |
|
|
{
|
181 |
|
|
if (strcasecmp(cptr,"MD5") == 0)
|
182 |
|
|
defaultAuthType = usmHMACMD5AuthProtocol;
|
183 |
|
|
else if (strcasecmp(cptr,"SHA") == 0)
|
184 |
|
|
defaultAuthType = usmHMACMD5AuthProtocol;
|
185 |
|
|
else
|
186 |
|
|
config_perror("Unknown authentication type");
|
187 |
|
|
defaultAuthTypeLen = USM_LENGTH_OID_TRANSFORM;
|
188 |
|
|
DEBUGMSGTL(("snmpv3","set default authentication type: %s\n", cptr));
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
oid *
|
192 |
|
|
get_default_authtype(size_t *len)
|
193 |
|
|
{
|
194 |
|
|
if (defaultAuthType == NULL) {
|
195 |
|
|
defaultAuthType = SNMP_DEFAULT_AUTH_PROTO;
|
196 |
|
|
defaultAuthTypeLen = SNMP_DEFAULT_AUTH_PROTOLEN;
|
197 |
|
|
}
|
198 |
|
|
if (len)
|
199 |
|
|
*len = defaultAuthTypeLen;
|
200 |
|
|
return defaultAuthType;
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
void
|
204 |
|
|
snmpv3_privtype_conf(const char *word, char *cptr)
|
205 |
|
|
{
|
206 |
|
|
if (strcasecmp(cptr,"DES") == 0)
|
207 |
|
|
defaultPrivType = SNMP_DEFAULT_PRIV_PROTO;
|
208 |
|
|
else
|
209 |
|
|
config_perror("Unknown privacy type");
|
210 |
|
|
defaultPrivTypeLen = SNMP_DEFAULT_PRIV_PROTOLEN;
|
211 |
|
|
DEBUGMSGTL(("snmpv3","set default privacy type: %s\n", cptr));
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
oid *
|
215 |
|
|
get_default_privtype(size_t *len)
|
216 |
|
|
{
|
217 |
|
|
if (defaultAuthType == NULL) {
|
218 |
|
|
defaultAuthType = usmDESPrivProtocol;
|
219 |
|
|
defaultPrivTypeLen = USM_LENGTH_OID_TRANSFORM;
|
220 |
|
|
}
|
221 |
|
|
if (len)
|
222 |
|
|
*len = defaultPrivTypeLen;
|
223 |
|
|
return defaultPrivType;
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
/*******************************************************************-o-******
|
227 |
|
|
* snmpv3_secLevel_conf
|
228 |
|
|
*
|
229 |
|
|
* Parameters:
|
230 |
|
|
* *word
|
231 |
|
|
* *cptr
|
232 |
|
|
*
|
233 |
|
|
* Line syntax:
|
234 |
|
|
* defSecurityLevel "noAuthNoPriv" | "authNoPriv" | "authPriv"
|
235 |
|
|
*/
|
236 |
|
|
void
|
237 |
|
|
snmpv3_secLevel_conf(const char *word, char *cptr)
|
238 |
|
|
{
|
239 |
|
|
char buf[1024];
|
240 |
|
|
|
241 |
|
|
if (strcasecmp(cptr,"noAuthNoPriv") == 0 || strcmp(cptr, "1") == 0
|
242 |
|
|
|| strcasecmp(cptr, "nanp") == 0)
|
243 |
|
|
ds_set_int(DS_LIBRARY_ID, DS_LIB_SECLEVEL, SNMP_SEC_LEVEL_NOAUTH);
|
244 |
|
|
else if (strcasecmp(cptr,"authNoPriv") == 0 || strcmp(cptr, "2") == 0
|
245 |
|
|
|| strcasecmp(cptr, "anp") == 0)
|
246 |
|
|
ds_set_int(DS_LIBRARY_ID, DS_LIB_SECLEVEL, SNMP_SEC_LEVEL_AUTHNOPRIV);
|
247 |
|
|
else if (strcasecmp(cptr,"authPriv") == 0 || strcmp(cptr, "3") == 0
|
248 |
|
|
|| strcasecmp(cptr, "ap") == 0)
|
249 |
|
|
ds_set_int(DS_LIBRARY_ID, DS_LIB_SECLEVEL, SNMP_SEC_LEVEL_AUTHPRIV);
|
250 |
|
|
else {
|
251 |
|
|
sprintf(buf,"Unknown security level: %s", cptr);
|
252 |
|
|
config_perror(buf);
|
253 |
|
|
}
|
254 |
|
|
DEBUGMSGTL(("snmpv3","default secLevel set to: %s = %d\n", cptr,
|
255 |
|
|
ds_get_int(DS_LIBRARY_ID, DS_LIB_SECLEVEL)));
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
/*******************************************************************-o-******
|
259 |
|
|
* setup_engineID
|
260 |
|
|
*
|
261 |
|
|
* Parameters:
|
262 |
|
|
* **eidp
|
263 |
|
|
* *text Printable (?) text to be plugged into the snmpEngineID.
|
264 |
|
|
*
|
265 |
|
|
* Return:
|
266 |
|
|
* Length of allocated engineID string in bytes, -OR-
|
267 |
|
|
* -1 on error.
|
268 |
|
|
*
|
269 |
|
|
*
|
270 |
|
|
* Create an snmpEngineID using text and the local IP address. If eidp
|
271 |
|
|
* is defined, use it to return a pointer to the newly allocated data.
|
272 |
|
|
* Otherwise, use the result to define engineID defined in this module.
|
273 |
|
|
*
|
274 |
|
|
* Line syntax:
|
275 |
|
|
* engineID <text> | NULL
|
276 |
|
|
*
|
277 |
|
|
* XXX What if a node has multiple interfaces?
|
278 |
|
|
* XXX What if multiple engines all choose the same address?
|
279 |
|
|
* (answer: You're screwed, because you might need a kul database
|
280 |
|
|
* which is dependant on the current engineID. Enumeration and other
|
281 |
|
|
* tricks won't work).
|
282 |
|
|
*/
|
283 |
|
|
int
|
284 |
|
|
setup_engineID(u_char **eidp, const char *text)
|
285 |
|
|
{
|
286 |
|
|
int enterpriseid = htonl(ENTERPRISE_NUMBER),
|
287 |
|
|
localsetup = (eidp) ? 0 : 1;
|
288 |
|
|
/* Use local engineID if *eidp == NULL. */
|
289 |
|
|
#ifdef HAVE_GETHOSTNAME
|
290 |
|
|
u_char buf[SNMP_MAXBUF_SMALL];
|
291 |
|
|
struct hostent *hent;
|
292 |
|
|
#endif
|
293 |
|
|
u_char *bufp = NULL;
|
294 |
|
|
size_t len;
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
/*
|
298 |
|
|
* Determine length of the engineID string.
|
299 |
|
|
*/
|
300 |
|
|
if (text) {
|
301 |
|
|
len = 5+strlen(text); /* 5 leading bytes+text. */
|
302 |
|
|
|
303 |
|
|
} else {
|
304 |
|
|
len = 5 + 4; /* 5 leading bytes + four byte IPv4 address */
|
305 |
|
|
#ifdef HAVE_GETHOSTNAME
|
306 |
|
|
gethostname((char *)buf, sizeof(buf));
|
307 |
|
|
hent = gethostbyname((char *)buf);
|
308 |
|
|
#ifdef AF_INET6
|
309 |
|
|
if (hent && hent->h_addrtype == AF_INET6)
|
310 |
|
|
len += 12; /* 16 bytes total for IPv6 address. */
|
311 |
|
|
#endif
|
312 |
|
|
#endif /* HAVE_GETHOSTNAME */
|
313 |
|
|
} /* endif -- text (1) */
|
314 |
|
|
|
315 |
|
|
|
316 |
|
|
/*
|
317 |
|
|
* Allocate memory and store enterprise ID.
|
318 |
|
|
*/
|
319 |
|
|
if ((bufp = (u_char *) malloc(len)) == NULL) {
|
320 |
|
|
snmp_log_perror("setup_engineID malloc");
|
321 |
|
|
return -1;
|
322 |
|
|
}
|
323 |
|
|
|
324 |
|
|
memcpy(bufp, &enterpriseid, sizeof(enterpriseid)); /* XXX Must be 4 bytes! */
|
325 |
|
|
bufp[0] |= 0x80;
|
326 |
|
|
|
327 |
|
|
|
328 |
|
|
/*
|
329 |
|
|
* Store the given text -OR- the first found IP address.
|
330 |
|
|
*/
|
331 |
|
|
if (text) {
|
332 |
|
|
bufp[4] = 4;
|
333 |
|
|
memcpy((char *)bufp+5, text, strlen(text));
|
334 |
|
|
|
335 |
|
|
} else {
|
336 |
|
|
bufp[4] = 1;
|
337 |
|
|
#ifdef HAVE_GETHOSTNAME
|
338 |
|
|
gethostname((char *)buf, sizeof(buf));
|
339 |
|
|
hent = gethostbyname((char *)buf);
|
340 |
|
|
|
341 |
|
|
if (hent && hent->h_addrtype == AF_INET) {
|
342 |
|
|
memcpy(bufp+5, hent->h_addr_list[0], hent->h_length);
|
343 |
|
|
|
344 |
|
|
#ifdef AF_INET6
|
345 |
|
|
} else if (hent && hent->h_addrtype == AF_INET6) {
|
346 |
|
|
bufp[4] = 2;
|
347 |
|
|
memcpy(bufp+5, hent->h_addr_list[0], hent->h_length);
|
348 |
|
|
#endif
|
349 |
|
|
|
350 |
|
|
} else { /* Unknown address type. Default to 127.0.0.1. */
|
351 |
|
|
|
352 |
|
|
bufp[5] = 127;
|
353 |
|
|
bufp[6] = 0;
|
354 |
|
|
bufp[7] = 0;
|
355 |
|
|
bufp[8] = 1;
|
356 |
|
|
}
|
357 |
|
|
#else /* HAVE_GETHOSTNAME */
|
358 |
|
|
/* Unknown address type. Default to 127.0.0.1. */
|
359 |
|
|
|
360 |
|
|
bufp[5] = 127;
|
361 |
|
|
bufp[6] = 0;
|
362 |
|
|
bufp[7] = 0;
|
363 |
|
|
bufp[8] = 1;
|
364 |
|
|
#endif /* HAVE_GETHOSTNAME */
|
365 |
|
|
|
366 |
|
|
} /* endif -- text (2) */
|
367 |
|
|
|
368 |
|
|
|
369 |
|
|
/*
|
370 |
|
|
* Pass the string back to the calling environment, or use it for
|
371 |
|
|
* our local engineID.
|
372 |
|
|
*/
|
373 |
|
|
if (localsetup) {
|
374 |
|
|
SNMP_FREE(engineID);
|
375 |
|
|
engineID = bufp;
|
376 |
|
|
engineIDLength = len;
|
377 |
|
|
|
378 |
|
|
} else {
|
379 |
|
|
*eidp = bufp;
|
380 |
|
|
}
|
381 |
|
|
|
382 |
|
|
|
383 |
|
|
return len;
|
384 |
|
|
|
385 |
|
|
} /* end setup_engineID() */
|
386 |
|
|
|
387 |
|
|
void
|
388 |
|
|
usm_parse_create_usmUser(const char *token, char *line) {
|
389 |
|
|
char *cp;
|
390 |
|
|
char buf[SNMP_MAXBUF_MEDIUM];
|
391 |
|
|
struct usmUser *newuser;
|
392 |
|
|
u_char userKey[SNMP_MAXBUF_SMALL];
|
393 |
|
|
size_t userKeyLen = SNMP_MAXBUF_SMALL;
|
394 |
|
|
int ret;
|
395 |
|
|
|
396 |
|
|
newuser = usm_create_user();
|
397 |
|
|
|
398 |
|
|
/* READ: Security Name */
|
399 |
|
|
cp = copy_word(line, buf);
|
400 |
|
|
newuser->secName = strdup(buf);
|
401 |
|
|
newuser->name = strdup(buf);
|
402 |
|
|
|
403 |
|
|
newuser->engineID = snmpv3_generate_engineID(&ret);
|
404 |
|
|
if ( ret < 0 ) {
|
405 |
|
|
usm_free_user(newuser);
|
406 |
|
|
return;
|
407 |
|
|
}
|
408 |
|
|
newuser->engineIDLen = ret;
|
409 |
|
|
|
410 |
|
|
if (!cp)
|
411 |
|
|
goto add; /* no authentication or privacy type */
|
412 |
|
|
|
413 |
|
|
/* READ: Authentication Type */
|
414 |
|
|
if (strncmp(cp, "MD5", 3) == 0) {
|
415 |
|
|
memcpy(newuser->authProtocol, usmHMACMD5AuthProtocol,
|
416 |
|
|
sizeof(usmHMACMD5AuthProtocol));
|
417 |
|
|
} else if (strncmp(cp, "SHA", 3) == 0) {
|
418 |
|
|
memcpy(newuser->authProtocol, usmHMACSHA1AuthProtocol,
|
419 |
|
|
sizeof(usmHMACSHA1AuthProtocol));
|
420 |
|
|
} else {
|
421 |
|
|
config_perror("Unknown authentication protocol");
|
422 |
|
|
usm_free_user(newuser);
|
423 |
|
|
return;
|
424 |
|
|
}
|
425 |
|
|
|
426 |
|
|
cp = skip_token(cp);
|
427 |
|
|
|
428 |
|
|
/* READ: Authentication Pass Phrase */
|
429 |
|
|
if (!cp) {
|
430 |
|
|
config_perror("no authentication pass phrase");
|
431 |
|
|
usm_free_user(newuser);
|
432 |
|
|
return;
|
433 |
|
|
}
|
434 |
|
|
cp = copy_word(cp, buf);
|
435 |
|
|
/* And turn it into a localized key */
|
436 |
|
|
ret = generate_Ku(newuser->authProtocol, newuser->authProtocolLen,
|
437 |
|
|
(u_char *)buf, strlen(buf),
|
438 |
|
|
userKey, &userKeyLen );
|
439 |
|
|
if (ret != SNMPERR_SUCCESS) {
|
440 |
|
|
config_perror("Error generating auth key from pass phrase.");
|
441 |
|
|
usm_free_user(newuser);
|
442 |
|
|
return;
|
443 |
|
|
}
|
444 |
|
|
newuser->authKeyLen =
|
445 |
|
|
sc_get_properlength(newuser->authProtocol, newuser->authProtocolLen);
|
446 |
|
|
newuser->authKey = (u_char *) malloc(newuser->authKeyLen);
|
447 |
|
|
ret = generate_kul(newuser->authProtocol, newuser->authProtocolLen,
|
448 |
|
|
newuser->engineID, newuser->engineIDLen,
|
449 |
|
|
userKey, userKeyLen,
|
450 |
|
|
newuser->authKey, &newuser->authKeyLen );
|
451 |
|
|
if (ret != SNMPERR_SUCCESS) {
|
452 |
|
|
config_perror("Error generating localized auth key (Kul) from Ku.");
|
453 |
|
|
usm_free_user(newuser);
|
454 |
|
|
return;
|
455 |
|
|
}
|
456 |
|
|
|
457 |
|
|
if (!cp)
|
458 |
|
|
goto add; /* no privacy type (which is legal) */
|
459 |
|
|
|
460 |
|
|
/* READ: Privacy Type */
|
461 |
|
|
if (strncmp(cp, "DES", 3) == 0) {
|
462 |
|
|
memcpy(newuser->privProtocol, usmDESPrivProtocol,
|
463 |
|
|
sizeof(usmDESPrivProtocol));
|
464 |
|
|
} else {
|
465 |
|
|
config_perror("Unknown privacy protocol");
|
466 |
|
|
usm_free_user(newuser);
|
467 |
|
|
return;
|
468 |
|
|
}
|
469 |
|
|
|
470 |
|
|
cp = skip_token(cp);
|
471 |
|
|
/* READ: Authentication Pass Phrase */
|
472 |
|
|
if (!cp) {
|
473 |
|
|
/* assume the same as the authentication key */
|
474 |
|
|
memdup(&newuser->privKey, newuser->authKey, newuser->authKeyLen);
|
475 |
|
|
} else {
|
476 |
|
|
cp = copy_word(cp, buf);
|
477 |
|
|
/* And turn it into a localized key */
|
478 |
|
|
ret = generate_Ku(newuser->authProtocol, newuser->authProtocolLen,
|
479 |
|
|
(u_char *)buf, strlen(buf),
|
480 |
|
|
userKey, &userKeyLen );
|
481 |
|
|
if (ret != SNMPERR_SUCCESS) {
|
482 |
|
|
config_perror("Error generating priv key from pass phrase.");
|
483 |
|
|
usm_free_user(newuser);
|
484 |
|
|
return;
|
485 |
|
|
}
|
486 |
|
|
|
487 |
|
|
ret = sc_get_properlength(newuser->authProtocol, newuser->authProtocolLen);
|
488 |
|
|
if (ret < 0) {
|
489 |
|
|
config_perror("Error getting proper key length for priv algorithm.");
|
490 |
|
|
usm_free_user(newuser);
|
491 |
|
|
return;
|
492 |
|
|
}
|
493 |
|
|
newuser->privKeyLen = ret;
|
494 |
|
|
|
495 |
|
|
newuser->privKey = (u_char *) malloc(newuser->privKeyLen);
|
496 |
|
|
ret = generate_kul(newuser->authProtocol, newuser->authProtocolLen,
|
497 |
|
|
newuser->engineID, newuser->engineIDLen,
|
498 |
|
|
userKey, userKeyLen,
|
499 |
|
|
newuser->privKey, &newuser->privKeyLen );
|
500 |
|
|
if (ret != SNMPERR_SUCCESS) {
|
501 |
|
|
config_perror("Error generating localized priv key (Kul) from Ku.");
|
502 |
|
|
usm_free_user(newuser);
|
503 |
|
|
return;
|
504 |
|
|
}
|
505 |
|
|
}
|
506 |
|
|
add:
|
507 |
|
|
usm_add_user(newuser);
|
508 |
|
|
DEBUGMSGTL(("usmUser","created a new user %s\n", newuser->secName));
|
509 |
|
|
}
|
510 |
|
|
|
511 |
|
|
/*******************************************************************-o-******
|
512 |
|
|
* engineBoots_conf
|
513 |
|
|
*
|
514 |
|
|
* Parameters:
|
515 |
|
|
* *word
|
516 |
|
|
* *cptr
|
517 |
|
|
*
|
518 |
|
|
* Line syntax:
|
519 |
|
|
* engineBoots <num_boots>
|
520 |
|
|
*/
|
521 |
|
|
void
|
522 |
|
|
engineBoots_conf(const char *word, char *cptr)
|
523 |
|
|
{
|
524 |
|
|
engineBoots = atoi(cptr)+1;
|
525 |
|
|
DEBUGMSGTL(("snmpv3","engineBoots: %d\n",engineBoots));
|
526 |
|
|
}
|
527 |
|
|
|
528 |
|
|
|
529 |
|
|
|
530 |
|
|
/*******************************************************************-o-******
|
531 |
|
|
* engineID_conf
|
532 |
|
|
*
|
533 |
|
|
* Parameters:
|
534 |
|
|
* *word
|
535 |
|
|
* *cptr
|
536 |
|
|
*
|
537 |
|
|
* This function reads a string from the configuration file and uses that
|
538 |
|
|
* string to initialize the engineID. It's assumed to be human readable.
|
539 |
|
|
*/
|
540 |
|
|
void
|
541 |
|
|
engineID_conf(const char *word, char *cptr)
|
542 |
|
|
{
|
543 |
|
|
setup_engineID(NULL, cptr);
|
544 |
|
|
DEBUGMSGTL(("snmpv3","initialized engineID with: %s\n",cptr));
|
545 |
|
|
}
|
546 |
|
|
|
547 |
|
|
void
|
548 |
|
|
version_conf(const char *word, char *cptr)
|
549 |
|
|
{
|
550 |
|
|
if (strcmp(cptr,"1") == 0) {
|
551 |
|
|
ds_set_int(DS_LIBRARY_ID, DS_LIB_SNMPVERSION, SNMP_VERSION_1);
|
552 |
|
|
} else if (strcasecmp(cptr,"2c") == 0) {
|
553 |
|
|
ds_set_int(DS_LIBRARY_ID, DS_LIB_SNMPVERSION, SNMP_VERSION_2c);
|
554 |
|
|
} else if (strcmp(cptr,"3") == 0) {
|
555 |
|
|
ds_set_int(DS_LIBRARY_ID, DS_LIB_SNMPVERSION, SNMP_VERSION_3);
|
556 |
|
|
} else {
|
557 |
|
|
config_perror("Unknown version specification");
|
558 |
|
|
return;
|
559 |
|
|
}
|
560 |
|
|
DEBUGMSGTL(("snmpv3","set default version to %d\n",
|
561 |
|
|
ds_get_int(DS_LIBRARY_ID, DS_LIB_SNMPVERSION)));
|
562 |
|
|
}
|
563 |
|
|
|
564 |
|
|
/* engineID_old_conf(const char *, char *):
|
565 |
|
|
|
566 |
|
|
Reads a octet string encoded engineID into the oldEngineID and
|
567 |
|
|
oldEngineIDLen pointers.
|
568 |
|
|
*/
|
569 |
|
|
void
|
570 |
|
|
oldengineID_conf(const char *word, char *cptr)
|
571 |
|
|
{
|
572 |
|
|
read_config_read_octet_string(cptr, &oldEngineID, &oldEngineIDLength);
|
573 |
|
|
}
|
574 |
|
|
|
575 |
|
|
|
576 |
|
|
/*******************************************************************-o-******
|
577 |
|
|
* init_snmpv3
|
578 |
|
|
*
|
579 |
|
|
* Parameters:
|
580 |
|
|
* *type Label for the config file "type" used by calling entity.
|
581 |
|
|
*
|
582 |
|
|
* Set time and engineID.
|
583 |
|
|
* Set parsing functions for config file tokens.
|
584 |
|
|
* Initialize SNMP Crypto API (SCAPI).
|
585 |
|
|
*/
|
586 |
|
|
void
|
587 |
|
|
init_snmpv3(const char *type) {
|
588 |
|
|
gettimeofday(&snmpv3starttime, NULL);
|
589 |
|
|
|
590 |
|
|
if (type == NULL)
|
591 |
|
|
type = "snmpapp";
|
592 |
|
|
|
593 |
|
|
if (type && !strcmp(type,"snmpapp")) {
|
594 |
|
|
setup_engineID(NULL,"__snmpapp__");
|
595 |
|
|
} else {
|
596 |
|
|
setup_engineID(NULL, NULL);
|
597 |
|
|
}
|
598 |
|
|
|
599 |
|
|
/* initialize submodules */
|
600 |
|
|
init_usm();
|
601 |
|
|
|
602 |
|
|
/* we need to be called back later */
|
603 |
|
|
snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_POST_READ_CONFIG,
|
604 |
|
|
init_snmpv3_post_config, NULL);
|
605 |
|
|
/* we need to be called back later */
|
606 |
|
|
snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
|
607 |
|
|
snmpv3_store, (void *) type);
|
608 |
|
|
|
609 |
|
|
|
610 |
|
|
#if !defined(USE_INTERNAL_MD5)
|
611 |
|
|
/* doesn't belong here at all */
|
612 |
|
|
sc_init();
|
613 |
|
|
#endif /* !USE_INTERNAL_MD5 */
|
614 |
|
|
|
615 |
|
|
/* register all our configuration handlers (ack, there's a lot) */
|
616 |
|
|
|
617 |
|
|
/* handle engineID setup before everything else which may depend on it */
|
618 |
|
|
register_premib_handler(type,"engineID", engineID_conf, NULL, "string");
|
619 |
|
|
register_premib_handler(type,"oldEngineID", oldengineID_conf, NULL, NULL);
|
620 |
|
|
register_config_handler(type,"engineBoots", engineBoots_conf, NULL, NULL);
|
621 |
|
|
|
622 |
|
|
/* default store config entries */
|
623 |
|
|
ds_register_config(ASN_OCTET_STR, "snmp", "defSecurityName", DS_LIBRARY_ID,
|
624 |
|
|
DS_LIB_SECNAME);
|
625 |
|
|
ds_register_config(ASN_OCTET_STR, "snmp", "defContext", DS_LIBRARY_ID,
|
626 |
|
|
DS_LIB_CONTEXT);
|
627 |
|
|
ds_register_config(ASN_OCTET_STR, "snmp", "defPassphrase", DS_LIBRARY_ID,
|
628 |
|
|
DS_LIB_PASSPHRASE);
|
629 |
|
|
ds_register_config(ASN_OCTET_STR, "snmp", "defAuthPassphrase", DS_LIBRARY_ID,
|
630 |
|
|
DS_LIB_AUTHPASSPHRASE);
|
631 |
|
|
ds_register_config(ASN_OCTET_STR, "snmp", "defPrivPassphrase", DS_LIBRARY_ID,
|
632 |
|
|
DS_LIB_PRIVPASSPHRASE);
|
633 |
|
|
register_config_handler("snmp","defVersion", version_conf, NULL, "1|2c|3");
|
634 |
|
|
|
635 |
|
|
register_config_handler("snmp","defAuthType", snmpv3_authtype_conf, NULL,
|
636 |
|
|
"MD5|SHA");
|
637 |
|
|
register_config_handler("snmp","defPrivType", snmpv3_privtype_conf, NULL,
|
638 |
|
|
"DES (currently the only possible value)");
|
639 |
|
|
register_config_handler("snmp","defSecurityLevel", snmpv3_secLevel_conf,
|
640 |
|
|
NULL, "noAuthNoPriv|authNoPriv|authPriv");
|
641 |
|
|
register_config_handler(type,"userSetAuthPass", usm_set_password, NULL,
|
642 |
|
|
"secname engineIDLen engineID pass");
|
643 |
|
|
register_config_handler(type,"userSetPrivPass", usm_set_password, NULL,
|
644 |
|
|
"secname engineIDLen engineID pass");
|
645 |
|
|
register_config_handler(type,"userSetAuthKey", usm_set_password, NULL,
|
646 |
|
|
"secname engineIDLen engineID KuLen Ku");
|
647 |
|
|
register_config_handler(type,"userSetPrivKey", usm_set_password, NULL,
|
648 |
|
|
"secname engineIDLen engineID KuLen Ku");
|
649 |
|
|
register_config_handler(type,"userSetAuthLocalKey", usm_set_password, NULL,
|
650 |
|
|
"secname engineIDLen engineID KulLen Kul");
|
651 |
|
|
register_config_handler(type,"userSetPrivLocalKey", usm_set_password, NULL,
|
652 |
|
|
"secname engineIDLen engineID KulLen Kul");
|
653 |
|
|
}
|
654 |
|
|
|
655 |
|
|
/*
|
656 |
|
|
* initializations for SNMPv3 to be called after the configuration files
|
657 |
|
|
* have been read.
|
658 |
|
|
*/
|
659 |
|
|
|
660 |
|
|
int
|
661 |
|
|
init_snmpv3_post_config(int majorid, int minorid, void *serverarg,
|
662 |
|
|
void *clientarg) {
|
663 |
|
|
|
664 |
|
|
int engineIDLen;
|
665 |
|
|
u_char *c_engineID;
|
666 |
|
|
|
667 |
|
|
c_engineID = snmpv3_generate_engineID(&engineIDLen);
|
668 |
|
|
|
669 |
|
|
if ( engineIDLen < 0 ) {
|
670 |
|
|
/* Somethine went wrong - help! */
|
671 |
|
|
return SNMPERR_GENERR;
|
672 |
|
|
}
|
673 |
|
|
|
674 |
|
|
/* if our engineID has changed at all, the boots record must be set to 1 */
|
675 |
|
|
if (engineIDLen != (int)oldEngineIDLength ||
|
676 |
|
|
oldEngineID == NULL || c_engineID == NULL ||
|
677 |
|
|
memcmp(oldEngineID, c_engineID, engineIDLen) != 0) {
|
678 |
|
|
engineBoots = 1;
|
679 |
|
|
}
|
680 |
|
|
|
681 |
|
|
/* set our local engineTime in the LCD timing cache */
|
682 |
|
|
set_enginetime(c_engineID, engineIDLen,
|
683 |
|
|
snmpv3_local_snmpEngineBoots(),
|
684 |
|
|
snmpv3_local_snmpEngineTime(),
|
685 |
|
|
TRUE);
|
686 |
|
|
|
687 |
|
|
free(c_engineID);
|
688 |
|
|
return SNMPERR_SUCCESS;
|
689 |
|
|
}
|
690 |
|
|
|
691 |
|
|
/*******************************************************************-o-******
|
692 |
|
|
* store_snmpv3
|
693 |
|
|
*
|
694 |
|
|
* Parameters:
|
695 |
|
|
* *type
|
696 |
|
|
*/
|
697 |
|
|
int
|
698 |
|
|
snmpv3_store(int majorID, int minorID, void *serverarg, void *clientarg) {
|
699 |
|
|
char line[SNMP_MAXBUF_SMALL];
|
700 |
|
|
u_char c_engineID[SNMP_MAXBUF_SMALL];
|
701 |
|
|
int engineIDLen;
|
702 |
|
|
const char *type = (const char *) clientarg;
|
703 |
|
|
|
704 |
|
|
if (type == NULL) /* should never happen, since the arg is ours */
|
705 |
|
|
type = "unknown";
|
706 |
|
|
|
707 |
|
|
sprintf(line, "engineBoots %ld", engineBoots);
|
708 |
|
|
read_config_store(type, line);
|
709 |
|
|
|
710 |
|
|
engineIDLen = snmpv3_get_engineID(c_engineID, SNMP_MAXBUF_SMALL);
|
711 |
|
|
|
712 |
|
|
if (engineIDLen) {
|
713 |
|
|
/* store the engineID used for this run */
|
714 |
|
|
sprintf(line, "oldEngineID ");
|
715 |
|
|
read_config_save_octet_string(line+strlen(line), c_engineID,
|
716 |
|
|
engineIDLen);
|
717 |
|
|
read_config_store(type, line);
|
718 |
|
|
}
|
719 |
|
|
return SNMPERR_SUCCESS;
|
720 |
|
|
} /* snmpv3_store() */
|
721 |
|
|
|
722 |
|
|
u_long
|
723 |
|
|
snmpv3_local_snmpEngineBoots(void)
|
724 |
|
|
{
|
725 |
|
|
return engineBoots;
|
726 |
|
|
}
|
727 |
|
|
|
728 |
|
|
|
729 |
|
|
/*******************************************************************-o-******
|
730 |
|
|
* snmpv3_get_engineID
|
731 |
|
|
*
|
732 |
|
|
* Parameters:
|
733 |
|
|
* *buf
|
734 |
|
|
* buflen
|
735 |
|
|
*
|
736 |
|
|
* Returns:
|
737 |
|
|
* Length of engineID On Success
|
738 |
|
|
* SNMPERR_GENERR Otherwise.
|
739 |
|
|
*
|
740 |
|
|
*
|
741 |
|
|
* Store engineID in buf; return the length.
|
742 |
|
|
*
|
743 |
|
|
*/
|
744 |
|
|
int
|
745 |
|
|
snmpv3_get_engineID(u_char *buf, size_t buflen)
|
746 |
|
|
{
|
747 |
|
|
/*
|
748 |
|
|
* Sanity check.
|
749 |
|
|
*/
|
750 |
|
|
if ( !buf || (buflen < engineIDLength) ) {
|
751 |
|
|
return SNMPERR_GENERR;
|
752 |
|
|
}
|
753 |
|
|
|
754 |
|
|
memcpy(buf,engineID,engineIDLength);
|
755 |
|
|
return engineIDLength;
|
756 |
|
|
|
757 |
|
|
} /* end snmpv3_get_engineID() */
|
758 |
|
|
|
759 |
|
|
/*******************************************************************-o-******
|
760 |
|
|
* snmpv3_clone_engineID
|
761 |
|
|
*
|
762 |
|
|
* Parameters:
|
763 |
|
|
* **dest
|
764 |
|
|
* *dest_len
|
765 |
|
|
* src
|
766 |
|
|
* srclen
|
767 |
|
|
*
|
768 |
|
|
* Returns:
|
769 |
|
|
* Length of engineID On Success
|
770 |
|
|
* 0 Otherwise.
|
771 |
|
|
*
|
772 |
|
|
*
|
773 |
|
|
* Clones engineID, creates memory
|
774 |
|
|
*
|
775 |
|
|
*/
|
776 |
|
|
int
|
777 |
|
|
snmpv3_clone_engineID(u_char **dest, size_t* destlen, u_char*src, size_t srclen)
|
778 |
|
|
{
|
779 |
|
|
if ( !dest || !destlen ) return 0;
|
780 |
|
|
|
781 |
|
|
*dest = NULL; *destlen = 0;
|
782 |
|
|
|
783 |
|
|
if (srclen && src) {
|
784 |
|
|
*dest = (u_char*)malloc((unsigned)srclen * sizeof(u_char));
|
785 |
|
|
if (*dest == NULL) return 0;
|
786 |
|
|
memmove(*dest, src, srclen * sizeof(u_char));
|
787 |
|
|
*destlen = srclen;
|
788 |
|
|
}
|
789 |
|
|
return *destlen;
|
790 |
|
|
} /* end snmpv3_clone_engineID() */
|
791 |
|
|
|
792 |
|
|
|
793 |
|
|
/*******************************************************************-o-******
|
794 |
|
|
* snmpv3_generate_engineID
|
795 |
|
|
*
|
796 |
|
|
* Parameters:
|
797 |
|
|
* *length
|
798 |
|
|
*
|
799 |
|
|
* Returns:
|
800 |
|
|
* Pointer to copy of engineID On Success.
|
801 |
|
|
* NULL If malloc() or snmpv3_get_engineID()
|
802 |
|
|
* fail.
|
803 |
|
|
*
|
804 |
|
|
* Generates a malloced copy of our engineID.
|
805 |
|
|
*
|
806 |
|
|
* 'length' is set to the length of engineID -OR- < 0 on failure.
|
807 |
|
|
*/
|
808 |
|
|
u_char *
|
809 |
|
|
snmpv3_generate_engineID(int *length)
|
810 |
|
|
{
|
811 |
|
|
u_char *newID;
|
812 |
|
|
newID = (u_char *) malloc(engineIDLength);
|
813 |
|
|
|
814 |
|
|
if (newID) {
|
815 |
|
|
*length = snmpv3_get_engineID(newID, engineIDLength);
|
816 |
|
|
}
|
817 |
|
|
|
818 |
|
|
if (*length < 0) {
|
819 |
|
|
SNMP_FREE(newID);
|
820 |
|
|
newID = NULL;
|
821 |
|
|
}
|
822 |
|
|
|
823 |
|
|
return newID;
|
824 |
|
|
|
825 |
|
|
} /* end snmpv3_generate_engineID() */
|
826 |
|
|
|
827 |
|
|
/* snmpv3_local_snmpEngineTime(): return the number of seconds since the
|
828 |
|
|
snmpv3 engine last incremented engine_boots */
|
829 |
|
|
u_long
|
830 |
|
|
snmpv3_local_snmpEngineTime(void)
|
831 |
|
|
{
|
832 |
|
|
struct timeval now;
|
833 |
|
|
|
834 |
|
|
gettimeofday(&now, NULL);
|
835 |
|
|
return calculate_time_diff(&now, &snmpv3starttime)/100;
|
836 |
|
|
}
|
837 |
|
|
|
838 |
|
|
#ifdef SNMP_TESTING_CODE
|
839 |
|
|
/* snmpv3_set_engineBootsAndTime(): this function does not exist. Go away. */
|
840 |
|
|
/* It certainly should never be used, unless in a testing scenero,
|
841 |
|
|
which is why it was created */
|
842 |
|
|
void
|
843 |
|
|
snmpv3_set_engineBootsAndTime(int boots, int ttime) {
|
844 |
|
|
engineBoots = boots;
|
845 |
|
|
gettimeofday(&snmpv3starttime, NULL);
|
846 |
|
|
snmpv3starttime.tv_sec -= ttime;
|
847 |
|
|
}
|
848 |
|
|
#endif
|
849 |
|
|
|
850 |
|
|
#endif /* CYGPKG_SNMPAGENT_V3_SUPPORT */
|