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