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) {
182 dgram_zero(&netfd6.dgram);
186 result_bind = dgram_bind(&netfd6.dgram,
187 res_addr->ai_addr->sa_family, &port);
189 if (result_bind != 0) {
192 netfd6.handle = NULL;
193 netfd6.pkt.body = NULL;
194 netfd6.recv_security_ok = &bsd_recv_security_ok;
195 netfd6.prefix_packet = &bsd_prefix_packet;
197 * We must have a reserved port. Bomb if we didn't get one.
199 if (port >= IPPORT_RESERVED) {
200 security_seterror(&bh->sech,
201 _("unable to bind to a reserved port (got port %u)"),
203 (*fn)(arg, &bh->sech, S_ERROR);
214 /* IPv4 socket already bound */
215 if (res_addr->ai_addr->sa_family == AF_INET && not_init4 == 0) {
220 * Only init the IPv4 socket once
222 if (res_addr->ai_addr->sa_family == AF_INET && not_init4 == 1) {
224 dgram_zero(&netfd4.dgram);
228 result_bind = dgram_bind(&netfd4.dgram,
229 res_addr->ai_addr->sa_family, &port);
231 if (result_bind != 0) {
234 netfd4.handle = NULL;
235 netfd4.pkt.body = NULL;
236 netfd4.recv_security_ok = &bsd_recv_security_ok;
237 netfd4.prefix_packet = &bsd_prefix_packet;
239 * We must have a reserved port. Bomb if we didn't get one.
241 if (port >= IPPORT_RESERVED) {
242 security_seterror(&bh->sech,
243 "unable to bind to a reserved port (got port %u)",
245 (*fn)(arg, &bh->sech, S_ERROR);
256 if (res_addr == NULL) {
257 dbprintf(_("Can't bind a socket to connect to %s\n"), hostname);
258 security_seterror(&bh->sech,
259 _("Can't bind a socket to connect to %s\n"), hostname);
260 (*fn)(arg, &bh->sech, S_ERROR);
266 if (res_addr->ai_addr->sa_family == AF_INET6)
272 auth_debug(1, _("Resolved hostname=%s\n"), canonname);
275 service = conf_fn("client_port", datap);
276 if (!service || strlen(service) <= 1)
281 port = find_port_for_service(service, "udp");
283 security_seterror(&bh->sech, _("%s/udp unknown protocol"), service);
284 (*fn)(arg, &bh->sech, S_ERROR);
289 amanda_gettimeofday(&sequence_time);
290 sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
292 g_snprintf(handle, 14, "000-%08x", (unsigned)newhandle++);
293 if (udp_inithandle(bh->udp, bh, canonname,
294 (sockaddr_union *)res_addr->ai_addr, port, handle, sequence) < 0) {
295 (*fn)(arg, &bh->sech, S_ERROR);
296 amfree(bh->hostname);
300 (*fn)(arg, &bh->sech, S_OK);
309 * Setup to accept new incoming connections
313 const struct security_driver * driver,
314 char *(*conf_fn)(char *, void *),
317 void (*fn)(security_handle_t *, pkt_t *),
321 assert(in >= 0 && out >= 0);
324 (void)out; /* Quiet unused parameter warning */
325 (void)driver; /* Quiet unused parameter warning */
330 * We assume in and out point to the same socket, and just use
333 dgram_socket(&netfd4.dgram, in);
334 dgram_socket(&netfd6.dgram, in);
337 * Assign the function and return. When they call recvpkt later,
338 * the recvpkt callback will call this function when it discovers
339 * new incoming connections
341 netfd4.accept_fn = fn;
342 netfd4.recv_security_ok = &bsd_recv_security_ok;
343 netfd4.prefix_packet = &bsd_prefix_packet;
344 netfd4.driver = &bsd_security_driver;
346 udp_addref(&netfd4, &udp_netfd_read_callback);
350 * Frees a handle allocated by the above
356 struct sec_handle *bh = cookie;
358 if(bh->proto_handle == NULL) {
362 auth_debug(1, _("bsd: close handle '%s'\n"), bh->proto_handle);
364 udp_recvpkt_cancel(bh);
366 bh->next->prev = bh->prev;
369 if (!not_init6 && netfd6.bh_last == bh)
370 netfd6.bh_last = bh->prev;
372 netfd4.bh_last = bh->prev;
375 bh->prev->next = bh->next;
378 if (!not_init6 && netfd6.bh_first == bh)
379 netfd6.bh_first = bh->next;
381 netfd4.bh_first = bh->next;
384 amfree(bh->proto_handle);
385 amfree(bh->hostname);
390 * Create the server end of a stream. For bsd, this means setup a tcp
391 * socket for receiving a connection.
397 struct sec_stream *bs = NULL;
398 struct sec_handle *bh = h;
402 bs = g_new0(struct sec_stream, 1);
403 security_streaminit(&bs->secstr, &bsd_security_driver);
404 bs->socket = stream_server(SU_GET_FAMILY(&bh->udp->peer), &bs->port,
405 (size_t)STREAM_BUFSIZE, (size_t)STREAM_BUFSIZE,
407 if (bs->socket < 0) {
408 security_seterror(&bh->sech,
409 _("can't create server stream: %s"), strerror(errno));
419 * Accepts a new connection on unconnected streams. Assumes it is ok to
426 struct sec_stream *bs = s;
429 assert(bs->socket != -1);
432 bs->fd = stream_accept(bs->socket, 30, STREAM_BUFSIZE, STREAM_BUFSIZE);
434 security_stream_seterror(&bs->secstr,
435 _("can't accept new stream connection: %s"), strerror(errno));
442 * Return a connected stream
449 struct sec_stream *bs = NULL;
450 struct sec_handle *bh = h;
451 #ifdef DUMPER_SOCKET_BUFFERING
452 int rcvbuf = SIZEOF(bs->databuf) * 2;
457 bs = g_new0(struct sec_stream, 1);
458 security_streaminit(&bs->secstr, &bsd_security_driver);
459 bs->fd = stream_client(bh->hostname, (in_port_t)id,
460 STREAM_BUFSIZE, STREAM_BUFSIZE, &bs->port, 0);
462 security_seterror(&bh->sech,
463 _("can't connect stream to %s port %d: %s"), bh->hostname,
464 id, strerror(errno));
468 bs->socket = -1; /* we're a client */
470 #ifdef DUMPER_SOCKET_BUFFERING
471 setsockopt(bs->fd, SOL_SOCKET, SO_RCVBUF, (void *)&rcvbuf, SIZEOF(rcvbuf));
477 * Close and unallocate resources for a stream
483 struct sec_stream *bs = s;
489 if (bs->socket != -1)
491 bsd_stream_read_cancel(bs);
496 * Authenticate a stream. bsd streams have no authentication
502 (void)s; /* Quiet unused parameter warning */
504 return (0); /* success */
508 * Returns the stream id for this stream. This is just the local port.
514 struct sec_stream *bs = s;
518 return ((int)bs->port);
522 * Submit a request to read some data. Calls back with the given function
523 * and arg when completed.
528 void (*fn)(void *, void *, ssize_t),
531 struct sec_stream *bs = s;
534 * Only one read request can be active per stream.
536 if (bs->ev_read != NULL)
537 event_release(bs->ev_read);
539 bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD, stream_read_callback, bs);
544 /* buffer for bsd_stream_read_sync function */
545 static ssize_t sync_pktlen;
546 static void *sync_pkt;
549 * Read a chunk of data to a stream. Blocks until completion.
552 bsd_stream_read_sync(
556 struct sec_stream *bs = s;
561 * Only one read request can be active per stream.
563 if(bs->ev_read != NULL) {
568 bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD,
569 stream_read_sync_callback, bs);
570 event_wait(bs->ev_read);
572 return (sync_pktlen);
577 * Callback for bsd_stream_read_sync
580 stream_read_sync_callback(
583 struct sec_stream *bs = s;
588 auth_debug(1, _("bsd: stream_read_callback_sync: fd %d\n"), bs->fd);
591 * Remove the event first, in case they reschedule it in the callback.
593 bsd_stream_read_cancel(bs);
595 n = read(bs->fd, bs->databuf, sizeof(bs->databuf));
596 } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
598 security_stream_seterror(&bs->secstr, "%s", strerror(errno));
600 sync_pktlen = bs->len;
601 sync_pkt = malloc(sync_pktlen);
602 memcpy(sync_pkt, bs->databuf, sync_pktlen);
606 * Cancel a previous stream read request. It's ok if we didn't
607 * have a read scheduled.
610 bsd_stream_read_cancel(
613 struct sec_stream *bs = s;
616 if (bs->ev_read != NULL) {
617 event_release(bs->ev_read);
623 * Callback for bsd_stream_read
626 stream_read_callback(
629 struct sec_stream *bs = arg;
635 * Remove the event first, in case they reschedule it in the callback.
637 bsd_stream_read_cancel(bs);
639 n = read(bs->fd, bs->databuf, SIZEOF(bs->databuf));
640 } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
643 security_stream_seterror(&bs->secstr, "%s", strerror(errno));
645 (*bs->fn)(bs->arg, bs->databuf, n);