Imported Upstream version 2.5.2p1
[debian/amanda] / common-src / bsdtcp-security.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1999 University of Maryland
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 /*
28  * $Id: bsdtcp-security.c,v 1.7 2006/07/13 03:22:20 paddy_s Exp $
29  *
30  * bsdtcp-security.c - security and transport over bsdtcp or a bsdtcp-like command.
31  *
32  * XXX still need to check for initial keyword on connect so we can skip
33  * over shell garbage and other stuff that bsdtcp might want to spew out.
34  */
35
36 #include "amanda.h"
37 #include "util.h"
38 #include "event.h"
39 #include "packet.h"
40 #include "queue.h"
41 #include "security.h"
42 #include "security-util.h"
43 #include "stream.h"
44 #include "version.h"
45
46 #ifdef BSDTCP_SECURITY
47
48 /*
49  * Number of seconds bsdtcp has to start up
50  */
51 #define CONNECT_TIMEOUT 20
52
53 /*
54  * Interface functions
55  */
56 static void bsdtcp_accept(const struct security_driver *, int, int,
57     void (*)(security_handle_t *, pkt_t *));
58 static void bsdtcp_connect(const char *,
59     char *(*)(char *, void *), 
60     void (*)(void *, security_handle_t *, security_status_t), void *, void *);
61
62 /*
63  * This is our interface to the outside world.
64  */
65 const security_driver_t bsdtcp_security_driver = {
66     "BSDTCP",
67     bsdtcp_connect,
68     bsdtcp_accept,
69     sec_close,
70     stream_sendpkt,
71     stream_recvpkt,
72     stream_recvpkt_cancel,
73     tcpma_stream_server,
74     tcpma_stream_accept,
75     tcpma_stream_client,
76     tcpma_stream_close,
77     sec_stream_auth,
78     sec_stream_id,
79     tcpm_stream_write,
80     tcpm_stream_read,
81     tcpm_stream_read_sync,
82     tcpm_stream_read_cancel,
83     tcpm_close_connection,
84     NULL,
85     NULL
86 };
87
88 static int newhandle = 1;
89
90 /*
91  * Local functions
92  */
93 static int runbsdtcp(struct sec_handle *);
94
95
96 /*
97  * bsdtcp version of a security handle allocator.  Logically sets
98  * up a network "connection".
99  */
100 static void
101 bsdtcp_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 *rh;
109     int result;
110     struct addrinfo hints;
111     struct addrinfo *res = NULL;
112
113     assert(fn != NULL);
114     assert(hostname != NULL);
115     (void)conf_fn;      /* Quiet unused parameter warning */
116     (void)datap;        /* Quiet unused parameter warning */
117
118     auth_debug(1, ("%s: bsdtcp: bsdtcp_connect: %s\n", debug_prefix_time(NULL),
119                    hostname));
120
121     rh = alloc(sizeof(*rh));
122     security_handleinit(&rh->sech, &bsdtcp_security_driver);
123     rh->hostname = NULL;
124     rh->rs = NULL;
125     rh->ev_timeout = NULL;
126     rh->rc = NULL;
127
128 #ifdef WORKING_IPV6
129     hints.ai_flags = AI_CANONNAME | AI_V4MAPPED | AI_ALL;
130     hints.ai_family = AF_UNSPEC;
131 #else
132     hints.ai_flags = AI_CANONNAME;
133     hints.ai_family = AF_INET;
134 #endif
135     hints.ai_socktype = SOCK_DGRAM;
136     hints.ai_protocol = IPPROTO_UDP;
137     hints.ai_addrlen = 0;
138     hints.ai_addr = NULL;
139     hints.ai_canonname = NULL;
140     hints.ai_next = NULL;
141     result = getaddrinfo(hostname, NULL, &hints, &res);
142 #ifdef WORKING_IPV6
143     if (result != 0) {
144         hints.ai_flags = AI_CANONNAME;
145         hints.ai_family = AF_UNSPEC;
146         result = getaddrinfo(hostname, NULL, &hints, &res);
147     }
148 #endif
149     if(result != 0) {
150         dbprintf(("getaddrinfo(%s): %s\n", hostname, gai_strerror(result)));
151         security_seterror(&rh->sech, "getaddrinfo(%s): %s\n", hostname,
152                           gai_strerror(result));
153         (*fn)(arg, &rh->sech, S_ERROR);
154         return;
155     }
156     if (res->ai_canonname == NULL) {
157         dbprintf(("getaddrinfo(%s) did not return a canonical name\n", hostname));
158         security_seterror(&rh->sech,
159                 _("getaddrinfo(%s) did not return a canonical name\n"), hostname);
160         (*fn)(arg, &rh->sech, S_ERROR);
161        return;
162     }
163
164     rh->hostname = stralloc(res->ai_canonname); /* will be replaced */
165     rh->rs = tcpma_stream_client(rh, newhandle++);
166     rh->rc->recv_security_ok = &bsd_recv_security_ok;
167     rh->rc->prefix_packet = &bsd_prefix_packet;
168
169     if (rh->rs == NULL)
170         goto error;
171
172     amfree(rh->hostname);
173     rh->hostname = stralloc(rh->rs->rc->hostname);
174
175     /*
176      * We need to open a new connection.
177      *
178      * XXX need to eventually limit number of outgoing connections here.
179      */
180     if(rh->rc->read == -1) {
181         if (runbsdtcp(rh) < 0)
182             goto error;
183         rh->rc->refcnt++;
184     }
185
186     /*
187      * The socket will be opened async so hosts that are down won't
188      * block everything.  We need to register a write event
189      * so we will know when the socket comes alive.
190      *
191      * Overload rh->rs->ev_read to provide a write event handle.
192      * We also register a timeout.
193      */
194     rh->fn.connect = fn;
195     rh->arg = arg;
196     rh->rs->ev_read = event_register((event_id_t)(rh->rs->rc->write),
197         EV_WRITEFD, sec_connect_callback, rh);
198     rh->ev_timeout = event_register(CONNECT_TIMEOUT, EV_TIME,
199         sec_connect_timeout, rh);
200
201     freeaddrinfo(res);
202     return;
203
204 error:
205     (*fn)(arg, &rh->sech, S_ERROR);
206     freeaddrinfo(res);
207 }
208
209 /*
210  * Setup to handle new incoming connections
211  */
212 static void
213 bsdtcp_accept(
214     const struct security_driver *driver,
215     int         in,
216     int         out,
217     void        (*fn)(security_handle_t *, pkt_t *))
218 {
219     struct sockaddr_storage sin;
220     socklen_t len;
221     struct tcp_conn *rc;
222     char hostname[NI_MAXHOST];
223     int result;
224     char *errmsg = NULL;
225
226     len = sizeof(sin);
227     if (getpeername(in, (struct sockaddr *)&sin, &len) < 0) {
228         dbprintf(("%s: getpeername returned: %s\n", debug_prefix_time(NULL),
229                   strerror(errno)));
230         return;
231     }
232     if ((result = getnameinfo((struct sockaddr *)&sin, len,
233                               hostname, NI_MAXHOST, NULL, 0, 0) != 0)) {
234         dbprintf(("%s: getnameinfo failed: %s\n",
235                   debug_prefix_time(NULL), gai_strerror(result)));
236         return;
237     }
238     if (check_name_give_sockaddr(hostname,
239                                  (struct sockaddr *)&sin, &errmsg) < 0) {
240         amfree(errmsg);
241         return;
242     }
243
244     rc = sec_tcp_conn_get(hostname, 0);
245     rc->recv_security_ok = &bsd_recv_security_ok;
246     rc->prefix_packet = &bsd_prefix_packet;
247     memcpy(&rc->peer, &sin, sizeof(rc->peer));
248     rc->read = in;
249     rc->write = out;
250     rc->accept_fn = fn;
251     rc->driver = driver;
252     sec_tcp_conn_read(rc);
253 }
254
255 /*
256  * Forks a bsdtcp to the host listed in rc->hostname
257  * Returns negative on error, with an errmsg in rc->errmsg.
258  */
259 static int
260 runbsdtcp(
261     struct sec_handle * rh)
262 {
263     struct servent *    sp;
264     int                 server_socket;
265     in_port_t           my_port;
266     uid_t               euid;
267     struct tcp_conn *   rc = rh->rc;
268
269     if ((sp = getservbyname(AMANDA_SERVICE_NAME, "tcp")) == NULL) {
270         error("%s/tcp unknown protocol", "amanda");
271     }
272
273     euid = geteuid();
274     seteuid(0);
275
276     server_socket = stream_client_privileged(rc->hostname,
277                                      (in_port_t)(ntohs((in_port_t)sp->s_port)),
278                                      STREAM_BUFSIZE,
279                                      STREAM_BUFSIZE,
280                                      &my_port,
281                                      0);
282
283     if(server_socket < 0) {
284         security_seterror(&rh->sech,
285             "%s", strerror(errno));
286         
287         return -1;
288     }
289     seteuid(euid);
290
291     if(my_port >= IPPORT_RESERVED) {
292         security_seterror(&rh->sech,
293                           "did not get a reserved port: %d", my_port);
294     }
295
296     rc->read = rc->write = server_socket;
297     return 0;
298 }
299
300 #endif /* BSDTCP_SECURITY */