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>
57 #ifdef USE_TCPWRAPPERS
58 #include <arpa/inet.h>
68 #include "trans_inet.h"
70 #define PATH_PID "/var/run/%s.pid"
71 #define PATH_CONFIG "/etc/%s.config"
72 #define PATH_ENGINE "/var/%s.engine"
74 uint64_t this_tick; /* start of processing of current packet (absolute) */
75 uint64_t start_tick; /* start of processing */
77 struct systemg systemg = {
79 { 8, { 1, 3, 6, 1, 4, 1, 1115, 7352 }},
84 struct debug debug = {
86 LOG_DEBUG, /* log_pri */
90 struct snmpd snmpd = {
95 {0, 0, 0, 0}, /* trap1addr */
96 VERS_ENABLE_ALL,/* version_enable */
98 struct snmpd_stats snmpd_stats;
100 struct snmpd_usmstat snmpd_usmstats;
103 struct snmp_engine snmpd_engine;
106 int32_t snmp_serial_no;
108 struct snmpd_target_stats snmpd_target_stats;
110 /* search path for config files */
111 const char *syspath = PATH_SYSCONFIG;
113 /* list of all loaded modules */
114 struct lmodules lmodules = TAILQ_HEAD_INITIALIZER(lmodules);
116 /* list of loaded modules during start-up in the order they were loaded */
117 static struct lmodules modules_start = TAILQ_HEAD_INITIALIZER(modules_start);
119 /* list of all known communities */
120 struct community_list community_list = TAILQ_HEAD_INITIALIZER(community_list);
122 /* list of all known USM users */
123 static struct usm_userlist usm_userlist = SLIST_HEAD_INITIALIZER(usm_userlist);
125 /* A list of all VACM users configured, including v1, v2c and v3 */
126 static struct vacm_userlist vacm_userlist =
127 SLIST_HEAD_INITIALIZER(vacm_userlist);
129 /* A list of all VACM groups */
130 static struct vacm_grouplist vacm_grouplist =
131 SLIST_HEAD_INITIALIZER(vacm_grouplist);
133 static struct vacm_group vacm_default_group = {
137 /* The list of configured access entries */
138 static struct vacm_accesslist vacm_accesslist =
139 TAILQ_HEAD_INITIALIZER(vacm_accesslist);
141 /* The list of configured views */
142 static struct vacm_viewlist vacm_viewlist =
143 SLIST_HEAD_INITIALIZER(vacm_viewlist);
145 /* The list of configured contexts */
146 static struct vacm_contextlist vacm_contextlist =
147 SLIST_HEAD_INITIALIZER(vacm_contextlist);
149 /* list of all installed object resources */
150 struct objres_list objres_list = TAILQ_HEAD_INITIALIZER(objres_list);
152 /* community value generator */
153 static u_int next_community_index = 1;
155 /* list of all known ranges */
156 struct idrange_list idrange_list = TAILQ_HEAD_INITIALIZER(idrange_list);
158 /* identifier generator */
159 u_int next_idrange = 1;
161 /* list of all current timers */
162 struct timer_list timer_list = LIST_HEAD_INITIALIZER(timer_list);
164 /* list of file descriptors */
165 struct fdesc_list fdesc_list = LIST_HEAD_INITIALIZER(fdesc_list);
167 /* program arguments */
168 static char **progargs;
169 static int nprogargs;
171 /* current community */
173 static struct community *comm;
175 /* current USM user */
176 struct usm_user *usm_user;
179 static char config_file[MAXPATHLEN + 1];
180 static char pid_file[MAXPATHLEN + 1];
181 char engine_file[MAXPATHLEN + 1];
183 #ifndef USE_LIBBEGEMOT
185 static evContext evctx;
189 static sigset_t blocked_sigs;
191 /* signal handling */
193 #define WORK_DOINFO 0x0001
194 #define WORK_RECONFIG 0x0002
197 static const struct asn_oid
198 oid_snmpMIB = OIDX_snmpMIB,
199 oid_begemotSnmpd = OIDX_begemotSnmpd,
200 oid_coldStart = OIDX_coldStart,
201 oid_authenticationFailure = OIDX_authenticationFailure;
203 const struct asn_oid oid_zeroDotZero = { 2, { 0, 0 }};
205 const struct asn_oid oid_usmUnknownEngineIDs =
206 { 11, { 1, 3, 6, 1, 6, 3, 15, 1, 1, 4, 0}};
208 const struct asn_oid oid_usmNotInTimeWindows =
209 { 11, { 1, 3, 6, 1, 6, 3, 15, 1, 1, 2, 0}};
211 /* request id generator for traps */
215 static const char usgtxt[] = "\
216 Begemot simple SNMP daemon. Copyright (c) 2001-2002 Fraunhofer Institute for\n\
217 Open Communication Systems (FhG Fokus). All rights reserved.\n\
218 Copyright (c) 2010 The FreeBSD Foundation. All rights reserved.\n\
219 usage: snmpd [-dh] [-c file] [-D options] [-e file] [-I path]\n\
220 [-l prefix] [-m variable=value] [-p file]\n\
222 -d don't daemonize\n\
223 -h print this info\n\
224 -c file specify configuration file\n\
225 -D options debugging options\n\
226 -e file specify engine id file\n\
227 -I path system include path\n\
228 -l prefix default basename for pid and config file\n\
229 -m var=val define variable\n\
230 -p file specify pid file\n\
233 /* hosts_access(3) request */
234 #ifdef USE_TCPWRAPPERS
235 static struct request_info req;
239 extern const struct transport_def udp_trans;
240 extern const struct transport_def lsock_trans;
242 struct transport_list transport_list = TAILQ_HEAD_INITIALIZER(transport_list);
244 /* forward declarations */
245 static void snmp_printf_func(const char *fmt, ...);
246 static void snmp_error_func(const char *err, ...);
247 static void snmp_debug_func(const char *err, ...);
248 static void asn_error_func(const struct asn_buf *b, const char *err, ...);
251 * Allocate rx/tx buffer. We allocate one byte more for rx.
258 if ((buf = malloc(tx ? snmpd.txbuf : snmpd.rxbuf)) == NULL) {
259 syslog(LOG_CRIT, "cannot allocate buffer");
261 snmpd_stats.noTxbuf++;
263 snmpd_stats.noRxbuf++;
270 * Return the buffer size.
275 return (tx ? snmpd.txbuf : snmpd.rxbuf);
279 * Prepare a PDU for output
282 snmp_output(struct snmp_pdu *pdu, u_char *sndbuf, size_t *sndlen,
285 struct asn_buf resp_b;
288 resp_b.asn_ptr = sndbuf;
289 resp_b.asn_len = snmpd.txbuf;
291 if ((code = snmp_pdu_encode(pdu, &resp_b)) != SNMP_CODE_OK) {
292 syslog(LOG_ERR, "cannot encode message (code=%d)", code);
295 if (debug.dump_pdus) {
296 snmp_printf("%s <- ", dest);
299 *sndlen = (size_t)(resp_b.asn_ptr - sndbuf);
303 * Check USM PDU header credentials against local SNMP Engine & users.
305 static enum snmp_code
306 snmp_pdu_auth_user(struct snmp_pdu *pdu)
310 /* un-authenticated snmpEngineId discovery */
311 if (pdu->engine.engine_len == 0 && strlen(pdu->user.sec_name) == 0) {
312 pdu->engine.engine_len = snmpd_engine.engine_len;
313 memcpy(pdu->engine.engine_id, snmpd_engine.engine_id,
314 snmpd_engine.engine_len);
315 update_snmpd_engine_time();
316 pdu->engine.engine_boots = snmpd_engine.engine_boots;
317 pdu->engine.engine_time = snmpd_engine.engine_time;
318 pdu->flags |= SNMP_MSG_AUTODISCOVER;
319 return (SNMP_CODE_OK);
322 if ((usm_user = usm_find_user(pdu->engine.engine_id,
323 pdu->engine.engine_len, pdu->user.sec_name)) == NULL ||
324 usm_user->status != 1 /* active */)
325 return (SNMP_CODE_BADUSER);
327 if (usm_user->user_engine_len != snmpd_engine.engine_len ||
328 memcmp(usm_user->user_engine_id, snmpd_engine.engine_id,
329 snmpd_engine.engine_len) != 0)
330 return (SNMP_CODE_BADENGINE);
332 pdu->user.priv_proto = usm_user->suser.priv_proto;
333 memcpy(pdu->user.priv_key, usm_user->suser.priv_key,
334 sizeof(pdu->user.priv_key));
336 /* authenticated snmpEngineId discovery */
337 if ((pdu->flags & SNMP_MSG_AUTH_FLAG) != 0) {
338 update_snmpd_engine_time();
339 pdu->user.auth_proto = usm_user->suser.auth_proto;
340 memcpy(pdu->user.auth_key, usm_user->suser.auth_key,
341 sizeof(pdu->user.auth_key));
343 if (pdu->engine.engine_boots == 0 &&
344 pdu->engine.engine_time == 0) {
345 update_snmpd_engine_time();
346 pdu->flags |= SNMP_MSG_AUTODISCOVER;
347 return (SNMP_CODE_OK);
350 if (pdu->engine.engine_boots != snmpd_engine.engine_boots ||
351 abs(pdu->engine.engine_time - snmpd_engine.engine_time) >
353 return (SNMP_CODE_NOTINTIME);
356 if (((pdu->flags & SNMP_MSG_PRIV_FLAG) != 0 &&
357 (pdu->flags & SNMP_MSG_AUTH_FLAG) == 0) ||
358 ((pdu->flags & SNMP_MSG_AUTH_FLAG) == 0 &&
359 usm_user->suser.auth_proto != SNMP_AUTH_NOAUTH) ||
360 ((pdu->flags & SNMP_MSG_PRIV_FLAG) == 0 &&
361 usm_user->suser.priv_proto != SNMP_PRIV_NOPRIV))
362 return (SNMP_CODE_BADSECLEVEL);
364 return (SNMP_CODE_OK);
368 * Check whether access to each of var bindings in the PDU is allowed based
369 * on the user credentials against the configured User groups & VACM views.
372 snmp_pdu_auth_access(struct snmp_pdu *pdu, int32_t *ip)
375 int32_t suboid, smodel;
377 struct vacm_user *vuser;
378 struct vacm_access *acl;
379 struct vacm_context *vacmctx;
380 struct vacm_view *view;
383 * At least a default context exists if the snmpd_vacm(3) module is
386 if (SLIST_EMPTY(&vacm_contextlist) ||
387 (pdu->flags & SNMP_MSG_AUTODISCOVER) != 0)
388 return (SNMP_CODE_OK);
390 switch (pdu->version) {
392 if ((uname = comm_string(community)) == NULL)
393 return (SNMP_CODE_FAILED);
394 smodel = SNMP_SECMODEL_SNMPv1;
398 if ((uname = comm_string(community)) == NULL)
399 return (SNMP_CODE_FAILED);
400 smodel = SNMP_SECMODEL_SNMPv2c;
404 uname = pdu->user.sec_name;
405 if ((smodel = pdu->security_model) != SNMP_SECMODEL_USM)
406 return (SNMP_CODE_FAILED);
407 /* Compare the PDU context engine id against the agent's */
408 if (pdu->context_engine_len != snmpd_engine.engine_len ||
409 memcmp(pdu->context_engine, snmpd_engine.engine_id,
410 snmpd_engine.engine_len) != 0)
411 return (SNMP_CODE_FAILED);
418 SLIST_FOREACH(vuser, &vacm_userlist, vvu)
419 if (strcmp(uname, vuser->secname) == 0 &&
420 vuser->sec_model == smodel)
423 if (vuser == NULL || vuser->group == NULL)
424 return (SNMP_CODE_FAILED);
426 /* XXX: shteryana - recheck */
427 TAILQ_FOREACH_REVERSE(acl, &vacm_accesslist, vacm_accesslist, vva) {
428 if (acl->group != vuser->group)
430 SLIST_FOREACH(vacmctx, &vacm_contextlist, vcl)
431 if (memcmp(vacmctx->ctxname, acl->ctx_prefix,
432 acl->ctx_match) == 0)
436 return (SNMP_CODE_FAILED);
442 case SNMP_PDU_GETNEXT:
443 case SNMP_PDU_GETBULK:
444 if ((view = acl->read_view) == NULL)
445 return (SNMP_CODE_FAILED);
449 if ((view = acl->write_view) == NULL)
450 return (SNMP_CODE_FAILED);
454 case SNMP_PDU_INFORM:
456 case SNMP_PDU_REPORT:
457 if ((view = acl->notify_view) == NULL)
458 return (SNMP_CODE_FAILED);
460 case SNMP_PDU_RESPONSE:
462 return (SNMP_CODE_FAILED);
467 for (i = 0; i < pdu->nbindings; i++) {
468 /* XXX - view->mask*/
469 suboid = asn_is_suboid(&view->subtree, &pdu->bindings[i].var);
470 if ((!suboid && !view->exclude) || (suboid && view->exclude)) {
472 return (SNMP_CODE_FAILED);
476 return (SNMP_CODE_OK);
480 * SNMP input. Start: decode the PDU, find the user or community.
483 snmp_input_start(const u_char *buf, size_t len, const char *source,
484 struct snmp_pdu *pdu, int32_t *ip, size_t *pdulen)
488 enum snmpd_input_err ret;
492 this_tick = get_ticks();
497 ret = SNMPD_INPUT_OK;
499 /* look whether we have enough bytes for the entire PDU. */
500 switch (sret = snmp_pdu_snoop(&b)) {
503 return (SNMPD_INPUT_TRUNC);
506 snmpd_stats.inASNParseErrs++;
507 return (SNMPD_INPUT_FAILED);
509 b.asn_len = *pdulen = (size_t)sret;
511 memset(pdu, 0, sizeof(*pdu));
512 if ((code = snmp_pdu_decode_header(&b, pdu)) != SNMP_CODE_OK)
515 if (pdu->version == SNMP_V3) {
516 if (pdu->security_model != SNMP_SECMODEL_USM) {
517 code = SNMP_CODE_FAILED;
520 if ((code = snmp_pdu_auth_user(pdu)) != SNMP_CODE_OK)
522 if ((code = snmp_pdu_decode_secmode(&b, pdu)) != SNMP_CODE_OK)
525 code = snmp_pdu_decode_scoped(&b, pdu, ip);
528 snmpd_stats.inPkts++;
532 case SNMP_CODE_FAILED:
533 snmpd_stats.inASNParseErrs++;
534 return (SNMPD_INPUT_FAILED);
536 case SNMP_CODE_BADVERS:
538 snmpd_stats.inBadVersions++;
539 return (SNMPD_INPUT_FAILED);
541 case SNMP_CODE_BADLEN:
542 if (pdu->type == SNMP_OP_SET)
543 ret = SNMPD_INPUT_VALBADLEN;
546 case SNMP_CODE_OORANGE:
547 if (pdu->type == SNMP_OP_SET)
548 ret = SNMPD_INPUT_VALRANGE;
551 case SNMP_CODE_BADENC:
552 if (pdu->type == SNMP_OP_SET)
553 ret = SNMPD_INPUT_VALBADENC;
556 case SNMP_CODE_BADSECLEVEL:
557 snmpd_usmstats.unsupported_seclevels++;
558 return (SNMPD_INPUT_FAILED);
560 case SNMP_CODE_NOTINTIME:
561 snmpd_usmstats.not_in_time_windows++;
562 return (SNMPD_INPUT_FAILED);
564 case SNMP_CODE_BADUSER:
565 snmpd_usmstats.unknown_users++;
566 return (SNMPD_INPUT_FAILED);
568 case SNMP_CODE_BADENGINE:
569 snmpd_usmstats.unknown_engine_ids++;
570 return (SNMPD_INPUT_FAILED);
572 case SNMP_CODE_BADDIGEST:
573 snmpd_usmstats.wrong_digests++;
574 return (SNMPD_INPUT_FAILED);
576 case SNMP_CODE_EDECRYPT:
577 snmpd_usmstats.decrypt_errors++;
578 return (SNMPD_INPUT_FAILED);
581 switch (pdu->version) {
584 if (!(snmpd.version_enable & VERS_ENABLE_V1))
589 if (!(snmpd.version_enable & VERS_ENABLE_V2C))
594 if (!(snmpd.version_enable & VERS_ENABLE_V3))
604 if (debug.dump_pdus) {
605 snmp_printf("%s -> ", source);
610 * Look, whether we know the community or user
613 if (pdu->version != SNMP_V3) {
614 TAILQ_FOREACH(comm, &community_list, link)
615 if (comm->string != NULL &&
616 strcmp(comm->string, pdu->community) == 0)
620 snmpd_stats.inBadCommunityNames++;
622 if (snmpd.auth_traps)
623 snmp_send_trap(&oid_authenticationFailure,
624 (struct snmp_value *)NULL);
625 ret = SNMPD_INPUT_BAD_COMM;
627 community = comm->value;
628 } else if (pdu->nbindings == 0) {
629 /* RFC 3414 - snmpEngineID Discovery */
630 if (strlen(pdu->user.sec_name) == 0) {
631 asn_append_oid(&(pdu->bindings[pdu->nbindings++].var),
632 &oid_usmUnknownEngineIDs);
633 pdu->context_engine_len = snmpd_engine.engine_len;
634 memcpy(pdu->context_engine, snmpd_engine.engine_id,
635 snmpd_engine.engine_len);
636 } else if (pdu->engine.engine_boots == 0 &&
637 pdu->engine.engine_time == 0) {
638 asn_append_oid(&(pdu->bindings[pdu->nbindings++].var),
639 &oid_usmNotInTimeWindows);
640 update_snmpd_engine_time();
641 pdu->engine.engine_boots = snmpd_engine.engine_boots;
642 pdu->engine.engine_time = snmpd_engine.engine_time;
644 } else if (usm_user->suser.auth_proto != SNMP_AUTH_NOAUTH &&
645 (pdu->engine.engine_boots == 0 || pdu->engine.engine_time == 0)) {
646 snmpd_usmstats.not_in_time_windows++;
647 ret = SNMPD_INPUT_FAILED;
650 if ((code = snmp_pdu_auth_access(pdu, ip)) != SNMP_CODE_OK)
651 ret = SNMPD_INPUT_FAILED;
657 * Will return only _OK or _FAILED
660 snmp_input_finish(struct snmp_pdu *pdu, const u_char *rcvbuf, size_t rcvlen,
661 u_char *sndbuf, size_t *sndlen, const char *source,
662 enum snmpd_input_err ierr, int32_t ivar, void *data)
664 struct snmp_pdu resp;
665 struct asn_buf resp_b, pdu_b;
668 resp_b.asn_ptr = sndbuf;
669 resp_b.asn_len = snmpd.txbuf;
671 pdu_b.asn_cptr = rcvbuf;
672 pdu_b.asn_len = rcvlen;
674 if (ierr != SNMPD_INPUT_OK) {
675 /* error decoding the input of a SET */
676 if (pdu->version == SNMP_V1)
677 pdu->error_status = SNMP_ERR_BADVALUE;
678 else if (ierr == SNMPD_INPUT_VALBADLEN)
679 pdu->error_status = SNMP_ERR_WRONG_LENGTH;
680 else if (ierr == SNMPD_INPUT_VALRANGE)
681 pdu->error_status = SNMP_ERR_WRONG_VALUE;
683 pdu->error_status = SNMP_ERR_WRONG_ENCODING;
685 pdu->error_index = ivar;
687 if (snmp_make_errresp(pdu, &pdu_b, &resp_b) == SNMP_RET_IGN) {
688 syslog(LOG_WARNING, "could not encode error response");
689 snmpd_stats.silentDrops++;
690 return (SNMPD_INPUT_FAILED);
693 if (debug.dump_pdus) {
694 snmp_printf("%s <- ", source);
697 *sndlen = (size_t)(resp_b.asn_ptr - sndbuf);
698 return (SNMPD_INPUT_OK);
704 ret = snmp_get(pdu, &resp_b, &resp, data);
707 case SNMP_PDU_GETNEXT:
708 ret = snmp_getnext(pdu, &resp_b, &resp, data);
712 ret = snmp_set(pdu, &resp_b, &resp, data);
715 case SNMP_PDU_GETBULK:
716 ret = snmp_getbulk(pdu, &resp_b, &resp, data);
727 /* normal return - send a response */
728 if (debug.dump_pdus) {
729 snmp_printf("%s <- ", source);
730 snmp_pdu_dump(&resp);
732 *sndlen = (size_t)(resp_b.asn_ptr - sndbuf);
733 snmp_pdu_free(&resp);
734 return (SNMPD_INPUT_OK);
737 /* error - send nothing */
738 snmpd_stats.silentDrops++;
739 return (SNMPD_INPUT_FAILED);
742 /* error - send error response. The snmp routine has
743 * changed the error fields in the original message. */
744 resp_b.asn_ptr = sndbuf;
745 resp_b.asn_len = snmpd.txbuf;
746 if (snmp_make_errresp(pdu, &pdu_b, &resp_b) == SNMP_RET_IGN) {
747 syslog(LOG_WARNING, "could not encode error response");
748 snmpd_stats.silentDrops++;
749 return (SNMPD_INPUT_FAILED);
751 if (debug.dump_pdus) {
752 snmp_printf("%s <- ", source);
755 *sndlen = (size_t)(resp_b.asn_ptr - sndbuf);
756 return (SNMPD_INPUT_OK);
763 * Insert a port into the right place in the transport's table of ports
766 trans_insert_port(struct transport *t, struct tport *port)
771 TAILQ_FOREACH(p, &t->table, link) {
772 if (asn_compare_oid(&p->index, &port->index) > 0) {
773 TAILQ_INSERT_BEFORE(p, port, link);
777 TAILQ_INSERT_TAIL(&t->table, port, link);
781 * Remove a port from a transport's list
784 trans_remove_port(struct tport *port)
787 TAILQ_REMOVE(&port->transport->table, port, link);
791 * Find a port on a transport's list
794 trans_find_port(struct transport *t, const struct asn_oid *idx, u_int sub)
797 return (FIND_OBJECT_OID(&t->table, idx, sub));
801 * Find next port on a transport's list
804 trans_next_port(struct transport *t, const struct asn_oid *idx, u_int sub)
807 return (NEXT_OBJECT_OID(&t->table, idx, sub));
814 trans_first_port(struct transport *t)
817 return (TAILQ_FIRST(&t->table));
821 * Iterate through all ports until a function returns a 0.
824 trans_iter_port(struct transport *t, int (*func)(struct tport *, intptr_t),
829 TAILQ_FOREACH(p, &t->table, link)
830 if (func(p, arg) == 0)
836 * Register a transport
839 trans_register(const struct transport_def *def, struct transport **pp)
844 if ((*pp = malloc(sizeof(**pp))) == NULL)
845 return (SNMP_ERR_GENERR);
847 /* construct index */
848 (*pp)->index.len = strlen(def->name) + 1;
849 (*pp)->index.subs[0] = strlen(def->name);
850 for (i = 0; i < (*pp)->index.subs[0]; i++)
851 (*pp)->index.subs[i + 1] = def->name[i];
855 if (FIND_OBJECT_OID(&transport_list, &(*pp)->index, 0) != NULL) {
857 return (SNMP_ERR_INCONS_VALUE);
860 /* register module */
861 snprintf(or_descr, sizeof(or_descr), "%s transport mapping", def->name);
862 if (((*pp)->or_index = or_register(&def->id, or_descr, NULL)) == 0) {
864 return (SNMP_ERR_GENERR);
867 INSERT_OBJECT_OID((*pp), &transport_list);
869 TAILQ_INIT(&(*pp)->table);
871 return (SNMP_ERR_NOERROR);
875 * Unregister transport
878 trans_unregister(struct transport *t)
880 if (!TAILQ_EMPTY(&t->table))
881 return (SNMP_ERR_INCONS_VALUE);
883 or_unregister(t->or_index);
884 TAILQ_REMOVE(&transport_list, t, link);
886 return (SNMP_ERR_NOERROR);
890 * File descriptor support
892 #ifdef USE_LIBBEGEMOT
894 input(int fd, int mask __unused, void *uap)
897 input(evContext ctx __unused, void *uap, int fd, int mask __unused)
900 struct fdesc *f = uap;
902 (*f->func)(fd, f->udata);
910 #ifdef USE_LIBBEGEMOT
912 poll_unregister(f->id);
916 if (evTestID(f->id)) {
917 (void)evDeselectFD(evctx, f->id);
929 #ifdef USE_LIBBEGEMOT
932 if ((f->id = poll_register(f->fd, input, f, RPOLL_IN)) < 0) {
934 syslog(LOG_ERR, "select fd %d: %m", f->fd);
941 if (evSelectFD(evctx, f->fd, EV_READ, input, f, &f->id)) {
943 syslog(LOG_ERR, "select fd %d: %m", f->fd);
952 fd_select(int fd, void (*func)(int, void *), void *udata, struct lmodule *mod)
957 if ((f = malloc(sizeof(struct fdesc))) == NULL) {
959 syslog(LOG_ERR, "fd_select: %m");
967 #ifdef USE_LIBBEGEMOT
980 LIST_INSERT_HEAD(&fdesc_list, f, link);
990 LIST_REMOVE(f, link);
996 fd_flush(struct lmodule *mod)
998 struct fdesc *t, *t1;
1000 t = LIST_FIRST(&fdesc_list);
1002 t1 = LIST_NEXT(t, link);
1003 if (t->owner == mod)
1010 * Consume a message from the input buffer
1013 snmp_input_consume(struct port_input *pi)
1016 /* always consume everything */
1020 if (pi->consumed >= pi->length) {
1021 /* all bytes consumed */
1025 memmove(pi->buf, pi->buf + pi->consumed, pi->length - pi->consumed);
1026 pi->length -= pi->consumed;
1030 * Input from a socket
1033 snmpd_input(struct port_input *pi, struct tport *tport)
1037 struct snmp_pdu pdu;
1038 enum snmpd_input_err ierr, ferr;
1039 enum snmpd_proxy_err perr;
1042 #ifdef USE_TCPWRAPPERS
1043 char client[INET6_ADDRSTRLEN];
1046 ret = tport->transport->vtab->recv(tport, pi);
1050 #ifdef USE_TCPWRAPPERS
1052 * In case of AF_INET{6} peer, do hosts_access(5) check.
1054 if (pi->peer->sa_family != AF_LOCAL &&
1055 inet_ntop(pi->peer->sa_family,
1056 &((const struct sockaddr_in *)(const void *)pi->peer)->sin_addr,
1057 client, sizeof(client)) != NULL) {
1058 request_set(&req, RQ_CLIENT_ADDR, client, 0);
1059 if (hosts_access(&req) == 0) {
1060 syslog(LOG_ERR, "refused connection from %.500s",
1064 } else if (pi->peer->sa_family != AF_LOCAL)
1065 syslog(LOG_ERR, "inet_ntop(): %m");
1071 ierr = snmp_input_start(pi->buf, pi->length, "SNMP", &pdu, &vi,
1073 if (ierr == SNMPD_INPUT_TRUNC) {
1074 /* need more bytes. This is ok only for streaming transports.
1075 * but only if we have not reached bufsiz yet. */
1077 if (pi->length == buf_size(0)) {
1078 snmpd_stats.silentDrops++;
1083 snmpd_stats.silentDrops++;
1087 /* can't check for bad SET pdus here, because a proxy may have to
1088 * check the access first. We don't want to return an error response
1089 * to a proxy PDU with a wrong community */
1090 if (ierr == SNMPD_INPUT_FAILED) {
1091 /* for streaming transports this is fatal */
1094 snmp_input_consume(pi);
1097 if (ierr == SNMPD_INPUT_BAD_COMM) {
1098 snmp_input_consume(pi);
1103 * If that is a module community and the module has a proxy function,
1104 * the hand it over to the module.
1106 if (comm != NULL && comm->owner != NULL &&
1107 comm->owner->config->proxy != NULL) {
1108 perr = (*comm->owner->config->proxy)(&pdu, tport->transport,
1109 &tport->index, pi->peer, pi->peerlen, ierr, vi,
1110 !pi->cred || pi->priv);
1114 case SNMPD_PROXY_OK:
1115 snmp_input_consume(pi);
1118 case SNMPD_PROXY_REJ:
1121 case SNMPD_PROXY_DROP:
1122 snmp_input_consume(pi);
1123 snmp_pdu_free(&pdu);
1124 snmpd_stats.proxyDrops++;
1127 case SNMPD_PROXY_BADCOMM:
1128 snmp_input_consume(pi);
1129 snmp_pdu_free(&pdu);
1130 snmpd_stats.inBadCommunityNames++;
1131 if (snmpd.auth_traps)
1132 snmp_send_trap(&oid_authenticationFailure,
1133 (struct snmp_value *)NULL);
1136 case SNMPD_PROXY_BADCOMMUSE:
1137 snmp_input_consume(pi);
1138 snmp_pdu_free(&pdu);
1139 snmpd_stats.inBadCommunityUses++;
1140 if (snmpd.auth_traps)
1141 snmp_send_trap(&oid_authenticationFailure,
1142 (struct snmp_value *)NULL);
1150 if (pdu.type == SNMP_PDU_RESPONSE ||
1151 pdu.type == SNMP_PDU_TRAP ||
1152 pdu.type == SNMP_PDU_TRAP2) {
1153 snmpd_stats.silentDrops++;
1154 snmpd_stats.inBadPduTypes++;
1155 snmp_pdu_free(&pdu);
1156 snmp_input_consume(pi);
1163 if (pdu.version < SNMP_V3 &&
1164 ((pi->cred && !pi->priv && pdu.type == SNMP_PDU_SET) ||
1165 (comm != NULL && comm->private != COMM_WRITE &&
1166 (pdu.type == SNMP_PDU_SET || comm->private != COMM_READ)))) {
1167 snmpd_stats.inBadCommunityUses++;
1168 snmp_pdu_free(&pdu);
1169 snmp_input_consume(pi);
1170 if (snmpd.auth_traps)
1171 snmp_send_trap(&oid_authenticationFailure,
1172 (struct snmp_value *)NULL);
1179 if ((sndbuf = buf_alloc(1)) == NULL) {
1180 snmpd_stats.silentDrops++;
1181 snmp_pdu_free(&pdu);
1182 snmp_input_consume(pi);
1185 ferr = snmp_input_finish(&pdu, pi->buf, pi->length,
1186 sndbuf, &sndlen, "SNMP", ierr, vi, NULL);
1188 if (ferr == SNMPD_INPUT_OK) {
1189 if (tport->transport->vtab->send != NULL)
1190 slen = tport->transport->vtab->send(tport, sndbuf,
1191 sndlen, pi->peer, pi->peerlen);
1193 slen = tport->transport->vtab->send2(tport, sndbuf,
1196 syslog(LOG_ERR, "send*: %m");
1197 else if ((size_t)slen != sndlen)
1198 syslog(LOG_ERR, "send*: short write %zu/%zu", sndlen,
1202 snmp_pdu_free(&pdu);
1204 snmp_input_consume(pi);
1210 * Send a PDU to a given port. If this is a multi-socket port, use the
1214 snmp_send_port(void *targ, const struct asn_oid *port, struct snmp_pdu *pdu,
1215 const struct sockaddr *addr, socklen_t addrlen)
1217 struct transport *trans = targ;
1223 TAILQ_FOREACH(tp, &trans->table, link)
1224 if (asn_compare_oid(port, &tp->index) == 0)
1229 if ((sndbuf = buf_alloc(1)) == NULL)
1232 snmp_output(pdu, sndbuf, &sndlen, "SNMP PROXY");
1234 if (trans->vtab->send != NULL)
1235 len = trans->vtab->send(tp, sndbuf, sndlen, addr, addrlen);
1237 len = trans->vtab->send2(tp, sndbuf, sndlen, NULL);
1240 syslog(LOG_ERR, "sendto: %m");
1241 else if ((size_t)len != sndlen)
1242 syslog(LOG_ERR, "sendto: short write %zu/%zu",
1243 sndlen, (size_t)len);
1250 * Close an input source
1252 * \param pi input instance
1255 snmpd_input_close(struct port_input *pi)
1257 if (pi->id != NULL) {
1258 fd_deselect(pi->id);
1262 (void)close(pi->fd);
1265 if (pi->buf != NULL) {
1272 * Initialize an input source.
1274 * \param pi input instance
1277 snmpd_input_init(struct port_input *pi)
1285 * Dump internal state.
1287 #ifdef USE_LIBBEGEMOT
1292 info_func(evContext ctx __unused, void *uap __unused, const void *tag __unused)
1299 syslog(LOG_DEBUG, "Dump of SNMPd %lu\n", (u_long)getpid());
1300 for (i = 0; i < tree_size; i++) {
1301 switch (tree[i].type) {
1303 case SNMP_NODE_LEAF:
1304 sprintf(buf, "LEAF: %s %s", tree[i].name,
1305 asn_oid2str(&tree[i].oid));
1308 case SNMP_NODE_COLUMN:
1309 sprintf(buf, "COL: %s %s", tree[i].name,
1310 asn_oid2str(&tree[i].oid));
1313 syslog(LOG_DEBUG, "%s", buf);
1316 TAILQ_FOREACH(m, &lmodules, link)
1317 if (m->config->dump)
1318 (*m->config->dump)();
1322 * Re-read configuration
1324 #ifdef USE_LIBBEGEMOT
1329 config_func(evContext ctx __unused, void *uap __unused,
1330 const void *tag __unused)
1335 if (read_config(config_file, NULL)) {
1336 syslog(LOG_ERR, "error reading config file '%s'", config_file);
1339 TAILQ_FOREACH(m, &lmodules, link)
1340 if (m->config->config)
1341 (*m->config->config)();
1345 * On USR1 dump actual configuration.
1348 onusr1(int s __unused)
1351 work |= WORK_DOINFO;
1354 onhup(int s __unused)
1357 work |= WORK_RECONFIG;
1361 onterm(int s __unused)
1364 /* allow clean-up */
1371 struct sigaction sa;
1373 sa.sa_handler = onusr1;
1374 sa.sa_flags = SA_RESTART;
1375 sigemptyset(&sa.sa_mask);
1376 if (sigaction(SIGUSR1, &sa, NULL)) {
1377 syslog(LOG_ERR, "sigaction: %m");
1381 sa.sa_handler = onhup;
1382 if (sigaction(SIGHUP, &sa, NULL)) {
1383 syslog(LOG_ERR, "sigaction: %m");
1387 sa.sa_handler = onterm;
1389 sigemptyset(&sa.sa_mask);
1390 if (sigaction(SIGTERM, &sa, NULL)) {
1391 syslog(LOG_ERR, "sigaction: %m");
1394 if (sigaction(SIGINT, &sa, NULL)) {
1395 syslog(LOG_ERR, "sigaction: %m");
1406 if (sigprocmask(SIG_BLOCK, &set, &blocked_sigs) == -1) {
1407 syslog(LOG_ERR, "SIG_BLOCK: %m");
1414 if (sigprocmask(SIG_SETMASK, &blocked_sigs, NULL) == -1) {
1415 syslog(LOG_ERR, "SIG_SETMASK: %m");
1426 (void)unlink(pid_file);
1432 struct transport *t;
1434 TAILQ_FOREACH(t, &transport_list, link)
1435 (void)t->vtab->stop(1);
1439 * Define a macro from the command line
1447 if ((eq = strchr(arg, '=')) == NULL)
1448 err = define_macro(arg, "");
1451 err = define_macro(arg, eq);
1454 syslog(LOG_ERR, "cannot save macro: %m");
1460 * Re-implement getsubopt from scratch, because the second argument is broken
1461 * and will not compile with WARNS=5.
1464 getsubopt1(char **arg, const char *const *options, char **valp, char **optp)
1466 static const char *const delim = ",\t ";
1472 /* skip leading junk */
1473 for (ptr = *arg; *ptr != '\0'; ptr++)
1474 if (strchr(delim, *ptr) == NULL)
1482 /* find the end of the option */
1483 while (*++ptr != '\0')
1484 if (strchr(delim, *ptr) != NULL || *ptr == '=')
1491 while (*ptr != '\0' && strchr(delim, *ptr) == NULL)
1501 for (i = 0; *options != NULL; options++, i++)
1502 if (strcmp(*optp, *options) == 0)
1508 main(int argc, char *argv[])
1514 const char *prefix = "snmpd";
1516 char *value = NULL, *option; /* XXX */
1517 struct transport *t;
1520 #define DBG_EVENTS 1
1522 static const char *const debug_opts[] = {
1529 snmp_printf = snmp_printf_func;
1530 snmp_error = snmp_error_func;
1531 snmp_debug = snmp_debug_func;
1532 asn_error = asn_error_func;
1534 while ((opt = getopt(argc, argv, "c:dD:e:hI:l:m:p:")) != EOF)
1538 strlcpy(config_file, optarg, sizeof(config_file));
1547 switch (getsubopt1(&optarg, debug_opts,
1551 debug.dump_pdus = 1;
1561 "no value for 'trace'");
1563 snmp_trace = strtoul(value,
1570 "unknown debug flag '%s'",
1574 "missing debug flag");
1581 strlcpy(engine_file, optarg, sizeof(engine_file));
1584 fprintf(stderr, "%s", usgtxt);
1600 strlcpy(pid_file, optarg, sizeof(pid_file));
1604 openlog(prefix, LOG_PID | (background ? 0 : LOG_PERROR), LOG_USER);
1605 setlogmask(LOG_UPTO(debug.logpri - 1));
1607 if (background && daemon(0, 0) < 0) {
1608 syslog(LOG_ERR, "daemon: %m");
1620 snmp_serial_no = random();
1622 #ifdef USE_TCPWRAPPERS
1624 * Initialize hosts_access(3) handler.
1626 request_init(&req, RQ_DAEMON, "snmpd", 0);
1631 * Initialize the tree.
1633 if ((tree = malloc(sizeof(struct snmp_node) * CTREE_SIZE)) == NULL) {
1634 syslog(LOG_ERR, "%m");
1637 memcpy(tree, ctree, sizeof(struct snmp_node) * CTREE_SIZE);
1638 tree_size = CTREE_SIZE;
1641 * Get standard communities
1643 comm_define(COMM_READ, "SNMP read", NULL, NULL);
1644 comm_define(COMM_WRITE, "SNMP write", NULL, NULL);
1645 community = COMM_INITIALIZE;
1647 trap_reqid = reqid_allocate(512, NULL);
1649 if (config_file[0] == '\0')
1650 snprintf(config_file, sizeof(config_file), PATH_CONFIG, prefix);
1653 init_snmpd_engine();
1655 this_tick = get_ticks();
1656 start_tick = this_tick;
1658 /* start transports */
1659 if (atexit(trans_stop) == -1) {
1660 syslog(LOG_ERR, "atexit failed: %m");
1663 if (udp_trans.start() != SNMP_ERR_NOERROR)
1664 syslog(LOG_WARNING, "cannot start UDP transport");
1665 if (lsock_trans.start() != SNMP_ERR_NOERROR)
1666 syslog(LOG_WARNING, "cannot start LSOCK transport");
1667 if (inet_trans.start() != SNMP_ERR_NOERROR)
1668 syslog(LOG_WARNING, "cannot start INET transport");
1670 #ifdef USE_LIBBEGEMOT
1671 if (debug.evdebug > 0)
1674 if (evCreate(&evctx)) {
1675 syslog(LOG_ERR, "evCreate: %m");
1678 if (debug.evdebug > 0)
1679 evSetDebug(evctx, 10, stderr);
1682 if (engine_file[0] == '\0')
1683 snprintf(engine_file, sizeof(engine_file), PATH_ENGINE, prefix);
1685 if (read_config(config_file, NULL)) {
1686 syslog(LOG_ERR, "error in config file");
1690 TAILQ_FOREACH(t, &transport_list, link)
1691 TAILQ_FOREACH(p, &t->table, link)
1692 t->vtab->init_port(p);
1696 if (pid_file[0] == '\0')
1697 snprintf(pid_file, sizeof(pid_file), PATH_PID, prefix);
1699 if ((fp = fopen(pid_file, "w")) != NULL) {
1700 fprintf(fp, "%u", getpid());
1702 if (atexit(term) == -1) {
1703 syslog(LOG_ERR, "atexit failed: %m");
1704 (void)remove(pid_file);
1709 if (or_register(&oid_snmpMIB, "The MIB module for SNMPv2 entities.",
1711 syslog(LOG_ERR, "cannot register SNMPv2 MIB");
1714 if (or_register(&oid_begemotSnmpd, "The MIB module for the Begemot SNMPd.",
1716 syslog(LOG_ERR, "cannot register begemotSnmpd MIB");
1720 while ((m = TAILQ_FIRST(&modules_start)) != NULL) {
1721 m->flags &= ~LM_ONSTARTLIST;
1722 TAILQ_REMOVE(&modules_start, m, start);
1726 snmp_send_trap(&oid_coldStart, (struct snmp_value *)NULL);
1729 #ifndef USE_LIBBEGEMOT
1732 struct lmodule *mod;
1734 TAILQ_FOREACH(mod, &lmodules, link)
1735 if (mod->config->idle != NULL)
1736 (*mod->config->idle)();
1738 #ifndef USE_LIBBEGEMOT
1739 if (evGetNext(evctx, &event, EV_WAIT) == 0) {
1740 if (evDispatch(evctx, event))
1741 syslog(LOG_ERR, "evDispatch: %m");
1742 } else if (errno != EINTR) {
1743 syslog(LOG_ERR, "evGetNext: %m");
1752 if (work & WORK_DOINFO) {
1753 #ifdef USE_LIBBEGEMOT
1756 if (evWaitFor(evctx, &work, info_func,
1757 NULL, NULL) == -1) {
1758 syslog(LOG_ERR, "evWaitFor: %m");
1763 if (work & WORK_RECONFIG) {
1764 #ifdef USE_LIBBEGEMOT
1767 if (evWaitFor(evctx, &work, config_func,
1768 NULL, NULL) == -1) {
1769 syslog(LOG_ERR, "evWaitFor: %m");
1776 #ifndef USE_LIBBEGEMOT
1777 if (evDo(evctx, &work) == -1) {
1778 syslog(LOG_ERR, "evDo: %m");
1794 if (gettimeofday(&tv, NULL))
1796 ret = tv.tv_sec * 100ULL + tv.tv_usec / 10000ULL;
1805 * Trampoline for the non-repeatable timers.
1807 #ifdef USE_LIBBEGEMOT
1809 tfunc(int tid __unused, void *uap)
1812 tfunc(evContext ctx __unused, void *uap, struct timespec due __unused,
1813 struct timespec inter __unused)
1816 struct timer *tp = uap;
1818 LIST_REMOVE(tp, link);
1819 tp->func(tp->udata);
1824 * Trampoline for the repeatable timers.
1826 #ifdef USE_LIBBEGEMOT
1828 trfunc(int tid __unused, void *uap)
1831 trfunc(evContext ctx __unused, void *uap, struct timespec due __unused,
1832 struct timespec inter __unused)
1835 struct timer *tp = uap;
1837 tp->func(tp->udata);
1841 * Start a one-shot timer
1844 timer_start(u_int ticks, void (*func)(void *), void *udata, struct lmodule *mod)
1847 #ifndef USE_LIBBEGEMOT
1848 struct timespec due;
1851 if ((tp = malloc(sizeof(struct timer))) == NULL) {
1852 syslog(LOG_CRIT, "out of memory for timer");
1856 #ifndef USE_LIBBEGEMOT
1857 due = evAddTime(evNowTime(),
1858 evConsTime(ticks / 100, (ticks % 100) * 10000));
1865 LIST_INSERT_HEAD(&timer_list, tp, link);
1867 #ifdef USE_LIBBEGEMOT
1868 if ((tp->id = poll_start_timer(ticks * 10, 0, tfunc, tp)) < 0) {
1869 syslog(LOG_ERR, "cannot set timer: %m");
1873 if (evSetTimer(evctx, tfunc, tp, due, evConsTime(0, 0), &tp->id)
1875 syslog(LOG_ERR, "cannot set timer: %m");
1883 * Start a repeatable timer. When used with USE_LIBBEGEMOT the first argument
1884 * is currently ignored and the initial number of ticks is set to the
1885 * repeat number of ticks.
1888 timer_start_repeat(u_int ticks __unused, u_int repeat_ticks,
1889 void (*func)(void *), void *udata, struct lmodule *mod)
1892 #ifndef USE_LIBBEGEMOT
1893 struct timespec due;
1894 struct timespec inter;
1897 if ((tp = malloc(sizeof(struct timer))) == NULL) {
1898 syslog(LOG_CRIT, "out of memory for timer");
1902 #ifndef USE_LIBBEGEMOT
1903 due = evAddTime(evNowTime(),
1904 evConsTime(ticks / 100, (ticks % 100) * 10000));
1905 inter = evConsTime(repeat_ticks / 100, (repeat_ticks % 100) * 10000);
1912 LIST_INSERT_HEAD(&timer_list, tp, link);
1914 #ifdef USE_LIBBEGEMOT
1915 if ((tp->id = poll_start_timer(repeat_ticks * 10, 1, trfunc, tp)) < 0) {
1916 syslog(LOG_ERR, "cannot set timer: %m");
1920 if (evSetTimer(evctx, trfunc, tp, due, inter, &tp->id) == -1) {
1921 syslog(LOG_ERR, "cannot set timer: %m");
1934 struct timer *tp = p;
1936 LIST_REMOVE(tp, link);
1937 #ifdef USE_LIBBEGEMOT
1938 poll_stop_timer(tp->id);
1940 if (evClearTimer(evctx, tp->id) == -1) {
1941 syslog(LOG_ERR, "cannot stop timer: %m");
1949 timer_flush(struct lmodule *mod)
1951 struct timer *t, *t1;
1953 t = LIST_FIRST(&timer_list);
1955 t1 = LIST_NEXT(t, link);
1956 if (t->owner == mod)
1963 snmp_printf_func(const char *fmt, ...)
1966 static char *pend = NULL;
1970 vasprintf(&ret, fmt, ap);
1976 if ((new = realloc(pend, strlen(pend) + strlen(ret) + 1))
1987 while ((ret = strchr(pend, '\n')) != NULL) {
1989 syslog(LOG_DEBUG, "%s", pend);
1990 if (strlen(ret + 1) == 0) {
1995 strcpy(pend, ret + 1);
2000 snmp_error_func(const char *err, ...)
2005 if (!(snmp_trace & LOG_SNMP_ERRORS))
2009 snprintf(errbuf, sizeof(errbuf), "SNMP: ");
2010 vsnprintf(errbuf + strlen(errbuf),
2011 sizeof(errbuf) - strlen(errbuf), err, ap);
2014 syslog(LOG_ERR, "%s", errbuf);
2018 snmp_debug_func(const char *err, ...)
2024 snprintf(errbuf, sizeof(errbuf), "SNMP: ");
2025 vsnprintf(errbuf+strlen(errbuf), sizeof(errbuf)-strlen(errbuf),
2029 syslog(LOG_DEBUG, "%s", errbuf);
2033 asn_error_func(const struct asn_buf *b, const char *err, ...)
2039 if (!(snmp_trace & LOG_ASN1_ERRORS))
2043 snprintf(errbuf, sizeof(errbuf), "ASN.1: ");
2044 vsnprintf(errbuf + strlen(errbuf),
2045 sizeof(errbuf) - strlen(errbuf), err, ap);
2049 snprintf(errbuf + strlen(errbuf),
2050 sizeof(errbuf) - strlen(errbuf), " at");
2051 for (i = 0; b->asn_len > i; i++)
2052 snprintf(errbuf + strlen(errbuf),
2053 sizeof(errbuf) - strlen(errbuf),
2054 " %02x", b->asn_cptr[i]);
2057 syslog(LOG_ERR, "%s", errbuf);
2061 * Create a new community
2064 comm_define_ordered(u_int priv, const char *descr, struct asn_oid *idx,
2065 struct lmodule *owner, const char *str)
2067 struct community *c, *p;
2070 ncomm = idx->subs[idx->len - 1];
2072 /* check that community doesn't already exist */
2073 TAILQ_FOREACH(c, &community_list, link)
2074 if (c->value == ncomm)
2077 if ((c = malloc(sizeof(struct community))) == NULL) {
2078 syslog(LOG_ERR, "%s: %m", __func__);
2088 if((c->string = malloc(strlen(str)+1)) == NULL) {
2092 strcpy(c->string, str);
2098 TAILQ_FOREACH(p, &community_list, link) {
2099 if (asn_compare_oid(&p->index, &c->index) > 0) {
2100 TAILQ_INSERT_BEFORE(p, c, link);
2105 TAILQ_INSERT_TAIL(&community_list, c, link);
2110 comm_define(u_int priv, const char *descr, struct lmodule *owner,
2113 struct asn_oid idx, *p;
2114 struct community *c;
2117 /* generate an identifier */
2119 if ((ncomm = next_community_index++) == UINT_MAX)
2120 next_community_index = 1;
2121 TAILQ_FOREACH(c, &community_list, link)
2122 if (c->value == ncomm)
2124 } while (c != NULL);
2134 p->subs[p->len++] = ncomm;
2135 c = comm_define_ordered(priv, descr, p, owner, str);
2142 comm_string(u_int ncomm)
2144 struct community *p;
2146 TAILQ_FOREACH(p, &community_list, link)
2147 if (p->value == ncomm)
2153 * Delete all communities allocated by a module
2156 comm_flush(struct lmodule *mod)
2158 struct community *p, *p1;
2160 p = TAILQ_FIRST(&community_list);
2162 p1 = TAILQ_NEXT(p, link);
2163 if (p->owner == mod) {
2165 TAILQ_REMOVE(&community_list, p, link);
2173 * Request ID handling.
2175 * Allocate a new range of request ids. Use a first fit algorithm.
2178 reqid_allocate(int size, struct lmodule *mod)
2181 struct idrange *r, *r1;
2183 if (size <= 0 || size > INT32_MAX) {
2184 syslog(LOG_CRIT, "%s: size out of range: %d", __func__, size);
2187 /* allocate a type id */
2189 if ((type = next_idrange++) == UINT_MAX)
2191 TAILQ_FOREACH(r, &idrange_list, link)
2192 if (r->type == type)
2197 if (TAILQ_EMPTY(&idrange_list))
2200 r = TAILQ_FIRST(&idrange_list);
2201 if (r->base < size) {
2202 while((r1 = TAILQ_NEXT(r, link)) != NULL) {
2203 if (r1->base - (r->base + r->size) >= size)
2210 r1 = TAILQ_LAST(&idrange_list, idrange_list);
2211 if (INT32_MAX - size + 1 < r1->base + r1->size) {
2212 syslog(LOG_ERR, "out of id ranges (%u)", size);
2218 /* allocate structure */
2219 if ((r1 = malloc(sizeof(struct idrange))) == NULL) {
2220 syslog(LOG_ERR, "%s: %m", __FUNCTION__);
2227 if (TAILQ_EMPTY(&idrange_list) || r == TAILQ_FIRST(&idrange_list)) {
2229 TAILQ_INSERT_HEAD(&idrange_list, r1, link);
2230 } else if (r == NULL) {
2231 r = TAILQ_LAST(&idrange_list, idrange_list);
2232 r1->base = r->base + r->size;
2233 TAILQ_INSERT_TAIL(&idrange_list, r1, link);
2235 r = TAILQ_PREV(r, idrange_list, link);
2236 r1->base = r->base + r->size;
2237 TAILQ_INSERT_AFTER(&idrange_list, r, r1, link);
2239 r1->next = r1->base;
2245 reqid_next(u_int type)
2250 TAILQ_FOREACH(r, &idrange_list, link)
2251 if (r->type == type)
2254 syslog(LOG_CRIT, "wrong idrange type");
2257 if ((id = r->next++) == r->base + (r->size - 1))
2263 reqid_base(u_int type)
2267 TAILQ_FOREACH(r, &idrange_list, link)
2268 if (r->type == type)
2270 syslog(LOG_CRIT, "wrong idrange type");
2275 reqid_type(int32_t reqid)
2279 TAILQ_FOREACH(r, &idrange_list, link)
2280 if (reqid >= r->base && reqid <= r->base + (r->size - 1))
2286 reqid_istype(int32_t reqid, u_int type)
2288 return (reqid_type(reqid) == type);
2292 * Delete all communities allocated by a module
2295 reqid_flush(struct lmodule *mod)
2297 struct idrange *p, *p1;
2299 p = TAILQ_FIRST(&idrange_list);
2301 p1 = TAILQ_NEXT(p, link);
2302 if (p->owner == mod) {
2303 TAILQ_REMOVE(&idrange_list, p, link);
2311 * Merge the given tree for the given module into the main tree.
2314 compare_node(const void *v1, const void *v2)
2316 const struct snmp_node *n1 = v1;
2317 const struct snmp_node *n2 = v2;
2319 return (asn_compare_oid(&n1->oid, &n2->oid));
2322 tree_merge(const struct snmp_node *ntree, u_int nsize, struct lmodule *mod)
2324 struct snmp_node *xtree;
2327 xtree = realloc(tree, sizeof(*tree) * (tree_size + nsize));
2328 if (xtree == NULL) {
2329 syslog(LOG_ERR, "tree_merge: %m");
2333 memcpy(&tree[tree_size], ntree, sizeof(*tree) * nsize);
2335 for (i = 0; i < nsize; i++)
2336 tree[tree_size + i].tree_data = mod;
2340 qsort(tree, tree_size, sizeof(tree[0]), compare_node);
2346 * Remove all nodes belonging to the loadable module
2349 tree_unmerge(struct lmodule *mod)
2353 for(s = d = 0; s < tree_size; s++)
2354 if (tree[s].tree_data != mod) {
2366 lm_load(const char *path, const char *section)
2371 char *av[MAX_MOD_ARGS + 1];
2375 if ((m = malloc(sizeof(*m))) == NULL) {
2376 syslog(LOG_ERR, "lm_load: %m");
2381 strlcpy(m->section, section, sizeof(m->section));
2383 if ((m->path = strdup(path)) == NULL) {
2384 syslog(LOG_ERR, "lm_load: %m");
2391 m->index.subs[0] = strlen(section);
2392 m->index.len = m->index.subs[0] + 1;
2393 for (u = 0; u < m->index.subs[0]; u++)
2394 m->index.subs[u + 1] = section[u];
2397 * Load the object file and locate the config structure
2399 if ((m->handle = dlopen(m->path, RTLD_NOW|RTLD_GLOBAL)) == NULL) {
2400 syslog(LOG_ERR, "lm_load: open %s", dlerror());
2404 if ((m->config = dlsym(m->handle, "config")) == NULL) {
2405 syslog(LOG_ERR, "lm_load: no 'config' symbol %s", dlerror());
2410 * Insert it into the right place
2412 INSERT_OBJECT_OID(m, &lmodules);
2414 /* preserve order */
2415 if (community == COMM_INITIALIZE) {
2416 m->flags |= LM_ONSTARTLIST;
2417 TAILQ_INSERT_TAIL(&modules_start, m, start);
2421 * make the argument vector.
2424 for (i = 0; i < nprogargs; i++) {
2425 if (strlen(progargs[i]) >= strlen(section) + 1 &&
2426 strncmp(progargs[i], section, strlen(section)) == 0 &&
2427 progargs[i][strlen(section)] == ':') {
2428 if (ac == MAX_MOD_ARGS) {
2429 syslog(LOG_WARNING, "too many arguments for "
2430 "module '%s", section);
2433 av[ac++] = &progargs[i][strlen(section)+1];
2439 * Run the initialization function
2441 if ((err = (*m->config->init)(m, ac, av)) != 0) {
2442 syslog(LOG_ERR, "lm_load: init failed: %d", err);
2443 TAILQ_REMOVE(&lmodules, m, link);
2450 if ((m->flags & LM_ONSTARTLIST) != 0)
2451 TAILQ_REMOVE(&modules_start, m, start);
2463 lm_start(struct lmodule *mod)
2465 const struct lmodule *m;
2468 * Merge tree. If this fails, unload the module.
2470 if (tree_merge(mod->config->tree, mod->config->tree_size, mod)) {
2476 * Read configuration
2478 if (read_config(config_file, mod)) {
2479 syslog(LOG_ERR, "error in config file");
2483 if (mod->config->start)
2484 (*mod->config->start)();
2486 mod->flags |= LM_STARTED;
2489 * Inform other modules
2491 TAILQ_FOREACH(m, &lmodules, link)
2492 if (m->config->loading)
2493 (*m->config->loading)(mod, 1);
2501 lm_unload(struct lmodule *m)
2504 const struct lmodule *mod;
2506 TAILQ_REMOVE(&lmodules, m, link);
2507 if (m->flags & LM_ONSTARTLIST)
2508 TAILQ_REMOVE(&modules_start, m, start);
2511 if ((m->flags & LM_STARTED) && m->config->fini &&
2512 (err = (*m->config->fini)()) != 0)
2513 syslog(LOG_WARNING, "lm_unload(%s): fini %d", m->section, err);
2524 * Inform other modules
2526 TAILQ_FOREACH(mod, &lmodules, link)
2527 if (mod->config->loading)
2528 (*mod->config->loading)(m, 0);
2534 * Register an object resource and return the index (or 0 on failures)
2537 or_register(const struct asn_oid *or, const char *descr, struct lmodule *mod)
2539 struct objres *objres, *or1;
2542 /* find a free index */
2544 for (objres = TAILQ_FIRST(&objres_list);
2546 objres = TAILQ_NEXT(objres, link)) {
2547 if ((or1 = TAILQ_NEXT(objres, link)) == NULL ||
2548 or1->index > objres->index + 1) {
2549 idx = objres->index + 1;
2554 if ((objres = malloc(sizeof(*objres))) == NULL)
2557 objres->index = idx;
2559 strlcpy(objres->descr, descr, sizeof(objres->descr));
2560 objres->uptime = (uint32_t)(get_ticks() - start_tick);
2561 objres->module = mod;
2563 INSERT_OBJECT_INT(objres, &objres_list);
2565 systemg.or_last_change = objres->uptime;
2571 or_unregister(u_int idx)
2573 struct objres *objres;
2575 TAILQ_FOREACH(objres, &objres_list, link)
2576 if (objres->index == idx) {
2577 TAILQ_REMOVE(&objres_list, objres, link);
2584 * RFC 3414 User-based Security Model support
2587 struct snmpd_usmstat *
2588 bsnmpd_get_usm_stats(void)
2590 return (&snmpd_usmstats);
2594 bsnmpd_reset_usm_stats(void)
2596 memset(&snmpd_usmstats, 0, sizeof(snmpd_usmstats));
2600 usm_first_user(void)
2602 return (SLIST_FIRST(&usm_userlist));
2606 usm_next_user(struct usm_user *uuser)
2611 return (SLIST_NEXT(uuser, up));
2615 usm_find_user(uint8_t *engine, uint32_t elen, char *uname)
2617 struct usm_user *uuser;
2619 SLIST_FOREACH(uuser, &usm_userlist, up)
2620 if (uuser->user_engine_len == elen &&
2621 memcmp(uuser->user_engine_id, engine, elen) == 0 &&
2622 strlen(uuser->suser.sec_name) == strlen(uname) &&
2623 strcmp(uuser->suser.sec_name, uname) == 0)
2630 usm_compare_user(struct usm_user *u1, struct usm_user *u2)
2634 if (u1->user_engine_len < u2->user_engine_len)
2636 if (u1->user_engine_len > u2->user_engine_len)
2639 for (i = 0; i < u1->user_engine_len; i++) {
2640 if (u1->user_engine_id[i] < u2->user_engine_id[i])
2642 if (u1->user_engine_id[i] > u2->user_engine_id[i])
2646 if (strlen(u1->suser.sec_name) < strlen(u2->suser.sec_name))
2648 if (strlen(u1->suser.sec_name) > strlen(u2->suser.sec_name))
2651 for (i = 0; i < strlen(u1->suser.sec_name); i++) {
2652 if (u1->suser.sec_name[i] < u2->suser.sec_name[i])
2654 if (u1->suser.sec_name[i] > u2->suser.sec_name[i])
2662 usm_new_user(uint8_t *eid, uint32_t elen, char *uname)
2665 struct usm_user *uuser, *temp, *prev;
2667 for (uuser = usm_first_user(); uuser != NULL;
2668 (uuser = usm_next_user(uuser))) {
2669 if (uuser->user_engine_len == elen &&
2670 strlen(uname) == strlen(uuser->suser.sec_name) &&
2671 strcmp(uname, uuser->suser.sec_name) == 0 &&
2672 memcmp(eid, uuser->user_engine_id, elen) == 0)
2676 if ((uuser = (struct usm_user *)malloc(sizeof(*uuser))) == NULL)
2679 memset(uuser, 0, sizeof(*uuser));
2680 strlcpy(uuser->suser.sec_name, uname, SNMP_ADM_STR32_SIZ);
2681 memcpy(uuser->user_engine_id, eid, elen);
2682 uuser->user_engine_len = elen;
2684 if ((prev = SLIST_FIRST(&usm_userlist)) == NULL ||
2685 usm_compare_user(uuser, prev) < 0) {
2686 SLIST_INSERT_HEAD(&usm_userlist, uuser, up);
2690 SLIST_FOREACH(temp, &usm_userlist, up) {
2691 if ((cmp = usm_compare_user(uuser, temp)) <= 0)
2696 if (temp == NULL || cmp < 0)
2697 SLIST_INSERT_AFTER(prev, uuser, up);
2699 SLIST_INSERT_AFTER(temp, uuser, up);
2701 syslog(LOG_ERR, "User %s exists", uuser->suser.sec_name);
2710 usm_delete_user(struct usm_user *uuser)
2712 SLIST_REMOVE(&usm_userlist, uuser, usm_user, up);
2717 usm_flush_users(void)
2719 struct usm_user *uuser;
2721 while ((uuser = SLIST_FIRST(&usm_userlist)) != NULL) {
2722 SLIST_REMOVE_HEAD(&usm_userlist, up);
2726 SLIST_INIT(&usm_userlist);
2730 * RFC 3415 View-based Access Control Model support
2733 vacm_first_user(void)
2735 return (SLIST_FIRST(&vacm_userlist));
2739 vacm_next_user(struct vacm_user *vuser)
2744 return (SLIST_NEXT(vuser, vvu));
2748 vacm_compare_user(struct vacm_user *v1, struct vacm_user *v2)
2752 if (v1->sec_model < v2->sec_model)
2754 if (v1->sec_model > v2->sec_model)
2757 if (strlen(v1->secname) < strlen(v2->secname))
2759 if (strlen(v1->secname) > strlen(v2->secname))
2762 for (i = 0; i < strlen(v1->secname); i++) {
2763 if (v1->secname[i] < v2->secname[i])
2765 if (v1->secname[i] > v2->secname[i])
2773 vacm_new_user(int32_t smodel, char *uname)
2776 struct vacm_user *user, *temp, *prev;
2778 SLIST_FOREACH(user, &vacm_userlist, vvu)
2779 if (strcmp(uname, user->secname) == 0 &&
2780 smodel == user->sec_model)
2783 if ((user = (struct vacm_user *)malloc(sizeof(*user))) == NULL)
2786 memset(user, 0, sizeof(*user));
2787 user->group = &vacm_default_group;
2788 SLIST_INSERT_HEAD(&vacm_default_group.group_users, user, vvg);
2789 user->sec_model = smodel;
2790 strlcpy(user->secname, uname, sizeof(user->secname));
2792 if ((prev = SLIST_FIRST(&vacm_userlist)) == NULL ||
2793 vacm_compare_user(user, prev) < 0) {
2794 SLIST_INSERT_HEAD(&vacm_userlist, user, vvu);
2798 SLIST_FOREACH(temp, &vacm_userlist, vvu) {
2799 if ((cmp = vacm_compare_user(user, temp)) <= 0)
2804 if (temp == NULL || cmp < 0)
2805 SLIST_INSERT_AFTER(prev, user, vvu);
2807 SLIST_INSERT_AFTER(temp, user, vvu);
2809 syslog(LOG_ERR, "User %s exists", user->secname);
2818 vacm_delete_user(struct vacm_user *user)
2820 if (user->group != NULL && user->group != &vacm_default_group) {
2821 SLIST_REMOVE(&user->group->group_users, user, vacm_user, vvg);
2822 if (SLIST_EMPTY(&user->group->group_users)) {
2823 SLIST_REMOVE(&vacm_grouplist, user->group,
2829 SLIST_REMOVE(&vacm_userlist, user, vacm_user, vvu);
2836 vacm_user_set_group(struct vacm_user *user, u_char *octets, u_int len)
2838 struct vacm_group *group;
2840 if (len >= SNMP_ADM_STR32_SIZ)
2843 SLIST_FOREACH(group, &vacm_grouplist, vge)
2844 if (strlen(group->groupname) == len &&
2845 memcmp(octets, group->groupname, len) == 0)
2848 if (group == NULL) {
2849 if ((group = (struct vacm_group *)malloc(sizeof(*group))) == NULL)
2851 memset(group, 0, sizeof(*group));
2852 memcpy(group->groupname, octets, len);
2853 group->groupname[len] = '\0';
2854 SLIST_INSERT_HEAD(&vacm_grouplist, group, vge);
2857 SLIST_REMOVE(&user->group->group_users, user, vacm_user, vvg);
2858 SLIST_INSERT_HEAD(&group->group_users, user, vvg);
2859 user->group = group;
2865 vacm_groups_init(void)
2867 SLIST_INSERT_HEAD(&vacm_grouplist, &vacm_default_group, vge);
2870 struct vacm_access *
2871 vacm_first_access_rule(void)
2873 return (TAILQ_FIRST(&vacm_accesslist));
2876 struct vacm_access *
2877 vacm_next_access_rule(struct vacm_access *acl)
2882 return (TAILQ_NEXT(acl, vva));
2886 vacm_compare_access_rule(struct vacm_access *v1, struct vacm_access *v2)
2890 if (strlen(v1->group->groupname) < strlen(v2->group->groupname))
2892 if (strlen(v1->group->groupname) > strlen(v2->group->groupname))
2895 for (i = 0; i < strlen(v1->group->groupname); i++) {
2896 if (v1->group->groupname[i] < v2->group->groupname[i])
2898 if (v1->group->groupname[i] > v2->group->groupname[i])
2902 if (strlen(v1->ctx_prefix) < strlen(v2->ctx_prefix))
2904 if (strlen(v1->ctx_prefix) > strlen(v2->ctx_prefix))
2907 for (i = 0; i < strlen(v1->ctx_prefix); i++) {
2908 if (v1->ctx_prefix[i] < v2->ctx_prefix[i])
2910 if (v1->ctx_prefix[i] > v2->ctx_prefix[i])
2914 if (v1->sec_model < v2->sec_model)
2916 if (v1->sec_model > v2->sec_model)
2919 if (v1->sec_level < v2->sec_level)
2921 if (v1->sec_level > v2->sec_level)
2927 struct vacm_access *
2928 vacm_new_access_rule(char *gname, char *cprefix, int32_t smodel, int32_t slevel)
2930 struct vacm_group *group;
2931 struct vacm_access *acl, *temp;
2933 TAILQ_FOREACH(acl, &vacm_accesslist, vva) {
2934 if (acl->group == NULL)
2936 if (strcmp(gname, acl->group->groupname) == 0 &&
2937 strcmp(cprefix, acl->ctx_prefix) == 0 &&
2938 acl->sec_model == smodel && acl->sec_level == slevel)
2942 /* Make sure the group exists */
2943 SLIST_FOREACH(group, &vacm_grouplist, vge)
2944 if (strcmp(gname, group->groupname) == 0)
2950 if ((acl = (struct vacm_access *)malloc(sizeof(*acl))) == NULL)
2953 memset(acl, 0, sizeof(*acl));
2955 strlcpy(acl->ctx_prefix, cprefix, sizeof(acl->ctx_prefix));
2956 acl->sec_model = smodel;
2957 acl->sec_level = slevel;
2959 if ((temp = TAILQ_FIRST(&vacm_accesslist)) == NULL ||
2960 vacm_compare_access_rule(acl, temp) < 0) {
2961 TAILQ_INSERT_HEAD(&vacm_accesslist, acl, vva);
2965 TAILQ_FOREACH(temp, &vacm_accesslist, vva)
2966 if (vacm_compare_access_rule(acl, temp) < 0) {
2967 TAILQ_INSERT_BEFORE(temp, acl, vva);
2971 TAILQ_INSERT_TAIL(&vacm_accesslist, acl, vva);
2977 vacm_delete_access_rule(struct vacm_access *acl)
2979 TAILQ_REMOVE(&vacm_accesslist, acl, vva);
2986 vacm_first_view(void)
2988 return (SLIST_FIRST(&vacm_viewlist));
2992 vacm_next_view(struct vacm_view *view)
2997 return (SLIST_NEXT(view, vvl));
3001 vacm_compare_view(struct vacm_view *v1, struct vacm_view *v2)
3005 if (strlen(v1->viewname) < strlen(v2->viewname))
3007 if (strlen(v1->viewname) > strlen(v2->viewname))
3010 for (i = 0; i < strlen(v1->viewname); i++) {
3011 if (v1->viewname[i] < v2->viewname[i])
3013 if (v1->viewname[i] > v2->viewname[i])
3017 return (asn_compare_oid(&v1->subtree, &v2->subtree));
3021 vacm_new_view(char *vname, struct asn_oid *oid)
3024 struct vacm_view *view, *temp, *prev;
3026 SLIST_FOREACH(view, &vacm_viewlist, vvl)
3027 if (strcmp(vname, view->viewname) == 0)
3030 if ((view = (struct vacm_view *)malloc(sizeof(*view))) == NULL)
3033 memset(view, 0, sizeof(*view));
3034 strlcpy(view->viewname, vname, sizeof(view->viewname));
3035 asn_append_oid(&view->subtree, oid);
3037 if ((prev = SLIST_FIRST(&vacm_viewlist)) == NULL ||
3038 vacm_compare_view(view, prev) < 0) {
3039 SLIST_INSERT_HEAD(&vacm_viewlist, view, vvl);
3043 SLIST_FOREACH(temp, &vacm_viewlist, vvl) {
3044 if ((cmp = vacm_compare_view(view, temp)) <= 0)
3049 if (temp == NULL || cmp < 0)
3050 SLIST_INSERT_AFTER(prev, view, vvl);
3052 SLIST_INSERT_AFTER(temp, view, vvl);
3054 syslog(LOG_ERR, "View %s exists", view->viewname);
3063 vacm_delete_view(struct vacm_view *view)
3065 SLIST_REMOVE(&vacm_viewlist, view, vacm_view, vvl);
3071 struct vacm_context *
3072 vacm_first_context(void)
3074 return (SLIST_FIRST(&vacm_contextlist));
3077 struct vacm_context *
3078 vacm_next_context(struct vacm_context *vacmctx)
3080 if (vacmctx == NULL)
3083 return (SLIST_NEXT(vacmctx, vcl));
3086 struct vacm_context *
3087 vacm_add_context(char *ctxname, int regid)
3090 struct vacm_context *ctx, *temp, *prev;
3092 SLIST_FOREACH(ctx, &vacm_contextlist, vcl)
3093 if (strcmp(ctxname, ctx->ctxname) == 0) {
3094 syslog(LOG_ERR, "Context %s exists", ctx->ctxname);
3098 if ((ctx = (struct vacm_context *)malloc(sizeof(*ctx))) == NULL)
3101 memset(ctx, 0, sizeof(*ctx));
3102 strlcpy(ctx->ctxname, ctxname, sizeof(ctx->ctxname));
3105 if ((prev = SLIST_FIRST(&vacm_contextlist)) == NULL ||
3106 strlen(ctx->ctxname) < strlen(prev->ctxname) ||
3107 strcmp(ctx->ctxname, prev->ctxname) < 0) {
3108 SLIST_INSERT_HEAD(&vacm_contextlist, ctx, vcl);
3112 SLIST_FOREACH(temp, &vacm_contextlist, vcl) {
3113 if (strlen(ctx->ctxname) < strlen(temp->ctxname) ||
3114 strcmp(ctx->ctxname, temp->ctxname) < 0) {
3121 if (temp == NULL || cmp < 0)
3122 SLIST_INSERT_AFTER(prev, ctx, vcl);
3124 SLIST_INSERT_AFTER(temp, ctx, vcl);
3126 syslog(LOG_ERR, "Context %s exists", ctx->ctxname);
3135 vacm_flush_contexts(int regid)
3137 struct vacm_context *ctx, *temp;
3139 SLIST_FOREACH_SAFE(ctx, &vacm_contextlist, vcl, temp)
3140 if (ctx->regid == regid) {
3141 SLIST_REMOVE(&vacm_contextlist, ctx, vacm_context, vcl);