1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// ./lib/current/src/snmp_auth.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 |
|
|
* snmp_auth.c
|
94 |
|
|
*
|
95 |
|
|
* Community name parse/build routines.
|
96 |
|
|
*/
|
97 |
|
|
/**********************************************************************
|
98 |
|
|
Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
|
99 |
|
|
|
100 |
|
|
All Rights Reserved
|
101 |
|
|
|
102 |
|
|
Permission to use, copy, modify, and distribute this software and its
|
103 |
|
|
documentation for any purpose and without fee is hereby granted,
|
104 |
|
|
provided that the above copyright notice appear in all copies and that
|
105 |
|
|
both that copyright notice and this permission notice appear in
|
106 |
|
|
supporting documentation, and that the name of CMU not be
|
107 |
|
|
used in advertising or publicity pertaining to distribution of the
|
108 |
|
|
software without specific, written prior permission.
|
109 |
|
|
|
110 |
|
|
CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
|
111 |
|
|
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
|
112 |
|
|
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
|
113 |
|
|
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
114 |
|
|
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
115 |
|
|
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
116 |
|
|
SOFTWARE.
|
117 |
|
|
******************************************************************/
|
118 |
|
|
|
119 |
|
|
#include <config.h>
|
120 |
|
|
|
121 |
|
|
#ifdef KINETICS
|
122 |
|
|
#include "gw.h"
|
123 |
|
|
#include "fp4/cmdmacro.h"
|
124 |
|
|
#endif
|
125 |
|
|
|
126 |
|
|
#include <stdio.h>
|
127 |
|
|
#if HAVE_STRING_H
|
128 |
|
|
#include <string.h>
|
129 |
|
|
#else
|
130 |
|
|
#include <strings.h>
|
131 |
|
|
#endif
|
132 |
|
|
#include <sys/types.h>
|
133 |
|
|
#if TIME_WITH_SYS_TIME
|
134 |
|
|
# ifdef WIN32
|
135 |
|
|
# include <sys/timeb.h>
|
136 |
|
|
# else
|
137 |
|
|
# include <sys/time.h>
|
138 |
|
|
# endif
|
139 |
|
|
# include <time.h>
|
140 |
|
|
#else
|
141 |
|
|
# if HAVE_SYS_TIME_H
|
142 |
|
|
# include <sys/time.h>
|
143 |
|
|
# else
|
144 |
|
|
# include <time.h>
|
145 |
|
|
# endif
|
146 |
|
|
#endif
|
147 |
|
|
#if HAVE_SYS_SELECT_H
|
148 |
|
|
#include <sys/select.h>
|
149 |
|
|
#endif
|
150 |
|
|
#if HAVE_NETINET_IN_H
|
151 |
|
|
#include <netinet/in.h>
|
152 |
|
|
#endif
|
153 |
|
|
#if HAVE_ARPA_INET_H
|
154 |
|
|
#include <arpa/inet.h>
|
155 |
|
|
#endif
|
156 |
|
|
|
157 |
|
|
#if HAVE_DMALLOC_H
|
158 |
|
|
#include <dmalloc.h>
|
159 |
|
|
#endif
|
160 |
|
|
|
161 |
|
|
#if HAVE_WINSOCK_H
|
162 |
|
|
#include <winsock.h>
|
163 |
|
|
#endif
|
164 |
|
|
|
165 |
|
|
#ifdef vms
|
166 |
|
|
#include <in.h>
|
167 |
|
|
#endif
|
168 |
|
|
|
169 |
|
|
#include "asn1.h"
|
170 |
|
|
#include "snmp.h"
|
171 |
|
|
#include "snmp_api.h"
|
172 |
|
|
#include "snmp_impl.h"
|
173 |
|
|
#include "mib.h"
|
174 |
|
|
#include "md5.h"
|
175 |
|
|
#include "system.h"
|
176 |
|
|
#include "tools.h"
|
177 |
|
|
#include "snmp_debug.h"
|
178 |
|
|
#include "scapi.h"
|
179 |
|
|
|
180 |
|
|
/*
|
181 |
|
|
* Globals.
|
182 |
|
|
*/
|
183 |
|
|
|
184 |
|
|
/*******************************************************************-o-******
|
185 |
|
|
* snmp_comstr_parse
|
186 |
|
|
*
|
187 |
|
|
* Parameters:
|
188 |
|
|
* *data (I) Message.
|
189 |
|
|
* *length (I/O) Bytes left in message.
|
190 |
|
|
* *psid (O) Community string.
|
191 |
|
|
* *slen (O) Length of community string.
|
192 |
|
|
* *version (O) Message version.
|
193 |
|
|
*
|
194 |
|
|
* Returns:
|
195 |
|
|
* Pointer to the remainder of data.
|
196 |
|
|
*
|
197 |
|
|
*
|
198 |
|
|
* Parse the header of a community string-based message such as that found
|
199 |
|
|
* in SNMPv1 and SNMPv2c.
|
200 |
|
|
*/
|
201 |
|
|
u_char *
|
202 |
|
|
snmp_comstr_parse(u_char *data,
|
203 |
|
|
size_t *length,
|
204 |
|
|
u_char *psid,
|
205 |
|
|
size_t *slen,
|
206 |
|
|
long *version)
|
207 |
|
|
{
|
208 |
|
|
u_char type;
|
209 |
|
|
long ver;
|
210 |
|
|
|
211 |
|
|
|
212 |
|
|
/* Message is an ASN.1 SEQUENCE.
|
213 |
|
|
*/
|
214 |
|
|
data = asn_parse_sequence(data, length, &type,
|
215 |
|
|
(ASN_SEQUENCE | ASN_CONSTRUCTOR), "auth message");
|
216 |
|
|
if (data == NULL){
|
217 |
|
|
return NULL;
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
/* First field is the version.
|
221 |
|
|
*/
|
222 |
|
|
DEBUGDUMPHEADER("dump_recv", "Parsing SNMP version\n");
|
223 |
|
|
data = asn_parse_int(data, length, &type, &ver, sizeof(ver));
|
224 |
|
|
DEBUGINDENTLESS();
|
225 |
|
|
*version = ver;
|
226 |
|
|
if (data == NULL){
|
227 |
|
|
ERROR_MSG("bad parse of version");
|
228 |
|
|
return NULL;
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
/* second field is the community string for SNMPv1 & SNMPv2c */
|
232 |
|
|
DEBUGDUMPHEADER("dump_recv", "Parsing community string\n");
|
233 |
|
|
data = asn_parse_string(data, length, &type, psid, slen);
|
234 |
|
|
DEBUGINDENTLESS();
|
235 |
|
|
if (data == NULL){
|
236 |
|
|
ERROR_MSG("bad parse of community");
|
237 |
|
|
return NULL;
|
238 |
|
|
}
|
239 |
|
|
psid[*slen] = '\0';
|
240 |
|
|
return (u_char *)data;
|
241 |
|
|
|
242 |
|
|
} /* end snmp_comstr_parse() */
|
243 |
|
|
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
|
247 |
|
|
/*******************************************************************-o-******
|
248 |
|
|
* snmp_comstr_build
|
249 |
|
|
*
|
250 |
|
|
* Parameters:
|
251 |
|
|
* *data
|
252 |
|
|
* *length
|
253 |
|
|
* *psid
|
254 |
|
|
* *slen
|
255 |
|
|
* *version
|
256 |
|
|
* messagelen
|
257 |
|
|
*
|
258 |
|
|
* Returns:
|
259 |
|
|
* Pointer into 'data' after built section.
|
260 |
|
|
*
|
261 |
|
|
*
|
262 |
|
|
* Build the header of a community string-based message such as that found
|
263 |
|
|
* in SNMPv1 and SNMPv2c.
|
264 |
|
|
*
|
265 |
|
|
* NOTE: The length of the message will have to be inserted later,
|
266 |
|
|
* if not known.
|
267 |
|
|
*
|
268 |
|
|
* NOTE: Version is an 'int'. (CMU had it as a long, but was passing
|
269 |
|
|
* in a *int. Grrr.) Assign version to verfix and pass in
|
270 |
|
|
* that to asn_build_int instead which expects a long. -- WH
|
271 |
|
|
*/
|
272 |
|
|
u_char *
|
273 |
|
|
snmp_comstr_build( u_char *data,
|
274 |
|
|
size_t *length,
|
275 |
|
|
u_char *psid,
|
276 |
|
|
size_t *slen,
|
277 |
|
|
long *version,
|
278 |
|
|
size_t messagelen)
|
279 |
|
|
{
|
280 |
|
|
long verfix = *version;
|
281 |
|
|
u_char *h1 = data;
|
282 |
|
|
u_char *h1e;
|
283 |
|
|
size_t hlength = *length;
|
284 |
|
|
|
285 |
|
|
|
286 |
|
|
/* Build the the message wrapper (note length will be inserted later).
|
287 |
|
|
*/
|
288 |
|
|
data = asn_build_sequence(data, length, (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR), 0);
|
289 |
|
|
if (data == NULL){
|
290 |
|
|
return NULL;
|
291 |
|
|
}
|
292 |
|
|
h1e = data;
|
293 |
|
|
|
294 |
|
|
|
295 |
|
|
/* Store the version field.
|
296 |
|
|
*/
|
297 |
|
|
data = asn_build_int(data, length,
|
298 |
|
|
(u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
|
299 |
|
|
&verfix, sizeof(verfix));
|
300 |
|
|
if (data == NULL){
|
301 |
|
|
return NULL;
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
|
305 |
|
|
/* Store the community string.
|
306 |
|
|
*/
|
307 |
|
|
data = asn_build_string(data, length,
|
308 |
|
|
(u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR),
|
309 |
|
|
psid, *(u_char *)slen);
|
310 |
|
|
if (data == NULL){
|
311 |
|
|
return NULL;
|
312 |
|
|
}
|
313 |
|
|
|
314 |
|
|
|
315 |
|
|
/* Insert length.
|
316 |
|
|
*/
|
317 |
|
|
asn_build_sequence(h1, &hlength, (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR),
|
318 |
|
|
data-h1e + messagelen);
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
return data;
|
322 |
|
|
|
323 |
|
|
} /* end snmp_comstr_build() */
|
324 |
|
|
|