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: rsh-security.c,v 1.31.2.1 2006/10/25 19:13:11 martinea Exp $
30 * rsh-security.c - security and transport over rsh or a rsh-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 rsh might want to spew out.
42 #include "security-util.h"
51 #define rshprintf(x) dbprintf(x)
57 * Path to the rsh binary. This should be configurable.
59 #define RSH_PATH "/usr/bin/rsh"
62 * Arguments to rsh. This should also be configurable
67 * Number of seconds rsh has to start up
69 #define CONNECT_TIMEOUT 20
72 * Magic values for rsh_conn->handle
74 #define H_TAKEN -1 /* rsh_conn->tok was already read */
75 #define H_EOF -2 /* this connection has been shut down */
80 static void rsh_connect(const char *, char *(*)(char *, void *),
81 void (*)(void *, security_handle_t *, security_status_t),
85 * This is our interface to the outside world.
87 const security_driver_t rsh_security_driver = {
94 stream_recvpkt_cancel,
103 tcpm_stream_read_sync,
104 tcpm_stream_read_cancel,
105 tcpm_close_connection,
108 static int newhandle = 1;
113 static int runrsh(struct tcp_conn *, const char *, const char *);
117 * rsh version of a security handle allocator. Logically sets
118 * up a network "connection".
122 const char * hostname,
123 char * (*conf_fn)(char *, void *),
124 void (*fn)(void *, security_handle_t *, security_status_t),
128 struct sec_handle *rh;
130 char *amandad_path=NULL, *client_username=NULL;
133 assert(hostname != NULL);
135 rshprintf(("%s: rsh: rsh_connect: %s\n", debug_prefix_time(NULL),
138 rh = alloc(SIZEOF(*rh));
139 security_handleinit(&rh->sech, &rsh_security_driver);
142 rh->ev_timeout = NULL;
146 if ((he = gethostbyname(hostname)) == NULL) {
147 security_seterror(&rh->sech,
148 "%s: could not resolve hostname", hostname);
149 (*fn)(arg, &rh->sech, S_ERROR);
152 rh->hostname = stralloc(he->h_name); /* will be replaced */
153 rh->rs = tcpma_stream_client(rh, newhandle++);
158 amfree(rh->hostname);
159 rh->hostname = stralloc(rh->rs->rc->hostname);
162 * We need to open a new connection.
164 * XXX need to eventually limit number of outgoing connections here.
167 amandad_path = conf_fn("amandad_path", datap);
168 client_username = conf_fn("client_username", datap);
170 if(rh->rc->read == -1) {
171 if (runrsh(rh->rs->rc, amandad_path, client_username) < 0) {
172 security_seterror(&rh->sech, "can't connect to %s: %s",
173 hostname, rh->rs->rc->errmsg);
180 * The socket will be opened async so hosts that are down won't
181 * block everything. We need to register a write event
182 * so we will know when the socket comes alive.
184 * Overload rh->rs->ev_read to provide a write event handle.
185 * We also register a timeout.
189 rh->rs->ev_read = event_register((event_id_t)rh->rs->rc->write, EV_WRITEFD,
190 sec_connect_callback, rh);
191 rh->ev_timeout = event_register((event_id_t)CONNECT_TIMEOUT, EV_TIME,
192 sec_connect_timeout, rh);
197 (*fn)(arg, &rh->sech, S_ERROR);
201 * Forks a rsh to the host listed in rc->hostname
202 * Returns negative on error, with an errmsg in rc->errmsg.
206 struct tcp_conn * rc,
207 const char * amandad_path,
208 const char * client_username)
210 int rpipe[2], wpipe[2];
211 char *xamandad_path = (char *)amandad_path;
212 char *xclient_username = (char *)client_username;
214 memset(rpipe, -1, SIZEOF(rpipe));
215 memset(wpipe, -1, SIZEOF(wpipe));
216 if (pipe(rpipe) < 0 || pipe(wpipe) < 0) {
217 rc->errmsg = newvstralloc(rc->errmsg, "pipe: ", strerror(errno), NULL);
221 switch (rc->pid = fork()) {
223 rc->errmsg = newvstralloc(rc->errmsg, "fork: ", strerror(errno), NULL);
236 rc->write = wpipe[1];
243 if(!xamandad_path || strlen(xamandad_path) <= 1)
244 xamandad_path = vstralloc(libexecdir, "/", "amandad",
245 versionsuffix(), NULL);
246 if(!xclient_username || strlen(xclient_username) <= 1)
247 xclient_username = CLIENT_LOGIN;
249 execlp(RSH_PATH, RSH_PATH, "-l", xclient_username,
250 rc->hostname, xamandad_path, "-auth=rsh", "amdump", "amindexd",
251 "amidxtaped", (char *)NULL);
252 error("error: couldn't exec %s: %s", RSH_PATH, strerror(errno));
254 /* should never go here, shut up compiler warning */
258 #endif /* RSH_SECURITY */