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 #ifdef BSDUDP_SECURITY
46 #undef DUMPER_SOCKET_BUFFERING
50 * Change the following from #undef to #define to cause detailed logging
51 * of the security steps, e.g. into /tmp/amanda/amandad*debug.
53 #undef SHOW_SECURITY_DETAIL
58 static void bsdudp_connect(const char *,
59 char *(*)(char *, void *),
60 void (*)(void *, security_handle_t *, security_status_t), void *, void *);
61 static void bsdudp_accept(const struct security_driver *, int, int, void (*)(security_handle_t *, pkt_t *));
62 static void bsdudp_close(void *);
65 * This is our interface to the outside world
67 const security_driver_t bsdudp_security_driver = {
83 tcpm_stream_read_sync,
84 tcpm_stream_read_cancel,
85 sec_close_connection_none,
91 * This is data local to the datagram socket. We have one datagram
92 * per process, so it is global.
94 static udp_handle_t netfd4;
95 static udp_handle_t netfd6;
96 static int not_init4 = 1;
97 static int not_init6 = 1;
99 /* generate new handles from here */
100 static unsigned int newhandle = 0;
103 * Setup and return a handle outgoing to a client
107 const char *hostname,
108 char * (*conf_fn)(char *, void *),
109 void (*fn)(void *, security_handle_t *, security_status_t),
113 struct sec_handle *bh;
116 struct timeval sequence_time;
117 amanda_timezone dontcare;
121 struct addrinfo hints;
122 struct addrinfo *res = NULL;
124 (void)conf_fn; /* Quiet unused parameter warning */
125 (void)datap; /* Quiet unused parameter warning */
126 assert(hostname != NULL);
128 bh = alloc(sizeof(*bh));
129 bh->proto_handle=NULL;
131 security_handleinit(&bh->sech, &bsdudp_security_driver);
134 hints.ai_flags = AI_CANONNAME | AI_V4MAPPED | AI_ALL;
135 hints.ai_family = AF_INET6;
137 hints.ai_flags = AI_CANONNAME;
138 hints.ai_family = AF_INET;
140 hints.ai_socktype = SOCK_DGRAM;
141 hints.ai_protocol = IPPROTO_UDP;
142 hints.ai_addrlen = 0;
143 hints.ai_addr = NULL;
144 hints.ai_canonname = NULL;
145 hints.ai_next = NULL;
146 result = getaddrinfo(hostname, NULL, &hints, &res);
149 hints.ai_flags = AI_CANONNAME;
150 hints.ai_family = AF_UNSPEC;
151 result = getaddrinfo(hostname, NULL, &hints, &res);
155 dbprintf(("getaddrinfo: %s\n", gai_strerror(result)));
156 security_seterror(&bh->sech, "getaddrinfo(%s): %s\n", hostname,
157 gai_strerror(result));
158 (*fn)(arg, &bh->sech, S_ERROR);
161 if (res->ai_canonname == NULL) {
162 dbprintf(("getaddrinfo(%s) did not return a canonical name\n", hostname));
163 security_seterror(&bh->sech,
164 _("getaddrinfo(%s) did not return a canonical name\n"), hostname);
165 (*fn)(arg, &bh->sech, S_ERROR);
170 * Only init the socket once
173 if (res->ai_addr->sa_family == AF_INET6 && not_init6 == 1) {
175 dgram_zero(&netfd6.dgram);
179 dgram_bind(&netfd6.dgram, res->ai_addr->sa_family, &port);
181 netfd6.handle = NULL;
182 netfd6.pkt.body = NULL;
183 netfd6.recv_security_ok = &bsd_recv_security_ok;
184 netfd6.prefix_packet = &bsd_prefix_packet;
186 * We must have a reserved port. Bomb if we didn't get one.
188 if (port >= IPPORT_RESERVED) {
189 security_seterror(&bh->sech,
190 "unable to bind to a reserved port (got port %u)",
192 (*fn)(arg, &bh->sech, S_ERROR);
200 if (res->ai_addr->sa_family == AF_INET && not_init4 == 1) {
202 dgram_zero(&netfd4.dgram);
206 dgram_bind(&netfd4.dgram, res->ai_addr->sa_family, &port);
208 netfd4.handle = NULL;
209 netfd4.pkt.body = NULL;
210 netfd4.recv_security_ok = &bsd_recv_security_ok;
211 netfd4.prefix_packet = &bsd_prefix_packet;
213 * We must have a reserved port. Bomb if we didn't get one.
215 if (port >= IPPORT_RESERVED) {
216 security_seterror(&bh->sech,
217 "unable to bind to a reserved port (got port %u)",
219 (*fn)(arg, &bh->sech, S_ERROR);
227 if (res->ai_addr->sa_family == AF_INET6)
233 auth_debug(1, ("Resolved hostname=%s\n", res->ai_canonname));
234 if ((se = getservbyname(AMANDA_SERVICE_NAME, "udp")) == NULL)
235 port = AMANDA_SERVICE_DEFAULT;
237 port = (in_port_t)ntohs(se->s_port);
238 amanda_gettimeofday(&sequence_time, &dontcare);
239 sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
241 snprintf(handle,14,"000-%08x", newhandle++);
242 if (udp_inithandle(bh->udp, bh, res->ai_canonname,
243 (struct sockaddr_storage *)res->ai_addr, port,
244 handle, sequence) < 0) {
245 (*fn)(arg, &bh->sech, S_ERROR);
246 amfree(bh->hostname);
249 (*fn)(arg, &bh->sech, S_OK);
257 * Setup to accept new incoming connections
261 const struct security_driver *driver,
264 void (*fn)(security_handle_t *, pkt_t *))
266 (void)driver; /* Quiet unused parameter warning */
267 (void)out; /* Quiet unused parameter warning */
269 assert(in >= 0 && out >= 0);
273 * We assume in and out point to the same socket, and just use
276 dgram_socket(&netfd4.dgram, in);
277 dgram_socket(&netfd6.dgram, in);
280 * Assign the function and return. When they call recvpkt later,
281 * the recvpkt callback will call this function when it discovers
282 * new incoming connections
284 netfd4.accept_fn = fn;
285 netfd4.recv_security_ok = &bsd_recv_security_ok;
286 netfd4.prefix_packet = &bsd_prefix_packet;
287 netfd4.driver = &bsdudp_security_driver;
290 udp_addref(&netfd4, &udp_netfd_read_callback);
294 * Frees a handle allocated by the above
300 struct sec_handle *bh = cookie;
302 if(bh->proto_handle == NULL) {
306 auth_debug(1, ("%s: bsdudp: close handle '%s'\n",
307 debug_prefix_time(NULL), bh->proto_handle));
309 udp_recvpkt_cancel(bh);
311 bh->next->prev = bh->prev;
314 if (!not_init6 && netfd6.bh_last == bh)
315 netfd6.bh_last = bh->prev;
317 netfd4.bh_last = bh->prev;
320 bh->prev->next = bh->next;
323 if (!not_init6 && netfd6.bh_first == bh)
324 netfd6.bh_first = bh->next;
326 netfd4.bh_first = bh->next;
329 amfree(bh->proto_handle);
330 amfree(bh->hostname);
334 #endif /* BSDUDP_SECURITY */ /* } */