blob: b06cecf7d6d7c51ca369bd5f45d9f273c83f0088 [file] [log] [blame]
Damien Miller57cf6382006-07-10 21:13:46 +10001/* $OpenBSD: monitor.c,v 1.80 2006/07/09 15:15:10 stevesk Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100031#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110032#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000033
Damien Miller57cf6382006-07-10 21:13:46 +100034#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110035#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110036#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110037#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100038#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110039#include <signal.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000040
41#ifdef SKEY
42#include <skey.h>
43#endif
44
Damien Miller03e20032006-03-15 11:16:59 +110045#include <openssl/dh.h>
46
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000047#include "ssh.h"
48#include "auth.h"
49#include "kex.h"
50#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000051#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
52#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000053#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000054#define TARGET_OS_MAC 1
55#else
56#include "zlib.h"
57#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000058#include "packet.h"
59#include "auth-options.h"
60#include "sshpty.h"
61#include "channels.h"
62#include "session.h"
63#include "sshlogin.h"
64#include "canohost.h"
65#include "log.h"
66#include "servconf.h"
67#include "monitor.h"
68#include "monitor_mm.h"
69#include "monitor_wrap.h"
70#include "monitor_fdpass.h"
71#include "xmalloc.h"
72#include "misc.h"
73#include "buffer.h"
74#include "bufaux.h"
75#include "compat.h"
76#include "ssh2.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000077
Darren Tucker0efd1552003-08-26 11:49:55 +100078#ifdef GSSAPI
79#include "ssh-gss.h"
80static Gssctxt *gsscontext = NULL;
81#endif
82
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000083/* Imports */
84extern ServerOptions options;
85extern u_int utmp_len;
86extern Newkeys *current_keys[];
87extern z_stream incoming_stream;
88extern z_stream outgoing_stream;
89extern u_char session_id[];
90extern Buffer input, output;
91extern Buffer auth_debug;
92extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +100093extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000094
95/* State exported from the child */
96
97struct {
98 z_stream incoming;
99 z_stream outgoing;
100 u_char *keyin;
101 u_int keyinlen;
102 u_char *keyout;
103 u_int keyoutlen;
104 u_char *ivin;
105 u_int ivinlen;
106 u_char *ivout;
107 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000108 u_char *ssh1key;
109 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000110 int ssh1cipher;
111 int ssh1protoflags;
112 u_char *input;
113 u_int ilen;
114 u_char *output;
115 u_int olen;
116} child_state;
117
Damien Miller469954d2003-06-18 20:25:33 +1000118/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000119
120int mm_answer_moduli(int, Buffer *);
121int mm_answer_sign(int, Buffer *);
122int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000123int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000124int mm_answer_authserv(int, Buffer *);
125int mm_answer_authpassword(int, Buffer *);
126int mm_answer_bsdauthquery(int, Buffer *);
127int mm_answer_bsdauthrespond(int, Buffer *);
128int mm_answer_skeyquery(int, Buffer *);
129int mm_answer_skeyrespond(int, Buffer *);
130int mm_answer_keyallowed(int, Buffer *);
131int mm_answer_keyverify(int, Buffer *);
132int mm_answer_pty(int, Buffer *);
133int mm_answer_pty_cleanup(int, Buffer *);
134int mm_answer_term(int, Buffer *);
135int mm_answer_rsa_keyallowed(int, Buffer *);
136int mm_answer_rsa_challenge(int, Buffer *);
137int mm_answer_rsa_response(int, Buffer *);
138int mm_answer_sesskey(int, Buffer *);
139int mm_answer_sessid(int, Buffer *);
140
Damien Miller79418552002-04-23 20:28:48 +1000141#ifdef USE_PAM
142int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000143int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000144int mm_answer_pam_init_ctx(int, Buffer *);
145int mm_answer_pam_query(int, Buffer *);
146int mm_answer_pam_respond(int, Buffer *);
147int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000148#endif
149
Darren Tucker0efd1552003-08-26 11:49:55 +1000150#ifdef GSSAPI
151int mm_answer_gss_setup_ctx(int, Buffer *);
152int mm_answer_gss_accept_ctx(int, Buffer *);
153int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100154int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000155#endif
Damien Miller25162f22002-09-12 09:47:29 +1000156
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100157#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100158int mm_answer_audit_event(int, Buffer *);
159int mm_answer_audit_command(int, Buffer *);
160#endif
161
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000162static Authctxt *authctxt;
163static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
164
165/* local state for key verify */
166static u_char *key_blob = NULL;
167static u_int key_bloblen = 0;
168static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000169static char *hostbased_cuser = NULL;
170static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000172static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000173static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000174static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000175
176struct mon_table {
177 enum monitor_reqtype type;
178 int flags;
179 int (*f)(int, Buffer *);
180};
181
182#define MON_ISAUTH 0x0004 /* Required for Authentication */
183#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
184#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100185#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000186
187#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
188
189#define MON_PERMIT 0x1000 /* Request is permitted */
190
191struct mon_table mon_dispatch_proto20[] = {
192 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
193 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
194 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
195 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000196 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000197 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000198#ifdef USE_PAM
199 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000200 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000201 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
202 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
203 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
204 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000205#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100206#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100207 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100208#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000209#ifdef BSD_AUTH
210 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100211 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000212#endif
213#ifdef SKEY
214 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
215 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
216#endif
217 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
218 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000219#ifdef GSSAPI
220 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
221 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
222 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100223 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000224#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000225 {0, 0, NULL}
226};
227
228struct mon_table mon_dispatch_postauth20[] = {
229 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
230 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
231 {MONITOR_REQ_PTY, 0, mm_answer_pty},
232 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
233 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100234#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100235 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
236 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
237#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000238 {0, 0, NULL}
239};
240
241struct mon_table mon_dispatch_proto15[] = {
242 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
243 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
244 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
245 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100246 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
247 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000248 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
249 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
250#ifdef BSD_AUTH
251 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100252 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000253#endif
254#ifdef SKEY
255 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
256 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
257#endif
Damien Millera33501b2002-05-08 12:24:42 +1000258#ifdef USE_PAM
259 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000260 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000261 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
262 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
263 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
264 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000265#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100266#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100267 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100268#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000269 {0, 0, NULL}
270};
271
272struct mon_table mon_dispatch_postauth15[] = {
273 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
274 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
275 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100276#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100277 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker3745e2b2005-03-06 22:31:35 +1100278 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100279#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000280 {0, 0, NULL}
281};
282
283struct mon_table *mon_dispatch;
284
285/* Specifies if a certain message is allowed at the moment */
286
287static void
288monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
289{
290 while (ent->f != NULL) {
291 if (ent->type == type) {
292 ent->flags &= ~MON_PERMIT;
293 ent->flags |= permit ? MON_PERMIT : 0;
294 return;
295 }
296 ent++;
297 }
298}
299
300static void
301monitor_permit_authentications(int permit)
302{
303 struct mon_table *ent = mon_dispatch;
304
305 while (ent->f != NULL) {
306 if (ent->flags & MON_AUTH) {
307 ent->flags &= ~MON_PERMIT;
308 ent->flags |= permit ? MON_PERMIT : 0;
309 }
310 ent++;
311 }
312}
313
Darren Tucker3e33cec2003-10-02 16:12:36 +1000314void
315monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000316{
317 struct mon_table *ent;
318 int authenticated = 0;
319
320 debug3("preauth child monitor started");
321
Darren Tucker3e33cec2003-10-02 16:12:36 +1000322 authctxt = _authctxt;
323 memset(authctxt, 0, sizeof(*authctxt));
324
Darren Tuckerde0de392005-03-31 23:52:04 +1000325 authctxt->loginmsg = &loginmsg;
326
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000327 if (compat20) {
328 mon_dispatch = mon_dispatch_proto20;
329
330 /* Permit requests for moduli and signatures */
331 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
332 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
333 } else {
334 mon_dispatch = mon_dispatch_proto15;
335
336 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
337 }
338
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339 /* The first few requests do not require asynchronous access */
340 while (!authenticated) {
Damien Miller7a8f5b32006-03-31 23:14:23 +1100341 auth_method = "unknown";
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000342 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000343 if (authenticated) {
344 if (!(ent->flags & MON_AUTHDECIDE))
345 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000346 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000347 if (authctxt->pw->pw_uid == 0 &&
348 !auth_root_allowed(auth_method))
349 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000350#ifdef USE_PAM
351 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100352 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000353 Buffer m;
354
355 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100356 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000357 MONITOR_REQ_PAM_ACCOUNT, &m);
358 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
359 buffer_free(&m);
360 }
361#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000362 }
363
Damien Miller7a8f5b32006-03-31 23:14:23 +1100364 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000365 auth_log(authctxt, authenticated, auth_method,
366 compat20 ? " ssh2" : "");
367 if (!authenticated)
368 authctxt->failures++;
369 }
370 }
371
372 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000373 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100374 if (strcmp(auth_method, "unknown") == 0)
375 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000376
377 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000378 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000379
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000380 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000381}
382
Damien Millerbe64d432003-05-14 19:31:12 +1000383static void
384monitor_set_child_handler(pid_t pid)
385{
386 monitor_child_pid = pid;
387}
388
389static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000390monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000391{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000392 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000393}
394
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000395void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000396monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000397{
Damien Millerbe64d432003-05-14 19:31:12 +1000398 monitor_set_child_handler(pmonitor->m_pid);
399 signal(SIGHUP, &monitor_child_handler);
400 signal(SIGTERM, &monitor_child_handler);
401
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000402 if (compat20) {
403 mon_dispatch = mon_dispatch_postauth20;
404
405 /* Permit requests for moduli and signatures */
406 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
407 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
408 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409 } else {
410 mon_dispatch = mon_dispatch_postauth15;
411 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
412 }
413 if (!no_pty_flag) {
414 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
415 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
416 }
417
418 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000419 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000420}
421
422void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000423monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000424{
Damien Miller2d6b8332002-06-21 15:59:49 +1000425 if (options.compression) {
426 /* The member allocation is not visible, so sync it */
427 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
428 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000429}
430
431int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000432monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000433 struct mon_table **pent)
434{
435 Buffer m;
436 int ret;
437 u_char type;
438
439 buffer_init(&m);
440
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000441 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000442 type = buffer_get_char(&m);
443
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000444 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000445
446 while (ent->f != NULL) {
447 if (ent->type == type)
448 break;
449 ent++;
450 }
451
452 if (ent->f != NULL) {
453 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000454 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000455 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000456 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000457 buffer_free(&m);
458
459 /* The child may use this request only once, disable it */
460 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000461 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000462 type);
463 ent->flags &= ~MON_PERMIT;
464 }
465
466 if (pent != NULL)
467 *pent = ent;
468
469 return ret;
470 }
471
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000472 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000473
474 /* NOTREACHED */
475 return (-1);
476}
477
478/* allowed key state */
479static int
480monitor_allowed_key(u_char *blob, u_int bloblen)
481{
482 /* make sure key is allowed */
483 if (key_blob == NULL || key_bloblen != bloblen ||
484 memcmp(key_blob, blob, key_bloblen))
485 return (0);
486 return (1);
487}
488
489static void
490monitor_reset_key_state(void)
491{
492 /* reset state */
493 if (key_blob != NULL)
494 xfree(key_blob);
495 if (hostbased_cuser != NULL)
496 xfree(hostbased_cuser);
497 if (hostbased_chost != NULL)
498 xfree(hostbased_chost);
499 key_blob = NULL;
500 key_bloblen = 0;
501 key_blobtype = MM_NOKEY;
502 hostbased_cuser = NULL;
503 hostbased_chost = NULL;
504}
505
506int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000507mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000508{
509 DH *dh;
510 int min, want, max;
511
512 min = buffer_get_int(m);
513 want = buffer_get_int(m);
514 max = buffer_get_int(m);
515
516 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000517 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000518 /* We need to check here, too, in case the child got corrupted */
519 if (max < min || want < min || max < want)
520 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000521 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000522
523 buffer_clear(m);
524
525 dh = choose_dh(min, want, max);
526 if (dh == NULL) {
527 buffer_put_char(m, 0);
528 return (0);
529 } else {
530 /* Send first bignum */
531 buffer_put_char(m, 1);
532 buffer_put_bignum2(m, dh->p);
533 buffer_put_bignum2(m, dh->g);
534
535 DH_free(dh);
536 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000537 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538 return (0);
539}
540
541int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000542mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000543{
544 Key *key;
545 u_char *p;
546 u_char *signature;
547 u_int siglen, datlen;
548 int keyid;
549
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000550 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551
552 keyid = buffer_get_int(m);
553 p = buffer_get_string(m, &datlen);
554
Damien Millera63128d2006-03-15 12:08:28 +1100555 /*
Damien Millerc91e5562006-03-26 13:58:55 +1100556 * Supported KEX types will only return SHA1 (20 byte) or
Damien Millera63128d2006-03-15 12:08:28 +1100557 * SHA256 (32 byte) hashes
558 */
559 if (datlen != 20 && datlen != 32)
Ben Lindstromd5502182002-06-27 00:12:57 +0000560 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561
Ben Lindstromf67e0772002-06-06 20:58:19 +0000562 /* save session id, it will be passed on the first call */
563 if (session_id2_len == 0) {
564 session_id2_len = datlen;
565 session_id2 = xmalloc(session_id2_len);
566 memcpy(session_id2, p, session_id2_len);
567 }
568
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000569 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000570 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000571 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000572 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000573
Ben Lindstromd5502182002-06-27 00:12:57 +0000574 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575
576 buffer_clear(m);
577 buffer_put_string(m, signature, siglen);
578
579 xfree(p);
580 xfree(signature);
581
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000582 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000583
584 /* Turn on permissions for getpwnam */
585 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
586
587 return (0);
588}
589
590/* Retrieves the password entry and also checks if the user is permitted */
591
592int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000593mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000595 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596 struct passwd *pwent;
597 int allowed = 0;
598
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000599 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600
601 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000602 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000603
Darren Tuckerb09b6772004-06-22 15:06:46 +1000604 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605
Darren Tuckerb09b6772004-06-22 15:06:46 +1000606 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000607
Darren Tuckerb09b6772004-06-22 15:06:46 +1000608 authctxt->user = xstrdup(username);
609 setproctitle("%s [priv]", pwent ? username : "unknown");
610 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000611
612 buffer_clear(m);
613
614 if (pwent == NULL) {
615 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100616 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000617 goto out;
618 }
619
620 allowed = 1;
621 authctxt->pw = pwent;
622 authctxt->valid = 1;
623
624 buffer_put_char(m, 1);
625 buffer_put_string(m, pwent, sizeof(struct passwd));
626 buffer_put_cstring(m, pwent->pw_name);
627 buffer_put_cstring(m, "*");
628 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000629#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000630 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000631#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000632 buffer_put_cstring(m, pwent->pw_dir);
633 buffer_put_cstring(m, pwent->pw_shell);
634
635 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000636 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000637 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000638
639 /* For SSHv1 allow authentication now */
640 if (!compat20)
641 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000642 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000643 /* Allow service/style information on the auth context */
644 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000645 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
646 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000647
Damien Millera33501b2002-05-08 12:24:42 +1000648#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000649 if (options.use_pam)
650 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000651#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100652#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100653 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100654#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655
656 return (0);
657}
658
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000659int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000660{
661 char *banner;
662
663 buffer_clear(m);
664 banner = auth2_read_banner();
665 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000666 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000667
668 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000669 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000670
671 return (0);
672}
673
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000674int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000675mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000676{
677 monitor_permit_authentications(1);
678
679 authctxt->service = buffer_get_string(m, NULL);
680 authctxt->style = buffer_get_string(m, NULL);
681 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000682 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000683
684 if (strlen(authctxt->style) == 0) {
685 xfree(authctxt->style);
686 authctxt->style = NULL;
687 }
688
689 return (0);
690}
691
692int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000693mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000694{
695 static int call_count;
696 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000697 int authenticated;
698 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000699
700 passwd = buffer_get_string(m, &plen);
701 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000702 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000703 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000704 memset(passwd, 0, strlen(passwd));
705 xfree(passwd);
706
707 buffer_clear(m);
708 buffer_put_int(m, authenticated);
709
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000710 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000711 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712
713 call_count++;
714 if (plen == 0 && call_count == 1)
715 auth_method = "none";
716 else
717 auth_method = "password";
718
719 /* Causes monitor loop to terminate if authenticated */
720 return (authenticated);
721}
722
723#ifdef BSD_AUTH
724int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000725mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000726{
727 char *name, *infotxt;
728 u_int numprompts;
729 u_int *echo_on;
730 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100731 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000732
Damien Millerb7df3af2003-02-24 11:55:46 +1100733 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
734 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000735
736 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100737 buffer_put_int(m, success);
738 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739 buffer_put_cstring(m, prompts[0]);
740
Damien Millerb7df3af2003-02-24 11:55:46 +1100741 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000742 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743
Damien Millerb7df3af2003-02-24 11:55:46 +1100744 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000745 xfree(name);
746 xfree(infotxt);
747 xfree(prompts);
748 xfree(echo_on);
749 }
750
751 return (0);
752}
753
754int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000755mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000756{
757 char *response;
758 int authok;
759
760 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000761 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000762
763 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000764 authok = options.challenge_response_authentication &&
765 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000766 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000767 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000768 xfree(response);
769
770 buffer_clear(m);
771 buffer_put_int(m, authok);
772
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000773 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000774 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000775
776 auth_method = "bsdauth";
777
778 return (authok != 0);
779}
780#endif
781
782#ifdef SKEY
783int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000784mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000785{
786 struct skey skey;
787 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100788 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000789
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000790 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
791 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000792
793 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100794 buffer_put_int(m, success);
795 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000796 buffer_put_cstring(m, challenge);
797
Damien Millerb7df3af2003-02-24 11:55:46 +1100798 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000799 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000800
801 return (0);
802}
803
804int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000805mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000806{
807 char *response;
808 int authok;
809
810 response = buffer_get_string(m, NULL);
811
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000812 authok = (options.challenge_response_authentication &&
813 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000814 skey_haskey(authctxt->pw->pw_name) == 0 &&
815 skey_passcheck(authctxt->pw->pw_name, response) != -1);
816
817 xfree(response);
818
819 buffer_clear(m);
820 buffer_put_int(m, authok);
821
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000822 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000823 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000824
825 auth_method = "skey";
826
827 return (authok != 0);
828}
829#endif
830
Damien Miller79418552002-04-23 20:28:48 +1000831#ifdef USE_PAM
832int
Darren Tucker7eda5922004-06-22 13:22:50 +1000833mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000834{
Damien Miller4e448a32003-05-14 15:11:48 +1000835 if (!options.use_pam)
836 fatal("UsePAM not set, but ended up in %s anyway", __func__);
837
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100838 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000839
Damien Miller1f499fd2003-08-25 13:08:49 +1000840 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
841
Damien Miller79418552002-04-23 20:28:48 +1000842 return (0);
843}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000844
Damien Miller1f499fd2003-08-25 13:08:49 +1000845int
Darren Tucker7eda5922004-06-22 13:22:50 +1000846mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000847{
848 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100849
Damien Miller1f499fd2003-08-25 13:08:49 +1000850 if (!options.use_pam)
851 fatal("UsePAM not set, but ended up in %s anyway", __func__);
852
853 ret = do_pam_account();
854
855 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000856 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000857
Darren Tucker7eda5922004-06-22 13:22:50 +1000858 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000859
860 return (ret);
861}
862
Damien Miller4f9f42a2003-05-10 19:28:02 +1000863static void *sshpam_ctxt, *sshpam_authok;
864extern KbdintDevice sshpam_device;
865
866int
Darren Tucker7eda5922004-06-22 13:22:50 +1000867mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000868{
869
870 debug3("%s", __func__);
871 authctxt->user = buffer_get_string(m, NULL);
872 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
873 sshpam_authok = NULL;
874 buffer_clear(m);
875 if (sshpam_ctxt != NULL) {
876 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
877 buffer_put_int(m, 1);
878 } else {
879 buffer_put_int(m, 0);
880 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000881 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000882 return (0);
883}
884
885int
Darren Tucker7eda5922004-06-22 13:22:50 +1000886mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000887{
888 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000889 u_int i, num, *echo_on;
890 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000891
892 debug3("%s", __func__);
893 sshpam_authok = NULL;
894 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
895 if (ret == 0 && num == 0)
896 sshpam_authok = sshpam_ctxt;
897 if (num > 1 || name == NULL || info == NULL)
898 ret = -1;
899 buffer_clear(m);
900 buffer_put_int(m, ret);
901 buffer_put_cstring(m, name);
902 xfree(name);
903 buffer_put_cstring(m, info);
904 xfree(info);
905 buffer_put_int(m, num);
906 for (i = 0; i < num; ++i) {
907 buffer_put_cstring(m, prompts[i]);
908 xfree(prompts[i]);
909 buffer_put_int(m, echo_on[i]);
910 }
911 if (prompts != NULL)
912 xfree(prompts);
913 if (echo_on != NULL)
914 xfree(echo_on);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000915 auth_method = "keyboard-interactive/pam";
Darren Tucker7eda5922004-06-22 13:22:50 +1000916 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000917 return (0);
918}
919
920int
Darren Tucker7eda5922004-06-22 13:22:50 +1000921mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000922{
923 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000924 u_int i, num;
925 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000926
927 debug3("%s", __func__);
928 sshpam_authok = NULL;
929 num = buffer_get_int(m);
930 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +1000931 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000932 for (i = 0; i < num; ++i)
933 resp[i] = buffer_get_string(m, NULL);
934 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
935 for (i = 0; i < num; ++i)
936 xfree(resp[i]);
937 xfree(resp);
938 } else {
939 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
940 }
941 buffer_clear(m);
942 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000943 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000944 auth_method = "keyboard-interactive/pam";
945 if (ret == 0)
946 sshpam_authok = sshpam_ctxt;
947 return (0);
948}
949
950int
Darren Tucker7eda5922004-06-22 13:22:50 +1000951mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000952{
953
954 debug3("%s", __func__);
955 (sshpam_device.free_ctx)(sshpam_ctxt);
956 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000957 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000958 auth_method = "keyboard-interactive/pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000959 return (sshpam_authok == sshpam_ctxt);
960}
Damien Miller79418552002-04-23 20:28:48 +1000961#endif
962
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000963static void
964mm_append_debug(Buffer *m)
965{
966 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000967 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000968 buffer_append(m, buffer_ptr(&auth_debug),
969 buffer_len(&auth_debug));
970 buffer_clear(&auth_debug);
971 }
972}
973
974int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000975mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976{
977 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000978 char *cuser, *chost;
979 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000980 u_int bloblen;
981 enum mm_keytype type = 0;
982 int allowed = 0;
983
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000984 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000985
986 type = buffer_get_int(m);
987 cuser = buffer_get_string(m, NULL);
988 chost = buffer_get_string(m, NULL);
989 blob = buffer_get_string(m, &bloblen);
990
991 key = key_from_blob(blob, bloblen);
992
993 if ((compat20 && type == MM_RSAHOSTKEY) ||
994 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000995 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000996
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000997 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998
Damien Miller3e3b5142003-11-17 21:13:40 +1100999 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001000 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001001 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001002 allowed = options.pubkey_authentication &&
1003 user_key_allowed(authctxt->pw, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001004 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005 break;
1006 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001007 allowed = options.hostbased_authentication &&
1008 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001009 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001010 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001011 break;
1012 case MM_RSAHOSTKEY:
1013 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001014 allowed = options.rhosts_rsa_authentication &&
1015 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001016 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001017 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001018 break;
1019 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001020 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001021 break;
1022 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001023 }
Damien Miller00111382003-03-10 11:21:17 +11001024 if (key != NULL)
1025 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001026
1027 /* clear temporarily storage (used by verify) */
1028 monitor_reset_key_state();
1029
1030 if (allowed) {
1031 /* Save temporarily for comparison in verify */
1032 key_blob = blob;
1033 key_bloblen = bloblen;
1034 key_blobtype = type;
1035 hostbased_cuser = cuser;
1036 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001037 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001038 /* Log failed attempt */
1039 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
Damien Miller96937bd2006-03-26 14:01:54 +11001040 xfree(blob);
1041 xfree(cuser);
1042 xfree(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001043 }
1044
1045 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001046 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001047
1048 buffer_clear(m);
1049 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001050 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001051
1052 mm_append_debug(m);
1053
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001054 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001055
1056 if (type == MM_RSAHOSTKEY)
1057 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1058
1059 return (0);
1060}
1061
1062static int
1063monitor_valid_userblob(u_char *data, u_int datalen)
1064{
1065 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001066 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001067 u_int len;
1068 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001069
1070 buffer_init(&b);
1071 buffer_append(&b, data, datalen);
1072
1073 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001074 p = buffer_ptr(&b);
1075 len = buffer_len(&b);
1076 if ((session_id2 == NULL) ||
1077 (len < session_id2_len) ||
1078 (memcmp(p, session_id2, session_id2_len) != 0))
1079 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001080 buffer_consume(&b, session_id2_len);
1081 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001082 p = buffer_get_string(&b, &len);
1083 if ((session_id2 == NULL) ||
1084 (len != session_id2_len) ||
1085 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001086 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001087 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001088 }
1089 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1090 fail++;
1091 p = buffer_get_string(&b, NULL);
1092 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001093 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001094 authctxt->user, p);
1095 fail++;
1096 }
1097 xfree(p);
1098 buffer_skip_string(&b);
1099 if (datafellows & SSH_BUG_PKAUTH) {
1100 if (!buffer_get_char(&b))
1101 fail++;
1102 } else {
1103 p = buffer_get_string(&b, NULL);
1104 if (strcmp("publickey", p) != 0)
1105 fail++;
1106 xfree(p);
1107 if (!buffer_get_char(&b))
1108 fail++;
1109 buffer_skip_string(&b);
1110 }
1111 buffer_skip_string(&b);
1112 if (buffer_len(&b) != 0)
1113 fail++;
1114 buffer_free(&b);
1115 return (fail == 0);
1116}
1117
1118static int
Damien Millera10f5612002-09-12 09:49:15 +10001119monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1120 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001121{
1122 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001123 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001124 u_int len;
1125 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001126
1127 buffer_init(&b);
1128 buffer_append(&b, data, datalen);
1129
Ben Lindstromf67e0772002-06-06 20:58:19 +00001130 p = buffer_get_string(&b, &len);
1131 if ((session_id2 == NULL) ||
1132 (len != session_id2_len) ||
1133 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001134 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001135 xfree(p);
1136
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001137 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1138 fail++;
1139 p = buffer_get_string(&b, NULL);
1140 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001141 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001142 authctxt->user, p);
1143 fail++;
1144 }
1145 xfree(p);
1146 buffer_skip_string(&b); /* service */
1147 p = buffer_get_string(&b, NULL);
1148 if (strcmp(p, "hostbased") != 0)
1149 fail++;
1150 xfree(p);
1151 buffer_skip_string(&b); /* pkalg */
1152 buffer_skip_string(&b); /* pkblob */
1153
1154 /* verify client host, strip trailing dot if necessary */
1155 p = buffer_get_string(&b, NULL);
1156 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1157 p[len - 1] = '\0';
1158 if (strcmp(p, chost) != 0)
1159 fail++;
1160 xfree(p);
1161
1162 /* verify client user */
1163 p = buffer_get_string(&b, NULL);
1164 if (strcmp(p, cuser) != 0)
1165 fail++;
1166 xfree(p);
1167
1168 if (buffer_len(&b) != 0)
1169 fail++;
1170 buffer_free(&b);
1171 return (fail == 0);
1172}
1173
1174int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001175mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001176{
1177 Key *key;
1178 u_char *signature, *data, *blob;
1179 u_int signaturelen, datalen, bloblen;
1180 int verified = 0;
1181 int valid_data = 0;
1182
1183 blob = buffer_get_string(m, &bloblen);
1184 signature = buffer_get_string(m, &signaturelen);
1185 data = buffer_get_string(m, &datalen);
1186
1187 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001188 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001189 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001190
1191 key = key_from_blob(blob, bloblen);
1192 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001193 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001194
1195 switch (key_blobtype) {
1196 case MM_USERKEY:
1197 valid_data = monitor_valid_userblob(data, datalen);
1198 break;
1199 case MM_HOSTKEY:
1200 valid_data = monitor_valid_hostbasedblob(data, datalen,
1201 hostbased_cuser, hostbased_chost);
1202 break;
1203 default:
1204 valid_data = 0;
1205 break;
1206 }
1207 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001208 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001209
1210 verified = key_verify(key, signature, signaturelen, data, datalen);
1211 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001212 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001213
1214 key_free(key);
1215 xfree(blob);
1216 xfree(signature);
1217 xfree(data);
1218
Ben Lindstrome1c09122002-06-23 00:38:24 +00001219 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1220
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001221 monitor_reset_key_state();
1222
1223 buffer_clear(m);
1224 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001225 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001226
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001227 return (verified);
1228}
1229
1230static void
1231mm_record_login(Session *s, struct passwd *pw)
1232{
1233 socklen_t fromlen;
1234 struct sockaddr_storage from;
1235
1236 /*
1237 * Get IP address of client. If the connection is not a socket, let
1238 * the address be 0.0.0.0.
1239 */
1240 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001241 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001242 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001243 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001244 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001246 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001247 }
1248 }
1249 /* Record that there was a login on that tty from the remote host. */
1250 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001251 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001252 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001253}
1254
1255static void
1256mm_session_close(Session *s)
1257{
Damien Miller04bd8b02003-05-25 14:38:33 +10001258 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001259 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001260 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261 session_pty_cleanup2(s);
1262 }
1263 s->used = 0;
1264}
1265
1266int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001267mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001268{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001269 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001270 Session *s;
1271 int res, fd0;
1272
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001273 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001274
1275 buffer_clear(m);
1276 s = session_new();
1277 if (s == NULL)
1278 goto error;
1279 s->authctxt = authctxt;
1280 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001281 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1283 if (res == 0)
1284 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001285 pty_setowner(authctxt->pw, s->tty);
1286
1287 buffer_put_int(m, 1);
1288 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001289
1290 /* We need to trick ttyslot */
1291 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001292 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293
1294 mm_record_login(s, authctxt->pw);
1295
1296 /* Now we can close the file descriptor again */
1297 close(0);
1298
Darren Tucker09991742004-07-17 17:05:14 +10001299 /* send messages generated by record_login */
1300 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1301 buffer_clear(&loginmsg);
1302
1303 mm_request_send(sock, MONITOR_ANS_PTY, m);
1304
1305 mm_send_fd(sock, s->ptyfd);
1306 mm_send_fd(sock, s->ttyfd);
1307
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001308 /* make sure nothing uses fd 0 */
1309 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001310 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001311 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001312 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001313
1314 /* slave is not needed */
1315 close(s->ttyfd);
1316 s->ttyfd = s->ptyfd;
1317 /* no need to dup() because nobody closes ptyfd */
1318 s->ptymaster = s->ptyfd;
1319
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001320 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001321
1322 return (0);
1323
1324 error:
1325 if (s != NULL)
1326 mm_session_close(s);
1327 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001328 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329 return (0);
1330}
1331
1332int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001333mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001334{
1335 Session *s;
1336 char *tty;
1337
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001338 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001339
1340 tty = buffer_get_string(m, NULL);
1341 if ((s = session_by_tty(tty)) != NULL)
1342 mm_session_close(s);
1343 buffer_clear(m);
1344 xfree(tty);
1345 return (0);
1346}
1347
1348int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001349mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001350{
1351 BIGNUM *p;
1352 int rsafail;
1353
1354 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001355 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001356
1357 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001358 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001359
1360 buffer_get_bignum2(m, p);
1361
1362 rsafail = ssh1_session_key(p);
1363
1364 buffer_clear(m);
1365 buffer_put_int(m, rsafail);
1366 buffer_put_bignum2(m, p);
1367
1368 BN_clear_free(p);
1369
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001370 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001371
1372 /* Turn on permissions for sessid passing */
1373 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1374
1375 return (0);
1376}
1377
1378int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001379mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001380{
1381 int i;
1382
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001383 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001384
1385 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001386 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001387 for (i = 0; i < 16; i++)
1388 session_id[i] = buffer_get_char(m);
1389
1390 /* Turn on permissions for getpwnam */
1391 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1392
1393 return (0);
1394}
1395
1396int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001397mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001398{
1399 BIGNUM *client_n;
1400 Key *key = NULL;
1401 u_char *blob = NULL;
1402 u_int blen = 0;
1403 int allowed = 0;
1404
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001405 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001406
Damien Miller7a8f5b32006-03-31 23:14:23 +11001407 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001408 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001409 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001410 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001411 buffer_get_bignum2(m, client_n);
1412 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1413 BN_clear_free(client_n);
1414 }
1415 buffer_clear(m);
1416 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001417 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001418
1419 /* clear temporarily storage (used by generate challenge) */
1420 monitor_reset_key_state();
1421
1422 if (allowed && key != NULL) {
1423 key->type = KEY_RSA; /* cheat for key_to_blob */
1424 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001425 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001426 buffer_put_string(m, blob, blen);
1427
1428 /* Save temporarily for comparison in verify */
1429 key_blob = blob;
1430 key_bloblen = blen;
1431 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001432 }
Damien Miller00111382003-03-10 11:21:17 +11001433 if (key != NULL)
1434 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001435
1436 mm_append_debug(m);
1437
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001438 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001439
1440 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1441 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1442 return (0);
1443}
1444
1445int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001446mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447{
1448 Key *key = NULL;
1449 u_char *blob;
1450 u_int blen;
1451
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001452 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001453
1454 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001455 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001456 blob = buffer_get_string(m, &blen);
1457 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001458 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001459 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001460 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001461 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001462 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463
1464 if (ssh1_challenge)
1465 BN_clear_free(ssh1_challenge);
1466 ssh1_challenge = auth_rsa_generate_challenge(key);
1467
1468 buffer_clear(m);
1469 buffer_put_bignum2(m, ssh1_challenge);
1470
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001471 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001472 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001473
1474 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001475
1476 xfree(blob);
1477 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001478 return (0);
1479}
1480
1481int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001482mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001483{
1484 Key *key = NULL;
1485 u_char *blob, *response;
1486 u_int blen, len;
1487 int success;
1488
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001489 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001490
1491 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001492 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001493 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001494 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001495
1496 blob = buffer_get_string(m, &blen);
1497 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001498 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001499 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001500 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001501 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001502 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001503 response = buffer_get_string(m, &len);
1504 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001505 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001506 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1507
Damien Miller00111382003-03-10 11:21:17 +11001508 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001509 key_free(key);
1510 xfree(response);
1511
1512 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1513
1514 /* reset state */
1515 BN_clear_free(ssh1_challenge);
1516 ssh1_challenge = NULL;
1517 monitor_reset_key_state();
1518
1519 buffer_clear(m);
1520 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001521 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001522
1523 return (success);
1524}
1525
1526int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001527mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001528{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001529 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001530 int res, status;
1531
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001532 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001533
1534 /* The child is terminating */
1535 session_destroy_all(&mm_session_close);
1536
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001537 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001538 if (errno != EINTR)
1539 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001540
1541 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1542
1543 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001544 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001545}
1546
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001547#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001548/* Report that an audit event occurred */
1549int
1550mm_answer_audit_event(int socket, Buffer *m)
1551{
1552 ssh_audit_event_t event;
1553
1554 debug3("%s entering", __func__);
1555
1556 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001557 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001558 case SSH_AUTH_FAIL_PUBKEY:
1559 case SSH_AUTH_FAIL_HOSTBASED:
1560 case SSH_AUTH_FAIL_GSSAPI:
1561 case SSH_LOGIN_EXCEED_MAXTRIES:
1562 case SSH_LOGIN_ROOT_DENIED:
1563 case SSH_CONNECTION_CLOSE:
1564 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001565 audit_event(event);
1566 break;
1567 default:
1568 fatal("Audit event type %d not permitted", event);
1569 }
1570
1571 return (0);
1572}
1573
1574int
1575mm_answer_audit_command(int socket, Buffer *m)
1576{
1577 u_int len;
1578 char *cmd;
1579
1580 debug3("%s entering", __func__);
1581 cmd = buffer_get_string(m, &len);
1582 /* sanity check command, if so how? */
1583 audit_run_command(cmd);
1584 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001585 return (0);
1586}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001587#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001588
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001589void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001590monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001591{
1592 if (compat20) {
1593 set_newkeys(MODE_IN);
1594 set_newkeys(MODE_OUT);
1595 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001596 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001597 packet_set_encryption_key(child_state.ssh1key,
1598 child_state.ssh1keylen, child_state.ssh1cipher);
1599 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001600 }
1601
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001602 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001603 packet_set_keycontext(MODE_OUT, child_state.keyout);
1604 xfree(child_state.keyout);
1605 packet_set_keycontext(MODE_IN, child_state.keyin);
1606 xfree(child_state.keyin);
1607
1608 if (!compat20) {
1609 packet_set_iv(MODE_OUT, child_state.ivout);
1610 xfree(child_state.ivout);
1611 packet_set_iv(MODE_IN, child_state.ivin);
1612 xfree(child_state.ivin);
1613 }
1614
1615 memcpy(&incoming_stream, &child_state.incoming,
1616 sizeof(incoming_stream));
1617 memcpy(&outgoing_stream, &child_state.outgoing,
1618 sizeof(outgoing_stream));
1619
1620 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001621 if (options.compression)
1622 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001623
1624 /* Network I/O buffers */
1625 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1626 buffer_clear(&input);
1627 buffer_append(&input, child_state.input, child_state.ilen);
1628 memset(child_state.input, 0, child_state.ilen);
1629 xfree(child_state.input);
1630
1631 buffer_clear(&output);
1632 buffer_append(&output, child_state.output, child_state.olen);
1633 memset(child_state.output, 0, child_state.olen);
1634 xfree(child_state.output);
1635}
1636
1637static Kex *
1638mm_get_kex(Buffer *m)
1639{
1640 Kex *kex;
1641 void *blob;
1642 u_int bloblen;
1643
Damien Miller07d86be2006-03-26 14:19:21 +11001644 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001645 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001646 if ((session_id2 == NULL) ||
1647 (kex->session_id_len != session_id2_len) ||
1648 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1649 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001650 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001651 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001652 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001653 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001654 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001655 kex->server = 1;
1656 kex->hostkey_type = buffer_get_int(m);
1657 kex->kex_type = buffer_get_int(m);
1658 blob = buffer_get_string(m, &bloblen);
1659 buffer_init(&kex->my);
1660 buffer_append(&kex->my, blob, bloblen);
1661 xfree(blob);
1662 blob = buffer_get_string(m, &bloblen);
1663 buffer_init(&kex->peer);
1664 buffer_append(&kex->peer, blob, bloblen);
1665 xfree(blob);
1666 kex->done = 1;
1667 kex->flags = buffer_get_int(m);
1668 kex->client_version_string = buffer_get_string(m, NULL);
1669 kex->server_version_string = buffer_get_string(m, NULL);
1670 kex->load_host_key=&get_hostkey_by_type;
1671 kex->host_key_index=&get_hostkey_index;
1672
1673 return (kex);
1674}
1675
1676/* This function requries careful sanity checking */
1677
1678void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001679mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001680{
1681 Buffer m;
1682 u_char *blob, *p;
1683 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001684 u_int32_t seqnr, packets;
1685 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001686
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001687 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001688
1689 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001690 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001691 if (!compat20) {
1692 child_state.ssh1protoflags = buffer_get_int(&m);
1693 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001694 child_state.ssh1key = buffer_get_string(&m,
1695 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001696 child_state.ivout = buffer_get_string(&m,
1697 &child_state.ivoutlen);
1698 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1699 goto skip;
1700 } else {
1701 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001702 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001703 }
1704
1705 blob = buffer_get_string(&m, &bloblen);
1706 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1707 xfree(blob);
1708
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001709 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001710 blob = buffer_get_string(&m, &bloblen);
1711 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1712 xfree(blob);
1713
1714 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001715 seqnr = buffer_get_int(&m);
1716 blocks = buffer_get_int64(&m);
1717 packets = buffer_get_int(&m);
1718 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1719 seqnr = buffer_get_int(&m);
1720 blocks = buffer_get_int64(&m);
1721 packets = buffer_get_int(&m);
1722 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001723
1724 skip:
1725 /* Get the key context */
1726 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1727 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1728
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001729 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001730 /* Get compression state */
1731 p = buffer_get_string(&m, &plen);
1732 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001733 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001734 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1735 xfree(p);
1736
1737 p = buffer_get_string(&m, &plen);
1738 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001739 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001740 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1741 xfree(p);
1742
1743 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001744 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001745 child_state.input = buffer_get_string(&m, &child_state.ilen);
1746 child_state.output = buffer_get_string(&m, &child_state.olen);
1747
1748 buffer_free(&m);
1749}
1750
1751
1752/* Allocation functions for zlib */
1753void *
1754mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1755{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001756 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001757 void *address;
1758
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001759 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001760 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1761
1762 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001763
1764 return (address);
1765}
1766
1767void
1768mm_zfree(struct mm_master *mm, void *address)
1769{
1770 mm_free(mm, address);
1771}
1772
1773void
1774mm_init_compression(struct mm_master *mm)
1775{
1776 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1777 outgoing_stream.zfree = (free_func)mm_zfree;
1778 outgoing_stream.opaque = mm;
1779
1780 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1781 incoming_stream.zfree = (free_func)mm_zfree;
1782 incoming_stream.opaque = mm;
1783}
1784
1785/* XXX */
1786
1787#define FD_CLOSEONEXEC(x) do { \
1788 if (fcntl(x, F_SETFD, 1) == -1) \
1789 fatal("fcntl(%d, F_SETFD)", x); \
1790} while (0)
1791
1792static void
1793monitor_socketpair(int *pair)
1794{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001795#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001796 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001797 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001798#else
1799 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001800 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001801#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001802 FD_CLOSEONEXEC(pair[0]);
1803 FD_CLOSEONEXEC(pair[1]);
1804}
1805
1806#define MM_MEMSIZE 65536
1807
1808struct monitor *
1809monitor_init(void)
1810{
1811 struct monitor *mon;
1812 int pair[2];
1813
Damien Miller07d86be2006-03-26 14:19:21 +11001814 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001815
1816 monitor_socketpair(pair);
1817
1818 mon->m_recvfd = pair[0];
1819 mon->m_sendfd = pair[1];
1820
1821 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001822 if (options.compression) {
1823 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1824 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001825
Damien Miller2d6b8332002-06-21 15:59:49 +10001826 /* Compression needs to share state across borders */
1827 mm_init_compression(mon->m_zlib);
1828 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001829
1830 return mon;
1831}
1832
1833void
1834monitor_reinit(struct monitor *mon)
1835{
1836 int pair[2];
1837
1838 monitor_socketpair(pair);
1839
1840 mon->m_recvfd = pair[0];
1841 mon->m_sendfd = pair[1];
1842}
Darren Tucker0efd1552003-08-26 11:49:55 +10001843
1844#ifdef GSSAPI
1845int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001846mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001847{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001848 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001849 OM_uint32 major;
1850 u_int len;
1851
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001852 goid.elements = buffer_get_string(m, &len);
1853 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001854
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001855 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001856
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001857 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001858
1859 buffer_clear(m);
1860 buffer_put_int(m, major);
1861
Damien Miller6fd6def2005-11-05 15:07:05 +11001862 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001863
1864 /* Now we have a context, enable the step */
1865 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1866
1867 return (0);
1868}
1869
1870int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001871mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001872{
1873 gss_buffer_desc in;
1874 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001875 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001876 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001877 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001878
Darren Tucker600ad8d2003-08-26 12:10:48 +10001879 in.value = buffer_get_string(m, &len);
1880 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001881 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1882 xfree(in.value);
1883
1884 buffer_clear(m);
1885 buffer_put_int(m, major);
1886 buffer_put_string(m, out.value, out.length);
1887 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001888 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001889
1890 gss_release_buffer(&minor, &out);
1891
Damien Miller6fd6def2005-11-05 15:07:05 +11001892 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001893 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1894 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001895 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001896 }
1897 return (0);
1898}
1899
1900int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001901mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001902{
1903 gss_buffer_desc gssbuf, mic;
1904 OM_uint32 ret;
1905 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001906
Damien Miller0425d402003-11-17 22:18:21 +11001907 gssbuf.value = buffer_get_string(m, &len);
1908 gssbuf.length = len;
1909 mic.value = buffer_get_string(m, &len);
1910 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001911
Damien Miller0425d402003-11-17 22:18:21 +11001912 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001913
Damien Miller0425d402003-11-17 22:18:21 +11001914 xfree(gssbuf.value);
1915 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001916
Damien Miller0425d402003-11-17 22:18:21 +11001917 buffer_clear(m);
1918 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001919
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001920 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001921
Damien Miller0425d402003-11-17 22:18:21 +11001922 if (!GSS_ERROR(ret))
1923 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001924
Damien Miller0425d402003-11-17 22:18:21 +11001925 return (0);
1926}
1927
1928int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001929mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001930{
1931 int authenticated;
1932
1933 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1934
1935 buffer_clear(m);
1936 buffer_put_int(m, authenticated);
1937
1938 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001939 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001940
Damien Miller6fd6def2005-11-05 15:07:05 +11001941 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001942
1943 /* Monitor loop will terminate if authenticated */
1944 return (authenticated);
1945}
1946#endif /* GSSAPI */