blob: c4f9a48fbdf56ccef7d7edee4e61373561d47b2e [file] [log] [blame]
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001/*
2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2002 Markus Friedl <markus@openbsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Miller0425d402003-11-17 22:18:21 +110028RCSID("$OpenBSD: monitor.c,v 1.52 2003/11/17 11:06:07 markus Exp $");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
30#include <openssl/dh.h>
31
32#ifdef SKEY
33#include <skey.h>
34#endif
35
36#include "ssh.h"
37#include "auth.h"
38#include "kex.h"
39#include "dh.h"
40#include "zlib.h"
41#include "packet.h"
42#include "auth-options.h"
43#include "sshpty.h"
44#include "channels.h"
45#include "session.h"
46#include "sshlogin.h"
47#include "canohost.h"
48#include "log.h"
49#include "servconf.h"
50#include "monitor.h"
51#include "monitor_mm.h"
52#include "monitor_wrap.h"
53#include "monitor_fdpass.h"
54#include "xmalloc.h"
55#include "misc.h"
56#include "buffer.h"
57#include "bufaux.h"
58#include "compat.h"
59#include "ssh2.h"
60#include "mpaux.h"
61
Darren Tucker0efd1552003-08-26 11:49:55 +100062#ifdef GSSAPI
63#include "ssh-gss.h"
64static Gssctxt *gsscontext = NULL;
65#endif
66
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000067/* Imports */
68extern ServerOptions options;
69extern u_int utmp_len;
70extern Newkeys *current_keys[];
71extern z_stream incoming_stream;
72extern z_stream outgoing_stream;
73extern u_char session_id[];
74extern Buffer input, output;
75extern Buffer auth_debug;
76extern int auth_debug_init;
77
78/* State exported from the child */
79
80struct {
81 z_stream incoming;
82 z_stream outgoing;
83 u_char *keyin;
84 u_int keyinlen;
85 u_char *keyout;
86 u_int keyoutlen;
87 u_char *ivin;
88 u_int ivinlen;
89 u_char *ivout;
90 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000091 u_char *ssh1key;
92 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093 int ssh1cipher;
94 int ssh1protoflags;
95 u_char *input;
96 u_int ilen;
97 u_char *output;
98 u_int olen;
99} child_state;
100
Damien Miller469954d2003-06-18 20:25:33 +1000101/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102
103int mm_answer_moduli(int, Buffer *);
104int mm_answer_sign(int, Buffer *);
105int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000106int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107int mm_answer_authserv(int, Buffer *);
108int mm_answer_authpassword(int, Buffer *);
109int mm_answer_bsdauthquery(int, Buffer *);
110int mm_answer_bsdauthrespond(int, Buffer *);
111int mm_answer_skeyquery(int, Buffer *);
112int mm_answer_skeyrespond(int, Buffer *);
113int mm_answer_keyallowed(int, Buffer *);
114int mm_answer_keyverify(int, Buffer *);
115int mm_answer_pty(int, Buffer *);
116int mm_answer_pty_cleanup(int, Buffer *);
117int mm_answer_term(int, Buffer *);
118int mm_answer_rsa_keyallowed(int, Buffer *);
119int mm_answer_rsa_challenge(int, Buffer *);
120int mm_answer_rsa_response(int, Buffer *);
121int mm_answer_sesskey(int, Buffer *);
122int mm_answer_sessid(int, Buffer *);
123
Damien Miller79418552002-04-23 20:28:48 +1000124#ifdef USE_PAM
125int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000126int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000127int mm_answer_pam_init_ctx(int, Buffer *);
128int mm_answer_pam_query(int, Buffer *);
129int mm_answer_pam_respond(int, Buffer *);
130int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000131#endif
132
Darren Tucker0efd1552003-08-26 11:49:55 +1000133#ifdef GSSAPI
134int mm_answer_gss_setup_ctx(int, Buffer *);
135int mm_answer_gss_accept_ctx(int, Buffer *);
136int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100137int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000138#endif
Damien Miller25162f22002-09-12 09:47:29 +1000139
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000140static Authctxt *authctxt;
141static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
142
143/* local state for key verify */
144static u_char *key_blob = NULL;
145static u_int key_bloblen = 0;
146static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000147static char *hostbased_cuser = NULL;
148static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000149static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000150static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000151static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000152static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000153
154struct mon_table {
155 enum monitor_reqtype type;
156 int flags;
157 int (*f)(int, Buffer *);
158};
159
160#define MON_ISAUTH 0x0004 /* Required for Authentication */
161#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
162#define MON_ONCE 0x0010 /* Disable after calling */
163
164#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
165
166#define MON_PERMIT 0x1000 /* Request is permitted */
167
168struct mon_table mon_dispatch_proto20[] = {
169 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
170 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
171 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
172 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000173 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000174 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000175#ifdef USE_PAM
176 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000177 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000178 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
179 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
180 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
181 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000182#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000183#ifdef BSD_AUTH
184 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
185 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
186#endif
187#ifdef SKEY
188 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
189 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
190#endif
191 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
192 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000193#ifdef GSSAPI
194 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
195 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
196 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100197 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000198#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000199 {0, 0, NULL}
200};
201
202struct mon_table mon_dispatch_postauth20[] = {
203 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
204 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
205 {MONITOR_REQ_PTY, 0, mm_answer_pty},
206 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
207 {MONITOR_REQ_TERM, 0, mm_answer_term},
208 {0, 0, NULL}
209};
210
211struct mon_table mon_dispatch_proto15[] = {
212 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
213 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
214 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
215 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
216 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
217 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
218 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
219 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
220#ifdef BSD_AUTH
221 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
222 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
223#endif
224#ifdef SKEY
225 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
226 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
227#endif
Damien Millera33501b2002-05-08 12:24:42 +1000228#ifdef USE_PAM
229 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000230 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000231 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
232 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
233 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
234 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000235#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000236 {0, 0, NULL}
237};
238
239struct mon_table mon_dispatch_postauth15[] = {
240 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
241 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
242 {MONITOR_REQ_TERM, 0, mm_answer_term},
243 {0, 0, NULL}
244};
245
246struct mon_table *mon_dispatch;
247
248/* Specifies if a certain message is allowed at the moment */
249
250static void
251monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
252{
253 while (ent->f != NULL) {
254 if (ent->type == type) {
255 ent->flags &= ~MON_PERMIT;
256 ent->flags |= permit ? MON_PERMIT : 0;
257 return;
258 }
259 ent++;
260 }
261}
262
263static void
264monitor_permit_authentications(int permit)
265{
266 struct mon_table *ent = mon_dispatch;
267
268 while (ent->f != NULL) {
269 if (ent->flags & MON_AUTH) {
270 ent->flags &= ~MON_PERMIT;
271 ent->flags |= permit ? MON_PERMIT : 0;
272 }
273 ent++;
274 }
275}
276
Darren Tucker3e33cec2003-10-02 16:12:36 +1000277void
278monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000279{
280 struct mon_table *ent;
281 int authenticated = 0;
282
283 debug3("preauth child monitor started");
284
Darren Tucker3e33cec2003-10-02 16:12:36 +1000285 authctxt = _authctxt;
286 memset(authctxt, 0, sizeof(*authctxt));
287
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000288 if (compat20) {
289 mon_dispatch = mon_dispatch_proto20;
290
291 /* Permit requests for moduli and signatures */
292 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
293 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
294 } else {
295 mon_dispatch = mon_dispatch_proto15;
296
297 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
298 }
299
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000300 /* The first few requests do not require asynchronous access */
301 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000302 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000303 if (authenticated) {
304 if (!(ent->flags & MON_AUTHDECIDE))
305 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000306 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000307 if (authctxt->pw->pw_uid == 0 &&
308 !auth_root_allowed(auth_method))
309 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000310#ifdef USE_PAM
311 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100312 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000313 Buffer m;
314
315 buffer_init(&m);
316 mm_request_receive_expect(pmonitor->m_sendfd,
317 MONITOR_REQ_PAM_ACCOUNT, &m);
318 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
319 buffer_free(&m);
320 }
321#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000322 }
323
324 if (ent->flags & MON_AUTHDECIDE) {
325 auth_log(authctxt, authenticated, auth_method,
326 compat20 ? " ssh2" : "");
327 if (!authenticated)
328 authctxt->failures++;
329 }
330 }
331
332 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000333 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000334
335 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000336 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000337
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000338 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339}
340
Damien Millerbe64d432003-05-14 19:31:12 +1000341static void
342monitor_set_child_handler(pid_t pid)
343{
344 monitor_child_pid = pid;
345}
346
347static void
348monitor_child_handler(int signal)
349{
350 kill(monitor_child_pid, signal);
351}
352
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000353void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000354monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000355{
Damien Millerbe64d432003-05-14 19:31:12 +1000356 monitor_set_child_handler(pmonitor->m_pid);
357 signal(SIGHUP, &monitor_child_handler);
358 signal(SIGTERM, &monitor_child_handler);
359
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360 if (compat20) {
361 mon_dispatch = mon_dispatch_postauth20;
362
363 /* Permit requests for moduli and signatures */
364 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
365 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
366 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000367 } else {
368 mon_dispatch = mon_dispatch_postauth15;
369 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
370 }
371 if (!no_pty_flag) {
372 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
373 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
374 }
375
376 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000377 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000378}
379
380void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000381monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000382{
Damien Miller2d6b8332002-06-21 15:59:49 +1000383 if (options.compression) {
384 /* The member allocation is not visible, so sync it */
385 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
386 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000387}
388
389int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000390monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000391 struct mon_table **pent)
392{
393 Buffer m;
394 int ret;
395 u_char type;
396
397 buffer_init(&m);
398
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000399 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000400 type = buffer_get_char(&m);
401
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000402 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000403
404 while (ent->f != NULL) {
405 if (ent->type == type)
406 break;
407 ent++;
408 }
409
410 if (ent->f != NULL) {
411 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000412 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000413 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000414 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000415 buffer_free(&m);
416
417 /* The child may use this request only once, disable it */
418 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000419 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000420 type);
421 ent->flags &= ~MON_PERMIT;
422 }
423
424 if (pent != NULL)
425 *pent = ent;
426
427 return ret;
428 }
429
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000430 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000431
432 /* NOTREACHED */
433 return (-1);
434}
435
436/* allowed key state */
437static int
438monitor_allowed_key(u_char *blob, u_int bloblen)
439{
440 /* make sure key is allowed */
441 if (key_blob == NULL || key_bloblen != bloblen ||
442 memcmp(key_blob, blob, key_bloblen))
443 return (0);
444 return (1);
445}
446
447static void
448monitor_reset_key_state(void)
449{
450 /* reset state */
451 if (key_blob != NULL)
452 xfree(key_blob);
453 if (hostbased_cuser != NULL)
454 xfree(hostbased_cuser);
455 if (hostbased_chost != NULL)
456 xfree(hostbased_chost);
457 key_blob = NULL;
458 key_bloblen = 0;
459 key_blobtype = MM_NOKEY;
460 hostbased_cuser = NULL;
461 hostbased_chost = NULL;
462}
463
464int
465mm_answer_moduli(int socket, Buffer *m)
466{
467 DH *dh;
468 int min, want, max;
469
470 min = buffer_get_int(m);
471 want = buffer_get_int(m);
472 max = buffer_get_int(m);
473
474 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000475 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000476 /* We need to check here, too, in case the child got corrupted */
477 if (max < min || want < min || max < want)
478 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000479 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000480
481 buffer_clear(m);
482
483 dh = choose_dh(min, want, max);
484 if (dh == NULL) {
485 buffer_put_char(m, 0);
486 return (0);
487 } else {
488 /* Send first bignum */
489 buffer_put_char(m, 1);
490 buffer_put_bignum2(m, dh->p);
491 buffer_put_bignum2(m, dh->g);
492
493 DH_free(dh);
494 }
495 mm_request_send(socket, MONITOR_ANS_MODULI, m);
496 return (0);
497}
498
499int
500mm_answer_sign(int socket, Buffer *m)
501{
502 Key *key;
503 u_char *p;
504 u_char *signature;
505 u_int siglen, datlen;
506 int keyid;
507
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000508 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000509
510 keyid = buffer_get_int(m);
511 p = buffer_get_string(m, &datlen);
512
513 if (datlen != 20)
Ben Lindstromd5502182002-06-27 00:12:57 +0000514 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000515
Ben Lindstromf67e0772002-06-06 20:58:19 +0000516 /* save session id, it will be passed on the first call */
517 if (session_id2_len == 0) {
518 session_id2_len = datlen;
519 session_id2 = xmalloc(session_id2_len);
520 memcpy(session_id2, p, session_id2_len);
521 }
522
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000524 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000525 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000526 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000527
Ben Lindstromd5502182002-06-27 00:12:57 +0000528 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000529
530 buffer_clear(m);
531 buffer_put_string(m, signature, siglen);
532
533 xfree(p);
534 xfree(signature);
535
536 mm_request_send(socket, MONITOR_ANS_SIGN, m);
537
538 /* Turn on permissions for getpwnam */
539 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
540
541 return (0);
542}
543
544/* Retrieves the password entry and also checks if the user is permitted */
545
546int
547mm_answer_pwnamallow(int socket, Buffer *m)
548{
549 char *login;
550 struct passwd *pwent;
551 int allowed = 0;
552
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000553 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000554
555 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000556 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000557
558 login = buffer_get_string(m, NULL);
559
560 pwent = getpwnamallow(login);
561
562 authctxt->user = xstrdup(login);
563 setproctitle("%s [priv]", pwent ? login : "unknown");
564 xfree(login);
565
566 buffer_clear(m);
567
568 if (pwent == NULL) {
569 buffer_put_char(m, 0);
570 goto out;
571 }
572
573 allowed = 1;
574 authctxt->pw = pwent;
575 authctxt->valid = 1;
576
577 buffer_put_char(m, 1);
578 buffer_put_string(m, pwent, sizeof(struct passwd));
579 buffer_put_cstring(m, pwent->pw_name);
580 buffer_put_cstring(m, "*");
581 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000582#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000583 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000584#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585 buffer_put_cstring(m, pwent->pw_dir);
586 buffer_put_cstring(m, pwent->pw_shell);
587
588 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000589 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
591
592 /* For SSHv1 allow authentication now */
593 if (!compat20)
594 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000595 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596 /* Allow service/style information on the auth context */
597 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000598 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
599 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600
Damien Millera33501b2002-05-08 12:24:42 +1000601#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000602 if (options.use_pam)
603 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000604#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605
606 return (0);
607}
608
Damien Miller5ad9fd92002-05-13 11:07:41 +1000609int mm_answer_auth2_read_banner(int socket, Buffer *m)
610{
611 char *banner;
612
613 buffer_clear(m);
614 banner = auth2_read_banner();
615 buffer_put_cstring(m, banner != NULL ? banner : "");
616 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
617
618 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000619 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000620
621 return (0);
622}
623
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000624int
625mm_answer_authserv(int socket, Buffer *m)
626{
627 monitor_permit_authentications(1);
628
629 authctxt->service = buffer_get_string(m, NULL);
630 authctxt->style = buffer_get_string(m, NULL);
631 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000632 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000633
634 if (strlen(authctxt->style) == 0) {
635 xfree(authctxt->style);
636 authctxt->style = NULL;
637 }
638
639 return (0);
640}
641
642int
643mm_answer_authpassword(int socket, Buffer *m)
644{
645 static int call_count;
646 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000647 int authenticated;
648 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649
650 passwd = buffer_get_string(m, &plen);
651 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000652 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000653 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000654 memset(passwd, 0, strlen(passwd));
655 xfree(passwd);
656
657 buffer_clear(m);
658 buffer_put_int(m, authenticated);
659
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000660 debug3("%s: sending result %d", __func__, authenticated);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000661 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
662
663 call_count++;
664 if (plen == 0 && call_count == 1)
665 auth_method = "none";
666 else
667 auth_method = "password";
668
669 /* Causes monitor loop to terminate if authenticated */
670 return (authenticated);
671}
672
673#ifdef BSD_AUTH
674int
675mm_answer_bsdauthquery(int socket, Buffer *m)
676{
677 char *name, *infotxt;
678 u_int numprompts;
679 u_int *echo_on;
680 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100681 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682
Damien Millerb7df3af2003-02-24 11:55:46 +1100683 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
684 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685
686 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100687 buffer_put_int(m, success);
688 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000689 buffer_put_cstring(m, prompts[0]);
690
Damien Millerb7df3af2003-02-24 11:55:46 +1100691 debug3("%s: sending challenge success: %u", __func__, success);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
693
Damien Millerb7df3af2003-02-24 11:55:46 +1100694 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000695 xfree(name);
696 xfree(infotxt);
697 xfree(prompts);
698 xfree(echo_on);
699 }
700
701 return (0);
702}
703
704int
705mm_answer_bsdauthrespond(int socket, Buffer *m)
706{
707 char *response;
708 int authok;
709
710 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000711 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712
713 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000714 authok = options.challenge_response_authentication &&
715 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000716 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000717 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000718 xfree(response);
719
720 buffer_clear(m);
721 buffer_put_int(m, authok);
722
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000723 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000724 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
725
726 auth_method = "bsdauth";
727
728 return (authok != 0);
729}
730#endif
731
732#ifdef SKEY
733int
734mm_answer_skeyquery(int socket, Buffer *m)
735{
736 struct skey skey;
737 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100738 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739
Damien Millerb7df3af2003-02-24 11:55:46 +1100740 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000741
742 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100743 buffer_put_int(m, success);
744 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000745 buffer_put_cstring(m, challenge);
746
Damien Millerb7df3af2003-02-24 11:55:46 +1100747 debug3("%s: sending challenge success: %u", __func__, success);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
749
750 return (0);
751}
752
753int
754mm_answer_skeyrespond(int socket, Buffer *m)
755{
756 char *response;
757 int authok;
758
759 response = buffer_get_string(m, NULL);
760
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000761 authok = (options.challenge_response_authentication &&
762 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000763 skey_haskey(authctxt->pw->pw_name) == 0 &&
764 skey_passcheck(authctxt->pw->pw_name, response) != -1);
765
766 xfree(response);
767
768 buffer_clear(m);
769 buffer_put_int(m, authok);
770
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000771 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000772 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
773
774 auth_method = "skey";
775
776 return (authok != 0);
777}
778#endif
779
Damien Miller79418552002-04-23 20:28:48 +1000780#ifdef USE_PAM
781int
782mm_answer_pam_start(int socket, Buffer *m)
783{
784 char *user;
785
Damien Miller4e448a32003-05-14 15:11:48 +1000786 if (!options.use_pam)
787 fatal("UsePAM not set, but ended up in %s anyway", __func__);
788
Damien Miller79418552002-04-23 20:28:48 +1000789 user = buffer_get_string(m, NULL);
790
791 start_pam(user);
792
793 xfree(user);
794
Damien Miller1f499fd2003-08-25 13:08:49 +1000795 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
796
Damien Miller79418552002-04-23 20:28:48 +1000797 return (0);
798}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000799
Damien Miller1f499fd2003-08-25 13:08:49 +1000800int
801mm_answer_pam_account(int socket, Buffer *m)
802{
803 u_int ret;
804
805 if (!options.use_pam)
806 fatal("UsePAM not set, but ended up in %s anyway", __func__);
807
808 ret = do_pam_account();
809
810 buffer_put_int(m, ret);
811
812 mm_request_send(socket, MONITOR_ANS_PAM_ACCOUNT, m);
813
814 return (ret);
815}
816
Damien Miller4f9f42a2003-05-10 19:28:02 +1000817static void *sshpam_ctxt, *sshpam_authok;
818extern KbdintDevice sshpam_device;
819
820int
821mm_answer_pam_init_ctx(int socket, Buffer *m)
822{
823
824 debug3("%s", __func__);
825 authctxt->user = buffer_get_string(m, NULL);
826 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
827 sshpam_authok = NULL;
828 buffer_clear(m);
829 if (sshpam_ctxt != NULL) {
830 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
831 buffer_put_int(m, 1);
832 } else {
833 buffer_put_int(m, 0);
834 }
835 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
836 return (0);
837}
838
839int
840mm_answer_pam_query(int socket, Buffer *m)
841{
842 char *name, *info, **prompts;
843 u_int num, *echo_on;
844 int i, ret;
845
846 debug3("%s", __func__);
847 sshpam_authok = NULL;
848 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
849 if (ret == 0 && num == 0)
850 sshpam_authok = sshpam_ctxt;
851 if (num > 1 || name == NULL || info == NULL)
852 ret = -1;
853 buffer_clear(m);
854 buffer_put_int(m, ret);
855 buffer_put_cstring(m, name);
856 xfree(name);
857 buffer_put_cstring(m, info);
858 xfree(info);
859 buffer_put_int(m, num);
860 for (i = 0; i < num; ++i) {
861 buffer_put_cstring(m, prompts[i]);
862 xfree(prompts[i]);
863 buffer_put_int(m, echo_on[i]);
864 }
865 if (prompts != NULL)
866 xfree(prompts);
867 if (echo_on != NULL)
868 xfree(echo_on);
869 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
870 return (0);
871}
872
873int
874mm_answer_pam_respond(int socket, Buffer *m)
875{
876 char **resp;
877 u_int num;
878 int i, ret;
879
880 debug3("%s", __func__);
881 sshpam_authok = NULL;
882 num = buffer_get_int(m);
883 if (num > 0) {
884 resp = xmalloc(num * sizeof(char *));
885 for (i = 0; i < num; ++i)
886 resp[i] = buffer_get_string(m, NULL);
887 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
888 for (i = 0; i < num; ++i)
889 xfree(resp[i]);
890 xfree(resp);
891 } else {
892 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
893 }
894 buffer_clear(m);
895 buffer_put_int(m, ret);
896 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
897 auth_method = "keyboard-interactive/pam";
898 if (ret == 0)
899 sshpam_authok = sshpam_ctxt;
900 return (0);
901}
902
903int
904mm_answer_pam_free_ctx(int socket, Buffer *m)
905{
906
907 debug3("%s", __func__);
908 (sshpam_device.free_ctx)(sshpam_ctxt);
909 buffer_clear(m);
910 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
911 return (sshpam_authok == sshpam_ctxt);
912}
Damien Miller79418552002-04-23 20:28:48 +1000913#endif
914
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000915static void
916mm_append_debug(Buffer *m)
917{
918 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000919 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000920 buffer_append(m, buffer_ptr(&auth_debug),
921 buffer_len(&auth_debug));
922 buffer_clear(&auth_debug);
923 }
924}
925
926int
927mm_answer_keyallowed(int socket, Buffer *m)
928{
929 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000930 char *cuser, *chost;
931 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932 u_int bloblen;
933 enum mm_keytype type = 0;
934 int allowed = 0;
935
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000936 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000937
938 type = buffer_get_int(m);
939 cuser = buffer_get_string(m, NULL);
940 chost = buffer_get_string(m, NULL);
941 blob = buffer_get_string(m, &bloblen);
942
943 key = key_from_blob(blob, bloblen);
944
945 if ((compat20 && type == MM_RSAHOSTKEY) ||
946 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000947 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000948
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000949 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000950
Damien Miller3e3b5142003-11-17 21:13:40 +1100951 if (key != NULL && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952 switch(type) {
953 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000954 allowed = options.pubkey_authentication &&
955 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000956 break;
957 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000958 allowed = options.hostbased_authentication &&
959 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000960 cuser, chost, key);
961 break;
962 case MM_RSAHOSTKEY:
963 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000964 allowed = options.rhosts_rsa_authentication &&
965 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966 cuser, chost, key);
967 break;
968 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000969 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000970 break;
971 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972 }
Damien Miller00111382003-03-10 11:21:17 +1100973 if (key != NULL)
974 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000975
976 /* clear temporarily storage (used by verify) */
977 monitor_reset_key_state();
978
979 if (allowed) {
980 /* Save temporarily for comparison in verify */
981 key_blob = blob;
982 key_bloblen = bloblen;
983 key_blobtype = type;
984 hostbased_cuser = cuser;
985 hostbased_chost = chost;
986 }
987
988 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000989 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000990
991 buffer_clear(m);
992 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +1100993 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000994
995 mm_append_debug(m);
996
997 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
998
999 if (type == MM_RSAHOSTKEY)
1000 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1001
1002 return (0);
1003}
1004
1005static int
1006monitor_valid_userblob(u_char *data, u_int datalen)
1007{
1008 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001009 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001010 u_int len;
1011 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001012
1013 buffer_init(&b);
1014 buffer_append(&b, data, datalen);
1015
1016 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001017 p = buffer_ptr(&b);
1018 len = buffer_len(&b);
1019 if ((session_id2 == NULL) ||
1020 (len < session_id2_len) ||
1021 (memcmp(p, session_id2, session_id2_len) != 0))
1022 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001023 buffer_consume(&b, session_id2_len);
1024 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001025 p = buffer_get_string(&b, &len);
1026 if ((session_id2 == NULL) ||
1027 (len != session_id2_len) ||
1028 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001029 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001030 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031 }
1032 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1033 fail++;
1034 p = buffer_get_string(&b, NULL);
1035 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001036 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001037 authctxt->user, p);
1038 fail++;
1039 }
1040 xfree(p);
1041 buffer_skip_string(&b);
1042 if (datafellows & SSH_BUG_PKAUTH) {
1043 if (!buffer_get_char(&b))
1044 fail++;
1045 } else {
1046 p = buffer_get_string(&b, NULL);
1047 if (strcmp("publickey", p) != 0)
1048 fail++;
1049 xfree(p);
1050 if (!buffer_get_char(&b))
1051 fail++;
1052 buffer_skip_string(&b);
1053 }
1054 buffer_skip_string(&b);
1055 if (buffer_len(&b) != 0)
1056 fail++;
1057 buffer_free(&b);
1058 return (fail == 0);
1059}
1060
1061static int
Damien Millera10f5612002-09-12 09:49:15 +10001062monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1063 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064{
1065 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001066 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001067 u_int len;
1068 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001069
1070 buffer_init(&b);
1071 buffer_append(&b, data, datalen);
1072
Ben Lindstromf67e0772002-06-06 20:58:19 +00001073 p = buffer_get_string(&b, &len);
1074 if ((session_id2 == NULL) ||
1075 (len != session_id2_len) ||
1076 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001077 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001078 xfree(p);
1079
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001080 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1081 fail++;
1082 p = buffer_get_string(&b, NULL);
1083 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001084 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001085 authctxt->user, p);
1086 fail++;
1087 }
1088 xfree(p);
1089 buffer_skip_string(&b); /* service */
1090 p = buffer_get_string(&b, NULL);
1091 if (strcmp(p, "hostbased") != 0)
1092 fail++;
1093 xfree(p);
1094 buffer_skip_string(&b); /* pkalg */
1095 buffer_skip_string(&b); /* pkblob */
1096
1097 /* verify client host, strip trailing dot if necessary */
1098 p = buffer_get_string(&b, NULL);
1099 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1100 p[len - 1] = '\0';
1101 if (strcmp(p, chost) != 0)
1102 fail++;
1103 xfree(p);
1104
1105 /* verify client user */
1106 p = buffer_get_string(&b, NULL);
1107 if (strcmp(p, cuser) != 0)
1108 fail++;
1109 xfree(p);
1110
1111 if (buffer_len(&b) != 0)
1112 fail++;
1113 buffer_free(&b);
1114 return (fail == 0);
1115}
1116
1117int
1118mm_answer_keyverify(int socket, Buffer *m)
1119{
1120 Key *key;
1121 u_char *signature, *data, *blob;
1122 u_int signaturelen, datalen, bloblen;
1123 int verified = 0;
1124 int valid_data = 0;
1125
1126 blob = buffer_get_string(m, &bloblen);
1127 signature = buffer_get_string(m, &signaturelen);
1128 data = buffer_get_string(m, &datalen);
1129
1130 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001131 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001132 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001133
1134 key = key_from_blob(blob, bloblen);
1135 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001136 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001137
1138 switch (key_blobtype) {
1139 case MM_USERKEY:
1140 valid_data = monitor_valid_userblob(data, datalen);
1141 break;
1142 case MM_HOSTKEY:
1143 valid_data = monitor_valid_hostbasedblob(data, datalen,
1144 hostbased_cuser, hostbased_chost);
1145 break;
1146 default:
1147 valid_data = 0;
1148 break;
1149 }
1150 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001151 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001152
1153 verified = key_verify(key, signature, signaturelen, data, datalen);
1154 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001155 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001156
1157 key_free(key);
1158 xfree(blob);
1159 xfree(signature);
1160 xfree(data);
1161
Ben Lindstrome1c09122002-06-23 00:38:24 +00001162 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1163
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001164 monitor_reset_key_state();
1165
1166 buffer_clear(m);
1167 buffer_put_int(m, verified);
1168 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1169
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001170 return (verified);
1171}
1172
1173static void
1174mm_record_login(Session *s, struct passwd *pw)
1175{
1176 socklen_t fromlen;
1177 struct sockaddr_storage from;
1178
1179 /*
1180 * Get IP address of client. If the connection is not a socket, let
1181 * the address be 0.0.0.0.
1182 */
1183 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001184 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001185 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001186 if (getpeername(packet_get_connection_in(),
1187 (struct sockaddr *) & from, &fromlen) < 0) {
1188 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001189 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001190 }
1191 }
1192 /* Record that there was a login on that tty from the remote host. */
1193 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001194 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001195 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001196}
1197
1198static void
1199mm_session_close(Session *s)
1200{
Damien Miller04bd8b02003-05-25 14:38:33 +10001201 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001203 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001204 session_pty_cleanup2(s);
1205 }
1206 s->used = 0;
1207}
1208
1209int
1210mm_answer_pty(int socket, Buffer *m)
1211{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001212 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001213 Session *s;
1214 int res, fd0;
1215
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001216 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001217
1218 buffer_clear(m);
1219 s = session_new();
1220 if (s == NULL)
1221 goto error;
1222 s->authctxt = authctxt;
1223 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001224 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001225 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1226 if (res == 0)
1227 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001228 pty_setowner(authctxt->pw, s->tty);
1229
1230 buffer_put_int(m, 1);
1231 buffer_put_cstring(m, s->tty);
1232 mm_request_send(socket, MONITOR_ANS_PTY, m);
1233
1234 mm_send_fd(socket, s->ptyfd);
1235 mm_send_fd(socket, s->ttyfd);
1236
1237 /* We need to trick ttyslot */
1238 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001239 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001240
1241 mm_record_login(s, authctxt->pw);
1242
1243 /* Now we can close the file descriptor again */
1244 close(0);
1245
1246 /* make sure nothing uses fd 0 */
1247 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001248 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001249 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001250 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001251
1252 /* slave is not needed */
1253 close(s->ttyfd);
1254 s->ttyfd = s->ptyfd;
1255 /* no need to dup() because nobody closes ptyfd */
1256 s->ptymaster = s->ptyfd;
1257
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001258 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001259
1260 return (0);
1261
1262 error:
1263 if (s != NULL)
1264 mm_session_close(s);
1265 buffer_put_int(m, 0);
1266 mm_request_send(socket, MONITOR_ANS_PTY, m);
1267 return (0);
1268}
1269
1270int
1271mm_answer_pty_cleanup(int socket, Buffer *m)
1272{
1273 Session *s;
1274 char *tty;
1275
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001276 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001277
1278 tty = buffer_get_string(m, NULL);
1279 if ((s = session_by_tty(tty)) != NULL)
1280 mm_session_close(s);
1281 buffer_clear(m);
1282 xfree(tty);
1283 return (0);
1284}
1285
1286int
1287mm_answer_sesskey(int socket, Buffer *m)
1288{
1289 BIGNUM *p;
1290 int rsafail;
1291
1292 /* Turn off permissions */
1293 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1294
1295 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001296 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001297
1298 buffer_get_bignum2(m, p);
1299
1300 rsafail = ssh1_session_key(p);
1301
1302 buffer_clear(m);
1303 buffer_put_int(m, rsafail);
1304 buffer_put_bignum2(m, p);
1305
1306 BN_clear_free(p);
1307
1308 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1309
1310 /* Turn on permissions for sessid passing */
1311 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1312
1313 return (0);
1314}
1315
1316int
1317mm_answer_sessid(int socket, Buffer *m)
1318{
1319 int i;
1320
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001321 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001322
1323 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001324 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001325 for (i = 0; i < 16; i++)
1326 session_id[i] = buffer_get_char(m);
1327
1328 /* Turn on permissions for getpwnam */
1329 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1330
1331 return (0);
1332}
1333
1334int
1335mm_answer_rsa_keyallowed(int socket, Buffer *m)
1336{
1337 BIGNUM *client_n;
1338 Key *key = NULL;
1339 u_char *blob = NULL;
1340 u_int blen = 0;
1341 int allowed = 0;
1342
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001343 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001344
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001345 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001346 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001347 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001348 buffer_get_bignum2(m, client_n);
1349 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1350 BN_clear_free(client_n);
1351 }
1352 buffer_clear(m);
1353 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001354 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001355
1356 /* clear temporarily storage (used by generate challenge) */
1357 monitor_reset_key_state();
1358
1359 if (allowed && key != NULL) {
1360 key->type = KEY_RSA; /* cheat for key_to_blob */
1361 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001362 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001363 buffer_put_string(m, blob, blen);
1364
1365 /* Save temporarily for comparison in verify */
1366 key_blob = blob;
1367 key_bloblen = blen;
1368 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001369 }
Damien Miller00111382003-03-10 11:21:17 +11001370 if (key != NULL)
1371 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001372
1373 mm_append_debug(m);
1374
1375 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1376
1377 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1378 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1379 return (0);
1380}
1381
1382int
1383mm_answer_rsa_challenge(int socket, Buffer *m)
1384{
1385 Key *key = NULL;
1386 u_char *blob;
1387 u_int blen;
1388
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001389 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001390
1391 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001392 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001393 blob = buffer_get_string(m, &blen);
1394 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001395 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001396 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001397 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001398 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001399 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001400
1401 if (ssh1_challenge)
1402 BN_clear_free(ssh1_challenge);
1403 ssh1_challenge = auth_rsa_generate_challenge(key);
1404
1405 buffer_clear(m);
1406 buffer_put_bignum2(m, ssh1_challenge);
1407
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001408 debug3("%s sending reply", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001409 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1410
1411 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001412
1413 xfree(blob);
1414 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001415 return (0);
1416}
1417
1418int
1419mm_answer_rsa_response(int socket, Buffer *m)
1420{
1421 Key *key = NULL;
1422 u_char *blob, *response;
1423 u_int blen, len;
1424 int success;
1425
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001426 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001427
1428 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001429 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001430 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001431 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001432
1433 blob = buffer_get_string(m, &blen);
1434 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001435 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001436 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001437 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001438 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001439 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440 response = buffer_get_string(m, &len);
1441 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001442 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001443 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1444
Damien Miller00111382003-03-10 11:21:17 +11001445 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446 key_free(key);
1447 xfree(response);
1448
1449 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1450
1451 /* reset state */
1452 BN_clear_free(ssh1_challenge);
1453 ssh1_challenge = NULL;
1454 monitor_reset_key_state();
1455
1456 buffer_clear(m);
1457 buffer_put_int(m, success);
1458 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1459
1460 return (success);
1461}
1462
1463int
1464mm_answer_term(int socket, Buffer *req)
1465{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001466 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001467 int res, status;
1468
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001469 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001470
1471 /* The child is terminating */
1472 session_destroy_all(&mm_session_close);
1473
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001474 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001475 if (errno != EINTR)
1476 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001477
1478 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1479
1480 /* Terminate process */
1481 exit (res);
1482}
1483
1484void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001485monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486{
1487 if (compat20) {
1488 set_newkeys(MODE_IN);
1489 set_newkeys(MODE_OUT);
1490 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001491 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001492 packet_set_encryption_key(child_state.ssh1key,
1493 child_state.ssh1keylen, child_state.ssh1cipher);
1494 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001495 }
1496
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001497 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001498 packet_set_keycontext(MODE_OUT, child_state.keyout);
1499 xfree(child_state.keyout);
1500 packet_set_keycontext(MODE_IN, child_state.keyin);
1501 xfree(child_state.keyin);
1502
1503 if (!compat20) {
1504 packet_set_iv(MODE_OUT, child_state.ivout);
1505 xfree(child_state.ivout);
1506 packet_set_iv(MODE_IN, child_state.ivin);
1507 xfree(child_state.ivin);
1508 }
1509
1510 memcpy(&incoming_stream, &child_state.incoming,
1511 sizeof(incoming_stream));
1512 memcpy(&outgoing_stream, &child_state.outgoing,
1513 sizeof(outgoing_stream));
1514
1515 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001516 if (options.compression)
1517 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001518
1519 /* Network I/O buffers */
1520 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1521 buffer_clear(&input);
1522 buffer_append(&input, child_state.input, child_state.ilen);
1523 memset(child_state.input, 0, child_state.ilen);
1524 xfree(child_state.input);
1525
1526 buffer_clear(&output);
1527 buffer_append(&output, child_state.output, child_state.olen);
1528 memset(child_state.output, 0, child_state.olen);
1529 xfree(child_state.output);
1530}
1531
1532static Kex *
1533mm_get_kex(Buffer *m)
1534{
1535 Kex *kex;
1536 void *blob;
1537 u_int bloblen;
1538
1539 kex = xmalloc(sizeof(*kex));
1540 memset(kex, 0, sizeof(*kex));
1541 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001542 if ((session_id2 == NULL) ||
1543 (kex->session_id_len != session_id2_len) ||
1544 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1545 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001546 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001547 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1548 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001549 kex->server = 1;
1550 kex->hostkey_type = buffer_get_int(m);
1551 kex->kex_type = buffer_get_int(m);
1552 blob = buffer_get_string(m, &bloblen);
1553 buffer_init(&kex->my);
1554 buffer_append(&kex->my, blob, bloblen);
1555 xfree(blob);
1556 blob = buffer_get_string(m, &bloblen);
1557 buffer_init(&kex->peer);
1558 buffer_append(&kex->peer, blob, bloblen);
1559 xfree(blob);
1560 kex->done = 1;
1561 kex->flags = buffer_get_int(m);
1562 kex->client_version_string = buffer_get_string(m, NULL);
1563 kex->server_version_string = buffer_get_string(m, NULL);
1564 kex->load_host_key=&get_hostkey_by_type;
1565 kex->host_key_index=&get_hostkey_index;
1566
1567 return (kex);
1568}
1569
1570/* This function requries careful sanity checking */
1571
1572void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001573mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001574{
1575 Buffer m;
1576 u_char *blob, *p;
1577 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001578 u_int32_t seqnr, packets;
1579 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001580
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001581 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001582
1583 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001584 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001585 if (!compat20) {
1586 child_state.ssh1protoflags = buffer_get_int(&m);
1587 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001588 child_state.ssh1key = buffer_get_string(&m,
1589 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001590 child_state.ivout = buffer_get_string(&m,
1591 &child_state.ivoutlen);
1592 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1593 goto skip;
1594 } else {
1595 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001596 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001597 }
1598
1599 blob = buffer_get_string(&m, &bloblen);
1600 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1601 xfree(blob);
1602
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001603 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001604 blob = buffer_get_string(&m, &bloblen);
1605 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1606 xfree(blob);
1607
1608 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001609 seqnr = buffer_get_int(&m);
1610 blocks = buffer_get_int64(&m);
1611 packets = buffer_get_int(&m);
1612 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1613 seqnr = buffer_get_int(&m);
1614 blocks = buffer_get_int64(&m);
1615 packets = buffer_get_int(&m);
1616 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001617
1618 skip:
1619 /* Get the key context */
1620 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1621 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1622
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001623 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001624 /* Get compression state */
1625 p = buffer_get_string(&m, &plen);
1626 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001627 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001628 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1629 xfree(p);
1630
1631 p = buffer_get_string(&m, &plen);
1632 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001633 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001634 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1635 xfree(p);
1636
1637 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001638 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001639 child_state.input = buffer_get_string(&m, &child_state.ilen);
1640 child_state.output = buffer_get_string(&m, &child_state.olen);
1641
1642 buffer_free(&m);
1643}
1644
1645
1646/* Allocation functions for zlib */
1647void *
1648mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1649{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001650 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001651 void *address;
1652
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001653 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001654 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1655
1656 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001657
1658 return (address);
1659}
1660
1661void
1662mm_zfree(struct mm_master *mm, void *address)
1663{
1664 mm_free(mm, address);
1665}
1666
1667void
1668mm_init_compression(struct mm_master *mm)
1669{
1670 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1671 outgoing_stream.zfree = (free_func)mm_zfree;
1672 outgoing_stream.opaque = mm;
1673
1674 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1675 incoming_stream.zfree = (free_func)mm_zfree;
1676 incoming_stream.opaque = mm;
1677}
1678
1679/* XXX */
1680
1681#define FD_CLOSEONEXEC(x) do { \
1682 if (fcntl(x, F_SETFD, 1) == -1) \
1683 fatal("fcntl(%d, F_SETFD)", x); \
1684} while (0)
1685
1686static void
1687monitor_socketpair(int *pair)
1688{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001689#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001690 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001691 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001692#else
1693 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001694 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001695#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001696 FD_CLOSEONEXEC(pair[0]);
1697 FD_CLOSEONEXEC(pair[1]);
1698}
1699
1700#define MM_MEMSIZE 65536
1701
1702struct monitor *
1703monitor_init(void)
1704{
1705 struct monitor *mon;
1706 int pair[2];
1707
1708 mon = xmalloc(sizeof(*mon));
1709
1710 monitor_socketpair(pair);
1711
1712 mon->m_recvfd = pair[0];
1713 mon->m_sendfd = pair[1];
1714
1715 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001716 if (options.compression) {
1717 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1718 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001719
Damien Miller2d6b8332002-06-21 15:59:49 +10001720 /* Compression needs to share state across borders */
1721 mm_init_compression(mon->m_zlib);
1722 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001723
1724 return mon;
1725}
1726
1727void
1728monitor_reinit(struct monitor *mon)
1729{
1730 int pair[2];
1731
1732 monitor_socketpair(pair);
1733
1734 mon->m_recvfd = pair[0];
1735 mon->m_sendfd = pair[1];
1736}
Darren Tucker0efd1552003-08-26 11:49:55 +10001737
1738#ifdef GSSAPI
1739int
1740mm_answer_gss_setup_ctx(int socket, Buffer *m)
1741{
1742 gss_OID_desc oid;
1743 OM_uint32 major;
1744 u_int len;
1745
1746 oid.elements = buffer_get_string(m, &len);
1747 oid.length = len;
1748
1749 major = ssh_gssapi_server_ctx(&gsscontext, &oid);
1750
1751 xfree(oid.elements);
1752
1753 buffer_clear(m);
1754 buffer_put_int(m, major);
1755
1756 mm_request_send(socket,MONITOR_ANS_GSSSETUP, m);
1757
1758 /* Now we have a context, enable the step */
1759 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1760
1761 return (0);
1762}
1763
1764int
1765mm_answer_gss_accept_ctx(int socket, Buffer *m)
1766{
1767 gss_buffer_desc in;
1768 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1769 OM_uint32 major,minor;
1770 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001771 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001772
Darren Tucker600ad8d2003-08-26 12:10:48 +10001773 in.value = buffer_get_string(m, &len);
1774 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001775 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1776 xfree(in.value);
1777
1778 buffer_clear(m);
1779 buffer_put_int(m, major);
1780 buffer_put_string(m, out.value, out.length);
1781 buffer_put_int(m, flags);
1782 mm_request_send(socket, MONITOR_ANS_GSSSTEP, m);
1783
1784 gss_release_buffer(&minor, &out);
1785
Darren Tucker0efd1552003-08-26 11:49:55 +10001786 if (major==GSS_S_COMPLETE) {
1787 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1788 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001789 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001790 }
1791 return (0);
1792}
1793
1794int
Damien Miller0425d402003-11-17 22:18:21 +11001795mm_answer_gss_checkmic(int socket, Buffer *m)
1796{
1797 gss_buffer_desc gssbuf, mic;
1798 OM_uint32 ret;
1799 u_int len;
1800
1801 gssbuf.value = buffer_get_string(m, &len);
1802 gssbuf.length = len;
1803 mic.value = buffer_get_string(m, &len);
1804 mic.length = len;
1805
1806 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1807
1808 xfree(gssbuf.value);
1809 xfree(mic.value);
1810
1811 buffer_clear(m);
1812 buffer_put_int(m, ret);
1813
1814 mm_request_send(socket, MONITOR_ANS_GSSCHECKMIC, m);
1815
1816 if (!GSS_ERROR(ret))
1817 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1818
1819 return (0);
1820}
1821
1822int
Darren Tucker0efd1552003-08-26 11:49:55 +10001823mm_answer_gss_userok(int socket, Buffer *m)
1824{
1825 int authenticated;
1826
1827 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1828
1829 buffer_clear(m);
1830 buffer_put_int(m, authenticated);
1831
1832 debug3("%s: sending result %d", __func__, authenticated);
1833 mm_request_send(socket, MONITOR_ANS_GSSUSEROK, m);
1834
Damien Miller0425d402003-11-17 22:18:21 +11001835 auth_method="gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001836
1837 /* Monitor loop will terminate if authenticated */
1838 return (authenticated);
1839}
1840#endif /* GSSAPI */