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>
48 #include "trans_lsock.h"
52 static const struct asn_oid
53 oid_begemotSnmpdLocalPortTable = OIDX_begemotSnmpdLocalPortTable;
55 static int lsock_start(void);
56 static int lsock_stop(int);
57 static void lsock_close_port(struct tport *);
58 static int lsock_init_port(struct tport *);
59 static ssize_t lsock_send(struct tport *, const u_char *, size_t,
60 const struct sockaddr *, size_t);
63 const struct transport_def lsock_trans = {
65 OIDX_begemotSnmpdTransLsock,
72 static struct transport *my_trans;
75 lsock_remove(struct tport *tp, intptr_t arg __unused)
77 struct lsock_port *port = (struct lsock_port *)tp;
79 (void)remove(port->name);
88 if (my_trans != NULL) {
89 if (!force && trans_first_port(my_trans) != NULL)
90 return (SNMP_ERR_GENERR);
91 trans_iter_port(my_trans, lsock_remove, 0);
92 return (trans_unregister(my_trans));
94 return (SNMP_ERR_NOERROR);
100 return (trans_register(&lsock_trans, &my_trans));
104 * Open a local port. If this is a datagram socket create also the
108 lsock_open_port(u_char *name, size_t namelen, struct lsock_port **pp,
111 struct lsock_port *port;
112 struct lsock_peer *peer = NULL;
113 int is_stream, need_cred;
116 struct sockaddr_un sa;
118 if (namelen == 0 || namelen + 1 > sizeof(sa.sun_path))
119 return (SNMP_ERR_BADVALUE);
122 case LOCP_DGRAM_UNPRIV:
127 case LOCP_DGRAM_PRIV:
132 case LOCP_STREAM_UNPRIV:
137 case LOCP_STREAM_PRIV:
143 return (SNMP_ERR_BADVALUE);
146 if ((port = calloc(1, sizeof(*port))) == NULL)
147 return (SNMP_ERR_GENERR);
150 if ((peer = calloc(1, sizeof(*peer))) == NULL) {
152 return (SNMP_ERR_GENERR);
155 if ((port->name = malloc(namelen + 1)) == NULL) {
159 return (SNMP_ERR_GENERR);
161 strncpy(port->name, name, namelen);
162 port->name[namelen] = '\0';
166 LIST_INIT(&port->peers);
168 port->tport.index.len = namelen + 1;
169 port->tport.index.subs[0] = namelen;
170 for (u = 0; u < namelen; u++)
171 port->tport.index.subs[u + 1] = name[u];
174 LIST_INSERT_HEAD(&port->peers, peer, link);
179 peer->input.id = NULL;
180 peer->input.stream = is_stream;
181 peer->input.cred = need_cred;
182 peer->input.peer = (struct sockaddr *)&peer->peer;
185 trans_insert_port(my_trans, &port->tport);
187 if (community != COMM_INITIALIZE &&
188 (err = lsock_init_port(&port->tport)) != SNMP_ERR_NOERROR) {
189 lsock_close_port(&port->tport);
195 return (SNMP_ERR_NOERROR);
199 * Close a local domain peer
202 lsock_peer_close(struct lsock_peer *peer)
205 LIST_REMOVE(peer, link);
206 snmpd_input_close(&peer->input);
214 lsock_close_port(struct tport *tp)
216 struct lsock_port *port = (struct lsock_port *)tp;
217 struct lsock_peer *peer;
219 if (port->str_id != NULL)
220 fd_deselect(port->str_id);
221 if (port->str_sock >= 0)
222 (void)close(port->str_sock);
223 (void)remove(port->name);
225 trans_remove_port(tp);
227 while ((peer = LIST_FIRST(&port->peers)) != NULL)
228 lsock_peer_close(peer);
235 * Input on a local socket (either datagram or stream)
238 lsock_input(int fd __unused, void *udata)
240 struct lsock_peer *peer = udata;
241 struct lsock_port *p = peer->port;
243 peer->input.peerlen = sizeof(peer->peer);
244 if (snmpd_input(&peer->input, &p->tport) == -1 && peer->input.stream)
245 /* framing or other input error */
246 lsock_peer_close(peer);
250 * A UNIX domain listening socket is ready. This means we have a peer
251 * that we need to accept
254 lsock_listen_input(int fd, void *udata)
256 struct lsock_port *p = udata;
257 struct lsock_peer *peer;
259 if ((peer = calloc(1, sizeof(*peer))) == NULL) {
260 syslog(LOG_WARNING, "%s: peer malloc failed", p->name);
261 (void)close(accept(fd, NULL, NULL));
267 peer->input.stream = 1;
268 peer->input.cred = (p->type == LOCP_DGRAM_PRIV ||
269 p->type == LOCP_STREAM_PRIV);
270 peer->input.peerlen = sizeof(peer->peer);
271 peer->input.peer = (struct sockaddr *)&peer->peer;
273 peer->input.fd = accept(fd, peer->input.peer, &peer->input.peerlen);
274 if (peer->input.fd == -1) {
275 syslog(LOG_WARNING, "%s: accept failed: %m", p->name);
280 if ((peer->input.id = fd_select(peer->input.fd, lsock_input,
281 peer, NULL)) == NULL) {
282 close(peer->input.fd);
287 LIST_INSERT_HEAD(&p->peers, peer, link);
291 * Create a local socket
294 lsock_init_port(struct tport *tp)
296 struct lsock_port *p = (struct lsock_port *)tp;
297 struct sockaddr_un sa;
299 if (p->type == LOCP_STREAM_PRIV || p->type == LOCP_STREAM_UNPRIV) {
300 if ((p->str_sock = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
301 syslog(LOG_ERR, "creating local socket: %m");
302 return (SNMP_ERR_RES_UNAVAIL);
305 strcpy(sa.sun_path, p->name);
306 sa.sun_family = AF_LOCAL;
307 sa.sun_len = strlen(p->name) +
308 offsetof(struct sockaddr_un, sun_path);
310 (void)remove(p->name);
312 if (bind(p->str_sock, (struct sockaddr *)&sa, sizeof(sa))) {
313 if (errno == EADDRNOTAVAIL) {
316 return (SNMP_ERR_INCONS_NAME);
318 syslog(LOG_ERR, "bind: %s %m", p->name);
321 return (SNMP_ERR_GENERR);
323 if (chmod(p->name, 0666) == -1)
324 syslog(LOG_WARNING, "chmod(%s,0666): %m", p->name);
326 if (listen(p->str_sock, 10) == -1) {
327 syslog(LOG_ERR, "listen: %s %m", p->name);
328 (void)remove(p->name);
331 return (SNMP_ERR_GENERR);
334 p->str_id = fd_select(p->str_sock, lsock_listen_input, p, NULL);
335 if (p->str_id == NULL) {
336 (void)remove(p->name);
339 return (SNMP_ERR_GENERR);
342 struct lsock_peer *peer;
345 peer = LIST_FIRST(&p->peers);
347 if ((peer->input.fd = socket(PF_LOCAL, SOCK_DGRAM, 0)) < 0) {
348 syslog(LOG_ERR, "creating local socket: %m");
349 return (SNMP_ERR_RES_UNAVAIL);
352 if (setsockopt(peer->input.fd, 0, LOCAL_CREDS, &on,
354 syslog(LOG_ERR, "setsockopt(LOCAL_CREDS): %m");
355 close(peer->input.fd);
357 return (SNMP_ERR_GENERR);
360 strcpy(sa.sun_path, p->name);
361 sa.sun_family = AF_LOCAL;
362 sa.sun_len = strlen(p->name) +
363 offsetof(struct sockaddr_un, sun_path);
365 (void)remove(p->name);
367 if (bind(peer->input.fd, (struct sockaddr *)&sa, sizeof(sa))) {
368 if (errno == EADDRNOTAVAIL) {
369 close(peer->input.fd);
371 return (SNMP_ERR_INCONS_NAME);
373 syslog(LOG_ERR, "bind: %s %m", p->name);
374 close(peer->input.fd);
376 return (SNMP_ERR_GENERR);
378 if (chmod(p->name, 0666) == -1)
379 syslog(LOG_WARNING, "chmod(%s,0666): %m", p->name);
381 peer->input.id = fd_select(peer->input.fd, lsock_input,
383 if (peer->input.id == NULL) {
384 (void)remove(p->name);
385 close(peer->input.fd);
387 return (SNMP_ERR_GENERR);
390 return (SNMP_ERR_NOERROR);
397 lsock_send(struct tport *tp, const u_char *buf, size_t len,
398 const struct sockaddr *addr, size_t addrlen)
400 struct lsock_port *p = (struct lsock_port *)tp;
401 struct lsock_peer *peer;
403 if (p->type == LOCP_DGRAM_PRIV || p->type == LOCP_DGRAM_UNPRIV) {
404 peer = LIST_FIRST(&p->peers);
407 /* search for the peer */
408 LIST_FOREACH(peer, &p->peers, link)
409 if (peer->input.peerlen == addrlen &&
410 memcmp(peer->input.peer, addr, addrlen) == 0)
418 return (sendto(peer->input.fd, buf, len, 0, addr, addrlen));
422 * Dependency to create a lsock port
425 struct snmp_dependency dep;
427 /* index (path name) */
432 struct lsock_port *port;
434 /* which of the fields are set */
437 /* type of the port */
444 #define LD_STATUS 0x02
445 #define LD_CREATE 0x04 /* rollback create */
446 #define LD_DELETE 0x08 /* rollback delete */
449 * dependency handler for lsock ports
452 lsock_func(struct snmp_context *ctx, struct snmp_dependency *dep,
455 struct lsock_dep *ld = (struct lsock_dep *)(void *)dep;
456 int err = SNMP_ERR_NOERROR;
460 case SNMP_DEPOP_COMMIT:
461 if (!(ld->set & LD_STATUS))
462 err = SNMP_ERR_BADVALUE;
463 else if (ld->port == NULL) {
465 err = SNMP_ERR_BADVALUE;
469 err = lsock_open_port(ld->path, ld->pathlen,
470 &ld->port, ld->type);
471 if (err == SNMP_ERR_NOERROR)
472 ld->set |= LD_CREATE;
474 } else if (!ld->status) {
475 /* delete - hard to roll back so defer to finalizer */
476 ld->set |= LD_DELETE;
478 /* modify - read-only */
479 err = SNMP_ERR_READONLY;
483 case SNMP_DEPOP_ROLLBACK:
484 if (ld->set & LD_CREATE) {
486 lsock_close_port(&ld->port->tport);
488 return (SNMP_ERR_NOERROR);
490 case SNMP_DEPOP_FINISH:
491 if ((ld->set & LD_DELETE) && ctx->code == SNMP_RET_OK)
492 lsock_close_port(&ld->port->tport);
494 return (SNMP_ERR_NOERROR);
503 op_lsock_port(struct snmp_context *ctx, struct snmp_value *value,
504 u_int sub, u_int iidx, enum snmp_op op)
506 asn_subid_t which = value->var.subs[sub-1];
507 struct lsock_port *p;
510 struct lsock_dep *ld;
515 case SNMP_OP_GETNEXT:
516 if ((p = (struct lsock_port *)trans_next_port(my_trans,
517 &value->var, sub)) == NULL)
518 return (SNMP_ERR_NOSUCHNAME);
519 index_append(&value->var, sub, &p->tport.index);
523 if ((p = (struct lsock_port *)trans_find_port(my_trans,
524 &value->var, sub)) == NULL)
525 return (SNMP_ERR_NOSUCHNAME);
529 p = (struct lsock_port *)trans_find_port(my_trans,
532 if (index_decode(&value->var, sub, iidx, &name, &namelen))
533 return (SNMP_ERR_NO_CREATION);
535 asn_slice_oid(&didx, &value->var, sub, value->var.len);
536 if ((ld = (struct lsock_dep *)(void *)snmp_dep_lookup(ctx,
537 &oid_begemotSnmpdLocalPortTable, &didx, sizeof(*ld),
538 lsock_func)) == NULL) {
540 return (SNMP_ERR_GENERR);
543 if (ld->path == NULL) {
545 ld->pathlen = namelen;
553 case LEAF_begemotSnmpdLocalPortStatus:
554 if (ld->set & LD_STATUS)
555 return (SNMP_ERR_INCONS_VALUE);
556 if (!TRUTH_OK(value->v.integer))
557 return (SNMP_ERR_WRONG_VALUE);
559 ld->status = TRUTH_GET(value->v.integer);
560 ld->set |= LD_STATUS;
563 case LEAF_begemotSnmpdLocalPortType:
564 if (ld->set & LD_TYPE)
565 return (SNMP_ERR_INCONS_VALUE);
566 if (value->v.integer < 1 || value->v.integer > 4)
567 return (SNMP_ERR_WRONG_VALUE);
569 ld->type = value->v.integer;
573 return (SNMP_ERR_NOERROR);
575 case SNMP_OP_ROLLBACK:
577 return (SNMP_ERR_NOERROR);
584 * Come here to fetch the value
588 case LEAF_begemotSnmpdLocalPortStatus:
589 value->v.integer = 1;
592 case LEAF_begemotSnmpdLocalPortType:
593 value->v.integer = p->type;
600 return (SNMP_ERR_NOERROR);