2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1999 University of Maryland at College Park
4 * Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of U.M. not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. U.M. makes no representations about the
14 * suitability of this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
17 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * Authors: the Amanda Development Team. Its members are listed in a
25 * file named AUTHORS, in the root directory of this distribution.
28 * $Id: bsd-security.c,v 1.75 2006/07/19 17:41:14 martinea Exp $
30 * "BSD" security module
40 #include "security-util.h"
41 #include "sockaddr-util.h"
45 #undef DUMPER_SOCKET_BUFFERING
51 static void bsd_connect(const char *, char *(*)(char *, void *),
52 void (*)(void *, security_handle_t *, security_status_t),
54 static void bsd_accept(const struct security_driver *,
55 char *(*)(char *, void *),
57 void (*)(security_handle_t *, pkt_t *),
59 static void bsd_close(void *);
60 static void * bsd_stream_server(void *);
61 static int bsd_stream_accept(void *);
62 static void * bsd_stream_client(void *, int);
63 static void bsd_stream_close(void *);
64 static int bsd_stream_auth(void *);
65 static int bsd_stream_id(void *);
66 static void bsd_stream_read(void *, void (*)(void *, void *, ssize_t), void *);
67 static ssize_t bsd_stream_read_sync(void *, void **);
68 static void bsd_stream_read_cancel(void *);
71 * This is our interface to the outside world
73 const security_driver_t bsd_security_driver = {
77 sec_get_authenticated_peer_name_hostname,
91 bsd_stream_read_cancel,
92 sec_close_connection_none,
98 * This is data local to the datagram socket. We have one datagram
99 * per process, so it is global.
101 static udp_handle_t netfd4;
102 static udp_handle_t netfd6;
103 static int not_init4 = 1;
104 static int not_init6 = 1;
106 /* generate new handles from here */
107 static int newhandle = 0;
110 * These are the internal helper functions
112 static void stream_read_callback(void *);
113 static void stream_read_sync_callback(void *);
116 * Setup and return a handle outgoing to a client
121 const char * hostname,
122 char * (*conf_fn)(char *, void *),
123 void (*fn)(void *, security_handle_t *, security_status_t),
127 struct sec_handle *bh;
129 struct timeval sequence_time;
133 struct addrinfo *res, *res_addr;
138 assert(hostname != NULL);
140 (void)conf_fn; /* Quiet unused parameter warning */
141 (void)datap; /* Quiet unused parameter warning */
143 bh = g_new0(struct sec_handle, 1);
144 bh->proto_handle=NULL;
145 security_handleinit(&bh->sech, &bsd_security_driver);
147 result = resolve_hostname(hostname, SOCK_DGRAM, &res, &canonname);
149 dbprintf(_("resolve_hostname(%s): %s\n"), hostname, gai_strerror(result));
150 security_seterror(&bh->sech, _("resolve_hostname(%s): %s\n"), hostname,
151 gai_strerror(result));
152 (*fn)(arg, &bh->sech, S_ERROR);
155 if (canonname == NULL) {
156 dbprintf(_("resolve_hostname(%s) did not return a canonical name\n"), hostname);
157 security_seterror(&bh->sech,
158 _("resolve_hostname(%s) did not return a canonical name\n"), hostname);
159 (*fn)(arg, &bh->sech, S_ERROR);
160 if (res) freeaddrinfo(res);
164 dbprintf(_("resolve_hostname(%s): no results\n"), hostname);
165 security_seterror(&bh->sech,
166 _("resolve_hostname(%s): no results\n"), hostname);
167 (*fn)(arg, &bh->sech, S_ERROR);
172 for (res_addr = res; res_addr != NULL; res_addr = res_addr->ai_next) {
174 /* IPv6 socket already bound */
175 if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 0) {
179 * Only init the IPv6 socket once
181 if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 1) {
182 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) {
222 dgram_zero(&netfd4.dgram);
225 result_bind = dgram_bind(&netfd4.dgram,
226 res_addr->ai_addr->sa_family, &port);
228 if (result_bind != 0) {
231 netfd4.handle = NULL;
232 netfd4.pkt.body = NULL;
233 netfd4.recv_security_ok = &bsd_recv_security_ok;
234 netfd4.prefix_packet = &bsd_prefix_packet;
236 * We must have a reserved port. Bomb if we didn't get one.
238 if (port >= IPPORT_RESERVED) {
239 security_seterror(&bh->sech,
240 "unable to bind to a reserved port (got port %u)",
242 (*fn)(arg, &bh->sech, S_ERROR);
253 if (res_addr == NULL) {
254 dbprintf(_("Can't bind a socket to connect to %s\n"), hostname);
255 security_seterror(&bh->sech,
256 _("Can't bind a socket to connect to %s\n"), hostname);
257 (*fn)(arg, &bh->sech, S_ERROR);
263 if (res_addr->ai_addr->sa_family == AF_INET6)
269 auth_debug(1, _("Resolved hostname=%s\n"), canonname);
272 service = conf_fn("client_port", datap);
273 if (!service || strlen(service) <= 1)
278 port = find_port_for_service(service, "udp");
280 security_seterror(&bh->sech, _("%s/udp unknown protocol"), service);
281 (*fn)(arg, &bh->sech, S_ERROR);
286 amanda_gettimeofday(&sequence_time);
287 sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
289 g_snprintf(handle, 14, "000-%08x", (unsigned)newhandle++);
290 if (udp_inithandle(bh->udp, bh, canonname,
291 (sockaddr_union *)res_addr->ai_addr, port, handle, sequence) < 0) {
292 (*fn)(arg, &bh->sech, S_ERROR);
293 amfree(bh->hostname);
297 (*fn)(arg, &bh->sech, S_OK);
306 * Setup to accept new incoming connections
310 const struct security_driver * driver,
311 char *(*conf_fn)(char *, void *),
314 void (*fn)(security_handle_t *, pkt_t *),
319 assert(in >= 0 && out >= 0);
322 (void)out; /* Quiet unused parameter warning */
323 (void)driver; /* Quiet unused parameter warning */
328 * We assume in and out point to the same socket, and just use
331 dgram_socket(&netfd4.dgram, in);
332 dgram_socket(&netfd6.dgram, in);
335 * Assign the function and return. When they call recvpkt later,
336 * the recvpkt callback will call this function when it discovers
337 * new incoming connections
339 netfd4.accept_fn = fn;
340 netfd4.recv_security_ok = &bsd_recv_security_ok;
341 netfd4.prefix_packet = &bsd_prefix_packet;
342 netfd4.driver = &bsd_security_driver;
344 /* check if in is a socket */
346 if (S_ISSOCK(sbuf.st_mode)) {
347 udp_addref(&netfd4, &udp_netfd_read_callback);
349 g_warning("input file descriptor is not a socket; cannot use BSD auth");
354 * Frees a handle allocated by the above
360 struct sec_handle *bh = cookie;
362 if(bh->proto_handle == NULL) {
366 auth_debug(1, _("bsd: close handle '%s'\n"), bh->proto_handle);
368 udp_recvpkt_cancel(bh);
370 bh->next->prev = bh->prev;
373 if (!not_init6 && netfd6.bh_last == bh)
374 netfd6.bh_last = bh->prev;
376 netfd4.bh_last = bh->prev;
379 bh->prev->next = bh->next;
382 if (!not_init6 && netfd6.bh_first == bh)
383 netfd6.bh_first = bh->next;
385 netfd4.bh_first = bh->next;
388 amfree(bh->proto_handle);
389 amfree(bh->hostname);
394 * Create the server end of a stream. For bsd, this means setup a tcp
395 * socket for receiving a connection.
401 struct sec_stream *bs = NULL;
402 struct sec_handle *bh = h;
406 bs = g_new0(struct sec_stream, 1);
407 security_streaminit(&bs->secstr, &bsd_security_driver);
408 bs->socket = stream_server(SU_GET_FAMILY(&bh->udp->peer), &bs->port,
409 (size_t)STREAM_BUFSIZE, (size_t)STREAM_BUFSIZE,
411 if (bs->socket < 0) {
412 security_seterror(&bh->sech,
413 _("can't create server stream: %s"), strerror(errno));
423 * Accepts a new connection on unconnected streams. Assumes it is ok to
430 struct sec_stream *bs = s;
433 assert(bs->socket != -1);
436 bs->fd = stream_accept(bs->socket, 30, STREAM_BUFSIZE, STREAM_BUFSIZE);
438 security_stream_seterror(&bs->secstr,
439 _("can't accept new stream connection: %s"), strerror(errno));
446 * Return a connected stream
453 struct sec_stream *bs = NULL;
454 struct sec_handle *bh = h;
455 #ifdef DUMPER_SOCKET_BUFFERING
456 int rcvbuf = SIZEOF(bs->databuf) * 2;
461 bs = g_new0(struct sec_stream, 1);
462 security_streaminit(&bs->secstr, &bsd_security_driver);
463 bs->fd = stream_client(bh->hostname, (in_port_t)id,
464 STREAM_BUFSIZE, STREAM_BUFSIZE, &bs->port, 0);
466 security_seterror(&bh->sech,
467 _("can't connect stream to %s port %d: %s"), bh->hostname,
468 id, strerror(errno));
472 bs->socket = -1; /* we're a client */
474 #ifdef DUMPER_SOCKET_BUFFERING
475 setsockopt(bs->fd, SOL_SOCKET, SO_RCVBUF, (void *)&rcvbuf, SIZEOF(rcvbuf));
481 * Close and unallocate resources for a stream
487 struct sec_stream *bs = s;
493 if (bs->socket != -1)
495 bsd_stream_read_cancel(bs);
500 * Authenticate a stream. bsd streams have no authentication
506 (void)s; /* Quiet unused parameter warning */
508 return (0); /* success */
512 * Returns the stream id for this stream. This is just the local port.
518 struct sec_stream *bs = s;
522 return ((int)bs->port);
526 * Submit a request to read some data. Calls back with the given function
527 * and arg when completed.
532 void (*fn)(void *, void *, ssize_t),
535 struct sec_stream *bs = s;
538 * Only one read request can be active per stream.
540 if (bs->ev_read != NULL)
541 event_release(bs->ev_read);
543 bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD, stream_read_callback, bs);
548 /* buffer for bsd_stream_read_sync function */
549 static ssize_t sync_pktlen;
550 static void *sync_pkt;
553 * Read a chunk of data to a stream. Blocks until completion.
556 bsd_stream_read_sync(
560 struct sec_stream *bs = s;
565 * Only one read request can be active per stream.
567 if(bs->ev_read != NULL) {
572 bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD,
573 stream_read_sync_callback, bs);
574 event_wait(bs->ev_read);
576 return (sync_pktlen);
581 * Callback for bsd_stream_read_sync
584 stream_read_sync_callback(
587 struct sec_stream *bs = s;
592 auth_debug(1, _("bsd: stream_read_callback_sync: fd %d\n"), bs->fd);
595 * Remove the event first, in case they reschedule it in the callback.
597 bsd_stream_read_cancel(bs);
599 n = read(bs->fd, bs->databuf, sizeof(bs->databuf));
600 } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
602 security_stream_seterror(&bs->secstr, "%s", strerror(errno));
604 sync_pktlen = bs->len;
605 if (sync_pktlen > 0) {
606 sync_pkt = malloc(sync_pktlen);
607 memcpy(sync_pkt, bs->databuf, sync_pktlen);
614 * Cancel a previous stream read request. It's ok if we didn't
615 * have a read scheduled.
618 bsd_stream_read_cancel(
621 struct sec_stream *bs = s;
624 if (bs->ev_read != NULL) {
625 event_release(bs->ev_read);
631 * Callback for bsd_stream_read
634 stream_read_callback(
637 struct sec_stream *bs = arg;
643 n = read(bs->fd, bs->databuf, SIZEOF(bs->databuf));
644 } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
647 bsd_stream_read_cancel(bs);
649 security_stream_seterror(&bs->secstr, "%s", strerror(errno));
651 (*bs->fn)(bs->arg, bs->databuf, n);