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.
41 #include "security-util.h"
42 #include "sockaddr-util.h"
46 * Number of seconds bsdtcp has to start up
48 #define CONNECT_TIMEOUT 20
53 static void bsdtcp_accept(const struct security_driver *,
54 char *(*)(char *, void *),
56 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 *, in_port_t port);
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;
115 assert(hostname != NULL);
116 (void)conf_fn; /* Quiet unused parameter warning */
117 (void)datap; /* Quiet unused parameter warning */
119 auth_debug(1, _("bsdtcp: bsdtcp_connect: %s\n"), hostname);
121 rh = alloc(sizeof(*rh));
122 security_handleinit(&rh->sech, &bsdtcp_security_driver);
125 rh->ev_timeout = NULL;
128 result = resolve_hostname(hostname, 0, NULL, &canonname);
130 dbprintf(_("resolve_hostname(%s): %s\n"), hostname, gai_strerror(result));
131 security_seterror(&rh->sech, _("resolve_hostname(%s): %s\n"), hostname,
132 gai_strerror(result));
133 (*fn)(arg, &rh->sech, S_ERROR);
136 if (canonname == NULL) {
137 dbprintf(_("resolve_hostname(%s) did not return a canonical name\n"), hostname);
138 security_seterror(&rh->sech,
139 _("resolve_hostname(%s) did not return a canonical name\n"), hostname);
140 (*fn)(arg, &rh->sech, S_ERROR);
144 rh->hostname = canonname; /* will be replaced */
145 canonname = NULL; /* steal reference */
146 rh->rs = tcpma_stream_client(rh, newhandle++);
147 rh->rc->recv_security_ok = &bsd_recv_security_ok;
148 rh->rc->prefix_packet = &bsd_prefix_packet;
153 amfree(rh->hostname);
154 rh->hostname = stralloc(rh->rs->rc->hostname);
157 service = conf_fn("client_port", datap);
158 if (!service || strlen(service) <= 1)
163 port = find_port_for_service(service, "tcp");
165 security_seterror(&rh->sech, _("%s/tcp unknown protocol"), service);
170 * We need to open a new connection.
172 * XXX need to eventually limit number of outgoing connections here.
174 if(rh->rc->read == -1) {
175 if (runbsdtcp(rh, port) < 0)
181 * The socket will be opened async so hosts that are down won't
182 * block everything. We need to register a write event
183 * so we will know when the socket comes alive.
185 * Overload rh->rs->ev_read to provide a write event handle.
186 * We also register a timeout.
190 rh->rs->ev_read = event_register((event_id_t)(rh->rs->rc->write),
191 EV_WRITEFD, sec_connect_callback, rh);
192 rh->ev_timeout = event_register(CONNECT_TIMEOUT, EV_TIME,
193 sec_connect_timeout, rh);
198 (*fn)(arg, &rh->sech, S_ERROR);
202 * Setup to handle new incoming connections
206 const struct security_driver *driver,
207 char * (*conf_fn)(char *, void *),
210 void (*fn)(security_handle_t *, pkt_t *),
216 char hostname[NI_MAXHOST];
221 if (getpeername(in, (struct sockaddr *)&sin, &len) < 0) {
222 dbprintf(_("getpeername returned: %s\n"), strerror(errno));
225 if ((result = getnameinfo((struct sockaddr *)&sin, len,
226 hostname, NI_MAXHOST, NULL, 0, 0) != 0)) {
227 dbprintf(_("getnameinfo failed: %s\n"),
228 gai_strerror(result));
231 if (check_name_give_sockaddr(hostname,
232 (struct sockaddr *)&sin, &errmsg) < 0) {
237 rc = sec_tcp_conn_get(hostname, 0);
238 rc->recv_security_ok = &bsd_recv_security_ok;
239 rc->prefix_packet = &bsd_prefix_packet;
240 copy_sockaddr(&rc->peer, &sin);
245 rc->conf_fn = conf_fn;
247 sec_tcp_conn_read(rc);
251 * Forks a bsdtcp to the host listed in rc->hostname
252 * Returns negative on error, with an errmsg in rc->errmsg.
256 struct sec_handle * rh,
261 struct tcp_conn * rc = rh->rc;
265 server_socket = stream_client_privileged(rc->hostname,
273 if(server_socket < 0) {
274 security_seterror(&rh->sech,
275 "%s", strerror(errno));
280 if(my_port >= IPPORT_RESERVED) {
281 security_seterror(&rh->sech,
282 _("did not get a reserved port: %d"), my_port);
285 rc->read = rc->write = server_socket;