Debian Codegraph @

agent/mibgroup/target/snmpTargetAddrEntry_data.c

raw
1 /*
2 * snmpTargetAddrEntry MIB
3 *
4 * This file was created to separate notification data storage from
5 * the MIB implementation.
6 *
7 * Portions of this file are copyrighted by:
8 * Copyright (c) 2016 VMware, Inc. All rights reserved.
9 * Use is subject to license terms specified in the COPYING file
10 * distributed with the Net-SNMP package.
11 */
12
13 #include <net-snmp/net-snmp-config.h>
14 #ifdef HAVE_STRING_H
15 #include <string.h>
16 #else
17 #include <strings.h>
18 #endif
19 #include <stdlib.h>
20 #include <ctype.h>
21
22 #include <net-snmp/net-snmp-includes.h>
23 #include <net-snmp/agent/net-snmp-agent-includes.h>
24
25 #include "snmpTargetAddrEntry_data.h"
26 #include "util_funcs/header_generic.h"
27
28 netsnmp_feature_require(container_compare_mem);
29
30 static struct targetAddrTable_struct *aAddrTable = NULL;
31 static int _active = 0;
32
33 /*
34 * Utility routines
35 */
36 static int store_snmpTargetAddrEntry(int majorID, int minorID, void *serverarg,
37 void *clientarg);
38 static void snmpd_parse_config_targetAddr(const char *token, char *char_ptr);
39
40 struct targetAddrTable_struct *
41 get_addrTable(void)
42 {
43 return aAddrTable;
44 }
45
46 struct targetAddrTable_struct *
47 get_addrForName2(const char *name, size_t nameLen)
48 {
49 struct targetAddrTable_struct *ptr;
50 for (ptr = aAddrTable; ptr != NULL; ptr = ptr->next) {
51 if (ptr->nameLen == nameLen && ptr->nameData &&
52 memcmp(ptr->nameData, name, nameLen) == 0)
53 return ptr;
54 }
55 return NULL;
56 }
57
58
59 /*
60 * TargetAddrTable_create creates and returns a pointer
61 * to a targetAddrTable_struct with default values set
62 */
63 struct targetAddrTable_struct *
64 snmpTargetAddrTable_create(void)
65 {
66 struct targetAddrTable_struct *newEntry;
67
68 newEntry = (struct targetAddrTable_struct *)
69 calloc(1, sizeof(struct targetAddrTable_struct));
70
71 if (newEntry) {
72 ++_active;
73 newEntry->timeout = 1500;
74 newEntry->retryCount = 3;
75
76 newEntry->tagListData = strdup("");
77 newEntry->tagListLen = 0;
78
79 newEntry->storageType = SNMP_STORAGE_NONVOLATILE;
80 newEntry->rowStatus = SNMP_ROW_NONEXISTENT;
81
82 newEntry->close_sess = 1;
83 }
84
85 return newEntry;
86 } /* snmpTargetAddrTable_create */
87
88
89 /*
90 * TargetAddrTable_dispose frees the space allocated to a
91 * targetAddrTable_struct
92 */
93 void
94 snmpTargetAddrTable_dispose(struct targetAddrTable_struct *reaped)
95 {
96 if (NULL == reaped)
97 return;
98
99 if (reaped->sess && reaped->close_sess)
100 snmp_close(reaped->sess);
101 SNMP_FREE(reaped->tAddress);
102 SNMP_FREE(reaped->nameData);
103 SNMP_FREE(reaped->tagListData);
104 SNMP_FREE(reaped->paramsData);
105
106 SNMP_FREE(reaped);
107 --_active;
108 } /* snmpTargetAddrTable_dispose */
109
110 /*
111 * snmpTargetAddrTable_addToList adds a targetAddrTable_struct
112 * to a list passed in. The list is assumed to be in a sorted order,
113 * low to high and this procedure inserts a new struct in the proper
114 * location. Sorting uses OID values based on name. A new equal value
115 * overwrites a current one.
116 */
117 void
118 snmpTargetAddrTable_addToList(struct targetAddrTable_struct *newEntry,
119 struct targetAddrTable_struct **listPtr)
120 {
121 static struct targetAddrTable_struct *curr_struct, *prev_struct;
122 int i;
123
124 /*
125 * if the list is empty, add the new entry to the top
126 */
127 if ((prev_struct = curr_struct = *listPtr) == NULL) {
128 *listPtr = newEntry;
129 return;
130 } else {
131 /*
132 * search through the list for an equal or greater OID value
133 */
134 while (curr_struct != NULL) {
135 i = netsnmp_compare_mem(newEntry->nameData,
136 newEntry->nameLen,
137 curr_struct->nameData,
138 curr_struct->nameLen);
139 if (i == 0) { /* Exact match, overwrite with new struct */
140 newEntry->next = curr_struct->next;
141 /*
142 * if curr_struct is the top of the list
143 */
144 if (*listPtr == curr_struct)
145 *listPtr = newEntry;
146 else
147 prev_struct->next = newEntry;
148 snmpTargetAddrTable_dispose(curr_struct);
149 return;
150 } else if (i < 0) { /* Found a greater OID, insert struct in front of it. */
151 newEntry->next = curr_struct;
152 /*
153 * if curr_struct is the top of the list
154 */
155 if (*listPtr == curr_struct)
156 *listPtr = newEntry;
157 else
158 prev_struct->next = newEntry;
159 return;
160 }
161 prev_struct = curr_struct;
162 curr_struct = curr_struct->next;
163 }
164 }
165 /*
166 * if we're here, no larger OID was ever found, insert on end of list
167 */
168 prev_struct->next = newEntry;
169 } /* snmpTargeAddrTable_addToList */
170
171
172 void
173 snmpTargetAddrTable_add(struct targetAddrTable_struct *newEntry)
174 {
175 snmpTargetAddrTable_addToList(newEntry, &aAddrTable);
176 }
177
178
179 /*
180 * snmpTargetAddrTable_remFromList removes a targetAddrTable_struct
181 * from the list passed in
182 */
183 void
184 snmpTargetAddrTable_remFromList(struct targetAddrTable_struct *oldEntry,
185 struct targetAddrTable_struct **listPtr)
186 {
187 struct targetAddrTable_struct *tptr;
188
189 if ((tptr = *listPtr) == NULL)
190 return;
191 else if (tptr == oldEntry) {
192 *listPtr = (*listPtr)->next;
193 snmpTargetAddrTable_dispose(tptr);
194 return;
195 } else {
196 while (tptr->next != NULL) {
197 if (tptr->next == oldEntry) {
198 tptr->next = tptr->next->next;
199 snmpTargetAddrTable_dispose(oldEntry);
200 return;
201 }
202 tptr = tptr->next;
203 }
204 }
205 } /* snmpTargetAddrTable_remFromList */
206
207 void
208 snmpTargetAddrTable_remove(struct targetAddrTable_struct *entry)
209 {
210 snmpTargetAddrTable_remFromList(entry, &aAddrTable);
211 }
212
213 /*
214 * lookup OID in the link list of Addr Table Entries
215 */
216 struct targetAddrTable_struct *
217 search_snmpTargetAddrTable(oid * baseName,
218 size_t baseNameLen,
219 oid * name, size_t * length, int exact)
220 {
221 static struct targetAddrTable_struct *temp_struct;
222 int i;
223 size_t myOIDLen = 0;
224 oid newNum[128];
225
226 /*
227 * lookup entry in addrTable linked list, Get Current MIB ID
228 */
229 memcpy(newNum, baseName, baseNameLen * sizeof(oid));
230
231 for (temp_struct = aAddrTable; temp_struct != NULL;
232 temp_struct = temp_struct->next) {
233 for (i = 0; i < temp_struct->nameLen; i++) {
234 newNum[baseNameLen + i] = temp_struct->nameData[i];
235 }
236 myOIDLen = baseNameLen + i;
237 i = snmp_oid_compare(name, *length, newNum, myOIDLen);
238 /*
239 * Assumes that the linked list sorted by OID, low to high
240 */
241 if ((i == 0 && exact != 0) || (i < 0 && exact == 0)) {
242 if (exact == 0) {
243 memcpy(name, newNum, myOIDLen * sizeof(oid));
244 *length = myOIDLen;
245 }
246 return temp_struct;
247 }
248 }
249 return NULL;
250 } /* search_snmpTargetAddrTable */
251
252
253 /*
254 * Init routines
255 */
256
257 void
258 init_snmpTargetAddrEntry_data(void)
259 {
260 static int done = 0;
261
262 if (++done != 1)
263 return;
264
265 snmpd_register_config_handler("targetAddr",
266 snmpd_parse_config_targetAddr,
267 NULL, NULL);
268
269 /*
270 * we need to be called back later
271 */
272 snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
273 store_snmpTargetAddrEntry, NULL);
274
275 } /* init_snmpTargetAddrEntry */
276
277
278 /*
279 * Shutdown routines
280 */
281
282 void
283 shutdown_snmpTargetAddrEntry_data(void)
284 {
285 struct targetAddrTable_struct *ptr;
286 struct targetAddrTable_struct *next;
287
288 snmp_unregister_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
289 store_snmpTargetAddrEntry, NULL, FALSE);
290
291 DEBUGMSGTL(("trap:targetAddr:shutdown", "clearing %d object(s)\n",
292 _active));
293 for (ptr = aAddrTable; ptr; ptr = next) {
294 next = ptr->next;
295 snmpTargetAddrTable_dispose(ptr);
296 }
297 aAddrTable = NULL;
298
299 DEBUGMSGTL(("trap:targetAddr:shutdown", "active count %d\n",_active));
300 if (_active != 0) {
301 DEBUGMSGTL(("trap:targetAddr:shutdown",
302 "unexpected count %d after cleanup!\n", _active));
303 snmp_log(LOG_WARNING, "targetAddr count %d, not 0, after shutdown.\n",
304 _active);
305 }
306 }
307
308 /*
309 * store_snmpTargetAddrEntry handles the persistent storage proccess
310 * for this MIB table. It writes out all the non-volatile rows
311 * to permanent storage on a shutdown
312 */
313 static int
314 store_snmpTargetAddrEntry(int majorID, int minorID, void *serverarg,
315 void *clientarg)
316 {
317 const struct targetAddrTable_struct *curr_struct;
318 char line[1024], *ep = line + sizeof(line);
319 int i;
320
321 curr_struct = aAddrTable;
322 while (curr_struct != NULL) {
323 if ((curr_struct->storageType == SNMP_STORAGE_NONVOLATILE ||
324 curr_struct->storageType == SNMP_STORAGE_PERMANENT) &&
325 (curr_struct->rowStatus == SNMP_ROW_ACTIVE ||
326 curr_struct->rowStatus == SNMP_ROW_NOTINSERVICE)) {
327 char *cur;
328
329 cur = line + snprintf(line, sizeof(line), "targetAddr ");
330 cur = read_config_save_octet_string(
331 cur, (const u_char*)curr_struct->nameData,
332 curr_struct->nameLen);
333 *cur++ = ' ';
334 for (i = 0; i < curr_struct->tDomainLen; i++) {
335 cur += snprintf(cur, ep - cur, ".%i",
336 (int) curr_struct->tDomain[i]);
337 }
338 *cur++ = ' ';
339 cur = read_config_save_octet_string(
340 cur, curr_struct->tAddress, curr_struct->tAddressLen);
341 snprintf(cur, ep - cur, " %i %i \"%s\" %s %i %i",
342 curr_struct->timeout,
343 curr_struct->retryCount, curr_struct->tagListData,
344 curr_struct->paramsData, curr_struct->storageType,
345 curr_struct->rowStatus);
346
347 /*
348 * store to file
349 */
350 snmpd_store_config(line);
351 }
352
353 curr_struct = curr_struct->next;
354 }
355
356 return SNMPERR_SUCCESS;
357 } /* store_snmpTargetAddrEntry */
358
359 static int
360 snmpTargetAddr_addTDomain(struct targetAddrTable_struct *entry, char *cptr)
361 {
362 size_t len = 128;
363
364 if (cptr == NULL) {
365 DEBUGMSGTL(("snmpTargetAddrEntry",
366 "ERROR snmpTargetAddrEntry: no tDomain in config string\n"));
367 return (0);
368 }
369
370 if (!read_objid(cptr, entry->tDomain, &len)) {
371 DEBUGMSGTL(("snmpTargetAddrEntry",
372 "ERROR snmpTargetAddrEntry: tDomain unreadable in config string\n"));
373 return (0);
374 }
375
376 /*
377 * spec check for oid 1-128
378 */
379 if (len < 1 || len > 128) {
380 DEBUGMSGTL(("snmpTargetAddrEntry",
381 "ERROR snmpTargetAddrEntry: tDomain out of range in config string\n"));
382 return (0);
383 }
384
385 entry->tDomainLen = len;
386 return (1);
387 } /* snmpTargetAddr_addTDomain */
388
389
390 static int
391 snmpTargetAddr_addTimeout(struct targetAddrTable_struct *entry, char *cptr)
392 {
393 if (cptr == NULL) {
394 DEBUGMSGTL(("snmpTargetAddrEntry",
395 "ERROR snmpTargetParamsEntry: no Timeout in config string\n"));
396 return (0);
397 } else if (!(isdigit((unsigned char)(*cptr)))) {
398 DEBUGMSGTL(("snmpTargetAddrEntry",
399 "ERROR snmpTargeParamsEntry: Timeout is not a digit in config string\n"));
400 return (0);
401 }
402 /*
403 * check Timeout >= 0
404 */
405 else if ((entry->timeout = (int) strtol(cptr, (char **) NULL, 0)) < 0) {
406 DEBUGMSGTL(("snmpTargetAddrEntry",
407 "ERROR snmpTargeParamsEntry: Timeout out of range in config string\n"));
408 return (0);
409 }
410 return (1);
411 } /* snmpTargetAddr_addTimeout */
412
413
414 static int
415 snmpTargetAddr_addRetryCount(struct targetAddrTable_struct *entry,
416 char *cptr)
417 {
418 if (cptr == NULL) {
419 DEBUGMSGTL(("snmpTargetAddrEntry",
420 "ERROR snmpTargetParamsEntry: no Retry Count in config string\n"));
421 return (0);
422 } else if (!(isdigit((unsigned char)(*cptr)))) {
423 DEBUGMSGTL(("snmpTargetAddrEntry",
424 "ERROR snmpTargeParamsEntry: Retry Count is not a digit in config string\n"));
425 return (0);
426 }
427 /*
428 * spec check 0..255
429 */
430 else {
431 entry->retryCount = (int) strtol(cptr, (char **) NULL, 0);
432 if ((entry->retryCount < 0) || (entry->retryCount > 255)) {
433 DEBUGMSGTL(("snmpTargetAddrEntry",
434 "ERROR snmpTargeParamsEntry: Retry Count is out of range in config string\n"));
435 return (0);
436 }
437 }
438 return (1);
439 } /* snmpTargetAddr_addRetryCount */
440
441
442 static int
443 snmpTargetAddr_addTagList(struct targetAddrTable_struct *entry, char *cptr)
444 {
445 if (cptr == NULL) {
446 DEBUGMSGTL(("snmpTargetAddrEntry",
447 "ERROR snmpTargetAddrEntry: no tag list in config string\n"));
448 return (0);
449 } else {
450 size_t len = strlen(cptr);
451 /*
452 * spec check for string 0-255
453 */
454 if (len > 255) {
455 DEBUGMSGTL(("snmpTargetAddrEntry",
456 "ERROR snmpTargetAddrEntry: tag list out of range in config string\n"));
457 return (0);
458 }
459 SNMP_FREE(entry->tagListData);
460 entry->tagListData = strdup(cptr);
461 entry->tagListLen = strlen(cptr);
462 }
463 return (1);
464 } /* snmpTargetAddr_addTagList */
465
466
467 static int
468 snmpTargetAddr_addParams(struct targetAddrTable_struct *entry, char *cptr)
469 {
470 size_t len;
471 if (cptr == NULL) {
472 DEBUGMSGTL(("snmpTargetAddrEntry",
473 "ERROR snmpTargetAddrEntry: no params in config string\n"));
474 return (0);
475 } else {
476 len = strlen(cptr);
477 /*
478 * spec check for string 1-32
479 */
480 if (len < 1 || len > 32) {
481 DEBUGMSGTL(("snmpTargetAddrEntry",
482 "ERROR snmpTargetAddrEntry: params out of range in config string\n"));
483 return (0);
484 }
485 entry->paramsData = strdup(cptr);
486 entry->paramsLen = strlen(cptr);
487 }
488 return (1);
489 } /* snmpTargetAddr_addParams */
490
491
492 static int
493 snmpTargetAddr_addStorageType(struct targetAddrTable_struct *entry,
494 char *cptr)
495 {
496 if (cptr == NULL) {
497 DEBUGMSGTL(("snmpTargetAddrEntry",
498 "ERROR snmpTargetAddrEntry: no storage type in config "
499 "string\n"));
500 return (0);
501 } else if (!(isdigit((unsigned char)(*cptr)))) {
502 DEBUGMSGTL(("snmpTargetAddrEntry",
503 "ERROR snmpTargetAddrEntry: storage type is not a digit "
504 "in config string\n"));
505 return (0);
506 }
507 /*
508 * check that storage type is a possible value
509 */
510 else if (((entry->storageType = (int) strtol(cptr, (char **) NULL, 0))
511 != SNMP_STORAGE_OTHER) &&
512 (entry->storageType != SNMP_STORAGE_VOLATILE) &&
513 (entry->storageType != SNMP_STORAGE_NONVOLATILE) &&
514 (entry->storageType != SNMP_STORAGE_PERMANENT) &&
515 (entry->storageType != SNMP_STORAGE_READONLY)) {
516 DEBUGMSGTL(("snmpTargetAddrEntry",
517 "ERROR snmpTargetAddrEntry: storage type not a valid "
518 "value of other(%d), volatile(%d), nonvolatile(%d), "
519 "permanent(%d), or readonly(%d) in config string.\n",
520 SNMP_STORAGE_OTHER, SNMP_STORAGE_VOLATILE,
521 SNMP_STORAGE_NONVOLATILE, SNMP_STORAGE_PERMANENT,
522 SNMP_STORAGE_READONLY));
523 return (0);
524 }
525 return (1);
526 } /* snmpTargetAddr_addStorageType */
527
528
529 static int
530 snmpTargetAddr_addRowStatus(struct targetAddrTable_struct *entry,
531 char *cptr)
532 {
533 if (cptr == NULL) {
534 DEBUGMSGTL(("snmpTargetAddrEntry",
535 "ERROR snmpTargetAddrEntry: no Row Status in config "
536 "string\n"));
537 return (0);
538 } else if (!(isdigit((unsigned char)(*cptr)))) {
539 DEBUGMSGTL(("snmpTargetAddrEntry",
540 "ERROR snmpTargetAddrEntry: Row Status is not a digit in "
541 "config string\n"));
542 return (0);
543 }
544 /*
545 * check that row status is a valid value
546 */
547 else if (((entry->rowStatus = (int) strtol(cptr, (char **) NULL, 0))
548 != SNMP_ROW_ACTIVE) &&
549 (entry->rowStatus != SNMP_ROW_NOTINSERVICE) &&
550 (entry->rowStatus != SNMP_ROW_NOTREADY)) {
551 DEBUGMSGTL(("snmpTargetAddrEntry",
552 "ERROR snmpTargetAddrEntry: Row Status is not a valid "
553 "value of active(%d), notinservice(%d), or notready(%d) "
554 "in config string.\n",
555 SNMP_ROW_ACTIVE, SNMP_ROW_NOTINSERVICE, SNMP_ROW_NOTREADY));
556 return (0);
557 }
558 return (1);
559 } /* snmpTargetAddr_addRowStatus */
560
561
562
563 static void
564 snmpd_parse_config_targetAddr(const char *token, char *char_ptr)
565 {
566 const char *cptr = char_ptr;
567 char buff[1024], *bptr;
568 struct targetAddrTable_struct *newEntry;
569 int i;
570 size_t bufl;
571
572 newEntry = snmpTargetAddrTable_create();
573
574 cptr = skip_white_const(cptr);
575 if (cptr == NULL) {
576 DEBUGMSGTL(("snmpTargetAddrEntry",
577 "ERROR snmpTargetAddrEntry: no name in config string\n"));
578 snmpTargetAddrTable_dispose(newEntry);
579 return;
580 }
581
582 bufl = 0;
583 cptr = read_config_read_octet_string_const(cptr,
584 (u_char**)&newEntry->nameData,
585 &bufl);
586 if (bufl < 1 || bufl > 32) {
587 DEBUGMSGTL(("snmpTargetAddrEntry",
588 "ERROR snmpTargetAddrEntry: name out of range in config "
589 "string\n"));
590 snmpTargetAddrTable_dispose(newEntry);
591 return;
592 }
593 newEntry->nameLen = bufl;
594
595 cptr = copy_nword_const(cptr, buff, sizeof(buff));
596 if (snmpTargetAddr_addTDomain(newEntry, buff) == 0) {
597 snmpTargetAddrTable_dispose(newEntry);
598 return;
599 }
600 cptr =
601 read_config_read_octet_string_const(cptr,
602 (u_char **) & newEntry->tAddress,
603 &newEntry->tAddressLen);
604 if (!cptr || !newEntry->tAddress || (newEntry->tAddressLen < 1) ||
605 (newEntry->tAddressLen > 32)) {
606 DEBUGMSGTL(("snmpTargetAddrEntry",
607 "ERROR snmpTargetAddrEntry: no/bd TAddress in config string\n"));
608 snmpTargetAddrTable_dispose(newEntry);
609 return;
610 }
611 cptr = copy_nword_const(cptr, buff, sizeof(buff));
612 if (snmpTargetAddr_addTimeout(newEntry, buff) == 0) {
613 snmpTargetAddrTable_dispose(newEntry);
614 return;
615 }
616 cptr = copy_nword_const(cptr, buff, sizeof(buff));
617 if (snmpTargetAddr_addRetryCount(newEntry, buff) == 0) {
618 snmpTargetAddrTable_dispose(newEntry);
619 return;
620 }
621 cptr = copy_nword_const(cptr, buff, sizeof(buff));
622 if (snmpTargetAddr_addTagList(newEntry, buff) == 0) {
623 snmpTargetAddrTable_dispose(newEntry);
624 return;
625 }
626 cptr = copy_nword_const(cptr, buff, sizeof(buff));
627 if (snmpTargetAddr_addParams(newEntry, buff) == 0) {
628 snmpTargetAddrTable_dispose(newEntry);
629 return;
630 }
631 cptr = copy_nword_const(cptr, buff, sizeof(buff));
632 if (snmpTargetAddr_addStorageType(newEntry, buff) == 0) {
633 snmpTargetAddrTable_dispose(newEntry);
634 return;
635 }
636 cptr = copy_nword_const(cptr, buff, sizeof(buff));
637 if (snmpTargetAddr_addRowStatus(newEntry, buff) == 0) {
638 snmpTargetAddrTable_dispose(newEntry);
639 return;
640 }
641 bptr = buff;
642 bptr += sprintf(bptr, "snmp_parse_config_targetAddr, read: ");
643 bptr = read_config_save_octet_string(bptr, (u_char*)newEntry->nameData,
644 newEntry->nameLen);
645 *bptr++ = '\n';
646 for (i = 0; i < newEntry->tDomainLen; i++) {
647 bptr += snprintf(bptr, buff + sizeof(buff) - bptr,
648 ".%d", (int) newEntry->tDomain[i]);
649 }
650 for (i = 0; i < newEntry->tAddressLen; i++)
651 bptr += snprintf(bptr, buff + sizeof(buff) - bptr, " %02x",
652 ((u_char *)newEntry->tAddress)[i]);
653 bptr += snprintf(bptr, buff + sizeof(buff) - bptr,
654 " %d %d %s %s %d %d\n", newEntry->timeout,
655 newEntry->retryCount, newEntry->tagListData,
656 newEntry->paramsData, newEntry->storageType,
657 newEntry->rowStatus);
658 buff[ sizeof(buff) - 1 ] = 0;
659 DEBUGMSGTL(("snmpTargetAddrEntry", "%s", buff));
660
661 snmpTargetAddrTable_addToList(newEntry, &aAddrTable);
662 } /* snmpd_parse_config_target */
663