blob: 502d54efa62bc7927053a13210a6e7e7cca8db63 [file] [log] [blame]
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001/*
2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2002 Markus Friedl <markus@openbsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Millera63128d2006-03-15 12:08:28 +110028RCSID("$OpenBSD: monitor.c,v 1.69 2006/03/07 09:07:40 djm Exp $");
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
31#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000032
Damien Miller6645e7a2006-03-15 14:42:54 +110033#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110034#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110035#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110036#include <signal.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000037
38#ifdef SKEY
39#include <skey.h>
40#endif
41
Damien Miller03e20032006-03-15 11:16:59 +110042#include <openssl/dh.h>
43
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000044#include "ssh.h"
45#include "auth.h"
46#include "kex.h"
47#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000048#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
49#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000050#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000051#define TARGET_OS_MAC 1
52#else
53#include "zlib.h"
54#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000055#include "packet.h"
56#include "auth-options.h"
57#include "sshpty.h"
58#include "channels.h"
59#include "session.h"
60#include "sshlogin.h"
61#include "canohost.h"
62#include "log.h"
63#include "servconf.h"
64#include "monitor.h"
65#include "monitor_mm.h"
66#include "monitor_wrap.h"
67#include "monitor_fdpass.h"
68#include "xmalloc.h"
69#include "misc.h"
70#include "buffer.h"
71#include "bufaux.h"
72#include "compat.h"
73#include "ssh2.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074
Darren Tucker0efd1552003-08-26 11:49:55 +100075#ifdef GSSAPI
76#include "ssh-gss.h"
77static Gssctxt *gsscontext = NULL;
78#endif
79
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000080/* Imports */
81extern ServerOptions options;
82extern u_int utmp_len;
83extern Newkeys *current_keys[];
84extern z_stream incoming_stream;
85extern z_stream outgoing_stream;
86extern u_char session_id[];
87extern Buffer input, output;
88extern Buffer auth_debug;
89extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +100090extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000091
92/* State exported from the child */
93
94struct {
95 z_stream incoming;
96 z_stream outgoing;
97 u_char *keyin;
98 u_int keyinlen;
99 u_char *keyout;
100 u_int keyoutlen;
101 u_char *ivin;
102 u_int ivinlen;
103 u_char *ivout;
104 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000105 u_char *ssh1key;
106 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107 int ssh1cipher;
108 int ssh1protoflags;
109 u_char *input;
110 u_int ilen;
111 u_char *output;
112 u_int olen;
113} child_state;
114
Damien Miller469954d2003-06-18 20:25:33 +1000115/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116
117int mm_answer_moduli(int, Buffer *);
118int mm_answer_sign(int, Buffer *);
119int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000120int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000121int mm_answer_authserv(int, Buffer *);
122int mm_answer_authpassword(int, Buffer *);
123int mm_answer_bsdauthquery(int, Buffer *);
124int mm_answer_bsdauthrespond(int, Buffer *);
125int mm_answer_skeyquery(int, Buffer *);
126int mm_answer_skeyrespond(int, Buffer *);
127int mm_answer_keyallowed(int, Buffer *);
128int mm_answer_keyverify(int, Buffer *);
129int mm_answer_pty(int, Buffer *);
130int mm_answer_pty_cleanup(int, Buffer *);
131int mm_answer_term(int, Buffer *);
132int mm_answer_rsa_keyallowed(int, Buffer *);
133int mm_answer_rsa_challenge(int, Buffer *);
134int mm_answer_rsa_response(int, Buffer *);
135int mm_answer_sesskey(int, Buffer *);
136int mm_answer_sessid(int, Buffer *);
137
Damien Miller79418552002-04-23 20:28:48 +1000138#ifdef USE_PAM
139int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000140int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000141int mm_answer_pam_init_ctx(int, Buffer *);
142int mm_answer_pam_query(int, Buffer *);
143int mm_answer_pam_respond(int, Buffer *);
144int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000145#endif
146
Darren Tucker0efd1552003-08-26 11:49:55 +1000147#ifdef GSSAPI
148int mm_answer_gss_setup_ctx(int, Buffer *);
149int mm_answer_gss_accept_ctx(int, Buffer *);
150int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100151int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000152#endif
Damien Miller25162f22002-09-12 09:47:29 +1000153
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100154#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100155int mm_answer_audit_event(int, Buffer *);
156int mm_answer_audit_command(int, Buffer *);
157#endif
158
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000159static Authctxt *authctxt;
160static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
161
162/* local state for key verify */
163static u_char *key_blob = NULL;
164static u_int key_bloblen = 0;
165static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000166static char *hostbased_cuser = NULL;
167static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000168static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000169static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000170static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000171static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000172
173struct mon_table {
174 enum monitor_reqtype type;
175 int flags;
176 int (*f)(int, Buffer *);
177};
178
179#define MON_ISAUTH 0x0004 /* Required for Authentication */
180#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
181#define MON_ONCE 0x0010 /* Disable after calling */
182
183#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
184
185#define MON_PERMIT 0x1000 /* Request is permitted */
186
187struct mon_table mon_dispatch_proto20[] = {
188 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
189 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
190 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
191 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000192 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000194#ifdef USE_PAM
195 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000196 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000197 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
198 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
199 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
200 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000201#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100202#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100203 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100204#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000205#ifdef BSD_AUTH
206 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100207 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208#endif
209#ifdef SKEY
210 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
211 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
212#endif
213 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
214 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000215#ifdef GSSAPI
216 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
217 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
218 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100219 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000220#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221 {0, 0, NULL}
222};
223
224struct mon_table mon_dispatch_postauth20[] = {
225 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
226 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
227 {MONITOR_REQ_PTY, 0, mm_answer_pty},
228 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
229 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100230#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100231 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
232 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
233#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000234 {0, 0, NULL}
235};
236
237struct mon_table mon_dispatch_proto15[] = {
238 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
239 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
240 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
241 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
242 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
243 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
244 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
245 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
246#ifdef BSD_AUTH
247 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100248 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000249#endif
250#ifdef SKEY
251 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
252 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
253#endif
Damien Millera33501b2002-05-08 12:24:42 +1000254#ifdef USE_PAM
255 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000256 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000257 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
258 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
259 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
260 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000261#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100262#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100263 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100264#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000265 {0, 0, NULL}
266};
267
268struct mon_table mon_dispatch_postauth15[] = {
269 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
270 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
271 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100272#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100273 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker3745e2b2005-03-06 22:31:35 +1100274 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100275#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000276 {0, 0, NULL}
277};
278
279struct mon_table *mon_dispatch;
280
281/* Specifies if a certain message is allowed at the moment */
282
283static void
284monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
285{
286 while (ent->f != NULL) {
287 if (ent->type == type) {
288 ent->flags &= ~MON_PERMIT;
289 ent->flags |= permit ? MON_PERMIT : 0;
290 return;
291 }
292 ent++;
293 }
294}
295
296static void
297monitor_permit_authentications(int permit)
298{
299 struct mon_table *ent = mon_dispatch;
300
301 while (ent->f != NULL) {
302 if (ent->flags & MON_AUTH) {
303 ent->flags &= ~MON_PERMIT;
304 ent->flags |= permit ? MON_PERMIT : 0;
305 }
306 ent++;
307 }
308}
309
Darren Tucker3e33cec2003-10-02 16:12:36 +1000310void
311monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000312{
313 struct mon_table *ent;
314 int authenticated = 0;
315
316 debug3("preauth child monitor started");
317
Darren Tucker3e33cec2003-10-02 16:12:36 +1000318 authctxt = _authctxt;
319 memset(authctxt, 0, sizeof(*authctxt));
320
Darren Tuckerde0de392005-03-31 23:52:04 +1000321 authctxt->loginmsg = &loginmsg;
322
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000323 if (compat20) {
324 mon_dispatch = mon_dispatch_proto20;
325
326 /* Permit requests for moduli and signatures */
327 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
328 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
329 } else {
330 mon_dispatch = mon_dispatch_proto15;
331
332 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
333 }
334
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000335 /* The first few requests do not require asynchronous access */
336 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000337 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000338 if (authenticated) {
339 if (!(ent->flags & MON_AUTHDECIDE))
340 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000341 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000342 if (authctxt->pw->pw_uid == 0 &&
343 !auth_root_allowed(auth_method))
344 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000345#ifdef USE_PAM
346 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100347 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000348 Buffer m;
349
350 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100351 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000352 MONITOR_REQ_PAM_ACCOUNT, &m);
353 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
354 buffer_free(&m);
355 }
356#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357 }
358
359 if (ent->flags & MON_AUTHDECIDE) {
360 auth_log(authctxt, authenticated, auth_method,
361 compat20 ? " ssh2" : "");
362 if (!authenticated)
363 authctxt->failures++;
364 }
365 }
366
367 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000368 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369
370 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000371 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000373 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000374}
375
Damien Millerbe64d432003-05-14 19:31:12 +1000376static void
377monitor_set_child_handler(pid_t pid)
378{
379 monitor_child_pid = pid;
380}
381
382static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000383monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000384{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000385 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000386}
387
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000389monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000390{
Damien Millerbe64d432003-05-14 19:31:12 +1000391 monitor_set_child_handler(pmonitor->m_pid);
392 signal(SIGHUP, &monitor_child_handler);
393 signal(SIGTERM, &monitor_child_handler);
394
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000395 if (compat20) {
396 mon_dispatch = mon_dispatch_postauth20;
397
398 /* Permit requests for moduli and signatures */
399 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
400 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
401 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000402 } else {
403 mon_dispatch = mon_dispatch_postauth15;
404 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
405 }
406 if (!no_pty_flag) {
407 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
408 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
409 }
410
411 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000412 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000413}
414
415void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000416monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000417{
Damien Miller2d6b8332002-06-21 15:59:49 +1000418 if (options.compression) {
419 /* The member allocation is not visible, so sync it */
420 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
421 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000422}
423
424int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000425monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000426 struct mon_table **pent)
427{
428 Buffer m;
429 int ret;
430 u_char type;
431
432 buffer_init(&m);
433
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000434 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000435 type = buffer_get_char(&m);
436
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000437 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000438
439 while (ent->f != NULL) {
440 if (ent->type == type)
441 break;
442 ent++;
443 }
444
445 if (ent->f != NULL) {
446 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000447 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000448 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000449 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000450 buffer_free(&m);
451
452 /* The child may use this request only once, disable it */
453 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000454 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000455 type);
456 ent->flags &= ~MON_PERMIT;
457 }
458
459 if (pent != NULL)
460 *pent = ent;
461
462 return ret;
463 }
464
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000465 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466
467 /* NOTREACHED */
468 return (-1);
469}
470
471/* allowed key state */
472static int
473monitor_allowed_key(u_char *blob, u_int bloblen)
474{
475 /* make sure key is allowed */
476 if (key_blob == NULL || key_bloblen != bloblen ||
477 memcmp(key_blob, blob, key_bloblen))
478 return (0);
479 return (1);
480}
481
482static void
483monitor_reset_key_state(void)
484{
485 /* reset state */
486 if (key_blob != NULL)
487 xfree(key_blob);
488 if (hostbased_cuser != NULL)
489 xfree(hostbased_cuser);
490 if (hostbased_chost != NULL)
491 xfree(hostbased_chost);
492 key_blob = NULL;
493 key_bloblen = 0;
494 key_blobtype = MM_NOKEY;
495 hostbased_cuser = NULL;
496 hostbased_chost = NULL;
497}
498
499int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000500mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000501{
502 DH *dh;
503 int min, want, max;
504
505 min = buffer_get_int(m);
506 want = buffer_get_int(m);
507 max = buffer_get_int(m);
508
509 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000510 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000511 /* We need to check here, too, in case the child got corrupted */
512 if (max < min || want < min || max < want)
513 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000514 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000515
516 buffer_clear(m);
517
518 dh = choose_dh(min, want, max);
519 if (dh == NULL) {
520 buffer_put_char(m, 0);
521 return (0);
522 } else {
523 /* Send first bignum */
524 buffer_put_char(m, 1);
525 buffer_put_bignum2(m, dh->p);
526 buffer_put_bignum2(m, dh->g);
527
528 DH_free(dh);
529 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000530 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000531 return (0);
532}
533
534int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000535mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000536{
537 Key *key;
538 u_char *p;
539 u_char *signature;
540 u_int siglen, datlen;
541 int keyid;
542
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000543 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000544
545 keyid = buffer_get_int(m);
546 p = buffer_get_string(m, &datlen);
547
Damien Millera63128d2006-03-15 12:08:28 +1100548 /*
549 * Supported KEX types will only return SHA1 (20 byte) or
550 * SHA256 (32 byte) hashes
551 */
552 if (datlen != 20 && datlen != 32)
Ben Lindstromd5502182002-06-27 00:12:57 +0000553 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000554
Ben Lindstromf67e0772002-06-06 20:58:19 +0000555 /* save session id, it will be passed on the first call */
556 if (session_id2_len == 0) {
557 session_id2_len = datlen;
558 session_id2 = xmalloc(session_id2_len);
559 memcpy(session_id2, p, session_id2_len);
560 }
561
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000562 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000563 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000564 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000565 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000566
Ben Lindstromd5502182002-06-27 00:12:57 +0000567 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000568
569 buffer_clear(m);
570 buffer_put_string(m, signature, siglen);
571
572 xfree(p);
573 xfree(signature);
574
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000575 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000576
577 /* Turn on permissions for getpwnam */
578 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
579
580 return (0);
581}
582
583/* Retrieves the password entry and also checks if the user is permitted */
584
585int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000586mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000588 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000589 struct passwd *pwent;
590 int allowed = 0;
591
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000592 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000593
594 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000595 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596
Darren Tuckerb09b6772004-06-22 15:06:46 +1000597 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598
Darren Tuckerb09b6772004-06-22 15:06:46 +1000599 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600
Darren Tuckerb09b6772004-06-22 15:06:46 +1000601 authctxt->user = xstrdup(username);
602 setproctitle("%s [priv]", pwent ? username : "unknown");
603 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604
605 buffer_clear(m);
606
607 if (pwent == NULL) {
608 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100609 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000610 goto out;
611 }
612
613 allowed = 1;
614 authctxt->pw = pwent;
615 authctxt->valid = 1;
616
617 buffer_put_char(m, 1);
618 buffer_put_string(m, pwent, sizeof(struct passwd));
619 buffer_put_cstring(m, pwent->pw_name);
620 buffer_put_cstring(m, "*");
621 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000622#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000623 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000624#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000625 buffer_put_cstring(m, pwent->pw_dir);
626 buffer_put_cstring(m, pwent->pw_shell);
627
628 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000629 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000630 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000631
632 /* For SSHv1 allow authentication now */
633 if (!compat20)
634 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000635 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000636 /* Allow service/style information on the auth context */
637 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000638 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
639 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000640
Damien Millera33501b2002-05-08 12:24:42 +1000641#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000642 if (options.use_pam)
643 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000644#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100645#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100646 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100647#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648
649 return (0);
650}
651
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000652int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000653{
654 char *banner;
655
656 buffer_clear(m);
657 banner = auth2_read_banner();
658 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000659 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000660
661 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000662 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000663
664 return (0);
665}
666
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000667int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000668mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669{
670 monitor_permit_authentications(1);
671
672 authctxt->service = buffer_get_string(m, NULL);
673 authctxt->style = buffer_get_string(m, NULL);
674 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000675 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000676
677 if (strlen(authctxt->style) == 0) {
678 xfree(authctxt->style);
679 authctxt->style = NULL;
680 }
681
682 return (0);
683}
684
685int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000686mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000687{
688 static int call_count;
689 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000690 int authenticated;
691 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692
693 passwd = buffer_get_string(m, &plen);
694 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000695 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000696 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000697 memset(passwd, 0, strlen(passwd));
698 xfree(passwd);
699
700 buffer_clear(m);
701 buffer_put_int(m, authenticated);
702
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000703 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000704 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000705
706 call_count++;
707 if (plen == 0 && call_count == 1)
708 auth_method = "none";
709 else
710 auth_method = "password";
711
712 /* Causes monitor loop to terminate if authenticated */
713 return (authenticated);
714}
715
716#ifdef BSD_AUTH
717int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000718mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000719{
720 char *name, *infotxt;
721 u_int numprompts;
722 u_int *echo_on;
723 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100724 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725
Damien Millerb7df3af2003-02-24 11:55:46 +1100726 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
727 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728
729 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100730 buffer_put_int(m, success);
731 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000732 buffer_put_cstring(m, prompts[0]);
733
Damien Millerb7df3af2003-02-24 11:55:46 +1100734 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000735 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736
Damien Millerb7df3af2003-02-24 11:55:46 +1100737 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000738 xfree(name);
739 xfree(infotxt);
740 xfree(prompts);
741 xfree(echo_on);
742 }
743
744 return (0);
745}
746
747int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000748mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000749{
750 char *response;
751 int authok;
752
753 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000754 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000755
756 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000757 authok = options.challenge_response_authentication &&
758 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000759 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000760 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000761 xfree(response);
762
763 buffer_clear(m);
764 buffer_put_int(m, authok);
765
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000766 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000767 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000768
769 auth_method = "bsdauth";
770
771 return (authok != 0);
772}
773#endif
774
775#ifdef SKEY
776int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000777mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000778{
779 struct skey skey;
780 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100781 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000782
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000783 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
784 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000785
786 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100787 buffer_put_int(m, success);
788 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000789 buffer_put_cstring(m, challenge);
790
Damien Millerb7df3af2003-02-24 11:55:46 +1100791 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000792 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000793
794 return (0);
795}
796
797int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000798mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000799{
800 char *response;
801 int authok;
802
803 response = buffer_get_string(m, NULL);
804
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000805 authok = (options.challenge_response_authentication &&
806 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000807 skey_haskey(authctxt->pw->pw_name) == 0 &&
808 skey_passcheck(authctxt->pw->pw_name, response) != -1);
809
810 xfree(response);
811
812 buffer_clear(m);
813 buffer_put_int(m, authok);
814
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000815 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000816 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000817
818 auth_method = "skey";
819
820 return (authok != 0);
821}
822#endif
823
Damien Miller79418552002-04-23 20:28:48 +1000824#ifdef USE_PAM
825int
Darren Tucker7eda5922004-06-22 13:22:50 +1000826mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000827{
Damien Miller4e448a32003-05-14 15:11:48 +1000828 if (!options.use_pam)
829 fatal("UsePAM not set, but ended up in %s anyway", __func__);
830
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100831 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000832
Damien Miller1f499fd2003-08-25 13:08:49 +1000833 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
834
Damien Miller79418552002-04-23 20:28:48 +1000835 return (0);
836}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000837
Damien Miller1f499fd2003-08-25 13:08:49 +1000838int
Darren Tucker7eda5922004-06-22 13:22:50 +1000839mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000840{
841 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100842
Damien Miller1f499fd2003-08-25 13:08:49 +1000843 if (!options.use_pam)
844 fatal("UsePAM not set, but ended up in %s anyway", __func__);
845
846 ret = do_pam_account();
847
848 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000849 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000850
Darren Tucker7eda5922004-06-22 13:22:50 +1000851 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000852
853 return (ret);
854}
855
Damien Miller4f9f42a2003-05-10 19:28:02 +1000856static void *sshpam_ctxt, *sshpam_authok;
857extern KbdintDevice sshpam_device;
858
859int
Darren Tucker7eda5922004-06-22 13:22:50 +1000860mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000861{
862
863 debug3("%s", __func__);
864 authctxt->user = buffer_get_string(m, NULL);
865 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
866 sshpam_authok = NULL;
867 buffer_clear(m);
868 if (sshpam_ctxt != NULL) {
869 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
870 buffer_put_int(m, 1);
871 } else {
872 buffer_put_int(m, 0);
873 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000874 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000875 return (0);
876}
877
878int
Darren Tucker7eda5922004-06-22 13:22:50 +1000879mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000880{
881 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000882 u_int i, num, *echo_on;
883 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000884
885 debug3("%s", __func__);
886 sshpam_authok = NULL;
887 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
888 if (ret == 0 && num == 0)
889 sshpam_authok = sshpam_ctxt;
890 if (num > 1 || name == NULL || info == NULL)
891 ret = -1;
892 buffer_clear(m);
893 buffer_put_int(m, ret);
894 buffer_put_cstring(m, name);
895 xfree(name);
896 buffer_put_cstring(m, info);
897 xfree(info);
898 buffer_put_int(m, num);
899 for (i = 0; i < num; ++i) {
900 buffer_put_cstring(m, prompts[i]);
901 xfree(prompts[i]);
902 buffer_put_int(m, echo_on[i]);
903 }
904 if (prompts != NULL)
905 xfree(prompts);
906 if (echo_on != NULL)
907 xfree(echo_on);
Darren Tucker7eda5922004-06-22 13:22:50 +1000908 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000909 return (0);
910}
911
912int
Darren Tucker7eda5922004-06-22 13:22:50 +1000913mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000914{
915 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000916 u_int i, num;
917 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000918
919 debug3("%s", __func__);
920 sshpam_authok = NULL;
921 num = buffer_get_int(m);
922 if (num > 0) {
923 resp = xmalloc(num * sizeof(char *));
924 for (i = 0; i < num; ++i)
925 resp[i] = buffer_get_string(m, NULL);
926 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
927 for (i = 0; i < num; ++i)
928 xfree(resp[i]);
929 xfree(resp);
930 } else {
931 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
932 }
933 buffer_clear(m);
934 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000935 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000936 auth_method = "keyboard-interactive/pam";
937 if (ret == 0)
938 sshpam_authok = sshpam_ctxt;
939 return (0);
940}
941
942int
Darren Tucker7eda5922004-06-22 13:22:50 +1000943mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000944{
945
946 debug3("%s", __func__);
947 (sshpam_device.free_ctx)(sshpam_ctxt);
948 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000949 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000950 return (sshpam_authok == sshpam_ctxt);
951}
Damien Miller79418552002-04-23 20:28:48 +1000952#endif
953
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954static void
955mm_append_debug(Buffer *m)
956{
957 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000958 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000959 buffer_append(m, buffer_ptr(&auth_debug),
960 buffer_len(&auth_debug));
961 buffer_clear(&auth_debug);
962 }
963}
964
965int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000966mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000967{
968 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000969 char *cuser, *chost;
970 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000971 u_int bloblen;
972 enum mm_keytype type = 0;
973 int allowed = 0;
974
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000975 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976
977 type = buffer_get_int(m);
978 cuser = buffer_get_string(m, NULL);
979 chost = buffer_get_string(m, NULL);
980 blob = buffer_get_string(m, &bloblen);
981
982 key = key_from_blob(blob, bloblen);
983
984 if ((compat20 && type == MM_RSAHOSTKEY) ||
985 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000986 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000988 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000989
Damien Miller3e3b5142003-11-17 21:13:40 +1100990 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +1100991 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000992 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000993 allowed = options.pubkey_authentication &&
994 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000995 break;
996 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000997 allowed = options.hostbased_authentication &&
998 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000999 cuser, chost, key);
1000 break;
1001 case MM_RSAHOSTKEY:
1002 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001003 allowed = options.rhosts_rsa_authentication &&
1004 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005 cuser, chost, key);
1006 break;
1007 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001008 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001009 break;
1010 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001011 }
Damien Miller00111382003-03-10 11:21:17 +11001012 if (key != NULL)
1013 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014
1015 /* clear temporarily storage (used by verify) */
1016 monitor_reset_key_state();
1017
1018 if (allowed) {
1019 /* Save temporarily for comparison in verify */
1020 key_blob = blob;
1021 key_bloblen = bloblen;
1022 key_blobtype = type;
1023 hostbased_cuser = cuser;
1024 hostbased_chost = chost;
1025 }
1026
1027 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001028 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001029
1030 buffer_clear(m);
1031 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001032 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001033
1034 mm_append_debug(m);
1035
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001036 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001037
1038 if (type == MM_RSAHOSTKEY)
1039 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1040
1041 return (0);
1042}
1043
1044static int
1045monitor_valid_userblob(u_char *data, u_int datalen)
1046{
1047 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001048 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001049 u_int len;
1050 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001051
1052 buffer_init(&b);
1053 buffer_append(&b, data, datalen);
1054
1055 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001056 p = buffer_ptr(&b);
1057 len = buffer_len(&b);
1058 if ((session_id2 == NULL) ||
1059 (len < session_id2_len) ||
1060 (memcmp(p, session_id2, session_id2_len) != 0))
1061 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001062 buffer_consume(&b, session_id2_len);
1063 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001064 p = buffer_get_string(&b, &len);
1065 if ((session_id2 == NULL) ||
1066 (len != session_id2_len) ||
1067 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001068 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001069 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001070 }
1071 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1072 fail++;
1073 p = buffer_get_string(&b, NULL);
1074 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001075 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001076 authctxt->user, p);
1077 fail++;
1078 }
1079 xfree(p);
1080 buffer_skip_string(&b);
1081 if (datafellows & SSH_BUG_PKAUTH) {
1082 if (!buffer_get_char(&b))
1083 fail++;
1084 } else {
1085 p = buffer_get_string(&b, NULL);
1086 if (strcmp("publickey", p) != 0)
1087 fail++;
1088 xfree(p);
1089 if (!buffer_get_char(&b))
1090 fail++;
1091 buffer_skip_string(&b);
1092 }
1093 buffer_skip_string(&b);
1094 if (buffer_len(&b) != 0)
1095 fail++;
1096 buffer_free(&b);
1097 return (fail == 0);
1098}
1099
1100static int
Damien Millera10f5612002-09-12 09:49:15 +10001101monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1102 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001103{
1104 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001105 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001106 u_int len;
1107 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001108
1109 buffer_init(&b);
1110 buffer_append(&b, data, datalen);
1111
Ben Lindstromf67e0772002-06-06 20:58:19 +00001112 p = buffer_get_string(&b, &len);
1113 if ((session_id2 == NULL) ||
1114 (len != session_id2_len) ||
1115 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001116 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001117 xfree(p);
1118
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001119 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1120 fail++;
1121 p = buffer_get_string(&b, NULL);
1122 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001123 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001124 authctxt->user, p);
1125 fail++;
1126 }
1127 xfree(p);
1128 buffer_skip_string(&b); /* service */
1129 p = buffer_get_string(&b, NULL);
1130 if (strcmp(p, "hostbased") != 0)
1131 fail++;
1132 xfree(p);
1133 buffer_skip_string(&b); /* pkalg */
1134 buffer_skip_string(&b); /* pkblob */
1135
1136 /* verify client host, strip trailing dot if necessary */
1137 p = buffer_get_string(&b, NULL);
1138 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1139 p[len - 1] = '\0';
1140 if (strcmp(p, chost) != 0)
1141 fail++;
1142 xfree(p);
1143
1144 /* verify client user */
1145 p = buffer_get_string(&b, NULL);
1146 if (strcmp(p, cuser) != 0)
1147 fail++;
1148 xfree(p);
1149
1150 if (buffer_len(&b) != 0)
1151 fail++;
1152 buffer_free(&b);
1153 return (fail == 0);
1154}
1155
1156int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001157mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001158{
1159 Key *key;
1160 u_char *signature, *data, *blob;
1161 u_int signaturelen, datalen, bloblen;
1162 int verified = 0;
1163 int valid_data = 0;
1164
1165 blob = buffer_get_string(m, &bloblen);
1166 signature = buffer_get_string(m, &signaturelen);
1167 data = buffer_get_string(m, &datalen);
1168
1169 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001170 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001171 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001172
1173 key = key_from_blob(blob, bloblen);
1174 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001175 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001176
1177 switch (key_blobtype) {
1178 case MM_USERKEY:
1179 valid_data = monitor_valid_userblob(data, datalen);
1180 break;
1181 case MM_HOSTKEY:
1182 valid_data = monitor_valid_hostbasedblob(data, datalen,
1183 hostbased_cuser, hostbased_chost);
1184 break;
1185 default:
1186 valid_data = 0;
1187 break;
1188 }
1189 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001190 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001191
1192 verified = key_verify(key, signature, signaturelen, data, datalen);
1193 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001194 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001195
1196 key_free(key);
1197 xfree(blob);
1198 xfree(signature);
1199 xfree(data);
1200
Ben Lindstrome1c09122002-06-23 00:38:24 +00001201 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1202
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001203 monitor_reset_key_state();
1204
1205 buffer_clear(m);
1206 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001207 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001208
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001209 return (verified);
1210}
1211
1212static void
1213mm_record_login(Session *s, struct passwd *pw)
1214{
1215 socklen_t fromlen;
1216 struct sockaddr_storage from;
1217
1218 /*
1219 * Get IP address of client. If the connection is not a socket, let
1220 * the address be 0.0.0.0.
1221 */
1222 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001223 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001224 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001225 if (getpeername(packet_get_connection_in(),
1226 (struct sockaddr *) & from, &fromlen) < 0) {
1227 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001228 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001229 }
1230 }
1231 /* Record that there was a login on that tty from the remote host. */
1232 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001233 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001234 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001235}
1236
1237static void
1238mm_session_close(Session *s)
1239{
Damien Miller04bd8b02003-05-25 14:38:33 +10001240 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001241 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001242 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001243 session_pty_cleanup2(s);
1244 }
1245 s->used = 0;
1246}
1247
1248int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001249mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001250{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001251 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001252 Session *s;
1253 int res, fd0;
1254
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001255 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001256
1257 buffer_clear(m);
1258 s = session_new();
1259 if (s == NULL)
1260 goto error;
1261 s->authctxt = authctxt;
1262 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001263 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001264 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1265 if (res == 0)
1266 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001267 pty_setowner(authctxt->pw, s->tty);
1268
1269 buffer_put_int(m, 1);
1270 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001271
1272 /* We need to trick ttyslot */
1273 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001274 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001275
1276 mm_record_login(s, authctxt->pw);
1277
1278 /* Now we can close the file descriptor again */
1279 close(0);
1280
Darren Tucker09991742004-07-17 17:05:14 +10001281 /* send messages generated by record_login */
1282 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1283 buffer_clear(&loginmsg);
1284
1285 mm_request_send(sock, MONITOR_ANS_PTY, m);
1286
1287 mm_send_fd(sock, s->ptyfd);
1288 mm_send_fd(sock, s->ttyfd);
1289
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001290 /* make sure nothing uses fd 0 */
1291 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001292 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001294 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295
1296 /* slave is not needed */
1297 close(s->ttyfd);
1298 s->ttyfd = s->ptyfd;
1299 /* no need to dup() because nobody closes ptyfd */
1300 s->ptymaster = s->ptyfd;
1301
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001302 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001303
1304 return (0);
1305
1306 error:
1307 if (s != NULL)
1308 mm_session_close(s);
1309 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001310 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001311 return (0);
1312}
1313
1314int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001315mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001316{
1317 Session *s;
1318 char *tty;
1319
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001320 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001321
1322 tty = buffer_get_string(m, NULL);
1323 if ((s = session_by_tty(tty)) != NULL)
1324 mm_session_close(s);
1325 buffer_clear(m);
1326 xfree(tty);
1327 return (0);
1328}
1329
1330int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001331mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001332{
1333 BIGNUM *p;
1334 int rsafail;
1335
1336 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001337 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001338
1339 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001340 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001341
1342 buffer_get_bignum2(m, p);
1343
1344 rsafail = ssh1_session_key(p);
1345
1346 buffer_clear(m);
1347 buffer_put_int(m, rsafail);
1348 buffer_put_bignum2(m, p);
1349
1350 BN_clear_free(p);
1351
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001352 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001353
1354 /* Turn on permissions for sessid passing */
1355 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1356
1357 return (0);
1358}
1359
1360int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001361mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001362{
1363 int i;
1364
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001365 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001366
1367 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001368 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001369 for (i = 0; i < 16; i++)
1370 session_id[i] = buffer_get_char(m);
1371
1372 /* Turn on permissions for getpwnam */
1373 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1374
1375 return (0);
1376}
1377
1378int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001379mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001380{
1381 BIGNUM *client_n;
1382 Key *key = NULL;
1383 u_char *blob = NULL;
1384 u_int blen = 0;
1385 int allowed = 0;
1386
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001387 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001388
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001389 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001390 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001391 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001392 buffer_get_bignum2(m, client_n);
1393 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1394 BN_clear_free(client_n);
1395 }
1396 buffer_clear(m);
1397 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001398 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001399
1400 /* clear temporarily storage (used by generate challenge) */
1401 monitor_reset_key_state();
1402
1403 if (allowed && key != NULL) {
1404 key->type = KEY_RSA; /* cheat for key_to_blob */
1405 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001406 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001407 buffer_put_string(m, blob, blen);
1408
1409 /* Save temporarily for comparison in verify */
1410 key_blob = blob;
1411 key_bloblen = blen;
1412 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001413 }
Damien Miller00111382003-03-10 11:21:17 +11001414 if (key != NULL)
1415 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001416
1417 mm_append_debug(m);
1418
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001419 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420
1421 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1422 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1423 return (0);
1424}
1425
1426int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001427mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428{
1429 Key *key = NULL;
1430 u_char *blob;
1431 u_int blen;
1432
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001433 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434
1435 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001436 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001437 blob = buffer_get_string(m, &blen);
1438 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001439 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001441 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001443 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001444
1445 if (ssh1_challenge)
1446 BN_clear_free(ssh1_challenge);
1447 ssh1_challenge = auth_rsa_generate_challenge(key);
1448
1449 buffer_clear(m);
1450 buffer_put_bignum2(m, ssh1_challenge);
1451
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001452 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001453 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001454
1455 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001456
1457 xfree(blob);
1458 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001459 return (0);
1460}
1461
1462int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001463mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001464{
1465 Key *key = NULL;
1466 u_char *blob, *response;
1467 u_int blen, len;
1468 int success;
1469
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001470 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001471
1472 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001473 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001474 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001475 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001476
1477 blob = buffer_get_string(m, &blen);
1478 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001479 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001480 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001481 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001482 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001483 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001484 response = buffer_get_string(m, &len);
1485 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001486 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001487 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1488
Damien Miller00111382003-03-10 11:21:17 +11001489 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001490 key_free(key);
1491 xfree(response);
1492
1493 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1494
1495 /* reset state */
1496 BN_clear_free(ssh1_challenge);
1497 ssh1_challenge = NULL;
1498 monitor_reset_key_state();
1499
1500 buffer_clear(m);
1501 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001502 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001503
1504 return (success);
1505}
1506
1507int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001508mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001509{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001510 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001511 int res, status;
1512
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001513 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514
1515 /* The child is terminating */
1516 session_destroy_all(&mm_session_close);
1517
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001518 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001519 if (errno != EINTR)
1520 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001521
1522 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1523
1524 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001525 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001526}
1527
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001528#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001529/* Report that an audit event occurred */
1530int
1531mm_answer_audit_event(int socket, Buffer *m)
1532{
1533 ssh_audit_event_t event;
1534
1535 debug3("%s entering", __func__);
1536
1537 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001538 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001539 case SSH_AUTH_FAIL_PUBKEY:
1540 case SSH_AUTH_FAIL_HOSTBASED:
1541 case SSH_AUTH_FAIL_GSSAPI:
1542 case SSH_LOGIN_EXCEED_MAXTRIES:
1543 case SSH_LOGIN_ROOT_DENIED:
1544 case SSH_CONNECTION_CLOSE:
1545 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001546 audit_event(event);
1547 break;
1548 default:
1549 fatal("Audit event type %d not permitted", event);
1550 }
1551
1552 return (0);
1553}
1554
1555int
1556mm_answer_audit_command(int socket, Buffer *m)
1557{
1558 u_int len;
1559 char *cmd;
1560
1561 debug3("%s entering", __func__);
1562 cmd = buffer_get_string(m, &len);
1563 /* sanity check command, if so how? */
1564 audit_run_command(cmd);
1565 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001566 return (0);
1567}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001568#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001569
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001570void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001571monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001572{
1573 if (compat20) {
1574 set_newkeys(MODE_IN);
1575 set_newkeys(MODE_OUT);
1576 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001577 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001578 packet_set_encryption_key(child_state.ssh1key,
1579 child_state.ssh1keylen, child_state.ssh1cipher);
1580 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001581 }
1582
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001583 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001584 packet_set_keycontext(MODE_OUT, child_state.keyout);
1585 xfree(child_state.keyout);
1586 packet_set_keycontext(MODE_IN, child_state.keyin);
1587 xfree(child_state.keyin);
1588
1589 if (!compat20) {
1590 packet_set_iv(MODE_OUT, child_state.ivout);
1591 xfree(child_state.ivout);
1592 packet_set_iv(MODE_IN, child_state.ivin);
1593 xfree(child_state.ivin);
1594 }
1595
1596 memcpy(&incoming_stream, &child_state.incoming,
1597 sizeof(incoming_stream));
1598 memcpy(&outgoing_stream, &child_state.outgoing,
1599 sizeof(outgoing_stream));
1600
1601 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001602 if (options.compression)
1603 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001604
1605 /* Network I/O buffers */
1606 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1607 buffer_clear(&input);
1608 buffer_append(&input, child_state.input, child_state.ilen);
1609 memset(child_state.input, 0, child_state.ilen);
1610 xfree(child_state.input);
1611
1612 buffer_clear(&output);
1613 buffer_append(&output, child_state.output, child_state.olen);
1614 memset(child_state.output, 0, child_state.olen);
1615 xfree(child_state.output);
1616}
1617
1618static Kex *
1619mm_get_kex(Buffer *m)
1620{
1621 Kex *kex;
1622 void *blob;
1623 u_int bloblen;
1624
1625 kex = xmalloc(sizeof(*kex));
1626 memset(kex, 0, sizeof(*kex));
1627 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001628 if ((session_id2 == NULL) ||
1629 (kex->session_id_len != session_id2_len) ||
1630 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1631 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001632 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001633 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001634 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001635 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001636 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001637 kex->server = 1;
1638 kex->hostkey_type = buffer_get_int(m);
1639 kex->kex_type = buffer_get_int(m);
1640 blob = buffer_get_string(m, &bloblen);
1641 buffer_init(&kex->my);
1642 buffer_append(&kex->my, blob, bloblen);
1643 xfree(blob);
1644 blob = buffer_get_string(m, &bloblen);
1645 buffer_init(&kex->peer);
1646 buffer_append(&kex->peer, blob, bloblen);
1647 xfree(blob);
1648 kex->done = 1;
1649 kex->flags = buffer_get_int(m);
1650 kex->client_version_string = buffer_get_string(m, NULL);
1651 kex->server_version_string = buffer_get_string(m, NULL);
1652 kex->load_host_key=&get_hostkey_by_type;
1653 kex->host_key_index=&get_hostkey_index;
1654
1655 return (kex);
1656}
1657
1658/* This function requries careful sanity checking */
1659
1660void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001661mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001662{
1663 Buffer m;
1664 u_char *blob, *p;
1665 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001666 u_int32_t seqnr, packets;
1667 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001668
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001669 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001670
1671 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001672 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001673 if (!compat20) {
1674 child_state.ssh1protoflags = buffer_get_int(&m);
1675 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001676 child_state.ssh1key = buffer_get_string(&m,
1677 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001678 child_state.ivout = buffer_get_string(&m,
1679 &child_state.ivoutlen);
1680 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1681 goto skip;
1682 } else {
1683 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001684 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001685 }
1686
1687 blob = buffer_get_string(&m, &bloblen);
1688 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1689 xfree(blob);
1690
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001691 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001692 blob = buffer_get_string(&m, &bloblen);
1693 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1694 xfree(blob);
1695
1696 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001697 seqnr = buffer_get_int(&m);
1698 blocks = buffer_get_int64(&m);
1699 packets = buffer_get_int(&m);
1700 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1701 seqnr = buffer_get_int(&m);
1702 blocks = buffer_get_int64(&m);
1703 packets = buffer_get_int(&m);
1704 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001705
1706 skip:
1707 /* Get the key context */
1708 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1709 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1710
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001711 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001712 /* Get compression state */
1713 p = buffer_get_string(&m, &plen);
1714 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001715 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001716 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1717 xfree(p);
1718
1719 p = buffer_get_string(&m, &plen);
1720 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001721 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001722 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1723 xfree(p);
1724
1725 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001726 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001727 child_state.input = buffer_get_string(&m, &child_state.ilen);
1728 child_state.output = buffer_get_string(&m, &child_state.olen);
1729
1730 buffer_free(&m);
1731}
1732
1733
1734/* Allocation functions for zlib */
1735void *
1736mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1737{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001738 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001739 void *address;
1740
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001741 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001742 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1743
1744 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001745
1746 return (address);
1747}
1748
1749void
1750mm_zfree(struct mm_master *mm, void *address)
1751{
1752 mm_free(mm, address);
1753}
1754
1755void
1756mm_init_compression(struct mm_master *mm)
1757{
1758 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1759 outgoing_stream.zfree = (free_func)mm_zfree;
1760 outgoing_stream.opaque = mm;
1761
1762 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1763 incoming_stream.zfree = (free_func)mm_zfree;
1764 incoming_stream.opaque = mm;
1765}
1766
1767/* XXX */
1768
1769#define FD_CLOSEONEXEC(x) do { \
1770 if (fcntl(x, F_SETFD, 1) == -1) \
1771 fatal("fcntl(%d, F_SETFD)", x); \
1772} while (0)
1773
1774static void
1775monitor_socketpair(int *pair)
1776{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001777#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001778 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001779 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001780#else
1781 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001782 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001783#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001784 FD_CLOSEONEXEC(pair[0]);
1785 FD_CLOSEONEXEC(pair[1]);
1786}
1787
1788#define MM_MEMSIZE 65536
1789
1790struct monitor *
1791monitor_init(void)
1792{
1793 struct monitor *mon;
1794 int pair[2];
1795
1796 mon = xmalloc(sizeof(*mon));
1797
Darren Tuckera8be9e22004-02-06 16:40:27 +11001798 mon->m_pid = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001799 monitor_socketpair(pair);
1800
1801 mon->m_recvfd = pair[0];
1802 mon->m_sendfd = pair[1];
1803
1804 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001805 if (options.compression) {
1806 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1807 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001808
Damien Miller2d6b8332002-06-21 15:59:49 +10001809 /* Compression needs to share state across borders */
1810 mm_init_compression(mon->m_zlib);
1811 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001812
1813 return mon;
1814}
1815
1816void
1817monitor_reinit(struct monitor *mon)
1818{
1819 int pair[2];
1820
1821 monitor_socketpair(pair);
1822
1823 mon->m_recvfd = pair[0];
1824 mon->m_sendfd = pair[1];
1825}
Darren Tucker0efd1552003-08-26 11:49:55 +10001826
1827#ifdef GSSAPI
1828int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001829mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001830{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001831 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001832 OM_uint32 major;
1833 u_int len;
1834
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001835 goid.elements = buffer_get_string(m, &len);
1836 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001837
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001838 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001839
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001840 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001841
1842 buffer_clear(m);
1843 buffer_put_int(m, major);
1844
Damien Miller6fd6def2005-11-05 15:07:05 +11001845 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001846
1847 /* Now we have a context, enable the step */
1848 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1849
1850 return (0);
1851}
1852
1853int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001854mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001855{
1856 gss_buffer_desc in;
1857 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001858 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001859 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001860 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001861
Darren Tucker600ad8d2003-08-26 12:10:48 +10001862 in.value = buffer_get_string(m, &len);
1863 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001864 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1865 xfree(in.value);
1866
1867 buffer_clear(m);
1868 buffer_put_int(m, major);
1869 buffer_put_string(m, out.value, out.length);
1870 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001871 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001872
1873 gss_release_buffer(&minor, &out);
1874
Damien Miller6fd6def2005-11-05 15:07:05 +11001875 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001876 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1877 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001878 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001879 }
1880 return (0);
1881}
1882
1883int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001884mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001885{
1886 gss_buffer_desc gssbuf, mic;
1887 OM_uint32 ret;
1888 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001889
Damien Miller0425d402003-11-17 22:18:21 +11001890 gssbuf.value = buffer_get_string(m, &len);
1891 gssbuf.length = len;
1892 mic.value = buffer_get_string(m, &len);
1893 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001894
Damien Miller0425d402003-11-17 22:18:21 +11001895 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001896
Damien Miller0425d402003-11-17 22:18:21 +11001897 xfree(gssbuf.value);
1898 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001899
Damien Miller0425d402003-11-17 22:18:21 +11001900 buffer_clear(m);
1901 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001902
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001903 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001904
Damien Miller0425d402003-11-17 22:18:21 +11001905 if (!GSS_ERROR(ret))
1906 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001907
Damien Miller0425d402003-11-17 22:18:21 +11001908 return (0);
1909}
1910
1911int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001912mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001913{
1914 int authenticated;
1915
1916 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1917
1918 buffer_clear(m);
1919 buffer_put_int(m, authenticated);
1920
1921 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001922 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001923
Damien Miller6fd6def2005-11-05 15:07:05 +11001924 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001925
1926 /* Monitor loop will terminate if authenticated */
1927 return (authenticated);
1928}
1929#endif /* GSSAPI */