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