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/trans_lsock.c,v 1.6 2005/02/25 11:50:25 brandt_h Exp $
31 * Local domain socket transport
33 #include <sys/types.h>
34 #include <sys/queue.h>
36 #include <sys/ucred.h>
49 #include "trans_lsock.h"
53 static const struct asn_oid
54 oid_begemotSnmpdLocalPortTable = OIDX_begemotSnmpdLocalPortTable;
56 static int lsock_start(void);
57 static int lsock_stop(int);
58 static void lsock_close_port(struct tport *);
59 static int lsock_init_port(struct tport *);
60 static ssize_t lsock_send(struct tport *, const u_char *, size_t,
61 const struct sockaddr *, size_t);
62 static ssize_t lsock_recv(struct tport *, struct port_input *);
65 const struct transport_def lsock_trans = {
67 OIDX_begemotSnmpdTransLsock,
75 static struct transport *my_trans;
78 lsock_remove(struct tport *tp, intptr_t arg __unused)
80 struct lsock_port *port = (struct lsock_port *)tp;
82 (void)remove(port->name);
91 if (my_trans != NULL) {
92 if (!force && trans_first_port(my_trans) != NULL)
93 return (SNMP_ERR_GENERR);
94 trans_iter_port(my_trans, lsock_remove, 0);
95 return (trans_unregister(my_trans));
97 return (SNMP_ERR_NOERROR);
103 return (trans_register(&lsock_trans, &my_trans));
107 * Open a local port. If this is a datagram socket create also the
111 lsock_open_port(u_char *name, size_t namelen, struct lsock_port **pp,
114 struct lsock_port *port;
115 struct lsock_peer *peer = NULL;
116 int is_stream, need_cred;
119 struct sockaddr_un sa;
121 if (namelen == 0 || namelen + 1 > sizeof(sa.sun_path))
122 return (SNMP_ERR_BADVALUE);
125 case LOCP_DGRAM_UNPRIV:
130 case LOCP_DGRAM_PRIV:
135 case LOCP_STREAM_UNPRIV:
140 case LOCP_STREAM_PRIV:
146 return (SNMP_ERR_BADVALUE);
149 if ((port = calloc(1, sizeof(*port))) == NULL)
150 return (SNMP_ERR_GENERR);
153 if ((peer = calloc(1, sizeof(*peer))) == NULL) {
155 return (SNMP_ERR_GENERR);
158 if ((port->name = malloc(namelen + 1)) == NULL) {
162 return (SNMP_ERR_GENERR);
164 strncpy(port->name, name, namelen);
165 port->name[namelen] = '\0';
169 LIST_INIT(&port->peers);
171 port->tport.index.len = namelen + 1;
172 port->tport.index.subs[0] = namelen;
173 for (u = 0; u < namelen; u++)
174 port->tport.index.subs[u + 1] = name[u];
177 LIST_INSERT_HEAD(&port->peers, peer, link);
182 peer->input.id = NULL;
183 peer->input.stream = is_stream;
184 peer->input.cred = need_cred;
185 peer->input.peer = (struct sockaddr *)&peer->peer;
188 trans_insert_port(my_trans, &port->tport);
190 if (community != COMM_INITIALIZE &&
191 (err = lsock_init_port(&port->tport)) != SNMP_ERR_NOERROR) {
192 lsock_close_port(&port->tport);
198 return (SNMP_ERR_NOERROR);
202 * Close a local domain peer
205 lsock_peer_close(struct lsock_peer *peer)
208 LIST_REMOVE(peer, link);
209 snmpd_input_close(&peer->input);
217 lsock_close_port(struct tport *tp)
219 struct lsock_port *port = (struct lsock_port *)tp;
220 struct lsock_peer *peer;
222 if (port->str_id != NULL)
223 fd_deselect(port->str_id);
224 if (port->str_sock >= 0)
225 (void)close(port->str_sock);
226 (void)remove(port->name);
228 trans_remove_port(tp);
230 while ((peer = LIST_FIRST(&port->peers)) != NULL)
231 lsock_peer_close(peer);
238 * Input on a local socket (either datagram or stream)
241 lsock_input(int fd __unused, void *udata)
243 struct lsock_peer *peer = udata;
244 struct lsock_port *p = peer->port;
246 peer->input.peerlen = sizeof(peer->peer);
247 if (snmpd_input(&peer->input, &p->tport) == -1 && peer->input.stream)
248 /* framing or other input error */
249 lsock_peer_close(peer);
253 * A UNIX domain listening socket is ready. This means we have a peer
254 * that we need to accept
257 lsock_listen_input(int fd, void *udata)
259 struct lsock_port *p = udata;
260 struct lsock_peer *peer;
262 if ((peer = calloc(1, sizeof(*peer))) == NULL) {
263 syslog(LOG_WARNING, "%s: peer malloc failed", p->name);
264 (void)close(accept(fd, NULL, NULL));
270 peer->input.stream = 1;
271 peer->input.cred = (p->type == LOCP_DGRAM_PRIV ||
272 p->type == LOCP_STREAM_PRIV);
273 peer->input.peerlen = sizeof(peer->peer);
274 peer->input.peer = (struct sockaddr *)&peer->peer;
276 peer->input.fd = accept(fd, peer->input.peer, &peer->input.peerlen);
277 if (peer->input.fd == -1) {
278 syslog(LOG_WARNING, "%s: accept failed: %m", p->name);
283 if ((peer->input.id = fd_select(peer->input.fd, lsock_input,
284 peer, NULL)) == NULL) {
285 close(peer->input.fd);
290 LIST_INSERT_HEAD(&p->peers, peer, link);
294 * Create a local socket
297 lsock_init_port(struct tport *tp)
299 struct lsock_port *p = (struct lsock_port *)tp;
300 struct sockaddr_un sa;
302 if (p->type == LOCP_STREAM_PRIV || p->type == LOCP_STREAM_UNPRIV) {
303 if ((p->str_sock = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
304 syslog(LOG_ERR, "creating local socket: %m");
305 return (SNMP_ERR_RES_UNAVAIL);
308 strlcpy(sa.sun_path, p->name, sizeof(sa.sun_path));
309 sa.sun_family = AF_LOCAL;
310 sa.sun_len = SUN_LEN(&sa);
312 (void)remove(p->name);
314 if (bind(p->str_sock, (struct sockaddr *)&sa, sizeof(sa))) {
315 if (errno == EADDRNOTAVAIL) {
318 return (SNMP_ERR_INCONS_NAME);
320 syslog(LOG_ERR, "bind: %s %m", p->name);
323 return (SNMP_ERR_GENERR);
325 if (chmod(p->name, 0666) == -1)
326 syslog(LOG_WARNING, "chmod(%s,0666): %m", p->name);
328 if (listen(p->str_sock, 10) == -1) {
329 syslog(LOG_ERR, "listen: %s %m", p->name);
330 (void)remove(p->name);
333 return (SNMP_ERR_GENERR);
336 p->str_id = fd_select(p->str_sock, lsock_listen_input, p, NULL);
337 if (p->str_id == NULL) {
338 (void)remove(p->name);
341 return (SNMP_ERR_GENERR);
344 struct lsock_peer *peer;
347 peer = LIST_FIRST(&p->peers);
349 if ((peer->input.fd = socket(PF_LOCAL, SOCK_DGRAM, 0)) < 0) {
350 syslog(LOG_ERR, "creating local socket: %m");
351 return (SNMP_ERR_RES_UNAVAIL);
354 if (setsockopt(peer->input.fd, 0, LOCAL_CREDS, &on,
356 syslog(LOG_ERR, "setsockopt(LOCAL_CREDS): %m");
357 close(peer->input.fd);
359 return (SNMP_ERR_GENERR);
362 strlcpy(sa.sun_path, p->name, sizeof(sa.sun_path));
363 sa.sun_family = AF_LOCAL;
364 sa.sun_len = SUN_LEN(&sa);
366 (void)remove(p->name);
368 if (bind(peer->input.fd, (struct sockaddr *)&sa, sizeof(sa))) {
369 if (errno == EADDRNOTAVAIL) {
370 close(peer->input.fd);
372 return (SNMP_ERR_INCONS_NAME);
374 syslog(LOG_ERR, "bind: %s %m", p->name);
375 close(peer->input.fd);
377 return (SNMP_ERR_GENERR);
379 if (chmod(p->name, 0666) == -1)
380 syslog(LOG_WARNING, "chmod(%s,0666): %m", p->name);
382 peer->input.id = fd_select(peer->input.fd, lsock_input,
384 if (peer->input.id == NULL) {
385 (void)remove(p->name);
386 close(peer->input.fd);
388 return (SNMP_ERR_GENERR);
391 return (SNMP_ERR_NOERROR);
398 lsock_send(struct tport *tp, const u_char *buf, size_t len,
399 const struct sockaddr *addr, size_t addrlen)
401 struct lsock_port *p = (struct lsock_port *)tp;
402 struct lsock_peer *peer;
404 if (p->type == LOCP_DGRAM_PRIV || p->type == LOCP_DGRAM_UNPRIV) {
405 peer = LIST_FIRST(&p->peers);
408 /* search for the peer */
409 LIST_FOREACH(peer, &p->peers, link)
410 if (peer->input.peerlen == addrlen &&
411 memcmp(peer->input.peer, addr, addrlen) == 0)
419 return (sendto(peer->input.fd, buf, len, 0, addr, addrlen));
423 check_priv_stream(struct port_input *pi)
428 /* obtain the accept time credentials */
429 ucredlen = sizeof(ucred);
431 if (getsockopt(pi->fd, 0, LOCAL_PEERCRED, &ucred, &ucredlen) == 0 &&
432 ucredlen >= sizeof(ucred) && ucred.cr_version == XUCRED_VERSION)
433 pi->priv = (ucred.cr_uid == 0);
442 lsock_recv(struct tport *tp __unused, struct port_input *pi)
448 msg.msg_control = NULL;
449 msg.msg_controllen = 0;
451 if (pi->buf == NULL) {
452 /* no buffer yet - allocate one */
453 if ((pi->buf = buf_alloc(0)) == NULL) {
454 /* ups - could not get buffer. Return an error
455 * the caller must close the transport. */
458 pi->buflen = buf_size(0);
463 /* try to get a message */
464 msg.msg_name = pi->peer;
465 msg.msg_namelen = pi->peerlen;
468 msg.msg_control = NULL;
469 msg.msg_controllen = 0;
472 iov[0].iov_base = pi->buf + pi->length;
473 iov[0].iov_len = pi->buflen - pi->length;
475 len = recvmsg(pi->fd, &msg, 0);
477 if (len == -1 || len == 0)
484 check_priv_stream(pi);
490 * Dependency to create a lsock port
493 struct snmp_dependency dep;
495 /* index (path name) */
500 struct lsock_port *port;
502 /* which of the fields are set */
505 /* type of the port */
512 #define LD_STATUS 0x02
513 #define LD_CREATE 0x04 /* rollback create */
514 #define LD_DELETE 0x08 /* rollback delete */
517 * dependency handler for lsock ports
520 lsock_func(struct snmp_context *ctx, struct snmp_dependency *dep,
523 struct lsock_dep *ld = (struct lsock_dep *)(void *)dep;
524 int err = SNMP_ERR_NOERROR;
528 case SNMP_DEPOP_COMMIT:
529 if (!(ld->set & LD_STATUS))
530 err = SNMP_ERR_BADVALUE;
531 else if (ld->port == NULL) {
533 err = SNMP_ERR_BADVALUE;
537 err = lsock_open_port(ld->path, ld->pathlen,
538 &ld->port, ld->type);
539 if (err == SNMP_ERR_NOERROR)
540 ld->set |= LD_CREATE;
542 } else if (!ld->status) {
543 /* delete - hard to roll back so defer to finalizer */
544 ld->set |= LD_DELETE;
546 /* modify - read-only */
547 err = SNMP_ERR_READONLY;
551 case SNMP_DEPOP_ROLLBACK:
552 if (ld->set & LD_CREATE) {
554 lsock_close_port(&ld->port->tport);
556 return (SNMP_ERR_NOERROR);
558 case SNMP_DEPOP_FINISH:
559 if ((ld->set & LD_DELETE) && ctx->code == SNMP_RET_OK)
560 lsock_close_port(&ld->port->tport);
562 return (SNMP_ERR_NOERROR);
571 op_lsock_port(struct snmp_context *ctx, struct snmp_value *value,
572 u_int sub, u_int iidx, enum snmp_op op)
574 asn_subid_t which = value->var.subs[sub-1];
575 struct lsock_port *p;
578 struct lsock_dep *ld;
583 case SNMP_OP_GETNEXT:
584 if ((p = (struct lsock_port *)trans_next_port(my_trans,
585 &value->var, sub)) == NULL)
586 return (SNMP_ERR_NOSUCHNAME);
587 index_append(&value->var, sub, &p->tport.index);
591 if ((p = (struct lsock_port *)trans_find_port(my_trans,
592 &value->var, sub)) == NULL)
593 return (SNMP_ERR_NOSUCHNAME);
597 p = (struct lsock_port *)trans_find_port(my_trans,
600 if (index_decode(&value->var, sub, iidx, &name, &namelen))
601 return (SNMP_ERR_NO_CREATION);
603 asn_slice_oid(&didx, &value->var, sub, value->var.len);
604 if ((ld = (struct lsock_dep *)(void *)snmp_dep_lookup(ctx,
605 &oid_begemotSnmpdLocalPortTable, &didx, sizeof(*ld),
606 lsock_func)) == NULL) {
608 return (SNMP_ERR_GENERR);
611 if (ld->path == NULL) {
613 ld->pathlen = namelen;
621 case LEAF_begemotSnmpdLocalPortStatus:
622 if (ld->set & LD_STATUS)
623 return (SNMP_ERR_INCONS_VALUE);
624 if (!TRUTH_OK(value->v.integer))
625 return (SNMP_ERR_WRONG_VALUE);
627 ld->status = TRUTH_GET(value->v.integer);
628 ld->set |= LD_STATUS;
631 case LEAF_begemotSnmpdLocalPortType:
632 if (ld->set & LD_TYPE)
633 return (SNMP_ERR_INCONS_VALUE);
634 if (value->v.integer < 1 || value->v.integer > 4)
635 return (SNMP_ERR_WRONG_VALUE);
637 ld->type = value->v.integer;
641 return (SNMP_ERR_NOERROR);
643 case SNMP_OP_ROLLBACK:
645 return (SNMP_ERR_NOERROR);
652 * Come here to fetch the value
656 case LEAF_begemotSnmpdLocalPortStatus:
657 value->v.integer = 1;
660 case LEAF_begemotSnmpdLocalPortType:
661 value->v.integer = p->type;
668 return (SNMP_ERR_NOERROR);