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