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