Imported Upstream version 3.3.3
[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  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of U.M. not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  U.M. makes no representations about the
14  * suitability of this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  *
17  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Authors: the Amanda Development Team.  Its members are listed in a
25  * file named AUTHORS, in the root directory of this distribution.
26  */
27 /*
28  * $Id: bsdudp-security.c,v 1.7 2006/07/05 13:18:20 martinea Exp $
29  *
30  * "BSD" security module
31  */
32
33 #include "amanda.h"
34 #include "util.h"
35 #include "clock.h"
36 #include "dgram.h"
37 #include "event.h"
38 #include "packet.h"
39 #include "security.h"
40 #include "security-util.h"
41 #include "stream.h"
42
43 #ifndef SO_RCVBUF
44 #undef DUMPER_SOCKET_BUFFERING
45 #endif
46
47 /*
48  * Interface functions
49  */
50 static void bsdudp_connect(const char *,
51     char *(*)(char *, void *), 
52     void (*)(void *, security_handle_t *, security_status_t), void *, void *);
53 static void bsdudp_accept(const struct security_driver *,
54     char *(*)(char *, void *),
55     int, int,
56     void (*)(security_handle_t *, pkt_t *),
57     void *);
58 static void bsdudp_close(void *);
59
60 /*
61  * This is our interface to the outside world
62  */
63 const security_driver_t bsdudp_security_driver = {
64     "BSDUDP",
65     bsdudp_connect,
66     bsdudp_accept,
67     sec_get_authenticated_peer_name_hostname,
68     bsdudp_close,
69     udpbsd_sendpkt,
70     udp_recvpkt,
71     udp_recvpkt_cancel,
72     tcp1_stream_server,
73     tcp1_stream_accept,
74     tcp1_stream_client,
75     tcpma_stream_close,
76     sec_stream_auth,
77     sec_stream_id,
78     tcpm_stream_write,
79     tcpm_stream_read,
80     tcpm_stream_read_sync,
81     tcpm_stream_read_cancel,
82     sec_close_connection_none,
83     NULL,
84     NULL
85 };
86
87 /*
88  * This is data local to the datagram socket.  We have one datagram
89  * per process, so it is global.
90  */
91 static udp_handle_t netfd4;
92 static udp_handle_t netfd6;
93 static int not_init4 = 1;
94 static int not_init6 = 1;
95
96 /* generate new handles from here */
97 static unsigned int newhandle = 0;
98
99 /*
100  * Setup and return a handle outgoing to a client
101  */
102 static void
103 bsdudp_connect(
104     const char *hostname,
105     char *      (*conf_fn)(char *, void *),
106     void        (*fn)(void *, security_handle_t *, security_status_t),
107     void *      arg,
108     void *      datap)
109 {
110     struct sec_handle *bh;
111     in_port_t port;
112     struct timeval sequence_time;
113     int sequence;
114     char *handle;
115     int result;
116     char *canonname;
117     struct addrinfo *res = NULL, *res_addr;
118     int result_bind;
119     char *service;
120
121     (void)conf_fn;      /* Quiet unused parameter warning */
122     (void)datap;        /* Quiet unused parameter warning */
123     assert(hostname != NULL);
124
125     bh = g_new0(struct sec_handle, 1);
126     bh->proto_handle=NULL;
127     bh->rc = NULL;
128     security_handleinit(&bh->sech, &bsdudp_security_driver);
129
130     result = resolve_hostname(hostname, SOCK_DGRAM, &res, &canonname);
131     if(result != 0) {
132         dbprintf(_("resolve_hostname(%s): %s\n"), hostname, gai_strerror(result));
133         security_seterror(&bh->sech, _("resolve_hostname(%s): %s\n"), hostname,
134                           gai_strerror(result));
135         (*fn)(arg, &bh->sech, S_ERROR);
136         return;
137     }
138     if (canonname == NULL) {
139         dbprintf(_("resolve_hostname(%s) did not return a canonical name\n"), hostname);
140         security_seterror(&bh->sech,
141                 _("resolve_hostname(%s) did not return a canonical name\n"), hostname);
142         (*fn)(arg, &bh->sech, S_ERROR);
143        return;
144     }
145     if (res == NULL) {
146         dbprintf(_("resolve_hostname(%s): no results\n"), hostname);
147         security_seterror(&bh->sech,
148                 _("resolve_hostname(%s): no results\n"), hostname);
149         (*fn)(arg, &bh->sech, S_ERROR);
150        amfree(canonname);
151        return;
152     }
153
154     for (res_addr = res; res_addr != NULL; res_addr = res_addr->ai_next) {
155 #ifdef WORKING_IPV6
156         /* IPv6 socket already bound */
157         if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 0) {
158             break;
159         }
160         /*
161          * Only init the IPv6 socket once
162          */
163         if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 1) {
164             dgram_zero(&netfd6.dgram);
165
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             dgram_zero(&netfd4.dgram);
205
206             set_root_privs(1);
207             result_bind = dgram_bind(&netfd4.dgram,
208                                      res_addr->ai_addr->sa_family, &port);
209             set_root_privs(0);
210             if (result_bind != 0) {
211                 continue;
212             }
213             netfd4.handle = NULL;
214             netfd4.pkt.body = NULL;
215             netfd4.recv_security_ok = &bsd_recv_security_ok;
216             netfd4.prefix_packet = &bsd_prefix_packet;
217             /*
218              * We must have a reserved port.  Bomb if we didn't get one.
219              */
220             if (port >= IPPORT_RESERVED) {
221                 security_seterror(&bh->sech,
222                     "unable to bind to a reserved port (got port %u)",
223                     (unsigned int)port);
224                 (*fn)(arg, &bh->sech, S_ERROR);
225                 freeaddrinfo(res);
226                 amfree(canonname);
227                 return;
228             }
229             not_init4 = 0;
230             bh->udp = &netfd4;
231             break;
232         }
233     }
234
235     if (res_addr == NULL) {
236         dbprintf(_("Can't bind a socket to connect to %s\n"), hostname);
237         security_seterror(&bh->sech,
238                 _("Can't bind a socket to connect to %s\n"), hostname);
239         (*fn)(arg, &bh->sech, S_ERROR);
240        amfree(canonname);
241        return;
242     }
243
244 #ifdef WORKING_IPV6
245     if (res_addr->ai_addr->sa_family == AF_INET6)
246         bh->udp = &netfd6;
247     else
248 #endif
249         bh->udp = &netfd4;
250
251     auth_debug(1, _("Resolved hostname=%s\n"), canonname);
252     if (conf_fn) {
253         service = conf_fn("client_port", datap);
254         if (!service || strlen(service) <= 1)
255             service = "amanda";
256     } else {
257         service = "amanda";
258     }
259     port = find_port_for_service(service, "udp");
260     if (port == 0) {
261         security_seterror(&bh->sech, _("%s/udp unknown protocol"), service);
262         (*fn)(arg, &bh->sech, S_ERROR);
263         amfree(canonname);
264         return;
265     }
266
267     amanda_gettimeofday(&sequence_time);
268     sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
269     handle=alloc(15);
270     g_snprintf(handle,14,"000-%08x", newhandle++);
271     if (udp_inithandle(bh->udp, bh, canonname,
272                        (sockaddr_union *)res_addr->ai_addr, port,
273                        handle, sequence) < 0) {
274         (*fn)(arg, &bh->sech, S_ERROR);
275         amfree(bh->hostname);
276         amfree(bh);
277     } else {
278         (*fn)(arg, &bh->sech, S_OK);
279     }
280     amfree(handle);
281     amfree(canonname);
282
283     if (res) freeaddrinfo(res);
284 }
285
286 /*
287  * Setup to accept new incoming connections
288  */
289 static void
290 bsdudp_accept(
291     const struct security_driver *driver,
292     char *      (*conf_fn)(char *, void *),
293     int         in,
294     int         out,
295     void        (*fn)(security_handle_t *, pkt_t *),
296     void       *datap)
297 {
298     (void)driver;       /* Quiet unused parameter warning */
299     (void)out;          /* Quiet unused parameter warning */
300     (void)conf_fn;
301     (void)datap;
302
303     assert(in >= 0 && out >= 0);
304     assert(fn != NULL);
305
306     /*
307      * We assume in and out point to the same socket, and just use
308      * in.
309      */
310     dgram_socket(&netfd4.dgram, in);
311     dgram_socket(&netfd6.dgram, in);
312
313     /*
314      * Assign the function and return.  When they call recvpkt later,
315      * the recvpkt callback will call this function when it discovers
316      * new incoming connections
317      */
318     netfd4.accept_fn = fn;
319     netfd4.recv_security_ok = &bsd_recv_security_ok;
320     netfd4.prefix_packet = &bsd_prefix_packet;
321     netfd4.driver = &bsdudp_security_driver;
322
323
324     udp_addref(&netfd4, &udp_netfd_read_callback);
325 }
326
327 /*
328  * Frees a handle allocated by the above
329  */
330 static void
331 bsdudp_close(
332     void *cookie)
333 {
334     struct sec_handle *bh = cookie;
335
336     if(bh->proto_handle == NULL) {
337         return;
338     }
339
340     auth_debug(1, _("bsdudp: close handle '%s'\n"), bh->proto_handle);
341
342     udp_recvpkt_cancel(bh);
343     if(bh->next) {
344         bh->next->prev = bh->prev;
345     }
346     else {
347         if (!not_init6 && netfd6.bh_last == bh)
348             netfd6.bh_last = bh->prev;
349         else
350             netfd4.bh_last = bh->prev;
351     }
352     if(bh->prev) {
353         bh->prev->next = bh->next;
354     }
355     else {
356         if (!not_init6 && netfd6.bh_first == bh)
357             netfd6.bh_first = bh->next;
358         else
359             netfd4.bh_first = bh->next;
360     }
361
362     amfree(bh->proto_handle);
363     amfree(bh->hostname);
364     amfree(bh);
365 }
366