blob: 04f6924b6bf177ea2788ef162dfb5077ce24c841 [file] [log] [blame]
Damien Millerb84886b2008-05-19 15:05:07 +10001/* $OpenBSD: monitor.c,v 1.95 2008/05/08 12:02:23 djm 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 Miller8dbffe72006-08-05 11:02:17 +100031#include <sys/param.h>
Damien Millere3b60b52006-07-10 21:08:03 +100032#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110034#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000035
Darren Tucker39972492006-07-12 22:22:46 +100036#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100037#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110039#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110040#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100041#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110042#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100043#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100044#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045#include <string.h>
Damien Miller607aede2006-09-01 15:48:19 +100046#include <unistd.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000047
48#ifdef SKEY
49#include <skey.h>
50#endif
51
Damien Miller03e20032006-03-15 11:16:59 +110052#include <openssl/dh.h>
53
Damien Millerb84886b2008-05-19 15:05:07 +100054#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000056#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100057#include "key.h"
58#include "buffer.h"
59#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000060#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100061#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000062#include "kex.h"
63#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000064#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
65#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000066#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000067#define TARGET_OS_MAC 1
68#else
69#include "zlib.h"
70#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000071#include "packet.h"
72#include "auth-options.h"
73#include "sshpty.h"
74#include "channels.h"
75#include "session.h"
76#include "sshlogin.h"
77#include "canohost.h"
78#include "log.h"
79#include "servconf.h"
80#include "monitor.h"
81#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100082#ifdef GSSAPI
83#include "ssh-gss.h"
84#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000085#include "monitor_wrap.h"
86#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000087#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088#include "compat.h"
89#include "ssh2.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000090
Darren Tucker0efd1552003-08-26 11:49:55 +100091#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +100092static Gssctxt *gsscontext = NULL;
93#endif
94
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095/* Imports */
96extern ServerOptions options;
97extern u_int utmp_len;
98extern Newkeys *current_keys[];
99extern z_stream incoming_stream;
100extern z_stream outgoing_stream;
101extern u_char session_id[];
102extern Buffer input, output;
103extern Buffer auth_debug;
104extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000105extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000106
107/* State exported from the child */
108
109struct {
110 z_stream incoming;
111 z_stream outgoing;
112 u_char *keyin;
113 u_int keyinlen;
114 u_char *keyout;
115 u_int keyoutlen;
116 u_char *ivin;
117 u_int ivinlen;
118 u_char *ivout;
119 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000120 u_char *ssh1key;
121 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000122 int ssh1cipher;
123 int ssh1protoflags;
124 u_char *input;
125 u_int ilen;
126 u_char *output;
127 u_int olen;
128} child_state;
129
Damien Miller469954d2003-06-18 20:25:33 +1000130/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131
132int mm_answer_moduli(int, Buffer *);
133int mm_answer_sign(int, Buffer *);
134int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000135int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000136int mm_answer_authserv(int, Buffer *);
137int mm_answer_authpassword(int, Buffer *);
138int mm_answer_bsdauthquery(int, Buffer *);
139int mm_answer_bsdauthrespond(int, Buffer *);
140int mm_answer_skeyquery(int, Buffer *);
141int mm_answer_skeyrespond(int, Buffer *);
142int mm_answer_keyallowed(int, Buffer *);
143int mm_answer_keyverify(int, Buffer *);
144int mm_answer_pty(int, Buffer *);
145int mm_answer_pty_cleanup(int, Buffer *);
146int mm_answer_term(int, Buffer *);
147int mm_answer_rsa_keyallowed(int, Buffer *);
148int mm_answer_rsa_challenge(int, Buffer *);
149int mm_answer_rsa_response(int, Buffer *);
150int mm_answer_sesskey(int, Buffer *);
151int mm_answer_sessid(int, Buffer *);
152
Damien Miller79418552002-04-23 20:28:48 +1000153#ifdef USE_PAM
154int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000155int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000156int mm_answer_pam_init_ctx(int, Buffer *);
157int mm_answer_pam_query(int, Buffer *);
158int mm_answer_pam_respond(int, Buffer *);
159int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000160#endif
161
Darren Tucker0efd1552003-08-26 11:49:55 +1000162#ifdef GSSAPI
163int mm_answer_gss_setup_ctx(int, Buffer *);
164int mm_answer_gss_accept_ctx(int, Buffer *);
165int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100166int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000167#endif
Damien Miller25162f22002-09-12 09:47:29 +1000168
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100169#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100170int mm_answer_audit_event(int, Buffer *);
171int mm_answer_audit_command(int, Buffer *);
172#endif
173
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000174static Authctxt *authctxt;
175static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
176
177/* local state for key verify */
178static u_char *key_blob = NULL;
179static u_int key_bloblen = 0;
180static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000181static char *hostbased_cuser = NULL;
182static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000183static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000184static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000185static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000186static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000187
188struct mon_table {
189 enum monitor_reqtype type;
190 int flags;
191 int (*f)(int, Buffer *);
192};
193
194#define MON_ISAUTH 0x0004 /* Required for Authentication */
195#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
196#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100197#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000198
199#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
200
201#define MON_PERMIT 0x1000 /* Request is permitted */
202
203struct mon_table mon_dispatch_proto20[] = {
204 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
205 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
206 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
207 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000208 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000209 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000210#ifdef USE_PAM
211 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000212 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000213 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
214 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
215 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
216 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000217#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100218#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100219 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100220#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221#ifdef BSD_AUTH
222 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100223 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224#endif
225#ifdef SKEY
226 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
227 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
228#endif
229 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
230 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000231#ifdef GSSAPI
232 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
233 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
234 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100235 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000236#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237 {0, 0, NULL}
238};
239
240struct mon_table mon_dispatch_postauth20[] = {
241 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
242 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
243 {MONITOR_REQ_PTY, 0, mm_answer_pty},
244 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
245 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100246#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100247 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
248 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
249#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000250 {0, 0, NULL}
251};
252
253struct mon_table mon_dispatch_proto15[] = {
254 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
255 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
256 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
257 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100258 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
259 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000260 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
261 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
262#ifdef BSD_AUTH
263 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100264 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000265#endif
266#ifdef SKEY
267 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
268 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
269#endif
Damien Millera33501b2002-05-08 12:24:42 +1000270#ifdef USE_PAM
271 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000272 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000273 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
274 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
275 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
276 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000277#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100278#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100279 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100280#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000281 {0, 0, NULL}
282};
283
284struct mon_table mon_dispatch_postauth15[] = {
285 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
286 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
287 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100288#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100289 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000290 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100291#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000292 {0, 0, NULL}
293};
294
295struct mon_table *mon_dispatch;
296
297/* Specifies if a certain message is allowed at the moment */
298
299static void
300monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
301{
302 while (ent->f != NULL) {
303 if (ent->type == type) {
304 ent->flags &= ~MON_PERMIT;
305 ent->flags |= permit ? MON_PERMIT : 0;
306 return;
307 }
308 ent++;
309 }
310}
311
312static void
313monitor_permit_authentications(int permit)
314{
315 struct mon_table *ent = mon_dispatch;
316
317 while (ent->f != NULL) {
318 if (ent->flags & MON_AUTH) {
319 ent->flags &= ~MON_PERMIT;
320 ent->flags |= permit ? MON_PERMIT : 0;
321 }
322 ent++;
323 }
324}
325
Darren Tucker3e33cec2003-10-02 16:12:36 +1000326void
327monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000328{
329 struct mon_table *ent;
330 int authenticated = 0;
331
332 debug3("preauth child monitor started");
333
Darren Tucker3e33cec2003-10-02 16:12:36 +1000334 authctxt = _authctxt;
335 memset(authctxt, 0, sizeof(*authctxt));
336
Darren Tuckerde0de392005-03-31 23:52:04 +1000337 authctxt->loginmsg = &loginmsg;
338
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339 if (compat20) {
340 mon_dispatch = mon_dispatch_proto20;
341
342 /* Permit requests for moduli and signatures */
343 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
344 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
345 } else {
346 mon_dispatch = mon_dispatch_proto15;
347
348 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
349 }
350
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000351 /* The first few requests do not require asynchronous access */
352 while (!authenticated) {
Damien Miller7a8f5b32006-03-31 23:14:23 +1100353 auth_method = "unknown";
Darren Tuckerfbba7352006-11-07 23:16:08 +1100354 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000355 if (authenticated) {
356 if (!(ent->flags & MON_AUTHDECIDE))
357 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000358 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000359 if (authctxt->pw->pw_uid == 0 &&
360 !auth_root_allowed(auth_method))
361 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000362#ifdef USE_PAM
363 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100364 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000365 Buffer m;
366
367 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100368 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000369 MONITOR_REQ_PAM_ACCOUNT, &m);
370 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
371 buffer_free(&m);
372 }
373#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000374 }
375
Damien Miller7a8f5b32006-03-31 23:14:23 +1100376 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000377 auth_log(authctxt, authenticated, auth_method,
378 compat20 ? " ssh2" : "");
379 if (!authenticated)
380 authctxt->failures++;
381 }
382 }
383
384 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000385 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100386 if (strcmp(auth_method, "unknown") == 0)
387 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388
389 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000390 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000391
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000392 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000393}
394
Damien Millerbe64d432003-05-14 19:31:12 +1000395static void
396monitor_set_child_handler(pid_t pid)
397{
398 monitor_child_pid = pid;
399}
400
401static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000402monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000403{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000404 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000405}
406
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000407void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000408monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409{
Damien Millerbe64d432003-05-14 19:31:12 +1000410 monitor_set_child_handler(pmonitor->m_pid);
411 signal(SIGHUP, &monitor_child_handler);
412 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000413 signal(SIGINT, &monitor_child_handler);
Damien Millerbe64d432003-05-14 19:31:12 +1000414
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000415 if (compat20) {
416 mon_dispatch = mon_dispatch_postauth20;
417
418 /* Permit requests for moduli and signatures */
419 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
420 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
421 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000422 } else {
423 mon_dispatch = mon_dispatch_postauth15;
424 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
425 }
426 if (!no_pty_flag) {
427 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
428 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
429 }
430
431 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000432 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000433}
434
435void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000436monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000437{
Damien Miller2d6b8332002-06-21 15:59:49 +1000438 if (options.compression) {
439 /* The member allocation is not visible, so sync it */
440 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
441 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000442}
443
444int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000445monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000446 struct mon_table **pent)
447{
448 Buffer m;
449 int ret;
450 u_char type;
451
452 buffer_init(&m);
453
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000454 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000455 type = buffer_get_char(&m);
456
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000457 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000458
459 while (ent->f != NULL) {
460 if (ent->type == type)
461 break;
462 ent++;
463 }
464
465 if (ent->f != NULL) {
466 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000467 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000468 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000469 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000470 buffer_free(&m);
471
472 /* The child may use this request only once, disable it */
473 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000474 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000475 type);
476 ent->flags &= ~MON_PERMIT;
477 }
478
479 if (pent != NULL)
480 *pent = ent;
481
482 return ret;
483 }
484
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000485 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000486
487 /* NOTREACHED */
488 return (-1);
489}
490
491/* allowed key state */
492static int
493monitor_allowed_key(u_char *blob, u_int bloblen)
494{
495 /* make sure key is allowed */
496 if (key_blob == NULL || key_bloblen != bloblen ||
497 memcmp(key_blob, blob, key_bloblen))
498 return (0);
499 return (1);
500}
501
502static void
503monitor_reset_key_state(void)
504{
505 /* reset state */
506 if (key_blob != NULL)
507 xfree(key_blob);
508 if (hostbased_cuser != NULL)
509 xfree(hostbased_cuser);
510 if (hostbased_chost != NULL)
511 xfree(hostbased_chost);
512 key_blob = NULL;
513 key_bloblen = 0;
514 key_blobtype = MM_NOKEY;
515 hostbased_cuser = NULL;
516 hostbased_chost = NULL;
517}
518
519int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000520mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000521{
522 DH *dh;
523 int min, want, max;
524
525 min = buffer_get_int(m);
526 want = buffer_get_int(m);
527 max = buffer_get_int(m);
528
529 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000530 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000531 /* We need to check here, too, in case the child got corrupted */
532 if (max < min || want < min || max < want)
533 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000534 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000535
536 buffer_clear(m);
537
538 dh = choose_dh(min, want, max);
539 if (dh == NULL) {
540 buffer_put_char(m, 0);
541 return (0);
542 } else {
543 /* Send first bignum */
544 buffer_put_char(m, 1);
545 buffer_put_bignum2(m, dh->p);
546 buffer_put_bignum2(m, dh->g);
547
548 DH_free(dh);
549 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000550 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551 return (0);
552}
553
554int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000555mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000556{
557 Key *key;
558 u_char *p;
559 u_char *signature;
560 u_int siglen, datlen;
561 int keyid;
562
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000563 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000564
565 keyid = buffer_get_int(m);
566 p = buffer_get_string(m, &datlen);
567
Damien Millera63128d2006-03-15 12:08:28 +1100568 /*
Damien Millerc91e5562006-03-26 13:58:55 +1100569 * Supported KEX types will only return SHA1 (20 byte) or
Damien Millera63128d2006-03-15 12:08:28 +1100570 * SHA256 (32 byte) hashes
571 */
572 if (datlen != 20 && datlen != 32)
Ben Lindstromd5502182002-06-27 00:12:57 +0000573 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000574
Ben Lindstromf67e0772002-06-06 20:58:19 +0000575 /* save session id, it will be passed on the first call */
576 if (session_id2_len == 0) {
577 session_id2_len = datlen;
578 session_id2 = xmalloc(session_id2_len);
579 memcpy(session_id2, p, session_id2_len);
580 }
581
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000583 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000584 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000585 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
Ben Lindstromd5502182002-06-27 00:12:57 +0000587 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000588
589 buffer_clear(m);
590 buffer_put_string(m, signature, siglen);
591
592 xfree(p);
593 xfree(signature);
594
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000595 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596
597 /* Turn on permissions for getpwnam */
598 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
599
600 return (0);
601}
602
603/* Retrieves the password entry and also checks if the user is permitted */
604
605int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000606mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000607{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000608 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609 struct passwd *pwent;
610 int allowed = 0;
611
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000612 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000613
614 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000615 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000616
Darren Tuckerb09b6772004-06-22 15:06:46 +1000617 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000618
Darren Tuckerb09b6772004-06-22 15:06:46 +1000619 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000620
Darren Tuckerb09b6772004-06-22 15:06:46 +1000621 authctxt->user = xstrdup(username);
622 setproctitle("%s [priv]", pwent ? username : "unknown");
623 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000624
625 buffer_clear(m);
626
627 if (pwent == NULL) {
628 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100629 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000630 goto out;
631 }
632
633 allowed = 1;
634 authctxt->pw = pwent;
635 authctxt->valid = 1;
636
637 buffer_put_char(m, 1);
638 buffer_put_string(m, pwent, sizeof(struct passwd));
639 buffer_put_cstring(m, pwent->pw_name);
640 buffer_put_cstring(m, "*");
641 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000642#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000643 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000644#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000645 buffer_put_cstring(m, pwent->pw_dir);
646 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100647
648 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100649 buffer_put_string(m, &options, sizeof(options));
650 if (options.banner != NULL)
651 buffer_put_cstring(m, options.banner);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000652 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000653 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000654
655 /* For SSHv1 allow authentication now */
656 if (!compat20)
657 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000658 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659 /* Allow service/style information on the auth context */
660 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000661 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
662 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663
Damien Millera33501b2002-05-08 12:24:42 +1000664#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000665 if (options.use_pam)
666 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000667#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000668
669 return (0);
670}
671
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000672int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000673{
674 char *banner;
675
676 buffer_clear(m);
677 banner = auth2_read_banner();
678 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000679 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000680
681 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000682 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000683
684 return (0);
685}
686
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000687int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000688mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000689{
690 monitor_permit_authentications(1);
691
692 authctxt->service = buffer_get_string(m, NULL);
693 authctxt->style = buffer_get_string(m, NULL);
694 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000695 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696
697 if (strlen(authctxt->style) == 0) {
698 xfree(authctxt->style);
699 authctxt->style = NULL;
700 }
701
702 return (0);
703}
704
705int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000706mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000707{
708 static int call_count;
709 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000710 int authenticated;
711 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712
713 passwd = buffer_get_string(m, &plen);
714 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000715 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000716 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000717 memset(passwd, 0, strlen(passwd));
718 xfree(passwd);
719
720 buffer_clear(m);
721 buffer_put_int(m, authenticated);
722
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000723 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000724 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725
726 call_count++;
727 if (plen == 0 && call_count == 1)
728 auth_method = "none";
729 else
730 auth_method = "password";
731
732 /* Causes monitor loop to terminate if authenticated */
733 return (authenticated);
734}
735
736#ifdef BSD_AUTH
737int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000738mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739{
740 char *name, *infotxt;
741 u_int numprompts;
742 u_int *echo_on;
743 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100744 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000745
Damien Millerb7df3af2003-02-24 11:55:46 +1100746 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
747 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748
749 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100750 buffer_put_int(m, success);
751 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000752 buffer_put_cstring(m, prompts[0]);
753
Damien Millerb7df3af2003-02-24 11:55:46 +1100754 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000755 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000756
Damien Millerb7df3af2003-02-24 11:55:46 +1100757 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000758 xfree(name);
759 xfree(infotxt);
760 xfree(prompts);
761 xfree(echo_on);
762 }
763
764 return (0);
765}
766
767int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000768mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000769{
770 char *response;
771 int authok;
772
773 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000774 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000775
776 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000777 authok = options.challenge_response_authentication &&
778 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000779 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000780 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000781 xfree(response);
782
783 buffer_clear(m);
784 buffer_put_int(m, authok);
785
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000786 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000787 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000788
789 auth_method = "bsdauth";
790
791 return (authok != 0);
792}
793#endif
794
795#ifdef SKEY
796int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000797mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000798{
799 struct skey skey;
800 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100801 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000802
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000803 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
804 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000805
806 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100807 buffer_put_int(m, success);
808 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000809 buffer_put_cstring(m, challenge);
810
Damien Millerb7df3af2003-02-24 11:55:46 +1100811 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000812 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000813
814 return (0);
815}
816
817int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000818mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000819{
820 char *response;
821 int authok;
822
823 response = buffer_get_string(m, NULL);
824
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000825 authok = (options.challenge_response_authentication &&
826 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000827 skey_haskey(authctxt->pw->pw_name) == 0 &&
828 skey_passcheck(authctxt->pw->pw_name, response) != -1);
829
830 xfree(response);
831
832 buffer_clear(m);
833 buffer_put_int(m, authok);
834
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000835 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000836 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000837
838 auth_method = "skey";
839
840 return (authok != 0);
841}
842#endif
843
Damien Miller79418552002-04-23 20:28:48 +1000844#ifdef USE_PAM
845int
Darren Tucker7eda5922004-06-22 13:22:50 +1000846mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000847{
Damien Miller4e448a32003-05-14 15:11:48 +1000848 if (!options.use_pam)
849 fatal("UsePAM not set, but ended up in %s anyway", __func__);
850
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100851 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000852
Damien Miller1f499fd2003-08-25 13:08:49 +1000853 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
854
Damien Miller79418552002-04-23 20:28:48 +1000855 return (0);
856}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000857
Damien Miller1f499fd2003-08-25 13:08:49 +1000858int
Darren Tucker7eda5922004-06-22 13:22:50 +1000859mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000860{
861 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100862
Damien Miller1f499fd2003-08-25 13:08:49 +1000863 if (!options.use_pam)
864 fatal("UsePAM not set, but ended up in %s anyway", __func__);
865
866 ret = do_pam_account();
867
868 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000869 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000870
Darren Tucker7eda5922004-06-22 13:22:50 +1000871 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000872
873 return (ret);
874}
875
Damien Miller4f9f42a2003-05-10 19:28:02 +1000876static void *sshpam_ctxt, *sshpam_authok;
877extern KbdintDevice sshpam_device;
878
879int
Darren Tucker7eda5922004-06-22 13:22:50 +1000880mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000881{
882
883 debug3("%s", __func__);
884 authctxt->user = buffer_get_string(m, NULL);
885 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
886 sshpam_authok = NULL;
887 buffer_clear(m);
888 if (sshpam_ctxt != NULL) {
889 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
890 buffer_put_int(m, 1);
891 } else {
892 buffer_put_int(m, 0);
893 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000894 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000895 return (0);
896}
897
898int
Darren Tucker7eda5922004-06-22 13:22:50 +1000899mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000900{
901 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000902 u_int i, num, *echo_on;
903 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000904
905 debug3("%s", __func__);
906 sshpam_authok = NULL;
907 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
908 if (ret == 0 && num == 0)
909 sshpam_authok = sshpam_ctxt;
910 if (num > 1 || name == NULL || info == NULL)
911 ret = -1;
912 buffer_clear(m);
913 buffer_put_int(m, ret);
914 buffer_put_cstring(m, name);
915 xfree(name);
916 buffer_put_cstring(m, info);
917 xfree(info);
918 buffer_put_int(m, num);
919 for (i = 0; i < num; ++i) {
920 buffer_put_cstring(m, prompts[i]);
921 xfree(prompts[i]);
922 buffer_put_int(m, echo_on[i]);
923 }
924 if (prompts != NULL)
925 xfree(prompts);
926 if (echo_on != NULL)
927 xfree(echo_on);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000928 auth_method = "keyboard-interactive/pam";
Darren Tucker7eda5922004-06-22 13:22:50 +1000929 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000930 return (0);
931}
932
933int
Darren Tucker7eda5922004-06-22 13:22:50 +1000934mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000935{
936 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000937 u_int i, num;
938 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000939
940 debug3("%s", __func__);
941 sshpam_authok = NULL;
942 num = buffer_get_int(m);
943 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +1000944 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000945 for (i = 0; i < num; ++i)
946 resp[i] = buffer_get_string(m, NULL);
947 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
948 for (i = 0; i < num; ++i)
949 xfree(resp[i]);
950 xfree(resp);
951 } else {
952 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
953 }
954 buffer_clear(m);
955 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000956 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000957 auth_method = "keyboard-interactive/pam";
958 if (ret == 0)
959 sshpam_authok = sshpam_ctxt;
960 return (0);
961}
962
963int
Darren Tucker7eda5922004-06-22 13:22:50 +1000964mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000965{
966
967 debug3("%s", __func__);
968 (sshpam_device.free_ctx)(sshpam_ctxt);
969 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000970 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000971 auth_method = "keyboard-interactive/pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000972 return (sshpam_authok == sshpam_ctxt);
973}
Damien Miller79418552002-04-23 20:28:48 +1000974#endif
975
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976static void
977mm_append_debug(Buffer *m)
978{
979 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000980 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981 buffer_append(m, buffer_ptr(&auth_debug),
982 buffer_len(&auth_debug));
983 buffer_clear(&auth_debug);
984 }
985}
986
987int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000988mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000989{
990 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000991 char *cuser, *chost;
992 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000993 u_int bloblen;
994 enum mm_keytype type = 0;
995 int allowed = 0;
996
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000997 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998
999 type = buffer_get_int(m);
1000 cuser = buffer_get_string(m, NULL);
1001 chost = buffer_get_string(m, NULL);
1002 blob = buffer_get_string(m, &bloblen);
1003
1004 key = key_from_blob(blob, bloblen);
1005
1006 if ((compat20 && type == MM_RSAHOSTKEY) ||
1007 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001008 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001009
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001010 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001011
Damien Miller3e3b5142003-11-17 21:13:40 +11001012 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001013 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001015 allowed = options.pubkey_authentication &&
1016 user_key_allowed(authctxt->pw, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001017 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001018 break;
1019 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001020 allowed = options.hostbased_authentication &&
1021 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001023 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001024 break;
1025 case MM_RSAHOSTKEY:
1026 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001027 allowed = options.rhosts_rsa_authentication &&
1028 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001029 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001030 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031 break;
1032 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001033 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001034 break;
1035 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001036 }
Damien Miller00111382003-03-10 11:21:17 +11001037 if (key != NULL)
1038 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001039
1040 /* clear temporarily storage (used by verify) */
1041 monitor_reset_key_state();
1042
1043 if (allowed) {
1044 /* Save temporarily for comparison in verify */
1045 key_blob = blob;
1046 key_bloblen = bloblen;
1047 key_blobtype = type;
1048 hostbased_cuser = cuser;
1049 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001050 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001051 /* Log failed attempt */
1052 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
Damien Miller96937bd2006-03-26 14:01:54 +11001053 xfree(blob);
1054 xfree(cuser);
1055 xfree(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001056 }
1057
1058 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001059 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001060
1061 buffer_clear(m);
1062 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001063 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064
1065 mm_append_debug(m);
1066
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001067 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001068
1069 if (type == MM_RSAHOSTKEY)
1070 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1071
1072 return (0);
1073}
1074
1075static int
1076monitor_valid_userblob(u_char *data, u_int datalen)
1077{
1078 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001079 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001080 u_int len;
1081 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001082
1083 buffer_init(&b);
1084 buffer_append(&b, data, datalen);
1085
1086 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001087 p = buffer_ptr(&b);
1088 len = buffer_len(&b);
1089 if ((session_id2 == NULL) ||
1090 (len < session_id2_len) ||
1091 (memcmp(p, session_id2, session_id2_len) != 0))
1092 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001093 buffer_consume(&b, session_id2_len);
1094 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001095 p = buffer_get_string(&b, &len);
1096 if ((session_id2 == NULL) ||
1097 (len != session_id2_len) ||
1098 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001099 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001100 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001101 }
1102 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1103 fail++;
1104 p = buffer_get_string(&b, NULL);
1105 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001106 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001107 authctxt->user, p);
1108 fail++;
1109 }
1110 xfree(p);
1111 buffer_skip_string(&b);
1112 if (datafellows & SSH_BUG_PKAUTH) {
1113 if (!buffer_get_char(&b))
1114 fail++;
1115 } else {
1116 p = buffer_get_string(&b, NULL);
1117 if (strcmp("publickey", p) != 0)
1118 fail++;
1119 xfree(p);
1120 if (!buffer_get_char(&b))
1121 fail++;
1122 buffer_skip_string(&b);
1123 }
1124 buffer_skip_string(&b);
1125 if (buffer_len(&b) != 0)
1126 fail++;
1127 buffer_free(&b);
1128 return (fail == 0);
1129}
1130
1131static int
Damien Millera10f5612002-09-12 09:49:15 +10001132monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1133 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001134{
1135 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001136 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001137 u_int len;
1138 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001139
1140 buffer_init(&b);
1141 buffer_append(&b, data, datalen);
1142
Ben Lindstromf67e0772002-06-06 20:58:19 +00001143 p = buffer_get_string(&b, &len);
1144 if ((session_id2 == NULL) ||
1145 (len != session_id2_len) ||
1146 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001147 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001148 xfree(p);
1149
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001150 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1151 fail++;
1152 p = buffer_get_string(&b, NULL);
1153 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001154 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001155 authctxt->user, p);
1156 fail++;
1157 }
1158 xfree(p);
1159 buffer_skip_string(&b); /* service */
1160 p = buffer_get_string(&b, NULL);
1161 if (strcmp(p, "hostbased") != 0)
1162 fail++;
1163 xfree(p);
1164 buffer_skip_string(&b); /* pkalg */
1165 buffer_skip_string(&b); /* pkblob */
1166
1167 /* verify client host, strip trailing dot if necessary */
1168 p = buffer_get_string(&b, NULL);
1169 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1170 p[len - 1] = '\0';
1171 if (strcmp(p, chost) != 0)
1172 fail++;
1173 xfree(p);
1174
1175 /* verify client user */
1176 p = buffer_get_string(&b, NULL);
1177 if (strcmp(p, cuser) != 0)
1178 fail++;
1179 xfree(p);
1180
1181 if (buffer_len(&b) != 0)
1182 fail++;
1183 buffer_free(&b);
1184 return (fail == 0);
1185}
1186
1187int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001188mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001189{
1190 Key *key;
1191 u_char *signature, *data, *blob;
1192 u_int signaturelen, datalen, bloblen;
1193 int verified = 0;
1194 int valid_data = 0;
1195
1196 blob = buffer_get_string(m, &bloblen);
1197 signature = buffer_get_string(m, &signaturelen);
1198 data = buffer_get_string(m, &datalen);
1199
1200 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001201 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001202 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001203
1204 key = key_from_blob(blob, bloblen);
1205 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001206 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001207
1208 switch (key_blobtype) {
1209 case MM_USERKEY:
1210 valid_data = monitor_valid_userblob(data, datalen);
1211 break;
1212 case MM_HOSTKEY:
1213 valid_data = monitor_valid_hostbasedblob(data, datalen,
1214 hostbased_cuser, hostbased_chost);
1215 break;
1216 default:
1217 valid_data = 0;
1218 break;
1219 }
1220 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001221 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001222
1223 verified = key_verify(key, signature, signaturelen, data, datalen);
1224 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001225 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001226
1227 key_free(key);
1228 xfree(blob);
1229 xfree(signature);
1230 xfree(data);
1231
Ben Lindstrome1c09122002-06-23 00:38:24 +00001232 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1233
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001234 monitor_reset_key_state();
1235
1236 buffer_clear(m);
1237 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001238 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001239
Darren Tuckerfbba7352006-11-07 23:16:08 +11001240 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001241}
1242
1243static void
1244mm_record_login(Session *s, struct passwd *pw)
1245{
1246 socklen_t fromlen;
1247 struct sockaddr_storage from;
1248
1249 /*
1250 * Get IP address of client. If the connection is not a socket, let
1251 * the address be 0.0.0.0.
1252 */
1253 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001254 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001255 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001256 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001257 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001258 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001259 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001260 }
1261 }
1262 /* Record that there was a login on that tty from the remote host. */
1263 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001264 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001265 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001266}
1267
1268static void
1269mm_session_close(Session *s)
1270{
Damien Miller04bd8b02003-05-25 14:38:33 +10001271 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001272 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001273 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001274 session_pty_cleanup2(s);
1275 }
1276 s->used = 0;
1277}
1278
1279int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001280mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001281{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001282 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001283 Session *s;
1284 int res, fd0;
1285
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001286 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001287
1288 buffer_clear(m);
1289 s = session_new();
1290 if (s == NULL)
1291 goto error;
1292 s->authctxt = authctxt;
1293 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001294 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1296 if (res == 0)
1297 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001298 pty_setowner(authctxt->pw, s->tty);
1299
1300 buffer_put_int(m, 1);
1301 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001302
1303 /* We need to trick ttyslot */
1304 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001305 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001306
1307 mm_record_login(s, authctxt->pw);
1308
1309 /* Now we can close the file descriptor again */
1310 close(0);
1311
Darren Tucker09991742004-07-17 17:05:14 +10001312 /* send messages generated by record_login */
1313 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1314 buffer_clear(&loginmsg);
1315
1316 mm_request_send(sock, MONITOR_ANS_PTY, m);
1317
Damien Miller54fd7cf2007-09-17 12:04:08 +10001318 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1319 mm_send_fd(sock, s->ttyfd) == -1)
1320 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001321
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001322 /* make sure nothing uses fd 0 */
1323 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001324 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001325 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001326 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001327
1328 /* slave is not needed */
1329 close(s->ttyfd);
1330 s->ttyfd = s->ptyfd;
1331 /* no need to dup() because nobody closes ptyfd */
1332 s->ptymaster = s->ptyfd;
1333
Damien Miller9ab00b42006-08-05 12:40:11 +10001334 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001335
1336 return (0);
1337
1338 error:
1339 if (s != NULL)
1340 mm_session_close(s);
1341 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001342 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001343 return (0);
1344}
1345
1346int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001347mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001348{
1349 Session *s;
1350 char *tty;
1351
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001352 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001353
1354 tty = buffer_get_string(m, NULL);
1355 if ((s = session_by_tty(tty)) != NULL)
1356 mm_session_close(s);
1357 buffer_clear(m);
1358 xfree(tty);
1359 return (0);
1360}
1361
1362int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001363mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001364{
1365 BIGNUM *p;
1366 int rsafail;
1367
1368 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001369 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001370
1371 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001372 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001373
1374 buffer_get_bignum2(m, p);
1375
1376 rsafail = ssh1_session_key(p);
1377
1378 buffer_clear(m);
1379 buffer_put_int(m, rsafail);
1380 buffer_put_bignum2(m, p);
1381
1382 BN_clear_free(p);
1383
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001384 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001385
1386 /* Turn on permissions for sessid passing */
1387 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1388
1389 return (0);
1390}
1391
1392int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001393mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001394{
1395 int i;
1396
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001397 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001398
1399 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001400 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001401 for (i = 0; i < 16; i++)
1402 session_id[i] = buffer_get_char(m);
1403
1404 /* Turn on permissions for getpwnam */
1405 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1406
1407 return (0);
1408}
1409
1410int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001411mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001412{
1413 BIGNUM *client_n;
1414 Key *key = NULL;
1415 u_char *blob = NULL;
1416 u_int blen = 0;
1417 int allowed = 0;
1418
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001419 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420
Damien Miller7a8f5b32006-03-31 23:14:23 +11001421 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001422 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001423 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001424 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001425 buffer_get_bignum2(m, client_n);
1426 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1427 BN_clear_free(client_n);
1428 }
1429 buffer_clear(m);
1430 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001431 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001432
1433 /* clear temporarily storage (used by generate challenge) */
1434 monitor_reset_key_state();
1435
1436 if (allowed && key != NULL) {
1437 key->type = KEY_RSA; /* cheat for key_to_blob */
1438 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001439 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440 buffer_put_string(m, blob, blen);
1441
1442 /* Save temporarily for comparison in verify */
1443 key_blob = blob;
1444 key_bloblen = blen;
1445 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446 }
Damien Miller00111382003-03-10 11:21:17 +11001447 if (key != NULL)
1448 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001449
1450 mm_append_debug(m);
1451
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001452 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001453
1454 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1455 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1456 return (0);
1457}
1458
1459int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001460mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001461{
1462 Key *key = NULL;
1463 u_char *blob;
1464 u_int blen;
1465
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001466 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001467
1468 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001469 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001470 blob = buffer_get_string(m, &blen);
1471 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001472 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001473 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001474 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001475 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001476 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001477
1478 if (ssh1_challenge)
1479 BN_clear_free(ssh1_challenge);
1480 ssh1_challenge = auth_rsa_generate_challenge(key);
1481
1482 buffer_clear(m);
1483 buffer_put_bignum2(m, ssh1_challenge);
1484
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001485 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001486 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001487
1488 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001489
1490 xfree(blob);
1491 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492 return (0);
1493}
1494
1495int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001496mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001497{
1498 Key *key = NULL;
1499 u_char *blob, *response;
1500 u_int blen, len;
1501 int success;
1502
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001503 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504
1505 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001506 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001507 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001508 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001509
1510 blob = buffer_get_string(m, &blen);
1511 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001512 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001513 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001514 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001515 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001516 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001517 response = buffer_get_string(m, &len);
1518 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001519 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001520 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1521
Damien Miller00111382003-03-10 11:21:17 +11001522 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001523 key_free(key);
1524 xfree(response);
1525
1526 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1527
1528 /* reset state */
1529 BN_clear_free(ssh1_challenge);
1530 ssh1_challenge = NULL;
1531 monitor_reset_key_state();
1532
1533 buffer_clear(m);
1534 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001535 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001536
1537 return (success);
1538}
1539
1540int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001541mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001542{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001543 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001544 int res, status;
1545
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001546 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001547
1548 /* The child is terminating */
1549 session_destroy_all(&mm_session_close);
1550
Darren Tucker52358d62008-03-11 22:58:25 +11001551#ifdef USE_PAM
1552 if (options.use_pam)
1553 sshpam_cleanup();
1554#endif
1555
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001556 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001557 if (errno != EINTR)
1558 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001559
1560 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1561
1562 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001563 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001564}
1565
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001566#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001567/* Report that an audit event occurred */
1568int
1569mm_answer_audit_event(int socket, Buffer *m)
1570{
1571 ssh_audit_event_t event;
1572
1573 debug3("%s entering", __func__);
1574
1575 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001576 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001577 case SSH_AUTH_FAIL_PUBKEY:
1578 case SSH_AUTH_FAIL_HOSTBASED:
1579 case SSH_AUTH_FAIL_GSSAPI:
1580 case SSH_LOGIN_EXCEED_MAXTRIES:
1581 case SSH_LOGIN_ROOT_DENIED:
1582 case SSH_CONNECTION_CLOSE:
1583 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001584 audit_event(event);
1585 break;
1586 default:
1587 fatal("Audit event type %d not permitted", event);
1588 }
1589
1590 return (0);
1591}
1592
1593int
1594mm_answer_audit_command(int socket, Buffer *m)
1595{
1596 u_int len;
1597 char *cmd;
1598
1599 debug3("%s entering", __func__);
1600 cmd = buffer_get_string(m, &len);
1601 /* sanity check command, if so how? */
1602 audit_run_command(cmd);
1603 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001604 return (0);
1605}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001606#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001607
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001608void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001609monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001610{
1611 if (compat20) {
1612 set_newkeys(MODE_IN);
1613 set_newkeys(MODE_OUT);
1614 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001615 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001616 packet_set_encryption_key(child_state.ssh1key,
1617 child_state.ssh1keylen, child_state.ssh1cipher);
1618 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001619 }
1620
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001621 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001622 packet_set_keycontext(MODE_OUT, child_state.keyout);
1623 xfree(child_state.keyout);
1624 packet_set_keycontext(MODE_IN, child_state.keyin);
1625 xfree(child_state.keyin);
1626
1627 if (!compat20) {
1628 packet_set_iv(MODE_OUT, child_state.ivout);
1629 xfree(child_state.ivout);
1630 packet_set_iv(MODE_IN, child_state.ivin);
1631 xfree(child_state.ivin);
1632 }
1633
1634 memcpy(&incoming_stream, &child_state.incoming,
1635 sizeof(incoming_stream));
1636 memcpy(&outgoing_stream, &child_state.outgoing,
1637 sizeof(outgoing_stream));
1638
1639 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001640 if (options.compression)
1641 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001642
1643 /* Network I/O buffers */
1644 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1645 buffer_clear(&input);
1646 buffer_append(&input, child_state.input, child_state.ilen);
1647 memset(child_state.input, 0, child_state.ilen);
1648 xfree(child_state.input);
1649
1650 buffer_clear(&output);
1651 buffer_append(&output, child_state.output, child_state.olen);
1652 memset(child_state.output, 0, child_state.olen);
1653 xfree(child_state.output);
1654}
1655
1656static Kex *
1657mm_get_kex(Buffer *m)
1658{
1659 Kex *kex;
1660 void *blob;
1661 u_int bloblen;
1662
Damien Miller07d86be2006-03-26 14:19:21 +11001663 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001664 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001665 if ((session_id2 == NULL) ||
1666 (kex->session_id_len != session_id2_len) ||
1667 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1668 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001669 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001670 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001671 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001672 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001673 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001674 kex->server = 1;
1675 kex->hostkey_type = buffer_get_int(m);
1676 kex->kex_type = buffer_get_int(m);
1677 blob = buffer_get_string(m, &bloblen);
1678 buffer_init(&kex->my);
1679 buffer_append(&kex->my, blob, bloblen);
1680 xfree(blob);
1681 blob = buffer_get_string(m, &bloblen);
1682 buffer_init(&kex->peer);
1683 buffer_append(&kex->peer, blob, bloblen);
1684 xfree(blob);
1685 kex->done = 1;
1686 kex->flags = buffer_get_int(m);
1687 kex->client_version_string = buffer_get_string(m, NULL);
1688 kex->server_version_string = buffer_get_string(m, NULL);
1689 kex->load_host_key=&get_hostkey_by_type;
1690 kex->host_key_index=&get_hostkey_index;
1691
1692 return (kex);
1693}
1694
1695/* This function requries careful sanity checking */
1696
1697void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001698mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001699{
1700 Buffer m;
1701 u_char *blob, *p;
1702 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001703 u_int32_t seqnr, packets;
1704 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001705
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001706 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001707
1708 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001709 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001710 if (!compat20) {
1711 child_state.ssh1protoflags = buffer_get_int(&m);
1712 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001713 child_state.ssh1key = buffer_get_string(&m,
1714 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001715 child_state.ivout = buffer_get_string(&m,
1716 &child_state.ivoutlen);
1717 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1718 goto skip;
1719 } else {
1720 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001721 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001722 }
1723
1724 blob = buffer_get_string(&m, &bloblen);
1725 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1726 xfree(blob);
1727
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001728 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001729 blob = buffer_get_string(&m, &bloblen);
1730 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1731 xfree(blob);
1732
1733 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001734 seqnr = buffer_get_int(&m);
1735 blocks = buffer_get_int64(&m);
1736 packets = buffer_get_int(&m);
1737 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1738 seqnr = buffer_get_int(&m);
1739 blocks = buffer_get_int64(&m);
1740 packets = buffer_get_int(&m);
1741 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001742
1743 skip:
1744 /* Get the key context */
1745 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1746 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1747
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001748 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001749 /* Get compression state */
1750 p = buffer_get_string(&m, &plen);
1751 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001752 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001753 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1754 xfree(p);
1755
1756 p = buffer_get_string(&m, &plen);
1757 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001758 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001759 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1760 xfree(p);
1761
1762 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001763 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001764 child_state.input = buffer_get_string(&m, &child_state.ilen);
1765 child_state.output = buffer_get_string(&m, &child_state.olen);
1766
1767 buffer_free(&m);
1768}
1769
1770
1771/* Allocation functions for zlib */
1772void *
1773mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1774{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001775 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001776 void *address;
1777
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001778 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001779 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1780
1781 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001782
1783 return (address);
1784}
1785
1786void
1787mm_zfree(struct mm_master *mm, void *address)
1788{
1789 mm_free(mm, address);
1790}
1791
1792void
1793mm_init_compression(struct mm_master *mm)
1794{
1795 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1796 outgoing_stream.zfree = (free_func)mm_zfree;
1797 outgoing_stream.opaque = mm;
1798
1799 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1800 incoming_stream.zfree = (free_func)mm_zfree;
1801 incoming_stream.opaque = mm;
1802}
1803
1804/* XXX */
1805
1806#define FD_CLOSEONEXEC(x) do { \
1807 if (fcntl(x, F_SETFD, 1) == -1) \
1808 fatal("fcntl(%d, F_SETFD)", x); \
1809} while (0)
1810
1811static void
1812monitor_socketpair(int *pair)
1813{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001814#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001815 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001816 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001817#else
1818 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001819 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001820#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001821 FD_CLOSEONEXEC(pair[0]);
1822 FD_CLOSEONEXEC(pair[1]);
1823}
1824
1825#define MM_MEMSIZE 65536
1826
1827struct monitor *
1828monitor_init(void)
1829{
1830 struct monitor *mon;
1831 int pair[2];
1832
Damien Miller07d86be2006-03-26 14:19:21 +11001833 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001834
1835 monitor_socketpair(pair);
1836
1837 mon->m_recvfd = pair[0];
1838 mon->m_sendfd = pair[1];
1839
1840 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001841 if (options.compression) {
1842 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1843 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001844
Damien Miller2d6b8332002-06-21 15:59:49 +10001845 /* Compression needs to share state across borders */
1846 mm_init_compression(mon->m_zlib);
1847 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001848
1849 return mon;
1850}
1851
1852void
1853monitor_reinit(struct monitor *mon)
1854{
1855 int pair[2];
1856
1857 monitor_socketpair(pair);
1858
1859 mon->m_recvfd = pair[0];
1860 mon->m_sendfd = pair[1];
1861}
Darren Tucker0efd1552003-08-26 11:49:55 +10001862
1863#ifdef GSSAPI
1864int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001865mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001866{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001867 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001868 OM_uint32 major;
1869 u_int len;
1870
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001871 goid.elements = buffer_get_string(m, &len);
1872 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001873
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001874 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001875
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001876 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001877
1878 buffer_clear(m);
1879 buffer_put_int(m, major);
1880
Damien Miller6fd6def2005-11-05 15:07:05 +11001881 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001882
1883 /* Now we have a context, enable the step */
1884 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1885
1886 return (0);
1887}
1888
1889int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001890mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001891{
1892 gss_buffer_desc in;
1893 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001894 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001895 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001896 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001897
Darren Tucker600ad8d2003-08-26 12:10:48 +10001898 in.value = buffer_get_string(m, &len);
1899 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001900 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1901 xfree(in.value);
1902
1903 buffer_clear(m);
1904 buffer_put_int(m, major);
1905 buffer_put_string(m, out.value, out.length);
1906 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001907 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001908
1909 gss_release_buffer(&minor, &out);
1910
Damien Miller6fd6def2005-11-05 15:07:05 +11001911 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001912 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1913 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001914 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001915 }
1916 return (0);
1917}
1918
1919int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001920mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001921{
1922 gss_buffer_desc gssbuf, mic;
1923 OM_uint32 ret;
1924 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001925
Damien Miller0425d402003-11-17 22:18:21 +11001926 gssbuf.value = buffer_get_string(m, &len);
1927 gssbuf.length = len;
1928 mic.value = buffer_get_string(m, &len);
1929 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001930
Damien Miller0425d402003-11-17 22:18:21 +11001931 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001932
Damien Miller0425d402003-11-17 22:18:21 +11001933 xfree(gssbuf.value);
1934 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001935
Damien Miller0425d402003-11-17 22:18:21 +11001936 buffer_clear(m);
1937 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001938
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001939 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001940
Damien Miller0425d402003-11-17 22:18:21 +11001941 if (!GSS_ERROR(ret))
1942 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001943
Damien Miller0425d402003-11-17 22:18:21 +11001944 return (0);
1945}
1946
1947int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001948mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001949{
1950 int authenticated;
1951
1952 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1953
1954 buffer_clear(m);
1955 buffer_put_int(m, authenticated);
1956
1957 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001958 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001959
Damien Miller6fd6def2005-11-05 15:07:05 +11001960 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001961
1962 /* Monitor loop will terminate if authenticated */
1963 return (authenticated);
1964}
1965#endif /* GSSAPI */