cd265b3b47b5053e79c6e09bdbfce0bfab347bea
[debian/amanda] / common-src / bsdudp-security.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1999 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: bsdudp-security.c,v 1.7 2006/07/05 13:18:20 martinea Exp $
28  *
29  * "BSD" security module
30  */
31
32 #include "amanda.h"
33 #include "util.h"
34 #include "clock.h"
35 #include "dgram.h"
36 #include "event.h"
37 #include "packet.h"
38 #include "security.h"
39 #include "security-util.h"
40 #include "stream.h"
41
42 #ifndef SO_RCVBUF
43 #undef DUMPER_SOCKET_BUFFERING
44 #endif
45
46 /*
47  * Interface functions
48  */
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 *),
54     int, int,
55     void (*)(security_handle_t *, pkt_t *),
56     void *);
57 static void bsdudp_close(void *);
58
59 /*
60  * This is our interface to the outside world
61  */
62 const security_driver_t bsdudp_security_driver = {
63     "BSDUDP",
64     bsdudp_connect,
65     bsdudp_accept,
66     bsdudp_close,
67     udpbsd_sendpkt,
68     udp_recvpkt,
69     udp_recvpkt_cancel,
70     tcp1_stream_server,
71     tcp1_stream_accept,
72     tcp1_stream_client,
73     tcpma_stream_close,
74     sec_stream_auth,
75     sec_stream_id,
76     tcpm_stream_write,
77     tcpm_stream_read,
78     tcpm_stream_read_sync,
79     tcpm_stream_read_cancel,
80     sec_close_connection_none,
81     NULL,
82     NULL
83 };
84
85 /*
86  * This is data local to the datagram socket.  We have one datagram
87  * per process, so it is global.
88  */
89 static udp_handle_t netfd4;
90 static udp_handle_t netfd6;
91 static int not_init4 = 1;
92 static int not_init6 = 1;
93
94 /* generate new handles from here */
95 static unsigned int newhandle = 0;
96
97 /*
98  * Setup and return a handle outgoing to a client
99  */
100 static void
101 bsdudp_connect(
102     const char *hostname,
103     char *      (*conf_fn)(char *, void *),
104     void        (*fn)(void *, security_handle_t *, security_status_t),
105     void *      arg,
106     void *      datap)
107 {
108     struct sec_handle *bh;
109     in_port_t port;
110     struct timeval sequence_time;
111     int sequence;
112     char *handle;
113     int result;
114     char *canonname;
115     struct addrinfo *res = NULL, *res_addr;
116     int result_bind;
117     char *service;
118
119     (void)conf_fn;      /* Quiet unused parameter warning */
120     (void)datap;        /* Quiet unused parameter warning */
121     assert(hostname != NULL);
122
123     bh = alloc(sizeof(*bh));
124     bh->proto_handle=NULL;
125     bh->rc = NULL;
126     security_handleinit(&bh->sech, &bsdudp_security_driver);
127
128     result = resolve_hostname(hostname, SOCK_DGRAM, &res, &canonname);
129     if(result != 0) {
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);
134         return;
135     }
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);
141        return;
142     }
143     if (res == NULL) {
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);
148        amfree(canonname);
149        return;
150     }
151
152     for (res_addr = res; res_addr != NULL; res_addr = res_addr->ai_next) {
153 #ifdef WORKING_IPV6
154         /* IPv6 socket already bound */
155         if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 0) {
156             break;
157         }
158         /*
159          * Only init the IPv6 socket once
160          */
161         if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 1) {
162             uid_t euid;
163             dgram_zero(&netfd6.dgram);
164
165             euid = geteuid();
166             set_root_privs(1);
167             result_bind = dgram_bind(&netfd6.dgram,
168                                      res_addr->ai_addr->sa_family, &port);
169             set_root_privs(0);
170             if (result_bind != 0) {
171                 continue;
172             }
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;
177             /*
178              * We must have a reserved port.  Bomb if we didn't get one.
179              */
180             if (port >= IPPORT_RESERVED) {
181                 security_seterror(&bh->sech,
182                     _("unable to bind to a reserved port (got port %u)"),
183                     (unsigned int)port);
184                 (*fn)(arg, &bh->sech, S_ERROR);
185                 freeaddrinfo(res);
186                 amfree(canonname);
187                 return;
188             }
189             not_init6 = 0;
190             bh->udp = &netfd6;
191             break;
192         }
193 #endif
194
195         /* IPv4 socket already bound */
196         if (res_addr->ai_addr->sa_family == AF_INET && not_init4 == 0) {
197             break;
198         }
199
200         /*
201          * Only init the IPv4 socket once
202          */
203         if (res_addr->ai_addr->sa_family == AF_INET && not_init4 == 1) {
204             uid_t euid;
205             dgram_zero(&netfd4.dgram);
206
207             euid = geteuid();
208             set_root_privs(1);
209             result_bind = dgram_bind(&netfd4.dgram,
210                                      res_addr->ai_addr->sa_family, &port);
211             set_root_privs(0);
212             if (result_bind != 0) {
213                 continue;
214             }
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;
219             /*
220              * We must have a reserved port.  Bomb if we didn't get one.
221              */
222             if (port >= IPPORT_RESERVED) {
223                 security_seterror(&bh->sech,
224                     "unable to bind to a reserved port (got port %u)",
225                     (unsigned int)port);
226                 (*fn)(arg, &bh->sech, S_ERROR);
227                 freeaddrinfo(res);
228                 amfree(canonname);
229                 return;
230             }
231             not_init4 = 0;
232             bh->udp = &netfd4;
233             break;
234         }
235     }
236
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);
242        amfree(canonname);
243        return;
244     }
245
246 #ifdef WORKING_IPV6
247     if (res_addr->ai_addr->sa_family == AF_INET6)
248         bh->udp = &netfd6;
249     else
250 #endif
251         bh->udp = &netfd4;
252
253     auth_debug(1, _("Resolved hostname=%s\n"), canonname);
254     if (conf_fn) {
255         service = conf_fn("client_port", datap);
256         if (!service || strlen(service) <= 1)
257             service = "amanda";
258     } else {
259         service = "amanda";
260     }
261     port = find_port_for_service(service, "udp");
262     if (port == 0) {
263         security_seterror(&bh->sech, _("%s/udp unknown protocol"), service);
264         (*fn)(arg, &bh->sech, S_ERROR);
265         amfree(canonname);
266         return;
267     }
268
269     amanda_gettimeofday(&sequence_time);
270     sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
271     handle=alloc(15);
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);
278         amfree(bh);
279     } else {
280         (*fn)(arg, &bh->sech, S_OK);
281     }
282     amfree(handle);
283     amfree(canonname);
284
285     if (res) freeaddrinfo(res);
286 }
287
288 /*
289  * Setup to accept new incoming connections
290  */
291 static void
292 bsdudp_accept(
293     const struct security_driver *driver,
294     char *      (*conf_fn)(char *, void *),
295     int         in,
296     int         out,
297     void        (*fn)(security_handle_t *, pkt_t *),
298     void       *datap)
299 {
300     (void)driver;       /* Quiet unused parameter warning */
301     (void)out;          /* Quiet unused parameter warning */
302     (void)conf_fn;
303     (void)datap;
304
305     assert(in >= 0 && out >= 0);
306     assert(fn != NULL);
307
308     /*
309      * We assume in and out point to the same socket, and just use
310      * in.
311      */
312     dgram_socket(&netfd4.dgram, in);
313     dgram_socket(&netfd6.dgram, in);
314
315     /*
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
319      */
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;
324
325
326     udp_addref(&netfd4, &udp_netfd_read_callback);
327 }
328
329 /*
330  * Frees a handle allocated by the above
331  */
332 static void
333 bsdudp_close(
334     void *cookie)
335 {
336     struct sec_handle *bh = cookie;
337
338     if(bh->proto_handle == NULL) {
339         return;
340     }
341
342     auth_debug(1, _("bsdudp: close handle '%s'\n"), bh->proto_handle);
343
344     udp_recvpkt_cancel(bh);
345     if(bh->next) {
346         bh->next->prev = bh->prev;
347     }
348     else {
349         if (!not_init6 && netfd6.bh_last == bh)
350             netfd6.bh_last = bh->prev;
351         else
352             netfd4.bh_last = bh->prev;
353     }
354     if(bh->prev) {
355         bh->prev->next = bh->next;
356     }
357     else {
358         if (!not_init6 && netfd6.bh_first == bh)
359             netfd6.bh_first = bh->next;
360         else
361             netfd4.bh_first = bh->next;
362     }
363
364     amfree(bh->proto_handle);
365     amfree(bh->hostname);
366     amfree(bh);
367 }
368