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