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