blob: b20d0c726a010435a1bbe843faa4c20452277fea [file] [log] [blame]
Damien Miller1c89ce02006-08-19 00:22:40 +10001/* $OpenBSD: monitor.c,v 1.88 2006/08/12 20:46:46 miod 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 Millerd7834352006-08-05 12:39:39 +100054#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000055#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100056#include "key.h"
57#include "buffer.h"
58#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000059#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100060#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000061#include "kex.h"
62#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000063#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
64#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000065#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000066#define TARGET_OS_MAC 1
67#else
68#include "zlib.h"
69#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "packet.h"
71#include "auth-options.h"
72#include "sshpty.h"
73#include "channels.h"
74#include "session.h"
75#include "sshlogin.h"
76#include "canohost.h"
77#include "log.h"
78#include "servconf.h"
79#include "monitor.h"
80#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100081#ifdef GSSAPI
82#include "ssh-gss.h"
83#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000084#include "monitor_wrap.h"
85#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000086#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000087#include "compat.h"
88#include "ssh2.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000089
Darren Tucker0efd1552003-08-26 11:49:55 +100090#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +100091static Gssctxt *gsscontext = NULL;
92#endif
93
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000094/* Imports */
95extern ServerOptions options;
96extern u_int utmp_len;
97extern Newkeys *current_keys[];
98extern z_stream incoming_stream;
99extern z_stream outgoing_stream;
100extern u_char session_id[];
101extern Buffer input, output;
102extern Buffer auth_debug;
103extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000104extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105
106/* State exported from the child */
107
108struct {
109 z_stream incoming;
110 z_stream outgoing;
111 u_char *keyin;
112 u_int keyinlen;
113 u_char *keyout;
114 u_int keyoutlen;
115 u_char *ivin;
116 u_int ivinlen;
117 u_char *ivout;
118 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000119 u_char *ssh1key;
120 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000121 int ssh1cipher;
122 int ssh1protoflags;
123 u_char *input;
124 u_int ilen;
125 u_char *output;
126 u_int olen;
127} child_state;
128
Damien Miller469954d2003-06-18 20:25:33 +1000129/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130
131int mm_answer_moduli(int, Buffer *);
132int mm_answer_sign(int, Buffer *);
133int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000134int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000135int mm_answer_authserv(int, Buffer *);
136int mm_answer_authpassword(int, Buffer *);
137int mm_answer_bsdauthquery(int, Buffer *);
138int mm_answer_bsdauthrespond(int, Buffer *);
139int mm_answer_skeyquery(int, Buffer *);
140int mm_answer_skeyrespond(int, Buffer *);
141int mm_answer_keyallowed(int, Buffer *);
142int mm_answer_keyverify(int, Buffer *);
143int mm_answer_pty(int, Buffer *);
144int mm_answer_pty_cleanup(int, Buffer *);
145int mm_answer_term(int, Buffer *);
146int mm_answer_rsa_keyallowed(int, Buffer *);
147int mm_answer_rsa_challenge(int, Buffer *);
148int mm_answer_rsa_response(int, Buffer *);
149int mm_answer_sesskey(int, Buffer *);
150int mm_answer_sessid(int, Buffer *);
151
Damien Miller79418552002-04-23 20:28:48 +1000152#ifdef USE_PAM
153int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000154int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000155int mm_answer_pam_init_ctx(int, Buffer *);
156int mm_answer_pam_query(int, Buffer *);
157int mm_answer_pam_respond(int, Buffer *);
158int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000159#endif
160
Darren Tucker0efd1552003-08-26 11:49:55 +1000161#ifdef GSSAPI
162int mm_answer_gss_setup_ctx(int, Buffer *);
163int mm_answer_gss_accept_ctx(int, Buffer *);
164int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100165int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000166#endif
Damien Miller25162f22002-09-12 09:47:29 +1000167
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100168#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100169int mm_answer_audit_event(int, Buffer *);
170int mm_answer_audit_command(int, Buffer *);
171#endif
172
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000173static Authctxt *authctxt;
174static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
175
176/* local state for key verify */
177static u_char *key_blob = NULL;
178static u_int key_bloblen = 0;
179static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000180static char *hostbased_cuser = NULL;
181static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000182static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000183static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000184static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000185static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000186
187struct mon_table {
188 enum monitor_reqtype type;
189 int flags;
190 int (*f)(int, Buffer *);
191};
192
193#define MON_ISAUTH 0x0004 /* Required for Authentication */
194#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
195#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100196#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000197
198#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
199
200#define MON_PERMIT 0x1000 /* Request is permitted */
201
202struct mon_table mon_dispatch_proto20[] = {
203 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
204 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
205 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
206 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000207 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000209#ifdef USE_PAM
210 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000211 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000212 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
213 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
214 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
215 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000216#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100217#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100218 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100219#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220#ifdef BSD_AUTH
221 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100222 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000223#endif
224#ifdef SKEY
225 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
226 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
227#endif
228 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
229 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000230#ifdef GSSAPI
231 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
232 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
233 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100234 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000235#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000236 {0, 0, NULL}
237};
238
239struct mon_table mon_dispatch_postauth20[] = {
240 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
241 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
242 {MONITOR_REQ_PTY, 0, mm_answer_pty},
243 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
244 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100245#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100246 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
247 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
248#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000249 {0, 0, NULL}
250};
251
252struct mon_table mon_dispatch_proto15[] = {
253 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
254 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
255 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
256 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100257 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
258 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000259 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
260 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
261#ifdef BSD_AUTH
262 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100263 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000264#endif
265#ifdef SKEY
266 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
267 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
268#endif
Damien Millera33501b2002-05-08 12:24:42 +1000269#ifdef USE_PAM
270 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000271 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000272 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
273 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
274 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
275 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000276#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100277#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100278 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
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_postauth15[] = {
284 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
285 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
286 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100287#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100288 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000289 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100290#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000291 {0, 0, NULL}
292};
293
294struct mon_table *mon_dispatch;
295
296/* Specifies if a certain message is allowed at the moment */
297
298static void
299monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
300{
301 while (ent->f != NULL) {
302 if (ent->type == type) {
303 ent->flags &= ~MON_PERMIT;
304 ent->flags |= permit ? MON_PERMIT : 0;
305 return;
306 }
307 ent++;
308 }
309}
310
311static void
312monitor_permit_authentications(int permit)
313{
314 struct mon_table *ent = mon_dispatch;
315
316 while (ent->f != NULL) {
317 if (ent->flags & MON_AUTH) {
318 ent->flags &= ~MON_PERMIT;
319 ent->flags |= permit ? MON_PERMIT : 0;
320 }
321 ent++;
322 }
323}
324
Darren Tucker3e33cec2003-10-02 16:12:36 +1000325void
326monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000327{
328 struct mon_table *ent;
329 int authenticated = 0;
330
331 debug3("preauth child monitor started");
332
Darren Tucker3e33cec2003-10-02 16:12:36 +1000333 authctxt = _authctxt;
334 memset(authctxt, 0, sizeof(*authctxt));
335
Darren Tuckerde0de392005-03-31 23:52:04 +1000336 authctxt->loginmsg = &loginmsg;
337
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000338 if (compat20) {
339 mon_dispatch = mon_dispatch_proto20;
340
341 /* Permit requests for moduli and signatures */
342 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
343 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
344 } else {
345 mon_dispatch = mon_dispatch_proto15;
346
347 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
348 }
349
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000350 /* The first few requests do not require asynchronous access */
351 while (!authenticated) {
Damien Miller7a8f5b32006-03-31 23:14:23 +1100352 auth_method = "unknown";
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000353 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000354 if (authenticated) {
355 if (!(ent->flags & MON_AUTHDECIDE))
356 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000357 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358 if (authctxt->pw->pw_uid == 0 &&
359 !auth_root_allowed(auth_method))
360 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000361#ifdef USE_PAM
362 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100363 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000364 Buffer m;
365
366 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100367 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000368 MONITOR_REQ_PAM_ACCOUNT, &m);
369 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
370 buffer_free(&m);
371 }
372#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000373 }
374
Damien Miller7a8f5b32006-03-31 23:14:23 +1100375 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000376 auth_log(authctxt, authenticated, auth_method,
377 compat20 ? " ssh2" : "");
378 if (!authenticated)
379 authctxt->failures++;
380 }
381 }
382
383 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000384 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100385 if (strcmp(auth_method, "unknown") == 0)
386 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000387
388 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000389 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000390
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000391 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000392}
393
Damien Millerbe64d432003-05-14 19:31:12 +1000394static void
395monitor_set_child_handler(pid_t pid)
396{
397 monitor_child_pid = pid;
398}
399
400static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000401monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000402{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000403 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000404}
405
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000407monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000408{
Damien Millerbe64d432003-05-14 19:31:12 +1000409 monitor_set_child_handler(pmonitor->m_pid);
410 signal(SIGHUP, &monitor_child_handler);
411 signal(SIGTERM, &monitor_child_handler);
412
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000413 if (compat20) {
414 mon_dispatch = mon_dispatch_postauth20;
415
416 /* Permit requests for moduli and signatures */
417 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
418 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
419 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000420 } else {
421 mon_dispatch = mon_dispatch_postauth15;
422 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
423 }
424 if (!no_pty_flag) {
425 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
426 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
427 }
428
429 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000430 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000431}
432
433void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000434monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000435{
Damien Miller2d6b8332002-06-21 15:59:49 +1000436 if (options.compression) {
437 /* The member allocation is not visible, so sync it */
438 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
439 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000440}
441
442int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000443monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000444 struct mon_table **pent)
445{
446 Buffer m;
447 int ret;
448 u_char type;
449
450 buffer_init(&m);
451
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000452 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000453 type = buffer_get_char(&m);
454
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000455 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000456
457 while (ent->f != NULL) {
458 if (ent->type == type)
459 break;
460 ent++;
461 }
462
463 if (ent->f != NULL) {
464 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000465 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000467 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000468 buffer_free(&m);
469
470 /* The child may use this request only once, disable it */
471 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000472 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000473 type);
474 ent->flags &= ~MON_PERMIT;
475 }
476
477 if (pent != NULL)
478 *pent = ent;
479
480 return ret;
481 }
482
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000483 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000484
485 /* NOTREACHED */
486 return (-1);
487}
488
489/* allowed key state */
490static int
491monitor_allowed_key(u_char *blob, u_int bloblen)
492{
493 /* make sure key is allowed */
494 if (key_blob == NULL || key_bloblen != bloblen ||
495 memcmp(key_blob, blob, key_bloblen))
496 return (0);
497 return (1);
498}
499
500static void
501monitor_reset_key_state(void)
502{
503 /* reset state */
504 if (key_blob != NULL)
505 xfree(key_blob);
506 if (hostbased_cuser != NULL)
507 xfree(hostbased_cuser);
508 if (hostbased_chost != NULL)
509 xfree(hostbased_chost);
510 key_blob = NULL;
511 key_bloblen = 0;
512 key_blobtype = MM_NOKEY;
513 hostbased_cuser = NULL;
514 hostbased_chost = NULL;
515}
516
517int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000518mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000519{
520 DH *dh;
521 int min, want, max;
522
523 min = buffer_get_int(m);
524 want = buffer_get_int(m);
525 max = buffer_get_int(m);
526
527 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000528 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000529 /* We need to check here, too, in case the child got corrupted */
530 if (max < min || want < min || max < want)
531 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000532 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000533
534 buffer_clear(m);
535
536 dh = choose_dh(min, want, max);
537 if (dh == NULL) {
538 buffer_put_char(m, 0);
539 return (0);
540 } else {
541 /* Send first bignum */
542 buffer_put_char(m, 1);
543 buffer_put_bignum2(m, dh->p);
544 buffer_put_bignum2(m, dh->g);
545
546 DH_free(dh);
547 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000548 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000549 return (0);
550}
551
552int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000553mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000554{
555 Key *key;
556 u_char *p;
557 u_char *signature;
558 u_int siglen, datlen;
559 int keyid;
560
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000561 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000562
563 keyid = buffer_get_int(m);
564 p = buffer_get_string(m, &datlen);
565
Damien Millera63128d2006-03-15 12:08:28 +1100566 /*
Damien Millerc91e5562006-03-26 13:58:55 +1100567 * Supported KEX types will only return SHA1 (20 byte) or
Damien Millera63128d2006-03-15 12:08:28 +1100568 * SHA256 (32 byte) hashes
569 */
570 if (datlen != 20 && datlen != 32)
Ben Lindstromd5502182002-06-27 00:12:57 +0000571 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000572
Ben Lindstromf67e0772002-06-06 20:58:19 +0000573 /* save session id, it will be passed on the first call */
574 if (session_id2_len == 0) {
575 session_id2_len = datlen;
576 session_id2 = xmalloc(session_id2_len);
577 memcpy(session_id2, p, session_id2_len);
578 }
579
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000580 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000581 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000583 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000584
Ben Lindstromd5502182002-06-27 00:12:57 +0000585 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
587 buffer_clear(m);
588 buffer_put_string(m, signature, siglen);
589
590 xfree(p);
591 xfree(signature);
592
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000593 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594
595 /* Turn on permissions for getpwnam */
596 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
597
598 return (0);
599}
600
601/* Retrieves the password entry and also checks if the user is permitted */
602
603int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000604mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000606 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000607 struct passwd *pwent;
608 int allowed = 0;
609
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000610 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000611
612 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000613 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614
Darren Tuckerb09b6772004-06-22 15:06:46 +1000615 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000616
Darren Tuckerb09b6772004-06-22 15:06:46 +1000617 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000618
Darren Tuckerb09b6772004-06-22 15:06:46 +1000619 authctxt->user = xstrdup(username);
620 setproctitle("%s [priv]", pwent ? username : "unknown");
621 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000622
623 buffer_clear(m);
624
625 if (pwent == NULL) {
626 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100627 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000628 goto out;
629 }
630
631 allowed = 1;
632 authctxt->pw = pwent;
633 authctxt->valid = 1;
634
635 buffer_put_char(m, 1);
636 buffer_put_string(m, pwent, sizeof(struct passwd));
637 buffer_put_cstring(m, pwent->pw_name);
638 buffer_put_cstring(m, "*");
639 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000640#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000641 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000642#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000643 buffer_put_cstring(m, pwent->pw_dir);
644 buffer_put_cstring(m, pwent->pw_shell);
645
646 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000647 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000648 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649
650 /* For SSHv1 allow authentication now */
651 if (!compat20)
652 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000653 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000654 /* Allow service/style information on the auth context */
655 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000656 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
657 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000658
Damien Millera33501b2002-05-08 12:24:42 +1000659#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000660 if (options.use_pam)
661 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000662#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663
664 return (0);
665}
666
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000667int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000668{
669 char *banner;
670
671 buffer_clear(m);
672 banner = auth2_read_banner();
673 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000674 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000675
676 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000677 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000678
679 return (0);
680}
681
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000683mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684{
685 monitor_permit_authentications(1);
686
687 authctxt->service = buffer_get_string(m, NULL);
688 authctxt->style = buffer_get_string(m, NULL);
689 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000690 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000691
692 if (strlen(authctxt->style) == 0) {
693 xfree(authctxt->style);
694 authctxt->style = NULL;
695 }
696
697 return (0);
698}
699
700int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000701mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000702{
703 static int call_count;
704 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000705 int authenticated;
706 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000707
708 passwd = buffer_get_string(m, &plen);
709 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000710 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000711 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712 memset(passwd, 0, strlen(passwd));
713 xfree(passwd);
714
715 buffer_clear(m);
716 buffer_put_int(m, authenticated);
717
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000718 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000719 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720
721 call_count++;
722 if (plen == 0 && call_count == 1)
723 auth_method = "none";
724 else
725 auth_method = "password";
726
727 /* Causes monitor loop to terminate if authenticated */
728 return (authenticated);
729}
730
731#ifdef BSD_AUTH
732int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000733mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000734{
735 char *name, *infotxt;
736 u_int numprompts;
737 u_int *echo_on;
738 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100739 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000740
Damien Millerb7df3af2003-02-24 11:55:46 +1100741 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
742 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743
744 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100745 buffer_put_int(m, success);
746 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000747 buffer_put_cstring(m, prompts[0]);
748
Damien Millerb7df3af2003-02-24 11:55:46 +1100749 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000750 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000751
Damien Millerb7df3af2003-02-24 11:55:46 +1100752 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753 xfree(name);
754 xfree(infotxt);
755 xfree(prompts);
756 xfree(echo_on);
757 }
758
759 return (0);
760}
761
762int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000763mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000764{
765 char *response;
766 int authok;
767
768 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000769 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000770
771 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000772 authok = options.challenge_response_authentication &&
773 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000774 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000775 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000776 xfree(response);
777
778 buffer_clear(m);
779 buffer_put_int(m, authok);
780
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000781 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000782 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000783
784 auth_method = "bsdauth";
785
786 return (authok != 0);
787}
788#endif
789
790#ifdef SKEY
791int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000792mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000793{
794 struct skey skey;
795 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100796 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000797
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000798 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
799 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000800
801 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100802 buffer_put_int(m, success);
803 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000804 buffer_put_cstring(m, challenge);
805
Damien Millerb7df3af2003-02-24 11:55:46 +1100806 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000807 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000808
809 return (0);
810}
811
812int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000813mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000814{
815 char *response;
816 int authok;
817
818 response = buffer_get_string(m, NULL);
819
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000820 authok = (options.challenge_response_authentication &&
821 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000822 skey_haskey(authctxt->pw->pw_name) == 0 &&
823 skey_passcheck(authctxt->pw->pw_name, response) != -1);
824
825 xfree(response);
826
827 buffer_clear(m);
828 buffer_put_int(m, authok);
829
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000830 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000831 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000832
833 auth_method = "skey";
834
835 return (authok != 0);
836}
837#endif
838
Damien Miller79418552002-04-23 20:28:48 +1000839#ifdef USE_PAM
840int
Darren Tucker7eda5922004-06-22 13:22:50 +1000841mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000842{
Damien Miller4e448a32003-05-14 15:11:48 +1000843 if (!options.use_pam)
844 fatal("UsePAM not set, but ended up in %s anyway", __func__);
845
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100846 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000847
Damien Miller1f499fd2003-08-25 13:08:49 +1000848 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
849
Damien Miller79418552002-04-23 20:28:48 +1000850 return (0);
851}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000852
Damien Miller1f499fd2003-08-25 13:08:49 +1000853int
Darren Tucker7eda5922004-06-22 13:22:50 +1000854mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000855{
856 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100857
Damien Miller1f499fd2003-08-25 13:08:49 +1000858 if (!options.use_pam)
859 fatal("UsePAM not set, but ended up in %s anyway", __func__);
860
861 ret = do_pam_account();
862
863 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000864 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000865
Darren Tucker7eda5922004-06-22 13:22:50 +1000866 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000867
868 return (ret);
869}
870
Damien Miller4f9f42a2003-05-10 19:28:02 +1000871static void *sshpam_ctxt, *sshpam_authok;
872extern KbdintDevice sshpam_device;
873
874int
Darren Tucker7eda5922004-06-22 13:22:50 +1000875mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000876{
877
878 debug3("%s", __func__);
879 authctxt->user = buffer_get_string(m, NULL);
880 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
881 sshpam_authok = NULL;
882 buffer_clear(m);
883 if (sshpam_ctxt != NULL) {
884 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
885 buffer_put_int(m, 1);
886 } else {
887 buffer_put_int(m, 0);
888 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000889 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000890 return (0);
891}
892
893int
Darren Tucker7eda5922004-06-22 13:22:50 +1000894mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000895{
896 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000897 u_int i, num, *echo_on;
898 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000899
900 debug3("%s", __func__);
901 sshpam_authok = NULL;
902 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
903 if (ret == 0 && num == 0)
904 sshpam_authok = sshpam_ctxt;
905 if (num > 1 || name == NULL || info == NULL)
906 ret = -1;
907 buffer_clear(m);
908 buffer_put_int(m, ret);
909 buffer_put_cstring(m, name);
910 xfree(name);
911 buffer_put_cstring(m, info);
912 xfree(info);
913 buffer_put_int(m, num);
914 for (i = 0; i < num; ++i) {
915 buffer_put_cstring(m, prompts[i]);
916 xfree(prompts[i]);
917 buffer_put_int(m, echo_on[i]);
918 }
919 if (prompts != NULL)
920 xfree(prompts);
921 if (echo_on != NULL)
922 xfree(echo_on);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000923 auth_method = "keyboard-interactive/pam";
Darren Tucker7eda5922004-06-22 13:22:50 +1000924 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000925 return (0);
926}
927
928int
Darren Tucker7eda5922004-06-22 13:22:50 +1000929mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000930{
931 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000932 u_int i, num;
933 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000934
935 debug3("%s", __func__);
936 sshpam_authok = NULL;
937 num = buffer_get_int(m);
938 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +1000939 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000940 for (i = 0; i < num; ++i)
941 resp[i] = buffer_get_string(m, NULL);
942 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
943 for (i = 0; i < num; ++i)
944 xfree(resp[i]);
945 xfree(resp);
946 } else {
947 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
948 }
949 buffer_clear(m);
950 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000951 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000952 auth_method = "keyboard-interactive/pam";
953 if (ret == 0)
954 sshpam_authok = sshpam_ctxt;
955 return (0);
956}
957
958int
Darren Tucker7eda5922004-06-22 13:22:50 +1000959mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000960{
961
962 debug3("%s", __func__);
963 (sshpam_device.free_ctx)(sshpam_ctxt);
964 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000965 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000966 auth_method = "keyboard-interactive/pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000967 return (sshpam_authok == sshpam_ctxt);
968}
Damien Miller79418552002-04-23 20:28:48 +1000969#endif
970
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000971static void
972mm_append_debug(Buffer *m)
973{
974 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000975 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976 buffer_append(m, buffer_ptr(&auth_debug),
977 buffer_len(&auth_debug));
978 buffer_clear(&auth_debug);
979 }
980}
981
982int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000983mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984{
985 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000986 char *cuser, *chost;
987 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000988 u_int bloblen;
989 enum mm_keytype type = 0;
990 int allowed = 0;
991
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000992 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000993
994 type = buffer_get_int(m);
995 cuser = buffer_get_string(m, NULL);
996 chost = buffer_get_string(m, NULL);
997 blob = buffer_get_string(m, &bloblen);
998
999 key = key_from_blob(blob, bloblen);
1000
1001 if ((compat20 && type == MM_RSAHOSTKEY) ||
1002 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001003 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001004
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001005 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001006
Damien Miller3e3b5142003-11-17 21:13:40 +11001007 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001008 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001009 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001010 allowed = options.pubkey_authentication &&
1011 user_key_allowed(authctxt->pw, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001012 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001013 break;
1014 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001015 allowed = options.hostbased_authentication &&
1016 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001017 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001018 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001019 break;
1020 case MM_RSAHOSTKEY:
1021 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001022 allowed = options.rhosts_rsa_authentication &&
1023 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001024 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001025 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001026 break;
1027 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001028 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001029 break;
1030 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031 }
Damien Miller00111382003-03-10 11:21:17 +11001032 if (key != NULL)
1033 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001034
1035 /* clear temporarily storage (used by verify) */
1036 monitor_reset_key_state();
1037
1038 if (allowed) {
1039 /* Save temporarily for comparison in verify */
1040 key_blob = blob;
1041 key_bloblen = bloblen;
1042 key_blobtype = type;
1043 hostbased_cuser = cuser;
1044 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001045 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001046 /* Log failed attempt */
1047 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
Damien Miller96937bd2006-03-26 14:01:54 +11001048 xfree(blob);
1049 xfree(cuser);
1050 xfree(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001051 }
1052
1053 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001054 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001055
1056 buffer_clear(m);
1057 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001058 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001059
1060 mm_append_debug(m);
1061
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001062 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001063
1064 if (type == MM_RSAHOSTKEY)
1065 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1066
1067 return (0);
1068}
1069
1070static int
1071monitor_valid_userblob(u_char *data, u_int datalen)
1072{
1073 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001074 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001075 u_int len;
1076 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001077
1078 buffer_init(&b);
1079 buffer_append(&b, data, datalen);
1080
1081 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001082 p = buffer_ptr(&b);
1083 len = buffer_len(&b);
1084 if ((session_id2 == NULL) ||
1085 (len < session_id2_len) ||
1086 (memcmp(p, session_id2, session_id2_len) != 0))
1087 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001088 buffer_consume(&b, session_id2_len);
1089 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001090 p = buffer_get_string(&b, &len);
1091 if ((session_id2 == NULL) ||
1092 (len != session_id2_len) ||
1093 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001094 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001095 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001096 }
1097 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1098 fail++;
1099 p = buffer_get_string(&b, NULL);
1100 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001101 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001102 authctxt->user, p);
1103 fail++;
1104 }
1105 xfree(p);
1106 buffer_skip_string(&b);
1107 if (datafellows & SSH_BUG_PKAUTH) {
1108 if (!buffer_get_char(&b))
1109 fail++;
1110 } else {
1111 p = buffer_get_string(&b, NULL);
1112 if (strcmp("publickey", p) != 0)
1113 fail++;
1114 xfree(p);
1115 if (!buffer_get_char(&b))
1116 fail++;
1117 buffer_skip_string(&b);
1118 }
1119 buffer_skip_string(&b);
1120 if (buffer_len(&b) != 0)
1121 fail++;
1122 buffer_free(&b);
1123 return (fail == 0);
1124}
1125
1126static int
Damien Millera10f5612002-09-12 09:49:15 +10001127monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1128 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001129{
1130 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001131 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001132 u_int len;
1133 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001134
1135 buffer_init(&b);
1136 buffer_append(&b, data, datalen);
1137
Ben Lindstromf67e0772002-06-06 20:58:19 +00001138 p = buffer_get_string(&b, &len);
1139 if ((session_id2 == NULL) ||
1140 (len != session_id2_len) ||
1141 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001142 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001143 xfree(p);
1144
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001145 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1146 fail++;
1147 p = buffer_get_string(&b, NULL);
1148 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001149 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001150 authctxt->user, p);
1151 fail++;
1152 }
1153 xfree(p);
1154 buffer_skip_string(&b); /* service */
1155 p = buffer_get_string(&b, NULL);
1156 if (strcmp(p, "hostbased") != 0)
1157 fail++;
1158 xfree(p);
1159 buffer_skip_string(&b); /* pkalg */
1160 buffer_skip_string(&b); /* pkblob */
1161
1162 /* verify client host, strip trailing dot if necessary */
1163 p = buffer_get_string(&b, NULL);
1164 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1165 p[len - 1] = '\0';
1166 if (strcmp(p, chost) != 0)
1167 fail++;
1168 xfree(p);
1169
1170 /* verify client user */
1171 p = buffer_get_string(&b, NULL);
1172 if (strcmp(p, cuser) != 0)
1173 fail++;
1174 xfree(p);
1175
1176 if (buffer_len(&b) != 0)
1177 fail++;
1178 buffer_free(&b);
1179 return (fail == 0);
1180}
1181
1182int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001183mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001184{
1185 Key *key;
1186 u_char *signature, *data, *blob;
1187 u_int signaturelen, datalen, bloblen;
1188 int verified = 0;
1189 int valid_data = 0;
1190
1191 blob = buffer_get_string(m, &bloblen);
1192 signature = buffer_get_string(m, &signaturelen);
1193 data = buffer_get_string(m, &datalen);
1194
1195 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001196 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001197 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001198
1199 key = key_from_blob(blob, bloblen);
1200 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001201 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202
1203 switch (key_blobtype) {
1204 case MM_USERKEY:
1205 valid_data = monitor_valid_userblob(data, datalen);
1206 break;
1207 case MM_HOSTKEY:
1208 valid_data = monitor_valid_hostbasedblob(data, datalen,
1209 hostbased_cuser, hostbased_chost);
1210 break;
1211 default:
1212 valid_data = 0;
1213 break;
1214 }
1215 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001216 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001217
1218 verified = key_verify(key, signature, signaturelen, data, datalen);
1219 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001220 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001221
1222 key_free(key);
1223 xfree(blob);
1224 xfree(signature);
1225 xfree(data);
1226
Ben Lindstrome1c09122002-06-23 00:38:24 +00001227 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1228
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001229 monitor_reset_key_state();
1230
1231 buffer_clear(m);
1232 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001233 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001234
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001235 return (verified);
1236}
1237
1238static void
1239mm_record_login(Session *s, struct passwd *pw)
1240{
1241 socklen_t fromlen;
1242 struct sockaddr_storage from;
1243
1244 /*
1245 * Get IP address of client. If the connection is not a socket, let
1246 * the address be 0.0.0.0.
1247 */
1248 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001249 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001250 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001251 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001252 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001253 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001254 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001255 }
1256 }
1257 /* Record that there was a login on that tty from the remote host. */
1258 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001259 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001260 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261}
1262
1263static void
1264mm_session_close(Session *s)
1265{
Damien Miller04bd8b02003-05-25 14:38:33 +10001266 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001267 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001268 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001269 session_pty_cleanup2(s);
1270 }
1271 s->used = 0;
1272}
1273
1274int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001275mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001276{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001277 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001278 Session *s;
1279 int res, fd0;
1280
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001281 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282
1283 buffer_clear(m);
1284 s = session_new();
1285 if (s == NULL)
1286 goto error;
1287 s->authctxt = authctxt;
1288 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001289 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001290 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1291 if (res == 0)
1292 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293 pty_setowner(authctxt->pw, s->tty);
1294
1295 buffer_put_int(m, 1);
1296 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001297
1298 /* We need to trick ttyslot */
1299 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001300 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001301
1302 mm_record_login(s, authctxt->pw);
1303
1304 /* Now we can close the file descriptor again */
1305 close(0);
1306
Darren Tucker09991742004-07-17 17:05:14 +10001307 /* send messages generated by record_login */
1308 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1309 buffer_clear(&loginmsg);
1310
1311 mm_request_send(sock, MONITOR_ANS_PTY, m);
1312
1313 mm_send_fd(sock, s->ptyfd);
1314 mm_send_fd(sock, s->ttyfd);
1315
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001316 /* make sure nothing uses fd 0 */
1317 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001318 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001319 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001320 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001321
1322 /* slave is not needed */
1323 close(s->ttyfd);
1324 s->ttyfd = s->ptyfd;
1325 /* no need to dup() because nobody closes ptyfd */
1326 s->ptymaster = s->ptyfd;
1327
Damien Miller9ab00b42006-08-05 12:40:11 +10001328 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329
1330 return (0);
1331
1332 error:
1333 if (s != NULL)
1334 mm_session_close(s);
1335 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001336 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001337 return (0);
1338}
1339
1340int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001341mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001342{
1343 Session *s;
1344 char *tty;
1345
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001346 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001347
1348 tty = buffer_get_string(m, NULL);
1349 if ((s = session_by_tty(tty)) != NULL)
1350 mm_session_close(s);
1351 buffer_clear(m);
1352 xfree(tty);
1353 return (0);
1354}
1355
1356int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001357mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001358{
1359 BIGNUM *p;
1360 int rsafail;
1361
1362 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001363 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001364
1365 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001366 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001367
1368 buffer_get_bignum2(m, p);
1369
1370 rsafail = ssh1_session_key(p);
1371
1372 buffer_clear(m);
1373 buffer_put_int(m, rsafail);
1374 buffer_put_bignum2(m, p);
1375
1376 BN_clear_free(p);
1377
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001378 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001379
1380 /* Turn on permissions for sessid passing */
1381 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1382
1383 return (0);
1384}
1385
1386int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001387mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001388{
1389 int i;
1390
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001391 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001392
1393 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001394 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001395 for (i = 0; i < 16; i++)
1396 session_id[i] = buffer_get_char(m);
1397
1398 /* Turn on permissions for getpwnam */
1399 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1400
1401 return (0);
1402}
1403
1404int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001405mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001406{
1407 BIGNUM *client_n;
1408 Key *key = NULL;
1409 u_char *blob = NULL;
1410 u_int blen = 0;
1411 int allowed = 0;
1412
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001413 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001414
Damien Miller7a8f5b32006-03-31 23:14:23 +11001415 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001416 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001417 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001418 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001419 buffer_get_bignum2(m, client_n);
1420 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1421 BN_clear_free(client_n);
1422 }
1423 buffer_clear(m);
1424 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001425 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001426
1427 /* clear temporarily storage (used by generate challenge) */
1428 monitor_reset_key_state();
1429
1430 if (allowed && key != NULL) {
1431 key->type = KEY_RSA; /* cheat for key_to_blob */
1432 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001433 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434 buffer_put_string(m, blob, blen);
1435
1436 /* Save temporarily for comparison in verify */
1437 key_blob = blob;
1438 key_bloblen = blen;
1439 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440 }
Damien Miller00111382003-03-10 11:21:17 +11001441 if (key != NULL)
1442 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001443
1444 mm_append_debug(m);
1445
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001446 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447
1448 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1449 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1450 return (0);
1451}
1452
1453int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001454mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001455{
1456 Key *key = NULL;
1457 u_char *blob;
1458 u_int blen;
1459
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001460 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001461
1462 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001463 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001464 blob = buffer_get_string(m, &blen);
1465 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001466 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001467 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001468 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001469 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001470 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001471
1472 if (ssh1_challenge)
1473 BN_clear_free(ssh1_challenge);
1474 ssh1_challenge = auth_rsa_generate_challenge(key);
1475
1476 buffer_clear(m);
1477 buffer_put_bignum2(m, ssh1_challenge);
1478
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001479 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001480 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481
1482 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001483
1484 xfree(blob);
1485 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486 return (0);
1487}
1488
1489int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001490mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001491{
1492 Key *key = NULL;
1493 u_char *blob, *response;
1494 u_int blen, len;
1495 int success;
1496
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001497 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001498
1499 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001500 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001501 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001502 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001503
1504 blob = buffer_get_string(m, &blen);
1505 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001506 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001507 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001508 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001509 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001510 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001511 response = buffer_get_string(m, &len);
1512 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001513 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1515
Damien Miller00111382003-03-10 11:21:17 +11001516 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001517 key_free(key);
1518 xfree(response);
1519
1520 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1521
1522 /* reset state */
1523 BN_clear_free(ssh1_challenge);
1524 ssh1_challenge = NULL;
1525 monitor_reset_key_state();
1526
1527 buffer_clear(m);
1528 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001529 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001530
1531 return (success);
1532}
1533
1534int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001535mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001536{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001537 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001538 int res, status;
1539
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001540 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001541
1542 /* The child is terminating */
1543 session_destroy_all(&mm_session_close);
1544
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001545 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001546 if (errno != EINTR)
1547 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001548
1549 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1550
1551 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001552 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001553}
1554
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001555#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001556/* Report that an audit event occurred */
1557int
1558mm_answer_audit_event(int socket, Buffer *m)
1559{
1560 ssh_audit_event_t event;
1561
1562 debug3("%s entering", __func__);
1563
1564 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001565 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001566 case SSH_AUTH_FAIL_PUBKEY:
1567 case SSH_AUTH_FAIL_HOSTBASED:
1568 case SSH_AUTH_FAIL_GSSAPI:
1569 case SSH_LOGIN_EXCEED_MAXTRIES:
1570 case SSH_LOGIN_ROOT_DENIED:
1571 case SSH_CONNECTION_CLOSE:
1572 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001573 audit_event(event);
1574 break;
1575 default:
1576 fatal("Audit event type %d not permitted", event);
1577 }
1578
1579 return (0);
1580}
1581
1582int
1583mm_answer_audit_command(int socket, Buffer *m)
1584{
1585 u_int len;
1586 char *cmd;
1587
1588 debug3("%s entering", __func__);
1589 cmd = buffer_get_string(m, &len);
1590 /* sanity check command, if so how? */
1591 audit_run_command(cmd);
1592 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001593 return (0);
1594}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001595#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001596
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001597void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001598monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001599{
1600 if (compat20) {
1601 set_newkeys(MODE_IN);
1602 set_newkeys(MODE_OUT);
1603 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001604 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001605 packet_set_encryption_key(child_state.ssh1key,
1606 child_state.ssh1keylen, child_state.ssh1cipher);
1607 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001608 }
1609
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001610 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001611 packet_set_keycontext(MODE_OUT, child_state.keyout);
1612 xfree(child_state.keyout);
1613 packet_set_keycontext(MODE_IN, child_state.keyin);
1614 xfree(child_state.keyin);
1615
1616 if (!compat20) {
1617 packet_set_iv(MODE_OUT, child_state.ivout);
1618 xfree(child_state.ivout);
1619 packet_set_iv(MODE_IN, child_state.ivin);
1620 xfree(child_state.ivin);
1621 }
1622
1623 memcpy(&incoming_stream, &child_state.incoming,
1624 sizeof(incoming_stream));
1625 memcpy(&outgoing_stream, &child_state.outgoing,
1626 sizeof(outgoing_stream));
1627
1628 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001629 if (options.compression)
1630 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001631
1632 /* Network I/O buffers */
1633 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1634 buffer_clear(&input);
1635 buffer_append(&input, child_state.input, child_state.ilen);
1636 memset(child_state.input, 0, child_state.ilen);
1637 xfree(child_state.input);
1638
1639 buffer_clear(&output);
1640 buffer_append(&output, child_state.output, child_state.olen);
1641 memset(child_state.output, 0, child_state.olen);
1642 xfree(child_state.output);
1643}
1644
1645static Kex *
1646mm_get_kex(Buffer *m)
1647{
1648 Kex *kex;
1649 void *blob;
1650 u_int bloblen;
1651
Damien Miller07d86be2006-03-26 14:19:21 +11001652 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001653 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001654 if ((session_id2 == NULL) ||
1655 (kex->session_id_len != session_id2_len) ||
1656 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1657 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001658 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001659 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001660 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001661 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001662 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001663 kex->server = 1;
1664 kex->hostkey_type = buffer_get_int(m);
1665 kex->kex_type = buffer_get_int(m);
1666 blob = buffer_get_string(m, &bloblen);
1667 buffer_init(&kex->my);
1668 buffer_append(&kex->my, blob, bloblen);
1669 xfree(blob);
1670 blob = buffer_get_string(m, &bloblen);
1671 buffer_init(&kex->peer);
1672 buffer_append(&kex->peer, blob, bloblen);
1673 xfree(blob);
1674 kex->done = 1;
1675 kex->flags = buffer_get_int(m);
1676 kex->client_version_string = buffer_get_string(m, NULL);
1677 kex->server_version_string = buffer_get_string(m, NULL);
1678 kex->load_host_key=&get_hostkey_by_type;
1679 kex->host_key_index=&get_hostkey_index;
1680
1681 return (kex);
1682}
1683
1684/* This function requries careful sanity checking */
1685
1686void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001687mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001688{
1689 Buffer m;
1690 u_char *blob, *p;
1691 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001692 u_int32_t seqnr, packets;
1693 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001694
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001695 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001696
1697 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001698 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001699 if (!compat20) {
1700 child_state.ssh1protoflags = buffer_get_int(&m);
1701 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001702 child_state.ssh1key = buffer_get_string(&m,
1703 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001704 child_state.ivout = buffer_get_string(&m,
1705 &child_state.ivoutlen);
1706 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1707 goto skip;
1708 } else {
1709 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001710 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001711 }
1712
1713 blob = buffer_get_string(&m, &bloblen);
1714 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1715 xfree(blob);
1716
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001717 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001718 blob = buffer_get_string(&m, &bloblen);
1719 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1720 xfree(blob);
1721
1722 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001723 seqnr = buffer_get_int(&m);
1724 blocks = buffer_get_int64(&m);
1725 packets = buffer_get_int(&m);
1726 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1727 seqnr = buffer_get_int(&m);
1728 blocks = buffer_get_int64(&m);
1729 packets = buffer_get_int(&m);
1730 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001731
1732 skip:
1733 /* Get the key context */
1734 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1735 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1736
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001737 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001738 /* Get compression state */
1739 p = buffer_get_string(&m, &plen);
1740 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001741 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001742 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1743 xfree(p);
1744
1745 p = buffer_get_string(&m, &plen);
1746 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001747 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001748 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1749 xfree(p);
1750
1751 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001752 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001753 child_state.input = buffer_get_string(&m, &child_state.ilen);
1754 child_state.output = buffer_get_string(&m, &child_state.olen);
1755
1756 buffer_free(&m);
1757}
1758
1759
1760/* Allocation functions for zlib */
1761void *
1762mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1763{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001764 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001765 void *address;
1766
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001767 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001768 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1769
1770 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001771
1772 return (address);
1773}
1774
1775void
1776mm_zfree(struct mm_master *mm, void *address)
1777{
1778 mm_free(mm, address);
1779}
1780
1781void
1782mm_init_compression(struct mm_master *mm)
1783{
1784 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1785 outgoing_stream.zfree = (free_func)mm_zfree;
1786 outgoing_stream.opaque = mm;
1787
1788 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1789 incoming_stream.zfree = (free_func)mm_zfree;
1790 incoming_stream.opaque = mm;
1791}
1792
1793/* XXX */
1794
1795#define FD_CLOSEONEXEC(x) do { \
1796 if (fcntl(x, F_SETFD, 1) == -1) \
1797 fatal("fcntl(%d, F_SETFD)", x); \
1798} while (0)
1799
1800static void
1801monitor_socketpair(int *pair)
1802{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001803#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001804 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001805 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001806#else
1807 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001808 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001809#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001810 FD_CLOSEONEXEC(pair[0]);
1811 FD_CLOSEONEXEC(pair[1]);
1812}
1813
1814#define MM_MEMSIZE 65536
1815
1816struct monitor *
1817monitor_init(void)
1818{
1819 struct monitor *mon;
1820 int pair[2];
1821
Damien Miller07d86be2006-03-26 14:19:21 +11001822 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001823
1824 monitor_socketpair(pair);
1825
1826 mon->m_recvfd = pair[0];
1827 mon->m_sendfd = pair[1];
1828
1829 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001830 if (options.compression) {
1831 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1832 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001833
Damien Miller2d6b8332002-06-21 15:59:49 +10001834 /* Compression needs to share state across borders */
1835 mm_init_compression(mon->m_zlib);
1836 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001837
1838 return mon;
1839}
1840
1841void
1842monitor_reinit(struct monitor *mon)
1843{
1844 int pair[2];
1845
1846 monitor_socketpair(pair);
1847
1848 mon->m_recvfd = pair[0];
1849 mon->m_sendfd = pair[1];
1850}
Darren Tucker0efd1552003-08-26 11:49:55 +10001851
1852#ifdef GSSAPI
1853int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001854mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001855{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001856 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001857 OM_uint32 major;
1858 u_int len;
1859
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001860 goid.elements = buffer_get_string(m, &len);
1861 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001862
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001863 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001864
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001865 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001866
1867 buffer_clear(m);
1868 buffer_put_int(m, major);
1869
Damien Miller6fd6def2005-11-05 15:07:05 +11001870 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001871
1872 /* Now we have a context, enable the step */
1873 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1874
1875 return (0);
1876}
1877
1878int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001879mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001880{
1881 gss_buffer_desc in;
1882 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001883 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001884 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001885 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001886
Darren Tucker600ad8d2003-08-26 12:10:48 +10001887 in.value = buffer_get_string(m, &len);
1888 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001889 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1890 xfree(in.value);
1891
1892 buffer_clear(m);
1893 buffer_put_int(m, major);
1894 buffer_put_string(m, out.value, out.length);
1895 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001896 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001897
1898 gss_release_buffer(&minor, &out);
1899
Damien Miller6fd6def2005-11-05 15:07:05 +11001900 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001901 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1902 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001903 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001904 }
1905 return (0);
1906}
1907
1908int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001909mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001910{
1911 gss_buffer_desc gssbuf, mic;
1912 OM_uint32 ret;
1913 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001914
Damien Miller0425d402003-11-17 22:18:21 +11001915 gssbuf.value = buffer_get_string(m, &len);
1916 gssbuf.length = len;
1917 mic.value = buffer_get_string(m, &len);
1918 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001919
Damien Miller0425d402003-11-17 22:18:21 +11001920 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001921
Damien Miller0425d402003-11-17 22:18:21 +11001922 xfree(gssbuf.value);
1923 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001924
Damien Miller0425d402003-11-17 22:18:21 +11001925 buffer_clear(m);
1926 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001927
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001928 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001929
Damien Miller0425d402003-11-17 22:18:21 +11001930 if (!GSS_ERROR(ret))
1931 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001932
Damien Miller0425d402003-11-17 22:18:21 +11001933 return (0);
1934}
1935
1936int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001937mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001938{
1939 int authenticated;
1940
1941 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1942
1943 buffer_clear(m);
1944 buffer_put_int(m, authenticated);
1945
1946 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001947 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001948
Damien Miller6fd6def2005-11-05 15:07:05 +11001949 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001950
1951 /* Monitor loop will terminate if authenticated */
1952 return (authenticated);
1953}
1954#endif /* GSSAPI */