Imported Upstream version 3.2.0
[debian/amanda] / common-src / rsh-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: rsh-security.c,v 1.31 2006/08/21 20:17:10 martinea Exp $
29  *
30  * rsh-security.c - security and transport over rsh or a rsh-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 rsh 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 "security.h"
41 #include "security-util.h"
42 #include "stream.h"
43
44 /*
45  * Path to the rsh binary.  This should be configurable.
46  */
47 #define RSH_PATH        "/usr/bin/rsh"
48
49 /*
50  * Arguments to rsh.  This should also be configurable
51  */
52 /*#define       RSH_ARGS        */
53
54 /*
55  * Number of seconds rsh has to start up
56  */
57 #define CONNECT_TIMEOUT 20
58
59 /*
60  * Magic values for rsh_conn->handle
61  */
62 #define H_TAKEN -1              /* rsh_conn->tok was already read */
63 #define H_EOF   -2              /* this connection has been shut down */
64
65 /*
66  * Interface functions
67  */
68 static void rsh_connect(const char *, char *(*)(char *, void *),
69                         void (*)(void *, security_handle_t *, security_status_t),
70                         void *, void *);
71
72 /*
73  * This is our interface to the outside world.
74  */
75 const security_driver_t rsh_security_driver = {
76     "RSH",
77     rsh_connect,
78     sec_accept,
79     sec_get_authenticated_peer_name_hostname,
80     sec_close,
81     stream_sendpkt,
82     stream_recvpkt,
83     stream_recvpkt_cancel,
84     tcpma_stream_server,
85     tcpma_stream_accept,
86     tcpma_stream_client,
87     tcpma_stream_close,
88     sec_stream_auth,
89     sec_stream_id,
90     tcpm_stream_write,
91     tcpm_stream_read,
92     tcpm_stream_read_sync,
93     tcpm_stream_read_cancel,
94     tcpm_close_connection,
95     NULL,
96     NULL
97 };
98
99 static int newhandle = 1;
100
101 /*
102  * Local functions
103  */
104 static int runrsh(struct tcp_conn *, const char *, const char *);
105
106
107 /*
108  * rsh version of a security handle allocator.  Logically sets
109  * up a network "connection".
110  */
111 static void
112 rsh_connect(
113     const char *        hostname,
114     char *              (*conf_fn)(char *, void *),
115     void                (*fn)(void *, security_handle_t *, security_status_t),
116     void *              arg,
117     void *              datap)
118 {
119     int result;
120     struct sec_handle *rh;
121     char *amandad_path=NULL, *client_username=NULL;
122
123     assert(fn != NULL);
124     assert(hostname != NULL);
125
126     auth_debug(1, _("rsh: rsh_connect: %s\n"), hostname);
127
128     rh = alloc(SIZEOF(*rh));
129     security_handleinit(&rh->sech, &rsh_security_driver);
130     rh->hostname = NULL;
131     rh->rs = NULL;
132     rh->ev_timeout = NULL;
133     rh->rc = NULL;
134
135     /* get the canonical hostname */
136     rh->hostname = NULL;
137     if ((result = resolve_hostname(hostname, 0, NULL, &rh->hostname)) || rh->hostname == NULL) {
138         security_seterror(&rh->sech,
139             _("rsh_security could not find canonical name for '%s': %s"),
140             hostname, gai_strerror(result));
141         (*fn)(arg, &rh->sech, S_ERROR);
142         return;
143     }
144     rh->rs = tcpma_stream_client(rh, newhandle++);
145
146     if (rh->rs == NULL)
147         goto error;
148
149     amfree(rh->hostname);
150     rh->hostname = stralloc(rh->rs->rc->hostname);
151
152     /*
153      * We need to open a new connection.
154      *
155      * XXX need to eventually limit number of outgoing connections here.
156      */
157     if(conf_fn) {
158         amandad_path    = conf_fn("amandad_path", datap);
159         client_username = conf_fn("client_username", datap);
160     }
161     if(rh->rc->read == -1) {
162         if (runrsh(rh->rs->rc, amandad_path, client_username) < 0) {
163             security_seterror(&rh->sech, _("can't connect to %s: %s"),
164                               hostname, rh->rs->rc->errmsg);
165             goto error;
166         }
167         rh->rc->refcnt++;
168     }
169
170     /*
171      * The socket will be opened async so hosts that are down won't
172      * block everything.  We need to register a write event
173      * so we will know when the socket comes alive.
174      *
175      * Overload rh->rs->ev_read to provide a write event handle.
176      * We also register a timeout.
177      */
178     rh->fn.connect = fn;
179     rh->arg = arg;
180     rh->rs->ev_read = event_register((event_id_t)rh->rs->rc->write, EV_WRITEFD,
181         sec_connect_callback, rh);
182     rh->ev_timeout = event_register((event_id_t)CONNECT_TIMEOUT, EV_TIME,
183         sec_connect_timeout, rh);
184
185     return;
186
187 error:
188     (*fn)(arg, &rh->sech, S_ERROR);
189 }
190
191 /*
192  * Forks a rsh to the host listed in rc->hostname
193  * Returns negative on error, with an errmsg in rc->errmsg.
194  */
195 static int
196 runrsh(
197     struct tcp_conn *   rc,
198     const char *        amandad_path,
199     const char *        client_username)
200 {
201     int rpipe[2], wpipe[2];
202     char *xamandad_path = (char *)amandad_path;
203     char *xclient_username = (char *)client_username;
204
205     memset(rpipe, -1, SIZEOF(rpipe));
206     memset(wpipe, -1, SIZEOF(wpipe));
207     if (pipe(rpipe) < 0 || pipe(wpipe) < 0) {
208         rc->errmsg = newvstrallocf(rc->errmsg, _("pipe: %s"), strerror(errno));
209         return (-1);
210     }
211
212     switch (rc->pid = fork()) {
213     case -1:
214         rc->errmsg = newvstrallocf(rc->errmsg, _("fork: %s"), strerror(errno));
215         aclose(rpipe[0]);
216         aclose(rpipe[1]);
217         aclose(wpipe[0]);
218         aclose(wpipe[1]);
219         return (-1);
220     case 0:
221         dup2(wpipe[0], 0);
222         dup2(rpipe[1], 1);
223         break;
224     default:
225         rc->read = rpipe[0];
226         aclose(rpipe[1]);
227         rc->write = wpipe[1];
228         aclose(wpipe[0]);
229         return (0);
230     }
231
232     /* drop root privs permanently */
233     set_root_privs(-1);
234
235     safe_fd(-1, 0);
236
237     if(!xamandad_path || strlen(xamandad_path) <= 1) 
238         xamandad_path = vstralloc(amlibexecdir, "/", "amandad", NULL);
239     if(!xclient_username || strlen(xclient_username) <= 1)
240         xclient_username = CLIENT_LOGIN;
241
242     execlp(RSH_PATH, RSH_PATH, "-l", xclient_username,
243            rc->hostname, xamandad_path, "-auth=rsh", "amdump", "amindexd",
244            "amidxtaped", (char *)NULL);
245     error(_("error: couldn't exec %s: %s"), RSH_PATH, strerror(errno));
246
247     /* should never go here, shut up compiler warning */
248     return(-1);
249 }