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