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