]> andersk Git - openssh.git/blame - monitor.c
- andreas@cvs.openbsd.org 2009/05/28 16:50:16
[openssh.git] / monitor.c
CommitLineData
dc1f1948 1/* $OpenBSD: monitor.c,v 1.102 2009/05/25 06:48:01 andreas Exp $ */
1853d1ef 2/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
aa2eae64 29
30#include <sys/types.h>
536c14e8 31#include <sys/param.h>
5b04a8bf 32#include <sys/socket.h>
31652869 33#include "openbsd-compat/sys-tree.h"
aa2eae64 34#include <sys/wait.h>
1853d1ef 35
028094f4 36#include <errno.h>
d3221cca 37#include <fcntl.h>
b5b88c19 38#ifdef HAVE_PATHS_H
a75f5360 39#include <paths.h>
b5b88c19 40#endif
b1842393 41#include <pwd.h>
ada68823 42#include <signal.h>
24436b92 43#include <stdarg.h>
ffa517a8 44#include <stdlib.h>
00146caa 45#include <string.h>
aa751414 46#include <unistd.h>
1853d1ef 47
48#ifdef SKEY
49#include <skey.h>
50#endif
51
a75f5360 52#include <openssl/dh.h>
53
3593bdc0 54#include "openbsd-compat/sys-queue.h"
31652869 55#include "xmalloc.h"
1853d1ef 56#include "ssh.h"
31652869 57#include "key.h"
58#include "buffer.h"
59#include "hostfile.h"
1853d1ef 60#include "auth.h"
31652869 61#include "cipher.h"
1853d1ef 62#include "kex.h"
63#include "dh.h"
a655c012 64#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
65#undef TARGET_OS_MAC
9b08c23f 66#include "zlib.h"
a655c012 67#define TARGET_OS_MAC 1
68#else
69#include "zlib.h"
70#endif
1853d1ef 71#include "packet.h"
72#include "auth-options.h"
73#include "sshpty.h"
74#include "channels.h"
75#include "session.h"
76#include "sshlogin.h"
77#include "canohost.h"
78#include "log.h"
79#include "servconf.h"
80#include "monitor.h"
81#include "monitor_mm.h"
31652869 82#ifdef GSSAPI
83#include "ssh-gss.h"
84#endif
1853d1ef 85#include "monitor_wrap.h"
86#include "monitor_fdpass.h"
1853d1ef 87#include "misc.h"
1853d1ef 88#include "compat.h"
89#include "ssh2.h"
5adf6b9a 90#include "jpake.h"
1853d1ef 91
7364bd04 92#ifdef GSSAPI
7364bd04 93static Gssctxt *gsscontext = NULL;
94#endif
95
1853d1ef 96/* Imports */
97extern ServerOptions options;
98extern u_int utmp_len;
99extern Newkeys *current_keys[];
100extern z_stream incoming_stream;
101extern z_stream outgoing_stream;
102extern u_char session_id[];
1853d1ef 103extern Buffer auth_debug;
104extern int auth_debug_init;
be2ca0c9 105extern Buffer loginmsg;
1853d1ef 106
107/* State exported from the child */
108
109struct {
110 z_stream incoming;
111 z_stream outgoing;
112 u_char *keyin;
113 u_int keyinlen;
114 u_char *keyout;
115 u_int keyoutlen;
116 u_char *ivin;
117 u_int ivinlen;
118 u_char *ivout;
119 u_int ivoutlen;
9459414c 120 u_char *ssh1key;
121 u_int ssh1keylen;
1853d1ef 122 int ssh1cipher;
123 int ssh1protoflags;
124 u_char *input;
125 u_int ilen;
126 u_char *output;
127 u_int olen;
128} child_state;
129
b8e04133 130/* Functions on the monitor that answer unprivileged requests */
1853d1ef 131
132int mm_answer_moduli(int, Buffer *);
133int mm_answer_sign(int, Buffer *);
134int mm_answer_pwnamallow(int, Buffer *);
94a73cdc 135int mm_answer_auth2_read_banner(int, Buffer *);
1853d1ef 136int mm_answer_authserv(int, Buffer *);
137int mm_answer_authpassword(int, Buffer *);
138int mm_answer_bsdauthquery(int, Buffer *);
139int mm_answer_bsdauthrespond(int, Buffer *);
140int mm_answer_skeyquery(int, Buffer *);
141int mm_answer_skeyrespond(int, Buffer *);
142int mm_answer_keyallowed(int, Buffer *);
143int mm_answer_keyverify(int, Buffer *);
144int mm_answer_pty(int, Buffer *);
145int mm_answer_pty_cleanup(int, Buffer *);
146int mm_answer_term(int, Buffer *);
147int mm_answer_rsa_keyallowed(int, Buffer *);
148int mm_answer_rsa_challenge(int, Buffer *);
149int mm_answer_rsa_response(int, Buffer *);
150int mm_answer_sesskey(int, Buffer *);
151int mm_answer_sessid(int, Buffer *);
5adf6b9a 152int mm_answer_jpake_get_pwdata(int, Buffer *);
153int mm_answer_jpake_step1(int, Buffer *);
154int mm_answer_jpake_step2(int, Buffer *);
155int mm_answer_jpake_key_confirm(int, Buffer *);
156int mm_answer_jpake_check_confirm(int, Buffer *);
1853d1ef 157
ad200abb 158#ifdef USE_PAM
159int mm_answer_pam_start(int, Buffer *);
5b9e2464 160int mm_answer_pam_account(int, Buffer *);
05114c74 161int mm_answer_pam_init_ctx(int, Buffer *);
162int mm_answer_pam_query(int, Buffer *);
163int mm_answer_pam_respond(int, Buffer *);
164int mm_answer_pam_free_ctx(int, Buffer *);
ad200abb 165#endif
166
7364bd04 167#ifdef GSSAPI
168int mm_answer_gss_setup_ctx(int, Buffer *);
169int mm_answer_gss_accept_ctx(int, Buffer *);
170int mm_answer_gss_userok(int, Buffer *);
0789992b 171int mm_answer_gss_checkmic(int, Buffer *);
7364bd04 172#endif
696f6bef 173
6039eeef 174#ifdef SSH_AUDIT_EVENTS
c00e4d75 175int mm_answer_audit_event(int, Buffer *);
176int mm_answer_audit_command(int, Buffer *);
177#endif
178
1853d1ef 179static Authctxt *authctxt;
180static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
181
182/* local state for key verify */
183static u_char *key_blob = NULL;
184static u_int key_bloblen = 0;
185static int key_blobtype = MM_NOKEY;
661e45a0 186static char *hostbased_cuser = NULL;
187static char *hostbased_chost = NULL;
1853d1ef 188static char *auth_method = "unknown";
a27002e5 189static u_int session_id2_len = 0;
521d606b 190static u_char *session_id2 = NULL;
3a39206f 191static pid_t monitor_child_pid;
1853d1ef 192
193struct mon_table {
194 enum monitor_reqtype type;
195 int flags;
196 int (*f)(int, Buffer *);
197};
198
199#define MON_ISAUTH 0x0004 /* Required for Authentication */
200#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
201#define MON_ONCE 0x0010 /* Disable after calling */
c023a130 202#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
1853d1ef 203
204#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
205
206#define MON_PERMIT 0x1000 /* Request is permitted */
207
208struct mon_table mon_dispatch_proto20[] = {
209 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
210 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
211 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
212 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
94a73cdc 213 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
1853d1ef 214 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
ad200abb 215#ifdef USE_PAM
216 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
5b9e2464 217 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
05114c74 218 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
219 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
220 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
221 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
8b314ec9 222#endif
6039eeef 223#ifdef SSH_AUDIT_EVENTS
d9bc3cde 224 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
c00e4d75 225#endif
1853d1ef 226#ifdef BSD_AUTH
227 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
1720c23b 228 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
1853d1ef 229#endif
230#ifdef SKEY
231 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
232 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
233#endif
234 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
235 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
7364bd04 236#ifdef GSSAPI
237 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
238 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
239 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
0789992b 240 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
5adf6b9a 241#endif
242#ifdef JPAKE
243 {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
244 {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
245 {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
246 {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
247 {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
802e01b8 248#endif
1853d1ef 249 {0, 0, NULL}
250};
251
252struct mon_table mon_dispatch_postauth20[] = {
253 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
254 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
255 {MONITOR_REQ_PTY, 0, mm_answer_pty},
256 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
257 {MONITOR_REQ_TERM, 0, mm_answer_term},
6039eeef 258#ifdef SSH_AUDIT_EVENTS
c00e4d75 259 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
260 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
261#endif
1853d1ef 262 {0, 0, NULL}
263};
264
265struct mon_table mon_dispatch_proto15[] = {
266 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
267 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
268 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
269 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
c023a130 270 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
271 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
1853d1ef 272 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
273 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
274#ifdef BSD_AUTH
275 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
1720c23b 276 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
1853d1ef 277#endif
278#ifdef SKEY
279 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
280 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
efe44db6 281#endif
282#ifdef USE_PAM
283 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
5b9e2464 284 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
05114c74 285 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
286 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
287 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
288 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
c00e4d75 289#endif
6039eeef 290#ifdef SSH_AUDIT_EVENTS
d9bc3cde 291 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
1853d1ef 292#endif
293 {0, 0, NULL}
294};
295
296struct mon_table mon_dispatch_postauth15[] = {
297 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
298 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
299 {MONITOR_REQ_TERM, 0, mm_answer_term},
6039eeef 300#ifdef SSH_AUDIT_EVENTS
c00e4d75 301 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
bb09a477 302 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
c00e4d75 303#endif
1853d1ef 304 {0, 0, NULL}
305};
306
307struct mon_table *mon_dispatch;
308
309/* Specifies if a certain message is allowed at the moment */
310
311static void
312monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
313{
314 while (ent->f != NULL) {
315 if (ent->type == type) {
316 ent->flags &= ~MON_PERMIT;
317 ent->flags |= permit ? MON_PERMIT : 0;
318 return;
319 }
320 ent++;
321 }
322}
323
324static void
325monitor_permit_authentications(int permit)
326{
327 struct mon_table *ent = mon_dispatch;
328
329 while (ent->f != NULL) {
330 if (ent->flags & MON_AUTH) {
331 ent->flags &= ~MON_PERMIT;
332 ent->flags |= permit ? MON_PERMIT : 0;
333 }
334 ent++;
335 }
336}
337
2362db19 338void
339monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
1853d1ef 340{
341 struct mon_table *ent;
342 int authenticated = 0;
343
344 debug3("preauth child monitor started");
345
2362db19 346 authctxt = _authctxt;
347 memset(authctxt, 0, sizeof(*authctxt));
348
e05df884 349 authctxt->loginmsg = &loginmsg;
350
1853d1ef 351 if (compat20) {
352 mon_dispatch = mon_dispatch_proto20;
353
354 /* Permit requests for moduli and signatures */
355 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
356 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
357 } else {
358 mon_dispatch = mon_dispatch_proto15;
359
360 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
361 }
362
1853d1ef 363 /* The first few requests do not require asynchronous access */
364 while (!authenticated) {
c023a130 365 auth_method = "unknown";
89916e8c 366 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
1853d1ef 367 if (authenticated) {
368 if (!(ent->flags & MON_AUTHDECIDE))
369 fatal("%s: unexpected authentication from %d",
1588c277 370 __func__, ent->type);
1853d1ef 371 if (authctxt->pw->pw_uid == 0 &&
372 !auth_root_allowed(auth_method))
373 authenticated = 0;
5b9e2464 374#ifdef USE_PAM
375 /* PAM needs to perform account checks after auth */
4d1de3a3 376 if (options.use_pam && authenticated) {
5b9e2464 377 Buffer m;
378
379 buffer_init(&m);
aff51935 380 mm_request_receive_expect(pmonitor->m_sendfd,
5b9e2464 381 MONITOR_REQ_PAM_ACCOUNT, &m);
382 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
383 buffer_free(&m);
384 }
385#endif
1853d1ef 386 }
387
c023a130 388 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
1853d1ef 389 auth_log(authctxt, authenticated, auth_method,
390 compat20 ? " ssh2" : "");
391 if (!authenticated)
392 authctxt->failures++;
393 }
5adf6b9a 394#ifdef JPAKE
395 /* Cleanup JPAKE context after authentication */
396 if (ent->flags & MON_AUTHDECIDE) {
397 if (authctxt->jpake_ctx != NULL) {
398 jpake_free(authctxt->jpake_ctx);
399 authctxt->jpake_ctx = NULL;
400 }
401 }
402#endif
1853d1ef 403 }
404
405 if (!authctxt->valid)
1588c277 406 fatal("%s: authenticated invalid user", __func__);
c023a130 407 if (strcmp(auth_method, "unknown") == 0)
408 fatal("%s: authentication method name unknown", __func__);
1853d1ef 409
410 debug("%s: %s has been authenticated by privileged process",
1588c277 411 __func__, authctxt->user);
1853d1ef 412
b5a28cbc 413 mm_get_keystate(pmonitor);
1853d1ef 414}
415
3a39206f 416static void
417monitor_set_child_handler(pid_t pid)
418{
419 monitor_child_pid = pid;
420}
421
422static void
ca75d7de 423monitor_child_handler(int sig)
3a39206f 424{
ca75d7de 425 kill(monitor_child_pid, sig);
3a39206f 426}
427
1853d1ef 428void
b5a28cbc 429monitor_child_postauth(struct monitor *pmonitor)
1853d1ef 430{
3a39206f 431 monitor_set_child_handler(pmonitor->m_pid);
432 signal(SIGHUP, &monitor_child_handler);
433 signal(SIGTERM, &monitor_child_handler);
21cd42db 434 signal(SIGINT, &monitor_child_handler);
3a39206f 435
1853d1ef 436 if (compat20) {
437 mon_dispatch = mon_dispatch_postauth20;
438
439 /* Permit requests for moduli and signatures */
440 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
441 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
442 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
1853d1ef 443 } else {
444 mon_dispatch = mon_dispatch_postauth15;
445 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
446 }
447 if (!no_pty_flag) {
448 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
449 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
450 }
451
452 for (;;)
b5a28cbc 453 monitor_read(pmonitor, mon_dispatch, NULL);
1853d1ef 454}
455
456void
b5a28cbc 457monitor_sync(struct monitor *pmonitor)
1853d1ef 458{
0496cf34 459 if (options.compression) {
460 /* The member allocation is not visible, so sync it */
461 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
462 }
1853d1ef 463}
464
465int
b5a28cbc 466monitor_read(struct monitor *pmonitor, struct mon_table *ent,
1853d1ef 467 struct mon_table **pent)
468{
469 Buffer m;
470 int ret;
471 u_char type;
472
473 buffer_init(&m);
474
b5a28cbc 475 mm_request_receive(pmonitor->m_sendfd, &m);
1853d1ef 476 type = buffer_get_char(&m);
477
1588c277 478 debug3("%s: checking request %d", __func__, type);
1853d1ef 479
480 while (ent->f != NULL) {
481 if (ent->type == type)
482 break;
483 ent++;
484 }
485
486 if (ent->f != NULL) {
487 if (!(ent->flags & MON_PERMIT))
1588c277 488 fatal("%s: unpermitted request %d", __func__,
1853d1ef 489 type);
b5a28cbc 490 ret = (*ent->f)(pmonitor->m_sendfd, &m);
1853d1ef 491 buffer_free(&m);
492
493 /* The child may use this request only once, disable it */
494 if (ent->flags & MON_ONCE) {
1588c277 495 debug2("%s: %d used once, disabling now", __func__,
1853d1ef 496 type);
497 ent->flags &= ~MON_PERMIT;
498 }
499
500 if (pent != NULL)
501 *pent = ent;
502
503 return ret;
504 }
505
1588c277 506 fatal("%s: unsupported request: %d", __func__, type);
1853d1ef 507
508 /* NOTREACHED */
509 return (-1);
510}
511
512/* allowed key state */
513static int
514monitor_allowed_key(u_char *blob, u_int bloblen)
515{
516 /* make sure key is allowed */
517 if (key_blob == NULL || key_bloblen != bloblen ||
518 memcmp(key_blob, blob, key_bloblen))
519 return (0);
520 return (1);
521}
522
523static void
524monitor_reset_key_state(void)
525{
526 /* reset state */
527 if (key_blob != NULL)
528 xfree(key_blob);
529 if (hostbased_cuser != NULL)
530 xfree(hostbased_cuser);
531 if (hostbased_chost != NULL)
532 xfree(hostbased_chost);
533 key_blob = NULL;
534 key_bloblen = 0;
535 key_blobtype = MM_NOKEY;
536 hostbased_cuser = NULL;
537 hostbased_chost = NULL;
538}
539
540int
ca75d7de 541mm_answer_moduli(int sock, Buffer *m)
1853d1ef 542{
543 DH *dh;
544 int min, want, max;
545
546 min = buffer_get_int(m);
547 want = buffer_get_int(m);
548 max = buffer_get_int(m);
549
550 debug3("%s: got parameters: %d %d %d",
1588c277 551 __func__, min, want, max);
1853d1ef 552 /* We need to check here, too, in case the child got corrupted */
553 if (max < min || want < min || max < want)
554 fatal("%s: bad parameters: %d %d %d",
1588c277 555 __func__, min, want, max);
1853d1ef 556
557 buffer_clear(m);
558
559 dh = choose_dh(min, want, max);
560 if (dh == NULL) {
561 buffer_put_char(m, 0);
562 return (0);
563 } else {
564 /* Send first bignum */
565 buffer_put_char(m, 1);
566 buffer_put_bignum2(m, dh->p);
567 buffer_put_bignum2(m, dh->g);
568
569 DH_free(dh);
570 }
ca75d7de 571 mm_request_send(sock, MONITOR_ANS_MODULI, m);
1853d1ef 572 return (0);
573}
574
575int
ca75d7de 576mm_answer_sign(int sock, Buffer *m)
1853d1ef 577{
578 Key *key;
579 u_char *p;
580 u_char *signature;
581 u_int siglen, datlen;
582 int keyid;
583
1588c277 584 debug3("%s", __func__);
1853d1ef 585
586 keyid = buffer_get_int(m);
587 p = buffer_get_string(m, &datlen);
588
2ff8003a 589 /*
b4bbf172 590 * Supported KEX types will only return SHA1 (20 byte) or
2ff8003a 591 * SHA256 (32 byte) hashes
592 */
593 if (datlen != 20 && datlen != 32)
d1ff09ba 594 fatal("%s: data length incorrect: %u", __func__, datlen);
1853d1ef 595
521d606b 596 /* save session id, it will be passed on the first call */
597 if (session_id2_len == 0) {
598 session_id2_len = datlen;
599 session_id2 = xmalloc(session_id2_len);
600 memcpy(session_id2, p, session_id2_len);
601 }
602
1853d1ef 603 if ((key = get_hostkey_by_index(keyid)) == NULL)
1588c277 604 fatal("%s: no hostkey from index %d", __func__, keyid);
1853d1ef 605 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
1588c277 606 fatal("%s: key_sign failed", __func__);
1853d1ef 607
d1ff09ba 608 debug3("%s: signature %p(%u)", __func__, signature, siglen);
1853d1ef 609
610 buffer_clear(m);
611 buffer_put_string(m, signature, siglen);
612
613 xfree(p);
614 xfree(signature);
615
ca75d7de 616 mm_request_send(sock, MONITOR_ANS_SIGN, m);
1853d1ef 617
618 /* Turn on permissions for getpwnam */
619 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
620
621 return (0);
622}
623
624/* Retrieves the password entry and also checks if the user is permitted */
625
626int
ca75d7de 627mm_answer_pwnamallow(int sock, Buffer *m)
1853d1ef 628{
18a8f313 629 char *username;
1853d1ef 630 struct passwd *pwent;
631 int allowed = 0;
632
1588c277 633 debug3("%s", __func__);
1853d1ef 634
635 if (authctxt->attempt++ != 0)
1588c277 636 fatal("%s: multiple attempts for getpwnam", __func__);
1853d1ef 637
18a8f313 638 username = buffer_get_string(m, NULL);
1853d1ef 639
18a8f313 640 pwent = getpwnamallow(username);
1853d1ef 641
18a8f313 642 authctxt->user = xstrdup(username);
643 setproctitle("%s [priv]", pwent ? username : "unknown");
644 xfree(username);
1853d1ef 645
646 buffer_clear(m);
647
648 if (pwent == NULL) {
649 buffer_put_char(m, 0);
b2a5802b 650 authctxt->pw = fakepw();
1853d1ef 651 goto out;
652 }
653
654 allowed = 1;
655 authctxt->pw = pwent;
656 authctxt->valid = 1;
657
658 buffer_put_char(m, 1);
659 buffer_put_string(m, pwent, sizeof(struct passwd));
660 buffer_put_cstring(m, pwent->pw_name);
661 buffer_put_cstring(m, "*");
662 buffer_put_cstring(m, pwent->pw_gecos);
7b18c353 663#ifdef HAVE_PW_CLASS_IN_PASSWD
1853d1ef 664 buffer_put_cstring(m, pwent->pw_class);
7b18c353 665#endif
1853d1ef 666 buffer_put_cstring(m, pwent->pw_dir);
667 buffer_put_cstring(m, pwent->pw_shell);
dc0cae51 668
669 out:
03bcbf84 670 buffer_put_string(m, &options, sizeof(options));
671 if (options.banner != NULL)
672 buffer_put_cstring(m, options.banner);
1588c277 673 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
ca75d7de 674 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
1853d1ef 675
676 /* For SSHv1 allow authentication now */
677 if (!compat20)
678 monitor_permit_authentications(1);
94a73cdc 679 else {
1853d1ef 680 /* Allow service/style information on the auth context */
681 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
94a73cdc 682 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
683 }
1853d1ef 684
efe44db6 685#ifdef USE_PAM
7fceb20d 686 if (options.use_pam)
687 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
efe44db6 688#endif
1853d1ef 689
690 return (0);
691}
692
ca75d7de 693int mm_answer_auth2_read_banner(int sock, Buffer *m)
94a73cdc 694{
695 char *banner;
696
697 buffer_clear(m);
698 banner = auth2_read_banner();
699 buffer_put_cstring(m, banner != NULL ? banner : "");
ca75d7de 700 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
94a73cdc 701
702 if (banner != NULL)
30e37ee6 703 xfree(banner);
94a73cdc 704
705 return (0);
706}
707
1853d1ef 708int
ca75d7de 709mm_answer_authserv(int sock, Buffer *m)
1853d1ef 710{
711 monitor_permit_authentications(1);
712
713 authctxt->service = buffer_get_string(m, NULL);
714 authctxt->style = buffer_get_string(m, NULL);
715 debug3("%s: service=%s, style=%s",
1588c277 716 __func__, authctxt->service, authctxt->style);
1853d1ef 717
718 if (strlen(authctxt->style) == 0) {
719 xfree(authctxt->style);
720 authctxt->style = NULL;
721 }
722
723 return (0);
724}
725
726int
ca75d7de 727mm_answer_authpassword(int sock, Buffer *m)
1853d1ef 728{
729 static int call_count;
730 char *passwd;
d341742a 731 int authenticated;
732 u_int plen;
1853d1ef 733
734 passwd = buffer_get_string(m, &plen);
735 /* Only authenticate if the context is valid */
aa586f8e 736 authenticated = options.password_authentication &&
96d0bf74 737 auth_password(authctxt, passwd);
1853d1ef 738 memset(passwd, 0, strlen(passwd));
739 xfree(passwd);
740
741 buffer_clear(m);
742 buffer_put_int(m, authenticated);
743
1588c277 744 debug3("%s: sending result %d", __func__, authenticated);
ca75d7de 745 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
1853d1ef 746
747 call_count++;
748 if (plen == 0 && call_count == 1)
749 auth_method = "none";
750 else
751 auth_method = "password";
752
753 /* Causes monitor loop to terminate if authenticated */
754 return (authenticated);
755}
756
757#ifdef BSD_AUTH
758int
ca75d7de 759mm_answer_bsdauthquery(int sock, Buffer *m)
1853d1ef 760{
761 char *name, *infotxt;
762 u_int numprompts;
763 u_int *echo_on;
764 char **prompts;
6d6c25e3 765 u_int success;
1853d1ef 766
6d6c25e3 767 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
768 &prompts, &echo_on) < 0 ? 0 : 1;
1853d1ef 769
770 buffer_clear(m);
6d6c25e3 771 buffer_put_int(m, success);
772 if (success)
1853d1ef 773 buffer_put_cstring(m, prompts[0]);
774
6d6c25e3 775 debug3("%s: sending challenge success: %u", __func__, success);
ca75d7de 776 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
1853d1ef 777
6d6c25e3 778 if (success) {
1853d1ef 779 xfree(name);
780 xfree(infotxt);
781 xfree(prompts);
782 xfree(echo_on);
783 }
784
785 return (0);
786}
787
788int
ca75d7de 789mm_answer_bsdauthrespond(int sock, Buffer *m)
1853d1ef 790{
791 char *response;
792 int authok;
793
794 if (authctxt->as == 0)
1588c277 795 fatal("%s: no bsd auth session", __func__);
1853d1ef 796
797 response = buffer_get_string(m, NULL);
aa586f8e 798 authok = options.challenge_response_authentication &&
799 auth_userresponse(authctxt->as, response, 0);
1853d1ef 800 authctxt->as = NULL;
1588c277 801 debug3("%s: <%s> = <%d>", __func__, response, authok);
1853d1ef 802 xfree(response);
803
804 buffer_clear(m);
805 buffer_put_int(m, authok);
806
1588c277 807 debug3("%s: sending authenticated: %d", __func__, authok);
ca75d7de 808 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
1853d1ef 809
810 auth_method = "bsdauth";
811
812 return (authok != 0);
813}
814#endif
815
816#ifdef SKEY
817int
ca75d7de 818mm_answer_skeyquery(int sock, Buffer *m)
1853d1ef 819{
820 struct skey skey;
821 char challenge[1024];
6d6c25e3 822 u_int success;
1853d1ef 823
f2b7b5c8 824 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
825 sizeof(challenge)) < 0 ? 0 : 1;
1853d1ef 826
827 buffer_clear(m);
6d6c25e3 828 buffer_put_int(m, success);
829 if (success)
1853d1ef 830 buffer_put_cstring(m, challenge);
831
6d6c25e3 832 debug3("%s: sending challenge success: %u", __func__, success);
ca75d7de 833 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
1853d1ef 834
835 return (0);
836}
837
838int
ca75d7de 839mm_answer_skeyrespond(int sock, Buffer *m)
1853d1ef 840{
841 char *response;
842 int authok;
843
844 response = buffer_get_string(m, NULL);
845
aa586f8e 846 authok = (options.challenge_response_authentication &&
847 authctxt->valid &&
1853d1ef 848 skey_haskey(authctxt->pw->pw_name) == 0 &&
849 skey_passcheck(authctxt->pw->pw_name, response) != -1);
850
851 xfree(response);
852
853 buffer_clear(m);
854 buffer_put_int(m, authok);
855
1588c277 856 debug3("%s: sending authenticated: %d", __func__, authok);
ca75d7de 857 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
1853d1ef 858
859 auth_method = "skey";
860
861 return (authok != 0);
862}
863#endif
864
ad200abb 865#ifdef USE_PAM
866int
8b3ec5fd 867mm_answer_pam_start(int sock, Buffer *m)
ad200abb 868{
7fceb20d 869 if (!options.use_pam)
870 fatal("UsePAM not set, but ended up in %s anyway", __func__);
871
529d73ab 872 start_pam(authctxt);
ad200abb 873
5b9e2464 874 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
875
ad200abb 876 return (0);
877}
05114c74 878
5b9e2464 879int
8b3ec5fd 880mm_answer_pam_account(int sock, Buffer *m)
5b9e2464 881{
882 u_int ret;
b6453d99 883
5b9e2464 884 if (!options.use_pam)
885 fatal("UsePAM not set, but ended up in %s anyway", __func__);
886
887 ret = do_pam_account();
888
889 buffer_put_int(m, ret);
cfb60d3a 890 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
5b9e2464 891
8b3ec5fd 892 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
5b9e2464 893
894 return (ret);
895}
896
05114c74 897static void *sshpam_ctxt, *sshpam_authok;
898extern KbdintDevice sshpam_device;
899
900int
8b3ec5fd 901mm_answer_pam_init_ctx(int sock, Buffer *m)
05114c74 902{
903
904 debug3("%s", __func__);
905 authctxt->user = buffer_get_string(m, NULL);
906 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
907 sshpam_authok = NULL;
908 buffer_clear(m);
909 if (sshpam_ctxt != NULL) {
910 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
911 buffer_put_int(m, 1);
912 } else {
913 buffer_put_int(m, 0);
914 }
8b3ec5fd 915 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
05114c74 916 return (0);
917}
918
919int
8b3ec5fd 920mm_answer_pam_query(int sock, Buffer *m)
05114c74 921{
922 char *name, *info, **prompts;
a1a073cc 923 u_int i, num, *echo_on;
924 int ret;
05114c74 925
926 debug3("%s", __func__);
927 sshpam_authok = NULL;
928 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
929 if (ret == 0 && num == 0)
930 sshpam_authok = sshpam_ctxt;
931 if (num > 1 || name == NULL || info == NULL)
932 ret = -1;
933 buffer_clear(m);
934 buffer_put_int(m, ret);
935 buffer_put_cstring(m, name);
936 xfree(name);
937 buffer_put_cstring(m, info);
938 xfree(info);
939 buffer_put_int(m, num);
940 for (i = 0; i < num; ++i) {
941 buffer_put_cstring(m, prompts[i]);
942 xfree(prompts[i]);
943 buffer_put_int(m, echo_on[i]);
944 }
945 if (prompts != NULL)
946 xfree(prompts);
947 if (echo_on != NULL)
948 xfree(echo_on);
2ae7f715 949 auth_method = "keyboard-interactive/pam";
8b3ec5fd 950 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
05114c74 951 return (0);
952}
953
954int
8b3ec5fd 955mm_answer_pam_respond(int sock, Buffer *m)
05114c74 956{
957 char **resp;
a1a073cc 958 u_int i, num;
959 int ret;
05114c74 960
961 debug3("%s", __func__);
962 sshpam_authok = NULL;
963 num = buffer_get_int(m);
964 if (num > 0) {
01d35895 965 resp = xcalloc(num, sizeof(char *));
05114c74 966 for (i = 0; i < num; ++i)
967 resp[i] = buffer_get_string(m, NULL);
968 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
969 for (i = 0; i < num; ++i)
970 xfree(resp[i]);
971 xfree(resp);
972 } else {
973 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
974 }
975 buffer_clear(m);
976 buffer_put_int(m, ret);
8b3ec5fd 977 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
05114c74 978 auth_method = "keyboard-interactive/pam";
979 if (ret == 0)
980 sshpam_authok = sshpam_ctxt;
981 return (0);
982}
983
984int
8b3ec5fd 985mm_answer_pam_free_ctx(int sock, Buffer *m)
05114c74 986{
987
988 debug3("%s", __func__);
989 (sshpam_device.free_ctx)(sshpam_ctxt);
990 buffer_clear(m);
8b3ec5fd 991 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
2ae7f715 992 auth_method = "keyboard-interactive/pam";
05114c74 993 return (sshpam_authok == sshpam_ctxt);
994}
ad200abb 995#endif
996
1853d1ef 997static void
998mm_append_debug(Buffer *m)
999{
1000 if (auth_debug_init && buffer_len(&auth_debug)) {
1588c277 1001 debug3("%s: Appending debug messages for child", __func__);
1853d1ef 1002 buffer_append(m, buffer_ptr(&auth_debug),
1003 buffer_len(&auth_debug));
1004 buffer_clear(&auth_debug);
1005 }
1006}
1007
1008int
ca75d7de 1009mm_answer_keyallowed(int sock, Buffer *m)
1853d1ef 1010{
1011 Key *key;
661e45a0 1012 char *cuser, *chost;
1013 u_char *blob;
1853d1ef 1014 u_int bloblen;
1015 enum mm_keytype type = 0;
1016 int allowed = 0;
1017
1588c277 1018 debug3("%s entering", __func__);
1853d1ef 1019
1020 type = buffer_get_int(m);
1021 cuser = buffer_get_string(m, NULL);
1022 chost = buffer_get_string(m, NULL);
1023 blob = buffer_get_string(m, &bloblen);
1024
1025 key = key_from_blob(blob, bloblen);
1026
1027 if ((compat20 && type == MM_RSAHOSTKEY) ||
1028 (!compat20 && type != MM_RSAHOSTKEY))
1588c277 1029 fatal("%s: key type and protocol mismatch", __func__);
1853d1ef 1030
1588c277 1031 debug3("%s: key_from_blob: %p", __func__, key);
1853d1ef 1032
fdaef11e 1033 if (key != NULL && authctxt->valid) {
f8cc7664 1034 switch (type) {
1853d1ef 1035 case MM_USERKEY:
aa586f8e 1036 allowed = options.pubkey_authentication &&
1037 user_key_allowed(authctxt->pw, key);
c023a130 1038 auth_method = "publickey";
52ad6b9a 1039 if (options.pubkey_authentication && allowed != 1)
1040 auth_clear_options();
1853d1ef 1041 break;
1042 case MM_HOSTKEY:
aa586f8e 1043 allowed = options.hostbased_authentication &&
1044 hostbased_key_allowed(authctxt->pw,
1853d1ef 1045 cuser, chost, key);
c023a130 1046 auth_method = "hostbased";
1853d1ef 1047 break;
1048 case MM_RSAHOSTKEY:
1049 key->type = KEY_RSA1; /* XXX */
aa586f8e 1050 allowed = options.rhosts_rsa_authentication &&
1051 auth_rhosts_rsa_key_allowed(authctxt->pw,
1853d1ef 1052 cuser, chost, key);
52ad6b9a 1053 if (options.rhosts_rsa_authentication && allowed != 1)
1054 auth_clear_options();
c023a130 1055 auth_method = "rsa";
1853d1ef 1056 break;
1057 default:
1588c277 1058 fatal("%s: unknown key type %d", __func__, type);
1853d1ef 1059 break;
1060 }
1853d1ef 1061 }
3e2f2431 1062 if (key != NULL)
1063 key_free(key);
1853d1ef 1064
1065 /* clear temporarily storage (used by verify) */
1066 monitor_reset_key_state();
1067
1068 if (allowed) {
1069 /* Save temporarily for comparison in verify */
1070 key_blob = blob;
1071 key_bloblen = bloblen;
1072 key_blobtype = type;
1073 hostbased_cuser = cuser;
1074 hostbased_chost = chost;
98d40a74 1075 } else {
c023a130 1076 /* Log failed attempt */
1077 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
98d40a74 1078 xfree(blob);
1079 xfree(cuser);
1080 xfree(chost);
1853d1ef 1081 }
1082
1083 debug3("%s: key %p is %s",
c54d3d1c 1084 __func__, key, allowed ? "allowed" : "not allowed");
1853d1ef 1085
1086 buffer_clear(m);
1087 buffer_put_int(m, allowed);
92e15201 1088 buffer_put_int(m, forced_command != NULL);
1853d1ef 1089
1090 mm_append_debug(m);
1091
ca75d7de 1092 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1853d1ef 1093
1094 if (type == MM_RSAHOSTKEY)
1095 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1096
1097 return (0);
1098}
1099
1100static int
1101monitor_valid_userblob(u_char *data, u_int datalen)
1102{
1103 Buffer b;
661e45a0 1104 char *p;
1853d1ef 1105 u_int len;
1106 int fail = 0;
1853d1ef 1107
1108 buffer_init(&b);
1109 buffer_append(&b, data, datalen);
1110
1111 if (datafellows & SSH_OLD_SESSIONID) {
521d606b 1112 p = buffer_ptr(&b);
1113 len = buffer_len(&b);
1114 if ((session_id2 == NULL) ||
1115 (len < session_id2_len) ||
1116 (memcmp(p, session_id2, session_id2_len) != 0))
1117 fail++;
1853d1ef 1118 buffer_consume(&b, session_id2_len);
1119 } else {
521d606b 1120 p = buffer_get_string(&b, &len);
1121 if ((session_id2 == NULL) ||
1122 (len != session_id2_len) ||
1123 (memcmp(p, session_id2, session_id2_len) != 0))
1853d1ef 1124 fail++;
521d606b 1125 xfree(p);
1853d1ef 1126 }
1127 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1128 fail++;
1129 p = buffer_get_string(&b, NULL);
1130 if (strcmp(authctxt->user, p) != 0) {
bbe88b6d 1131 logit("wrong user name passed to monitor: expected %s != %.100s",
1853d1ef 1132 authctxt->user, p);
1133 fail++;
1134 }
1135 xfree(p);
1136 buffer_skip_string(&b);
1137 if (datafellows & SSH_BUG_PKAUTH) {
1138 if (!buffer_get_char(&b))
1139 fail++;
1140 } else {
1141 p = buffer_get_string(&b, NULL);
1142 if (strcmp("publickey", p) != 0)
1143 fail++;
1144 xfree(p);
1145 if (!buffer_get_char(&b))
1146 fail++;
1147 buffer_skip_string(&b);
1148 }
1149 buffer_skip_string(&b);
1150 if (buffer_len(&b) != 0)
1151 fail++;
1152 buffer_free(&b);
1153 return (fail == 0);
1154}
1155
1156static int
661e45a0 1157monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1158 char *chost)
1853d1ef 1159{
1160 Buffer b;
661e45a0 1161 char *p;
1853d1ef 1162 u_int len;
1163 int fail = 0;
1853d1ef 1164
1165 buffer_init(&b);
1166 buffer_append(&b, data, datalen);
1167
521d606b 1168 p = buffer_get_string(&b, &len);
1169 if ((session_id2 == NULL) ||
1170 (len != session_id2_len) ||
1171 (memcmp(p, session_id2, session_id2_len) != 0))
1853d1ef 1172 fail++;
521d606b 1173 xfree(p);
1174
1853d1ef 1175 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1176 fail++;
1177 p = buffer_get_string(&b, NULL);
1178 if (strcmp(authctxt->user, p) != 0) {
bbe88b6d 1179 logit("wrong user name passed to monitor: expected %s != %.100s",
1853d1ef 1180 authctxt->user, p);
1181 fail++;
1182 }
1183 xfree(p);
1184 buffer_skip_string(&b); /* service */
1185 p = buffer_get_string(&b, NULL);
1186 if (strcmp(p, "hostbased") != 0)
1187 fail++;
1188 xfree(p);
1189 buffer_skip_string(&b); /* pkalg */
1190 buffer_skip_string(&b); /* pkblob */
1191
1192 /* verify client host, strip trailing dot if necessary */
1193 p = buffer_get_string(&b, NULL);
1194 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1195 p[len - 1] = '\0';
1196 if (strcmp(p, chost) != 0)
1197 fail++;
1198 xfree(p);
1199
1200 /* verify client user */
1201 p = buffer_get_string(&b, NULL);
1202 if (strcmp(p, cuser) != 0)
1203 fail++;
1204 xfree(p);
1205
1206 if (buffer_len(&b) != 0)
1207 fail++;
1208 buffer_free(&b);
1209 return (fail == 0);
1210}
1211
1212int
ca75d7de 1213mm_answer_keyverify(int sock, Buffer *m)
1853d1ef 1214{
1215 Key *key;
1216 u_char *signature, *data, *blob;
1217 u_int signaturelen, datalen, bloblen;
1218 int verified = 0;
1219 int valid_data = 0;
1220
1221 blob = buffer_get_string(m, &bloblen);
1222 signature = buffer_get_string(m, &signaturelen);
1223 data = buffer_get_string(m, &datalen);
1224
1225 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
300e01c4 1226 !monitor_allowed_key(blob, bloblen))
1588c277 1227 fatal("%s: bad key, not previously allowed", __func__);
1853d1ef 1228
1229 key = key_from_blob(blob, bloblen);
1230 if (key == NULL)
1588c277 1231 fatal("%s: bad public key blob", __func__);
1853d1ef 1232
1233 switch (key_blobtype) {
1234 case MM_USERKEY:
1235 valid_data = monitor_valid_userblob(data, datalen);
1236 break;
1237 case MM_HOSTKEY:
1238 valid_data = monitor_valid_hostbasedblob(data, datalen,
1239 hostbased_cuser, hostbased_chost);
1240 break;
1241 default:
1242 valid_data = 0;
1243 break;
1244 }
1245 if (!valid_data)
1588c277 1246 fatal("%s: bad signature data blob", __func__);
1853d1ef 1247
1248 verified = key_verify(key, signature, signaturelen, data, datalen);
1249 debug3("%s: key %p signature %s",
89916e8c 1250 __func__, key, (verified == 1) ? "verified" : "unverified");
1853d1ef 1251
1252 key_free(key);
1253 xfree(blob);
1254 xfree(signature);
1255 xfree(data);
1256
d17ef027 1257 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1258
1853d1ef 1259 monitor_reset_key_state();
1260
1261 buffer_clear(m);
1262 buffer_put_int(m, verified);
ca75d7de 1263 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1853d1ef 1264
89916e8c 1265 return (verified == 1);
1853d1ef 1266}
1267
1268static void
1269mm_record_login(Session *s, struct passwd *pw)
1270{
1271 socklen_t fromlen;
1272 struct sockaddr_storage from;
1273
1274 /*
1275 * Get IP address of client. If the connection is not a socket, let
1276 * the address be 0.0.0.0.
1277 */
1278 memset(&from, 0, sizeof(from));
ba1566dd 1279 fromlen = sizeof(from);
1853d1ef 1280 if (packet_connection_is_on_socket()) {
1853d1ef 1281 if (getpeername(packet_get_connection_in(),
b248a875 1282 (struct sockaddr *)&from, &fromlen) < 0) {
1853d1ef 1283 debug("getpeername: %.100s", strerror(errno));
2362db19 1284 cleanup_exit(255);
1853d1ef 1285 }
1286 }
1287 /* Record that there was a login on that tty from the remote host. */
1288 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
c5a7d788 1289 get_remote_name_or_ip(utmp_len, options.use_dns),
ba1566dd 1290 (struct sockaddr *)&from, fromlen);
1853d1ef 1291}
1292
1293static void
1294mm_session_close(Session *s)
1295{
b9ed513a 1296 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1853d1ef 1297 if (s->ttyfd != -1) {
c56969f9 1298 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1853d1ef 1299 session_pty_cleanup2(s);
1300 }
c6dca55e 1301 session_unused(s->self);
1853d1ef 1302}
1303
1304int
ca75d7de 1305mm_answer_pty(int sock, Buffer *m)
1853d1ef 1306{
b5a28cbc 1307 extern struct monitor *pmonitor;
1853d1ef 1308 Session *s;
1309 int res, fd0;
1310
1588c277 1311 debug3("%s entering", __func__);
1853d1ef 1312
1313 buffer_clear(m);
1314 s = session_new();
1315 if (s == NULL)
1316 goto error;
1317 s->authctxt = authctxt;
1318 s->pw = authctxt->pw;
b5a28cbc 1319 s->pid = pmonitor->m_pid;
1853d1ef 1320 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1321 if (res == 0)
1322 goto error;
1853d1ef 1323 pty_setowner(authctxt->pw, s->tty);
1324
1325 buffer_put_int(m, 1);
1326 buffer_put_cstring(m, s->tty);
1853d1ef 1327
1328 /* We need to trick ttyslot */
1329 if (dup2(s->ttyfd, 0) == -1)
1588c277 1330 fatal("%s: dup2", __func__);
1853d1ef 1331
1332 mm_record_login(s, authctxt->pw);
1333
1334 /* Now we can close the file descriptor again */
1335 close(0);
1336
be2ca0c9 1337 /* send messages generated by record_login */
1338 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1339 buffer_clear(&loginmsg);
1340
1341 mm_request_send(sock, MONITOR_ANS_PTY, m);
1342
8de7aaab 1343 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1344 mm_send_fd(sock, s->ttyfd) == -1)
1345 fatal("%s: send fds failed", __func__);
be2ca0c9 1346
1853d1ef 1347 /* make sure nothing uses fd 0 */
1348 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1588c277 1349 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1853d1ef 1350 if (fd0 != 0)
1588c277 1351 error("%s: fd0 %d != 0", __func__, fd0);
1853d1ef 1352
1353 /* slave is not needed */
1354 close(s->ttyfd);
1355 s->ttyfd = s->ptyfd;
1356 /* no need to dup() because nobody closes ptyfd */
1357 s->ptymaster = s->ptyfd;
1358
c56969f9 1359 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1853d1ef 1360
1361 return (0);
1362
1363 error:
1364 if (s != NULL)
1365 mm_session_close(s);
1366 buffer_put_int(m, 0);
ca75d7de 1367 mm_request_send(sock, MONITOR_ANS_PTY, m);
1853d1ef 1368 return (0);
1369}
1370
1371int
ca75d7de 1372mm_answer_pty_cleanup(int sock, Buffer *m)
1853d1ef 1373{
1374 Session *s;
1375 char *tty;
1376
1588c277 1377 debug3("%s entering", __func__);
1853d1ef 1378
1379 tty = buffer_get_string(m, NULL);
1380 if ((s = session_by_tty(tty)) != NULL)
1381 mm_session_close(s);
1382 buffer_clear(m);
1383 xfree(tty);
1384 return (0);
1385}
1386
1387int
ca75d7de 1388mm_answer_sesskey(int sock, Buffer *m)
1853d1ef 1389{
1390 BIGNUM *p;
1391 int rsafail;
1392
1393 /* Turn off permissions */
75cccc2c 1394 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1853d1ef 1395
1396 if ((p = BN_new()) == NULL)
1588c277 1397 fatal("%s: BN_new", __func__);
1853d1ef 1398
1399 buffer_get_bignum2(m, p);
1400
1401 rsafail = ssh1_session_key(p);
1402
1403 buffer_clear(m);
1404 buffer_put_int(m, rsafail);
1405 buffer_put_bignum2(m, p);
1406
1407 BN_clear_free(p);
1408
ca75d7de 1409 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1853d1ef 1410
1411 /* Turn on permissions for sessid passing */
1412 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1413
1414 return (0);
1415}
1416
1417int
ca75d7de 1418mm_answer_sessid(int sock, Buffer *m)
1853d1ef 1419{
1420 int i;
1421
1588c277 1422 debug3("%s entering", __func__);
1853d1ef 1423
1424 if (buffer_len(m) != 16)
1588c277 1425 fatal("%s: bad ssh1 session id", __func__);
1853d1ef 1426 for (i = 0; i < 16; i++)
1427 session_id[i] = buffer_get_char(m);
1428
1429 /* Turn on permissions for getpwnam */
1430 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1431
1432 return (0);
1433}
1434
1435int
ca75d7de 1436mm_answer_rsa_keyallowed(int sock, Buffer *m)
1853d1ef 1437{
1438 BIGNUM *client_n;
1439 Key *key = NULL;
1440 u_char *blob = NULL;
1441 u_int blen = 0;
1442 int allowed = 0;
1443
1588c277 1444 debug3("%s entering", __func__);
1853d1ef 1445
c023a130 1446 auth_method = "rsa";
aa586f8e 1447 if (options.rsa_authentication && authctxt->valid) {
1853d1ef 1448 if ((client_n = BN_new()) == NULL)
1588c277 1449 fatal("%s: BN_new", __func__);
1853d1ef 1450 buffer_get_bignum2(m, client_n);
1451 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1452 BN_clear_free(client_n);
1453 }
1454 buffer_clear(m);
1455 buffer_put_int(m, allowed);
92e15201 1456 buffer_put_int(m, forced_command != NULL);
1853d1ef 1457
1458 /* clear temporarily storage (used by generate challenge) */
1459 monitor_reset_key_state();
1460
1461 if (allowed && key != NULL) {
1462 key->type = KEY_RSA; /* cheat for key_to_blob */
1463 if (key_to_blob(key, &blob, &blen) == 0)
1588c277 1464 fatal("%s: key_to_blob failed", __func__);
1853d1ef 1465 buffer_put_string(m, blob, blen);
1466
1467 /* Save temporarily for comparison in verify */
1468 key_blob = blob;
1469 key_bloblen = blen;
1470 key_blobtype = MM_RSAUSERKEY;
1853d1ef 1471 }
3e2f2431 1472 if (key != NULL)
1473 key_free(key);
1853d1ef 1474
1475 mm_append_debug(m);
1476
ca75d7de 1477 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1853d1ef 1478
1479 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1480 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1481 return (0);
1482}
1483
1484int
ca75d7de 1485mm_answer_rsa_challenge(int sock, Buffer *m)
1853d1ef 1486{
1487 Key *key = NULL;
1488 u_char *blob;
1489 u_int blen;
1490
1588c277 1491 debug3("%s entering", __func__);
1853d1ef 1492
1493 if (!authctxt->valid)
1588c277 1494 fatal("%s: authctxt not valid", __func__);
1853d1ef 1495 blob = buffer_get_string(m, &blen);
1496 if (!monitor_allowed_key(blob, blen))
1588c277 1497 fatal("%s: bad key, not previously allowed", __func__);
1853d1ef 1498 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1588c277 1499 fatal("%s: key type mismatch", __func__);
1853d1ef 1500 if ((key = key_from_blob(blob, blen)) == NULL)
1588c277 1501 fatal("%s: received bad key", __func__);
28b5d376 1502 if (key->type != KEY_RSA)
1503 fatal("%s: received bad key type %d", __func__, key->type);
1504 key->type = KEY_RSA1;
1853d1ef 1505 if (ssh1_challenge)
1506 BN_clear_free(ssh1_challenge);
1507 ssh1_challenge = auth_rsa_generate_challenge(key);
1508
1509 buffer_clear(m);
1510 buffer_put_bignum2(m, ssh1_challenge);
1511
1588c277 1512 debug3("%s sending reply", __func__);
ca75d7de 1513 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1853d1ef 1514
1515 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
3e2f2431 1516
1517 xfree(blob);
1518 key_free(key);
1853d1ef 1519 return (0);
1520}
1521
1522int
ca75d7de 1523mm_answer_rsa_response(int sock, Buffer *m)
1853d1ef 1524{
1525 Key *key = NULL;
1526 u_char *blob, *response;
1527 u_int blen, len;
1528 int success;
1529
1588c277 1530 debug3("%s entering", __func__);
1853d1ef 1531
1532 if (!authctxt->valid)
1588c277 1533 fatal("%s: authctxt not valid", __func__);
1853d1ef 1534 if (ssh1_challenge == NULL)
1588c277 1535 fatal("%s: no ssh1_challenge", __func__);
1853d1ef 1536
1537 blob = buffer_get_string(m, &blen);
1538 if (!monitor_allowed_key(blob, blen))
1588c277 1539 fatal("%s: bad key, not previously allowed", __func__);
1853d1ef 1540 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1588c277 1541 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1853d1ef 1542 if ((key = key_from_blob(blob, blen)) == NULL)
1588c277 1543 fatal("%s: received bad key", __func__);
1853d1ef 1544 response = buffer_get_string(m, &len);
1545 if (len != 16)
1588c277 1546 fatal("%s: received bad response to challenge", __func__);
1853d1ef 1547 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1548
3e2f2431 1549 xfree(blob);
1853d1ef 1550 key_free(key);
1551 xfree(response);
1552
1553 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1554
1555 /* reset state */
1556 BN_clear_free(ssh1_challenge);
1557 ssh1_challenge = NULL;
1558 monitor_reset_key_state();
1559
1560 buffer_clear(m);
1561 buffer_put_int(m, success);
ca75d7de 1562 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1853d1ef 1563
1564 return (success);
1565}
1566
1567int
ca75d7de 1568mm_answer_term(int sock, Buffer *req)
1853d1ef 1569{
b5a28cbc 1570 extern struct monitor *pmonitor;
1853d1ef 1571 int res, status;
1572
1588c277 1573 debug3("%s: tearing down sessions", __func__);
1853d1ef 1574
1575 /* The child is terminating */
1576 session_destroy_all(&mm_session_close);
1577
83fa7e8d 1578#ifdef USE_PAM
1579 if (options.use_pam)
1580 sshpam_cleanup();
1581#endif
1582
b5a28cbc 1583 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
8c38e88b 1584 if (errno != EINTR)
1585 exit(1);
1853d1ef 1586
1587 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1588
1589 /* Terminate process */
f6be21a0 1590 exit(res);
1853d1ef 1591}
1592
6039eeef 1593#ifdef SSH_AUDIT_EVENTS
c00e4d75 1594/* Report that an audit event occurred */
1595int
1596mm_answer_audit_event(int socket, Buffer *m)
1597{
1598 ssh_audit_event_t event;
1599
1600 debug3("%s entering", __func__);
1601
1602 event = buffer_get_int(m);
c00e4d75 1603 switch(event) {
6039eeef 1604 case SSH_AUTH_FAIL_PUBKEY:
1605 case SSH_AUTH_FAIL_HOSTBASED:
1606 case SSH_AUTH_FAIL_GSSAPI:
1607 case SSH_LOGIN_EXCEED_MAXTRIES:
1608 case SSH_LOGIN_ROOT_DENIED:
1609 case SSH_CONNECTION_CLOSE:
1610 case SSH_INVALID_USER:
c00e4d75 1611 audit_event(event);
1612 break;
1613 default:
1614 fatal("Audit event type %d not permitted", event);
1615 }
1616
1617 return (0);
1618}
1619
1620int
1621mm_answer_audit_command(int socket, Buffer *m)
1622{
1623 u_int len;
1624 char *cmd;
1625
1626 debug3("%s entering", __func__);
1627 cmd = buffer_get_string(m, &len);
1628 /* sanity check command, if so how? */
1629 audit_run_command(cmd);
1630 xfree(cmd);
c00e4d75 1631 return (0);
1632}
6039eeef 1633#endif /* SSH_AUDIT_EVENTS */
c00e4d75 1634
1853d1ef 1635void
b5a28cbc 1636monitor_apply_keystate(struct monitor *pmonitor)
1853d1ef 1637{
1638 if (compat20) {
1639 set_newkeys(MODE_IN);
1640 set_newkeys(MODE_OUT);
1641 } else {
1853d1ef 1642 packet_set_protocol_flags(child_state.ssh1protoflags);
9459414c 1643 packet_set_encryption_key(child_state.ssh1key,
1644 child_state.ssh1keylen, child_state.ssh1cipher);
1645 xfree(child_state.ssh1key);
1853d1ef 1646 }
1647
9459414c 1648 /* for rc4 and other stateful ciphers */
1853d1ef 1649 packet_set_keycontext(MODE_OUT, child_state.keyout);
1650 xfree(child_state.keyout);
1651 packet_set_keycontext(MODE_IN, child_state.keyin);
1652 xfree(child_state.keyin);
1653
1654 if (!compat20) {
1655 packet_set_iv(MODE_OUT, child_state.ivout);
1656 xfree(child_state.ivout);
1657 packet_set_iv(MODE_IN, child_state.ivin);
1658 xfree(child_state.ivin);
1659 }
1660
1661 memcpy(&incoming_stream, &child_state.incoming,
1662 sizeof(incoming_stream));
1663 memcpy(&outgoing_stream, &child_state.outgoing,
1664 sizeof(outgoing_stream));
1665
1666 /* Update with new address */
0496cf34 1667 if (options.compression)
1668 mm_init_compression(pmonitor->m_zlib);
1853d1ef 1669
1670 /* Network I/O buffers */
1671 /* XXX inefficient for large buffers, need: buffer_init_from_string */
dc1f1948 1672 buffer_clear(packet_get_input());
1673 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
1853d1ef 1674 memset(child_state.input, 0, child_state.ilen);
1675 xfree(child_state.input);
1676
dc1f1948 1677 buffer_clear(packet_get_output());
1678 buffer_append(packet_get_output(), child_state.output,
1679 child_state.olen);
1853d1ef 1680 memset(child_state.output, 0, child_state.olen);
1681 xfree(child_state.output);
1682}
1683
1684static Kex *
1685mm_get_kex(Buffer *m)
1686{
1687 Kex *kex;
1688 void *blob;
1689 u_int bloblen;
1690
52e3daed 1691 kex = xcalloc(1, sizeof(*kex));
1853d1ef 1692 kex->session_id = buffer_get_string(m, &kex->session_id_len);
521d606b 1693 if ((session_id2 == NULL) ||
1694 (kex->session_id_len != session_id2_len) ||
1695 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1696 fatal("mm_get_get: internal error: bad session id");
1853d1ef 1697 kex->we_need = buffer_get_int(m);
cdb64c4d 1698 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
41e5bd9a 1699 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
cdb64c4d 1700 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
2ff8003a 1701 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1853d1ef 1702 kex->server = 1;
1703 kex->hostkey_type = buffer_get_int(m);
1704 kex->kex_type = buffer_get_int(m);
1705 blob = buffer_get_string(m, &bloblen);
1706 buffer_init(&kex->my);
1707 buffer_append(&kex->my, blob, bloblen);
1708 xfree(blob);
1709 blob = buffer_get_string(m, &bloblen);
1710 buffer_init(&kex->peer);
1711 buffer_append(&kex->peer, blob, bloblen);
1712 xfree(blob);
1713 kex->done = 1;
1714 kex->flags = buffer_get_int(m);
1715 kex->client_version_string = buffer_get_string(m, NULL);
1716 kex->server_version_string = buffer_get_string(m, NULL);
1717 kex->load_host_key=&get_hostkey_by_type;
1718 kex->host_key_index=&get_hostkey_index;
1719
1720 return (kex);
1721}
1722
1723/* This function requries careful sanity checking */
1724
1725void
b5a28cbc 1726mm_get_keystate(struct monitor *pmonitor)
1853d1ef 1727{
1728 Buffer m;
1729 u_char *blob, *p;
1730 u_int bloblen, plen;
ffd7b36b 1731 u_int32_t seqnr, packets;
e8e08a80 1732 u_int64_t blocks, bytes;
1853d1ef 1733
1588c277 1734 debug3("%s: Waiting for new keys", __func__);
1853d1ef 1735
1736 buffer_init(&m);
b5a28cbc 1737 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1853d1ef 1738 if (!compat20) {
1739 child_state.ssh1protoflags = buffer_get_int(&m);
1740 child_state.ssh1cipher = buffer_get_int(&m);
9459414c 1741 child_state.ssh1key = buffer_get_string(&m,
1742 &child_state.ssh1keylen);
1853d1ef 1743 child_state.ivout = buffer_get_string(&m,
1744 &child_state.ivoutlen);
1745 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1746 goto skip;
1747 } else {
1748 /* Get the Kex for rekeying */
b5a28cbc 1749 *pmonitor->m_pkex = mm_get_kex(&m);
1853d1ef 1750 }
1751
1752 blob = buffer_get_string(&m, &bloblen);
1753 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1754 xfree(blob);
1755
1588c277 1756 debug3("%s: Waiting for second key", __func__);
1853d1ef 1757 blob = buffer_get_string(&m, &bloblen);
1758 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1759 xfree(blob);
1760
1761 /* Now get sequence numbers for the packets */
ffd7b36b 1762 seqnr = buffer_get_int(&m);
1763 blocks = buffer_get_int64(&m);
1764 packets = buffer_get_int(&m);
e8e08a80 1765 bytes = buffer_get_int64(&m);
1766 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
ffd7b36b 1767 seqnr = buffer_get_int(&m);
1768 blocks = buffer_get_int64(&m);
1769 packets = buffer_get_int(&m);
e8e08a80 1770 bytes = buffer_get_int64(&m);
1771 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
1853d1ef 1772
1773 skip:
1774 /* Get the key context */
1775 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1776 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1777
1588c277 1778 debug3("%s: Getting compression state", __func__);
1853d1ef 1779 /* Get compression state */
1780 p = buffer_get_string(&m, &plen);
1781 if (plen != sizeof(child_state.outgoing))
1588c277 1782 fatal("%s: bad request size", __func__);
1853d1ef 1783 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1784 xfree(p);
1785
1786 p = buffer_get_string(&m, &plen);
1787 if (plen != sizeof(child_state.incoming))
1588c277 1788 fatal("%s: bad request size", __func__);
1853d1ef 1789 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1790 xfree(p);
1791
1792 /* Network I/O buffers */
1588c277 1793 debug3("%s: Getting Network I/O buffers", __func__);
1853d1ef 1794 child_state.input = buffer_get_string(&m, &child_state.ilen);
1795 child_state.output = buffer_get_string(&m, &child_state.olen);
1796
1797 buffer_free(&m);
1798}
1799
1800
1801/* Allocation functions for zlib */
1802void *
1803mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1804{
39e71188 1805 size_t len = (size_t) size * ncount;
1853d1ef 1806 void *address;
1807
b85698ab 1808 if (len == 0 || ncount > SIZE_T_MAX / size)
2f095a0e 1809 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1810
1811 address = mm_malloc(mm, len);
1853d1ef 1812
1813 return (address);
1814}
1815
1816void
1817mm_zfree(struct mm_master *mm, void *address)
1818{
1819 mm_free(mm, address);
1820}
1821
1822void
1823mm_init_compression(struct mm_master *mm)
1824{
1825 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1826 outgoing_stream.zfree = (free_func)mm_zfree;
1827 outgoing_stream.opaque = mm;
1828
1829 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1830 incoming_stream.zfree = (free_func)mm_zfree;
1831 incoming_stream.opaque = mm;
1832}
1833
1834/* XXX */
1835
1836#define FD_CLOSEONEXEC(x) do { \
1837 if (fcntl(x, F_SETFD, 1) == -1) \
1838 fatal("fcntl(%d, F_SETFD)", x); \
1839} while (0)
1840
1841static void
1842monitor_socketpair(int *pair)
1843{
f1af2dbf 1844#ifdef HAVE_SOCKETPAIR
1853d1ef 1845 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1588c277 1846 fatal("%s: socketpair", __func__);
f1af2dbf 1847#else
1848 fatal("%s: UsePrivilegeSeparation=yes not supported",
1588c277 1849 __func__);
f1af2dbf 1850#endif
1853d1ef 1851 FD_CLOSEONEXEC(pair[0]);
1852 FD_CLOSEONEXEC(pair[1]);
1853}
1854
1855#define MM_MEMSIZE 65536
1856
1857struct monitor *
1858monitor_init(void)
1859{
1860 struct monitor *mon;
1861 int pair[2];
1862
52e3daed 1863 mon = xcalloc(1, sizeof(*mon));
1853d1ef 1864
1865 monitor_socketpair(pair);
1866
1867 mon->m_recvfd = pair[0];
1868 mon->m_sendfd = pair[1];
1869
1870 /* Used to share zlib space across processes */
0496cf34 1871 if (options.compression) {
1872 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1873 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1853d1ef 1874
0496cf34 1875 /* Compression needs to share state across borders */
1876 mm_init_compression(mon->m_zlib);
1877 }
1853d1ef 1878
1879 return mon;
1880}
1881
1882void
1883monitor_reinit(struct monitor *mon)
1884{
1885 int pair[2];
1886
1887 monitor_socketpair(pair);
1888
1889 mon->m_recvfd = pair[0];
1890 mon->m_sendfd = pair[1];
1891}
7364bd04 1892
1893#ifdef GSSAPI
1894int
ca75d7de 1895mm_answer_gss_setup_ctx(int sock, Buffer *m)
7364bd04 1896{
ca75d7de 1897 gss_OID_desc goid;
7364bd04 1898 OM_uint32 major;
1899 u_int len;
1900
ca75d7de 1901 goid.elements = buffer_get_string(m, &len);
1902 goid.length = len;
7364bd04 1903
ca75d7de 1904 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
7364bd04 1905
ca75d7de 1906 xfree(goid.elements);
7364bd04 1907
1908 buffer_clear(m);
1909 buffer_put_int(m, major);
1910
8442cc66 1911 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
7364bd04 1912
1913 /* Now we have a context, enable the step */
1914 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1915
1916 return (0);
1917}
1918
1919int
ca75d7de 1920mm_answer_gss_accept_ctx(int sock, Buffer *m)
7364bd04 1921{
1922 gss_buffer_desc in;
1923 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
8442cc66 1924 OM_uint32 major, minor;
7364bd04 1925 OM_uint32 flags = 0; /* GSI needs this */
f99e1ca4 1926 u_int len;
7364bd04 1927
f99e1ca4 1928 in.value = buffer_get_string(m, &len);
1929 in.length = len;
7364bd04 1930 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1931 xfree(in.value);
1932
1933 buffer_clear(m);
1934 buffer_put_int(m, major);
1935 buffer_put_string(m, out.value, out.length);
1936 buffer_put_int(m, flags);
ca75d7de 1937 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
7364bd04 1938
1939 gss_release_buffer(&minor, &out);
1940
8442cc66 1941 if (major == GSS_S_COMPLETE) {
7364bd04 1942 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1943 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
0789992b 1944 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
7364bd04 1945 }
1946 return (0);
1947}
1948
0789992b 1949int
ca75d7de 1950mm_answer_gss_checkmic(int sock, Buffer *m)
0789992b 1951{
1952 gss_buffer_desc gssbuf, mic;
1953 OM_uint32 ret;
1954 u_int len;
b6453d99 1955
0789992b 1956 gssbuf.value = buffer_get_string(m, &len);
1957 gssbuf.length = len;
1958 mic.value = buffer_get_string(m, &len);
1959 mic.length = len;
b6453d99 1960
0789992b 1961 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
b6453d99 1962
0789992b 1963 xfree(gssbuf.value);
1964 xfree(mic.value);
b6453d99 1965
0789992b 1966 buffer_clear(m);
1967 buffer_put_int(m, ret);
b6453d99 1968
ca75d7de 1969 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
b6453d99 1970
0789992b 1971 if (!GSS_ERROR(ret))
1972 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
b6453d99 1973
0789992b 1974 return (0);
1975}
1976
7364bd04 1977int
ca75d7de 1978mm_answer_gss_userok(int sock, Buffer *m)
7364bd04 1979{
1980 int authenticated;
1981
1982 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1983
1984 buffer_clear(m);
1985 buffer_put_int(m, authenticated);
1986
1987 debug3("%s: sending result %d", __func__, authenticated);
ca75d7de 1988 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
7364bd04 1989
8442cc66 1990 auth_method = "gssapi-with-mic";
7364bd04 1991
1992 /* Monitor loop will terminate if authenticated */
1993 return (authenticated);
1994}
1995#endif /* GSSAPI */
5adf6b9a 1996
1997#ifdef JPAKE
1998int
1999mm_answer_jpake_step1(int sock, Buffer *m)
2000{
2001 struct jpake_ctx *pctx;
2002 u_char *x3_proof, *x4_proof;
2003 u_int x3_proof_len, x4_proof_len;
2004
2005 if (!options.zero_knowledge_password_authentication)
2006 fatal("zero_knowledge_password_authentication disabled");
2007
2008 if (authctxt->jpake_ctx != NULL)
2009 fatal("%s: authctxt->jpake_ctx already set (%p)",
2010 __func__, authctxt->jpake_ctx);
2011 authctxt->jpake_ctx = pctx = jpake_new();
2012
2013 jpake_step1(pctx->grp,
2014 &pctx->server_id, &pctx->server_id_len,
2015 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2016 &x3_proof, &x3_proof_len,
2017 &x4_proof, &x4_proof_len);
2018
2019 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2020
2021 buffer_clear(m);
2022
2023 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2024 buffer_put_bignum2(m, pctx->g_x3);
2025 buffer_put_bignum2(m, pctx->g_x4);
2026 buffer_put_string(m, x3_proof, x3_proof_len);
2027 buffer_put_string(m, x4_proof, x4_proof_len);
2028
2029 debug3("%s: sending step1", __func__);
2030 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2031
2032 bzero(x3_proof, x3_proof_len);
2033 bzero(x4_proof, x4_proof_len);
2034 xfree(x3_proof);
2035 xfree(x4_proof);
2036
2037 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2038 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2039
2040 return 0;
2041}
2042
2043int
2044mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2045{
2046 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2047 char *hash_scheme, *salt;
2048
2049 if (pctx == NULL)
2050 fatal("%s: pctx == NULL", __func__);
2051
2052 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2053
2054 buffer_clear(m);
2055 /* pctx->s is sensitive, not returned to slave */
2056 buffer_put_cstring(m, hash_scheme);
2057 buffer_put_cstring(m, salt);
2058
2059 debug3("%s: sending pwdata", __func__);
2060 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2061
2062 bzero(hash_scheme, strlen(hash_scheme));
2063 bzero(salt, strlen(salt));
2064 xfree(hash_scheme);
2065 xfree(salt);
2066
2067 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2068
2069 return 0;
2070}
2071
2072int
2073mm_answer_jpake_step2(int sock, Buffer *m)
2074{
2075 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2076 u_char *x1_proof, *x2_proof, *x4_s_proof;
2077 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2078
2079 if (pctx == NULL)
2080 fatal("%s: pctx == NULL", __func__);
2081
2082 if ((pctx->g_x1 = BN_new()) == NULL ||
2083 (pctx->g_x2 = BN_new()) == NULL)
2084 fatal("%s: BN_new", __func__);
2085 buffer_get_bignum2(m, pctx->g_x1);
2086 buffer_get_bignum2(m, pctx->g_x2);
2087 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2088 x1_proof = buffer_get_string(m, &x1_proof_len);
2089 x2_proof = buffer_get_string(m, &x2_proof_len);
2090
2091 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2092 pctx->g_x1, pctx->g_x2, pctx->x4,
2093 pctx->client_id, pctx->client_id_len,
2094 pctx->server_id, pctx->server_id_len,
2095 x1_proof, x1_proof_len,
2096 x2_proof, x2_proof_len,
2097 &pctx->b,
2098 &x4_s_proof, &x4_s_proof_len);
2099
2100 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2101
2102 bzero(x1_proof, x1_proof_len);
2103 bzero(x2_proof, x2_proof_len);
2104 xfree(x1_proof);
2105 xfree(x2_proof);
2106
2107 buffer_clear(m);
2108
2109 buffer_put_bignum2(m, pctx->b);
2110 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2111
2112 debug3("%s: sending step2", __func__);
2113 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2114
2115 bzero(x4_s_proof, x4_s_proof_len);
2116 xfree(x4_s_proof);
2117
2118 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2119
2120 return 0;
2121}
2122
2123int
2124mm_answer_jpake_key_confirm(int sock, Buffer *m)
2125{
2126 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2127 u_char *x2_s_proof;
2128 u_int x2_s_proof_len;
2129
2130 if (pctx == NULL)
2131 fatal("%s: pctx == NULL", __func__);
2132
2133 if ((pctx->a = BN_new()) == NULL)
2134 fatal("%s: BN_new", __func__);
2135 buffer_get_bignum2(m, pctx->a);
2136 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2137
2138 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2139 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2140 pctx->server_id, pctx->server_id_len,
2141 pctx->client_id, pctx->client_id_len,
2142 session_id2, session_id2_len,
2143 x2_s_proof, x2_s_proof_len,
2144 &pctx->k,
2145 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2146
2147 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2148
2149 bzero(x2_s_proof, x2_s_proof_len);
2150 buffer_clear(m);
2151
2152 /* pctx->k is sensitive, not sent */
2153 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2154
2155 debug3("%s: sending confirmation hash", __func__);
2156 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2157
2158 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2159
2160 return 0;
2161}
2162
2163int
2164mm_answer_jpake_check_confirm(int sock, Buffer *m)
2165{
2166 int authenticated = 0;
2167 u_char *peer_confirm_hash;
2168 u_int peer_confirm_hash_len;
2169 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2170
2171 if (pctx == NULL)
2172 fatal("%s: pctx == NULL", __func__);
2173
2174 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2175
2176 authenticated = jpake_check_confirm(pctx->k,
2177 pctx->client_id, pctx->client_id_len,
2178 session_id2, session_id2_len,
2179 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2180
2181 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2182
2183 bzero(peer_confirm_hash, peer_confirm_hash_len);
2184 xfree(peer_confirm_hash);
2185
2186 buffer_clear(m);
2187 buffer_put_int(m, authenticated);
2188
2189 debug3("%s: sending result %d", __func__, authenticated);
2190 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2191
2192 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2193
2194 auth_method = "jpake-01@openssh.com";
2195 return authenticated;
2196}
2197
2198#endif /* JPAKE */
This page took 0.738807 seconds and 5 git commands to generate.