2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
5 * Copyright (c) 2004-2006
9 * Author: Harti Brandt <harti@freebsd.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Begemot: action.c 517 2006-10-31 08:52:04Z brandt_h $
34 * Variable access for SNMPd
36 #include <sys/types.h>
37 #include <sys/queue.h>
38 #include <sys/sysctl.h>
40 #include <sys/utsname.h>
55 static const struct asn_oid
56 oid_begemotSnmpdModuleTable = OIDX_begemotSnmpdModuleTable;
59 static const struct asn_oid
60 oid_freeBSDVersion = OIDX_freeBSDVersion;
64 * Get an integer value from the KERN sysctl subtree.
67 act_getkernint(int id)
77 if (sysctl(mib, 2, &value, &len, NULL, 0) != 0)
80 if ((string = malloc(20)) == NULL)
82 sprintf(string, "%lu", value);
87 * Initialize global variables of the system group.
100 if (uname(&uts) == -1)
103 if ((systemg.name = strdup(uts.nodename)) == NULL)
106 if ((hostid = act_getkernint(KERN_HOSTID)) == NULL)
109 len = strlen(uts.nodename) + 1;
110 len += strlen(hostid) + 1;
111 len += strlen(uts.sysname) + 1;
112 len += strlen(uts.release) + 1;
114 if ((systemg.descr = malloc(len)) == NULL) {
118 sprintf(systemg.descr, "%s %s %s %s", uts.nodename, hostid, uts.sysname,
123 * Construct a FreeBSD oid
125 systemg.object_id = oid_freeBSDVersion;
127 while ((p = strsep(&rel, ".")) != NULL &&
128 systemg.object_id.len < ASN_MAXOIDLEN) {
129 systemg.object_id.subs[systemg.object_id.len] = 0;
131 num = strtoul(p, &end, 10);
134 systemg.object_id.subs[systemg.object_id.len] = num;
136 systemg.object_id.len++;
146 * Initialize global variables of the snmpEngine group.
149 init_snmpd_engine(void)
153 snmpd_engine.engine_boots = 1;
154 snmpd_engine.engine_time = 1;
155 snmpd_engine.max_msg_size = 1500; /* XXX */
157 snmpd_engine.engine_id[0] = ((OID_freeBSD & 0xff000000) >> 24) | 0x80;
158 snmpd_engine.engine_id[1] = (OID_freeBSD & 0xff0000) >> 16;
159 snmpd_engine.engine_id[2] = (OID_freeBSD & 0xff00) >> 8;
160 snmpd_engine.engine_id[3] = OID_freeBSD & 0xff;
161 snmpd_engine.engine_id[4] = 128;
162 snmpd_engine.engine_len = 5;
164 if ((hostid = act_getkernint(KERN_HOSTID)) == NULL)
167 if (strlen(hostid) > SNMP_ENGINE_ID_SIZ - snmpd_engine.engine_len) {
168 memcpy(snmpd_engine.engine_id + snmpd_engine.engine_len,
169 hostid, SNMP_ENGINE_ID_SIZ - snmpd_engine.engine_len);
170 snmpd_engine.engine_len = SNMP_ENGINE_ID_SIZ;
172 memcpy(snmpd_engine.engine_id + snmpd_engine.engine_len,
173 hostid, strlen(hostid));
174 snmpd_engine.engine_len += strlen(hostid);
183 set_snmpd_engine(void)
187 uint8_t *cptr, engine[2 * SNMP_ENGINE_ID_SIZ + 2];
188 uint8_t myengine[2 * SNMP_ENGINE_ID_SIZ + 2];
190 if (engine_file[0] == '\0')
194 for (i = 0; i < snmpd_engine.engine_len; i++)
195 cptr += sprintf(cptr, "%.2x", snmpd_engine.engine_id[i]);
199 if ((fp = fopen(engine_file, "r+")) != NULL) {
200 if (fgets(engine, sizeof(engine) - 1, fp) == NULL ||
201 fscanf(fp, "%u", &snmpd_engine.engine_boots) <= 0) {
207 if (strcmp(myengine, engine) != 0)
208 snmpd_engine.engine_boots = 1;
210 snmpd_engine.engine_boots++;
211 } else if (errno != ENOENT)
215 if ((fp = fopen(engine_file, "w+")) == NULL)
217 fprintf(fp, "%s%u\n", myengine, snmpd_engine.engine_boots);
224 update_snmpd_engine_time(void)
228 etime = (get_ticks() - start_tick) / 100ULL;
229 if (etime < INT32_MAX)
230 snmpd_engine.engine_time = etime;
232 start_tick = get_ticks();
233 (void)set_snmpd_engine();
234 snmpd_engine.engine_time = start_tick;
238 /*************************************************************
243 op_system_group(struct snmp_context *ctx, struct snmp_value *value,
244 u_int sub, u_int iidx __unused, enum snmp_op op)
246 asn_subid_t which = value->var.subs[sub - 1];
250 case SNMP_OP_GETNEXT:
260 if (community != COMM_INITIALIZE)
261 return (SNMP_ERR_NOT_WRITEABLE);
262 return (string_save(value, ctx, -1, &systemg.descr));
264 case LEAF_sysObjectId:
265 if (community != COMM_INITIALIZE)
266 return (SNMP_ERR_NOT_WRITEABLE);
267 return (oid_save(value, ctx, &systemg.object_id));
269 case LEAF_sysContact:
270 return (string_save(value, ctx, -1, &systemg.contact));
273 return (string_save(value, ctx, -1, &systemg.name));
275 case LEAF_sysLocation:
276 return (string_save(value, ctx, -1, &systemg.location));
278 return (SNMP_ERR_NO_CREATION);
280 case SNMP_OP_ROLLBACK:
284 string_rollback(ctx, &systemg.descr);
285 return (SNMP_ERR_NOERROR);
286 case LEAF_sysObjectId:
287 oid_rollback(ctx, &systemg.object_id);
288 return (SNMP_ERR_NOERROR);
289 case LEAF_sysContact:
290 string_rollback(ctx, &systemg.contact);
291 return (SNMP_ERR_NOERROR);
293 string_rollback(ctx, &systemg.name);
294 return (SNMP_ERR_NOERROR);
295 case LEAF_sysLocation:
296 string_rollback(ctx, &systemg.location);
297 return (SNMP_ERR_NOERROR);
306 return (SNMP_ERR_NOERROR);
307 case LEAF_sysObjectId:
309 return (SNMP_ERR_NOERROR);
310 case LEAF_sysContact:
312 return (SNMP_ERR_NOERROR);
315 return (SNMP_ERR_NOERROR);
316 case LEAF_sysLocation:
318 return (SNMP_ERR_NOERROR);
329 return (string_get(value, systemg.descr, -1));
330 case LEAF_sysObjectId:
331 return (oid_get(value, &systemg.object_id));
333 value->v.uint32 = get_ticks() - start_tick;
335 case LEAF_sysContact:
336 return (string_get(value, systemg.contact, -1));
338 return (string_get(value, systemg.name, -1));
339 case LEAF_sysLocation:
340 return (string_get(value, systemg.location, -1));
341 case LEAF_sysServices:
342 value->v.integer = systemg.services;
344 case LEAF_sysORLastChange:
345 value->v.uint32 = systemg.or_last_change;
348 return (SNMP_ERR_NOERROR);
351 /*************************************************************
356 op_debug(struct snmp_context *ctx, struct snmp_value *value, u_int sub,
357 u_int iidx __unused, enum snmp_op op)
359 asn_subid_t which = value->var.subs[sub - 1];
363 case SNMP_OP_GETNEXT:
369 case LEAF_begemotSnmpdDebugDumpPdus:
370 value->v.integer = TRUTH_MK(debug.dump_pdus);
373 case LEAF_begemotSnmpdDebugSnmpTrace:
374 value->v.uint32 = snmp_trace;
377 case LEAF_begemotSnmpdDebugSyslogPri:
378 value->v.integer = debug.logpri;
381 return (SNMP_ERR_NOERROR);
386 case LEAF_begemotSnmpdDebugDumpPdus:
387 if (!TRUTH_OK(value->v.integer))
388 return (SNMP_ERR_WRONG_VALUE);
389 ctx->scratch->int1 = debug.dump_pdus;
390 debug.dump_pdus = TRUTH_GET(value->v.integer);
391 return (SNMP_ERR_NOERROR);
393 case LEAF_begemotSnmpdDebugSnmpTrace:
394 ctx->scratch->int1 = snmp_trace;
395 snmp_trace = value->v.uint32;
396 return (SNMP_ERR_NOERROR);
398 case LEAF_begemotSnmpdDebugSyslogPri:
399 if (value->v.integer < 0 || value->v.integer > 8)
400 return (SNMP_ERR_WRONG_VALUE);
401 ctx->scratch->int1 = debug.logpri;
402 debug.logpri = (u_int)value->v.integer;
403 return (SNMP_ERR_NOERROR);
405 return (SNMP_ERR_NO_CREATION);
407 case SNMP_OP_ROLLBACK:
410 case LEAF_begemotSnmpdDebugDumpPdus:
411 debug.dump_pdus = ctx->scratch->int1;
412 return (SNMP_ERR_NOERROR);
414 case LEAF_begemotSnmpdDebugSnmpTrace:
415 snmp_trace = ctx->scratch->int1;
416 return (SNMP_ERR_NOERROR);
418 case LEAF_begemotSnmpdDebugSyslogPri:
419 debug.logpri = ctx->scratch->int1;
420 return (SNMP_ERR_NOERROR);
427 case LEAF_begemotSnmpdDebugDumpPdus:
428 case LEAF_begemotSnmpdDebugSnmpTrace:
429 return (SNMP_ERR_NOERROR);
431 case LEAF_begemotSnmpdDebugSyslogPri:
432 if (debug.logpri == 0)
435 setlogmask(LOG_UPTO(debug.logpri - 1));
436 return (SNMP_ERR_NOERROR);
443 /*************************************************************
448 op_or_table(struct snmp_context *ctx __unused, struct snmp_value *value,
449 u_int sub, u_int iidx __unused, enum snmp_op op)
451 struct objres *objres;
455 case SNMP_OP_GETNEXT:
456 if ((objres = NEXT_OBJECT_INT(&objres_list, &value->var, sub))
458 return (SNMP_ERR_NOSUCHNAME);
459 value->var.subs[sub] = objres->index;
460 value->var.len = sub + 1;
464 if ((objres = FIND_OBJECT_INT(&objres_list, &value->var, sub))
466 return (SNMP_ERR_NOSUCHNAME);
470 if ((objres = FIND_OBJECT_INT(&objres_list, &value->var, sub))
472 return (SNMP_ERR_NO_CREATION);
473 return (SNMP_ERR_NOT_WRITEABLE);
475 case SNMP_OP_ROLLBACK:
482 * Come here for GET, GETNEXT.
484 switch (value->var.subs[sub - 1]) {
487 value->v.oid = objres->oid;
490 case LEAF_sysORDescr:
491 return (string_get(value, objres->descr, -1));
493 case LEAF_sysORUpTime:
494 value->v.uint32 = objres->uptime;
497 return (SNMP_ERR_NOERROR);
500 /*************************************************************
505 op_snmp(struct snmp_context *ctx, struct snmp_value *value,
506 u_int sub, u_int iidx __unused, enum snmp_op op)
510 case SNMP_OP_GETNEXT:
514 switch (value->var.subs[sub - 1]) {
516 case LEAF_snmpInPkts:
517 value->v.uint32 = snmpd_stats.inPkts;
520 case LEAF_snmpInBadVersions:
521 value->v.uint32 = snmpd_stats.inBadVersions;
524 case LEAF_snmpInBadCommunityNames:
525 value->v.uint32 = snmpd_stats.inBadCommunityNames;
528 case LEAF_snmpInBadCommunityUses:
529 value->v.uint32 = snmpd_stats.inBadCommunityUses;
532 case LEAF_snmpInASNParseErrs:
533 value->v.uint32 = snmpd_stats.inASNParseErrs;
536 case LEAF_snmpEnableAuthenTraps:
537 value->v.integer = TRUTH_MK(snmpd.auth_traps);
540 case LEAF_snmpSilentDrops:
541 value->v.uint32 = snmpd_stats.silentDrops;
544 case LEAF_snmpProxyDrops:
545 value->v.uint32 = snmpd_stats.proxyDrops;
549 return (SNMP_ERR_NOSUCHNAME);
552 return (SNMP_ERR_NOERROR);
555 switch (value->var.subs[sub - 1]) {
556 case LEAF_snmpEnableAuthenTraps:
557 if (!TRUTH_OK(value->v.integer))
558 return (SNMP_ERR_WRONG_VALUE);
559 ctx->scratch->int1 = value->v.integer;
560 snmpd.auth_traps = TRUTH_GET(value->v.integer);
561 return (SNMP_ERR_NOERROR);
565 case SNMP_OP_ROLLBACK:
566 switch (value->var.subs[sub - 1]) {
567 case LEAF_snmpEnableAuthenTraps:
568 snmpd.auth_traps = ctx->scratch->int1;
569 return (SNMP_ERR_NOERROR);
574 switch (value->var.subs[sub - 1]) {
575 case LEAF_snmpEnableAuthenTraps:
576 return (SNMP_ERR_NOERROR);
583 /*************************************************************
585 * SNMPd statistics group
588 op_snmpd_stats(struct snmp_context *ctx __unused, struct snmp_value *value,
589 u_int sub, u_int iidx __unused, enum snmp_op op)
594 switch (value->var.subs[sub - 1]) {
596 case LEAF_begemotSnmpdStatsNoRxBufs:
597 value->v.uint32 = snmpd_stats.noRxbuf;
600 case LEAF_begemotSnmpdStatsNoTxBufs:
601 value->v.uint32 = snmpd_stats.noTxbuf;
604 case LEAF_begemotSnmpdStatsInTooLongPkts:
605 value->v.uint32 = snmpd_stats.inTooLong;
608 case LEAF_begemotSnmpdStatsInBadPduTypes:
609 value->v.uint32 = snmpd_stats.inBadPduTypes;
613 return (SNMP_ERR_NOSUCHNAME);
615 return (SNMP_ERR_NOERROR);
618 case SNMP_OP_ROLLBACK:
620 case SNMP_OP_GETNEXT:
627 * SNMPd configuration scalars
630 op_snmpd_config(struct snmp_context *ctx, struct snmp_value *value,
631 u_int sub, u_int iidx __unused, enum snmp_op op)
633 asn_subid_t which = value->var.subs[sub - 1];
637 case SNMP_OP_GETNEXT:
643 case LEAF_begemotSnmpdTransmitBuffer:
644 value->v.integer = snmpd.txbuf;
646 case LEAF_begemotSnmpdReceiveBuffer:
647 value->v.integer = snmpd.rxbuf;
649 case LEAF_begemotSnmpdCommunityDisable:
650 value->v.integer = TRUTH_MK(snmpd.comm_dis);
652 case LEAF_begemotSnmpdTrap1Addr:
653 return (ip_get(value, snmpd.trap1addr));
654 case LEAF_begemotSnmpdVersionEnable:
655 value->v.uint32 = snmpd.version_enable;
658 return (SNMP_ERR_NOSUCHNAME);
660 return (SNMP_ERR_NOERROR);
665 case LEAF_begemotSnmpdTransmitBuffer:
666 ctx->scratch->int1 = snmpd.txbuf;
667 if (value->v.integer < 484 ||
668 value->v.integer > 65535)
669 return (SNMP_ERR_WRONG_VALUE);
670 snmpd.txbuf = value->v.integer;
671 return (SNMP_ERR_NOERROR);
673 case LEAF_begemotSnmpdReceiveBuffer:
674 ctx->scratch->int1 = snmpd.rxbuf;
675 if (value->v.integer < 484 ||
676 value->v.integer > 65535)
677 return (SNMP_ERR_WRONG_VALUE);
678 snmpd.rxbuf = value->v.integer;
679 return (SNMP_ERR_NOERROR);
681 case LEAF_begemotSnmpdCommunityDisable:
682 ctx->scratch->int1 = snmpd.comm_dis;
683 if (!TRUTH_OK(value->v.integer))
684 return (SNMP_ERR_WRONG_VALUE);
685 if (TRUTH_GET(value->v.integer)) {
689 return (SNMP_ERR_WRONG_VALUE);
691 return (SNMP_ERR_NOERROR);
693 case LEAF_begemotSnmpdTrap1Addr:
694 return (ip_save(value, ctx, snmpd.trap1addr));
696 case LEAF_begemotSnmpdVersionEnable:
697 if (community != COMM_INITIALIZE)
698 return (SNMP_ERR_NOT_WRITEABLE);
699 ctx->scratch->int1 = snmpd.version_enable;
700 if (value->v.uint32 == 0 ||
701 (value->v.uint32 & ~VERS_ENABLE_ALL))
702 return (SNMP_ERR_WRONG_VALUE);
703 snmpd.version_enable = value->v.uint32;
704 return (SNMP_ERR_NOERROR);
708 case SNMP_OP_ROLLBACK:
711 case LEAF_begemotSnmpdTransmitBuffer:
712 snmpd.rxbuf = ctx->scratch->int1;
713 return (SNMP_ERR_NOERROR);
714 case LEAF_begemotSnmpdReceiveBuffer:
715 snmpd.txbuf = ctx->scratch->int1;
716 return (SNMP_ERR_NOERROR);
717 case LEAF_begemotSnmpdCommunityDisable:
718 snmpd.comm_dis = ctx->scratch->int1;
719 return (SNMP_ERR_NOERROR);
720 case LEAF_begemotSnmpdTrap1Addr:
721 ip_rollback(ctx, snmpd.trap1addr);
722 return (SNMP_ERR_NOERROR);
723 case LEAF_begemotSnmpdVersionEnable:
724 snmpd.version_enable = ctx->scratch->int1;
725 return (SNMP_ERR_NOERROR);
732 case LEAF_begemotSnmpdTransmitBuffer:
733 case LEAF_begemotSnmpdReceiveBuffer:
734 case LEAF_begemotSnmpdCommunityDisable:
735 return (SNMP_ERR_NOERROR);
736 case LEAF_begemotSnmpdTrap1Addr:
738 return (SNMP_ERR_NOERROR);
739 case LEAF_begemotSnmpdVersionEnable:
740 return (SNMP_ERR_NOERROR);
748 * The community table
751 op_community(struct snmp_context *ctx, struct snmp_value *value,
752 u_int sub, u_int iidx __unused, enum snmp_op op)
756 asn_subid_t which = value->var.subs[sub - 1];
760 case SNMP_OP_GETNEXT:
761 if ((community != COMM_INITIALIZE && snmpd.comm_dis) ||
762 (c = NEXT_OBJECT_OID(&community_list, &value->var, sub)) == NULL)
763 return (SNMP_ERR_NOSUCHNAME);
764 index_append(&value->var, sub, &c->index);
768 if ((community != COMM_INITIALIZE && snmpd.comm_dis) ||
769 (c = FIND_OBJECT_OID(&community_list, &value->var, sub)) == NULL)
770 return (SNMP_ERR_NOSUCHNAME);
774 if (community != COMM_INITIALIZE && snmpd.comm_dis)
775 return (SNMP_ERR_NOT_WRITEABLE);
778 idx.subs[1] = value->var.subs[value->var.len - 1];
780 case LEAF_begemotSnmpdCommunityString:
781 /* check that given string is unique */
782 TAILQ_FOREACH(c, &community_list, link) {
783 if (!asn_compare_oid(&idx, &c->index))
785 if (c->string != NULL && strcmp(c->string,
786 value->v.octetstring.octets) == 0)
787 return (SNMP_ERR_WRONG_VALUE);
789 case LEAF_begemotSnmpdCommunityPermission:
792 return (SNMP_ERR_NOT_WRITEABLE);
794 if ((c = FIND_OBJECT_OID(&community_list, &value->var,
796 /* create new community and use user sepcified index */
797 c = comm_define_ordered(COMM_READ, "SNMP Custom Community",
800 return (SNMP_ERR_NO_CREATION);
803 case LEAF_begemotSnmpdCommunityString:
804 return (string_save(value, ctx, -1, &c->string));
805 case LEAF_begemotSnmpdCommunityPermission:
806 if (value->v.integer != COMM_READ &&
807 value->v.integer != COMM_WRITE)
808 return (SNMP_ERR_WRONG_VALUE);
809 c->private = value->v.integer;
812 return (SNMP_ERR_NOT_WRITEABLE);
814 return (SNMP_ERR_NOERROR);
816 case SNMP_OP_ROLLBACK:
817 if (which == LEAF_begemotSnmpdCommunityString) {
818 if ((c = FIND_OBJECT_OID(&community_list, &value->var,
822 string_rollback(ctx, &c->string);
823 return (SNMP_ERR_NOERROR);
825 if (which == LEAF_begemotSnmpdCommunityPermission)
826 return (SNMP_ERR_NOERROR);
830 if (which == LEAF_begemotSnmpdCommunityString) {
831 if ((c = FIND_OBJECT_OID(&community_list, &value->var,
836 return (SNMP_ERR_NOERROR);
838 if (which == LEAF_begemotSnmpdCommunityPermission)
839 return (SNMP_ERR_NOERROR);
848 case LEAF_begemotSnmpdCommunityString:
849 return (string_get(value, c->string, -1));
851 case LEAF_begemotSnmpdCommunityDescr:
852 return (string_get(value, c->descr, -1));
854 case LEAF_begemotSnmpdCommunityPermission:
855 value->v.integer = c->private;
856 return (SNMP_ERR_NOERROR);
858 return (SNMP_ERR_NOT_WRITEABLE);
867 struct snmp_dependency dep;
868 u_char section[LM_SECTION_MAX + 1];
874 dep_modules(struct snmp_context *ctx, struct snmp_dependency *dep,
877 struct module_dep *mdep = (struct module_dep *)(void *)dep;
881 case SNMP_DEPOP_COMMIT:
882 if (mdep->path == NULL) {
883 /* unload - find the module */
884 TAILQ_FOREACH(mdep->m, &lmodules, link)
885 if (strcmp(mdep->m->section,
889 /* no such module - that's ok */
890 return (SNMP_ERR_NOERROR);
892 /* handle unloading in the finalizer */
893 return (SNMP_ERR_NOERROR);
896 if ((mdep->m = lm_load(mdep->path, mdep->section)) == NULL) {
898 return (SNMP_ERR_RES_UNAVAIL);
900 /* start in finalizer */
901 return (SNMP_ERR_NOERROR);
903 case SNMP_DEPOP_ROLLBACK:
904 if (mdep->path == NULL) {
905 /* rollback unload - the finalizer takes care */
906 return (SNMP_ERR_NOERROR);
910 return (SNMP_ERR_NOERROR);
912 case SNMP_DEPOP_FINISH:
913 if (mdep->path == NULL) {
914 if (mdep->m != NULL && ctx->code == SNMP_RET_OK)
917 if (mdep->m != NULL && ctx->code == SNMP_RET_OK &&
918 community != COMM_INITIALIZE)
922 return (SNMP_ERR_NOERROR);
928 op_modules(struct snmp_context *ctx, struct snmp_value *value,
929 u_int sub, u_int iidx, enum snmp_op op)
931 asn_subid_t which = value->var.subs[sub - 1];
933 u_char *section, *ptr;
935 struct module_dep *mdep;
940 case SNMP_OP_GETNEXT:
941 if ((m = NEXT_OBJECT_OID(&lmodules, &value->var, sub)) == NULL)
942 return (SNMP_ERR_NOSUCHNAME);
943 index_append(&value->var, sub, &m->index);
947 if ((m = FIND_OBJECT_OID(&lmodules, &value->var, sub)) == NULL)
948 return (SNMP_ERR_NOSUCHNAME);
952 m = FIND_OBJECT_OID(&lmodules, &value->var, sub);
953 if (which != LEAF_begemotSnmpdModulePath) {
955 return (SNMP_ERR_NO_CREATION);
956 return (SNMP_ERR_NOT_WRITEABLE);
959 /* the errors in the next few statements can only happen when
960 * m is NULL, hence the NO_CREATION error. */
961 if (index_decode(&value->var, sub, iidx,
963 return (SNMP_ERR_NO_CREATION);
965 /* check the section name */
966 if (seclen > LM_SECTION_MAX || seclen == 0) {
968 return (SNMP_ERR_NO_CREATION);
970 for (ptr = section; ptr < section + seclen; ptr++)
971 if (!isascii(*ptr) || !isalnum(*ptr)) {
973 return (SNMP_ERR_NO_CREATION);
975 if (!isalpha(section[0])) {
977 return (SNMP_ERR_NO_CREATION);
981 for (ptr = value->v.octetstring.octets;
982 ptr < value->v.octetstring.octets + value->v.octetstring.len;
986 return (SNMP_ERR_WRONG_VALUE);
991 if (value->v.octetstring.len == 0) {
993 return (SNMP_ERR_INCONS_VALUE);
996 if (value->v.octetstring.len != 0) {
998 return (SNMP_ERR_INCONS_VALUE);
1002 asn_slice_oid(&idx, &value->var, sub, value->var.len);
1004 /* so far, so good */
1005 mdep = (struct module_dep *)(void *)snmp_dep_lookup(ctx,
1006 &oid_begemotSnmpdModuleTable, &idx,
1007 sizeof(*mdep), dep_modules);
1010 return (SNMP_ERR_RES_UNAVAIL);
1013 if (mdep->section[0] != '\0') {
1014 /* two writes to the same entry - bad */
1016 return (SNMP_ERR_INCONS_VALUE);
1019 strncpy(mdep->section, section, seclen);
1020 mdep->section[seclen] = '\0';
1023 if (value->v.octetstring.len == 0)
1026 if ((mdep->path = malloc(value->v.octetstring.len + 1)) == NULL)
1027 return (SNMP_ERR_RES_UNAVAIL);
1028 strncpy(mdep->path, value->v.octetstring.octets,
1029 value->v.octetstring.len);
1030 mdep->path[value->v.octetstring.len] = '\0';
1032 ctx->scratch->ptr1 = mdep;
1033 return (SNMP_ERR_NOERROR);
1035 case SNMP_OP_ROLLBACK:
1036 case SNMP_OP_COMMIT:
1037 return (SNMP_ERR_NOERROR);
1045 case LEAF_begemotSnmpdModulePath:
1046 return (string_get(value, m->path, -1));
1048 case LEAF_begemotSnmpdModuleComment:
1049 return (string_get(value, m->config->comment, -1));
1055 op_snmp_set(struct snmp_context *ctx __unused, struct snmp_value *value,
1056 u_int sub, u_int iidx __unused, enum snmp_op op)
1060 case SNMP_OP_GETNEXT:
1064 switch (value->var.subs[sub - 1]) {
1066 case LEAF_snmpSetSerialNo:
1067 value->v.integer = snmp_serial_no;
1073 return (SNMP_ERR_NOERROR);
1076 switch (value->var.subs[sub - 1]) {
1078 case LEAF_snmpSetSerialNo:
1079 if (value->v.integer != snmp_serial_no)
1080 return (SNMP_ERR_INCONS_VALUE);
1086 return (SNMP_ERR_NOERROR);
1088 case SNMP_OP_ROLLBACK:
1089 return (SNMP_ERR_NOERROR);
1091 case SNMP_OP_COMMIT:
1092 if (snmp_serial_no++ == 2147483647)
1094 return (SNMP_ERR_NOERROR);
1103 op_snmp_engine(struct snmp_context *ctx __unused, struct snmp_value *value,
1104 u_int sub, u_int iidx __unused, enum snmp_op op)
1106 asn_subid_t which = value->var.subs[sub - 1];
1109 case SNMP_OP_GETNEXT:
1116 if (community != COMM_INITIALIZE)
1117 return (SNMP_ERR_NOT_WRITEABLE);
1119 case LEAF_snmpEngineID:
1120 if (value->v.octetstring.len > SNMP_ENGINE_ID_SIZ)
1121 return (SNMP_ERR_WRONG_VALUE);
1122 ctx->scratch->ptr1 = malloc(snmpd_engine.engine_len);
1123 if (ctx->scratch->ptr1 == NULL)
1124 return (SNMP_ERR_GENERR);
1125 memcpy(ctx->scratch->ptr1, snmpd_engine.engine_id,
1126 snmpd_engine.engine_len);
1127 ctx->scratch->int1 = snmpd_engine.engine_len;
1128 snmpd_engine.engine_len = value->v.octetstring.len;
1129 memcpy(snmpd_engine.engine_id,
1130 value->v.octetstring.octets,
1131 value->v.octetstring.len);
1134 case LEAF_snmpEngineMaxMessageSize:
1135 ctx->scratch->int1 = snmpd_engine.max_msg_size;
1136 snmpd_engine.max_msg_size = value->v.integer;
1140 return (SNMP_ERR_NOT_WRITEABLE);
1142 return (SNMP_ERR_NOERROR);
1144 case SNMP_OP_ROLLBACK:
1146 case LEAF_snmpEngineID:
1147 snmpd_engine.engine_len = ctx->scratch->int1;
1148 memcpy(snmpd_engine.engine_id, ctx->scratch->ptr1,
1149 snmpd_engine.engine_len);
1150 free(ctx->scratch->ptr1);
1153 case LEAF_snmpEngineMaxMessageSize:
1154 snmpd_engine.max_msg_size = ctx->scratch->int1;
1160 return (SNMP_ERR_NOERROR);
1162 case SNMP_OP_COMMIT:
1163 if (which == LEAF_snmpEngineID) {
1164 if (set_snmpd_engine() < 0) {
1165 snmpd_engine.engine_len = ctx->scratch->int1;
1166 memcpy(snmpd_engine.engine_id,
1167 ctx->scratch->ptr1, ctx->scratch->int1);
1169 free(ctx->scratch->ptr1);
1171 return (SNMP_ERR_NOERROR);
1176 case LEAF_snmpEngineID:
1177 return (string_get(value, snmpd_engine.engine_id,
1178 snmpd_engine.engine_len));
1179 case LEAF_snmpEngineBoots:
1180 value->v.integer = snmpd_engine.engine_boots;
1182 case LEAF_snmpEngineTime:
1183 update_snmpd_engine_time();
1184 value->v.integer = snmpd_engine.engine_time;
1186 case LEAF_snmpEngineMaxMessageSize:
1187 value->v.integer = snmpd_engine.max_msg_size;
1190 return (SNMP_ERR_NOSUCHNAME);
1193 return (SNMP_ERR_NOERROR);
1200 op_transport_table(struct snmp_context *ctx __unused, struct snmp_value *value,
1201 u_int sub, u_int iidx, enum snmp_op op)
1203 asn_subid_t which = value->var.subs[sub - 1];
1204 struct transport *t;
1205 u_char *tname, *ptr;
1210 case SNMP_OP_GETNEXT:
1211 if ((t = NEXT_OBJECT_OID(&transport_list, &value->var, sub))
1213 return (SNMP_ERR_NOSUCHNAME);
1214 index_append(&value->var, sub, &t->index);
1218 if ((t = FIND_OBJECT_OID(&transport_list, &value->var, sub))
1220 return (SNMP_ERR_NOSUCHNAME);
1224 t = FIND_OBJECT_OID(&transport_list, &value->var, sub);
1225 if (which != LEAF_begemotSnmpdTransportStatus) {
1227 return (SNMP_ERR_NO_CREATION);
1228 return (SNMP_ERR_NOT_WRITEABLE);
1231 /* the errors in the next few statements can only happen when
1232 * t is NULL, hence the NO_CREATION error. */
1233 if (index_decode(&value->var, sub, iidx,
1235 return (SNMP_ERR_NO_CREATION);
1237 /* check the section name */
1238 if (tnamelen >= TRANS_NAMELEN || tnamelen == 0) {
1240 return (SNMP_ERR_NO_CREATION);
1242 for (ptr = tname; ptr < tname + tnamelen; ptr++) {
1243 if (!isascii(*ptr) || !isalnum(*ptr)) {
1245 return (SNMP_ERR_NO_CREATION);
1250 return (SNMP_ERR_NOT_WRITEABLE);
1252 case SNMP_OP_ROLLBACK:
1253 case SNMP_OP_COMMIT:
1254 return (SNMP_ERR_NOERROR);
1261 case LEAF_begemotSnmpdTransportStatus:
1262 value->v.integer = 1;
1265 case LEAF_begemotSnmpdTransportOid:
1266 memcpy(&value->v.oid, &t->vtab->id, sizeof(t->vtab->id));
1269 return (SNMP_ERR_NOERROR);