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