]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/bsnmp/snmpd/trans_lsock.c
MFC r311384:
[FreeBSD/stable/10.git] / contrib / bsnmp / snmpd / trans_lsock.c
1 /*
2  * Copyright (c) 2003
3  *      Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4  *      All rights reserved.
5  *
6  * Author: Harti Brandt <harti@freebsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $Begemot: bsnmp/snmpd/trans_lsock.c,v 1.6 2005/02/25 11:50:25 brandt_h Exp $
30  *
31  * Local domain socket transport
32  */
33 #include <sys/types.h>
34 #include <sys/queue.h>
35 #include <sys/stat.h>
36 #include <sys/un.h>
37
38 #include <errno.h>
39 #include <stddef.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <syslog.h>
44 #include <unistd.h>
45
46 #include "snmpmod.h"
47 #include "snmpd.h"
48 #include "trans_lsock.h"
49 #include "tree.h"
50 #include "oid.h"
51
52 static const struct asn_oid
53         oid_begemotSnmpdLocalPortTable = OIDX_begemotSnmpdLocalPortTable;
54
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);
61
62 /* exported */
63 const struct transport_def lsock_trans = {
64         "lsock",
65         OIDX_begemotSnmpdTransLsock,
66         lsock_start,
67         lsock_stop,
68         lsock_close_port,
69         lsock_init_port,
70         lsock_send
71 };
72 static struct transport *my_trans;
73
74 static int
75 lsock_remove(struct tport *tp, intptr_t arg __unused)
76 {
77         struct lsock_port *port = (struct lsock_port *)tp;
78
79         (void)remove(port->name);
80
81         return (-1);
82 }
83
84 static int
85 lsock_stop(int force)
86 {
87
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));
93         }
94         return (SNMP_ERR_NOERROR);
95 }
96
97 static int
98 lsock_start(void)
99 {
100         return (trans_register(&lsock_trans, &my_trans));
101 }
102
103 /*
104  * Open a local port. If this is a datagram socket create also the
105  * one and only peer.
106  */
107 static int
108 lsock_open_port(u_char *name, size_t namelen, struct lsock_port **pp,
109     int type)
110 {
111         struct lsock_port *port;
112         struct lsock_peer *peer = NULL;
113         int is_stream, need_cred;
114         size_t u;
115         int err;
116         struct sockaddr_un sa;
117
118         if (namelen == 0 || namelen + 1 > sizeof(sa.sun_path))
119                 return (SNMP_ERR_BADVALUE);
120
121         switch (type) {
122           case LOCP_DGRAM_UNPRIV:
123                 is_stream = 0;
124                 need_cred = 0;
125                 break;
126
127           case LOCP_DGRAM_PRIV:
128                 is_stream = 0;
129                 need_cred = 1;
130                 break;
131
132           case LOCP_STREAM_UNPRIV:
133                 is_stream = 1;
134                 need_cred = 0;
135                 break;
136
137           case LOCP_STREAM_PRIV:
138                 is_stream = 1;
139                 need_cred = 1;
140                 break;
141
142           default:
143                 return (SNMP_ERR_BADVALUE);
144         }
145
146         if ((port = calloc(1, sizeof(*port))) == NULL)
147                 return (SNMP_ERR_GENERR);
148
149         if (!is_stream) {
150                 if ((peer = calloc(1, sizeof(*peer))) == NULL) {
151                         free(port);
152                         return (SNMP_ERR_GENERR);
153                 }
154         }
155         if ((port->name = malloc(namelen + 1)) == NULL) {
156                 free(port);
157                 if (!is_stream)
158                         free(peer);
159                 return (SNMP_ERR_GENERR);
160         }
161         strncpy(port->name, name, namelen);
162         port->name[namelen] = '\0';
163
164         port->type = type;
165         port->str_sock = -1;
166         LIST_INIT(&port->peers);
167
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];
172
173         if (peer != NULL) {
174                 LIST_INSERT_HEAD(&port->peers, peer, link);
175
176                 peer->port = port;
177
178                 peer->input.fd = -1;
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;
183         }
184
185         trans_insert_port(my_trans, &port->tport);
186
187         if (community != COMM_INITIALIZE &&
188             (err = lsock_init_port(&port->tport)) != SNMP_ERR_NOERROR) {
189                 lsock_close_port(&port->tport);
190                 return (err);
191         }
192
193         *pp = port;
194
195         return (SNMP_ERR_NOERROR);
196 }
197
198 /*
199  * Close a local domain peer
200  */
201 static void
202 lsock_peer_close(struct lsock_peer *peer)
203 {
204
205         LIST_REMOVE(peer, link);
206         snmpd_input_close(&peer->input);
207         free(peer);
208 }
209
210 /*
211  * Close a local port
212  */
213 static void
214 lsock_close_port(struct tport *tp)
215 {
216         struct lsock_port *port = (struct lsock_port *)tp;
217         struct lsock_peer *peer;
218
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);
224
225         trans_remove_port(tp);
226
227         while ((peer = LIST_FIRST(&port->peers)) != NULL)
228                 lsock_peer_close(peer);
229
230         free(port->name);
231         free(port);
232 }
233
234 /*
235  * Input on a local socket (either datagram or stream)
236  */
237 static void
238 lsock_input(int fd __unused, void *udata)
239 {
240         struct lsock_peer *peer = udata;
241         struct lsock_port *p = peer->port;
242
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);
247 }
248
249 /*
250  * A UNIX domain listening socket is ready. This means we have a peer
251  * that we need to accept
252  */
253 static void
254 lsock_listen_input(int fd, void *udata)
255 {
256         struct lsock_port *p = udata;
257         struct lsock_peer *peer;
258
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));
262                 return;
263         }
264
265         peer->port = p;
266
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;
272
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);
276                 free(peer);
277                 return;
278         }
279
280         if ((peer->input.id = fd_select(peer->input.fd, lsock_input,
281             peer, NULL)) == NULL) {
282                 close(peer->input.fd);
283                 free(peer);
284                 return;
285         }
286
287         LIST_INSERT_HEAD(&p->peers, peer, link);
288 }
289
290 /*
291  * Create a local socket
292  */
293 static int
294 lsock_init_port(struct tport *tp)
295 {
296         struct lsock_port *p = (struct lsock_port *)tp;
297         struct sockaddr_un sa;
298
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);
303                 }
304
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);
309
310                 (void)remove(p->name);
311
312                 if (bind(p->str_sock, (struct sockaddr *)&sa, sizeof(sa))) {
313                         if (errno == EADDRNOTAVAIL) {
314                                 close(p->str_sock);
315                                 p->str_sock = -1;
316                                 return (SNMP_ERR_INCONS_NAME);
317                         }
318                         syslog(LOG_ERR, "bind: %s %m", p->name);
319                         close(p->str_sock);
320                         p->str_sock = -1;
321                         return (SNMP_ERR_GENERR);
322                 }
323                 if (chmod(p->name, 0666) == -1)
324                         syslog(LOG_WARNING, "chmod(%s,0666): %m", p->name);
325
326                 if (listen(p->str_sock, 10) == -1) {
327                         syslog(LOG_ERR, "listen: %s %m", p->name);
328                         (void)remove(p->name);
329                         close(p->str_sock);
330                         p->str_sock = -1;
331                         return (SNMP_ERR_GENERR);
332                 }
333
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);
337                         close(p->str_sock);
338                         p->str_sock = -1;
339                         return (SNMP_ERR_GENERR);
340                 }
341         } else {
342                 struct lsock_peer *peer;
343                 const int on = 1;
344
345                 peer = LIST_FIRST(&p->peers);
346
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);
350                 }
351
352                 if (setsockopt(peer->input.fd, 0, LOCAL_CREDS, &on,
353                     sizeof(on)) == -1) {
354                         syslog(LOG_ERR, "setsockopt(LOCAL_CREDS): %m");
355                         close(peer->input.fd);
356                         peer->input.fd = -1;
357                         return (SNMP_ERR_GENERR);
358                 }
359
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);
364
365                 (void)remove(p->name);
366
367                 if (bind(peer->input.fd, (struct sockaddr *)&sa, sizeof(sa))) {
368                         if (errno == EADDRNOTAVAIL) {
369                                 close(peer->input.fd);
370                                 peer->input.fd = -1;
371                                 return (SNMP_ERR_INCONS_NAME);
372                         }
373                         syslog(LOG_ERR, "bind: %s %m", p->name);
374                         close(peer->input.fd);
375                         peer->input.fd = -1;
376                         return (SNMP_ERR_GENERR);
377                 }
378                 if (chmod(p->name, 0666) == -1)
379                         syslog(LOG_WARNING, "chmod(%s,0666): %m", p->name);
380
381                 peer->input.id = fd_select(peer->input.fd, lsock_input,
382                     peer, NULL);
383                 if (peer->input.id == NULL) {
384                         (void)remove(p->name);
385                         close(peer->input.fd);
386                         peer->input.fd = -1;
387                         return (SNMP_ERR_GENERR);
388                 }
389         }
390         return (SNMP_ERR_NOERROR);
391 }
392
393 /*
394  * Send something
395  */
396 static ssize_t
397 lsock_send(struct tport *tp, const u_char *buf, size_t len,
398     const struct sockaddr *addr, size_t addrlen)
399 {
400         struct lsock_port *p = (struct lsock_port *)tp;
401         struct lsock_peer *peer;
402
403         if (p->type == LOCP_DGRAM_PRIV || p->type == LOCP_DGRAM_UNPRIV) {
404                 peer = LIST_FIRST(&p->peers);
405
406         } else {
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)
411                                 break;
412                 if (peer == NULL) {
413                         errno = ENOTCONN;
414                         return (-1);
415                 }
416         }
417
418         return (sendto(peer->input.fd, buf, len, 0, addr, addrlen));
419 }
420
421 /*
422  * Dependency to create a lsock port
423  */
424 struct lsock_dep {
425         struct snmp_dependency dep;
426
427         /* index (path name) */
428         u_char *path;
429         size_t pathlen;
430
431         /* the port */
432         struct lsock_port *port;
433
434         /* which of the fields are set */
435         u_int set;
436
437         /* type of the port */
438         int type;
439
440         /* status */
441         int status;
442 };
443 #define LD_TYPE         0x01
444 #define LD_STATUS       0x02
445 #define LD_CREATE       0x04    /* rollback create */
446 #define LD_DELETE       0x08    /* rollback delete */
447
448 /*
449  * dependency handler for lsock ports
450  */
451 static int
452 lsock_func(struct snmp_context *ctx, struct snmp_dependency *dep,
453     enum snmp_depop op)
454 {
455         struct lsock_dep *ld = (struct lsock_dep *)(void *)dep;
456         int err = SNMP_ERR_NOERROR;
457
458         switch (op) {
459
460           case SNMP_DEPOP_COMMIT:
461                 if (!(ld->set & LD_STATUS))
462                         err = SNMP_ERR_BADVALUE;
463                 else if (ld->port == NULL) {
464                         if (!ld->status)
465                                 err = SNMP_ERR_BADVALUE;
466
467                         else {
468                                 /* create */
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;
473                         }
474                 } else if (!ld->status) {
475                         /* delete - hard to roll back so defer to finalizer */
476                         ld->set |= LD_DELETE;
477                 } else
478                         /* modify - read-only */
479                         err = SNMP_ERR_READONLY;
480
481                 return (err);
482
483           case SNMP_DEPOP_ROLLBACK:
484                 if (ld->set & LD_CREATE) {
485                         /* was create */
486                         lsock_close_port(&ld->port->tport);
487                 }
488                 return (SNMP_ERR_NOERROR);
489
490           case SNMP_DEPOP_FINISH:
491                 if ((ld->set & LD_DELETE) && ctx->code == SNMP_RET_OK)
492                         lsock_close_port(&ld->port->tport);
493                 free(ld->path);
494                 return (SNMP_ERR_NOERROR);
495         }
496         abort();
497 }
498
499 /*
500  * Local port table
501  */
502 int
503 op_lsock_port(struct snmp_context *ctx, struct snmp_value *value,
504     u_int sub, u_int iidx, enum snmp_op op)
505 {
506         asn_subid_t which = value->var.subs[sub-1];
507         struct lsock_port *p;
508         u_char *name;
509         size_t namelen;
510         struct lsock_dep *ld;
511         struct asn_oid didx;
512
513         switch (op) {
514
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);
520                 break;
521
522           case SNMP_OP_GET:
523                 if ((p = (struct lsock_port *)trans_find_port(my_trans,
524                     &value->var, sub)) == NULL)
525                         return (SNMP_ERR_NOSUCHNAME);
526                 break;
527
528           case SNMP_OP_SET:
529                 p = (struct lsock_port *)trans_find_port(my_trans,
530                     &value->var, sub);
531
532                 if (index_decode(&value->var, sub, iidx, &name, &namelen))
533                         return (SNMP_ERR_NO_CREATION);
534
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) {
539                         free(name);
540                         return (SNMP_ERR_GENERR);
541                 }
542
543                 if (ld->path == NULL) {
544                         ld->path = name;
545                         ld->pathlen = namelen;
546                 } else {
547                         free(name);
548                 }
549                 ld->port = p;
550
551                 switch (which) {
552
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);
558
559                         ld->status = TRUTH_GET(value->v.integer);
560                         ld->set |= LD_STATUS;
561                         break;
562
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);
568
569                         ld->type = value->v.integer;
570                         ld->set |= LD_TYPE;
571                         break;
572                 }
573                 return (SNMP_ERR_NOERROR);
574
575           case SNMP_OP_ROLLBACK:
576           case SNMP_OP_COMMIT:
577                 return (SNMP_ERR_NOERROR);
578
579           default:
580                 abort();
581         }
582
583         /*
584          * Come here to fetch the value
585          */
586         switch (which) {
587
588           case LEAF_begemotSnmpdLocalPortStatus:
589                 value->v.integer = 1;
590                 break;
591
592           case LEAF_begemotSnmpdLocalPortType:
593                 value->v.integer = p->type;
594                 break;
595
596           default:
597                 abort();
598         }
599
600         return (SNMP_ERR_NOERROR);
601 }