2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Copyright (c) 2010 The FreeBSD Foundation
11 * Portions of this software were developed by Shteryana Sotirova Shopova
12 * under sponsorship from the FreeBSD Foundation.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $Begemot: bsnmp/snmpd/main.c,v 1.100 2006/02/14 09:04:20 brandt_h Exp $
40 #include <sys/queue.h>
41 #include <sys/param.h>
43 #include <sys/ucred.h>
58 #ifdef USE_TCPWRAPPERS
59 #include <arpa/inet.h>
69 #define PATH_PID "/var/run/%s.pid"
70 #define PATH_CONFIG "/etc/%s.config"
71 #define PATH_ENGINE "/var/%s.engine"
73 uint64_t this_tick; /* start of processing of current packet (absolute) */
74 uint64_t start_tick; /* start of processing */
76 struct systemg systemg = {
78 { 8, { 1, 3, 6, 1, 4, 1, 1115, 7352 }},
83 struct debug debug = {
85 LOG_DEBUG, /* log_pri */
89 struct snmpd snmpd = {
94 {0, 0, 0, 0}, /* trap1addr */
95 VERS_ENABLE_ALL,/* version_enable */
97 struct snmpd_stats snmpd_stats;
99 struct snmpd_usmstat snmpd_usmstats;
102 struct snmp_engine snmpd_engine;
105 int32_t snmp_serial_no;
107 struct snmpd_target_stats snmpd_target_stats;
109 /* search path for config files */
110 const char *syspath = PATH_SYSCONFIG;
112 /* list of all loaded modules */
113 struct lmodules lmodules = TAILQ_HEAD_INITIALIZER(lmodules);
115 /* list of loaded modules during start-up in the order they were loaded */
116 static struct lmodules modules_start = TAILQ_HEAD_INITIALIZER(modules_start);
118 /* list of all known communities */
119 struct community_list community_list = TAILQ_HEAD_INITIALIZER(community_list);
121 /* list of all known USM users */
122 static struct usm_userlist usm_userlist = SLIST_HEAD_INITIALIZER(usm_userlist);
124 /* A list of all VACM users configured, including v1, v2c and v3 */
125 static struct vacm_userlist vacm_userlist =
126 SLIST_HEAD_INITIALIZER(vacm_userlist);
128 /* A list of all VACM groups */
129 static struct vacm_grouplist vacm_grouplist =
130 SLIST_HEAD_INITIALIZER(vacm_grouplist);
132 static struct vacm_group vacm_default_group = {
136 /* The list of configured access entries */
137 static struct vacm_accesslist vacm_accesslist =
138 TAILQ_HEAD_INITIALIZER(vacm_accesslist);
140 /* The list of configured views */
141 static struct vacm_viewlist vacm_viewlist =
142 SLIST_HEAD_INITIALIZER(vacm_viewlist);
144 /* The list of configured contexts */
145 static struct vacm_contextlist vacm_contextlist =
146 SLIST_HEAD_INITIALIZER(vacm_contextlist);
148 /* list of all installed object resources */
149 struct objres_list objres_list = TAILQ_HEAD_INITIALIZER(objres_list);
151 /* community value generator */
152 static u_int next_community_index = 1;
154 /* list of all known ranges */
155 struct idrange_list idrange_list = TAILQ_HEAD_INITIALIZER(idrange_list);
157 /* identifier generator */
158 u_int next_idrange = 1;
160 /* list of all current timers */
161 struct timer_list timer_list = LIST_HEAD_INITIALIZER(timer_list);
163 /* list of file descriptors */
164 struct fdesc_list fdesc_list = LIST_HEAD_INITIALIZER(fdesc_list);
166 /* program arguments */
167 static char **progargs;
168 static int nprogargs;
170 /* current community */
172 static struct community *comm;
174 /* current USM user */
175 struct usm_user *usm_user;
178 static char config_file[MAXPATHLEN + 1];
179 static char pid_file[MAXPATHLEN + 1];
180 char engine_file[MAXPATHLEN + 1];
182 #ifndef USE_LIBBEGEMOT
184 static evContext evctx;
188 static sigset_t blocked_sigs;
190 /* signal handling */
192 #define WORK_DOINFO 0x0001
193 #define WORK_RECONFIG 0x0002
196 static const struct asn_oid
197 oid_snmpMIB = OIDX_snmpMIB,
198 oid_begemotSnmpd = OIDX_begemotSnmpd,
199 oid_coldStart = OIDX_coldStart,
200 oid_authenticationFailure = OIDX_authenticationFailure;
202 const struct asn_oid oid_zeroDotZero = { 2, { 0, 0 }};
204 const struct asn_oid oid_usmUnknownEngineIDs =
205 { 11, { 1, 3, 6, 1, 6, 3, 15, 1, 1, 4, 0}};
207 const struct asn_oid oid_usmNotInTimeWindows =
208 { 11, { 1, 3, 6, 1, 6, 3, 15, 1, 1, 2, 0}};
210 /* request id generator for traps */
214 static const char usgtxt[] = "\
215 Begemot simple SNMP daemon. Copyright (c) 2001-2002 Fraunhofer Institute for\n\
216 Open Communication Systems (FhG Fokus). All rights reserved.\n\
217 Copyright (c) 2010 The FreeBSD Foundation. All rights reserved.\n\
218 usage: snmpd [-dh] [-c file] [-D options] [-e file] [-I path]\n\
219 [-l prefix] [-m variable=value] [-p file]\n\
221 -d don't daemonize\n\
222 -h print this info\n\
223 -c file specify configuration file\n\
224 -D options debugging options\n\
225 -e file specify engine id file\n\
226 -I path system include path\n\
227 -l prefix default basename for pid and config file\n\
228 -m var=val define variable\n\
229 -p file specify pid file\n\
232 /* hosts_access(3) request */
233 #ifdef USE_TCPWRAPPERS
234 static struct request_info req;
238 extern const struct transport_def udp_trans;
239 extern const struct transport_def lsock_trans;
241 struct transport_list transport_list = TAILQ_HEAD_INITIALIZER(transport_list);
243 /* forward declarations */
244 static void snmp_printf_func(const char *fmt, ...);
245 static void snmp_error_func(const char *err, ...);
246 static void snmp_debug_func(const char *err, ...);
247 static void asn_error_func(const struct asn_buf *b, const char *err, ...);
250 * Allocate rx/tx buffer. We allocate one byte more for rx.
257 if ((buf = malloc(tx ? snmpd.txbuf : snmpd.rxbuf)) == NULL) {
258 syslog(LOG_CRIT, "cannot allocate buffer");
260 snmpd_stats.noTxbuf++;
262 snmpd_stats.noRxbuf++;
269 * Return the buffer size.
274 return (tx ? snmpd.txbuf : snmpd.rxbuf);
278 * Prepare a PDU for output
281 snmp_output(struct snmp_pdu *pdu, u_char *sndbuf, size_t *sndlen,
284 struct asn_buf resp_b;
286 resp_b.asn_ptr = sndbuf;
287 resp_b.asn_len = snmpd.txbuf;
289 if (snmp_pdu_encode(pdu, &resp_b) != 0) {
290 syslog(LOG_ERR, "cannot encode message");
293 if (debug.dump_pdus) {
294 snmp_printf("%s <- ", dest);
297 *sndlen = (size_t)(resp_b.asn_ptr - sndbuf);
301 * Check USM PDU header credentials against local SNMP Engine & users.
303 static enum snmp_code
304 snmp_pdu_auth_user(struct snmp_pdu *pdu)
309 /* un-authenticated snmpEngineId discovery */
310 if (pdu->engine.engine_len == 0 && strlen(pdu->user.sec_name) == 0) {
311 pdu->engine.engine_len = snmpd_engine.engine_len;
312 memcpy(pdu->engine.engine_id, snmpd_engine.engine_id,
313 snmpd_engine.engine_len);
314 pdu->engine.engine_boots = snmpd_engine.engine_boots;
315 pdu->engine.engine_time = snmpd_engine.engine_time;
316 pdu->flags |= SNMP_MSG_AUTODISCOVER;
317 return (SNMP_CODE_OK);
320 if ((usm_user = usm_find_user(pdu->engine.engine_id,
321 pdu->engine.engine_len, pdu->user.sec_name)) == NULL ||
322 usm_user->status != 1 /* active */)
323 return (SNMP_CODE_BADUSER);
325 if (usm_user->user_engine_len != snmpd_engine.engine_len ||
326 memcmp(usm_user->user_engine_id, snmpd_engine.engine_id,
327 snmpd_engine.engine_len) != 0)
328 return (SNMP_CODE_BADENGINE);
330 pdu->user.priv_proto = usm_user->suser.priv_proto;
331 memcpy(pdu->user.priv_key, usm_user->suser.priv_key,
332 sizeof(pdu->user.priv_key));
334 /* authenticated snmpEngineId discovery */
335 if ((pdu->flags & SNMP_MSG_AUTH_FLAG) != 0) {
336 etime = (get_ticks() - start_tick) / 100ULL;
337 if (etime < INT32_MAX)
338 snmpd_engine.engine_time = etime;
340 start_tick = get_ticks();
342 snmpd_engine.engine_time = start_tick;
345 pdu->user.auth_proto = usm_user->suser.auth_proto;
346 memcpy(pdu->user.auth_key, usm_user->suser.auth_key,
347 sizeof(pdu->user.auth_key));
349 if (pdu->engine.engine_boots == 0 &&
350 pdu->engine.engine_time == 0) {
351 pdu->flags |= SNMP_MSG_AUTODISCOVER;
352 return (SNMP_CODE_OK);
355 if (pdu->engine.engine_boots != snmpd_engine.engine_boots ||
356 abs(pdu->engine.engine_time - snmpd_engine.engine_time) >
358 return (SNMP_CODE_NOTINTIME);
361 if (((pdu->flags & SNMP_MSG_PRIV_FLAG) != 0 &&
362 (pdu->flags & SNMP_MSG_AUTH_FLAG) == 0) ||
363 ((pdu->flags & SNMP_MSG_AUTH_FLAG) == 0 &&
364 usm_user->suser.auth_proto != SNMP_AUTH_NOAUTH) ||
365 ((pdu->flags & SNMP_MSG_PRIV_FLAG) == 0 &&
366 usm_user->suser.priv_proto != SNMP_PRIV_NOPRIV))
367 return (SNMP_CODE_BADSECLEVEL);
369 return (SNMP_CODE_OK);
373 * Check whether access to each of var bindings in the PDU is allowed based
374 * on the user credentials against the configured User groups & VACM views.
377 snmp_pdu_auth_access(struct snmp_pdu *pdu, int32_t *ip)
380 int32_t suboid, smodel;
382 struct vacm_user *vuser;
383 struct vacm_access *acl;
384 struct vacm_context *vacmctx;
385 struct vacm_view *view;
388 * At least a default context exists if the snmpd_vacm(3) module is
391 if (SLIST_EMPTY(&vacm_contextlist) ||
392 (pdu->flags & SNMP_MSG_AUTODISCOVER) != 0)
393 return (SNMP_CODE_OK);
395 switch (pdu->version) {
397 if ((uname = comm_string(community)) == NULL)
398 return (SNMP_CODE_FAILED);
399 smodel = SNMP_SECMODEL_SNMPv1;
403 if ((uname = comm_string(community)) == NULL)
404 return (SNMP_CODE_FAILED);
405 smodel = SNMP_SECMODEL_SNMPv2c;
409 uname = pdu->user.sec_name;
410 if ((smodel = pdu->security_model) != SNMP_SECMODEL_USM)
411 return (SNMP_CODE_FAILED);
412 /* Compare the PDU context engine id against the agent's */
413 if (pdu->context_engine_len != snmpd_engine.engine_len ||
414 memcmp(pdu->context_engine, snmpd_engine.engine_id,
415 snmpd_engine.engine_len) != 0)
416 return (SNMP_CODE_FAILED);
423 SLIST_FOREACH(vuser, &vacm_userlist, vvu)
424 if (strcmp(uname, vuser->secname) == 0 &&
425 vuser->sec_model == smodel)
428 if (vuser == NULL || vuser->group == NULL)
429 return (SNMP_CODE_FAILED);
431 /* XXX: shteryana - recheck */
432 TAILQ_FOREACH_REVERSE(acl, &vacm_accesslist, vacm_accesslist, vva) {
433 if (acl->group != vuser->group)
435 SLIST_FOREACH(vacmctx, &vacm_contextlist, vcl)
436 if (memcmp(vacmctx->ctxname, acl->ctx_prefix,
437 acl->ctx_match) == 0)
441 return (SNMP_CODE_FAILED);
447 case SNMP_PDU_GETNEXT:
448 case SNMP_PDU_GETBULK:
449 if ((view = acl->read_view) == NULL)
450 return (SNMP_CODE_FAILED);
454 if ((view = acl->write_view) == NULL)
455 return (SNMP_CODE_FAILED);
459 case SNMP_PDU_INFORM:
461 case SNMP_PDU_REPORT:
462 if ((view = acl->notify_view) == NULL)
463 return (SNMP_CODE_FAILED);
465 case SNMP_PDU_RESPONSE:
467 return (SNMP_CODE_FAILED);
472 for (i = 0; i < pdu->nbindings; i++) {
473 /* XXX - view->mask*/
474 suboid = asn_is_suboid(&view->subtree, &pdu->bindings[i].var);
475 if ((!suboid && !view->exclude) || (suboid && view->exclude)) {
477 return (SNMP_CODE_FAILED);
481 return (SNMP_CODE_OK);
485 * SNMP input. Start: decode the PDU, find the user or community.
488 snmp_input_start(const u_char *buf, size_t len, const char *source,
489 struct snmp_pdu *pdu, int32_t *ip, size_t *pdulen)
493 enum snmpd_input_err ret;
497 this_tick = get_ticks();
502 /* look whether we have enough bytes for the entire PDU. */
503 switch (sret = snmp_pdu_snoop(&b)) {
506 return (SNMPD_INPUT_TRUNC);
509 snmpd_stats.inASNParseErrs++;
510 return (SNMPD_INPUT_FAILED);
512 b.asn_len = *pdulen = (size_t)sret;
514 memset(pdu, 0, sizeof(*pdu));
515 if ((code = snmp_pdu_decode_header(&b, pdu)) != SNMP_CODE_OK)
518 if (pdu->version == SNMP_V3) {
519 if (pdu->security_model != SNMP_SECMODEL_USM) {
520 code = SNMP_CODE_FAILED;
523 if ((code = snmp_pdu_auth_user(pdu)) != SNMP_CODE_OK)
525 if ((code = snmp_pdu_decode_secmode(&b, pdu)) != SNMP_CODE_OK)
528 code = snmp_pdu_decode_scoped(&b, pdu, ip);
530 ret = SNMPD_INPUT_OK;
533 snmpd_stats.inPkts++;
537 case SNMP_CODE_FAILED:
538 snmpd_stats.inASNParseErrs++;
539 return (SNMPD_INPUT_FAILED);
541 case SNMP_CODE_BADVERS:
543 snmpd_stats.inBadVersions++;
544 return (SNMPD_INPUT_FAILED);
546 case SNMP_CODE_BADLEN:
547 if (pdu->type == SNMP_OP_SET)
548 ret = SNMPD_INPUT_VALBADLEN;
551 case SNMP_CODE_OORANGE:
552 if (pdu->type == SNMP_OP_SET)
553 ret = SNMPD_INPUT_VALRANGE;
556 case SNMP_CODE_BADENC:
557 if (pdu->type == SNMP_OP_SET)
558 ret = SNMPD_INPUT_VALBADENC;
561 case SNMP_CODE_BADSECLEVEL:
562 snmpd_usmstats.unsupported_seclevels++;
563 return (SNMPD_INPUT_FAILED);
565 case SNMP_CODE_NOTINTIME:
566 snmpd_usmstats.not_in_time_windows++;
567 return (SNMPD_INPUT_FAILED);
569 case SNMP_CODE_BADUSER:
570 snmpd_usmstats.unknown_users++;
571 return (SNMPD_INPUT_FAILED);
573 case SNMP_CODE_BADENGINE:
574 snmpd_usmstats.unknown_engine_ids++;
575 return (SNMPD_INPUT_FAILED);
577 case SNMP_CODE_BADDIGEST:
578 snmpd_usmstats.wrong_digests++;
579 return (SNMPD_INPUT_FAILED);
581 case SNMP_CODE_EDECRYPT:
582 snmpd_usmstats.decrypt_errors++;
583 return (SNMPD_INPUT_FAILED);
586 switch (pdu->version) {
589 if (!(snmpd.version_enable & VERS_ENABLE_V1))
594 if (!(snmpd.version_enable & VERS_ENABLE_V2C))
599 if (!(snmpd.version_enable & VERS_ENABLE_V3))
609 if (debug.dump_pdus) {
610 snmp_printf("%s -> ", source);
615 * Look, whether we know the community or user
618 if (pdu->version != SNMP_V3) {
619 TAILQ_FOREACH(comm, &community_list, link)
620 if (comm->string != NULL &&
621 strcmp(comm->string, pdu->community) == 0)
625 snmpd_stats.inBadCommunityNames++;
627 if (snmpd.auth_traps)
628 snmp_send_trap(&oid_authenticationFailure,
629 (struct snmp_value *)NULL);
630 ret = SNMPD_INPUT_BAD_COMM;
632 community = comm->value;
633 } else if (pdu->nbindings == 0) {
634 /* RFC 3414 - snmpEngineID Discovery */
635 if (strlen(pdu->user.sec_name) == 0) {
636 asn_append_oid(&(pdu->bindings[pdu->nbindings++].var),
637 &oid_usmUnknownEngineIDs);
638 pdu->context_engine_len = snmpd_engine.engine_len;
639 memcpy(pdu->context_engine, snmpd_engine.engine_id,
640 snmpd_engine.engine_len);
641 } else if (pdu->engine.engine_boots == 0 &&
642 pdu->engine.engine_time == 0) {
643 asn_append_oid(&(pdu->bindings[pdu->nbindings++].var),
644 &oid_usmNotInTimeWindows);
645 pdu->engine.engine_boots = snmpd_engine.engine_boots;
646 pdu->engine.engine_time = snmpd_engine.engine_time;
648 } else if (usm_user->suser.auth_proto != SNMP_AUTH_NOAUTH &&
649 (pdu->engine.engine_boots == 0 || pdu->engine.engine_time == 0)) {
650 snmpd_usmstats.not_in_time_windows++;
651 ret = SNMP_CODE_FAILED;
654 if ((code = snmp_pdu_auth_access(pdu, ip)) != SNMP_CODE_OK)
655 ret = SNMP_CODE_FAILED;
661 * Will return only _OK or _FAILED
664 snmp_input_finish(struct snmp_pdu *pdu, const u_char *rcvbuf, size_t rcvlen,
665 u_char *sndbuf, size_t *sndlen, const char *source,
666 enum snmpd_input_err ierr, int32_t ivar, void *data)
668 struct snmp_pdu resp;
669 struct asn_buf resp_b, pdu_b;
672 resp_b.asn_ptr = sndbuf;
673 resp_b.asn_len = snmpd.txbuf;
675 pdu_b.asn_cptr = rcvbuf;
676 pdu_b.asn_len = rcvlen;
678 if (ierr != SNMPD_INPUT_OK) {
679 /* error decoding the input of a SET */
680 if (pdu->version == SNMP_V1)
681 pdu->error_status = SNMP_ERR_BADVALUE;
682 else if (ierr == SNMPD_INPUT_VALBADLEN)
683 pdu->error_status = SNMP_ERR_WRONG_LENGTH;
684 else if (ierr == SNMPD_INPUT_VALRANGE)
685 pdu->error_status = SNMP_ERR_WRONG_VALUE;
687 pdu->error_status = SNMP_ERR_WRONG_ENCODING;
689 pdu->error_index = ivar;
691 if (snmp_make_errresp(pdu, &pdu_b, &resp_b) == SNMP_RET_IGN) {
692 syslog(LOG_WARNING, "could not encode error response");
693 snmpd_stats.silentDrops++;
694 return (SNMPD_INPUT_FAILED);
697 if (debug.dump_pdus) {
698 snmp_printf("%s <- ", source);
701 *sndlen = (size_t)(resp_b.asn_ptr - sndbuf);
702 return (SNMPD_INPUT_OK);
708 ret = snmp_get(pdu, &resp_b, &resp, data);
711 case SNMP_PDU_GETNEXT:
712 ret = snmp_getnext(pdu, &resp_b, &resp, data);
716 ret = snmp_set(pdu, &resp_b, &resp, data);
719 case SNMP_PDU_GETBULK:
720 ret = snmp_getbulk(pdu, &resp_b, &resp, data);
731 /* normal return - send a response */
732 if (debug.dump_pdus) {
733 snmp_printf("%s <- ", source);
734 snmp_pdu_dump(&resp);
736 *sndlen = (size_t)(resp_b.asn_ptr - sndbuf);
737 snmp_pdu_free(&resp);
738 return (SNMPD_INPUT_OK);
741 /* error - send nothing */
742 snmpd_stats.silentDrops++;
743 return (SNMPD_INPUT_FAILED);
746 /* error - send error response. The snmp routine has
747 * changed the error fields in the original message. */
748 resp_b.asn_ptr = sndbuf;
749 resp_b.asn_len = snmpd.txbuf;
750 if (snmp_make_errresp(pdu, &pdu_b, &resp_b) == SNMP_RET_IGN) {
751 syslog(LOG_WARNING, "could not encode error response");
752 snmpd_stats.silentDrops++;
753 return (SNMPD_INPUT_FAILED);
755 if (debug.dump_pdus) {
756 snmp_printf("%s <- ", source);
759 *sndlen = (size_t)(resp_b.asn_ptr - sndbuf);
760 return (SNMPD_INPUT_OK);
767 * Insert a port into the right place in the transport's table of ports
770 trans_insert_port(struct transport *t, struct tport *port)
774 TAILQ_FOREACH(p, &t->table, link) {
775 if (asn_compare_oid(&p->index, &port->index) > 0) {
776 TAILQ_INSERT_BEFORE(p, port, link);
781 TAILQ_INSERT_TAIL(&t->table, port, link);
785 * Remove a port from a transport's list
788 trans_remove_port(struct tport *port)
791 TAILQ_REMOVE(&port->transport->table, port, link);
795 * Find a port on a transport's list
798 trans_find_port(struct transport *t, const struct asn_oid *idx, u_int sub)
801 return (FIND_OBJECT_OID(&t->table, idx, sub));
805 * Find next port on a transport's list
808 trans_next_port(struct transport *t, const struct asn_oid *idx, u_int sub)
811 return (NEXT_OBJECT_OID(&t->table, idx, sub));
818 trans_first_port(struct transport *t)
821 return (TAILQ_FIRST(&t->table));
825 * Iterate through all ports until a function returns a 0.
828 trans_iter_port(struct transport *t, int (*func)(struct tport *, intptr_t),
833 TAILQ_FOREACH(p, &t->table, link)
834 if (func(p, arg) == 0)
840 * Register a transport
843 trans_register(const struct transport_def *def, struct transport **pp)
848 if ((*pp = malloc(sizeof(**pp))) == NULL)
849 return (SNMP_ERR_GENERR);
851 /* construct index */
852 (*pp)->index.len = strlen(def->name) + 1;
853 (*pp)->index.subs[0] = strlen(def->name);
854 for (i = 0; i < (*pp)->index.subs[0]; i++)
855 (*pp)->index.subs[i + 1] = def->name[i];
859 if (FIND_OBJECT_OID(&transport_list, &(*pp)->index, 0) != NULL) {
861 return (SNMP_ERR_INCONS_VALUE);
864 /* register module */
865 snprintf(or_descr, sizeof(or_descr), "%s transport mapping", def->name);
866 if (((*pp)->or_index = or_register(&def->id, or_descr, NULL)) == 0) {
868 return (SNMP_ERR_GENERR);
871 INSERT_OBJECT_OID((*pp), &transport_list);
873 TAILQ_INIT(&(*pp)->table);
875 return (SNMP_ERR_NOERROR);
879 * Unregister transport
882 trans_unregister(struct transport *t)
884 if (!TAILQ_EMPTY(&t->table))
885 return (SNMP_ERR_INCONS_VALUE);
887 or_unregister(t->or_index);
888 TAILQ_REMOVE(&transport_list, t, link);
890 return (SNMP_ERR_NOERROR);
894 * File descriptor support
896 #ifdef USE_LIBBEGEMOT
898 input(int fd, int mask __unused, void *uap)
901 input(evContext ctx __unused, void *uap, int fd, int mask __unused)
904 struct fdesc *f = uap;
906 (*f->func)(fd, f->udata);
914 #ifdef USE_LIBBEGEMOT
916 poll_unregister(f->id);
920 if (evTestID(f->id)) {
921 (void)evDeselectFD(evctx, f->id);
933 #ifdef USE_LIBBEGEMOT
936 if ((f->id = poll_register(f->fd, input, f, POLL_IN)) < 0) {
938 syslog(LOG_ERR, "select fd %d: %m", f->fd);
945 if (evSelectFD(evctx, f->fd, EV_READ, input, f, &f->id)) {
947 syslog(LOG_ERR, "select fd %d: %m", f->fd);
956 fd_select(int fd, void (*func)(int, void *), void *udata, struct lmodule *mod)
961 if ((f = malloc(sizeof(struct fdesc))) == NULL) {
963 syslog(LOG_ERR, "fd_select: %m");
971 #ifdef USE_LIBBEGEMOT
984 LIST_INSERT_HEAD(&fdesc_list, f, link);
994 LIST_REMOVE(f, link);
1000 fd_flush(struct lmodule *mod)
1002 struct fdesc *t, *t1;
1004 t = LIST_FIRST(&fdesc_list);
1006 t1 = LIST_NEXT(t, link);
1007 if (t->owner == mod)
1014 * Consume a message from the input buffer
1017 snmp_input_consume(struct port_input *pi)
1020 /* always consume everything */
1024 if (pi->consumed >= pi->length) {
1025 /* all bytes consumed */
1029 memmove(pi->buf, pi->buf + pi->consumed, pi->length - pi->consumed);
1030 pi->length -= pi->consumed;
1034 check_priv_dgram(struct port_input *pi, struct sockcred *cred)
1037 /* process explicitly sends credentials */
1039 pi->priv = (cred->sc_euid == 0);
1045 check_priv_stream(struct port_input *pi)
1047 struct xucred ucred;
1050 /* obtain the accept time credentials */
1051 ucredlen = sizeof(ucred);
1053 if (getsockopt(pi->fd, 0, LOCAL_PEERCRED, &ucred, &ucredlen) == 0 &&
1054 ucredlen >= sizeof(ucred) && ucred.cr_version == XUCRED_VERSION)
1055 pi->priv = (ucred.cr_uid == 0);
1061 * Input from a stream socket.
1064 recv_stream(struct port_input *pi)
1067 struct iovec iov[1];
1070 if (pi->buf == NULL) {
1071 /* no buffer yet - allocate one */
1072 if ((pi->buf = buf_alloc(0)) == NULL) {
1073 /* ups - could not get buffer. Return an error
1074 * the caller must close the transport. */
1077 pi->buflen = buf_size(0);
1082 /* try to get a message */
1083 msg.msg_name = pi->peer;
1084 msg.msg_namelen = pi->peerlen;
1087 msg.msg_control = NULL;
1088 msg.msg_controllen = 0;
1091 iov[0].iov_base = pi->buf + pi->length;
1092 iov[0].iov_len = pi->buflen - pi->length;
1094 len = recvmsg(pi->fd, &msg, 0);
1096 if (len == -1 || len == 0)
1103 check_priv_stream(pi);
1109 * Input from a datagram socket.
1110 * Each receive should return one datagram.
1113 recv_dgram(struct port_input *pi, struct in_addr *laddr)
1116 char cbuf[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) +
1117 CMSG_SPACE(sizeof(struct in_addr))];
1119 struct iovec iov[1];
1121 struct cmsghdr *cmsg;
1122 struct sockcred *cred = NULL;
1124 if (pi->buf == NULL) {
1125 /* no buffer yet - allocate one */
1126 if ((pi->buf = buf_alloc(0)) == NULL) {
1127 /* ups - could not get buffer. Read away input
1129 (void)recvfrom(pi->fd, embuf, sizeof(embuf),
1134 pi->buflen = buf_size(0);
1137 /* try to get a message */
1138 msg.msg_name = pi->peer;
1139 msg.msg_namelen = pi->peerlen;
1142 memset(cbuf, 0, sizeof(cbuf));
1143 msg.msg_control = cbuf;
1144 msg.msg_controllen = sizeof(cbuf);
1147 iov[0].iov_base = pi->buf;
1148 iov[0].iov_len = pi->buflen;
1150 len = recvmsg(pi->fd, &msg, 0);
1152 if (len == -1 || len == 0)
1156 if (msg.msg_flags & MSG_TRUNC) {
1157 /* truncated - drop */
1158 snmpd_stats.silentDrops++;
1159 snmpd_stats.inTooLong++;
1163 pi->length = (size_t)len;
1165 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
1166 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1167 if (cmsg->cmsg_level == IPPROTO_IP &&
1168 cmsg->cmsg_type == IP_RECVDSTADDR)
1169 memcpy(laddr, CMSG_DATA(cmsg), sizeof(struct in_addr));
1170 if (cmsg->cmsg_level == SOL_SOCKET &&
1171 cmsg->cmsg_type == SCM_CREDS)
1172 cred = (struct sockcred *)CMSG_DATA(cmsg);
1176 check_priv_dgram(pi, cred);
1182 * Input from a socket
1185 snmpd_input(struct port_input *pi, struct tport *tport)
1189 struct snmp_pdu pdu;
1190 enum snmpd_input_err ierr, ferr;
1191 enum snmpd_proxy_err perr;
1195 #ifdef USE_TCPWRAPPERS
1199 struct iovec iov[1];
1200 char cbuf[CMSG_SPACE(sizeof(struct in_addr))];
1201 struct cmsghdr *cmsgp;
1203 /* get input depending on the transport */
1205 msg.msg_control = NULL;
1206 msg.msg_controllen = 0;
1208 ret = recv_stream(pi);
1210 struct in_addr *laddr;
1212 memset(cbuf, 0, CMSG_SPACE(sizeof(struct in_addr)));
1213 msg.msg_control = cbuf;
1214 msg.msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
1215 cmsgp = CMSG_FIRSTHDR(&msg);
1216 cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
1217 cmsgp->cmsg_level = IPPROTO_IP;
1218 cmsgp->cmsg_type = IP_SENDSRCADDR;
1219 laddr = (struct in_addr *)CMSG_DATA(cmsgp);
1221 ret = recv_dgram(pi, laddr);
1223 if (laddr->s_addr == 0) {
1224 msg.msg_control = NULL;
1225 msg.msg_controllen = 0;
1232 #ifdef USE_TCPWRAPPERS
1234 * In case of AF_INET{6} peer, do hosts_access(5) check.
1236 if (pi->peer->sa_family != AF_LOCAL &&
1237 inet_ntop(pi->peer->sa_family,
1238 &((const struct sockaddr_in *)(const void *)pi->peer)->sin_addr,
1239 client, sizeof(client)) != NULL) {
1240 request_set(&req, RQ_CLIENT_ADDR, client, 0);
1241 if (hosts_access(&req) == 0) {
1242 syslog(LOG_ERR, "refused connection from %.500s",
1246 } else if (pi->peer->sa_family != AF_LOCAL)
1247 syslog(LOG_ERR, "inet_ntop(): %m");
1253 ierr = snmp_input_start(pi->buf, pi->length, "SNMP", &pdu, &vi,
1255 if (ierr == SNMPD_INPUT_TRUNC) {
1256 /* need more bytes. This is ok only for streaming transports.
1257 * but only if we have not reached bufsiz yet. */
1259 if (pi->length == buf_size(0)) {
1260 snmpd_stats.silentDrops++;
1265 snmpd_stats.silentDrops++;
1269 /* can't check for bad SET pdus here, because a proxy may have to
1270 * check the access first. We don't want to return an error response
1271 * to a proxy PDU with a wrong community */
1272 if (ierr == SNMPD_INPUT_FAILED) {
1273 /* for streaming transports this is fatal */
1276 snmp_input_consume(pi);
1279 if (ierr == SNMPD_INPUT_BAD_COMM) {
1280 snmp_input_consume(pi);
1285 * If that is a module community and the module has a proxy function,
1286 * the hand it over to the module.
1288 if (comm != NULL && comm->owner != NULL &&
1289 comm->owner->config->proxy != NULL) {
1290 perr = (*comm->owner->config->proxy)(&pdu, tport->transport,
1291 &tport->index, pi->peer, pi->peerlen, ierr, vi,
1292 !pi->cred || pi->priv);
1296 case SNMPD_PROXY_OK:
1297 snmp_input_consume(pi);
1300 case SNMPD_PROXY_REJ:
1303 case SNMPD_PROXY_DROP:
1304 snmp_input_consume(pi);
1305 snmp_pdu_free(&pdu);
1306 snmpd_stats.proxyDrops++;
1309 case SNMPD_PROXY_BADCOMM:
1310 snmp_input_consume(pi);
1311 snmp_pdu_free(&pdu);
1312 snmpd_stats.inBadCommunityNames++;
1313 if (snmpd.auth_traps)
1314 snmp_send_trap(&oid_authenticationFailure,
1315 (struct snmp_value *)NULL);
1318 case SNMPD_PROXY_BADCOMMUSE:
1319 snmp_input_consume(pi);
1320 snmp_pdu_free(&pdu);
1321 snmpd_stats.inBadCommunityUses++;
1322 if (snmpd.auth_traps)
1323 snmp_send_trap(&oid_authenticationFailure,
1324 (struct snmp_value *)NULL);
1332 if (pdu.type == SNMP_PDU_RESPONSE ||
1333 pdu.type == SNMP_PDU_TRAP ||
1334 pdu.type == SNMP_PDU_TRAP2) {
1335 snmpd_stats.silentDrops++;
1336 snmpd_stats.inBadPduTypes++;
1337 snmp_pdu_free(&pdu);
1338 snmp_input_consume(pi);
1345 if (pdu.version < SNMP_V3 &&
1346 ((pi->cred && !pi->priv && pdu.type == SNMP_PDU_SET) ||
1347 (community != COMM_WRITE &&
1348 (pdu.type == SNMP_PDU_SET || community != COMM_READ)))) {
1349 snmpd_stats.inBadCommunityUses++;
1350 snmp_pdu_free(&pdu);
1351 snmp_input_consume(pi);
1352 if (snmpd.auth_traps)
1353 snmp_send_trap(&oid_authenticationFailure,
1354 (struct snmp_value *)NULL);
1361 if ((sndbuf = buf_alloc(1)) == NULL) {
1362 snmpd_stats.silentDrops++;
1363 snmp_pdu_free(&pdu);
1364 snmp_input_consume(pi);
1367 ferr = snmp_input_finish(&pdu, pi->buf, pi->length,
1368 sndbuf, &sndlen, "SNMP", ierr, vi, NULL);
1370 if (ferr == SNMPD_INPUT_OK) {
1371 msg.msg_name = pi->peer;
1372 msg.msg_namelen = pi->peerlen;
1376 iov[0].iov_base = sndbuf;
1377 iov[0].iov_len = sndlen;
1379 slen = sendmsg(pi->fd, &msg, 0);
1381 syslog(LOG_ERR, "sendmsg: %m");
1382 else if ((size_t)slen != sndlen)
1383 syslog(LOG_ERR, "sendmsg: short write %zu/%zu",
1384 sndlen, (size_t)slen);
1386 snmp_pdu_free(&pdu);
1388 snmp_input_consume(pi);
1394 * Send a PDU to a given port
1397 snmp_send_port(void *targ, const struct asn_oid *port, struct snmp_pdu *pdu,
1398 const struct sockaddr *addr, socklen_t addrlen)
1400 struct transport *trans = targ;
1406 TAILQ_FOREACH(tp, &trans->table, link)
1407 if (asn_compare_oid(port, &tp->index) == 0)
1412 if ((sndbuf = buf_alloc(1)) == NULL)
1415 snmp_output(pdu, sndbuf, &sndlen, "SNMP PROXY");
1417 len = trans->vtab->send(tp, sndbuf, sndlen, addr, addrlen);
1420 syslog(LOG_ERR, "sendto: %m");
1421 else if ((size_t)len != sndlen)
1422 syslog(LOG_ERR, "sendto: short write %zu/%zu",
1423 sndlen, (size_t)len);
1430 * Close an input source
1433 snmpd_input_close(struct port_input *pi)
1436 fd_deselect(pi->id);
1438 (void)close(pi->fd);
1439 if (pi->buf != NULL)
1444 * Dump internal state.
1446 #ifdef USE_LIBBEGEMOT
1451 info_func(evContext ctx __unused, void *uap __unused, const void *tag __unused)
1458 syslog(LOG_DEBUG, "Dump of SNMPd %lu\n", (u_long)getpid());
1459 for (i = 0; i < tree_size; i++) {
1460 switch (tree[i].type) {
1462 case SNMP_NODE_LEAF:
1463 sprintf(buf, "LEAF: %s %s", tree[i].name,
1464 asn_oid2str(&tree[i].oid));
1467 case SNMP_NODE_COLUMN:
1468 sprintf(buf, "COL: %s %s", tree[i].name,
1469 asn_oid2str(&tree[i].oid));
1472 syslog(LOG_DEBUG, "%s", buf);
1475 TAILQ_FOREACH(m, &lmodules, link)
1476 if (m->config->dump)
1477 (*m->config->dump)();
1481 * Re-read configuration
1483 #ifdef USE_LIBBEGEMOT
1488 config_func(evContext ctx __unused, void *uap __unused,
1489 const void *tag __unused)
1494 if (read_config(config_file, NULL)) {
1495 syslog(LOG_ERR, "error reading config file '%s'", config_file);
1498 TAILQ_FOREACH(m, &lmodules, link)
1499 if (m->config->config)
1500 (*m->config->config)();
1504 * On USR1 dump actual configuration.
1507 onusr1(int s __unused)
1510 work |= WORK_DOINFO;
1513 onhup(int s __unused)
1516 work |= WORK_RECONFIG;
1520 onterm(int s __unused)
1523 /* allow clean-up */
1530 struct sigaction sa;
1532 sa.sa_handler = onusr1;
1533 sa.sa_flags = SA_RESTART;
1534 sigemptyset(&sa.sa_mask);
1535 if (sigaction(SIGUSR1, &sa, NULL)) {
1536 syslog(LOG_ERR, "sigaction: %m");
1540 sa.sa_handler = onhup;
1541 if (sigaction(SIGHUP, &sa, NULL)) {
1542 syslog(LOG_ERR, "sigaction: %m");
1546 sa.sa_handler = onterm;
1548 sigemptyset(&sa.sa_mask);
1549 if (sigaction(SIGTERM, &sa, NULL)) {
1550 syslog(LOG_ERR, "sigaction: %m");
1553 if (sigaction(SIGINT, &sa, NULL)) {
1554 syslog(LOG_ERR, "sigaction: %m");
1565 if (sigprocmask(SIG_BLOCK, &set, &blocked_sigs) == -1) {
1566 syslog(LOG_ERR, "SIG_BLOCK: %m");
1573 if (sigprocmask(SIG_SETMASK, &blocked_sigs, NULL) == -1) {
1574 syslog(LOG_ERR, "SIG_SETMASK: %m");
1585 (void)unlink(pid_file);
1591 struct transport *t;
1593 TAILQ_FOREACH(t, &transport_list, link)
1594 (void)t->vtab->stop(1);
1598 * Define a macro from the command line
1606 if ((eq = strchr(arg, '=')) == NULL)
1607 err = define_macro(arg, "");
1610 err = define_macro(arg, eq);
1613 syslog(LOG_ERR, "cannot save macro: %m");
1619 * Re-implement getsubopt from scratch, because the second argument is broken
1620 * and will not compile with WARNS=5.
1623 getsubopt1(char **arg, const char *const *options, char **valp, char **optp)
1625 static const char *const delim = ",\t ";
1631 /* skip leading junk */
1632 for (ptr = *arg; *ptr != '\0'; ptr++)
1633 if (strchr(delim, *ptr) == NULL)
1641 /* find the end of the option */
1642 while (*++ptr != '\0')
1643 if (strchr(delim, *ptr) != NULL || *ptr == '=')
1650 while (*ptr != '\0' && strchr(delim, *ptr) == NULL)
1660 for (i = 0; *options != NULL; options++, i++)
1661 if (strcmp(*optp, *options) == 0)
1667 main(int argc, char *argv[])
1673 const char *prefix = "snmpd";
1675 char *value = NULL, *option; /* XXX */
1676 struct transport *t;
1679 #define DBG_EVENTS 1
1681 static const char *const debug_opts[] = {
1688 snmp_printf = snmp_printf_func;
1689 snmp_error = snmp_error_func;
1690 snmp_debug = snmp_debug_func;
1691 asn_error = asn_error_func;
1693 while ((opt = getopt(argc, argv, "c:dD:e:hI:l:m:p:")) != EOF)
1697 strlcpy(config_file, optarg, sizeof(config_file));
1706 switch (getsubopt1(&optarg, debug_opts,
1710 debug.dump_pdus = 1;
1720 "no value for 'trace'");
1722 snmp_trace = strtoul(value,
1729 "unknown debug flag '%s'",
1733 "missing debug flag");
1740 strlcpy(engine_file, optarg, sizeof(engine_file));
1743 fprintf(stderr, "%s", usgtxt);
1759 strlcpy(pid_file, optarg, sizeof(pid_file));
1763 openlog(prefix, LOG_PID | (background ? 0 : LOG_PERROR), LOG_USER);
1764 setlogmask(LOG_UPTO(debug.logpri - 1));
1766 if (background && daemon(0, 0) < 0) {
1767 syslog(LOG_ERR, "daemon: %m");
1779 snmp_serial_no = random();
1781 #ifdef USE_TCPWRAPPERS
1783 * Initialize hosts_access(3) handler.
1785 request_init(&req, RQ_DAEMON, "snmpd", 0);
1790 * Initialize the tree.
1792 if ((tree = malloc(sizeof(struct snmp_node) * CTREE_SIZE)) == NULL) {
1793 syslog(LOG_ERR, "%m");
1796 memcpy(tree, ctree, sizeof(struct snmp_node) * CTREE_SIZE);
1797 tree_size = CTREE_SIZE;
1800 * Get standard communities
1802 (void)comm_define(1, "SNMP read", NULL, NULL);
1803 (void)comm_define(2, "SNMP write", NULL, NULL);
1804 community = COMM_INITIALIZE;
1806 trap_reqid = reqid_allocate(512, NULL);
1808 if (config_file[0] == '\0')
1809 snprintf(config_file, sizeof(config_file), PATH_CONFIG, prefix);
1812 init_snmpd_engine();
1814 this_tick = get_ticks();
1815 start_tick = this_tick;
1817 /* start transports */
1818 if (atexit(trans_stop) == -1) {
1819 syslog(LOG_ERR, "atexit failed: %m");
1822 if (udp_trans.start() != SNMP_ERR_NOERROR)
1823 syslog(LOG_WARNING, "cannot start UDP transport");
1824 if (lsock_trans.start() != SNMP_ERR_NOERROR)
1825 syslog(LOG_WARNING, "cannot start LSOCK transport");
1827 #ifdef USE_LIBBEGEMOT
1828 if (debug.evdebug > 0)
1831 if (evCreate(&evctx)) {
1832 syslog(LOG_ERR, "evCreate: %m");
1835 if (debug.evdebug > 0)
1836 evSetDebug(evctx, 10, stderr);
1839 if (engine_file[0] == '\0')
1840 snprintf(engine_file, sizeof(engine_file), PATH_ENGINE, prefix);
1842 if (read_config(config_file, NULL)) {
1843 syslog(LOG_ERR, "error in config file");
1847 TAILQ_FOREACH(t, &transport_list, link)
1848 TAILQ_FOREACH(p, &t->table, link)
1849 t->vtab->init_port(p);
1853 if (pid_file[0] == '\0')
1854 snprintf(pid_file, sizeof(pid_file), PATH_PID, prefix);
1856 if ((fp = fopen(pid_file, "w")) != NULL) {
1857 fprintf(fp, "%u", getpid());
1859 if (atexit(term) == -1) {
1860 syslog(LOG_ERR, "atexit failed: %m");
1861 (void)remove(pid_file);
1866 if (or_register(&oid_snmpMIB, "The MIB module for SNMPv2 entities.",
1868 syslog(LOG_ERR, "cannot register SNMPv2 MIB");
1871 if (or_register(&oid_begemotSnmpd, "The MIB module for the Begemot SNMPd.",
1873 syslog(LOG_ERR, "cannot register begemotSnmpd MIB");
1877 while ((m = TAILQ_FIRST(&modules_start)) != NULL) {
1878 m->flags &= ~LM_ONSTARTLIST;
1879 TAILQ_REMOVE(&modules_start, m, start);
1883 snmp_send_trap(&oid_coldStart, (struct snmp_value *)NULL);
1886 #ifndef USE_LIBBEGEMOT
1889 struct lmodule *mod;
1891 TAILQ_FOREACH(mod, &lmodules, link)
1892 if (mod->config->idle != NULL)
1893 (*mod->config->idle)();
1895 #ifndef USE_LIBBEGEMOT
1896 if (evGetNext(evctx, &event, EV_WAIT) == 0) {
1897 if (evDispatch(evctx, event))
1898 syslog(LOG_ERR, "evDispatch: %m");
1899 } else if (errno != EINTR) {
1900 syslog(LOG_ERR, "evGetNext: %m");
1909 if (work & WORK_DOINFO) {
1910 #ifdef USE_LIBBEGEMOT
1913 if (evWaitFor(evctx, &work, info_func,
1914 NULL, NULL) == -1) {
1915 syslog(LOG_ERR, "evWaitFor: %m");
1920 if (work & WORK_RECONFIG) {
1921 #ifdef USE_LIBBEGEMOT
1924 if (evWaitFor(evctx, &work, config_func,
1925 NULL, NULL) == -1) {
1926 syslog(LOG_ERR, "evWaitFor: %m");
1933 #ifndef USE_LIBBEGEMOT
1934 if (evDo(evctx, &work) == -1) {
1935 syslog(LOG_ERR, "evDo: %m");
1951 if (gettimeofday(&tv, NULL))
1953 ret = tv.tv_sec * 100ULL + tv.tv_usec / 10000ULL;
1962 * Trampoline for the non-repeatable timers.
1964 #ifdef USE_LIBBEGEMOT
1966 tfunc(int tid __unused, void *uap)
1969 tfunc(evContext ctx __unused, void *uap, struct timespec due __unused,
1970 struct timespec inter __unused)
1973 struct timer *tp = uap;
1975 LIST_REMOVE(tp, link);
1976 tp->func(tp->udata);
1981 * Trampoline for the repeatable timers.
1983 #ifdef USE_LIBBEGEMOT
1985 trfunc(int tid __unused, void *uap)
1988 trfunc(evContext ctx __unused, void *uap, struct timespec due __unused,
1989 struct timespec inter __unused)
1992 struct timer *tp = uap;
1994 tp->func(tp->udata);
1998 * Start a one-shot timer
2001 timer_start(u_int ticks, void (*func)(void *), void *udata, struct lmodule *mod)
2004 #ifndef USE_LIBBEGEMOT
2005 struct timespec due;
2008 if ((tp = malloc(sizeof(struct timer))) == NULL) {
2009 syslog(LOG_CRIT, "out of memory for timer");
2013 #ifndef USE_LIBBEGEMOT
2014 due = evAddTime(evNowTime(),
2015 evConsTime(ticks / 100, (ticks % 100) * 10000));
2022 LIST_INSERT_HEAD(&timer_list, tp, link);
2024 #ifdef USE_LIBBEGEMOT
2025 if ((tp->id = poll_start_timer(ticks * 10, 0, tfunc, tp)) < 0) {
2026 syslog(LOG_ERR, "cannot set timer: %m");
2030 if (evSetTimer(evctx, tfunc, tp, due, evConsTime(0, 0), &tp->id)
2032 syslog(LOG_ERR, "cannot set timer: %m");
2040 * Start a repeatable timer. When used with USE_LIBBEGEMOT the first argument
2041 * is currently ignored and the initial number of ticks is set to the
2042 * repeat number of ticks.
2045 timer_start_repeat(u_int ticks __unused, u_int repeat_ticks,
2046 void (*func)(void *), void *udata, struct lmodule *mod)
2049 #ifndef USE_LIBBEGEMOT
2050 struct timespec due;
2051 struct timespec inter;
2054 if ((tp = malloc(sizeof(struct timer))) == NULL) {
2055 syslog(LOG_CRIT, "out of memory for timer");
2059 #ifndef USE_LIBBEGEMOT
2060 due = evAddTime(evNowTime(),
2061 evConsTime(ticks / 100, (ticks % 100) * 10000));
2062 inter = evConsTime(repeat_ticks / 100, (repeat_ticks % 100) * 10000);
2069 LIST_INSERT_HEAD(&timer_list, tp, link);
2071 #ifdef USE_LIBBEGEMOT
2072 if ((tp->id = poll_start_timer(repeat_ticks * 10, 1, trfunc, tp)) < 0) {
2073 syslog(LOG_ERR, "cannot set timer: %m");
2077 if (evSetTimer(evctx, trfunc, tp, due, inter, &tp->id) == -1) {
2078 syslog(LOG_ERR, "cannot set timer: %m");
2091 struct timer *tp = p;
2093 LIST_REMOVE(tp, link);
2094 #ifdef USE_LIBBEGEMOT
2095 poll_stop_timer(tp->id);
2097 if (evClearTimer(evctx, tp->id) == -1) {
2098 syslog(LOG_ERR, "cannot stop timer: %m");
2106 timer_flush(struct lmodule *mod)
2108 struct timer *t, *t1;
2110 t = LIST_FIRST(&timer_list);
2112 t1 = LIST_NEXT(t, link);
2113 if (t->owner == mod)
2120 snmp_printf_func(const char *fmt, ...)
2123 static char *pend = NULL;
2127 vasprintf(&ret, fmt, ap);
2133 if ((new = realloc(pend, strlen(pend) + strlen(ret) + 1))
2144 while ((ret = strchr(pend, '\n')) != NULL) {
2146 syslog(LOG_DEBUG, "%s", pend);
2147 if (strlen(ret + 1) == 0) {
2152 strcpy(pend, ret + 1);
2157 snmp_error_func(const char *err, ...)
2162 if (!(snmp_trace & LOG_SNMP_ERRORS))
2166 snprintf(errbuf, sizeof(errbuf), "SNMP: ");
2167 vsnprintf(errbuf + strlen(errbuf),
2168 sizeof(errbuf) - strlen(errbuf), err, ap);
2171 syslog(LOG_ERR, "%s", errbuf);
2175 snmp_debug_func(const char *err, ...)
2181 snprintf(errbuf, sizeof(errbuf), "SNMP: ");
2182 vsnprintf(errbuf+strlen(errbuf), sizeof(errbuf)-strlen(errbuf),
2186 syslog(LOG_DEBUG, "%s", errbuf);
2190 asn_error_func(const struct asn_buf *b, const char *err, ...)
2196 if (!(snmp_trace & LOG_ASN1_ERRORS))
2200 snprintf(errbuf, sizeof(errbuf), "ASN.1: ");
2201 vsnprintf(errbuf + strlen(errbuf),
2202 sizeof(errbuf) - strlen(errbuf), err, ap);
2206 snprintf(errbuf + strlen(errbuf),
2207 sizeof(errbuf) - strlen(errbuf), " at");
2208 for (i = 0; b->asn_len > i; i++)
2209 snprintf(errbuf + strlen(errbuf),
2210 sizeof(errbuf) - strlen(errbuf),
2211 " %02x", b->asn_cptr[i]);
2214 syslog(LOG_ERR, "%s", errbuf);
2218 * Create a new community
2221 comm_define(u_int priv, const char *descr, struct lmodule *owner,
2224 struct community *c, *p;
2227 /* generate an identifier */
2229 if ((ncomm = next_community_index++) == UINT_MAX)
2230 next_community_index = 1;
2231 TAILQ_FOREACH(c, &community_list, link)
2232 if (c->value == ncomm)
2234 } while (c != NULL);
2236 if ((c = malloc(sizeof(struct community))) == NULL) {
2237 syslog(LOG_ERR, "comm_define: %m");
2247 if((c->string = malloc(strlen(str)+1)) == NULL) {
2251 strcpy(c->string, str);
2255 if (c->owner == NULL) {
2257 c->index.subs[0] = 0;
2259 c->index = c->owner->index;
2261 c->index.subs[c->index.len++] = c->private;
2266 TAILQ_FOREACH(p, &community_list, link) {
2267 if (asn_compare_oid(&p->index, &c->index) > 0) {
2268 TAILQ_INSERT_BEFORE(p, c, link);
2273 TAILQ_INSERT_TAIL(&community_list, c, link);
2278 comm_string(u_int ncomm)
2280 struct community *p;
2282 TAILQ_FOREACH(p, &community_list, link)
2283 if (p->value == ncomm)
2289 * Delete all communities allocated by a module
2292 comm_flush(struct lmodule *mod)
2294 struct community *p, *p1;
2296 p = TAILQ_FIRST(&community_list);
2298 p1 = TAILQ_NEXT(p, link);
2299 if (p->owner == mod) {
2301 TAILQ_REMOVE(&community_list, p, link);
2309 * Request ID handling.
2311 * Allocate a new range of request ids. Use a first fit algorithm.
2314 reqid_allocate(int size, struct lmodule *mod)
2317 struct idrange *r, *r1;
2319 if (size <= 0 || size > INT32_MAX) {
2320 syslog(LOG_CRIT, "%s: size out of range: %d", __func__, size);
2323 /* allocate a type id */
2325 if ((type = next_idrange++) == UINT_MAX)
2327 TAILQ_FOREACH(r, &idrange_list, link)
2328 if (r->type == type)
2333 if (TAILQ_EMPTY(&idrange_list))
2336 r = TAILQ_FIRST(&idrange_list);
2337 if (r->base < size) {
2338 while((r1 = TAILQ_NEXT(r, link)) != NULL) {
2339 if (r1->base - (r->base + r->size) >= size)
2346 r1 = TAILQ_LAST(&idrange_list, idrange_list);
2347 if (INT32_MAX - size + 1 < r1->base + r1->size) {
2348 syslog(LOG_ERR, "out of id ranges (%u)", size);
2354 /* allocate structure */
2355 if ((r1 = malloc(sizeof(struct idrange))) == NULL) {
2356 syslog(LOG_ERR, "%s: %m", __FUNCTION__);
2363 if (TAILQ_EMPTY(&idrange_list) || r == TAILQ_FIRST(&idrange_list)) {
2365 TAILQ_INSERT_HEAD(&idrange_list, r1, link);
2366 } else if (r == NULL) {
2367 r = TAILQ_LAST(&idrange_list, idrange_list);
2368 r1->base = r->base + r->size;
2369 TAILQ_INSERT_TAIL(&idrange_list, r1, link);
2371 r = TAILQ_PREV(r, idrange_list, link);
2372 r1->base = r->base + r->size;
2373 TAILQ_INSERT_AFTER(&idrange_list, r, r1, link);
2375 r1->next = r1->base;
2381 reqid_next(u_int type)
2386 TAILQ_FOREACH(r, &idrange_list, link)
2387 if (r->type == type)
2390 syslog(LOG_CRIT, "wrong idrange type");
2393 if ((id = r->next++) == r->base + (r->size - 1))
2399 reqid_base(u_int type)
2403 TAILQ_FOREACH(r, &idrange_list, link)
2404 if (r->type == type)
2406 syslog(LOG_CRIT, "wrong idrange type");
2411 reqid_type(int32_t reqid)
2415 TAILQ_FOREACH(r, &idrange_list, link)
2416 if (reqid >= r->base && reqid <= r->base + (r->size - 1))
2422 reqid_istype(int32_t reqid, u_int type)
2424 return (reqid_type(reqid) == type);
2428 * Delete all communities allocated by a module
2431 reqid_flush(struct lmodule *mod)
2433 struct idrange *p, *p1;
2435 p = TAILQ_FIRST(&idrange_list);
2437 p1 = TAILQ_NEXT(p, link);
2438 if (p->owner == mod) {
2439 TAILQ_REMOVE(&idrange_list, p, link);
2447 * Merge the given tree for the given module into the main tree.
2450 compare_node(const void *v1, const void *v2)
2452 const struct snmp_node *n1 = v1;
2453 const struct snmp_node *n2 = v2;
2455 return (asn_compare_oid(&n1->oid, &n2->oid));
2458 tree_merge(const struct snmp_node *ntree, u_int nsize, struct lmodule *mod)
2460 struct snmp_node *xtree;
2463 xtree = realloc(tree, sizeof(*tree) * (tree_size + nsize));
2464 if (xtree == NULL) {
2465 syslog(LOG_ERR, "tree_merge: %m");
2469 memcpy(&tree[tree_size], ntree, sizeof(*tree) * nsize);
2471 for (i = 0; i < nsize; i++)
2472 tree[tree_size + i].tree_data = mod;
2476 qsort(tree, tree_size, sizeof(tree[0]), compare_node);
2482 * Remove all nodes belonging to the loadable module
2485 tree_unmerge(struct lmodule *mod)
2489 for(s = d = 0; s < tree_size; s++)
2490 if (tree[s].tree_data != mod) {
2502 lm_load(const char *path, const char *section)
2507 char *av[MAX_MOD_ARGS + 1];
2511 if ((m = malloc(sizeof(*m))) == NULL) {
2512 syslog(LOG_ERR, "lm_load: %m");
2517 strcpy(m->section, section);
2519 if ((m->path = malloc(strlen(path) + 1)) == NULL) {
2520 syslog(LOG_ERR, "lm_load: %m");
2523 strcpy(m->path, path);
2528 m->index.subs[0] = strlen(section);
2529 m->index.len = m->index.subs[0] + 1;
2530 for (u = 0; u < m->index.subs[0]; u++)
2531 m->index.subs[u + 1] = section[u];
2534 * Load the object file and locate the config structure
2536 if ((m->handle = dlopen(m->path, RTLD_NOW|RTLD_GLOBAL)) == NULL) {
2537 syslog(LOG_ERR, "lm_load: open %s", dlerror());
2541 if ((m->config = dlsym(m->handle, "config")) == NULL) {
2542 syslog(LOG_ERR, "lm_load: no 'config' symbol %s", dlerror());
2547 * Insert it into the right place
2549 INSERT_OBJECT_OID(m, &lmodules);
2551 /* preserve order */
2552 if (community == COMM_INITIALIZE) {
2553 m->flags |= LM_ONSTARTLIST;
2554 TAILQ_INSERT_TAIL(&modules_start, m, start);
2558 * make the argument vector.
2561 for (i = 0; i < nprogargs; i++) {
2562 if (strlen(progargs[i]) >= strlen(section) + 1 &&
2563 strncmp(progargs[i], section, strlen(section)) == 0 &&
2564 progargs[i][strlen(section)] == ':') {
2565 if (ac == MAX_MOD_ARGS) {
2566 syslog(LOG_WARNING, "too many arguments for "
2567 "module '%s", section);
2570 av[ac++] = &progargs[i][strlen(section)+1];
2576 * Run the initialization function
2578 if ((err = (*m->config->init)(m, ac, av)) != 0) {
2579 syslog(LOG_ERR, "lm_load: init failed: %d", err);
2580 TAILQ_REMOVE(&lmodules, m, link);
2587 if ((m->flags & LM_ONSTARTLIST) != 0)
2588 TAILQ_REMOVE(&modules_start, m, start);
2600 lm_start(struct lmodule *mod)
2602 const struct lmodule *m;
2605 * Merge tree. If this fails, unload the module.
2607 if (tree_merge(mod->config->tree, mod->config->tree_size, mod)) {
2613 * Read configuration
2615 if (read_config(config_file, mod)) {
2616 syslog(LOG_ERR, "error in config file");
2620 if (mod->config->start)
2621 (*mod->config->start)();
2623 mod->flags |= LM_STARTED;
2626 * Inform other modules
2628 TAILQ_FOREACH(m, &lmodules, link)
2629 if (m->config->loading)
2630 (*m->config->loading)(mod, 1);
2638 lm_unload(struct lmodule *m)
2641 const struct lmodule *mod;
2643 TAILQ_REMOVE(&lmodules, m, link);
2644 if (m->flags & LM_ONSTARTLIST)
2645 TAILQ_REMOVE(&modules_start, m, start);
2648 if ((m->flags & LM_STARTED) && m->config->fini &&
2649 (err = (*m->config->fini)()) != 0)
2650 syslog(LOG_WARNING, "lm_unload(%s): fini %d", m->section, err);
2661 * Inform other modules
2663 TAILQ_FOREACH(mod, &lmodules, link)
2664 if (mod->config->loading)
2665 (*mod->config->loading)(m, 0);
2671 * Register an object resource and return the index (or 0 on failures)
2674 or_register(const struct asn_oid *or, const char *descr, struct lmodule *mod)
2676 struct objres *objres, *or1;
2679 /* find a free index */
2681 for (objres = TAILQ_FIRST(&objres_list);
2683 objres = TAILQ_NEXT(objres, link)) {
2684 if ((or1 = TAILQ_NEXT(objres, link)) == NULL ||
2685 or1->index > objres->index + 1) {
2686 idx = objres->index + 1;
2691 if ((objres = malloc(sizeof(*objres))) == NULL)
2694 objres->index = idx;
2696 strlcpy(objres->descr, descr, sizeof(objres->descr));
2697 objres->uptime = (uint32_t)(get_ticks() - start_tick);
2698 objres->module = mod;
2700 INSERT_OBJECT_INT(objres, &objres_list);
2702 systemg.or_last_change = objres->uptime;
2708 or_unregister(u_int idx)
2710 struct objres *objres;
2712 TAILQ_FOREACH(objres, &objres_list, link)
2713 if (objres->index == idx) {
2714 TAILQ_REMOVE(&objres_list, objres, link);
2721 * RFC 3414 User-based Security Model support
2724 struct snmpd_usmstat *
2725 bsnmpd_get_usm_stats(void)
2727 return (&snmpd_usmstats);
2731 bsnmpd_reset_usm_stats(void)
2733 memset(&snmpd_usmstats, 0, sizeof(snmpd_usmstats));
2737 usm_first_user(void)
2739 return (SLIST_FIRST(&usm_userlist));
2743 usm_next_user(struct usm_user *uuser)
2748 return (SLIST_NEXT(uuser, up));
2752 usm_find_user(uint8_t *engine, uint32_t elen, char *uname)
2754 struct usm_user *uuser;
2756 SLIST_FOREACH(uuser, &usm_userlist, up)
2757 if (uuser->user_engine_len == elen &&
2758 memcmp(uuser->user_engine_id, engine, elen) == 0 &&
2759 strlen(uuser->suser.sec_name) == strlen(uname) &&
2760 strcmp(uuser->suser.sec_name, uname) == 0)
2767 usm_compare_user(struct usm_user *u1, struct usm_user *u2)
2771 if (u1->user_engine_len < u2->user_engine_len)
2773 if (u1->user_engine_len > u2->user_engine_len)
2776 for (i = 0; i < u1->user_engine_len; i++) {
2777 if (u1->user_engine_id[i] < u2->user_engine_id[i])
2779 if (u1->user_engine_id[i] > u2->user_engine_id[i])
2783 if (strlen(u1->suser.sec_name) < strlen(u2->suser.sec_name))
2785 if (strlen(u1->suser.sec_name) > strlen(u2->suser.sec_name))
2788 for (i = 0; i < strlen(u1->suser.sec_name); i++) {
2789 if (u1->suser.sec_name[i] < u2->suser.sec_name[i])
2791 if (u1->suser.sec_name[i] > u2->suser.sec_name[i])
2799 usm_new_user(uint8_t *eid, uint32_t elen, char *uname)
2802 struct usm_user *uuser, *temp, *prev;
2804 for (uuser = usm_first_user(); uuser != NULL;
2805 (uuser = usm_next_user(uuser))) {
2806 if (uuser->user_engine_len == elen &&
2807 strlen(uname) == strlen(uuser->suser.sec_name) &&
2808 strcmp(uname, uuser->suser.sec_name) == 0 &&
2809 memcmp(eid, uuser->user_engine_id, elen) == 0)
2813 if ((uuser = (struct usm_user *)malloc(sizeof(*uuser))) == NULL)
2816 memset(uuser, 0, sizeof(struct usm_user));
2817 strlcpy(uuser->suser.sec_name, uname, SNMP_ADM_STR32_SIZ);
2818 memcpy(uuser->user_engine_id, eid, elen);
2819 uuser->user_engine_len = elen;
2821 if ((prev = SLIST_FIRST(&usm_userlist)) == NULL ||
2822 usm_compare_user(uuser, prev) < 0) {
2823 SLIST_INSERT_HEAD(&usm_userlist, uuser, up);
2827 SLIST_FOREACH(temp, &usm_userlist, up) {
2828 if ((cmp = usm_compare_user(uuser, temp)) <= 0)
2833 if (temp == NULL || cmp < 0)
2834 SLIST_INSERT_AFTER(prev, uuser, up);
2836 SLIST_INSERT_AFTER(temp, uuser, up);
2838 syslog(LOG_ERR, "User %s exists", uuser->suser.sec_name);
2847 usm_delete_user(struct usm_user *uuser)
2849 SLIST_REMOVE(&usm_userlist, uuser, usm_user, up);
2854 usm_flush_users(void)
2856 struct usm_user *uuser;
2858 while ((uuser = SLIST_FIRST(&usm_userlist)) != NULL) {
2859 SLIST_REMOVE_HEAD(&usm_userlist, up);
2863 SLIST_INIT(&usm_userlist);
2867 * RFC 3415 View-based Access Control Model support
2870 vacm_first_user(void)
2872 return (SLIST_FIRST(&vacm_userlist));
2876 vacm_next_user(struct vacm_user *vuser)
2881 return (SLIST_NEXT(vuser, vvu));
2885 vacm_compare_user(struct vacm_user *v1, struct vacm_user *v2)
2889 if (v1->sec_model < v2->sec_model)
2891 if (v1->sec_model > v2->sec_model)
2894 if (strlen(v1->secname) < strlen(v2->secname))
2896 if (strlen(v1->secname) > strlen(v2->secname))
2899 for (i = 0; i < strlen(v1->secname); i++) {
2900 if (v1->secname[i] < v2->secname[i])
2902 if (v1->secname[i] > v2->secname[i])
2910 vacm_new_user(int32_t smodel, char *uname)
2913 struct vacm_user *user, *temp, *prev;
2915 SLIST_FOREACH(user, &vacm_userlist, vvu)
2916 if (strcmp(uname, user->secname) == 0 &&
2917 smodel == user->sec_model)
2920 if ((user = (struct vacm_user *)malloc(sizeof(*user))) == NULL)
2923 memset(user, 0, sizeof(*user));
2924 user->group = &vacm_default_group;
2925 SLIST_INSERT_HEAD(&vacm_default_group.group_users, user, vvg);
2926 user->sec_model = smodel;
2927 strlcpy(user->secname, uname, sizeof(user->secname));
2929 if ((prev = SLIST_FIRST(&vacm_userlist)) == NULL ||
2930 vacm_compare_user(user, prev) < 0) {
2931 SLIST_INSERT_HEAD(&vacm_userlist, user, vvu);
2935 SLIST_FOREACH(temp, &vacm_userlist, vvu) {
2936 if ((cmp = vacm_compare_user(user, temp)) <= 0)
2941 if (temp == NULL || cmp < 0)
2942 SLIST_INSERT_AFTER(prev, user, vvu);
2944 SLIST_INSERT_AFTER(temp, user, vvu);
2946 syslog(LOG_ERR, "User %s exists", user->secname);
2955 vacm_delete_user(struct vacm_user *user)
2957 if (user->group != NULL && user->group != &vacm_default_group) {
2958 SLIST_REMOVE(&user->group->group_users, user, vacm_user, vvg);
2959 if (SLIST_EMPTY(&user->group->group_users)) {
2960 SLIST_REMOVE(&vacm_grouplist, user->group,
2966 SLIST_REMOVE(&vacm_userlist, user, vacm_user, vvu);
2973 vacm_user_set_group(struct vacm_user *user, u_char *octets, u_int len)
2975 struct vacm_group *group;
2977 if (len >= SNMP_ADM_STR32_SIZ)
2980 SLIST_FOREACH(group, &vacm_grouplist, vge)
2981 if (strlen(group->groupname) == len &&
2982 memcmp(octets, group->groupname, len) == 0)
2985 if (group == NULL) {
2986 if ((group = (struct vacm_group *)malloc(sizeof(*group))) == NULL)
2988 memset(group, 0, sizeof(*group));
2989 memcpy(group->groupname, octets, len);
2990 group->groupname[len] = '\0';
2991 SLIST_INSERT_HEAD(&vacm_grouplist, group, vge);
2994 SLIST_REMOVE(&user->group->group_users, user, vacm_user, vvg);
2995 SLIST_INSERT_HEAD(&group->group_users, user, vvg);
2996 user->group = group;
3002 vacm_groups_init(void)
3004 SLIST_INSERT_HEAD(&vacm_grouplist, &vacm_default_group, vge);
3007 struct vacm_access *
3008 vacm_first_access_rule(void)
3010 return (TAILQ_FIRST(&vacm_accesslist));
3013 struct vacm_access *
3014 vacm_next_access_rule(struct vacm_access *acl)
3019 return (TAILQ_NEXT(acl, vva));
3023 vacm_compare_access_rule(struct vacm_access *v1, struct vacm_access *v2)
3027 if (strlen(v1->group->groupname) < strlen(v2->group->groupname))
3029 if (strlen(v1->group->groupname) > strlen(v2->group->groupname))
3032 for (i = 0; i < strlen(v1->group->groupname); i++) {
3033 if (v1->group->groupname[i] < v2->group->groupname[i])
3035 if (v1->group->groupname[i] > v2->group->groupname[i])
3039 if (strlen(v1->ctx_prefix) < strlen(v2->ctx_prefix))
3041 if (strlen(v1->ctx_prefix) > strlen(v2->ctx_prefix))
3044 for (i = 0; i < strlen(v1->ctx_prefix); i++) {
3045 if (v1->ctx_prefix[i] < v2->ctx_prefix[i])
3047 if (v1->ctx_prefix[i] > v2->ctx_prefix[i])
3051 if (v1->sec_model < v2->sec_model)
3053 if (v1->sec_model > v2->sec_model)
3056 if (v1->sec_level < v2->sec_level)
3058 if (v1->sec_level > v2->sec_level)
3064 struct vacm_access *
3065 vacm_new_access_rule(char *gname, char *cprefix, int32_t smodel, int32_t slevel)
3067 struct vacm_group *group;
3068 struct vacm_access *acl, *temp;
3070 TAILQ_FOREACH(acl, &vacm_accesslist, vva) {
3071 if (acl->group == NULL)
3073 if (strcmp(gname, acl->group->groupname) == 0 &&
3074 strcmp(cprefix, acl->ctx_prefix) == 0 &&
3075 acl->sec_model == smodel && acl->sec_level == slevel)
3079 /* Make sure the group exists */
3080 SLIST_FOREACH(group, &vacm_grouplist, vge)
3081 if (strcmp(gname, group->groupname) == 0)
3087 if ((acl = (struct vacm_access *)malloc(sizeof(*acl))) == NULL)
3090 memset(acl, 0, sizeof(*acl));
3092 strlcpy(acl->ctx_prefix, cprefix, sizeof(acl->ctx_prefix));
3093 acl->sec_model = smodel;
3094 acl->sec_level = slevel;
3096 if ((temp = TAILQ_FIRST(&vacm_accesslist)) == NULL ||
3097 vacm_compare_access_rule(acl, temp) < 0) {
3098 TAILQ_INSERT_HEAD(&vacm_accesslist, acl, vva);
3102 TAILQ_FOREACH(temp, &vacm_accesslist, vva)
3103 if (vacm_compare_access_rule(acl, temp) < 0) {
3104 TAILQ_INSERT_BEFORE(temp, acl, vva);
3108 TAILQ_INSERT_TAIL(&vacm_accesslist, acl, vva);
3114 vacm_delete_access_rule(struct vacm_access *acl)
3116 TAILQ_REMOVE(&vacm_accesslist, acl, vva);
3123 vacm_first_view(void)
3125 return (SLIST_FIRST(&vacm_viewlist));
3129 vacm_next_view(struct vacm_view *view)
3134 return (SLIST_NEXT(view, vvl));
3138 vacm_compare_view(struct vacm_view *v1, struct vacm_view *v2)
3142 if (strlen(v1->viewname) < strlen(v2->viewname))
3144 if (strlen(v1->viewname) > strlen(v2->viewname))
3147 for (i = 0; i < strlen(v1->viewname); i++) {
3148 if (v1->viewname[i] < v2->viewname[i])
3150 if (v1->viewname[i] > v2->viewname[i])
3154 return (asn_compare_oid(&v1->subtree, &v2->subtree));
3158 vacm_new_view(char *vname, struct asn_oid *oid)
3161 struct vacm_view *view, *temp, *prev;
3163 SLIST_FOREACH(view, &vacm_viewlist, vvl)
3164 if (strcmp(vname, view->viewname) == 0)
3167 if ((view = (struct vacm_view *)malloc(sizeof(*view))) == NULL)
3170 memset(view, 0, sizeof(*view));
3171 strlcpy(view->viewname, vname, sizeof(view->viewname));
3172 asn_append_oid(&view->subtree, oid);
3174 if ((prev = SLIST_FIRST(&vacm_viewlist)) == NULL ||
3175 vacm_compare_view(view, prev) < 0) {
3176 SLIST_INSERT_HEAD(&vacm_viewlist, view, vvl);
3180 SLIST_FOREACH(temp, &vacm_viewlist, vvl) {
3181 if ((cmp = vacm_compare_view(view, temp)) <= 0)
3186 if (temp == NULL || cmp < 0)
3187 SLIST_INSERT_AFTER(prev, view, vvl);
3189 SLIST_INSERT_AFTER(temp, view, vvl);
3191 syslog(LOG_ERR, "View %s exists", view->viewname);
3200 vacm_delete_view(struct vacm_view *view)
3202 SLIST_REMOVE(&vacm_viewlist, view, vacm_view, vvl);
3208 struct vacm_context *
3209 vacm_first_context(void)
3211 return (SLIST_FIRST(&vacm_contextlist));
3214 struct vacm_context *
3215 vacm_next_context(struct vacm_context *vacmctx)
3217 if (vacmctx == NULL)
3220 return (SLIST_NEXT(vacmctx, vcl));
3223 struct vacm_context *
3224 vacm_add_context(char *ctxname, int regid)
3227 struct vacm_context *ctx, *temp, *prev;
3229 SLIST_FOREACH(ctx, &vacm_contextlist, vcl)
3230 if (strcmp(ctxname, ctx->ctxname) == 0) {
3231 syslog(LOG_ERR, "Context %s exists", ctx->ctxname);
3235 if ((ctx = (struct vacm_context *)malloc(sizeof(*ctx))) == NULL)
3238 memset(ctx, 0, sizeof(*ctx));
3239 strlcpy(ctx->ctxname, ctxname, sizeof(ctx->ctxname));
3242 if ((prev = SLIST_FIRST(&vacm_contextlist)) == NULL ||
3243 strlen(ctx->ctxname) < strlen(prev->ctxname) ||
3244 strcmp(ctx->ctxname, prev->ctxname) < 0) {
3245 SLIST_INSERT_HEAD(&vacm_contextlist, ctx, vcl);
3249 SLIST_FOREACH(temp, &vacm_contextlist, vcl) {
3250 if (strlen(ctx->ctxname) < strlen(temp->ctxname) ||
3251 strcmp(ctx->ctxname, temp->ctxname) < 0) {
3258 if (temp == NULL || cmp < 0)
3259 SLIST_INSERT_AFTER(prev, ctx, vcl);
3261 SLIST_INSERT_AFTER(temp, ctx, vcl);
3263 syslog(LOG_ERR, "Context %s exists", ctx->ctxname);
3272 vacm_flush_contexts(int regid)
3274 struct vacm_context *ctx, *temp;
3276 SLIST_FOREACH_SAFE(ctx, &vacm_contextlist, vcl, temp)
3277 if (ctx->regid == regid) {
3278 SLIST_REMOVE(&vacm_contextlist, ctx, vacm_context, vcl);