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