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