blob: f90a904619b5624b45112a25c88a4ba7649a0e04 [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"
Darren Tucker0efd1552003-08-26 11:49:55 +100028RCSID("$OpenBSD: monitor.c,v 1.46 2003/08/22 10:56:09 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
Damien Miller25162f22002-09-12 09:47:29 +1000133#ifdef KRB5
134int mm_answer_krb5(int, Buffer *);
135#endif
Darren Tucker0efd1552003-08-26 11:49:55 +1000136#ifdef GSSAPI
137int mm_answer_gss_setup_ctx(int, Buffer *);
138int mm_answer_gss_accept_ctx(int, Buffer *);
139int mm_answer_gss_userok(int, Buffer *);
140#endif
Damien Miller25162f22002-09-12 09:47:29 +1000141
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000142static Authctxt *authctxt;
143static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
144
145/* local state for key verify */
146static u_char *key_blob = NULL;
147static u_int key_bloblen = 0;
148static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000149static char *hostbased_cuser = NULL;
150static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000151static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000152static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000153static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000154static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000155
156struct mon_table {
157 enum monitor_reqtype type;
158 int flags;
159 int (*f)(int, Buffer *);
160};
161
162#define MON_ISAUTH 0x0004 /* Required for Authentication */
163#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
164#define MON_ONCE 0x0010 /* Disable after calling */
165
166#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
167
168#define MON_PERMIT 0x1000 /* Request is permitted */
169
170struct mon_table mon_dispatch_proto20[] = {
171 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
172 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
173 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
174 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000175 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000176 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000177#ifdef USE_PAM
178 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000179 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000180 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
181 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
182 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
183 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000184#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000185#ifdef BSD_AUTH
186 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
187 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
188#endif
189#ifdef SKEY
190 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
191 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
192#endif
193 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
194 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Damien Miller3ab496b2003-05-14 13:47:37 +1000195#ifdef KRB5
196 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
197#endif
Darren Tucker0efd1552003-08-26 11:49:55 +1000198#ifdef GSSAPI
199 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
200 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
201 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
202#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203 {0, 0, NULL}
204};
205
206struct mon_table mon_dispatch_postauth20[] = {
207 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
208 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
209 {MONITOR_REQ_PTY, 0, mm_answer_pty},
210 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
211 {MONITOR_REQ_TERM, 0, mm_answer_term},
212 {0, 0, NULL}
213};
214
215struct mon_table mon_dispatch_proto15[] = {
216 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
217 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
218 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
219 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
220 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
221 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
222 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
223 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
224#ifdef BSD_AUTH
225 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
226 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
227#endif
228#ifdef SKEY
229 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
230 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
231#endif
Damien Millera33501b2002-05-08 12:24:42 +1000232#ifdef USE_PAM
233 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000234 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000235 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
236 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
237 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
238 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000239#endif
Damien Miller25162f22002-09-12 09:47:29 +1000240#ifdef KRB5
241 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
242#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000243 {0, 0, NULL}
244};
245
246struct mon_table mon_dispatch_postauth15[] = {
247 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
248 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
249 {MONITOR_REQ_TERM, 0, mm_answer_term},
250 {0, 0, NULL}
251};
252
253struct mon_table *mon_dispatch;
254
255/* Specifies if a certain message is allowed at the moment */
256
257static void
258monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
259{
260 while (ent->f != NULL) {
261 if (ent->type == type) {
262 ent->flags &= ~MON_PERMIT;
263 ent->flags |= permit ? MON_PERMIT : 0;
264 return;
265 }
266 ent++;
267 }
268}
269
270static void
271monitor_permit_authentications(int permit)
272{
273 struct mon_table *ent = mon_dispatch;
274
275 while (ent->f != NULL) {
276 if (ent->flags & MON_AUTH) {
277 ent->flags &= ~MON_PERMIT;
278 ent->flags |= permit ? MON_PERMIT : 0;
279 }
280 ent++;
281 }
282}
283
284Authctxt *
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000285monitor_child_preauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286{
287 struct mon_table *ent;
288 int authenticated = 0;
289
290 debug3("preauth child monitor started");
291
292 if (compat20) {
293 mon_dispatch = mon_dispatch_proto20;
294
295 /* Permit requests for moduli and signatures */
296 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
297 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
298 } else {
299 mon_dispatch = mon_dispatch_proto15;
300
301 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
302 }
303
304 authctxt = authctxt_new();
305
306 /* The first few requests do not require asynchronous access */
307 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000308 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000309 if (authenticated) {
310 if (!(ent->flags & MON_AUTHDECIDE))
311 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000312 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000313 if (authctxt->pw->pw_uid == 0 &&
314 !auth_root_allowed(auth_method))
315 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000316#ifdef USE_PAM
317 /* PAM needs to perform account checks after auth */
318 if (options.use_pam) {
319 Buffer m;
320
321 buffer_init(&m);
322 mm_request_receive_expect(pmonitor->m_sendfd,
323 MONITOR_REQ_PAM_ACCOUNT, &m);
324 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
325 buffer_free(&m);
326 }
327#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000328 }
329
330 if (ent->flags & MON_AUTHDECIDE) {
331 auth_log(authctxt, authenticated, auth_method,
332 compat20 ? " ssh2" : "");
333 if (!authenticated)
334 authctxt->failures++;
335 }
336 }
337
338 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000339 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000340
341 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000342 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000343
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000344 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000345
346 return (authctxt);
347}
348
Damien Millerbe64d432003-05-14 19:31:12 +1000349static void
350monitor_set_child_handler(pid_t pid)
351{
352 monitor_child_pid = pid;
353}
354
355static void
356monitor_child_handler(int signal)
357{
358 kill(monitor_child_pid, signal);
359}
360
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000362monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363{
Damien Millerbe64d432003-05-14 19:31:12 +1000364 monitor_set_child_handler(pmonitor->m_pid);
365 signal(SIGHUP, &monitor_child_handler);
366 signal(SIGTERM, &monitor_child_handler);
367
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000368 if (compat20) {
369 mon_dispatch = mon_dispatch_postauth20;
370
371 /* Permit requests for moduli and signatures */
372 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
373 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
374 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000375 } else {
376 mon_dispatch = mon_dispatch_postauth15;
377 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
378 }
379 if (!no_pty_flag) {
380 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
381 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
382 }
383
384 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000385 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000386}
387
388void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000389monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000390{
Damien Miller2d6b8332002-06-21 15:59:49 +1000391 if (options.compression) {
392 /* The member allocation is not visible, so sync it */
393 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
394 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000395}
396
397int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000398monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000399 struct mon_table **pent)
400{
401 Buffer m;
402 int ret;
403 u_char type;
404
405 buffer_init(&m);
406
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000407 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000408 type = buffer_get_char(&m);
409
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000410 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000411
412 while (ent->f != NULL) {
413 if (ent->type == type)
414 break;
415 ent++;
416 }
417
418 if (ent->f != NULL) {
419 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000420 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000421 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000422 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000423 buffer_free(&m);
424
425 /* The child may use this request only once, disable it */
426 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000427 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000428 type);
429 ent->flags &= ~MON_PERMIT;
430 }
431
432 if (pent != NULL)
433 *pent = ent;
434
435 return ret;
436 }
437
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000438 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000439
440 /* NOTREACHED */
441 return (-1);
442}
443
444/* allowed key state */
445static int
446monitor_allowed_key(u_char *blob, u_int bloblen)
447{
448 /* make sure key is allowed */
449 if (key_blob == NULL || key_bloblen != bloblen ||
450 memcmp(key_blob, blob, key_bloblen))
451 return (0);
452 return (1);
453}
454
455static void
456monitor_reset_key_state(void)
457{
458 /* reset state */
459 if (key_blob != NULL)
460 xfree(key_blob);
461 if (hostbased_cuser != NULL)
462 xfree(hostbased_cuser);
463 if (hostbased_chost != NULL)
464 xfree(hostbased_chost);
465 key_blob = NULL;
466 key_bloblen = 0;
467 key_blobtype = MM_NOKEY;
468 hostbased_cuser = NULL;
469 hostbased_chost = NULL;
470}
471
472int
473mm_answer_moduli(int socket, Buffer *m)
474{
475 DH *dh;
476 int min, want, max;
477
478 min = buffer_get_int(m);
479 want = buffer_get_int(m);
480 max = buffer_get_int(m);
481
482 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000483 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000484 /* We need to check here, too, in case the child got corrupted */
485 if (max < min || want < min || max < want)
486 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000487 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000488
489 buffer_clear(m);
490
491 dh = choose_dh(min, want, max);
492 if (dh == NULL) {
493 buffer_put_char(m, 0);
494 return (0);
495 } else {
496 /* Send first bignum */
497 buffer_put_char(m, 1);
498 buffer_put_bignum2(m, dh->p);
499 buffer_put_bignum2(m, dh->g);
500
501 DH_free(dh);
502 }
503 mm_request_send(socket, MONITOR_ANS_MODULI, m);
504 return (0);
505}
506
507int
508mm_answer_sign(int socket, Buffer *m)
509{
510 Key *key;
511 u_char *p;
512 u_char *signature;
513 u_int siglen, datlen;
514 int keyid;
515
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000516 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000517
518 keyid = buffer_get_int(m);
519 p = buffer_get_string(m, &datlen);
520
521 if (datlen != 20)
Ben Lindstromd5502182002-06-27 00:12:57 +0000522 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523
Ben Lindstromf67e0772002-06-06 20:58:19 +0000524 /* save session id, it will be passed on the first call */
525 if (session_id2_len == 0) {
526 session_id2_len = datlen;
527 session_id2 = xmalloc(session_id2_len);
528 memcpy(session_id2, p, session_id2_len);
529 }
530
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000531 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000532 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000533 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000534 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000535
Ben Lindstromd5502182002-06-27 00:12:57 +0000536 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000537
538 buffer_clear(m);
539 buffer_put_string(m, signature, siglen);
540
541 xfree(p);
542 xfree(signature);
543
544 mm_request_send(socket, MONITOR_ANS_SIGN, m);
545
546 /* Turn on permissions for getpwnam */
547 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
548
549 return (0);
550}
551
552/* Retrieves the password entry and also checks if the user is permitted */
553
554int
555mm_answer_pwnamallow(int socket, Buffer *m)
556{
557 char *login;
558 struct passwd *pwent;
559 int allowed = 0;
560
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000561 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000562
563 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000564 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000565
566 login = buffer_get_string(m, NULL);
567
568 pwent = getpwnamallow(login);
569
570 authctxt->user = xstrdup(login);
571 setproctitle("%s [priv]", pwent ? login : "unknown");
572 xfree(login);
573
574 buffer_clear(m);
575
576 if (pwent == NULL) {
577 buffer_put_char(m, 0);
578 goto out;
579 }
580
581 allowed = 1;
582 authctxt->pw = pwent;
583 authctxt->valid = 1;
584
585 buffer_put_char(m, 1);
586 buffer_put_string(m, pwent, sizeof(struct passwd));
587 buffer_put_cstring(m, pwent->pw_name);
588 buffer_put_cstring(m, "*");
589 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000590#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000591 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000592#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000593 buffer_put_cstring(m, pwent->pw_dir);
594 buffer_put_cstring(m, pwent->pw_shell);
595
596 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000597 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
599
600 /* For SSHv1 allow authentication now */
601 if (!compat20)
602 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000603 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604 /* Allow service/style information on the auth context */
605 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000606 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
607 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608
Damien Millera33501b2002-05-08 12:24:42 +1000609#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000610 if (options.use_pam)
611 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000612#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000613
614 return (0);
615}
616
Damien Miller5ad9fd92002-05-13 11:07:41 +1000617int mm_answer_auth2_read_banner(int socket, Buffer *m)
618{
619 char *banner;
620
621 buffer_clear(m);
622 banner = auth2_read_banner();
623 buffer_put_cstring(m, banner != NULL ? banner : "");
624 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
625
626 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000627 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000628
629 return (0);
630}
631
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000632int
633mm_answer_authserv(int socket, Buffer *m)
634{
635 monitor_permit_authentications(1);
636
637 authctxt->service = buffer_get_string(m, NULL);
638 authctxt->style = buffer_get_string(m, NULL);
639 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000640 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000641
642 if (strlen(authctxt->style) == 0) {
643 xfree(authctxt->style);
644 authctxt->style = NULL;
645 }
646
647 return (0);
648}
649
650int
651mm_answer_authpassword(int socket, Buffer *m)
652{
653 static int call_count;
654 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000655 int authenticated;
656 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000657
658 passwd = buffer_get_string(m, &plen);
659 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000660 authenticated = options.password_authentication &&
Damien Millereab4bae2003-04-29 23:22:40 +1000661 auth_password(authctxt, passwd) && authctxt->valid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000662 memset(passwd, 0, strlen(passwd));
663 xfree(passwd);
664
665 buffer_clear(m);
666 buffer_put_int(m, authenticated);
667
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000668 debug3("%s: sending result %d", __func__, authenticated);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
670
671 call_count++;
672 if (plen == 0 && call_count == 1)
673 auth_method = "none";
674 else
675 auth_method = "password";
676
677 /* Causes monitor loop to terminate if authenticated */
678 return (authenticated);
679}
680
681#ifdef BSD_AUTH
682int
683mm_answer_bsdauthquery(int socket, Buffer *m)
684{
685 char *name, *infotxt;
686 u_int numprompts;
687 u_int *echo_on;
688 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100689 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690
Damien Millerb7df3af2003-02-24 11:55:46 +1100691 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
692 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000693
694 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100695 buffer_put_int(m, success);
696 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000697 buffer_put_cstring(m, prompts[0]);
698
Damien Millerb7df3af2003-02-24 11:55:46 +1100699 debug3("%s: sending challenge success: %u", __func__, success);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000700 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
701
Damien Millerb7df3af2003-02-24 11:55:46 +1100702 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703 xfree(name);
704 xfree(infotxt);
705 xfree(prompts);
706 xfree(echo_on);
707 }
708
709 return (0);
710}
711
712int
713mm_answer_bsdauthrespond(int socket, Buffer *m)
714{
715 char *response;
716 int authok;
717
718 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000719 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720
721 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000722 authok = options.challenge_response_authentication &&
723 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000724 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000725 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000726 xfree(response);
727
728 buffer_clear(m);
729 buffer_put_int(m, authok);
730
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000731 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000732 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
733
734 auth_method = "bsdauth";
735
736 return (authok != 0);
737}
738#endif
739
740#ifdef SKEY
741int
742mm_answer_skeyquery(int socket, Buffer *m)
743{
744 struct skey skey;
745 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100746 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000747
Damien Millerb7df3af2003-02-24 11:55:46 +1100748 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000749
750 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100751 buffer_put_int(m, success);
752 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753 buffer_put_cstring(m, challenge);
754
Damien Millerb7df3af2003-02-24 11:55:46 +1100755 debug3("%s: sending challenge success: %u", __func__, success);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000756 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
757
758 return (0);
759}
760
761int
762mm_answer_skeyrespond(int socket, Buffer *m)
763{
764 char *response;
765 int authok;
766
767 response = buffer_get_string(m, NULL);
768
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000769 authok = (options.challenge_response_authentication &&
770 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000771 skey_haskey(authctxt->pw->pw_name) == 0 &&
772 skey_passcheck(authctxt->pw->pw_name, response) != -1);
773
774 xfree(response);
775
776 buffer_clear(m);
777 buffer_put_int(m, authok);
778
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000779 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000780 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
781
782 auth_method = "skey";
783
784 return (authok != 0);
785}
786#endif
787
Damien Miller79418552002-04-23 20:28:48 +1000788#ifdef USE_PAM
789int
790mm_answer_pam_start(int socket, Buffer *m)
791{
792 char *user;
793
Damien Miller4e448a32003-05-14 15:11:48 +1000794 if (!options.use_pam)
795 fatal("UsePAM not set, but ended up in %s anyway", __func__);
796
Damien Miller79418552002-04-23 20:28:48 +1000797 user = buffer_get_string(m, NULL);
798
799 start_pam(user);
800
801 xfree(user);
802
Damien Miller1f499fd2003-08-25 13:08:49 +1000803 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
804
Damien Miller79418552002-04-23 20:28:48 +1000805 return (0);
806}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000807
Damien Miller1f499fd2003-08-25 13:08:49 +1000808int
809mm_answer_pam_account(int socket, Buffer *m)
810{
811 u_int ret;
812
813 if (!options.use_pam)
814 fatal("UsePAM not set, but ended up in %s anyway", __func__);
815
816 ret = do_pam_account();
817
818 buffer_put_int(m, ret);
819
820 mm_request_send(socket, MONITOR_ANS_PAM_ACCOUNT, m);
821
822 return (ret);
823}
824
Damien Miller4f9f42a2003-05-10 19:28:02 +1000825static void *sshpam_ctxt, *sshpam_authok;
826extern KbdintDevice sshpam_device;
827
828int
829mm_answer_pam_init_ctx(int socket, Buffer *m)
830{
831
832 debug3("%s", __func__);
833 authctxt->user = buffer_get_string(m, NULL);
834 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
835 sshpam_authok = NULL;
836 buffer_clear(m);
837 if (sshpam_ctxt != NULL) {
838 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
839 buffer_put_int(m, 1);
840 } else {
841 buffer_put_int(m, 0);
842 }
843 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
844 return (0);
845}
846
847int
848mm_answer_pam_query(int socket, Buffer *m)
849{
850 char *name, *info, **prompts;
851 u_int num, *echo_on;
852 int i, ret;
853
854 debug3("%s", __func__);
855 sshpam_authok = NULL;
856 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
857 if (ret == 0 && num == 0)
858 sshpam_authok = sshpam_ctxt;
859 if (num > 1 || name == NULL || info == NULL)
860 ret = -1;
861 buffer_clear(m);
862 buffer_put_int(m, ret);
863 buffer_put_cstring(m, name);
864 xfree(name);
865 buffer_put_cstring(m, info);
866 xfree(info);
867 buffer_put_int(m, num);
868 for (i = 0; i < num; ++i) {
869 buffer_put_cstring(m, prompts[i]);
870 xfree(prompts[i]);
871 buffer_put_int(m, echo_on[i]);
872 }
873 if (prompts != NULL)
874 xfree(prompts);
875 if (echo_on != NULL)
876 xfree(echo_on);
877 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
878 return (0);
879}
880
881int
882mm_answer_pam_respond(int socket, Buffer *m)
883{
884 char **resp;
885 u_int num;
886 int i, ret;
887
888 debug3("%s", __func__);
889 sshpam_authok = NULL;
890 num = buffer_get_int(m);
891 if (num > 0) {
892 resp = xmalloc(num * sizeof(char *));
893 for (i = 0; i < num; ++i)
894 resp[i] = buffer_get_string(m, NULL);
895 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
896 for (i = 0; i < num; ++i)
897 xfree(resp[i]);
898 xfree(resp);
899 } else {
900 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
901 }
902 buffer_clear(m);
903 buffer_put_int(m, ret);
904 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
905 auth_method = "keyboard-interactive/pam";
906 if (ret == 0)
907 sshpam_authok = sshpam_ctxt;
908 return (0);
909}
910
911int
912mm_answer_pam_free_ctx(int socket, Buffer *m)
913{
914
915 debug3("%s", __func__);
916 (sshpam_device.free_ctx)(sshpam_ctxt);
917 buffer_clear(m);
918 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
919 return (sshpam_authok == sshpam_ctxt);
920}
Damien Miller79418552002-04-23 20:28:48 +1000921#endif
922
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000923static void
924mm_append_debug(Buffer *m)
925{
926 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000927 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000928 buffer_append(m, buffer_ptr(&auth_debug),
929 buffer_len(&auth_debug));
930 buffer_clear(&auth_debug);
931 }
932}
933
934int
935mm_answer_keyallowed(int socket, Buffer *m)
936{
937 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000938 char *cuser, *chost;
939 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000940 u_int bloblen;
941 enum mm_keytype type = 0;
942 int allowed = 0;
943
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000944 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945
946 type = buffer_get_int(m);
947 cuser = buffer_get_string(m, NULL);
948 chost = buffer_get_string(m, NULL);
949 blob = buffer_get_string(m, &bloblen);
950
951 key = key_from_blob(blob, bloblen);
952
953 if ((compat20 && type == MM_RSAHOSTKEY) ||
954 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000955 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000956
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000957 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958
959 if (key != NULL && authctxt->pw != NULL) {
960 switch(type) {
961 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000962 allowed = options.pubkey_authentication &&
963 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964 break;
965 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000966 allowed = options.hostbased_authentication &&
967 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000968 cuser, chost, key);
969 break;
970 case MM_RSAHOSTKEY:
971 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000972 allowed = options.rhosts_rsa_authentication &&
973 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000974 cuser, chost, key);
975 break;
976 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000977 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000978 break;
979 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000980 }
Damien Miller00111382003-03-10 11:21:17 +1100981 if (key != NULL)
982 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000983
984 /* clear temporarily storage (used by verify) */
985 monitor_reset_key_state();
986
987 if (allowed) {
988 /* Save temporarily for comparison in verify */
989 key_blob = blob;
990 key_bloblen = bloblen;
991 key_blobtype = type;
992 hostbased_cuser = cuser;
993 hostbased_chost = chost;
994 }
995
996 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000997 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998
999 buffer_clear(m);
1000 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001001 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001002
1003 mm_append_debug(m);
1004
1005 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
1006
1007 if (type == MM_RSAHOSTKEY)
1008 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1009
1010 return (0);
1011}
1012
1013static int
1014monitor_valid_userblob(u_char *data, u_int datalen)
1015{
1016 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001017 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001018 u_int len;
1019 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001020
1021 buffer_init(&b);
1022 buffer_append(&b, data, datalen);
1023
1024 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001025 p = buffer_ptr(&b);
1026 len = buffer_len(&b);
1027 if ((session_id2 == NULL) ||
1028 (len < session_id2_len) ||
1029 (memcmp(p, session_id2, session_id2_len) != 0))
1030 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031 buffer_consume(&b, session_id2_len);
1032 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001033 p = buffer_get_string(&b, &len);
1034 if ((session_id2 == NULL) ||
1035 (len != session_id2_len) ||
1036 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001037 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001038 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001039 }
1040 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1041 fail++;
1042 p = buffer_get_string(&b, NULL);
1043 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001044 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001045 authctxt->user, p);
1046 fail++;
1047 }
1048 xfree(p);
1049 buffer_skip_string(&b);
1050 if (datafellows & SSH_BUG_PKAUTH) {
1051 if (!buffer_get_char(&b))
1052 fail++;
1053 } else {
1054 p = buffer_get_string(&b, NULL);
1055 if (strcmp("publickey", p) != 0)
1056 fail++;
1057 xfree(p);
1058 if (!buffer_get_char(&b))
1059 fail++;
1060 buffer_skip_string(&b);
1061 }
1062 buffer_skip_string(&b);
1063 if (buffer_len(&b) != 0)
1064 fail++;
1065 buffer_free(&b);
1066 return (fail == 0);
1067}
1068
1069static int
Damien Millera10f5612002-09-12 09:49:15 +10001070monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1071 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001072{
1073 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001074 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001075 u_int len;
1076 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001077
1078 buffer_init(&b);
1079 buffer_append(&b, data, datalen);
1080
Ben Lindstromf67e0772002-06-06 20:58:19 +00001081 p = buffer_get_string(&b, &len);
1082 if ((session_id2 == NULL) ||
1083 (len != session_id2_len) ||
1084 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001085 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001086 xfree(p);
1087
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001088 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1089 fail++;
1090 p = buffer_get_string(&b, NULL);
1091 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001092 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001093 authctxt->user, p);
1094 fail++;
1095 }
1096 xfree(p);
1097 buffer_skip_string(&b); /* service */
1098 p = buffer_get_string(&b, NULL);
1099 if (strcmp(p, "hostbased") != 0)
1100 fail++;
1101 xfree(p);
1102 buffer_skip_string(&b); /* pkalg */
1103 buffer_skip_string(&b); /* pkblob */
1104
1105 /* verify client host, strip trailing dot if necessary */
1106 p = buffer_get_string(&b, NULL);
1107 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1108 p[len - 1] = '\0';
1109 if (strcmp(p, chost) != 0)
1110 fail++;
1111 xfree(p);
1112
1113 /* verify client user */
1114 p = buffer_get_string(&b, NULL);
1115 if (strcmp(p, cuser) != 0)
1116 fail++;
1117 xfree(p);
1118
1119 if (buffer_len(&b) != 0)
1120 fail++;
1121 buffer_free(&b);
1122 return (fail == 0);
1123}
1124
1125int
1126mm_answer_keyverify(int socket, Buffer *m)
1127{
1128 Key *key;
1129 u_char *signature, *data, *blob;
1130 u_int signaturelen, datalen, bloblen;
1131 int verified = 0;
1132 int valid_data = 0;
1133
1134 blob = buffer_get_string(m, &bloblen);
1135 signature = buffer_get_string(m, &signaturelen);
1136 data = buffer_get_string(m, &datalen);
1137
1138 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001139 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001140 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001141
1142 key = key_from_blob(blob, bloblen);
1143 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001144 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001145
1146 switch (key_blobtype) {
1147 case MM_USERKEY:
1148 valid_data = monitor_valid_userblob(data, datalen);
1149 break;
1150 case MM_HOSTKEY:
1151 valid_data = monitor_valid_hostbasedblob(data, datalen,
1152 hostbased_cuser, hostbased_chost);
1153 break;
1154 default:
1155 valid_data = 0;
1156 break;
1157 }
1158 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001159 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001160
1161 verified = key_verify(key, signature, signaturelen, data, datalen);
1162 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001163 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001164
1165 key_free(key);
1166 xfree(blob);
1167 xfree(signature);
1168 xfree(data);
1169
Ben Lindstrome1c09122002-06-23 00:38:24 +00001170 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1171
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001172 monitor_reset_key_state();
1173
1174 buffer_clear(m);
1175 buffer_put_int(m, verified);
1176 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1177
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001178 return (verified);
1179}
1180
1181static void
1182mm_record_login(Session *s, struct passwd *pw)
1183{
1184 socklen_t fromlen;
1185 struct sockaddr_storage from;
1186
1187 /*
1188 * Get IP address of client. If the connection is not a socket, let
1189 * the address be 0.0.0.0.
1190 */
1191 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001192 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001193 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001194 if (getpeername(packet_get_connection_in(),
1195 (struct sockaddr *) & from, &fromlen) < 0) {
1196 debug("getpeername: %.100s", strerror(errno));
1197 fatal_cleanup();
1198 }
1199 }
1200 /* Record that there was a login on that tty from the remote host. */
1201 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001202 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001203 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001204}
1205
1206static void
1207mm_session_close(Session *s)
1208{
Damien Miller04bd8b02003-05-25 14:38:33 +10001209 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001210 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001211 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1213 session_pty_cleanup2(s);
1214 }
1215 s->used = 0;
1216}
1217
1218int
1219mm_answer_pty(int socket, Buffer *m)
1220{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001221 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001222 Session *s;
1223 int res, fd0;
1224
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001225 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001226
1227 buffer_clear(m);
1228 s = session_new();
1229 if (s == NULL)
1230 goto error;
1231 s->authctxt = authctxt;
1232 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001233 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001234 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1235 if (res == 0)
1236 goto error;
1237 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1238 pty_setowner(authctxt->pw, s->tty);
1239
1240 buffer_put_int(m, 1);
1241 buffer_put_cstring(m, s->tty);
1242 mm_request_send(socket, MONITOR_ANS_PTY, m);
1243
1244 mm_send_fd(socket, s->ptyfd);
1245 mm_send_fd(socket, s->ttyfd);
1246
1247 /* We need to trick ttyslot */
1248 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001249 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001250
1251 mm_record_login(s, authctxt->pw);
1252
1253 /* Now we can close the file descriptor again */
1254 close(0);
1255
1256 /* make sure nothing uses fd 0 */
1257 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001258 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001259 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001260 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261
1262 /* slave is not needed */
1263 close(s->ttyfd);
1264 s->ttyfd = s->ptyfd;
1265 /* no need to dup() because nobody closes ptyfd */
1266 s->ptymaster = s->ptyfd;
1267
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001268 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001269
1270 return (0);
1271
1272 error:
1273 if (s != NULL)
1274 mm_session_close(s);
1275 buffer_put_int(m, 0);
1276 mm_request_send(socket, MONITOR_ANS_PTY, m);
1277 return (0);
1278}
1279
1280int
1281mm_answer_pty_cleanup(int socket, Buffer *m)
1282{
1283 Session *s;
1284 char *tty;
1285
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001286 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001287
1288 tty = buffer_get_string(m, NULL);
1289 if ((s = session_by_tty(tty)) != NULL)
1290 mm_session_close(s);
1291 buffer_clear(m);
1292 xfree(tty);
1293 return (0);
1294}
1295
1296int
1297mm_answer_sesskey(int socket, Buffer *m)
1298{
1299 BIGNUM *p;
1300 int rsafail;
1301
1302 /* Turn off permissions */
1303 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1304
1305 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001306 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001307
1308 buffer_get_bignum2(m, p);
1309
1310 rsafail = ssh1_session_key(p);
1311
1312 buffer_clear(m);
1313 buffer_put_int(m, rsafail);
1314 buffer_put_bignum2(m, p);
1315
1316 BN_clear_free(p);
1317
1318 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1319
1320 /* Turn on permissions for sessid passing */
1321 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1322
1323 return (0);
1324}
1325
1326int
1327mm_answer_sessid(int socket, Buffer *m)
1328{
1329 int i;
1330
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001331 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001332
1333 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001334 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001335 for (i = 0; i < 16; i++)
1336 session_id[i] = buffer_get_char(m);
1337
1338 /* Turn on permissions for getpwnam */
1339 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1340
1341 return (0);
1342}
1343
1344int
1345mm_answer_rsa_keyallowed(int socket, Buffer *m)
1346{
1347 BIGNUM *client_n;
1348 Key *key = NULL;
1349 u_char *blob = NULL;
1350 u_int blen = 0;
1351 int allowed = 0;
1352
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001353 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001355 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001356 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001357 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001358 buffer_get_bignum2(m, client_n);
1359 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1360 BN_clear_free(client_n);
1361 }
1362 buffer_clear(m);
1363 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001364 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001365
1366 /* clear temporarily storage (used by generate challenge) */
1367 monitor_reset_key_state();
1368
1369 if (allowed && key != NULL) {
1370 key->type = KEY_RSA; /* cheat for key_to_blob */
1371 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001372 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001373 buffer_put_string(m, blob, blen);
1374
1375 /* Save temporarily for comparison in verify */
1376 key_blob = blob;
1377 key_bloblen = blen;
1378 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001379 }
Damien Miller00111382003-03-10 11:21:17 +11001380 if (key != NULL)
1381 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001382
1383 mm_append_debug(m);
1384
1385 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1386
1387 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1388 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1389 return (0);
1390}
1391
1392int
1393mm_answer_rsa_challenge(int socket, Buffer *m)
1394{
1395 Key *key = NULL;
1396 u_char *blob;
1397 u_int blen;
1398
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001399 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001400
1401 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001402 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001403 blob = buffer_get_string(m, &blen);
1404 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001405 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001406 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001407 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001409 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001410
1411 if (ssh1_challenge)
1412 BN_clear_free(ssh1_challenge);
1413 ssh1_challenge = auth_rsa_generate_challenge(key);
1414
1415 buffer_clear(m);
1416 buffer_put_bignum2(m, ssh1_challenge);
1417
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001418 debug3("%s sending reply", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001419 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1420
1421 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001422
1423 xfree(blob);
1424 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001425 return (0);
1426}
1427
1428int
1429mm_answer_rsa_response(int socket, Buffer *m)
1430{
1431 Key *key = NULL;
1432 u_char *blob, *response;
1433 u_int blen, len;
1434 int success;
1435
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001436 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001437
1438 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001439 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001441 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442
1443 blob = buffer_get_string(m, &blen);
1444 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001445 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001447 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001448 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001449 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001450 response = buffer_get_string(m, &len);
1451 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001452 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001453 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1454
Damien Miller00111382003-03-10 11:21:17 +11001455 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001456 key_free(key);
1457 xfree(response);
1458
1459 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1460
1461 /* reset state */
1462 BN_clear_free(ssh1_challenge);
1463 ssh1_challenge = NULL;
1464 monitor_reset_key_state();
1465
1466 buffer_clear(m);
1467 buffer_put_int(m, success);
1468 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1469
1470 return (success);
1471}
1472
Damien Miller25162f22002-09-12 09:47:29 +10001473#ifdef KRB5
1474int
1475mm_answer_krb5(int socket, Buffer *m)
1476{
1477 krb5_data tkt, reply;
1478 char *client_user;
1479 u_int len;
1480 int success;
1481
1482 /* use temporary var to avoid size issues on 64bit arch */
1483 tkt.data = buffer_get_string(m, &len);
1484 tkt.length = len;
1485
Damien Milleref73f502002-09-25 12:20:17 +10001486 success = options.kerberos_authentication &&
1487 authctxt->valid &&
Damien Miller7db40c92002-09-25 12:19:39 +10001488 auth_krb5(authctxt, &tkt, &client_user, &reply);
Damien Miller25162f22002-09-12 09:47:29 +10001489
1490 if (tkt.length)
1491 xfree(tkt.data);
1492
1493 buffer_clear(m);
1494 buffer_put_int(m, success);
1495
1496 if (success) {
1497 buffer_put_cstring(m, client_user);
1498 buffer_put_string(m, reply.data, reply.length);
1499 if (client_user)
1500 xfree(client_user);
1501 if (reply.length)
1502 xfree(reply.data);
1503 }
1504 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1505
Damien Miller3ab496b2003-05-14 13:47:37 +10001506 auth_method = "kerberos";
1507
Damien Miller25162f22002-09-12 09:47:29 +10001508 return success;
1509}
1510#endif
1511
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001512int
1513mm_answer_term(int socket, Buffer *req)
1514{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001515 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001516 int res, status;
1517
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001518 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001519
1520 /* The child is terminating */
1521 session_destroy_all(&mm_session_close);
1522
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001523 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001524 if (errno != EINTR)
1525 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001526
1527 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1528
1529 /* Terminate process */
1530 exit (res);
1531}
1532
1533void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001534monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001535{
1536 if (compat20) {
1537 set_newkeys(MODE_IN);
1538 set_newkeys(MODE_OUT);
1539 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001540 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001541 packet_set_encryption_key(child_state.ssh1key,
1542 child_state.ssh1keylen, child_state.ssh1cipher);
1543 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001544 }
1545
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001546 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001547 packet_set_keycontext(MODE_OUT, child_state.keyout);
1548 xfree(child_state.keyout);
1549 packet_set_keycontext(MODE_IN, child_state.keyin);
1550 xfree(child_state.keyin);
1551
1552 if (!compat20) {
1553 packet_set_iv(MODE_OUT, child_state.ivout);
1554 xfree(child_state.ivout);
1555 packet_set_iv(MODE_IN, child_state.ivin);
1556 xfree(child_state.ivin);
1557 }
1558
1559 memcpy(&incoming_stream, &child_state.incoming,
1560 sizeof(incoming_stream));
1561 memcpy(&outgoing_stream, &child_state.outgoing,
1562 sizeof(outgoing_stream));
1563
1564 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001565 if (options.compression)
1566 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001567
1568 /* Network I/O buffers */
1569 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1570 buffer_clear(&input);
1571 buffer_append(&input, child_state.input, child_state.ilen);
1572 memset(child_state.input, 0, child_state.ilen);
1573 xfree(child_state.input);
1574
1575 buffer_clear(&output);
1576 buffer_append(&output, child_state.output, child_state.olen);
1577 memset(child_state.output, 0, child_state.olen);
1578 xfree(child_state.output);
1579}
1580
1581static Kex *
1582mm_get_kex(Buffer *m)
1583{
1584 Kex *kex;
1585 void *blob;
1586 u_int bloblen;
1587
1588 kex = xmalloc(sizeof(*kex));
1589 memset(kex, 0, sizeof(*kex));
1590 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001591 if ((session_id2 == NULL) ||
1592 (kex->session_id_len != session_id2_len) ||
1593 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1594 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001595 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001596 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1597 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001598 kex->server = 1;
1599 kex->hostkey_type = buffer_get_int(m);
1600 kex->kex_type = buffer_get_int(m);
1601 blob = buffer_get_string(m, &bloblen);
1602 buffer_init(&kex->my);
1603 buffer_append(&kex->my, blob, bloblen);
1604 xfree(blob);
1605 blob = buffer_get_string(m, &bloblen);
1606 buffer_init(&kex->peer);
1607 buffer_append(&kex->peer, blob, bloblen);
1608 xfree(blob);
1609 kex->done = 1;
1610 kex->flags = buffer_get_int(m);
1611 kex->client_version_string = buffer_get_string(m, NULL);
1612 kex->server_version_string = buffer_get_string(m, NULL);
1613 kex->load_host_key=&get_hostkey_by_type;
1614 kex->host_key_index=&get_hostkey_index;
1615
1616 return (kex);
1617}
1618
1619/* This function requries careful sanity checking */
1620
1621void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001622mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001623{
1624 Buffer m;
1625 u_char *blob, *p;
1626 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001627 u_int32_t seqnr, packets;
1628 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001629
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001630 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001631
1632 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001633 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001634 if (!compat20) {
1635 child_state.ssh1protoflags = buffer_get_int(&m);
1636 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001637 child_state.ssh1key = buffer_get_string(&m,
1638 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001639 child_state.ivout = buffer_get_string(&m,
1640 &child_state.ivoutlen);
1641 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1642 goto skip;
1643 } else {
1644 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001645 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001646 }
1647
1648 blob = buffer_get_string(&m, &bloblen);
1649 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1650 xfree(blob);
1651
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001652 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001653 blob = buffer_get_string(&m, &bloblen);
1654 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1655 xfree(blob);
1656
1657 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001658 seqnr = buffer_get_int(&m);
1659 blocks = buffer_get_int64(&m);
1660 packets = buffer_get_int(&m);
1661 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1662 seqnr = buffer_get_int(&m);
1663 blocks = buffer_get_int64(&m);
1664 packets = buffer_get_int(&m);
1665 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666
1667 skip:
1668 /* Get the key context */
1669 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1670 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1671
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001672 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001673 /* Get compression state */
1674 p = buffer_get_string(&m, &plen);
1675 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001676 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001677 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1678 xfree(p);
1679
1680 p = buffer_get_string(&m, &plen);
1681 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001682 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001683 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1684 xfree(p);
1685
1686 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001687 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001688 child_state.input = buffer_get_string(&m, &child_state.ilen);
1689 child_state.output = buffer_get_string(&m, &child_state.olen);
1690
1691 buffer_free(&m);
1692}
1693
1694
1695/* Allocation functions for zlib */
1696void *
1697mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1698{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001699 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001700 void *address;
1701
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001702 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001703 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1704
1705 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001706
1707 return (address);
1708}
1709
1710void
1711mm_zfree(struct mm_master *mm, void *address)
1712{
1713 mm_free(mm, address);
1714}
1715
1716void
1717mm_init_compression(struct mm_master *mm)
1718{
1719 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1720 outgoing_stream.zfree = (free_func)mm_zfree;
1721 outgoing_stream.opaque = mm;
1722
1723 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1724 incoming_stream.zfree = (free_func)mm_zfree;
1725 incoming_stream.opaque = mm;
1726}
1727
1728/* XXX */
1729
1730#define FD_CLOSEONEXEC(x) do { \
1731 if (fcntl(x, F_SETFD, 1) == -1) \
1732 fatal("fcntl(%d, F_SETFD)", x); \
1733} while (0)
1734
1735static void
1736monitor_socketpair(int *pair)
1737{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001738#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001739 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001740 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001741#else
1742 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001743 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001744#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001745 FD_CLOSEONEXEC(pair[0]);
1746 FD_CLOSEONEXEC(pair[1]);
1747}
1748
1749#define MM_MEMSIZE 65536
1750
1751struct monitor *
1752monitor_init(void)
1753{
1754 struct monitor *mon;
1755 int pair[2];
1756
1757 mon = xmalloc(sizeof(*mon));
1758
1759 monitor_socketpair(pair);
1760
1761 mon->m_recvfd = pair[0];
1762 mon->m_sendfd = pair[1];
1763
1764 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001765 if (options.compression) {
1766 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1767 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001768
Damien Miller2d6b8332002-06-21 15:59:49 +10001769 /* Compression needs to share state across borders */
1770 mm_init_compression(mon->m_zlib);
1771 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001772
1773 return mon;
1774}
1775
1776void
1777monitor_reinit(struct monitor *mon)
1778{
1779 int pair[2];
1780
1781 monitor_socketpair(pair);
1782
1783 mon->m_recvfd = pair[0];
1784 mon->m_sendfd = pair[1];
1785}
Darren Tucker0efd1552003-08-26 11:49:55 +10001786
1787#ifdef GSSAPI
1788int
1789mm_answer_gss_setup_ctx(int socket, Buffer *m)
1790{
1791 gss_OID_desc oid;
1792 OM_uint32 major;
1793 u_int len;
1794
1795 oid.elements = buffer_get_string(m, &len);
1796 oid.length = len;
1797
1798 major = ssh_gssapi_server_ctx(&gsscontext, &oid);
1799
1800 xfree(oid.elements);
1801
1802 buffer_clear(m);
1803 buffer_put_int(m, major);
1804
1805 mm_request_send(socket,MONITOR_ANS_GSSSETUP, m);
1806
1807 /* Now we have a context, enable the step */
1808 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1809
1810 return (0);
1811}
1812
1813int
1814mm_answer_gss_accept_ctx(int socket, Buffer *m)
1815{
1816 gss_buffer_desc in;
1817 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1818 OM_uint32 major,minor;
1819 OM_uint32 flags = 0; /* GSI needs this */
1820
1821 in.value = buffer_get_string(m, &in.length);
1822 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1823 xfree(in.value);
1824
1825 buffer_clear(m);
1826 buffer_put_int(m, major);
1827 buffer_put_string(m, out.value, out.length);
1828 buffer_put_int(m, flags);
1829 mm_request_send(socket, MONITOR_ANS_GSSSTEP, m);
1830
1831 gss_release_buffer(&minor, &out);
1832
1833 /* Complete - now we can do signing */
1834 if (major==GSS_S_COMPLETE) {
1835 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1836 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1837 }
1838 return (0);
1839}
1840
1841int
1842mm_answer_gss_userok(int socket, Buffer *m)
1843{
1844 int authenticated;
1845
1846 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1847
1848 buffer_clear(m);
1849 buffer_put_int(m, authenticated);
1850
1851 debug3("%s: sending result %d", __func__, authenticated);
1852 mm_request_send(socket, MONITOR_ANS_GSSUSEROK, m);
1853
1854 auth_method="gssapi";
1855
1856 /* Monitor loop will terminate if authenticated */
1857 return (authenticated);
1858}
1859#endif /* GSSAPI */