Debian Codegraph @

agent/mibgroup/tlstm-mib/snmpTlstmCertToTSNTable/snmpTlstmCertToTSNTable.c

raw
1 /*
2 * Note: this file originally auto-generated by mib2c using
3 * $
4 */
5
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-features.h>
8 #include <net-snmp/net-snmp-includes.h>
9 #include <net-snmp/agent/net-snmp-agent-includes.h>
10 #include <openssl/ssl.h>
11 #include <openssl/x509.h>
12 #include <net-snmp/library/cert_util.h>
13 #include "tlstm-mib.h"
14 #include "snmpTlstmCertToTSNTable.h"
15
16 netsnmp_feature_require(table_tdata);
17 netsnmp_feature_require(cert_fingerprints);
18 netsnmp_feature_require(table_tdata_delete_table);
19 netsnmp_feature_require(table_tdata_extract_table);
20 netsnmp_feature_require(table_tdata_remove_row);
21 netsnmp_feature_require(tls_fingerprint_build);
22 #ifndef NETSNMP_NO_WRITE_SUPPORT
23 netsnmp_feature_require(check_vb_storagetype);
24 netsnmp_feature_require(check_vb_type_and_max_size);
25 netsnmp_feature_require(check_vb_rowstatus_with_storagetype);
26 netsnmp_feature_require(table_tdata_insert_row);
27 #endif /* NETSNMP_NO_WRITE_SUPPORT */
28
29 /** XXX - move these to table_data header? */
30 #define FATE_NEWLY_CREATED 1
31 #define FATE_NO_CHANGE 0
32 #define FATE_DELETE_ME -1
33
34 #define MAP_MIB_CONFIG_TOKEN "snmpTlstmCertToTSNEntry"
35
36 /*
37 * structure for undo storage and other vars for set processing
38 */
39 typedef struct certToTSN_undo_s {
40 char fate;
41 char copied;
42 signed char is_consistent;
43 netsnmp_request_info *req[SNMPTLSTMCERTTOTSN_TABLE_MAX_COL+1];
44
45 /*
46 * undo Column space
47 */
48 char fingerprint[SNMPTLSTMCERTTOTSN_FINGERPRINT_MAX_SIZE];
49 size_t fingerprint_len;
50 int mapType;
51 char data[SNMPTLSTMCERTTOTSN_DATA_MAX_SIZE];
52 size_t data_len;
53 u_char hashType;
54 char storageType;
55 char rowStatus;
56 } certToTSN_undo;
57
58 /*
59 * Typical data structure for a row entry
60 */
61 typedef struct certToTSN_entry_s {
62 /*
63 * Index values
64 */
65 u_long tlstmCertToTSNID;
66
67 /*
68 * Column values
69 */
70 char fingerprint[SNMPTLSTMCERTTOTSN_FINGERPRINT_MAX_SIZE];
71 size_t fingerprint_len;
72 int mapType;
73 char data[SNMPTLSTMCERTTOTSN_DATA_MAX_SIZE];
74 size_t data_len;
75 char storageType;
76 char rowStatus;
77 u_char hashType;
78 char map_flags;
79
80 /*
81 * used during set processing
82 */
83 certToTSN_undo *undo;
84 } certToTSN_entry;
85
86 static netsnmp_handler_registration *to_tsn_reg;
87 static netsnmp_handler_registration *to_tsn_count_reg;
88 static netsnmp_handler_registration *to_tsn_last_changed_reg;
89 static netsnmp_table_registration_info *to_tsn_table;
90
91 static netsnmp_tdata *_table = NULL;
92 static uint32_t _last_changed = 0;
93
94 /*
95 * create a new row in the table
96 */
97 netsnmp_tdata_row *
98 tlstmCertToTSNTable_createEntry(netsnmp_tdata * table, u_long tlstmCertToTSNID)
99 {
100 certToTSN_entry *entry;
101 netsnmp_tdata_row *row;
102
103 entry = SNMP_MALLOC_TYPEDEF(certToTSN_entry);
104 if (!entry)
105 return NULL;
106
107 row = netsnmp_tdata_create_row();
108 if (!row) {
109 SNMP_FREE(entry);
110 return NULL;
111 }
112 row->data = entry;
113
114 DEBUGMSGT(("tlstmCertToSN:entry:create", "entry %p / row %p\n",
115 entry, row));
116 /*
117 * populate index
118 */
119 entry->tlstmCertToTSNID = tlstmCertToTSNID;
120 netsnmp_tdata_row_add_index(row, ASN_UNSIGNED,
121 &(entry->tlstmCertToTSNID),
122 sizeof(entry->tlstmCertToTSNID));
123 /*
124 * assign default column values
125 */
126 entry->mapType = TSNM_tlstmCertSpecified;
127 entry->storageType = ST_NONVOLATILE;
128 entry->rowStatus = RS_NOTREADY;
129
130 if (table) {
131 DEBUGMSGTL(("tlstmCertToTSN:row:insert", "row %p\n", row));
132 netsnmp_tdata_add_row(table, row);
133 }
134 return row;
135 }
136
137 /*
138 * allocate undo resources
139 */
140 static certToTSN_undo *
141 _allocUndo(certToTSN_entry * entry)
142 {
143 if (!entry)
144 return NULL;
145
146 netsnmp_assert(!entry->undo);
147
148 entry->undo = SNMP_MALLOC_TYPEDEF(certToTSN_undo);
149 if (!entry->undo)
150 return NULL;
151
152 entry->undo->is_consistent = -1; /* don't know */
153
154 return entry->undo;
155 }
156
157 /*
158 * free undo resources
159 */
160 static void
161 _freeUndo(certToTSN_entry * entry)
162 {
163 if (!entry || !entry->undo)
164 return;
165
166 /*
167 * TODO: release any allocated resources
168 */
169 SNMP_FREE(entry->undo);
170 }
171
172 /*
173 * remove a row from the table
174 */
175 void
176 tlstmCertToTSNTable_removeEntry(netsnmp_tdata * table,
177 netsnmp_tdata_row * row)
178 {
179 certToTSN_entry *entry;
180
181 if (!row)
182 return; /* Nothing to remove */
183
184 entry = (certToTSN_entry *) row->data;
185
186 DEBUGMSGT(("tlstmCertToSN:entry:delete", "entry %p / row %p\n",
187 entry, row));
188
189 if (entry && entry->undo)
190 _freeUndo(entry);
191 SNMP_FREE(entry);
192
193 if (table) {
194 DEBUGMSGTL(("tlstmCertToSN:row:remove", "row %p\n", row));
195 netsnmp_tdata_remove_and_delete_row(table, row);
196 }
197 else
198 netsnmp_tdata_delete_row(row);
199 }
200
201 static oid
202 _oid2type(const oid *val, int val_len)
203 {
204 static const oid _tsnm_base[] = { SNMP_TLS_TM_BASE, 1, 1 };
205 static const int _tsnm_base_len = sizeof(_tsnm_base);
206
207 netsnmp_assert(val);
208
209 if (val_len != (_tsnm_base_len + sizeof(oid)))
210 return -1;
211
212 if (memcmp(_tsnm_base,val,_tsnm_base_len) != 0)
213 return -2;
214
215 if ((val[OID_LENGTH(_tsnm_base)] > TSNM_tlstmCert_MAX) ||
216 (0 == val[OID_LENGTH(_tsnm_base)]))
217 return -3;
218
219 return val[OID_LENGTH(_tsnm_base)];
220 }
221
222 static void
223 _cert_map_add(certToTSN_entry *entry)
224 {
225 netsnmp_cert_map *map;
226
227 if (NULL == entry)
228 return;
229
230 DEBUGMSGTL(("tlstmCertToTSNTable:map:add", "pri %ld, fp %s\n",
231 entry->tlstmCertToTSNID, entry->fingerprint));
232
233 map = netsnmp_cert_map_alloc(entry->fingerprint, NULL);
234 if (NULL == map)
235 return;
236
237 map->priority = entry->tlstmCertToTSNID;
238 map->mapType = entry->mapType;
239 map->data = strdup(entry->data);
240 map->hashType = entry->hashType;
241
242 map->flags = NSCM_FROM_MIB;
243 if (entry->storageType == ST_NONVOLATILE)
244 map->flags |= NSCM_NONVOLATILE;
245
246 if (netsnmp_cert_map_add(map) != 0)
247 netsnmp_cert_map_free(map);
248 }
249
250 static void
251 _cert_map_remove(certToTSN_entry *entry)
252 {
253 netsnmp_container *maps;
254 netsnmp_cert_map map;
255
256 if (NULL == entry)
257 return;
258
259 DEBUGMSGTL(("tlstmCertToTSNTable:map:remove", "pri %ld, fp %s\n",
260 entry->tlstmCertToTSNID, entry->fingerprint));
261
262 /** get current active maps */
263 maps = netsnmp_cert_map_container();
264 if (NULL == maps)
265 return;
266
267 map.priority = entry->tlstmCertToTSNID;
268 map.fingerprint = entry->fingerprint;
269
270 if (CONTAINER_REMOVE(maps, &map) != 0) {
271 snmp_log(LOG_ERR, "could not remove certificate map");
272 }
273 entry->map_flags = 0;
274 }
275
276 static void
277 _cert_map_tweak_storage(certToTSN_entry *entry)
278 {
279 netsnmp_container *maps;
280 netsnmp_cert_map *map, index;
281
282 if (NULL == entry)
283 return;
284
285 DEBUGMSGTL(("tlstmCertToTSNTable:map:tweak", "pri %ld, st %d\n",
286 entry->tlstmCertToTSNID, entry->storageType));
287
288 /** get current active maps */
289 maps = netsnmp_cert_map_container();
290 if (NULL == maps)
291 return;
292
293 index.priority = entry->tlstmCertToTSNID;
294 map = CONTAINER_FIND(maps, &index);
295 if (NULL == map) {
296 DEBUGMSGTL(("tlstmCertToTSNTable:map:tweak", "couldn't find map!\n"));
297 return;
298 }
299
300 if (entry->storageType == ST_NONVOLATILE)
301 map->flags |= NSCM_NONVOLATILE;
302 else
303 map->flags &= ~NSCM_NONVOLATILE;
304 }
305
306 /** handles requests for the tlstmCertToTSNTable table */
307 static int
308 tlstmCertToTSNTable_handler(netsnmp_mib_handler *handler,
309 netsnmp_handler_registration *reginfo,
310 netsnmp_agent_request_info *reqinfo,
311 netsnmp_request_info *requests)
312 {
313 oid tsnm[] = { SNMP_TLS_TM_BASE, 1, 1, 0 };
314 static const int tsnm_pos = OID_LENGTH(tsnm) - 1;
315 netsnmp_request_info *request = NULL;
316 netsnmp_table_request_info *info;
317 netsnmp_tdata *table;
318 netsnmp_tdata_row *row;
319 certToTSN_entry *entry;
320 int ret = SNMP_ERR_NOERROR;
321
322 DEBUGMSGTL(("tlstmCertToSN:handler", "Processing request (mode %s (%d))\n",
323 se_find_label_in_slist("agent_mode", reqinfo->mode),
324 reqinfo->mode));
325
326 switch (reqinfo->mode) {
327 /** ######################################################### GET #####
328 *
329 * Read-support (also covers GetNext requests)
330 */
331 case MODE_GET:
332 for (request = requests; request; request = request->next) {
333 if (request->processed)
334 continue;
335
336 entry = (certToTSN_entry *) netsnmp_tdata_extract_entry(request);
337 info = netsnmp_extract_table_info(request);
338 netsnmp_assert(entry && info);
339
340 switch (info->colnum) {
341 case COL_SNMPTLSTMCERTTOTSN_FINGERPRINT:
342 {
343 /*
344 * build SnmpTLSFingerprint
345 */
346 u_char bin[EVP_MAX_MD_SIZE+1], *ptr = bin;
347 size_t len = sizeof(bin);
348 int rc;
349 rc = netsnmp_tls_fingerprint_build(entry->hashType,
350 entry->fingerprint,
351 &ptr, &len, 0);
352 if (SNMPERR_SUCCESS != rc)
353 netsnmp_set_request_error(reqinfo, request,
354 SNMP_ERR_GENERR);
355 else
356 snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
357 bin, len);
358 }
359 break; /* case COL_SNMPTLSTMCERTTOTSN_FINGERPRINT */
360 case COL_SNMPTLSTMCERTTOTSN_MAPTYPE:
361 tsnm[tsnm_pos] = entry->mapType;
362 snmp_set_var_typed_value(request->requestvb, ASN_OBJECT_ID,
363 tsnm, sizeof(tsnm));
364 break; /* case COL_SNMPTLSTMCERTTOTSN_MAPTYPE */
365 case COL_SNMPTLSTMCERTTOTSN_DATA:
366 snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
367 entry->data, entry->data_len);
368 break; /* case COL_SNMPTLSTMCERTTOTSN_DATA */
369 case COL_SNMPTLSTMCERTTOTSN_STORAGETYPE:
370 snmp_set_var_typed_integer(request->requestvb, ASN_INTEGER,
371 entry->storageType);
372 break; /* case COL_SNMPTLSTMCERTTOTSN_STORAGETYPE */
373 case COL_SNMPTLSTMCERTTOTSN_ROWSTATUS:
374 snmp_set_var_typed_integer(request->requestvb, ASN_INTEGER,
375 entry->rowStatus);
376 break; /* case COL_SNMPTLSTMCERTTOTSN_ROWSTATUS */
377 default:
378 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
379 break;
380 } /* switch colnum */
381 } /* for requests */
382 break; /* case MODE_GET */
383
384 /*
385 * Write-support
386 */
387 /** #################################################### RESERVE1 #####
388 *
389 * In RESERVE1 we are just checking basic ASN.1 size/type restrictions.
390 * You probably don't need to change any of this code. Don't change any
391 * of the column values here. Save that for the ACTION phase.
392 *
393 * The next phase is RESERVE2 or FREE.
394 */
395 case MODE_SET_RESERVE1:
396 for (request = requests; request; request = request->next) {
397 netsnmp_assert(request->processed == 0);
398
399 entry = (certToTSN_entry *) netsnmp_tdata_extract_entry(request);
400 info = netsnmp_extract_table_info(request);
401
402 if ((NULL != entry) && (ST_READONLY == entry->storageType)) {
403 ret = SNMP_ERR_NOTWRITABLE;
404 break;
405 }
406
407 switch (info->colnum) {
408 case COL_SNMPTLSTMCERTTOTSN_FINGERPRINT:
409 ret = netsnmp_check_vb_type_and_max_size
410 (request->requestvb, ASN_OCTET_STR,
411 sizeof(entry->fingerprint));
412 /** check len/algorithm MIB requirements */
413 if (ret == SNMP_ERR_NOERROR)
414 ret = netsnmp_cert_check_vb_fingerprint(request->requestvb);
415 break; /* case COL_SNMPTLSTMCERTTOTSN_FINGERPRINT */
416 case COL_SNMPTLSTMCERTTOTSN_MAPTYPE:
417 ret = netsnmp_check_vb_type_and_max_size
418 (request->requestvb, ASN_OBJECT_ID,
419 SNMPTLSTMCERTTOTSN_MAPTYPE_MAX_SIZE);
420 if (ret == SNMP_ERR_NOERROR) {
421 if (_oid2type(request->requestvb->val.objid,
422 request->requestvb->val_len) >
423 TSNM_tlstmCert_MAX)
424 ret = SNMP_ERR_WRONGVALUE;
425 }
426 break; /* case COL_SNMPTLSTMCERTTOTSN_MAPTYPE */
427 case COL_SNMPTLSTMCERTTOTSN_DATA:
428 ret = netsnmp_check_vb_type_and_max_size
429 (request->requestvb, ASN_OCTET_STR, sizeof(entry->data));
430 break; /* case COL_SNMPTLSTMCERTTOTSN_DATA */
431 case COL_SNMPTLSTMCERTTOTSN_STORAGETYPE:
432 ret = netsnmp_check_vb_storagetype
433 (request->requestvb,(entry ? entry->storageType : ST_NONE));
434 break; /* case COL_SNMPTLSTMCERTTOTSN_STORAGETYPE */
435 case COL_SNMPTLSTMCERTTOTSN_ROWSTATUS:
436 ret = netsnmp_check_vb_rowstatus_with_storagetype
437 (request->requestvb,
438 (entry ? entry->rowStatus :RS_NONEXISTENT),
439 (entry ? entry->storageType :ST_NONE));
440 break; /* case COL_SNMPTLSTMCERTTOTSN_ROWSTATUS */
441 default:
442 ret = SNMP_ERR_NOTWRITABLE;
443 } /* switch colnum */
444
445 if (ret != SNMP_ERR_NOERROR)
446 break;
447 } /* for requests */
448 break; /* case MODE_SET_RESERVE1 */
449
450 /** #################################################### RESERVE2 #####
451 *
452 * RESERVE2 is for checking additional restrictions from the MIB.
453 * Since these restrictions are often in the description of the object,
454 * mib2c can't generate code. It's possible that you need to add
455 * additional checks here. However, don't change any of the column
456 * values here. Save that for the ACTION phase.
457 *
458 * The next phase is ACTION or FREE.
459 */
460 case MODE_SET_RESERVE2:
461 for (request = requests; request; request = request->next) {
462 netsnmp_assert(request->processed == 0);
463
464 entry = (certToTSN_entry *) netsnmp_tdata_extract_entry(request);
465 table = netsnmp_tdata_extract_table(request);
466 info = netsnmp_extract_table_info(request);
467 /*
468 * if no row, create one
469 */
470 if (!entry) {
471 row = tlstmCertToTSNTable_createEntry
472 (table,*info->indexes->val.integer);
473 if (!row) {
474 ret = SNMP_ERR_RESOURCEUNAVAILABLE;
475 break;
476 }
477 entry = row->data;
478 _allocUndo(entry);
479 if (!entry->undo) {
480 tlstmCertToTSNTable_removeEntry(table, row);
481 row = NULL;
482 ret = SNMP_ERR_RESOURCEUNAVAILABLE;
483 break;
484 }
485 entry->undo->fate = FATE_NEWLY_CREATED;
486 /** associate row with requests */
487 netsnmp_insert_tdata_row(request, row);
488 }
489
490 /** allocate undo structure, if needed */
491 if (!entry->undo) {
492 _allocUndo(entry);
493 if (!entry->undo) {
494 ret = SNMP_ERR_RESOURCEUNAVAILABLE;
495 break;
496 }
497 }
498
499 /*
500 * save request ptr for column. if we already
501 * have a value, bail.
502 */
503 if (entry->undo->req[info->colnum]) {
504 DEBUGMSGT(("tlstmCertToSN:reserve2",
505 "multiple sets to col %d in request\n",
506 info->colnum));
507 if (FATE_NEWLY_CREATED == entry->undo->fate)
508 ret = SNMP_ERR_INCONSISTENTNAME;
509 else
510 ret = SNMP_ERR_INCONSISTENTVALUE;
511 break;
512 }
513 entry->undo->req[info->colnum] = request;
514 if (ret != SNMP_ERR_NOERROR)
515 break;
516 } /* for requests */
517
518 if (ret == SNMP_ERR_NOERROR) {
519 /** make sure rowstatus is used to create rows */
520 for (request = requests; request; request = request->next) {
521 if (request->processed)
522 continue;
523
524 entry = (certToTSN_entry *)
525 netsnmp_tdata_extract_entry(request);
526 if ((entry->undo->fate != FATE_NEWLY_CREATED) ||
527 (entry->undo->req[COL_SNMPTLSTMCERTTOTSN_ROWSTATUS]))
528 continue;
529 ret = SNMP_ERR_INCONSISTENTNAME;
530 break;
531 } /* creation for requests */
532 } /* no error */
533 break; /* case MODE_SET_RESERVE2 */
534
535 /** ######################################################## FREE #####
536 *
537 * FREE is for cleaning up after a failed request (during either
538 * RESERVE1 or RESERVE2). So any allocated resources need to be
539 * released.
540 *
541 * This the final phase for this path in the state machine.
542 */
543 case MODE_SET_FREE:
544 /*
545 * release undo resources
546 * remove any newly created rows
547 */
548 for (request = requests; request; request = request->next) {
549 table = netsnmp_tdata_extract_table(request);
550 row = netsnmp_tdata_extract_row(request);
551 entry = (certToTSN_entry *) netsnmp_tdata_extract_entry(request);
552
553 if (!entry || !entry->undo)
554 continue;
555
556 /** disassociate row with requests */
557 netsnmp_remove_tdata_row(request, row);
558
559 if (FATE_NEWLY_CREATED == entry->undo->fate)
560 tlstmCertToTSNTable_removeEntry(table, row);
561 else
562 _freeUndo(entry);
563 }
564 break; /* case MODE_SET_FREE */
565
566 /** ###################################################### ACTION #####
567 *
568 * In the ACTION phase, we perform any sets that can be undone.
569 * (Save anything that can't be undone for the COMMIT phase.)
570 *
571 * After individual columns have been done, you should check that the
572 * row as a whole is consistent.
573 *
574 * The next phase is UNDO or COMMIT.
575 */
576 case MODE_SET_ACTION:
577 for (request = requests; request; request = request->next) {
578 entry = (certToTSN_entry *) netsnmp_tdata_extract_entry(request);
579 info = netsnmp_extract_table_info(request);
580
581 /** reserve2 should enforce this */
582 netsnmp_assert(request == entry->undo->req[info->colnum]);
583
584 /*
585 * for each col, save old value and the set new value
586 */
587 switch (info->colnum) {
588 case COL_SNMPTLSTMCERTTOTSN_FINGERPRINT:
589 {
590 u_char *tmp = (u_char*)entry->fingerprint;
591 u_int size = sizeof(entry->fingerprint);
592 netsnmp_variable_list *vb = request->requestvb;
593
594 memcpy(entry->undo->fingerprint,
595 entry->fingerprint, sizeof(entry->fingerprint));
596 entry->undo->fingerprint_len = entry->fingerprint_len;
597 entry->undo->hashType = entry->hashType;
598 memset(entry->fingerprint, 0, sizeof(entry->fingerprint));
599
600 (void)netsnmp_tls_fingerprint_parse(vb->val.string, vb->val_len,
601 (char**)&tmp, &size, 0,
602 &entry->hashType);
603 entry->fingerprint_len = size;
604 if (0 == entry->fingerprint_len)
605 ret = SNMP_ERR_GENERR;
606 }
607 break; /* case COL_SNMPTLSTMCERTTOTSN_FINGERPRINT */
608 case COL_SNMPTLSTMCERTTOTSN_MAPTYPE:
609 entry->undo->mapType = entry->mapType;
610 entry->mapType = _oid2type(request->requestvb->val.objid,
611 request->requestvb->val_len);
612 break; /* case COL_SNMPTLSTMCERTTOTSN_MAPTYPE */
613 case COL_SNMPTLSTMCERTTOTSN_DATA:
614 memcpy(entry->undo->data, entry->data, sizeof(entry->data));
615 entry->undo->data_len = entry->data_len;
616 memset(entry->data, 0, sizeof(entry->data));
617 memcpy(entry->data, request->requestvb->val.string,
618 request->requestvb->val_len);
619 entry->data_len = request->requestvb->val_len;
620 break; /* case COL_SNMPTLSTMCERTTOTSN_DATA */
621 case COL_SNMPTLSTMCERTTOTSN_STORAGETYPE:
622 entry->undo->storageType = entry->storageType;
623 entry->storageType = *request->requestvb->val.integer;
624 break; /* case COL_SNMPTLSTMCERTTOTSN_STORAGETYPE */
625 case COL_SNMPTLSTMCERTTOTSN_ROWSTATUS:
626 entry->undo->rowStatus = entry->rowStatus;
627 entry->rowStatus = *request->requestvb->val.integer;
628 break; /* case COL_SNMPTLSTMCERTTOTSN_ROWSTATUS */
629 } /* switch colnum */
630 } /* set values for requests */
631
632 if (ret != SNMP_ERR_NOERROR)
633 break; /* skip consistency if we've already got error */
634
635 /*
636 * All columns now have their final values set. check the
637 * internal consistency of each row.
638 */
639 for (request = requests; request; request = request->next) {
640 entry = (certToTSN_entry *) netsnmp_tdata_extract_entry(request);
641 info = netsnmp_extract_table_info(request);
642
643 if (entry->undo->is_consistent != -1)
644 continue; /* already checked */
645
646 /** assume consistency */
647 entry->undo->is_consistent = 1;
648
649 /*
650 * per mib, can't have empty fingerprint and must
651 * have data if indicated by map type.
652 */
653 if (0 == entry->fingerprint_len) {
654 DEBUGMSGTL(("tlstmCertToTSNTable:inconsistent",
655 "fingerprint must not be empty\n"));
656 entry->undo->is_consistent = 0;
657 }
658 else if ((TSNM_tlstmCertSpecified == entry->mapType) &&
659 (0 == entry->data_len)) {
660 DEBUGMSGTL(("tlstmCertToTSNTable:inconsistent",
661 "must specify Data for CertSpecified identity\n"));
662 entry->undo->is_consistent = 0;
663 }
664
665 if ((RS_IS_ACTIVE(entry->rowStatus)) &&
666 ((!entry->undo->req[COL_SNMPTLSTMCERTTOTSN_ROWSTATUS]) ||
667 (RS_IS_ACTIVE(entry->undo->rowStatus)))) {
668 /*
669 * per mib, can't modify these while row active
670 */
671 char _cols[3] = { COL_SNMPTLSTMCERTTOTSN_FINGERPRINT,
672 COL_SNMPTLSTMCERTTOTSN_MAPTYPE, COL_SNMPTLSTMCERTTOTSN_DATA };
673 int i;
674 for (i=0; i < 3; ++i ) {
675 if (!entry->undo->req[i])
676 continue;
677 DEBUGMSGTL(("tlstmCertToTSNTable:inconsistent",
678 "can't modify row %d while active\n",
679 _cols[i]));
680 entry->undo->is_consistent = 0;
681 ret = SNMP_ERR_NOTWRITABLE;
682 request= entry->undo->req[i];
683 break;
684 }
685 } else if (RS_IS_GOING_ACTIVE(entry->rowStatus)) {
686 /*
687 * if going active, inconsistency is fatal
688 */
689 if (!entry->undo->is_consistent) {
690 netsnmp_assert(entry->undo->req[COL_SNMPTLSTMCERTTOTSN_ROWSTATUS]);
691 if (FATE_NEWLY_CREATED == entry->undo->fate)
692 ret = SNMP_ERR_INCONSISTENTNAME;
693 else
694 ret = SNMP_ERR_INCONSISTENTVALUE;
695 request = entry->undo->req[COL_SNMPTLSTMCERTTOTSN_ROWSTATUS];
696 }
697 } else if (RS_DESTROY == entry->rowStatus) {
698 /*
699 * can't destroy active row
700 */
701 if (RS_IS_ACTIVE(entry->undo->rowStatus)) {
702 DEBUGMSGTL(("tlstmCertToTSNTable:inconsistent",
703 "can't destroy active row\n"));
704 netsnmp_assert(entry->undo->req[COL_SNMPTLSTMCERTTOTSN_ROWSTATUS]);
705 ret = SNMP_ERR_INCONSISTENTVALUE;
706 request = entry->undo->req[COL_SNMPTLSTMCERTTOTSN_ROWSTATUS];
707 }
708 }
709 if (ret != SNMP_ERR_NOERROR)
710 break;
711 } /* consistency for requests */
712 break; /* case MODE_SET_ACTION */
713
714 /** ######################################################## UNDO #####
715 *
716 * UNDO is for cleaning up any failed requests that went through the
717 * ACTION phase.
718 *
719 * This the final phase for this path in the state machine.
720 */
721 case MODE_SET_UNDO:
722 for (request = requests; request; request = request->next) {
723 row = netsnmp_tdata_extract_row(request);
724 entry = (certToTSN_entry *) netsnmp_tdata_extract_entry(request);
725 info = netsnmp_extract_table_info(request);
726
727 /*
728 * skip newly created rows, as we're going to delete
729 * them below anyways
730 */
731 if (FATE_NEWLY_CREATED == entry->undo->fate)
732 continue;
733
734 /*
735 * restore values
736 */
737 switch (info->colnum) {
738 case COL_SNMPTLSTMCERTTOTSN_FINGERPRINT:
739 memcpy(entry->fingerprint, entry->undo->fingerprint,
740 sizeof(entry->fingerprint));
741 entry->fingerprint_len = entry->undo->fingerprint_len;
742 entry->hashType = entry->undo->hashType;
743 break; /* case COL_SNMPTLSTMCERTTOTSN_FINGERPRINT */
744 case COL_SNMPTLSTMCERTTOTSN_MAPTYPE:
745 entry->mapType = entry->undo->mapType;
746 break; /* case COL_SNMPTLSTMCERTTOTSN_MAPTYPE */
747 case COL_SNMPTLSTMCERTTOTSN_DATA:
748 memcpy(entry->data, entry->undo->data, sizeof(entry->data));
749 entry->data_len = entry->undo->data_len;
750 break; /* case COL_SNMPTLSTMCERTTOTSN_DATA */
751 case COL_SNMPTLSTMCERTTOTSN_STORAGETYPE:
752 entry->storageType = entry->undo->storageType;
753 break; /* case COL_SNMPTLSTMCERTTOTSN_STORAGETYPE */
754 case COL_SNMPTLSTMCERTTOTSN_ROWSTATUS:
755 entry->rowStatus = entry->undo->rowStatus;
756 break; /* case COL_SNMPTLSTMCERTTOTSN_ROWSTATUS */
757 } /* switch colnum */
758 } /* for requests */
759
760 /*
761 * release undo data
762 * or remove any newly created rows
763 */
764 for (request = requests; request; request = request->next) {
765 table = netsnmp_tdata_extract_table(request);
766 row = netsnmp_tdata_extract_row(request);
767 entry = (certToTSN_entry *) netsnmp_tdata_extract_entry(request);
768
769 if (!entry || !entry->undo)
770 continue;
771
772 /** disassociate row with requests */
773 netsnmp_remove_tdata_row(request, row);
774
775 if (FATE_NEWLY_CREATED == entry->undo->fate)
776 tlstmCertToTSNTable_removeEntry(table, row);
777 else
778 _freeUndo(entry);
779 } /* for requests */
780 break; /* case MODE_SET_UNDO */
781
782 /** ###################################################### COMMIT #####
783 *
784 * COMMIT is the final success state, when all changes are finalized.
785 * There is not recovery state should something faile here.
786 *
787 * This the final phase for this path in the state machine.
788 */
789 case MODE_SET_COMMIT:
790 for (request = requests; request; request = request->next) {
791 row = netsnmp_tdata_extract_row(request);
792 table = netsnmp_tdata_extract_table(request);
793 info = netsnmp_extract_table_info(request);
794 entry = (certToTSN_entry *) netsnmp_tdata_extract_entry(request);
795
796 if ((RS_NOTREADY == entry->rowStatus) && entry->undo->is_consistent)
797 entry->rowStatus = RS_NOTINSERVICE;
798 else if ((RS_NOTINSERVICE == entry->rowStatus) &&
799 (0 == entry->undo->is_consistent))
800 entry->rowStatus = RS_NOTREADY;
801
802 /** release undo data for requests with no rowstatus */
803 if (entry->undo && !entry->undo->req[COL_SNMPTLSTMCERTTOTSN_ROWSTATUS]) {
804 _freeUndo(entry);
805 if ((0 == entry->map_flags) && (entry->rowStatus == RS_ACTIVE))
806 _cert_map_add(entry);
807 else if ((0 != entry->map_flags) &&
808 (entry->rowStatus == RS_DESTROY))
809 _cert_map_remove(entry);
810 }
811
812 switch (info->colnum) {
813 case COL_SNMPTLSTMCERTTOTSN_ROWSTATUS:
814 switch (entry->rowStatus) {
815 case RS_CREATEANDGO:
816 /** Fall-through */
817 case RS_ACTIVE:
818 netsnmp_assert(entry->undo->is_consistent);
819 entry->rowStatus = RS_ACTIVE;
820 if (0 == entry->map_flags)
821 _cert_map_add(entry);
822 break;
823
824 case RS_CREATEANDWAIT:
825 /** Fall-through */
826 case RS_NOTINSERVICE:
827 /** simply set status based on consistency */
828 if (entry->undo->is_consistent)
829 entry->rowStatus = RS_NOTINSERVICE;
830 else
831 entry->rowStatus = RS_NOTREADY;
832 if (0 != entry->map_flags)
833 _cert_map_remove(entry);
834 break;
835
836 case RS_DESTROY:
837 /** remove from cert map */
838 if (0 != entry->map_flags)
839 _cert_map_remove(entry);
840 /** disassociate row with requests */
841 netsnmp_remove_tdata_row(request, row);
842 tlstmCertToTSNTable_removeEntry(table, row);
843 entry = NULL;
844 }
845 /** release undo data */
846 _freeUndo(entry);
847 break; /* case COL_SNMPTLSTMCERTTOTSN_ROWSTATUS */
848
849 case COL_SNMPTLSTMCERTTOTSN_STORAGETYPE:
850 if (RS_ACTIVE == entry->rowStatus)
851 _cert_map_tweak_storage(entry);
852 break; /* case COL_SNMPTLSTMCERTTOTSN_STORAGETYPE */
853
854 case COL_SNMPTLSTMCERTTOTSN_FINGERPRINT:
855 case COL_SNMPTLSTMCERTTOTSN_MAPTYPE:
856 case COL_SNMPTLSTMCERTTOTSN_DATA:
857 break;
858 } /* switch colnum */
859
860 } /* for requests */
861
862 /** update last changed */
863 _last_changed = netsnmp_get_agent_uptime();
864
865 /** set up to save persistent store */
866 snmp_store_needed(NULL);
867
868 break; /* case MODE_SET_COMMIT */
869 } /* switch (reqinfo->mode) */
870
871 if (ret != SNMP_ERR_NOERROR)
872 netsnmp_set_request_error(reqinfo, request, ret);
873
874 return SNMP_ERR_NOERROR;
875 }
876
877 static int
878 _count_handler(netsnmp_mib_handler *handler,
879 netsnmp_handler_registration *reginfo,
880 netsnmp_agent_request_info *reqinfo,
881 netsnmp_request_info *requests)
882 {
883 int val;
884
885 if (MODE_GET != reqinfo->mode) {
886 snmp_log(LOG_ERR, "bad mode in RO handler");
887 return SNMP_ERR_GENERR;
888 }
889
890 if (NULL == _table->container)
891 val = 0;
892 else
893 val = CONTAINER_SIZE(_table->container);
894
895 snmp_set_var_typed_value(requests->requestvb, ASN_GAUGE,
896 (u_char *) &val, sizeof(val));
897
898 if (handler->next && handler->next->access_method)
899 return netsnmp_call_next_handler(handler, reginfo, reqinfo,
900 requests);
901
902 return SNMP_ERR_NOERROR;
903 }
904
905 static netsnmp_tdata_row *
906 _entry_from_map(netsnmp_cert_map *map)
907 {
908 netsnmp_tdata_row *row;
909 certToTSN_entry *entry;
910
911 row = tlstmCertToTSNTable_createEntry(NULL, map->priority);
912 if (NULL == row) {
913 snmp_log(LOG_ERR, "can create tlstmCertToTSN row entry\n");
914 return NULL;
915 }
916 entry = row->data;
917
918 if (map->flags & NSCM_FROM_CONFIG)
919 entry->storageType = ST_PERMANENT;
920 else if (! (map->flags & NSCM_NONVOLATILE))
921 entry->storageType = ST_VOLATILE;
922 entry->map_flags = map->flags;
923
924 entry->fingerprint_len = strlen(map->fingerprint);
925 if (entry->fingerprint_len > sizeof(entry->fingerprint))
926 entry->fingerprint_len = sizeof(entry->fingerprint) - 1;
927 memcpy(entry->fingerprint, map->fingerprint, entry->fingerprint_len);
928 entry->fingerprint[sizeof(entry->fingerprint) - 1] = 0;
929 entry->hashType = map->hashType;
930
931 if (map->data) {
932 entry->data_len = strlen(map->data);
933 if (entry->data_len) {
934 if (entry->data_len > sizeof(entry->data))
935 entry->data_len = sizeof(entry->data) - 1;
936 memcpy(entry->data, map->data, entry->data_len);
937 entry->data[sizeof(entry->data) - 1] = 0;
938 }
939 }
940 entry->mapType = map->mapType;
941
942 return row;
943 }
944
945 static int
946 _cache_load(netsnmp_cache *cache, void *q)
947 {
948 netsnmp_tdata *table = q;
949 netsnmp_container *maps;
950 netsnmp_iterator *map_itr;
951 netsnmp_cert_map *map;
952 netsnmp_tdata_row *row;
953 certToTSN_entry *entry;
954 int rc = 0;
955
956 DEBUGMSGTL(("tlstmCertToTSNTable:cache:load", "called, %" NETSNMP_PRIz "d rows\n",
957 CONTAINER_SIZE(table->container)));
958
959 /** get current active maps */
960 maps = netsnmp_cert_map_container();
961 if (NULL == maps)
962 return 0;
963 DEBUGMSGTL(("tlstmCertToTSNTable:cache:load", "maps %" NETSNMP_PRIz "d rows\n",
964 CONTAINER_SIZE(maps)));
965
966 map_itr = CONTAINER_ITERATOR(maps);
967 if (NULL == map_itr) {
968 DEBUGMSGTL(("tlstmCertToTSNTable:cache:load",
969 "cant get map iterator\n"));
970 return -1;
971 }
972
973 /*
974 * insert rows for active maps into tbl container
975 */
976 map = ITERATOR_FIRST(map_itr);
977 for( ; map; map = ITERATOR_NEXT(map_itr)) {
978
979 row = _entry_from_map(map);
980 if (NULL == row) {
981 rc =-1;
982 break;
983 }
984 entry = (certToTSN_entry*)row->data;
985 entry->rowStatus = RS_ACTIVE;
986
987 if (netsnmp_tdata_add_row(table, row) != SNMPERR_SUCCESS) {
988 tlstmCertToTSNTable_removeEntry(NULL, row);
989 rc = -1;
990 break;
991 }
992 }
993 ITERATOR_RELEASE(map_itr);
994
995 DEBUGMSGTL(("tlstmCertToTSNTable:cache:load", "done, %" NETSNMP_PRIz "d rows\n",
996 CONTAINER_SIZE(table->container)));
997
998 return rc;
999 }
1000
1001 static void
1002 _cache_free(netsnmp_cache *cache, void *q)
1003 {
1004 netsnmp_tdata *table = q;
1005 netsnmp_tdata_row *row;
1006 netsnmp_iterator *tbl_itr;
1007 certToTSN_entry *entry;
1008
1009 DEBUGMSGTL(("tlstmCertToTSNTable:cache:free", "called, %" NETSNMP_PRIz "d rows\n",
1010 CONTAINER_SIZE(table->container)));
1011
1012 /** insert rows for active maps into tbl container */
1013 tbl_itr = CONTAINER_ITERATOR(table->container);
1014 if (NULL == tbl_itr) {
1015 DEBUGMSGTL(("tlstmCertToTSNTable:cache:free",
1016 "cant get map iterator\n"));
1017 return;
1018 }
1019
1020 row = ITERATOR_FIRST(tbl_itr);
1021 for( ; row; row = ITERATOR_NEXT(tbl_itr)) {
1022 entry = row->data;
1023
1024 /*
1025 * remove all active rows (they are in the maps container kept
1026 * by the library). Keep inactive ones for next time.
1027 */
1028 if (entry->rowStatus == RS_ACTIVE) {
1029 tlstmCertToTSNTable_removeEntry(NULL, row);
1030 ITERATOR_REMOVE(tbl_itr);
1031 continue;
1032 }
1033 }
1034 ITERATOR_RELEASE(tbl_itr);
1035
1036 DEBUGMSGTL(("tlstmCertToTSNTable:cache:free", "done, %" NETSNMP_PRIz "d rows\n",
1037 CONTAINER_SIZE(table->container)));
1038 }
1039
1040 static void
1041 _parse_mib_maps(const char *token, char *line)
1042 {
1043 netsnmp_tdata_row *row;
1044 certToTSN_entry *entry;
1045 netsnmp_cert_map *map = netsnmp_certToTSN_parse_common(&line);
1046
1047 if (NULL == line) {
1048 netsnmp_config_error("incomplete line");
1049 netsnmp_cert_map_free(map);
1050 return;
1051 }
1052
1053 map->flags = NSCM_FROM_MIB | NSCM_NONVOLATILE;
1054 row = _entry_from_map(map);
1055 if (NULL == row) {
1056 netsnmp_cert_map_free(map);
1057 return;
1058 }
1059
1060 entry = (certToTSN_entry*)row->data;
1061 entry->rowStatus = atoi(line);
1062 entry->storageType = ST_NONVOLATILE;
1063
1064 /*
1065 * if row is active, add it to the maps container so it is available
1066 * for use. Do not add it to the table, since it will be added
1067 * during cache_load.
1068 */
1069 if (RS_ACTIVE == entry->rowStatus) {
1070 if (netsnmp_cert_map_add(map) != 0)
1071 netsnmp_cert_map_free(map);
1072 }
1073 else {
1074 netsnmp_cert_map_free(map);
1075 if (netsnmp_tdata_add_row(_table, row) != SNMPERR_SUCCESS)
1076 tlstmCertToTSNTable_removeEntry(NULL, row);
1077 }
1078 }
1079
1080 static int
1081 _save_entry(certToTSN_entry *entry, void *app_type)
1082 {
1083 char *buf = NULL, *hashType, *mapType, *data = NULL;
1084
1085 if (NULL == entry)
1086 return SNMP_ERR_GENERR;
1087
1088 hashType = se_find_label_in_slist("cert_hash_alg", entry->hashType);
1089 if (NULL == hashType) {
1090 snmp_log(LOG_ERR, "skipping entry unknown hash type %d\n",
1091 entry->hashType);
1092 return SNMP_ERR_GENERR;
1093 }
1094 mapType = se_find_label_in_slist("cert_map_type", entry->mapType);
1095 if (TSNM_tlstmCertSpecified == entry->mapType)
1096 data = entry->data;
1097 if (asprintf(&buf, "%s %ld --%s %s --%s %s %d", MAP_MIB_CONFIG_TOKEN,
1098 entry->tlstmCertToTSNID, hashType, entry->fingerprint,
1099 mapType, data ? data : "", entry->rowStatus) < 0) {
1100 return SNMP_ERR_GENERR;
1101 }
1102
1103 DEBUGMSGTL(("tlstmCertToTSNTable:save", "saving '%s'\n", buf));
1104 read_config_store(app_type, buf);
1105 free(buf);
1106
1107 return SNMP_ERR_NOERROR;
1108 }
1109
1110 static int
1111 _save_map(netsnmp_cert_map *map, int row_status, void *app_type)
1112 {
1113 char buf[SNMP_MAXBUF_SMALL], *hashType, *mapType, *data = NULL;
1114
1115 if (NULL == map)
1116 return SNMP_ERR_GENERR;
1117
1118 /** don't store values from conf files */
1119 if (! (map->flags & NSCM_NONVOLATILE)) {
1120 DEBUGMSGT(("tlstmCertToTSNTable:save",
1121 "skipping RO/permanent/volatile row\n"));
1122 return SNMP_ERR_NOERROR;
1123 }
1124
1125 hashType = se_find_label_in_slist("cert_hash_alg", map->hashType);
1126 if (NULL == hashType) {
1127 snmp_log(LOG_ERR, "skipping entry unknown hash type %d\n",
1128 map->hashType);
1129 return SNMP_ERR_GENERR;
1130 }
1131 mapType = se_find_label_in_slist("cert_map_type", map->mapType);
1132 if (TSNM_tlstmCertSpecified == map->mapType)
1133 data = (char*)map->data;
1134 snprintf(buf, sizeof(buf), "%s %d --%s %s --%s %s %d",
1135 MAP_MIB_CONFIG_TOKEN, map->priority, hashType, map->fingerprint,
1136 mapType, data ? data : "", row_status);
1137
1138 DEBUGMSGTL(("tlstmCertToTSNTable:save", "saving '%s'\n", buf));
1139 read_config_store(app_type, buf);
1140
1141 return SNMP_ERR_NOERROR;
1142 }
1143
1144 static int
1145 _save_maps(int majorID, int minorID, void *serverarg, void *clientarg)
1146 {
1147 char sep[] =
1148 "\n##############################################################";
1149 char buf[] =
1150 "#\n" "# certificate secName mapping persistent data\n" "#";
1151 char *type = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
1152 NETSNMP_DS_LIB_APPTYPE);
1153 netsnmp_container *maps = netsnmp_cert_map_container();
1154 netsnmp_tdata_row *row;
1155 netsnmp_iterator *tbl_itr, *map_itr;
1156 netsnmp_cert_map *map;
1157 certToTSN_entry *entry;
1158
1159 if ((NULL == maps) || ((CONTAINER_SIZE(maps) == 0) &&
1160 (CONTAINER_SIZE(_table->container) == 0)))
1161 return SNMPERR_SUCCESS;
1162
1163 read_config_store((char *) type, sep);
1164 read_config_store((char *) type, buf);
1165
1166 /*
1167 * save active rows from maps
1168 */
1169 if (NULL != maps) {
1170 map_itr = CONTAINER_ITERATOR(maps);
1171 if (NULL == map_itr) {
1172 DEBUGMSGTL(("tlstmCertToTSNTable:save",
1173 "cant get map iterator\n"));
1174 map = NULL;
1175 }
1176 else
1177 map = ITERATOR_FIRST(map_itr);
1178
1179 for( ; map; map = ITERATOR_NEXT(map_itr)) {
1180 /** don't store config rows */
1181 if (map->flags & NSCM_FROM_CONFIG)
1182 continue;
1183 _save_map(map, RS_ACTIVE, type);
1184 }
1185 ITERATOR_RELEASE(map_itr);
1186 }
1187
1188 /*
1189 * save inactive rows from mib
1190 */
1191 tbl_itr = CONTAINER_ITERATOR(_table->container);
1192 if (NULL == tbl_itr)
1193 DEBUGMSGTL(("tlstmCertToTSNTable:save", "cant get table iterator\n"));
1194 else {
1195 row = ITERATOR_FIRST(tbl_itr);
1196 for( ; row; row = ITERATOR_NEXT(tbl_itr)) {
1197 entry = row->data;
1198
1199 /*
1200 * skip all active rows (should be in maps and thus saved
1201 * above) and volatile rows.
1202 */
1203 if ((entry->rowStatus == RS_ACTIVE) ||
1204 (entry->storageType != ST_NONVOLATILE))
1205 continue;
1206
1207 _save_entry(entry, type);
1208 }
1209 ITERATOR_RELEASE(tbl_itr);
1210 }
1211
1212 read_config_store((char *) type, sep);
1213 read_config_store((char *) type, "\n");
1214
1215 /*
1216 * never fails
1217 */
1218 return SNMPERR_SUCCESS;
1219 }
1220
1221 void
1222 init_snmpTlstmCertToTSNTable_context(const char *contextName)
1223 {
1224 oid reg_oid[] = { SNMP_TLS_TM_CERT_TABLE };
1225 const size_t reg_oid_len = OID_LENGTH(reg_oid);
1226 netsnmp_cache *cache;
1227 netsnmp_watcher_info *watcher;
1228 static const char mib_map_help[] =
1229 MAP_MIB_CONFIG_TOKEN " table persistence (internal use)";
1230 int rc;
1231
1232 DEBUGMSGTL(("tlstmCertToSN:init",
1233 "initializing table tlstmCertToTSNTable\n"));
1234
1235 to_tsn_reg = netsnmp_create_handler_registration
1236 ("tlstmCertToTSNTable", tlstmCertToTSNTable_handler,
1237 reg_oid, reg_oid_len,
1238 HANDLER_CAN_RWRITE);
1239 if (!to_tsn_reg) {
1240 snmp_log(LOG_ERR,
1241 "error creating handler registration for tlstmCertToSN\n");
1242 return;
1243 }
1244
1245 if (NULL != contextName)
1246 to_tsn_reg->contextName = strdup(contextName);
1247
1248 _table = netsnmp_tdata_create_table("tlstmCertToTSNTable", 0);
1249 if (NULL == _table) {
1250 snmp_log(LOG_ERR,
1251 "error creating tdata table for tlstmCertToTSNTable\n");
1252 return;
1253 }
1254 to_tsn_table = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
1255 if (!to_tsn_table) {
1256 snmp_log(LOG_ERR,
1257 "error creating table info for tlstmCertToTSNTable\n");
1258 netsnmp_tdata_delete_table(_table);
1259 _table = NULL;
1260 return;
1261 }
1262 netsnmp_table_helper_add_indexes(to_tsn_table,
1263 /* index: tlstmCertToTSNID */
1264 ASN_UNSIGNED, 0);
1265
1266 to_tsn_table->min_column = SNMPTLSTMCERTTOTSN_TABLE_MIN_COL;
1267 to_tsn_table->max_column = SNMPTLSTMCERTTOTSN_TABLE_MAX_COL;
1268
1269 /*
1270 * cache init
1271 */
1272 cache = netsnmp_cache_create(30, _cache_load, _cache_free,
1273 reg_oid, reg_oid_len);
1274 if (NULL == cache) {
1275 snmp_log(LOG_ERR,"error creating cache for tlstmCertToTSNTable\n");
1276 netsnmp_tdata_delete_table(_table);
1277 _table = NULL;
1278 return;
1279 }
1280 cache->magic = (void *)_table;
1281 cache->flags = NETSNMP_CACHE_DONT_INVALIDATE_ON_SET;
1282
1283 rc = netsnmp_tdata_register(to_tsn_reg, _table, to_tsn_table);
1284 if (rc) {
1285 snmp_log(LOG_ERR, "%s: netsnmp_tdata_register() returned %d\n",
1286 __func__, rc);
1287 return;
1288 }
1289 if (cache)
1290 netsnmp_inject_handler_before(to_tsn_reg,
1291 netsnmp_cache_handler_get(cache),
1292 "table_container");
1293
1294 /*
1295 * register scalars
1296 */
1297 reg_oid[10] = 1;
1298 to_tsn_count_reg =
1299 netsnmp_create_handler_registration("snmpTlstmCertToTSNCount",
1300 _count_handler, reg_oid,
1301 OID_LENGTH(reg_oid),
1302 HANDLER_CAN_RONLY);
1303 if (!to_tsn_count_reg)
1304 snmp_log(LOG_ERR,
1305 "could not create handler for snmpTlstmCertToTSNCount\n");
1306 else {
1307 int rc;
1308
1309 if (NULL != contextName)
1310 to_tsn_count_reg->contextName = strdup(contextName);
1311
1312 rc = netsnmp_register_scalar(to_tsn_count_reg);
1313 if (rc) {
1314 snmp_log(LOG_ERR, "%s: netsnmp_register_scalar() returned %d\n",
1315 __func__, rc);
1316 return;
1317 }
1318 if (cache)
1319 netsnmp_inject_handler_before(to_tsn_count_reg,
1320 netsnmp_cache_handler_get(cache),
1321 "snmpTlstmCertToTSNCount");
1322 }
1323
1324 reg_oid[10] = 2;
1325 watcher = netsnmp_create_watcher_info((void*)&_last_changed,
1326 sizeof(_last_changed),
1327 ASN_TIMETICKS,
1328 WATCHER_FIXED_SIZE);
1329 if (watcher) {
1330 to_tsn_last_changed_reg = netsnmp_create_handler_registration(
1331 "snmpTlstmCertToTSNTableLastChanged", NULL, reg_oid,
1332 OID_LENGTH(reg_oid), HANDLER_CAN_RONLY);
1333 }
1334 if (!watcher || !to_tsn_last_changed_reg) {
1335 snmp_log(LOG_ERR,
1336 "could not create handler for snmpTlstmCertToTSNCount\n");
1337 free(watcher);
1338 }
1339 else {
1340 if (NULL != contextName)
1341 to_tsn_last_changed_reg->contextName = strdup(contextName);
1342 netsnmp_register_watched_scalar2(to_tsn_last_changed_reg, watcher);
1343 }
1344
1345 /*
1346 * persistence
1347 */
1348 register_config_handler(NULL, MAP_MIB_CONFIG_TOKEN, _parse_mib_maps, NULL,
1349 mib_map_help);
1350 if (snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
1351 _save_maps, NULL) != SNMP_ERR_NOERROR)
1352 snmp_log(LOG_ERR, "error registering for STORE_DATA callback "
1353 "for certToTSN\n");
1354
1355 }
1356
1357 /** Initializes the tlstmCertToTSNTable module */
1358 void
1359 init_snmpTlstmCertToTSNTable(void)
1360 {
1361 init_snmpTlstmCertToTSNTable_context(NULL);
1362 }
1363
1364 void
1365 shutdown_snmpTlstmCertToTSNTable(void)
1366 {
1367 netsnmp_tdata_unregister(to_tsn_last_changed_reg);
1368 netsnmp_tdata_unregister(to_tsn_count_reg);
1369 netsnmp_tdata_unregister(to_tsn_reg);
1370 netsnmp_table_registration_info_free(to_tsn_table);
1371 }