]> andersk Git - gssapi-openssh.git/blame - openssh/monitor.c
http://www.sxw.org.uk/computing/patches/openssh-4.6p1-gsskex-20070312.patch committed...
[gssapi-openssh.git] / openssh / monitor.c
CommitLineData
799ae497 1/* $OpenBSD: monitor.c,v 1.90 2007/02/19 10:45:58 dtucker 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 *);
d617aef3 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},
d617aef3 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[] = {
d617aef3 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);
d617aef3 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);
d617aef3 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);
799ae497 660 buffer_put_string(m, &options, sizeof(options));
661 if (options.banner != NULL)
662 buffer_put_cstring(m, options.banner);
700318f3 663
664 out:
f5799ae1 665 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
c9f39d2c 666 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
700318f3 667
668 /* For SSHv1 allow authentication now */
669 if (!compat20)
670 monitor_permit_authentications(1);
671 else {
672 /* Allow service/style information on the auth context */
673 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
674 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
675 }
676
677#ifdef USE_PAM
0fff78ff 678 if (options.use_pam)
679 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
700318f3 680#endif
681
682 return (0);
683}
684
c9f39d2c 685int mm_answer_auth2_read_banner(int sock, Buffer *m)
700318f3 686{
687 char *banner;
688
689 buffer_clear(m);
690 banner = auth2_read_banner();
691 buffer_put_cstring(m, banner != NULL ? banner : "");
c9f39d2c 692 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
700318f3 693
694 if (banner != NULL)
41b2f314 695 xfree(banner);
700318f3 696
697 return (0);
698}
699
700int
c9f39d2c 701mm_answer_authserv(int sock, Buffer *m)
700318f3 702{
703 monitor_permit_authentications(1);
704
705 authctxt->service = buffer_get_string(m, NULL);
706 authctxt->style = buffer_get_string(m, NULL);
707 debug3("%s: service=%s, style=%s",
f5799ae1 708 __func__, authctxt->service, authctxt->style);
700318f3 709
710 if (strlen(authctxt->style) == 0) {
711 xfree(authctxt->style);
712 authctxt->style = NULL;
713 }
714
715 return (0);
716}
717
718int
c9f39d2c 719mm_answer_authpassword(int sock, Buffer *m)
700318f3 720{
721 static int call_count;
722 char *passwd;
41b2f314 723 int authenticated;
724 u_int plen;
700318f3 725
726 passwd = buffer_get_string(m, &plen);
727 /* Only authenticate if the context is valid */
f5799ae1 728 authenticated = options.password_authentication &&
0fff78ff 729 auth_password(authctxt, passwd);
700318f3 730 memset(passwd, 0, strlen(passwd));
731 xfree(passwd);
732
733 buffer_clear(m);
734 buffer_put_int(m, authenticated);
735
f5799ae1 736 debug3("%s: sending result %d", __func__, authenticated);
c9f39d2c 737 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
700318f3 738
739 call_count++;
740 if (plen == 0 && call_count == 1)
741 auth_method = "none";
742 else
743 auth_method = "password";
744
745 /* Causes monitor loop to terminate if authenticated */
746 return (authenticated);
747}
748
749#ifdef BSD_AUTH
750int
c9f39d2c 751mm_answer_bsdauthquery(int sock, Buffer *m)
700318f3 752{
753 char *name, *infotxt;
754 u_int numprompts;
755 u_int *echo_on;
756 char **prompts;
6a9b3198 757 u_int success;
700318f3 758
6a9b3198 759 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
760 &prompts, &echo_on) < 0 ? 0 : 1;
700318f3 761
762 buffer_clear(m);
6a9b3198 763 buffer_put_int(m, success);
764 if (success)
700318f3 765 buffer_put_cstring(m, prompts[0]);
766
6a9b3198 767 debug3("%s: sending challenge success: %u", __func__, success);
c9f39d2c 768 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
700318f3 769
6a9b3198 770 if (success) {
700318f3 771 xfree(name);
772 xfree(infotxt);
773 xfree(prompts);
774 xfree(echo_on);
775 }
776
777 return (0);
778}
779
780int
c9f39d2c 781mm_answer_bsdauthrespond(int sock, Buffer *m)
700318f3 782{
783 char *response;
784 int authok;
785
786 if (authctxt->as == 0)
f5799ae1 787 fatal("%s: no bsd auth session", __func__);
700318f3 788
789 response = buffer_get_string(m, NULL);
f5799ae1 790 authok = options.challenge_response_authentication &&
791 auth_userresponse(authctxt->as, response, 0);
700318f3 792 authctxt->as = NULL;
f5799ae1 793 debug3("%s: <%s> = <%d>", __func__, response, authok);
700318f3 794 xfree(response);
795
796 buffer_clear(m);
797 buffer_put_int(m, authok);
798
f5799ae1 799 debug3("%s: sending authenticated: %d", __func__, authok);
c9f39d2c 800 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
700318f3 801
802 auth_method = "bsdauth";
803
804 return (authok != 0);
805}
806#endif
807
808#ifdef SKEY
809int
c9f39d2c 810mm_answer_skeyquery(int sock, Buffer *m)
700318f3 811{
812 struct skey skey;
813 char challenge[1024];
6a9b3198 814 u_int success;
700318f3 815
99be0775 816 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
817 sizeof(challenge)) < 0 ? 0 : 1;
700318f3 818
819 buffer_clear(m);
6a9b3198 820 buffer_put_int(m, success);
821 if (success)
700318f3 822 buffer_put_cstring(m, challenge);
823
6a9b3198 824 debug3("%s: sending challenge success: %u", __func__, success);
c9f39d2c 825 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
700318f3 826
827 return (0);
828}
829
830int
c9f39d2c 831mm_answer_skeyrespond(int sock, Buffer *m)
700318f3 832{
833 char *response;
834 int authok;
835
836 response = buffer_get_string(m, NULL);
837
f5799ae1 838 authok = (options.challenge_response_authentication &&
839 authctxt->valid &&
700318f3 840 skey_haskey(authctxt->pw->pw_name) == 0 &&
841 skey_passcheck(authctxt->pw->pw_name, response) != -1);
842
843 xfree(response);
844
845 buffer_clear(m);
846 buffer_put_int(m, authok);
847
f5799ae1 848 debug3("%s: sending authenticated: %d", __func__, authok);
c9f39d2c 849 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
700318f3 850
851 auth_method = "skey";
852
853 return (authok != 0);
854}
855#endif
856
857#ifdef USE_PAM
858int
c9f39d2c 859mm_answer_pam_start(int sock, Buffer *m)
700318f3 860{
0fff78ff 861 if (!options.use_pam)
862 fatal("UsePAM not set, but ended up in %s anyway", __func__);
863
99be0775 864 start_pam(authctxt);
700318f3 865
0fff78ff 866 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
867
700318f3 868 return (0);
869}
0fff78ff 870
871int
c9f39d2c 872mm_answer_pam_account(int sock, Buffer *m)
0fff78ff 873{
874 u_int ret;
cdd66111 875
0fff78ff 876 if (!options.use_pam)
877 fatal("UsePAM not set, but ended up in %s anyway", __func__);
878
879 ret = do_pam_account();
880
881 buffer_put_int(m, ret);
2c06c99b 882 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
0fff78ff 883
c9f39d2c 884 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
0fff78ff 885
886 return (ret);
887}
888
889static void *sshpam_ctxt, *sshpam_authok;
890extern KbdintDevice sshpam_device;
891
892int
c9f39d2c 893mm_answer_pam_init_ctx(int sock, Buffer *m)
0fff78ff 894{
895
896 debug3("%s", __func__);
897 authctxt->user = buffer_get_string(m, NULL);
898 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
899 sshpam_authok = NULL;
900 buffer_clear(m);
901 if (sshpam_ctxt != NULL) {
902 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
903 buffer_put_int(m, 1);
904 } else {
905 buffer_put_int(m, 0);
906 }
c9f39d2c 907 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
0fff78ff 908 return (0);
909}
910
911int
c9f39d2c 912mm_answer_pam_query(int sock, Buffer *m)
0fff78ff 913{
914 char *name, *info, **prompts;
665a873d 915 u_int i, num, *echo_on;
916 int ret;
0fff78ff 917
918 debug3("%s", __func__);
919 sshpam_authok = NULL;
920 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
921 if (ret == 0 && num == 0)
922 sshpam_authok = sshpam_ctxt;
923 if (num > 1 || name == NULL || info == NULL)
924 ret = -1;
925 buffer_clear(m);
926 buffer_put_int(m, ret);
927 buffer_put_cstring(m, name);
928 xfree(name);
929 buffer_put_cstring(m, info);
930 xfree(info);
931 buffer_put_int(m, num);
932 for (i = 0; i < num; ++i) {
933 buffer_put_cstring(m, prompts[i]);
934 xfree(prompts[i]);
935 buffer_put_int(m, echo_on[i]);
936 }
937 if (prompts != NULL)
938 xfree(prompts);
939 if (echo_on != NULL)
940 xfree(echo_on);
9108f8d9 941 auth_method = "keyboard-interactive/pam";
c9f39d2c 942 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
0fff78ff 943 return (0);
944}
945
946int
c9f39d2c 947mm_answer_pam_respond(int sock, Buffer *m)
0fff78ff 948{
949 char **resp;
665a873d 950 u_int i, num;
951 int ret;
0fff78ff 952
953 debug3("%s", __func__);
954 sshpam_authok = NULL;
955 num = buffer_get_int(m);
956 if (num > 0) {
9108f8d9 957 resp = xcalloc(num, sizeof(char *));
0fff78ff 958 for (i = 0; i < num; ++i)
959 resp[i] = buffer_get_string(m, NULL);
960 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
961 for (i = 0; i < num; ++i)
962 xfree(resp[i]);
963 xfree(resp);
964 } else {
965 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
966 }
967 buffer_clear(m);
968 buffer_put_int(m, ret);
c9f39d2c 969 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
0fff78ff 970 auth_method = "keyboard-interactive/pam";
971 if (ret == 0)
972 sshpam_authok = sshpam_ctxt;
973 return (0);
974}
975
976int
c9f39d2c 977mm_answer_pam_free_ctx(int sock, Buffer *m)
0fff78ff 978{
979
980 debug3("%s", __func__);
981 (sshpam_device.free_ctx)(sshpam_ctxt);
982 buffer_clear(m);
c9f39d2c 983 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
9108f8d9 984 auth_method = "keyboard-interactive/pam";
0fff78ff 985 return (sshpam_authok == sshpam_ctxt);
986}
700318f3 987#endif
988
989static void
990mm_append_debug(Buffer *m)
991{
992 if (auth_debug_init && buffer_len(&auth_debug)) {
f5799ae1 993 debug3("%s: Appending debug messages for child", __func__);
700318f3 994 buffer_append(m, buffer_ptr(&auth_debug),
995 buffer_len(&auth_debug));
996 buffer_clear(&auth_debug);
997 }
998}
999
1000int
c9f39d2c 1001mm_answer_keyallowed(int sock, Buffer *m)
700318f3 1002{
1003 Key *key;
41b2f314 1004 char *cuser, *chost;
1005 u_char *blob;
700318f3 1006 u_int bloblen;
1007 enum mm_keytype type = 0;
1008 int allowed = 0;
1009
f5799ae1 1010 debug3("%s entering", __func__);
700318f3 1011
1012 type = buffer_get_int(m);
1013 cuser = buffer_get_string(m, NULL);
1014 chost = buffer_get_string(m, NULL);
1015 blob = buffer_get_string(m, &bloblen);
1016
1017 key = key_from_blob(blob, bloblen);
1018
1019 if ((compat20 && type == MM_RSAHOSTKEY) ||
1020 (!compat20 && type != MM_RSAHOSTKEY))
f5799ae1 1021 fatal("%s: key type and protocol mismatch", __func__);
700318f3 1022
f5799ae1 1023 debug3("%s: key_from_blob: %p", __func__, key);
700318f3 1024
cdd66111 1025 if (key != NULL && authctxt->valid) {
dec6d9fe 1026 switch (type) {
700318f3 1027 case MM_USERKEY:
f5799ae1 1028 allowed = options.pubkey_authentication &&
1029 user_key_allowed(authctxt->pw, key);
9108f8d9 1030 auth_method = "publickey";
700318f3 1031 break;
1032 case MM_HOSTKEY:
f5799ae1 1033 allowed = options.hostbased_authentication &&
1034 hostbased_key_allowed(authctxt->pw,
700318f3 1035 cuser, chost, key);
9108f8d9 1036 auth_method = "hostbased";
700318f3 1037 break;
1038 case MM_RSAHOSTKEY:
1039 key->type = KEY_RSA1; /* XXX */
f5799ae1 1040 allowed = options.rhosts_rsa_authentication &&
1041 auth_rhosts_rsa_key_allowed(authctxt->pw,
700318f3 1042 cuser, chost, key);
9108f8d9 1043 auth_method = "rsa";
700318f3 1044 break;
1045 default:
f5799ae1 1046 fatal("%s: unknown key type %d", __func__, type);
700318f3 1047 break;
1048 }
700318f3 1049 }
6a9b3198 1050 if (key != NULL)
1051 key_free(key);
700318f3 1052
1053 /* clear temporarily storage (used by verify) */
1054 monitor_reset_key_state();
1055
1056 if (allowed) {
1057 /* Save temporarily for comparison in verify */
1058 key_blob = blob;
1059 key_bloblen = bloblen;
1060 key_blobtype = type;
1061 hostbased_cuser = cuser;
1062 hostbased_chost = chost;
9108f8d9 1063 } else {
1064 /* Log failed attempt */
1065 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1066 xfree(blob);
1067 xfree(cuser);
1068 xfree(chost);
700318f3 1069 }
1070
1071 debug3("%s: key %p is %s",
f5799ae1 1072 __func__, key, allowed ? "allowed" : "disallowed");
700318f3 1073
1074 buffer_clear(m);
1075 buffer_put_int(m, allowed);
6a9b3198 1076 buffer_put_int(m, forced_command != NULL);
700318f3 1077
1078 mm_append_debug(m);
1079
c9f39d2c 1080 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
700318f3 1081
1082 if (type == MM_RSAHOSTKEY)
1083 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1084
1085 return (0);
1086}
1087
1088static int
1089monitor_valid_userblob(u_char *data, u_int datalen)
1090{
1091 Buffer b;
41b2f314 1092 char *p;
700318f3 1093 u_int len;
1094 int fail = 0;
700318f3 1095
1096 buffer_init(&b);
1097 buffer_append(&b, data, datalen);
1098
1099 if (datafellows & SSH_OLD_SESSIONID) {
f5799ae1 1100 p = buffer_ptr(&b);
1101 len = buffer_len(&b);
1102 if ((session_id2 == NULL) ||
1103 (len < session_id2_len) ||
1104 (memcmp(p, session_id2, session_id2_len) != 0))
1105 fail++;
700318f3 1106 buffer_consume(&b, session_id2_len);
1107 } else {
f5799ae1 1108 p = buffer_get_string(&b, &len);
1109 if ((session_id2 == NULL) ||
1110 (len != session_id2_len) ||
1111 (memcmp(p, session_id2, session_id2_len) != 0))
700318f3 1112 fail++;
f5799ae1 1113 xfree(p);
700318f3 1114 }
1115 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1116 fail++;
1117 p = buffer_get_string(&b, NULL);
1118 if (strcmp(authctxt->user, p) != 0) {
0fff78ff 1119 logit("wrong user name passed to monitor: expected %s != %.100s",
700318f3 1120 authctxt->user, p);
1121 fail++;
1122 }
1123 xfree(p);
1124 buffer_skip_string(&b);
1125 if (datafellows & SSH_BUG_PKAUTH) {
1126 if (!buffer_get_char(&b))
1127 fail++;
1128 } else {
1129 p = buffer_get_string(&b, NULL);
1130 if (strcmp("publickey", p) != 0)
1131 fail++;
1132 xfree(p);
1133 if (!buffer_get_char(&b))
1134 fail++;
1135 buffer_skip_string(&b);
1136 }
1137 buffer_skip_string(&b);
1138 if (buffer_len(&b) != 0)
1139 fail++;
1140 buffer_free(&b);
1141 return (fail == 0);
1142}
1143
1144static int
41b2f314 1145monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1146 char *chost)
700318f3 1147{
1148 Buffer b;
41b2f314 1149 char *p;
700318f3 1150 u_int len;
1151 int fail = 0;
700318f3 1152
1153 buffer_init(&b);
1154 buffer_append(&b, data, datalen);
1155
f5799ae1 1156 p = buffer_get_string(&b, &len);
1157 if ((session_id2 == NULL) ||
1158 (len != session_id2_len) ||
1159 (memcmp(p, session_id2, session_id2_len) != 0))
700318f3 1160 fail++;
f5799ae1 1161 xfree(p);
1162
700318f3 1163 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1164 fail++;
1165 p = buffer_get_string(&b, NULL);
1166 if (strcmp(authctxt->user, p) != 0) {
0fff78ff 1167 logit("wrong user name passed to monitor: expected %s != %.100s",
700318f3 1168 authctxt->user, p);
1169 fail++;
1170 }
1171 xfree(p);
1172 buffer_skip_string(&b); /* service */
1173 p = buffer_get_string(&b, NULL);
1174 if (strcmp(p, "hostbased") != 0)
1175 fail++;
1176 xfree(p);
1177 buffer_skip_string(&b); /* pkalg */
1178 buffer_skip_string(&b); /* pkblob */
1179
1180 /* verify client host, strip trailing dot if necessary */
1181 p = buffer_get_string(&b, NULL);
1182 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1183 p[len - 1] = '\0';
1184 if (strcmp(p, chost) != 0)
1185 fail++;
1186 xfree(p);
1187
1188 /* verify client user */
1189 p = buffer_get_string(&b, NULL);
1190 if (strcmp(p, cuser) != 0)
1191 fail++;
1192 xfree(p);
1193
1194 if (buffer_len(&b) != 0)
1195 fail++;
1196 buffer_free(&b);
1197 return (fail == 0);
1198}
1199
1200int
c9f39d2c 1201mm_answer_keyverify(int sock, Buffer *m)
700318f3 1202{
1203 Key *key;
1204 u_char *signature, *data, *blob;
1205 u_int signaturelen, datalen, bloblen;
1206 int verified = 0;
1207 int valid_data = 0;
1208
1209 blob = buffer_get_string(m, &bloblen);
1210 signature = buffer_get_string(m, &signaturelen);
1211 data = buffer_get_string(m, &datalen);
1212
1213 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1214 !monitor_allowed_key(blob, bloblen))
f5799ae1 1215 fatal("%s: bad key, not previously allowed", __func__);
700318f3 1216
1217 key = key_from_blob(blob, bloblen);
1218 if (key == NULL)
f5799ae1 1219 fatal("%s: bad public key blob", __func__);
700318f3 1220
1221 switch (key_blobtype) {
1222 case MM_USERKEY:
1223 valid_data = monitor_valid_userblob(data, datalen);
1224 break;
1225 case MM_HOSTKEY:
1226 valid_data = monitor_valid_hostbasedblob(data, datalen,
1227 hostbased_cuser, hostbased_chost);
1228 break;
1229 default:
1230 valid_data = 0;
1231 break;
1232 }
1233 if (!valid_data)
f5799ae1 1234 fatal("%s: bad signature data blob", __func__);
700318f3 1235
1236 verified = key_verify(key, signature, signaturelen, data, datalen);
1237 debug3("%s: key %p signature %s",
ff7ec503 1238 __func__, key, (verified == 1) ? "verified" : "unverified");
700318f3 1239
1240 key_free(key);
1241 xfree(blob);
1242 xfree(signature);
1243 xfree(data);
1244
680cee3b 1245 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1246
700318f3 1247 monitor_reset_key_state();
1248
1249 buffer_clear(m);
1250 buffer_put_int(m, verified);
c9f39d2c 1251 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
700318f3 1252
ff7ec503 1253 return (verified == 1);
700318f3 1254}
1255
1256static void
1257mm_record_login(Session *s, struct passwd *pw)
1258{
1259 socklen_t fromlen;
1260 struct sockaddr_storage from;
1261
1262 /*
1263 * Get IP address of client. If the connection is not a socket, let
1264 * the address be 0.0.0.0.
1265 */
1266 memset(&from, 0, sizeof(from));
41b2f314 1267 fromlen = sizeof(from);
700318f3 1268 if (packet_connection_is_on_socket()) {
700318f3 1269 if (getpeername(packet_get_connection_in(),
9108f8d9 1270 (struct sockaddr *)&from, &fromlen) < 0) {
700318f3 1271 debug("getpeername: %.100s", strerror(errno));
cdd66111 1272 cleanup_exit(255);
700318f3 1273 }
1274 }
1275 /* Record that there was a login on that tty from the remote host. */
1276 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
0fff78ff 1277 get_remote_name_or_ip(utmp_len, options.use_dns),
41b2f314 1278 (struct sockaddr *)&from, fromlen);
700318f3 1279}
1280
1281static void
1282mm_session_close(Session *s)
1283{
0fff78ff 1284 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
700318f3 1285 if (s->ttyfd != -1) {
9108f8d9 1286 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
700318f3 1287 session_pty_cleanup2(s);
1288 }
1289 s->used = 0;
1290}
1291
1292int
c9f39d2c 1293mm_answer_pty(int sock, Buffer *m)
700318f3 1294{
1295 extern struct monitor *pmonitor;
1296 Session *s;
1297 int res, fd0;
1298
f5799ae1 1299 debug3("%s entering", __func__);
700318f3 1300
1301 buffer_clear(m);
1302 s = session_new();
1303 if (s == NULL)
1304 goto error;
1305 s->authctxt = authctxt;
1306 s->pw = authctxt->pw;
1307 s->pid = pmonitor->m_pid;
1308 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1309 if (res == 0)
1310 goto error;
700318f3 1311 pty_setowner(authctxt->pw, s->tty);
1312
1313 buffer_put_int(m, 1);
1314 buffer_put_cstring(m, s->tty);
700318f3 1315
1316 /* We need to trick ttyslot */
1317 if (dup2(s->ttyfd, 0) == -1)
f5799ae1 1318 fatal("%s: dup2", __func__);
700318f3 1319
1320 mm_record_login(s, authctxt->pw);
1321
1322 /* Now we can close the file descriptor again */
1323 close(0);
1324
c9f39d2c 1325 /* send messages generated by record_login */
1326 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1327 buffer_clear(&loginmsg);
1328
1329 mm_request_send(sock, MONITOR_ANS_PTY, m);
1330
1331 mm_send_fd(sock, s->ptyfd);
1332 mm_send_fd(sock, s->ttyfd);
1333
700318f3 1334 /* make sure nothing uses fd 0 */
1335 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
f5799ae1 1336 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
700318f3 1337 if (fd0 != 0)
f5799ae1 1338 error("%s: fd0 %d != 0", __func__, fd0);
700318f3 1339
1340 /* slave is not needed */
1341 close(s->ttyfd);
1342 s->ttyfd = s->ptyfd;
1343 /* no need to dup() because nobody closes ptyfd */
1344 s->ptymaster = s->ptyfd;
1345
9108f8d9 1346 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
700318f3 1347
1348 return (0);
1349
1350 error:
1351 if (s != NULL)
1352 mm_session_close(s);
1353 buffer_put_int(m, 0);
c9f39d2c 1354 mm_request_send(sock, MONITOR_ANS_PTY, m);
700318f3 1355 return (0);
1356}
1357
1358int
c9f39d2c 1359mm_answer_pty_cleanup(int sock, Buffer *m)
700318f3 1360{
1361 Session *s;
1362 char *tty;
1363
f5799ae1 1364 debug3("%s entering", __func__);
700318f3 1365
1366 tty = buffer_get_string(m, NULL);
1367 if ((s = session_by_tty(tty)) != NULL)
1368 mm_session_close(s);
1369 buffer_clear(m);
1370 xfree(tty);
1371 return (0);
1372}
1373
1374int
c9f39d2c 1375mm_answer_sesskey(int sock, Buffer *m)
700318f3 1376{
1377 BIGNUM *p;
1378 int rsafail;
1379
1380 /* Turn off permissions */
996d5e62 1381 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
700318f3 1382
1383 if ((p = BN_new()) == NULL)
f5799ae1 1384 fatal("%s: BN_new", __func__);
700318f3 1385
1386 buffer_get_bignum2(m, p);
1387
1388 rsafail = ssh1_session_key(p);
1389
1390 buffer_clear(m);
1391 buffer_put_int(m, rsafail);
1392 buffer_put_bignum2(m, p);
1393
1394 BN_clear_free(p);
1395
c9f39d2c 1396 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
700318f3 1397
1398 /* Turn on permissions for sessid passing */
1399 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1400
1401 return (0);
1402}
1403
1404int
c9f39d2c 1405mm_answer_sessid(int sock, Buffer *m)
700318f3 1406{
1407 int i;
1408
f5799ae1 1409 debug3("%s entering", __func__);
700318f3 1410
1411 if (buffer_len(m) != 16)
f5799ae1 1412 fatal("%s: bad ssh1 session id", __func__);
700318f3 1413 for (i = 0; i < 16; i++)
1414 session_id[i] = buffer_get_char(m);
1415
1416 /* Turn on permissions for getpwnam */
1417 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1418
1419 return (0);
1420}
1421
1422int
c9f39d2c 1423mm_answer_rsa_keyallowed(int sock, Buffer *m)
700318f3 1424{
1425 BIGNUM *client_n;
1426 Key *key = NULL;
1427 u_char *blob = NULL;
1428 u_int blen = 0;
1429 int allowed = 0;
1430
f5799ae1 1431 debug3("%s entering", __func__);
700318f3 1432
9108f8d9 1433 auth_method = "rsa";
f5799ae1 1434 if (options.rsa_authentication && authctxt->valid) {
700318f3 1435 if ((client_n = BN_new()) == NULL)
f5799ae1 1436 fatal("%s: BN_new", __func__);
700318f3 1437 buffer_get_bignum2(m, client_n);
1438 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1439 BN_clear_free(client_n);
1440 }
1441 buffer_clear(m);
1442 buffer_put_int(m, allowed);
6a9b3198 1443 buffer_put_int(m, forced_command != NULL);
700318f3 1444
1445 /* clear temporarily storage (used by generate challenge) */
1446 monitor_reset_key_state();
1447
1448 if (allowed && key != NULL) {
1449 key->type = KEY_RSA; /* cheat for key_to_blob */
1450 if (key_to_blob(key, &blob, &blen) == 0)
f5799ae1 1451 fatal("%s: key_to_blob failed", __func__);
700318f3 1452 buffer_put_string(m, blob, blen);
1453
1454 /* Save temporarily for comparison in verify */
1455 key_blob = blob;
1456 key_bloblen = blen;
1457 key_blobtype = MM_RSAUSERKEY;
700318f3 1458 }
6a9b3198 1459 if (key != NULL)
1460 key_free(key);
700318f3 1461
1462 mm_append_debug(m);
1463
c9f39d2c 1464 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
700318f3 1465
1466 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1467 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1468 return (0);
1469}
1470
1471int
c9f39d2c 1472mm_answer_rsa_challenge(int sock, Buffer *m)
700318f3 1473{
1474 Key *key = NULL;
1475 u_char *blob;
1476 u_int blen;
1477
f5799ae1 1478 debug3("%s entering", __func__);
700318f3 1479
1480 if (!authctxt->valid)
f5799ae1 1481 fatal("%s: authctxt not valid", __func__);
700318f3 1482 blob = buffer_get_string(m, &blen);
1483 if (!monitor_allowed_key(blob, blen))
f5799ae1 1484 fatal("%s: bad key, not previously allowed", __func__);
700318f3 1485 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
f5799ae1 1486 fatal("%s: key type mismatch", __func__);
700318f3 1487 if ((key = key_from_blob(blob, blen)) == NULL)
f5799ae1 1488 fatal("%s: received bad key", __func__);
700318f3 1489
1490 if (ssh1_challenge)
1491 BN_clear_free(ssh1_challenge);
1492 ssh1_challenge = auth_rsa_generate_challenge(key);
1493
1494 buffer_clear(m);
1495 buffer_put_bignum2(m, ssh1_challenge);
1496
f5799ae1 1497 debug3("%s sending reply", __func__);
c9f39d2c 1498 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
700318f3 1499
1500 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
6a9b3198 1501
1502 xfree(blob);
1503 key_free(key);
700318f3 1504 return (0);
1505}
1506
1507int
c9f39d2c 1508mm_answer_rsa_response(int sock, Buffer *m)
700318f3 1509{
1510 Key *key = NULL;
1511 u_char *blob, *response;
1512 u_int blen, len;
1513 int success;
1514
f5799ae1 1515 debug3("%s entering", __func__);
700318f3 1516
1517 if (!authctxt->valid)
f5799ae1 1518 fatal("%s: authctxt not valid", __func__);
700318f3 1519 if (ssh1_challenge == NULL)
f5799ae1 1520 fatal("%s: no ssh1_challenge", __func__);
700318f3 1521
1522 blob = buffer_get_string(m, &blen);
1523 if (!monitor_allowed_key(blob, blen))
f5799ae1 1524 fatal("%s: bad key, not previously allowed", __func__);
700318f3 1525 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
f5799ae1 1526 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
700318f3 1527 if ((key = key_from_blob(blob, blen)) == NULL)
f5799ae1 1528 fatal("%s: received bad key", __func__);
700318f3 1529 response = buffer_get_string(m, &len);
1530 if (len != 16)
f5799ae1 1531 fatal("%s: received bad response to challenge", __func__);
700318f3 1532 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1533
6a9b3198 1534 xfree(blob);
700318f3 1535 key_free(key);
1536 xfree(response);
1537
1538 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1539
1540 /* reset state */
1541 BN_clear_free(ssh1_challenge);
1542 ssh1_challenge = NULL;
1543 monitor_reset_key_state();
1544
1545 buffer_clear(m);
1546 buffer_put_int(m, success);
c9f39d2c 1547 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
700318f3 1548
1549 return (success);
1550}
1551
1552int
c9f39d2c 1553mm_answer_term(int sock, Buffer *req)
700318f3 1554{
1555 extern struct monitor *pmonitor;
1556 int res, status;
1557
f5799ae1 1558 debug3("%s: tearing down sessions", __func__);
700318f3 1559
1560 /* The child is terminating */
1561 session_destroy_all(&mm_session_close);
1562
1563 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1564 if (errno != EINTR)
1565 exit(1);
1566
1567 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1568
1569 /* Terminate process */
c9f39d2c 1570 exit(res);
700318f3 1571}
1572
996d5e62 1573#ifdef SSH_AUDIT_EVENTS
1574/* Report that an audit event occurred */
1575int
1576mm_answer_audit_event(int socket, Buffer *m)
1577{
1578 ssh_audit_event_t event;
1579
1580 debug3("%s entering", __func__);
1581
1582 event = buffer_get_int(m);
996d5e62 1583 switch(event) {
1584 case SSH_AUTH_FAIL_PUBKEY:
1585 case SSH_AUTH_FAIL_HOSTBASED:
1586 case SSH_AUTH_FAIL_GSSAPI:
1587 case SSH_LOGIN_EXCEED_MAXTRIES:
1588 case SSH_LOGIN_ROOT_DENIED:
1589 case SSH_CONNECTION_CLOSE:
1590 case SSH_INVALID_USER:
1591 audit_event(event);
1592 break;
1593 default:
1594 fatal("Audit event type %d not permitted", event);
1595 }
1596
1597 return (0);
1598}
1599
1600int
1601mm_answer_audit_command(int socket, Buffer *m)
1602{
1603 u_int len;
1604 char *cmd;
1605
1606 debug3("%s entering", __func__);
1607 cmd = buffer_get_string(m, &len);
1608 /* sanity check command, if so how? */
1609 audit_run_command(cmd);
1610 xfree(cmd);
996d5e62 1611 return (0);
1612}
1613#endif /* SSH_AUDIT_EVENTS */
1614
700318f3 1615void
1616monitor_apply_keystate(struct monitor *pmonitor)
1617{
1618 if (compat20) {
1619 set_newkeys(MODE_IN);
1620 set_newkeys(MODE_OUT);
1621 } else {
700318f3 1622 packet_set_protocol_flags(child_state.ssh1protoflags);
f5799ae1 1623 packet_set_encryption_key(child_state.ssh1key,
1624 child_state.ssh1keylen, child_state.ssh1cipher);
1625 xfree(child_state.ssh1key);
700318f3 1626 }
1627
f5799ae1 1628 /* for rc4 and other stateful ciphers */
700318f3 1629 packet_set_keycontext(MODE_OUT, child_state.keyout);
1630 xfree(child_state.keyout);
1631 packet_set_keycontext(MODE_IN, child_state.keyin);
1632 xfree(child_state.keyin);
1633
1634 if (!compat20) {
1635 packet_set_iv(MODE_OUT, child_state.ivout);
1636 xfree(child_state.ivout);
1637 packet_set_iv(MODE_IN, child_state.ivin);
1638 xfree(child_state.ivin);
1639 }
1640
1641 memcpy(&incoming_stream, &child_state.incoming,
1642 sizeof(incoming_stream));
1643 memcpy(&outgoing_stream, &child_state.outgoing,
1644 sizeof(outgoing_stream));
1645
1646 /* Update with new address */
f5799ae1 1647 if (options.compression)
1648 mm_init_compression(pmonitor->m_zlib);
700318f3 1649
1650 /* Network I/O buffers */
1651 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1652 buffer_clear(&input);
1653 buffer_append(&input, child_state.input, child_state.ilen);
1654 memset(child_state.input, 0, child_state.ilen);
1655 xfree(child_state.input);
1656
1657 buffer_clear(&output);
1658 buffer_append(&output, child_state.output, child_state.olen);
1659 memset(child_state.output, 0, child_state.olen);
1660 xfree(child_state.output);
1661}
1662
1663static Kex *
1664mm_get_kex(Buffer *m)
1665{
1666 Kex *kex;
1667 void *blob;
1668 u_int bloblen;
1669
9108f8d9 1670 kex = xcalloc(1, sizeof(*kex));
700318f3 1671 kex->session_id = buffer_get_string(m, &kex->session_id_len);
f5799ae1 1672 if ((session_id2 == NULL) ||
1673 (kex->session_id_len != session_id2_len) ||
1674 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1675 fatal("mm_get_get: internal error: bad session id");
700318f3 1676 kex->we_need = buffer_get_int(m);
6a9b3198 1677 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
c9f39d2c 1678 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
6a9b3198 1679 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
9108f8d9 1680 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
d617aef3 1681#ifdef GSSAPI
1682 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
1683 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
1684 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
1685#endif
700318f3 1686 kex->server = 1;
1687 kex->hostkey_type = buffer_get_int(m);
1688 kex->kex_type = buffer_get_int(m);
1689 blob = buffer_get_string(m, &bloblen);
1690 buffer_init(&kex->my);
1691 buffer_append(&kex->my, blob, bloblen);
1692 xfree(blob);
1693 blob = buffer_get_string(m, &bloblen);
1694 buffer_init(&kex->peer);
1695 buffer_append(&kex->peer, blob, bloblen);
1696 xfree(blob);
1697 kex->done = 1;
1698 kex->flags = buffer_get_int(m);
1699 kex->client_version_string = buffer_get_string(m, NULL);
1700 kex->server_version_string = buffer_get_string(m, NULL);
1701 kex->load_host_key=&get_hostkey_by_type;
1702 kex->host_key_index=&get_hostkey_index;
1703
1704 return (kex);
1705}
1706
1707/* This function requries careful sanity checking */
1708
1709void
1710mm_get_keystate(struct monitor *pmonitor)
1711{
1712 Buffer m;
1713 u_char *blob, *p;
1714 u_int bloblen, plen;
0fff78ff 1715 u_int32_t seqnr, packets;
1716 u_int64_t blocks;
700318f3 1717
f5799ae1 1718 debug3("%s: Waiting for new keys", __func__);
700318f3 1719
1720 buffer_init(&m);
1721 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1722 if (!compat20) {
1723 child_state.ssh1protoflags = buffer_get_int(&m);
1724 child_state.ssh1cipher = buffer_get_int(&m);
f5799ae1 1725 child_state.ssh1key = buffer_get_string(&m,
1726 &child_state.ssh1keylen);
700318f3 1727 child_state.ivout = buffer_get_string(&m,
1728 &child_state.ivoutlen);
1729 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1730 goto skip;
1731 } else {
1732 /* Get the Kex for rekeying */
1733 *pmonitor->m_pkex = mm_get_kex(&m);
1734 }
1735
1736 blob = buffer_get_string(&m, &bloblen);
1737 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1738 xfree(blob);
1739
f5799ae1 1740 debug3("%s: Waiting for second key", __func__);
700318f3 1741 blob = buffer_get_string(&m, &bloblen);
1742 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1743 xfree(blob);
1744
1745 /* Now get sequence numbers for the packets */
0fff78ff 1746 seqnr = buffer_get_int(&m);
1747 blocks = buffer_get_int64(&m);
1748 packets = buffer_get_int(&m);
1749 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1750 seqnr = buffer_get_int(&m);
1751 blocks = buffer_get_int64(&m);
1752 packets = buffer_get_int(&m);
1753 packet_set_state(MODE_IN, seqnr, blocks, packets);
700318f3 1754
1755 skip:
1756 /* Get the key context */
1757 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1758 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1759
f5799ae1 1760 debug3("%s: Getting compression state", __func__);
700318f3 1761 /* Get compression state */
1762 p = buffer_get_string(&m, &plen);
1763 if (plen != sizeof(child_state.outgoing))
f5799ae1 1764 fatal("%s: bad request size", __func__);
700318f3 1765 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1766 xfree(p);
1767
1768 p = buffer_get_string(&m, &plen);
1769 if (plen != sizeof(child_state.incoming))
f5799ae1 1770 fatal("%s: bad request size", __func__);
700318f3 1771 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1772 xfree(p);
1773
1774 /* Network I/O buffers */
f5799ae1 1775 debug3("%s: Getting Network I/O buffers", __func__);
700318f3 1776 child_state.input = buffer_get_string(&m, &child_state.ilen);
1777 child_state.output = buffer_get_string(&m, &child_state.olen);
1778
1779 buffer_free(&m);
1780}
1781
1782
1783/* Allocation functions for zlib */
1784void *
1785mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1786{
6a9b3198 1787 size_t len = (size_t) size * ncount;
700318f3 1788 void *address;
1789
41b2f314 1790 if (len == 0 || ncount > SIZE_T_MAX / size)
680cee3b 1791 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1792
1793 address = mm_malloc(mm, len);
700318f3 1794
1795 return (address);
1796}
1797
1798void
1799mm_zfree(struct mm_master *mm, void *address)
1800{
1801 mm_free(mm, address);
1802}
1803
1804void
1805mm_init_compression(struct mm_master *mm)
1806{
1807 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1808 outgoing_stream.zfree = (free_func)mm_zfree;
1809 outgoing_stream.opaque = mm;
1810
1811 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1812 incoming_stream.zfree = (free_func)mm_zfree;
1813 incoming_stream.opaque = mm;
1814}
1815
1816/* XXX */
1817
1818#define FD_CLOSEONEXEC(x) do { \
1819 if (fcntl(x, F_SETFD, 1) == -1) \
1820 fatal("fcntl(%d, F_SETFD)", x); \
1821} while (0)
1822
1823static void
1824monitor_socketpair(int *pair)
1825{
1826#ifdef HAVE_SOCKETPAIR
1827 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
f5799ae1 1828 fatal("%s: socketpair", __func__);
700318f3 1829#else
1830 fatal("%s: UsePrivilegeSeparation=yes not supported",
f5799ae1 1831 __func__);
700318f3 1832#endif
1833 FD_CLOSEONEXEC(pair[0]);
1834 FD_CLOSEONEXEC(pair[1]);
1835}
1836
1837#define MM_MEMSIZE 65536
1838
1839struct monitor *
1840monitor_init(void)
1841{
1842 struct monitor *mon;
1843 int pair[2];
1844
9108f8d9 1845 mon = xcalloc(1, sizeof(*mon));
700318f3 1846
1847 monitor_socketpair(pair);
1848
1849 mon->m_recvfd = pair[0];
1850 mon->m_sendfd = pair[1];
1851
1852 /* Used to share zlib space across processes */
f5799ae1 1853 if (options.compression) {
1854 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1855 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
700318f3 1856
f5799ae1 1857 /* Compression needs to share state across borders */
1858 mm_init_compression(mon->m_zlib);
1859 }
700318f3 1860
1861 return mon;
1862}
1863
1864void
1865monitor_reinit(struct monitor *mon)
1866{
1867 int pair[2];
1868
1869 monitor_socketpair(pair);
1870
1871 mon->m_recvfd = pair[0];
1872 mon->m_sendfd = pair[1];
1873}
0fff78ff 1874
1875#ifdef GSSAPI
1876int
c9f39d2c 1877mm_answer_gss_setup_ctx(int sock, Buffer *m)
0fff78ff 1878{
c9f39d2c 1879 gss_OID_desc goid;
0fff78ff 1880 OM_uint32 major;
1881 u_int len;
1882
c9f39d2c 1883 goid.elements = buffer_get_string(m, &len);
1884 goid.length = len;
0fff78ff 1885
c9f39d2c 1886 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
0fff78ff 1887
c9f39d2c 1888 xfree(goid.elements);
0fff78ff 1889
1890 buffer_clear(m);
1891 buffer_put_int(m, major);
1892
2c06c99b 1893 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
0fff78ff 1894
1895 /* Now we have a context, enable the step */
1896 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1897
1898 return (0);
1899}
1900
1901int
c9f39d2c 1902mm_answer_gss_accept_ctx(int sock, Buffer *m)
0fff78ff 1903{
1904 gss_buffer_desc in;
1905 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
2c06c99b 1906 OM_uint32 major, minor;
0fff78ff 1907 OM_uint32 flags = 0; /* GSI needs this */
1908 u_int len;
1909
1910 in.value = buffer_get_string(m, &len);
1911 in.length = len;
1912 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1913 xfree(in.value);
1914
1915 buffer_clear(m);
1916 buffer_put_int(m, major);
1917 buffer_put_string(m, out.value, out.length);
1918 buffer_put_int(m, flags);
c9f39d2c 1919 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
0fff78ff 1920
1921 gss_release_buffer(&minor, &out);
1922
2c06c99b 1923 if (major == GSS_S_COMPLETE) {
0fff78ff 1924 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1925 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
cdd66111 1926 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
d617aef3 1927 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
0fff78ff 1928 }
1929 return (0);
1930}
1931
cdd66111 1932int
c9f39d2c 1933mm_answer_gss_checkmic(int sock, Buffer *m)
cdd66111 1934{
1935 gss_buffer_desc gssbuf, mic;
1936 OM_uint32 ret;
1937 u_int len;
1938
1939 gssbuf.value = buffer_get_string(m, &len);
1940 gssbuf.length = len;
1941 mic.value = buffer_get_string(m, &len);
1942 mic.length = len;
1943
1944 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1945
1946 xfree(gssbuf.value);
1947 xfree(mic.value);
1948
1949 buffer_clear(m);
1950 buffer_put_int(m, ret);
1951
c9f39d2c 1952 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
cdd66111 1953
1954 if (!GSS_ERROR(ret))
1955 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1956
1957 return (0);
1958}
1959
0fff78ff 1960int
c9f39d2c 1961mm_answer_gss_userok(int sock, Buffer *m)
0fff78ff 1962{
1963 int authenticated;
1964
1965 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1966
1967 buffer_clear(m);
1968 buffer_put_int(m, authenticated);
1969
1970 debug3("%s: sending result %d", __func__, authenticated);
c9f39d2c 1971 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
0fff78ff 1972
2c06c99b 1973 auth_method = "gssapi-with-mic";
0fff78ff 1974
1975 /* Monitor loop will terminate if authenticated */
1976 return (authenticated);
1977}
d617aef3 1978
1979int
1980mm_answer_gss_sign(int socket, Buffer *m)
1981{
1982 gss_buffer_desc data;
1983 gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
1984 OM_uint32 major, minor;
1985 u_int len;
1986
1987 data.value = buffer_get_string(m, &len);
1988 data.length = len;
1989 if (data.length != 20)
1990 fatal("%s: data length incorrect: %d", __func__, data.length);
1991
1992 /* Save the session ID on the first time around */
1993 if (session_id2_len == 0) {
1994 session_id2_len = data.length;
1995 session_id2 = xmalloc(session_id2_len);
1996 memcpy(session_id2, data.value, session_id2_len);
1997 }
1998 major = ssh_gssapi_sign(gsscontext, &data, &hash);
1999
2000 xfree(data.value);
2001
2002 buffer_clear(m);
2003 buffer_put_int(m, major);
2004 buffer_put_string(m, hash.value, hash.length);
2005
2006 mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
2007
2008 gss_release_buffer(&minor, &hash);
2009
2010 /* Turn on getpwnam permissions */
2011 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
2012
2013 return (0);
2014}
2015
0fff78ff 2016#endif /* GSSAPI */
This page took 2.927392 seconds and 5 git commands to generate.