added files
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / snmp_auth.c
1 /*
2  * snmp_auth.c
3  *
4  * Community name parse/build routines.
5  */
6 /**********************************************************************
7     Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
8
9                          All Rights Reserved
10
11 Permission to use, copy, modify, and distribute this software and its
12 documentation for any purpose and without fee is hereby granted,
13 provided that the above copyright notice appear in all copies and that
14 both that copyright notice and this permission notice appear in
15 supporting documentation, and that the name of CMU not be
16 used in advertising or publicity pertaining to distribution of the
17 software without specific, written prior permission.
18
19 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
20 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
21 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
22 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
25 SOFTWARE.
26 ******************************************************************/
27
28 #include <net-snmp/net-snmp-config.h>
29
30 #ifdef KINETICS
31 #include "gw.h"
32 #include "fp4/cmdmacro.h"
33 #endif
34
35 #include <stdio.h>
36 #if HAVE_STRING_H
37 #include <string.h>
38 #else
39 #include <strings.h>
40 #endif
41 #include <sys/types.h>
42 #if TIME_WITH_SYS_TIME
43 # ifdef WIN32
44 #  include <sys/timeb.h>
45 # else
46 #  include <sys/time.h>
47 # endif
48 # include <time.h>
49 #else
50 # if HAVE_SYS_TIME_H
51 #  include <sys/time.h>
52 # else
53 #  include <time.h>
54 # endif
55 #endif
56 #if HAVE_SYS_SELECT_H
57 #include <sys/select.h>
58 #endif
59 #if HAVE_NETINET_IN_H
60 #include <netinet/in.h>
61 #endif
62 #if HAVE_ARPA_INET_H
63 #include <arpa/inet.h>
64 #endif
65
66 #if HAVE_DMALLOC_H
67 #include <dmalloc.h>
68 #endif
69
70 #if HAVE_WINSOCK_H
71 #include <winsock.h>
72 #endif
73
74 #ifdef vms
75 #include <in.h>
76 #endif
77
78 #include <net-snmp/types.h>
79 #include <net-snmp/output_api.h>
80 #include <net-snmp/utilities.h>
81
82 #include <net-snmp/library/asn1.h>
83 #include <net-snmp/library/snmp_api.h>
84 #include <net-snmp/library/mib.h>
85 #include <net-snmp/library/md5.h>
86 #include <net-snmp/library/scapi.h>
87
88 /*
89  * Globals.
90  */
91
92 /*******************************************************************-o-******
93  * snmp_comstr_parse
94  *
95  * Parameters:
96  *      *data           (I)   Message.
97  *      *length         (I/O) Bytes left in message.
98  *      *psid           (O)   Community string.
99  *      *slen           (O)   Length of community string.
100  *      *version        (O)   Message version.
101  *      
102  * Returns:
103  *      Pointer to the remainder of data.
104  *
105  *
106  * Parse the header of a community string-based message such as that found
107  * in SNMPv1 and SNMPv2c.
108  */
109 u_char         *
110 snmp_comstr_parse(u_char * data,
111                   size_t * length,
112                   u_char * psid, size_t * slen, long *version)
113 {
114     u_char          type;
115     long            ver;
116     size_t          origlen = *slen;
117
118     /*
119      * Message is an ASN.1 SEQUENCE.
120      */
121     data = asn_parse_sequence(data, length, &type,
122                               (ASN_SEQUENCE | ASN_CONSTRUCTOR),
123                               "auth message");
124     if (data == NULL) {
125         return NULL;
126     }
127
128     /*
129      * First field is the version.
130      */
131     DEBUGDUMPHEADER("recv", "SNMP version");
132     data = asn_parse_int(data, length, &type, &ver, sizeof(ver));
133     DEBUGINDENTLESS();
134     *version = ver;
135     if (data == NULL) {
136         ERROR_MSG("bad parse of version");
137         return NULL;
138     }
139
140     /*
141      * second field is the community string for SNMPv1 & SNMPv2c 
142      */
143     DEBUGDUMPHEADER("recv", "community string");
144     data = asn_parse_string(data, length, &type, psid, slen);
145     DEBUGINDENTLESS();
146     if (data == NULL) {
147         ERROR_MSG("bad parse of community");
148         return NULL;
149     }
150     psid[SNMP_MIN(*slen, origlen - 1)] = '\0';
151     return (u_char *) data;
152
153 }                               /* end snmp_comstr_parse() */
154
155
156
157
158 /*******************************************************************-o-******
159  * snmp_comstr_build
160  *
161  * Parameters:
162  *      *data
163  *      *length
164  *      *psid
165  *      *slen
166  *      *version
167  *       messagelen
168  *      
169  * Returns:
170  *      Pointer into 'data' after built section.
171  *
172  *
173  * Build the header of a community string-based message such as that found
174  * in SNMPv1 and SNMPv2c.
175  *
176  * NOTE:        The length of the message will have to be inserted later,
177  *              if not known.
178  *
179  * NOTE:        Version is an 'int'.  (CMU had it as a long, but was passing
180  *              in a *int.  Grrr.)  Assign version to verfix and pass in
181  *              that to asn_build_int instead which expects a long.  -- WH
182  */
183 u_char         *
184 snmp_comstr_build(u_char * data,
185                   size_t * length,
186                   u_char * psid,
187                   size_t * slen, long *version, size_t messagelen)
188 {
189     long            verfix = *version;
190     u_char         *h1 = data;
191     u_char         *h1e;
192     size_t          hlength = *length;
193
194
195     /*
196      * Build the the message wrapper (note length will be inserted later).
197      */
198     data =
199         asn_build_sequence(data, length,
200                            (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR), 0);
201     if (data == NULL) {
202         return NULL;
203     }
204     h1e = data;
205
206
207     /*
208      * Store the version field.
209      */
210     data = asn_build_int(data, length,
211                          (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE |
212                                    ASN_INTEGER), &verfix, sizeof(verfix));
213     if (data == NULL) {
214         return NULL;
215     }
216
217
218     /*
219      * Store the community string.
220      */
221     data = asn_build_string(data, length,
222                             (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE |
223                                       ASN_OCTET_STR), psid,
224                             *(u_char *) slen);
225     if (data == NULL) {
226         return NULL;
227     }
228
229
230     /*
231      * Insert length.
232      */
233     asn_build_sequence(h1, &hlength,
234                        (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR),
235                        data - h1e + messagelen);
236
237
238     return data;
239
240 }                               /* end snmp_comstr_build() */