2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: bsnmp/snmpd/main.c,v 1.100 2006/02/14 09:04:20 brandt_h Exp $
33 #include <sys/param.h>
35 #include <sys/ucred.h>
50 #ifdef USE_TCPWRAPPERS
51 #include <arpa/inet.h>
61 #define PATH_PID "/var/run/%s.pid"
62 #define PATH_CONFIG "/etc/%s.config"
64 uint64_t this_tick; /* start of processing of current packet (absolute) */
65 uint64_t start_tick; /* start of processing */
67 struct systemg systemg = {
69 { 8, { 1, 3, 6, 1, 4, 1, 1115, 7352 }},
74 struct debug debug = {
76 LOG_DEBUG, /* log_pri */
80 struct snmpd snmpd = {
85 {0, 0, 0, 0}, /* trap1addr */
86 VERS_ENABLE_ALL,/* version_enable */
88 struct snmpd_stats snmpd_stats;
91 int32_t snmp_serial_no;
93 /* search path for config files */
94 const char *syspath = PATH_SYSCONFIG;
96 /* list of all loaded modules */
97 struct lmodules lmodules = TAILQ_HEAD_INITIALIZER(lmodules);
99 /* list of loaded modules during start-up in the order they were loaded */
100 static struct lmodules modules_start = TAILQ_HEAD_INITIALIZER(modules_start);
102 /* list of all known communities */
103 struct community_list community_list = TAILQ_HEAD_INITIALIZER(community_list);
105 /* list of all installed object resources */
106 struct objres_list objres_list = TAILQ_HEAD_INITIALIZER(objres_list);
108 /* community value generator */
109 static u_int next_community_index = 1;
111 /* list of all known ranges */
112 struct idrange_list idrange_list = TAILQ_HEAD_INITIALIZER(idrange_list);
114 /* identifier generator */
115 u_int next_idrange = 1;
117 /* list of all current timers */
118 struct timer_list timer_list = LIST_HEAD_INITIALIZER(timer_list);
120 /* list of file descriptors */
121 struct fdesc_list fdesc_list = LIST_HEAD_INITIALIZER(fdesc_list);
123 /* program arguments */
124 static char **progargs;
125 static int nprogargs;
127 /* current community */
129 static struct community *comm;
132 static char config_file[MAXPATHLEN + 1];
133 static char pid_file[MAXPATHLEN + 1];
135 #ifndef USE_LIBBEGEMOT
137 static evContext evctx;
141 static sigset_t blocked_sigs;
143 /* signal handling */
145 #define WORK_DOINFO 0x0001
146 #define WORK_RECONFIG 0x0002
149 static const struct asn_oid
150 oid_snmpMIB = OIDX_snmpMIB,
151 oid_begemotSnmpd = OIDX_begemotSnmpd,
152 oid_coldStart = OIDX_coldStart,
153 oid_authenticationFailure = OIDX_authenticationFailure;
155 const struct asn_oid oid_zeroDotZero = { 2, { 0, 0 }};
157 /* request id generator for traps */
161 static const char usgtxt[] = "\
162 Begemot simple SNMP daemon. Copyright (c) 2001-2002 Fraunhofer Institute for\n\
163 Open Communication Systems (FhG Fokus). All rights reserved.\n\
164 usage: snmpd [-dh] [-c file] [-D options] [-I path] [-l prefix]\n\
165 [-m variable=value] [-p file]\n\
167 -d don't daemonize\n\
168 -h print this info\n\
169 -c file specify configuration file\n\
170 -D options debugging options\n\
171 -I path system include path\n\
172 -l prefix default basename for pid and config file\n\
173 -m var=val define variable\n\
174 -p file specify pid file\n\
177 /* hosts_access(3) request */
178 #ifdef USE_TCPWRAPPERS
179 static struct request_info req;
183 extern const struct transport_def udp_trans;
184 extern const struct transport_def lsock_trans;
186 struct transport_list transport_list = TAILQ_HEAD_INITIALIZER(transport_list);
188 /* forward declarations */
189 static void snmp_printf_func(const char *fmt, ...);
190 static void snmp_error_func(const char *err, ...);
191 static void snmp_debug_func(const char *err, ...);
192 static void asn_error_func(const struct asn_buf *b, const char *err, ...);
195 * Allocate rx/tx buffer. We allocate one byte more for rx.
202 if ((buf = malloc(tx ? snmpd.txbuf : snmpd.rxbuf)) == NULL) {
203 syslog(LOG_CRIT, "cannot allocate buffer");
205 snmpd_stats.noTxbuf++;
207 snmpd_stats.noRxbuf++;
214 * Return the buffer size.
219 return (tx ? snmpd.txbuf : snmpd.rxbuf);
223 * Prepare a PDU for output
226 snmp_output(struct snmp_pdu *pdu, u_char *sndbuf, size_t *sndlen,
229 struct asn_buf resp_b;
231 resp_b.asn_ptr = sndbuf;
232 resp_b.asn_len = snmpd.txbuf;
234 if (snmp_pdu_encode(pdu, &resp_b) != 0) {
235 syslog(LOG_ERR, "cannot encode message");
238 if (debug.dump_pdus) {
239 snmp_printf("%s <- ", dest);
242 *sndlen = (size_t)(resp_b.asn_ptr - sndbuf);
246 * SNMP input. Start: decode the PDU, find the community.
249 snmp_input_start(const u_char *buf, size_t len, const char *source,
250 struct snmp_pdu *pdu, int32_t *ip, size_t *pdulen)
254 enum snmpd_input_err ret;
260 /* look whether we have enough bytes for the entire PDU. */
261 switch (sret = snmp_pdu_snoop(&b)) {
264 return (SNMPD_INPUT_TRUNC);
267 snmpd_stats.inASNParseErrs++;
268 return (SNMPD_INPUT_FAILED);
270 b.asn_len = *pdulen = (size_t)sret;
272 code = snmp_pdu_decode(&b, pdu, ip);
274 snmpd_stats.inPkts++;
276 ret = SNMPD_INPUT_OK;
279 case SNMP_CODE_FAILED:
280 snmpd_stats.inASNParseErrs++;
281 return (SNMPD_INPUT_FAILED);
283 case SNMP_CODE_BADVERS:
285 snmpd_stats.inBadVersions++;
286 return (SNMPD_INPUT_FAILED);
288 case SNMP_CODE_BADLEN:
289 if (pdu->type == SNMP_OP_SET)
290 ret = SNMPD_INPUT_VALBADLEN;
293 case SNMP_CODE_OORANGE:
294 if (pdu->type == SNMP_OP_SET)
295 ret = SNMPD_INPUT_VALRANGE;
298 case SNMP_CODE_BADENC:
299 if (pdu->type == SNMP_OP_SET)
300 ret = SNMPD_INPUT_VALBADENC;
304 switch (pdu->version) {
307 if (!(snmpd.version_enable & VERS_ENABLE_V1))
312 if (!(snmpd.version_enable & VERS_ENABLE_V2C))
322 if (debug.dump_pdus) {
323 snmp_printf("%s -> ", source);
328 * Look, whether we know the community
330 TAILQ_FOREACH(comm, &community_list, link)
331 if (comm->string != NULL &&
332 strcmp(comm->string, pdu->community) == 0)
336 snmpd_stats.inBadCommunityNames++;
338 if (snmpd.auth_traps)
339 snmp_send_trap(&oid_authenticationFailure,
340 (struct snmp_value *)NULL);
341 ret = SNMPD_INPUT_BAD_COMM;
343 community = comm->value;
346 this_tick = get_ticks();
352 * Will return only _OK or _FAILED
355 snmp_input_finish(struct snmp_pdu *pdu, const u_char *rcvbuf, size_t rcvlen,
356 u_char *sndbuf, size_t *sndlen, const char *source,
357 enum snmpd_input_err ierr, int32_t ivar, void *data)
359 struct snmp_pdu resp;
360 struct asn_buf resp_b, pdu_b;
363 resp_b.asn_ptr = sndbuf;
364 resp_b.asn_len = snmpd.txbuf;
366 pdu_b.asn_cptr = rcvbuf;
367 pdu_b.asn_len = rcvlen;
369 if (ierr != SNMPD_INPUT_OK) {
370 /* error decoding the input of a SET */
371 if (pdu->version == SNMP_V1)
372 pdu->error_status = SNMP_ERR_BADVALUE;
373 else if (ierr == SNMPD_INPUT_VALBADLEN)
374 pdu->error_status = SNMP_ERR_WRONG_LENGTH;
375 else if (ierr == SNMPD_INPUT_VALRANGE)
376 pdu->error_status = SNMP_ERR_WRONG_VALUE;
378 pdu->error_status = SNMP_ERR_WRONG_ENCODING;
380 pdu->error_index = ivar;
382 if (snmp_make_errresp(pdu, &pdu_b, &resp_b) == SNMP_RET_IGN) {
383 syslog(LOG_WARNING, "could not encode error response");
384 snmpd_stats.silentDrops++;
385 return (SNMPD_INPUT_FAILED);
388 if (debug.dump_pdus) {
389 snmp_printf("%s <- ", source);
392 *sndlen = (size_t)(resp_b.asn_ptr - sndbuf);
393 return (SNMPD_INPUT_OK);
399 ret = snmp_get(pdu, &resp_b, &resp, data);
402 case SNMP_PDU_GETNEXT:
403 ret = snmp_getnext(pdu, &resp_b, &resp, data);
407 ret = snmp_set(pdu, &resp_b, &resp, data);
410 case SNMP_PDU_GETBULK:
411 ret = snmp_getbulk(pdu, &resp_b, &resp, data);
422 /* normal return - send a response */
423 if (debug.dump_pdus) {
424 snmp_printf("%s <- ", source);
425 snmp_pdu_dump(&resp);
427 *sndlen = (size_t)(resp_b.asn_ptr - sndbuf);
428 snmp_pdu_free(&resp);
429 return (SNMPD_INPUT_OK);
432 /* error - send nothing */
433 snmpd_stats.silentDrops++;
434 return (SNMPD_INPUT_FAILED);
437 /* error - send error response. The snmp routine has
438 * changed the error fields in the original message. */
439 resp_b.asn_ptr = sndbuf;
440 resp_b.asn_len = snmpd.txbuf;
441 if (snmp_make_errresp(pdu, &pdu_b, &resp_b) == SNMP_RET_IGN) {
442 syslog(LOG_WARNING, "could not encode error response");
443 snmpd_stats.silentDrops++;
444 return (SNMPD_INPUT_FAILED);
446 if (debug.dump_pdus) {
447 snmp_printf("%s <- ", source);
450 *sndlen = (size_t)(resp_b.asn_ptr - sndbuf);
451 return (SNMPD_INPUT_OK);
458 * Insert a port into the right place in the transport's table of ports
461 trans_insert_port(struct transport *t, struct tport *port)
465 TAILQ_FOREACH(p, &t->table, link) {
466 if (asn_compare_oid(&p->index, &port->index) > 0) {
467 TAILQ_INSERT_BEFORE(p, port, link);
472 TAILQ_INSERT_TAIL(&t->table, port, link);
476 * Remove a port from a transport's list
479 trans_remove_port(struct tport *port)
482 TAILQ_REMOVE(&port->transport->table, port, link);
486 * Find a port on a transport's list
489 trans_find_port(struct transport *t, const struct asn_oid *idx, u_int sub)
492 return (FIND_OBJECT_OID(&t->table, idx, sub));
496 * Find next port on a transport's list
499 trans_next_port(struct transport *t, const struct asn_oid *idx, u_int sub)
502 return (NEXT_OBJECT_OID(&t->table, idx, sub));
509 trans_first_port(struct transport *t)
512 return (TAILQ_FIRST(&t->table));
516 * Iterate through all ports until a function returns a 0.
519 trans_iter_port(struct transport *t, int (*func)(struct tport *, intptr_t),
524 TAILQ_FOREACH(p, &t->table, link)
525 if (func(p, arg) == 0)
531 * Register a transport
534 trans_register(const struct transport_def *def, struct transport **pp)
539 if ((*pp = malloc(sizeof(**pp))) == NULL)
540 return (SNMP_ERR_GENERR);
542 /* construct index */
543 (*pp)->index.len = strlen(def->name) + 1;
544 (*pp)->index.subs[0] = strlen(def->name);
545 for (i = 0; i < (*pp)->index.subs[0]; i++)
546 (*pp)->index.subs[i + 1] = def->name[i];
550 if (FIND_OBJECT_OID(&transport_list, &(*pp)->index, 0) != NULL) {
552 return (SNMP_ERR_INCONS_VALUE);
555 /* register module */
556 snprintf(or_descr, sizeof(or_descr), "%s transport mapping", def->name);
557 if (((*pp)->or_index = or_register(&def->id, or_descr, NULL)) == 0) {
559 return (SNMP_ERR_GENERR);
562 INSERT_OBJECT_OID((*pp), &transport_list);
564 TAILQ_INIT(&(*pp)->table);
566 return (SNMP_ERR_NOERROR);
570 * Unregister transport
573 trans_unregister(struct transport *t)
575 if (!TAILQ_EMPTY(&t->table))
576 return (SNMP_ERR_INCONS_VALUE);
578 or_unregister(t->or_index);
579 TAILQ_REMOVE(&transport_list, t, link);
581 return (SNMP_ERR_NOERROR);
585 * File descriptor support
587 #ifdef USE_LIBBEGEMOT
589 input(int fd, int mask __unused, void *uap)
592 input(evContext ctx __unused, void *uap, int fd, int mask __unused)
595 struct fdesc *f = uap;
597 (*f->func)(fd, f->udata);
605 #ifdef USE_LIBBEGEMOT
607 poll_unregister(f->id);
611 if (evTestID(f->id)) {
612 (void)evDeselectFD(evctx, f->id);
624 #ifdef USE_LIBBEGEMOT
627 if ((f->id = poll_register(f->fd, input, f, POLL_IN)) < 0) {
629 syslog(LOG_ERR, "select fd %d: %m", f->fd);
636 if (evSelectFD(evctx, f->fd, EV_READ, input, f, &f->id)) {
638 syslog(LOG_ERR, "select fd %d: %m", f->fd);
647 fd_select(int fd, void (*func)(int, void *), void *udata, struct lmodule *mod)
652 if ((f = malloc(sizeof(struct fdesc))) == NULL) {
654 syslog(LOG_ERR, "fd_select: %m");
662 #ifdef USE_LIBBEGEMOT
675 LIST_INSERT_HEAD(&fdesc_list, f, link);
685 LIST_REMOVE(f, link);
691 fd_flush(struct lmodule *mod)
693 struct fdesc *t, *t1;
695 t = LIST_FIRST(&fdesc_list);
697 t1 = LIST_NEXT(t, link);
705 * Consume a message from the input buffer
708 snmp_input_consume(struct port_input *pi)
711 /* always consume everything */
715 if (pi->consumed >= pi->length) {
716 /* all bytes consumed */
720 memmove(pi->buf, pi->buf + pi->consumed, pi->length - pi->consumed);
721 pi->length -= pi->consumed;
726 struct cmsgcred cred;
730 check_priv(struct port_input *pi, struct msghdr *msg)
732 struct credmsg *cmsg;
738 if (msg->msg_controllen == sizeof(*cmsg)) {
739 /* process explicitly sends credentials */
741 cmsg = (struct credmsg *)msg->msg_control;
742 pi->priv = (cmsg->cred.cmcred_euid == 0);
746 /* ok, obtain the accept time credentials */
747 ucredlen = sizeof(ucred);
749 if (getsockopt(pi->fd, 0, LOCAL_PEERCRED, &ucred, &ucredlen) == 0 &&
750 ucredlen >= sizeof(ucred) && ucred.cr_version == XUCRED_VERSION)
751 pi->priv = (ucred.cr_uid == 0);
755 * Input from a stream socket.
758 recv_stream(struct port_input *pi)
765 if (pi->buf == NULL) {
766 /* no buffer yet - allocate one */
767 if ((pi->buf = buf_alloc(0)) == NULL) {
768 /* ups - could not get buffer. Return an error
769 * the caller must close the transport. */
772 pi->buflen = buf_size(0);
777 /* try to get a message */
778 msg.msg_name = pi->peer;
779 msg.msg_namelen = pi->peerlen;
783 msg.msg_control = &cmsg;
784 msg.msg_controllen = sizeof(cmsg);
786 cmsg.hdr.cmsg_len = sizeof(cmsg);
787 cmsg.hdr.cmsg_level = SOL_SOCKET;
788 cmsg.hdr.cmsg_type = SCM_CREDS;
790 msg.msg_control = NULL;
791 msg.msg_controllen = 0;
795 iov[0].iov_base = pi->buf + pi->length;
796 iov[0].iov_len = pi->buflen - pi->length;
798 len = recvmsg(pi->fd, &msg, 0);
800 if (len == -1 || len == 0)
807 check_priv(pi, &msg);
813 * Input from a datagram socket.
814 * Each receive should return one datagram.
817 recv_dgram(struct port_input *pi)
825 if (pi->buf == NULL) {
826 /* no buffer yet - allocate one */
827 if ((pi->buf = buf_alloc(0)) == NULL) {
828 /* ups - could not get buffer. Read away input
830 (void)recvfrom(pi->fd, embuf, sizeof(embuf),
835 pi->buflen = buf_size(0);
838 /* try to get a message */
839 msg.msg_name = pi->peer;
840 msg.msg_namelen = pi->peerlen;
844 msg.msg_control = &cmsg;
845 msg.msg_controllen = sizeof(cmsg);
847 cmsg.hdr.cmsg_len = sizeof(cmsg);
848 cmsg.hdr.cmsg_level = SOL_SOCKET;
849 cmsg.hdr.cmsg_type = SCM_CREDS;
851 msg.msg_control = NULL;
852 msg.msg_controllen = 0;
856 iov[0].iov_base = pi->buf;
857 iov[0].iov_len = pi->buflen;
859 len = recvmsg(pi->fd, &msg, 0);
861 if (len == -1 || len == 0)
865 if (msg.msg_flags & MSG_TRUNC) {
866 /* truncated - drop */
867 snmpd_stats.silentDrops++;
868 snmpd_stats.inTooLong++;
872 pi->length = (size_t)len;
875 check_priv(pi, &msg);
881 * Input from a socket
884 snmpd_input(struct port_input *pi, struct tport *tport)
889 enum snmpd_input_err ierr, ferr;
890 enum snmpd_proxy_err perr;
894 #ifdef USE_TCPWRAPPERS
898 /* get input depending on the transport */
900 ret = recv_stream(pi);
902 ret = recv_dgram(pi);
908 #ifdef USE_TCPWRAPPERS
910 * In case of AF_INET{6} peer, do hosts_access(5) check.
912 if (pi->peer->sa_family != AF_LOCAL &&
913 inet_ntop(pi->peer->sa_family,
914 &((const struct sockaddr_in *)(const void *)pi->peer)->sin_addr,
915 client, sizeof(client)) != NULL) {
916 request_set(&req, RQ_CLIENT_ADDR, client, 0);
917 if (hosts_access(&req) == 0) {
918 syslog(LOG_ERR, "refused connection from %.500s",
922 } else if (pi->peer->sa_family != AF_LOCAL)
923 syslog(LOG_ERR, "inet_ntop(): %m");
929 ierr = snmp_input_start(pi->buf, pi->length, "SNMP", &pdu, &vi,
931 if (ierr == SNMPD_INPUT_TRUNC) {
932 /* need more bytes. This is ok only for streaming transports.
933 * but only if we have not reached bufsiz yet. */
935 if (pi->length == buf_size(0)) {
936 snmpd_stats.silentDrops++;
941 snmpd_stats.silentDrops++;
945 /* can't check for bad SET pdus here, because a proxy may have to
946 * check the access first. We don't want to return an error response
947 * to a proxy PDU with a wrong community */
948 if (ierr == SNMPD_INPUT_FAILED) {
949 /* for streaming transports this is fatal */
952 snmp_input_consume(pi);
955 if (ierr == SNMPD_INPUT_BAD_COMM) {
956 snmp_input_consume(pi);
961 * If that is a module community and the module has a proxy function,
962 * the hand it over to the module.
964 if (comm->owner != NULL && comm->owner->config->proxy != NULL) {
965 perr = (*comm->owner->config->proxy)(&pdu, tport->transport,
966 &tport->index, pi->peer, pi->peerlen, ierr, vi,
967 !pi->cred || pi->priv);
972 snmp_input_consume(pi);
975 case SNMPD_PROXY_REJ:
978 case SNMPD_PROXY_DROP:
979 snmp_input_consume(pi);
981 snmpd_stats.proxyDrops++;
984 case SNMPD_PROXY_BADCOMM:
985 snmp_input_consume(pi);
987 snmpd_stats.inBadCommunityNames++;
988 if (snmpd.auth_traps)
989 snmp_send_trap(&oid_authenticationFailure,
990 (struct snmp_value *)NULL);
993 case SNMPD_PROXY_BADCOMMUSE:
994 snmp_input_consume(pi);
996 snmpd_stats.inBadCommunityUses++;
997 if (snmpd.auth_traps)
998 snmp_send_trap(&oid_authenticationFailure,
999 (struct snmp_value *)NULL);
1007 if (pdu.type == SNMP_PDU_RESPONSE ||
1008 pdu.type == SNMP_PDU_TRAP ||
1009 pdu.type == SNMP_PDU_TRAP2) {
1010 snmpd_stats.silentDrops++;
1011 snmpd_stats.inBadPduTypes++;
1012 snmp_pdu_free(&pdu);
1013 snmp_input_consume(pi);
1020 if ((pi->cred && !pi->priv && pdu.type == SNMP_PDU_SET) ||
1021 (community != COMM_WRITE &&
1022 (pdu.type == SNMP_PDU_SET || community != COMM_READ))) {
1023 snmpd_stats.inBadCommunityUses++;
1024 snmp_pdu_free(&pdu);
1025 snmp_input_consume(pi);
1026 if (snmpd.auth_traps)
1027 snmp_send_trap(&oid_authenticationFailure,
1028 (struct snmp_value *)NULL);
1035 if ((sndbuf = buf_alloc(1)) == NULL) {
1036 snmpd_stats.silentDrops++;
1037 snmp_pdu_free(&pdu);
1038 snmp_input_consume(pi);
1041 ferr = snmp_input_finish(&pdu, pi->buf, pi->length,
1042 sndbuf, &sndlen, "SNMP", ierr, vi, NULL);
1044 if (ferr == SNMPD_INPUT_OK) {
1045 slen = sendto(pi->fd, sndbuf, sndlen, 0, pi->peer, pi->peerlen);
1047 syslog(LOG_ERR, "sendto: %m");
1048 else if ((size_t)slen != sndlen)
1049 syslog(LOG_ERR, "sendto: short write %zu/%zu",
1050 sndlen, (size_t)slen);
1052 snmp_pdu_free(&pdu);
1054 snmp_input_consume(pi);
1060 * Send a PDU to a given port
1063 snmp_send_port(void *targ, const struct asn_oid *port, struct snmp_pdu *pdu,
1064 const struct sockaddr *addr, socklen_t addrlen)
1066 struct transport *trans = targ;
1072 TAILQ_FOREACH(tp, &trans->table, link)
1073 if (asn_compare_oid(port, &tp->index) == 0)
1078 if ((sndbuf = buf_alloc(1)) == NULL)
1081 snmp_output(pdu, sndbuf, &sndlen, "SNMP PROXY");
1083 len = trans->vtab->send(tp, sndbuf, sndlen, addr, addrlen);
1086 syslog(LOG_ERR, "sendto: %m");
1087 else if ((size_t)len != sndlen)
1088 syslog(LOG_ERR, "sendto: short write %zu/%zu",
1089 sndlen, (size_t)len);
1096 * Close an input source
1099 snmpd_input_close(struct port_input *pi)
1102 fd_deselect(pi->id);
1104 (void)close(pi->fd);
1105 if (pi->buf != NULL)
1110 * Dump internal state.
1112 #ifdef USE_LIBBEGEMOT
1117 info_func(evContext ctx __unused, void *uap __unused, const void *tag __unused)
1124 syslog(LOG_DEBUG, "Dump of SNMPd %lu\n", (u_long)getpid());
1125 for (i = 0; i < tree_size; i++) {
1126 switch (tree[i].type) {
1128 case SNMP_NODE_LEAF:
1129 sprintf(buf, "LEAF: %s %s", tree[i].name,
1130 asn_oid2str(&tree[i].oid));
1133 case SNMP_NODE_COLUMN:
1134 sprintf(buf, "COL: %s %s", tree[i].name,
1135 asn_oid2str(&tree[i].oid));
1138 syslog(LOG_DEBUG, "%s", buf);
1141 TAILQ_FOREACH(m, &lmodules, link)
1142 if (m->config->dump)
1143 (*m->config->dump)();
1147 * Re-read configuration
1149 #ifdef USE_LIBBEGEMOT
1154 config_func(evContext ctx __unused, void *uap __unused,
1155 const void *tag __unused)
1160 if (read_config(config_file, NULL)) {
1161 syslog(LOG_ERR, "error reading config file '%s'", config_file);
1164 TAILQ_FOREACH(m, &lmodules, link)
1165 if (m->config->config)
1166 (*m->config->config)();
1170 * On USR1 dump actual configuration.
1173 onusr1(int s __unused)
1176 work |= WORK_DOINFO;
1179 onhup(int s __unused)
1182 work |= WORK_RECONFIG;
1186 onterm(int s __unused)
1189 /* allow clean-up */
1196 struct sigaction sa;
1198 sa.sa_handler = onusr1;
1199 sa.sa_flags = SA_RESTART;
1200 sigemptyset(&sa.sa_mask);
1201 if (sigaction(SIGUSR1, &sa, NULL)) {
1202 syslog(LOG_ERR, "sigaction: %m");
1206 sa.sa_handler = onhup;
1207 if (sigaction(SIGHUP, &sa, NULL)) {
1208 syslog(LOG_ERR, "sigaction: %m");
1212 sa.sa_handler = onterm;
1214 sigemptyset(&sa.sa_mask);
1215 if (sigaction(SIGTERM, &sa, NULL)) {
1216 syslog(LOG_ERR, "sigaction: %m");
1219 if (sigaction(SIGINT, &sa, NULL)) {
1220 syslog(LOG_ERR, "sigaction: %m");
1231 if (sigprocmask(SIG_BLOCK, &set, &blocked_sigs) == -1) {
1232 syslog(LOG_ERR, "SIG_BLOCK: %m");
1239 if (sigprocmask(SIG_SETMASK, &blocked_sigs, NULL) == -1) {
1240 syslog(LOG_ERR, "SIG_SETMASK: %m");
1251 (void)unlink(pid_file);
1257 struct transport *t;
1259 TAILQ_FOREACH(t, &transport_list, link)
1260 (void)t->vtab->stop(1);
1264 * Define a macro from the command line
1272 if ((eq = strchr(arg, '=')) == NULL)
1273 err = define_macro(arg, "");
1276 err = define_macro(arg, eq);
1279 syslog(LOG_ERR, "cannot save macro: %m");
1285 * Re-implement getsubopt from scratch, because the second argument is broken
1286 * and will not compile with WARNS=5.
1289 getsubopt1(char **arg, const char *const *options, char **valp, char **optp)
1291 static const char *const delim = ",\t ";
1297 /* skip leading junk */
1298 for (ptr = *arg; *ptr != '\0'; ptr++)
1299 if (strchr(delim, *ptr) == NULL)
1307 /* find the end of the option */
1308 while (*++ptr != '\0')
1309 if (strchr(delim, *ptr) != NULL || *ptr == '=')
1316 while (*ptr != '\0' && strchr(delim, *ptr) == NULL)
1326 for (i = 0; *options != NULL; options++, i++)
1327 if (strcmp(*optp, *options) == 0)
1333 main(int argc, char *argv[])
1339 const char *prefix = "snmpd";
1341 char *value, *option;
1342 struct transport *t;
1345 #define DBG_EVENTS 1
1347 static const char *const debug_opts[] = {
1354 snmp_printf = snmp_printf_func;
1355 snmp_error = snmp_error_func;
1356 snmp_debug = snmp_debug_func;
1357 asn_error = asn_error_func;
1359 while ((opt = getopt(argc, argv, "c:dD:hI:l:m:p:")) != EOF)
1363 strlcpy(config_file, optarg, sizeof(config_file));
1372 switch (getsubopt1(&optarg, debug_opts,
1376 debug.dump_pdus = 1;
1386 "no value for 'trace'");
1388 snmp_trace = strtoul(value,
1395 "unknown debug flag '%s'",
1399 "missing debug flag");
1406 fprintf(stderr, "%s", usgtxt);
1422 strlcpy(pid_file, optarg, sizeof(pid_file));
1426 openlog(prefix, LOG_PID | (background ? 0 : LOG_PERROR), LOG_USER);
1427 setlogmask(LOG_UPTO(debug.logpri - 1));
1429 if (background && daemon(0, 0) < 0) {
1430 syslog(LOG_ERR, "daemon: %m");
1442 snmp_serial_no = random();
1444 #ifdef USE_TCPWRAPPERS
1446 * Initialize hosts_access(3) handler.
1448 request_init(&req, RQ_DAEMON, "snmpd", 0);
1453 * Initialize the tree.
1455 if ((tree = malloc(sizeof(struct snmp_node) * CTREE_SIZE)) == NULL) {
1456 syslog(LOG_ERR, "%m");
1459 memcpy(tree, ctree, sizeof(struct snmp_node) * CTREE_SIZE);
1460 tree_size = CTREE_SIZE;
1463 * Get standard communities
1465 (void)comm_define(1, "SNMP read", NULL, NULL);
1466 (void)comm_define(2, "SNMP write", NULL, NULL);
1467 community = COMM_INITIALIZE;
1469 trap_reqid = reqid_allocate(512, NULL);
1471 if (config_file[0] == '\0')
1472 snprintf(config_file, sizeof(config_file), PATH_CONFIG, prefix);
1476 this_tick = get_ticks();
1477 start_tick = this_tick;
1479 /* start transports */
1480 if (atexit(trans_stop) == -1) {
1481 syslog(LOG_ERR, "atexit failed: %m");
1484 if (udp_trans.start() != SNMP_ERR_NOERROR)
1485 syslog(LOG_WARNING, "cannot start UDP transport");
1486 if (lsock_trans.start() != SNMP_ERR_NOERROR)
1487 syslog(LOG_WARNING, "cannot start LSOCK transport");
1489 #ifdef USE_LIBBEGEMOT
1490 if (debug.evdebug > 0)
1493 if (evCreate(&evctx)) {
1494 syslog(LOG_ERR, "evCreate: %m");
1497 if (debug.evdebug > 0)
1498 evSetDebug(evctx, 10, stderr);
1501 if (read_config(config_file, NULL)) {
1502 syslog(LOG_ERR, "error in config file");
1506 TAILQ_FOREACH(t, &transport_list, link)
1507 TAILQ_FOREACH(p, &t->table, link)
1508 t->vtab->init_port(p);
1512 if (pid_file[0] == '\0')
1513 snprintf(pid_file, sizeof(pid_file), PATH_PID, prefix);
1515 if ((fp = fopen(pid_file, "w")) != NULL) {
1516 fprintf(fp, "%u", getpid());
1518 if (atexit(term) == -1) {
1519 syslog(LOG_ERR, "atexit failed: %m");
1520 (void)remove(pid_file);
1525 if (or_register(&oid_snmpMIB, "The MIB module for SNMPv2 entities.",
1527 syslog(LOG_ERR, "cannot register SNMPv2 MIB");
1530 if (or_register(&oid_begemotSnmpd, "The MIB module for the Begemot SNMPd.",
1532 syslog(LOG_ERR, "cannot register begemotSnmpd MIB");
1536 snmp_send_trap(&oid_coldStart, (struct snmp_value *)NULL);
1538 while ((m = TAILQ_FIRST(&modules_start)) != NULL) {
1539 m->flags &= ~LM_ONSTARTLIST;
1540 TAILQ_REMOVE(&modules_start, m, start);
1545 #ifndef USE_LIBBEGEMOT
1548 struct lmodule *mod;
1550 TAILQ_FOREACH(mod, &lmodules, link)
1551 if (mod->config->idle != NULL)
1552 (*mod->config->idle)();
1554 #ifndef USE_LIBBEGEMOT
1555 if (evGetNext(evctx, &event, EV_WAIT) == 0) {
1556 if (evDispatch(evctx, event))
1557 syslog(LOG_ERR, "evDispatch: %m");
1558 } else if (errno != EINTR) {
1559 syslog(LOG_ERR, "evGetNext: %m");
1568 if (work & WORK_DOINFO) {
1569 #ifdef USE_LIBBEGEMOT
1572 if (evWaitFor(evctx, &work, info_func,
1573 NULL, NULL) == -1) {
1574 syslog(LOG_ERR, "evWaitFor: %m");
1579 if (work & WORK_RECONFIG) {
1580 #ifdef USE_LIBBEGEMOT
1583 if (evWaitFor(evctx, &work, config_func,
1584 NULL, NULL) == -1) {
1585 syslog(LOG_ERR, "evWaitFor: %m");
1592 #ifndef USE_LIBBEGEMOT
1593 if (evDo(evctx, &work) == -1) {
1594 syslog(LOG_ERR, "evDo: %m");
1610 if (gettimeofday(&tv, NULL))
1612 ret = tv.tv_sec * 100ULL + tv.tv_usec / 10000ULL;
1621 * Trampoline for the non-repeatable timers.
1623 #ifdef USE_LIBBEGEMOT
1625 tfunc(int tid __unused, void *uap)
1628 tfunc(evContext ctx __unused, void *uap, struct timespec due __unused,
1629 struct timespec inter __unused)
1632 struct timer *tp = uap;
1634 LIST_REMOVE(tp, link);
1635 tp->func(tp->udata);
1640 * Trampoline for the repeatable timers.
1642 #ifdef USE_LIBBEGEMOT
1644 trfunc(int tid __unused, void *uap)
1647 trfunc(evContext ctx __unused, void *uap, struct timespec due __unused,
1648 struct timespec inter __unused)
1651 struct timer *tp = uap;
1653 tp->func(tp->udata);
1657 * Start a one-shot timer
1660 timer_start(u_int ticks, void (*func)(void *), void *udata, struct lmodule *mod)
1663 #ifndef USE_LIBBEGEMOT
1664 struct timespec due;
1667 if ((tp = malloc(sizeof(struct timer))) == NULL) {
1668 syslog(LOG_CRIT, "out of memory for timer");
1672 #ifndef USE_LIBBEGEMOT
1673 due = evAddTime(evNowTime(),
1674 evConsTime(ticks / 100, (ticks % 100) * 10000));
1681 LIST_INSERT_HEAD(&timer_list, tp, link);
1683 #ifdef USE_LIBBEGEMOT
1684 if ((tp->id = poll_start_timer(ticks * 10, 0, tfunc, tp)) < 0) {
1685 syslog(LOG_ERR, "cannot set timer: %m");
1689 if (evSetTimer(evctx, tfunc, tp, due, evConsTime(0, 0), &tp->id)
1691 syslog(LOG_ERR, "cannot set timer: %m");
1699 * Start a repeatable timer. When used with USE_LIBBEGEMOT the first argument
1700 * is currently ignored and the initial number of ticks is set to the
1701 * repeat number of ticks.
1704 timer_start_repeat(u_int ticks __unused, u_int repeat_ticks,
1705 void (*func)(void *), void *udata, struct lmodule *mod)
1708 #ifndef USE_LIBBEGEMOT
1709 struct timespec due;
1710 struct timespec inter;
1713 if ((tp = malloc(sizeof(struct timer))) == NULL) {
1714 syslog(LOG_CRIT, "out of memory for timer");
1718 #ifndef USE_LIBBEGEMOT
1719 due = evAddTime(evNowTime(),
1720 evConsTime(ticks / 100, (ticks % 100) * 10000));
1721 inter = evConsTime(repeat_ticks / 100, (repeat_ticks % 100) * 10000);
1728 LIST_INSERT_HEAD(&timer_list, tp, link);
1730 #ifdef USE_LIBBEGEMOT
1731 if ((tp->id = poll_start_timer(repeat_ticks * 10, 1, trfunc, tp)) < 0) {
1732 syslog(LOG_ERR, "cannot set timer: %m");
1736 if (evSetTimer(evctx, trfunc, tp, due, inter, &tp->id) == -1) {
1737 syslog(LOG_ERR, "cannot set timer: %m");
1750 struct timer *tp = p;
1752 LIST_REMOVE(tp, link);
1753 #ifdef USE_LIBBEGEMOT
1754 poll_stop_timer(tp->id);
1756 if (evClearTimer(evctx, tp->id) == -1) {
1757 syslog(LOG_ERR, "cannot stop timer: %m");
1765 timer_flush(struct lmodule *mod)
1767 struct timer *t, *t1;
1769 t = LIST_FIRST(&timer_list);
1771 t1 = LIST_NEXT(t, link);
1772 if (t->owner == mod)
1779 snmp_printf_func(const char *fmt, ...)
1782 static char *pend = NULL;
1786 vasprintf(&ret, fmt, ap);
1792 if ((new = realloc(pend, strlen(pend) + strlen(ret) + 1))
1803 while ((ret = strchr(pend, '\n')) != NULL) {
1805 syslog(LOG_DEBUG, "%s", pend);
1806 if (strlen(ret + 1) == 0) {
1811 strcpy(pend, ret + 1);
1816 snmp_error_func(const char *err, ...)
1821 if (!(snmp_trace & LOG_SNMP_ERRORS))
1825 snprintf(errbuf, sizeof(errbuf), "SNMP: ");
1826 vsnprintf(errbuf + strlen(errbuf),
1827 sizeof(errbuf) - strlen(errbuf), err, ap);
1830 syslog(LOG_ERR, "%s", errbuf);
1834 snmp_debug_func(const char *err, ...)
1840 snprintf(errbuf, sizeof(errbuf), "SNMP: ");
1841 vsnprintf(errbuf+strlen(errbuf), sizeof(errbuf)-strlen(errbuf),
1845 syslog(LOG_DEBUG, "%s", errbuf);
1849 asn_error_func(const struct asn_buf *b, const char *err, ...)
1855 if (!(snmp_trace & LOG_ASN1_ERRORS))
1859 snprintf(errbuf, sizeof(errbuf), "ASN.1: ");
1860 vsnprintf(errbuf + strlen(errbuf),
1861 sizeof(errbuf) - strlen(errbuf), err, ap);
1865 snprintf(errbuf + strlen(errbuf),
1866 sizeof(errbuf) - strlen(errbuf), " at");
1867 for (i = 0; b->asn_len > i; i++)
1868 snprintf(errbuf + strlen(errbuf),
1869 sizeof(errbuf) - strlen(errbuf),
1870 " %02x", b->asn_cptr[i]);
1873 syslog(LOG_ERR, "%s", errbuf);
1877 * Create a new community
1880 comm_define(u_int priv, const char *descr, struct lmodule *owner,
1883 struct community *c, *p;
1886 /* generate an identifier */
1888 if ((ncomm = next_community_index++) == UINT_MAX)
1889 next_community_index = 1;
1890 TAILQ_FOREACH(c, &community_list, link)
1891 if (c->value == ncomm)
1893 } while (c != NULL);
1895 if ((c = malloc(sizeof(struct community))) == NULL) {
1896 syslog(LOG_ERR, "comm_define: %m");
1906 if((c->string = malloc(strlen(str)+1)) == NULL) {
1910 strcpy(c->string, str);
1914 if (c->owner == NULL) {
1916 c->index.subs[0] = 0;
1918 c->index = c->owner->index;
1920 c->index.subs[c->index.len++] = c->private;
1925 TAILQ_FOREACH(p, &community_list, link) {
1926 if (asn_compare_oid(&p->index, &c->index) > 0) {
1927 TAILQ_INSERT_BEFORE(p, c, link);
1932 TAILQ_INSERT_TAIL(&community_list, c, link);
1937 comm_string(u_int ncomm)
1939 struct community *p;
1941 TAILQ_FOREACH(p, &community_list, link)
1942 if (p->value == ncomm)
1948 * Delete all communities allocated by a module
1951 comm_flush(struct lmodule *mod)
1953 struct community *p, *p1;
1955 p = TAILQ_FIRST(&community_list);
1957 p1 = TAILQ_NEXT(p, link);
1958 if (p->owner == mod) {
1960 TAILQ_REMOVE(&community_list, p, link);
1968 * Request ID handling.
1970 * Allocate a new range of request ids. Use a first fit algorithm.
1973 reqid_allocate(int size, struct lmodule *mod)
1976 struct idrange *r, *r1;
1978 if (size <= 0 || size > INT32_MAX) {
1979 syslog(LOG_CRIT, "%s: size out of range: %d", __func__, size);
1982 /* allocate a type id */
1984 if ((type = next_idrange++) == UINT_MAX)
1986 TAILQ_FOREACH(r, &idrange_list, link)
1987 if (r->type == type)
1992 if (TAILQ_EMPTY(&idrange_list))
1995 r = TAILQ_FIRST(&idrange_list);
1996 if (r->base < size) {
1997 while((r1 = TAILQ_NEXT(r, link)) != NULL) {
1998 if (r1->base - (r->base + r->size) >= size)
2005 r1 = TAILQ_LAST(&idrange_list, idrange_list);
2006 if (INT32_MAX - size + 1 < r1->base + r1->size) {
2007 syslog(LOG_ERR, "out of id ranges (%u)", size);
2013 /* allocate structure */
2014 if ((r1 = malloc(sizeof(struct idrange))) == NULL) {
2015 syslog(LOG_ERR, "%s: %m", __FUNCTION__);
2022 if (TAILQ_EMPTY(&idrange_list) || r == TAILQ_FIRST(&idrange_list)) {
2024 TAILQ_INSERT_HEAD(&idrange_list, r1, link);
2025 } else if (r == NULL) {
2026 r = TAILQ_LAST(&idrange_list, idrange_list);
2027 r1->base = r->base + r->size;
2028 TAILQ_INSERT_TAIL(&idrange_list, r1, link);
2030 r = TAILQ_PREV(r, idrange_list, link);
2031 r1->base = r->base + r->size;
2032 TAILQ_INSERT_AFTER(&idrange_list, r, r1, link);
2034 r1->next = r1->base;
2040 reqid_next(u_int type)
2045 TAILQ_FOREACH(r, &idrange_list, link)
2046 if (r->type == type)
2049 syslog(LOG_CRIT, "wrong idrange type");
2052 if ((id = r->next++) == r->base + (r->size - 1))
2058 reqid_base(u_int type)
2062 TAILQ_FOREACH(r, &idrange_list, link)
2063 if (r->type == type)
2065 syslog(LOG_CRIT, "wrong idrange type");
2070 reqid_type(int32_t reqid)
2074 TAILQ_FOREACH(r, &idrange_list, link)
2075 if (reqid >= r->base && reqid <= r->base + (r->size - 1))
2081 reqid_istype(int32_t reqid, u_int type)
2083 return (reqid_type(reqid) == type);
2087 * Delete all communities allocated by a module
2090 reqid_flush(struct lmodule *mod)
2092 struct idrange *p, *p1;
2094 p = TAILQ_FIRST(&idrange_list);
2096 p1 = TAILQ_NEXT(p, link);
2097 if (p->owner == mod) {
2098 TAILQ_REMOVE(&idrange_list, p, link);
2106 * Merge the given tree for the given module into the main tree.
2109 compare_node(const void *v1, const void *v2)
2111 const struct snmp_node *n1 = v1;
2112 const struct snmp_node *n2 = v2;
2114 return (asn_compare_oid(&n1->oid, &n2->oid));
2117 tree_merge(const struct snmp_node *ntree, u_int nsize, struct lmodule *mod)
2119 struct snmp_node *xtree;
2122 xtree = realloc(tree, sizeof(*tree) * (tree_size + nsize));
2123 if (xtree == NULL) {
2124 syslog(LOG_ERR, "tree_merge: %m");
2128 memcpy(&tree[tree_size], ntree, sizeof(*tree) * nsize);
2130 for (i = 0; i < nsize; i++)
2131 tree[tree_size + i].tree_data = mod;
2135 qsort(tree, tree_size, sizeof(tree[0]), compare_node);
2141 * Remove all nodes belonging to the loadable module
2144 tree_unmerge(struct lmodule *mod)
2148 for(s = d = 0; s < tree_size; s++)
2149 if (tree[s].tree_data != mod) {
2161 lm_load(const char *path, const char *section)
2166 char *av[MAX_MOD_ARGS + 1];
2170 if ((m = malloc(sizeof(*m))) == NULL) {
2171 syslog(LOG_ERR, "lm_load: %m");
2176 strcpy(m->section, section);
2178 if ((m->path = malloc(strlen(path) + 1)) == NULL) {
2179 syslog(LOG_ERR, "lm_load: %m");
2182 strcpy(m->path, path);
2187 m->index.subs[0] = strlen(section);
2188 m->index.len = m->index.subs[0] + 1;
2189 for (u = 0; u < m->index.subs[0]; u++)
2190 m->index.subs[u + 1] = section[u];
2193 * Load the object file and locate the config structure
2195 if ((m->handle = dlopen(m->path, RTLD_NOW|RTLD_GLOBAL)) == NULL) {
2196 syslog(LOG_ERR, "lm_load: open %s", dlerror());
2200 if ((m->config = dlsym(m->handle, "config")) == NULL) {
2201 syslog(LOG_ERR, "lm_load: no 'config' symbol %s", dlerror());
2206 * Insert it into the right place
2208 INSERT_OBJECT_OID(m, &lmodules);
2210 /* preserve order */
2211 if (community == COMM_INITIALIZE) {
2212 m->flags |= LM_ONSTARTLIST;
2213 TAILQ_INSERT_TAIL(&modules_start, m, start);
2217 * make the argument vector.
2220 for (i = 0; i < nprogargs; i++) {
2221 if (strlen(progargs[i]) >= strlen(section) + 1 &&
2222 strncmp(progargs[i], section, strlen(section)) == 0 &&
2223 progargs[i][strlen(section)] == ':') {
2224 if (ac == MAX_MOD_ARGS) {
2225 syslog(LOG_WARNING, "too many arguments for "
2226 "module '%s", section);
2229 av[ac++] = &progargs[i][strlen(section)+1];
2235 * Run the initialization function
2237 if ((err = (*m->config->init)(m, ac, av)) != 0) {
2238 syslog(LOG_ERR, "lm_load: init failed: %d", err);
2239 TAILQ_REMOVE(&lmodules, m, link);
2246 if ((m->flags & LM_ONSTARTLIST) != 0)
2247 TAILQ_REMOVE(&modules_start, m, start);
2259 lm_start(struct lmodule *mod)
2261 const struct lmodule *m;
2264 * Merge tree. If this fails, unload the module.
2266 if (tree_merge(mod->config->tree, mod->config->tree_size, mod)) {
2272 * Read configuration
2274 if (read_config(config_file, mod)) {
2275 syslog(LOG_ERR, "error in config file");
2279 if (mod->config->start)
2280 (*mod->config->start)();
2282 mod->flags |= LM_STARTED;
2285 * Inform other modules
2287 TAILQ_FOREACH(m, &lmodules, link)
2288 if (m->config->loading)
2289 (*m->config->loading)(mod, 1);
2297 lm_unload(struct lmodule *m)
2300 const struct lmodule *mod;
2302 TAILQ_REMOVE(&lmodules, m, link);
2303 if (m->flags & LM_ONSTARTLIST)
2304 TAILQ_REMOVE(&modules_start, m, start);
2307 if ((m->flags & LM_STARTED) && m->config->fini &&
2308 (err = (*m->config->fini)()) != 0)
2309 syslog(LOG_WARNING, "lm_unload(%s): fini %d", m->section, err);
2320 * Inform other modules
2322 TAILQ_FOREACH(mod, &lmodules, link)
2323 if (mod->config->loading)
2324 (*mod->config->loading)(m, 0);
2330 * Register an object resource and return the index (or 0 on failures)
2333 or_register(const struct asn_oid *or, const char *descr, struct lmodule *mod)
2335 struct objres *objres, *or1;
2338 /* find a free index */
2340 for (objres = TAILQ_FIRST(&objres_list);
2342 objres = TAILQ_NEXT(objres, link)) {
2343 if ((or1 = TAILQ_NEXT(objres, link)) == NULL ||
2344 or1->index > objres->index + 1) {
2345 idx = objres->index + 1;
2350 if ((objres = malloc(sizeof(*objres))) == NULL)
2353 objres->index = idx;
2355 strlcpy(objres->descr, descr, sizeof(objres->descr));
2356 objres->uptime = (uint32_t)(get_ticks() - start_tick);
2357 objres->module = mod;
2359 INSERT_OBJECT_INT(objres, &objres_list);
2361 systemg.or_last_change = objres->uptime;
2367 or_unregister(u_int idx)
2369 struct objres *objres;
2371 TAILQ_FOREACH(objres, &objres_list, link)
2372 if (objres->index == idx) {
2373 TAILQ_REMOVE(&objres_list, objres, link);