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"
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 = {
90 bsd_stream_read_cancel,
91 sec_close_connection_none,
97 * This is data local to the datagram socket. We have one datagram
98 * per process, so it is global.
100 static udp_handle_t netfd4;
101 static udp_handle_t netfd6;
102 static int not_init4 = 1;
103 static int not_init6 = 1;
105 /* generate new handles from here */
106 static int newhandle = 0;
109 * These are the internal helper functions
111 static void stream_read_callback(void *);
112 static void stream_read_sync_callback(void *);
115 * Setup and return a handle outgoing to a client
120 const char * hostname,
121 char * (*conf_fn)(char *, void *),
122 void (*fn)(void *, security_handle_t *, security_status_t),
126 struct sec_handle *bh;
129 struct timeval sequence_time;
133 struct addrinfo *res, *res_addr;
137 assert(hostname != NULL);
139 (void)conf_fn; /* Quiet unused parameter warning */
140 (void)datap; /* Quiet unused parameter warning */
142 bh = alloc(SIZEOF(*bh));
143 bh->proto_handle=NULL;
144 security_handleinit(&bh->sech, &bsd_security_driver);
146 result = resolve_hostname(hostname, SOCK_DGRAM, &res, &canonname);
148 dbprintf(_("resolve_hostname(%s): %s\n"), hostname, gai_strerror(result));
149 security_seterror(&bh->sech, _("resolve_hostname(%s): %s\n"), hostname,
150 gai_strerror(result));
151 (*fn)(arg, &bh->sech, S_ERROR);
154 if (canonname == NULL) {
155 dbprintf(_("resolve_hostname(%s) did not return a canonical name\n"), hostname);
156 security_seterror(&bh->sech,
157 _("resolve_hostname(%s) did not return a canonical name\n"), hostname);
158 (*fn)(arg, &bh->sech, S_ERROR);
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);
272 if ((se = getservbyname(AMANDA_SERVICE_NAME, "udp")) == NULL)
273 port = AMANDA_SERVICE_DEFAULT;
275 port = (in_port_t)ntohs(se->s_port);
276 amanda_gettimeofday(&sequence_time);
277 sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
279 g_snprintf(handle, 14, "000-%08x", (unsigned)newhandle++);
280 if (udp_inithandle(bh->udp, bh, canonname,
281 (sockaddr_union *)res_addr->ai_addr, port, handle, sequence) < 0) {
282 (*fn)(arg, &bh->sech, S_ERROR);
283 amfree(bh->hostname);
287 (*fn)(arg, &bh->sech, S_OK);
296 * Setup to accept new incoming connections
300 const struct security_driver * driver,
301 char *(*conf_fn)(char *, void *),
304 void (*fn)(security_handle_t *, pkt_t *),
308 assert(in >= 0 && out >= 0);
311 (void)out; /* Quiet unused parameter warning */
312 (void)driver; /* Quiet unused parameter warning */
317 * We assume in and out point to the same socket, and just use
320 dgram_socket(&netfd4.dgram, in);
321 dgram_socket(&netfd6.dgram, in);
324 * Assign the function and return. When they call recvpkt later,
325 * the recvpkt callback will call this function when it discovers
326 * new incoming connections
328 netfd4.accept_fn = fn;
329 netfd4.recv_security_ok = &bsd_recv_security_ok;
330 netfd4.prefix_packet = &bsd_prefix_packet;
331 netfd4.driver = &bsd_security_driver;
333 udp_addref(&netfd4, &udp_netfd_read_callback);
337 * Frees a handle allocated by the above
343 struct sec_handle *bh = cookie;
345 if(bh->proto_handle == NULL) {
349 auth_debug(1, _("bsd: close handle '%s'\n"), bh->proto_handle);
351 udp_recvpkt_cancel(bh);
353 bh->next->prev = bh->prev;
356 if (!not_init6 && netfd6.bh_last == bh)
357 netfd6.bh_last = bh->prev;
359 netfd4.bh_last = bh->prev;
362 bh->prev->next = bh->next;
365 if (!not_init6 && netfd6.bh_first == bh)
366 netfd6.bh_first = bh->next;
368 netfd4.bh_first = bh->next;
371 amfree(bh->proto_handle);
372 amfree(bh->hostname);
377 * Create the server end of a stream. For bsd, this means setup a tcp
378 * socket for receiving a connection.
384 struct sec_stream *bs = NULL;
385 struct sec_handle *bh = h;
389 bs = alloc(SIZEOF(*bs));
390 security_streaminit(&bs->secstr, &bsd_security_driver);
391 bs->socket = stream_server(SU_GET_FAMILY(&bh->udp->peer), &bs->port,
392 (size_t)STREAM_BUFSIZE, (size_t)STREAM_BUFSIZE,
394 if (bs->socket < 0) {
395 security_seterror(&bh->sech,
396 _("can't create server stream: %s"), strerror(errno));
406 * Accepts a new connection on unconnected streams. Assumes it is ok to
413 struct sec_stream *bs = s;
416 assert(bs->socket != -1);
419 bs->fd = stream_accept(bs->socket, 30, STREAM_BUFSIZE, STREAM_BUFSIZE);
421 security_stream_seterror(&bs->secstr,
422 _("can't accept new stream connection: %s"), strerror(errno));
429 * Return a connected stream
436 struct sec_stream *bs = NULL;
437 struct sec_handle *bh = h;
438 #ifdef DUMPER_SOCKET_BUFFERING
439 int rcvbuf = SIZEOF(bs->databuf) * 2;
444 bs = alloc(SIZEOF(*bs));
445 security_streaminit(&bs->secstr, &bsd_security_driver);
446 bs->fd = stream_client(bh->hostname, (in_port_t)id,
447 STREAM_BUFSIZE, STREAM_BUFSIZE, &bs->port, 0);
449 security_seterror(&bh->sech,
450 _("can't connect stream to %s port %d: %s"), bh->hostname,
451 id, strerror(errno));
455 bs->socket = -1; /* we're a client */
457 #ifdef DUMPER_SOCKET_BUFFERING
458 setsockopt(bs->fd, SOL_SOCKET, SO_RCVBUF, (void *)&rcvbuf, SIZEOF(rcvbuf));
464 * Close and unallocate resources for a stream
470 struct sec_stream *bs = s;
476 if (bs->socket != -1)
478 bsd_stream_read_cancel(bs);
483 * Authenticate a stream. bsd streams have no authentication
489 (void)s; /* Quiet unused parameter warning */
491 return (0); /* success */
495 * Returns the stream id for this stream. This is just the local port.
501 struct sec_stream *bs = s;
505 return ((int)bs->port);
509 * Submit a request to read some data. Calls back with the given function
510 * and arg when completed.
515 void (*fn)(void *, void *, ssize_t),
518 struct sec_stream *bs = s;
521 * Only one read request can be active per stream.
523 if (bs->ev_read != NULL)
524 event_release(bs->ev_read);
526 bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD, stream_read_callback, bs);
532 * Read a chunk of data to a stream. Blocks until completion.
535 bsd_stream_read_sync(
539 struct sec_stream *bs = s;
544 * Only one read request can be active per stream.
546 if(bs->ev_read != NULL) {
549 bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD,
550 stream_read_sync_callback, bs);
551 event_wait(bs->ev_read);
558 * Callback for bsd_stream_read_sync
561 stream_read_sync_callback(
564 struct sec_stream *bs = s;
569 auth_debug(1, _("bsd: stream_read_callback_sync: fd %d\n"), bs->fd);
572 * Remove the event first, in case they reschedule it in the callback.
574 bsd_stream_read_cancel(bs);
576 n = read(bs->fd, bs->databuf, sizeof(bs->databuf));
577 } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
579 security_stream_seterror(&bs->secstr, strerror(errno));
584 * Cancel a previous stream read request. It's ok if we didn't
585 * have a read scheduled.
588 bsd_stream_read_cancel(
591 struct sec_stream *bs = s;
594 if (bs->ev_read != NULL) {
595 event_release(bs->ev_read);
601 * Callback for bsd_stream_read
604 stream_read_callback(
607 struct sec_stream *bs = arg;
613 * Remove the event first, in case they reschedule it in the callback.
615 bsd_stream_read_cancel(bs);
617 n = read(bs->fd, bs->databuf, SIZEOF(bs->databuf));
618 } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
621 security_stream_seterror(&bs->secstr, strerror(errno));
623 (*bs->fn)(bs->arg, bs->databuf, n);