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 2006/08/21 20:17:10 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.
41 #include "security-util.h"
45 * Path to the rsh binary. This should be configurable.
47 #define RSH_PATH "/usr/bin/rsh"
50 * Arguments to rsh. This should also be configurable
55 * Number of seconds rsh has to start up
57 #define CONNECT_TIMEOUT 20
60 * Magic values for rsh_conn->handle
62 #define H_TAKEN -1 /* rsh_conn->tok was already read */
63 #define H_EOF -2 /* this connection has been shut down */
68 static void rsh_connect(const char *, char *(*)(char *, void *),
69 void (*)(void *, security_handle_t *, security_status_t),
73 * This is our interface to the outside world.
75 const security_driver_t rsh_security_driver = {
82 stream_recvpkt_cancel,
91 tcpm_stream_read_sync,
92 tcpm_stream_read_cancel,
93 tcpm_close_connection,
98 static int newhandle = 1;
103 static int runrsh(struct tcp_conn *, const char *, const char *);
107 * rsh version of a security handle allocator. Logically sets
108 * up a network "connection".
112 const char * hostname,
113 char * (*conf_fn)(char *, void *),
114 void (*fn)(void *, security_handle_t *, security_status_t),
119 struct sec_handle *rh;
120 char *amandad_path=NULL, *client_username=NULL;
123 assert(hostname != NULL);
125 auth_debug(1, _("rsh: rsh_connect: %s\n"), hostname);
127 rh = alloc(SIZEOF(*rh));
128 security_handleinit(&rh->sech, &rsh_security_driver);
131 rh->ev_timeout = NULL;
134 /* get the canonical hostname */
136 if ((result = resolve_hostname(hostname, 0, NULL, &rh->hostname)) || rh->hostname == NULL) {
137 security_seterror(&rh->sech,
138 _("rsh_security could not find canonical name for '%s': %s"),
139 hostname, gai_strerror(result));
140 (*fn)(arg, &rh->sech, S_ERROR);
143 rh->rs = tcpma_stream_client(rh, newhandle++);
148 amfree(rh->hostname);
149 rh->hostname = stralloc(rh->rs->rc->hostname);
152 * We need to open a new connection.
154 * XXX need to eventually limit number of outgoing connections here.
157 amandad_path = conf_fn("amandad_path", datap);
158 client_username = conf_fn("client_username", datap);
160 if(rh->rc->read == -1) {
161 if (runrsh(rh->rs->rc, amandad_path, client_username) < 0) {
162 security_seterror(&rh->sech, _("can't connect to %s: %s"),
163 hostname, rh->rs->rc->errmsg);
170 * The socket will be opened async so hosts that are down won't
171 * block everything. We need to register a write event
172 * so we will know when the socket comes alive.
174 * Overload rh->rs->ev_read to provide a write event handle.
175 * We also register a timeout.
179 rh->rs->ev_read = event_register((event_id_t)rh->rs->rc->write, EV_WRITEFD,
180 sec_connect_callback, rh);
181 rh->ev_timeout = event_register((event_id_t)CONNECT_TIMEOUT, EV_TIME,
182 sec_connect_timeout, rh);
187 (*fn)(arg, &rh->sech, S_ERROR);
191 * Forks a rsh to the host listed in rc->hostname
192 * Returns negative on error, with an errmsg in rc->errmsg.
196 struct tcp_conn * rc,
197 const char * amandad_path,
198 const char * client_username)
200 int rpipe[2], wpipe[2];
201 char *xamandad_path = (char *)amandad_path;
202 char *xclient_username = (char *)client_username;
204 memset(rpipe, -1, SIZEOF(rpipe));
205 memset(wpipe, -1, SIZEOF(wpipe));
206 if (pipe(rpipe) < 0 || pipe(wpipe) < 0) {
207 rc->errmsg = newvstrallocf(rc->errmsg, _("pipe: %s"), strerror(errno));
211 switch (rc->pid = fork()) {
213 rc->errmsg = newvstrallocf(rc->errmsg, _("fork: %s"), strerror(errno));
226 rc->write = wpipe[1];
231 /* drop root privs permanently */
236 if(!xamandad_path || strlen(xamandad_path) <= 1)
237 xamandad_path = vstralloc(amlibexecdir, "/", "amandad", NULL);
238 if(!xclient_username || strlen(xclient_username) <= 1)
239 xclient_username = CLIENT_LOGIN;
241 execlp(RSH_PATH, RSH_PATH, "-l", xclient_username,
242 rc->hostname, xamandad_path, "-auth=rsh", "amdump", "amindexd",
243 "amidxtaped", (char *)NULL);
244 error(_("error: couldn't exec %s: %s"), RSH_PATH, strerror(errno));
246 /* should never go here, shut up compiler warning */