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