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);
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);
273 if ((se = getservbyname(AMANDA_SERVICE_NAME, "udp")) == NULL)
274 port = AMANDA_SERVICE_DEFAULT;
276 port = (in_port_t)ntohs(se->s_port);
277 amanda_gettimeofday(&sequence_time);
278 sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
280 g_snprintf(handle, 14, "000-%08x", (unsigned)newhandle++);
281 if (udp_inithandle(bh->udp, bh, canonname,
282 (sockaddr_union *)res_addr->ai_addr, port, handle, sequence) < 0) {
283 (*fn)(arg, &bh->sech, S_ERROR);
284 amfree(bh->hostname);
288 (*fn)(arg, &bh->sech, S_OK);
297 * Setup to accept new incoming connections
301 const struct security_driver * driver,
302 char *(*conf_fn)(char *, void *),
305 void (*fn)(security_handle_t *, pkt_t *),
309 assert(in >= 0 && out >= 0);
312 (void)out; /* Quiet unused parameter warning */
313 (void)driver; /* Quiet unused parameter warning */
318 * We assume in and out point to the same socket, and just use
321 dgram_socket(&netfd4.dgram, in);
322 dgram_socket(&netfd6.dgram, in);
325 * Assign the function and return. When they call recvpkt later,
326 * the recvpkt callback will call this function when it discovers
327 * new incoming connections
329 netfd4.accept_fn = fn;
330 netfd4.recv_security_ok = &bsd_recv_security_ok;
331 netfd4.prefix_packet = &bsd_prefix_packet;
332 netfd4.driver = &bsd_security_driver;
334 udp_addref(&netfd4, &udp_netfd_read_callback);
338 * Frees a handle allocated by the above
344 struct sec_handle *bh = cookie;
346 if(bh->proto_handle == NULL) {
350 auth_debug(1, _("bsd: close handle '%s'\n"), bh->proto_handle);
352 udp_recvpkt_cancel(bh);
354 bh->next->prev = bh->prev;
357 if (!not_init6 && netfd6.bh_last == bh)
358 netfd6.bh_last = bh->prev;
360 netfd4.bh_last = bh->prev;
363 bh->prev->next = bh->next;
366 if (!not_init6 && netfd6.bh_first == bh)
367 netfd6.bh_first = bh->next;
369 netfd4.bh_first = bh->next;
372 amfree(bh->proto_handle);
373 amfree(bh->hostname);
378 * Create the server end of a stream. For bsd, this means setup a tcp
379 * socket for receiving a connection.
385 struct sec_stream *bs = NULL;
386 struct sec_handle *bh = h;
390 bs = alloc(SIZEOF(*bs));
391 security_streaminit(&bs->secstr, &bsd_security_driver);
392 bs->socket = stream_server(SU_GET_FAMILY(&bh->udp->peer), &bs->port,
393 (size_t)STREAM_BUFSIZE, (size_t)STREAM_BUFSIZE,
395 if (bs->socket < 0) {
396 security_seterror(&bh->sech,
397 _("can't create server stream: %s"), strerror(errno));
407 * Accepts a new connection on unconnected streams. Assumes it is ok to
414 struct sec_stream *bs = s;
417 assert(bs->socket != -1);
420 bs->fd = stream_accept(bs->socket, 30, STREAM_BUFSIZE, STREAM_BUFSIZE);
422 security_stream_seterror(&bs->secstr,
423 _("can't accept new stream connection: %s"), strerror(errno));
430 * Return a connected stream
437 struct sec_stream *bs = NULL;
438 struct sec_handle *bh = h;
439 #ifdef DUMPER_SOCKET_BUFFERING
440 int rcvbuf = SIZEOF(bs->databuf) * 2;
445 bs = alloc(SIZEOF(*bs));
446 security_streaminit(&bs->secstr, &bsd_security_driver);
447 bs->fd = stream_client(bh->hostname, (in_port_t)id,
448 STREAM_BUFSIZE, STREAM_BUFSIZE, &bs->port, 0);
450 security_seterror(&bh->sech,
451 _("can't connect stream to %s port %d: %s"), bh->hostname,
452 id, strerror(errno));
456 bs->socket = -1; /* we're a client */
458 #ifdef DUMPER_SOCKET_BUFFERING
459 setsockopt(bs->fd, SOL_SOCKET, SO_RCVBUF, (void *)&rcvbuf, SIZEOF(rcvbuf));
465 * Close and unallocate resources for a stream
471 struct sec_stream *bs = s;
477 if (bs->socket != -1)
479 bsd_stream_read_cancel(bs);
484 * Authenticate a stream. bsd streams have no authentication
490 (void)s; /* Quiet unused parameter warning */
492 return (0); /* success */
496 * Returns the stream id for this stream. This is just the local port.
502 struct sec_stream *bs = s;
506 return ((int)bs->port);
510 * Submit a request to read some data. Calls back with the given function
511 * and arg when completed.
516 void (*fn)(void *, void *, ssize_t),
519 struct sec_stream *bs = s;
522 * Only one read request can be active per stream.
524 if (bs->ev_read != NULL)
525 event_release(bs->ev_read);
527 bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD, stream_read_callback, bs);
533 * Read a chunk of data to a stream. Blocks until completion.
536 bsd_stream_read_sync(
540 struct sec_stream *bs = s;
545 * Only one read request can be active per stream.
547 if(bs->ev_read != NULL) {
550 bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD,
551 stream_read_sync_callback, bs);
552 event_wait(bs->ev_read);
559 * Callback for bsd_stream_read_sync
562 stream_read_sync_callback(
565 struct sec_stream *bs = s;
570 auth_debug(1, _("bsd: stream_read_callback_sync: fd %d\n"), bs->fd);
573 * Remove the event first, in case they reschedule it in the callback.
575 bsd_stream_read_cancel(bs);
577 n = read(bs->fd, bs->databuf, sizeof(bs->databuf));
578 } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
580 security_stream_seterror(&bs->secstr, "%s", strerror(errno));
585 * Cancel a previous stream read request. It's ok if we didn't
586 * have a read scheduled.
589 bsd_stream_read_cancel(
592 struct sec_stream *bs = s;
595 if (bs->ev_read != NULL) {
596 event_release(bs->ev_read);
602 * Callback for bsd_stream_read
605 stream_read_callback(
608 struct sec_stream *bs = arg;
614 * Remove the event first, in case they reschedule it in the callback.
616 bsd_stream_read_cancel(bs);
618 n = read(bs->fd, bs->databuf, SIZEOF(bs->databuf));
619 } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
622 security_stream_seterror(&bs->secstr, "%s", strerror(errno));
624 (*bs->fn)(bs->arg, bs->databuf, n);