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 = {
76 sec_get_authenticated_peer_name_hostname,
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;
128 struct timeval sequence_time;
132 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 = g_new0(struct sec_handle, 1);
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);
159 if (res) freeaddrinfo(res);
163 dbprintf(_("resolve_hostname(%s): no results\n"), hostname);
164 security_seterror(&bh->sech,
165 _("resolve_hostname(%s): no results\n"), hostname);
166 (*fn)(arg, &bh->sech, S_ERROR);
171 for (res_addr = res; res_addr != NULL; res_addr = res_addr->ai_next) {
173 /* IPv6 socket already bound */
174 if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 0) {
178 * Only init the IPv6 socket once
180 if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 1) {
181 dgram_zero(&netfd6.dgram);
184 result_bind = dgram_bind(&netfd6.dgram,
185 res_addr->ai_addr->sa_family, &port);
187 if (result_bind != 0) {
190 netfd6.handle = NULL;
191 netfd6.pkt.body = NULL;
192 netfd6.recv_security_ok = &bsd_recv_security_ok;
193 netfd6.prefix_packet = &bsd_prefix_packet;
195 * We must have a reserved port. Bomb if we didn't get one.
197 if (port >= IPPORT_RESERVED) {
198 security_seterror(&bh->sech,
199 _("unable to bind to a reserved port (got port %u)"),
201 (*fn)(arg, &bh->sech, S_ERROR);
212 /* IPv4 socket already bound */
213 if (res_addr->ai_addr->sa_family == AF_INET && not_init4 == 0) {
218 * Only init the IPv4 socket once
220 if (res_addr->ai_addr->sa_family == AF_INET && not_init4 == 1) {
221 dgram_zero(&netfd4.dgram);
224 result_bind = dgram_bind(&netfd4.dgram,
225 res_addr->ai_addr->sa_family, &port);
227 if (result_bind != 0) {
230 netfd4.handle = NULL;
231 netfd4.pkt.body = NULL;
232 netfd4.recv_security_ok = &bsd_recv_security_ok;
233 netfd4.prefix_packet = &bsd_prefix_packet;
235 * We must have a reserved port. Bomb if we didn't get one.
237 if (port >= IPPORT_RESERVED) {
238 security_seterror(&bh->sech,
239 "unable to bind to a reserved port (got port %u)",
241 (*fn)(arg, &bh->sech, S_ERROR);
252 if (res_addr == NULL) {
253 dbprintf(_("Can't bind a socket to connect to %s\n"), hostname);
254 security_seterror(&bh->sech,
255 _("Can't bind a socket to connect to %s\n"), hostname);
256 (*fn)(arg, &bh->sech, S_ERROR);
262 if (res_addr->ai_addr->sa_family == AF_INET6)
268 auth_debug(1, _("Resolved hostname=%s\n"), canonname);
271 service = conf_fn("client_port", datap);
272 if (!service || strlen(service) <= 1)
277 port = find_port_for_service(service, "udp");
279 security_seterror(&bh->sech, _("%s/udp unknown protocol"), service);
280 (*fn)(arg, &bh->sech, S_ERROR);
285 amanda_gettimeofday(&sequence_time);
286 sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
288 g_snprintf(handle, 14, "000-%08x", (unsigned)newhandle++);
289 if (udp_inithandle(bh->udp, bh, canonname,
290 (sockaddr_union *)res_addr->ai_addr, port, handle, sequence) < 0) {
291 (*fn)(arg, &bh->sech, S_ERROR);
292 amfree(bh->hostname);
296 (*fn)(arg, &bh->sech, S_OK);
305 * Setup to accept new incoming connections
309 const struct security_driver * driver,
310 char *(*conf_fn)(char *, void *),
313 void (*fn)(security_handle_t *, pkt_t *),
318 assert(in >= 0 && out >= 0);
321 (void)out; /* Quiet unused parameter warning */
322 (void)driver; /* Quiet unused parameter warning */
327 * We assume in and out point to the same socket, and just use
330 dgram_socket(&netfd4.dgram, in);
331 dgram_socket(&netfd6.dgram, in);
334 * Assign the function and return. When they call recvpkt later,
335 * the recvpkt callback will call this function when it discovers
336 * new incoming connections
338 netfd4.accept_fn = fn;
339 netfd4.recv_security_ok = &bsd_recv_security_ok;
340 netfd4.prefix_packet = &bsd_prefix_packet;
341 netfd4.driver = &bsd_security_driver;
343 /* check if in is a socket */
345 if (S_ISSOCK(sbuf.st_mode)) {
346 udp_addref(&netfd4, &udp_netfd_read_callback);
348 g_warning("input file descriptor is not a socket; cannot use BSD auth");
353 * Frees a handle allocated by the above
359 struct sec_handle *bh = cookie;
361 if(bh->proto_handle == NULL) {
365 auth_debug(1, _("bsd: close handle '%s'\n"), bh->proto_handle);
367 udp_recvpkt_cancel(bh);
369 bh->next->prev = bh->prev;
372 if (!not_init6 && netfd6.bh_last == bh)
373 netfd6.bh_last = bh->prev;
375 netfd4.bh_last = bh->prev;
378 bh->prev->next = bh->next;
381 if (!not_init6 && netfd6.bh_first == bh)
382 netfd6.bh_first = bh->next;
384 netfd4.bh_first = bh->next;
387 amfree(bh->proto_handle);
388 amfree(bh->hostname);
393 * Create the server end of a stream. For bsd, this means setup a tcp
394 * socket for receiving a connection.
400 struct sec_stream *bs = NULL;
401 struct sec_handle *bh = h;
405 bs = g_new0(struct sec_stream, 1);
406 security_streaminit(&bs->secstr, &bsd_security_driver);
407 bs->socket = stream_server(SU_GET_FAMILY(&bh->udp->peer), &bs->port,
408 (size_t)STREAM_BUFSIZE, (size_t)STREAM_BUFSIZE,
410 if (bs->socket < 0) {
411 security_seterror(&bh->sech,
412 _("can't create server stream: %s"), strerror(errno));
422 * Accepts a new connection on unconnected streams. Assumes it is ok to
429 struct sec_stream *bs = s;
432 assert(bs->socket != -1);
435 bs->fd = stream_accept(bs->socket, 30, STREAM_BUFSIZE, STREAM_BUFSIZE);
437 security_stream_seterror(&bs->secstr,
438 _("can't accept new stream connection: %s"), strerror(errno));
445 * Return a connected stream
452 struct sec_stream *bs = NULL;
453 struct sec_handle *bh = h;
454 #ifdef DUMPER_SOCKET_BUFFERING
455 int rcvbuf = SIZEOF(bs->databuf) * 2;
460 bs = g_new0(struct sec_stream, 1);
461 security_streaminit(&bs->secstr, &bsd_security_driver);
462 bs->fd = stream_client(bh->hostname, (in_port_t)id,
463 STREAM_BUFSIZE, STREAM_BUFSIZE, &bs->port, 0);
465 security_seterror(&bh->sech,
466 _("can't connect stream to %s port %d: %s"), bh->hostname,
467 id, strerror(errno));
471 bs->socket = -1; /* we're a client */
473 #ifdef DUMPER_SOCKET_BUFFERING
474 setsockopt(bs->fd, SOL_SOCKET, SO_RCVBUF, (void *)&rcvbuf, SIZEOF(rcvbuf));
480 * Close and unallocate resources for a stream
486 struct sec_stream *bs = s;
492 if (bs->socket != -1)
494 bsd_stream_read_cancel(bs);
499 * Authenticate a stream. bsd streams have no authentication
505 (void)s; /* Quiet unused parameter warning */
507 return (0); /* success */
511 * Returns the stream id for this stream. This is just the local port.
517 struct sec_stream *bs = s;
521 return ((int)bs->port);
525 * Submit a request to read some data. Calls back with the given function
526 * and arg when completed.
531 void (*fn)(void *, void *, ssize_t),
534 struct sec_stream *bs = s;
537 * Only one read request can be active per stream.
539 if (bs->ev_read != NULL)
540 event_release(bs->ev_read);
542 bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD, stream_read_callback, bs);
547 /* buffer for bsd_stream_read_sync function */
548 static ssize_t sync_pktlen;
549 static void *sync_pkt;
552 * Read a chunk of data to a stream. Blocks until completion.
555 bsd_stream_read_sync(
559 struct sec_stream *bs = s;
564 * Only one read request can be active per stream.
566 if(bs->ev_read != NULL) {
571 bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD,
572 stream_read_sync_callback, bs);
573 event_wait(bs->ev_read);
575 return (sync_pktlen);
580 * Callback for bsd_stream_read_sync
583 stream_read_sync_callback(
586 struct sec_stream *bs = s;
591 auth_debug(1, _("bsd: stream_read_callback_sync: fd %d\n"), bs->fd);
594 * Remove the event first, in case they reschedule it in the callback.
596 bsd_stream_read_cancel(bs);
598 n = read(bs->fd, bs->databuf, sizeof(bs->databuf));
599 } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
601 security_stream_seterror(&bs->secstr, "%s", strerror(errno));
603 sync_pktlen = bs->len;
604 sync_pkt = malloc(sync_pktlen);
605 memcpy(sync_pkt, bs->databuf, sync_pktlen);
609 * Cancel a previous stream read request. It's ok if we didn't
610 * have a read scheduled.
613 bsd_stream_read_cancel(
616 struct sec_stream *bs = s;
619 if (bs->ev_read != NULL) {
620 event_release(bs->ev_read);
626 * Callback for bsd_stream_read
629 stream_read_callback(
632 struct sec_stream *bs = arg;
638 * Remove the event first, in case they reschedule it in the callback.
640 bsd_stream_read_cancel(bs);
642 n = read(bs->fd, bs->databuf, SIZEOF(bs->databuf));
643 } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
646 security_stream_seterror(&bs->secstr, "%s", strerror(errno));
648 (*bs->fn)(bs->arg, bs->databuf, n);