| 1 |
/* |
| 2 |
* TargetParamTable data storage |
| 3 |
* |
| 4 |
* This file was created to separate data storage from the MIB implementation |
| 5 |
* |
| 6 |
* Portions of this file are copyrighted by: |
| 7 |
* Copyright (c) 2016 VMware, Inc. All rights reserved. |
| 8 |
* Use is subject to license terms specified in the COPYING file |
| 9 |
* distributed with the Net-SNMP package. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <net-snmp/net-snmp-config.h> |
| 13 |
#ifdef HAVE_STRING_H |
| 14 |
#include <string.h> |
| 15 |
#else |
| 16 |
#include <strings.h> |
| 17 |
#endif |
| 18 |
#include <stdlib.h> |
| 19 |
#include <ctype.h> |
| 20 |
|
| 21 |
#include <net-snmp/net-snmp-includes.h> |
| 22 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 23 |
|
| 24 |
#include "snmpTargetParamsEntry_data.h" |
| 25 |
|
| 26 |
netsnmp_feature_require(container_compare_mem); |
| 27 |
|
| 28 |
static struct targetParamTable_struct *aPTable = NULL; |
| 29 |
static int _active = 0; |
| 30 |
|
| 31 |
/* |
| 32 |
* Utility routines |
| 33 |
*/ |
| 34 |
static int |
| 35 |
store_snmpTargetParamsEntry(int majorID, int minorID, void *serverarg, |
| 36 |
void *clientarg); |
| 37 |
|
| 38 |
/* |
| 39 |
* TargetParamTable_create creates and returns a pointer |
| 40 |
* to a targetParamTable_struct with default values set |
| 41 |
*/ |
| 42 |
struct targetParamTable_struct |
| 43 |
* |
| 44 |
snmpTargetParamTable_create(void) |
| 45 |
{ |
| 46 |
struct targetParamTable_struct *newEntry; |
| 47 |
|
| 48 |
newEntry = (struct targetParamTable_struct *) |
| 49 |
calloc(1, sizeof(struct targetParamTable_struct)); |
| 50 |
|
| 51 |
if (NULL == newEntry) |
| 52 |
return NULL; |
| 53 |
|
| 54 |
++_active; |
| 55 |
|
| 56 |
newEntry->mpModel = -1; |
| 57 |
|
| 58 |
newEntry->secModel = -1; |
| 59 |
newEntry->secLevel = -1; |
| 60 |
|
| 61 |
newEntry->storageType = SNMP_STORAGE_NONVOLATILE; |
| 62 |
newEntry->rowStatus = SNMP_ROW_NONEXISTENT; |
| 63 |
|
| 64 |
return newEntry; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
/* |
| 69 |
* TargetParamTable_dispose frees the space allocated to a |
| 70 |
* targetParamTable_struct |
| 71 |
*/ |
| 72 |
void |
| 73 |
snmpTargetParamTable_dispose(struct targetParamTable_struct *reaped) |
| 74 |
{ |
| 75 |
SNMP_FREE(reaped->paramNameData); |
| 76 |
SNMP_FREE(reaped->secNameData); |
| 77 |
|
| 78 |
free(reaped); |
| 79 |
--_active; |
| 80 |
} /* snmpTargetParamTable_dispose */ |
| 81 |
|
| 82 |
|
| 83 |
/* |
| 84 |
* snmpTargetParamTable_addToList adds a targetParamTable_struct |
| 85 |
* to a list passed in. The list is assumed to be in a sorted order, |
| 86 |
* low to high and this procedure inserts a new struct in the proper |
| 87 |
* location. Sorting uses OID values based on paramName. A new equal value |
| 88 |
* overwrites a current one. |
| 89 |
*/ |
| 90 |
void |
| 91 |
snmpTargetParamTable_addToList(struct targetParamTable_struct *newEntry, |
| 92 |
struct targetParamTable_struct **listPtr) |
| 93 |
{ |
| 94 |
static struct targetParamTable_struct *curr_struct, *prev_struct; |
| 95 |
int i; |
| 96 |
|
| 97 |
snmp_store_needed(NULL); |
| 98 |
|
| 99 |
/* |
| 100 |
* if the list is empty, add the new entry to the top |
| 101 |
*/ |
| 102 |
if ((prev_struct = curr_struct = *listPtr) == NULL) { |
| 103 |
*listPtr = newEntry; |
| 104 |
return; |
| 105 |
} else { |
| 106 |
/* |
| 107 |
* search through the list for an equal or greater OID value |
| 108 |
*/ |
| 109 |
while (curr_struct != NULL) { |
| 110 |
i = netsnmp_compare_mem(newEntry->paramNameData, |
| 111 |
newEntry->paramNameLen, |
| 112 |
curr_struct->paramNameData, |
| 113 |
curr_struct->paramNameLen); |
| 114 |
if (i == 0) { /* Exact match, overwrite with new struct */ |
| 115 |
newEntry->next = curr_struct->next; |
| 116 |
/* |
| 117 |
* if curr_struct is the top of the list |
| 118 |
*/ |
| 119 |
if (*listPtr == curr_struct) |
| 120 |
*listPtr = newEntry; |
| 121 |
else |
| 122 |
prev_struct->next = newEntry; |
| 123 |
snmpTargetParamTable_dispose(curr_struct); |
| 124 |
return; |
| 125 |
} else if (i < 0) { /* Found a greater OID, insert struct in front of it. */ |
| 126 |
newEntry->next = curr_struct; |
| 127 |
/* |
| 128 |
* if curr_struct is the top of the list |
| 129 |
*/ |
| 130 |
if (*listPtr == curr_struct) |
| 131 |
*listPtr = newEntry; |
| 132 |
else |
| 133 |
prev_struct->next = newEntry; |
| 134 |
return; |
| 135 |
} |
| 136 |
prev_struct = curr_struct; |
| 137 |
curr_struct = curr_struct->next; |
| 138 |
} |
| 139 |
} |
| 140 |
/* |
| 141 |
* if we're here, no larger OID was ever found, insert on end of list |
| 142 |
*/ |
| 143 |
prev_struct->next = newEntry; |
| 144 |
} /* snmpTargeParamTable_addToList */ |
| 145 |
|
| 146 |
void |
| 147 |
snmpTargetParamTable_add(struct targetParamTable_struct *newEntry) |
| 148 |
{ |
| 149 |
snmpTargetParamTable_addToList(newEntry, &aPTable); |
| 150 |
} |
| 151 |
|
| 152 |
/* |
| 153 |
* snmpTargetParamTable_remFromList removes a targetParamTable_struct |
| 154 |
* from the list passed in |
| 155 |
*/ |
| 156 |
void |
| 157 |
snmpTargetParamTable_remFromList(struct targetParamTable_struct *oldEntry, |
| 158 |
struct targetParamTable_struct **listPtr) |
| 159 |
{ |
| 160 |
struct targetParamTable_struct *tptr; |
| 161 |
|
| 162 |
if ((tptr = *listPtr) == NULL) |
| 163 |
return; |
| 164 |
else if (tptr == oldEntry) { |
| 165 |
*listPtr = (*listPtr)->next; |
| 166 |
snmpTargetParamTable_dispose(tptr); |
| 167 |
snmp_store_needed(NULL); |
| 168 |
} else { |
| 169 |
while (tptr->next != NULL) { |
| 170 |
if (tptr->next == oldEntry) { |
| 171 |
tptr->next = tptr->next->next; |
| 172 |
snmpTargetParamTable_dispose(oldEntry); |
| 173 |
snmp_store_needed(NULL); |
| 174 |
break; |
| 175 |
} |
| 176 |
tptr = tptr->next; |
| 177 |
} |
| 178 |
} |
| 179 |
} /* snmpTargetParamTable_remFromList */ |
| 180 |
|
| 181 |
void |
| 182 |
snmpTargetParamTable_remove(struct targetParamTable_struct *entry) |
| 183 |
{ |
| 184 |
snmpTargetParamTable_remFromList(entry, &aPTable); |
| 185 |
} |
| 186 |
|
| 187 |
/* |
| 188 |
* lookup OID in the link list of Table Entries |
| 189 |
*/ |
| 190 |
struct targetParamTable_struct * |
| 191 |
search_snmpTargetParamsTable(oid * baseName, |
| 192 |
size_t baseNameLen, |
| 193 |
oid * name, size_t * length, int exact) |
| 194 |
{ |
| 195 |
static struct targetParamTable_struct *temp_struct; |
| 196 |
int i; |
| 197 |
size_t myOIDLen = 0; |
| 198 |
oid newNum[128]; |
| 199 |
|
| 200 |
/* |
| 201 |
* lookup entry in p / * Get Current MIB ID |
| 202 |
*/ |
| 203 |
memcpy(newNum, baseName, baseNameLen * sizeof(oid)); |
| 204 |
|
| 205 |
for (temp_struct = aPTable; temp_struct != NULL; |
| 206 |
temp_struct = temp_struct->next) { |
| 207 |
for (i = 0; i < temp_struct->paramNameLen; i++) { |
| 208 |
newNum[baseNameLen + i] = temp_struct->paramNameData[i]; |
| 209 |
} |
| 210 |
myOIDLen = baseNameLen + temp_struct->paramNameLen; |
| 211 |
i = snmp_oid_compare(name, *length, newNum, myOIDLen); |
| 212 |
/* |
| 213 |
* Assumes that the linked list sorted by OID, low to high |
| 214 |
*/ |
| 215 |
if ((i == 0 && exact != 0) || (i < 0 && exact == 0)) { |
| 216 |
if (exact == 0) { |
| 217 |
memcpy(name, newNum, myOIDLen * sizeof(oid)); |
| 218 |
*length = myOIDLen; |
| 219 |
} |
| 220 |
return temp_struct; |
| 221 |
} |
| 222 |
} |
| 223 |
return NULL; |
| 224 |
} /* search_snmpTargetParamsTable */ |
| 225 |
|
| 226 |
|
| 227 |
struct targetParamTable_struct * |
| 228 |
get_paramEntry2(const char *name, size_t nameLen) |
| 229 |
{ |
| 230 |
static struct targetParamTable_struct *ptr; |
| 231 |
for (ptr = aPTable; ptr; ptr = ptr->next) { |
| 232 |
if (nameLen == ptr->paramNameLen && |
| 233 |
memcmp(ptr->paramNameData, name, nameLen) == 0) { |
| 234 |
return ptr; |
| 235 |
} |
| 236 |
} |
| 237 |
return NULL; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
/* |
| 242 |
* initialization routines |
| 243 |
*/ |
| 244 |
|
| 245 |
void |
| 246 |
init_snmpTargetParamsEntry_data(void) |
| 247 |
{ |
| 248 |
static int done = 0; |
| 249 |
|
| 250 |
if (++done != 1) |
| 251 |
return; |
| 252 |
|
| 253 |
/* |
| 254 |
* we need to be called back later |
| 255 |
*/ |
| 256 |
snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA, |
| 257 |
store_snmpTargetParamsEntry, NULL); |
| 258 |
|
| 259 |
} /* init_snmpTargetParmsEntry */ |
| 260 |
|
| 261 |
|
| 262 |
/* |
| 263 |
* shutdown routines |
| 264 |
*/ |
| 265 |
|
| 266 |
void |
| 267 |
shutdown_snmpTargetParamsEntry_data(void) |
| 268 |
{ |
| 269 |
DEBUGMSGTL(("trap:targetParam:shutdown", "clearing %d object(s)\n", |
| 270 |
_active)); |
| 271 |
|
| 272 |
while (aPTable) |
| 273 |
snmpTargetParamTable_remFromList(aPTable, &aPTable); |
| 274 |
|
| 275 |
DEBUGMSGTL(("trap:targetParam:shutdown", "active count %d\n", _active)); |
| 276 |
if (_active != 0) { |
| 277 |
DEBUGMSGTL(("trap:targetParam:shutdown", |
| 278 |
"unexpected count %d after cleanup!\n", _active)); |
| 279 |
snmp_log(LOG_WARNING, "targetAddr count %d, not 0, after shutdown.\n", |
| 280 |
_active); |
| 281 |
} |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
/* |
| 286 |
* store_snmpTargetParamsEntry handles the presistent storage proccess |
| 287 |
* for this MIB table. It writes out all the non-volatile rows |
| 288 |
* to permanent storage on a shutdown |
| 289 |
*/ |
| 290 |
static int |
| 291 |
store_snmpTargetParamsEntry(int majorID, int minorID, void *serverarg, |
| 292 |
void *clientarg) |
| 293 |
{ |
| 294 |
struct targetParamTable_struct *curr_struct; |
| 295 |
char line[1024]; |
| 296 |
|
| 297 |
strcpy(line, ""); |
| 298 |
if ((curr_struct = aPTable) != NULL) { |
| 299 |
while (curr_struct != NULL) { |
| 300 |
if ((curr_struct->storageType == SNMP_STORAGE_NONVOLATILE || |
| 301 |
curr_struct->storageType == SNMP_STORAGE_PERMANENT) |
| 302 |
&& |
| 303 |
(curr_struct->rowStatus == SNMP_ROW_ACTIVE || |
| 304 |
curr_struct->rowStatus == SNMP_ROW_NOTINSERVICE)) { |
| 305 |
snprintf(line, sizeof(line), |
| 306 |
"targetParams %s %i %i %s %i %i %i\n", |
| 307 |
curr_struct->paramNameData, curr_struct->mpModel, |
| 308 |
curr_struct->secModel, curr_struct->secNameData, |
| 309 |
curr_struct->secLevel, curr_struct->storageType, |
| 310 |
curr_struct->rowStatus); |
| 311 |
line[ sizeof(line)-1 ] = 0; |
| 312 |
|
| 313 |
/* |
| 314 |
* store to file |
| 315 |
*/ |
| 316 |
snmpd_store_config(line); |
| 317 |
} |
| 318 |
curr_struct = curr_struct->next; |
| 319 |
} |
| 320 |
} |
| 321 |
return SNMPERR_SUCCESS; |
| 322 |
} /* store_snmpTargetParmsEntry */ |
| 323 |
|