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: bsdudp-security.c,v 1.7 2006/07/05 13:18:20 martinea Exp $
29 * "BSD" security module
39 #include "security-util.h"
43 #undef DUMPER_SOCKET_BUFFERING
49 static void bsdudp_connect(const char *,
50 char *(*)(char *, void *),
51 void (*)(void *, security_handle_t *, security_status_t), void *, void *);
52 static void bsdudp_accept(const struct security_driver *,
53 char *(*)(char *, void *),
55 void (*)(security_handle_t *, pkt_t *),
57 static void bsdudp_close(void *);
60 * This is our interface to the outside world
62 const security_driver_t bsdudp_security_driver = {
78 tcpm_stream_read_sync,
79 tcpm_stream_read_cancel,
80 sec_close_connection_none,
86 * This is data local to the datagram socket. We have one datagram
87 * per process, so it is global.
89 static udp_handle_t netfd4;
90 static udp_handle_t netfd6;
91 static int not_init4 = 1;
92 static int not_init6 = 1;
94 /* generate new handles from here */
95 static unsigned int newhandle = 0;
98 * Setup and return a handle outgoing to a client
102 const char *hostname,
103 char * (*conf_fn)(char *, void *),
104 void (*fn)(void *, security_handle_t *, security_status_t),
108 struct sec_handle *bh;
110 struct timeval sequence_time;
115 struct addrinfo *res = NULL, *res_addr;
119 (void)conf_fn; /* Quiet unused parameter warning */
120 (void)datap; /* Quiet unused parameter warning */
121 assert(hostname != NULL);
123 bh = alloc(sizeof(*bh));
124 bh->proto_handle=NULL;
126 security_handleinit(&bh->sech, &bsdudp_security_driver);
128 result = resolve_hostname(hostname, SOCK_DGRAM, &res, &canonname);
130 dbprintf(_("resolve_hostname(%s): %s\n"), hostname, gai_strerror(result));
131 security_seterror(&bh->sech, _("resolve_hostname(%s): %s\n"), hostname,
132 gai_strerror(result));
133 (*fn)(arg, &bh->sech, S_ERROR);
136 if (canonname == NULL) {
137 dbprintf(_("resolve_hostname(%s) did not return a canonical name\n"), hostname);
138 security_seterror(&bh->sech,
139 _("resolve_hostname(%s) did not return a canonical name\n"), hostname);
140 (*fn)(arg, &bh->sech, S_ERROR);
144 dbprintf(_("resolve_hostname(%s): no results\n"), hostname);
145 security_seterror(&bh->sech,
146 _("resolve_hostname(%s): no results\n"), hostname);
147 (*fn)(arg, &bh->sech, S_ERROR);
152 for (res_addr = res; res_addr != NULL; res_addr = res_addr->ai_next) {
154 /* IPv6 socket already bound */
155 if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 0) {
159 * Only init the IPv6 socket once
161 if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 1) {
163 dgram_zero(&netfd6.dgram);
167 result_bind = dgram_bind(&netfd6.dgram,
168 res_addr->ai_addr->sa_family, &port);
170 if (result_bind != 0) {
173 netfd6.handle = NULL;
174 netfd6.pkt.body = NULL;
175 netfd6.recv_security_ok = &bsd_recv_security_ok;
176 netfd6.prefix_packet = &bsd_prefix_packet;
178 * We must have a reserved port. Bomb if we didn't get one.
180 if (port >= IPPORT_RESERVED) {
181 security_seterror(&bh->sech,
182 _("unable to bind to a reserved port (got port %u)"),
184 (*fn)(arg, &bh->sech, S_ERROR);
195 /* IPv4 socket already bound */
196 if (res_addr->ai_addr->sa_family == AF_INET && not_init4 == 0) {
201 * Only init the IPv4 socket once
203 if (res_addr->ai_addr->sa_family == AF_INET && not_init4 == 1) {
205 dgram_zero(&netfd4.dgram);
209 result_bind = dgram_bind(&netfd4.dgram,
210 res_addr->ai_addr->sa_family, &port);
212 if (result_bind != 0) {
215 netfd4.handle = NULL;
216 netfd4.pkt.body = NULL;
217 netfd4.recv_security_ok = &bsd_recv_security_ok;
218 netfd4.prefix_packet = &bsd_prefix_packet;
220 * We must have a reserved port. Bomb if we didn't get one.
222 if (port >= IPPORT_RESERVED) {
223 security_seterror(&bh->sech,
224 "unable to bind to a reserved port (got port %u)",
226 (*fn)(arg, &bh->sech, S_ERROR);
237 if (res_addr == NULL) {
238 dbprintf(_("Can't bind a socket to connect to %s\n"), hostname);
239 security_seterror(&bh->sech,
240 _("Can't bind a socket to connect to %s\n"), hostname);
241 (*fn)(arg, &bh->sech, S_ERROR);
247 if (res_addr->ai_addr->sa_family == AF_INET6)
253 auth_debug(1, _("Resolved hostname=%s\n"), canonname);
255 service = conf_fn("client_port", datap);
256 if (!service || strlen(service) <= 1)
261 port = find_port_for_service(service, "udp");
263 security_seterror(&bh->sech, _("%s/udp unknown protocol"), service);
264 (*fn)(arg, &bh->sech, S_ERROR);
269 amanda_gettimeofday(&sequence_time);
270 sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
272 g_snprintf(handle,14,"000-%08x", newhandle++);
273 if (udp_inithandle(bh->udp, bh, canonname,
274 (sockaddr_union *)res_addr->ai_addr, port,
275 handle, sequence) < 0) {
276 (*fn)(arg, &bh->sech, S_ERROR);
277 amfree(bh->hostname);
280 (*fn)(arg, &bh->sech, S_OK);
285 if (res) freeaddrinfo(res);
289 * Setup to accept new incoming connections
293 const struct security_driver *driver,
294 char * (*conf_fn)(char *, void *),
297 void (*fn)(security_handle_t *, pkt_t *),
300 (void)driver; /* Quiet unused parameter warning */
301 (void)out; /* Quiet unused parameter warning */
305 assert(in >= 0 && out >= 0);
309 * We assume in and out point to the same socket, and just use
312 dgram_socket(&netfd4.dgram, in);
313 dgram_socket(&netfd6.dgram, in);
316 * Assign the function and return. When they call recvpkt later,
317 * the recvpkt callback will call this function when it discovers
318 * new incoming connections
320 netfd4.accept_fn = fn;
321 netfd4.recv_security_ok = &bsd_recv_security_ok;
322 netfd4.prefix_packet = &bsd_prefix_packet;
323 netfd4.driver = &bsdudp_security_driver;
326 udp_addref(&netfd4, &udp_netfd_read_callback);
330 * Frees a handle allocated by the above
336 struct sec_handle *bh = cookie;
338 if(bh->proto_handle == NULL) {
342 auth_debug(1, _("bsdudp: close handle '%s'\n"), bh->proto_handle);
344 udp_recvpkt_cancel(bh);
346 bh->next->prev = bh->prev;
349 if (!not_init6 && netfd6.bh_last == bh)
350 netfd6.bh_last = bh->prev;
352 netfd4.bh_last = bh->prev;
355 bh->prev->next = bh->next;
358 if (!not_init6 && netfd6.bh_first == bh)
359 netfd6.bh_first = bh->next;
361 netfd4.bh_first = bh->next;
364 amfree(bh->proto_handle);
365 amfree(bh->hostname);