blob: 8b304f99c08ea22a7bf67eec930949bded19feb9 [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 Miller9cf6d072006-03-15 11:29:24 +110028
29#include <sys/types.h>
30#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000031
Damien Miller6645e7a2006-03-15 14:42:54 +110032#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110033#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110034#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110035#include <signal.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000036
37#ifdef SKEY
38#include <skey.h>
39#endif
40
Damien Miller03e20032006-03-15 11:16:59 +110041#include <openssl/dh.h>
42
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000043#include "ssh.h"
44#include "auth.h"
45#include "kex.h"
46#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000047#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
48#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000049#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000050#define TARGET_OS_MAC 1
51#else
52#include "zlib.h"
53#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000054#include "packet.h"
55#include "auth-options.h"
56#include "sshpty.h"
57#include "channels.h"
58#include "session.h"
59#include "sshlogin.h"
60#include "canohost.h"
61#include "log.h"
62#include "servconf.h"
63#include "monitor.h"
64#include "monitor_mm.h"
65#include "monitor_wrap.h"
66#include "monitor_fdpass.h"
67#include "xmalloc.h"
68#include "misc.h"
69#include "buffer.h"
70#include "bufaux.h"
71#include "compat.h"
72#include "ssh2.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000073
Darren Tucker0efd1552003-08-26 11:49:55 +100074#ifdef GSSAPI
75#include "ssh-gss.h"
76static Gssctxt *gsscontext = NULL;
77#endif
78
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079/* Imports */
80extern ServerOptions options;
81extern u_int utmp_len;
82extern Newkeys *current_keys[];
83extern z_stream incoming_stream;
84extern z_stream outgoing_stream;
85extern u_char session_id[];
86extern Buffer input, output;
87extern Buffer auth_debug;
88extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +100089extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000090
91/* State exported from the child */
92
93struct {
94 z_stream incoming;
95 z_stream outgoing;
96 u_char *keyin;
97 u_int keyinlen;
98 u_char *keyout;
99 u_int keyoutlen;
100 u_char *ivin;
101 u_int ivinlen;
102 u_char *ivout;
103 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000104 u_char *ssh1key;
105 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000106 int ssh1cipher;
107 int ssh1protoflags;
108 u_char *input;
109 u_int ilen;
110 u_char *output;
111 u_int olen;
112} child_state;
113
Damien Miller469954d2003-06-18 20:25:33 +1000114/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115
116int mm_answer_moduli(int, Buffer *);
117int mm_answer_sign(int, Buffer *);
118int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000119int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000120int mm_answer_authserv(int, Buffer *);
121int mm_answer_authpassword(int, Buffer *);
122int mm_answer_bsdauthquery(int, Buffer *);
123int mm_answer_bsdauthrespond(int, Buffer *);
124int mm_answer_skeyquery(int, Buffer *);
125int mm_answer_skeyrespond(int, Buffer *);
126int mm_answer_keyallowed(int, Buffer *);
127int mm_answer_keyverify(int, Buffer *);
128int mm_answer_pty(int, Buffer *);
129int mm_answer_pty_cleanup(int, Buffer *);
130int mm_answer_term(int, Buffer *);
131int mm_answer_rsa_keyallowed(int, Buffer *);
132int mm_answer_rsa_challenge(int, Buffer *);
133int mm_answer_rsa_response(int, Buffer *);
134int mm_answer_sesskey(int, Buffer *);
135int mm_answer_sessid(int, Buffer *);
136
Damien Miller79418552002-04-23 20:28:48 +1000137#ifdef USE_PAM
138int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000139int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000140int mm_answer_pam_init_ctx(int, Buffer *);
141int mm_answer_pam_query(int, Buffer *);
142int mm_answer_pam_respond(int, Buffer *);
143int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000144#endif
145
Darren Tucker0efd1552003-08-26 11:49:55 +1000146#ifdef GSSAPI
147int mm_answer_gss_setup_ctx(int, Buffer *);
148int mm_answer_gss_accept_ctx(int, Buffer *);
149int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100150int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000151#endif
Damien Miller25162f22002-09-12 09:47:29 +1000152
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100153#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100154int mm_answer_audit_event(int, Buffer *);
155int mm_answer_audit_command(int, Buffer *);
156#endif
157
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000158static Authctxt *authctxt;
159static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
160
161/* local state for key verify */
162static u_char *key_blob = NULL;
163static u_int key_bloblen = 0;
164static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000165static char *hostbased_cuser = NULL;
166static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000168static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000169static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000170static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171
172struct mon_table {
173 enum monitor_reqtype type;
174 int flags;
175 int (*f)(int, Buffer *);
176};
177
178#define MON_ISAUTH 0x0004 /* Required for Authentication */
179#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
180#define MON_ONCE 0x0010 /* Disable after calling */
181
182#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
183
184#define MON_PERMIT 0x1000 /* Request is permitted */
185
186struct mon_table mon_dispatch_proto20[] = {
187 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
188 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
189 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
190 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000191 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000192 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000193#ifdef USE_PAM
194 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000195 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000196 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
197 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
198 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
199 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000200#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100201#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100202 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100203#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000204#ifdef BSD_AUTH
205 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100206 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207#endif
208#ifdef SKEY
209 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
210 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
211#endif
212 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
213 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000214#ifdef GSSAPI
215 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
216 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
217 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100218 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000219#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220 {0, 0, NULL}
221};
222
223struct mon_table mon_dispatch_postauth20[] = {
224 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
225 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
226 {MONITOR_REQ_PTY, 0, mm_answer_pty},
227 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
228 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100229#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100230 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
231 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
232#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000233 {0, 0, NULL}
234};
235
236struct mon_table mon_dispatch_proto15[] = {
237 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
238 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
239 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
240 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
241 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
242 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
243 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
244 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
245#ifdef BSD_AUTH
246 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100247 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000248#endif
249#ifdef SKEY
250 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
251 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
252#endif
Damien Millera33501b2002-05-08 12:24:42 +1000253#ifdef USE_PAM
254 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000255 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000256 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
257 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
258 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
259 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000260#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100261#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100262 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100263#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000264 {0, 0, NULL}
265};
266
267struct mon_table mon_dispatch_postauth15[] = {
268 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
269 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
270 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100271#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100272 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker3745e2b2005-03-06 22:31:35 +1100273 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100274#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000275 {0, 0, NULL}
276};
277
278struct mon_table *mon_dispatch;
279
280/* Specifies if a certain message is allowed at the moment */
281
282static void
283monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
284{
285 while (ent->f != NULL) {
286 if (ent->type == type) {
287 ent->flags &= ~MON_PERMIT;
288 ent->flags |= permit ? MON_PERMIT : 0;
289 return;
290 }
291 ent++;
292 }
293}
294
295static void
296monitor_permit_authentications(int permit)
297{
298 struct mon_table *ent = mon_dispatch;
299
300 while (ent->f != NULL) {
301 if (ent->flags & MON_AUTH) {
302 ent->flags &= ~MON_PERMIT;
303 ent->flags |= permit ? MON_PERMIT : 0;
304 }
305 ent++;
306 }
307}
308
Darren Tucker3e33cec2003-10-02 16:12:36 +1000309void
310monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000311{
312 struct mon_table *ent;
313 int authenticated = 0;
314
315 debug3("preauth child monitor started");
316
Darren Tucker3e33cec2003-10-02 16:12:36 +1000317 authctxt = _authctxt;
318 memset(authctxt, 0, sizeof(*authctxt));
319
Darren Tuckerde0de392005-03-31 23:52:04 +1000320 authctxt->loginmsg = &loginmsg;
321
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000322 if (compat20) {
323 mon_dispatch = mon_dispatch_proto20;
324
325 /* Permit requests for moduli and signatures */
326 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
327 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
328 } else {
329 mon_dispatch = mon_dispatch_proto15;
330
331 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
332 }
333
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000334 /* The first few requests do not require asynchronous access */
335 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000336 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000337 if (authenticated) {
338 if (!(ent->flags & MON_AUTHDECIDE))
339 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000340 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000341 if (authctxt->pw->pw_uid == 0 &&
342 !auth_root_allowed(auth_method))
343 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000344#ifdef USE_PAM
345 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100346 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000347 Buffer m;
348
349 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100350 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000351 MONITOR_REQ_PAM_ACCOUNT, &m);
352 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
353 buffer_free(&m);
354 }
355#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356 }
357
358 if (ent->flags & MON_AUTHDECIDE) {
359 auth_log(authctxt, authenticated, auth_method,
360 compat20 ? " ssh2" : "");
361 if (!authenticated)
362 authctxt->failures++;
363 }
364 }
365
366 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000367 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000368
369 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000370 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000371
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000372 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000373}
374
Damien Millerbe64d432003-05-14 19:31:12 +1000375static void
376monitor_set_child_handler(pid_t pid)
377{
378 monitor_child_pid = pid;
379}
380
381static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000382monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000383{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000384 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000385}
386
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000387void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000388monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000389{
Damien Millerbe64d432003-05-14 19:31:12 +1000390 monitor_set_child_handler(pmonitor->m_pid);
391 signal(SIGHUP, &monitor_child_handler);
392 signal(SIGTERM, &monitor_child_handler);
393
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000394 if (compat20) {
395 mon_dispatch = mon_dispatch_postauth20;
396
397 /* Permit requests for moduli and signatures */
398 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
399 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
400 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000401 } else {
402 mon_dispatch = mon_dispatch_postauth15;
403 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
404 }
405 if (!no_pty_flag) {
406 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
407 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
408 }
409
410 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000411 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000412}
413
414void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000415monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000416{
Damien Miller2d6b8332002-06-21 15:59:49 +1000417 if (options.compression) {
418 /* The member allocation is not visible, so sync it */
419 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
420 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000421}
422
423int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000424monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000425 struct mon_table **pent)
426{
427 Buffer m;
428 int ret;
429 u_char type;
430
431 buffer_init(&m);
432
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000433 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000434 type = buffer_get_char(&m);
435
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000436 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000437
438 while (ent->f != NULL) {
439 if (ent->type == type)
440 break;
441 ent++;
442 }
443
444 if (ent->f != NULL) {
445 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000446 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000447 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000448 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000449 buffer_free(&m);
450
451 /* The child may use this request only once, disable it */
452 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000453 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000454 type);
455 ent->flags &= ~MON_PERMIT;
456 }
457
458 if (pent != NULL)
459 *pent = ent;
460
461 return ret;
462 }
463
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000464 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000465
466 /* NOTREACHED */
467 return (-1);
468}
469
470/* allowed key state */
471static int
472monitor_allowed_key(u_char *blob, u_int bloblen)
473{
474 /* make sure key is allowed */
475 if (key_blob == NULL || key_bloblen != bloblen ||
476 memcmp(key_blob, blob, key_bloblen))
477 return (0);
478 return (1);
479}
480
481static void
482monitor_reset_key_state(void)
483{
484 /* reset state */
485 if (key_blob != NULL)
486 xfree(key_blob);
487 if (hostbased_cuser != NULL)
488 xfree(hostbased_cuser);
489 if (hostbased_chost != NULL)
490 xfree(hostbased_chost);
491 key_blob = NULL;
492 key_bloblen = 0;
493 key_blobtype = MM_NOKEY;
494 hostbased_cuser = NULL;
495 hostbased_chost = NULL;
496}
497
498int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000499mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000500{
501 DH *dh;
502 int min, want, max;
503
504 min = buffer_get_int(m);
505 want = buffer_get_int(m);
506 max = buffer_get_int(m);
507
508 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000509 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000510 /* We need to check here, too, in case the child got corrupted */
511 if (max < min || want < min || max < want)
512 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000513 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000514
515 buffer_clear(m);
516
517 dh = choose_dh(min, want, max);
518 if (dh == NULL) {
519 buffer_put_char(m, 0);
520 return (0);
521 } else {
522 /* Send first bignum */
523 buffer_put_char(m, 1);
524 buffer_put_bignum2(m, dh->p);
525 buffer_put_bignum2(m, dh->g);
526
527 DH_free(dh);
528 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000529 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000530 return (0);
531}
532
533int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000534mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000535{
536 Key *key;
537 u_char *p;
538 u_char *signature;
539 u_int siglen, datlen;
540 int keyid;
541
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000542 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000543
544 keyid = buffer_get_int(m);
545 p = buffer_get_string(m, &datlen);
546
Damien Millera63128d2006-03-15 12:08:28 +1100547 /*
Damien Millerc91e5562006-03-26 13:58:55 +1100548 * Supported KEX types will only return SHA1 (20 byte) or
Damien Millera63128d2006-03-15 12:08:28 +1100549 * SHA256 (32 byte) hashes
550 */
551 if (datlen != 20 && datlen != 32)
Ben Lindstromd5502182002-06-27 00:12:57 +0000552 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000553
Ben Lindstromf67e0772002-06-06 20:58:19 +0000554 /* save session id, it will be passed on the first call */
555 if (session_id2_len == 0) {
556 session_id2_len = datlen;
557 session_id2 = xmalloc(session_id2_len);
558 memcpy(session_id2, p, session_id2_len);
559 }
560
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000562 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000563 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000564 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000565
Ben Lindstromd5502182002-06-27 00:12:57 +0000566 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000567
568 buffer_clear(m);
569 buffer_put_string(m, signature, siglen);
570
571 xfree(p);
572 xfree(signature);
573
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000574 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575
576 /* Turn on permissions for getpwnam */
577 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
578
579 return (0);
580}
581
582/* Retrieves the password entry and also checks if the user is permitted */
583
584int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000585mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000587 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000588 struct passwd *pwent;
589 int allowed = 0;
590
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000591 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592
593 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000594 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000595
Darren Tuckerb09b6772004-06-22 15:06:46 +1000596 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597
Darren Tuckerb09b6772004-06-22 15:06:46 +1000598 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000599
Darren Tuckerb09b6772004-06-22 15:06:46 +1000600 authctxt->user = xstrdup(username);
601 setproctitle("%s [priv]", pwent ? username : "unknown");
602 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000603
604 buffer_clear(m);
605
606 if (pwent == NULL) {
607 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100608 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609 goto out;
610 }
611
612 allowed = 1;
613 authctxt->pw = pwent;
614 authctxt->valid = 1;
615
616 buffer_put_char(m, 1);
617 buffer_put_string(m, pwent, sizeof(struct passwd));
618 buffer_put_cstring(m, pwent->pw_name);
619 buffer_put_cstring(m, "*");
620 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000621#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000622 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000623#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000624 buffer_put_cstring(m, pwent->pw_dir);
625 buffer_put_cstring(m, pwent->pw_shell);
626
627 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000628 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000629 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000630
631 /* For SSHv1 allow authentication now */
632 if (!compat20)
633 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000634 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000635 /* Allow service/style information on the auth context */
636 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000637 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
638 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000639
Damien Millera33501b2002-05-08 12:24:42 +1000640#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000641 if (options.use_pam)
642 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000643#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100644#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100645 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100646#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000647
648 return (0);
649}
650
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000651int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000652{
653 char *banner;
654
655 buffer_clear(m);
656 banner = auth2_read_banner();
657 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000658 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000659
660 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000661 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000662
663 return (0);
664}
665
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000666int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000667mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000668{
669 monitor_permit_authentications(1);
670
671 authctxt->service = buffer_get_string(m, NULL);
672 authctxt->style = buffer_get_string(m, NULL);
673 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000674 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000675
676 if (strlen(authctxt->style) == 0) {
677 xfree(authctxt->style);
678 authctxt->style = NULL;
679 }
680
681 return (0);
682}
683
684int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000685mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000686{
687 static int call_count;
688 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000689 int authenticated;
690 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000691
692 passwd = buffer_get_string(m, &plen);
693 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000694 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000695 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696 memset(passwd, 0, strlen(passwd));
697 xfree(passwd);
698
699 buffer_clear(m);
700 buffer_put_int(m, authenticated);
701
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000702 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000703 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000704
705 call_count++;
706 if (plen == 0 && call_count == 1)
707 auth_method = "none";
708 else
709 auth_method = "password";
710
711 /* Causes monitor loop to terminate if authenticated */
712 return (authenticated);
713}
714
715#ifdef BSD_AUTH
716int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000717mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000718{
719 char *name, *infotxt;
720 u_int numprompts;
721 u_int *echo_on;
722 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100723 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000724
Damien Millerb7df3af2003-02-24 11:55:46 +1100725 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
726 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727
728 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100729 buffer_put_int(m, success);
730 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000731 buffer_put_cstring(m, prompts[0]);
732
Damien Millerb7df3af2003-02-24 11:55:46 +1100733 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000734 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000735
Damien Millerb7df3af2003-02-24 11:55:46 +1100736 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000737 xfree(name);
738 xfree(infotxt);
739 xfree(prompts);
740 xfree(echo_on);
741 }
742
743 return (0);
744}
745
746int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000747mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748{
749 char *response;
750 int authok;
751
752 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000753 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000754
755 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000756 authok = options.challenge_response_authentication &&
757 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000758 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000759 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000760 xfree(response);
761
762 buffer_clear(m);
763 buffer_put_int(m, authok);
764
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000765 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000766 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000767
768 auth_method = "bsdauth";
769
770 return (authok != 0);
771}
772#endif
773
774#ifdef SKEY
775int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000776mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000777{
778 struct skey skey;
779 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100780 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000781
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000782 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
783 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000784
785 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100786 buffer_put_int(m, success);
787 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000788 buffer_put_cstring(m, challenge);
789
Damien Millerb7df3af2003-02-24 11:55:46 +1100790 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000791 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000792
793 return (0);
794}
795
796int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000797mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000798{
799 char *response;
800 int authok;
801
802 response = buffer_get_string(m, NULL);
803
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000804 authok = (options.challenge_response_authentication &&
805 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000806 skey_haskey(authctxt->pw->pw_name) == 0 &&
807 skey_passcheck(authctxt->pw->pw_name, response) != -1);
808
809 xfree(response);
810
811 buffer_clear(m);
812 buffer_put_int(m, authok);
813
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000814 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000815 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000816
817 auth_method = "skey";
818
819 return (authok != 0);
820}
821#endif
822
Damien Miller79418552002-04-23 20:28:48 +1000823#ifdef USE_PAM
824int
Darren Tucker7eda5922004-06-22 13:22:50 +1000825mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000826{
Damien Miller4e448a32003-05-14 15:11:48 +1000827 if (!options.use_pam)
828 fatal("UsePAM not set, but ended up in %s anyway", __func__);
829
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100830 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000831
Damien Miller1f499fd2003-08-25 13:08:49 +1000832 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
833
Damien Miller79418552002-04-23 20:28:48 +1000834 return (0);
835}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000836
Damien Miller1f499fd2003-08-25 13:08:49 +1000837int
Darren Tucker7eda5922004-06-22 13:22:50 +1000838mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000839{
840 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100841
Damien Miller1f499fd2003-08-25 13:08:49 +1000842 if (!options.use_pam)
843 fatal("UsePAM not set, but ended up in %s anyway", __func__);
844
845 ret = do_pam_account();
846
847 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000848 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000849
Darren Tucker7eda5922004-06-22 13:22:50 +1000850 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000851
852 return (ret);
853}
854
Damien Miller4f9f42a2003-05-10 19:28:02 +1000855static void *sshpam_ctxt, *sshpam_authok;
856extern KbdintDevice sshpam_device;
857
858int
Darren Tucker7eda5922004-06-22 13:22:50 +1000859mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000860{
861
862 debug3("%s", __func__);
863 authctxt->user = buffer_get_string(m, NULL);
864 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
865 sshpam_authok = NULL;
866 buffer_clear(m);
867 if (sshpam_ctxt != NULL) {
868 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
869 buffer_put_int(m, 1);
870 } else {
871 buffer_put_int(m, 0);
872 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000873 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000874 return (0);
875}
876
877int
Darren Tucker7eda5922004-06-22 13:22:50 +1000878mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000879{
880 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000881 u_int i, num, *echo_on;
882 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000883
884 debug3("%s", __func__);
885 sshpam_authok = NULL;
886 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
887 if (ret == 0 && num == 0)
888 sshpam_authok = sshpam_ctxt;
889 if (num > 1 || name == NULL || info == NULL)
890 ret = -1;
891 buffer_clear(m);
892 buffer_put_int(m, ret);
893 buffer_put_cstring(m, name);
894 xfree(name);
895 buffer_put_cstring(m, info);
896 xfree(info);
897 buffer_put_int(m, num);
898 for (i = 0; i < num; ++i) {
899 buffer_put_cstring(m, prompts[i]);
900 xfree(prompts[i]);
901 buffer_put_int(m, echo_on[i]);
902 }
903 if (prompts != NULL)
904 xfree(prompts);
905 if (echo_on != NULL)
906 xfree(echo_on);
Darren Tucker7eda5922004-06-22 13:22:50 +1000907 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000908 return (0);
909}
910
911int
Darren Tucker7eda5922004-06-22 13:22:50 +1000912mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000913{
914 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000915 u_int i, num;
916 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000917
918 debug3("%s", __func__);
919 sshpam_authok = NULL;
920 num = buffer_get_int(m);
921 if (num > 0) {
922 resp = xmalloc(num * sizeof(char *));
923 for (i = 0; i < num; ++i)
924 resp[i] = buffer_get_string(m, NULL);
925 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
926 for (i = 0; i < num; ++i)
927 xfree(resp[i]);
928 xfree(resp);
929 } else {
930 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
931 }
932 buffer_clear(m);
933 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000934 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000935 auth_method = "keyboard-interactive/pam";
936 if (ret == 0)
937 sshpam_authok = sshpam_ctxt;
938 return (0);
939}
940
941int
Darren Tucker7eda5922004-06-22 13:22:50 +1000942mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000943{
944
945 debug3("%s", __func__);
946 (sshpam_device.free_ctx)(sshpam_ctxt);
947 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000948 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000949 return (sshpam_authok == sshpam_ctxt);
950}
Damien Miller79418552002-04-23 20:28:48 +1000951#endif
952
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953static void
954mm_append_debug(Buffer *m)
955{
956 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000957 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958 buffer_append(m, buffer_ptr(&auth_debug),
959 buffer_len(&auth_debug));
960 buffer_clear(&auth_debug);
961 }
962}
963
964int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000965mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966{
967 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000968 char *cuser, *chost;
969 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000970 u_int bloblen;
971 enum mm_keytype type = 0;
972 int allowed = 0;
973
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000974 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000975
976 type = buffer_get_int(m);
977 cuser = buffer_get_string(m, NULL);
978 chost = buffer_get_string(m, NULL);
979 blob = buffer_get_string(m, &bloblen);
980
981 key = key_from_blob(blob, bloblen);
982
983 if ((compat20 && type == MM_RSAHOSTKEY) ||
984 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000985 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000986
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000987 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000988
Damien Miller3e3b5142003-11-17 21:13:40 +1100989 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +1100990 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000991 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000992 allowed = options.pubkey_authentication &&
993 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000994 break;
995 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000996 allowed = options.hostbased_authentication &&
997 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998 cuser, chost, key);
999 break;
1000 case MM_RSAHOSTKEY:
1001 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001002 allowed = options.rhosts_rsa_authentication &&
1003 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001004 cuser, chost, key);
1005 break;
1006 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001007 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001008 break;
1009 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001010 }
Damien Miller00111382003-03-10 11:21:17 +11001011 if (key != NULL)
1012 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001013
1014 /* clear temporarily storage (used by verify) */
1015 monitor_reset_key_state();
1016
1017 if (allowed) {
1018 /* Save temporarily for comparison in verify */
1019 key_blob = blob;
1020 key_bloblen = bloblen;
1021 key_blobtype = type;
1022 hostbased_cuser = cuser;
1023 hostbased_chost = chost;
1024 }
1025
1026 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001027 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001028
1029 buffer_clear(m);
1030 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001031 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001032
1033 mm_append_debug(m);
1034
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001035 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001036
1037 if (type == MM_RSAHOSTKEY)
1038 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1039
1040 return (0);
1041}
1042
1043static int
1044monitor_valid_userblob(u_char *data, u_int datalen)
1045{
1046 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001047 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001048 u_int len;
1049 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001050
1051 buffer_init(&b);
1052 buffer_append(&b, data, datalen);
1053
1054 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001055 p = buffer_ptr(&b);
1056 len = buffer_len(&b);
1057 if ((session_id2 == NULL) ||
1058 (len < session_id2_len) ||
1059 (memcmp(p, session_id2, session_id2_len) != 0))
1060 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001061 buffer_consume(&b, session_id2_len);
1062 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001063 p = buffer_get_string(&b, &len);
1064 if ((session_id2 == NULL) ||
1065 (len != session_id2_len) ||
1066 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001067 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001068 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001069 }
1070 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1071 fail++;
1072 p = buffer_get_string(&b, NULL);
1073 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001074 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001075 authctxt->user, p);
1076 fail++;
1077 }
1078 xfree(p);
1079 buffer_skip_string(&b);
1080 if (datafellows & SSH_BUG_PKAUTH) {
1081 if (!buffer_get_char(&b))
1082 fail++;
1083 } else {
1084 p = buffer_get_string(&b, NULL);
1085 if (strcmp("publickey", p) != 0)
1086 fail++;
1087 xfree(p);
1088 if (!buffer_get_char(&b))
1089 fail++;
1090 buffer_skip_string(&b);
1091 }
1092 buffer_skip_string(&b);
1093 if (buffer_len(&b) != 0)
1094 fail++;
1095 buffer_free(&b);
1096 return (fail == 0);
1097}
1098
1099static int
Damien Millera10f5612002-09-12 09:49:15 +10001100monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1101 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001102{
1103 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001104 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001105 u_int len;
1106 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001107
1108 buffer_init(&b);
1109 buffer_append(&b, data, datalen);
1110
Ben Lindstromf67e0772002-06-06 20:58:19 +00001111 p = buffer_get_string(&b, &len);
1112 if ((session_id2 == NULL) ||
1113 (len != session_id2_len) ||
1114 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001115 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001116 xfree(p);
1117
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001118 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1119 fail++;
1120 p = buffer_get_string(&b, NULL);
1121 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001122 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001123 authctxt->user, p);
1124 fail++;
1125 }
1126 xfree(p);
1127 buffer_skip_string(&b); /* service */
1128 p = buffer_get_string(&b, NULL);
1129 if (strcmp(p, "hostbased") != 0)
1130 fail++;
1131 xfree(p);
1132 buffer_skip_string(&b); /* pkalg */
1133 buffer_skip_string(&b); /* pkblob */
1134
1135 /* verify client host, strip trailing dot if necessary */
1136 p = buffer_get_string(&b, NULL);
1137 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1138 p[len - 1] = '\0';
1139 if (strcmp(p, chost) != 0)
1140 fail++;
1141 xfree(p);
1142
1143 /* verify client user */
1144 p = buffer_get_string(&b, NULL);
1145 if (strcmp(p, cuser) != 0)
1146 fail++;
1147 xfree(p);
1148
1149 if (buffer_len(&b) != 0)
1150 fail++;
1151 buffer_free(&b);
1152 return (fail == 0);
1153}
1154
1155int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001156mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001157{
1158 Key *key;
1159 u_char *signature, *data, *blob;
1160 u_int signaturelen, datalen, bloblen;
1161 int verified = 0;
1162 int valid_data = 0;
1163
1164 blob = buffer_get_string(m, &bloblen);
1165 signature = buffer_get_string(m, &signaturelen);
1166 data = buffer_get_string(m, &datalen);
1167
1168 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001169 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001170 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001171
1172 key = key_from_blob(blob, bloblen);
1173 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001174 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001175
1176 switch (key_blobtype) {
1177 case MM_USERKEY:
1178 valid_data = monitor_valid_userblob(data, datalen);
1179 break;
1180 case MM_HOSTKEY:
1181 valid_data = monitor_valid_hostbasedblob(data, datalen,
1182 hostbased_cuser, hostbased_chost);
1183 break;
1184 default:
1185 valid_data = 0;
1186 break;
1187 }
1188 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001189 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001190
1191 verified = key_verify(key, signature, signaturelen, data, datalen);
1192 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001193 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001194
1195 key_free(key);
1196 xfree(blob);
1197 xfree(signature);
1198 xfree(data);
1199
Ben Lindstrome1c09122002-06-23 00:38:24 +00001200 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1201
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202 monitor_reset_key_state();
1203
1204 buffer_clear(m);
1205 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001206 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001207
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001208 return (verified);
1209}
1210
1211static void
1212mm_record_login(Session *s, struct passwd *pw)
1213{
1214 socklen_t fromlen;
1215 struct sockaddr_storage from;
1216
1217 /*
1218 * Get IP address of client. If the connection is not a socket, let
1219 * the address be 0.0.0.0.
1220 */
1221 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001222 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001223 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001224 if (getpeername(packet_get_connection_in(),
1225 (struct sockaddr *) & from, &fromlen) < 0) {
1226 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001227 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001228 }
1229 }
1230 /* Record that there was a login on that tty from the remote host. */
1231 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001232 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001233 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001234}
1235
1236static void
1237mm_session_close(Session *s)
1238{
Damien Miller04bd8b02003-05-25 14:38:33 +10001239 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001240 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001241 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001242 session_pty_cleanup2(s);
1243 }
1244 s->used = 0;
1245}
1246
1247int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001248mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001249{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001250 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001251 Session *s;
1252 int res, fd0;
1253
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001254 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001255
1256 buffer_clear(m);
1257 s = session_new();
1258 if (s == NULL)
1259 goto error;
1260 s->authctxt = authctxt;
1261 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001262 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001263 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1264 if (res == 0)
1265 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001266 pty_setowner(authctxt->pw, s->tty);
1267
1268 buffer_put_int(m, 1);
1269 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001270
1271 /* We need to trick ttyslot */
1272 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001273 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001274
1275 mm_record_login(s, authctxt->pw);
1276
1277 /* Now we can close the file descriptor again */
1278 close(0);
1279
Darren Tucker09991742004-07-17 17:05:14 +10001280 /* send messages generated by record_login */
1281 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1282 buffer_clear(&loginmsg);
1283
1284 mm_request_send(sock, MONITOR_ANS_PTY, m);
1285
1286 mm_send_fd(sock, s->ptyfd);
1287 mm_send_fd(sock, s->ttyfd);
1288
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001289 /* make sure nothing uses fd 0 */
1290 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001291 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001292 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001293 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001294
1295 /* slave is not needed */
1296 close(s->ttyfd);
1297 s->ttyfd = s->ptyfd;
1298 /* no need to dup() because nobody closes ptyfd */
1299 s->ptymaster = s->ptyfd;
1300
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001301 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001302
1303 return (0);
1304
1305 error:
1306 if (s != NULL)
1307 mm_session_close(s);
1308 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001309 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001310 return (0);
1311}
1312
1313int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001314mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001315{
1316 Session *s;
1317 char *tty;
1318
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001319 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001320
1321 tty = buffer_get_string(m, NULL);
1322 if ((s = session_by_tty(tty)) != NULL)
1323 mm_session_close(s);
1324 buffer_clear(m);
1325 xfree(tty);
1326 return (0);
1327}
1328
1329int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001330mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001331{
1332 BIGNUM *p;
1333 int rsafail;
1334
1335 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001336 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001337
1338 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001339 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001340
1341 buffer_get_bignum2(m, p);
1342
1343 rsafail = ssh1_session_key(p);
1344
1345 buffer_clear(m);
1346 buffer_put_int(m, rsafail);
1347 buffer_put_bignum2(m, p);
1348
1349 BN_clear_free(p);
1350
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001351 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001352
1353 /* Turn on permissions for sessid passing */
1354 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1355
1356 return (0);
1357}
1358
1359int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001360mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001361{
1362 int i;
1363
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001364 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001365
1366 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001367 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001368 for (i = 0; i < 16; i++)
1369 session_id[i] = buffer_get_char(m);
1370
1371 /* Turn on permissions for getpwnam */
1372 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1373
1374 return (0);
1375}
1376
1377int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001378mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001379{
1380 BIGNUM *client_n;
1381 Key *key = NULL;
1382 u_char *blob = NULL;
1383 u_int blen = 0;
1384 int allowed = 0;
1385
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001386 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001387
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001388 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001389 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001390 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001391 buffer_get_bignum2(m, client_n);
1392 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1393 BN_clear_free(client_n);
1394 }
1395 buffer_clear(m);
1396 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001397 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001398
1399 /* clear temporarily storage (used by generate challenge) */
1400 monitor_reset_key_state();
1401
1402 if (allowed && key != NULL) {
1403 key->type = KEY_RSA; /* cheat for key_to_blob */
1404 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001405 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001406 buffer_put_string(m, blob, blen);
1407
1408 /* Save temporarily for comparison in verify */
1409 key_blob = blob;
1410 key_bloblen = blen;
1411 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001412 }
Damien Miller00111382003-03-10 11:21:17 +11001413 if (key != NULL)
1414 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001415
1416 mm_append_debug(m);
1417
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001418 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001419
1420 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1421 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1422 return (0);
1423}
1424
1425int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001426mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001427{
1428 Key *key = NULL;
1429 u_char *blob;
1430 u_int blen;
1431
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001432 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001433
1434 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001435 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001436 blob = buffer_get_string(m, &blen);
1437 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001438 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001439 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001440 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001441 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001442 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001443
1444 if (ssh1_challenge)
1445 BN_clear_free(ssh1_challenge);
1446 ssh1_challenge = auth_rsa_generate_challenge(key);
1447
1448 buffer_clear(m);
1449 buffer_put_bignum2(m, ssh1_challenge);
1450
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001451 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001452 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001453
1454 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001455
1456 xfree(blob);
1457 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001458 return (0);
1459}
1460
1461int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001462mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463{
1464 Key *key = NULL;
1465 u_char *blob, *response;
1466 u_int blen, len;
1467 int success;
1468
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001469 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001470
1471 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001472 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001473 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001474 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001475
1476 blob = buffer_get_string(m, &blen);
1477 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001478 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001479 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001480 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001482 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001483 response = buffer_get_string(m, &len);
1484 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001485 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1487
Damien Miller00111382003-03-10 11:21:17 +11001488 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001489 key_free(key);
1490 xfree(response);
1491
1492 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1493
1494 /* reset state */
1495 BN_clear_free(ssh1_challenge);
1496 ssh1_challenge = NULL;
1497 monitor_reset_key_state();
1498
1499 buffer_clear(m);
1500 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001501 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502
1503 return (success);
1504}
1505
1506int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001507mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001508{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001509 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001510 int res, status;
1511
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001512 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001513
1514 /* The child is terminating */
1515 session_destroy_all(&mm_session_close);
1516
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001517 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001518 if (errno != EINTR)
1519 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001520
1521 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1522
1523 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001524 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001525}
1526
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001527#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001528/* Report that an audit event occurred */
1529int
1530mm_answer_audit_event(int socket, Buffer *m)
1531{
1532 ssh_audit_event_t event;
1533
1534 debug3("%s entering", __func__);
1535
1536 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001537 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001538 case SSH_AUTH_FAIL_PUBKEY:
1539 case SSH_AUTH_FAIL_HOSTBASED:
1540 case SSH_AUTH_FAIL_GSSAPI:
1541 case SSH_LOGIN_EXCEED_MAXTRIES:
1542 case SSH_LOGIN_ROOT_DENIED:
1543 case SSH_CONNECTION_CLOSE:
1544 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001545 audit_event(event);
1546 break;
1547 default:
1548 fatal("Audit event type %d not permitted", event);
1549 }
1550
1551 return (0);
1552}
1553
1554int
1555mm_answer_audit_command(int socket, Buffer *m)
1556{
1557 u_int len;
1558 char *cmd;
1559
1560 debug3("%s entering", __func__);
1561 cmd = buffer_get_string(m, &len);
1562 /* sanity check command, if so how? */
1563 audit_run_command(cmd);
1564 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001565 return (0);
1566}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001567#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001568
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001569void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001570monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001571{
1572 if (compat20) {
1573 set_newkeys(MODE_IN);
1574 set_newkeys(MODE_OUT);
1575 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001576 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001577 packet_set_encryption_key(child_state.ssh1key,
1578 child_state.ssh1keylen, child_state.ssh1cipher);
1579 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001580 }
1581
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001582 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001583 packet_set_keycontext(MODE_OUT, child_state.keyout);
1584 xfree(child_state.keyout);
1585 packet_set_keycontext(MODE_IN, child_state.keyin);
1586 xfree(child_state.keyin);
1587
1588 if (!compat20) {
1589 packet_set_iv(MODE_OUT, child_state.ivout);
1590 xfree(child_state.ivout);
1591 packet_set_iv(MODE_IN, child_state.ivin);
1592 xfree(child_state.ivin);
1593 }
1594
1595 memcpy(&incoming_stream, &child_state.incoming,
1596 sizeof(incoming_stream));
1597 memcpy(&outgoing_stream, &child_state.outgoing,
1598 sizeof(outgoing_stream));
1599
1600 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001601 if (options.compression)
1602 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001603
1604 /* Network I/O buffers */
1605 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1606 buffer_clear(&input);
1607 buffer_append(&input, child_state.input, child_state.ilen);
1608 memset(child_state.input, 0, child_state.ilen);
1609 xfree(child_state.input);
1610
1611 buffer_clear(&output);
1612 buffer_append(&output, child_state.output, child_state.olen);
1613 memset(child_state.output, 0, child_state.olen);
1614 xfree(child_state.output);
1615}
1616
1617static Kex *
1618mm_get_kex(Buffer *m)
1619{
1620 Kex *kex;
1621 void *blob;
1622 u_int bloblen;
1623
1624 kex = xmalloc(sizeof(*kex));
1625 memset(kex, 0, sizeof(*kex));
1626 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001627 if ((session_id2 == NULL) ||
1628 (kex->session_id_len != session_id2_len) ||
1629 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1630 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001631 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001632 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001633 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001634 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001635 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001636 kex->server = 1;
1637 kex->hostkey_type = buffer_get_int(m);
1638 kex->kex_type = buffer_get_int(m);
1639 blob = buffer_get_string(m, &bloblen);
1640 buffer_init(&kex->my);
1641 buffer_append(&kex->my, blob, bloblen);
1642 xfree(blob);
1643 blob = buffer_get_string(m, &bloblen);
1644 buffer_init(&kex->peer);
1645 buffer_append(&kex->peer, blob, bloblen);
1646 xfree(blob);
1647 kex->done = 1;
1648 kex->flags = buffer_get_int(m);
1649 kex->client_version_string = buffer_get_string(m, NULL);
1650 kex->server_version_string = buffer_get_string(m, NULL);
1651 kex->load_host_key=&get_hostkey_by_type;
1652 kex->host_key_index=&get_hostkey_index;
1653
1654 return (kex);
1655}
1656
1657/* This function requries careful sanity checking */
1658
1659void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001660mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001661{
1662 Buffer m;
1663 u_char *blob, *p;
1664 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001665 u_int32_t seqnr, packets;
1666 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001667
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001668 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001669
1670 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001671 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001672 if (!compat20) {
1673 child_state.ssh1protoflags = buffer_get_int(&m);
1674 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001675 child_state.ssh1key = buffer_get_string(&m,
1676 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001677 child_state.ivout = buffer_get_string(&m,
1678 &child_state.ivoutlen);
1679 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1680 goto skip;
1681 } else {
1682 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001683 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001684 }
1685
1686 blob = buffer_get_string(&m, &bloblen);
1687 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1688 xfree(blob);
1689
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001690 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001691 blob = buffer_get_string(&m, &bloblen);
1692 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1693 xfree(blob);
1694
1695 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001696 seqnr = buffer_get_int(&m);
1697 blocks = buffer_get_int64(&m);
1698 packets = buffer_get_int(&m);
1699 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1700 seqnr = buffer_get_int(&m);
1701 blocks = buffer_get_int64(&m);
1702 packets = buffer_get_int(&m);
1703 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001704
1705 skip:
1706 /* Get the key context */
1707 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1708 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1709
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001710 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001711 /* Get compression state */
1712 p = buffer_get_string(&m, &plen);
1713 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001714 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001715 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1716 xfree(p);
1717
1718 p = buffer_get_string(&m, &plen);
1719 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001720 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001721 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1722 xfree(p);
1723
1724 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001725 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001726 child_state.input = buffer_get_string(&m, &child_state.ilen);
1727 child_state.output = buffer_get_string(&m, &child_state.olen);
1728
1729 buffer_free(&m);
1730}
1731
1732
1733/* Allocation functions for zlib */
1734void *
1735mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1736{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001737 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001738 void *address;
1739
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001740 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001741 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1742
1743 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001744
1745 return (address);
1746}
1747
1748void
1749mm_zfree(struct mm_master *mm, void *address)
1750{
1751 mm_free(mm, address);
1752}
1753
1754void
1755mm_init_compression(struct mm_master *mm)
1756{
1757 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1758 outgoing_stream.zfree = (free_func)mm_zfree;
1759 outgoing_stream.opaque = mm;
1760
1761 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1762 incoming_stream.zfree = (free_func)mm_zfree;
1763 incoming_stream.opaque = mm;
1764}
1765
1766/* XXX */
1767
1768#define FD_CLOSEONEXEC(x) do { \
1769 if (fcntl(x, F_SETFD, 1) == -1) \
1770 fatal("fcntl(%d, F_SETFD)", x); \
1771} while (0)
1772
1773static void
1774monitor_socketpair(int *pair)
1775{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001776#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001777 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001778 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001779#else
1780 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001781 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001782#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001783 FD_CLOSEONEXEC(pair[0]);
1784 FD_CLOSEONEXEC(pair[1]);
1785}
1786
1787#define MM_MEMSIZE 65536
1788
1789struct monitor *
1790monitor_init(void)
1791{
1792 struct monitor *mon;
1793 int pair[2];
1794
1795 mon = xmalloc(sizeof(*mon));
1796
Darren Tuckera8be9e22004-02-06 16:40:27 +11001797 mon->m_pid = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001798 monitor_socketpair(pair);
1799
1800 mon->m_recvfd = pair[0];
1801 mon->m_sendfd = pair[1];
1802
1803 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001804 if (options.compression) {
1805 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1806 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001807
Damien Miller2d6b8332002-06-21 15:59:49 +10001808 /* Compression needs to share state across borders */
1809 mm_init_compression(mon->m_zlib);
1810 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001811
1812 return mon;
1813}
1814
1815void
1816monitor_reinit(struct monitor *mon)
1817{
1818 int pair[2];
1819
1820 monitor_socketpair(pair);
1821
1822 mon->m_recvfd = pair[0];
1823 mon->m_sendfd = pair[1];
1824}
Darren Tucker0efd1552003-08-26 11:49:55 +10001825
1826#ifdef GSSAPI
1827int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001828mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001829{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001830 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001831 OM_uint32 major;
1832 u_int len;
1833
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001834 goid.elements = buffer_get_string(m, &len);
1835 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001836
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001837 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001838
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001839 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001840
1841 buffer_clear(m);
1842 buffer_put_int(m, major);
1843
Damien Miller6fd6def2005-11-05 15:07:05 +11001844 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001845
1846 /* Now we have a context, enable the step */
1847 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1848
1849 return (0);
1850}
1851
1852int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001853mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001854{
1855 gss_buffer_desc in;
1856 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001857 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001858 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001859 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001860
Darren Tucker600ad8d2003-08-26 12:10:48 +10001861 in.value = buffer_get_string(m, &len);
1862 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001863 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1864 xfree(in.value);
1865
1866 buffer_clear(m);
1867 buffer_put_int(m, major);
1868 buffer_put_string(m, out.value, out.length);
1869 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001870 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001871
1872 gss_release_buffer(&minor, &out);
1873
Damien Miller6fd6def2005-11-05 15:07:05 +11001874 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001875 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1876 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001877 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001878 }
1879 return (0);
1880}
1881
1882int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001883mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001884{
1885 gss_buffer_desc gssbuf, mic;
1886 OM_uint32 ret;
1887 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001888
Damien Miller0425d402003-11-17 22:18:21 +11001889 gssbuf.value = buffer_get_string(m, &len);
1890 gssbuf.length = len;
1891 mic.value = buffer_get_string(m, &len);
1892 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001893
Damien Miller0425d402003-11-17 22:18:21 +11001894 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001895
Damien Miller0425d402003-11-17 22:18:21 +11001896 xfree(gssbuf.value);
1897 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001898
Damien Miller0425d402003-11-17 22:18:21 +11001899 buffer_clear(m);
1900 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001901
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001902 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001903
Damien Miller0425d402003-11-17 22:18:21 +11001904 if (!GSS_ERROR(ret))
1905 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001906
Damien Miller0425d402003-11-17 22:18:21 +11001907 return (0);
1908}
1909
1910int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001911mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001912{
1913 int authenticated;
1914
1915 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1916
1917 buffer_clear(m);
1918 buffer_put_int(m, authenticated);
1919
1920 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001921 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001922
Damien Miller6fd6def2005-11-05 15:07:05 +11001923 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001924
1925 /* Monitor loop will terminate if authenticated */
1926 return (authenticated);
1927}
1928#endif /* GSSAPI */