Imported Upstream version 2.5.1
[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 #include "version.h"
42
43 #ifdef BSDUDP_SECURITY
44
45 /*#define       BSDUDP_DEBUG*/
46
47 #ifdef BSDUDP_DEBUG
48 #define bsdudpprintf(x)    dbprintf(x)
49 #else
50 #define bsdudpprintf(x)
51 #endif
52
53 #ifndef SO_RCVBUF
54 #undef DUMPER_SOCKET_BUFFERING
55 #endif
56
57 /*
58  * Change the following from #undef to #define to cause detailed logging
59  * of the security steps, e.g. into /tmp/amanda/amandad*debug.
60  */
61 #undef SHOW_SECURITY_DETAIL
62
63 #if defined(TEST)                                               /* { */
64 #define SHOW_SECURITY_DETAIL
65 #undef bsdudpprintf
66 #define bsdudpprintf(p) printf p
67 #endif                                                          /* } */
68
69 /*
70  * Interface functions
71  */
72 static void bsdudp_connect(const char *,
73     char *(*)(char *, void *), 
74     void (*)(void *, security_handle_t *, security_status_t), void *, void *);
75 static void bsdudp_accept(const struct security_driver *, int, int, void (*)(security_handle_t *, pkt_t *));
76 static void bsdudp_close(void *);
77
78 /*
79  * This is our interface to the outside world
80  */
81 const security_driver_t bsdudp_security_driver = {
82     "BSDUDP",
83     bsdudp_connect,
84     bsdudp_accept,
85     bsdudp_close,
86     udpbsd_sendpkt,
87     udp_recvpkt,
88     udp_recvpkt_cancel,
89     tcp1_stream_server,
90     tcp1_stream_accept,
91     tcp1_stream_client,
92     tcpma_stream_close,
93     sec_stream_auth,
94     sec_stream_id,
95     tcpm_stream_write,
96     tcpm_stream_read,
97     tcpm_stream_read_sync,
98     tcpm_stream_read_cancel,
99     sec_close_connection_none,
100 };
101
102 /*
103  * This is data local to the datagram socket.  We have one datagram
104  * per process, so it is global.
105  */
106 static udp_handle_t netfd;
107 static int not_init = 1;
108
109 /* generate new handles from here */
110 static unsigned int newhandle = 0;
111
112 /*
113  * Setup and return a handle outgoing to a client
114  */
115 static void
116 bsdudp_connect(
117     const char *hostname,
118     char *      (*conf_fn)(char *, void *),
119     void        (*fn)(void *, security_handle_t *, security_status_t),
120     void *      arg,
121     void *      datap)
122 {
123     struct sec_handle *bh;
124     struct servent *se;
125     struct hostent *he;
126     in_port_t port;
127     struct timeval sequence_time;
128     amanda_timezone dontcare;
129     int sequence;
130     char *handle;
131
132     (void)conf_fn;      /* Quiet unused parameter warning */
133     (void)datap;        /* Quiet unused parameter warning */
134     assert(hostname != NULL);
135
136     bh = alloc(sizeof(*bh));
137     bh->proto_handle=NULL;
138     bh->udp = &netfd;
139     bh->rc = NULL;
140     security_handleinit(&bh->sech, &bsdudp_security_driver);
141
142     /*
143      * Only init the socket once
144      */
145     if (not_init == 1) {
146         uid_t euid;
147         dgram_zero(&netfd.dgram);
148         
149         euid = geteuid();
150         seteuid(0);
151         dgram_bind(&netfd.dgram, &port);
152         seteuid(euid);
153         netfd.handle = NULL;
154         netfd.pkt.body = NULL;
155         netfd.recv_security_ok = &bsd_recv_security_ok;
156         netfd.prefix_packet = &bsd_prefix_packet;
157         /*
158          * We must have a reserved port.  Bomb if we didn't get one.
159          */
160         if (port >= IPPORT_RESERVED) {
161             security_seterror(&bh->sech,
162                 "unable to bind to a reserved port (got port %u)",
163                 (unsigned int)port);
164             (*fn)(arg, &bh->sech, S_ERROR);
165             return;
166         }
167         not_init = 0;
168     }
169
170     if ((he = gethostbyname(hostname)) == NULL) {
171         security_seterror(&bh->sech,
172             "%s: could not resolve hostname", hostname);
173         (*fn)(arg, &bh->sech, S_ERROR);
174         return;
175     }
176     if ((se = getservbyname(AMANDA_SERVICE_NAME, "udp")) == NULL)
177         port = (in_port_t)htons(AMANDA_SERVICE_DEFAULT);
178     else
179         port = (in_port_t)se->s_port;
180     amanda_gettimeofday(&sequence_time, &dontcare);
181     sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
182     handle=alloc(15);
183     snprintf(handle,14,"000-%08x", newhandle++);
184     if (udp_inithandle(&netfd, bh, he, port, handle, sequence) < 0) {
185         (*fn)(arg, &bh->sech, S_ERROR);
186         amfree(bh->hostname);
187         amfree(bh);
188     } else {
189         (*fn)(arg, &bh->sech, S_OK);
190     }
191     amfree(handle);
192 }
193
194 /*
195  * Setup to accept new incoming connections
196  */
197 static void
198 bsdudp_accept(
199     const struct security_driver *driver,
200     int         in,
201     int         out,
202     void        (*fn)(security_handle_t *, pkt_t *))
203 {
204     (void)driver;       /* Quiet unused parameter warning */
205     (void)out;          /* Quiet unused parameter warning */
206
207     assert(in >= 0 && out >= 0);
208     assert(fn != NULL);
209
210     /*
211      * We assume in and out point to the same socket, and just use
212      * in.
213      */
214     dgram_socket(&netfd.dgram, in);
215
216     /*
217      * Assign the function and return.  When they call recvpkt later,
218      * the recvpkt callback will call this function when it discovers
219      * new incoming connections
220      */
221     netfd.accept_fn = fn;
222     netfd.recv_security_ok = &bsd_recv_security_ok;
223     netfd.prefix_packet = &bsd_prefix_packet;
224     netfd.driver = &bsdudp_security_driver;
225
226
227     udp_addref(&netfd, &udp_netfd_read_callback);
228 }
229
230 /*
231  * Frees a handle allocated by the above
232  */
233 static void
234 bsdudp_close(
235     void *cookie)
236 {
237     struct sec_handle *bh = cookie;
238
239     if(bh->proto_handle == NULL) {
240         return;
241     }
242
243     bsdudpprintf(("%s: bsdudp: close handle '%s'\n",
244                debug_prefix_time(NULL), bh->proto_handle));
245
246     udp_recvpkt_cancel(bh);
247     if(bh->next) {
248         bh->next->prev = bh->prev;
249     }
250     else {
251         netfd.bh_last = bh->prev;
252     }
253     if(bh->prev) {
254         bh->prev->next = bh->next;
255     }
256     else {
257         netfd.bh_first = bh->next;
258     }
259
260     amfree(bh->proto_handle);
261     amfree(bh->hostname);
262     amfree(bh);
263 }
264
265 #endif  /* BSDUDP_SECURITY */                                   /* } */
266