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