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