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