# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / snmp_secmod.c
1 /*
2  * security service wrapper to support pluggable security models 
3  */
4
5 #include <net-snmp/net-snmp-config.h>
6 #include <stdio.h>
7 #include <ctype.h>
8 #if HAVE_STDLIB_H
9 #include <stdlib.h>
10 #endif
11 #if HAVE_STRING_H
12 #include <string.h>
13 #else
14 #include <strings.h>
15 #endif
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19
20 #if HAVE_DMALLOC_H
21 #include <dmalloc.h>
22 #endif
23
24 #include <net-snmp/types.h>
25 #include <net-snmp/output_api.h>
26 #include <net-snmp/config_api.h>
27 #include <net-snmp/utilities.h>
28
29 #include <net-snmp/library/snmp_api.h>
30 #include <net-snmp/library/snmp_enum.h>
31 #include <net-snmp/library/callback.h>
32 #include <net-snmp/library/snmp_secmod.h>
33 #include <net-snmp/library/snmpusm.h>
34
35 static struct snmp_secmod_list *registered_services = NULL;
36
37 static SNMPCallback set_default_secmod;
38
39 void
40 init_secmod(void)
41 {
42     snmp_register_callback(SNMP_CALLBACK_LIBRARY,
43                            SNMP_CALLBACK_SESSION_INIT, set_default_secmod,
44                            NULL);
45
46     netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defSecurityModel",
47                                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_SECMODEL);
48     /*
49      * this file is generated by configure for all the stuff we're using 
50      */
51 #ifdef BRCM_SNMP_MIB_SUPPORT
52 #include "snmpsm_init.h"
53 #endif
54 }
55
56
57 int
58 register_sec_mod(int secmod, const char *modname,
59                  struct snmp_secmod_def *newdef)
60 {
61     int             result;
62     struct snmp_secmod_list *sptr;
63     char           *othername;
64
65     for (sptr = registered_services; sptr; sptr = sptr->next) {
66         if (sptr->securityModel == secmod) {
67             return SNMPERR_GENERR;
68         }
69     }
70     sptr = SNMP_MALLOC_STRUCT(snmp_secmod_list);
71     if (sptr == NULL)
72         return SNMPERR_MALLOC;
73     sptr->secDef = newdef;
74     sptr->securityModel = secmod;
75     sptr->next = registered_services;
76     registered_services = sptr;
77     if ((result =
78          se_add_pair_to_slist("snmp_secmods", strdup(modname), secmod))
79         != SE_OK) {
80         switch (result) {
81         case SE_NOMEM:
82             snmp_log(LOG_CRIT, "snmp_secmod: no memory\n");
83             break;
84
85         case SE_ALREADY_THERE:
86             othername = se_find_label_in_slist("snmp_secmods", secmod);
87             if (strcmp(othername, modname) != 0) {
88                 snmp_log(LOG_ERR,
89                          "snmp_secmod: two security modules %s and %s registered with the same security number\n",
90                          secmod, othername);
91             }
92             break;
93
94         default:
95             snmp_log(LOG_ERR,
96                      "snmp_secmod: unknown error trying to register a new security module\n");
97             break;
98         }
99         return SNMPERR_GENERR;
100     }
101     return SNMPERR_SUCCESS;
102 }
103
104 int
105 unregister_sec_mod(int secmod)
106 {
107     struct snmp_secmod_list *sptr, *lptr;
108
109     for (sptr = registered_services, lptr = NULL; sptr;
110          lptr = sptr, sptr = sptr->next) {
111         if (sptr->securityModel == secmod) {
112             lptr->next = sptr->next;
113             free(sptr);
114             return SNMPERR_SUCCESS;
115         }
116     }
117     /*
118      * not registered 
119      */
120     return SNMPERR_GENERR;
121 }
122
123 struct snmp_secmod_def *
124 find_sec_mod(int secmod)
125 {
126     struct snmp_secmod_list *sptr;
127
128     for (sptr = registered_services; sptr; sptr = sptr->next) {
129         if (sptr->securityModel == secmod) {
130             return sptr->secDef;
131         }
132     }
133     /*
134      * not registered 
135      */
136     return NULL;
137 }
138
139 static int
140 set_default_secmod(int major, int minor, void *serverarg, void *clientarg)
141 {
142     netsnmp_session *sess = (netsnmp_session *) serverarg;
143     char           *cptr;
144     int             model;
145
146     if (!sess)
147         return SNMPERR_GENERR;
148     if (sess->securityModel == SNMP_DEFAULT_SECMODEL) {
149         if ((cptr = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
150                                           NETSNMP_DS_LIB_SECMODEL)) != NULL) {
151             if ((model = se_find_value_in_slist("snmp_secmods", cptr))
152                 != SE_DNE) {
153                 sess->securityModel = model;
154             } else {
155                 snmp_log(LOG_ERR,
156                          "unknown security model name: %s.  Forcing USM instead.\n",
157                          cptr);
158                 sess->securityModel = USM_SEC_MODEL_NUMBER;
159                 return SNMPERR_GENERR;
160             }
161         } else {
162             sess->securityModel = USM_SEC_MODEL_NUMBER;
163         }
164     }
165     return SNMPERR_SUCCESS;
166 }