| 1 |
27 |
unneback |
//==========================================================================
|
| 2 |
|
|
//
|
| 3 |
|
|
// ./lib/current/src/vacm.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 |
|
|
* vacm.c
|
| 94 |
|
|
*
|
| 95 |
|
|
* SNMPv3 View-based Access Control Model
|
| 96 |
|
|
*/
|
| 97 |
|
|
|
| 98 |
|
|
#include <config.h>
|
| 99 |
|
|
|
| 100 |
|
|
#ifdef CYGPKG_SNMPAGENT_V3_SUPPORT
|
| 101 |
|
|
#if HAVE_STDLIB_H
|
| 102 |
|
|
#include <stdlib.h>
|
| 103 |
|
|
#endif
|
| 104 |
|
|
#if HAVE_STRING_H
|
| 105 |
|
|
#include <string.h>
|
| 106 |
|
|
#else
|
| 107 |
|
|
#include <strings.h>
|
| 108 |
|
|
#endif
|
| 109 |
|
|
#if HAVE_UNISTD_H
|
| 110 |
|
|
#include <unistd.h>
|
| 111 |
|
|
#endif
|
| 112 |
|
|
#include <sys/types.h>
|
| 113 |
|
|
#include <stdio.h>
|
| 114 |
|
|
#if TIME_WITH_SYS_TIME
|
| 115 |
|
|
# ifdef WIN32
|
| 116 |
|
|
# include <sys/timeb.h>
|
| 117 |
|
|
# else
|
| 118 |
|
|
# include <sys/time.h>
|
| 119 |
|
|
# endif
|
| 120 |
|
|
# include <time.h>
|
| 121 |
|
|
#else
|
| 122 |
|
|
# if HAVE_SYS_TIME_H
|
| 123 |
|
|
# include <sys/time.h>
|
| 124 |
|
|
# else
|
| 125 |
|
|
# include <time.h>
|
| 126 |
|
|
# endif
|
| 127 |
|
|
#endif
|
| 128 |
|
|
|
| 129 |
|
|
#if HAVE_WINSOCK_H
|
| 130 |
|
|
#include <winsock.h>
|
| 131 |
|
|
#endif
|
| 132 |
|
|
|
| 133 |
|
|
#if HAVE_NETINET_IN_H
|
| 134 |
|
|
#include <netinet/in.h>
|
| 135 |
|
|
#endif
|
| 136 |
|
|
|
| 137 |
|
|
#if HAVE_DMALLOC_H
|
| 138 |
|
|
#include <dmalloc.h>
|
| 139 |
|
|
#endif
|
| 140 |
|
|
|
| 141 |
|
|
#include "asn1.h"
|
| 142 |
|
|
#include "snmp.h"
|
| 143 |
|
|
#include "snmp_api.h"
|
| 144 |
|
|
#include "vacm.h"
|
| 145 |
|
|
#include "snmp_debug.h"
|
| 146 |
|
|
|
| 147 |
|
|
static struct vacm_viewEntry *viewList = NULL, *viewScanPtr = NULL;
|
| 148 |
|
|
static struct vacm_accessEntry *accessList = NULL, *accessScanPtr = NULL;
|
| 149 |
|
|
static struct vacm_groupEntry *groupList = NULL, *groupScanPtr = NULL;
|
| 150 |
|
|
|
| 151 |
|
|
struct vacm_viewEntry *
|
| 152 |
|
|
vacm_getViewEntry(const char *viewName,
|
| 153 |
|
|
oid *viewSubtree,
|
| 154 |
|
|
size_t viewSubtreeLen)
|
| 155 |
|
|
{
|
| 156 |
|
|
struct vacm_viewEntry *vp, *vpret = NULL;
|
| 157 |
|
|
char view[VACMSTRINGLEN];
|
| 158 |
|
|
int found, glen;
|
| 159 |
|
|
|
| 160 |
|
|
glen = (int)strlen(viewName);
|
| 161 |
|
|
if (glen < 0 || glen >= VACM_MAX_STRING)
|
| 162 |
|
|
return NULL;
|
| 163 |
|
|
view[0] = glen;
|
| 164 |
|
|
strcpy(view+1, viewName);
|
| 165 |
|
|
for(vp = viewList; vp; vp = vp->next){
|
| 166 |
|
|
if (!memcmp(view, vp->viewName,glen+1)
|
| 167 |
|
|
&& viewSubtreeLen >= vp->viewSubtreeLen) {
|
| 168 |
|
|
int mask = 0x80, maskpos = 0;
|
| 169 |
|
|
int oidpos;
|
| 170 |
|
|
found = 1;
|
| 171 |
|
|
for (oidpos = 0; found && oidpos < (int)vp->viewSubtreeLen; oidpos++) {
|
| 172 |
|
|
if ((vp->viewMask[maskpos] & mask) != 0) {
|
| 173 |
|
|
if (viewSubtree[oidpos] != vp->viewSubtree[oidpos])
|
| 174 |
|
|
found = 0;
|
| 175 |
|
|
}
|
| 176 |
|
|
if (mask == 1) {
|
| 177 |
|
|
mask = 0x80;
|
| 178 |
|
|
maskpos++;
|
| 179 |
|
|
}
|
| 180 |
|
|
else mask >>= 1;
|
| 181 |
|
|
}
|
| 182 |
|
|
if (found) {
|
| 183 |
|
|
/* match successful, keep this node if its longer than
|
| 184 |
|
|
the previous or (equal and lexicographically greater
|
| 185 |
|
|
than the previous). */
|
| 186 |
|
|
if (vpret == NULL || vp->viewSubtreeLen > vpret->viewSubtreeLen ||
|
| 187 |
|
|
(vp->viewSubtreeLen == vpret->viewSubtreeLen &&
|
| 188 |
|
|
snmp_oid_compare(vp->viewSubtree, vp->viewSubtreeLen,
|
| 189 |
|
|
vpret->viewSubtree,
|
| 190 |
|
|
vpret->viewSubtreeLen) > 0))
|
| 191 |
|
|
vpret = vp;
|
| 192 |
|
|
}
|
| 193 |
|
|
}
|
| 194 |
|
|
}
|
| 195 |
|
|
DEBUGMSGTL(("vacm:getView", ", %s", (vpret)?"found":"none"));
|
| 196 |
|
|
return vpret;
|
| 197 |
|
|
}
|
| 198 |
|
|
|
| 199 |
|
|
void
|
| 200 |
|
|
vacm_scanViewInit (void)
|
| 201 |
|
|
{
|
| 202 |
|
|
viewScanPtr = viewList;
|
| 203 |
|
|
}
|
| 204 |
|
|
|
| 205 |
|
|
struct vacm_viewEntry *
|
| 206 |
|
|
vacm_scanViewNext (void)
|
| 207 |
|
|
{
|
| 208 |
|
|
struct vacm_viewEntry *returnval = viewScanPtr;
|
| 209 |
|
|
if (viewScanPtr) viewScanPtr = viewScanPtr->next;
|
| 210 |
|
|
return returnval;
|
| 211 |
|
|
}
|
| 212 |
|
|
|
| 213 |
|
|
struct vacm_viewEntry *
|
| 214 |
|
|
vacm_createViewEntry(const char *viewName,
|
| 215 |
|
|
oid *viewSubtree,
|
| 216 |
|
|
size_t viewSubtreeLen)
|
| 217 |
|
|
{
|
| 218 |
|
|
struct vacm_viewEntry *vp, *lp, *op = NULL;
|
| 219 |
|
|
int cmp, glen;
|
| 220 |
|
|
|
| 221 |
|
|
glen = (int)strlen(viewName);
|
| 222 |
|
|
if (glen < 0 || glen >= VACM_MAX_STRING)
|
| 223 |
|
|
return NULL;
|
| 224 |
|
|
vp = (struct vacm_viewEntry *)calloc(1, sizeof(struct vacm_viewEntry));
|
| 225 |
|
|
if (vp == NULL)
|
| 226 |
|
|
return NULL;
|
| 227 |
|
|
vp->reserved = (struct vacm_viewEntry *)calloc(1, sizeof(struct vacm_viewEntry));
|
| 228 |
|
|
if (vp->reserved == NULL) {
|
| 229 |
|
|
free(vp);
|
| 230 |
|
|
return NULL;
|
| 231 |
|
|
}
|
| 232 |
|
|
|
| 233 |
|
|
vp->viewName[0] = glen;
|
| 234 |
|
|
strcpy(vp->viewName+1, viewName);
|
| 235 |
|
|
memcpy(vp->viewSubtree, viewSubtree, viewSubtreeLen * sizeof(oid));
|
| 236 |
|
|
vp->viewSubtreeLen = viewSubtreeLen;
|
| 237 |
|
|
|
| 238 |
|
|
lp = viewList;
|
| 239 |
|
|
while (lp) {
|
| 240 |
|
|
cmp = memcmp(lp->viewName, vp->viewName, glen+1);
|
| 241 |
|
|
if (cmp > 0) break;
|
| 242 |
|
|
if (cmp < 0) goto next;
|
| 243 |
|
|
|
| 244 |
|
|
next:
|
| 245 |
|
|
op = lp;
|
| 246 |
|
|
lp = lp->next;
|
| 247 |
|
|
}
|
| 248 |
|
|
vp->next = lp;
|
| 249 |
|
|
if (op) op->next = vp;
|
| 250 |
|
|
else viewList = vp;
|
| 251 |
|
|
return vp;
|
| 252 |
|
|
}
|
| 253 |
|
|
|
| 254 |
|
|
void
|
| 255 |
|
|
vacm_destroyViewEntry(const char *viewName,
|
| 256 |
|
|
oid *viewSubtree,
|
| 257 |
|
|
size_t viewSubtreeLen)
|
| 258 |
|
|
{
|
| 259 |
|
|
struct vacm_viewEntry *vp, *lastvp = NULL;
|
| 260 |
|
|
|
| 261 |
|
|
if (viewList && !strcmp(viewList->viewName+1, viewName)
|
| 262 |
|
|
&& viewList->viewSubtreeLen == viewSubtreeLen
|
| 263 |
|
|
&& !memcmp((char *)viewList->viewSubtree, (char *)viewSubtree,
|
| 264 |
|
|
viewSubtreeLen * sizeof(oid))){
|
| 265 |
|
|
vp = viewList;
|
| 266 |
|
|
viewList = viewList->next;
|
| 267 |
|
|
} else {
|
| 268 |
|
|
for (vp = viewList; vp; vp = vp->next){
|
| 269 |
|
|
if (!strcmp(vp->viewName+1, viewName)
|
| 270 |
|
|
&& vp->viewSubtreeLen == viewSubtreeLen
|
| 271 |
|
|
&& !memcmp((char *)vp->viewSubtree, (char *)viewSubtree,
|
| 272 |
|
|
viewSubtreeLen * sizeof(oid)))
|
| 273 |
|
|
break;
|
| 274 |
|
|
lastvp = vp;
|
| 275 |
|
|
}
|
| 276 |
|
|
if (!vp)
|
| 277 |
|
|
return;
|
| 278 |
|
|
lastvp->next = vp->next;
|
| 279 |
|
|
}
|
| 280 |
|
|
if (vp->reserved)
|
| 281 |
|
|
free(vp->reserved);
|
| 282 |
|
|
free(vp);
|
| 283 |
|
|
return;
|
| 284 |
|
|
}
|
| 285 |
|
|
|
| 286 |
|
|
void vacm_destroyAllViewEntries (void)
|
| 287 |
|
|
{
|
| 288 |
|
|
struct vacm_viewEntry *vp;
|
| 289 |
|
|
while ((vp = viewList)) {
|
| 290 |
|
|
viewList = vp->next;
|
| 291 |
|
|
if (vp->reserved) free(vp->reserved);
|
| 292 |
|
|
free(vp);
|
| 293 |
|
|
}
|
| 294 |
|
|
}
|
| 295 |
|
|
|
| 296 |
|
|
struct vacm_groupEntry *
|
| 297 |
|
|
vacm_getGroupEntry(int securityModel,
|
| 298 |
|
|
const char *securityName)
|
| 299 |
|
|
{
|
| 300 |
|
|
struct vacm_groupEntry *vp;
|
| 301 |
|
|
char secname[VACMSTRINGLEN];
|
| 302 |
|
|
int glen;
|
| 303 |
|
|
|
| 304 |
|
|
glen = (int)strlen(securityName);
|
| 305 |
|
|
if (glen < 0 || glen >= VACM_MAX_STRING)
|
| 306 |
|
|
return NULL;
|
| 307 |
|
|
secname[0] = glen;
|
| 308 |
|
|
strcpy(secname+1, securityName);
|
| 309 |
|
|
|
| 310 |
|
|
for (vp = groupList; vp; vp = vp->next) {
|
| 311 |
|
|
if ((securityModel == vp->securityModel || vp->securityModel == SNMP_SEC_MODEL_ANY)
|
| 312 |
|
|
&& !memcmp(vp->securityName, secname,glen+1))
|
| 313 |
|
|
return vp;
|
| 314 |
|
|
}
|
| 315 |
|
|
return NULL;
|
| 316 |
|
|
}
|
| 317 |
|
|
|
| 318 |
|
|
void
|
| 319 |
|
|
vacm_scanGroupInit (void)
|
| 320 |
|
|
{
|
| 321 |
|
|
groupScanPtr = groupList;
|
| 322 |
|
|
}
|
| 323 |
|
|
|
| 324 |
|
|
struct vacm_groupEntry *
|
| 325 |
|
|
vacm_scanGroupNext (void)
|
| 326 |
|
|
{
|
| 327 |
|
|
struct vacm_groupEntry *returnval = groupScanPtr;
|
| 328 |
|
|
if (groupScanPtr) groupScanPtr = groupScanPtr->next;
|
| 329 |
|
|
return returnval;
|
| 330 |
|
|
}
|
| 331 |
|
|
|
| 332 |
|
|
struct vacm_groupEntry *
|
| 333 |
|
|
vacm_createGroupEntry(int securityModel,
|
| 334 |
|
|
const char *securityName)
|
| 335 |
|
|
{
|
| 336 |
|
|
struct vacm_groupEntry *gp, *lg, *og;
|
| 337 |
|
|
int cmp, glen;
|
| 338 |
|
|
|
| 339 |
|
|
glen = (int)strlen(securityName);
|
| 340 |
|
|
if (glen < 0 || glen >= VACM_MAX_STRING)
|
| 341 |
|
|
return NULL;
|
| 342 |
|
|
gp = (struct vacm_groupEntry *)calloc(1, sizeof(struct vacm_groupEntry));
|
| 343 |
|
|
if (gp == NULL)
|
| 344 |
|
|
return NULL;
|
| 345 |
|
|
gp->reserved = (struct vacm_groupEntry *)calloc(1, sizeof(struct vacm_groupEntry));
|
| 346 |
|
|
if (gp->reserved == NULL) {
|
| 347 |
|
|
free(gp);
|
| 348 |
|
|
return NULL;
|
| 349 |
|
|
}
|
| 350 |
|
|
|
| 351 |
|
|
gp->securityModel = securityModel;
|
| 352 |
|
|
gp->securityName[0] = glen;
|
| 353 |
|
|
strcpy(gp->securityName+1, securityName);
|
| 354 |
|
|
|
| 355 |
|
|
lg = groupList;
|
| 356 |
|
|
og = NULL;
|
| 357 |
|
|
while (lg) {
|
| 358 |
|
|
if (lg->securityModel > securityModel) break;
|
| 359 |
|
|
if (lg->securityModel == securityModel &&
|
| 360 |
|
|
(cmp = memcmp(lg->securityName, gp->securityName, glen+1)) > 0) break;
|
| 361 |
|
|
/* if (lg->securityModel == securityModel && cmp == 0) abort(); */
|
| 362 |
|
|
og = lg; lg = lg->next;
|
| 363 |
|
|
}
|
| 364 |
|
|
gp->next = lg;
|
| 365 |
|
|
if (og == NULL) groupList = gp;
|
| 366 |
|
|
else og->next = gp;
|
| 367 |
|
|
return gp;
|
| 368 |
|
|
}
|
| 369 |
|
|
|
| 370 |
|
|
void
|
| 371 |
|
|
vacm_destroyGroupEntry(int securityModel,
|
| 372 |
|
|
const char *securityName)
|
| 373 |
|
|
{
|
| 374 |
|
|
struct vacm_groupEntry *vp, *lastvp = NULL;
|
| 375 |
|
|
|
| 376 |
|
|
if (groupList && groupList->securityModel == securityModel
|
| 377 |
|
|
&& !strcmp(groupList->securityName+1, securityName)) {
|
| 378 |
|
|
vp = groupList;
|
| 379 |
|
|
groupList = groupList->next;
|
| 380 |
|
|
} else {
|
| 381 |
|
|
for (vp = groupList; vp; vp = vp->next){
|
| 382 |
|
|
if (vp->securityModel == securityModel
|
| 383 |
|
|
&& !strcmp(vp->securityName+1, securityName))
|
| 384 |
|
|
break;
|
| 385 |
|
|
lastvp = vp;
|
| 386 |
|
|
}
|
| 387 |
|
|
if (!vp)
|
| 388 |
|
|
return;
|
| 389 |
|
|
lastvp->next = vp->next;
|
| 390 |
|
|
}
|
| 391 |
|
|
if (vp->reserved)
|
| 392 |
|
|
free(vp->reserved);
|
| 393 |
|
|
free(vp);
|
| 394 |
|
|
return;
|
| 395 |
|
|
}
|
| 396 |
|
|
|
| 397 |
|
|
void vacm_destroyAllGroupEntries (void)
|
| 398 |
|
|
{
|
| 399 |
|
|
struct vacm_groupEntry *gp;
|
| 400 |
|
|
while ((gp = groupList)) {
|
| 401 |
|
|
groupList = gp->next;
|
| 402 |
|
|
if (gp->reserved) free(gp->reserved);
|
| 403 |
|
|
free(gp);
|
| 404 |
|
|
}
|
| 405 |
|
|
}
|
| 406 |
|
|
|
| 407 |
|
|
struct vacm_accessEntry *
|
| 408 |
|
|
vacm_getAccessEntry(const char *groupName,
|
| 409 |
|
|
const char *contextPrefix,
|
| 410 |
|
|
int securityModel,
|
| 411 |
|
|
int securityLevel)
|
| 412 |
|
|
{
|
| 413 |
|
|
struct vacm_accessEntry *vp;
|
| 414 |
|
|
char group[VACMSTRINGLEN];
|
| 415 |
|
|
char context[VACMSTRINGLEN];
|
| 416 |
|
|
int glen, clen;
|
| 417 |
|
|
|
| 418 |
|
|
glen = (int)strlen(groupName);
|
| 419 |
|
|
if (glen < 0 || glen >= VACM_MAX_STRING)
|
| 420 |
|
|
return NULL;
|
| 421 |
|
|
clen = (int)strlen(contextPrefix);
|
| 422 |
|
|
if (clen < 0 || clen >= VACM_MAX_STRING)
|
| 423 |
|
|
return NULL;
|
| 424 |
|
|
|
| 425 |
|
|
group[0] = glen;
|
| 426 |
|
|
strcpy(group+1, groupName);
|
| 427 |
|
|
context[0] = clen;
|
| 428 |
|
|
strcpy(context+1, contextPrefix);
|
| 429 |
|
|
for(vp = accessList; vp; vp = vp->next){
|
| 430 |
|
|
if ((securityModel == vp->securityModel || vp->securityModel == SNMP_SEC_MODEL_ANY)
|
| 431 |
|
|
&& securityLevel >= vp->securityLevel
|
| 432 |
|
|
&& !memcmp(vp->groupName, group, glen+1)
|
| 433 |
|
|
&& !memcmp(vp->contextPrefix, context, clen+1))
|
| 434 |
|
|
return vp;
|
| 435 |
|
|
}
|
| 436 |
|
|
return NULL;
|
| 437 |
|
|
}
|
| 438 |
|
|
|
| 439 |
|
|
void
|
| 440 |
|
|
vacm_scanAccessInit (void)
|
| 441 |
|
|
{
|
| 442 |
|
|
accessScanPtr = accessList;
|
| 443 |
|
|
}
|
| 444 |
|
|
|
| 445 |
|
|
struct vacm_accessEntry *
|
| 446 |
|
|
vacm_scanAccessNext (void)
|
| 447 |
|
|
{
|
| 448 |
|
|
struct vacm_accessEntry *returnval = accessScanPtr;
|
| 449 |
|
|
if (accessScanPtr) accessScanPtr = accessScanPtr->next;
|
| 450 |
|
|
return returnval;
|
| 451 |
|
|
}
|
| 452 |
|
|
|
| 453 |
|
|
struct vacm_accessEntry *
|
| 454 |
|
|
vacm_createAccessEntry(const char *groupName,
|
| 455 |
|
|
const char *contextPrefix,
|
| 456 |
|
|
int securityModel,
|
| 457 |
|
|
int securityLevel)
|
| 458 |
|
|
{
|
| 459 |
|
|
struct vacm_accessEntry *vp, *lp, *op = NULL;
|
| 460 |
|
|
int cmp, glen, clen;
|
| 461 |
|
|
|
| 462 |
|
|
glen = (int)strlen(groupName);
|
| 463 |
|
|
if (glen < 0 || glen >= VACM_MAX_STRING)
|
| 464 |
|
|
return NULL;
|
| 465 |
|
|
clen = (int)strlen(contextPrefix);
|
| 466 |
|
|
if (clen < 0 || clen >= VACM_MAX_STRING)
|
| 467 |
|
|
return NULL;
|
| 468 |
|
|
vp = (struct vacm_accessEntry *)calloc(1, sizeof(struct vacm_accessEntry));
|
| 469 |
|
|
if (vp == NULL)
|
| 470 |
|
|
return NULL;
|
| 471 |
|
|
vp->reserved = (struct vacm_accessEntry *)calloc(1, sizeof(struct vacm_accessEntry));
|
| 472 |
|
|
if (vp->reserved == NULL) {
|
| 473 |
|
|
free(vp);
|
| 474 |
|
|
return NULL;
|
| 475 |
|
|
}
|
| 476 |
|
|
|
| 477 |
|
|
vp->securityModel = securityModel;
|
| 478 |
|
|
vp->securityLevel = securityLevel;
|
| 479 |
|
|
vp->groupName[0] = glen;
|
| 480 |
|
|
strcpy(vp->groupName+1, groupName);
|
| 481 |
|
|
vp->contextPrefix[0] = clen;
|
| 482 |
|
|
strcpy(vp->contextPrefix+1, contextPrefix);
|
| 483 |
|
|
|
| 484 |
|
|
lp = accessList;
|
| 485 |
|
|
while (lp) {
|
| 486 |
|
|
cmp = memcmp(lp->groupName, vp->groupName, glen+1);
|
| 487 |
|
|
if (cmp > 0) break;
|
| 488 |
|
|
if (cmp < 0) goto next;
|
| 489 |
|
|
cmp = memcmp(lp->contextPrefix, vp->contextPrefix, clen+1);
|
| 490 |
|
|
if (cmp > 0) break;
|
| 491 |
|
|
if (cmp < 0) goto next;
|
| 492 |
|
|
if (lp->securityModel > securityModel) break;
|
| 493 |
|
|
if (lp->securityModel < securityModel) goto next;
|
| 494 |
|
|
if (lp->securityLevel > securityLevel) break;
|
| 495 |
|
|
next:
|
| 496 |
|
|
op = lp;
|
| 497 |
|
|
lp = lp->next;
|
| 498 |
|
|
}
|
| 499 |
|
|
vp->next = lp;
|
| 500 |
|
|
if (op == NULL) accessList = vp;
|
| 501 |
|
|
else op->next = vp;
|
| 502 |
|
|
return vp;
|
| 503 |
|
|
}
|
| 504 |
|
|
|
| 505 |
|
|
void
|
| 506 |
|
|
vacm_destroyAccessEntry(const char *groupName,
|
| 507 |
|
|
const char *contextPrefix,
|
| 508 |
|
|
int securityModel,
|
| 509 |
|
|
int securityLevel)
|
| 510 |
|
|
{
|
| 511 |
|
|
struct vacm_accessEntry *vp, *lastvp = NULL;
|
| 512 |
|
|
|
| 513 |
|
|
if (accessList && accessList->securityModel == securityModel
|
| 514 |
|
|
&& accessList->securityModel == securityModel
|
| 515 |
|
|
&& !strcmp(accessList->groupName+1, groupName)
|
| 516 |
|
|
&& !strcmp(accessList->contextPrefix+1, contextPrefix)) {
|
| 517 |
|
|
vp = accessList;
|
| 518 |
|
|
accessList = accessList->next;
|
| 519 |
|
|
} else {
|
| 520 |
|
|
for (vp = accessList; vp; vp = vp->next){
|
| 521 |
|
|
if (vp->securityModel == securityModel
|
| 522 |
|
|
&& vp->securityLevel == securityLevel
|
| 523 |
|
|
&& !strcmp(vp->groupName+1, groupName)
|
| 524 |
|
|
&& !strcmp(vp->contextPrefix+1, contextPrefix))
|
| 525 |
|
|
break;
|
| 526 |
|
|
lastvp = vp;
|
| 527 |
|
|
}
|
| 528 |
|
|
if (!vp)
|
| 529 |
|
|
return;
|
| 530 |
|
|
lastvp->next = vp->next;
|
| 531 |
|
|
}
|
| 532 |
|
|
if (vp->reserved)
|
| 533 |
|
|
free(vp->reserved);
|
| 534 |
|
|
free(vp);
|
| 535 |
|
|
return;
|
| 536 |
|
|
}
|
| 537 |
|
|
|
| 538 |
|
|
void vacm_destroyAllAccessEntries (void)
|
| 539 |
|
|
{
|
| 540 |
|
|
struct vacm_accessEntry *ap;
|
| 541 |
|
|
while ((ap = accessList)) {
|
| 542 |
|
|
accessList = ap->next;
|
| 543 |
|
|
if (ap->reserved) free(ap->reserved);
|
| 544 |
|
|
free(ap);
|
| 545 |
|
|
}
|
| 546 |
|
|
}
|
| 547 |
|
|
|
| 548 |
|
|
/* returns 1 if vacm has *any* configuration entries in it (regardless
|
| 549 |
|
|
of weather or not there is enough to make a decision based on it),
|
| 550 |
|
|
else return 0 */
|
| 551 |
|
|
int vacm_is_configured(void) {
|
| 552 |
|
|
if (viewList == NULL && accessList == NULL && groupList == NULL)
|
| 553 |
|
|
return 0;
|
| 554 |
|
|
return 1;
|
| 555 |
|
|
}
|
| 556 |
|
|
|
| 557 |
|
|
#endif /* CYGPKG_SNMPAGENT_V3_SUPPORT */
|