2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1999 University of Maryland
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.
28 * $Id: bsdtcp-security.c,v 1.7 2006/07/13 03:22:20 paddy_s Exp $
30 * bsdtcp-security.c - security and transport over bsdtcp or a bsdtcp-like command.
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.
42 #include "security-util.h"
46 #ifdef BSDTCP_SECURITY
49 * Number of seconds bsdtcp has to start up
51 #define CONNECT_TIMEOUT 20
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 *);
63 * This is our interface to the outside world.
65 const security_driver_t bsdtcp_security_driver = {
72 stream_recvpkt_cancel,
81 tcpm_stream_read_sync,
82 tcpm_stream_read_cancel,
83 tcpm_close_connection,
88 static int newhandle = 1;
93 static int runbsdtcp(struct sec_handle *);
97 * bsdtcp version of a security handle allocator. Logically sets
98 * up a network "connection".
102 const char *hostname,
103 char * (*conf_fn)(char *, void *),
104 void (*fn)(void *, security_handle_t *, security_status_t),
108 struct sec_handle *rh;
110 struct addrinfo hints;
111 struct addrinfo *res = NULL;
114 assert(hostname != NULL);
115 (void)conf_fn; /* Quiet unused parameter warning */
116 (void)datap; /* Quiet unused parameter warning */
118 auth_debug(1, ("%s: bsdtcp: bsdtcp_connect: %s\n", debug_prefix_time(NULL),
121 rh = alloc(sizeof(*rh));
122 security_handleinit(&rh->sech, &bsdtcp_security_driver);
125 rh->ev_timeout = NULL;
129 hints.ai_flags = AI_CANONNAME | AI_V4MAPPED | AI_ALL;
130 hints.ai_family = AF_UNSPEC;
132 hints.ai_flags = AI_CANONNAME;
133 hints.ai_family = AF_INET;
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);
144 hints.ai_flags = AI_CANONNAME;
145 hints.ai_family = AF_UNSPEC;
146 result = getaddrinfo(hostname, NULL, &hints, &res);
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);
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);
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;
172 amfree(rh->hostname);
173 rh->hostname = stralloc(rh->rs->rc->hostname);
176 * We need to open a new connection.
178 * XXX need to eventually limit number of outgoing connections here.
180 if(rh->rc->read == -1) {
181 if (runbsdtcp(rh) < 0)
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.
191 * Overload rh->rs->ev_read to provide a write event handle.
192 * We also register a timeout.
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);
205 (*fn)(arg, &rh->sech, S_ERROR);
210 * Setup to handle new incoming connections
214 const struct security_driver *driver,
217 void (*fn)(security_handle_t *, pkt_t *))
219 struct sockaddr_storage sin;
222 char hostname[NI_MAXHOST];
227 if (getpeername(in, (struct sockaddr *)&sin, &len) < 0) {
228 dbprintf(("%s: getpeername returned: %s\n", debug_prefix_time(NULL),
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)));
238 if (check_name_give_sockaddr(hostname,
239 (struct sockaddr *)&sin, &errmsg) < 0) {
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));
252 sec_tcp_conn_read(rc);
256 * Forks a bsdtcp to the host listed in rc->hostname
257 * Returns negative on error, with an errmsg in rc->errmsg.
261 struct sec_handle * rh)
267 struct tcp_conn * rc = rh->rc;
269 if ((sp = getservbyname(AMANDA_SERVICE_NAME, "tcp")) == NULL) {
270 error("%s/tcp unknown protocol", "amanda");
276 server_socket = stream_client_privileged(rc->hostname,
277 (in_port_t)(ntohs((in_port_t)sp->s_port)),
283 if(server_socket < 0) {
284 security_seterror(&rh->sech,
285 "%s", strerror(errno));
291 if(my_port >= IPPORT_RESERVED) {
292 security_seterror(&rh->sech,
293 "did not get a reserved port: %d", my_port);
296 rc->read = rc->write = server_socket;
300 #endif /* BSDTCP_SECURITY */