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