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