2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1999 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: bsd-security.c,v 1.75 2006/07/19 17:41:14 martinea Exp $
29 * "BSD" security module
39 #include "security-util.h"
40 #include "sockaddr-util.h"
44 #undef DUMPER_SOCKET_BUFFERING
50 static void bsd_connect(const char *, char *(*)(char *, void *),
51 void (*)(void *, security_handle_t *, security_status_t),
53 static void bsd_accept(const struct security_driver *,
54 char *(*)(char *, void *),
56 void (*)(security_handle_t *, pkt_t *),
58 static void bsd_close(void *);
59 static void * bsd_stream_server(void *);
60 static int bsd_stream_accept(void *);
61 static void * bsd_stream_client(void *, int);
62 static void bsd_stream_close(void *);
63 static int bsd_stream_auth(void *);
64 static int bsd_stream_id(void *);
65 static void bsd_stream_read(void *, void (*)(void *, void *, ssize_t), void *);
66 static ssize_t bsd_stream_read_sync(void *, void **);
67 static void bsd_stream_read_cancel(void *);
70 * This is our interface to the outside world
72 const security_driver_t bsd_security_driver = {
89 bsd_stream_read_cancel,
90 sec_close_connection_none,
96 * This is data local to the datagram socket. We have one datagram
97 * per process, so it is global.
99 static udp_handle_t netfd4;
100 static udp_handle_t netfd6;
101 static int not_init4 = 1;
102 static int not_init6 = 1;
104 /* generate new handles from here */
105 static int newhandle = 0;
108 * These are the internal helper functions
110 static void stream_read_callback(void *);
111 static void stream_read_sync_callback(void *);
114 * Setup and return a handle outgoing to a client
119 const char * hostname,
120 char * (*conf_fn)(char *, void *),
121 void (*fn)(void *, security_handle_t *, security_status_t),
125 struct sec_handle *bh;
127 struct timeval sequence_time;
131 struct addrinfo *res, *res_addr;
136 assert(hostname != NULL);
138 (void)conf_fn; /* Quiet unused parameter warning */
139 (void)datap; /* Quiet unused parameter warning */
141 bh = alloc(SIZEOF(*bh));
142 bh->proto_handle=NULL;
143 security_handleinit(&bh->sech, &bsd_security_driver);
145 result = resolve_hostname(hostname, SOCK_DGRAM, &res, &canonname);
147 dbprintf(_("resolve_hostname(%s): %s\n"), hostname, gai_strerror(result));
148 security_seterror(&bh->sech, _("resolve_hostname(%s): %s\n"), hostname,
149 gai_strerror(result));
150 (*fn)(arg, &bh->sech, S_ERROR);
153 if (canonname == NULL) {
154 dbprintf(_("resolve_hostname(%s) did not return a canonical name\n"), hostname);
155 security_seterror(&bh->sech,
156 _("resolve_hostname(%s) did not return a canonical name\n"), hostname);
157 (*fn)(arg, &bh->sech, S_ERROR);
158 if (res) freeaddrinfo(res);
162 dbprintf(_("resolve_hostname(%s): no results\n"), hostname);
163 security_seterror(&bh->sech,
164 _("resolve_hostname(%s): no results\n"), hostname);
165 (*fn)(arg, &bh->sech, S_ERROR);
170 for (res_addr = res; res_addr != NULL; res_addr = res_addr->ai_next) {
172 /* IPv6 socket already bound */
173 if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 0) {
177 * Only init the IPv6 socket once
179 if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 1) {
181 dgram_zero(&netfd6.dgram);
185 result_bind = dgram_bind(&netfd6.dgram,
186 res_addr->ai_addr->sa_family, &port);
188 if (result_bind != 0) {
191 netfd6.handle = NULL;
192 netfd6.pkt.body = NULL;
193 netfd6.recv_security_ok = &bsd_recv_security_ok;
194 netfd6.prefix_packet = &bsd_prefix_packet;
196 * We must have a reserved port. Bomb if we didn't get one.
198 if (port >= IPPORT_RESERVED) {
199 security_seterror(&bh->sech,
200 _("unable to bind to a reserved port (got port %u)"),
202 (*fn)(arg, &bh->sech, S_ERROR);
213 /* IPv4 socket already bound */
214 if (res_addr->ai_addr->sa_family == AF_INET && not_init4 == 0) {
219 * Only init the IPv4 socket once
221 if (res_addr->ai_addr->sa_family == AF_INET && not_init4 == 1) {
223 dgram_zero(&netfd4.dgram);
227 result_bind = dgram_bind(&netfd4.dgram,
228 res_addr->ai_addr->sa_family, &port);
230 if (result_bind != 0) {
233 netfd4.handle = NULL;
234 netfd4.pkt.body = NULL;
235 netfd4.recv_security_ok = &bsd_recv_security_ok;
236 netfd4.prefix_packet = &bsd_prefix_packet;
238 * We must have a reserved port. Bomb if we didn't get one.
240 if (port >= IPPORT_RESERVED) {
241 security_seterror(&bh->sech,
242 "unable to bind to a reserved port (got port %u)",
244 (*fn)(arg, &bh->sech, S_ERROR);
255 if (res_addr == NULL) {
256 dbprintf(_("Can't bind a socket to connect to %s\n"), hostname);
257 security_seterror(&bh->sech,
258 _("Can't bind a socket to connect to %s\n"), hostname);
259 (*fn)(arg, &bh->sech, S_ERROR);
265 if (res_addr->ai_addr->sa_family == AF_INET6)
271 auth_debug(1, _("Resolved hostname=%s\n"), canonname);
274 service = conf_fn("client_port", datap);
275 if (!service || strlen(service) <= 1)
280 port = find_port_for_service(service, "udp");
282 security_seterror(&bh->sech, _("%s/udp unknown protocol"), service);
283 (*fn)(arg, &bh->sech, S_ERROR);
288 amanda_gettimeofday(&sequence_time);
289 sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
291 g_snprintf(handle, 14, "000-%08x", (unsigned)newhandle++);
292 if (udp_inithandle(bh->udp, bh, canonname,
293 (sockaddr_union *)res_addr->ai_addr, port, handle, sequence) < 0) {
294 (*fn)(arg, &bh->sech, S_ERROR);
295 amfree(bh->hostname);
299 (*fn)(arg, &bh->sech, S_OK);
308 * Setup to accept new incoming connections
312 const struct security_driver * driver,
313 char *(*conf_fn)(char *, void *),
316 void (*fn)(security_handle_t *, pkt_t *),
320 assert(in >= 0 && out >= 0);
323 (void)out; /* Quiet unused parameter warning */
324 (void)driver; /* Quiet unused parameter warning */
329 * We assume in and out point to the same socket, and just use
332 dgram_socket(&netfd4.dgram, in);
333 dgram_socket(&netfd6.dgram, in);
336 * Assign the function and return. When they call recvpkt later,
337 * the recvpkt callback will call this function when it discovers
338 * new incoming connections
340 netfd4.accept_fn = fn;
341 netfd4.recv_security_ok = &bsd_recv_security_ok;
342 netfd4.prefix_packet = &bsd_prefix_packet;
343 netfd4.driver = &bsd_security_driver;
345 udp_addref(&netfd4, &udp_netfd_read_callback);
349 * Frees a handle allocated by the above
355 struct sec_handle *bh = cookie;
357 if(bh->proto_handle == NULL) {
361 auth_debug(1, _("bsd: close handle '%s'\n"), bh->proto_handle);
363 udp_recvpkt_cancel(bh);
365 bh->next->prev = bh->prev;
368 if (!not_init6 && netfd6.bh_last == bh)
369 netfd6.bh_last = bh->prev;
371 netfd4.bh_last = bh->prev;
374 bh->prev->next = bh->next;
377 if (!not_init6 && netfd6.bh_first == bh)
378 netfd6.bh_first = bh->next;
380 netfd4.bh_first = bh->next;
383 amfree(bh->proto_handle);
384 amfree(bh->hostname);
389 * Create the server end of a stream. For bsd, this means setup a tcp
390 * socket for receiving a connection.
396 struct sec_stream *bs = NULL;
397 struct sec_handle *bh = h;
401 bs = alloc(SIZEOF(*bs));
402 security_streaminit(&bs->secstr, &bsd_security_driver);
403 bs->socket = stream_server(SU_GET_FAMILY(&bh->udp->peer), &bs->port,
404 (size_t)STREAM_BUFSIZE, (size_t)STREAM_BUFSIZE,
406 if (bs->socket < 0) {
407 security_seterror(&bh->sech,
408 _("can't create server stream: %s"), strerror(errno));
418 * Accepts a new connection on unconnected streams. Assumes it is ok to
425 struct sec_stream *bs = s;
428 assert(bs->socket != -1);
431 bs->fd = stream_accept(bs->socket, 30, STREAM_BUFSIZE, STREAM_BUFSIZE);
433 security_stream_seterror(&bs->secstr,
434 _("can't accept new stream connection: %s"), strerror(errno));
441 * Return a connected stream
448 struct sec_stream *bs = NULL;
449 struct sec_handle *bh = h;
450 #ifdef DUMPER_SOCKET_BUFFERING
451 int rcvbuf = SIZEOF(bs->databuf) * 2;
456 bs = alloc(SIZEOF(*bs));
457 security_streaminit(&bs->secstr, &bsd_security_driver);
458 bs->fd = stream_client(bh->hostname, (in_port_t)id,
459 STREAM_BUFSIZE, STREAM_BUFSIZE, &bs->port, 0);
461 security_seterror(&bh->sech,
462 _("can't connect stream to %s port %d: %s"), bh->hostname,
463 id, strerror(errno));
467 bs->socket = -1; /* we're a client */
469 #ifdef DUMPER_SOCKET_BUFFERING
470 setsockopt(bs->fd, SOL_SOCKET, SO_RCVBUF, (void *)&rcvbuf, SIZEOF(rcvbuf));
476 * Close and unallocate resources for a stream
482 struct sec_stream *bs = s;
488 if (bs->socket != -1)
490 bsd_stream_read_cancel(bs);
495 * Authenticate a stream. bsd streams have no authentication
501 (void)s; /* Quiet unused parameter warning */
503 return (0); /* success */
507 * Returns the stream id for this stream. This is just the local port.
513 struct sec_stream *bs = s;
517 return ((int)bs->port);
521 * Submit a request to read some data. Calls back with the given function
522 * and arg when completed.
527 void (*fn)(void *, void *, ssize_t),
530 struct sec_stream *bs = s;
533 * Only one read request can be active per stream.
535 if (bs->ev_read != NULL)
536 event_release(bs->ev_read);
538 bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD, stream_read_callback, bs);
543 /* buffer for bsd_stream_read_sync function */
544 static ssize_t sync_pktlen;
545 static void *sync_pkt;
548 * Read a chunk of data to a stream. Blocks until completion.
551 bsd_stream_read_sync(
555 struct sec_stream *bs = s;
560 * Only one read request can be active per stream.
562 if(bs->ev_read != NULL) {
567 bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD,
568 stream_read_sync_callback, bs);
569 event_wait(bs->ev_read);
571 return (sync_pktlen);
576 * Callback for bsd_stream_read_sync
579 stream_read_sync_callback(
582 struct sec_stream *bs = s;
587 auth_debug(1, _("bsd: stream_read_callback_sync: fd %d\n"), bs->fd);
590 * Remove the event first, in case they reschedule it in the callback.
592 bsd_stream_read_cancel(bs);
594 n = read(bs->fd, bs->databuf, sizeof(bs->databuf));
595 } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
597 security_stream_seterror(&bs->secstr, "%s", strerror(errno));
599 sync_pktlen = bs->len;
600 sync_pkt = malloc(sync_pktlen);
601 memcpy(sync_pkt, bs->databuf, sync_pktlen);
605 * Cancel a previous stream read request. It's ok if we didn't
606 * have a read scheduled.
609 bsd_stream_read_cancel(
612 struct sec_stream *bs = s;
615 if (bs->ev_read != NULL) {
616 event_release(bs->ev_read);
622 * Callback for bsd_stream_read
625 stream_read_callback(
628 struct sec_stream *bs = arg;
634 * Remove the event first, in case they reschedule it in the callback.
636 bsd_stream_read_cancel(bs);
638 n = read(bs->fd, bs->databuf, SIZEOF(bs->databuf));
639 } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
642 security_stream_seterror(&bs->secstr, "%s", strerror(errno));
644 (*bs->fn)(bs->arg, bs->databuf, n);