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 (inet_ntop(pi->peer->sa_family,
913 &((const struct sockaddr_in *)(const void *)pi->peer)->sin_addr,
914 client, sizeof(client)) != NULL) {
915 request_set(&req, RQ_CLIENT_ADDR, client, 0);
916 if (hosts_access(&req) == 0) {
917 syslog(LOG_ERR, "refused connection from %.500s",
922 syslog(LOG_ERR, "inet_ntop(): %m");
928 ierr = snmp_input_start(pi->buf, pi->length, "SNMP", &pdu, &vi,
930 if (ierr == SNMPD_INPUT_TRUNC) {
931 /* need more bytes. This is ok only for streaming transports.
932 * but only if we have not reached bufsiz yet. */
934 if (pi->length == buf_size(0)) {
935 snmpd_stats.silentDrops++;
940 snmpd_stats.silentDrops++;
944 /* can't check for bad SET pdus here, because a proxy may have to
945 * check the access first. We don't want to return an error response
946 * to a proxy PDU with a wrong community */
947 if (ierr == SNMPD_INPUT_FAILED) {
948 /* for streaming transports this is fatal */
951 snmp_input_consume(pi);
954 if (ierr == SNMPD_INPUT_BAD_COMM) {
955 snmp_input_consume(pi);
960 * If that is a module community and the module has a proxy function,
961 * the hand it over to the module.
963 if (comm->owner != NULL && comm->owner->config->proxy != NULL) {
964 perr = (*comm->owner->config->proxy)(&pdu, tport->transport,
965 &tport->index, pi->peer, pi->peerlen, ierr, vi,
966 !pi->cred || pi->priv);
971 snmp_input_consume(pi);
974 case SNMPD_PROXY_REJ:
977 case SNMPD_PROXY_DROP:
978 snmp_input_consume(pi);
980 snmpd_stats.proxyDrops++;
983 case SNMPD_PROXY_BADCOMM:
984 snmp_input_consume(pi);
986 snmpd_stats.inBadCommunityNames++;
987 if (snmpd.auth_traps)
988 snmp_send_trap(&oid_authenticationFailure,
989 (struct snmp_value *)NULL);
992 case SNMPD_PROXY_BADCOMMUSE:
993 snmp_input_consume(pi);
995 snmpd_stats.inBadCommunityUses++;
996 if (snmpd.auth_traps)
997 snmp_send_trap(&oid_authenticationFailure,
998 (struct snmp_value *)NULL);
1006 if (pdu.type == SNMP_PDU_RESPONSE ||
1007 pdu.type == SNMP_PDU_TRAP ||
1008 pdu.type == SNMP_PDU_TRAP2) {
1009 snmpd_stats.silentDrops++;
1010 snmpd_stats.inBadPduTypes++;
1011 snmp_pdu_free(&pdu);
1012 snmp_input_consume(pi);
1019 if ((pi->cred && !pi->priv && pdu.type == SNMP_PDU_SET) ||
1020 (community != COMM_WRITE &&
1021 (pdu.type == SNMP_PDU_SET || community != COMM_READ))) {
1022 snmpd_stats.inBadCommunityUses++;
1023 snmp_pdu_free(&pdu);
1024 snmp_input_consume(pi);
1025 if (snmpd.auth_traps)
1026 snmp_send_trap(&oid_authenticationFailure,
1027 (struct snmp_value *)NULL);
1034 if ((sndbuf = buf_alloc(1)) == NULL) {
1035 snmpd_stats.silentDrops++;
1036 snmp_pdu_free(&pdu);
1037 snmp_input_consume(pi);
1040 ferr = snmp_input_finish(&pdu, pi->buf, pi->length,
1041 sndbuf, &sndlen, "SNMP", ierr, vi, NULL);
1043 if (ferr == SNMPD_INPUT_OK) {
1044 slen = sendto(pi->fd, sndbuf, sndlen, 0, pi->peer, pi->peerlen);
1046 syslog(LOG_ERR, "sendto: %m");
1047 else if ((size_t)slen != sndlen)
1048 syslog(LOG_ERR, "sendto: short write %zu/%zu",
1049 sndlen, (size_t)slen);
1051 snmp_pdu_free(&pdu);
1053 snmp_input_consume(pi);
1059 * Send a PDU to a given port
1062 snmp_send_port(void *targ, const struct asn_oid *port, struct snmp_pdu *pdu,
1063 const struct sockaddr *addr, socklen_t addrlen)
1065 struct transport *trans = targ;
1071 TAILQ_FOREACH(tp, &trans->table, link)
1072 if (asn_compare_oid(port, &tp->index) == 0)
1077 if ((sndbuf = buf_alloc(1)) == NULL)
1080 snmp_output(pdu, sndbuf, &sndlen, "SNMP PROXY");
1082 len = trans->vtab->send(tp, sndbuf, sndlen, addr, addrlen);
1085 syslog(LOG_ERR, "sendto: %m");
1086 else if ((size_t)len != sndlen)
1087 syslog(LOG_ERR, "sendto: short write %zu/%zu",
1088 sndlen, (size_t)len);
1095 * Close an input source
1098 snmpd_input_close(struct port_input *pi)
1101 fd_deselect(pi->id);
1103 (void)close(pi->fd);
1104 if (pi->buf != NULL)
1109 * Dump internal state.
1111 #ifdef USE_LIBBEGEMOT
1116 info_func(evContext ctx __unused, void *uap __unused, const void *tag __unused)
1123 syslog(LOG_DEBUG, "Dump of SNMPd %lu\n", (u_long)getpid());
1124 for (i = 0; i < tree_size; i++) {
1125 switch (tree[i].type) {
1127 case SNMP_NODE_LEAF:
1128 sprintf(buf, "LEAF: %s %s", tree[i].name,
1129 asn_oid2str(&tree[i].oid));
1132 case SNMP_NODE_COLUMN:
1133 sprintf(buf, "COL: %s %s", tree[i].name,
1134 asn_oid2str(&tree[i].oid));
1137 syslog(LOG_DEBUG, "%s", buf);
1140 TAILQ_FOREACH(m, &lmodules, link)
1141 if (m->config->dump)
1142 (*m->config->dump)();
1146 * Re-read configuration
1148 #ifdef USE_LIBBEGEMOT
1153 config_func(evContext ctx __unused, void *uap __unused,
1154 const void *tag __unused)
1159 if (read_config(config_file, NULL)) {
1160 syslog(LOG_ERR, "error reading config file '%s'", config_file);
1163 TAILQ_FOREACH(m, &lmodules, link)
1164 if (m->config->config)
1165 (*m->config->config)();
1169 * On USR1 dump actual configuration.
1172 onusr1(int s __unused)
1175 work |= WORK_DOINFO;
1178 onhup(int s __unused)
1181 work |= WORK_RECONFIG;
1185 onterm(int s __unused)
1188 /* allow clean-up */
1195 struct sigaction sa;
1197 sa.sa_handler = onusr1;
1198 sa.sa_flags = SA_RESTART;
1199 sigemptyset(&sa.sa_mask);
1200 if (sigaction(SIGUSR1, &sa, NULL)) {
1201 syslog(LOG_ERR, "sigaction: %m");
1205 sa.sa_handler = onhup;
1206 if (sigaction(SIGHUP, &sa, NULL)) {
1207 syslog(LOG_ERR, "sigaction: %m");
1211 sa.sa_handler = onterm;
1213 sigemptyset(&sa.sa_mask);
1214 if (sigaction(SIGTERM, &sa, NULL)) {
1215 syslog(LOG_ERR, "sigaction: %m");
1218 if (sigaction(SIGINT, &sa, NULL)) {
1219 syslog(LOG_ERR, "sigaction: %m");
1230 if (sigprocmask(SIG_BLOCK, &set, &blocked_sigs) == -1) {
1231 syslog(LOG_ERR, "SIG_BLOCK: %m");
1238 if (sigprocmask(SIG_SETMASK, &blocked_sigs, NULL) == -1) {
1239 syslog(LOG_ERR, "SIG_SETMASK: %m");
1250 (void)unlink(pid_file);
1256 struct transport *t;
1258 TAILQ_FOREACH(t, &transport_list, link)
1259 (void)t->vtab->stop(1);
1263 * Define a macro from the command line
1271 if ((eq = strchr(arg, '=')) == NULL)
1272 err = define_macro(arg, "");
1275 err = define_macro(arg, eq);
1278 syslog(LOG_ERR, "cannot save macro: %m");
1284 * Re-implement getsubopt from scratch, because the second argument is broken
1285 * and will not compile with WARNS=5.
1288 getsubopt1(char **arg, const char *const *options, char **valp, char **optp)
1290 static const char *const delim = ",\t ";
1296 /* skip leading junk */
1297 for (ptr = *arg; *ptr != '\0'; ptr++)
1298 if (strchr(delim, *ptr) == NULL)
1306 /* find the end of the option */
1307 while (*++ptr != '\0')
1308 if (strchr(delim, *ptr) != NULL || *ptr == '=')
1315 while (*ptr != '\0' && strchr(delim, *ptr) == NULL)
1325 for (i = 0; *options != NULL; options++, i++)
1326 if (strcmp(*optp, *options) == 0)
1332 main(int argc, char *argv[])
1338 const char *prefix = "snmpd";
1340 char *value, *option;
1341 struct transport *t;
1344 #define DBG_EVENTS 1
1346 static const char *const debug_opts[] = {
1353 snmp_printf = snmp_printf_func;
1354 snmp_error = snmp_error_func;
1355 snmp_debug = snmp_debug_func;
1356 asn_error = asn_error_func;
1358 while ((opt = getopt(argc, argv, "c:dD:hI:l:m:p:")) != EOF)
1362 strlcpy(config_file, optarg, sizeof(config_file));
1371 switch (getsubopt1(&optarg, debug_opts,
1375 debug.dump_pdus = 1;
1385 "no value for 'trace'");
1387 snmp_trace = strtoul(value,
1394 "unknown debug flag '%s'",
1398 "missing debug flag");
1405 fprintf(stderr, "%s", usgtxt);
1421 strlcpy(pid_file, optarg, sizeof(pid_file));
1425 openlog(prefix, LOG_PID | (background ? 0 : LOG_PERROR), LOG_USER);
1426 setlogmask(LOG_UPTO(debug.logpri - 1));
1428 if (background && daemon(0, 0) < 0) {
1429 syslog(LOG_ERR, "daemon: %m");
1441 snmp_serial_no = random();
1443 #ifdef USE_TCPWRAPPERS
1445 * Initialize hosts_access(3) handler.
1447 request_init(&req, RQ_DAEMON, "snmpd", 0);
1452 * Initialize the tree.
1454 if ((tree = malloc(sizeof(struct snmp_node) * CTREE_SIZE)) == NULL) {
1455 syslog(LOG_ERR, "%m");
1458 memcpy(tree, ctree, sizeof(struct snmp_node) * CTREE_SIZE);
1459 tree_size = CTREE_SIZE;
1462 * Get standard communities
1464 (void)comm_define(1, "SNMP read", NULL, NULL);
1465 (void)comm_define(2, "SNMP write", NULL, NULL);
1466 community = COMM_INITIALIZE;
1468 trap_reqid = reqid_allocate(512, NULL);
1470 if (config_file[0] == '\0')
1471 snprintf(config_file, sizeof(config_file), PATH_CONFIG, prefix);
1475 this_tick = get_ticks();
1476 start_tick = this_tick;
1478 /* start transports */
1479 if (atexit(trans_stop) == -1) {
1480 syslog(LOG_ERR, "atexit failed: %m");
1483 if (udp_trans.start() != SNMP_ERR_NOERROR)
1484 syslog(LOG_WARNING, "cannot start UDP transport");
1485 if (lsock_trans.start() != SNMP_ERR_NOERROR)
1486 syslog(LOG_WARNING, "cannot start LSOCK transport");
1488 #ifdef USE_LIBBEGEMOT
1489 if (debug.evdebug > 0)
1492 if (evCreate(&evctx)) {
1493 syslog(LOG_ERR, "evCreate: %m");
1496 if (debug.evdebug > 0)
1497 evSetDebug(evctx, 10, stderr);
1500 if (read_config(config_file, NULL)) {
1501 syslog(LOG_ERR, "error in config file");
1505 TAILQ_FOREACH(t, &transport_list, link)
1506 TAILQ_FOREACH(p, &t->table, link)
1507 t->vtab->init_port(p);
1511 if (pid_file[0] == '\0')
1512 snprintf(pid_file, sizeof(pid_file), PATH_PID, prefix);
1514 if ((fp = fopen(pid_file, "w")) != NULL) {
1515 fprintf(fp, "%u", getpid());
1517 if (atexit(term) == -1) {
1518 syslog(LOG_ERR, "atexit failed: %m");
1519 (void)remove(pid_file);
1524 if (or_register(&oid_snmpMIB, "The MIB module for SNMPv2 entities.",
1526 syslog(LOG_ERR, "cannot register SNMPv2 MIB");
1529 if (or_register(&oid_begemotSnmpd, "The MIB module for the Begemot SNMPd.",
1531 syslog(LOG_ERR, "cannot register begemotSnmpd MIB");
1535 snmp_send_trap(&oid_coldStart, (struct snmp_value *)NULL);
1537 while ((m = TAILQ_FIRST(&modules_start)) != NULL) {
1538 m->flags &= ~LM_ONSTARTLIST;
1539 TAILQ_REMOVE(&modules_start, m, start);
1544 #ifndef USE_LIBBEGEMOT
1547 struct lmodule *mod;
1549 TAILQ_FOREACH(mod, &lmodules, link)
1550 if (mod->config->idle != NULL)
1551 (*mod->config->idle)();
1553 #ifndef USE_LIBBEGEMOT
1554 if (evGetNext(evctx, &event, EV_WAIT) == 0) {
1555 if (evDispatch(evctx, event))
1556 syslog(LOG_ERR, "evDispatch: %m");
1557 } else if (errno != EINTR) {
1558 syslog(LOG_ERR, "evGetNext: %m");
1567 if (work & WORK_DOINFO) {
1568 #ifdef USE_LIBBEGEMOT
1571 if (evWaitFor(evctx, &work, info_func,
1572 NULL, NULL) == -1) {
1573 syslog(LOG_ERR, "evWaitFor: %m");
1578 if (work & WORK_RECONFIG) {
1579 #ifdef USE_LIBBEGEMOT
1582 if (evWaitFor(evctx, &work, config_func,
1583 NULL, NULL) == -1) {
1584 syslog(LOG_ERR, "evWaitFor: %m");
1591 #ifndef USE_LIBBEGEMOT
1592 if (evDo(evctx, &work) == -1) {
1593 syslog(LOG_ERR, "evDo: %m");
1609 if (gettimeofday(&tv, NULL))
1611 ret = tv.tv_sec * 100ULL + tv.tv_usec / 10000ULL;
1620 * Trampoline for the non-repeatable timers.
1622 #ifdef USE_LIBBEGEMOT
1624 tfunc(int tid __unused, void *uap)
1627 tfunc(evContext ctx __unused, void *uap, struct timespec due __unused,
1628 struct timespec inter __unused)
1631 struct timer *tp = uap;
1633 LIST_REMOVE(tp, link);
1634 tp->func(tp->udata);
1639 * Trampoline for the repeatable timers.
1641 #ifdef USE_LIBBEGEMOT
1643 trfunc(int tid __unused, void *uap)
1646 trfunc(evContext ctx __unused, void *uap, struct timespec due __unused,
1647 struct timespec inter __unused)
1650 struct timer *tp = uap;
1652 tp->func(tp->udata);
1656 * Start a one-shot timer
1659 timer_start(u_int ticks, void (*func)(void *), void *udata, struct lmodule *mod)
1662 #ifndef USE_LIBBEGEMOT
1663 struct timespec due;
1666 if ((tp = malloc(sizeof(struct timer))) == NULL) {
1667 syslog(LOG_CRIT, "out of memory for timer");
1671 #ifndef USE_LIBBEGEMOT
1672 due = evAddTime(evNowTime(),
1673 evConsTime(ticks / 100, (ticks % 100) * 10000));
1680 LIST_INSERT_HEAD(&timer_list, tp, link);
1682 #ifdef USE_LIBBEGEMOT
1683 if ((tp->id = poll_start_timer(ticks * 10, 0, tfunc, tp)) < 0) {
1684 syslog(LOG_ERR, "cannot set timer: %m");
1688 if (evSetTimer(evctx, tfunc, tp, due, evConsTime(0, 0), &tp->id)
1690 syslog(LOG_ERR, "cannot set timer: %m");
1698 * Start a repeatable timer. When used with USE_LIBBEGEMOT the first argument
1699 * is currently ignored and the initial number of ticks is set to the
1700 * repeat number of ticks.
1703 timer_start_repeat(u_int ticks __unused, u_int repeat_ticks,
1704 void (*func)(void *), void *udata, struct lmodule *mod)
1707 #ifndef USE_LIBBEGEMOT
1708 struct timespec due;
1709 struct timespec inter;
1712 if ((tp = malloc(sizeof(struct timer))) == NULL) {
1713 syslog(LOG_CRIT, "out of memory for timer");
1717 #ifndef USE_LIBBEGEMOT
1718 due = evAddTime(evNowTime(),
1719 evConsTime(ticks / 100, (ticks % 100) * 10000));
1720 inter = evConsTime(repeat_ticks / 100, (repeat_ticks % 100) * 10000);
1727 LIST_INSERT_HEAD(&timer_list, tp, link);
1729 #ifdef USE_LIBBEGEMOT
1730 if ((tp->id = poll_start_timer(repeat_ticks * 10, 1, trfunc, tp)) < 0) {
1731 syslog(LOG_ERR, "cannot set timer: %m");
1735 if (evSetTimer(evctx, trfunc, tp, due, inter, &tp->id) == -1) {
1736 syslog(LOG_ERR, "cannot set timer: %m");
1749 struct timer *tp = p;
1751 LIST_REMOVE(tp, link);
1752 #ifdef USE_LIBBEGEMOT
1753 poll_stop_timer(tp->id);
1755 if (evClearTimer(evctx, tp->id) == -1) {
1756 syslog(LOG_ERR, "cannot stop timer: %m");
1764 timer_flush(struct lmodule *mod)
1766 struct timer *t, *t1;
1768 t = LIST_FIRST(&timer_list);
1770 t1 = LIST_NEXT(t, link);
1771 if (t->owner == mod)
1778 snmp_printf_func(const char *fmt, ...)
1781 static char *pend = NULL;
1785 vasprintf(&ret, fmt, ap);
1791 if ((new = realloc(pend, strlen(pend) + strlen(ret) + 1))
1802 while ((ret = strchr(pend, '\n')) != NULL) {
1804 syslog(LOG_DEBUG, "%s", pend);
1805 if (strlen(ret + 1) == 0) {
1810 strcpy(pend, ret + 1);
1815 snmp_error_func(const char *err, ...)
1820 if (!(snmp_trace & LOG_SNMP_ERRORS))
1824 snprintf(errbuf, sizeof(errbuf), "SNMP: ");
1825 vsnprintf(errbuf + strlen(errbuf),
1826 sizeof(errbuf) - strlen(errbuf), err, ap);
1829 syslog(LOG_ERR, "%s", errbuf);
1833 snmp_debug_func(const char *err, ...)
1839 snprintf(errbuf, sizeof(errbuf), "SNMP: ");
1840 vsnprintf(errbuf+strlen(errbuf), sizeof(errbuf)-strlen(errbuf),
1844 syslog(LOG_DEBUG, "%s", errbuf);
1848 asn_error_func(const struct asn_buf *b, const char *err, ...)
1854 if (!(snmp_trace & LOG_ASN1_ERRORS))
1858 snprintf(errbuf, sizeof(errbuf), "ASN.1: ");
1859 vsnprintf(errbuf + strlen(errbuf),
1860 sizeof(errbuf) - strlen(errbuf), err, ap);
1864 snprintf(errbuf + strlen(errbuf),
1865 sizeof(errbuf) - strlen(errbuf), " at");
1866 for (i = 0; b->asn_len > i; i++)
1867 snprintf(errbuf + strlen(errbuf),
1868 sizeof(errbuf) - strlen(errbuf),
1869 " %02x", b->asn_cptr[i]);
1872 syslog(LOG_ERR, "%s", errbuf);
1876 * Create a new community
1879 comm_define(u_int priv, const char *descr, struct lmodule *owner,
1882 struct community *c, *p;
1885 /* generate an identifier */
1887 if ((ncomm = next_community_index++) == UINT_MAX)
1888 next_community_index = 1;
1889 TAILQ_FOREACH(c, &community_list, link)
1890 if (c->value == ncomm)
1892 } while (c != NULL);
1894 if ((c = malloc(sizeof(struct community))) == NULL) {
1895 syslog(LOG_ERR, "comm_define: %m");
1905 if((c->string = malloc(strlen(str)+1)) == NULL) {
1909 strcpy(c->string, str);
1913 if (c->owner == NULL) {
1915 c->index.subs[0] = 0;
1917 c->index = c->owner->index;
1919 c->index.subs[c->index.len++] = c->private;
1924 TAILQ_FOREACH(p, &community_list, link) {
1925 if (asn_compare_oid(&p->index, &c->index) > 0) {
1926 TAILQ_INSERT_BEFORE(p, c, link);
1931 TAILQ_INSERT_TAIL(&community_list, c, link);
1936 comm_string(u_int ncomm)
1938 struct community *p;
1940 TAILQ_FOREACH(p, &community_list, link)
1941 if (p->value == ncomm)
1947 * Delete all communities allocated by a module
1950 comm_flush(struct lmodule *mod)
1952 struct community *p, *p1;
1954 p = TAILQ_FIRST(&community_list);
1956 p1 = TAILQ_NEXT(p, link);
1957 if (p->owner == mod) {
1959 TAILQ_REMOVE(&community_list, p, link);
1967 * Request ID handling.
1969 * Allocate a new range of request ids. Use a first fit algorithm.
1972 reqid_allocate(int size, struct lmodule *mod)
1975 struct idrange *r, *r1;
1977 if (size <= 0 || size > INT32_MAX) {
1978 syslog(LOG_CRIT, "%s: size out of range: %d", __func__, size);
1981 /* allocate a type id */
1983 if ((type = next_idrange++) == UINT_MAX)
1985 TAILQ_FOREACH(r, &idrange_list, link)
1986 if (r->type == type)
1991 if (TAILQ_EMPTY(&idrange_list))
1994 r = TAILQ_FIRST(&idrange_list);
1995 if (r->base < size) {
1996 while((r1 = TAILQ_NEXT(r, link)) != NULL) {
1997 if (r1->base - (r->base + r->size) >= size)
2004 r1 = TAILQ_LAST(&idrange_list, idrange_list);
2005 if (INT32_MAX - size + 1 < r1->base + r1->size) {
2006 syslog(LOG_ERR, "out of id ranges (%u)", size);
2012 /* allocate structure */
2013 if ((r1 = malloc(sizeof(struct idrange))) == NULL) {
2014 syslog(LOG_ERR, "%s: %m", __FUNCTION__);
2021 if (TAILQ_EMPTY(&idrange_list) || r == TAILQ_FIRST(&idrange_list)) {
2023 TAILQ_INSERT_HEAD(&idrange_list, r1, link);
2024 } else if (r == NULL) {
2025 r = TAILQ_LAST(&idrange_list, idrange_list);
2026 r1->base = r->base + r->size;
2027 TAILQ_INSERT_TAIL(&idrange_list, r1, link);
2029 r = TAILQ_PREV(r, idrange_list, link);
2030 r1->base = r->base + r->size;
2031 TAILQ_INSERT_AFTER(&idrange_list, r, r1, link);
2033 r1->next = r1->base;
2039 reqid_next(u_int type)
2044 TAILQ_FOREACH(r, &idrange_list, link)
2045 if (r->type == type)
2048 syslog(LOG_CRIT, "wrong idrange type");
2051 if ((id = r->next++) == r->base + (r->size - 1))
2057 reqid_base(u_int type)
2061 TAILQ_FOREACH(r, &idrange_list, link)
2062 if (r->type == type)
2064 syslog(LOG_CRIT, "wrong idrange type");
2069 reqid_type(int32_t reqid)
2073 TAILQ_FOREACH(r, &idrange_list, link)
2074 if (reqid >= r->base && reqid <= r->base + (r->size - 1))
2080 reqid_istype(int32_t reqid, u_int type)
2082 return (reqid_type(reqid) == type);
2086 * Delete all communities allocated by a module
2089 reqid_flush(struct lmodule *mod)
2091 struct idrange *p, *p1;
2093 p = TAILQ_FIRST(&idrange_list);
2095 p1 = TAILQ_NEXT(p, link);
2096 if (p->owner == mod) {
2097 TAILQ_REMOVE(&idrange_list, p, link);
2105 * Merge the given tree for the given module into the main tree.
2108 compare_node(const void *v1, const void *v2)
2110 const struct snmp_node *n1 = v1;
2111 const struct snmp_node *n2 = v2;
2113 return (asn_compare_oid(&n1->oid, &n2->oid));
2116 tree_merge(const struct snmp_node *ntree, u_int nsize, struct lmodule *mod)
2118 struct snmp_node *xtree;
2121 xtree = realloc(tree, sizeof(*tree) * (tree_size + nsize));
2122 if (xtree == NULL) {
2123 syslog(LOG_ERR, "tree_merge: %m");
2127 memcpy(&tree[tree_size], ntree, sizeof(*tree) * nsize);
2129 for (i = 0; i < nsize; i++)
2130 tree[tree_size + i].tree_data = mod;
2134 qsort(tree, tree_size, sizeof(tree[0]), compare_node);
2140 * Remove all nodes belonging to the loadable module
2143 tree_unmerge(struct lmodule *mod)
2147 for(s = d = 0; s < tree_size; s++)
2148 if (tree[s].tree_data != mod) {
2160 lm_load(const char *path, const char *section)
2165 char *av[MAX_MOD_ARGS + 1];
2169 if ((m = malloc(sizeof(*m))) == NULL) {
2170 syslog(LOG_ERR, "lm_load: %m");
2175 strcpy(m->section, section);
2177 if ((m->path = malloc(strlen(path) + 1)) == NULL) {
2178 syslog(LOG_ERR, "lm_load: %m");
2181 strcpy(m->path, path);
2186 m->index.subs[0] = strlen(section);
2187 m->index.len = m->index.subs[0] + 1;
2188 for (u = 0; u < m->index.subs[0]; u++)
2189 m->index.subs[u + 1] = section[u];
2192 * Load the object file and locate the config structure
2194 if ((m->handle = dlopen(m->path, RTLD_NOW|RTLD_GLOBAL)) == NULL) {
2195 syslog(LOG_ERR, "lm_load: open %s", dlerror());
2199 if ((m->config = dlsym(m->handle, "config")) == NULL) {
2200 syslog(LOG_ERR, "lm_load: no 'config' symbol %s", dlerror());
2205 * Insert it into the right place
2207 INSERT_OBJECT_OID(m, &lmodules);
2209 /* preserve order */
2210 if (community == COMM_INITIALIZE) {
2211 m->flags |= LM_ONSTARTLIST;
2212 TAILQ_INSERT_TAIL(&modules_start, m, start);
2216 * make the argument vector.
2219 for (i = 0; i < nprogargs; i++) {
2220 if (strlen(progargs[i]) >= strlen(section) + 1 &&
2221 strncmp(progargs[i], section, strlen(section)) == 0 &&
2222 progargs[i][strlen(section)] == ':') {
2223 if (ac == MAX_MOD_ARGS) {
2224 syslog(LOG_WARNING, "too many arguments for "
2225 "module '%s", section);
2228 av[ac++] = &progargs[i][strlen(section)+1];
2234 * Run the initialization function
2236 if ((err = (*m->config->init)(m, ac, av)) != 0) {
2237 syslog(LOG_ERR, "lm_load: init failed: %d", err);
2238 TAILQ_REMOVE(&lmodules, m, link);
2245 if ((m->flags & LM_ONSTARTLIST) != 0)
2246 TAILQ_REMOVE(&modules_start, m, start);
2258 lm_start(struct lmodule *mod)
2260 const struct lmodule *m;
2263 * Merge tree. If this fails, unload the module.
2265 if (tree_merge(mod->config->tree, mod->config->tree_size, mod)) {
2271 * Read configuration
2273 if (read_config(config_file, mod)) {
2274 syslog(LOG_ERR, "error in config file");
2278 if (mod->config->start)
2279 (*mod->config->start)();
2281 mod->flags |= LM_STARTED;
2284 * Inform other modules
2286 TAILQ_FOREACH(m, &lmodules, link)
2287 if (m->config->loading)
2288 (*m->config->loading)(mod, 1);
2296 lm_unload(struct lmodule *m)
2299 const struct lmodule *mod;
2301 TAILQ_REMOVE(&lmodules, m, link);
2302 if (m->flags & LM_ONSTARTLIST)
2303 TAILQ_REMOVE(&modules_start, m, start);
2306 if ((m->flags & LM_STARTED) && m->config->fini &&
2307 (err = (*m->config->fini)()) != 0)
2308 syslog(LOG_WARNING, "lm_unload(%s): fini %d", m->section, err);
2319 * Inform other modules
2321 TAILQ_FOREACH(mod, &lmodules, link)
2322 if (mod->config->loading)
2323 (*mod->config->loading)(m, 0);
2329 * Register an object resource and return the index (or 0 on failures)
2332 or_register(const struct asn_oid *or, const char *descr, struct lmodule *mod)
2334 struct objres *objres, *or1;
2337 /* find a free index */
2339 for (objres = TAILQ_FIRST(&objres_list);
2341 objres = TAILQ_NEXT(objres, link)) {
2342 if ((or1 = TAILQ_NEXT(objres, link)) == NULL ||
2343 or1->index > objres->index + 1) {
2344 idx = objres->index + 1;
2349 if ((objres = malloc(sizeof(*objres))) == NULL)
2352 objres->index = idx;
2354 strlcpy(objres->descr, descr, sizeof(objres->descr));
2355 objres->uptime = (uint32_t)(get_ticks() - start_tick);
2356 objres->module = mod;
2358 INSERT_OBJECT_INT(objres, &objres_list);
2360 systemg.or_last_change = objres->uptime;
2366 or_unregister(u_int idx)
2368 struct objres *objres;
2370 TAILQ_FOREACH(objres, &objres_list, link)
2371 if (objres->index == idx) {
2372 TAILQ_REMOVE(&objres_list, objres, link);